svn merge -r 218679:218997 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cp / parser.c
blob236c2eba01439734482f9f9130c752cb7db67643
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"
58 #include "gomp-constants.h"
61 /* The lexer. */
63 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
64 and c-lex.c) and the C++ parser. */
66 static cp_token eof_token =
68 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
71 /* The various kinds of non integral constant we encounter. */
72 typedef enum non_integral_constant {
73 NIC_NONE,
74 /* floating-point literal */
75 NIC_FLOAT,
76 /* %<this%> */
77 NIC_THIS,
78 /* %<__FUNCTION__%> */
79 NIC_FUNC_NAME,
80 /* %<__PRETTY_FUNCTION__%> */
81 NIC_PRETTY_FUNC,
82 /* %<__func__%> */
83 NIC_C99_FUNC,
84 /* "%<va_arg%> */
85 NIC_VA_ARG,
86 /* a cast */
87 NIC_CAST,
88 /* %<typeid%> operator */
89 NIC_TYPEID,
90 /* non-constant compound literals */
91 NIC_NCC,
92 /* a function call */
93 NIC_FUNC_CALL,
94 /* an increment */
95 NIC_INC,
96 /* an decrement */
97 NIC_DEC,
98 /* an array reference */
99 NIC_ARRAY_REF,
100 /* %<->%> */
101 NIC_ARROW,
102 /* %<.%> */
103 NIC_POINT,
104 /* the address of a label */
105 NIC_ADDR_LABEL,
106 /* %<*%> */
107 NIC_STAR,
108 /* %<&%> */
109 NIC_ADDR,
110 /* %<++%> */
111 NIC_PREINCREMENT,
112 /* %<--%> */
113 NIC_PREDECREMENT,
114 /* %<new%> */
115 NIC_NEW,
116 /* %<delete%> */
117 NIC_DEL,
118 /* calls to overloaded operators */
119 NIC_OVERLOADED,
120 /* an assignment */
121 NIC_ASSIGNMENT,
122 /* a comma operator */
123 NIC_COMMA,
124 /* a call to a constructor */
125 NIC_CONSTRUCTOR,
126 /* a transaction expression */
127 NIC_TRANSACTION
128 } non_integral_constant;
130 /* The various kinds of errors about name-lookup failing. */
131 typedef enum name_lookup_error {
132 /* NULL */
133 NLE_NULL,
134 /* is not a type */
135 NLE_TYPE,
136 /* is not a class or namespace */
137 NLE_CXX98,
138 /* is not a class, namespace, or enumeration */
139 NLE_NOT_CXX98
140 } name_lookup_error;
142 /* The various kinds of required token */
143 typedef enum required_token {
144 RT_NONE,
145 RT_SEMICOLON, /* ';' */
146 RT_OPEN_PAREN, /* '(' */
147 RT_CLOSE_BRACE, /* '}' */
148 RT_OPEN_BRACE, /* '{' */
149 RT_CLOSE_SQUARE, /* ']' */
150 RT_OPEN_SQUARE, /* '[' */
151 RT_COMMA, /* ',' */
152 RT_SCOPE, /* '::' */
153 RT_LESS, /* '<' */
154 RT_GREATER, /* '>' */
155 RT_EQ, /* '=' */
156 RT_ELLIPSIS, /* '...' */
157 RT_MULT, /* '*' */
158 RT_COMPL, /* '~' */
159 RT_COLON, /* ':' */
160 RT_COLON_SCOPE, /* ':' or '::' */
161 RT_CLOSE_PAREN, /* ')' */
162 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
163 RT_PRAGMA_EOL, /* end of line */
164 RT_NAME, /* identifier */
166 /* The type is CPP_KEYWORD */
167 RT_NEW, /* new */
168 RT_DELETE, /* delete */
169 RT_RETURN, /* return */
170 RT_WHILE, /* while */
171 RT_EXTERN, /* extern */
172 RT_STATIC_ASSERT, /* static_assert */
173 RT_DECLTYPE, /* decltype */
174 RT_OPERATOR, /* operator */
175 RT_CLASS, /* class */
176 RT_TEMPLATE, /* template */
177 RT_NAMESPACE, /* namespace */
178 RT_USING, /* using */
179 RT_ASM, /* asm */
180 RT_TRY, /* try */
181 RT_CATCH, /* catch */
182 RT_THROW, /* throw */
183 RT_LABEL, /* __label__ */
184 RT_AT_TRY, /* @try */
185 RT_AT_SYNCHRONIZED, /* @synchronized */
186 RT_AT_THROW, /* @throw */
188 RT_SELECT, /* selection-statement */
189 RT_INTERATION, /* iteration-statement */
190 RT_JUMP, /* jump-statement */
191 RT_CLASS_KEY, /* class-key */
192 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
193 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
194 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
195 RT_TRANSACTION_CANCEL /* __transaction_cancel */
196 } required_token;
198 /* Prototypes. */
200 static cp_lexer *cp_lexer_new_main
201 (void);
202 static cp_lexer *cp_lexer_new_from_tokens
203 (cp_token_cache *tokens);
204 static void cp_lexer_destroy
205 (cp_lexer *);
206 static int cp_lexer_saving_tokens
207 (const cp_lexer *);
208 static cp_token *cp_lexer_token_at
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer *, cp_token *);
212 static inline cp_token *cp_lexer_peek_token
213 (cp_lexer *);
214 static cp_token *cp_lexer_peek_nth_token
215 (cp_lexer *, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer *, enum rid);
222 static cp_token *cp_lexer_consume_token
223 (cp_lexer *);
224 static void cp_lexer_purge_token
225 (cp_lexer *);
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer *, cp_token_position);
228 static void cp_lexer_save_tokens
229 (cp_lexer *);
230 static void cp_lexer_commit_tokens
231 (cp_lexer *);
232 static void cp_lexer_rollback_tokens
233 (cp_lexer *);
234 static void cp_lexer_print_token
235 (FILE *, cp_token *);
236 static inline bool cp_lexer_debugging_p
237 (cp_lexer *);
238 static void cp_lexer_start_debugging
239 (cp_lexer *) ATTRIBUTE_UNUSED;
240 static void cp_lexer_stop_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static cp_token_cache *cp_token_cache_new
244 (cp_token *, cp_token *);
246 static void cp_parser_initial_pragma
247 (cp_token *);
249 static tree cp_literal_operator_id
250 (const char *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
261 /* Manifest constants. */
262 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
263 #define CP_SAVED_TOKEN_STACK 5
265 /* Variables. */
267 /* The stream to which debugging output should be written. */
268 static FILE *cp_lexer_debug_stream;
270 /* Nonzero if we are parsing an unevaluated operand: an operand to
271 sizeof, typeof, or alignof. */
272 int cp_unevaluated_operand;
274 /* Dump up to NUM tokens in BUFFER to FILE starting with token
275 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
276 first token in BUFFER. If NUM is 0, dump all the tokens. If
277 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
278 highlighted by surrounding it in [[ ]]. */
280 static void
281 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
282 cp_token *start_token, unsigned num,
283 cp_token *curr_token)
285 unsigned i, nprinted;
286 cp_token *token;
287 bool do_print;
289 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291 if (buffer == NULL)
292 return;
294 if (num == 0)
295 num = buffer->length ();
297 if (start_token == NULL)
298 start_token = buffer->address ();
300 if (start_token > buffer->address ())
302 cp_lexer_print_token (file, &(*buffer)[0]);
303 fprintf (file, " ... ");
306 do_print = false;
307 nprinted = 0;
308 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310 if (token == start_token)
311 do_print = true;
313 if (!do_print)
314 continue;
316 nprinted++;
317 if (token == curr_token)
318 fprintf (file, "[[");
320 cp_lexer_print_token (file, token);
322 if (token == curr_token)
323 fprintf (file, "]]");
325 switch (token->type)
327 case CPP_SEMICOLON:
328 case CPP_OPEN_BRACE:
329 case CPP_CLOSE_BRACE:
330 case CPP_EOF:
331 fputc ('\n', file);
332 break;
334 default:
335 fputc (' ', file);
339 if (i == num && i < buffer->length ())
341 fprintf (file, " ... ");
342 cp_lexer_print_token (file, &buffer->last ());
345 fprintf (file, "\n");
349 /* Dump all tokens in BUFFER to stderr. */
351 void
352 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 DEBUG_FUNCTION void
358 debug (vec<cp_token, va_gc> &ref)
360 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 DEBUG_FUNCTION void
364 debug (vec<cp_token, va_gc> *ptr)
366 if (ptr)
367 debug (*ptr);
368 else
369 fprintf (stderr, "<nil>\n");
373 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
374 description for T. */
376 static void
377 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 if (t)
381 fprintf (file, "%s: ", desc);
382 print_node_brief (file, "", t, 0);
387 /* Dump parser context C to FILE. */
389 static void
390 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
393 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
394 print_node_brief (file, "", c->object_type, 0);
395 fprintf (file, "}\n");
399 /* Print the stack of parsing contexts to FILE starting with FIRST. */
401 static void
402 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 unsigned i;
405 cp_parser_context *c;
407 fprintf (file, "Parsing context stack:\n");
408 for (i = 0, c = first; c; c = c->next, i++)
410 fprintf (file, "\t#%u: ", i);
411 cp_debug_print_context (file, c);
416 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
418 static void
419 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 if (flag)
422 fprintf (file, "%s: true\n", desc);
426 /* Print an unparsed function entry UF to FILE. */
428 static void
429 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 unsigned i;
432 cp_default_arg_entry *default_arg_fn;
433 tree fn;
435 fprintf (file, "\tFunctions with default args:\n");
436 for (i = 0;
437 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
438 i++)
440 fprintf (file, "\t\tClass type: ");
441 print_node_brief (file, "", default_arg_fn->class_type, 0);
442 fprintf (file, "\t\tDeclaration: ");
443 print_node_brief (file, "", default_arg_fn->decl, 0);
444 fprintf (file, "\n");
447 fprintf (file, "\n\tFunctions with definitions that require "
448 "post-processing\n\t\t");
449 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451 print_node_brief (file, "", fn, 0);
452 fprintf (file, " ");
454 fprintf (file, "\n");
456 fprintf (file, "\n\tNon-static data members with initializers that require "
457 "post-processing\n\t\t");
458 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460 print_node_brief (file, "", fn, 0);
461 fprintf (file, " ");
463 fprintf (file, "\n");
467 /* Print the stack of unparsed member functions S to FILE. */
469 static void
470 cp_debug_print_unparsed_queues (FILE *file,
471 vec<cp_unparsed_functions_entry, va_gc> *s)
473 unsigned i;
474 cp_unparsed_functions_entry *uf;
476 fprintf (file, "Unparsed functions\n");
477 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479 fprintf (file, "#%u:\n", i);
480 cp_debug_print_unparsed_function (file, uf);
485 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
486 the given PARSER. If FILE is NULL, the output is printed on stderr. */
488 static void
489 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 cp_token *next_token, *first_token, *start_token;
493 if (file == NULL)
494 file = stderr;
496 next_token = parser->lexer->next_token;
497 first_token = parser->lexer->buffer->address ();
498 start_token = (next_token > first_token + window_size / 2)
499 ? next_token - window_size / 2
500 : first_token;
501 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
502 next_token);
506 /* Dump debugging information for the given PARSER. If FILE is NULL,
507 the output is printed on stderr. */
509 void
510 cp_debug_parser (FILE *file, cp_parser *parser)
512 const size_t window_size = 20;
513 cp_token *token;
514 expanded_location eloc;
516 if (file == NULL)
517 file = stderr;
519 fprintf (file, "Parser state\n\n");
520 fprintf (file, "Number of tokens: %u\n",
521 vec_safe_length (parser->lexer->buffer));
522 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
523 cp_debug_print_tree_if_set (file, "Object scope",
524 parser->object_scope);
525 cp_debug_print_tree_if_set (file, "Qualifying scope",
526 parser->qualifying_scope);
527 cp_debug_print_context_stack (file, parser->context);
528 cp_debug_print_flag (file, "Allow GNU extensions",
529 parser->allow_gnu_extensions_p);
530 cp_debug_print_flag (file, "'>' token is greater-than",
531 parser->greater_than_is_operator_p);
532 cp_debug_print_flag (file, "Default args allowed in current "
533 "parameter list", parser->default_arg_ok_p);
534 cp_debug_print_flag (file, "Parsing integral constant-expression",
535 parser->integral_constant_expression_p);
536 cp_debug_print_flag (file, "Allow non-constant expression in current "
537 "constant-expression",
538 parser->allow_non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Seen non-constant expression",
540 parser->non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
542 "current context",
543 parser->local_variables_forbidden_p);
544 cp_debug_print_flag (file, "In unbraced linkage specification",
545 parser->in_unbraced_linkage_specification_p);
546 cp_debug_print_flag (file, "Parsing a declarator",
547 parser->in_declarator_p);
548 cp_debug_print_flag (file, "In template argument list",
549 parser->in_template_argument_list_p);
550 cp_debug_print_flag (file, "Parsing an iteration statement",
551 parser->in_statement & IN_ITERATION_STMT);
552 cp_debug_print_flag (file, "Parsing a switch statement",
553 parser->in_statement & IN_SWITCH_STMT);
554 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
555 parser->in_statement & IN_OMP_BLOCK);
556 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
557 parser->in_statement & IN_CILK_SIMD_FOR);
558 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
559 parser->in_statement & IN_OMP_FOR);
560 cp_debug_print_flag (file, "Parsing an if statement",
561 parser->in_statement & IN_IF_STMT);
562 cp_debug_print_flag (file, "Parsing a type-id in an expression "
563 "context", parser->in_type_id_in_expr_p);
564 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
565 parser->implicit_extern_c);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 if (parser->type_definition_forbidden_message)
576 fprintf (file, "Error message for forbidden type definitions: %s\n",
577 parser->type_definition_forbidden_message);
578 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
579 fprintf (file, "Number of class definitions in progress: %u\n",
580 parser->num_classes_being_defined);
581 fprintf (file, "Number of template parameter lists for the current "
582 "declaration: %u\n", parser->num_template_parameter_lists);
583 cp_debug_parser_tokens (file, parser, window_size);
584 token = parser->lexer->next_token;
585 fprintf (file, "Next token to parse:\n");
586 fprintf (file, "\tToken: ");
587 cp_lexer_print_token (file, token);
588 eloc = expand_location (token->location);
589 fprintf (file, "\n\tFile: %s\n", eloc.file);
590 fprintf (file, "\tLine: %d\n", eloc.line);
591 fprintf (file, "\tColumn: %d\n", eloc.column);
594 DEBUG_FUNCTION void
595 debug (cp_parser &ref)
597 cp_debug_parser (stderr, &ref);
600 DEBUG_FUNCTION void
601 debug (cp_parser *ptr)
603 if (ptr)
604 debug (*ptr);
605 else
606 fprintf (stderr, "<nil>\n");
609 /* Allocate memory for a new lexer object and return it. */
611 static cp_lexer *
612 cp_lexer_alloc (void)
614 cp_lexer *lexer;
616 c_common_no_more_pch ();
618 /* Allocate the memory. */
619 lexer = ggc_cleared_alloc<cp_lexer> ();
621 /* Initially we are not debugging. */
622 lexer->debugging_p = false;
624 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
626 /* Create the buffer. */
627 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
629 return lexer;
633 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 preprocessor. */
636 static cp_lexer *
637 cp_lexer_new_main (void)
639 cp_lexer *lexer;
640 cp_token token;
642 /* It's possible that parsing the first pragma will load a PCH file,
643 which is a GC collection point. So we have to do that before
644 allocating any memory. */
645 cp_parser_initial_pragma (&token);
647 lexer = cp_lexer_alloc ();
649 /* Put the first token in the buffer. */
650 lexer->buffer->quick_push (token);
652 /* Get the remaining tokens from the preprocessor. */
653 while (token.type != CPP_EOF)
655 cp_lexer_get_preprocessor_token (lexer, &token);
656 vec_safe_push (lexer->buffer, token);
659 lexer->last_token = lexer->buffer->address ()
660 + lexer->buffer->length ()
661 - 1;
662 lexer->next_token = lexer->buffer->length ()
663 ? lexer->buffer->address ()
664 : &eof_token;
666 /* Subsequent preprocessor diagnostics should use compiler
667 diagnostic functions to get the compiler source location. */
668 done_lexing = true;
670 gcc_assert (!lexer->next_token->purged_p);
671 return lexer;
674 /* Create a new lexer whose token stream is primed with the tokens in
675 CACHE. When these tokens are exhausted, no new tokens will be read. */
677 static cp_lexer *
678 cp_lexer_new_from_tokens (cp_token_cache *cache)
680 cp_token *first = cache->first;
681 cp_token *last = cache->last;
682 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
684 /* We do not own the buffer. */
685 lexer->buffer = NULL;
686 lexer->next_token = first == last ? &eof_token : first;
687 lexer->last_token = last;
689 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
691 /* Initially we are not debugging. */
692 lexer->debugging_p = false;
694 gcc_assert (!lexer->next_token->purged_p);
695 return lexer;
698 /* Frees all resources associated with LEXER. */
700 static void
701 cp_lexer_destroy (cp_lexer *lexer)
703 vec_free (lexer->buffer);
704 lexer->saved_tokens.release ();
705 ggc_free (lexer);
708 /* Returns nonzero if debugging information should be output. */
710 static inline bool
711 cp_lexer_debugging_p (cp_lexer *lexer)
713 return lexer->debugging_p;
717 static inline cp_token_position
718 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
720 gcc_assert (!previous_p || lexer->next_token != &eof_token);
722 return lexer->next_token - previous_p;
725 static inline cp_token *
726 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
728 return pos;
731 static inline void
732 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
734 lexer->next_token = cp_lexer_token_at (lexer, pos);
737 static inline cp_token_position
738 cp_lexer_previous_token_position (cp_lexer *lexer)
740 if (lexer->next_token == &eof_token)
741 return lexer->last_token - 1;
742 else
743 return cp_lexer_token_position (lexer, true);
746 static inline cp_token *
747 cp_lexer_previous_token (cp_lexer *lexer)
749 cp_token_position tp = cp_lexer_previous_token_position (lexer);
751 return cp_lexer_token_at (lexer, tp);
754 /* nonzero if we are presently saving tokens. */
756 static inline int
757 cp_lexer_saving_tokens (const cp_lexer* lexer)
759 return lexer->saved_tokens.length () != 0;
762 /* Store the next token from the preprocessor in *TOKEN. Return true
763 if we reach EOF. If LEXER is NULL, assume we are handling an
764 initial #pragma pch_preprocess, and thus want the lexer to return
765 processed strings. */
767 static void
768 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
770 static int is_extern_c = 0;
772 /* Get a new token from the preprocessor. */
773 token->type
774 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
775 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
776 token->keyword = RID_MAX;
777 token->pragma_kind = PRAGMA_NONE;
778 token->purged_p = false;
779 token->error_reported = false;
781 /* On some systems, some header files are surrounded by an
782 implicit extern "C" block. Set a flag in the token if it
783 comes from such a header. */
784 is_extern_c += pending_lang_change;
785 pending_lang_change = 0;
786 token->implicit_extern_c = is_extern_c > 0;
788 /* Check to see if this token is a keyword. */
789 if (token->type == CPP_NAME)
791 if (C_IS_RESERVED_WORD (token->u.value))
793 /* Mark this token as a keyword. */
794 token->type = CPP_KEYWORD;
795 /* Record which keyword. */
796 token->keyword = C_RID_CODE (token->u.value);
798 else
800 if (warn_cxx0x_compat
801 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
802 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
804 /* Warn about the C++0x keyword (but still treat it as
805 an identifier). */
806 warning (OPT_Wc__0x_compat,
807 "identifier %qE is a keyword in C++11",
808 token->u.value);
810 /* Clear out the C_RID_CODE so we don't warn about this
811 particular identifier-turned-keyword again. */
812 C_SET_RID_CODE (token->u.value, RID_MAX);
815 token->keyword = RID_MAX;
818 else if (token->type == CPP_AT_NAME)
820 /* This only happens in Objective-C++; it must be a keyword. */
821 token->type = CPP_KEYWORD;
822 switch (C_RID_CODE (token->u.value))
824 /* Replace 'class' with '@class', 'private' with '@private',
825 etc. This prevents confusion with the C++ keyword
826 'class', and makes the tokens consistent with other
827 Objective-C 'AT' keywords. For example '@class' is
828 reported as RID_AT_CLASS which is consistent with
829 '@synchronized', which is reported as
830 RID_AT_SYNCHRONIZED.
832 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
833 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
834 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
835 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
836 case RID_THROW: token->keyword = RID_AT_THROW; break;
837 case RID_TRY: token->keyword = RID_AT_TRY; break;
838 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
839 default: token->keyword = C_RID_CODE (token->u.value);
842 else if (token->type == CPP_PRAGMA)
844 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
845 token->pragma_kind = ((enum pragma_kind)
846 TREE_INT_CST_LOW (token->u.value));
847 token->u.value = NULL_TREE;
851 /* Update the globals input_location and the input file stack from TOKEN. */
852 static inline void
853 cp_lexer_set_source_position_from_token (cp_token *token)
855 if (token->type != CPP_EOF)
857 input_location = token->location;
861 /* Update the globals input_location and the input file stack from LEXER. */
862 static inline void
863 cp_lexer_set_source_position (cp_lexer *lexer)
865 cp_token *token = cp_lexer_peek_token (lexer);
866 cp_lexer_set_source_position_from_token (token);
869 /* Return a pointer to the next token in the token stream, but do not
870 consume it. */
872 static inline cp_token *
873 cp_lexer_peek_token (cp_lexer *lexer)
875 if (cp_lexer_debugging_p (lexer))
877 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
878 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
879 putc ('\n', cp_lexer_debug_stream);
881 return lexer->next_token;
884 /* Return true if the next token has the indicated TYPE. */
886 static inline bool
887 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
889 return cp_lexer_peek_token (lexer)->type == type;
892 /* Return true if the next token does not have the indicated TYPE. */
894 static inline bool
895 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
897 return !cp_lexer_next_token_is (lexer, type);
900 /* Return true if the next token is the indicated KEYWORD. */
902 static inline bool
903 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
905 return cp_lexer_peek_token (lexer)->keyword == keyword;
908 static inline bool
909 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
911 return cp_lexer_peek_nth_token (lexer, n)->type == type;
914 static inline bool
915 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
917 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
920 /* Return true if the next token is not the indicated KEYWORD. */
922 static inline bool
923 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
925 return cp_lexer_peek_token (lexer)->keyword != keyword;
928 /* Return true if the next token is a keyword for a decl-specifier. */
930 static bool
931 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
933 cp_token *token;
935 token = cp_lexer_peek_token (lexer);
936 switch (token->keyword)
938 /* auto specifier: storage-class-specifier in C++,
939 simple-type-specifier in C++0x. */
940 case RID_AUTO:
941 /* Storage classes. */
942 case RID_REGISTER:
943 case RID_STATIC:
944 case RID_EXTERN:
945 case RID_MUTABLE:
946 case RID_THREAD:
947 /* Elaborated type specifiers. */
948 case RID_ENUM:
949 case RID_CLASS:
950 case RID_STRUCT:
951 case RID_UNION:
952 case RID_TYPENAME:
953 /* Simple type specifiers. */
954 case RID_CHAR:
955 case RID_CHAR16:
956 case RID_CHAR32:
957 case RID_WCHAR:
958 case RID_BOOL:
959 case RID_SHORT:
960 case RID_INT:
961 case RID_LONG:
962 case RID_SIGNED:
963 case RID_UNSIGNED:
964 case RID_FLOAT:
965 case RID_DOUBLE:
966 case RID_VOID:
967 /* GNU extensions. */
968 case RID_ATTRIBUTE:
969 case RID_TYPEOF:
970 /* C++0x extensions. */
971 case RID_DECLTYPE:
972 case RID_UNDERLYING_TYPE:
973 return true;
975 default:
976 if (token->keyword >= RID_FIRST_INT_N
977 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
978 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
979 return true;
980 return false;
984 /* Returns TRUE iff the token T begins a decltype type. */
986 static bool
987 token_is_decltype (cp_token *t)
989 return (t->keyword == RID_DECLTYPE
990 || t->type == CPP_DECLTYPE);
993 /* Returns TRUE iff the next token begins a decltype type. */
995 static bool
996 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
998 cp_token *t = cp_lexer_peek_token (lexer);
999 return token_is_decltype (t);
1002 /* Return a pointer to the Nth token in the token stream. If N is 1,
1003 then this is precisely equivalent to cp_lexer_peek_token (except
1004 that it is not inline). One would like to disallow that case, but
1005 there is one case (cp_parser_nth_token_starts_template_id) where
1006 the caller passes a variable for N and it might be 1. */
1008 static cp_token *
1009 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1011 cp_token *token;
1013 /* N is 1-based, not zero-based. */
1014 gcc_assert (n > 0);
1016 if (cp_lexer_debugging_p (lexer))
1017 fprintf (cp_lexer_debug_stream,
1018 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1020 --n;
1021 token = lexer->next_token;
1022 gcc_assert (!n || token != &eof_token);
1023 while (n != 0)
1025 ++token;
1026 if (token == lexer->last_token)
1028 token = &eof_token;
1029 break;
1032 if (!token->purged_p)
1033 --n;
1036 if (cp_lexer_debugging_p (lexer))
1038 cp_lexer_print_token (cp_lexer_debug_stream, token);
1039 putc ('\n', cp_lexer_debug_stream);
1042 return token;
1045 /* Return the next token, and advance the lexer's next_token pointer
1046 to point to the next non-purged token. */
1048 static cp_token *
1049 cp_lexer_consume_token (cp_lexer* lexer)
1051 cp_token *token = lexer->next_token;
1053 gcc_assert (token != &eof_token);
1054 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1058 lexer->next_token++;
1059 if (lexer->next_token == lexer->last_token)
1061 lexer->next_token = &eof_token;
1062 break;
1066 while (lexer->next_token->purged_p);
1068 cp_lexer_set_source_position_from_token (token);
1070 /* Provide debugging output. */
1071 if (cp_lexer_debugging_p (lexer))
1073 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Permanently remove the next token from the token stream, and
1082 advance the next_token pointer to refer to the next non-purged
1083 token. */
1085 static void
1086 cp_lexer_purge_token (cp_lexer *lexer)
1088 cp_token *tok = lexer->next_token;
1090 gcc_assert (tok != &eof_token);
1091 tok->purged_p = true;
1092 tok->location = UNKNOWN_LOCATION;
1093 tok->u.value = NULL_TREE;
1094 tok->keyword = RID_MAX;
1098 tok++;
1099 if (tok == lexer->last_token)
1101 tok = &eof_token;
1102 break;
1105 while (tok->purged_p);
1106 lexer->next_token = tok;
1109 /* Permanently remove all tokens after TOK, up to, but not
1110 including, the token that will be returned next by
1111 cp_lexer_peek_token. */
1113 static void
1114 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1116 cp_token *peek = lexer->next_token;
1118 if (peek == &eof_token)
1119 peek = lexer->last_token;
1121 gcc_assert (tok < peek);
1123 for ( tok += 1; tok != peek; tok += 1)
1125 tok->purged_p = true;
1126 tok->location = UNKNOWN_LOCATION;
1127 tok->u.value = NULL_TREE;
1128 tok->keyword = RID_MAX;
1132 /* Begin saving tokens. All tokens consumed after this point will be
1133 preserved. */
1135 static void
1136 cp_lexer_save_tokens (cp_lexer* lexer)
1138 /* Provide debugging output. */
1139 if (cp_lexer_debugging_p (lexer))
1140 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1142 lexer->saved_tokens.safe_push (lexer->next_token);
1145 /* Commit to the portion of the token stream most recently saved. */
1147 static void
1148 cp_lexer_commit_tokens (cp_lexer* lexer)
1150 /* Provide debugging output. */
1151 if (cp_lexer_debugging_p (lexer))
1152 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1154 lexer->saved_tokens.pop ();
1157 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1158 to the token stream. Stop saving tokens. */
1160 static void
1161 cp_lexer_rollback_tokens (cp_lexer* lexer)
1163 /* Provide debugging output. */
1164 if (cp_lexer_debugging_p (lexer))
1165 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1167 lexer->next_token = lexer->saved_tokens.pop ();
1170 /* RAII wrapper around the above functions, with sanity checking. Creating
1171 a variable saves tokens, which are committed when the variable is
1172 destroyed unless they are explicitly rolled back by calling the rollback
1173 member function. */
1175 struct saved_token_sentinel
1177 cp_lexer *lexer;
1178 unsigned len;
1179 bool commit;
1180 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1182 len = lexer->saved_tokens.length ();
1183 cp_lexer_save_tokens (lexer);
1185 void rollback ()
1187 cp_lexer_rollback_tokens (lexer);
1188 commit = false;
1190 ~saved_token_sentinel()
1192 if (commit)
1193 cp_lexer_commit_tokens (lexer);
1194 gcc_assert (lexer->saved_tokens.length () == len);
1198 /* Print a representation of the TOKEN on the STREAM. */
1200 static void
1201 cp_lexer_print_token (FILE * stream, cp_token *token)
1203 /* We don't use cpp_type2name here because the parser defines
1204 a few tokens of its own. */
1205 static const char *const token_names[] = {
1206 /* cpplib-defined token types */
1207 #define OP(e, s) #e,
1208 #define TK(e, s) #e,
1209 TTYPE_TABLE
1210 #undef OP
1211 #undef TK
1212 /* C++ parser token types - see "Manifest constants", above. */
1213 "KEYWORD",
1214 "TEMPLATE_ID",
1215 "NESTED_NAME_SPECIFIER",
1218 /* For some tokens, print the associated data. */
1219 switch (token->type)
1221 case CPP_KEYWORD:
1222 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1223 For example, `struct' is mapped to an INTEGER_CST. */
1224 if (!identifier_p (token->u.value))
1225 break;
1226 /* else fall through */
1227 case CPP_NAME:
1228 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1229 break;
1231 case CPP_STRING:
1232 case CPP_STRING16:
1233 case CPP_STRING32:
1234 case CPP_WSTRING:
1235 case CPP_UTF8STRING:
1236 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1237 break;
1239 case CPP_NUMBER:
1240 print_generic_expr (stream, token->u.value, 0);
1241 break;
1243 default:
1244 /* If we have a name for the token, print it out. Otherwise, we
1245 simply give the numeric code. */
1246 if (token->type < ARRAY_SIZE(token_names))
1247 fputs (token_names[token->type], stream);
1248 else
1249 fprintf (stream, "[%d]", token->type);
1250 break;
1254 DEBUG_FUNCTION void
1255 debug (cp_token &ref)
1257 cp_lexer_print_token (stderr, &ref);
1258 fprintf (stderr, "\n");
1261 DEBUG_FUNCTION void
1262 debug (cp_token *ptr)
1264 if (ptr)
1265 debug (*ptr);
1266 else
1267 fprintf (stderr, "<nil>\n");
1271 /* Start emitting debugging information. */
1273 static void
1274 cp_lexer_start_debugging (cp_lexer* lexer)
1276 lexer->debugging_p = true;
1277 cp_lexer_debug_stream = stderr;
1280 /* Stop emitting debugging information. */
1282 static void
1283 cp_lexer_stop_debugging (cp_lexer* lexer)
1285 lexer->debugging_p = false;
1286 cp_lexer_debug_stream = NULL;
1289 /* Create a new cp_token_cache, representing a range of tokens. */
1291 static cp_token_cache *
1292 cp_token_cache_new (cp_token *first, cp_token *last)
1294 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1295 cache->first = first;
1296 cache->last = last;
1297 return cache;
1300 /* Diagnose if #pragma omp declare simd isn't followed immediately
1301 by function declaration or definition. */
1303 static inline void
1304 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1306 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1308 error ("%<#pragma omp declare simd%> not immediately followed by "
1309 "function declaration or definition");
1310 parser->omp_declare_simd = NULL;
1314 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1315 and put that into "omp declare simd" attribute. */
1317 static inline void
1318 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1320 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1322 if (fndecl == error_mark_node)
1324 parser->omp_declare_simd = NULL;
1325 return;
1327 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1329 cp_ensure_no_omp_declare_simd (parser);
1330 return;
1335 /* Decl-specifiers. */
1337 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1339 static void
1340 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1342 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1345 /* Declarators. */
1347 /* Nothing other than the parser should be creating declarators;
1348 declarators are a semi-syntactic representation of C++ entities.
1349 Other parts of the front end that need to create entities (like
1350 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1352 static cp_declarator *make_call_declarator
1353 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1354 static cp_declarator *make_array_declarator
1355 (cp_declarator *, tree);
1356 static cp_declarator *make_pointer_declarator
1357 (cp_cv_quals, cp_declarator *, tree);
1358 static cp_declarator *make_reference_declarator
1359 (cp_cv_quals, cp_declarator *, bool, tree);
1360 static cp_parameter_declarator *make_parameter_declarator
1361 (cp_decl_specifier_seq *, cp_declarator *, tree);
1362 static cp_declarator *make_ptrmem_declarator
1363 (cp_cv_quals, tree, cp_declarator *, tree);
1365 /* An erroneous declarator. */
1366 static cp_declarator *cp_error_declarator;
1368 /* The obstack on which declarators and related data structures are
1369 allocated. */
1370 static struct obstack declarator_obstack;
1372 /* Alloc BYTES from the declarator memory pool. */
1374 static inline void *
1375 alloc_declarator (size_t bytes)
1377 return obstack_alloc (&declarator_obstack, bytes);
1380 /* Allocate a declarator of the indicated KIND. Clear fields that are
1381 common to all declarators. */
1383 static cp_declarator *
1384 make_declarator (cp_declarator_kind kind)
1386 cp_declarator *declarator;
1388 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1389 declarator->kind = kind;
1390 declarator->attributes = NULL_TREE;
1391 declarator->std_attributes = NULL_TREE;
1392 declarator->declarator = NULL;
1393 declarator->parameter_pack_p = false;
1394 declarator->id_loc = UNKNOWN_LOCATION;
1396 return declarator;
1399 /* Make a declarator for a generalized identifier. If
1400 QUALIFYING_SCOPE is non-NULL, the identifier is
1401 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1402 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1403 is, if any. */
1405 static cp_declarator *
1406 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1407 special_function_kind sfk)
1409 cp_declarator *declarator;
1411 /* It is valid to write:
1413 class C { void f(); };
1414 typedef C D;
1415 void D::f();
1417 The standard is not clear about whether `typedef const C D' is
1418 legal; as of 2002-09-15 the committee is considering that
1419 question. EDG 3.0 allows that syntax. Therefore, we do as
1420 well. */
1421 if (qualifying_scope && TYPE_P (qualifying_scope))
1422 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1424 gcc_assert (identifier_p (unqualified_name)
1425 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1426 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1428 declarator = make_declarator (cdk_id);
1429 declarator->u.id.qualifying_scope = qualifying_scope;
1430 declarator->u.id.unqualified_name = unqualified_name;
1431 declarator->u.id.sfk = sfk;
1433 return declarator;
1436 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1437 of modifiers such as const or volatile to apply to the pointer
1438 type, represented as identifiers. ATTRIBUTES represent the attributes that
1439 appertain to the pointer or reference. */
1441 cp_declarator *
1442 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1443 tree attributes)
1445 cp_declarator *declarator;
1447 declarator = make_declarator (cdk_pointer);
1448 declarator->declarator = target;
1449 declarator->u.pointer.qualifiers = cv_qualifiers;
1450 declarator->u.pointer.class_type = NULL_TREE;
1451 if (target)
1453 declarator->id_loc = target->id_loc;
1454 declarator->parameter_pack_p = target->parameter_pack_p;
1455 target->parameter_pack_p = false;
1457 else
1458 declarator->parameter_pack_p = false;
1460 declarator->std_attributes = attributes;
1462 return declarator;
1465 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1466 represent the attributes that appertain to the pointer or
1467 reference. */
1469 cp_declarator *
1470 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1471 bool rvalue_ref, tree attributes)
1473 cp_declarator *declarator;
1475 declarator = make_declarator (cdk_reference);
1476 declarator->declarator = target;
1477 declarator->u.reference.qualifiers = cv_qualifiers;
1478 declarator->u.reference.rvalue_ref = rvalue_ref;
1479 if (target)
1481 declarator->id_loc = target->id_loc;
1482 declarator->parameter_pack_p = target->parameter_pack_p;
1483 target->parameter_pack_p = false;
1485 else
1486 declarator->parameter_pack_p = false;
1488 declarator->std_attributes = attributes;
1490 return declarator;
1493 /* Like make_pointer_declarator -- but for a pointer to a non-static
1494 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1497 cp_declarator *
1498 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1499 cp_declarator *pointee,
1500 tree attributes)
1502 cp_declarator *declarator;
1504 declarator = make_declarator (cdk_ptrmem);
1505 declarator->declarator = pointee;
1506 declarator->u.pointer.qualifiers = cv_qualifiers;
1507 declarator->u.pointer.class_type = class_type;
1509 if (pointee)
1511 declarator->parameter_pack_p = pointee->parameter_pack_p;
1512 pointee->parameter_pack_p = false;
1514 else
1515 declarator->parameter_pack_p = false;
1517 declarator->std_attributes = attributes;
1519 return declarator;
1522 /* Make a declarator for the function given by TARGET, with the
1523 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1524 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1525 indicates what exceptions can be thrown. */
1527 cp_declarator *
1528 make_call_declarator (cp_declarator *target,
1529 tree parms,
1530 cp_cv_quals cv_qualifiers,
1531 cp_virt_specifiers virt_specifiers,
1532 cp_ref_qualifier ref_qualifier,
1533 tree exception_specification,
1534 tree late_return_type)
1536 cp_declarator *declarator;
1538 declarator = make_declarator (cdk_function);
1539 declarator->declarator = target;
1540 declarator->u.function.parameters = parms;
1541 declarator->u.function.qualifiers = cv_qualifiers;
1542 declarator->u.function.virt_specifiers = virt_specifiers;
1543 declarator->u.function.ref_qualifier = ref_qualifier;
1544 declarator->u.function.exception_specification = exception_specification;
1545 declarator->u.function.late_return_type = late_return_type;
1546 if (target)
1548 declarator->id_loc = target->id_loc;
1549 declarator->parameter_pack_p = target->parameter_pack_p;
1550 target->parameter_pack_p = false;
1552 else
1553 declarator->parameter_pack_p = false;
1555 return declarator;
1558 /* Make a declarator for an array of BOUNDS elements, each of which is
1559 defined by ELEMENT. */
1561 cp_declarator *
1562 make_array_declarator (cp_declarator *element, tree bounds)
1564 cp_declarator *declarator;
1566 declarator = make_declarator (cdk_array);
1567 declarator->declarator = element;
1568 declarator->u.array.bounds = bounds;
1569 if (element)
1571 declarator->id_loc = element->id_loc;
1572 declarator->parameter_pack_p = element->parameter_pack_p;
1573 element->parameter_pack_p = false;
1575 else
1576 declarator->parameter_pack_p = false;
1578 return declarator;
1581 /* Determine whether the declarator we've seen so far can be a
1582 parameter pack, when followed by an ellipsis. */
1583 static bool
1584 declarator_can_be_parameter_pack (cp_declarator *declarator)
1586 /* Search for a declarator name, or any other declarator that goes
1587 after the point where the ellipsis could appear in a parameter
1588 pack. If we find any of these, then this declarator can not be
1589 made into a parameter pack. */
1590 bool found = false;
1591 while (declarator && !found)
1593 switch ((int)declarator->kind)
1595 case cdk_id:
1596 case cdk_array:
1597 found = true;
1598 break;
1600 case cdk_error:
1601 return true;
1603 default:
1604 declarator = declarator->declarator;
1605 break;
1609 return !found;
1612 cp_parameter_declarator *no_parameters;
1614 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1615 DECLARATOR and DEFAULT_ARGUMENT. */
1617 cp_parameter_declarator *
1618 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1619 cp_declarator *declarator,
1620 tree default_argument)
1622 cp_parameter_declarator *parameter;
1624 parameter = ((cp_parameter_declarator *)
1625 alloc_declarator (sizeof (cp_parameter_declarator)));
1626 parameter->next = NULL;
1627 if (decl_specifiers)
1628 parameter->decl_specifiers = *decl_specifiers;
1629 else
1630 clear_decl_specs (&parameter->decl_specifiers);
1631 parameter->declarator = declarator;
1632 parameter->default_argument = default_argument;
1633 parameter->ellipsis_p = false;
1635 return parameter;
1638 /* Returns true iff DECLARATOR is a declaration for a function. */
1640 static bool
1641 function_declarator_p (const cp_declarator *declarator)
1643 while (declarator)
1645 if (declarator->kind == cdk_function
1646 && declarator->declarator->kind == cdk_id)
1647 return true;
1648 if (declarator->kind == cdk_id
1649 || declarator->kind == cdk_error)
1650 return false;
1651 declarator = declarator->declarator;
1653 return false;
1656 /* The parser. */
1658 /* Overview
1659 --------
1661 A cp_parser parses the token stream as specified by the C++
1662 grammar. Its job is purely parsing, not semantic analysis. For
1663 example, the parser breaks the token stream into declarators,
1664 expressions, statements, and other similar syntactic constructs.
1665 It does not check that the types of the expressions on either side
1666 of an assignment-statement are compatible, or that a function is
1667 not declared with a parameter of type `void'.
1669 The parser invokes routines elsewhere in the compiler to perform
1670 semantic analysis and to build up the abstract syntax tree for the
1671 code processed.
1673 The parser (and the template instantiation code, which is, in a
1674 way, a close relative of parsing) are the only parts of the
1675 compiler that should be calling push_scope and pop_scope, or
1676 related functions. The parser (and template instantiation code)
1677 keeps track of what scope is presently active; everything else
1678 should simply honor that. (The code that generates static
1679 initializers may also need to set the scope, in order to check
1680 access control correctly when emitting the initializers.)
1682 Methodology
1683 -----------
1685 The parser is of the standard recursive-descent variety. Upcoming
1686 tokens in the token stream are examined in order to determine which
1687 production to use when parsing a non-terminal. Some C++ constructs
1688 require arbitrary look ahead to disambiguate. For example, it is
1689 impossible, in the general case, to tell whether a statement is an
1690 expression or declaration without scanning the entire statement.
1691 Therefore, the parser is capable of "parsing tentatively." When the
1692 parser is not sure what construct comes next, it enters this mode.
1693 Then, while we attempt to parse the construct, the parser queues up
1694 error messages, rather than issuing them immediately, and saves the
1695 tokens it consumes. If the construct is parsed successfully, the
1696 parser "commits", i.e., it issues any queued error messages and
1697 the tokens that were being preserved are permanently discarded.
1698 If, however, the construct is not parsed successfully, the parser
1699 rolls back its state completely so that it can resume parsing using
1700 a different alternative.
1702 Future Improvements
1703 -------------------
1705 The performance of the parser could probably be improved substantially.
1706 We could often eliminate the need to parse tentatively by looking ahead
1707 a little bit. In some places, this approach might not entirely eliminate
1708 the need to parse tentatively, but it might still speed up the average
1709 case. */
1711 /* Flags that are passed to some parsing functions. These values can
1712 be bitwise-ored together. */
1714 enum
1716 /* No flags. */
1717 CP_PARSER_FLAGS_NONE = 0x0,
1718 /* The construct is optional. If it is not present, then no error
1719 should be issued. */
1720 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1721 /* When parsing a type-specifier, treat user-defined type-names
1722 as non-type identifiers. */
1723 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1724 /* When parsing a type-specifier, do not try to parse a class-specifier
1725 or enum-specifier. */
1726 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1727 /* When parsing a decl-specifier-seq, only allow type-specifier or
1728 constexpr. */
1729 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1732 /* This type is used for parameters and variables which hold
1733 combinations of the above flags. */
1734 typedef int cp_parser_flags;
1736 /* The different kinds of declarators we want to parse. */
1738 typedef enum cp_parser_declarator_kind
1740 /* We want an abstract declarator. */
1741 CP_PARSER_DECLARATOR_ABSTRACT,
1742 /* We want a named declarator. */
1743 CP_PARSER_DECLARATOR_NAMED,
1744 /* We don't mind, but the name must be an unqualified-id. */
1745 CP_PARSER_DECLARATOR_EITHER
1746 } cp_parser_declarator_kind;
1748 /* The precedence values used to parse binary expressions. The minimum value
1749 of PREC must be 1, because zero is reserved to quickly discriminate
1750 binary operators from other tokens. */
1752 enum cp_parser_prec
1754 PREC_NOT_OPERATOR,
1755 PREC_LOGICAL_OR_EXPRESSION,
1756 PREC_LOGICAL_AND_EXPRESSION,
1757 PREC_INCLUSIVE_OR_EXPRESSION,
1758 PREC_EXCLUSIVE_OR_EXPRESSION,
1759 PREC_AND_EXPRESSION,
1760 PREC_EQUALITY_EXPRESSION,
1761 PREC_RELATIONAL_EXPRESSION,
1762 PREC_SHIFT_EXPRESSION,
1763 PREC_ADDITIVE_EXPRESSION,
1764 PREC_MULTIPLICATIVE_EXPRESSION,
1765 PREC_PM_EXPRESSION,
1766 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1769 /* A mapping from a token type to a corresponding tree node type, with a
1770 precedence value. */
1772 typedef struct cp_parser_binary_operations_map_node
1774 /* The token type. */
1775 enum cpp_ttype token_type;
1776 /* The corresponding tree code. */
1777 enum tree_code tree_type;
1778 /* The precedence of this operator. */
1779 enum cp_parser_prec prec;
1780 } cp_parser_binary_operations_map_node;
1782 typedef struct cp_parser_expression_stack_entry
1784 /* Left hand side of the binary operation we are currently
1785 parsing. */
1786 tree lhs;
1787 /* Original tree code for left hand side, if it was a binary
1788 expression itself (used for -Wparentheses). */
1789 enum tree_code lhs_type;
1790 /* Tree code for the binary operation we are parsing. */
1791 enum tree_code tree_type;
1792 /* Precedence of the binary operation we are parsing. */
1793 enum cp_parser_prec prec;
1794 /* Location of the binary operation we are parsing. */
1795 location_t loc;
1796 } cp_parser_expression_stack_entry;
1798 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1799 entries because precedence levels on the stack are monotonically
1800 increasing. */
1801 typedef struct cp_parser_expression_stack_entry
1802 cp_parser_expression_stack[NUM_PREC_VALUES];
1804 /* Prototypes. */
1806 /* Constructors and destructors. */
1808 static cp_parser_context *cp_parser_context_new
1809 (cp_parser_context *);
1811 /* Class variables. */
1813 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1815 /* The operator-precedence table used by cp_parser_binary_expression.
1816 Transformed into an associative array (binops_by_token) by
1817 cp_parser_new. */
1819 static const cp_parser_binary_operations_map_node binops[] = {
1820 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1821 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1823 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1825 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1827 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1828 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1830 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1831 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1833 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1836 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1838 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1839 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1841 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1843 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1845 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1847 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1849 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1852 /* The same as binops, but initialized by cp_parser_new so that
1853 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1854 for speed. */
1855 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1857 /* Constructors and destructors. */
1859 /* Construct a new context. The context below this one on the stack
1860 is given by NEXT. */
1862 static cp_parser_context *
1863 cp_parser_context_new (cp_parser_context* next)
1865 cp_parser_context *context;
1867 /* Allocate the storage. */
1868 if (cp_parser_context_free_list != NULL)
1870 /* Pull the first entry from the free list. */
1871 context = cp_parser_context_free_list;
1872 cp_parser_context_free_list = context->next;
1873 memset (context, 0, sizeof (*context));
1875 else
1876 context = ggc_cleared_alloc<cp_parser_context> ();
1878 /* No errors have occurred yet in this context. */
1879 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1880 /* If this is not the bottommost context, copy information that we
1881 need from the previous context. */
1882 if (next)
1884 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1885 expression, then we are parsing one in this context, too. */
1886 context->object_type = next->object_type;
1887 /* Thread the stack. */
1888 context->next = next;
1891 return context;
1894 /* Managing the unparsed function queues. */
1896 #define unparsed_funs_with_default_args \
1897 parser->unparsed_queues->last ().funs_with_default_args
1898 #define unparsed_funs_with_definitions \
1899 parser->unparsed_queues->last ().funs_with_definitions
1900 #define unparsed_nsdmis \
1901 parser->unparsed_queues->last ().nsdmis
1902 #define unparsed_classes \
1903 parser->unparsed_queues->last ().classes
1905 static void
1906 push_unparsed_function_queues (cp_parser *parser)
1908 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1909 vec_safe_push (parser->unparsed_queues, e);
1912 static void
1913 pop_unparsed_function_queues (cp_parser *parser)
1915 release_tree_vector (unparsed_funs_with_definitions);
1916 parser->unparsed_queues->pop ();
1919 /* Prototypes. */
1921 /* Constructors and destructors. */
1923 static cp_parser *cp_parser_new
1924 (void);
1926 /* Routines to parse various constructs.
1928 Those that return `tree' will return the error_mark_node (rather
1929 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1930 Sometimes, they will return an ordinary node if error-recovery was
1931 attempted, even though a parse error occurred. So, to check
1932 whether or not a parse error occurred, you should always use
1933 cp_parser_error_occurred. If the construct is optional (indicated
1934 either by an `_opt' in the name of the function that does the
1935 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1936 the construct is not present. */
1938 /* Lexical conventions [gram.lex] */
1940 static tree cp_parser_identifier
1941 (cp_parser *);
1942 static tree cp_parser_string_literal
1943 (cp_parser *, bool, bool, bool);
1944 static tree cp_parser_userdef_char_literal
1945 (cp_parser *);
1946 static tree cp_parser_userdef_string_literal
1947 (tree);
1948 static tree cp_parser_userdef_numeric_literal
1949 (cp_parser *);
1951 /* Basic concepts [gram.basic] */
1953 static bool cp_parser_translation_unit
1954 (cp_parser *);
1956 /* Expressions [gram.expr] */
1958 static tree cp_parser_primary_expression
1959 (cp_parser *, bool, bool, bool, cp_id_kind *);
1960 static tree cp_parser_id_expression
1961 (cp_parser *, bool, bool, bool *, bool, bool);
1962 static tree cp_parser_unqualified_id
1963 (cp_parser *, bool, bool, bool, bool);
1964 static tree cp_parser_nested_name_specifier_opt
1965 (cp_parser *, bool, bool, bool, bool);
1966 static tree cp_parser_nested_name_specifier
1967 (cp_parser *, bool, bool, bool, bool);
1968 static tree cp_parser_qualifying_entity
1969 (cp_parser *, bool, bool, bool, bool, bool);
1970 static tree cp_parser_postfix_expression
1971 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1972 static tree cp_parser_postfix_open_square_expression
1973 (cp_parser *, tree, bool, bool);
1974 static tree cp_parser_postfix_dot_deref_expression
1975 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1976 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1977 (cp_parser *, int, bool, bool, bool *, bool = false);
1978 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1979 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1980 static void cp_parser_pseudo_destructor_name
1981 (cp_parser *, tree, tree *, tree *);
1982 static tree cp_parser_unary_expression
1983 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1984 static enum tree_code cp_parser_unary_operator
1985 (cp_token *);
1986 static tree cp_parser_new_expression
1987 (cp_parser *);
1988 static vec<tree, va_gc> *cp_parser_new_placement
1989 (cp_parser *);
1990 static tree cp_parser_new_type_id
1991 (cp_parser *, tree *);
1992 static cp_declarator *cp_parser_new_declarator_opt
1993 (cp_parser *);
1994 static cp_declarator *cp_parser_direct_new_declarator
1995 (cp_parser *);
1996 static vec<tree, va_gc> *cp_parser_new_initializer
1997 (cp_parser *);
1998 static tree cp_parser_delete_expression
1999 (cp_parser *);
2000 static tree cp_parser_cast_expression
2001 (cp_parser *, bool, bool, bool, cp_id_kind *);
2002 static tree cp_parser_binary_expression
2003 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2004 static tree cp_parser_question_colon_clause
2005 (cp_parser *, tree);
2006 static tree cp_parser_assignment_expression
2007 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2008 static enum tree_code cp_parser_assignment_operator_opt
2009 (cp_parser *);
2010 static tree cp_parser_expression
2011 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2012 static tree cp_parser_constant_expression
2013 (cp_parser *, bool = false, bool * = NULL);
2014 static tree cp_parser_builtin_offsetof
2015 (cp_parser *);
2016 static tree cp_parser_lambda_expression
2017 (cp_parser *);
2018 static void cp_parser_lambda_introducer
2019 (cp_parser *, tree);
2020 static bool cp_parser_lambda_declarator_opt
2021 (cp_parser *, tree);
2022 static void cp_parser_lambda_body
2023 (cp_parser *, tree);
2025 /* Statements [gram.stmt.stmt] */
2027 static void cp_parser_statement
2028 (cp_parser *, tree, bool, bool *);
2029 static void cp_parser_label_for_labeled_statement
2030 (cp_parser *, tree);
2031 static tree cp_parser_expression_statement
2032 (cp_parser *, tree);
2033 static tree cp_parser_compound_statement
2034 (cp_parser *, tree, bool, bool);
2035 static void cp_parser_statement_seq_opt
2036 (cp_parser *, tree);
2037 static tree cp_parser_selection_statement
2038 (cp_parser *, bool *);
2039 static tree cp_parser_condition
2040 (cp_parser *);
2041 static tree cp_parser_iteration_statement
2042 (cp_parser *, bool);
2043 static bool cp_parser_for_init_statement
2044 (cp_parser *, tree *decl);
2045 static tree cp_parser_for
2046 (cp_parser *, bool);
2047 static tree cp_parser_c_for
2048 (cp_parser *, tree, tree, bool);
2049 static tree cp_parser_range_for
2050 (cp_parser *, tree, tree, tree, bool);
2051 static void do_range_for_auto_deduction
2052 (tree, tree);
2053 static tree cp_parser_perform_range_for_lookup
2054 (tree, tree *, tree *);
2055 static tree cp_parser_range_for_member_function
2056 (tree, tree);
2057 static tree cp_parser_jump_statement
2058 (cp_parser *);
2059 static void cp_parser_declaration_statement
2060 (cp_parser *);
2062 static tree cp_parser_implicitly_scoped_statement
2063 (cp_parser *, bool *);
2064 static void cp_parser_already_scoped_statement
2065 (cp_parser *);
2067 /* Declarations [gram.dcl.dcl] */
2069 static void cp_parser_declaration_seq_opt
2070 (cp_parser *);
2071 static void cp_parser_declaration
2072 (cp_parser *);
2073 static void cp_parser_block_declaration
2074 (cp_parser *, bool);
2075 static void cp_parser_simple_declaration
2076 (cp_parser *, bool, tree *);
2077 static void cp_parser_decl_specifier_seq
2078 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2079 static tree cp_parser_storage_class_specifier_opt
2080 (cp_parser *);
2081 static tree cp_parser_function_specifier_opt
2082 (cp_parser *, cp_decl_specifier_seq *);
2083 static tree cp_parser_type_specifier
2084 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2085 int *, bool *);
2086 static tree cp_parser_simple_type_specifier
2087 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2088 static tree cp_parser_type_name
2089 (cp_parser *);
2090 static tree cp_parser_nonclass_name
2091 (cp_parser* parser);
2092 static tree cp_parser_elaborated_type_specifier
2093 (cp_parser *, bool, bool);
2094 static tree cp_parser_enum_specifier
2095 (cp_parser *);
2096 static void cp_parser_enumerator_list
2097 (cp_parser *, tree);
2098 static void cp_parser_enumerator_definition
2099 (cp_parser *, tree);
2100 static tree cp_parser_namespace_name
2101 (cp_parser *);
2102 static void cp_parser_namespace_definition
2103 (cp_parser *);
2104 static void cp_parser_namespace_body
2105 (cp_parser *);
2106 static tree cp_parser_qualified_namespace_specifier
2107 (cp_parser *);
2108 static void cp_parser_namespace_alias_definition
2109 (cp_parser *);
2110 static bool cp_parser_using_declaration
2111 (cp_parser *, bool);
2112 static void cp_parser_using_directive
2113 (cp_parser *);
2114 static tree cp_parser_alias_declaration
2115 (cp_parser *);
2116 static void cp_parser_asm_definition
2117 (cp_parser *);
2118 static void cp_parser_linkage_specification
2119 (cp_parser *);
2120 static void cp_parser_static_assert
2121 (cp_parser *, bool);
2122 static tree cp_parser_decltype
2123 (cp_parser *);
2125 /* Declarators [gram.dcl.decl] */
2127 static tree cp_parser_init_declarator
2128 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
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;
11459 if (maybe_range_for_decl)
11460 *maybe_range_for_decl = NULL_TREE;
11462 /* Defer access checks until we know what is being declared; the
11463 checks for names appearing in the decl-specifier-seq should be
11464 done as if we were in the scope of the thing being declared. */
11465 push_deferring_access_checks (dk_deferred);
11467 /* Parse the decl-specifier-seq. We have to keep track of whether
11468 or not the decl-specifier-seq declares a named class or
11469 enumeration type, since that is the only case in which the
11470 init-declarator-list is allowed to be empty.
11472 [dcl.dcl]
11474 In a simple-declaration, the optional init-declarator-list can be
11475 omitted only when declaring a class or enumeration, that is when
11476 the decl-specifier-seq contains either a class-specifier, an
11477 elaborated-type-specifier, or an enum-specifier. */
11478 cp_parser_decl_specifier_seq (parser,
11479 CP_PARSER_FLAGS_OPTIONAL,
11480 &decl_specifiers,
11481 &declares_class_or_enum);
11482 /* We no longer need to defer access checks. */
11483 stop_deferring_access_checks ();
11485 /* In a block scope, a valid declaration must always have a
11486 decl-specifier-seq. By not trying to parse declarators, we can
11487 resolve the declaration/expression ambiguity more quickly. */
11488 if (!function_definition_allowed_p
11489 && !decl_specifiers.any_specifiers_p)
11491 cp_parser_error (parser, "expected declaration");
11492 goto done;
11495 /* If the next two tokens are both identifiers, the code is
11496 erroneous. The usual cause of this situation is code like:
11498 T t;
11500 where "T" should name a type -- but does not. */
11501 if (!decl_specifiers.any_type_specifiers_p
11502 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11504 /* If parsing tentatively, we should commit; we really are
11505 looking at a declaration. */
11506 cp_parser_commit_to_tentative_parse (parser);
11507 /* Give up. */
11508 goto done;
11511 /* If we have seen at least one decl-specifier, and the next token
11512 is not a parenthesis, then we must be looking at a declaration.
11513 (After "int (" we might be looking at a functional cast.) */
11514 if (decl_specifiers.any_specifiers_p
11515 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11516 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11517 && !cp_parser_error_occurred (parser))
11518 cp_parser_commit_to_tentative_parse (parser);
11520 /* Keep going until we hit the `;' at the end of the simple
11521 declaration. */
11522 saw_declarator = false;
11523 while (cp_lexer_next_token_is_not (parser->lexer,
11524 CPP_SEMICOLON))
11526 cp_token *token;
11527 bool function_definition_p;
11528 tree decl;
11530 if (saw_declarator)
11532 /* If we are processing next declarator, coma is expected */
11533 token = cp_lexer_peek_token (parser->lexer);
11534 gcc_assert (token->type == CPP_COMMA);
11535 cp_lexer_consume_token (parser->lexer);
11536 if (maybe_range_for_decl)
11537 *maybe_range_for_decl = error_mark_node;
11539 else
11540 saw_declarator = true;
11542 /* Parse the init-declarator. */
11543 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11544 /*checks=*/NULL,
11545 function_definition_allowed_p,
11546 /*member_p=*/false,
11547 declares_class_or_enum,
11548 &function_definition_p,
11549 maybe_range_for_decl);
11550 /* If an error occurred while parsing tentatively, exit quickly.
11551 (That usually happens when in the body of a function; each
11552 statement is treated as a declaration-statement until proven
11553 otherwise.) */
11554 if (cp_parser_error_occurred (parser))
11555 goto done;
11556 /* Handle function definitions specially. */
11557 if (function_definition_p)
11559 /* If the next token is a `,', then we are probably
11560 processing something like:
11562 void f() {}, *p;
11564 which is erroneous. */
11565 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11567 cp_token *token = cp_lexer_peek_token (parser->lexer);
11568 error_at (token->location,
11569 "mixing"
11570 " declarations and function-definitions is forbidden");
11572 /* Otherwise, we're done with the list of declarators. */
11573 else
11575 pop_deferring_access_checks ();
11576 return;
11579 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11580 *maybe_range_for_decl = decl;
11581 /* The next token should be either a `,' or a `;'. */
11582 token = cp_lexer_peek_token (parser->lexer);
11583 /* If it's a `,', there are more declarators to come. */
11584 if (token->type == CPP_COMMA)
11585 /* will be consumed next time around */;
11586 /* If it's a `;', we are done. */
11587 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11588 break;
11589 /* Anything else is an error. */
11590 else
11592 /* If we have already issued an error message we don't need
11593 to issue another one. */
11594 if (decl != error_mark_node
11595 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11596 cp_parser_error (parser, "expected %<,%> or %<;%>");
11597 /* Skip tokens until we reach the end of the statement. */
11598 cp_parser_skip_to_end_of_statement (parser);
11599 /* If the next token is now a `;', consume it. */
11600 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11601 cp_lexer_consume_token (parser->lexer);
11602 goto done;
11604 /* After the first time around, a function-definition is not
11605 allowed -- even if it was OK at first. For example:
11607 int i, f() {}
11609 is not valid. */
11610 function_definition_allowed_p = false;
11613 /* Issue an error message if no declarators are present, and the
11614 decl-specifier-seq does not itself declare a class or
11615 enumeration: [dcl.dcl]/3. */
11616 if (!saw_declarator)
11618 if (cp_parser_declares_only_class_p (parser))
11620 if (!declares_class_or_enum
11621 && decl_specifiers.type
11622 && OVERLOAD_TYPE_P (decl_specifiers.type))
11623 /* Ensure an error is issued anyway when finish_decltype_type,
11624 called via cp_parser_decl_specifier_seq, returns a class or
11625 an enumeration (c++/51786). */
11626 decl_specifiers.type = NULL_TREE;
11627 shadow_tag (&decl_specifiers);
11629 /* Perform any deferred access checks. */
11630 perform_deferred_access_checks (tf_warning_or_error);
11633 /* Consume the `;'. */
11634 if (!maybe_range_for_decl)
11635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11637 done:
11638 pop_deferring_access_checks ();
11641 /* Parse a decl-specifier-seq.
11643 decl-specifier-seq:
11644 decl-specifier-seq [opt] decl-specifier
11645 decl-specifier attribute-specifier-seq [opt] (C++11)
11647 decl-specifier:
11648 storage-class-specifier
11649 type-specifier
11650 function-specifier
11651 friend
11652 typedef
11654 GNU Extension:
11656 decl-specifier:
11657 attributes
11659 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11661 The parser flags FLAGS is used to control type-specifier parsing.
11663 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11664 flags:
11666 1: one of the decl-specifiers is an elaborated-type-specifier
11667 (i.e., a type declaration)
11668 2: one of the decl-specifiers is an enum-specifier or a
11669 class-specifier (i.e., a type definition)
11673 static void
11674 cp_parser_decl_specifier_seq (cp_parser* parser,
11675 cp_parser_flags flags,
11676 cp_decl_specifier_seq *decl_specs,
11677 int* declares_class_or_enum)
11679 bool constructor_possible_p = !parser->in_declarator_p;
11680 bool found_decl_spec = false;
11681 cp_token *start_token = NULL;
11682 cp_decl_spec ds;
11684 /* Clear DECL_SPECS. */
11685 clear_decl_specs (decl_specs);
11687 /* Assume no class or enumeration type is declared. */
11688 *declares_class_or_enum = 0;
11690 /* Keep reading specifiers until there are no more to read. */
11691 while (true)
11693 bool constructor_p;
11694 cp_token *token;
11695 ds = ds_last;
11697 /* Peek at the next token. */
11698 token = cp_lexer_peek_token (parser->lexer);
11700 /* Save the first token of the decl spec list for error
11701 reporting. */
11702 if (!start_token)
11703 start_token = token;
11704 /* Handle attributes. */
11705 if (cp_next_tokens_can_be_attribute_p (parser))
11707 /* Parse the attributes. */
11708 tree attrs = cp_parser_attributes_opt (parser);
11710 /* In a sequence of declaration specifiers, c++11 attributes
11711 appertain to the type that precede them. In that case
11712 [dcl.spec]/1 says:
11714 The attribute-specifier-seq affects the type only for
11715 the declaration it appears in, not other declarations
11716 involving the same type.
11718 But for now let's force the user to position the
11719 attribute either at the beginning of the declaration or
11720 after the declarator-id, which would clearly mean that it
11721 applies to the declarator. */
11722 if (cxx11_attribute_p (attrs))
11724 if (!found_decl_spec)
11725 /* The c++11 attribute is at the beginning of the
11726 declaration. It appertains to the entity being
11727 declared. */;
11728 else
11730 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11732 /* This is an attribute following a
11733 class-specifier. */
11734 if (decl_specs->type_definition_p)
11735 warn_misplaced_attr_for_class_type (token->location,
11736 decl_specs->type);
11737 attrs = NULL_TREE;
11739 else
11741 decl_specs->std_attributes
11742 = chainon (decl_specs->std_attributes,
11743 attrs);
11744 if (decl_specs->locations[ds_std_attribute] == 0)
11745 decl_specs->locations[ds_std_attribute] = token->location;
11747 continue;
11751 decl_specs->attributes
11752 = chainon (decl_specs->attributes,
11753 attrs);
11754 if (decl_specs->locations[ds_attribute] == 0)
11755 decl_specs->locations[ds_attribute] = token->location;
11756 continue;
11758 /* Assume we will find a decl-specifier keyword. */
11759 found_decl_spec = true;
11760 /* If the next token is an appropriate keyword, we can simply
11761 add it to the list. */
11762 switch (token->keyword)
11764 /* decl-specifier:
11765 friend
11766 constexpr */
11767 case RID_FRIEND:
11768 if (!at_class_scope_p ())
11770 error_at (token->location, "%<friend%> used outside of class");
11771 cp_lexer_purge_token (parser->lexer);
11773 else
11775 ds = ds_friend;
11776 /* Consume the token. */
11777 cp_lexer_consume_token (parser->lexer);
11779 break;
11781 case RID_CONSTEXPR:
11782 ds = ds_constexpr;
11783 cp_lexer_consume_token (parser->lexer);
11784 break;
11786 /* function-specifier:
11787 inline
11788 virtual
11789 explicit */
11790 case RID_INLINE:
11791 case RID_VIRTUAL:
11792 case RID_EXPLICIT:
11793 cp_parser_function_specifier_opt (parser, decl_specs);
11794 break;
11796 /* decl-specifier:
11797 typedef */
11798 case RID_TYPEDEF:
11799 ds = ds_typedef;
11800 /* Consume the token. */
11801 cp_lexer_consume_token (parser->lexer);
11802 /* A constructor declarator cannot appear in a typedef. */
11803 constructor_possible_p = false;
11804 /* The "typedef" keyword can only occur in a declaration; we
11805 may as well commit at this point. */
11806 cp_parser_commit_to_tentative_parse (parser);
11808 if (decl_specs->storage_class != sc_none)
11809 decl_specs->conflicting_specifiers_p = true;
11810 break;
11812 /* storage-class-specifier:
11813 auto
11814 register
11815 static
11816 extern
11817 mutable
11819 GNU Extension:
11820 thread */
11821 case RID_AUTO:
11822 if (cxx_dialect == cxx98)
11824 /* Consume the token. */
11825 cp_lexer_consume_token (parser->lexer);
11827 /* Complain about `auto' as a storage specifier, if
11828 we're complaining about C++0x compatibility. */
11829 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11830 " changes meaning in C++11; please remove it");
11832 /* Set the storage class anyway. */
11833 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11834 token);
11836 else
11837 /* C++0x auto type-specifier. */
11838 found_decl_spec = false;
11839 break;
11841 case RID_REGISTER:
11842 case RID_STATIC:
11843 case RID_EXTERN:
11844 case RID_MUTABLE:
11845 /* Consume the token. */
11846 cp_lexer_consume_token (parser->lexer);
11847 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11848 token);
11849 break;
11850 case RID_THREAD:
11851 /* Consume the token. */
11852 ds = ds_thread;
11853 cp_lexer_consume_token (parser->lexer);
11854 break;
11856 default:
11857 /* We did not yet find a decl-specifier yet. */
11858 found_decl_spec = false;
11859 break;
11862 if (found_decl_spec
11863 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11864 && token->keyword != RID_CONSTEXPR)
11865 error ("decl-specifier invalid in condition");
11867 if (ds != ds_last)
11868 set_and_check_decl_spec_loc (decl_specs, ds, token);
11870 /* Constructors are a special case. The `S' in `S()' is not a
11871 decl-specifier; it is the beginning of the declarator. */
11872 constructor_p
11873 = (!found_decl_spec
11874 && constructor_possible_p
11875 && (cp_parser_constructor_declarator_p
11876 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11878 /* If we don't have a DECL_SPEC yet, then we must be looking at
11879 a type-specifier. */
11880 if (!found_decl_spec && !constructor_p)
11882 int decl_spec_declares_class_or_enum;
11883 bool is_cv_qualifier;
11884 tree type_spec;
11886 type_spec
11887 = cp_parser_type_specifier (parser, flags,
11888 decl_specs,
11889 /*is_declaration=*/true,
11890 &decl_spec_declares_class_or_enum,
11891 &is_cv_qualifier);
11892 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11894 /* If this type-specifier referenced a user-defined type
11895 (a typedef, class-name, etc.), then we can't allow any
11896 more such type-specifiers henceforth.
11898 [dcl.spec]
11900 The longest sequence of decl-specifiers that could
11901 possibly be a type name is taken as the
11902 decl-specifier-seq of a declaration. The sequence shall
11903 be self-consistent as described below.
11905 [dcl.type]
11907 As a general rule, at most one type-specifier is allowed
11908 in the complete decl-specifier-seq of a declaration. The
11909 only exceptions are the following:
11911 -- const or volatile can be combined with any other
11912 type-specifier.
11914 -- signed or unsigned can be combined with char, long,
11915 short, or int.
11917 -- ..
11919 Example:
11921 typedef char* Pc;
11922 void g (const int Pc);
11924 Here, Pc is *not* part of the decl-specifier seq; it's
11925 the declarator. Therefore, once we see a type-specifier
11926 (other than a cv-qualifier), we forbid any additional
11927 user-defined types. We *do* still allow things like `int
11928 int' to be considered a decl-specifier-seq, and issue the
11929 error message later. */
11930 if (type_spec && !is_cv_qualifier)
11931 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11932 /* A constructor declarator cannot follow a type-specifier. */
11933 if (type_spec)
11935 constructor_possible_p = false;
11936 found_decl_spec = true;
11937 if (!is_cv_qualifier)
11938 decl_specs->any_type_specifiers_p = true;
11942 /* If we still do not have a DECL_SPEC, then there are no more
11943 decl-specifiers. */
11944 if (!found_decl_spec)
11945 break;
11947 decl_specs->any_specifiers_p = true;
11948 /* After we see one decl-specifier, further decl-specifiers are
11949 always optional. */
11950 flags |= CP_PARSER_FLAGS_OPTIONAL;
11953 /* Don't allow a friend specifier with a class definition. */
11954 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11955 && (*declares_class_or_enum & 2))
11956 error_at (decl_specs->locations[ds_friend],
11957 "class definition may not be declared a friend");
11960 /* Parse an (optional) storage-class-specifier.
11962 storage-class-specifier:
11963 auto
11964 register
11965 static
11966 extern
11967 mutable
11969 GNU Extension:
11971 storage-class-specifier:
11972 thread
11974 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11976 static tree
11977 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11979 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11981 case RID_AUTO:
11982 if (cxx_dialect != cxx98)
11983 return NULL_TREE;
11984 /* Fall through for C++98. */
11986 case RID_REGISTER:
11987 case RID_STATIC:
11988 case RID_EXTERN:
11989 case RID_MUTABLE:
11990 case RID_THREAD:
11991 /* Consume the token. */
11992 return cp_lexer_consume_token (parser->lexer)->u.value;
11994 default:
11995 return NULL_TREE;
11999 /* Parse an (optional) function-specifier.
12001 function-specifier:
12002 inline
12003 virtual
12004 explicit
12006 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12007 Updates DECL_SPECS, if it is non-NULL. */
12009 static tree
12010 cp_parser_function_specifier_opt (cp_parser* parser,
12011 cp_decl_specifier_seq *decl_specs)
12013 cp_token *token = cp_lexer_peek_token (parser->lexer);
12014 switch (token->keyword)
12016 case RID_INLINE:
12017 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12018 break;
12020 case RID_VIRTUAL:
12021 /* 14.5.2.3 [temp.mem]
12023 A member function template shall not be virtual. */
12024 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12025 error_at (token->location, "templates may not be %<virtual%>");
12026 else
12027 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12028 break;
12030 case RID_EXPLICIT:
12031 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12032 break;
12034 default:
12035 return NULL_TREE;
12038 /* Consume the token. */
12039 return cp_lexer_consume_token (parser->lexer)->u.value;
12042 /* Parse a linkage-specification.
12044 linkage-specification:
12045 extern string-literal { declaration-seq [opt] }
12046 extern string-literal declaration */
12048 static void
12049 cp_parser_linkage_specification (cp_parser* parser)
12051 tree linkage;
12053 /* Look for the `extern' keyword. */
12054 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12056 /* Look for the string-literal. */
12057 linkage = cp_parser_string_literal (parser, false, false);
12059 /* Transform the literal into an identifier. If the literal is a
12060 wide-character string, or contains embedded NULs, then we can't
12061 handle it as the user wants. */
12062 if (strlen (TREE_STRING_POINTER (linkage))
12063 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12065 cp_parser_error (parser, "invalid linkage-specification");
12066 /* Assume C++ linkage. */
12067 linkage = lang_name_cplusplus;
12069 else
12070 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12072 /* We're now using the new linkage. */
12073 push_lang_context (linkage);
12075 /* If the next token is a `{', then we're using the first
12076 production. */
12077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12079 cp_ensure_no_omp_declare_simd (parser);
12081 /* Consume the `{' token. */
12082 cp_lexer_consume_token (parser->lexer);
12083 /* Parse the declarations. */
12084 cp_parser_declaration_seq_opt (parser);
12085 /* Look for the closing `}'. */
12086 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12088 /* Otherwise, there's just one declaration. */
12089 else
12091 bool saved_in_unbraced_linkage_specification_p;
12093 saved_in_unbraced_linkage_specification_p
12094 = parser->in_unbraced_linkage_specification_p;
12095 parser->in_unbraced_linkage_specification_p = true;
12096 cp_parser_declaration (parser);
12097 parser->in_unbraced_linkage_specification_p
12098 = saved_in_unbraced_linkage_specification_p;
12101 /* We're done with the linkage-specification. */
12102 pop_lang_context ();
12105 /* Parse a static_assert-declaration.
12107 static_assert-declaration:
12108 static_assert ( constant-expression , string-literal ) ;
12110 If MEMBER_P, this static_assert is a class member. */
12112 static void
12113 cp_parser_static_assert(cp_parser *parser, bool member_p)
12115 tree condition;
12116 tree message;
12117 cp_token *token;
12118 location_t saved_loc;
12119 bool dummy;
12121 /* Peek at the `static_assert' token so we can keep track of exactly
12122 where the static assertion started. */
12123 token = cp_lexer_peek_token (parser->lexer);
12124 saved_loc = token->location;
12126 /* Look for the `static_assert' keyword. */
12127 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12128 RT_STATIC_ASSERT))
12129 return;
12131 /* We know we are in a static assertion; commit to any tentative
12132 parse. */
12133 if (cp_parser_parsing_tentatively (parser))
12134 cp_parser_commit_to_tentative_parse (parser);
12136 /* Parse the `(' starting the static assertion condition. */
12137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12139 /* Parse the constant-expression. Allow a non-constant expression
12140 here in order to give better diagnostics in finish_static_assert. */
12141 condition =
12142 cp_parser_constant_expression (parser,
12143 /*allow_non_constant_p=*/true,
12144 /*non_constant_p=*/&dummy);
12146 /* Parse the separating `,'. */
12147 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12149 /* Parse the string-literal message. */
12150 message = cp_parser_string_literal (parser,
12151 /*translate=*/false,
12152 /*wide_ok=*/true);
12154 /* A `)' completes the static assertion. */
12155 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12156 cp_parser_skip_to_closing_parenthesis (parser,
12157 /*recovering=*/true,
12158 /*or_comma=*/false,
12159 /*consume_paren=*/true);
12161 /* A semicolon terminates the declaration. */
12162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12164 /* Complete the static assertion, which may mean either processing
12165 the static assert now or saving it for template instantiation. */
12166 finish_static_assert (condition, message, saved_loc, member_p);
12169 /* Parse the expression in decltype ( expression ). */
12171 static tree
12172 cp_parser_decltype_expr (cp_parser *parser,
12173 bool &id_expression_or_member_access_p)
12175 cp_token *id_expr_start_token;
12176 tree expr;
12178 /* First, try parsing an id-expression. */
12179 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12180 cp_parser_parse_tentatively (parser);
12181 expr = cp_parser_id_expression (parser,
12182 /*template_keyword_p=*/false,
12183 /*check_dependency_p=*/true,
12184 /*template_p=*/NULL,
12185 /*declarator_p=*/false,
12186 /*optional_p=*/false);
12188 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12190 bool non_integral_constant_expression_p = false;
12191 tree id_expression = expr;
12192 cp_id_kind idk;
12193 const char *error_msg;
12195 if (identifier_p (expr))
12196 /* Lookup the name we got back from the id-expression. */
12197 expr = cp_parser_lookup_name_simple (parser, expr,
12198 id_expr_start_token->location);
12200 if (expr
12201 && expr != error_mark_node
12202 && TREE_CODE (expr) != TYPE_DECL
12203 && (TREE_CODE (expr) != BIT_NOT_EXPR
12204 || !TYPE_P (TREE_OPERAND (expr, 0)))
12205 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12207 /* Complete lookup of the id-expression. */
12208 expr = (finish_id_expression
12209 (id_expression, expr, parser->scope, &idk,
12210 /*integral_constant_expression_p=*/false,
12211 /*allow_non_integral_constant_expression_p=*/true,
12212 &non_integral_constant_expression_p,
12213 /*template_p=*/false,
12214 /*done=*/true,
12215 /*address_p=*/false,
12216 /*template_arg_p=*/false,
12217 &error_msg,
12218 id_expr_start_token->location));
12220 if (expr == error_mark_node)
12221 /* We found an id-expression, but it was something that we
12222 should not have found. This is an error, not something
12223 we can recover from, so note that we found an
12224 id-expression and we'll recover as gracefully as
12225 possible. */
12226 id_expression_or_member_access_p = true;
12229 if (expr
12230 && expr != error_mark_node
12231 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12232 /* We have an id-expression. */
12233 id_expression_or_member_access_p = true;
12236 if (!id_expression_or_member_access_p)
12238 /* Abort the id-expression parse. */
12239 cp_parser_abort_tentative_parse (parser);
12241 /* Parsing tentatively, again. */
12242 cp_parser_parse_tentatively (parser);
12244 /* Parse a class member access. */
12245 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12246 /*cast_p=*/false, /*decltype*/true,
12247 /*member_access_only_p=*/true, NULL);
12249 if (expr
12250 && expr != error_mark_node
12251 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12252 /* We have an id-expression. */
12253 id_expression_or_member_access_p = true;
12256 if (id_expression_or_member_access_p)
12257 /* We have parsed the complete id-expression or member access. */
12258 cp_parser_parse_definitely (parser);
12259 else
12261 /* Abort our attempt to parse an id-expression or member access
12262 expression. */
12263 cp_parser_abort_tentative_parse (parser);
12265 /* Parse a full expression. */
12266 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12267 /*decltype_p=*/true);
12270 return expr;
12273 /* Parse a `decltype' type. Returns the type.
12275 simple-type-specifier:
12276 decltype ( expression )
12277 C++14 proposal:
12278 decltype ( auto ) */
12280 static tree
12281 cp_parser_decltype (cp_parser *parser)
12283 tree expr;
12284 bool id_expression_or_member_access_p = false;
12285 const char *saved_message;
12286 bool saved_integral_constant_expression_p;
12287 bool saved_non_integral_constant_expression_p;
12288 bool saved_greater_than_is_operator_p;
12289 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12291 if (start_token->type == CPP_DECLTYPE)
12293 /* Already parsed. */
12294 cp_lexer_consume_token (parser->lexer);
12295 return start_token->u.value;
12298 /* Look for the `decltype' token. */
12299 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12300 return error_mark_node;
12302 /* Parse the opening `('. */
12303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12304 return error_mark_node;
12306 /* decltype (auto) */
12307 if (cxx_dialect >= cxx14
12308 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12310 cp_lexer_consume_token (parser->lexer);
12311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12312 return error_mark_node;
12313 expr = make_decltype_auto ();
12314 AUTO_IS_DECLTYPE (expr) = true;
12315 goto rewrite;
12318 /* Types cannot be defined in a `decltype' expression. Save away the
12319 old message. */
12320 saved_message = parser->type_definition_forbidden_message;
12322 /* And create the new one. */
12323 parser->type_definition_forbidden_message
12324 = G_("types may not be defined in %<decltype%> expressions");
12326 /* The restrictions on constant-expressions do not apply inside
12327 decltype expressions. */
12328 saved_integral_constant_expression_p
12329 = parser->integral_constant_expression_p;
12330 saved_non_integral_constant_expression_p
12331 = parser->non_integral_constant_expression_p;
12332 parser->integral_constant_expression_p = false;
12334 /* Within a parenthesized expression, a `>' token is always
12335 the greater-than operator. */
12336 saved_greater_than_is_operator_p
12337 = parser->greater_than_is_operator_p;
12338 parser->greater_than_is_operator_p = true;
12340 /* Do not actually evaluate the expression. */
12341 ++cp_unevaluated_operand;
12343 /* Do not warn about problems with the expression. */
12344 ++c_inhibit_evaluation_warnings;
12346 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12348 /* Go back to evaluating expressions. */
12349 --cp_unevaluated_operand;
12350 --c_inhibit_evaluation_warnings;
12352 /* The `>' token might be the end of a template-id or
12353 template-parameter-list now. */
12354 parser->greater_than_is_operator_p
12355 = saved_greater_than_is_operator_p;
12357 /* Restore the old message and the integral constant expression
12358 flags. */
12359 parser->type_definition_forbidden_message = saved_message;
12360 parser->integral_constant_expression_p
12361 = saved_integral_constant_expression_p;
12362 parser->non_integral_constant_expression_p
12363 = saved_non_integral_constant_expression_p;
12365 /* Parse to the closing `)'. */
12366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12368 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12369 /*consume_paren=*/true);
12370 return error_mark_node;
12373 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12374 tf_warning_or_error);
12376 rewrite:
12377 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12378 it again. */
12379 start_token->type = CPP_DECLTYPE;
12380 start_token->u.value = expr;
12381 start_token->keyword = RID_MAX;
12382 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12384 return expr;
12387 /* Special member functions [gram.special] */
12389 /* Parse a conversion-function-id.
12391 conversion-function-id:
12392 operator conversion-type-id
12394 Returns an IDENTIFIER_NODE representing the operator. */
12396 static tree
12397 cp_parser_conversion_function_id (cp_parser* parser)
12399 tree type;
12400 tree saved_scope;
12401 tree saved_qualifying_scope;
12402 tree saved_object_scope;
12403 tree pushed_scope = NULL_TREE;
12405 /* Look for the `operator' token. */
12406 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12407 return error_mark_node;
12408 /* When we parse the conversion-type-id, the current scope will be
12409 reset. However, we need that information in able to look up the
12410 conversion function later, so we save it here. */
12411 saved_scope = parser->scope;
12412 saved_qualifying_scope = parser->qualifying_scope;
12413 saved_object_scope = parser->object_scope;
12414 /* We must enter the scope of the class so that the names of
12415 entities declared within the class are available in the
12416 conversion-type-id. For example, consider:
12418 struct S {
12419 typedef int I;
12420 operator I();
12423 S::operator I() { ... }
12425 In order to see that `I' is a type-name in the definition, we
12426 must be in the scope of `S'. */
12427 if (saved_scope)
12428 pushed_scope = push_scope (saved_scope);
12429 /* Parse the conversion-type-id. */
12430 type = cp_parser_conversion_type_id (parser);
12431 /* Leave the scope of the class, if any. */
12432 if (pushed_scope)
12433 pop_scope (pushed_scope);
12434 /* Restore the saved scope. */
12435 parser->scope = saved_scope;
12436 parser->qualifying_scope = saved_qualifying_scope;
12437 parser->object_scope = saved_object_scope;
12438 /* If the TYPE is invalid, indicate failure. */
12439 if (type == error_mark_node)
12440 return error_mark_node;
12441 return mangle_conv_op_name_for_type (type);
12444 /* Parse a conversion-type-id:
12446 conversion-type-id:
12447 type-specifier-seq conversion-declarator [opt]
12449 Returns the TYPE specified. */
12451 static tree
12452 cp_parser_conversion_type_id (cp_parser* parser)
12454 tree attributes;
12455 cp_decl_specifier_seq type_specifiers;
12456 cp_declarator *declarator;
12457 tree type_specified;
12458 const char *saved_message;
12460 /* Parse the attributes. */
12461 attributes = cp_parser_attributes_opt (parser);
12463 saved_message = parser->type_definition_forbidden_message;
12464 parser->type_definition_forbidden_message
12465 = G_("types may not be defined in a conversion-type-id");
12467 /* Parse the type-specifiers. */
12468 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12469 /*is_trailing_return=*/false,
12470 &type_specifiers);
12472 parser->type_definition_forbidden_message = saved_message;
12474 /* If that didn't work, stop. */
12475 if (type_specifiers.type == error_mark_node)
12476 return error_mark_node;
12477 /* Parse the conversion-declarator. */
12478 declarator = cp_parser_conversion_declarator_opt (parser);
12480 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12481 /*initialized=*/0, &attributes);
12482 if (attributes)
12483 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12485 /* Don't give this error when parsing tentatively. This happens to
12486 work because we always parse this definitively once. */
12487 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12488 && type_uses_auto (type_specified))
12490 if (cxx_dialect < cxx14)
12492 error ("invalid use of %<auto%> in conversion operator");
12493 return error_mark_node;
12495 else if (template_parm_scope_p ())
12496 warning (0, "use of %<auto%> in member template "
12497 "conversion operator can never be deduced");
12500 return type_specified;
12503 /* Parse an (optional) conversion-declarator.
12505 conversion-declarator:
12506 ptr-operator conversion-declarator [opt]
12510 static cp_declarator *
12511 cp_parser_conversion_declarator_opt (cp_parser* parser)
12513 enum tree_code code;
12514 tree class_type, std_attributes = NULL_TREE;
12515 cp_cv_quals cv_quals;
12517 /* We don't know if there's a ptr-operator next, or not. */
12518 cp_parser_parse_tentatively (parser);
12519 /* Try the ptr-operator. */
12520 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12521 &std_attributes);
12522 /* If it worked, look for more conversion-declarators. */
12523 if (cp_parser_parse_definitely (parser))
12525 cp_declarator *declarator;
12527 /* Parse another optional declarator. */
12528 declarator = cp_parser_conversion_declarator_opt (parser);
12530 declarator = cp_parser_make_indirect_declarator
12531 (code, class_type, cv_quals, declarator, std_attributes);
12533 return declarator;
12536 return NULL;
12539 /* Parse an (optional) ctor-initializer.
12541 ctor-initializer:
12542 : mem-initializer-list
12544 Returns TRUE iff the ctor-initializer was actually present. */
12546 static bool
12547 cp_parser_ctor_initializer_opt (cp_parser* parser)
12549 /* If the next token is not a `:', then there is no
12550 ctor-initializer. */
12551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12553 /* Do default initialization of any bases and members. */
12554 if (DECL_CONSTRUCTOR_P (current_function_decl))
12555 finish_mem_initializers (NULL_TREE);
12557 return false;
12560 /* Consume the `:' token. */
12561 cp_lexer_consume_token (parser->lexer);
12562 /* And the mem-initializer-list. */
12563 cp_parser_mem_initializer_list (parser);
12565 return true;
12568 /* Parse a mem-initializer-list.
12570 mem-initializer-list:
12571 mem-initializer ... [opt]
12572 mem-initializer ... [opt] , mem-initializer-list */
12574 static void
12575 cp_parser_mem_initializer_list (cp_parser* parser)
12577 tree mem_initializer_list = NULL_TREE;
12578 tree target_ctor = error_mark_node;
12579 cp_token *token = cp_lexer_peek_token (parser->lexer);
12581 /* Let the semantic analysis code know that we are starting the
12582 mem-initializer-list. */
12583 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12584 error_at (token->location,
12585 "only constructors take member initializers");
12587 /* Loop through the list. */
12588 while (true)
12590 tree mem_initializer;
12592 token = cp_lexer_peek_token (parser->lexer);
12593 /* Parse the mem-initializer. */
12594 mem_initializer = cp_parser_mem_initializer (parser);
12595 /* If the next token is a `...', we're expanding member initializers. */
12596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12598 /* Consume the `...'. */
12599 cp_lexer_consume_token (parser->lexer);
12601 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12602 can be expanded but members cannot. */
12603 if (mem_initializer != error_mark_node
12604 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12606 error_at (token->location,
12607 "cannot expand initializer for member %<%D%>",
12608 TREE_PURPOSE (mem_initializer));
12609 mem_initializer = error_mark_node;
12612 /* Construct the pack expansion type. */
12613 if (mem_initializer != error_mark_node)
12614 mem_initializer = make_pack_expansion (mem_initializer);
12616 if (target_ctor != error_mark_node
12617 && mem_initializer != error_mark_node)
12619 error ("mem-initializer for %qD follows constructor delegation",
12620 TREE_PURPOSE (mem_initializer));
12621 mem_initializer = error_mark_node;
12623 /* Look for a target constructor. */
12624 if (mem_initializer != error_mark_node
12625 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12626 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12628 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12629 if (mem_initializer_list)
12631 error ("constructor delegation follows mem-initializer for %qD",
12632 TREE_PURPOSE (mem_initializer_list));
12633 mem_initializer = error_mark_node;
12635 target_ctor = mem_initializer;
12637 /* Add it to the list, unless it was erroneous. */
12638 if (mem_initializer != error_mark_node)
12640 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12641 mem_initializer_list = mem_initializer;
12643 /* If the next token is not a `,', we're done. */
12644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12645 break;
12646 /* Consume the `,' token. */
12647 cp_lexer_consume_token (parser->lexer);
12650 /* Perform semantic analysis. */
12651 if (DECL_CONSTRUCTOR_P (current_function_decl))
12652 finish_mem_initializers (mem_initializer_list);
12655 /* Parse a mem-initializer.
12657 mem-initializer:
12658 mem-initializer-id ( expression-list [opt] )
12659 mem-initializer-id braced-init-list
12661 GNU extension:
12663 mem-initializer:
12664 ( expression-list [opt] )
12666 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12667 class) or FIELD_DECL (for a non-static data member) to initialize;
12668 the TREE_VALUE is the expression-list. An empty initialization
12669 list is represented by void_list_node. */
12671 static tree
12672 cp_parser_mem_initializer (cp_parser* parser)
12674 tree mem_initializer_id;
12675 tree expression_list;
12676 tree member;
12677 cp_token *token = cp_lexer_peek_token (parser->lexer);
12679 /* Find out what is being initialized. */
12680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12682 permerror (token->location,
12683 "anachronistic old-style base class initializer");
12684 mem_initializer_id = NULL_TREE;
12686 else
12688 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12689 if (mem_initializer_id == error_mark_node)
12690 return mem_initializer_id;
12692 member = expand_member_init (mem_initializer_id);
12693 if (member && !DECL_P (member))
12694 in_base_initializer = 1;
12696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12698 bool expr_non_constant_p;
12699 cp_lexer_set_source_position (parser->lexer);
12700 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12701 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12702 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12703 expression_list = build_tree_list (NULL_TREE, expression_list);
12705 else
12707 vec<tree, va_gc> *vec;
12708 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12709 /*cast_p=*/false,
12710 /*allow_expansion_p=*/true,
12711 /*non_constant_p=*/NULL);
12712 if (vec == NULL)
12713 return error_mark_node;
12714 expression_list = build_tree_list_vec (vec);
12715 release_tree_vector (vec);
12718 if (expression_list == error_mark_node)
12719 return error_mark_node;
12720 if (!expression_list)
12721 expression_list = void_type_node;
12723 in_base_initializer = 0;
12725 return member ? build_tree_list (member, expression_list) : error_mark_node;
12728 /* Parse a mem-initializer-id.
12730 mem-initializer-id:
12731 :: [opt] nested-name-specifier [opt] class-name
12732 identifier
12734 Returns a TYPE indicating the class to be initializer for the first
12735 production. Returns an IDENTIFIER_NODE indicating the data member
12736 to be initialized for the second production. */
12738 static tree
12739 cp_parser_mem_initializer_id (cp_parser* parser)
12741 bool global_scope_p;
12742 bool nested_name_specifier_p;
12743 bool template_p = false;
12744 tree id;
12746 cp_token *token = cp_lexer_peek_token (parser->lexer);
12748 /* `typename' is not allowed in this context ([temp.res]). */
12749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12751 error_at (token->location,
12752 "keyword %<typename%> not allowed in this context (a qualified "
12753 "member initializer is implicitly a type)");
12754 cp_lexer_consume_token (parser->lexer);
12756 /* Look for the optional `::' operator. */
12757 global_scope_p
12758 = (cp_parser_global_scope_opt (parser,
12759 /*current_scope_valid_p=*/false)
12760 != NULL_TREE);
12761 /* Look for the optional nested-name-specifier. The simplest way to
12762 implement:
12764 [temp.res]
12766 The keyword `typename' is not permitted in a base-specifier or
12767 mem-initializer; in these contexts a qualified name that
12768 depends on a template-parameter is implicitly assumed to be a
12769 type name.
12771 is to assume that we have seen the `typename' keyword at this
12772 point. */
12773 nested_name_specifier_p
12774 = (cp_parser_nested_name_specifier_opt (parser,
12775 /*typename_keyword_p=*/true,
12776 /*check_dependency_p=*/true,
12777 /*type_p=*/true,
12778 /*is_declaration=*/true)
12779 != NULL_TREE);
12780 if (nested_name_specifier_p)
12781 template_p = cp_parser_optional_template_keyword (parser);
12782 /* If there is a `::' operator or a nested-name-specifier, then we
12783 are definitely looking for a class-name. */
12784 if (global_scope_p || nested_name_specifier_p)
12785 return cp_parser_class_name (parser,
12786 /*typename_keyword_p=*/true,
12787 /*template_keyword_p=*/template_p,
12788 typename_type,
12789 /*check_dependency_p=*/true,
12790 /*class_head_p=*/false,
12791 /*is_declaration=*/true);
12792 /* Otherwise, we could also be looking for an ordinary identifier. */
12793 cp_parser_parse_tentatively (parser);
12794 /* Try a class-name. */
12795 id = cp_parser_class_name (parser,
12796 /*typename_keyword_p=*/true,
12797 /*template_keyword_p=*/false,
12798 none_type,
12799 /*check_dependency_p=*/true,
12800 /*class_head_p=*/false,
12801 /*is_declaration=*/true);
12802 /* If we found one, we're done. */
12803 if (cp_parser_parse_definitely (parser))
12804 return id;
12805 /* Otherwise, look for an ordinary identifier. */
12806 return cp_parser_identifier (parser);
12809 /* Overloading [gram.over] */
12811 /* Parse an operator-function-id.
12813 operator-function-id:
12814 operator operator
12816 Returns an IDENTIFIER_NODE for the operator which is a
12817 human-readable spelling of the identifier, e.g., `operator +'. */
12819 static tree
12820 cp_parser_operator_function_id (cp_parser* parser)
12822 /* Look for the `operator' keyword. */
12823 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12824 return error_mark_node;
12825 /* And then the name of the operator itself. */
12826 return cp_parser_operator (parser);
12829 /* Return an identifier node for a user-defined literal operator.
12830 The suffix identifier is chained to the operator name identifier. */
12832 static tree
12833 cp_literal_operator_id (const char* name)
12835 tree identifier;
12836 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12837 + strlen (name) + 10);
12838 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12839 identifier = get_identifier (buffer);
12841 return identifier;
12844 /* Parse an operator.
12846 operator:
12847 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12848 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12849 || ++ -- , ->* -> () []
12851 GNU Extensions:
12853 operator:
12854 <? >? <?= >?=
12856 Returns an IDENTIFIER_NODE for the operator which is a
12857 human-readable spelling of the identifier, e.g., `operator +'. */
12859 static tree
12860 cp_parser_operator (cp_parser* parser)
12862 tree id = NULL_TREE;
12863 cp_token *token;
12864 bool utf8 = false;
12866 /* Peek at the next token. */
12867 token = cp_lexer_peek_token (parser->lexer);
12868 /* Figure out which operator we have. */
12869 switch (token->type)
12871 case CPP_KEYWORD:
12873 enum tree_code op;
12875 /* The keyword should be either `new' or `delete'. */
12876 if (token->keyword == RID_NEW)
12877 op = NEW_EXPR;
12878 else if (token->keyword == RID_DELETE)
12879 op = DELETE_EXPR;
12880 else
12881 break;
12883 /* Consume the `new' or `delete' token. */
12884 cp_lexer_consume_token (parser->lexer);
12886 /* Peek at the next token. */
12887 token = cp_lexer_peek_token (parser->lexer);
12888 /* If it's a `[' token then this is the array variant of the
12889 operator. */
12890 if (token->type == CPP_OPEN_SQUARE)
12892 /* Consume the `[' token. */
12893 cp_lexer_consume_token (parser->lexer);
12894 /* Look for the `]' token. */
12895 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12896 id = ansi_opname (op == NEW_EXPR
12897 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12899 /* Otherwise, we have the non-array variant. */
12900 else
12901 id = ansi_opname (op);
12903 return id;
12906 case CPP_PLUS:
12907 id = ansi_opname (PLUS_EXPR);
12908 break;
12910 case CPP_MINUS:
12911 id = ansi_opname (MINUS_EXPR);
12912 break;
12914 case CPP_MULT:
12915 id = ansi_opname (MULT_EXPR);
12916 break;
12918 case CPP_DIV:
12919 id = ansi_opname (TRUNC_DIV_EXPR);
12920 break;
12922 case CPP_MOD:
12923 id = ansi_opname (TRUNC_MOD_EXPR);
12924 break;
12926 case CPP_XOR:
12927 id = ansi_opname (BIT_XOR_EXPR);
12928 break;
12930 case CPP_AND:
12931 id = ansi_opname (BIT_AND_EXPR);
12932 break;
12934 case CPP_OR:
12935 id = ansi_opname (BIT_IOR_EXPR);
12936 break;
12938 case CPP_COMPL:
12939 id = ansi_opname (BIT_NOT_EXPR);
12940 break;
12942 case CPP_NOT:
12943 id = ansi_opname (TRUTH_NOT_EXPR);
12944 break;
12946 case CPP_EQ:
12947 id = ansi_assopname (NOP_EXPR);
12948 break;
12950 case CPP_LESS:
12951 id = ansi_opname (LT_EXPR);
12952 break;
12954 case CPP_GREATER:
12955 id = ansi_opname (GT_EXPR);
12956 break;
12958 case CPP_PLUS_EQ:
12959 id = ansi_assopname (PLUS_EXPR);
12960 break;
12962 case CPP_MINUS_EQ:
12963 id = ansi_assopname (MINUS_EXPR);
12964 break;
12966 case CPP_MULT_EQ:
12967 id = ansi_assopname (MULT_EXPR);
12968 break;
12970 case CPP_DIV_EQ:
12971 id = ansi_assopname (TRUNC_DIV_EXPR);
12972 break;
12974 case CPP_MOD_EQ:
12975 id = ansi_assopname (TRUNC_MOD_EXPR);
12976 break;
12978 case CPP_XOR_EQ:
12979 id = ansi_assopname (BIT_XOR_EXPR);
12980 break;
12982 case CPP_AND_EQ:
12983 id = ansi_assopname (BIT_AND_EXPR);
12984 break;
12986 case CPP_OR_EQ:
12987 id = ansi_assopname (BIT_IOR_EXPR);
12988 break;
12990 case CPP_LSHIFT:
12991 id = ansi_opname (LSHIFT_EXPR);
12992 break;
12994 case CPP_RSHIFT:
12995 id = ansi_opname (RSHIFT_EXPR);
12996 break;
12998 case CPP_LSHIFT_EQ:
12999 id = ansi_assopname (LSHIFT_EXPR);
13000 break;
13002 case CPP_RSHIFT_EQ:
13003 id = ansi_assopname (RSHIFT_EXPR);
13004 break;
13006 case CPP_EQ_EQ:
13007 id = ansi_opname (EQ_EXPR);
13008 break;
13010 case CPP_NOT_EQ:
13011 id = ansi_opname (NE_EXPR);
13012 break;
13014 case CPP_LESS_EQ:
13015 id = ansi_opname (LE_EXPR);
13016 break;
13018 case CPP_GREATER_EQ:
13019 id = ansi_opname (GE_EXPR);
13020 break;
13022 case CPP_AND_AND:
13023 id = ansi_opname (TRUTH_ANDIF_EXPR);
13024 break;
13026 case CPP_OR_OR:
13027 id = ansi_opname (TRUTH_ORIF_EXPR);
13028 break;
13030 case CPP_PLUS_PLUS:
13031 id = ansi_opname (POSTINCREMENT_EXPR);
13032 break;
13034 case CPP_MINUS_MINUS:
13035 id = ansi_opname (PREDECREMENT_EXPR);
13036 break;
13038 case CPP_COMMA:
13039 id = ansi_opname (COMPOUND_EXPR);
13040 break;
13042 case CPP_DEREF_STAR:
13043 id = ansi_opname (MEMBER_REF);
13044 break;
13046 case CPP_DEREF:
13047 id = ansi_opname (COMPONENT_REF);
13048 break;
13050 case CPP_OPEN_PAREN:
13051 /* Consume the `('. */
13052 cp_lexer_consume_token (parser->lexer);
13053 /* Look for the matching `)'. */
13054 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13055 return ansi_opname (CALL_EXPR);
13057 case CPP_OPEN_SQUARE:
13058 /* Consume the `['. */
13059 cp_lexer_consume_token (parser->lexer);
13060 /* Look for the matching `]'. */
13061 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13062 return ansi_opname (ARRAY_REF);
13064 case CPP_UTF8STRING:
13065 case CPP_UTF8STRING_USERDEF:
13066 utf8 = true;
13067 case CPP_STRING:
13068 case CPP_WSTRING:
13069 case CPP_STRING16:
13070 case CPP_STRING32:
13071 case CPP_STRING_USERDEF:
13072 case CPP_WSTRING_USERDEF:
13073 case CPP_STRING16_USERDEF:
13074 case CPP_STRING32_USERDEF:
13076 tree str, string_tree;
13077 int sz, len;
13079 if (cxx_dialect == cxx98)
13080 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13082 /* Consume the string. */
13083 str = cp_parser_string_literal (parser, /*translate=*/true,
13084 /*wide_ok=*/true, /*lookup_udlit=*/false);
13085 if (str == error_mark_node)
13086 return error_mark_node;
13087 else if (TREE_CODE (str) == USERDEF_LITERAL)
13089 string_tree = USERDEF_LITERAL_VALUE (str);
13090 id = USERDEF_LITERAL_SUFFIX_ID (str);
13092 else
13094 string_tree = str;
13095 /* Look for the suffix identifier. */
13096 token = cp_lexer_peek_token (parser->lexer);
13097 if (token->type == CPP_NAME)
13098 id = cp_parser_identifier (parser);
13099 else if (token->type == CPP_KEYWORD)
13101 error ("unexpected keyword;"
13102 " remove space between quotes and suffix identifier");
13103 return error_mark_node;
13105 else
13107 error ("expected suffix identifier");
13108 return error_mark_node;
13111 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13112 (TREE_TYPE (TREE_TYPE (string_tree))));
13113 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13114 if (len != 0)
13116 error ("expected empty string after %<operator%> keyword");
13117 return error_mark_node;
13119 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13120 != char_type_node)
13122 error ("invalid encoding prefix in literal operator");
13123 return error_mark_node;
13125 if (id != error_mark_node)
13127 const char *name = IDENTIFIER_POINTER (id);
13128 id = cp_literal_operator_id (name);
13130 return id;
13133 default:
13134 /* Anything else is an error. */
13135 break;
13138 /* If we have selected an identifier, we need to consume the
13139 operator token. */
13140 if (id)
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Otherwise, no valid operator name was present. */
13143 else
13145 cp_parser_error (parser, "expected operator");
13146 id = error_mark_node;
13149 return id;
13152 /* Parse a template-declaration.
13154 template-declaration:
13155 export [opt] template < template-parameter-list > declaration
13157 If MEMBER_P is TRUE, this template-declaration occurs within a
13158 class-specifier.
13160 The grammar rule given by the standard isn't correct. What
13161 is really meant is:
13163 template-declaration:
13164 export [opt] template-parameter-list-seq
13165 decl-specifier-seq [opt] init-declarator [opt] ;
13166 export [opt] template-parameter-list-seq
13167 function-definition
13169 template-parameter-list-seq:
13170 template-parameter-list-seq [opt]
13171 template < template-parameter-list > */
13173 static void
13174 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13176 /* Check for `export'. */
13177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13179 /* Consume the `export' token. */
13180 cp_lexer_consume_token (parser->lexer);
13181 /* Warn that we do not support `export'. */
13182 warning (0, "keyword %<export%> not implemented, and will be ignored");
13185 cp_parser_template_declaration_after_export (parser, member_p);
13188 /* Parse a template-parameter-list.
13190 template-parameter-list:
13191 template-parameter
13192 template-parameter-list , template-parameter
13194 Returns a TREE_LIST. Each node represents a template parameter.
13195 The nodes are connected via their TREE_CHAINs. */
13197 static tree
13198 cp_parser_template_parameter_list (cp_parser* parser)
13200 tree parameter_list = NULL_TREE;
13202 begin_template_parm_list ();
13204 /* The loop below parses the template parms. We first need to know
13205 the total number of template parms to be able to compute proper
13206 canonical types of each dependent type. So after the loop, when
13207 we know the total number of template parms,
13208 end_template_parm_list computes the proper canonical types and
13209 fixes up the dependent types accordingly. */
13210 while (true)
13212 tree parameter;
13213 bool is_non_type;
13214 bool is_parameter_pack;
13215 location_t parm_loc;
13217 /* Parse the template-parameter. */
13218 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13219 parameter = cp_parser_template_parameter (parser,
13220 &is_non_type,
13221 &is_parameter_pack);
13222 /* Add it to the list. */
13223 if (parameter != error_mark_node)
13224 parameter_list = process_template_parm (parameter_list,
13225 parm_loc,
13226 parameter,
13227 is_non_type,
13228 is_parameter_pack);
13229 else
13231 tree err_parm = build_tree_list (parameter, parameter);
13232 parameter_list = chainon (parameter_list, err_parm);
13235 /* If the next token is not a `,', we're done. */
13236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13237 break;
13238 /* Otherwise, consume the `,' token. */
13239 cp_lexer_consume_token (parser->lexer);
13242 return end_template_parm_list (parameter_list);
13245 /* Parse a template-parameter.
13247 template-parameter:
13248 type-parameter
13249 parameter-declaration
13251 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13252 the parameter. The TREE_PURPOSE is the default value, if any.
13253 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13254 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13255 set to true iff this parameter is a parameter pack. */
13257 static tree
13258 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13259 bool *is_parameter_pack)
13261 cp_token *token;
13262 cp_parameter_declarator *parameter_declarator;
13263 cp_declarator *id_declarator;
13264 tree parm;
13266 /* Assume it is a type parameter or a template parameter. */
13267 *is_non_type = false;
13268 /* Assume it not a parameter pack. */
13269 *is_parameter_pack = false;
13270 /* Peek at the next token. */
13271 token = cp_lexer_peek_token (parser->lexer);
13272 /* If it is `class' or `template', we have a type-parameter. */
13273 if (token->keyword == RID_TEMPLATE)
13274 return cp_parser_type_parameter (parser, is_parameter_pack);
13275 /* If it is `class' or `typename' we do not know yet whether it is a
13276 type parameter or a non-type parameter. Consider:
13278 template <typename T, typename T::X X> ...
13282 template <class C, class D*> ...
13284 Here, the first parameter is a type parameter, and the second is
13285 a non-type parameter. We can tell by looking at the token after
13286 the identifier -- if it is a `,', `=', or `>' then we have a type
13287 parameter. */
13288 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13290 /* Peek at the token after `class' or `typename'. */
13291 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13292 /* If it's an ellipsis, we have a template type parameter
13293 pack. */
13294 if (token->type == CPP_ELLIPSIS)
13295 return cp_parser_type_parameter (parser, is_parameter_pack);
13296 /* If it's an identifier, skip it. */
13297 if (token->type == CPP_NAME)
13298 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13299 /* Now, see if the token looks like the end of a template
13300 parameter. */
13301 if (token->type == CPP_COMMA
13302 || token->type == CPP_EQ
13303 || token->type == CPP_GREATER)
13304 return cp_parser_type_parameter (parser, is_parameter_pack);
13307 /* Otherwise, it is a non-type parameter.
13309 [temp.param]
13311 When parsing a default template-argument for a non-type
13312 template-parameter, the first non-nested `>' is taken as the end
13313 of the template parameter-list rather than a greater-than
13314 operator. */
13315 *is_non_type = true;
13316 parameter_declarator
13317 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13318 /*parenthesized_p=*/NULL);
13320 if (!parameter_declarator)
13321 return error_mark_node;
13323 /* If the parameter declaration is marked as a parameter pack, set
13324 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13325 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13326 grokdeclarator. */
13327 if (parameter_declarator->declarator
13328 && parameter_declarator->declarator->parameter_pack_p)
13330 *is_parameter_pack = true;
13331 parameter_declarator->declarator->parameter_pack_p = false;
13334 if (parameter_declarator->default_argument)
13336 /* Can happen in some cases of erroneous input (c++/34892). */
13337 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13338 /* Consume the `...' for better error recovery. */
13339 cp_lexer_consume_token (parser->lexer);
13341 /* If the next token is an ellipsis, and we don't already have it
13342 marked as a parameter pack, then we have a parameter pack (that
13343 has no declarator). */
13344 else if (!*is_parameter_pack
13345 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13346 && (declarator_can_be_parameter_pack
13347 (parameter_declarator->declarator)))
13349 /* Consume the `...'. */
13350 cp_lexer_consume_token (parser->lexer);
13351 maybe_warn_variadic_templates ();
13353 *is_parameter_pack = true;
13355 /* We might end up with a pack expansion as the type of the non-type
13356 template parameter, in which case this is a non-type template
13357 parameter pack. */
13358 else if (parameter_declarator->decl_specifiers.type
13359 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13361 *is_parameter_pack = true;
13362 parameter_declarator->decl_specifiers.type =
13363 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13366 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13368 /* Parameter packs cannot have default arguments. However, a
13369 user may try to do so, so we'll parse them and give an
13370 appropriate diagnostic here. */
13372 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13374 /* Find the name of the parameter pack. */
13375 id_declarator = parameter_declarator->declarator;
13376 while (id_declarator && id_declarator->kind != cdk_id)
13377 id_declarator = id_declarator->declarator;
13379 if (id_declarator && id_declarator->kind == cdk_id)
13380 error_at (start_token->location,
13381 "template parameter pack %qD cannot have a default argument",
13382 id_declarator->u.id.unqualified_name);
13383 else
13384 error_at (start_token->location,
13385 "template parameter pack cannot have a default argument");
13387 /* Parse the default argument, but throw away the result. */
13388 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13391 parm = grokdeclarator (parameter_declarator->declarator,
13392 &parameter_declarator->decl_specifiers,
13393 TPARM, /*initialized=*/0,
13394 /*attrlist=*/NULL);
13395 if (parm == error_mark_node)
13396 return error_mark_node;
13398 return build_tree_list (parameter_declarator->default_argument, parm);
13401 /* Parse a type-parameter.
13403 type-parameter:
13404 class identifier [opt]
13405 class identifier [opt] = type-id
13406 typename identifier [opt]
13407 typename identifier [opt] = type-id
13408 template < template-parameter-list > class identifier [opt]
13409 template < template-parameter-list > class identifier [opt]
13410 = id-expression
13412 GNU Extension (variadic templates):
13414 type-parameter:
13415 class ... identifier [opt]
13416 typename ... identifier [opt]
13418 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13419 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13420 the declaration of the parameter.
13422 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13424 static tree
13425 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13427 cp_token *token;
13428 tree parameter;
13430 /* Look for a keyword to tell us what kind of parameter this is. */
13431 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13432 if (!token)
13433 return error_mark_node;
13435 switch (token->keyword)
13437 case RID_CLASS:
13438 case RID_TYPENAME:
13440 tree identifier;
13441 tree default_argument;
13443 /* If the next token is an ellipsis, we have a template
13444 argument pack. */
13445 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13447 /* Consume the `...' token. */
13448 cp_lexer_consume_token (parser->lexer);
13449 maybe_warn_variadic_templates ();
13451 *is_parameter_pack = true;
13454 /* If the next token is an identifier, then it names the
13455 parameter. */
13456 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13457 identifier = cp_parser_identifier (parser);
13458 else
13459 identifier = NULL_TREE;
13461 /* Create the parameter. */
13462 parameter = finish_template_type_parm (class_type_node, identifier);
13464 /* If the next token is an `=', we have a default argument. */
13465 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13467 /* Consume the `=' token. */
13468 cp_lexer_consume_token (parser->lexer);
13469 /* Parse the default-argument. */
13470 push_deferring_access_checks (dk_no_deferred);
13471 default_argument = cp_parser_type_id (parser);
13473 /* Template parameter packs cannot have default
13474 arguments. */
13475 if (*is_parameter_pack)
13477 if (identifier)
13478 error_at (token->location,
13479 "template parameter pack %qD cannot have a "
13480 "default argument", identifier);
13481 else
13482 error_at (token->location,
13483 "template parameter packs cannot have "
13484 "default arguments");
13485 default_argument = NULL_TREE;
13487 pop_deferring_access_checks ();
13489 else
13490 default_argument = NULL_TREE;
13492 /* Create the combined representation of the parameter and the
13493 default argument. */
13494 parameter = build_tree_list (default_argument, parameter);
13496 break;
13498 case RID_TEMPLATE:
13500 tree identifier;
13501 tree default_argument;
13503 /* Look for the `<'. */
13504 cp_parser_require (parser, CPP_LESS, RT_LESS);
13505 /* Parse the template-parameter-list. */
13506 cp_parser_template_parameter_list (parser);
13507 /* Look for the `>'. */
13508 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13509 /* Look for the `class' or 'typename' keywords. */
13510 cp_parser_type_parameter_key (parser);
13511 /* If the next token is an ellipsis, we have a template
13512 argument pack. */
13513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13515 /* Consume the `...' token. */
13516 cp_lexer_consume_token (parser->lexer);
13517 maybe_warn_variadic_templates ();
13519 *is_parameter_pack = true;
13521 /* If the next token is an `=', then there is a
13522 default-argument. If the next token is a `>', we are at
13523 the end of the parameter-list. If the next token is a `,',
13524 then we are at the end of this parameter. */
13525 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13526 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13527 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13529 identifier = cp_parser_identifier (parser);
13530 /* Treat invalid names as if the parameter were nameless. */
13531 if (identifier == error_mark_node)
13532 identifier = NULL_TREE;
13534 else
13535 identifier = NULL_TREE;
13537 /* Create the template parameter. */
13538 parameter = finish_template_template_parm (class_type_node,
13539 identifier);
13541 /* If the next token is an `=', then there is a
13542 default-argument. */
13543 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13545 bool is_template;
13547 /* Consume the `='. */
13548 cp_lexer_consume_token (parser->lexer);
13549 /* Parse the id-expression. */
13550 push_deferring_access_checks (dk_no_deferred);
13551 /* save token before parsing the id-expression, for error
13552 reporting */
13553 token = cp_lexer_peek_token (parser->lexer);
13554 default_argument
13555 = cp_parser_id_expression (parser,
13556 /*template_keyword_p=*/false,
13557 /*check_dependency_p=*/true,
13558 /*template_p=*/&is_template,
13559 /*declarator_p=*/false,
13560 /*optional_p=*/false);
13561 if (TREE_CODE (default_argument) == TYPE_DECL)
13562 /* If the id-expression was a template-id that refers to
13563 a template-class, we already have the declaration here,
13564 so no further lookup is needed. */
13566 else
13567 /* Look up the name. */
13568 default_argument
13569 = cp_parser_lookup_name (parser, default_argument,
13570 none_type,
13571 /*is_template=*/is_template,
13572 /*is_namespace=*/false,
13573 /*check_dependency=*/true,
13574 /*ambiguous_decls=*/NULL,
13575 token->location);
13576 /* See if the default argument is valid. */
13577 default_argument
13578 = check_template_template_default_arg (default_argument);
13580 /* Template parameter packs cannot have default
13581 arguments. */
13582 if (*is_parameter_pack)
13584 if (identifier)
13585 error_at (token->location,
13586 "template parameter pack %qD cannot "
13587 "have a default argument",
13588 identifier);
13589 else
13590 error_at (token->location, "template parameter packs cannot "
13591 "have default arguments");
13592 default_argument = NULL_TREE;
13594 pop_deferring_access_checks ();
13596 else
13597 default_argument = NULL_TREE;
13599 /* Create the combined representation of the parameter and the
13600 default argument. */
13601 parameter = build_tree_list (default_argument, parameter);
13603 break;
13605 default:
13606 gcc_unreachable ();
13607 break;
13610 return parameter;
13613 /* Parse a template-id.
13615 template-id:
13616 template-name < template-argument-list [opt] >
13618 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13619 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13620 returned. Otherwise, if the template-name names a function, or set
13621 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13622 names a class, returns a TYPE_DECL for the specialization.
13624 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13625 uninstantiated templates. */
13627 static tree
13628 cp_parser_template_id (cp_parser *parser,
13629 bool template_keyword_p,
13630 bool check_dependency_p,
13631 enum tag_types tag_type,
13632 bool is_declaration)
13634 int i;
13635 tree templ;
13636 tree arguments;
13637 tree template_id;
13638 cp_token_position start_of_id = 0;
13639 deferred_access_check *chk;
13640 vec<deferred_access_check, va_gc> *access_check;
13641 cp_token *next_token = NULL, *next_token_2 = NULL;
13642 bool is_identifier;
13644 /* If the next token corresponds to a template-id, there is no need
13645 to reparse it. */
13646 next_token = cp_lexer_peek_token (parser->lexer);
13647 if (next_token->type == CPP_TEMPLATE_ID)
13649 struct tree_check *check_value;
13651 /* Get the stored value. */
13652 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13653 /* Perform any access checks that were deferred. */
13654 access_check = check_value->checks;
13655 if (access_check)
13657 FOR_EACH_VEC_ELT (*access_check, i, chk)
13658 perform_or_defer_access_check (chk->binfo,
13659 chk->decl,
13660 chk->diag_decl,
13661 tf_warning_or_error);
13663 /* Return the stored value. */
13664 return check_value->value;
13667 /* Avoid performing name lookup if there is no possibility of
13668 finding a template-id. */
13669 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13670 || (next_token->type == CPP_NAME
13671 && !cp_parser_nth_token_starts_template_argument_list_p
13672 (parser, 2)))
13674 cp_parser_error (parser, "expected template-id");
13675 return error_mark_node;
13678 /* Remember where the template-id starts. */
13679 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13680 start_of_id = cp_lexer_token_position (parser->lexer, false);
13682 push_deferring_access_checks (dk_deferred);
13684 /* Parse the template-name. */
13685 is_identifier = false;
13686 templ = cp_parser_template_name (parser, template_keyword_p,
13687 check_dependency_p,
13688 is_declaration,
13689 tag_type,
13690 &is_identifier);
13691 if (templ == error_mark_node || is_identifier)
13693 pop_deferring_access_checks ();
13694 return templ;
13697 /* If we find the sequence `[:' after a template-name, it's probably
13698 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13699 parse correctly the argument list. */
13700 next_token = cp_lexer_peek_token (parser->lexer);
13701 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13702 if (next_token->type == CPP_OPEN_SQUARE
13703 && next_token->flags & DIGRAPH
13704 && next_token_2->type == CPP_COLON
13705 && !(next_token_2->flags & PREV_WHITE))
13707 cp_parser_parse_tentatively (parser);
13708 /* Change `:' into `::'. */
13709 next_token_2->type = CPP_SCOPE;
13710 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13711 CPP_LESS. */
13712 cp_lexer_consume_token (parser->lexer);
13714 /* Parse the arguments. */
13715 arguments = cp_parser_enclosed_template_argument_list (parser);
13716 if (!cp_parser_parse_definitely (parser))
13718 /* If we couldn't parse an argument list, then we revert our changes
13719 and return simply an error. Maybe this is not a template-id
13720 after all. */
13721 next_token_2->type = CPP_COLON;
13722 cp_parser_error (parser, "expected %<<%>");
13723 pop_deferring_access_checks ();
13724 return error_mark_node;
13726 /* Otherwise, emit an error about the invalid digraph, but continue
13727 parsing because we got our argument list. */
13728 if (permerror (next_token->location,
13729 "%<<::%> cannot begin a template-argument list"))
13731 static bool hint = false;
13732 inform (next_token->location,
13733 "%<<:%> is an alternate spelling for %<[%>."
13734 " Insert whitespace between %<<%> and %<::%>");
13735 if (!hint && !flag_permissive)
13737 inform (next_token->location, "(if you use %<-fpermissive%> "
13738 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13739 "accept your code)");
13740 hint = true;
13744 else
13746 /* Look for the `<' that starts the template-argument-list. */
13747 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13749 pop_deferring_access_checks ();
13750 return error_mark_node;
13752 /* Parse the arguments. */
13753 arguments = cp_parser_enclosed_template_argument_list (parser);
13756 /* Build a representation of the specialization. */
13757 if (identifier_p (templ))
13758 template_id = build_min_nt_loc (next_token->location,
13759 TEMPLATE_ID_EXPR,
13760 templ, arguments);
13761 else if (DECL_TYPE_TEMPLATE_P (templ)
13762 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13764 bool entering_scope;
13765 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13766 template (rather than some instantiation thereof) only if
13767 is not nested within some other construct. For example, in
13768 "template <typename T> void f(T) { A<T>::", A<T> is just an
13769 instantiation of A. */
13770 entering_scope = (template_parm_scope_p ()
13771 && cp_lexer_next_token_is (parser->lexer,
13772 CPP_SCOPE));
13773 template_id
13774 = finish_template_type (templ, arguments, entering_scope);
13776 else if (variable_template_p (templ))
13778 template_id = lookup_template_variable (templ, arguments);
13780 else
13782 /* If it's not a class-template or a template-template, it should be
13783 a function-template. */
13784 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13785 || TREE_CODE (templ) == OVERLOAD
13786 || BASELINK_P (templ)));
13788 template_id = lookup_template_function (templ, arguments);
13791 /* If parsing tentatively, replace the sequence of tokens that makes
13792 up the template-id with a CPP_TEMPLATE_ID token. That way,
13793 should we re-parse the token stream, we will not have to repeat
13794 the effort required to do the parse, nor will we issue duplicate
13795 error messages about problems during instantiation of the
13796 template. */
13797 if (start_of_id
13798 /* Don't do this if we had a parse error in a declarator; re-parsing
13799 might succeed if a name changes meaning (60361). */
13800 && !(cp_parser_error_occurred (parser)
13801 && cp_parser_parsing_tentatively (parser)
13802 && parser->in_declarator_p))
13804 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13806 /* Reset the contents of the START_OF_ID token. */
13807 token->type = CPP_TEMPLATE_ID;
13808 /* Retrieve any deferred checks. Do not pop this access checks yet
13809 so the memory will not be reclaimed during token replacing below. */
13810 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13811 token->u.tree_check_value->value = template_id;
13812 token->u.tree_check_value->checks = get_deferred_access_checks ();
13813 token->keyword = RID_MAX;
13815 /* Purge all subsequent tokens. */
13816 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13818 /* ??? Can we actually assume that, if template_id ==
13819 error_mark_node, we will have issued a diagnostic to the
13820 user, as opposed to simply marking the tentative parse as
13821 failed? */
13822 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13823 error_at (token->location, "parse error in template argument list");
13826 pop_to_parent_deferring_access_checks ();
13827 return template_id;
13830 /* Parse a template-name.
13832 template-name:
13833 identifier
13835 The standard should actually say:
13837 template-name:
13838 identifier
13839 operator-function-id
13841 A defect report has been filed about this issue.
13843 A conversion-function-id cannot be a template name because they cannot
13844 be part of a template-id. In fact, looking at this code:
13846 a.operator K<int>()
13848 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13849 It is impossible to call a templated conversion-function-id with an
13850 explicit argument list, since the only allowed template parameter is
13851 the type to which it is converting.
13853 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13854 `template' keyword, in a construction like:
13856 T::template f<3>()
13858 In that case `f' is taken to be a template-name, even though there
13859 is no way of knowing for sure.
13861 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13862 name refers to a set of overloaded functions, at least one of which
13863 is a template, or an IDENTIFIER_NODE with the name of the template,
13864 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13865 names are looked up inside uninstantiated templates. */
13867 static tree
13868 cp_parser_template_name (cp_parser* parser,
13869 bool template_keyword_p,
13870 bool check_dependency_p,
13871 bool is_declaration,
13872 enum tag_types tag_type,
13873 bool *is_identifier)
13875 tree identifier;
13876 tree decl;
13877 tree fns;
13878 cp_token *token = cp_lexer_peek_token (parser->lexer);
13880 /* If the next token is `operator', then we have either an
13881 operator-function-id or a conversion-function-id. */
13882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13884 /* We don't know whether we're looking at an
13885 operator-function-id or a conversion-function-id. */
13886 cp_parser_parse_tentatively (parser);
13887 /* Try an operator-function-id. */
13888 identifier = cp_parser_operator_function_id (parser);
13889 /* If that didn't work, try a conversion-function-id. */
13890 if (!cp_parser_parse_definitely (parser))
13892 cp_parser_error (parser, "expected template-name");
13893 return error_mark_node;
13896 /* Look for the identifier. */
13897 else
13898 identifier = cp_parser_identifier (parser);
13900 /* If we didn't find an identifier, we don't have a template-id. */
13901 if (identifier == error_mark_node)
13902 return error_mark_node;
13904 /* If the name immediately followed the `template' keyword, then it
13905 is a template-name. However, if the next token is not `<', then
13906 we do not treat it as a template-name, since it is not being used
13907 as part of a template-id. This enables us to handle constructs
13908 like:
13910 template <typename T> struct S { S(); };
13911 template <typename T> S<T>::S();
13913 correctly. We would treat `S' as a template -- if it were `S<T>'
13914 -- but we do not if there is no `<'. */
13916 if (processing_template_decl
13917 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13919 /* In a declaration, in a dependent context, we pretend that the
13920 "template" keyword was present in order to improve error
13921 recovery. For example, given:
13923 template <typename T> void f(T::X<int>);
13925 we want to treat "X<int>" as a template-id. */
13926 if (is_declaration
13927 && !template_keyword_p
13928 && parser->scope && TYPE_P (parser->scope)
13929 && check_dependency_p
13930 && dependent_scope_p (parser->scope)
13931 /* Do not do this for dtors (or ctors), since they never
13932 need the template keyword before their name. */
13933 && !constructor_name_p (identifier, parser->scope))
13935 cp_token_position start = 0;
13937 /* Explain what went wrong. */
13938 error_at (token->location, "non-template %qD used as template",
13939 identifier);
13940 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13941 parser->scope, identifier);
13942 /* If parsing tentatively, find the location of the "<" token. */
13943 if (cp_parser_simulate_error (parser))
13944 start = cp_lexer_token_position (parser->lexer, true);
13945 /* Parse the template arguments so that we can issue error
13946 messages about them. */
13947 cp_lexer_consume_token (parser->lexer);
13948 cp_parser_enclosed_template_argument_list (parser);
13949 /* Skip tokens until we find a good place from which to
13950 continue parsing. */
13951 cp_parser_skip_to_closing_parenthesis (parser,
13952 /*recovering=*/true,
13953 /*or_comma=*/true,
13954 /*consume_paren=*/false);
13955 /* If parsing tentatively, permanently remove the
13956 template argument list. That will prevent duplicate
13957 error messages from being issued about the missing
13958 "template" keyword. */
13959 if (start)
13960 cp_lexer_purge_tokens_after (parser->lexer, start);
13961 if (is_identifier)
13962 *is_identifier = true;
13963 return identifier;
13966 /* If the "template" keyword is present, then there is generally
13967 no point in doing name-lookup, so we just return IDENTIFIER.
13968 But, if the qualifying scope is non-dependent then we can
13969 (and must) do name-lookup normally. */
13970 if (template_keyword_p
13971 && (!parser->scope
13972 || (TYPE_P (parser->scope)
13973 && dependent_type_p (parser->scope))))
13974 return identifier;
13977 /* Look up the name. */
13978 decl = cp_parser_lookup_name (parser, identifier,
13979 tag_type,
13980 /*is_template=*/true,
13981 /*is_namespace=*/false,
13982 check_dependency_p,
13983 /*ambiguous_decls=*/NULL,
13984 token->location);
13986 /* If DECL is a template, then the name was a template-name. */
13987 if (TREE_CODE (decl) == TEMPLATE_DECL)
13989 if (TREE_DEPRECATED (decl)
13990 && deprecated_state != DEPRECATED_SUPPRESS)
13991 warn_deprecated_use (decl, NULL_TREE);
13993 else
13995 tree fn = NULL_TREE;
13997 /* The standard does not explicitly indicate whether a name that
13998 names a set of overloaded declarations, some of which are
13999 templates, is a template-name. However, such a name should
14000 be a template-name; otherwise, there is no way to form a
14001 template-id for the overloaded templates. */
14002 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14003 if (TREE_CODE (fns) == OVERLOAD)
14004 for (fn = fns; fn; fn = OVL_NEXT (fn))
14005 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14006 break;
14008 if (!fn)
14010 /* The name does not name a template. */
14011 cp_parser_error (parser, "expected template-name");
14012 return error_mark_node;
14016 /* If DECL is dependent, and refers to a function, then just return
14017 its name; we will look it up again during template instantiation. */
14018 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14020 tree scope = ovl_scope (decl);
14021 if (TYPE_P (scope) && dependent_type_p (scope))
14022 return identifier;
14025 return decl;
14028 /* Parse a template-argument-list.
14030 template-argument-list:
14031 template-argument ... [opt]
14032 template-argument-list , template-argument ... [opt]
14034 Returns a TREE_VEC containing the arguments. */
14036 static tree
14037 cp_parser_template_argument_list (cp_parser* parser)
14039 tree fixed_args[10];
14040 unsigned n_args = 0;
14041 unsigned alloced = 10;
14042 tree *arg_ary = fixed_args;
14043 tree vec;
14044 bool saved_in_template_argument_list_p;
14045 bool saved_ice_p;
14046 bool saved_non_ice_p;
14048 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14049 parser->in_template_argument_list_p = true;
14050 /* Even if the template-id appears in an integral
14051 constant-expression, the contents of the argument list do
14052 not. */
14053 saved_ice_p = parser->integral_constant_expression_p;
14054 parser->integral_constant_expression_p = false;
14055 saved_non_ice_p = parser->non_integral_constant_expression_p;
14056 parser->non_integral_constant_expression_p = false;
14058 /* Parse the arguments. */
14061 tree argument;
14063 if (n_args)
14064 /* Consume the comma. */
14065 cp_lexer_consume_token (parser->lexer);
14067 /* Parse the template-argument. */
14068 argument = cp_parser_template_argument (parser);
14070 /* If the next token is an ellipsis, we're expanding a template
14071 argument pack. */
14072 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14074 if (argument == error_mark_node)
14076 cp_token *token = cp_lexer_peek_token (parser->lexer);
14077 error_at (token->location,
14078 "expected parameter pack before %<...%>");
14080 /* Consume the `...' token. */
14081 cp_lexer_consume_token (parser->lexer);
14083 /* Make the argument into a TYPE_PACK_EXPANSION or
14084 EXPR_PACK_EXPANSION. */
14085 argument = make_pack_expansion (argument);
14088 if (n_args == alloced)
14090 alloced *= 2;
14092 if (arg_ary == fixed_args)
14094 arg_ary = XNEWVEC (tree, alloced);
14095 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14097 else
14098 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14100 arg_ary[n_args++] = argument;
14102 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14104 vec = make_tree_vec (n_args);
14106 while (n_args--)
14107 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14109 if (arg_ary != fixed_args)
14110 free (arg_ary);
14111 parser->non_integral_constant_expression_p = saved_non_ice_p;
14112 parser->integral_constant_expression_p = saved_ice_p;
14113 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14114 #ifdef ENABLE_CHECKING
14115 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14116 #endif
14117 return vec;
14120 /* Parse a template-argument.
14122 template-argument:
14123 assignment-expression
14124 type-id
14125 id-expression
14127 The representation is that of an assignment-expression, type-id, or
14128 id-expression -- except that the qualified id-expression is
14129 evaluated, so that the value returned is either a DECL or an
14130 OVERLOAD.
14132 Although the standard says "assignment-expression", it forbids
14133 throw-expressions or assignments in the template argument.
14134 Therefore, we use "conditional-expression" instead. */
14136 static tree
14137 cp_parser_template_argument (cp_parser* parser)
14139 tree argument;
14140 bool template_p;
14141 bool address_p;
14142 bool maybe_type_id = false;
14143 cp_token *token = NULL, *argument_start_token = NULL;
14144 location_t loc = 0;
14145 cp_id_kind idk;
14147 /* There's really no way to know what we're looking at, so we just
14148 try each alternative in order.
14150 [temp.arg]
14152 In a template-argument, an ambiguity between a type-id and an
14153 expression is resolved to a type-id, regardless of the form of
14154 the corresponding template-parameter.
14156 Therefore, we try a type-id first. */
14157 cp_parser_parse_tentatively (parser);
14158 argument = cp_parser_template_type_arg (parser);
14159 /* If there was no error parsing the type-id but the next token is a
14160 '>>', our behavior depends on which dialect of C++ we're
14161 parsing. In C++98, we probably found a typo for '> >'. But there
14162 are type-id which are also valid expressions. For instance:
14164 struct X { int operator >> (int); };
14165 template <int V> struct Foo {};
14166 Foo<X () >> 5> r;
14168 Here 'X()' is a valid type-id of a function type, but the user just
14169 wanted to write the expression "X() >> 5". Thus, we remember that we
14170 found a valid type-id, but we still try to parse the argument as an
14171 expression to see what happens.
14173 In C++0x, the '>>' will be considered two separate '>'
14174 tokens. */
14175 if (!cp_parser_error_occurred (parser)
14176 && cxx_dialect == cxx98
14177 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14179 maybe_type_id = true;
14180 cp_parser_abort_tentative_parse (parser);
14182 else
14184 /* If the next token isn't a `,' or a `>', then this argument wasn't
14185 really finished. This means that the argument is not a valid
14186 type-id. */
14187 if (!cp_parser_next_token_ends_template_argument_p (parser))
14188 cp_parser_error (parser, "expected template-argument");
14189 /* If that worked, we're done. */
14190 if (cp_parser_parse_definitely (parser))
14191 return argument;
14193 /* We're still not sure what the argument will be. */
14194 cp_parser_parse_tentatively (parser);
14195 /* Try a template. */
14196 argument_start_token = cp_lexer_peek_token (parser->lexer);
14197 argument = cp_parser_id_expression (parser,
14198 /*template_keyword_p=*/false,
14199 /*check_dependency_p=*/true,
14200 &template_p,
14201 /*declarator_p=*/false,
14202 /*optional_p=*/false);
14203 /* If the next token isn't a `,' or a `>', then this argument wasn't
14204 really finished. */
14205 if (!cp_parser_next_token_ends_template_argument_p (parser))
14206 cp_parser_error (parser, "expected template-argument");
14207 if (!cp_parser_error_occurred (parser))
14209 /* Figure out what is being referred to. If the id-expression
14210 was for a class template specialization, then we will have a
14211 TYPE_DECL at this point. There is no need to do name lookup
14212 at this point in that case. */
14213 if (TREE_CODE (argument) != TYPE_DECL)
14214 argument = cp_parser_lookup_name (parser, argument,
14215 none_type,
14216 /*is_template=*/template_p,
14217 /*is_namespace=*/false,
14218 /*check_dependency=*/true,
14219 /*ambiguous_decls=*/NULL,
14220 argument_start_token->location);
14221 if (TREE_CODE (argument) != TEMPLATE_DECL
14222 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14223 cp_parser_error (parser, "expected template-name");
14225 if (cp_parser_parse_definitely (parser))
14227 if (TREE_DEPRECATED (argument))
14228 warn_deprecated_use (argument, NULL_TREE);
14229 return argument;
14231 /* It must be a non-type argument. There permitted cases are given
14232 in [temp.arg.nontype]:
14234 -- an integral constant-expression of integral or enumeration
14235 type; or
14237 -- the name of a non-type template-parameter; or
14239 -- the name of an object or function with external linkage...
14241 -- the address of an object or function with external linkage...
14243 -- a pointer to member... */
14244 /* Look for a non-type template parameter. */
14245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14247 cp_parser_parse_tentatively (parser);
14248 argument = cp_parser_primary_expression (parser,
14249 /*address_p=*/false,
14250 /*cast_p=*/false,
14251 /*template_arg_p=*/true,
14252 &idk);
14253 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14254 || !cp_parser_next_token_ends_template_argument_p (parser))
14255 cp_parser_simulate_error (parser);
14256 if (cp_parser_parse_definitely (parser))
14257 return argument;
14260 /* If the next token is "&", the argument must be the address of an
14261 object or function with external linkage. */
14262 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14263 if (address_p)
14265 loc = cp_lexer_peek_token (parser->lexer)->location;
14266 cp_lexer_consume_token (parser->lexer);
14268 /* See if we might have an id-expression. */
14269 token = cp_lexer_peek_token (parser->lexer);
14270 if (token->type == CPP_NAME
14271 || token->keyword == RID_OPERATOR
14272 || token->type == CPP_SCOPE
14273 || token->type == CPP_TEMPLATE_ID
14274 || token->type == CPP_NESTED_NAME_SPECIFIER)
14276 cp_parser_parse_tentatively (parser);
14277 argument = cp_parser_primary_expression (parser,
14278 address_p,
14279 /*cast_p=*/false,
14280 /*template_arg_p=*/true,
14281 &idk);
14282 if (cp_parser_error_occurred (parser)
14283 || !cp_parser_next_token_ends_template_argument_p (parser))
14284 cp_parser_abort_tentative_parse (parser);
14285 else
14287 tree probe;
14289 if (INDIRECT_REF_P (argument))
14291 /* Strip the dereference temporarily. */
14292 gcc_assert (REFERENCE_REF_P (argument));
14293 argument = TREE_OPERAND (argument, 0);
14296 /* If we're in a template, we represent a qualified-id referring
14297 to a static data member as a SCOPE_REF even if the scope isn't
14298 dependent so that we can check access control later. */
14299 probe = argument;
14300 if (TREE_CODE (probe) == SCOPE_REF)
14301 probe = TREE_OPERAND (probe, 1);
14302 if (VAR_P (probe))
14304 /* A variable without external linkage might still be a
14305 valid constant-expression, so no error is issued here
14306 if the external-linkage check fails. */
14307 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14308 cp_parser_simulate_error (parser);
14310 else if (is_overloaded_fn (argument))
14311 /* All overloaded functions are allowed; if the external
14312 linkage test does not pass, an error will be issued
14313 later. */
14315 else if (address_p
14316 && (TREE_CODE (argument) == OFFSET_REF
14317 || TREE_CODE (argument) == SCOPE_REF))
14318 /* A pointer-to-member. */
14320 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14322 else
14323 cp_parser_simulate_error (parser);
14325 if (cp_parser_parse_definitely (parser))
14327 if (address_p)
14328 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14329 tf_warning_or_error);
14330 else
14331 argument = convert_from_reference (argument);
14332 return argument;
14336 /* If the argument started with "&", there are no other valid
14337 alternatives at this point. */
14338 if (address_p)
14340 cp_parser_error (parser, "invalid non-type template argument");
14341 return error_mark_node;
14344 /* If the argument wasn't successfully parsed as a type-id followed
14345 by '>>', the argument can only be a constant expression now.
14346 Otherwise, we try parsing the constant-expression tentatively,
14347 because the argument could really be a type-id. */
14348 if (maybe_type_id)
14349 cp_parser_parse_tentatively (parser);
14350 argument = cp_parser_constant_expression (parser);
14352 if (!maybe_type_id)
14353 return argument;
14354 if (!cp_parser_next_token_ends_template_argument_p (parser))
14355 cp_parser_error (parser, "expected template-argument");
14356 if (cp_parser_parse_definitely (parser))
14357 return argument;
14358 /* We did our best to parse the argument as a non type-id, but that
14359 was the only alternative that matched (albeit with a '>' after
14360 it). We can assume it's just a typo from the user, and a
14361 diagnostic will then be issued. */
14362 return cp_parser_template_type_arg (parser);
14365 /* Parse an explicit-instantiation.
14367 explicit-instantiation:
14368 template declaration
14370 Although the standard says `declaration', what it really means is:
14372 explicit-instantiation:
14373 template decl-specifier-seq [opt] declarator [opt] ;
14375 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14376 supposed to be allowed. A defect report has been filed about this
14377 issue.
14379 GNU Extension:
14381 explicit-instantiation:
14382 storage-class-specifier template
14383 decl-specifier-seq [opt] declarator [opt] ;
14384 function-specifier template
14385 decl-specifier-seq [opt] declarator [opt] ; */
14387 static void
14388 cp_parser_explicit_instantiation (cp_parser* parser)
14390 int declares_class_or_enum;
14391 cp_decl_specifier_seq decl_specifiers;
14392 tree extension_specifier = NULL_TREE;
14394 timevar_push (TV_TEMPLATE_INST);
14396 /* Look for an (optional) storage-class-specifier or
14397 function-specifier. */
14398 if (cp_parser_allow_gnu_extensions_p (parser))
14400 extension_specifier
14401 = cp_parser_storage_class_specifier_opt (parser);
14402 if (!extension_specifier)
14403 extension_specifier
14404 = cp_parser_function_specifier_opt (parser,
14405 /*decl_specs=*/NULL);
14408 /* Look for the `template' keyword. */
14409 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14410 /* Let the front end know that we are processing an explicit
14411 instantiation. */
14412 begin_explicit_instantiation ();
14413 /* [temp.explicit] says that we are supposed to ignore access
14414 control while processing explicit instantiation directives. */
14415 push_deferring_access_checks (dk_no_check);
14416 /* Parse a decl-specifier-seq. */
14417 cp_parser_decl_specifier_seq (parser,
14418 CP_PARSER_FLAGS_OPTIONAL,
14419 &decl_specifiers,
14420 &declares_class_or_enum);
14421 /* If there was exactly one decl-specifier, and it declared a class,
14422 and there's no declarator, then we have an explicit type
14423 instantiation. */
14424 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14426 tree type;
14428 type = check_tag_decl (&decl_specifiers,
14429 /*explicit_type_instantiation_p=*/true);
14430 /* Turn access control back on for names used during
14431 template instantiation. */
14432 pop_deferring_access_checks ();
14433 if (type)
14434 do_type_instantiation (type, extension_specifier,
14435 /*complain=*/tf_error);
14437 else
14439 cp_declarator *declarator;
14440 tree decl;
14442 /* Parse the declarator. */
14443 declarator
14444 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14445 /*ctor_dtor_or_conv_p=*/NULL,
14446 /*parenthesized_p=*/NULL,
14447 /*member_p=*/false,
14448 /*friend_p=*/false);
14449 if (declares_class_or_enum & 2)
14450 cp_parser_check_for_definition_in_return_type (declarator,
14451 decl_specifiers.type,
14452 decl_specifiers.locations[ds_type_spec]);
14453 if (declarator != cp_error_declarator)
14455 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14456 permerror (decl_specifiers.locations[ds_inline],
14457 "explicit instantiation shall not use"
14458 " %<inline%> specifier");
14459 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14460 permerror (decl_specifiers.locations[ds_constexpr],
14461 "explicit instantiation shall not use"
14462 " %<constexpr%> specifier");
14464 decl = grokdeclarator (declarator, &decl_specifiers,
14465 NORMAL, 0, &decl_specifiers.attributes);
14466 /* Turn access control back on for names used during
14467 template instantiation. */
14468 pop_deferring_access_checks ();
14469 /* Do the explicit instantiation. */
14470 do_decl_instantiation (decl, extension_specifier);
14472 else
14474 pop_deferring_access_checks ();
14475 /* Skip the body of the explicit instantiation. */
14476 cp_parser_skip_to_end_of_statement (parser);
14479 /* We're done with the instantiation. */
14480 end_explicit_instantiation ();
14482 cp_parser_consume_semicolon_at_end_of_statement (parser);
14484 timevar_pop (TV_TEMPLATE_INST);
14487 /* Parse an explicit-specialization.
14489 explicit-specialization:
14490 template < > declaration
14492 Although the standard says `declaration', what it really means is:
14494 explicit-specialization:
14495 template <> decl-specifier [opt] init-declarator [opt] ;
14496 template <> function-definition
14497 template <> explicit-specialization
14498 template <> template-declaration */
14500 static void
14501 cp_parser_explicit_specialization (cp_parser* parser)
14503 bool need_lang_pop;
14504 cp_token *token = cp_lexer_peek_token (parser->lexer);
14506 /* Look for the `template' keyword. */
14507 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14508 /* Look for the `<'. */
14509 cp_parser_require (parser, CPP_LESS, RT_LESS);
14510 /* Look for the `>'. */
14511 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14512 /* We have processed another parameter list. */
14513 ++parser->num_template_parameter_lists;
14514 /* [temp]
14516 A template ... explicit specialization ... shall not have C
14517 linkage. */
14518 if (current_lang_name == lang_name_c)
14520 error_at (token->location, "template specialization with C linkage");
14521 /* Give it C++ linkage to avoid confusing other parts of the
14522 front end. */
14523 push_lang_context (lang_name_cplusplus);
14524 need_lang_pop = true;
14526 else
14527 need_lang_pop = false;
14528 /* Let the front end know that we are beginning a specialization. */
14529 if (!begin_specialization ())
14531 end_specialization ();
14532 return;
14535 /* If the next keyword is `template', we need to figure out whether
14536 or not we're looking a template-declaration. */
14537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14539 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14540 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14541 cp_parser_template_declaration_after_export (parser,
14542 /*member_p=*/false);
14543 else
14544 cp_parser_explicit_specialization (parser);
14546 else
14547 /* Parse the dependent declaration. */
14548 cp_parser_single_declaration (parser,
14549 /*checks=*/NULL,
14550 /*member_p=*/false,
14551 /*explicit_specialization_p=*/true,
14552 /*friend_p=*/NULL);
14553 /* We're done with the specialization. */
14554 end_specialization ();
14555 /* For the erroneous case of a template with C linkage, we pushed an
14556 implicit C++ linkage scope; exit that scope now. */
14557 if (need_lang_pop)
14558 pop_lang_context ();
14559 /* We're done with this parameter list. */
14560 --parser->num_template_parameter_lists;
14563 /* Parse a type-specifier.
14565 type-specifier:
14566 simple-type-specifier
14567 class-specifier
14568 enum-specifier
14569 elaborated-type-specifier
14570 cv-qualifier
14572 GNU Extension:
14574 type-specifier:
14575 __complex__
14577 Returns a representation of the type-specifier. For a
14578 class-specifier, enum-specifier, or elaborated-type-specifier, a
14579 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14581 The parser flags FLAGS is used to control type-specifier parsing.
14583 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14584 in a decl-specifier-seq.
14586 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14587 class-specifier, enum-specifier, or elaborated-type-specifier, then
14588 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14589 if a type is declared; 2 if it is defined. Otherwise, it is set to
14590 zero.
14592 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14593 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14594 is set to FALSE. */
14596 static tree
14597 cp_parser_type_specifier (cp_parser* parser,
14598 cp_parser_flags flags,
14599 cp_decl_specifier_seq *decl_specs,
14600 bool is_declaration,
14601 int* declares_class_or_enum,
14602 bool* is_cv_qualifier)
14604 tree type_spec = NULL_TREE;
14605 cp_token *token;
14606 enum rid keyword;
14607 cp_decl_spec ds = ds_last;
14609 /* Assume this type-specifier does not declare a new type. */
14610 if (declares_class_or_enum)
14611 *declares_class_or_enum = 0;
14612 /* And that it does not specify a cv-qualifier. */
14613 if (is_cv_qualifier)
14614 *is_cv_qualifier = false;
14615 /* Peek at the next token. */
14616 token = cp_lexer_peek_token (parser->lexer);
14618 /* If we're looking at a keyword, we can use that to guide the
14619 production we choose. */
14620 keyword = token->keyword;
14621 switch (keyword)
14623 case RID_ENUM:
14624 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14625 goto elaborated_type_specifier;
14627 /* Look for the enum-specifier. */
14628 type_spec = cp_parser_enum_specifier (parser);
14629 /* If that worked, we're done. */
14630 if (type_spec)
14632 if (declares_class_or_enum)
14633 *declares_class_or_enum = 2;
14634 if (decl_specs)
14635 cp_parser_set_decl_spec_type (decl_specs,
14636 type_spec,
14637 token,
14638 /*type_definition_p=*/true);
14639 return type_spec;
14641 else
14642 goto elaborated_type_specifier;
14644 /* Any of these indicate either a class-specifier, or an
14645 elaborated-type-specifier. */
14646 case RID_CLASS:
14647 case RID_STRUCT:
14648 case RID_UNION:
14649 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14650 goto elaborated_type_specifier;
14652 /* Parse tentatively so that we can back up if we don't find a
14653 class-specifier. */
14654 cp_parser_parse_tentatively (parser);
14655 /* Look for the class-specifier. */
14656 type_spec = cp_parser_class_specifier (parser);
14657 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14658 /* If that worked, we're done. */
14659 if (cp_parser_parse_definitely (parser))
14661 if (declares_class_or_enum)
14662 *declares_class_or_enum = 2;
14663 if (decl_specs)
14664 cp_parser_set_decl_spec_type (decl_specs,
14665 type_spec,
14666 token,
14667 /*type_definition_p=*/true);
14668 return type_spec;
14671 /* Fall through. */
14672 elaborated_type_specifier:
14673 /* We're declaring (not defining) a class or enum. */
14674 if (declares_class_or_enum)
14675 *declares_class_or_enum = 1;
14677 /* Fall through. */
14678 case RID_TYPENAME:
14679 /* Look for an elaborated-type-specifier. */
14680 type_spec
14681 = (cp_parser_elaborated_type_specifier
14682 (parser,
14683 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14684 is_declaration));
14685 if (decl_specs)
14686 cp_parser_set_decl_spec_type (decl_specs,
14687 type_spec,
14688 token,
14689 /*type_definition_p=*/false);
14690 return type_spec;
14692 case RID_CONST:
14693 ds = ds_const;
14694 if (is_cv_qualifier)
14695 *is_cv_qualifier = true;
14696 break;
14698 case RID_VOLATILE:
14699 ds = ds_volatile;
14700 if (is_cv_qualifier)
14701 *is_cv_qualifier = true;
14702 break;
14704 case RID_RESTRICT:
14705 ds = ds_restrict;
14706 if (is_cv_qualifier)
14707 *is_cv_qualifier = true;
14708 break;
14710 case RID_COMPLEX:
14711 /* The `__complex__' keyword is a GNU extension. */
14712 ds = ds_complex;
14713 break;
14715 default:
14716 break;
14719 /* Handle simple keywords. */
14720 if (ds != ds_last)
14722 if (decl_specs)
14724 set_and_check_decl_spec_loc (decl_specs, ds, token);
14725 decl_specs->any_specifiers_p = true;
14727 return cp_lexer_consume_token (parser->lexer)->u.value;
14730 /* If we do not already have a type-specifier, assume we are looking
14731 at a simple-type-specifier. */
14732 type_spec = cp_parser_simple_type_specifier (parser,
14733 decl_specs,
14734 flags);
14736 /* If we didn't find a type-specifier, and a type-specifier was not
14737 optional in this context, issue an error message. */
14738 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14740 cp_parser_error (parser, "expected type specifier");
14741 return error_mark_node;
14744 return type_spec;
14747 /* Parse a simple-type-specifier.
14749 simple-type-specifier:
14750 :: [opt] nested-name-specifier [opt] type-name
14751 :: [opt] nested-name-specifier template template-id
14752 char
14753 wchar_t
14754 bool
14755 short
14757 long
14758 signed
14759 unsigned
14760 float
14761 double
14762 void
14764 C++0x Extension:
14766 simple-type-specifier:
14767 auto
14768 decltype ( expression )
14769 char16_t
14770 char32_t
14771 __underlying_type ( type-id )
14773 GNU Extension:
14775 simple-type-specifier:
14776 __int128
14777 __typeof__ unary-expression
14778 __typeof__ ( type-id )
14779 __typeof__ ( type-id ) { initializer-list , [opt] }
14781 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14782 appropriately updated. */
14784 static tree
14785 cp_parser_simple_type_specifier (cp_parser* parser,
14786 cp_decl_specifier_seq *decl_specs,
14787 cp_parser_flags flags)
14789 tree type = NULL_TREE;
14790 cp_token *token;
14791 int idx;
14793 /* Peek at the next token. */
14794 token = cp_lexer_peek_token (parser->lexer);
14796 /* If we're looking at a keyword, things are easy. */
14797 switch (token->keyword)
14799 case RID_CHAR:
14800 if (decl_specs)
14801 decl_specs->explicit_char_p = true;
14802 type = char_type_node;
14803 break;
14804 case RID_CHAR16:
14805 type = char16_type_node;
14806 break;
14807 case RID_CHAR32:
14808 type = char32_type_node;
14809 break;
14810 case RID_WCHAR:
14811 type = wchar_type_node;
14812 break;
14813 case RID_BOOL:
14814 type = boolean_type_node;
14815 break;
14816 case RID_SHORT:
14817 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14818 type = short_integer_type_node;
14819 break;
14820 case RID_INT:
14821 if (decl_specs)
14822 decl_specs->explicit_int_p = true;
14823 type = integer_type_node;
14824 break;
14825 case RID_INT_N_0:
14826 case RID_INT_N_1:
14827 case RID_INT_N_2:
14828 case RID_INT_N_3:
14829 idx = token->keyword - RID_INT_N_0;
14830 if (! int_n_enabled_p [idx])
14831 break;
14832 if (decl_specs)
14834 decl_specs->explicit_intN_p = true;
14835 decl_specs->int_n_idx = idx;
14837 type = int_n_trees [idx].signed_type;
14838 break;
14839 case RID_LONG:
14840 if (decl_specs)
14841 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14842 type = long_integer_type_node;
14843 break;
14844 case RID_SIGNED:
14845 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14846 type = integer_type_node;
14847 break;
14848 case RID_UNSIGNED:
14849 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14850 type = unsigned_type_node;
14851 break;
14852 case RID_FLOAT:
14853 type = float_type_node;
14854 break;
14855 case RID_DOUBLE:
14856 type = double_type_node;
14857 break;
14858 case RID_VOID:
14859 type = void_type_node;
14860 break;
14862 case RID_AUTO:
14863 maybe_warn_cpp0x (CPP0X_AUTO);
14864 if (parser->auto_is_implicit_function_template_parm_p)
14866 if (cxx_dialect >= cxx14)
14867 type = synthesize_implicit_template_parm (parser);
14868 else
14869 type = error_mark_node;
14871 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14873 if (cxx_dialect < cxx14)
14874 error_at (token->location,
14875 "use of %<auto%> in lambda parameter declaration "
14876 "only available with "
14877 "-std=c++14 or -std=gnu++14");
14879 else if (cxx_dialect < cxx14)
14880 error_at (token->location,
14881 "use of %<auto%> in parameter declaration "
14882 "only available with "
14883 "-std=c++14 or -std=gnu++14");
14884 else
14885 pedwarn (token->location, OPT_Wpedantic,
14886 "ISO C++ forbids use of %<auto%> in parameter "
14887 "declaration");
14889 else
14890 type = make_auto ();
14891 break;
14893 case RID_DECLTYPE:
14894 /* Since DR 743, decltype can either be a simple-type-specifier by
14895 itself or begin a nested-name-specifier. Parsing it will replace
14896 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14897 handling below decide what to do. */
14898 cp_parser_decltype (parser);
14899 cp_lexer_set_token_position (parser->lexer, token);
14900 break;
14902 case RID_TYPEOF:
14903 /* Consume the `typeof' token. */
14904 cp_lexer_consume_token (parser->lexer);
14905 /* Parse the operand to `typeof'. */
14906 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14907 /* If it is not already a TYPE, take its type. */
14908 if (!TYPE_P (type))
14909 type = finish_typeof (type);
14911 if (decl_specs)
14912 cp_parser_set_decl_spec_type (decl_specs, type,
14913 token,
14914 /*type_definition_p=*/false);
14916 return type;
14918 case RID_UNDERLYING_TYPE:
14919 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14920 if (decl_specs)
14921 cp_parser_set_decl_spec_type (decl_specs, type,
14922 token,
14923 /*type_definition_p=*/false);
14925 return type;
14927 case RID_BASES:
14928 case RID_DIRECT_BASES:
14929 type = cp_parser_trait_expr (parser, token->keyword);
14930 if (decl_specs)
14931 cp_parser_set_decl_spec_type (decl_specs, type,
14932 token,
14933 /*type_definition_p=*/false);
14934 return type;
14935 default:
14936 break;
14939 /* If token is an already-parsed decltype not followed by ::,
14940 it's a simple-type-specifier. */
14941 if (token->type == CPP_DECLTYPE
14942 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14944 type = token->u.value;
14945 if (decl_specs)
14947 cp_parser_set_decl_spec_type (decl_specs, type,
14948 token,
14949 /*type_definition_p=*/false);
14950 /* Remember that we are handling a decltype in order to
14951 implement the resolution of DR 1510 when the argument
14952 isn't instantiation dependent. */
14953 decl_specs->decltype_p = true;
14955 cp_lexer_consume_token (parser->lexer);
14956 return type;
14959 /* If the type-specifier was for a built-in type, we're done. */
14960 if (type)
14962 /* Record the type. */
14963 if (decl_specs
14964 && (token->keyword != RID_SIGNED
14965 && token->keyword != RID_UNSIGNED
14966 && token->keyword != RID_SHORT
14967 && token->keyword != RID_LONG))
14968 cp_parser_set_decl_spec_type (decl_specs,
14969 type,
14970 token,
14971 /*type_definition_p=*/false);
14972 if (decl_specs)
14973 decl_specs->any_specifiers_p = true;
14975 /* Consume the token. */
14976 cp_lexer_consume_token (parser->lexer);
14978 if (type == error_mark_node)
14979 return error_mark_node;
14981 /* There is no valid C++ program where a non-template type is
14982 followed by a "<". That usually indicates that the user thought
14983 that the type was a template. */
14984 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14985 token->location);
14987 return TYPE_NAME (type);
14990 /* The type-specifier must be a user-defined type. */
14991 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14993 bool qualified_p;
14994 bool global_p;
14996 /* Don't gobble tokens or issue error messages if this is an
14997 optional type-specifier. */
14998 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14999 cp_parser_parse_tentatively (parser);
15001 /* Look for the optional `::' operator. */
15002 global_p
15003 = (cp_parser_global_scope_opt (parser,
15004 /*current_scope_valid_p=*/false)
15005 != NULL_TREE);
15006 /* Look for the nested-name specifier. */
15007 qualified_p
15008 = (cp_parser_nested_name_specifier_opt (parser,
15009 /*typename_keyword_p=*/false,
15010 /*check_dependency_p=*/true,
15011 /*type_p=*/false,
15012 /*is_declaration=*/false)
15013 != NULL_TREE);
15014 token = cp_lexer_peek_token (parser->lexer);
15015 /* If we have seen a nested-name-specifier, and the next token
15016 is `template', then we are using the template-id production. */
15017 if (parser->scope
15018 && cp_parser_optional_template_keyword (parser))
15020 /* Look for the template-id. */
15021 type = cp_parser_template_id (parser,
15022 /*template_keyword_p=*/true,
15023 /*check_dependency_p=*/true,
15024 none_type,
15025 /*is_declaration=*/false);
15026 /* If the template-id did not name a type, we are out of
15027 luck. */
15028 if (TREE_CODE (type) != TYPE_DECL)
15030 cp_parser_error (parser, "expected template-id for type");
15031 type = NULL_TREE;
15034 /* Otherwise, look for a type-name. */
15035 else
15036 type = cp_parser_type_name (parser);
15037 /* Keep track of all name-lookups performed in class scopes. */
15038 if (type
15039 && !global_p
15040 && !qualified_p
15041 && TREE_CODE (type) == TYPE_DECL
15042 && identifier_p (DECL_NAME (type)))
15043 maybe_note_name_used_in_class (DECL_NAME (type), type);
15044 /* If it didn't work out, we don't have a TYPE. */
15045 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15046 && !cp_parser_parse_definitely (parser))
15047 type = NULL_TREE;
15048 if (type && decl_specs)
15049 cp_parser_set_decl_spec_type (decl_specs, type,
15050 token,
15051 /*type_definition_p=*/false);
15054 /* If we didn't get a type-name, issue an error message. */
15055 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15057 cp_parser_error (parser, "expected type-name");
15058 return error_mark_node;
15061 if (type && type != error_mark_node)
15063 /* See if TYPE is an Objective-C type, and if so, parse and
15064 accept any protocol references following it. Do this before
15065 the cp_parser_check_for_invalid_template_id() call, because
15066 Objective-C types can be followed by '<...>' which would
15067 enclose protocol names rather than template arguments, and so
15068 everything is fine. */
15069 if (c_dialect_objc () && !parser->scope
15070 && (objc_is_id (type) || objc_is_class_name (type)))
15072 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15073 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15075 /* Clobber the "unqualified" type previously entered into
15076 DECL_SPECS with the new, improved protocol-qualified version. */
15077 if (decl_specs)
15078 decl_specs->type = qual_type;
15080 return qual_type;
15083 /* There is no valid C++ program where a non-template type is
15084 followed by a "<". That usually indicates that the user
15085 thought that the type was a template. */
15086 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15087 none_type,
15088 token->location);
15091 return type;
15094 /* Parse a type-name.
15096 type-name:
15097 class-name
15098 enum-name
15099 typedef-name
15100 simple-template-id [in c++0x]
15102 enum-name:
15103 identifier
15105 typedef-name:
15106 identifier
15108 Returns a TYPE_DECL for the type. */
15110 static tree
15111 cp_parser_type_name (cp_parser* parser)
15113 tree type_decl;
15115 /* We can't know yet whether it is a class-name or not. */
15116 cp_parser_parse_tentatively (parser);
15117 /* Try a class-name. */
15118 type_decl = cp_parser_class_name (parser,
15119 /*typename_keyword_p=*/false,
15120 /*template_keyword_p=*/false,
15121 none_type,
15122 /*check_dependency_p=*/true,
15123 /*class_head_p=*/false,
15124 /*is_declaration=*/false);
15125 /* If it's not a class-name, keep looking. */
15126 if (!cp_parser_parse_definitely (parser))
15128 if (cxx_dialect < cxx11)
15129 /* It must be a typedef-name or an enum-name. */
15130 return cp_parser_nonclass_name (parser);
15132 cp_parser_parse_tentatively (parser);
15133 /* It is either a simple-template-id representing an
15134 instantiation of an alias template... */
15135 type_decl = cp_parser_template_id (parser,
15136 /*template_keyword_p=*/false,
15137 /*check_dependency_p=*/true,
15138 none_type,
15139 /*is_declaration=*/false);
15140 /* Note that this must be an instantiation of an alias template
15141 because [temp.names]/6 says:
15143 A template-id that names an alias template specialization
15144 is a type-name.
15146 Whereas [temp.names]/7 says:
15148 A simple-template-id that names a class template
15149 specialization is a class-name. */
15150 if (type_decl != NULL_TREE
15151 && TREE_CODE (type_decl) == TYPE_DECL
15152 && TYPE_DECL_ALIAS_P (type_decl))
15153 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15154 else
15155 cp_parser_simulate_error (parser);
15157 if (!cp_parser_parse_definitely (parser))
15158 /* ... Or a typedef-name or an enum-name. */
15159 return cp_parser_nonclass_name (parser);
15162 return type_decl;
15165 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15167 enum-name:
15168 identifier
15170 typedef-name:
15171 identifier
15173 Returns a TYPE_DECL for the type. */
15175 static tree
15176 cp_parser_nonclass_name (cp_parser* parser)
15178 tree type_decl;
15179 tree identifier;
15181 cp_token *token = cp_lexer_peek_token (parser->lexer);
15182 identifier = cp_parser_identifier (parser);
15183 if (identifier == error_mark_node)
15184 return error_mark_node;
15186 /* Look up the type-name. */
15187 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15189 type_decl = strip_using_decl (type_decl);
15191 if (TREE_CODE (type_decl) != TYPE_DECL
15192 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15194 /* See if this is an Objective-C type. */
15195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15196 tree type = objc_get_protocol_qualified_type (identifier, protos);
15197 if (type)
15198 type_decl = TYPE_NAME (type);
15201 /* Issue an error if we did not find a type-name. */
15202 if (TREE_CODE (type_decl) != TYPE_DECL
15203 /* In Objective-C, we have the complication that class names are
15204 normally type names and start declarations (eg, the
15205 "NSObject" in "NSObject *object;"), but can be used in an
15206 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15207 is an expression. So, a classname followed by a dot is not a
15208 valid type-name. */
15209 || (objc_is_class_name (TREE_TYPE (type_decl))
15210 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15212 if (!cp_parser_simulate_error (parser))
15213 cp_parser_name_lookup_error (parser, identifier, type_decl,
15214 NLE_TYPE, token->location);
15215 return error_mark_node;
15217 /* Remember that the name was used in the definition of the
15218 current class so that we can check later to see if the
15219 meaning would have been different after the class was
15220 entirely defined. */
15221 else if (type_decl != error_mark_node
15222 && !parser->scope)
15223 maybe_note_name_used_in_class (identifier, type_decl);
15225 return type_decl;
15228 /* Parse an elaborated-type-specifier. Note that the grammar given
15229 here incorporates the resolution to DR68.
15231 elaborated-type-specifier:
15232 class-key :: [opt] nested-name-specifier [opt] identifier
15233 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15234 enum-key :: [opt] nested-name-specifier [opt] identifier
15235 typename :: [opt] nested-name-specifier identifier
15236 typename :: [opt] nested-name-specifier template [opt]
15237 template-id
15239 GNU extension:
15241 elaborated-type-specifier:
15242 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15243 class-key attributes :: [opt] nested-name-specifier [opt]
15244 template [opt] template-id
15245 enum attributes :: [opt] nested-name-specifier [opt] identifier
15247 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15248 declared `friend'. If IS_DECLARATION is TRUE, then this
15249 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15250 something is being declared.
15252 Returns the TYPE specified. */
15254 static tree
15255 cp_parser_elaborated_type_specifier (cp_parser* parser,
15256 bool is_friend,
15257 bool is_declaration)
15259 enum tag_types tag_type;
15260 tree identifier;
15261 tree type = NULL_TREE;
15262 tree attributes = NULL_TREE;
15263 tree globalscope;
15264 cp_token *token = NULL;
15266 /* See if we're looking at the `enum' keyword. */
15267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15269 /* Consume the `enum' token. */
15270 cp_lexer_consume_token (parser->lexer);
15271 /* Remember that it's an enumeration type. */
15272 tag_type = enum_type;
15273 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15274 enums) is used here. */
15275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15276 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15278 pedwarn (input_location, 0, "elaborated-type-specifier "
15279 "for a scoped enum must not use the %<%D%> keyword",
15280 cp_lexer_peek_token (parser->lexer)->u.value);
15281 /* Consume the `struct' or `class' and parse it anyway. */
15282 cp_lexer_consume_token (parser->lexer);
15284 /* Parse the attributes. */
15285 attributes = cp_parser_attributes_opt (parser);
15287 /* Or, it might be `typename'. */
15288 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15289 RID_TYPENAME))
15291 /* Consume the `typename' token. */
15292 cp_lexer_consume_token (parser->lexer);
15293 /* Remember that it's a `typename' type. */
15294 tag_type = typename_type;
15296 /* Otherwise it must be a class-key. */
15297 else
15299 tag_type = cp_parser_class_key (parser);
15300 if (tag_type == none_type)
15301 return error_mark_node;
15302 /* Parse the attributes. */
15303 attributes = cp_parser_attributes_opt (parser);
15306 /* Look for the `::' operator. */
15307 globalscope = cp_parser_global_scope_opt (parser,
15308 /*current_scope_valid_p=*/false);
15309 /* Look for the nested-name-specifier. */
15310 if (tag_type == typename_type && !globalscope)
15312 if (!cp_parser_nested_name_specifier (parser,
15313 /*typename_keyword_p=*/true,
15314 /*check_dependency_p=*/true,
15315 /*type_p=*/true,
15316 is_declaration))
15317 return error_mark_node;
15319 else
15320 /* Even though `typename' is not present, the proposed resolution
15321 to Core Issue 180 says that in `class A<T>::B', `B' should be
15322 considered a type-name, even if `A<T>' is dependent. */
15323 cp_parser_nested_name_specifier_opt (parser,
15324 /*typename_keyword_p=*/true,
15325 /*check_dependency_p=*/true,
15326 /*type_p=*/true,
15327 is_declaration);
15328 /* For everything but enumeration types, consider a template-id.
15329 For an enumeration type, consider only a plain identifier. */
15330 if (tag_type != enum_type)
15332 bool template_p = false;
15333 tree decl;
15335 /* Allow the `template' keyword. */
15336 template_p = cp_parser_optional_template_keyword (parser);
15337 /* If we didn't see `template', we don't know if there's a
15338 template-id or not. */
15339 if (!template_p)
15340 cp_parser_parse_tentatively (parser);
15341 /* Parse the template-id. */
15342 token = cp_lexer_peek_token (parser->lexer);
15343 decl = cp_parser_template_id (parser, template_p,
15344 /*check_dependency_p=*/true,
15345 tag_type,
15346 is_declaration);
15347 /* If we didn't find a template-id, look for an ordinary
15348 identifier. */
15349 if (!template_p && !cp_parser_parse_definitely (parser))
15351 /* We can get here when cp_parser_template_id, called by
15352 cp_parser_class_name with tag_type == none_type, succeeds
15353 and caches a BASELINK. Then, when called again here,
15354 instead of failing and returning an error_mark_node
15355 returns it (see template/typename17.C in C++11).
15356 ??? Could we diagnose this earlier? */
15357 else if (tag_type == typename_type && BASELINK_P (decl))
15359 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15360 type = error_mark_node;
15362 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15363 in effect, then we must assume that, upon instantiation, the
15364 template will correspond to a class. */
15365 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15366 && tag_type == typename_type)
15367 type = make_typename_type (parser->scope, decl,
15368 typename_type,
15369 /*complain=*/tf_error);
15370 /* If the `typename' keyword is in effect and DECL is not a type
15371 decl, then type is non existent. */
15372 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15374 else if (TREE_CODE (decl) == TYPE_DECL)
15375 type = check_elaborated_type_specifier (tag_type, decl,
15376 /*allow_template_p=*/true);
15377 else if (decl == error_mark_node)
15378 type = error_mark_node;
15381 if (!type)
15383 token = cp_lexer_peek_token (parser->lexer);
15384 identifier = cp_parser_identifier (parser);
15386 if (identifier == error_mark_node)
15388 parser->scope = NULL_TREE;
15389 return error_mark_node;
15392 /* For a `typename', we needn't call xref_tag. */
15393 if (tag_type == typename_type
15394 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15395 return cp_parser_make_typename_type (parser, identifier,
15396 token->location);
15398 /* Template parameter lists apply only if we are not within a
15399 function parameter list. */
15400 bool template_parm_lists_apply
15401 = parser->num_template_parameter_lists;
15402 if (template_parm_lists_apply)
15403 for (cp_binding_level *s = current_binding_level;
15404 s && s->kind != sk_template_parms;
15405 s = s->level_chain)
15406 if (s->kind == sk_function_parms)
15407 template_parm_lists_apply = false;
15409 /* Look up a qualified name in the usual way. */
15410 if (parser->scope)
15412 tree decl;
15413 tree ambiguous_decls;
15415 decl = cp_parser_lookup_name (parser, identifier,
15416 tag_type,
15417 /*is_template=*/false,
15418 /*is_namespace=*/false,
15419 /*check_dependency=*/true,
15420 &ambiguous_decls,
15421 token->location);
15423 /* If the lookup was ambiguous, an error will already have been
15424 issued. */
15425 if (ambiguous_decls)
15426 return error_mark_node;
15428 /* If we are parsing friend declaration, DECL may be a
15429 TEMPLATE_DECL tree node here. However, we need to check
15430 whether this TEMPLATE_DECL results in valid code. Consider
15431 the following example:
15433 namespace N {
15434 template <class T> class C {};
15436 class X {
15437 template <class T> friend class N::C; // #1, valid code
15439 template <class T> class Y {
15440 friend class N::C; // #2, invalid code
15443 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15444 name lookup of `N::C'. We see that friend declaration must
15445 be template for the code to be valid. Note that
15446 processing_template_decl does not work here since it is
15447 always 1 for the above two cases. */
15449 decl = (cp_parser_maybe_treat_template_as_class
15450 (decl, /*tag_name_p=*/is_friend
15451 && template_parm_lists_apply));
15453 if (TREE_CODE (decl) != TYPE_DECL)
15455 cp_parser_diagnose_invalid_type_name (parser,
15456 identifier,
15457 token->location);
15458 return error_mark_node;
15461 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15463 bool allow_template = (template_parm_lists_apply
15464 || DECL_SELF_REFERENCE_P (decl));
15465 type = check_elaborated_type_specifier (tag_type, decl,
15466 allow_template);
15468 if (type == error_mark_node)
15469 return error_mark_node;
15472 /* Forward declarations of nested types, such as
15474 class C1::C2;
15475 class C1::C2::C3;
15477 are invalid unless all components preceding the final '::'
15478 are complete. If all enclosing types are complete, these
15479 declarations become merely pointless.
15481 Invalid forward declarations of nested types are errors
15482 caught elsewhere in parsing. Those that are pointless arrive
15483 here. */
15485 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15486 && !is_friend && !processing_explicit_instantiation)
15487 warning (0, "declaration %qD does not declare anything", decl);
15489 type = TREE_TYPE (decl);
15491 else
15493 /* An elaborated-type-specifier sometimes introduces a new type and
15494 sometimes names an existing type. Normally, the rule is that it
15495 introduces a new type only if there is not an existing type of
15496 the same name already in scope. For example, given:
15498 struct S {};
15499 void f() { struct S s; }
15501 the `struct S' in the body of `f' is the same `struct S' as in
15502 the global scope; the existing definition is used. However, if
15503 there were no global declaration, this would introduce a new
15504 local class named `S'.
15506 An exception to this rule applies to the following code:
15508 namespace N { struct S; }
15510 Here, the elaborated-type-specifier names a new type
15511 unconditionally; even if there is already an `S' in the
15512 containing scope this declaration names a new type.
15513 This exception only applies if the elaborated-type-specifier
15514 forms the complete declaration:
15516 [class.name]
15518 A declaration consisting solely of `class-key identifier ;' is
15519 either a redeclaration of the name in the current scope or a
15520 forward declaration of the identifier as a class name. It
15521 introduces the name into the current scope.
15523 We are in this situation precisely when the next token is a `;'.
15525 An exception to the exception is that a `friend' declaration does
15526 *not* name a new type; i.e., given:
15528 struct S { friend struct T; };
15530 `T' is not a new type in the scope of `S'.
15532 Also, `new struct S' or `sizeof (struct S)' never results in the
15533 definition of a new type; a new type can only be declared in a
15534 declaration context. */
15536 tag_scope ts;
15537 bool template_p;
15539 if (is_friend)
15540 /* Friends have special name lookup rules. */
15541 ts = ts_within_enclosing_non_class;
15542 else if (is_declaration
15543 && cp_lexer_next_token_is (parser->lexer,
15544 CPP_SEMICOLON))
15545 /* This is a `class-key identifier ;' */
15546 ts = ts_current;
15547 else
15548 ts = ts_global;
15550 template_p =
15551 (template_parm_lists_apply
15552 && (cp_parser_next_token_starts_class_definition_p (parser)
15553 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15554 /* An unqualified name was used to reference this type, so
15555 there were no qualifying templates. */
15556 if (template_parm_lists_apply
15557 && !cp_parser_check_template_parameters (parser,
15558 /*num_templates=*/0,
15559 token->location,
15560 /*declarator=*/NULL))
15561 return error_mark_node;
15562 type = xref_tag (tag_type, identifier, ts, template_p);
15566 if (type == error_mark_node)
15567 return error_mark_node;
15569 /* Allow attributes on forward declarations of classes. */
15570 if (attributes)
15572 if (TREE_CODE (type) == TYPENAME_TYPE)
15573 warning (OPT_Wattributes,
15574 "attributes ignored on uninstantiated type");
15575 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15576 && ! processing_explicit_instantiation)
15577 warning (OPT_Wattributes,
15578 "attributes ignored on template instantiation");
15579 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15580 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15581 else
15582 warning (OPT_Wattributes,
15583 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15586 if (tag_type != enum_type)
15588 /* Indicate whether this class was declared as a `class' or as a
15589 `struct'. */
15590 if (TREE_CODE (type) == RECORD_TYPE)
15591 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15592 cp_parser_check_class_key (tag_type, type);
15595 /* A "<" cannot follow an elaborated type specifier. If that
15596 happens, the user was probably trying to form a template-id. */
15597 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15598 token->location);
15600 return type;
15603 /* Parse an enum-specifier.
15605 enum-specifier:
15606 enum-head { enumerator-list [opt] }
15607 enum-head { enumerator-list , } [C++0x]
15609 enum-head:
15610 enum-key identifier [opt] enum-base [opt]
15611 enum-key nested-name-specifier identifier enum-base [opt]
15613 enum-key:
15614 enum
15615 enum class [C++0x]
15616 enum struct [C++0x]
15618 enum-base: [C++0x]
15619 : type-specifier-seq
15621 opaque-enum-specifier:
15622 enum-key identifier enum-base [opt] ;
15624 GNU Extensions:
15625 enum-key attributes[opt] identifier [opt] enum-base [opt]
15626 { enumerator-list [opt] }attributes[opt]
15627 enum-key attributes[opt] identifier [opt] enum-base [opt]
15628 { enumerator-list, }attributes[opt] [C++0x]
15630 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15631 if the token stream isn't an enum-specifier after all. */
15633 static tree
15634 cp_parser_enum_specifier (cp_parser* parser)
15636 tree identifier;
15637 tree type = NULL_TREE;
15638 tree prev_scope;
15639 tree nested_name_specifier = NULL_TREE;
15640 tree attributes;
15641 bool scoped_enum_p = false;
15642 bool has_underlying_type = false;
15643 bool nested_being_defined = false;
15644 bool new_value_list = false;
15645 bool is_new_type = false;
15646 bool is_anonymous = false;
15647 tree underlying_type = NULL_TREE;
15648 cp_token *type_start_token = NULL;
15649 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15651 parser->colon_corrects_to_scope_p = false;
15653 /* Parse tentatively so that we can back up if we don't find a
15654 enum-specifier. */
15655 cp_parser_parse_tentatively (parser);
15657 /* Caller guarantees that the current token is 'enum', an identifier
15658 possibly follows, and the token after that is an opening brace.
15659 If we don't have an identifier, fabricate an anonymous name for
15660 the enumeration being defined. */
15661 cp_lexer_consume_token (parser->lexer);
15663 /* Parse the "class" or "struct", which indicates a scoped
15664 enumeration type in C++0x. */
15665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15666 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15668 if (cxx_dialect < cxx11)
15669 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15671 /* Consume the `struct' or `class' token. */
15672 cp_lexer_consume_token (parser->lexer);
15674 scoped_enum_p = true;
15677 attributes = cp_parser_attributes_opt (parser);
15679 /* Clear the qualification. */
15680 parser->scope = NULL_TREE;
15681 parser->qualifying_scope = NULL_TREE;
15682 parser->object_scope = NULL_TREE;
15684 /* Figure out in what scope the declaration is being placed. */
15685 prev_scope = current_scope ();
15687 type_start_token = cp_lexer_peek_token (parser->lexer);
15689 push_deferring_access_checks (dk_no_check);
15690 nested_name_specifier
15691 = cp_parser_nested_name_specifier_opt (parser,
15692 /*typename_keyword_p=*/true,
15693 /*check_dependency_p=*/false,
15694 /*type_p=*/false,
15695 /*is_declaration=*/false);
15697 if (nested_name_specifier)
15699 tree name;
15701 identifier = cp_parser_identifier (parser);
15702 name = cp_parser_lookup_name (parser, identifier,
15703 enum_type,
15704 /*is_template=*/false,
15705 /*is_namespace=*/false,
15706 /*check_dependency=*/true,
15707 /*ambiguous_decls=*/NULL,
15708 input_location);
15709 if (name && name != error_mark_node)
15711 type = TREE_TYPE (name);
15712 if (TREE_CODE (type) == TYPENAME_TYPE)
15714 /* Are template enums allowed in ISO? */
15715 if (template_parm_scope_p ())
15716 pedwarn (type_start_token->location, OPT_Wpedantic,
15717 "%qD is an enumeration template", name);
15718 /* ignore a typename reference, for it will be solved by name
15719 in start_enum. */
15720 type = NULL_TREE;
15723 else if (nested_name_specifier == error_mark_node)
15724 /* We already issued an error. */;
15725 else
15726 error_at (type_start_token->location,
15727 "%qD is not an enumerator-name", identifier);
15729 else
15731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15732 identifier = cp_parser_identifier (parser);
15733 else
15735 identifier = make_anon_name ();
15736 is_anonymous = true;
15737 if (scoped_enum_p)
15738 error_at (type_start_token->location,
15739 "anonymous scoped enum is not allowed");
15742 pop_deferring_access_checks ();
15744 /* Check for the `:' that denotes a specified underlying type in C++0x.
15745 Note that a ':' could also indicate a bitfield width, however. */
15746 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15748 cp_decl_specifier_seq type_specifiers;
15750 /* Consume the `:'. */
15751 cp_lexer_consume_token (parser->lexer);
15753 /* Parse the type-specifier-seq. */
15754 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15755 /*is_trailing_return=*/false,
15756 &type_specifiers);
15758 /* At this point this is surely not elaborated type specifier. */
15759 if (!cp_parser_parse_definitely (parser))
15760 return NULL_TREE;
15762 if (cxx_dialect < cxx11)
15763 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15765 has_underlying_type = true;
15767 /* If that didn't work, stop. */
15768 if (type_specifiers.type != error_mark_node)
15770 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15771 /*initialized=*/0, NULL);
15772 if (underlying_type == error_mark_node
15773 || check_for_bare_parameter_packs (underlying_type))
15774 underlying_type = NULL_TREE;
15778 /* Look for the `{' but don't consume it yet. */
15779 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15781 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15783 cp_parser_error (parser, "expected %<{%>");
15784 if (has_underlying_type)
15786 type = NULL_TREE;
15787 goto out;
15790 /* An opaque-enum-specifier must have a ';' here. */
15791 if ((scoped_enum_p || underlying_type)
15792 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15794 cp_parser_error (parser, "expected %<;%> or %<{%>");
15795 if (has_underlying_type)
15797 type = NULL_TREE;
15798 goto out;
15803 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15804 return NULL_TREE;
15806 if (nested_name_specifier)
15808 if (CLASS_TYPE_P (nested_name_specifier))
15810 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15811 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15812 push_scope (nested_name_specifier);
15814 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15816 push_nested_namespace (nested_name_specifier);
15820 /* Issue an error message if type-definitions are forbidden here. */
15821 if (!cp_parser_check_type_definition (parser))
15822 type = error_mark_node;
15823 else
15824 /* Create the new type. We do this before consuming the opening
15825 brace so the enum will be recorded as being on the line of its
15826 tag (or the 'enum' keyword, if there is no tag). */
15827 type = start_enum (identifier, type, underlying_type,
15828 scoped_enum_p, &is_new_type);
15830 /* If the next token is not '{' it is an opaque-enum-specifier or an
15831 elaborated-type-specifier. */
15832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15834 timevar_push (TV_PARSE_ENUM);
15835 if (nested_name_specifier
15836 && nested_name_specifier != error_mark_node)
15838 /* The following catches invalid code such as:
15839 enum class S<int>::E { A, B, C }; */
15840 if (!processing_specialization
15841 && CLASS_TYPE_P (nested_name_specifier)
15842 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15843 error_at (type_start_token->location, "cannot add an enumerator "
15844 "list to a template instantiation");
15846 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15848 error_at (type_start_token->location,
15849 "%<%T::%E%> has not been declared",
15850 TYPE_CONTEXT (nested_name_specifier),
15851 nested_name_specifier);
15852 type = error_mark_node;
15854 /* If that scope does not contain the scope in which the
15855 class was originally declared, the program is invalid. */
15856 else if (prev_scope && !is_ancestor (prev_scope,
15857 nested_name_specifier))
15859 if (at_namespace_scope_p ())
15860 error_at (type_start_token->location,
15861 "declaration of %qD in namespace %qD which does not "
15862 "enclose %qD",
15863 type, prev_scope, nested_name_specifier);
15864 else
15865 error_at (type_start_token->location,
15866 "declaration of %qD in %qD which does not "
15867 "enclose %qD",
15868 type, prev_scope, nested_name_specifier);
15869 type = error_mark_node;
15873 if (scoped_enum_p)
15874 begin_scope (sk_scoped_enum, type);
15876 /* Consume the opening brace. */
15877 cp_lexer_consume_token (parser->lexer);
15879 if (type == error_mark_node)
15880 ; /* Nothing to add */
15881 else if (OPAQUE_ENUM_P (type)
15882 || (cxx_dialect > cxx98 && processing_specialization))
15884 new_value_list = true;
15885 SET_OPAQUE_ENUM_P (type, false);
15886 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15888 else
15890 error_at (type_start_token->location,
15891 "multiple definition of %q#T", type);
15892 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15893 "previous definition here");
15894 type = error_mark_node;
15897 if (type == error_mark_node)
15898 cp_parser_skip_to_end_of_block_or_statement (parser);
15899 /* If the next token is not '}', then there are some enumerators. */
15900 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15902 if (is_anonymous && !scoped_enum_p)
15903 pedwarn (type_start_token->location, OPT_Wpedantic,
15904 "ISO C++ forbids empty anonymous enum");
15906 else
15907 cp_parser_enumerator_list (parser, type);
15909 /* Consume the final '}'. */
15910 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15912 if (scoped_enum_p)
15913 finish_scope ();
15914 timevar_pop (TV_PARSE_ENUM);
15916 else
15918 /* If a ';' follows, then it is an opaque-enum-specifier
15919 and additional restrictions apply. */
15920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15922 if (is_anonymous)
15923 error_at (type_start_token->location,
15924 "opaque-enum-specifier without name");
15925 else if (nested_name_specifier)
15926 error_at (type_start_token->location,
15927 "opaque-enum-specifier must use a simple identifier");
15931 /* Look for trailing attributes to apply to this enumeration, and
15932 apply them if appropriate. */
15933 if (cp_parser_allow_gnu_extensions_p (parser))
15935 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15936 trailing_attr = chainon (trailing_attr, attributes);
15937 cplus_decl_attributes (&type,
15938 trailing_attr,
15939 (int) ATTR_FLAG_TYPE_IN_PLACE);
15942 /* Finish up the enumeration. */
15943 if (type != error_mark_node)
15945 if (new_value_list)
15946 finish_enum_value_list (type);
15947 if (is_new_type)
15948 finish_enum (type);
15951 if (nested_name_specifier)
15953 if (CLASS_TYPE_P (nested_name_specifier))
15955 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15956 pop_scope (nested_name_specifier);
15958 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15960 pop_nested_namespace (nested_name_specifier);
15963 out:
15964 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15965 return type;
15968 /* Parse an enumerator-list. The enumerators all have the indicated
15969 TYPE.
15971 enumerator-list:
15972 enumerator-definition
15973 enumerator-list , enumerator-definition */
15975 static void
15976 cp_parser_enumerator_list (cp_parser* parser, tree type)
15978 while (true)
15980 /* Parse an enumerator-definition. */
15981 cp_parser_enumerator_definition (parser, type);
15983 /* If the next token is not a ',', we've reached the end of
15984 the list. */
15985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15986 break;
15987 /* Otherwise, consume the `,' and keep going. */
15988 cp_lexer_consume_token (parser->lexer);
15989 /* If the next token is a `}', there is a trailing comma. */
15990 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15992 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15993 pedwarn (input_location, OPT_Wpedantic,
15994 "comma at end of enumerator list");
15995 break;
16000 /* Parse an enumerator-definition. The enumerator has the indicated
16001 TYPE.
16003 enumerator-definition:
16004 enumerator
16005 enumerator = constant-expression
16007 enumerator:
16008 identifier */
16010 static void
16011 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16013 tree identifier;
16014 tree value;
16015 location_t loc;
16017 /* Save the input location because we are interested in the location
16018 of the identifier and not the location of the explicit value. */
16019 loc = cp_lexer_peek_token (parser->lexer)->location;
16021 /* Look for the identifier. */
16022 identifier = cp_parser_identifier (parser);
16023 if (identifier == error_mark_node)
16024 return;
16026 /* If the next token is an '=', then there is an explicit value. */
16027 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16029 /* Consume the `=' token. */
16030 cp_lexer_consume_token (parser->lexer);
16031 /* Parse the value. */
16032 value = cp_parser_constant_expression (parser);
16034 else
16035 value = NULL_TREE;
16037 /* If we are processing a template, make sure the initializer of the
16038 enumerator doesn't contain any bare template parameter pack. */
16039 if (check_for_bare_parameter_packs (value))
16040 value = error_mark_node;
16042 /* Create the enumerator. */
16043 build_enumerator (identifier, value, type, loc);
16046 /* Parse a namespace-name.
16048 namespace-name:
16049 original-namespace-name
16050 namespace-alias
16052 Returns the NAMESPACE_DECL for the namespace. */
16054 static tree
16055 cp_parser_namespace_name (cp_parser* parser)
16057 tree identifier;
16058 tree namespace_decl;
16060 cp_token *token = cp_lexer_peek_token (parser->lexer);
16062 /* Get the name of the namespace. */
16063 identifier = cp_parser_identifier (parser);
16064 if (identifier == error_mark_node)
16065 return error_mark_node;
16067 /* Look up the identifier in the currently active scope. Look only
16068 for namespaces, due to:
16070 [basic.lookup.udir]
16072 When looking up a namespace-name in a using-directive or alias
16073 definition, only namespace names are considered.
16075 And:
16077 [basic.lookup.qual]
16079 During the lookup of a name preceding the :: scope resolution
16080 operator, object, function, and enumerator names are ignored.
16082 (Note that cp_parser_qualifying_entity only calls this
16083 function if the token after the name is the scope resolution
16084 operator.) */
16085 namespace_decl = cp_parser_lookup_name (parser, identifier,
16086 none_type,
16087 /*is_template=*/false,
16088 /*is_namespace=*/true,
16089 /*check_dependency=*/true,
16090 /*ambiguous_decls=*/NULL,
16091 token->location);
16092 /* If it's not a namespace, issue an error. */
16093 if (namespace_decl == error_mark_node
16094 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16096 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16097 error_at (token->location, "%qD is not a namespace-name", identifier);
16098 cp_parser_error (parser, "expected namespace-name");
16099 namespace_decl = error_mark_node;
16102 return namespace_decl;
16105 /* Parse a namespace-definition.
16107 namespace-definition:
16108 named-namespace-definition
16109 unnamed-namespace-definition
16111 named-namespace-definition:
16112 original-namespace-definition
16113 extension-namespace-definition
16115 original-namespace-definition:
16116 namespace identifier { namespace-body }
16118 extension-namespace-definition:
16119 namespace original-namespace-name { namespace-body }
16121 unnamed-namespace-definition:
16122 namespace { namespace-body } */
16124 static void
16125 cp_parser_namespace_definition (cp_parser* parser)
16127 tree identifier, attribs;
16128 bool has_visibility;
16129 bool is_inline;
16131 cp_ensure_no_omp_declare_simd (parser);
16132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16134 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16135 is_inline = true;
16136 cp_lexer_consume_token (parser->lexer);
16138 else
16139 is_inline = false;
16141 /* Look for the `namespace' keyword. */
16142 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16144 /* Get the name of the namespace. We do not attempt to distinguish
16145 between an original-namespace-definition and an
16146 extension-namespace-definition at this point. The semantic
16147 analysis routines are responsible for that. */
16148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16149 identifier = cp_parser_identifier (parser);
16150 else
16151 identifier = NULL_TREE;
16153 /* Parse any specified attributes. */
16154 attribs = cp_parser_attributes_opt (parser);
16156 /* Look for the `{' to start the namespace. */
16157 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16158 /* Start the namespace. */
16159 push_namespace (identifier);
16161 /* "inline namespace" is equivalent to a stub namespace definition
16162 followed by a strong using directive. */
16163 if (is_inline)
16165 tree name_space = current_namespace;
16166 /* Set up namespace association. */
16167 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16168 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16169 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16170 /* Import the contents of the inline namespace. */
16171 pop_namespace ();
16172 do_using_directive (name_space);
16173 push_namespace (identifier);
16176 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16178 /* Parse the body of the namespace. */
16179 cp_parser_namespace_body (parser);
16181 if (has_visibility)
16182 pop_visibility (1);
16184 /* Finish the namespace. */
16185 pop_namespace ();
16186 /* Look for the final `}'. */
16187 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16190 /* Parse a namespace-body.
16192 namespace-body:
16193 declaration-seq [opt] */
16195 static void
16196 cp_parser_namespace_body (cp_parser* parser)
16198 cp_parser_declaration_seq_opt (parser);
16201 /* Parse a namespace-alias-definition.
16203 namespace-alias-definition:
16204 namespace identifier = qualified-namespace-specifier ; */
16206 static void
16207 cp_parser_namespace_alias_definition (cp_parser* parser)
16209 tree identifier;
16210 tree namespace_specifier;
16212 cp_token *token = cp_lexer_peek_token (parser->lexer);
16214 /* Look for the `namespace' keyword. */
16215 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16216 /* Look for the identifier. */
16217 identifier = cp_parser_identifier (parser);
16218 if (identifier == error_mark_node)
16219 return;
16220 /* Look for the `=' token. */
16221 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16222 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16224 error_at (token->location, "%<namespace%> definition is not allowed here");
16225 /* Skip the definition. */
16226 cp_lexer_consume_token (parser->lexer);
16227 if (cp_parser_skip_to_closing_brace (parser))
16228 cp_lexer_consume_token (parser->lexer);
16229 return;
16231 cp_parser_require (parser, CPP_EQ, RT_EQ);
16232 /* Look for the qualified-namespace-specifier. */
16233 namespace_specifier
16234 = cp_parser_qualified_namespace_specifier (parser);
16235 /* Look for the `;' token. */
16236 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16238 /* Register the alias in the symbol table. */
16239 do_namespace_alias (identifier, namespace_specifier);
16242 /* Parse a qualified-namespace-specifier.
16244 qualified-namespace-specifier:
16245 :: [opt] nested-name-specifier [opt] namespace-name
16247 Returns a NAMESPACE_DECL corresponding to the specified
16248 namespace. */
16250 static tree
16251 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16253 /* Look for the optional `::'. */
16254 cp_parser_global_scope_opt (parser,
16255 /*current_scope_valid_p=*/false);
16257 /* Look for the optional nested-name-specifier. */
16258 cp_parser_nested_name_specifier_opt (parser,
16259 /*typename_keyword_p=*/false,
16260 /*check_dependency_p=*/true,
16261 /*type_p=*/false,
16262 /*is_declaration=*/true);
16264 return cp_parser_namespace_name (parser);
16267 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16268 access declaration.
16270 using-declaration:
16271 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16272 using :: unqualified-id ;
16274 access-declaration:
16275 qualified-id ;
16279 static bool
16280 cp_parser_using_declaration (cp_parser* parser,
16281 bool access_declaration_p)
16283 cp_token *token;
16284 bool typename_p = false;
16285 bool global_scope_p;
16286 tree decl;
16287 tree identifier;
16288 tree qscope;
16289 int oldcount = errorcount;
16290 cp_token *diag_token = NULL;
16292 if (access_declaration_p)
16294 diag_token = cp_lexer_peek_token (parser->lexer);
16295 cp_parser_parse_tentatively (parser);
16297 else
16299 /* Look for the `using' keyword. */
16300 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16302 /* Peek at the next token. */
16303 token = cp_lexer_peek_token (parser->lexer);
16304 /* See if it's `typename'. */
16305 if (token->keyword == RID_TYPENAME)
16307 /* Remember that we've seen it. */
16308 typename_p = true;
16309 /* Consume the `typename' token. */
16310 cp_lexer_consume_token (parser->lexer);
16314 /* Look for the optional global scope qualification. */
16315 global_scope_p
16316 = (cp_parser_global_scope_opt (parser,
16317 /*current_scope_valid_p=*/false)
16318 != NULL_TREE);
16320 /* If we saw `typename', or didn't see `::', then there must be a
16321 nested-name-specifier present. */
16322 if (typename_p || !global_scope_p)
16324 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16325 /*check_dependency_p=*/true,
16326 /*type_p=*/false,
16327 /*is_declaration=*/true);
16328 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16330 cp_parser_skip_to_end_of_block_or_statement (parser);
16331 return false;
16334 /* Otherwise, we could be in either of the two productions. In that
16335 case, treat the nested-name-specifier as optional. */
16336 else
16337 qscope = cp_parser_nested_name_specifier_opt (parser,
16338 /*typename_keyword_p=*/false,
16339 /*check_dependency_p=*/true,
16340 /*type_p=*/false,
16341 /*is_declaration=*/true);
16342 if (!qscope)
16343 qscope = global_namespace;
16344 else if (UNSCOPED_ENUM_P (qscope))
16345 qscope = CP_TYPE_CONTEXT (qscope);
16347 if (access_declaration_p && cp_parser_error_occurred (parser))
16348 /* Something has already gone wrong; there's no need to parse
16349 further. Since an error has occurred, the return value of
16350 cp_parser_parse_definitely will be false, as required. */
16351 return cp_parser_parse_definitely (parser);
16353 token = cp_lexer_peek_token (parser->lexer);
16354 /* Parse the unqualified-id. */
16355 identifier = cp_parser_unqualified_id (parser,
16356 /*template_keyword_p=*/false,
16357 /*check_dependency_p=*/true,
16358 /*declarator_p=*/true,
16359 /*optional_p=*/false);
16361 if (access_declaration_p)
16363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16364 cp_parser_simulate_error (parser);
16365 if (!cp_parser_parse_definitely (parser))
16366 return false;
16369 /* The function we call to handle a using-declaration is different
16370 depending on what scope we are in. */
16371 if (qscope == error_mark_node || identifier == error_mark_node)
16373 else if (!identifier_p (identifier)
16374 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16375 /* [namespace.udecl]
16377 A using declaration shall not name a template-id. */
16378 error_at (token->location,
16379 "a template-id may not appear in a using-declaration");
16380 else
16382 if (at_class_scope_p ())
16384 /* Create the USING_DECL. */
16385 decl = do_class_using_decl (parser->scope, identifier);
16387 if (decl && typename_p)
16388 USING_DECL_TYPENAME_P (decl) = 1;
16390 if (check_for_bare_parameter_packs (decl))
16392 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16393 return false;
16395 else
16396 /* Add it to the list of members in this class. */
16397 finish_member_declaration (decl);
16399 else
16401 decl = cp_parser_lookup_name_simple (parser,
16402 identifier,
16403 token->location);
16404 if (decl == error_mark_node)
16405 cp_parser_name_lookup_error (parser, identifier,
16406 decl, NLE_NULL,
16407 token->location);
16408 else if (check_for_bare_parameter_packs (decl))
16410 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16411 return false;
16413 else if (!at_namespace_scope_p ())
16414 do_local_using_decl (decl, qscope, identifier);
16415 else
16416 do_toplevel_using_decl (decl, qscope, identifier);
16420 /* Look for the final `;'. */
16421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16423 if (access_declaration_p && errorcount == oldcount)
16424 warning_at (diag_token->location, OPT_Wdeprecated,
16425 "access declarations are deprecated "
16426 "in favour of using-declarations; "
16427 "suggestion: add the %<using%> keyword");
16429 return true;
16432 /* Parse an alias-declaration.
16434 alias-declaration:
16435 using identifier attribute-specifier-seq [opt] = type-id */
16437 static tree
16438 cp_parser_alias_declaration (cp_parser* parser)
16440 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16441 location_t id_location;
16442 cp_declarator *declarator;
16443 cp_decl_specifier_seq decl_specs;
16444 bool member_p;
16445 const char *saved_message = NULL;
16447 /* Look for the `using' keyword. */
16448 cp_token *using_token
16449 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16450 if (using_token == NULL)
16451 return error_mark_node;
16453 id_location = cp_lexer_peek_token (parser->lexer)->location;
16454 id = cp_parser_identifier (parser);
16455 if (id == error_mark_node)
16456 return error_mark_node;
16458 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16459 attributes = cp_parser_attributes_opt (parser);
16460 if (attributes == error_mark_node)
16461 return error_mark_node;
16463 cp_parser_require (parser, CPP_EQ, RT_EQ);
16465 if (cp_parser_error_occurred (parser))
16466 return error_mark_node;
16468 cp_parser_commit_to_tentative_parse (parser);
16470 /* Now we are going to parse the type-id of the declaration. */
16473 [dcl.type]/3 says:
16475 "A type-specifier-seq shall not define a class or enumeration
16476 unless it appears in the type-id of an alias-declaration (7.1.3) that
16477 is not the declaration of a template-declaration."
16479 In other words, if we currently are in an alias template, the
16480 type-id should not define a type.
16482 So let's set parser->type_definition_forbidden_message in that
16483 case; cp_parser_check_type_definition (called by
16484 cp_parser_class_specifier) will then emit an error if a type is
16485 defined in the type-id. */
16486 if (parser->num_template_parameter_lists)
16488 saved_message = parser->type_definition_forbidden_message;
16489 parser->type_definition_forbidden_message =
16490 G_("types may not be defined in alias template declarations");
16493 type = cp_parser_type_id (parser);
16495 /* Restore the error message if need be. */
16496 if (parser->num_template_parameter_lists)
16497 parser->type_definition_forbidden_message = saved_message;
16499 if (type == error_mark_node
16500 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16502 cp_parser_skip_to_end_of_block_or_statement (parser);
16503 return error_mark_node;
16506 /* A typedef-name can also be introduced by an alias-declaration. The
16507 identifier following the using keyword becomes a typedef-name. It has
16508 the same semantics as if it were introduced by the typedef
16509 specifier. In particular, it does not define a new type and it shall
16510 not appear in the type-id. */
16512 clear_decl_specs (&decl_specs);
16513 decl_specs.type = type;
16514 if (attributes != NULL_TREE)
16516 decl_specs.attributes = attributes;
16517 set_and_check_decl_spec_loc (&decl_specs,
16518 ds_attribute,
16519 attrs_token);
16521 set_and_check_decl_spec_loc (&decl_specs,
16522 ds_typedef,
16523 using_token);
16524 set_and_check_decl_spec_loc (&decl_specs,
16525 ds_alias,
16526 using_token);
16528 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16529 declarator->id_loc = id_location;
16531 member_p = at_class_scope_p ();
16532 if (member_p)
16533 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16534 NULL_TREE, attributes);
16535 else
16536 decl = start_decl (declarator, &decl_specs, 0,
16537 attributes, NULL_TREE, &pushed_scope);
16538 if (decl == error_mark_node)
16539 return decl;
16541 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16543 if (pushed_scope)
16544 pop_scope (pushed_scope);
16546 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16547 added into the symbol table; otherwise, return the TYPE_DECL. */
16548 if (DECL_LANG_SPECIFIC (decl)
16549 && DECL_TEMPLATE_INFO (decl)
16550 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16552 decl = DECL_TI_TEMPLATE (decl);
16553 if (member_p)
16554 check_member_template (decl);
16557 return decl;
16560 /* Parse a using-directive.
16562 using-directive:
16563 using namespace :: [opt] nested-name-specifier [opt]
16564 namespace-name ; */
16566 static void
16567 cp_parser_using_directive (cp_parser* parser)
16569 tree namespace_decl;
16570 tree attribs;
16572 /* Look for the `using' keyword. */
16573 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16574 /* And the `namespace' keyword. */
16575 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16576 /* Look for the optional `::' operator. */
16577 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16578 /* And the optional nested-name-specifier. */
16579 cp_parser_nested_name_specifier_opt (parser,
16580 /*typename_keyword_p=*/false,
16581 /*check_dependency_p=*/true,
16582 /*type_p=*/false,
16583 /*is_declaration=*/true);
16584 /* Get the namespace being used. */
16585 namespace_decl = cp_parser_namespace_name (parser);
16586 /* And any specified attributes. */
16587 attribs = cp_parser_attributes_opt (parser);
16588 /* Update the symbol table. */
16589 parse_using_directive (namespace_decl, attribs);
16590 /* Look for the final `;'. */
16591 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16594 /* Parse an asm-definition.
16596 asm-definition:
16597 asm ( string-literal ) ;
16599 GNU Extension:
16601 asm-definition:
16602 asm volatile [opt] ( string-literal ) ;
16603 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16604 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16605 : asm-operand-list [opt] ) ;
16606 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16607 : asm-operand-list [opt]
16608 : asm-clobber-list [opt] ) ;
16609 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16610 : asm-clobber-list [opt]
16611 : asm-goto-list ) ; */
16613 static void
16614 cp_parser_asm_definition (cp_parser* parser)
16616 tree string;
16617 tree outputs = NULL_TREE;
16618 tree inputs = NULL_TREE;
16619 tree clobbers = NULL_TREE;
16620 tree labels = NULL_TREE;
16621 tree asm_stmt;
16622 bool volatile_p = false;
16623 bool extended_p = false;
16624 bool invalid_inputs_p = false;
16625 bool invalid_outputs_p = false;
16626 bool goto_p = false;
16627 required_token missing = RT_NONE;
16629 /* Look for the `asm' keyword. */
16630 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16632 if (parser->in_function_body
16633 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16635 error ("%<asm%> in %<constexpr%> function");
16636 cp_function_chain->invalid_constexpr = true;
16639 /* See if the next token is `volatile'. */
16640 if (cp_parser_allow_gnu_extensions_p (parser)
16641 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16643 /* Remember that we saw the `volatile' keyword. */
16644 volatile_p = true;
16645 /* Consume the token. */
16646 cp_lexer_consume_token (parser->lexer);
16648 if (cp_parser_allow_gnu_extensions_p (parser)
16649 && parser->in_function_body
16650 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16652 /* Remember that we saw the `goto' keyword. */
16653 goto_p = true;
16654 /* Consume the token. */
16655 cp_lexer_consume_token (parser->lexer);
16657 /* Look for the opening `('. */
16658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16659 return;
16660 /* Look for the string. */
16661 string = cp_parser_string_literal (parser, false, false);
16662 if (string == error_mark_node)
16664 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16665 /*consume_paren=*/true);
16666 return;
16669 /* If we're allowing GNU extensions, check for the extended assembly
16670 syntax. Unfortunately, the `:' tokens need not be separated by
16671 a space in C, and so, for compatibility, we tolerate that here
16672 too. Doing that means that we have to treat the `::' operator as
16673 two `:' tokens. */
16674 if (cp_parser_allow_gnu_extensions_p (parser)
16675 && parser->in_function_body
16676 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16677 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16679 bool inputs_p = false;
16680 bool clobbers_p = false;
16681 bool labels_p = false;
16683 /* The extended syntax was used. */
16684 extended_p = true;
16686 /* Look for outputs. */
16687 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16689 /* Consume the `:'. */
16690 cp_lexer_consume_token (parser->lexer);
16691 /* Parse the output-operands. */
16692 if (cp_lexer_next_token_is_not (parser->lexer,
16693 CPP_COLON)
16694 && cp_lexer_next_token_is_not (parser->lexer,
16695 CPP_SCOPE)
16696 && cp_lexer_next_token_is_not (parser->lexer,
16697 CPP_CLOSE_PAREN)
16698 && !goto_p)
16699 outputs = cp_parser_asm_operand_list (parser);
16701 if (outputs == error_mark_node)
16702 invalid_outputs_p = true;
16704 /* If the next token is `::', there are no outputs, and the
16705 next token is the beginning of the inputs. */
16706 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16707 /* The inputs are coming next. */
16708 inputs_p = true;
16710 /* Look for inputs. */
16711 if (inputs_p
16712 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16714 /* Consume the `:' or `::'. */
16715 cp_lexer_consume_token (parser->lexer);
16716 /* Parse the output-operands. */
16717 if (cp_lexer_next_token_is_not (parser->lexer,
16718 CPP_COLON)
16719 && cp_lexer_next_token_is_not (parser->lexer,
16720 CPP_SCOPE)
16721 && cp_lexer_next_token_is_not (parser->lexer,
16722 CPP_CLOSE_PAREN))
16723 inputs = cp_parser_asm_operand_list (parser);
16725 if (inputs == error_mark_node)
16726 invalid_inputs_p = true;
16728 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16729 /* The clobbers are coming next. */
16730 clobbers_p = true;
16732 /* Look for clobbers. */
16733 if (clobbers_p
16734 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16736 clobbers_p = true;
16737 /* Consume the `:' or `::'. */
16738 cp_lexer_consume_token (parser->lexer);
16739 /* Parse the clobbers. */
16740 if (cp_lexer_next_token_is_not (parser->lexer,
16741 CPP_COLON)
16742 && cp_lexer_next_token_is_not (parser->lexer,
16743 CPP_CLOSE_PAREN))
16744 clobbers = cp_parser_asm_clobber_list (parser);
16746 else if (goto_p
16747 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16748 /* The labels are coming next. */
16749 labels_p = true;
16751 /* Look for labels. */
16752 if (labels_p
16753 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16755 labels_p = true;
16756 /* Consume the `:' or `::'. */
16757 cp_lexer_consume_token (parser->lexer);
16758 /* Parse the labels. */
16759 labels = cp_parser_asm_label_list (parser);
16762 if (goto_p && !labels_p)
16763 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16765 else if (goto_p)
16766 missing = RT_COLON_SCOPE;
16768 /* Look for the closing `)'. */
16769 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16770 missing ? missing : RT_CLOSE_PAREN))
16771 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16772 /*consume_paren=*/true);
16773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16775 if (!invalid_inputs_p && !invalid_outputs_p)
16777 /* Create the ASM_EXPR. */
16778 if (parser->in_function_body)
16780 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16781 inputs, clobbers, labels);
16782 /* If the extended syntax was not used, mark the ASM_EXPR. */
16783 if (!extended_p)
16785 tree temp = asm_stmt;
16786 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16787 temp = TREE_OPERAND (temp, 0);
16789 ASM_INPUT_P (temp) = 1;
16792 else
16793 symtab->finalize_toplevel_asm (string);
16797 /* Declarators [gram.dcl.decl] */
16799 /* Parse an init-declarator.
16801 init-declarator:
16802 declarator initializer [opt]
16804 GNU Extension:
16806 init-declarator:
16807 declarator asm-specification [opt] attributes [opt] initializer [opt]
16809 function-definition:
16810 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16811 function-body
16812 decl-specifier-seq [opt] declarator function-try-block
16814 GNU Extension:
16816 function-definition:
16817 __extension__ function-definition
16819 TM Extension:
16821 function-definition:
16822 decl-specifier-seq [opt] declarator function-transaction-block
16824 The DECL_SPECIFIERS apply to this declarator. Returns a
16825 representation of the entity declared. If MEMBER_P is TRUE, then
16826 this declarator appears in a class scope. The new DECL created by
16827 this declarator is returned.
16829 The CHECKS are access checks that should be performed once we know
16830 what entity is being declared (and, therefore, what classes have
16831 befriended it).
16833 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16834 for a function-definition here as well. If the declarator is a
16835 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16836 be TRUE upon return. By that point, the function-definition will
16837 have been completely parsed.
16839 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16840 is FALSE.
16842 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16843 parsed declaration if it is an uninitialized single declarator not followed
16844 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16845 if present, will not be consumed. If returned, this declarator will be
16846 created with SD_INITIALIZED but will not call cp_finish_decl. */
16848 static tree
16849 cp_parser_init_declarator (cp_parser* parser,
16850 cp_decl_specifier_seq *decl_specifiers,
16851 vec<deferred_access_check, va_gc> *checks,
16852 bool function_definition_allowed_p,
16853 bool member_p,
16854 int declares_class_or_enum,
16855 bool* function_definition_p,
16856 tree* maybe_range_for_decl)
16858 cp_token *token = NULL, *asm_spec_start_token = NULL,
16859 *attributes_start_token = NULL;
16860 cp_declarator *declarator;
16861 tree prefix_attributes;
16862 tree attributes = NULL;
16863 tree asm_specification;
16864 tree initializer;
16865 tree decl = NULL_TREE;
16866 tree scope;
16867 int is_initialized;
16868 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16869 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16870 "(...)". */
16871 enum cpp_ttype initialization_kind;
16872 bool is_direct_init = false;
16873 bool is_non_constant_init;
16874 int ctor_dtor_or_conv_p;
16875 bool friend_p = cp_parser_friend_p (decl_specifiers);
16876 tree pushed_scope = NULL_TREE;
16877 bool range_for_decl_p = false;
16878 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16880 /* Gather the attributes that were provided with the
16881 decl-specifiers. */
16882 prefix_attributes = decl_specifiers->attributes;
16884 /* Assume that this is not the declarator for a function
16885 definition. */
16886 if (function_definition_p)
16887 *function_definition_p = false;
16889 /* Default arguments are only permitted for function parameters. */
16890 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16891 parser->default_arg_ok_p = false;
16893 /* Defer access checks while parsing the declarator; we cannot know
16894 what names are accessible until we know what is being
16895 declared. */
16896 resume_deferring_access_checks ();
16898 /* Parse the declarator. */
16899 token = cp_lexer_peek_token (parser->lexer);
16900 declarator
16901 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16902 &ctor_dtor_or_conv_p,
16903 /*parenthesized_p=*/NULL,
16904 member_p, friend_p);
16905 /* Gather up the deferred checks. */
16906 stop_deferring_access_checks ();
16908 parser->default_arg_ok_p = saved_default_arg_ok_p;
16910 /* If the DECLARATOR was erroneous, there's no need to go
16911 further. */
16912 if (declarator == cp_error_declarator)
16913 return error_mark_node;
16915 /* Check that the number of template-parameter-lists is OK. */
16916 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16917 token->location))
16918 return error_mark_node;
16920 if (declares_class_or_enum & 2)
16921 cp_parser_check_for_definition_in_return_type (declarator,
16922 decl_specifiers->type,
16923 decl_specifiers->locations[ds_type_spec]);
16925 /* Figure out what scope the entity declared by the DECLARATOR is
16926 located in. `grokdeclarator' sometimes changes the scope, so
16927 we compute it now. */
16928 scope = get_scope_of_declarator (declarator);
16930 /* Perform any lookups in the declared type which were thought to be
16931 dependent, but are not in the scope of the declarator. */
16932 decl_specifiers->type
16933 = maybe_update_decl_type (decl_specifiers->type, scope);
16935 /* If we're allowing GNU extensions, look for an
16936 asm-specification. */
16937 if (cp_parser_allow_gnu_extensions_p (parser))
16939 /* Look for an asm-specification. */
16940 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16941 asm_specification = cp_parser_asm_specification_opt (parser);
16943 else
16944 asm_specification = NULL_TREE;
16946 /* Look for attributes. */
16947 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16948 attributes = cp_parser_attributes_opt (parser);
16950 /* Peek at the next token. */
16951 token = cp_lexer_peek_token (parser->lexer);
16953 bool bogus_implicit_tmpl = false;
16955 if (function_declarator_p (declarator))
16957 /* Check to see if the token indicates the start of a
16958 function-definition. */
16959 if (cp_parser_token_starts_function_definition_p (token))
16961 if (!function_definition_allowed_p)
16963 /* If a function-definition should not appear here, issue an
16964 error message. */
16965 cp_parser_error (parser,
16966 "a function-definition is not allowed here");
16967 return error_mark_node;
16970 location_t func_brace_location
16971 = cp_lexer_peek_token (parser->lexer)->location;
16973 /* Neither attributes nor an asm-specification are allowed
16974 on a function-definition. */
16975 if (asm_specification)
16976 error_at (asm_spec_start_token->location,
16977 "an asm-specification is not allowed "
16978 "on a function-definition");
16979 if (attributes)
16980 error_at (attributes_start_token->location,
16981 "attributes are not allowed "
16982 "on a function-definition");
16983 /* This is a function-definition. */
16984 *function_definition_p = true;
16986 /* Parse the function definition. */
16987 if (member_p)
16988 decl = cp_parser_save_member_function_body (parser,
16989 decl_specifiers,
16990 declarator,
16991 prefix_attributes);
16992 else
16993 decl =
16994 (cp_parser_function_definition_from_specifiers_and_declarator
16995 (parser, decl_specifiers, prefix_attributes, declarator));
16997 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16999 /* This is where the prologue starts... */
17000 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17001 = func_brace_location;
17004 return decl;
17007 else if (parser->fully_implicit_function_template_p)
17009 /* A non-template declaration involving a function parameter list
17010 containing an implicit template parameter will be made into a
17011 template. If the resulting declaration is not going to be an
17012 actual function then finish the template scope here to prevent it.
17013 An error message will be issued once we have a decl to talk about.
17015 FIXME probably we should do type deduction rather than create an
17016 implicit template, but the standard currently doesn't allow it. */
17017 bogus_implicit_tmpl = true;
17018 finish_fully_implicit_template (parser, NULL_TREE);
17021 /* [dcl.dcl]
17023 Only in function declarations for constructors, destructors, and
17024 type conversions can the decl-specifier-seq be omitted.
17026 We explicitly postpone this check past the point where we handle
17027 function-definitions because we tolerate function-definitions
17028 that are missing their return types in some modes. */
17029 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17031 cp_parser_error (parser,
17032 "expected constructor, destructor, or type conversion");
17033 return error_mark_node;
17036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17037 if (token->type == CPP_EQ
17038 || token->type == CPP_OPEN_PAREN
17039 || token->type == CPP_OPEN_BRACE)
17041 is_initialized = SD_INITIALIZED;
17042 initialization_kind = token->type;
17043 if (maybe_range_for_decl)
17044 *maybe_range_for_decl = error_mark_node;
17046 if (token->type == CPP_EQ
17047 && function_declarator_p (declarator))
17049 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17050 if (t2->keyword == RID_DEFAULT)
17051 is_initialized = SD_DEFAULTED;
17052 else if (t2->keyword == RID_DELETE)
17053 is_initialized = SD_DELETED;
17056 else
17058 /* If the init-declarator isn't initialized and isn't followed by a
17059 `,' or `;', it's not a valid init-declarator. */
17060 if (token->type != CPP_COMMA
17061 && token->type != CPP_SEMICOLON)
17063 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17064 range_for_decl_p = true;
17065 else
17067 cp_parser_error (parser, "expected initializer");
17068 return error_mark_node;
17071 is_initialized = SD_UNINITIALIZED;
17072 initialization_kind = CPP_EOF;
17075 /* Because start_decl has side-effects, we should only call it if we
17076 know we're going ahead. By this point, we know that we cannot
17077 possibly be looking at any other construct. */
17078 cp_parser_commit_to_tentative_parse (parser);
17080 /* Enter the newly declared entry in the symbol table. If we're
17081 processing a declaration in a class-specifier, we wait until
17082 after processing the initializer. */
17083 if (!member_p)
17085 if (parser->in_unbraced_linkage_specification_p)
17086 decl_specifiers->storage_class = sc_extern;
17087 decl = start_decl (declarator, decl_specifiers,
17088 range_for_decl_p? SD_INITIALIZED : is_initialized,
17089 attributes, prefix_attributes, &pushed_scope);
17090 cp_finalize_omp_declare_simd (parser, decl);
17091 /* Adjust location of decl if declarator->id_loc is more appropriate:
17092 set, and decl wasn't merged with another decl, in which case its
17093 location would be different from input_location, and more accurate. */
17094 if (DECL_P (decl)
17095 && declarator->id_loc != UNKNOWN_LOCATION
17096 && DECL_SOURCE_LOCATION (decl) == input_location)
17097 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17099 else if (scope)
17100 /* Enter the SCOPE. That way unqualified names appearing in the
17101 initializer will be looked up in SCOPE. */
17102 pushed_scope = push_scope (scope);
17104 /* Perform deferred access control checks, now that we know in which
17105 SCOPE the declared entity resides. */
17106 if (!member_p && decl)
17108 tree saved_current_function_decl = NULL_TREE;
17110 /* If the entity being declared is a function, pretend that we
17111 are in its scope. If it is a `friend', it may have access to
17112 things that would not otherwise be accessible. */
17113 if (TREE_CODE (decl) == FUNCTION_DECL)
17115 saved_current_function_decl = current_function_decl;
17116 current_function_decl = decl;
17119 /* Perform access checks for template parameters. */
17120 cp_parser_perform_template_parameter_access_checks (checks);
17122 /* Perform the access control checks for the declarator and the
17123 decl-specifiers. */
17124 perform_deferred_access_checks (tf_warning_or_error);
17126 /* Restore the saved value. */
17127 if (TREE_CODE (decl) == FUNCTION_DECL)
17128 current_function_decl = saved_current_function_decl;
17131 /* Parse the initializer. */
17132 initializer = NULL_TREE;
17133 is_direct_init = false;
17134 is_non_constant_init = true;
17135 if (is_initialized)
17137 if (function_declarator_p (declarator))
17139 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17140 if (initialization_kind == CPP_EQ)
17141 initializer = cp_parser_pure_specifier (parser);
17142 else
17144 /* If the declaration was erroneous, we don't really
17145 know what the user intended, so just silently
17146 consume the initializer. */
17147 if (decl != error_mark_node)
17148 error_at (initializer_start_token->location,
17149 "initializer provided for function");
17150 cp_parser_skip_to_closing_parenthesis (parser,
17151 /*recovering=*/true,
17152 /*or_comma=*/false,
17153 /*consume_paren=*/true);
17156 else
17158 /* We want to record the extra mangling scope for in-class
17159 initializers of class members and initializers of static data
17160 member templates. The former involves deferring
17161 parsing of the initializer until end of class as with default
17162 arguments. So right here we only handle the latter. */
17163 if (!member_p && processing_template_decl)
17164 start_lambda_scope (decl);
17165 initializer = cp_parser_initializer (parser,
17166 &is_direct_init,
17167 &is_non_constant_init);
17168 if (!member_p && processing_template_decl)
17169 finish_lambda_scope ();
17170 if (initializer == error_mark_node)
17171 cp_parser_skip_to_end_of_statement (parser);
17175 /* The old parser allows attributes to appear after a parenthesized
17176 initializer. Mark Mitchell proposed removing this functionality
17177 on the GCC mailing lists on 2002-08-13. This parser accepts the
17178 attributes -- but ignores them. */
17179 if (cp_parser_allow_gnu_extensions_p (parser)
17180 && initialization_kind == CPP_OPEN_PAREN)
17181 if (cp_parser_attributes_opt (parser))
17182 warning (OPT_Wattributes,
17183 "attributes after parenthesized initializer ignored");
17185 /* And now complain about a non-function implicit template. */
17186 if (bogus_implicit_tmpl)
17187 error_at (DECL_SOURCE_LOCATION (decl),
17188 "non-function %qD declared as implicit template", decl);
17190 /* For an in-class declaration, use `grokfield' to create the
17191 declaration. */
17192 if (member_p)
17194 if (pushed_scope)
17196 pop_scope (pushed_scope);
17197 pushed_scope = NULL_TREE;
17199 decl = grokfield (declarator, decl_specifiers,
17200 initializer, !is_non_constant_init,
17201 /*asmspec=*/NULL_TREE,
17202 chainon (attributes, prefix_attributes));
17203 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17204 cp_parser_save_default_args (parser, decl);
17205 cp_finalize_omp_declare_simd (parser, decl);
17208 /* Finish processing the declaration. But, skip member
17209 declarations. */
17210 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17212 cp_finish_decl (decl,
17213 initializer, !is_non_constant_init,
17214 asm_specification,
17215 /* If the initializer is in parentheses, then this is
17216 a direct-initialization, which means that an
17217 `explicit' constructor is OK. Otherwise, an
17218 `explicit' constructor cannot be used. */
17219 ((is_direct_init || !is_initialized)
17220 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17222 else if ((cxx_dialect != cxx98) && friend_p
17223 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17224 /* Core issue #226 (C++0x only): A default template-argument
17225 shall not be specified in a friend class template
17226 declaration. */
17227 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17228 /*is_partial=*/false, /*is_friend_decl=*/1);
17230 if (!friend_p && pushed_scope)
17231 pop_scope (pushed_scope);
17233 if (function_declarator_p (declarator)
17234 && parser->fully_implicit_function_template_p)
17236 if (member_p)
17237 decl = finish_fully_implicit_template (parser, decl);
17238 else
17239 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17242 return decl;
17245 /* Parse a declarator.
17247 declarator:
17248 direct-declarator
17249 ptr-operator declarator
17251 abstract-declarator:
17252 ptr-operator abstract-declarator [opt]
17253 direct-abstract-declarator
17255 GNU Extensions:
17257 declarator:
17258 attributes [opt] direct-declarator
17259 attributes [opt] ptr-operator declarator
17261 abstract-declarator:
17262 attributes [opt] ptr-operator abstract-declarator [opt]
17263 attributes [opt] direct-abstract-declarator
17265 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17266 detect constructor, destructor or conversion operators. It is set
17267 to -1 if the declarator is a name, and +1 if it is a
17268 function. Otherwise it is set to zero. Usually you just want to
17269 test for >0, but internally the negative value is used.
17271 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17272 a decl-specifier-seq unless it declares a constructor, destructor,
17273 or conversion. It might seem that we could check this condition in
17274 semantic analysis, rather than parsing, but that makes it difficult
17275 to handle something like `f()'. We want to notice that there are
17276 no decl-specifiers, and therefore realize that this is an
17277 expression, not a declaration.)
17279 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17280 the declarator is a direct-declarator of the form "(...)".
17282 MEMBER_P is true iff this declarator is a member-declarator.
17284 FRIEND_P is true iff this declarator is a friend. */
17286 static cp_declarator *
17287 cp_parser_declarator (cp_parser* parser,
17288 cp_parser_declarator_kind dcl_kind,
17289 int* ctor_dtor_or_conv_p,
17290 bool* parenthesized_p,
17291 bool member_p, bool friend_p)
17293 cp_declarator *declarator;
17294 enum tree_code code;
17295 cp_cv_quals cv_quals;
17296 tree class_type;
17297 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17299 /* Assume this is not a constructor, destructor, or type-conversion
17300 operator. */
17301 if (ctor_dtor_or_conv_p)
17302 *ctor_dtor_or_conv_p = 0;
17304 if (cp_parser_allow_gnu_extensions_p (parser))
17305 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17307 /* Check for the ptr-operator production. */
17308 cp_parser_parse_tentatively (parser);
17309 /* Parse the ptr-operator. */
17310 code = cp_parser_ptr_operator (parser,
17311 &class_type,
17312 &cv_quals,
17313 &std_attributes);
17315 /* If that worked, then we have a ptr-operator. */
17316 if (cp_parser_parse_definitely (parser))
17318 /* If a ptr-operator was found, then this declarator was not
17319 parenthesized. */
17320 if (parenthesized_p)
17321 *parenthesized_p = true;
17322 /* The dependent declarator is optional if we are parsing an
17323 abstract-declarator. */
17324 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17325 cp_parser_parse_tentatively (parser);
17327 /* Parse the dependent declarator. */
17328 declarator = cp_parser_declarator (parser, dcl_kind,
17329 /*ctor_dtor_or_conv_p=*/NULL,
17330 /*parenthesized_p=*/NULL,
17331 /*member_p=*/false,
17332 friend_p);
17334 /* If we are parsing an abstract-declarator, we must handle the
17335 case where the dependent declarator is absent. */
17336 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17337 && !cp_parser_parse_definitely (parser))
17338 declarator = NULL;
17340 declarator = cp_parser_make_indirect_declarator
17341 (code, class_type, cv_quals, declarator, std_attributes);
17343 /* Everything else is a direct-declarator. */
17344 else
17346 if (parenthesized_p)
17347 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17348 CPP_OPEN_PAREN);
17349 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17350 ctor_dtor_or_conv_p,
17351 member_p, friend_p);
17354 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17355 declarator->attributes = gnu_attributes;
17356 return declarator;
17359 /* Parse a direct-declarator or direct-abstract-declarator.
17361 direct-declarator:
17362 declarator-id
17363 direct-declarator ( parameter-declaration-clause )
17364 cv-qualifier-seq [opt]
17365 ref-qualifier [opt]
17366 exception-specification [opt]
17367 direct-declarator [ constant-expression [opt] ]
17368 ( declarator )
17370 direct-abstract-declarator:
17371 direct-abstract-declarator [opt]
17372 ( parameter-declaration-clause )
17373 cv-qualifier-seq [opt]
17374 ref-qualifier [opt]
17375 exception-specification [opt]
17376 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17377 ( abstract-declarator )
17379 Returns a representation of the declarator. DCL_KIND is
17380 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17381 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17382 we are parsing a direct-declarator. It is
17383 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17384 of ambiguity we prefer an abstract declarator, as per
17385 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17386 as for cp_parser_declarator. */
17388 static cp_declarator *
17389 cp_parser_direct_declarator (cp_parser* parser,
17390 cp_parser_declarator_kind dcl_kind,
17391 int* ctor_dtor_or_conv_p,
17392 bool member_p, bool friend_p)
17394 cp_token *token;
17395 cp_declarator *declarator = NULL;
17396 tree scope = NULL_TREE;
17397 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17398 bool saved_in_declarator_p = parser->in_declarator_p;
17399 bool first = true;
17400 tree pushed_scope = NULL_TREE;
17402 while (true)
17404 /* Peek at the next token. */
17405 token = cp_lexer_peek_token (parser->lexer);
17406 if (token->type == CPP_OPEN_PAREN)
17408 /* This is either a parameter-declaration-clause, or a
17409 parenthesized declarator. When we know we are parsing a
17410 named declarator, it must be a parenthesized declarator
17411 if FIRST is true. For instance, `(int)' is a
17412 parameter-declaration-clause, with an omitted
17413 direct-abstract-declarator. But `((*))', is a
17414 parenthesized abstract declarator. Finally, when T is a
17415 template parameter `(T)' is a
17416 parameter-declaration-clause, and not a parenthesized
17417 named declarator.
17419 We first try and parse a parameter-declaration-clause,
17420 and then try a nested declarator (if FIRST is true).
17422 It is not an error for it not to be a
17423 parameter-declaration-clause, even when FIRST is
17424 false. Consider,
17426 int i (int);
17427 int i (3);
17429 The first is the declaration of a function while the
17430 second is the definition of a variable, including its
17431 initializer.
17433 Having seen only the parenthesis, we cannot know which of
17434 these two alternatives should be selected. Even more
17435 complex are examples like:
17437 int i (int (a));
17438 int i (int (3));
17440 The former is a function-declaration; the latter is a
17441 variable initialization.
17443 Thus again, we try a parameter-declaration-clause, and if
17444 that fails, we back out and return. */
17446 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17448 tree params;
17449 bool is_declarator = false;
17451 /* In a member-declarator, the only valid interpretation
17452 of a parenthesis is the start of a
17453 parameter-declaration-clause. (It is invalid to
17454 initialize a static data member with a parenthesized
17455 initializer; only the "=" form of initialization is
17456 permitted.) */
17457 if (!member_p)
17458 cp_parser_parse_tentatively (parser);
17460 /* Consume the `('. */
17461 cp_lexer_consume_token (parser->lexer);
17462 if (first)
17464 /* If this is going to be an abstract declarator, we're
17465 in a declarator and we can't have default args. */
17466 parser->default_arg_ok_p = false;
17467 parser->in_declarator_p = true;
17470 begin_scope (sk_function_parms, NULL_TREE);
17472 /* Parse the parameter-declaration-clause. */
17473 params = cp_parser_parameter_declaration_clause (parser);
17475 /* Consume the `)'. */
17476 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17478 /* If all went well, parse the cv-qualifier-seq,
17479 ref-qualifier and the exception-specification. */
17480 if (member_p || cp_parser_parse_definitely (parser))
17482 cp_cv_quals cv_quals;
17483 cp_virt_specifiers virt_specifiers;
17484 cp_ref_qualifier ref_qual;
17485 tree exception_specification;
17486 tree late_return;
17487 tree attrs;
17488 bool memfn = (member_p || (pushed_scope
17489 && CLASS_TYPE_P (pushed_scope)));
17491 is_declarator = true;
17493 if (ctor_dtor_or_conv_p)
17494 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17495 first = false;
17497 /* Parse the cv-qualifier-seq. */
17498 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17499 /* Parse the ref-qualifier. */
17500 ref_qual = cp_parser_ref_qualifier_opt (parser);
17501 /* And the exception-specification. */
17502 exception_specification
17503 = cp_parser_exception_specification_opt (parser);
17505 attrs = cp_parser_std_attribute_spec_seq (parser);
17507 /* In here, we handle cases where attribute is used after
17508 the function declaration. For example:
17509 void func (int x) __attribute__((vector(..))); */
17510 if (flag_cilkplus
17511 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17513 cp_parser_parse_tentatively (parser);
17514 tree attr = cp_parser_gnu_attributes_opt (parser);
17515 if (cp_lexer_next_token_is_not (parser->lexer,
17516 CPP_SEMICOLON)
17517 && cp_lexer_next_token_is_not (parser->lexer,
17518 CPP_OPEN_BRACE))
17519 cp_parser_abort_tentative_parse (parser);
17520 else if (!cp_parser_parse_definitely (parser))
17522 else
17523 attrs = chainon (attr, attrs);
17525 late_return = (cp_parser_late_return_type_opt
17526 (parser, declarator,
17527 memfn ? cv_quals : -1));
17530 /* Parse the virt-specifier-seq. */
17531 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17533 /* Create the function-declarator. */
17534 declarator = make_call_declarator (declarator,
17535 params,
17536 cv_quals,
17537 virt_specifiers,
17538 ref_qual,
17539 exception_specification,
17540 late_return);
17541 declarator->std_attributes = attrs;
17542 /* Any subsequent parameter lists are to do with
17543 return type, so are not those of the declared
17544 function. */
17545 parser->default_arg_ok_p = false;
17548 /* Remove the function parms from scope. */
17549 pop_bindings_and_leave_scope ();
17551 if (is_declarator)
17552 /* Repeat the main loop. */
17553 continue;
17556 /* If this is the first, we can try a parenthesized
17557 declarator. */
17558 if (first)
17560 bool saved_in_type_id_in_expr_p;
17562 parser->default_arg_ok_p = saved_default_arg_ok_p;
17563 parser->in_declarator_p = saved_in_declarator_p;
17565 /* Consume the `('. */
17566 cp_lexer_consume_token (parser->lexer);
17567 /* Parse the nested declarator. */
17568 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17569 parser->in_type_id_in_expr_p = true;
17570 declarator
17571 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17572 /*parenthesized_p=*/NULL,
17573 member_p, friend_p);
17574 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17575 first = false;
17576 /* Expect a `)'. */
17577 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17578 declarator = cp_error_declarator;
17579 if (declarator == cp_error_declarator)
17580 break;
17582 goto handle_declarator;
17584 /* Otherwise, we must be done. */
17585 else
17586 break;
17588 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17589 && token->type == CPP_OPEN_SQUARE
17590 && !cp_next_tokens_can_be_attribute_p (parser))
17592 /* Parse an array-declarator. */
17593 tree bounds, attrs;
17595 if (ctor_dtor_or_conv_p)
17596 *ctor_dtor_or_conv_p = 0;
17598 first = false;
17599 parser->default_arg_ok_p = false;
17600 parser->in_declarator_p = true;
17601 /* Consume the `['. */
17602 cp_lexer_consume_token (parser->lexer);
17603 /* Peek at the next token. */
17604 token = cp_lexer_peek_token (parser->lexer);
17605 /* If the next token is `]', then there is no
17606 constant-expression. */
17607 if (token->type != CPP_CLOSE_SQUARE)
17609 bool non_constant_p;
17610 bounds
17611 = cp_parser_constant_expression (parser,
17612 /*allow_non_constant=*/true,
17613 &non_constant_p);
17614 if (!non_constant_p)
17615 /* OK */;
17616 else if (error_operand_p (bounds))
17617 /* Already gave an error. */;
17618 else if (!parser->in_function_body
17619 || current_binding_level->kind == sk_function_parms)
17621 /* Normally, the array bound must be an integral constant
17622 expression. However, as an extension, we allow VLAs
17623 in function scopes as long as they aren't part of a
17624 parameter declaration. */
17625 cp_parser_error (parser,
17626 "array bound is not an integer constant");
17627 bounds = error_mark_node;
17629 else if (processing_template_decl
17630 && !type_dependent_expression_p (bounds))
17632 /* Remember this wasn't a constant-expression. */
17633 bounds = build_nop (TREE_TYPE (bounds), bounds);
17634 TREE_SIDE_EFFECTS (bounds) = 1;
17637 else
17638 bounds = NULL_TREE;
17639 /* Look for the closing `]'. */
17640 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17642 declarator = cp_error_declarator;
17643 break;
17646 attrs = cp_parser_std_attribute_spec_seq (parser);
17647 declarator = make_array_declarator (declarator, bounds);
17648 declarator->std_attributes = attrs;
17650 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17653 tree qualifying_scope;
17654 tree unqualified_name;
17655 tree attrs;
17656 special_function_kind sfk;
17657 bool abstract_ok;
17658 bool pack_expansion_p = false;
17659 cp_token *declarator_id_start_token;
17661 /* Parse a declarator-id */
17662 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17663 if (abstract_ok)
17665 cp_parser_parse_tentatively (parser);
17667 /* If we see an ellipsis, we should be looking at a
17668 parameter pack. */
17669 if (token->type == CPP_ELLIPSIS)
17671 /* Consume the `...' */
17672 cp_lexer_consume_token (parser->lexer);
17674 pack_expansion_p = true;
17678 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17679 unqualified_name
17680 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17681 qualifying_scope = parser->scope;
17682 if (abstract_ok)
17684 bool okay = false;
17686 if (!unqualified_name && pack_expansion_p)
17688 /* Check whether an error occurred. */
17689 okay = !cp_parser_error_occurred (parser);
17691 /* We already consumed the ellipsis to mark a
17692 parameter pack, but we have no way to report it,
17693 so abort the tentative parse. We will be exiting
17694 immediately anyway. */
17695 cp_parser_abort_tentative_parse (parser);
17697 else
17698 okay = cp_parser_parse_definitely (parser);
17700 if (!okay)
17701 unqualified_name = error_mark_node;
17702 else if (unqualified_name
17703 && (qualifying_scope
17704 || (!identifier_p (unqualified_name))))
17706 cp_parser_error (parser, "expected unqualified-id");
17707 unqualified_name = error_mark_node;
17711 if (!unqualified_name)
17712 return NULL;
17713 if (unqualified_name == error_mark_node)
17715 declarator = cp_error_declarator;
17716 pack_expansion_p = false;
17717 declarator->parameter_pack_p = false;
17718 break;
17721 attrs = cp_parser_std_attribute_spec_seq (parser);
17723 if (qualifying_scope && at_namespace_scope_p ()
17724 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17726 /* In the declaration of a member of a template class
17727 outside of the class itself, the SCOPE will sometimes
17728 be a TYPENAME_TYPE. For example, given:
17730 template <typename T>
17731 int S<T>::R::i = 3;
17733 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17734 this context, we must resolve S<T>::R to an ordinary
17735 type, rather than a typename type.
17737 The reason we normally avoid resolving TYPENAME_TYPEs
17738 is that a specialization of `S' might render
17739 `S<T>::R' not a type. However, if `S' is
17740 specialized, then this `i' will not be used, so there
17741 is no harm in resolving the types here. */
17742 tree type;
17744 /* Resolve the TYPENAME_TYPE. */
17745 type = resolve_typename_type (qualifying_scope,
17746 /*only_current_p=*/false);
17747 /* If that failed, the declarator is invalid. */
17748 if (TREE_CODE (type) == TYPENAME_TYPE)
17750 if (typedef_variant_p (type))
17751 error_at (declarator_id_start_token->location,
17752 "cannot define member of dependent typedef "
17753 "%qT", type);
17754 else
17755 error_at (declarator_id_start_token->location,
17756 "%<%T::%E%> is not a type",
17757 TYPE_CONTEXT (qualifying_scope),
17758 TYPE_IDENTIFIER (qualifying_scope));
17760 qualifying_scope = type;
17763 sfk = sfk_none;
17765 if (unqualified_name)
17767 tree class_type;
17769 if (qualifying_scope
17770 && CLASS_TYPE_P (qualifying_scope))
17771 class_type = qualifying_scope;
17772 else
17773 class_type = current_class_type;
17775 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17777 tree name_type = TREE_TYPE (unqualified_name);
17778 if (class_type && same_type_p (name_type, class_type))
17780 if (qualifying_scope
17781 && CLASSTYPE_USE_TEMPLATE (name_type))
17783 error_at (declarator_id_start_token->location,
17784 "invalid use of constructor as a template");
17785 inform (declarator_id_start_token->location,
17786 "use %<%T::%D%> instead of %<%T::%D%> to "
17787 "name the constructor in a qualified name",
17788 class_type,
17789 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17790 class_type, name_type);
17791 declarator = cp_error_declarator;
17792 break;
17794 else
17795 unqualified_name = constructor_name (class_type);
17797 else
17799 /* We do not attempt to print the declarator
17800 here because we do not have enough
17801 information about its original syntactic
17802 form. */
17803 cp_parser_error (parser, "invalid declarator");
17804 declarator = cp_error_declarator;
17805 break;
17809 if (class_type)
17811 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17812 sfk = sfk_destructor;
17813 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17814 sfk = sfk_conversion;
17815 else if (/* There's no way to declare a constructor
17816 for an anonymous type, even if the type
17817 got a name for linkage purposes. */
17818 !TYPE_WAS_ANONYMOUS (class_type)
17819 /* Handle correctly (c++/19200):
17821 struct S {
17822 struct T{};
17823 friend void S(T);
17826 and also:
17828 namespace N {
17829 void S();
17832 struct S {
17833 friend void N::S();
17834 }; */
17835 && !(friend_p
17836 && class_type != qualifying_scope)
17837 && constructor_name_p (unqualified_name,
17838 class_type))
17840 unqualified_name = constructor_name (class_type);
17841 sfk = sfk_constructor;
17843 else if (is_overloaded_fn (unqualified_name)
17844 && DECL_CONSTRUCTOR_P (get_first_fn
17845 (unqualified_name)))
17846 sfk = sfk_constructor;
17848 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17849 *ctor_dtor_or_conv_p = -1;
17852 declarator = make_id_declarator (qualifying_scope,
17853 unqualified_name,
17854 sfk);
17855 declarator->std_attributes = attrs;
17856 declarator->id_loc = token->location;
17857 declarator->parameter_pack_p = pack_expansion_p;
17859 if (pack_expansion_p)
17860 maybe_warn_variadic_templates ();
17863 handle_declarator:;
17864 scope = get_scope_of_declarator (declarator);
17865 if (scope)
17867 /* Any names that appear after the declarator-id for a
17868 member are looked up in the containing scope. */
17869 if (at_function_scope_p ())
17871 /* But declarations with qualified-ids can't appear in a
17872 function. */
17873 cp_parser_error (parser, "qualified-id in declaration");
17874 declarator = cp_error_declarator;
17875 break;
17877 pushed_scope = push_scope (scope);
17879 parser->in_declarator_p = true;
17880 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17881 || (declarator && declarator->kind == cdk_id))
17882 /* Default args are only allowed on function
17883 declarations. */
17884 parser->default_arg_ok_p = saved_default_arg_ok_p;
17885 else
17886 parser->default_arg_ok_p = false;
17888 first = false;
17890 /* We're done. */
17891 else
17892 break;
17895 /* For an abstract declarator, we might wind up with nothing at this
17896 point. That's an error; the declarator is not optional. */
17897 if (!declarator)
17898 cp_parser_error (parser, "expected declarator");
17900 /* If we entered a scope, we must exit it now. */
17901 if (pushed_scope)
17902 pop_scope (pushed_scope);
17904 parser->default_arg_ok_p = saved_default_arg_ok_p;
17905 parser->in_declarator_p = saved_in_declarator_p;
17907 return declarator;
17910 /* Parse a ptr-operator.
17912 ptr-operator:
17913 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17914 * cv-qualifier-seq [opt]
17916 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17917 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17919 GNU Extension:
17921 ptr-operator:
17922 & cv-qualifier-seq [opt]
17924 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17925 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17926 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17927 filled in with the TYPE containing the member. *CV_QUALS is
17928 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17929 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17930 Note that the tree codes returned by this function have nothing
17931 to do with the types of trees that will be eventually be created
17932 to represent the pointer or reference type being parsed. They are
17933 just constants with suggestive names. */
17934 static enum tree_code
17935 cp_parser_ptr_operator (cp_parser* parser,
17936 tree* type,
17937 cp_cv_quals *cv_quals,
17938 tree *attributes)
17940 enum tree_code code = ERROR_MARK;
17941 cp_token *token;
17942 tree attrs = NULL_TREE;
17944 /* Assume that it's not a pointer-to-member. */
17945 *type = NULL_TREE;
17946 /* And that there are no cv-qualifiers. */
17947 *cv_quals = TYPE_UNQUALIFIED;
17949 /* Peek at the next token. */
17950 token = cp_lexer_peek_token (parser->lexer);
17952 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17953 if (token->type == CPP_MULT)
17954 code = INDIRECT_REF;
17955 else if (token->type == CPP_AND)
17956 code = ADDR_EXPR;
17957 else if ((cxx_dialect != cxx98) &&
17958 token->type == CPP_AND_AND) /* C++0x only */
17959 code = NON_LVALUE_EXPR;
17961 if (code != ERROR_MARK)
17963 /* Consume the `*', `&' or `&&'. */
17964 cp_lexer_consume_token (parser->lexer);
17966 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17967 `&', if we are allowing GNU extensions. (The only qualifier
17968 that can legally appear after `&' is `restrict', but that is
17969 enforced during semantic analysis. */
17970 if (code == INDIRECT_REF
17971 || cp_parser_allow_gnu_extensions_p (parser))
17972 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17974 attrs = cp_parser_std_attribute_spec_seq (parser);
17975 if (attributes != NULL)
17976 *attributes = attrs;
17978 else
17980 /* Try the pointer-to-member case. */
17981 cp_parser_parse_tentatively (parser);
17982 /* Look for the optional `::' operator. */
17983 cp_parser_global_scope_opt (parser,
17984 /*current_scope_valid_p=*/false);
17985 /* Look for the nested-name specifier. */
17986 token = cp_lexer_peek_token (parser->lexer);
17987 cp_parser_nested_name_specifier (parser,
17988 /*typename_keyword_p=*/false,
17989 /*check_dependency_p=*/true,
17990 /*type_p=*/false,
17991 /*is_declaration=*/false);
17992 /* If we found it, and the next token is a `*', then we are
17993 indeed looking at a pointer-to-member operator. */
17994 if (!cp_parser_error_occurred (parser)
17995 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17997 /* Indicate that the `*' operator was used. */
17998 code = INDIRECT_REF;
18000 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18001 error_at (token->location, "%qD is a namespace", parser->scope);
18002 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18003 error_at (token->location, "cannot form pointer to member of "
18004 "non-class %q#T", parser->scope);
18005 else
18007 /* The type of which the member is a member is given by the
18008 current SCOPE. */
18009 *type = parser->scope;
18010 /* The next name will not be qualified. */
18011 parser->scope = NULL_TREE;
18012 parser->qualifying_scope = NULL_TREE;
18013 parser->object_scope = NULL_TREE;
18014 /* Look for optional c++11 attributes. */
18015 attrs = cp_parser_std_attribute_spec_seq (parser);
18016 if (attributes != NULL)
18017 *attributes = attrs;
18018 /* Look for the optional cv-qualifier-seq. */
18019 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18022 /* If that didn't work we don't have a ptr-operator. */
18023 if (!cp_parser_parse_definitely (parser))
18024 cp_parser_error (parser, "expected ptr-operator");
18027 return code;
18030 /* Parse an (optional) cv-qualifier-seq.
18032 cv-qualifier-seq:
18033 cv-qualifier cv-qualifier-seq [opt]
18035 cv-qualifier:
18036 const
18037 volatile
18039 GNU Extension:
18041 cv-qualifier:
18042 __restrict__
18044 Returns a bitmask representing the cv-qualifiers. */
18046 static cp_cv_quals
18047 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18049 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18051 while (true)
18053 cp_token *token;
18054 cp_cv_quals cv_qualifier;
18056 /* Peek at the next token. */
18057 token = cp_lexer_peek_token (parser->lexer);
18058 /* See if it's a cv-qualifier. */
18059 switch (token->keyword)
18061 case RID_CONST:
18062 cv_qualifier = TYPE_QUAL_CONST;
18063 break;
18065 case RID_VOLATILE:
18066 cv_qualifier = TYPE_QUAL_VOLATILE;
18067 break;
18069 case RID_RESTRICT:
18070 cv_qualifier = TYPE_QUAL_RESTRICT;
18071 break;
18073 default:
18074 cv_qualifier = TYPE_UNQUALIFIED;
18075 break;
18078 if (!cv_qualifier)
18079 break;
18081 if (cv_quals & cv_qualifier)
18083 error_at (token->location, "duplicate cv-qualifier");
18084 cp_lexer_purge_token (parser->lexer);
18086 else
18088 cp_lexer_consume_token (parser->lexer);
18089 cv_quals |= cv_qualifier;
18093 return cv_quals;
18096 /* Parse an (optional) ref-qualifier
18098 ref-qualifier:
18102 Returns cp_ref_qualifier representing ref-qualifier. */
18104 static cp_ref_qualifier
18105 cp_parser_ref_qualifier_opt (cp_parser* parser)
18107 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18109 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18110 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18111 return ref_qual;
18113 while (true)
18115 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18116 cp_token *token = cp_lexer_peek_token (parser->lexer);
18118 switch (token->type)
18120 case CPP_AND:
18121 curr_ref_qual = REF_QUAL_LVALUE;
18122 break;
18124 case CPP_AND_AND:
18125 curr_ref_qual = REF_QUAL_RVALUE;
18126 break;
18128 default:
18129 curr_ref_qual = REF_QUAL_NONE;
18130 break;
18133 if (!curr_ref_qual)
18134 break;
18135 else if (ref_qual)
18137 error_at (token->location, "multiple ref-qualifiers");
18138 cp_lexer_purge_token (parser->lexer);
18140 else
18142 ref_qual = curr_ref_qual;
18143 cp_lexer_consume_token (parser->lexer);
18147 return ref_qual;
18150 /* Parse an (optional) virt-specifier-seq.
18152 virt-specifier-seq:
18153 virt-specifier virt-specifier-seq [opt]
18155 virt-specifier:
18156 override
18157 final
18159 Returns a bitmask representing the virt-specifiers. */
18161 static cp_virt_specifiers
18162 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18164 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18166 while (true)
18168 cp_token *token;
18169 cp_virt_specifiers virt_specifier;
18171 /* Peek at the next token. */
18172 token = cp_lexer_peek_token (parser->lexer);
18173 /* See if it's a virt-specifier-qualifier. */
18174 if (token->type != CPP_NAME)
18175 break;
18176 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18178 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18179 virt_specifier = VIRT_SPEC_OVERRIDE;
18181 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18183 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18184 virt_specifier = VIRT_SPEC_FINAL;
18186 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18188 virt_specifier = VIRT_SPEC_FINAL;
18190 else
18191 break;
18193 if (virt_specifiers & virt_specifier)
18195 error_at (token->location, "duplicate virt-specifier");
18196 cp_lexer_purge_token (parser->lexer);
18198 else
18200 cp_lexer_consume_token (parser->lexer);
18201 virt_specifiers |= virt_specifier;
18204 return virt_specifiers;
18207 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18208 is in scope even though it isn't real. */
18210 void
18211 inject_this_parameter (tree ctype, cp_cv_quals quals)
18213 tree this_parm;
18215 if (current_class_ptr)
18217 /* We don't clear this between NSDMIs. Is it already what we want? */
18218 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18219 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18220 && cp_type_quals (type) == quals)
18221 return;
18224 this_parm = build_this_parm (ctype, quals);
18225 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18226 current_class_ptr = NULL_TREE;
18227 current_class_ref
18228 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18229 current_class_ptr = this_parm;
18232 /* Return true iff our current scope is a non-static data member
18233 initializer. */
18235 bool
18236 parsing_nsdmi (void)
18238 /* We recognize NSDMI context by the context-less 'this' pointer set up
18239 by the function above. */
18240 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18241 return true;
18242 return false;
18245 /* Parse a late-specified return type, if any. This is not a separate
18246 non-terminal, but part of a function declarator, which looks like
18248 -> trailing-type-specifier-seq abstract-declarator(opt)
18250 Returns the type indicated by the type-id.
18252 In addition to this this parses any queued up omp declare simd
18253 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18255 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18256 function. */
18258 static tree
18259 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18260 cp_cv_quals quals)
18262 cp_token *token;
18263 tree type = NULL_TREE;
18264 bool declare_simd_p = (parser->omp_declare_simd
18265 && declarator
18266 && declarator->kind == cdk_id);
18268 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18269 && declarator && declarator->kind == cdk_id);
18271 /* Peek at the next token. */
18272 token = cp_lexer_peek_token (parser->lexer);
18273 /* A late-specified return type is indicated by an initial '->'. */
18274 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18275 return NULL_TREE;
18277 tree save_ccp = current_class_ptr;
18278 tree save_ccr = current_class_ref;
18279 if (quals >= 0)
18281 /* DR 1207: 'this' is in scope in the trailing return type. */
18282 inject_this_parameter (current_class_type, quals);
18285 if (token->type == CPP_DEREF)
18287 /* Consume the ->. */
18288 cp_lexer_consume_token (parser->lexer);
18290 type = cp_parser_trailing_type_id (parser);
18293 if (cilk_simd_fn_vector_p)
18294 declarator->std_attributes
18295 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18296 declarator->std_attributes);
18297 if (declare_simd_p)
18298 declarator->std_attributes
18299 = cp_parser_late_parsing_omp_declare_simd (parser,
18300 declarator->std_attributes);
18302 if (quals >= 0)
18304 current_class_ptr = save_ccp;
18305 current_class_ref = save_ccr;
18308 return type;
18311 /* Parse a declarator-id.
18313 declarator-id:
18314 id-expression
18315 :: [opt] nested-name-specifier [opt] type-name
18317 In the `id-expression' case, the value returned is as for
18318 cp_parser_id_expression if the id-expression was an unqualified-id.
18319 If the id-expression was a qualified-id, then a SCOPE_REF is
18320 returned. The first operand is the scope (either a NAMESPACE_DECL
18321 or TREE_TYPE), but the second is still just a representation of an
18322 unqualified-id. */
18324 static tree
18325 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18327 tree id;
18328 /* The expression must be an id-expression. Assume that qualified
18329 names are the names of types so that:
18331 template <class T>
18332 int S<T>::R::i = 3;
18334 will work; we must treat `S<T>::R' as the name of a type.
18335 Similarly, assume that qualified names are templates, where
18336 required, so that:
18338 template <class T>
18339 int S<T>::R<T>::i = 3;
18341 will work, too. */
18342 id = cp_parser_id_expression (parser,
18343 /*template_keyword_p=*/false,
18344 /*check_dependency_p=*/false,
18345 /*template_p=*/NULL,
18346 /*declarator_p=*/true,
18347 optional_p);
18348 if (id && BASELINK_P (id))
18349 id = BASELINK_FUNCTIONS (id);
18350 return id;
18353 /* Parse a type-id.
18355 type-id:
18356 type-specifier-seq abstract-declarator [opt]
18358 Returns the TYPE specified. */
18360 static tree
18361 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18362 bool is_trailing_return)
18364 cp_decl_specifier_seq type_specifier_seq;
18365 cp_declarator *abstract_declarator;
18367 /* Parse the type-specifier-seq. */
18368 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18369 is_trailing_return,
18370 &type_specifier_seq);
18371 if (type_specifier_seq.type == error_mark_node)
18372 return error_mark_node;
18374 /* There might or might not be an abstract declarator. */
18375 cp_parser_parse_tentatively (parser);
18376 /* Look for the declarator. */
18377 abstract_declarator
18378 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18379 /*parenthesized_p=*/NULL,
18380 /*member_p=*/false,
18381 /*friend_p=*/false);
18382 /* Check to see if there really was a declarator. */
18383 if (!cp_parser_parse_definitely (parser))
18384 abstract_declarator = NULL;
18386 if (type_specifier_seq.type
18387 /* None of the valid uses of 'auto' in C++14 involve the type-id
18388 nonterminal, but it is valid in a trailing-return-type. */
18389 && !(cxx_dialect >= cxx14 && is_trailing_return)
18390 && type_uses_auto (type_specifier_seq.type))
18392 /* A type-id with type 'auto' is only ok if the abstract declarator
18393 is a function declarator with a late-specified return type. */
18394 if (abstract_declarator
18395 && abstract_declarator->kind == cdk_function
18396 && abstract_declarator->u.function.late_return_type)
18397 /* OK */;
18398 else
18400 error ("invalid use of %<auto%>");
18401 return error_mark_node;
18405 return groktypename (&type_specifier_seq, abstract_declarator,
18406 is_template_arg);
18409 static tree cp_parser_type_id (cp_parser *parser)
18411 return cp_parser_type_id_1 (parser, false, false);
18414 static tree cp_parser_template_type_arg (cp_parser *parser)
18416 tree r;
18417 const char *saved_message = parser->type_definition_forbidden_message;
18418 parser->type_definition_forbidden_message
18419 = G_("types may not be defined in template arguments");
18420 r = cp_parser_type_id_1 (parser, true, false);
18421 parser->type_definition_forbidden_message = saved_message;
18422 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18424 error ("invalid use of %<auto%> in template argument");
18425 r = error_mark_node;
18427 return r;
18430 static tree cp_parser_trailing_type_id (cp_parser *parser)
18432 return cp_parser_type_id_1 (parser, false, true);
18435 /* Parse a type-specifier-seq.
18437 type-specifier-seq:
18438 type-specifier type-specifier-seq [opt]
18440 GNU extension:
18442 type-specifier-seq:
18443 attributes type-specifier-seq [opt]
18445 If IS_DECLARATION is true, we are at the start of a "condition" or
18446 exception-declaration, so we might be followed by a declarator-id.
18448 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18449 i.e. we've just seen "->".
18451 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18453 static void
18454 cp_parser_type_specifier_seq (cp_parser* parser,
18455 bool is_declaration,
18456 bool is_trailing_return,
18457 cp_decl_specifier_seq *type_specifier_seq)
18459 bool seen_type_specifier = false;
18460 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18461 cp_token *start_token = NULL;
18463 /* Clear the TYPE_SPECIFIER_SEQ. */
18464 clear_decl_specs (type_specifier_seq);
18466 /* In the context of a trailing return type, enum E { } is an
18467 elaborated-type-specifier followed by a function-body, not an
18468 enum-specifier. */
18469 if (is_trailing_return)
18470 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18472 /* Parse the type-specifiers and attributes. */
18473 while (true)
18475 tree type_specifier;
18476 bool is_cv_qualifier;
18478 /* Check for attributes first. */
18479 if (cp_next_tokens_can_be_attribute_p (parser))
18481 type_specifier_seq->attributes =
18482 chainon (type_specifier_seq->attributes,
18483 cp_parser_attributes_opt (parser));
18484 continue;
18487 /* record the token of the beginning of the type specifier seq,
18488 for error reporting purposes*/
18489 if (!start_token)
18490 start_token = cp_lexer_peek_token (parser->lexer);
18492 /* Look for the type-specifier. */
18493 type_specifier = cp_parser_type_specifier (parser,
18494 flags,
18495 type_specifier_seq,
18496 /*is_declaration=*/false,
18497 NULL,
18498 &is_cv_qualifier);
18499 if (!type_specifier)
18501 /* If the first type-specifier could not be found, this is not a
18502 type-specifier-seq at all. */
18503 if (!seen_type_specifier)
18505 /* Set in_declarator_p to avoid skipping to the semicolon. */
18506 int in_decl = parser->in_declarator_p;
18507 parser->in_declarator_p = true;
18509 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18510 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18511 cp_parser_error (parser, "expected type-specifier");
18513 parser->in_declarator_p = in_decl;
18515 type_specifier_seq->type = error_mark_node;
18516 return;
18518 /* If subsequent type-specifiers could not be found, the
18519 type-specifier-seq is complete. */
18520 break;
18523 seen_type_specifier = true;
18524 /* The standard says that a condition can be:
18526 type-specifier-seq declarator = assignment-expression
18528 However, given:
18530 struct S {};
18531 if (int S = ...)
18533 we should treat the "S" as a declarator, not as a
18534 type-specifier. The standard doesn't say that explicitly for
18535 type-specifier-seq, but it does say that for
18536 decl-specifier-seq in an ordinary declaration. Perhaps it
18537 would be clearer just to allow a decl-specifier-seq here, and
18538 then add a semantic restriction that if any decl-specifiers
18539 that are not type-specifiers appear, the program is invalid. */
18540 if (is_declaration && !is_cv_qualifier)
18541 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18545 /* Return whether the function currently being declared has an associated
18546 template parameter list. */
18548 static bool
18549 function_being_declared_is_template_p (cp_parser* parser)
18551 if (!current_template_parms || processing_template_parmlist)
18552 return false;
18554 if (parser->implicit_template_scope)
18555 return true;
18557 if (at_class_scope_p ()
18558 && TYPE_BEING_DEFINED (current_class_type))
18559 return parser->num_template_parameter_lists != 0;
18561 return ((int) parser->num_template_parameter_lists > template_class_depth
18562 (current_class_type));
18565 /* Parse a parameter-declaration-clause.
18567 parameter-declaration-clause:
18568 parameter-declaration-list [opt] ... [opt]
18569 parameter-declaration-list , ...
18571 Returns a representation for the parameter declarations. A return
18572 value of NULL indicates a parameter-declaration-clause consisting
18573 only of an ellipsis. */
18575 static tree
18576 cp_parser_parameter_declaration_clause (cp_parser* parser)
18578 tree parameters;
18579 cp_token *token;
18580 bool ellipsis_p;
18581 bool is_error;
18583 struct cleanup {
18584 cp_parser* parser;
18585 int auto_is_implicit_function_template_parm_p;
18586 ~cleanup() {
18587 parser->auto_is_implicit_function_template_parm_p
18588 = auto_is_implicit_function_template_parm_p;
18590 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18592 (void) cleanup;
18594 if (!processing_specialization
18595 && !processing_template_parmlist
18596 && !processing_explicit_instantiation)
18597 if (!current_function_decl
18598 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18599 parser->auto_is_implicit_function_template_parm_p = true;
18601 /* Peek at the next token. */
18602 token = cp_lexer_peek_token (parser->lexer);
18603 /* Check for trivial parameter-declaration-clauses. */
18604 if (token->type == CPP_ELLIPSIS)
18606 /* Consume the `...' token. */
18607 cp_lexer_consume_token (parser->lexer);
18608 return NULL_TREE;
18610 else if (token->type == CPP_CLOSE_PAREN)
18611 /* There are no parameters. */
18613 #ifndef NO_IMPLICIT_EXTERN_C
18614 if (in_system_header_at (input_location)
18615 && current_class_type == NULL
18616 && current_lang_name == lang_name_c)
18617 return NULL_TREE;
18618 else
18619 #endif
18620 return void_list_node;
18622 /* Check for `(void)', too, which is a special case. */
18623 else if (token->keyword == RID_VOID
18624 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18625 == CPP_CLOSE_PAREN))
18627 /* Consume the `void' token. */
18628 cp_lexer_consume_token (parser->lexer);
18629 /* There are no parameters. */
18630 return void_list_node;
18633 /* Parse the parameter-declaration-list. */
18634 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18635 /* If a parse error occurred while parsing the
18636 parameter-declaration-list, then the entire
18637 parameter-declaration-clause is erroneous. */
18638 if (is_error)
18639 return NULL;
18641 /* Peek at the next token. */
18642 token = cp_lexer_peek_token (parser->lexer);
18643 /* If it's a `,', the clause should terminate with an ellipsis. */
18644 if (token->type == CPP_COMMA)
18646 /* Consume the `,'. */
18647 cp_lexer_consume_token (parser->lexer);
18648 /* Expect an ellipsis. */
18649 ellipsis_p
18650 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18652 /* It might also be `...' if the optional trailing `,' was
18653 omitted. */
18654 else if (token->type == CPP_ELLIPSIS)
18656 /* Consume the `...' token. */
18657 cp_lexer_consume_token (parser->lexer);
18658 /* And remember that we saw it. */
18659 ellipsis_p = true;
18661 else
18662 ellipsis_p = false;
18664 /* Finish the parameter list. */
18665 if (!ellipsis_p)
18666 parameters = chainon (parameters, void_list_node);
18668 return parameters;
18671 /* Parse a parameter-declaration-list.
18673 parameter-declaration-list:
18674 parameter-declaration
18675 parameter-declaration-list , parameter-declaration
18677 Returns a representation of the parameter-declaration-list, as for
18678 cp_parser_parameter_declaration_clause. However, the
18679 `void_list_node' is never appended to the list. Upon return,
18680 *IS_ERROR will be true iff an error occurred. */
18682 static tree
18683 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18685 tree parameters = NULL_TREE;
18686 tree *tail = &parameters;
18687 bool saved_in_unbraced_linkage_specification_p;
18688 int index = 0;
18690 /* Assume all will go well. */
18691 *is_error = false;
18692 /* The special considerations that apply to a function within an
18693 unbraced linkage specifications do not apply to the parameters
18694 to the function. */
18695 saved_in_unbraced_linkage_specification_p
18696 = parser->in_unbraced_linkage_specification_p;
18697 parser->in_unbraced_linkage_specification_p = false;
18699 /* Look for more parameters. */
18700 while (true)
18702 cp_parameter_declarator *parameter;
18703 tree decl = error_mark_node;
18704 bool parenthesized_p = false;
18705 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18706 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18707 (current_template_parms)) : 0);
18709 /* Parse the parameter. */
18710 parameter
18711 = cp_parser_parameter_declaration (parser,
18712 /*template_parm_p=*/false,
18713 &parenthesized_p);
18715 /* We don't know yet if the enclosing context is deprecated, so wait
18716 and warn in grokparms if appropriate. */
18717 deprecated_state = DEPRECATED_SUPPRESS;
18719 if (parameter)
18721 /* If a function parameter pack was specified and an implicit template
18722 parameter was introduced during cp_parser_parameter_declaration,
18723 change any implicit parameters introduced into packs. */
18724 if (parser->implicit_template_parms
18725 && parameter->declarator
18726 && parameter->declarator->parameter_pack_p)
18728 int latest_template_parm_idx = TREE_VEC_LENGTH
18729 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18731 if (latest_template_parm_idx != template_parm_idx)
18732 parameter->decl_specifiers.type = convert_generic_types_to_packs
18733 (parameter->decl_specifiers.type,
18734 template_parm_idx, latest_template_parm_idx);
18737 decl = grokdeclarator (parameter->declarator,
18738 &parameter->decl_specifiers,
18739 PARM,
18740 parameter->default_argument != NULL_TREE,
18741 &parameter->decl_specifiers.attributes);
18744 deprecated_state = DEPRECATED_NORMAL;
18746 /* If a parse error occurred parsing the parameter declaration,
18747 then the entire parameter-declaration-list is erroneous. */
18748 if (decl == error_mark_node)
18750 *is_error = true;
18751 parameters = error_mark_node;
18752 break;
18755 if (parameter->decl_specifiers.attributes)
18756 cplus_decl_attributes (&decl,
18757 parameter->decl_specifiers.attributes,
18759 if (DECL_NAME (decl))
18760 decl = pushdecl (decl);
18762 if (decl != error_mark_node)
18764 retrofit_lang_decl (decl);
18765 DECL_PARM_INDEX (decl) = ++index;
18766 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18769 /* Add the new parameter to the list. */
18770 *tail = build_tree_list (parameter->default_argument, decl);
18771 tail = &TREE_CHAIN (*tail);
18773 /* Peek at the next token. */
18774 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18775 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18776 /* These are for Objective-C++ */
18777 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18778 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18779 /* The parameter-declaration-list is complete. */
18780 break;
18781 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18783 cp_token *token;
18785 /* Peek at the next token. */
18786 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18787 /* If it's an ellipsis, then the list is complete. */
18788 if (token->type == CPP_ELLIPSIS)
18789 break;
18790 /* Otherwise, there must be more parameters. Consume the
18791 `,'. */
18792 cp_lexer_consume_token (parser->lexer);
18793 /* When parsing something like:
18795 int i(float f, double d)
18797 we can tell after seeing the declaration for "f" that we
18798 are not looking at an initialization of a variable "i",
18799 but rather at the declaration of a function "i".
18801 Due to the fact that the parsing of template arguments
18802 (as specified to a template-id) requires backtracking we
18803 cannot use this technique when inside a template argument
18804 list. */
18805 if (!parser->in_template_argument_list_p
18806 && !parser->in_type_id_in_expr_p
18807 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18808 /* However, a parameter-declaration of the form
18809 "float(f)" (which is a valid declaration of a
18810 parameter "f") can also be interpreted as an
18811 expression (the conversion of "f" to "float"). */
18812 && !parenthesized_p)
18813 cp_parser_commit_to_tentative_parse (parser);
18815 else
18817 cp_parser_error (parser, "expected %<,%> or %<...%>");
18818 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18819 cp_parser_skip_to_closing_parenthesis (parser,
18820 /*recovering=*/true,
18821 /*or_comma=*/false,
18822 /*consume_paren=*/false);
18823 break;
18827 parser->in_unbraced_linkage_specification_p
18828 = saved_in_unbraced_linkage_specification_p;
18830 /* Reset implicit_template_scope if we are about to leave the function
18831 parameter list that introduced it. Note that for out-of-line member
18832 definitions, there will be one or more class scopes before we get to
18833 the template parameter scope. */
18835 if (cp_binding_level *its = parser->implicit_template_scope)
18836 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18838 while (maybe_its->kind == sk_class)
18839 maybe_its = maybe_its->level_chain;
18840 if (maybe_its == its)
18842 parser->implicit_template_parms = 0;
18843 parser->implicit_template_scope = 0;
18847 return parameters;
18850 /* Parse a parameter declaration.
18852 parameter-declaration:
18853 decl-specifier-seq ... [opt] declarator
18854 decl-specifier-seq declarator = assignment-expression
18855 decl-specifier-seq ... [opt] abstract-declarator [opt]
18856 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18858 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18859 declares a template parameter. (In that case, a non-nested `>'
18860 token encountered during the parsing of the assignment-expression
18861 is not interpreted as a greater-than operator.)
18863 Returns a representation of the parameter, or NULL if an error
18864 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18865 true iff the declarator is of the form "(p)". */
18867 static cp_parameter_declarator *
18868 cp_parser_parameter_declaration (cp_parser *parser,
18869 bool template_parm_p,
18870 bool *parenthesized_p)
18872 int declares_class_or_enum;
18873 cp_decl_specifier_seq decl_specifiers;
18874 cp_declarator *declarator;
18875 tree default_argument;
18876 cp_token *token = NULL, *declarator_token_start = NULL;
18877 const char *saved_message;
18879 /* In a template parameter, `>' is not an operator.
18881 [temp.param]
18883 When parsing a default template-argument for a non-type
18884 template-parameter, the first non-nested `>' is taken as the end
18885 of the template parameter-list rather than a greater-than
18886 operator. */
18888 /* Type definitions may not appear in parameter types. */
18889 saved_message = parser->type_definition_forbidden_message;
18890 parser->type_definition_forbidden_message
18891 = G_("types may not be defined in parameter types");
18893 /* Parse the declaration-specifiers. */
18894 cp_parser_decl_specifier_seq (parser,
18895 CP_PARSER_FLAGS_NONE,
18896 &decl_specifiers,
18897 &declares_class_or_enum);
18899 /* Complain about missing 'typename' or other invalid type names. */
18900 if (!decl_specifiers.any_type_specifiers_p
18901 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18902 decl_specifiers.type = error_mark_node;
18904 /* If an error occurred, there's no reason to attempt to parse the
18905 rest of the declaration. */
18906 if (cp_parser_error_occurred (parser))
18908 parser->type_definition_forbidden_message = saved_message;
18909 return NULL;
18912 /* Peek at the next token. */
18913 token = cp_lexer_peek_token (parser->lexer);
18915 /* If the next token is a `)', `,', `=', `>', or `...', then there
18916 is no declarator. However, when variadic templates are enabled,
18917 there may be a declarator following `...'. */
18918 if (token->type == CPP_CLOSE_PAREN
18919 || token->type == CPP_COMMA
18920 || token->type == CPP_EQ
18921 || token->type == CPP_GREATER)
18923 declarator = NULL;
18924 if (parenthesized_p)
18925 *parenthesized_p = false;
18927 /* Otherwise, there should be a declarator. */
18928 else
18930 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18931 parser->default_arg_ok_p = false;
18933 /* After seeing a decl-specifier-seq, if the next token is not a
18934 "(", there is no possibility that the code is a valid
18935 expression. Therefore, if parsing tentatively, we commit at
18936 this point. */
18937 if (!parser->in_template_argument_list_p
18938 /* In an expression context, having seen:
18940 (int((char ...
18942 we cannot be sure whether we are looking at a
18943 function-type (taking a "char" as a parameter) or a cast
18944 of some object of type "char" to "int". */
18945 && !parser->in_type_id_in_expr_p
18946 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18947 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18948 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18949 cp_parser_commit_to_tentative_parse (parser);
18950 /* Parse the declarator. */
18951 declarator_token_start = token;
18952 declarator = cp_parser_declarator (parser,
18953 CP_PARSER_DECLARATOR_EITHER,
18954 /*ctor_dtor_or_conv_p=*/NULL,
18955 parenthesized_p,
18956 /*member_p=*/false,
18957 /*friend_p=*/false);
18958 parser->default_arg_ok_p = saved_default_arg_ok_p;
18959 /* After the declarator, allow more attributes. */
18960 decl_specifiers.attributes
18961 = chainon (decl_specifiers.attributes,
18962 cp_parser_attributes_opt (parser));
18965 /* If the next token is an ellipsis, and we have not seen a
18966 declarator name, and the type of the declarator contains parameter
18967 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18968 a parameter pack expansion expression. Otherwise, leave the
18969 ellipsis for a C-style variadic function. */
18970 token = cp_lexer_peek_token (parser->lexer);
18971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18973 tree type = decl_specifiers.type;
18975 if (type && DECL_P (type))
18976 type = TREE_TYPE (type);
18978 if (type
18979 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18980 && declarator_can_be_parameter_pack (declarator)
18981 && (!declarator || !declarator->parameter_pack_p)
18982 && uses_parameter_packs (type))
18984 /* Consume the `...'. */
18985 cp_lexer_consume_token (parser->lexer);
18986 maybe_warn_variadic_templates ();
18988 /* Build a pack expansion type */
18989 if (declarator)
18990 declarator->parameter_pack_p = true;
18991 else
18992 decl_specifiers.type = make_pack_expansion (type);
18996 /* The restriction on defining new types applies only to the type
18997 of the parameter, not to the default argument. */
18998 parser->type_definition_forbidden_message = saved_message;
19000 /* If the next token is `=', then process a default argument. */
19001 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19003 token = cp_lexer_peek_token (parser->lexer);
19004 /* If we are defining a class, then the tokens that make up the
19005 default argument must be saved and processed later. */
19006 if (!template_parm_p && at_class_scope_p ()
19007 && TYPE_BEING_DEFINED (current_class_type)
19008 && !LAMBDA_TYPE_P (current_class_type))
19009 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19010 /* Outside of a class definition, we can just parse the
19011 assignment-expression. */
19012 else
19013 default_argument
19014 = cp_parser_default_argument (parser, template_parm_p);
19016 if (!parser->default_arg_ok_p)
19018 if (flag_permissive)
19019 warning (0, "deprecated use of default argument for parameter of non-function");
19020 else
19022 error_at (token->location,
19023 "default arguments are only "
19024 "permitted for function parameters");
19025 default_argument = NULL_TREE;
19028 else if ((declarator && declarator->parameter_pack_p)
19029 || (decl_specifiers.type
19030 && PACK_EXPANSION_P (decl_specifiers.type)))
19032 /* Find the name of the parameter pack. */
19033 cp_declarator *id_declarator = declarator;
19034 while (id_declarator && id_declarator->kind != cdk_id)
19035 id_declarator = id_declarator->declarator;
19037 if (id_declarator && id_declarator->kind == cdk_id)
19038 error_at (declarator_token_start->location,
19039 template_parm_p
19040 ? G_("template parameter pack %qD "
19041 "cannot have a default argument")
19042 : G_("parameter pack %qD cannot have "
19043 "a default argument"),
19044 id_declarator->u.id.unqualified_name);
19045 else
19046 error_at (declarator_token_start->location,
19047 template_parm_p
19048 ? G_("template parameter pack cannot have "
19049 "a default argument")
19050 : G_("parameter pack cannot have a "
19051 "default argument"));
19053 default_argument = NULL_TREE;
19056 else
19057 default_argument = NULL_TREE;
19059 return make_parameter_declarator (&decl_specifiers,
19060 declarator,
19061 default_argument);
19064 /* Parse a default argument and return it.
19066 TEMPLATE_PARM_P is true if this is a default argument for a
19067 non-type template parameter. */
19068 static tree
19069 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19071 tree default_argument = NULL_TREE;
19072 bool saved_greater_than_is_operator_p;
19073 bool saved_local_variables_forbidden_p;
19074 bool non_constant_p, is_direct_init;
19076 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19077 set correctly. */
19078 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19079 parser->greater_than_is_operator_p = !template_parm_p;
19080 /* Local variable names (and the `this' keyword) may not
19081 appear in a default argument. */
19082 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19083 parser->local_variables_forbidden_p = true;
19084 /* Parse the assignment-expression. */
19085 if (template_parm_p)
19086 push_deferring_access_checks (dk_no_deferred);
19087 tree saved_class_ptr = NULL_TREE;
19088 tree saved_class_ref = NULL_TREE;
19089 /* The "this" pointer is not valid in a default argument. */
19090 if (cfun)
19092 saved_class_ptr = current_class_ptr;
19093 cp_function_chain->x_current_class_ptr = NULL_TREE;
19094 saved_class_ref = current_class_ref;
19095 cp_function_chain->x_current_class_ref = NULL_TREE;
19097 default_argument
19098 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19099 /* Restore the "this" pointer. */
19100 if (cfun)
19102 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19103 cp_function_chain->x_current_class_ref = saved_class_ref;
19105 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19106 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19107 if (template_parm_p)
19108 pop_deferring_access_checks ();
19109 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19110 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19112 return default_argument;
19115 /* Parse a function-body.
19117 function-body:
19118 compound_statement */
19120 static void
19121 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19123 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19126 /* Parse a ctor-initializer-opt followed by a function-body. Return
19127 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19128 is true we are parsing a function-try-block. */
19130 static bool
19131 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19132 bool in_function_try_block)
19134 tree body, list;
19135 bool ctor_initializer_p;
19136 const bool check_body_p =
19137 DECL_CONSTRUCTOR_P (current_function_decl)
19138 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19139 tree last = NULL;
19141 /* Begin the function body. */
19142 body = begin_function_body ();
19143 /* Parse the optional ctor-initializer. */
19144 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19146 /* If we're parsing a constexpr constructor definition, we need
19147 to check that the constructor body is indeed empty. However,
19148 before we get to cp_parser_function_body lot of junk has been
19149 generated, so we can't just check that we have an empty block.
19150 Rather we take a snapshot of the outermost block, and check whether
19151 cp_parser_function_body changed its state. */
19152 if (check_body_p)
19154 list = cur_stmt_list;
19155 if (STATEMENT_LIST_TAIL (list))
19156 last = STATEMENT_LIST_TAIL (list)->stmt;
19158 /* Parse the function-body. */
19159 cp_parser_function_body (parser, in_function_try_block);
19160 if (check_body_p)
19161 check_constexpr_ctor_body (last, list, /*complain=*/true);
19162 /* Finish the function body. */
19163 finish_function_body (body);
19165 return ctor_initializer_p;
19168 /* Parse an initializer.
19170 initializer:
19171 = initializer-clause
19172 ( expression-list )
19174 Returns an expression representing the initializer. If no
19175 initializer is present, NULL_TREE is returned.
19177 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19178 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19179 set to TRUE if there is no initializer present. If there is an
19180 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19181 is set to true; otherwise it is set to false. */
19183 static tree
19184 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19185 bool* non_constant_p)
19187 cp_token *token;
19188 tree init;
19190 /* Peek at the next token. */
19191 token = cp_lexer_peek_token (parser->lexer);
19193 /* Let our caller know whether or not this initializer was
19194 parenthesized. */
19195 *is_direct_init = (token->type != CPP_EQ);
19196 /* Assume that the initializer is constant. */
19197 *non_constant_p = false;
19199 if (token->type == CPP_EQ)
19201 /* Consume the `='. */
19202 cp_lexer_consume_token (parser->lexer);
19203 /* Parse the initializer-clause. */
19204 init = cp_parser_initializer_clause (parser, non_constant_p);
19206 else if (token->type == CPP_OPEN_PAREN)
19208 vec<tree, va_gc> *vec;
19209 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19210 /*cast_p=*/false,
19211 /*allow_expansion_p=*/true,
19212 non_constant_p);
19213 if (vec == NULL)
19214 return error_mark_node;
19215 init = build_tree_list_vec (vec);
19216 release_tree_vector (vec);
19218 else if (token->type == CPP_OPEN_BRACE)
19220 cp_lexer_set_source_position (parser->lexer);
19221 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19222 init = cp_parser_braced_list (parser, non_constant_p);
19223 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19225 else
19227 /* Anything else is an error. */
19228 cp_parser_error (parser, "expected initializer");
19229 init = error_mark_node;
19232 return init;
19235 /* Parse an initializer-clause.
19237 initializer-clause:
19238 assignment-expression
19239 braced-init-list
19241 Returns an expression representing the initializer.
19243 If the `assignment-expression' production is used the value
19244 returned is simply a representation for the expression.
19246 Otherwise, calls cp_parser_braced_list. */
19248 static tree
19249 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19251 tree initializer;
19253 /* Assume the expression is constant. */
19254 *non_constant_p = false;
19256 /* If it is not a `{', then we are looking at an
19257 assignment-expression. */
19258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19260 initializer
19261 = cp_parser_constant_expression (parser,
19262 /*allow_non_constant_p=*/true,
19263 non_constant_p);
19265 else
19266 initializer = cp_parser_braced_list (parser, non_constant_p);
19268 return initializer;
19271 /* Parse a brace-enclosed initializer list.
19273 braced-init-list:
19274 { initializer-list , [opt] }
19277 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19278 the elements of the initializer-list (or NULL, if the last
19279 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19280 NULL_TREE. There is no way to detect whether or not the optional
19281 trailing `,' was provided. NON_CONSTANT_P is as for
19282 cp_parser_initializer. */
19284 static tree
19285 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19287 tree initializer;
19289 /* Consume the `{' token. */
19290 cp_lexer_consume_token (parser->lexer);
19291 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19292 initializer = make_node (CONSTRUCTOR);
19293 /* If it's not a `}', then there is a non-trivial initializer. */
19294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19296 /* Parse the initializer list. */
19297 CONSTRUCTOR_ELTS (initializer)
19298 = cp_parser_initializer_list (parser, non_constant_p);
19299 /* A trailing `,' token is allowed. */
19300 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19301 cp_lexer_consume_token (parser->lexer);
19303 else
19304 *non_constant_p = false;
19305 /* Now, there should be a trailing `}'. */
19306 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19307 TREE_TYPE (initializer) = init_list_type_node;
19308 return initializer;
19311 /* Consume tokens up to, and including, the next non-nested closing `]'.
19312 Returns true iff we found a closing `]'. */
19314 static bool
19315 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19317 unsigned square_depth = 0;
19319 while (true)
19321 cp_token * token = cp_lexer_peek_token (parser->lexer);
19323 switch (token->type)
19325 case CPP_EOF:
19326 case CPP_PRAGMA_EOL:
19327 /* If we've run out of tokens, then there is no closing `]'. */
19328 return false;
19330 case CPP_OPEN_SQUARE:
19331 ++square_depth;
19332 break;
19334 case CPP_CLOSE_SQUARE:
19335 if (!square_depth--)
19337 cp_lexer_consume_token (parser->lexer);
19338 return true;
19340 break;
19342 default:
19343 break;
19346 /* Consume the token. */
19347 cp_lexer_consume_token (parser->lexer);
19351 /* Return true if we are looking at an array-designator, false otherwise. */
19353 static bool
19354 cp_parser_array_designator_p (cp_parser *parser)
19356 /* Consume the `['. */
19357 cp_lexer_consume_token (parser->lexer);
19359 cp_lexer_save_tokens (parser->lexer);
19361 /* Skip tokens until the next token is a closing square bracket.
19362 If we find the closing `]', and the next token is a `=', then
19363 we are looking at an array designator. */
19364 bool array_designator_p
19365 = (cp_parser_skip_to_closing_square_bracket (parser)
19366 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19368 /* Roll back the tokens we skipped. */
19369 cp_lexer_rollback_tokens (parser->lexer);
19371 return array_designator_p;
19374 /* Parse an initializer-list.
19376 initializer-list:
19377 initializer-clause ... [opt]
19378 initializer-list , initializer-clause ... [opt]
19380 GNU Extension:
19382 initializer-list:
19383 designation initializer-clause ...[opt]
19384 initializer-list , designation initializer-clause ...[opt]
19386 designation:
19387 . identifier =
19388 identifier :
19389 [ constant-expression ] =
19391 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19392 for the initializer. If the INDEX of the elt is non-NULL, it is the
19393 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19394 as for cp_parser_initializer. */
19396 static vec<constructor_elt, va_gc> *
19397 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19399 vec<constructor_elt, va_gc> *v = NULL;
19401 /* Assume all of the expressions are constant. */
19402 *non_constant_p = false;
19404 /* Parse the rest of the list. */
19405 while (true)
19407 cp_token *token;
19408 tree designator;
19409 tree initializer;
19410 bool clause_non_constant_p;
19412 /* If the next token is an identifier and the following one is a
19413 colon, we are looking at the GNU designated-initializer
19414 syntax. */
19415 if (cp_parser_allow_gnu_extensions_p (parser)
19416 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19417 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19419 /* Warn the user that they are using an extension. */
19420 pedwarn (input_location, OPT_Wpedantic,
19421 "ISO C++ does not allow designated initializers");
19422 /* Consume the identifier. */
19423 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19424 /* Consume the `:'. */
19425 cp_lexer_consume_token (parser->lexer);
19427 /* Also handle the C99 syntax, '. id ='. */
19428 else if (cp_parser_allow_gnu_extensions_p (parser)
19429 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19430 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19431 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19433 /* Warn the user that they are using an extension. */
19434 pedwarn (input_location, OPT_Wpedantic,
19435 "ISO C++ does not allow C99 designated initializers");
19436 /* Consume the `.'. */
19437 cp_lexer_consume_token (parser->lexer);
19438 /* Consume the identifier. */
19439 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19440 /* Consume the `='. */
19441 cp_lexer_consume_token (parser->lexer);
19443 /* Also handle C99 array designators, '[ const ] ='. */
19444 else if (cp_parser_allow_gnu_extensions_p (parser)
19445 && !c_dialect_objc ()
19446 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19448 /* In C++11, [ could start a lambda-introducer. */
19449 bool non_const = false;
19451 cp_parser_parse_tentatively (parser);
19453 if (!cp_parser_array_designator_p (parser))
19455 cp_parser_simulate_error (parser);
19456 designator = NULL_TREE;
19458 else
19460 designator = cp_parser_constant_expression (parser, true,
19461 &non_const);
19462 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19463 cp_parser_require (parser, CPP_EQ, RT_EQ);
19466 if (!cp_parser_parse_definitely (parser))
19467 designator = NULL_TREE;
19468 else if (non_const)
19469 require_potential_rvalue_constant_expression (designator);
19471 else
19472 designator = NULL_TREE;
19474 /* Parse the initializer. */
19475 initializer = cp_parser_initializer_clause (parser,
19476 &clause_non_constant_p);
19477 /* If any clause is non-constant, so is the entire initializer. */
19478 if (clause_non_constant_p)
19479 *non_constant_p = true;
19481 /* If we have an ellipsis, this is an initializer pack
19482 expansion. */
19483 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19485 /* Consume the `...'. */
19486 cp_lexer_consume_token (parser->lexer);
19488 /* Turn the initializer into an initializer expansion. */
19489 initializer = make_pack_expansion (initializer);
19492 /* Add it to the vector. */
19493 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19495 /* If the next token is not a comma, we have reached the end of
19496 the list. */
19497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19498 break;
19500 /* Peek at the next token. */
19501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19502 /* If the next token is a `}', then we're still done. An
19503 initializer-clause can have a trailing `,' after the
19504 initializer-list and before the closing `}'. */
19505 if (token->type == CPP_CLOSE_BRACE)
19506 break;
19508 /* Consume the `,' token. */
19509 cp_lexer_consume_token (parser->lexer);
19512 return v;
19515 /* Classes [gram.class] */
19517 /* Parse a class-name.
19519 class-name:
19520 identifier
19521 template-id
19523 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19524 to indicate that names looked up in dependent types should be
19525 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19526 keyword has been used to indicate that the name that appears next
19527 is a template. TAG_TYPE indicates the explicit tag given before
19528 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19529 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19530 is the class being defined in a class-head.
19532 Returns the TYPE_DECL representing the class. */
19534 static tree
19535 cp_parser_class_name (cp_parser *parser,
19536 bool typename_keyword_p,
19537 bool template_keyword_p,
19538 enum tag_types tag_type,
19539 bool check_dependency_p,
19540 bool class_head_p,
19541 bool is_declaration)
19543 tree decl;
19544 tree scope;
19545 bool typename_p;
19546 cp_token *token;
19547 tree identifier = NULL_TREE;
19549 /* All class-names start with an identifier. */
19550 token = cp_lexer_peek_token (parser->lexer);
19551 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19553 cp_parser_error (parser, "expected class-name");
19554 return error_mark_node;
19557 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19558 to a template-id, so we save it here. */
19559 scope = parser->scope;
19560 if (scope == error_mark_node)
19561 return error_mark_node;
19563 /* Any name names a type if we're following the `typename' keyword
19564 in a qualified name where the enclosing scope is type-dependent. */
19565 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19566 && dependent_type_p (scope));
19567 /* Handle the common case (an identifier, but not a template-id)
19568 efficiently. */
19569 if (token->type == CPP_NAME
19570 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19572 cp_token *identifier_token;
19573 bool ambiguous_p;
19575 /* Look for the identifier. */
19576 identifier_token = cp_lexer_peek_token (parser->lexer);
19577 ambiguous_p = identifier_token->error_reported;
19578 identifier = cp_parser_identifier (parser);
19579 /* If the next token isn't an identifier, we are certainly not
19580 looking at a class-name. */
19581 if (identifier == error_mark_node)
19582 decl = error_mark_node;
19583 /* If we know this is a type-name, there's no need to look it
19584 up. */
19585 else if (typename_p)
19586 decl = identifier;
19587 else
19589 tree ambiguous_decls;
19590 /* If we already know that this lookup is ambiguous, then
19591 we've already issued an error message; there's no reason
19592 to check again. */
19593 if (ambiguous_p)
19595 cp_parser_simulate_error (parser);
19596 return error_mark_node;
19598 /* If the next token is a `::', then the name must be a type
19599 name.
19601 [basic.lookup.qual]
19603 During the lookup for a name preceding the :: scope
19604 resolution operator, object, function, and enumerator
19605 names are ignored. */
19606 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19607 tag_type = typename_type;
19608 /* Look up the name. */
19609 decl = cp_parser_lookup_name (parser, identifier,
19610 tag_type,
19611 /*is_template=*/false,
19612 /*is_namespace=*/false,
19613 check_dependency_p,
19614 &ambiguous_decls,
19615 identifier_token->location);
19616 if (ambiguous_decls)
19618 if (cp_parser_parsing_tentatively (parser))
19619 cp_parser_simulate_error (parser);
19620 return error_mark_node;
19624 else
19626 /* Try a template-id. */
19627 decl = cp_parser_template_id (parser, template_keyword_p,
19628 check_dependency_p,
19629 tag_type,
19630 is_declaration);
19631 if (decl == error_mark_node)
19632 return error_mark_node;
19635 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19637 /* If this is a typename, create a TYPENAME_TYPE. */
19638 if (typename_p && decl != error_mark_node)
19640 decl = make_typename_type (scope, decl, typename_type,
19641 /*complain=*/tf_error);
19642 if (decl != error_mark_node)
19643 decl = TYPE_NAME (decl);
19646 decl = strip_using_decl (decl);
19648 /* Check to see that it is really the name of a class. */
19649 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19650 && identifier_p (TREE_OPERAND (decl, 0))
19651 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19652 /* Situations like this:
19654 template <typename T> struct A {
19655 typename T::template X<int>::I i;
19658 are problematic. Is `T::template X<int>' a class-name? The
19659 standard does not seem to be definitive, but there is no other
19660 valid interpretation of the following `::'. Therefore, those
19661 names are considered class-names. */
19663 decl = make_typename_type (scope, decl, tag_type, tf_error);
19664 if (decl != error_mark_node)
19665 decl = TYPE_NAME (decl);
19667 else if (TREE_CODE (decl) != TYPE_DECL
19668 || TREE_TYPE (decl) == error_mark_node
19669 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19670 /* In Objective-C 2.0, a classname followed by '.' starts a
19671 dot-syntax expression, and it's not a type-name. */
19672 || (c_dialect_objc ()
19673 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19674 && objc_is_class_name (decl)))
19675 decl = error_mark_node;
19677 if (decl == error_mark_node)
19678 cp_parser_error (parser, "expected class-name");
19679 else if (identifier && !parser->scope)
19680 maybe_note_name_used_in_class (identifier, decl);
19682 return decl;
19685 /* Parse a class-specifier.
19687 class-specifier:
19688 class-head { member-specification [opt] }
19690 Returns the TREE_TYPE representing the class. */
19692 static tree
19693 cp_parser_class_specifier_1 (cp_parser* parser)
19695 tree type;
19696 tree attributes = NULL_TREE;
19697 bool nested_name_specifier_p;
19698 unsigned saved_num_template_parameter_lists;
19699 bool saved_in_function_body;
19700 unsigned char in_statement;
19701 bool in_switch_statement_p;
19702 bool saved_in_unbraced_linkage_specification_p;
19703 tree old_scope = NULL_TREE;
19704 tree scope = NULL_TREE;
19705 cp_token *closing_brace;
19707 push_deferring_access_checks (dk_no_deferred);
19709 /* Parse the class-head. */
19710 type = cp_parser_class_head (parser,
19711 &nested_name_specifier_p);
19712 /* If the class-head was a semantic disaster, skip the entire body
19713 of the class. */
19714 if (!type)
19716 cp_parser_skip_to_end_of_block_or_statement (parser);
19717 pop_deferring_access_checks ();
19718 return error_mark_node;
19721 /* Look for the `{'. */
19722 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19724 pop_deferring_access_checks ();
19725 return error_mark_node;
19728 cp_ensure_no_omp_declare_simd (parser);
19730 /* Issue an error message if type-definitions are forbidden here. */
19731 cp_parser_check_type_definition (parser);
19732 /* Remember that we are defining one more class. */
19733 ++parser->num_classes_being_defined;
19734 /* Inside the class, surrounding template-parameter-lists do not
19735 apply. */
19736 saved_num_template_parameter_lists
19737 = parser->num_template_parameter_lists;
19738 parser->num_template_parameter_lists = 0;
19739 /* We are not in a function body. */
19740 saved_in_function_body = parser->in_function_body;
19741 parser->in_function_body = false;
19742 /* Or in a loop. */
19743 in_statement = parser->in_statement;
19744 parser->in_statement = 0;
19745 /* Or in a switch. */
19746 in_switch_statement_p = parser->in_switch_statement_p;
19747 parser->in_switch_statement_p = false;
19748 /* We are not immediately inside an extern "lang" block. */
19749 saved_in_unbraced_linkage_specification_p
19750 = parser->in_unbraced_linkage_specification_p;
19751 parser->in_unbraced_linkage_specification_p = false;
19753 /* Start the class. */
19754 if (nested_name_specifier_p)
19756 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19757 old_scope = push_inner_scope (scope);
19759 type = begin_class_definition (type);
19761 if (type == error_mark_node)
19762 /* If the type is erroneous, skip the entire body of the class. */
19763 cp_parser_skip_to_closing_brace (parser);
19764 else
19765 /* Parse the member-specification. */
19766 cp_parser_member_specification_opt (parser);
19768 /* Look for the trailing `}'. */
19769 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19770 /* Look for trailing attributes to apply to this class. */
19771 if (cp_parser_allow_gnu_extensions_p (parser))
19772 attributes = cp_parser_gnu_attributes_opt (parser);
19773 if (type != error_mark_node)
19774 type = finish_struct (type, attributes);
19775 if (nested_name_specifier_p)
19776 pop_inner_scope (old_scope, scope);
19778 /* We've finished a type definition. Check for the common syntax
19779 error of forgetting a semicolon after the definition. We need to
19780 be careful, as we can't just check for not-a-semicolon and be done
19781 with it; the user might have typed:
19783 class X { } c = ...;
19784 class X { } *p = ...;
19786 and so forth. Instead, enumerate all the possible tokens that
19787 might follow this production; if we don't see one of them, then
19788 complain and silently insert the semicolon. */
19790 cp_token *token = cp_lexer_peek_token (parser->lexer);
19791 bool want_semicolon = true;
19793 if (cp_next_tokens_can_be_std_attribute_p (parser))
19794 /* Don't try to parse c++11 attributes here. As per the
19795 grammar, that should be a task for
19796 cp_parser_decl_specifier_seq. */
19797 want_semicolon = false;
19799 switch (token->type)
19801 case CPP_NAME:
19802 case CPP_SEMICOLON:
19803 case CPP_MULT:
19804 case CPP_AND:
19805 case CPP_OPEN_PAREN:
19806 case CPP_CLOSE_PAREN:
19807 case CPP_COMMA:
19808 want_semicolon = false;
19809 break;
19811 /* While it's legal for type qualifiers and storage class
19812 specifiers to follow type definitions in the grammar, only
19813 compiler testsuites contain code like that. Assume that if
19814 we see such code, then what we're really seeing is a case
19815 like:
19817 class X { }
19818 const <type> var = ...;
19822 class Y { }
19823 static <type> func (...) ...
19825 i.e. the qualifier or specifier applies to the next
19826 declaration. To do so, however, we need to look ahead one
19827 more token to see if *that* token is a type specifier.
19829 This code could be improved to handle:
19831 class Z { }
19832 static const <type> var = ...; */
19833 case CPP_KEYWORD:
19834 if (keyword_is_decl_specifier (token->keyword))
19836 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19838 /* Handling user-defined types here would be nice, but very
19839 tricky. */
19840 want_semicolon
19841 = (lookahead->type == CPP_KEYWORD
19842 && keyword_begins_type_specifier (lookahead->keyword));
19844 break;
19845 default:
19846 break;
19849 /* If we don't have a type, then something is very wrong and we
19850 shouldn't try to do anything clever. Likewise for not seeing the
19851 closing brace. */
19852 if (closing_brace && TYPE_P (type) && want_semicolon)
19854 cp_token_position prev
19855 = cp_lexer_previous_token_position (parser->lexer);
19856 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19857 location_t loc = prev_token->location;
19859 if (CLASSTYPE_DECLARED_CLASS (type))
19860 error_at (loc, "expected %<;%> after class definition");
19861 else if (TREE_CODE (type) == RECORD_TYPE)
19862 error_at (loc, "expected %<;%> after struct definition");
19863 else if (TREE_CODE (type) == UNION_TYPE)
19864 error_at (loc, "expected %<;%> after union definition");
19865 else
19866 gcc_unreachable ();
19868 /* Unget one token and smash it to look as though we encountered
19869 a semicolon in the input stream. */
19870 cp_lexer_set_token_position (parser->lexer, prev);
19871 token = cp_lexer_peek_token (parser->lexer);
19872 token->type = CPP_SEMICOLON;
19873 token->keyword = RID_MAX;
19877 /* If this class is not itself within the scope of another class,
19878 then we need to parse the bodies of all of the queued function
19879 definitions. Note that the queued functions defined in a class
19880 are not always processed immediately following the
19881 class-specifier for that class. Consider:
19883 struct A {
19884 struct B { void f() { sizeof (A); } };
19887 If `f' were processed before the processing of `A' were
19888 completed, there would be no way to compute the size of `A'.
19889 Note that the nesting we are interested in here is lexical --
19890 not the semantic nesting given by TYPE_CONTEXT. In particular,
19891 for:
19893 struct A { struct B; };
19894 struct A::B { void f() { } };
19896 there is no need to delay the parsing of `A::B::f'. */
19897 if (--parser->num_classes_being_defined == 0)
19899 tree decl;
19900 tree class_type = NULL_TREE;
19901 tree pushed_scope = NULL_TREE;
19902 unsigned ix;
19903 cp_default_arg_entry *e;
19904 tree save_ccp, save_ccr;
19906 /* In a first pass, parse default arguments to the functions.
19907 Then, in a second pass, parse the bodies of the functions.
19908 This two-phased approach handles cases like:
19910 struct S {
19911 void f() { g(); }
19912 void g(int i = 3);
19916 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19918 decl = e->decl;
19919 /* If there are default arguments that have not yet been processed,
19920 take care of them now. */
19921 if (class_type != e->class_type)
19923 if (pushed_scope)
19924 pop_scope (pushed_scope);
19925 class_type = e->class_type;
19926 pushed_scope = push_scope (class_type);
19928 /* Make sure that any template parameters are in scope. */
19929 maybe_begin_member_template_processing (decl);
19930 /* Parse the default argument expressions. */
19931 cp_parser_late_parsing_default_args (parser, decl);
19932 /* Remove any template parameters from the symbol table. */
19933 maybe_end_member_template_processing ();
19935 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19936 /* Now parse any NSDMIs. */
19937 save_ccp = current_class_ptr;
19938 save_ccr = current_class_ref;
19939 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19941 if (class_type != DECL_CONTEXT (decl))
19943 if (pushed_scope)
19944 pop_scope (pushed_scope);
19945 class_type = DECL_CONTEXT (decl);
19946 pushed_scope = push_scope (class_type);
19948 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19949 cp_parser_late_parsing_nsdmi (parser, decl);
19951 vec_safe_truncate (unparsed_nsdmis, 0);
19952 current_class_ptr = save_ccp;
19953 current_class_ref = save_ccr;
19954 if (pushed_scope)
19955 pop_scope (pushed_scope);
19957 /* Now do some post-NSDMI bookkeeping. */
19958 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19959 after_nsdmi_defaulted_late_checks (class_type);
19960 vec_safe_truncate (unparsed_classes, 0);
19961 after_nsdmi_defaulted_late_checks (type);
19963 /* Now parse the body of the functions. */
19964 if (flag_openmp)
19966 /* OpenMP UDRs need to be parsed before all other functions. */
19967 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19968 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19969 cp_parser_late_parsing_for_member (parser, decl);
19970 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19971 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19972 cp_parser_late_parsing_for_member (parser, decl);
19974 else
19975 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19976 cp_parser_late_parsing_for_member (parser, decl);
19977 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19979 else
19980 vec_safe_push (unparsed_classes, type);
19982 /* Put back any saved access checks. */
19983 pop_deferring_access_checks ();
19985 /* Restore saved state. */
19986 parser->in_switch_statement_p = in_switch_statement_p;
19987 parser->in_statement = in_statement;
19988 parser->in_function_body = saved_in_function_body;
19989 parser->num_template_parameter_lists
19990 = saved_num_template_parameter_lists;
19991 parser->in_unbraced_linkage_specification_p
19992 = saved_in_unbraced_linkage_specification_p;
19994 return type;
19997 static tree
19998 cp_parser_class_specifier (cp_parser* parser)
20000 tree ret;
20001 timevar_push (TV_PARSE_STRUCT);
20002 ret = cp_parser_class_specifier_1 (parser);
20003 timevar_pop (TV_PARSE_STRUCT);
20004 return ret;
20007 /* Parse a class-head.
20009 class-head:
20010 class-key identifier [opt] base-clause [opt]
20011 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20012 class-key nested-name-specifier [opt] template-id
20013 base-clause [opt]
20015 class-virt-specifier:
20016 final
20018 GNU Extensions:
20019 class-key attributes identifier [opt] base-clause [opt]
20020 class-key attributes nested-name-specifier identifier base-clause [opt]
20021 class-key attributes nested-name-specifier [opt] template-id
20022 base-clause [opt]
20024 Upon return BASES is initialized to the list of base classes (or
20025 NULL, if there are none) in the same form returned by
20026 cp_parser_base_clause.
20028 Returns the TYPE of the indicated class. Sets
20029 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20030 involving a nested-name-specifier was used, and FALSE otherwise.
20032 Returns error_mark_node if this is not a class-head.
20034 Returns NULL_TREE if the class-head is syntactically valid, but
20035 semantically invalid in a way that means we should skip the entire
20036 body of the class. */
20038 static tree
20039 cp_parser_class_head (cp_parser* parser,
20040 bool* nested_name_specifier_p)
20042 tree nested_name_specifier;
20043 enum tag_types class_key;
20044 tree id = NULL_TREE;
20045 tree type = NULL_TREE;
20046 tree attributes;
20047 tree bases;
20048 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20049 bool template_id_p = false;
20050 bool qualified_p = false;
20051 bool invalid_nested_name_p = false;
20052 bool invalid_explicit_specialization_p = false;
20053 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20054 tree pushed_scope = NULL_TREE;
20055 unsigned num_templates;
20056 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20057 /* Assume no nested-name-specifier will be present. */
20058 *nested_name_specifier_p = false;
20059 /* Assume no template parameter lists will be used in defining the
20060 type. */
20061 num_templates = 0;
20062 parser->colon_corrects_to_scope_p = false;
20064 /* Look for the class-key. */
20065 class_key = cp_parser_class_key (parser);
20066 if (class_key == none_type)
20067 return error_mark_node;
20069 /* Parse the attributes. */
20070 attributes = cp_parser_attributes_opt (parser);
20072 /* If the next token is `::', that is invalid -- but sometimes
20073 people do try to write:
20075 struct ::S {};
20077 Handle this gracefully by accepting the extra qualifier, and then
20078 issuing an error about it later if this really is a
20079 class-head. If it turns out just to be an elaborated type
20080 specifier, remain silent. */
20081 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20082 qualified_p = true;
20084 push_deferring_access_checks (dk_no_check);
20086 /* Determine the name of the class. Begin by looking for an
20087 optional nested-name-specifier. */
20088 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20089 nested_name_specifier
20090 = cp_parser_nested_name_specifier_opt (parser,
20091 /*typename_keyword_p=*/false,
20092 /*check_dependency_p=*/false,
20093 /*type_p=*/true,
20094 /*is_declaration=*/false);
20095 /* If there was a nested-name-specifier, then there *must* be an
20096 identifier. */
20097 if (nested_name_specifier)
20099 type_start_token = cp_lexer_peek_token (parser->lexer);
20100 /* Although the grammar says `identifier', it really means
20101 `class-name' or `template-name'. You are only allowed to
20102 define a class that has already been declared with this
20103 syntax.
20105 The proposed resolution for Core Issue 180 says that wherever
20106 you see `class T::X' you should treat `X' as a type-name.
20108 It is OK to define an inaccessible class; for example:
20110 class A { class B; };
20111 class A::B {};
20113 We do not know if we will see a class-name, or a
20114 template-name. We look for a class-name first, in case the
20115 class-name is a template-id; if we looked for the
20116 template-name first we would stop after the template-name. */
20117 cp_parser_parse_tentatively (parser);
20118 type = cp_parser_class_name (parser,
20119 /*typename_keyword_p=*/false,
20120 /*template_keyword_p=*/false,
20121 class_type,
20122 /*check_dependency_p=*/false,
20123 /*class_head_p=*/true,
20124 /*is_declaration=*/false);
20125 /* If that didn't work, ignore the nested-name-specifier. */
20126 if (!cp_parser_parse_definitely (parser))
20128 invalid_nested_name_p = true;
20129 type_start_token = cp_lexer_peek_token (parser->lexer);
20130 id = cp_parser_identifier (parser);
20131 if (id == error_mark_node)
20132 id = NULL_TREE;
20134 /* If we could not find a corresponding TYPE, treat this
20135 declaration like an unqualified declaration. */
20136 if (type == error_mark_node)
20137 nested_name_specifier = NULL_TREE;
20138 /* Otherwise, count the number of templates used in TYPE and its
20139 containing scopes. */
20140 else
20142 tree scope;
20144 for (scope = TREE_TYPE (type);
20145 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20146 scope = get_containing_scope (scope))
20147 if (TYPE_P (scope)
20148 && CLASS_TYPE_P (scope)
20149 && CLASSTYPE_TEMPLATE_INFO (scope)
20150 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20151 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20152 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20153 ++num_templates;
20156 /* Otherwise, the identifier is optional. */
20157 else
20159 /* We don't know whether what comes next is a template-id,
20160 an identifier, or nothing at all. */
20161 cp_parser_parse_tentatively (parser);
20162 /* Check for a template-id. */
20163 type_start_token = cp_lexer_peek_token (parser->lexer);
20164 id = cp_parser_template_id (parser,
20165 /*template_keyword_p=*/false,
20166 /*check_dependency_p=*/true,
20167 class_key,
20168 /*is_declaration=*/true);
20169 /* If that didn't work, it could still be an identifier. */
20170 if (!cp_parser_parse_definitely (parser))
20172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20174 type_start_token = cp_lexer_peek_token (parser->lexer);
20175 id = cp_parser_identifier (parser);
20177 else
20178 id = NULL_TREE;
20180 else
20182 template_id_p = true;
20183 ++num_templates;
20187 pop_deferring_access_checks ();
20189 if (id)
20191 cp_parser_check_for_invalid_template_id (parser, id,
20192 class_key,
20193 type_start_token->location);
20195 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20197 /* If it's not a `:' or a `{' then we can't really be looking at a
20198 class-head, since a class-head only appears as part of a
20199 class-specifier. We have to detect this situation before calling
20200 xref_tag, since that has irreversible side-effects. */
20201 if (!cp_parser_next_token_starts_class_definition_p (parser))
20203 cp_parser_error (parser, "expected %<{%> or %<:%>");
20204 type = error_mark_node;
20205 goto out;
20208 /* At this point, we're going ahead with the class-specifier, even
20209 if some other problem occurs. */
20210 cp_parser_commit_to_tentative_parse (parser);
20211 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20213 cp_parser_error (parser,
20214 "cannot specify %<override%> for a class");
20215 type = error_mark_node;
20216 goto out;
20218 /* Issue the error about the overly-qualified name now. */
20219 if (qualified_p)
20221 cp_parser_error (parser,
20222 "global qualification of class name is invalid");
20223 type = error_mark_node;
20224 goto out;
20226 else if (invalid_nested_name_p)
20228 cp_parser_error (parser,
20229 "qualified name does not name a class");
20230 type = error_mark_node;
20231 goto out;
20233 else if (nested_name_specifier)
20235 tree scope;
20237 /* Reject typedef-names in class heads. */
20238 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20240 error_at (type_start_token->location,
20241 "invalid class name in declaration of %qD",
20242 type);
20243 type = NULL_TREE;
20244 goto done;
20247 /* Figure out in what scope the declaration is being placed. */
20248 scope = current_scope ();
20249 /* If that scope does not contain the scope in which the
20250 class was originally declared, the program is invalid. */
20251 if (scope && !is_ancestor (scope, nested_name_specifier))
20253 if (at_namespace_scope_p ())
20254 error_at (type_start_token->location,
20255 "declaration of %qD in namespace %qD which does not "
20256 "enclose %qD",
20257 type, scope, nested_name_specifier);
20258 else
20259 error_at (type_start_token->location,
20260 "declaration of %qD in %qD which does not enclose %qD",
20261 type, scope, nested_name_specifier);
20262 type = NULL_TREE;
20263 goto done;
20265 /* [dcl.meaning]
20267 A declarator-id shall not be qualified except for the
20268 definition of a ... nested class outside of its class
20269 ... [or] the definition or explicit instantiation of a
20270 class member of a namespace outside of its namespace. */
20271 if (scope == nested_name_specifier)
20273 permerror (nested_name_specifier_token_start->location,
20274 "extra qualification not allowed");
20275 nested_name_specifier = NULL_TREE;
20276 num_templates = 0;
20279 /* An explicit-specialization must be preceded by "template <>". If
20280 it is not, try to recover gracefully. */
20281 if (at_namespace_scope_p ()
20282 && parser->num_template_parameter_lists == 0
20283 && template_id_p)
20285 error_at (type_start_token->location,
20286 "an explicit specialization must be preceded by %<template <>%>");
20287 invalid_explicit_specialization_p = true;
20288 /* Take the same action that would have been taken by
20289 cp_parser_explicit_specialization. */
20290 ++parser->num_template_parameter_lists;
20291 begin_specialization ();
20293 /* There must be no "return" statements between this point and the
20294 end of this function; set "type "to the correct return value and
20295 use "goto done;" to return. */
20296 /* Make sure that the right number of template parameters were
20297 present. */
20298 if (!cp_parser_check_template_parameters (parser, num_templates,
20299 type_start_token->location,
20300 /*declarator=*/NULL))
20302 /* If something went wrong, there is no point in even trying to
20303 process the class-definition. */
20304 type = NULL_TREE;
20305 goto done;
20308 /* Look up the type. */
20309 if (template_id_p)
20311 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20312 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20313 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20315 error_at (type_start_token->location,
20316 "function template %qD redeclared as a class template", id);
20317 type = error_mark_node;
20319 else
20321 type = TREE_TYPE (id);
20322 type = maybe_process_partial_specialization (type);
20324 if (nested_name_specifier)
20325 pushed_scope = push_scope (nested_name_specifier);
20327 else if (nested_name_specifier)
20329 tree class_type;
20331 /* Given:
20333 template <typename T> struct S { struct T };
20334 template <typename T> struct S<T>::T { };
20336 we will get a TYPENAME_TYPE when processing the definition of
20337 `S::T'. We need to resolve it to the actual type before we
20338 try to define it. */
20339 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20341 class_type = resolve_typename_type (TREE_TYPE (type),
20342 /*only_current_p=*/false);
20343 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20344 type = TYPE_NAME (class_type);
20345 else
20347 cp_parser_error (parser, "could not resolve typename type");
20348 type = error_mark_node;
20352 if (maybe_process_partial_specialization (TREE_TYPE (type))
20353 == error_mark_node)
20355 type = NULL_TREE;
20356 goto done;
20359 class_type = current_class_type;
20360 /* Enter the scope indicated by the nested-name-specifier. */
20361 pushed_scope = push_scope (nested_name_specifier);
20362 /* Get the canonical version of this type. */
20363 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20364 /* Call push_template_decl if it seems like we should be defining a
20365 template either from the template headers or the type we're
20366 defining, so that we diagnose both extra and missing headers. */
20367 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20368 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20369 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20371 type = push_template_decl (type);
20372 if (type == error_mark_node)
20374 type = NULL_TREE;
20375 goto done;
20379 type = TREE_TYPE (type);
20380 *nested_name_specifier_p = true;
20382 else /* The name is not a nested name. */
20384 /* If the class was unnamed, create a dummy name. */
20385 if (!id)
20386 id = make_anon_name ();
20387 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20388 parser->num_template_parameter_lists);
20391 /* Indicate whether this class was declared as a `class' or as a
20392 `struct'. */
20393 if (TREE_CODE (type) == RECORD_TYPE)
20394 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20395 cp_parser_check_class_key (class_key, type);
20397 /* If this type was already complete, and we see another definition,
20398 that's an error. */
20399 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20401 error_at (type_start_token->location, "redefinition of %q#T",
20402 type);
20403 error_at (type_start_token->location, "previous definition of %q+#T",
20404 type);
20405 type = NULL_TREE;
20406 goto done;
20408 else if (type == error_mark_node)
20409 type = NULL_TREE;
20411 if (type)
20413 /* Apply attributes now, before any use of the class as a template
20414 argument in its base list. */
20415 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20416 fixup_attribute_variants (type);
20419 /* We will have entered the scope containing the class; the names of
20420 base classes should be looked up in that context. For example:
20422 struct A { struct B {}; struct C; };
20423 struct A::C : B {};
20425 is valid. */
20427 /* Get the list of base-classes, if there is one. */
20428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20430 /* PR59482: enter the class scope so that base-specifiers are looked
20431 up correctly. */
20432 if (type)
20433 pushclass (type);
20434 bases = cp_parser_base_clause (parser);
20435 /* PR59482: get out of the previously pushed class scope so that the
20436 subsequent pops pop the right thing. */
20437 if (type)
20438 popclass ();
20440 else
20441 bases = NULL_TREE;
20443 /* If we're really defining a class, process the base classes.
20444 If they're invalid, fail. */
20445 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20446 && !xref_basetypes (type, bases))
20447 type = NULL_TREE;
20449 done:
20450 /* Leave the scope given by the nested-name-specifier. We will
20451 enter the class scope itself while processing the members. */
20452 if (pushed_scope)
20453 pop_scope (pushed_scope);
20455 if (invalid_explicit_specialization_p)
20457 end_specialization ();
20458 --parser->num_template_parameter_lists;
20461 if (type)
20462 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20463 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20464 CLASSTYPE_FINAL (type) = 1;
20465 out:
20466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20467 return type;
20470 /* Parse a class-key.
20472 class-key:
20473 class
20474 struct
20475 union
20477 Returns the kind of class-key specified, or none_type to indicate
20478 error. */
20480 static enum tag_types
20481 cp_parser_class_key (cp_parser* parser)
20483 cp_token *token;
20484 enum tag_types tag_type;
20486 /* Look for the class-key. */
20487 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20488 if (!token)
20489 return none_type;
20491 /* Check to see if the TOKEN is a class-key. */
20492 tag_type = cp_parser_token_is_class_key (token);
20493 if (!tag_type)
20494 cp_parser_error (parser, "expected class-key");
20495 return tag_type;
20498 /* Parse a type-parameter-key.
20500 type-parameter-key:
20501 class
20502 typename
20505 static void
20506 cp_parser_type_parameter_key (cp_parser* parser)
20508 /* Look for the type-parameter-key. */
20509 enum tag_types tag_type = none_type;
20510 cp_token *token = cp_lexer_peek_token (parser->lexer);
20511 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20513 cp_lexer_consume_token (parser->lexer);
20514 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20515 /* typename is not allowed in a template template parameter
20516 by the standard until C++1Z. */
20517 pedwarn (token->location, OPT_Wpedantic,
20518 "ISO C++ forbids typename key in template template parameter;"
20519 " use -std=c++1z or -std=gnu++1z");
20521 else
20522 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20524 return;
20527 /* Parse an (optional) member-specification.
20529 member-specification:
20530 member-declaration member-specification [opt]
20531 access-specifier : member-specification [opt] */
20533 static void
20534 cp_parser_member_specification_opt (cp_parser* parser)
20536 while (true)
20538 cp_token *token;
20539 enum rid keyword;
20541 /* Peek at the next token. */
20542 token = cp_lexer_peek_token (parser->lexer);
20543 /* If it's a `}', or EOF then we've seen all the members. */
20544 if (token->type == CPP_CLOSE_BRACE
20545 || token->type == CPP_EOF
20546 || token->type == CPP_PRAGMA_EOL)
20547 break;
20549 /* See if this token is a keyword. */
20550 keyword = token->keyword;
20551 switch (keyword)
20553 case RID_PUBLIC:
20554 case RID_PROTECTED:
20555 case RID_PRIVATE:
20556 /* Consume the access-specifier. */
20557 cp_lexer_consume_token (parser->lexer);
20558 /* Remember which access-specifier is active. */
20559 current_access_specifier = token->u.value;
20560 /* Look for the `:'. */
20561 cp_parser_require (parser, CPP_COLON, RT_COLON);
20562 break;
20564 default:
20565 /* Accept #pragmas at class scope. */
20566 if (token->type == CPP_PRAGMA)
20568 cp_parser_pragma (parser, pragma_member);
20569 break;
20572 /* Otherwise, the next construction must be a
20573 member-declaration. */
20574 cp_parser_member_declaration (parser);
20579 /* Parse a member-declaration.
20581 member-declaration:
20582 decl-specifier-seq [opt] member-declarator-list [opt] ;
20583 function-definition ; [opt]
20584 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20585 using-declaration
20586 template-declaration
20587 alias-declaration
20589 member-declarator-list:
20590 member-declarator
20591 member-declarator-list , member-declarator
20593 member-declarator:
20594 declarator pure-specifier [opt]
20595 declarator constant-initializer [opt]
20596 identifier [opt] : constant-expression
20598 GNU Extensions:
20600 member-declaration:
20601 __extension__ member-declaration
20603 member-declarator:
20604 declarator attributes [opt] pure-specifier [opt]
20605 declarator attributes [opt] constant-initializer [opt]
20606 identifier [opt] attributes [opt] : constant-expression
20608 C++0x Extensions:
20610 member-declaration:
20611 static_assert-declaration */
20613 static void
20614 cp_parser_member_declaration (cp_parser* parser)
20616 cp_decl_specifier_seq decl_specifiers;
20617 tree prefix_attributes;
20618 tree decl;
20619 int declares_class_or_enum;
20620 bool friend_p;
20621 cp_token *token = NULL;
20622 cp_token *decl_spec_token_start = NULL;
20623 cp_token *initializer_token_start = NULL;
20624 int saved_pedantic;
20625 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20627 /* Check for the `__extension__' keyword. */
20628 if (cp_parser_extension_opt (parser, &saved_pedantic))
20630 /* Recurse. */
20631 cp_parser_member_declaration (parser);
20632 /* Restore the old value of the PEDANTIC flag. */
20633 pedantic = saved_pedantic;
20635 return;
20638 /* Check for a template-declaration. */
20639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20641 /* An explicit specialization here is an error condition, and we
20642 expect the specialization handler to detect and report this. */
20643 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20644 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20645 cp_parser_explicit_specialization (parser);
20646 else
20647 cp_parser_template_declaration (parser, /*member_p=*/true);
20649 return;
20652 /* Check for a using-declaration. */
20653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20655 if (cxx_dialect < cxx11)
20657 /* Parse the using-declaration. */
20658 cp_parser_using_declaration (parser,
20659 /*access_declaration_p=*/false);
20660 return;
20662 else
20664 tree decl;
20665 bool alias_decl_expected;
20666 cp_parser_parse_tentatively (parser);
20667 decl = cp_parser_alias_declaration (parser);
20668 /* Note that if we actually see the '=' token after the
20669 identifier, cp_parser_alias_declaration commits the
20670 tentative parse. In that case, we really expects an
20671 alias-declaration. Otherwise, we expect a using
20672 declaration. */
20673 alias_decl_expected =
20674 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20675 cp_parser_parse_definitely (parser);
20677 if (alias_decl_expected)
20678 finish_member_declaration (decl);
20679 else
20680 cp_parser_using_declaration (parser,
20681 /*access_declaration_p=*/false);
20682 return;
20686 /* Check for @defs. */
20687 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20689 tree ivar, member;
20690 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20691 ivar = ivar_chains;
20692 while (ivar)
20694 member = ivar;
20695 ivar = TREE_CHAIN (member);
20696 TREE_CHAIN (member) = NULL_TREE;
20697 finish_member_declaration (member);
20699 return;
20702 /* If the next token is `static_assert' we have a static assertion. */
20703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20705 cp_parser_static_assert (parser, /*member_p=*/true);
20706 return;
20709 parser->colon_corrects_to_scope_p = false;
20711 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20712 goto out;
20714 /* Parse the decl-specifier-seq. */
20715 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20716 cp_parser_decl_specifier_seq (parser,
20717 CP_PARSER_FLAGS_OPTIONAL,
20718 &decl_specifiers,
20719 &declares_class_or_enum);
20720 /* Check for an invalid type-name. */
20721 if (!decl_specifiers.any_type_specifiers_p
20722 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20723 goto out;
20724 /* If there is no declarator, then the decl-specifier-seq should
20725 specify a type. */
20726 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20728 /* If there was no decl-specifier-seq, and the next token is a
20729 `;', then we have something like:
20731 struct S { ; };
20733 [class.mem]
20735 Each member-declaration shall declare at least one member
20736 name of the class. */
20737 if (!decl_specifiers.any_specifiers_p)
20739 cp_token *token = cp_lexer_peek_token (parser->lexer);
20740 if (!in_system_header_at (token->location))
20741 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20743 else
20745 tree type;
20747 /* See if this declaration is a friend. */
20748 friend_p = cp_parser_friend_p (&decl_specifiers);
20749 /* If there were decl-specifiers, check to see if there was
20750 a class-declaration. */
20751 type = check_tag_decl (&decl_specifiers,
20752 /*explicit_type_instantiation_p=*/false);
20753 /* Nested classes have already been added to the class, but
20754 a `friend' needs to be explicitly registered. */
20755 if (friend_p)
20757 /* If the `friend' keyword was present, the friend must
20758 be introduced with a class-key. */
20759 if (!declares_class_or_enum && cxx_dialect < cxx11)
20760 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20761 "in C++03 a class-key must be used "
20762 "when declaring a friend");
20763 /* In this case:
20765 template <typename T> struct A {
20766 friend struct A<T>::B;
20769 A<T>::B will be represented by a TYPENAME_TYPE, and
20770 therefore not recognized by check_tag_decl. */
20771 if (!type)
20773 type = decl_specifiers.type;
20774 if (type && TREE_CODE (type) == TYPE_DECL)
20775 type = TREE_TYPE (type);
20777 if (!type || !TYPE_P (type))
20778 error_at (decl_spec_token_start->location,
20779 "friend declaration does not name a class or "
20780 "function");
20781 else
20782 make_friend_class (current_class_type, type,
20783 /*complain=*/true);
20785 /* If there is no TYPE, an error message will already have
20786 been issued. */
20787 else if (!type || type == error_mark_node)
20789 /* An anonymous aggregate has to be handled specially; such
20790 a declaration really declares a data member (with a
20791 particular type), as opposed to a nested class. */
20792 else if (ANON_AGGR_TYPE_P (type))
20794 /* C++11 9.5/6. */
20795 if (decl_specifiers.storage_class != sc_none)
20796 error_at (decl_spec_token_start->location,
20797 "a storage class on an anonymous aggregate "
20798 "in class scope is not allowed");
20800 /* Remove constructors and such from TYPE, now that we
20801 know it is an anonymous aggregate. */
20802 fixup_anonymous_aggr (type);
20803 /* And make the corresponding data member. */
20804 decl = build_decl (decl_spec_token_start->location,
20805 FIELD_DECL, NULL_TREE, type);
20806 /* Add it to the class. */
20807 finish_member_declaration (decl);
20809 else
20810 cp_parser_check_access_in_redeclaration
20811 (TYPE_NAME (type),
20812 decl_spec_token_start->location);
20815 else
20817 bool assume_semicolon = false;
20819 /* Clear attributes from the decl_specifiers but keep them
20820 around as prefix attributes that apply them to the entity
20821 being declared. */
20822 prefix_attributes = decl_specifiers.attributes;
20823 decl_specifiers.attributes = NULL_TREE;
20825 /* See if these declarations will be friends. */
20826 friend_p = cp_parser_friend_p (&decl_specifiers);
20828 /* Keep going until we hit the `;' at the end of the
20829 declaration. */
20830 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20832 tree attributes = NULL_TREE;
20833 tree first_attribute;
20835 /* Peek at the next token. */
20836 token = cp_lexer_peek_token (parser->lexer);
20838 /* Check for a bitfield declaration. */
20839 if (token->type == CPP_COLON
20840 || (token->type == CPP_NAME
20841 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20842 == CPP_COLON))
20844 tree identifier;
20845 tree width;
20847 /* Get the name of the bitfield. Note that we cannot just
20848 check TOKEN here because it may have been invalidated by
20849 the call to cp_lexer_peek_nth_token above. */
20850 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20851 identifier = cp_parser_identifier (parser);
20852 else
20853 identifier = NULL_TREE;
20855 /* Consume the `:' token. */
20856 cp_lexer_consume_token (parser->lexer);
20857 /* Get the width of the bitfield. */
20858 width
20859 = cp_parser_constant_expression (parser);
20861 /* Look for attributes that apply to the bitfield. */
20862 attributes = cp_parser_attributes_opt (parser);
20863 /* Remember which attributes are prefix attributes and
20864 which are not. */
20865 first_attribute = attributes;
20866 /* Combine the attributes. */
20867 attributes = chainon (prefix_attributes, attributes);
20869 /* Create the bitfield declaration. */
20870 decl = grokbitfield (identifier
20871 ? make_id_declarator (NULL_TREE,
20872 identifier,
20873 sfk_none)
20874 : NULL,
20875 &decl_specifiers,
20876 width,
20877 attributes);
20879 else
20881 cp_declarator *declarator;
20882 tree initializer;
20883 tree asm_specification;
20884 int ctor_dtor_or_conv_p;
20886 /* Parse the declarator. */
20887 declarator
20888 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20889 &ctor_dtor_or_conv_p,
20890 /*parenthesized_p=*/NULL,
20891 /*member_p=*/true,
20892 friend_p);
20894 /* If something went wrong parsing the declarator, make sure
20895 that we at least consume some tokens. */
20896 if (declarator == cp_error_declarator)
20898 /* Skip to the end of the statement. */
20899 cp_parser_skip_to_end_of_statement (parser);
20900 /* If the next token is not a semicolon, that is
20901 probably because we just skipped over the body of
20902 a function. So, we consume a semicolon if
20903 present, but do not issue an error message if it
20904 is not present. */
20905 if (cp_lexer_next_token_is (parser->lexer,
20906 CPP_SEMICOLON))
20907 cp_lexer_consume_token (parser->lexer);
20908 goto out;
20911 if (declares_class_or_enum & 2)
20912 cp_parser_check_for_definition_in_return_type
20913 (declarator, decl_specifiers.type,
20914 decl_specifiers.locations[ds_type_spec]);
20916 /* Look for an asm-specification. */
20917 asm_specification = cp_parser_asm_specification_opt (parser);
20918 /* Look for attributes that apply to the declaration. */
20919 attributes = cp_parser_attributes_opt (parser);
20920 /* Remember which attributes are prefix attributes and
20921 which are not. */
20922 first_attribute = attributes;
20923 /* Combine the attributes. */
20924 attributes = chainon (prefix_attributes, attributes);
20926 /* If it's an `=', then we have a constant-initializer or a
20927 pure-specifier. It is not correct to parse the
20928 initializer before registering the member declaration
20929 since the member declaration should be in scope while
20930 its initializer is processed. However, the rest of the
20931 front end does not yet provide an interface that allows
20932 us to handle this correctly. */
20933 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20935 /* In [class.mem]:
20937 A pure-specifier shall be used only in the declaration of
20938 a virtual function.
20940 A member-declarator can contain a constant-initializer
20941 only if it declares a static member of integral or
20942 enumeration type.
20944 Therefore, if the DECLARATOR is for a function, we look
20945 for a pure-specifier; otherwise, we look for a
20946 constant-initializer. When we call `grokfield', it will
20947 perform more stringent semantics checks. */
20948 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20949 if (function_declarator_p (declarator)
20950 || (decl_specifiers.type
20951 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20952 && declarator->kind == cdk_id
20953 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20954 == FUNCTION_TYPE)))
20955 initializer = cp_parser_pure_specifier (parser);
20956 else if (decl_specifiers.storage_class != sc_static)
20957 initializer = cp_parser_save_nsdmi (parser);
20958 else if (cxx_dialect >= cxx11)
20960 bool nonconst;
20961 /* Don't require a constant rvalue in C++11, since we
20962 might want a reference constant. We'll enforce
20963 constancy later. */
20964 cp_lexer_consume_token (parser->lexer);
20965 /* Parse the initializer. */
20966 initializer = cp_parser_initializer_clause (parser,
20967 &nonconst);
20969 else
20970 /* Parse the initializer. */
20971 initializer = cp_parser_constant_initializer (parser);
20973 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20974 && !function_declarator_p (declarator))
20976 bool x;
20977 if (decl_specifiers.storage_class != sc_static)
20978 initializer = cp_parser_save_nsdmi (parser);
20979 else
20980 initializer = cp_parser_initializer (parser, &x, &x);
20982 /* Otherwise, there is no initializer. */
20983 else
20984 initializer = NULL_TREE;
20986 /* See if we are probably looking at a function
20987 definition. We are certainly not looking at a
20988 member-declarator. Calling `grokfield' has
20989 side-effects, so we must not do it unless we are sure
20990 that we are looking at a member-declarator. */
20991 if (cp_parser_token_starts_function_definition_p
20992 (cp_lexer_peek_token (parser->lexer)))
20994 /* The grammar does not allow a pure-specifier to be
20995 used when a member function is defined. (It is
20996 possible that this fact is an oversight in the
20997 standard, since a pure function may be defined
20998 outside of the class-specifier. */
20999 if (initializer && initializer_token_start)
21000 error_at (initializer_token_start->location,
21001 "pure-specifier on function-definition");
21002 decl = cp_parser_save_member_function_body (parser,
21003 &decl_specifiers,
21004 declarator,
21005 attributes);
21006 if (parser->fully_implicit_function_template_p)
21007 decl = finish_fully_implicit_template (parser, decl);
21008 /* If the member was not a friend, declare it here. */
21009 if (!friend_p)
21010 finish_member_declaration (decl);
21011 /* Peek at the next token. */
21012 token = cp_lexer_peek_token (parser->lexer);
21013 /* If the next token is a semicolon, consume it. */
21014 if (token->type == CPP_SEMICOLON)
21015 cp_lexer_consume_token (parser->lexer);
21016 goto out;
21018 else
21019 if (declarator->kind == cdk_function)
21020 declarator->id_loc = token->location;
21021 /* Create the declaration. */
21022 decl = grokfield (declarator, &decl_specifiers,
21023 initializer, /*init_const_expr_p=*/true,
21024 asm_specification, attributes);
21025 if (parser->fully_implicit_function_template_p)
21027 if (friend_p)
21028 finish_fully_implicit_template (parser, 0);
21029 else
21030 decl = finish_fully_implicit_template (parser, decl);
21034 cp_finalize_omp_declare_simd (parser, decl);
21036 /* Reset PREFIX_ATTRIBUTES. */
21037 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21038 attributes = TREE_CHAIN (attributes);
21039 if (attributes)
21040 TREE_CHAIN (attributes) = NULL_TREE;
21042 /* If there is any qualification still in effect, clear it
21043 now; we will be starting fresh with the next declarator. */
21044 parser->scope = NULL_TREE;
21045 parser->qualifying_scope = NULL_TREE;
21046 parser->object_scope = NULL_TREE;
21047 /* If it's a `,', then there are more declarators. */
21048 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21050 cp_lexer_consume_token (parser->lexer);
21051 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21053 cp_token *token = cp_lexer_previous_token (parser->lexer);
21054 error_at (token->location,
21055 "stray %<,%> at end of member declaration");
21058 /* If the next token isn't a `;', then we have a parse error. */
21059 else if (cp_lexer_next_token_is_not (parser->lexer,
21060 CPP_SEMICOLON))
21062 /* The next token might be a ways away from where the
21063 actual semicolon is missing. Find the previous token
21064 and use that for our error position. */
21065 cp_token *token = cp_lexer_previous_token (parser->lexer);
21066 error_at (token->location,
21067 "expected %<;%> at end of member declaration");
21069 /* Assume that the user meant to provide a semicolon. If
21070 we were to cp_parser_skip_to_end_of_statement, we might
21071 skip to a semicolon inside a member function definition
21072 and issue nonsensical error messages. */
21073 assume_semicolon = true;
21076 if (decl)
21078 /* Add DECL to the list of members. */
21079 if (!friend_p
21080 /* Explicitly include, eg, NSDMIs, for better error
21081 recovery (c++/58650). */
21082 || !DECL_DECLARES_FUNCTION_P (decl))
21083 finish_member_declaration (decl);
21085 if (TREE_CODE (decl) == FUNCTION_DECL)
21086 cp_parser_save_default_args (parser, decl);
21087 else if (TREE_CODE (decl) == FIELD_DECL
21088 && !DECL_C_BIT_FIELD (decl)
21089 && DECL_INITIAL (decl))
21090 /* Add DECL to the queue of NSDMI to be parsed later. */
21091 vec_safe_push (unparsed_nsdmis, decl);
21094 if (assume_semicolon)
21095 goto out;
21099 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21100 out:
21101 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21104 /* Parse a pure-specifier.
21106 pure-specifier:
21109 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21110 Otherwise, ERROR_MARK_NODE is returned. */
21112 static tree
21113 cp_parser_pure_specifier (cp_parser* parser)
21115 cp_token *token;
21117 /* Look for the `=' token. */
21118 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21119 return error_mark_node;
21120 /* Look for the `0' token. */
21121 token = cp_lexer_peek_token (parser->lexer);
21123 if (token->type == CPP_EOF
21124 || token->type == CPP_PRAGMA_EOL)
21125 return error_mark_node;
21127 cp_lexer_consume_token (parser->lexer);
21129 /* Accept = default or = delete in c++0x mode. */
21130 if (token->keyword == RID_DEFAULT
21131 || token->keyword == RID_DELETE)
21133 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21134 return token->u.value;
21137 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21138 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21140 cp_parser_error (parser,
21141 "invalid pure specifier (only %<= 0%> is allowed)");
21142 cp_parser_skip_to_end_of_statement (parser);
21143 return error_mark_node;
21145 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21147 error_at (token->location, "templates may not be %<virtual%>");
21148 return error_mark_node;
21151 return integer_zero_node;
21154 /* Parse a constant-initializer.
21156 constant-initializer:
21157 = constant-expression
21159 Returns a representation of the constant-expression. */
21161 static tree
21162 cp_parser_constant_initializer (cp_parser* parser)
21164 /* Look for the `=' token. */
21165 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21166 return error_mark_node;
21168 /* It is invalid to write:
21170 struct S { static const int i = { 7 }; };
21173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21175 cp_parser_error (parser,
21176 "a brace-enclosed initializer is not allowed here");
21177 /* Consume the opening brace. */
21178 cp_lexer_consume_token (parser->lexer);
21179 /* Skip the initializer. */
21180 cp_parser_skip_to_closing_brace (parser);
21181 /* Look for the trailing `}'. */
21182 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21184 return error_mark_node;
21187 return cp_parser_constant_expression (parser);
21190 /* Derived classes [gram.class.derived] */
21192 /* Parse a base-clause.
21194 base-clause:
21195 : base-specifier-list
21197 base-specifier-list:
21198 base-specifier ... [opt]
21199 base-specifier-list , base-specifier ... [opt]
21201 Returns a TREE_LIST representing the base-classes, in the order in
21202 which they were declared. The representation of each node is as
21203 described by cp_parser_base_specifier.
21205 In the case that no bases are specified, this function will return
21206 NULL_TREE, not ERROR_MARK_NODE. */
21208 static tree
21209 cp_parser_base_clause (cp_parser* parser)
21211 tree bases = NULL_TREE;
21213 /* Look for the `:' that begins the list. */
21214 cp_parser_require (parser, CPP_COLON, RT_COLON);
21216 /* Scan the base-specifier-list. */
21217 while (true)
21219 cp_token *token;
21220 tree base;
21221 bool pack_expansion_p = false;
21223 /* Look for the base-specifier. */
21224 base = cp_parser_base_specifier (parser);
21225 /* Look for the (optional) ellipsis. */
21226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21228 /* Consume the `...'. */
21229 cp_lexer_consume_token (parser->lexer);
21231 pack_expansion_p = true;
21234 /* Add BASE to the front of the list. */
21235 if (base && base != error_mark_node)
21237 if (pack_expansion_p)
21238 /* Make this a pack expansion type. */
21239 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21241 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21243 TREE_CHAIN (base) = bases;
21244 bases = base;
21247 /* Peek at the next token. */
21248 token = cp_lexer_peek_token (parser->lexer);
21249 /* If it's not a comma, then the list is complete. */
21250 if (token->type != CPP_COMMA)
21251 break;
21252 /* Consume the `,'. */
21253 cp_lexer_consume_token (parser->lexer);
21256 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21257 base class had a qualified name. However, the next name that
21258 appears is certainly not qualified. */
21259 parser->scope = NULL_TREE;
21260 parser->qualifying_scope = NULL_TREE;
21261 parser->object_scope = NULL_TREE;
21263 return nreverse (bases);
21266 /* Parse a base-specifier.
21268 base-specifier:
21269 :: [opt] nested-name-specifier [opt] class-name
21270 virtual access-specifier [opt] :: [opt] nested-name-specifier
21271 [opt] class-name
21272 access-specifier virtual [opt] :: [opt] nested-name-specifier
21273 [opt] class-name
21275 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21276 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21277 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21278 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21280 static tree
21281 cp_parser_base_specifier (cp_parser* parser)
21283 cp_token *token;
21284 bool done = false;
21285 bool virtual_p = false;
21286 bool duplicate_virtual_error_issued_p = false;
21287 bool duplicate_access_error_issued_p = false;
21288 bool class_scope_p, template_p;
21289 tree access = access_default_node;
21290 tree type;
21292 /* Process the optional `virtual' and `access-specifier'. */
21293 while (!done)
21295 /* Peek at the next token. */
21296 token = cp_lexer_peek_token (parser->lexer);
21297 /* Process `virtual'. */
21298 switch (token->keyword)
21300 case RID_VIRTUAL:
21301 /* If `virtual' appears more than once, issue an error. */
21302 if (virtual_p && !duplicate_virtual_error_issued_p)
21304 cp_parser_error (parser,
21305 "%<virtual%> specified more than once in base-specified");
21306 duplicate_virtual_error_issued_p = true;
21309 virtual_p = true;
21311 /* Consume the `virtual' token. */
21312 cp_lexer_consume_token (parser->lexer);
21314 break;
21316 case RID_PUBLIC:
21317 case RID_PROTECTED:
21318 case RID_PRIVATE:
21319 /* If more than one access specifier appears, issue an
21320 error. */
21321 if (access != access_default_node
21322 && !duplicate_access_error_issued_p)
21324 cp_parser_error (parser,
21325 "more than one access specifier in base-specified");
21326 duplicate_access_error_issued_p = true;
21329 access = ridpointers[(int) token->keyword];
21331 /* Consume the access-specifier. */
21332 cp_lexer_consume_token (parser->lexer);
21334 break;
21336 default:
21337 done = true;
21338 break;
21341 /* It is not uncommon to see programs mechanically, erroneously, use
21342 the 'typename' keyword to denote (dependent) qualified types
21343 as base classes. */
21344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21346 token = cp_lexer_peek_token (parser->lexer);
21347 if (!processing_template_decl)
21348 error_at (token->location,
21349 "keyword %<typename%> not allowed outside of templates");
21350 else
21351 error_at (token->location,
21352 "keyword %<typename%> not allowed in this context "
21353 "(the base class is implicitly a type)");
21354 cp_lexer_consume_token (parser->lexer);
21357 /* Look for the optional `::' operator. */
21358 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21359 /* Look for the nested-name-specifier. The simplest way to
21360 implement:
21362 [temp.res]
21364 The keyword `typename' is not permitted in a base-specifier or
21365 mem-initializer; in these contexts a qualified name that
21366 depends on a template-parameter is implicitly assumed to be a
21367 type name.
21369 is to pretend that we have seen the `typename' keyword at this
21370 point. */
21371 cp_parser_nested_name_specifier_opt (parser,
21372 /*typename_keyword_p=*/true,
21373 /*check_dependency_p=*/true,
21374 typename_type,
21375 /*is_declaration=*/true);
21376 /* If the base class is given by a qualified name, assume that names
21377 we see are type names or templates, as appropriate. */
21378 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21379 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21381 if (!parser->scope
21382 && cp_lexer_next_token_is_decltype (parser->lexer))
21383 /* DR 950 allows decltype as a base-specifier. */
21384 type = cp_parser_decltype (parser);
21385 else
21387 /* Otherwise, look for the class-name. */
21388 type = cp_parser_class_name (parser,
21389 class_scope_p,
21390 template_p,
21391 typename_type,
21392 /*check_dependency_p=*/true,
21393 /*class_head_p=*/false,
21394 /*is_declaration=*/true);
21395 type = TREE_TYPE (type);
21398 if (type == error_mark_node)
21399 return error_mark_node;
21401 return finish_base_specifier (type, access, virtual_p);
21404 /* Exception handling [gram.exception] */
21406 /* Parse an (optional) noexcept-specification.
21408 noexcept-specification:
21409 noexcept ( constant-expression ) [opt]
21411 If no noexcept-specification is present, returns NULL_TREE.
21412 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21413 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21414 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21415 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21416 in which case a boolean condition is returned instead. */
21418 static tree
21419 cp_parser_noexcept_specification_opt (cp_parser* parser,
21420 bool require_constexpr,
21421 bool* consumed_expr,
21422 bool return_cond)
21424 cp_token *token;
21425 const char *saved_message;
21427 /* Peek at the next token. */
21428 token = cp_lexer_peek_token (parser->lexer);
21430 /* Is it a noexcept-specification? */
21431 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21433 tree expr;
21434 cp_lexer_consume_token (parser->lexer);
21436 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21438 cp_lexer_consume_token (parser->lexer);
21440 if (require_constexpr)
21442 /* Types may not be defined in an exception-specification. */
21443 saved_message = parser->type_definition_forbidden_message;
21444 parser->type_definition_forbidden_message
21445 = G_("types may not be defined in an exception-specification");
21447 expr = cp_parser_constant_expression (parser);
21449 /* Restore the saved message. */
21450 parser->type_definition_forbidden_message = saved_message;
21452 else
21454 expr = cp_parser_expression (parser);
21455 *consumed_expr = true;
21458 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21460 else
21462 expr = boolean_true_node;
21463 if (!require_constexpr)
21464 *consumed_expr = false;
21467 /* We cannot build a noexcept-spec right away because this will check
21468 that expr is a constexpr. */
21469 if (!return_cond)
21470 return build_noexcept_spec (expr, tf_warning_or_error);
21471 else
21472 return expr;
21474 else
21475 return NULL_TREE;
21478 /* Parse an (optional) exception-specification.
21480 exception-specification:
21481 throw ( type-id-list [opt] )
21483 Returns a TREE_LIST representing the exception-specification. The
21484 TREE_VALUE of each node is a type. */
21486 static tree
21487 cp_parser_exception_specification_opt (cp_parser* parser)
21489 cp_token *token;
21490 tree type_id_list;
21491 const char *saved_message;
21493 /* Peek at the next token. */
21494 token = cp_lexer_peek_token (parser->lexer);
21496 /* Is it a noexcept-specification? */
21497 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21498 false);
21499 if (type_id_list != NULL_TREE)
21500 return type_id_list;
21502 /* If it's not `throw', then there's no exception-specification. */
21503 if (!cp_parser_is_keyword (token, RID_THROW))
21504 return NULL_TREE;
21506 #if 0
21507 /* Enable this once a lot of code has transitioned to noexcept? */
21508 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21509 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21510 "deprecated in C++0x; use %<noexcept%> instead");
21511 #endif
21513 /* Consume the `throw'. */
21514 cp_lexer_consume_token (parser->lexer);
21516 /* Look for the `('. */
21517 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21519 /* Peek at the next token. */
21520 token = cp_lexer_peek_token (parser->lexer);
21521 /* If it's not a `)', then there is a type-id-list. */
21522 if (token->type != CPP_CLOSE_PAREN)
21524 /* Types may not be defined in an exception-specification. */
21525 saved_message = parser->type_definition_forbidden_message;
21526 parser->type_definition_forbidden_message
21527 = G_("types may not be defined in an exception-specification");
21528 /* Parse the type-id-list. */
21529 type_id_list = cp_parser_type_id_list (parser);
21530 /* Restore the saved message. */
21531 parser->type_definition_forbidden_message = saved_message;
21533 else
21534 type_id_list = empty_except_spec;
21536 /* Look for the `)'. */
21537 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21539 return type_id_list;
21542 /* Parse an (optional) type-id-list.
21544 type-id-list:
21545 type-id ... [opt]
21546 type-id-list , type-id ... [opt]
21548 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21549 in the order that the types were presented. */
21551 static tree
21552 cp_parser_type_id_list (cp_parser* parser)
21554 tree types = NULL_TREE;
21556 while (true)
21558 cp_token *token;
21559 tree type;
21561 /* Get the next type-id. */
21562 type = cp_parser_type_id (parser);
21563 /* Parse the optional ellipsis. */
21564 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21566 /* Consume the `...'. */
21567 cp_lexer_consume_token (parser->lexer);
21569 /* Turn the type into a pack expansion expression. */
21570 type = make_pack_expansion (type);
21572 /* Add it to the list. */
21573 types = add_exception_specifier (types, type, /*complain=*/1);
21574 /* Peek at the next token. */
21575 token = cp_lexer_peek_token (parser->lexer);
21576 /* If it is not a `,', we are done. */
21577 if (token->type != CPP_COMMA)
21578 break;
21579 /* Consume the `,'. */
21580 cp_lexer_consume_token (parser->lexer);
21583 return nreverse (types);
21586 /* Parse a try-block.
21588 try-block:
21589 try compound-statement handler-seq */
21591 static tree
21592 cp_parser_try_block (cp_parser* parser)
21594 tree try_block;
21596 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21597 if (parser->in_function_body
21598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21599 error ("%<try%> in %<constexpr%> function");
21601 try_block = begin_try_block ();
21602 cp_parser_compound_statement (parser, NULL, true, false);
21603 finish_try_block (try_block);
21604 cp_parser_handler_seq (parser);
21605 finish_handler_sequence (try_block);
21607 return try_block;
21610 /* Parse a function-try-block.
21612 function-try-block:
21613 try ctor-initializer [opt] function-body handler-seq */
21615 static bool
21616 cp_parser_function_try_block (cp_parser* parser)
21618 tree compound_stmt;
21619 tree try_block;
21620 bool ctor_initializer_p;
21622 /* Look for the `try' keyword. */
21623 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21624 return false;
21625 /* Let the rest of the front end know where we are. */
21626 try_block = begin_function_try_block (&compound_stmt);
21627 /* Parse the function-body. */
21628 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21629 (parser, /*in_function_try_block=*/true);
21630 /* We're done with the `try' part. */
21631 finish_function_try_block (try_block);
21632 /* Parse the handlers. */
21633 cp_parser_handler_seq (parser);
21634 /* We're done with the handlers. */
21635 finish_function_handler_sequence (try_block, compound_stmt);
21637 return ctor_initializer_p;
21640 /* Parse a handler-seq.
21642 handler-seq:
21643 handler handler-seq [opt] */
21645 static void
21646 cp_parser_handler_seq (cp_parser* parser)
21648 while (true)
21650 cp_token *token;
21652 /* Parse the handler. */
21653 cp_parser_handler (parser);
21654 /* Peek at the next token. */
21655 token = cp_lexer_peek_token (parser->lexer);
21656 /* If it's not `catch' then there are no more handlers. */
21657 if (!cp_parser_is_keyword (token, RID_CATCH))
21658 break;
21662 /* Parse a handler.
21664 handler:
21665 catch ( exception-declaration ) compound-statement */
21667 static void
21668 cp_parser_handler (cp_parser* parser)
21670 tree handler;
21671 tree declaration;
21673 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21674 handler = begin_handler ();
21675 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21676 declaration = cp_parser_exception_declaration (parser);
21677 finish_handler_parms (declaration, handler);
21678 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21679 cp_parser_compound_statement (parser, NULL, false, false);
21680 finish_handler (handler);
21683 /* Parse an exception-declaration.
21685 exception-declaration:
21686 type-specifier-seq declarator
21687 type-specifier-seq abstract-declarator
21688 type-specifier-seq
21691 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21692 ellipsis variant is used. */
21694 static tree
21695 cp_parser_exception_declaration (cp_parser* parser)
21697 cp_decl_specifier_seq type_specifiers;
21698 cp_declarator *declarator;
21699 const char *saved_message;
21701 /* If it's an ellipsis, it's easy to handle. */
21702 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21704 /* Consume the `...' token. */
21705 cp_lexer_consume_token (parser->lexer);
21706 return NULL_TREE;
21709 /* Types may not be defined in exception-declarations. */
21710 saved_message = parser->type_definition_forbidden_message;
21711 parser->type_definition_forbidden_message
21712 = G_("types may not be defined in exception-declarations");
21714 /* Parse the type-specifier-seq. */
21715 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21716 /*is_trailing_return=*/false,
21717 &type_specifiers);
21718 /* If it's a `)', then there is no declarator. */
21719 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21720 declarator = NULL;
21721 else
21722 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21723 /*ctor_dtor_or_conv_p=*/NULL,
21724 /*parenthesized_p=*/NULL,
21725 /*member_p=*/false,
21726 /*friend_p=*/false);
21728 /* Restore the saved message. */
21729 parser->type_definition_forbidden_message = saved_message;
21731 if (!type_specifiers.any_specifiers_p)
21732 return error_mark_node;
21734 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21737 /* Parse a throw-expression.
21739 throw-expression:
21740 throw assignment-expression [opt]
21742 Returns a THROW_EXPR representing the throw-expression. */
21744 static tree
21745 cp_parser_throw_expression (cp_parser* parser)
21747 tree expression;
21748 cp_token* token;
21750 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21751 token = cp_lexer_peek_token (parser->lexer);
21752 /* Figure out whether or not there is an assignment-expression
21753 following the "throw" keyword. */
21754 if (token->type == CPP_COMMA
21755 || token->type == CPP_SEMICOLON
21756 || token->type == CPP_CLOSE_PAREN
21757 || token->type == CPP_CLOSE_SQUARE
21758 || token->type == CPP_CLOSE_BRACE
21759 || token->type == CPP_COLON)
21760 expression = NULL_TREE;
21761 else
21762 expression = cp_parser_assignment_expression (parser);
21764 return build_throw (expression);
21767 /* GNU Extensions */
21769 /* Parse an (optional) asm-specification.
21771 asm-specification:
21772 asm ( string-literal )
21774 If the asm-specification is present, returns a STRING_CST
21775 corresponding to the string-literal. Otherwise, returns
21776 NULL_TREE. */
21778 static tree
21779 cp_parser_asm_specification_opt (cp_parser* parser)
21781 cp_token *token;
21782 tree asm_specification;
21784 /* Peek at the next token. */
21785 token = cp_lexer_peek_token (parser->lexer);
21786 /* If the next token isn't the `asm' keyword, then there's no
21787 asm-specification. */
21788 if (!cp_parser_is_keyword (token, RID_ASM))
21789 return NULL_TREE;
21791 /* Consume the `asm' token. */
21792 cp_lexer_consume_token (parser->lexer);
21793 /* Look for the `('. */
21794 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21796 /* Look for the string-literal. */
21797 asm_specification = cp_parser_string_literal (parser, false, false);
21799 /* Look for the `)'. */
21800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21802 return asm_specification;
21805 /* Parse an asm-operand-list.
21807 asm-operand-list:
21808 asm-operand
21809 asm-operand-list , asm-operand
21811 asm-operand:
21812 string-literal ( expression )
21813 [ string-literal ] string-literal ( expression )
21815 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21816 each node is the expression. The TREE_PURPOSE is itself a
21817 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21818 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21819 is a STRING_CST for the string literal before the parenthesis. Returns
21820 ERROR_MARK_NODE if any of the operands are invalid. */
21822 static tree
21823 cp_parser_asm_operand_list (cp_parser* parser)
21825 tree asm_operands = NULL_TREE;
21826 bool invalid_operands = false;
21828 while (true)
21830 tree string_literal;
21831 tree expression;
21832 tree name;
21834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21836 /* Consume the `[' token. */
21837 cp_lexer_consume_token (parser->lexer);
21838 /* Read the operand name. */
21839 name = cp_parser_identifier (parser);
21840 if (name != error_mark_node)
21841 name = build_string (IDENTIFIER_LENGTH (name),
21842 IDENTIFIER_POINTER (name));
21843 /* Look for the closing `]'. */
21844 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21846 else
21847 name = NULL_TREE;
21848 /* Look for the string-literal. */
21849 string_literal = cp_parser_string_literal (parser, false, false);
21851 /* Look for the `('. */
21852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21853 /* Parse the expression. */
21854 expression = cp_parser_expression (parser);
21855 /* Look for the `)'. */
21856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21858 if (name == error_mark_node
21859 || string_literal == error_mark_node
21860 || expression == error_mark_node)
21861 invalid_operands = true;
21863 /* Add this operand to the list. */
21864 asm_operands = tree_cons (build_tree_list (name, string_literal),
21865 expression,
21866 asm_operands);
21867 /* If the next token is not a `,', there are no more
21868 operands. */
21869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21870 break;
21871 /* Consume the `,'. */
21872 cp_lexer_consume_token (parser->lexer);
21875 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21878 /* Parse an asm-clobber-list.
21880 asm-clobber-list:
21881 string-literal
21882 asm-clobber-list , string-literal
21884 Returns a TREE_LIST, indicating the clobbers in the order that they
21885 appeared. The TREE_VALUE of each node is a STRING_CST. */
21887 static tree
21888 cp_parser_asm_clobber_list (cp_parser* parser)
21890 tree clobbers = NULL_TREE;
21892 while (true)
21894 tree string_literal;
21896 /* Look for the string literal. */
21897 string_literal = cp_parser_string_literal (parser, false, false);
21898 /* Add it to the list. */
21899 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21900 /* If the next token is not a `,', then the list is
21901 complete. */
21902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21903 break;
21904 /* Consume the `,' token. */
21905 cp_lexer_consume_token (parser->lexer);
21908 return clobbers;
21911 /* Parse an asm-label-list.
21913 asm-label-list:
21914 identifier
21915 asm-label-list , identifier
21917 Returns a TREE_LIST, indicating the labels in the order that they
21918 appeared. The TREE_VALUE of each node is a label. */
21920 static tree
21921 cp_parser_asm_label_list (cp_parser* parser)
21923 tree labels = NULL_TREE;
21925 while (true)
21927 tree identifier, label, name;
21929 /* Look for the identifier. */
21930 identifier = cp_parser_identifier (parser);
21931 if (!error_operand_p (identifier))
21933 label = lookup_label (identifier);
21934 if (TREE_CODE (label) == LABEL_DECL)
21936 TREE_USED (label) = 1;
21937 check_goto (label);
21938 name = build_string (IDENTIFIER_LENGTH (identifier),
21939 IDENTIFIER_POINTER (identifier));
21940 labels = tree_cons (name, label, labels);
21943 /* If the next token is not a `,', then the list is
21944 complete. */
21945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21946 break;
21947 /* Consume the `,' token. */
21948 cp_lexer_consume_token (parser->lexer);
21951 return nreverse (labels);
21954 /* Return TRUE iff the next tokens in the stream are possibly the
21955 beginning of a GNU extension attribute. */
21957 static bool
21958 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21960 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21963 /* Return TRUE iff the next tokens in the stream are possibly the
21964 beginning of a standard C++-11 attribute specifier. */
21966 static bool
21967 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21969 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21972 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21973 beginning of a standard C++-11 attribute specifier. */
21975 static bool
21976 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21978 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21980 return (cxx_dialect >= cxx11
21981 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21982 || (token->type == CPP_OPEN_SQUARE
21983 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21984 && token->type == CPP_OPEN_SQUARE)));
21987 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21988 beginning of a GNU extension attribute. */
21990 static bool
21991 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21993 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21995 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21998 /* Return true iff the next tokens can be the beginning of either a
21999 GNU attribute list, or a standard C++11 attribute sequence. */
22001 static bool
22002 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22004 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22005 || cp_next_tokens_can_be_std_attribute_p (parser));
22008 /* Return true iff the next Nth tokens can be the beginning of either
22009 a GNU attribute list, or a standard C++11 attribute sequence. */
22011 static bool
22012 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22014 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22015 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22018 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22019 of GNU attributes, or return NULL. */
22021 static tree
22022 cp_parser_attributes_opt (cp_parser *parser)
22024 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22025 return cp_parser_gnu_attributes_opt (parser);
22026 return cp_parser_std_attribute_spec_seq (parser);
22029 #define CILK_SIMD_FN_CLAUSE_MASK \
22030 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22031 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22032 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22033 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22034 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22036 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22037 vector [(<clauses>)] */
22039 static void
22040 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22042 bool first_p = parser->cilk_simd_fn_info == NULL;
22043 cp_token *token = v_token;
22044 if (first_p)
22046 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22047 parser->cilk_simd_fn_info->error_seen = false;
22048 parser->cilk_simd_fn_info->fndecl_seen = false;
22049 parser->cilk_simd_fn_info->tokens = vNULL;
22051 int paren_scope = 0;
22052 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22054 cp_lexer_consume_token (parser->lexer);
22055 v_token = cp_lexer_peek_token (parser->lexer);
22056 paren_scope++;
22058 while (paren_scope > 0)
22060 token = cp_lexer_peek_token (parser->lexer);
22061 if (token->type == CPP_OPEN_PAREN)
22062 paren_scope++;
22063 else if (token->type == CPP_CLOSE_PAREN)
22064 paren_scope--;
22065 /* Do not push the last ')' */
22066 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22067 cp_lexer_consume_token (parser->lexer);
22070 token->type = CPP_PRAGMA_EOL;
22071 parser->lexer->next_token = token;
22072 cp_lexer_consume_token (parser->lexer);
22074 struct cp_token_cache *cp
22075 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22076 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22079 /* Parse an (optional) series of attributes.
22081 attributes:
22082 attributes attribute
22084 attribute:
22085 __attribute__ (( attribute-list [opt] ))
22087 The return value is as for cp_parser_gnu_attribute_list. */
22089 static tree
22090 cp_parser_gnu_attributes_opt (cp_parser* parser)
22092 tree attributes = NULL_TREE;
22094 while (true)
22096 cp_token *token;
22097 tree attribute_list;
22098 bool ok = true;
22100 /* Peek at the next token. */
22101 token = cp_lexer_peek_token (parser->lexer);
22102 /* If it's not `__attribute__', then we're done. */
22103 if (token->keyword != RID_ATTRIBUTE)
22104 break;
22106 /* Consume the `__attribute__' keyword. */
22107 cp_lexer_consume_token (parser->lexer);
22108 /* Look for the two `(' tokens. */
22109 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22110 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22112 /* Peek at the next token. */
22113 token = cp_lexer_peek_token (parser->lexer);
22114 if (token->type != CPP_CLOSE_PAREN)
22115 /* Parse the attribute-list. */
22116 attribute_list = cp_parser_gnu_attribute_list (parser);
22117 else
22118 /* If the next token is a `)', then there is no attribute
22119 list. */
22120 attribute_list = NULL;
22122 /* Look for the two `)' tokens. */
22123 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22124 ok = false;
22125 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22126 ok = false;
22127 if (!ok)
22128 cp_parser_skip_to_end_of_statement (parser);
22130 /* Add these new attributes to the list. */
22131 attributes = chainon (attributes, attribute_list);
22134 return attributes;
22137 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22138 "__vector" or "__vector__." */
22140 static inline bool
22141 is_cilkplus_vector_p (tree name)
22143 if (flag_cilkplus && is_attribute_p ("vector", name))
22144 return true;
22145 return false;
22148 /* Parse a GNU attribute-list.
22150 attribute-list:
22151 attribute
22152 attribute-list , attribute
22154 attribute:
22155 identifier
22156 identifier ( identifier )
22157 identifier ( identifier , expression-list )
22158 identifier ( expression-list )
22160 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22161 to an attribute. The TREE_PURPOSE of each node is the identifier
22162 indicating which attribute is in use. The TREE_VALUE represents
22163 the arguments, if any. */
22165 static tree
22166 cp_parser_gnu_attribute_list (cp_parser* parser)
22168 tree attribute_list = NULL_TREE;
22169 bool save_translate_strings_p = parser->translate_strings_p;
22171 parser->translate_strings_p = false;
22172 while (true)
22174 cp_token *token;
22175 tree identifier;
22176 tree attribute;
22178 /* Look for the identifier. We also allow keywords here; for
22179 example `__attribute__ ((const))' is legal. */
22180 token = cp_lexer_peek_token (parser->lexer);
22181 if (token->type == CPP_NAME
22182 || token->type == CPP_KEYWORD)
22184 tree arguments = NULL_TREE;
22186 /* Consume the token, but save it since we need it for the
22187 SIMD enabled function parsing. */
22188 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22190 /* Save away the identifier that indicates which attribute
22191 this is. */
22192 identifier = (token->type == CPP_KEYWORD)
22193 /* For keywords, use the canonical spelling, not the
22194 parsed identifier. */
22195 ? ridpointers[(int) token->keyword]
22196 : id_token->u.value;
22198 attribute = build_tree_list (identifier, NULL_TREE);
22200 /* Peek at the next token. */
22201 token = cp_lexer_peek_token (parser->lexer);
22202 /* If it's an `(', then parse the attribute arguments. */
22203 if (token->type == CPP_OPEN_PAREN)
22205 vec<tree, va_gc> *vec;
22206 int attr_flag = (attribute_takes_identifier_p (identifier)
22207 ? id_attr : normal_attr);
22208 if (is_cilkplus_vector_p (identifier))
22210 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22211 continue;
22213 else
22214 vec = cp_parser_parenthesized_expression_list
22215 (parser, attr_flag, /*cast_p=*/false,
22216 /*allow_expansion_p=*/false,
22217 /*non_constant_p=*/NULL);
22218 if (vec == NULL)
22219 arguments = error_mark_node;
22220 else
22222 arguments = build_tree_list_vec (vec);
22223 release_tree_vector (vec);
22225 /* Save the arguments away. */
22226 TREE_VALUE (attribute) = arguments;
22228 else if (is_cilkplus_vector_p (identifier))
22230 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22231 continue;
22234 if (arguments != error_mark_node)
22236 /* Add this attribute to the list. */
22237 TREE_CHAIN (attribute) = attribute_list;
22238 attribute_list = attribute;
22241 token = cp_lexer_peek_token (parser->lexer);
22243 /* Now, look for more attributes. If the next token isn't a
22244 `,', we're done. */
22245 if (token->type != CPP_COMMA)
22246 break;
22248 /* Consume the comma and keep going. */
22249 cp_lexer_consume_token (parser->lexer);
22251 parser->translate_strings_p = save_translate_strings_p;
22253 /* We built up the list in reverse order. */
22254 return nreverse (attribute_list);
22257 /* Parse a standard C++11 attribute.
22259 The returned representation is a TREE_LIST which TREE_PURPOSE is
22260 the scoped name of the attribute, and the TREE_VALUE is its
22261 arguments list.
22263 Note that the scoped name of the attribute is itself a TREE_LIST
22264 which TREE_PURPOSE is the namespace of the attribute, and
22265 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22266 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22267 and which TREE_PURPOSE is directly the attribute name.
22269 Clients of the attribute code should use get_attribute_namespace
22270 and get_attribute_name to get the actual namespace and name of
22271 attributes, regardless of their being GNU or C++11 attributes.
22273 attribute:
22274 attribute-token attribute-argument-clause [opt]
22276 attribute-token:
22277 identifier
22278 attribute-scoped-token
22280 attribute-scoped-token:
22281 attribute-namespace :: identifier
22283 attribute-namespace:
22284 identifier
22286 attribute-argument-clause:
22287 ( balanced-token-seq )
22289 balanced-token-seq:
22290 balanced-token [opt]
22291 balanced-token-seq balanced-token
22293 balanced-token:
22294 ( balanced-token-seq )
22295 [ balanced-token-seq ]
22296 { balanced-token-seq }. */
22298 static tree
22299 cp_parser_std_attribute (cp_parser *parser)
22301 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22302 cp_token *token;
22304 /* First, parse name of the the attribute, a.k.a
22305 attribute-token. */
22307 token = cp_lexer_peek_token (parser->lexer);
22308 if (token->type == CPP_NAME)
22309 attr_id = token->u.value;
22310 else if (token->type == CPP_KEYWORD)
22311 attr_id = ridpointers[(int) token->keyword];
22312 else if (token->flags & NAMED_OP)
22313 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22315 if (attr_id == NULL_TREE)
22316 return NULL_TREE;
22318 cp_lexer_consume_token (parser->lexer);
22320 token = cp_lexer_peek_token (parser->lexer);
22321 if (token->type == CPP_SCOPE)
22323 /* We are seeing a scoped attribute token. */
22325 cp_lexer_consume_token (parser->lexer);
22326 attr_ns = attr_id;
22328 token = cp_lexer_consume_token (parser->lexer);
22329 if (token->type == CPP_NAME)
22330 attr_id = token->u.value;
22331 else if (token->type == CPP_KEYWORD)
22332 attr_id = ridpointers[(int) token->keyword];
22333 else
22335 error_at (token->location,
22336 "expected an identifier for the attribute name");
22337 return error_mark_node;
22339 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22340 NULL_TREE);
22341 token = cp_lexer_peek_token (parser->lexer);
22343 else
22345 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22346 NULL_TREE);
22347 /* C++11 noreturn attribute is equivalent to GNU's. */
22348 if (is_attribute_p ("noreturn", attr_id))
22349 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22350 /* C++14 deprecated attribute is equivalent to GNU's. */
22351 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22353 if (cxx_dialect == cxx11)
22354 pedwarn (token->location, OPT_Wpedantic,
22355 "%<deprecated%> is a C++14 feature;"
22356 " use %<gnu::deprecated%>");
22357 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22361 /* Now parse the optional argument clause of the attribute. */
22363 if (token->type != CPP_OPEN_PAREN)
22364 return attribute;
22367 vec<tree, va_gc> *vec;
22368 int attr_flag = normal_attr;
22370 if (attr_ns == get_identifier ("gnu")
22371 && attribute_takes_identifier_p (attr_id))
22372 /* A GNU attribute that takes an identifier in parameter. */
22373 attr_flag = id_attr;
22375 vec = cp_parser_parenthesized_expression_list
22376 (parser, attr_flag, /*cast_p=*/false,
22377 /*allow_expansion_p=*/true,
22378 /*non_constant_p=*/NULL);
22379 if (vec == NULL)
22380 arguments = error_mark_node;
22381 else
22383 arguments = build_tree_list_vec (vec);
22384 release_tree_vector (vec);
22387 if (arguments == error_mark_node)
22388 attribute = error_mark_node;
22389 else
22390 TREE_VALUE (attribute) = arguments;
22393 return attribute;
22396 /* Parse a list of standard C++-11 attributes.
22398 attribute-list:
22399 attribute [opt]
22400 attribute-list , attribute[opt]
22401 attribute ...
22402 attribute-list , attribute ...
22405 static tree
22406 cp_parser_std_attribute_list (cp_parser *parser)
22408 tree attributes = NULL_TREE, attribute = NULL_TREE;
22409 cp_token *token = NULL;
22411 while (true)
22413 attribute = cp_parser_std_attribute (parser);
22414 if (attribute == error_mark_node)
22415 break;
22416 if (attribute != NULL_TREE)
22418 TREE_CHAIN (attribute) = attributes;
22419 attributes = attribute;
22421 token = cp_lexer_peek_token (parser->lexer);
22422 if (token->type != CPP_COMMA)
22423 break;
22424 cp_lexer_consume_token (parser->lexer);
22426 attributes = nreverse (attributes);
22427 return attributes;
22430 /* Parse a standard C++-11 attribute specifier.
22432 attribute-specifier:
22433 [ [ attribute-list ] ]
22434 alignment-specifier
22436 alignment-specifier:
22437 alignas ( type-id ... [opt] )
22438 alignas ( alignment-expression ... [opt] ). */
22440 static tree
22441 cp_parser_std_attribute_spec (cp_parser *parser)
22443 tree attributes = NULL_TREE;
22444 cp_token *token = cp_lexer_peek_token (parser->lexer);
22446 if (token->type == CPP_OPEN_SQUARE
22447 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22449 cp_lexer_consume_token (parser->lexer);
22450 cp_lexer_consume_token (parser->lexer);
22452 attributes = cp_parser_std_attribute_list (parser);
22454 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22455 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22456 cp_parser_skip_to_end_of_statement (parser);
22457 else
22458 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22459 when we are sure that we have actually parsed them. */
22460 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22462 else
22464 tree alignas_expr;
22466 /* Look for an alignment-specifier. */
22468 token = cp_lexer_peek_token (parser->lexer);
22470 if (token->type != CPP_KEYWORD
22471 || token->keyword != RID_ALIGNAS)
22472 return NULL_TREE;
22474 cp_lexer_consume_token (parser->lexer);
22475 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22477 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22479 cp_parser_error (parser, "expected %<(%>");
22480 return error_mark_node;
22483 cp_parser_parse_tentatively (parser);
22484 alignas_expr = cp_parser_type_id (parser);
22486 if (!cp_parser_parse_definitely (parser))
22488 gcc_assert (alignas_expr == error_mark_node
22489 || alignas_expr == NULL_TREE);
22491 alignas_expr =
22492 cp_parser_assignment_expression (parser);
22493 if (alignas_expr == error_mark_node)
22494 cp_parser_skip_to_end_of_statement (parser);
22495 if (alignas_expr == NULL_TREE
22496 || alignas_expr == error_mark_node)
22497 return alignas_expr;
22500 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22502 cp_parser_error (parser, "expected %<)%>");
22503 return error_mark_node;
22506 alignas_expr = cxx_alignas_expr (alignas_expr);
22508 /* Build the C++-11 representation of an 'aligned'
22509 attribute. */
22510 attributes =
22511 build_tree_list (build_tree_list (get_identifier ("gnu"),
22512 get_identifier ("aligned")),
22513 build_tree_list (NULL_TREE, alignas_expr));
22516 return attributes;
22519 /* Parse a standard C++-11 attribute-specifier-seq.
22521 attribute-specifier-seq:
22522 attribute-specifier-seq [opt] attribute-specifier
22525 static tree
22526 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22528 tree attr_specs = NULL;
22530 while (true)
22532 tree attr_spec = cp_parser_std_attribute_spec (parser);
22533 if (attr_spec == NULL_TREE)
22534 break;
22535 if (attr_spec == error_mark_node)
22536 return error_mark_node;
22538 TREE_CHAIN (attr_spec) = attr_specs;
22539 attr_specs = attr_spec;
22542 attr_specs = nreverse (attr_specs);
22543 return attr_specs;
22546 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22547 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22548 current value of the PEDANTIC flag, regardless of whether or not
22549 the `__extension__' keyword is present. The caller is responsible
22550 for restoring the value of the PEDANTIC flag. */
22552 static bool
22553 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22555 /* Save the old value of the PEDANTIC flag. */
22556 *saved_pedantic = pedantic;
22558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22560 /* Consume the `__extension__' token. */
22561 cp_lexer_consume_token (parser->lexer);
22562 /* We're not being pedantic while the `__extension__' keyword is
22563 in effect. */
22564 pedantic = 0;
22566 return true;
22569 return false;
22572 /* Parse a label declaration.
22574 label-declaration:
22575 __label__ label-declarator-seq ;
22577 label-declarator-seq:
22578 identifier , label-declarator-seq
22579 identifier */
22581 static void
22582 cp_parser_label_declaration (cp_parser* parser)
22584 /* Look for the `__label__' keyword. */
22585 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22587 while (true)
22589 tree identifier;
22591 /* Look for an identifier. */
22592 identifier = cp_parser_identifier (parser);
22593 /* If we failed, stop. */
22594 if (identifier == error_mark_node)
22595 break;
22596 /* Declare it as a label. */
22597 finish_label_decl (identifier);
22598 /* If the next token is a `;', stop. */
22599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22600 break;
22601 /* Look for the `,' separating the label declarations. */
22602 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22605 /* Look for the final `;'. */
22606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22609 /* Support Functions */
22611 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22612 NAME should have one of the representations used for an
22613 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22614 is returned. If PARSER->SCOPE is a dependent type, then a
22615 SCOPE_REF is returned.
22617 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22618 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22619 was formed. Abstractly, such entities should not be passed to this
22620 function, because they do not need to be looked up, but it is
22621 simpler to check for this special case here, rather than at the
22622 call-sites.
22624 In cases not explicitly covered above, this function returns a
22625 DECL, OVERLOAD, or baselink representing the result of the lookup.
22626 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22627 is returned.
22629 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22630 (e.g., "struct") that was used. In that case bindings that do not
22631 refer to types are ignored.
22633 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22634 ignored.
22636 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22637 are ignored.
22639 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22640 types.
22642 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22643 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22644 NULL_TREE otherwise. */
22646 static tree
22647 cp_parser_lookup_name (cp_parser *parser, tree name,
22648 enum tag_types tag_type,
22649 bool is_template,
22650 bool is_namespace,
22651 bool check_dependency,
22652 tree *ambiguous_decls,
22653 location_t name_location)
22655 tree decl;
22656 tree object_type = parser->context->object_type;
22658 /* Assume that the lookup will be unambiguous. */
22659 if (ambiguous_decls)
22660 *ambiguous_decls = NULL_TREE;
22662 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22663 no longer valid. Note that if we are parsing tentatively, and
22664 the parse fails, OBJECT_TYPE will be automatically restored. */
22665 parser->context->object_type = NULL_TREE;
22667 if (name == error_mark_node)
22668 return error_mark_node;
22670 /* A template-id has already been resolved; there is no lookup to
22671 do. */
22672 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22673 return name;
22674 if (BASELINK_P (name))
22676 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22677 == TEMPLATE_ID_EXPR);
22678 return name;
22681 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22682 it should already have been checked to make sure that the name
22683 used matches the type being destroyed. */
22684 if (TREE_CODE (name) == BIT_NOT_EXPR)
22686 tree type;
22688 /* Figure out to which type this destructor applies. */
22689 if (parser->scope)
22690 type = parser->scope;
22691 else if (object_type)
22692 type = object_type;
22693 else
22694 type = current_class_type;
22695 /* If that's not a class type, there is no destructor. */
22696 if (!type || !CLASS_TYPE_P (type))
22697 return error_mark_node;
22698 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22699 lazily_declare_fn (sfk_destructor, type);
22700 if (!CLASSTYPE_DESTRUCTORS (type))
22701 return error_mark_node;
22702 /* If it was a class type, return the destructor. */
22703 return CLASSTYPE_DESTRUCTORS (type);
22706 /* By this point, the NAME should be an ordinary identifier. If
22707 the id-expression was a qualified name, the qualifying scope is
22708 stored in PARSER->SCOPE at this point. */
22709 gcc_assert (identifier_p (name));
22711 /* Perform the lookup. */
22712 if (parser->scope)
22714 bool dependent_p;
22716 if (parser->scope == error_mark_node)
22717 return error_mark_node;
22719 /* If the SCOPE is dependent, the lookup must be deferred until
22720 the template is instantiated -- unless we are explicitly
22721 looking up names in uninstantiated templates. Even then, we
22722 cannot look up the name if the scope is not a class type; it
22723 might, for example, be a template type parameter. */
22724 dependent_p = (TYPE_P (parser->scope)
22725 && dependent_scope_p (parser->scope));
22726 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22727 && dependent_p)
22728 /* Defer lookup. */
22729 decl = error_mark_node;
22730 else
22732 tree pushed_scope = NULL_TREE;
22734 /* If PARSER->SCOPE is a dependent type, then it must be a
22735 class type, and we must not be checking dependencies;
22736 otherwise, we would have processed this lookup above. So
22737 that PARSER->SCOPE is not considered a dependent base by
22738 lookup_member, we must enter the scope here. */
22739 if (dependent_p)
22740 pushed_scope = push_scope (parser->scope);
22742 /* If the PARSER->SCOPE is a template specialization, it
22743 may be instantiated during name lookup. In that case,
22744 errors may be issued. Even if we rollback the current
22745 tentative parse, those errors are valid. */
22746 decl = lookup_qualified_name (parser->scope, name,
22747 tag_type != none_type,
22748 /*complain=*/true);
22750 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22751 lookup result and the nested-name-specifier nominates a class C:
22752 * if the name specified after the nested-name-specifier, when
22753 looked up in C, is the injected-class-name of C (Clause 9), or
22754 * if the name specified after the nested-name-specifier is the
22755 same as the identifier or the simple-template-id's template-
22756 name in the last component of the nested-name-specifier,
22757 the name is instead considered to name the constructor of
22758 class C. [ Note: for example, the constructor is not an
22759 acceptable lookup result in an elaborated-type-specifier so
22760 the constructor would not be used in place of the
22761 injected-class-name. --end note ] Such a constructor name
22762 shall be used only in the declarator-id of a declaration that
22763 names a constructor or in a using-declaration. */
22764 if (tag_type == none_type
22765 && DECL_SELF_REFERENCE_P (decl)
22766 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22767 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22768 tag_type != none_type,
22769 /*complain=*/true);
22771 /* If we have a single function from a using decl, pull it out. */
22772 if (TREE_CODE (decl) == OVERLOAD
22773 && !really_overloaded_fn (decl))
22774 decl = OVL_FUNCTION (decl);
22776 if (pushed_scope)
22777 pop_scope (pushed_scope);
22780 /* If the scope is a dependent type and either we deferred lookup or
22781 we did lookup but didn't find the name, rememeber the name. */
22782 if (decl == error_mark_node && TYPE_P (parser->scope)
22783 && dependent_type_p (parser->scope))
22785 if (tag_type)
22787 tree type;
22789 /* The resolution to Core Issue 180 says that `struct
22790 A::B' should be considered a type-name, even if `A'
22791 is dependent. */
22792 type = make_typename_type (parser->scope, name, tag_type,
22793 /*complain=*/tf_error);
22794 if (type != error_mark_node)
22795 decl = TYPE_NAME (type);
22797 else if (is_template
22798 && (cp_parser_next_token_ends_template_argument_p (parser)
22799 || cp_lexer_next_token_is (parser->lexer,
22800 CPP_CLOSE_PAREN)))
22801 decl = make_unbound_class_template (parser->scope,
22802 name, NULL_TREE,
22803 /*complain=*/tf_error);
22804 else
22805 decl = build_qualified_name (/*type=*/NULL_TREE,
22806 parser->scope, name,
22807 is_template);
22809 parser->qualifying_scope = parser->scope;
22810 parser->object_scope = NULL_TREE;
22812 else if (object_type)
22814 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22815 OBJECT_TYPE is not a class. */
22816 if (CLASS_TYPE_P (object_type))
22817 /* If the OBJECT_TYPE is a template specialization, it may
22818 be instantiated during name lookup. In that case, errors
22819 may be issued. Even if we rollback the current tentative
22820 parse, those errors are valid. */
22821 decl = lookup_member (object_type,
22822 name,
22823 /*protect=*/0,
22824 tag_type != none_type,
22825 tf_warning_or_error);
22826 else
22827 decl = NULL_TREE;
22829 if (!decl)
22830 /* Look it up in the enclosing context. */
22831 decl = lookup_name_real (name, tag_type != none_type,
22832 /*nonclass=*/0,
22833 /*block_p=*/true, is_namespace, 0);
22834 parser->object_scope = object_type;
22835 parser->qualifying_scope = NULL_TREE;
22837 else
22839 decl = lookup_name_real (name, tag_type != none_type,
22840 /*nonclass=*/0,
22841 /*block_p=*/true, is_namespace, 0);
22842 parser->qualifying_scope = NULL_TREE;
22843 parser->object_scope = NULL_TREE;
22846 /* If the lookup failed, let our caller know. */
22847 if (!decl || decl == error_mark_node)
22848 return error_mark_node;
22850 /* Pull out the template from an injected-class-name (or multiple). */
22851 if (is_template)
22852 decl = maybe_get_template_decl_from_type_decl (decl);
22854 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22855 if (TREE_CODE (decl) == TREE_LIST)
22857 if (ambiguous_decls)
22858 *ambiguous_decls = decl;
22859 /* The error message we have to print is too complicated for
22860 cp_parser_error, so we incorporate its actions directly. */
22861 if (!cp_parser_simulate_error (parser))
22863 error_at (name_location, "reference to %qD is ambiguous",
22864 name);
22865 print_candidates (decl);
22867 return error_mark_node;
22870 gcc_assert (DECL_P (decl)
22871 || TREE_CODE (decl) == OVERLOAD
22872 || TREE_CODE (decl) == SCOPE_REF
22873 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22874 || BASELINK_P (decl));
22876 /* If we have resolved the name of a member declaration, check to
22877 see if the declaration is accessible. When the name resolves to
22878 set of overloaded functions, accessibility is checked when
22879 overload resolution is done.
22881 During an explicit instantiation, access is not checked at all,
22882 as per [temp.explicit]. */
22883 if (DECL_P (decl))
22884 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22886 maybe_record_typedef_use (decl);
22888 return decl;
22891 /* Like cp_parser_lookup_name, but for use in the typical case where
22892 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22893 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22895 static tree
22896 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22898 return cp_parser_lookup_name (parser, name,
22899 none_type,
22900 /*is_template=*/false,
22901 /*is_namespace=*/false,
22902 /*check_dependency=*/true,
22903 /*ambiguous_decls=*/NULL,
22904 location);
22907 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22908 the current context, return the TYPE_DECL. If TAG_NAME_P is
22909 true, the DECL indicates the class being defined in a class-head,
22910 or declared in an elaborated-type-specifier.
22912 Otherwise, return DECL. */
22914 static tree
22915 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22917 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22918 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22920 struct A {
22921 template <typename T> struct B;
22924 template <typename T> struct A::B {};
22926 Similarly, in an elaborated-type-specifier:
22928 namespace N { struct X{}; }
22930 struct A {
22931 template <typename T> friend struct N::X;
22934 However, if the DECL refers to a class type, and we are in
22935 the scope of the class, then the name lookup automatically
22936 finds the TYPE_DECL created by build_self_reference rather
22937 than a TEMPLATE_DECL. For example, in:
22939 template <class T> struct S {
22940 S s;
22943 there is no need to handle such case. */
22945 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22946 return DECL_TEMPLATE_RESULT (decl);
22948 return decl;
22951 /* If too many, or too few, template-parameter lists apply to the
22952 declarator, issue an error message. Returns TRUE if all went well,
22953 and FALSE otherwise. */
22955 static bool
22956 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22957 cp_declarator *declarator,
22958 location_t declarator_location)
22960 switch (declarator->kind)
22962 case cdk_id:
22964 unsigned num_templates = 0;
22965 tree scope = declarator->u.id.qualifying_scope;
22967 if (scope)
22968 num_templates = num_template_headers_for_class (scope);
22969 else if (TREE_CODE (declarator->u.id.unqualified_name)
22970 == TEMPLATE_ID_EXPR)
22971 /* If the DECLARATOR has the form `X<y>' then it uses one
22972 additional level of template parameters. */
22973 ++num_templates;
22975 return cp_parser_check_template_parameters
22976 (parser, num_templates, declarator_location, declarator);
22979 case cdk_function:
22980 case cdk_array:
22981 case cdk_pointer:
22982 case cdk_reference:
22983 case cdk_ptrmem:
22984 return (cp_parser_check_declarator_template_parameters
22985 (parser, declarator->declarator, declarator_location));
22987 case cdk_error:
22988 return true;
22990 default:
22991 gcc_unreachable ();
22993 return false;
22996 /* NUM_TEMPLATES were used in the current declaration. If that is
22997 invalid, return FALSE and issue an error messages. Otherwise,
22998 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22999 declarator and we can print more accurate diagnostics. */
23001 static bool
23002 cp_parser_check_template_parameters (cp_parser* parser,
23003 unsigned num_templates,
23004 location_t location,
23005 cp_declarator *declarator)
23007 /* If there are the same number of template classes and parameter
23008 lists, that's OK. */
23009 if (parser->num_template_parameter_lists == num_templates)
23010 return true;
23011 /* If there are more, but only one more, then we are referring to a
23012 member template. That's OK too. */
23013 if (parser->num_template_parameter_lists == num_templates + 1)
23014 return true;
23015 /* If there are more template classes than parameter lists, we have
23016 something like:
23018 template <class T> void S<T>::R<T>::f (); */
23019 if (parser->num_template_parameter_lists < num_templates)
23021 if (declarator && !current_function_decl)
23022 error_at (location, "specializing member %<%T::%E%> "
23023 "requires %<template<>%> syntax",
23024 declarator->u.id.qualifying_scope,
23025 declarator->u.id.unqualified_name);
23026 else if (declarator)
23027 error_at (location, "invalid declaration of %<%T::%E%>",
23028 declarator->u.id.qualifying_scope,
23029 declarator->u.id.unqualified_name);
23030 else
23031 error_at (location, "too few template-parameter-lists");
23032 return false;
23034 /* Otherwise, there are too many template parameter lists. We have
23035 something like:
23037 template <class T> template <class U> void S::f(); */
23038 error_at (location, "too many template-parameter-lists");
23039 return false;
23042 /* Parse an optional `::' token indicating that the following name is
23043 from the global namespace. If so, PARSER->SCOPE is set to the
23044 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23045 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23046 Returns the new value of PARSER->SCOPE, if the `::' token is
23047 present, and NULL_TREE otherwise. */
23049 static tree
23050 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23052 cp_token *token;
23054 /* Peek at the next token. */
23055 token = cp_lexer_peek_token (parser->lexer);
23056 /* If we're looking at a `::' token then we're starting from the
23057 global namespace, not our current location. */
23058 if (token->type == CPP_SCOPE)
23060 /* Consume the `::' token. */
23061 cp_lexer_consume_token (parser->lexer);
23062 /* Set the SCOPE so that we know where to start the lookup. */
23063 parser->scope = global_namespace;
23064 parser->qualifying_scope = global_namespace;
23065 parser->object_scope = NULL_TREE;
23067 return parser->scope;
23069 else if (!current_scope_valid_p)
23071 parser->scope = NULL_TREE;
23072 parser->qualifying_scope = NULL_TREE;
23073 parser->object_scope = NULL_TREE;
23076 return NULL_TREE;
23079 /* Returns TRUE if the upcoming token sequence is the start of a
23080 constructor declarator. If FRIEND_P is true, the declarator is
23081 preceded by the `friend' specifier. */
23083 static bool
23084 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23086 bool constructor_p;
23087 bool outside_class_specifier_p;
23088 tree nested_name_specifier;
23089 cp_token *next_token;
23091 /* The common case is that this is not a constructor declarator, so
23092 try to avoid doing lots of work if at all possible. It's not
23093 valid declare a constructor at function scope. */
23094 if (parser->in_function_body)
23095 return false;
23096 /* And only certain tokens can begin a constructor declarator. */
23097 next_token = cp_lexer_peek_token (parser->lexer);
23098 if (next_token->type != CPP_NAME
23099 && next_token->type != CPP_SCOPE
23100 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23101 && next_token->type != CPP_TEMPLATE_ID)
23102 return false;
23104 /* Parse tentatively; we are going to roll back all of the tokens
23105 consumed here. */
23106 cp_parser_parse_tentatively (parser);
23107 /* Assume that we are looking at a constructor declarator. */
23108 constructor_p = true;
23110 /* Look for the optional `::' operator. */
23111 cp_parser_global_scope_opt (parser,
23112 /*current_scope_valid_p=*/false);
23113 /* Look for the nested-name-specifier. */
23114 nested_name_specifier
23115 = (cp_parser_nested_name_specifier_opt (parser,
23116 /*typename_keyword_p=*/false,
23117 /*check_dependency_p=*/false,
23118 /*type_p=*/false,
23119 /*is_declaration=*/false));
23121 outside_class_specifier_p = (!at_class_scope_p ()
23122 || !TYPE_BEING_DEFINED (current_class_type)
23123 || friend_p);
23125 /* Outside of a class-specifier, there must be a
23126 nested-name-specifier. */
23127 if (!nested_name_specifier && outside_class_specifier_p)
23128 constructor_p = false;
23129 else if (nested_name_specifier == error_mark_node)
23130 constructor_p = false;
23132 /* If we have a class scope, this is easy; DR 147 says that S::S always
23133 names the constructor, and no other qualified name could. */
23134 if (constructor_p && nested_name_specifier
23135 && CLASS_TYPE_P (nested_name_specifier))
23137 tree id = cp_parser_unqualified_id (parser,
23138 /*template_keyword_p=*/false,
23139 /*check_dependency_p=*/false,
23140 /*declarator_p=*/true,
23141 /*optional_p=*/false);
23142 if (is_overloaded_fn (id))
23143 id = DECL_NAME (get_first_fn (id));
23144 if (!constructor_name_p (id, nested_name_specifier))
23145 constructor_p = false;
23147 /* If we still think that this might be a constructor-declarator,
23148 look for a class-name. */
23149 else if (constructor_p)
23151 /* If we have:
23153 template <typename T> struct S {
23154 S();
23157 we must recognize that the nested `S' names a class. */
23158 tree type_decl;
23159 type_decl = cp_parser_class_name (parser,
23160 /*typename_keyword_p=*/false,
23161 /*template_keyword_p=*/false,
23162 none_type,
23163 /*check_dependency_p=*/false,
23164 /*class_head_p=*/false,
23165 /*is_declaration=*/false);
23166 /* If there was no class-name, then this is not a constructor.
23167 Otherwise, if we are in a class-specifier and we aren't
23168 handling a friend declaration, check that its type matches
23169 current_class_type (c++/38313). Note: error_mark_node
23170 is left alone for error recovery purposes. */
23171 constructor_p = (!cp_parser_error_occurred (parser)
23172 && (outside_class_specifier_p
23173 || type_decl == error_mark_node
23174 || same_type_p (current_class_type,
23175 TREE_TYPE (type_decl))));
23177 /* If we're still considering a constructor, we have to see a `(',
23178 to begin the parameter-declaration-clause, followed by either a
23179 `)', an `...', or a decl-specifier. We need to check for a
23180 type-specifier to avoid being fooled into thinking that:
23182 S (f) (int);
23184 is a constructor. (It is actually a function named `f' that
23185 takes one parameter (of type `int') and returns a value of type
23186 `S'. */
23187 if (constructor_p
23188 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23189 constructor_p = false;
23191 if (constructor_p
23192 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23193 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23194 /* A parameter declaration begins with a decl-specifier,
23195 which is either the "attribute" keyword, a storage class
23196 specifier, or (usually) a type-specifier. */
23197 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23199 tree type;
23200 tree pushed_scope = NULL_TREE;
23201 unsigned saved_num_template_parameter_lists;
23203 /* Names appearing in the type-specifier should be looked up
23204 in the scope of the class. */
23205 if (current_class_type)
23206 type = NULL_TREE;
23207 else
23209 type = TREE_TYPE (type_decl);
23210 if (TREE_CODE (type) == TYPENAME_TYPE)
23212 type = resolve_typename_type (type,
23213 /*only_current_p=*/false);
23214 if (TREE_CODE (type) == TYPENAME_TYPE)
23216 cp_parser_abort_tentative_parse (parser);
23217 return false;
23220 pushed_scope = push_scope (type);
23223 /* Inside the constructor parameter list, surrounding
23224 template-parameter-lists do not apply. */
23225 saved_num_template_parameter_lists
23226 = parser->num_template_parameter_lists;
23227 parser->num_template_parameter_lists = 0;
23229 /* Look for the type-specifier. */
23230 cp_parser_type_specifier (parser,
23231 CP_PARSER_FLAGS_NONE,
23232 /*decl_specs=*/NULL,
23233 /*is_declarator=*/true,
23234 /*declares_class_or_enum=*/NULL,
23235 /*is_cv_qualifier=*/NULL);
23237 parser->num_template_parameter_lists
23238 = saved_num_template_parameter_lists;
23240 /* Leave the scope of the class. */
23241 if (pushed_scope)
23242 pop_scope (pushed_scope);
23244 constructor_p = !cp_parser_error_occurred (parser);
23248 /* We did not really want to consume any tokens. */
23249 cp_parser_abort_tentative_parse (parser);
23251 return constructor_p;
23254 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23255 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23256 they must be performed once we are in the scope of the function.
23258 Returns the function defined. */
23260 static tree
23261 cp_parser_function_definition_from_specifiers_and_declarator
23262 (cp_parser* parser,
23263 cp_decl_specifier_seq *decl_specifiers,
23264 tree attributes,
23265 const cp_declarator *declarator)
23267 tree fn;
23268 bool success_p;
23270 /* Begin the function-definition. */
23271 success_p = start_function (decl_specifiers, declarator, attributes);
23273 /* The things we're about to see are not directly qualified by any
23274 template headers we've seen thus far. */
23275 reset_specialization ();
23277 /* If there were names looked up in the decl-specifier-seq that we
23278 did not check, check them now. We must wait until we are in the
23279 scope of the function to perform the checks, since the function
23280 might be a friend. */
23281 perform_deferred_access_checks (tf_warning_or_error);
23283 if (success_p)
23285 cp_finalize_omp_declare_simd (parser, current_function_decl);
23286 parser->omp_declare_simd = NULL;
23289 if (!success_p)
23291 /* Skip the entire function. */
23292 cp_parser_skip_to_end_of_block_or_statement (parser);
23293 fn = error_mark_node;
23295 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23297 /* Seen already, skip it. An error message has already been output. */
23298 cp_parser_skip_to_end_of_block_or_statement (parser);
23299 fn = current_function_decl;
23300 current_function_decl = NULL_TREE;
23301 /* If this is a function from a class, pop the nested class. */
23302 if (current_class_name)
23303 pop_nested_class ();
23305 else
23307 timevar_id_t tv;
23308 if (DECL_DECLARED_INLINE_P (current_function_decl))
23309 tv = TV_PARSE_INLINE;
23310 else
23311 tv = TV_PARSE_FUNC;
23312 timevar_push (tv);
23313 fn = cp_parser_function_definition_after_declarator (parser,
23314 /*inline_p=*/false);
23315 timevar_pop (tv);
23318 return fn;
23321 /* Parse the part of a function-definition that follows the
23322 declarator. INLINE_P is TRUE iff this function is an inline
23323 function defined within a class-specifier.
23325 Returns the function defined. */
23327 static tree
23328 cp_parser_function_definition_after_declarator (cp_parser* parser,
23329 bool inline_p)
23331 tree fn;
23332 bool ctor_initializer_p = false;
23333 bool saved_in_unbraced_linkage_specification_p;
23334 bool saved_in_function_body;
23335 unsigned saved_num_template_parameter_lists;
23336 cp_token *token;
23337 bool fully_implicit_function_template_p
23338 = parser->fully_implicit_function_template_p;
23339 parser->fully_implicit_function_template_p = false;
23340 tree implicit_template_parms
23341 = parser->implicit_template_parms;
23342 parser->implicit_template_parms = 0;
23343 cp_binding_level* implicit_template_scope
23344 = parser->implicit_template_scope;
23345 parser->implicit_template_scope = 0;
23347 saved_in_function_body = parser->in_function_body;
23348 parser->in_function_body = true;
23349 /* If the next token is `return', then the code may be trying to
23350 make use of the "named return value" extension that G++ used to
23351 support. */
23352 token = cp_lexer_peek_token (parser->lexer);
23353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23355 /* Consume the `return' keyword. */
23356 cp_lexer_consume_token (parser->lexer);
23357 /* Look for the identifier that indicates what value is to be
23358 returned. */
23359 cp_parser_identifier (parser);
23360 /* Issue an error message. */
23361 error_at (token->location,
23362 "named return values are no longer supported");
23363 /* Skip tokens until we reach the start of the function body. */
23364 while (true)
23366 cp_token *token = cp_lexer_peek_token (parser->lexer);
23367 if (token->type == CPP_OPEN_BRACE
23368 || token->type == CPP_EOF
23369 || token->type == CPP_PRAGMA_EOL)
23370 break;
23371 cp_lexer_consume_token (parser->lexer);
23374 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23375 anything declared inside `f'. */
23376 saved_in_unbraced_linkage_specification_p
23377 = parser->in_unbraced_linkage_specification_p;
23378 parser->in_unbraced_linkage_specification_p = false;
23379 /* Inside the function, surrounding template-parameter-lists do not
23380 apply. */
23381 saved_num_template_parameter_lists
23382 = parser->num_template_parameter_lists;
23383 parser->num_template_parameter_lists = 0;
23385 start_lambda_scope (current_function_decl);
23387 /* If the next token is `try', `__transaction_atomic', or
23388 `__transaction_relaxed`, then we are looking at either function-try-block
23389 or function-transaction-block. Note that all of these include the
23390 function-body. */
23391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23392 ctor_initializer_p = cp_parser_function_transaction (parser,
23393 RID_TRANSACTION_ATOMIC);
23394 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23395 RID_TRANSACTION_RELAXED))
23396 ctor_initializer_p = cp_parser_function_transaction (parser,
23397 RID_TRANSACTION_RELAXED);
23398 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23399 ctor_initializer_p = cp_parser_function_try_block (parser);
23400 else
23401 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23402 (parser, /*in_function_try_block=*/false);
23404 finish_lambda_scope ();
23406 /* Finish the function. */
23407 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23408 (inline_p ? 2 : 0));
23409 /* Generate code for it, if necessary. */
23410 expand_or_defer_fn (fn);
23411 /* Restore the saved values. */
23412 parser->in_unbraced_linkage_specification_p
23413 = saved_in_unbraced_linkage_specification_p;
23414 parser->num_template_parameter_lists
23415 = saved_num_template_parameter_lists;
23416 parser->in_function_body = saved_in_function_body;
23418 parser->fully_implicit_function_template_p
23419 = fully_implicit_function_template_p;
23420 parser->implicit_template_parms
23421 = implicit_template_parms;
23422 parser->implicit_template_scope
23423 = implicit_template_scope;
23425 if (parser->fully_implicit_function_template_p)
23426 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23428 return fn;
23431 /* Parse a template-declaration, assuming that the `export' (and
23432 `extern') keywords, if present, has already been scanned. MEMBER_P
23433 is as for cp_parser_template_declaration. */
23435 static void
23436 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23438 tree decl = NULL_TREE;
23439 vec<deferred_access_check, va_gc> *checks;
23440 tree parameter_list;
23441 bool friend_p = false;
23442 bool need_lang_pop;
23443 cp_token *token;
23445 /* Look for the `template' keyword. */
23446 token = cp_lexer_peek_token (parser->lexer);
23447 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23448 return;
23450 /* And the `<'. */
23451 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23452 return;
23453 if (at_class_scope_p () && current_function_decl)
23455 /* 14.5.2.2 [temp.mem]
23457 A local class shall not have member templates. */
23458 error_at (token->location,
23459 "invalid declaration of member template in local class");
23460 cp_parser_skip_to_end_of_block_or_statement (parser);
23461 return;
23463 /* [temp]
23465 A template ... shall not have C linkage. */
23466 if (current_lang_name == lang_name_c)
23468 error_at (token->location, "template with C linkage");
23469 /* Give it C++ linkage to avoid confusing other parts of the
23470 front end. */
23471 push_lang_context (lang_name_cplusplus);
23472 need_lang_pop = true;
23474 else
23475 need_lang_pop = false;
23477 /* We cannot perform access checks on the template parameter
23478 declarations until we know what is being declared, just as we
23479 cannot check the decl-specifier list. */
23480 push_deferring_access_checks (dk_deferred);
23482 /* If the next token is `>', then we have an invalid
23483 specialization. Rather than complain about an invalid template
23484 parameter, issue an error message here. */
23485 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23487 cp_parser_error (parser, "invalid explicit specialization");
23488 begin_specialization ();
23489 parameter_list = NULL_TREE;
23491 else
23493 /* Parse the template parameters. */
23494 parameter_list = cp_parser_template_parameter_list (parser);
23497 /* Get the deferred access checks from the parameter list. These
23498 will be checked once we know what is being declared, as for a
23499 member template the checks must be performed in the scope of the
23500 class containing the member. */
23501 checks = get_deferred_access_checks ();
23503 /* Look for the `>'. */
23504 cp_parser_skip_to_end_of_template_parameter_list (parser);
23505 /* We just processed one more parameter list. */
23506 ++parser->num_template_parameter_lists;
23507 /* If the next token is `template', there are more template
23508 parameters. */
23509 if (cp_lexer_next_token_is_keyword (parser->lexer,
23510 RID_TEMPLATE))
23511 cp_parser_template_declaration_after_export (parser, member_p);
23512 else if (cxx_dialect >= cxx11
23513 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23514 decl = cp_parser_alias_declaration (parser);
23515 else
23517 /* There are no access checks when parsing a template, as we do not
23518 know if a specialization will be a friend. */
23519 push_deferring_access_checks (dk_no_check);
23520 token = cp_lexer_peek_token (parser->lexer);
23521 decl = cp_parser_single_declaration (parser,
23522 checks,
23523 member_p,
23524 /*explicit_specialization_p=*/false,
23525 &friend_p);
23526 pop_deferring_access_checks ();
23528 /* If this is a member template declaration, let the front
23529 end know. */
23530 if (member_p && !friend_p && decl)
23532 if (TREE_CODE (decl) == TYPE_DECL)
23533 cp_parser_check_access_in_redeclaration (decl, token->location);
23535 decl = finish_member_template_decl (decl);
23537 else if (friend_p && decl
23538 && DECL_DECLARES_TYPE_P (decl))
23539 make_friend_class (current_class_type, TREE_TYPE (decl),
23540 /*complain=*/true);
23542 /* We are done with the current parameter list. */
23543 --parser->num_template_parameter_lists;
23545 pop_deferring_access_checks ();
23547 /* Finish up. */
23548 finish_template_decl (parameter_list);
23550 /* Check the template arguments for a literal operator template. */
23551 if (decl
23552 && DECL_DECLARES_FUNCTION_P (decl)
23553 && UDLIT_OPER_P (DECL_NAME (decl)))
23555 bool ok = true;
23556 if (parameter_list == NULL_TREE)
23557 ok = false;
23558 else
23560 int num_parms = TREE_VEC_LENGTH (parameter_list);
23561 if (num_parms == 1)
23563 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23564 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23565 if (TREE_TYPE (parm) != char_type_node
23566 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23567 ok = false;
23569 else if (num_parms == 2 && cxx_dialect >= cxx14)
23571 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23572 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23573 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23574 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23575 if (TREE_TYPE (parm) != TREE_TYPE (type)
23576 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23577 ok = false;
23579 else
23580 ok = false;
23582 if (!ok)
23584 if (cxx_dialect >= cxx14)
23585 error ("literal operator template %qD has invalid parameter list."
23586 " Expected non-type template argument pack <char...>"
23587 " or <typename CharT, CharT...>",
23588 decl);
23589 else
23590 error ("literal operator template %qD has invalid parameter list."
23591 " Expected non-type template argument pack <char...>",
23592 decl);
23595 /* Register member declarations. */
23596 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23597 finish_member_declaration (decl);
23598 /* For the erroneous case of a template with C linkage, we pushed an
23599 implicit C++ linkage scope; exit that scope now. */
23600 if (need_lang_pop)
23601 pop_lang_context ();
23602 /* If DECL is a function template, we must return to parse it later.
23603 (Even though there is no definition, there might be default
23604 arguments that need handling.) */
23605 if (member_p && decl
23606 && DECL_DECLARES_FUNCTION_P (decl))
23607 vec_safe_push (unparsed_funs_with_definitions, decl);
23610 /* Perform the deferred access checks from a template-parameter-list.
23611 CHECKS is a TREE_LIST of access checks, as returned by
23612 get_deferred_access_checks. */
23614 static void
23615 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23617 ++processing_template_parmlist;
23618 perform_access_checks (checks, tf_warning_or_error);
23619 --processing_template_parmlist;
23622 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23623 `function-definition' sequence that follows a template header.
23624 If MEMBER_P is true, this declaration appears in a class scope.
23626 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23627 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23629 static tree
23630 cp_parser_single_declaration (cp_parser* parser,
23631 vec<deferred_access_check, va_gc> *checks,
23632 bool member_p,
23633 bool explicit_specialization_p,
23634 bool* friend_p)
23636 int declares_class_or_enum;
23637 tree decl = NULL_TREE;
23638 cp_decl_specifier_seq decl_specifiers;
23639 bool function_definition_p = false;
23640 cp_token *decl_spec_token_start;
23642 /* This function is only used when processing a template
23643 declaration. */
23644 gcc_assert (innermost_scope_kind () == sk_template_parms
23645 || innermost_scope_kind () == sk_template_spec);
23647 /* Defer access checks until we know what is being declared. */
23648 push_deferring_access_checks (dk_deferred);
23650 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23651 alternative. */
23652 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23653 cp_parser_decl_specifier_seq (parser,
23654 CP_PARSER_FLAGS_OPTIONAL,
23655 &decl_specifiers,
23656 &declares_class_or_enum);
23657 if (friend_p)
23658 *friend_p = cp_parser_friend_p (&decl_specifiers);
23660 /* There are no template typedefs. */
23661 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23663 error_at (decl_spec_token_start->location,
23664 "template declaration of %<typedef%>");
23665 decl = error_mark_node;
23668 /* Gather up the access checks that occurred the
23669 decl-specifier-seq. */
23670 stop_deferring_access_checks ();
23672 /* Check for the declaration of a template class. */
23673 if (declares_class_or_enum)
23675 if (cp_parser_declares_only_class_p (parser))
23677 decl = shadow_tag (&decl_specifiers);
23679 /* In this case:
23681 struct C {
23682 friend template <typename T> struct A<T>::B;
23685 A<T>::B will be represented by a TYPENAME_TYPE, and
23686 therefore not recognized by shadow_tag. */
23687 if (friend_p && *friend_p
23688 && !decl
23689 && decl_specifiers.type
23690 && TYPE_P (decl_specifiers.type))
23691 decl = decl_specifiers.type;
23693 if (decl && decl != error_mark_node)
23694 decl = TYPE_NAME (decl);
23695 else
23696 decl = error_mark_node;
23698 /* Perform access checks for template parameters. */
23699 cp_parser_perform_template_parameter_access_checks (checks);
23703 /* Complain about missing 'typename' or other invalid type names. */
23704 if (!decl_specifiers.any_type_specifiers_p
23705 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23707 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23708 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23709 the rest of this declaration. */
23710 decl = error_mark_node;
23711 goto out;
23714 /* If it's not a template class, try for a template function. If
23715 the next token is a `;', then this declaration does not declare
23716 anything. But, if there were errors in the decl-specifiers, then
23717 the error might well have come from an attempted class-specifier.
23718 In that case, there's no need to warn about a missing declarator. */
23719 if (!decl
23720 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23721 || decl_specifiers.type != error_mark_node))
23723 decl = cp_parser_init_declarator (parser,
23724 &decl_specifiers,
23725 checks,
23726 /*function_definition_allowed_p=*/true,
23727 member_p,
23728 declares_class_or_enum,
23729 &function_definition_p,
23730 NULL);
23732 /* 7.1.1-1 [dcl.stc]
23734 A storage-class-specifier shall not be specified in an explicit
23735 specialization... */
23736 if (decl
23737 && explicit_specialization_p
23738 && decl_specifiers.storage_class != sc_none)
23740 error_at (decl_spec_token_start->location,
23741 "explicit template specialization cannot have a storage class");
23742 decl = error_mark_node;
23745 if (decl && VAR_P (decl))
23746 check_template_variable (decl);
23749 /* Look for a trailing `;' after the declaration. */
23750 if (!function_definition_p
23751 && (decl == error_mark_node
23752 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23753 cp_parser_skip_to_end_of_block_or_statement (parser);
23755 out:
23756 pop_deferring_access_checks ();
23758 /* Clear any current qualification; whatever comes next is the start
23759 of something new. */
23760 parser->scope = NULL_TREE;
23761 parser->qualifying_scope = NULL_TREE;
23762 parser->object_scope = NULL_TREE;
23764 return decl;
23767 /* Parse a cast-expression that is not the operand of a unary "&". */
23769 static tree
23770 cp_parser_simple_cast_expression (cp_parser *parser)
23772 return cp_parser_cast_expression (parser, /*address_p=*/false,
23773 /*cast_p=*/false, /*decltype*/false, NULL);
23776 /* Parse a functional cast to TYPE. Returns an expression
23777 representing the cast. */
23779 static tree
23780 cp_parser_functional_cast (cp_parser* parser, tree type)
23782 vec<tree, va_gc> *vec;
23783 tree expression_list;
23784 tree cast;
23785 bool nonconst_p;
23787 if (!type)
23788 type = error_mark_node;
23790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23792 cp_lexer_set_source_position (parser->lexer);
23793 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23794 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23795 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23796 if (TREE_CODE (type) == TYPE_DECL)
23797 type = TREE_TYPE (type);
23798 return finish_compound_literal (type, expression_list,
23799 tf_warning_or_error);
23803 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23804 /*cast_p=*/true,
23805 /*allow_expansion_p=*/true,
23806 /*non_constant_p=*/NULL);
23807 if (vec == NULL)
23808 expression_list = error_mark_node;
23809 else
23811 expression_list = build_tree_list_vec (vec);
23812 release_tree_vector (vec);
23815 cast = build_functional_cast (type, expression_list,
23816 tf_warning_or_error);
23817 /* [expr.const]/1: In an integral constant expression "only type
23818 conversions to integral or enumeration type can be used". */
23819 if (TREE_CODE (type) == TYPE_DECL)
23820 type = TREE_TYPE (type);
23821 if (cast != error_mark_node
23822 && !cast_valid_in_integral_constant_expression_p (type)
23823 && cp_parser_non_integral_constant_expression (parser,
23824 NIC_CONSTRUCTOR))
23825 return error_mark_node;
23826 return cast;
23829 /* Save the tokens that make up the body of a member function defined
23830 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23831 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23832 specifiers applied to the declaration. Returns the FUNCTION_DECL
23833 for the member function. */
23835 static tree
23836 cp_parser_save_member_function_body (cp_parser* parser,
23837 cp_decl_specifier_seq *decl_specifiers,
23838 cp_declarator *declarator,
23839 tree attributes)
23841 cp_token *first;
23842 cp_token *last;
23843 tree fn;
23845 /* Create the FUNCTION_DECL. */
23846 fn = grokmethod (decl_specifiers, declarator, attributes);
23847 cp_finalize_omp_declare_simd (parser, fn);
23848 /* If something went badly wrong, bail out now. */
23849 if (fn == error_mark_node)
23851 /* If there's a function-body, skip it. */
23852 if (cp_parser_token_starts_function_definition_p
23853 (cp_lexer_peek_token (parser->lexer)))
23854 cp_parser_skip_to_end_of_block_or_statement (parser);
23855 return error_mark_node;
23858 /* Remember it, if there default args to post process. */
23859 cp_parser_save_default_args (parser, fn);
23861 /* Save away the tokens that make up the body of the
23862 function. */
23863 first = parser->lexer->next_token;
23864 /* Handle function try blocks. */
23865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23866 cp_lexer_consume_token (parser->lexer);
23867 /* We can have braced-init-list mem-initializers before the fn body. */
23868 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23870 cp_lexer_consume_token (parser->lexer);
23871 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23873 /* cache_group will stop after an un-nested { } pair, too. */
23874 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23875 break;
23877 /* variadic mem-inits have ... after the ')'. */
23878 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23879 cp_lexer_consume_token (parser->lexer);
23882 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23883 /* Handle function try blocks. */
23884 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23885 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23886 last = parser->lexer->next_token;
23888 /* Save away the inline definition; we will process it when the
23889 class is complete. */
23890 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23891 DECL_PENDING_INLINE_P (fn) = 1;
23893 /* We need to know that this was defined in the class, so that
23894 friend templates are handled correctly. */
23895 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23897 /* Add FN to the queue of functions to be parsed later. */
23898 vec_safe_push (unparsed_funs_with_definitions, fn);
23900 return fn;
23903 /* Save the tokens that make up the in-class initializer for a non-static
23904 data member. Returns a DEFAULT_ARG. */
23906 static tree
23907 cp_parser_save_nsdmi (cp_parser* parser)
23909 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23912 /* Parse a template-argument-list, as well as the trailing ">" (but
23913 not the opening "<"). See cp_parser_template_argument_list for the
23914 return value. */
23916 static tree
23917 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23919 tree arguments;
23920 tree saved_scope;
23921 tree saved_qualifying_scope;
23922 tree saved_object_scope;
23923 bool saved_greater_than_is_operator_p;
23924 int saved_unevaluated_operand;
23925 int saved_inhibit_evaluation_warnings;
23927 /* [temp.names]
23929 When parsing a template-id, the first non-nested `>' is taken as
23930 the end of the template-argument-list rather than a greater-than
23931 operator. */
23932 saved_greater_than_is_operator_p
23933 = parser->greater_than_is_operator_p;
23934 parser->greater_than_is_operator_p = false;
23935 /* Parsing the argument list may modify SCOPE, so we save it
23936 here. */
23937 saved_scope = parser->scope;
23938 saved_qualifying_scope = parser->qualifying_scope;
23939 saved_object_scope = parser->object_scope;
23940 /* We need to evaluate the template arguments, even though this
23941 template-id may be nested within a "sizeof". */
23942 saved_unevaluated_operand = cp_unevaluated_operand;
23943 cp_unevaluated_operand = 0;
23944 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23945 c_inhibit_evaluation_warnings = 0;
23946 /* Parse the template-argument-list itself. */
23947 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23948 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23949 arguments = NULL_TREE;
23950 else
23951 arguments = cp_parser_template_argument_list (parser);
23952 /* Look for the `>' that ends the template-argument-list. If we find
23953 a '>>' instead, it's probably just a typo. */
23954 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23956 if (cxx_dialect != cxx98)
23958 /* In C++0x, a `>>' in a template argument list or cast
23959 expression is considered to be two separate `>'
23960 tokens. So, change the current token to a `>', but don't
23961 consume it: it will be consumed later when the outer
23962 template argument list (or cast expression) is parsed.
23963 Note that this replacement of `>' for `>>' is necessary
23964 even if we are parsing tentatively: in the tentative
23965 case, after calling
23966 cp_parser_enclosed_template_argument_list we will always
23967 throw away all of the template arguments and the first
23968 closing `>', either because the template argument list
23969 was erroneous or because we are replacing those tokens
23970 with a CPP_TEMPLATE_ID token. The second `>' (which will
23971 not have been thrown away) is needed either to close an
23972 outer template argument list or to complete a new-style
23973 cast. */
23974 cp_token *token = cp_lexer_peek_token (parser->lexer);
23975 token->type = CPP_GREATER;
23977 else if (!saved_greater_than_is_operator_p)
23979 /* If we're in a nested template argument list, the '>>' has
23980 to be a typo for '> >'. We emit the error message, but we
23981 continue parsing and we push a '>' as next token, so that
23982 the argument list will be parsed correctly. Note that the
23983 global source location is still on the token before the
23984 '>>', so we need to say explicitly where we want it. */
23985 cp_token *token = cp_lexer_peek_token (parser->lexer);
23986 error_at (token->location, "%<>>%> should be %<> >%> "
23987 "within a nested template argument list");
23989 token->type = CPP_GREATER;
23991 else
23993 /* If this is not a nested template argument list, the '>>'
23994 is a typo for '>'. Emit an error message and continue.
23995 Same deal about the token location, but here we can get it
23996 right by consuming the '>>' before issuing the diagnostic. */
23997 cp_token *token = cp_lexer_consume_token (parser->lexer);
23998 error_at (token->location,
23999 "spurious %<>>%>, use %<>%> to terminate "
24000 "a template argument list");
24003 else
24004 cp_parser_skip_to_end_of_template_parameter_list (parser);
24005 /* The `>' token might be a greater-than operator again now. */
24006 parser->greater_than_is_operator_p
24007 = saved_greater_than_is_operator_p;
24008 /* Restore the SAVED_SCOPE. */
24009 parser->scope = saved_scope;
24010 parser->qualifying_scope = saved_qualifying_scope;
24011 parser->object_scope = saved_object_scope;
24012 cp_unevaluated_operand = saved_unevaluated_operand;
24013 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24015 return arguments;
24018 /* MEMBER_FUNCTION is a member function, or a friend. If default
24019 arguments, or the body of the function have not yet been parsed,
24020 parse them now. */
24022 static void
24023 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24025 timevar_push (TV_PARSE_INMETH);
24026 /* If this member is a template, get the underlying
24027 FUNCTION_DECL. */
24028 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24029 member_function = DECL_TEMPLATE_RESULT (member_function);
24031 /* There should not be any class definitions in progress at this
24032 point; the bodies of members are only parsed outside of all class
24033 definitions. */
24034 gcc_assert (parser->num_classes_being_defined == 0);
24035 /* While we're parsing the member functions we might encounter more
24036 classes. We want to handle them right away, but we don't want
24037 them getting mixed up with functions that are currently in the
24038 queue. */
24039 push_unparsed_function_queues (parser);
24041 /* Make sure that any template parameters are in scope. */
24042 maybe_begin_member_template_processing (member_function);
24044 /* If the body of the function has not yet been parsed, parse it
24045 now. */
24046 if (DECL_PENDING_INLINE_P (member_function))
24048 tree function_scope;
24049 cp_token_cache *tokens;
24051 /* The function is no longer pending; we are processing it. */
24052 tokens = DECL_PENDING_INLINE_INFO (member_function);
24053 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24054 DECL_PENDING_INLINE_P (member_function) = 0;
24056 /* If this is a local class, enter the scope of the containing
24057 function. */
24058 function_scope = current_function_decl;
24059 if (function_scope)
24060 push_function_context ();
24062 /* Push the body of the function onto the lexer stack. */
24063 cp_parser_push_lexer_for_tokens (parser, tokens);
24065 /* Let the front end know that we going to be defining this
24066 function. */
24067 start_preparsed_function (member_function, NULL_TREE,
24068 SF_PRE_PARSED | SF_INCLASS_INLINE);
24070 /* Don't do access checking if it is a templated function. */
24071 if (processing_template_decl)
24072 push_deferring_access_checks (dk_no_check);
24074 /* #pragma omp declare reduction needs special parsing. */
24075 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24077 parser->lexer->in_pragma = true;
24078 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24079 finish_function (/*inline*/2);
24080 cp_check_omp_declare_reduction (member_function);
24082 else
24083 /* Now, parse the body of the function. */
24084 cp_parser_function_definition_after_declarator (parser,
24085 /*inline_p=*/true);
24087 if (processing_template_decl)
24088 pop_deferring_access_checks ();
24090 /* Leave the scope of the containing function. */
24091 if (function_scope)
24092 pop_function_context ();
24093 cp_parser_pop_lexer (parser);
24096 /* Remove any template parameters from the symbol table. */
24097 maybe_end_member_template_processing ();
24099 /* Restore the queue. */
24100 pop_unparsed_function_queues (parser);
24101 timevar_pop (TV_PARSE_INMETH);
24104 /* If DECL contains any default args, remember it on the unparsed
24105 functions queue. */
24107 static void
24108 cp_parser_save_default_args (cp_parser* parser, tree decl)
24110 tree probe;
24112 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24113 probe;
24114 probe = TREE_CHAIN (probe))
24115 if (TREE_PURPOSE (probe))
24117 cp_default_arg_entry entry = {current_class_type, decl};
24118 vec_safe_push (unparsed_funs_with_default_args, entry);
24119 break;
24123 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24124 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24125 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24126 from the parameter-type-list. */
24128 static tree
24129 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24130 tree default_arg, tree parmtype)
24132 cp_token_cache *tokens;
24133 tree parsed_arg;
24134 bool dummy;
24136 if (default_arg == error_mark_node)
24137 return error_mark_node;
24139 /* Push the saved tokens for the default argument onto the parser's
24140 lexer stack. */
24141 tokens = DEFARG_TOKENS (default_arg);
24142 cp_parser_push_lexer_for_tokens (parser, tokens);
24144 start_lambda_scope (decl);
24146 /* Parse the default argument. */
24147 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24148 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24151 finish_lambda_scope ();
24153 if (parsed_arg == error_mark_node)
24154 cp_parser_skip_to_end_of_statement (parser);
24156 if (!processing_template_decl)
24158 /* In a non-template class, check conversions now. In a template,
24159 we'll wait and instantiate these as needed. */
24160 if (TREE_CODE (decl) == PARM_DECL)
24161 parsed_arg = check_default_argument (parmtype, parsed_arg,
24162 tf_warning_or_error);
24163 else
24164 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24167 /* If the token stream has not been completely used up, then
24168 there was extra junk after the end of the default
24169 argument. */
24170 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24172 if (TREE_CODE (decl) == PARM_DECL)
24173 cp_parser_error (parser, "expected %<,%>");
24174 else
24175 cp_parser_error (parser, "expected %<;%>");
24178 /* Revert to the main lexer. */
24179 cp_parser_pop_lexer (parser);
24181 return parsed_arg;
24184 /* FIELD is a non-static data member with an initializer which we saved for
24185 later; parse it now. */
24187 static void
24188 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24190 tree def;
24192 maybe_begin_member_template_processing (field);
24194 push_unparsed_function_queues (parser);
24195 def = cp_parser_late_parse_one_default_arg (parser, field,
24196 DECL_INITIAL (field),
24197 NULL_TREE);
24198 pop_unparsed_function_queues (parser);
24200 maybe_end_member_template_processing ();
24202 DECL_INITIAL (field) = def;
24205 /* FN is a FUNCTION_DECL which may contains a parameter with an
24206 unparsed DEFAULT_ARG. Parse the default args now. This function
24207 assumes that the current scope is the scope in which the default
24208 argument should be processed. */
24210 static void
24211 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24213 bool saved_local_variables_forbidden_p;
24214 tree parm, parmdecl;
24216 /* While we're parsing the default args, we might (due to the
24217 statement expression extension) encounter more classes. We want
24218 to handle them right away, but we don't want them getting mixed
24219 up with default args that are currently in the queue. */
24220 push_unparsed_function_queues (parser);
24222 /* Local variable names (and the `this' keyword) may not appear
24223 in a default argument. */
24224 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24225 parser->local_variables_forbidden_p = true;
24227 push_defarg_context (fn);
24229 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24230 parmdecl = DECL_ARGUMENTS (fn);
24231 parm && parm != void_list_node;
24232 parm = TREE_CHAIN (parm),
24233 parmdecl = DECL_CHAIN (parmdecl))
24235 tree default_arg = TREE_PURPOSE (parm);
24236 tree parsed_arg;
24237 vec<tree, va_gc> *insts;
24238 tree copy;
24239 unsigned ix;
24241 if (!default_arg)
24242 continue;
24244 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24245 /* This can happen for a friend declaration for a function
24246 already declared with default arguments. */
24247 continue;
24249 parsed_arg
24250 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24251 default_arg,
24252 TREE_VALUE (parm));
24253 if (parsed_arg == error_mark_node)
24255 continue;
24258 TREE_PURPOSE (parm) = parsed_arg;
24260 /* Update any instantiations we've already created. */
24261 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24262 vec_safe_iterate (insts, ix, &copy); ix++)
24263 TREE_PURPOSE (copy) = parsed_arg;
24266 pop_defarg_context ();
24268 /* Make sure no default arg is missing. */
24269 check_default_args (fn);
24271 /* Restore the state of local_variables_forbidden_p. */
24272 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24274 /* Restore the queue. */
24275 pop_unparsed_function_queues (parser);
24278 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24280 sizeof ... ( identifier )
24282 where the 'sizeof' token has already been consumed. */
24284 static tree
24285 cp_parser_sizeof_pack (cp_parser *parser)
24287 /* Consume the `...'. */
24288 cp_lexer_consume_token (parser->lexer);
24289 maybe_warn_variadic_templates ();
24291 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24292 if (paren)
24293 cp_lexer_consume_token (parser->lexer);
24294 else
24295 permerror (cp_lexer_peek_token (parser->lexer)->location,
24296 "%<sizeof...%> argument must be surrounded by parentheses");
24298 cp_token *token = cp_lexer_peek_token (parser->lexer);
24299 tree name = cp_parser_identifier (parser);
24300 if (name == error_mark_node)
24301 return error_mark_node;
24302 /* The name is not qualified. */
24303 parser->scope = NULL_TREE;
24304 parser->qualifying_scope = NULL_TREE;
24305 parser->object_scope = NULL_TREE;
24306 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24307 if (expr == error_mark_node)
24308 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24309 token->location);
24310 if (TREE_CODE (expr) == TYPE_DECL)
24311 expr = TREE_TYPE (expr);
24312 else if (TREE_CODE (expr) == CONST_DECL)
24313 expr = DECL_INITIAL (expr);
24314 expr = make_pack_expansion (expr);
24316 if (paren)
24317 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24319 return expr;
24322 /* Parse the operand of `sizeof' (or a similar operator). Returns
24323 either a TYPE or an expression, depending on the form of the
24324 input. The KEYWORD indicates which kind of expression we have
24325 encountered. */
24327 static tree
24328 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24330 tree expr = NULL_TREE;
24331 const char *saved_message;
24332 char *tmp;
24333 bool saved_integral_constant_expression_p;
24334 bool saved_non_integral_constant_expression_p;
24336 /* If it's a `...', then we are computing the length of a parameter
24337 pack. */
24338 if (keyword == RID_SIZEOF
24339 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24340 return cp_parser_sizeof_pack (parser);
24342 /* Types cannot be defined in a `sizeof' expression. Save away the
24343 old message. */
24344 saved_message = parser->type_definition_forbidden_message;
24345 /* And create the new one. */
24346 tmp = concat ("types may not be defined in %<",
24347 IDENTIFIER_POINTER (ridpointers[keyword]),
24348 "%> expressions", NULL);
24349 parser->type_definition_forbidden_message = tmp;
24351 /* The restrictions on constant-expressions do not apply inside
24352 sizeof expressions. */
24353 saved_integral_constant_expression_p
24354 = parser->integral_constant_expression_p;
24355 saved_non_integral_constant_expression_p
24356 = parser->non_integral_constant_expression_p;
24357 parser->integral_constant_expression_p = false;
24359 /* Do not actually evaluate the expression. */
24360 ++cp_unevaluated_operand;
24361 ++c_inhibit_evaluation_warnings;
24362 /* If it's a `(', then we might be looking at the type-id
24363 construction. */
24364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24366 tree type = NULL_TREE;
24368 /* We can't be sure yet whether we're looking at a type-id or an
24369 expression. */
24370 cp_parser_parse_tentatively (parser);
24371 /* Note: as a GNU Extension, compound literals are considered
24372 postfix-expressions as they are in C99, so they are valid
24373 arguments to sizeof. See comment in cp_parser_cast_expression
24374 for details. */
24375 if (cp_parser_compound_literal_p (parser))
24376 cp_parser_simulate_error (parser);
24377 else
24379 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24380 parser->in_type_id_in_expr_p = true;
24381 /* Look for the type-id. */
24382 type = cp_parser_type_id (parser);
24383 /* Look for the closing `)'. */
24384 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24385 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24388 /* If all went well, then we're done. */
24389 if (cp_parser_parse_definitely (parser))
24391 cp_decl_specifier_seq decl_specs;
24393 /* Build a trivial decl-specifier-seq. */
24394 clear_decl_specs (&decl_specs);
24395 decl_specs.type = type;
24397 /* Call grokdeclarator to figure out what type this is. */
24398 expr = grokdeclarator (NULL,
24399 &decl_specs,
24400 TYPENAME,
24401 /*initialized=*/0,
24402 /*attrlist=*/NULL);
24406 /* If the type-id production did not work out, then we must be
24407 looking at the unary-expression production. */
24408 if (!expr)
24409 expr = cp_parser_unary_expression (parser);
24411 /* Go back to evaluating expressions. */
24412 --cp_unevaluated_operand;
24413 --c_inhibit_evaluation_warnings;
24415 /* Free the message we created. */
24416 free (tmp);
24417 /* And restore the old one. */
24418 parser->type_definition_forbidden_message = saved_message;
24419 parser->integral_constant_expression_p
24420 = saved_integral_constant_expression_p;
24421 parser->non_integral_constant_expression_p
24422 = saved_non_integral_constant_expression_p;
24424 return expr;
24427 /* If the current declaration has no declarator, return true. */
24429 static bool
24430 cp_parser_declares_only_class_p (cp_parser *parser)
24432 /* If the next token is a `;' or a `,' then there is no
24433 declarator. */
24434 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24435 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24438 /* Update the DECL_SPECS to reflect the storage class indicated by
24439 KEYWORD. */
24441 static void
24442 cp_parser_set_storage_class (cp_parser *parser,
24443 cp_decl_specifier_seq *decl_specs,
24444 enum rid keyword,
24445 cp_token *token)
24447 cp_storage_class storage_class;
24449 if (parser->in_unbraced_linkage_specification_p)
24451 error_at (token->location, "invalid use of %qD in linkage specification",
24452 ridpointers[keyword]);
24453 return;
24455 else if (decl_specs->storage_class != sc_none)
24457 decl_specs->conflicting_specifiers_p = true;
24458 return;
24461 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24462 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24463 && decl_specs->gnu_thread_keyword_p)
24465 pedwarn (decl_specs->locations[ds_thread], 0,
24466 "%<__thread%> before %qD", ridpointers[keyword]);
24469 switch (keyword)
24471 case RID_AUTO:
24472 storage_class = sc_auto;
24473 break;
24474 case RID_REGISTER:
24475 storage_class = sc_register;
24476 break;
24477 case RID_STATIC:
24478 storage_class = sc_static;
24479 break;
24480 case RID_EXTERN:
24481 storage_class = sc_extern;
24482 break;
24483 case RID_MUTABLE:
24484 storage_class = sc_mutable;
24485 break;
24486 default:
24487 gcc_unreachable ();
24489 decl_specs->storage_class = storage_class;
24490 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24492 /* A storage class specifier cannot be applied alongside a typedef
24493 specifier. If there is a typedef specifier present then set
24494 conflicting_specifiers_p which will trigger an error later
24495 on in grokdeclarator. */
24496 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24497 decl_specs->conflicting_specifiers_p = true;
24500 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24501 is true, the type is a class or enum definition. */
24503 static void
24504 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24505 tree type_spec,
24506 cp_token *token,
24507 bool type_definition_p)
24509 decl_specs->any_specifiers_p = true;
24511 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24512 (with, for example, in "typedef int wchar_t;") we remember that
24513 this is what happened. In system headers, we ignore these
24514 declarations so that G++ can work with system headers that are not
24515 C++-safe. */
24516 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24517 && !type_definition_p
24518 && (type_spec == boolean_type_node
24519 || type_spec == char16_type_node
24520 || type_spec == char32_type_node
24521 || type_spec == wchar_type_node)
24522 && (decl_specs->type
24523 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24524 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24525 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24526 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24528 decl_specs->redefined_builtin_type = type_spec;
24529 set_and_check_decl_spec_loc (decl_specs,
24530 ds_redefined_builtin_type_spec,
24531 token);
24532 if (!decl_specs->type)
24534 decl_specs->type = type_spec;
24535 decl_specs->type_definition_p = false;
24536 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24539 else if (decl_specs->type)
24540 decl_specs->multiple_types_p = true;
24541 else
24543 decl_specs->type = type_spec;
24544 decl_specs->type_definition_p = type_definition_p;
24545 decl_specs->redefined_builtin_type = NULL_TREE;
24546 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24550 /* True iff TOKEN is the GNU keyword __thread. */
24552 static bool
24553 token_is__thread (cp_token *token)
24555 gcc_assert (token->keyword == RID_THREAD);
24556 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24559 /* Set the location for a declarator specifier and check if it is
24560 duplicated.
24562 DECL_SPECS is the sequence of declarator specifiers onto which to
24563 set the location.
24565 DS is the single declarator specifier to set which location is to
24566 be set onto the existing sequence of declarators.
24568 LOCATION is the location for the declarator specifier to
24569 consider. */
24571 static void
24572 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24573 cp_decl_spec ds, cp_token *token)
24575 gcc_assert (ds < ds_last);
24577 if (decl_specs == NULL)
24578 return;
24580 source_location location = token->location;
24582 if (decl_specs->locations[ds] == 0)
24584 decl_specs->locations[ds] = location;
24585 if (ds == ds_thread)
24586 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24588 else
24590 if (ds == ds_long)
24592 if (decl_specs->locations[ds_long_long] != 0)
24593 error_at (location,
24594 "%<long long long%> is too long for GCC");
24595 else
24597 decl_specs->locations[ds_long_long] = location;
24598 pedwarn_cxx98 (location,
24599 OPT_Wlong_long,
24600 "ISO C++ 1998 does not support %<long long%>");
24603 else if (ds == ds_thread)
24605 bool gnu = token_is__thread (token);
24606 if (gnu != decl_specs->gnu_thread_keyword_p)
24607 error_at (location,
24608 "both %<__thread%> and %<thread_local%> specified");
24609 else
24610 error_at (location, "duplicate %qD", token->u.value);
24612 else
24614 static const char *const decl_spec_names[] = {
24615 "signed",
24616 "unsigned",
24617 "short",
24618 "long",
24619 "const",
24620 "volatile",
24621 "restrict",
24622 "inline",
24623 "virtual",
24624 "explicit",
24625 "friend",
24626 "typedef",
24627 "using",
24628 "constexpr",
24629 "__complex"
24631 error_at (location,
24632 "duplicate %qs", decl_spec_names[ds]);
24637 /* Return true iff the declarator specifier DS is present in the
24638 sequence of declarator specifiers DECL_SPECS. */
24640 bool
24641 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24642 cp_decl_spec ds)
24644 gcc_assert (ds < ds_last);
24646 if (decl_specs == NULL)
24647 return false;
24649 return decl_specs->locations[ds] != 0;
24652 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24653 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24655 static bool
24656 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24658 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24661 /* Issue an error message indicating that TOKEN_DESC was expected.
24662 If KEYWORD is true, it indicated this function is called by
24663 cp_parser_require_keword and the required token can only be
24664 a indicated keyword. */
24666 static void
24667 cp_parser_required_error (cp_parser *parser,
24668 required_token token_desc,
24669 bool keyword)
24671 switch (token_desc)
24673 case RT_NEW:
24674 cp_parser_error (parser, "expected %<new%>");
24675 return;
24676 case RT_DELETE:
24677 cp_parser_error (parser, "expected %<delete%>");
24678 return;
24679 case RT_RETURN:
24680 cp_parser_error (parser, "expected %<return%>");
24681 return;
24682 case RT_WHILE:
24683 cp_parser_error (parser, "expected %<while%>");
24684 return;
24685 case RT_EXTERN:
24686 cp_parser_error (parser, "expected %<extern%>");
24687 return;
24688 case RT_STATIC_ASSERT:
24689 cp_parser_error (parser, "expected %<static_assert%>");
24690 return;
24691 case RT_DECLTYPE:
24692 cp_parser_error (parser, "expected %<decltype%>");
24693 return;
24694 case RT_OPERATOR:
24695 cp_parser_error (parser, "expected %<operator%>");
24696 return;
24697 case RT_CLASS:
24698 cp_parser_error (parser, "expected %<class%>");
24699 return;
24700 case RT_TEMPLATE:
24701 cp_parser_error (parser, "expected %<template%>");
24702 return;
24703 case RT_NAMESPACE:
24704 cp_parser_error (parser, "expected %<namespace%>");
24705 return;
24706 case RT_USING:
24707 cp_parser_error (parser, "expected %<using%>");
24708 return;
24709 case RT_ASM:
24710 cp_parser_error (parser, "expected %<asm%>");
24711 return;
24712 case RT_TRY:
24713 cp_parser_error (parser, "expected %<try%>");
24714 return;
24715 case RT_CATCH:
24716 cp_parser_error (parser, "expected %<catch%>");
24717 return;
24718 case RT_THROW:
24719 cp_parser_error (parser, "expected %<throw%>");
24720 return;
24721 case RT_LABEL:
24722 cp_parser_error (parser, "expected %<__label__%>");
24723 return;
24724 case RT_AT_TRY:
24725 cp_parser_error (parser, "expected %<@try%>");
24726 return;
24727 case RT_AT_SYNCHRONIZED:
24728 cp_parser_error (parser, "expected %<@synchronized%>");
24729 return;
24730 case RT_AT_THROW:
24731 cp_parser_error (parser, "expected %<@throw%>");
24732 return;
24733 case RT_TRANSACTION_ATOMIC:
24734 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24735 return;
24736 case RT_TRANSACTION_RELAXED:
24737 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24738 return;
24739 default:
24740 break;
24742 if (!keyword)
24744 switch (token_desc)
24746 case RT_SEMICOLON:
24747 cp_parser_error (parser, "expected %<;%>");
24748 return;
24749 case RT_OPEN_PAREN:
24750 cp_parser_error (parser, "expected %<(%>");
24751 return;
24752 case RT_CLOSE_BRACE:
24753 cp_parser_error (parser, "expected %<}%>");
24754 return;
24755 case RT_OPEN_BRACE:
24756 cp_parser_error (parser, "expected %<{%>");
24757 return;
24758 case RT_CLOSE_SQUARE:
24759 cp_parser_error (parser, "expected %<]%>");
24760 return;
24761 case RT_OPEN_SQUARE:
24762 cp_parser_error (parser, "expected %<[%>");
24763 return;
24764 case RT_COMMA:
24765 cp_parser_error (parser, "expected %<,%>");
24766 return;
24767 case RT_SCOPE:
24768 cp_parser_error (parser, "expected %<::%>");
24769 return;
24770 case RT_LESS:
24771 cp_parser_error (parser, "expected %<<%>");
24772 return;
24773 case RT_GREATER:
24774 cp_parser_error (parser, "expected %<>%>");
24775 return;
24776 case RT_EQ:
24777 cp_parser_error (parser, "expected %<=%>");
24778 return;
24779 case RT_ELLIPSIS:
24780 cp_parser_error (parser, "expected %<...%>");
24781 return;
24782 case RT_MULT:
24783 cp_parser_error (parser, "expected %<*%>");
24784 return;
24785 case RT_COMPL:
24786 cp_parser_error (parser, "expected %<~%>");
24787 return;
24788 case RT_COLON:
24789 cp_parser_error (parser, "expected %<:%>");
24790 return;
24791 case RT_COLON_SCOPE:
24792 cp_parser_error (parser, "expected %<:%> or %<::%>");
24793 return;
24794 case RT_CLOSE_PAREN:
24795 cp_parser_error (parser, "expected %<)%>");
24796 return;
24797 case RT_COMMA_CLOSE_PAREN:
24798 cp_parser_error (parser, "expected %<,%> or %<)%>");
24799 return;
24800 case RT_PRAGMA_EOL:
24801 cp_parser_error (parser, "expected end of line");
24802 return;
24803 case RT_NAME:
24804 cp_parser_error (parser, "expected identifier");
24805 return;
24806 case RT_SELECT:
24807 cp_parser_error (parser, "expected selection-statement");
24808 return;
24809 case RT_INTERATION:
24810 cp_parser_error (parser, "expected iteration-statement");
24811 return;
24812 case RT_JUMP:
24813 cp_parser_error (parser, "expected jump-statement");
24814 return;
24815 case RT_CLASS_KEY:
24816 cp_parser_error (parser, "expected class-key");
24817 return;
24818 case RT_CLASS_TYPENAME_TEMPLATE:
24819 cp_parser_error (parser,
24820 "expected %<class%>, %<typename%>, or %<template%>");
24821 return;
24822 default:
24823 gcc_unreachable ();
24826 else
24827 gcc_unreachable ();
24832 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24833 issue an error message indicating that TOKEN_DESC was expected.
24835 Returns the token consumed, if the token had the appropriate type.
24836 Otherwise, returns NULL. */
24838 static cp_token *
24839 cp_parser_require (cp_parser* parser,
24840 enum cpp_ttype type,
24841 required_token token_desc)
24843 if (cp_lexer_next_token_is (parser->lexer, type))
24844 return cp_lexer_consume_token (parser->lexer);
24845 else
24847 /* Output the MESSAGE -- unless we're parsing tentatively. */
24848 if (!cp_parser_simulate_error (parser))
24849 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24850 return NULL;
24854 /* An error message is produced if the next token is not '>'.
24855 All further tokens are skipped until the desired token is
24856 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24858 static void
24859 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24861 /* Current level of '< ... >'. */
24862 unsigned level = 0;
24863 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24864 unsigned nesting_depth = 0;
24866 /* Are we ready, yet? If not, issue error message. */
24867 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24868 return;
24870 /* Skip tokens until the desired token is found. */
24871 while (true)
24873 /* Peek at the next token. */
24874 switch (cp_lexer_peek_token (parser->lexer)->type)
24876 case CPP_LESS:
24877 if (!nesting_depth)
24878 ++level;
24879 break;
24881 case CPP_RSHIFT:
24882 if (cxx_dialect == cxx98)
24883 /* C++0x views the `>>' operator as two `>' tokens, but
24884 C++98 does not. */
24885 break;
24886 else if (!nesting_depth && level-- == 0)
24888 /* We've hit a `>>' where the first `>' closes the
24889 template argument list, and the second `>' is
24890 spurious. Just consume the `>>' and stop; we've
24891 already produced at least one error. */
24892 cp_lexer_consume_token (parser->lexer);
24893 return;
24895 /* Fall through for C++0x, so we handle the second `>' in
24896 the `>>'. */
24898 case CPP_GREATER:
24899 if (!nesting_depth && level-- == 0)
24901 /* We've reached the token we want, consume it and stop. */
24902 cp_lexer_consume_token (parser->lexer);
24903 return;
24905 break;
24907 case CPP_OPEN_PAREN:
24908 case CPP_OPEN_SQUARE:
24909 ++nesting_depth;
24910 break;
24912 case CPP_CLOSE_PAREN:
24913 case CPP_CLOSE_SQUARE:
24914 if (nesting_depth-- == 0)
24915 return;
24916 break;
24918 case CPP_EOF:
24919 case CPP_PRAGMA_EOL:
24920 case CPP_SEMICOLON:
24921 case CPP_OPEN_BRACE:
24922 case CPP_CLOSE_BRACE:
24923 /* The '>' was probably forgotten, don't look further. */
24924 return;
24926 default:
24927 break;
24930 /* Consume this token. */
24931 cp_lexer_consume_token (parser->lexer);
24935 /* If the next token is the indicated keyword, consume it. Otherwise,
24936 issue an error message indicating that TOKEN_DESC was expected.
24938 Returns the token consumed, if the token had the appropriate type.
24939 Otherwise, returns NULL. */
24941 static cp_token *
24942 cp_parser_require_keyword (cp_parser* parser,
24943 enum rid keyword,
24944 required_token token_desc)
24946 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24948 if (token && token->keyword != keyword)
24950 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24951 return NULL;
24954 return token;
24957 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24958 function-definition. */
24960 static bool
24961 cp_parser_token_starts_function_definition_p (cp_token* token)
24963 return (/* An ordinary function-body begins with an `{'. */
24964 token->type == CPP_OPEN_BRACE
24965 /* A ctor-initializer begins with a `:'. */
24966 || token->type == CPP_COLON
24967 /* A function-try-block begins with `try'. */
24968 || token->keyword == RID_TRY
24969 /* A function-transaction-block begins with `__transaction_atomic'
24970 or `__transaction_relaxed'. */
24971 || token->keyword == RID_TRANSACTION_ATOMIC
24972 || token->keyword == RID_TRANSACTION_RELAXED
24973 /* The named return value extension begins with `return'. */
24974 || token->keyword == RID_RETURN);
24977 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24978 definition. */
24980 static bool
24981 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24983 cp_token *token;
24985 token = cp_lexer_peek_token (parser->lexer);
24986 return (token->type == CPP_OPEN_BRACE
24987 || (token->type == CPP_COLON
24988 && !parser->colon_doesnt_start_class_def_p));
24991 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24992 C++0x) ending a template-argument. */
24994 static bool
24995 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24997 cp_token *token;
24999 token = cp_lexer_peek_token (parser->lexer);
25000 return (token->type == CPP_COMMA
25001 || token->type == CPP_GREATER
25002 || token->type == CPP_ELLIPSIS
25003 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25006 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25007 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25009 static bool
25010 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25011 size_t n)
25013 cp_token *token;
25015 token = cp_lexer_peek_nth_token (parser->lexer, n);
25016 if (token->type == CPP_LESS)
25017 return true;
25018 /* Check for the sequence `<::' in the original code. It would be lexed as
25019 `[:', where `[' is a digraph, and there is no whitespace before
25020 `:'. */
25021 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25023 cp_token *token2;
25024 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25025 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25026 return true;
25028 return false;
25031 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25032 or none_type otherwise. */
25034 static enum tag_types
25035 cp_parser_token_is_class_key (cp_token* token)
25037 switch (token->keyword)
25039 case RID_CLASS:
25040 return class_type;
25041 case RID_STRUCT:
25042 return record_type;
25043 case RID_UNION:
25044 return union_type;
25046 default:
25047 return none_type;
25051 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25052 or none_type otherwise or if the token is null. */
25054 static enum tag_types
25055 cp_parser_token_is_type_parameter_key (cp_token* token)
25057 if (!token)
25058 return none_type;
25060 switch (token->keyword)
25062 case RID_CLASS:
25063 return class_type;
25064 case RID_TYPENAME:
25065 return typename_type;
25067 default:
25068 return none_type;
25072 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25074 static void
25075 cp_parser_check_class_key (enum tag_types class_key, tree type)
25077 if (type == error_mark_node)
25078 return;
25079 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25081 if (permerror (input_location, "%qs tag used in naming %q#T",
25082 class_key == union_type ? "union"
25083 : class_key == record_type ? "struct" : "class",
25084 type))
25085 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25086 "%q#T was previously declared here", type);
25090 /* Issue an error message if DECL is redeclared with different
25091 access than its original declaration [class.access.spec/3].
25092 This applies to nested classes and nested class templates.
25093 [class.mem/1]. */
25095 static void
25096 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25098 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25099 return;
25101 if ((TREE_PRIVATE (decl)
25102 != (current_access_specifier == access_private_node))
25103 || (TREE_PROTECTED (decl)
25104 != (current_access_specifier == access_protected_node)))
25105 error_at (location, "%qD redeclared with different access", decl);
25108 /* Look for the `template' keyword, as a syntactic disambiguator.
25109 Return TRUE iff it is present, in which case it will be
25110 consumed. */
25112 static bool
25113 cp_parser_optional_template_keyword (cp_parser *parser)
25115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25117 /* In C++98 the `template' keyword can only be used within templates;
25118 outside templates the parser can always figure out what is a
25119 template and what is not. In C++11, per the resolution of DR 468,
25120 `template' is allowed in cases where it is not strictly necessary. */
25121 if (!processing_template_decl
25122 && pedantic && cxx_dialect == cxx98)
25124 cp_token *token = cp_lexer_peek_token (parser->lexer);
25125 pedwarn (token->location, OPT_Wpedantic,
25126 "in C++98 %<template%> (as a disambiguator) is only "
25127 "allowed within templates");
25128 /* If this part of the token stream is rescanned, the same
25129 error message would be generated. So, we purge the token
25130 from the stream. */
25131 cp_lexer_purge_token (parser->lexer);
25132 return false;
25134 else
25136 /* Consume the `template' keyword. */
25137 cp_lexer_consume_token (parser->lexer);
25138 return true;
25141 return false;
25144 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25145 set PARSER->SCOPE, and perform other related actions. */
25147 static void
25148 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25150 int i;
25151 struct tree_check *check_value;
25152 deferred_access_check *chk;
25153 vec<deferred_access_check, va_gc> *checks;
25155 /* Get the stored value. */
25156 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25157 /* Perform any access checks that were deferred. */
25158 checks = check_value->checks;
25159 if (checks)
25161 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25162 perform_or_defer_access_check (chk->binfo,
25163 chk->decl,
25164 chk->diag_decl, tf_warning_or_error);
25166 /* Set the scope from the stored value. */
25167 parser->scope = check_value->value;
25168 parser->qualifying_scope = check_value->qualifying_scope;
25169 parser->object_scope = NULL_TREE;
25172 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25173 encounter the end of a block before what we were looking for. */
25175 static bool
25176 cp_parser_cache_group (cp_parser *parser,
25177 enum cpp_ttype end,
25178 unsigned depth)
25180 while (true)
25182 cp_token *token = cp_lexer_peek_token (parser->lexer);
25184 /* Abort a parenthesized expression if we encounter a semicolon. */
25185 if ((end == CPP_CLOSE_PAREN || depth == 0)
25186 && token->type == CPP_SEMICOLON)
25187 return true;
25188 /* If we've reached the end of the file, stop. */
25189 if (token->type == CPP_EOF
25190 || (end != CPP_PRAGMA_EOL
25191 && token->type == CPP_PRAGMA_EOL))
25192 return true;
25193 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25194 /* We've hit the end of an enclosing block, so there's been some
25195 kind of syntax error. */
25196 return true;
25198 /* Consume the token. */
25199 cp_lexer_consume_token (parser->lexer);
25200 /* See if it starts a new group. */
25201 if (token->type == CPP_OPEN_BRACE)
25203 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25204 /* In theory this should probably check end == '}', but
25205 cp_parser_save_member_function_body needs it to exit
25206 after either '}' or ')' when called with ')'. */
25207 if (depth == 0)
25208 return false;
25210 else if (token->type == CPP_OPEN_PAREN)
25212 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25213 if (depth == 0 && end == CPP_CLOSE_PAREN)
25214 return false;
25216 else if (token->type == CPP_PRAGMA)
25217 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25218 else if (token->type == end)
25219 return false;
25223 /* Like above, for caching a default argument or NSDMI. Both of these are
25224 terminated by a non-nested comma, but it can be unclear whether or not a
25225 comma is nested in a template argument list unless we do more parsing.
25226 In order to handle this ambiguity, when we encounter a ',' after a '<'
25227 we try to parse what follows as a parameter-declaration-list (in the
25228 case of a default argument) or a member-declarator (in the case of an
25229 NSDMI). If that succeeds, then we stop caching. */
25231 static tree
25232 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25234 unsigned depth = 0;
25235 int maybe_template_id = 0;
25236 cp_token *first_token;
25237 cp_token *token;
25238 tree default_argument;
25240 /* Add tokens until we have processed the entire default
25241 argument. We add the range [first_token, token). */
25242 first_token = cp_lexer_peek_token (parser->lexer);
25243 if (first_token->type == CPP_OPEN_BRACE)
25245 /* For list-initialization, this is straightforward. */
25246 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25247 token = cp_lexer_peek_token (parser->lexer);
25249 else while (true)
25251 bool done = false;
25253 /* Peek at the next token. */
25254 token = cp_lexer_peek_token (parser->lexer);
25255 /* What we do depends on what token we have. */
25256 switch (token->type)
25258 /* In valid code, a default argument must be
25259 immediately followed by a `,' `)', or `...'. */
25260 case CPP_COMMA:
25261 if (depth == 0 && maybe_template_id)
25263 /* If we've seen a '<', we might be in a
25264 template-argument-list. Until Core issue 325 is
25265 resolved, we don't know how this situation ought
25266 to be handled, so try to DTRT. We check whether
25267 what comes after the comma is a valid parameter
25268 declaration list. If it is, then the comma ends
25269 the default argument; otherwise the default
25270 argument continues. */
25271 bool error = false;
25273 /* Set ITALP so cp_parser_parameter_declaration_list
25274 doesn't decide to commit to this parse. */
25275 bool saved_italp = parser->in_template_argument_list_p;
25276 parser->in_template_argument_list_p = true;
25278 cp_parser_parse_tentatively (parser);
25279 cp_lexer_consume_token (parser->lexer);
25281 if (nsdmi)
25283 int ctor_dtor_or_conv_p;
25284 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25285 &ctor_dtor_or_conv_p,
25286 /*parenthesized_p=*/NULL,
25287 /*member_p=*/true,
25288 /*friend_p=*/false);
25290 else
25292 begin_scope (sk_function_parms, NULL_TREE);
25293 cp_parser_parameter_declaration_list (parser, &error);
25294 pop_bindings_and_leave_scope ();
25296 if (!cp_parser_error_occurred (parser) && !error)
25297 done = true;
25298 cp_parser_abort_tentative_parse (parser);
25300 parser->in_template_argument_list_p = saved_italp;
25301 break;
25303 case CPP_CLOSE_PAREN:
25304 case CPP_ELLIPSIS:
25305 /* If we run into a non-nested `;', `}', or `]',
25306 then the code is invalid -- but the default
25307 argument is certainly over. */
25308 case CPP_SEMICOLON:
25309 case CPP_CLOSE_BRACE:
25310 case CPP_CLOSE_SQUARE:
25311 if (depth == 0
25312 /* Handle correctly int n = sizeof ... ( p ); */
25313 && token->type != CPP_ELLIPSIS)
25314 done = true;
25315 /* Update DEPTH, if necessary. */
25316 else if (token->type == CPP_CLOSE_PAREN
25317 || token->type == CPP_CLOSE_BRACE
25318 || token->type == CPP_CLOSE_SQUARE)
25319 --depth;
25320 break;
25322 case CPP_OPEN_PAREN:
25323 case CPP_OPEN_SQUARE:
25324 case CPP_OPEN_BRACE:
25325 ++depth;
25326 break;
25328 case CPP_LESS:
25329 if (depth == 0)
25330 /* This might be the comparison operator, or it might
25331 start a template argument list. */
25332 ++maybe_template_id;
25333 break;
25335 case CPP_RSHIFT:
25336 if (cxx_dialect == cxx98)
25337 break;
25338 /* Fall through for C++0x, which treats the `>>'
25339 operator like two `>' tokens in certain
25340 cases. */
25342 case CPP_GREATER:
25343 if (depth == 0)
25345 /* This might be an operator, or it might close a
25346 template argument list. But if a previous '<'
25347 started a template argument list, this will have
25348 closed it, so we can't be in one anymore. */
25349 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25350 if (maybe_template_id < 0)
25351 maybe_template_id = 0;
25353 break;
25355 /* If we run out of tokens, issue an error message. */
25356 case CPP_EOF:
25357 case CPP_PRAGMA_EOL:
25358 error_at (token->location, "file ends in default argument");
25359 done = true;
25360 break;
25362 case CPP_NAME:
25363 case CPP_SCOPE:
25364 /* In these cases, we should look for template-ids.
25365 For example, if the default argument is
25366 `X<int, double>()', we need to do name lookup to
25367 figure out whether or not `X' is a template; if
25368 so, the `,' does not end the default argument.
25370 That is not yet done. */
25371 break;
25373 default:
25374 break;
25377 /* If we've reached the end, stop. */
25378 if (done)
25379 break;
25381 /* Add the token to the token block. */
25382 token = cp_lexer_consume_token (parser->lexer);
25385 /* Create a DEFAULT_ARG to represent the unparsed default
25386 argument. */
25387 default_argument = make_node (DEFAULT_ARG);
25388 DEFARG_TOKENS (default_argument)
25389 = cp_token_cache_new (first_token, token);
25390 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25392 return default_argument;
25395 /* Begin parsing tentatively. We always save tokens while parsing
25396 tentatively so that if the tentative parsing fails we can restore the
25397 tokens. */
25399 static void
25400 cp_parser_parse_tentatively (cp_parser* parser)
25402 /* Enter a new parsing context. */
25403 parser->context = cp_parser_context_new (parser->context);
25404 /* Begin saving tokens. */
25405 cp_lexer_save_tokens (parser->lexer);
25406 /* In order to avoid repetitive access control error messages,
25407 access checks are queued up until we are no longer parsing
25408 tentatively. */
25409 push_deferring_access_checks (dk_deferred);
25412 /* Commit to the currently active tentative parse. */
25414 static void
25415 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25417 cp_parser_context *context;
25418 cp_lexer *lexer;
25420 /* Mark all of the levels as committed. */
25421 lexer = parser->lexer;
25422 for (context = parser->context; context->next; context = context->next)
25424 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25425 break;
25426 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25427 while (!cp_lexer_saving_tokens (lexer))
25428 lexer = lexer->next;
25429 cp_lexer_commit_tokens (lexer);
25433 /* Commit to the topmost currently active tentative parse.
25435 Note that this function shouldn't be called when there are
25436 irreversible side-effects while in a tentative state. For
25437 example, we shouldn't create a permanent entry in the symbol
25438 table, or issue an error message that might not apply if the
25439 tentative parse is aborted. */
25441 static void
25442 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25444 cp_parser_context *context = parser->context;
25445 cp_lexer *lexer = parser->lexer;
25447 if (context)
25449 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25450 return;
25451 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25453 while (!cp_lexer_saving_tokens (lexer))
25454 lexer = lexer->next;
25455 cp_lexer_commit_tokens (lexer);
25459 /* Abort the currently active tentative parse. All consumed tokens
25460 will be rolled back, and no diagnostics will be issued. */
25462 static void
25463 cp_parser_abort_tentative_parse (cp_parser* parser)
25465 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25466 || errorcount > 0);
25467 cp_parser_simulate_error (parser);
25468 /* Now, pretend that we want to see if the construct was
25469 successfully parsed. */
25470 cp_parser_parse_definitely (parser);
25473 /* Stop parsing tentatively. If a parse error has occurred, restore the
25474 token stream. Otherwise, commit to the tokens we have consumed.
25475 Returns true if no error occurred; false otherwise. */
25477 static bool
25478 cp_parser_parse_definitely (cp_parser* parser)
25480 bool error_occurred;
25481 cp_parser_context *context;
25483 /* Remember whether or not an error occurred, since we are about to
25484 destroy that information. */
25485 error_occurred = cp_parser_error_occurred (parser);
25486 /* Remove the topmost context from the stack. */
25487 context = parser->context;
25488 parser->context = context->next;
25489 /* If no parse errors occurred, commit to the tentative parse. */
25490 if (!error_occurred)
25492 /* Commit to the tokens read tentatively, unless that was
25493 already done. */
25494 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25495 cp_lexer_commit_tokens (parser->lexer);
25497 pop_to_parent_deferring_access_checks ();
25499 /* Otherwise, if errors occurred, roll back our state so that things
25500 are just as they were before we began the tentative parse. */
25501 else
25503 cp_lexer_rollback_tokens (parser->lexer);
25504 pop_deferring_access_checks ();
25506 /* Add the context to the front of the free list. */
25507 context->next = cp_parser_context_free_list;
25508 cp_parser_context_free_list = context;
25510 return !error_occurred;
25513 /* Returns true if we are parsing tentatively and are not committed to
25514 this tentative parse. */
25516 static bool
25517 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25519 return (cp_parser_parsing_tentatively (parser)
25520 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25523 /* Returns nonzero iff an error has occurred during the most recent
25524 tentative parse. */
25526 static bool
25527 cp_parser_error_occurred (cp_parser* parser)
25529 return (cp_parser_parsing_tentatively (parser)
25530 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25533 /* Returns nonzero if GNU extensions are allowed. */
25535 static bool
25536 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25538 return parser->allow_gnu_extensions_p;
25541 /* Objective-C++ Productions */
25544 /* Parse an Objective-C expression, which feeds into a primary-expression
25545 above.
25547 objc-expression:
25548 objc-message-expression
25549 objc-string-literal
25550 objc-encode-expression
25551 objc-protocol-expression
25552 objc-selector-expression
25554 Returns a tree representation of the expression. */
25556 static tree
25557 cp_parser_objc_expression (cp_parser* parser)
25559 /* Try to figure out what kind of declaration is present. */
25560 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25562 switch (kwd->type)
25564 case CPP_OPEN_SQUARE:
25565 return cp_parser_objc_message_expression (parser);
25567 case CPP_OBJC_STRING:
25568 kwd = cp_lexer_consume_token (parser->lexer);
25569 return objc_build_string_object (kwd->u.value);
25571 case CPP_KEYWORD:
25572 switch (kwd->keyword)
25574 case RID_AT_ENCODE:
25575 return cp_parser_objc_encode_expression (parser);
25577 case RID_AT_PROTOCOL:
25578 return cp_parser_objc_protocol_expression (parser);
25580 case RID_AT_SELECTOR:
25581 return cp_parser_objc_selector_expression (parser);
25583 default:
25584 break;
25586 default:
25587 error_at (kwd->location,
25588 "misplaced %<@%D%> Objective-C++ construct",
25589 kwd->u.value);
25590 cp_parser_skip_to_end_of_block_or_statement (parser);
25593 return error_mark_node;
25596 /* Parse an Objective-C message expression.
25598 objc-message-expression:
25599 [ objc-message-receiver objc-message-args ]
25601 Returns a representation of an Objective-C message. */
25603 static tree
25604 cp_parser_objc_message_expression (cp_parser* parser)
25606 tree receiver, messageargs;
25608 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25609 receiver = cp_parser_objc_message_receiver (parser);
25610 messageargs = cp_parser_objc_message_args (parser);
25611 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25613 return objc_build_message_expr (receiver, messageargs);
25616 /* Parse an objc-message-receiver.
25618 objc-message-receiver:
25619 expression
25620 simple-type-specifier
25622 Returns a representation of the type or expression. */
25624 static tree
25625 cp_parser_objc_message_receiver (cp_parser* parser)
25627 tree rcv;
25629 /* An Objective-C message receiver may be either (1) a type
25630 or (2) an expression. */
25631 cp_parser_parse_tentatively (parser);
25632 rcv = cp_parser_expression (parser);
25634 if (cp_parser_parse_definitely (parser))
25635 return rcv;
25637 rcv = cp_parser_simple_type_specifier (parser,
25638 /*decl_specs=*/NULL,
25639 CP_PARSER_FLAGS_NONE);
25641 return objc_get_class_reference (rcv);
25644 /* Parse the arguments and selectors comprising an Objective-C message.
25646 objc-message-args:
25647 objc-selector
25648 objc-selector-args
25649 objc-selector-args , objc-comma-args
25651 objc-selector-args:
25652 objc-selector [opt] : assignment-expression
25653 objc-selector-args objc-selector [opt] : assignment-expression
25655 objc-comma-args:
25656 assignment-expression
25657 objc-comma-args , assignment-expression
25659 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25660 selector arguments and TREE_VALUE containing a list of comma
25661 arguments. */
25663 static tree
25664 cp_parser_objc_message_args (cp_parser* parser)
25666 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25667 bool maybe_unary_selector_p = true;
25668 cp_token *token = cp_lexer_peek_token (parser->lexer);
25670 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25672 tree selector = NULL_TREE, arg;
25674 if (token->type != CPP_COLON)
25675 selector = cp_parser_objc_selector (parser);
25677 /* Detect if we have a unary selector. */
25678 if (maybe_unary_selector_p
25679 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25680 return build_tree_list (selector, NULL_TREE);
25682 maybe_unary_selector_p = false;
25683 cp_parser_require (parser, CPP_COLON, RT_COLON);
25684 arg = cp_parser_assignment_expression (parser);
25686 sel_args
25687 = chainon (sel_args,
25688 build_tree_list (selector, arg));
25690 token = cp_lexer_peek_token (parser->lexer);
25693 /* Handle non-selector arguments, if any. */
25694 while (token->type == CPP_COMMA)
25696 tree arg;
25698 cp_lexer_consume_token (parser->lexer);
25699 arg = cp_parser_assignment_expression (parser);
25701 addl_args
25702 = chainon (addl_args,
25703 build_tree_list (NULL_TREE, arg));
25705 token = cp_lexer_peek_token (parser->lexer);
25708 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25710 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25711 return build_tree_list (error_mark_node, error_mark_node);
25714 return build_tree_list (sel_args, addl_args);
25717 /* Parse an Objective-C encode expression.
25719 objc-encode-expression:
25720 @encode objc-typename
25722 Returns an encoded representation of the type argument. */
25724 static tree
25725 cp_parser_objc_encode_expression (cp_parser* parser)
25727 tree type;
25728 cp_token *token;
25730 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25731 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25732 token = cp_lexer_peek_token (parser->lexer);
25733 type = complete_type (cp_parser_type_id (parser));
25734 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25736 if (!type)
25738 error_at (token->location,
25739 "%<@encode%> must specify a type as an argument");
25740 return error_mark_node;
25743 /* This happens if we find @encode(T) (where T is a template
25744 typename or something dependent on a template typename) when
25745 parsing a template. In that case, we can't compile it
25746 immediately, but we rather create an AT_ENCODE_EXPR which will
25747 need to be instantiated when the template is used.
25749 if (dependent_type_p (type))
25751 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25752 TREE_READONLY (value) = 1;
25753 return value;
25756 return objc_build_encode_expr (type);
25759 /* Parse an Objective-C @defs expression. */
25761 static tree
25762 cp_parser_objc_defs_expression (cp_parser *parser)
25764 tree name;
25766 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25767 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25768 name = cp_parser_identifier (parser);
25769 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25771 return objc_get_class_ivars (name);
25774 /* Parse an Objective-C protocol expression.
25776 objc-protocol-expression:
25777 @protocol ( identifier )
25779 Returns a representation of the protocol expression. */
25781 static tree
25782 cp_parser_objc_protocol_expression (cp_parser* parser)
25784 tree proto;
25786 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25787 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25788 proto = cp_parser_identifier (parser);
25789 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25791 return objc_build_protocol_expr (proto);
25794 /* Parse an Objective-C selector expression.
25796 objc-selector-expression:
25797 @selector ( objc-method-signature )
25799 objc-method-signature:
25800 objc-selector
25801 objc-selector-seq
25803 objc-selector-seq:
25804 objc-selector :
25805 objc-selector-seq objc-selector :
25807 Returns a representation of the method selector. */
25809 static tree
25810 cp_parser_objc_selector_expression (cp_parser* parser)
25812 tree sel_seq = NULL_TREE;
25813 bool maybe_unary_selector_p = true;
25814 cp_token *token;
25815 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25817 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25818 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25819 token = cp_lexer_peek_token (parser->lexer);
25821 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25822 || token->type == CPP_SCOPE)
25824 tree selector = NULL_TREE;
25826 if (token->type != CPP_COLON
25827 || token->type == CPP_SCOPE)
25828 selector = cp_parser_objc_selector (parser);
25830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25831 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25833 /* Detect if we have a unary selector. */
25834 if (maybe_unary_selector_p)
25836 sel_seq = selector;
25837 goto finish_selector;
25839 else
25841 cp_parser_error (parser, "expected %<:%>");
25844 maybe_unary_selector_p = false;
25845 token = cp_lexer_consume_token (parser->lexer);
25847 if (token->type == CPP_SCOPE)
25849 sel_seq
25850 = chainon (sel_seq,
25851 build_tree_list (selector, NULL_TREE));
25852 sel_seq
25853 = chainon (sel_seq,
25854 build_tree_list (NULL_TREE, NULL_TREE));
25856 else
25857 sel_seq
25858 = chainon (sel_seq,
25859 build_tree_list (selector, NULL_TREE));
25861 token = cp_lexer_peek_token (parser->lexer);
25864 finish_selector:
25865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25867 return objc_build_selector_expr (loc, sel_seq);
25870 /* Parse a list of identifiers.
25872 objc-identifier-list:
25873 identifier
25874 objc-identifier-list , identifier
25876 Returns a TREE_LIST of identifier nodes. */
25878 static tree
25879 cp_parser_objc_identifier_list (cp_parser* parser)
25881 tree identifier;
25882 tree list;
25883 cp_token *sep;
25885 identifier = cp_parser_identifier (parser);
25886 if (identifier == error_mark_node)
25887 return error_mark_node;
25889 list = build_tree_list (NULL_TREE, identifier);
25890 sep = cp_lexer_peek_token (parser->lexer);
25892 while (sep->type == CPP_COMMA)
25894 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25895 identifier = cp_parser_identifier (parser);
25896 if (identifier == error_mark_node)
25897 return list;
25899 list = chainon (list, build_tree_list (NULL_TREE,
25900 identifier));
25901 sep = cp_lexer_peek_token (parser->lexer);
25904 return list;
25907 /* Parse an Objective-C alias declaration.
25909 objc-alias-declaration:
25910 @compatibility_alias identifier identifier ;
25912 This function registers the alias mapping with the Objective-C front end.
25913 It returns nothing. */
25915 static void
25916 cp_parser_objc_alias_declaration (cp_parser* parser)
25918 tree alias, orig;
25920 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25921 alias = cp_parser_identifier (parser);
25922 orig = cp_parser_identifier (parser);
25923 objc_declare_alias (alias, orig);
25924 cp_parser_consume_semicolon_at_end_of_statement (parser);
25927 /* Parse an Objective-C class forward-declaration.
25929 objc-class-declaration:
25930 @class objc-identifier-list ;
25932 The function registers the forward declarations with the Objective-C
25933 front end. It returns nothing. */
25935 static void
25936 cp_parser_objc_class_declaration (cp_parser* parser)
25938 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25939 while (true)
25941 tree id;
25943 id = cp_parser_identifier (parser);
25944 if (id == error_mark_node)
25945 break;
25947 objc_declare_class (id);
25949 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25950 cp_lexer_consume_token (parser->lexer);
25951 else
25952 break;
25954 cp_parser_consume_semicolon_at_end_of_statement (parser);
25957 /* Parse a list of Objective-C protocol references.
25959 objc-protocol-refs-opt:
25960 objc-protocol-refs [opt]
25962 objc-protocol-refs:
25963 < objc-identifier-list >
25965 Returns a TREE_LIST of identifiers, if any. */
25967 static tree
25968 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25970 tree protorefs = NULL_TREE;
25972 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25974 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25975 protorefs = cp_parser_objc_identifier_list (parser);
25976 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25979 return protorefs;
25982 /* Parse a Objective-C visibility specification. */
25984 static void
25985 cp_parser_objc_visibility_spec (cp_parser* parser)
25987 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25989 switch (vis->keyword)
25991 case RID_AT_PRIVATE:
25992 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25993 break;
25994 case RID_AT_PROTECTED:
25995 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25996 break;
25997 case RID_AT_PUBLIC:
25998 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25999 break;
26000 case RID_AT_PACKAGE:
26001 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26002 break;
26003 default:
26004 return;
26007 /* Eat '@private'/'@protected'/'@public'. */
26008 cp_lexer_consume_token (parser->lexer);
26011 /* Parse an Objective-C method type. Return 'true' if it is a class
26012 (+) method, and 'false' if it is an instance (-) method. */
26014 static inline bool
26015 cp_parser_objc_method_type (cp_parser* parser)
26017 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26018 return true;
26019 else
26020 return false;
26023 /* Parse an Objective-C protocol qualifier. */
26025 static tree
26026 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26028 tree quals = NULL_TREE, node;
26029 cp_token *token = cp_lexer_peek_token (parser->lexer);
26031 node = token->u.value;
26033 while (node && identifier_p (node)
26034 && (node == ridpointers [(int) RID_IN]
26035 || node == ridpointers [(int) RID_OUT]
26036 || node == ridpointers [(int) RID_INOUT]
26037 || node == ridpointers [(int) RID_BYCOPY]
26038 || node == ridpointers [(int) RID_BYREF]
26039 || node == ridpointers [(int) RID_ONEWAY]))
26041 quals = tree_cons (NULL_TREE, node, quals);
26042 cp_lexer_consume_token (parser->lexer);
26043 token = cp_lexer_peek_token (parser->lexer);
26044 node = token->u.value;
26047 return quals;
26050 /* Parse an Objective-C typename. */
26052 static tree
26053 cp_parser_objc_typename (cp_parser* parser)
26055 tree type_name = NULL_TREE;
26057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26059 tree proto_quals, cp_type = NULL_TREE;
26061 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26062 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26064 /* An ObjC type name may consist of just protocol qualifiers, in which
26065 case the type shall default to 'id'. */
26066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26068 cp_type = cp_parser_type_id (parser);
26070 /* If the type could not be parsed, an error has already
26071 been produced. For error recovery, behave as if it had
26072 not been specified, which will use the default type
26073 'id'. */
26074 if (cp_type == error_mark_node)
26076 cp_type = NULL_TREE;
26077 /* We need to skip to the closing parenthesis as
26078 cp_parser_type_id() does not seem to do it for
26079 us. */
26080 cp_parser_skip_to_closing_parenthesis (parser,
26081 /*recovering=*/true,
26082 /*or_comma=*/false,
26083 /*consume_paren=*/false);
26087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26088 type_name = build_tree_list (proto_quals, cp_type);
26091 return type_name;
26094 /* Check to see if TYPE refers to an Objective-C selector name. */
26096 static bool
26097 cp_parser_objc_selector_p (enum cpp_ttype type)
26099 return (type == CPP_NAME || type == CPP_KEYWORD
26100 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26101 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26102 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26103 || type == CPP_XOR || type == CPP_XOR_EQ);
26106 /* Parse an Objective-C selector. */
26108 static tree
26109 cp_parser_objc_selector (cp_parser* parser)
26111 cp_token *token = cp_lexer_consume_token (parser->lexer);
26113 if (!cp_parser_objc_selector_p (token->type))
26115 error_at (token->location, "invalid Objective-C++ selector name");
26116 return error_mark_node;
26119 /* C++ operator names are allowed to appear in ObjC selectors. */
26120 switch (token->type)
26122 case CPP_AND_AND: return get_identifier ("and");
26123 case CPP_AND_EQ: return get_identifier ("and_eq");
26124 case CPP_AND: return get_identifier ("bitand");
26125 case CPP_OR: return get_identifier ("bitor");
26126 case CPP_COMPL: return get_identifier ("compl");
26127 case CPP_NOT: return get_identifier ("not");
26128 case CPP_NOT_EQ: return get_identifier ("not_eq");
26129 case CPP_OR_OR: return get_identifier ("or");
26130 case CPP_OR_EQ: return get_identifier ("or_eq");
26131 case CPP_XOR: return get_identifier ("xor");
26132 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26133 default: return token->u.value;
26137 /* Parse an Objective-C params list. */
26139 static tree
26140 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26142 tree params = NULL_TREE;
26143 bool maybe_unary_selector_p = true;
26144 cp_token *token = cp_lexer_peek_token (parser->lexer);
26146 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26148 tree selector = NULL_TREE, type_name, identifier;
26149 tree parm_attr = NULL_TREE;
26151 if (token->keyword == RID_ATTRIBUTE)
26152 break;
26154 if (token->type != CPP_COLON)
26155 selector = cp_parser_objc_selector (parser);
26157 /* Detect if we have a unary selector. */
26158 if (maybe_unary_selector_p
26159 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26161 params = selector; /* Might be followed by attributes. */
26162 break;
26165 maybe_unary_selector_p = false;
26166 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26168 /* Something went quite wrong. There should be a colon
26169 here, but there is not. Stop parsing parameters. */
26170 break;
26172 type_name = cp_parser_objc_typename (parser);
26173 /* New ObjC allows attributes on parameters too. */
26174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26175 parm_attr = cp_parser_attributes_opt (parser);
26176 identifier = cp_parser_identifier (parser);
26178 params
26179 = chainon (params,
26180 objc_build_keyword_decl (selector,
26181 type_name,
26182 identifier,
26183 parm_attr));
26185 token = cp_lexer_peek_token (parser->lexer);
26188 if (params == NULL_TREE)
26190 cp_parser_error (parser, "objective-c++ method declaration is expected");
26191 return error_mark_node;
26194 /* We allow tail attributes for the method. */
26195 if (token->keyword == RID_ATTRIBUTE)
26197 *attributes = cp_parser_attributes_opt (parser);
26198 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26199 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26200 return params;
26201 cp_parser_error (parser,
26202 "method attributes must be specified at the end");
26203 return error_mark_node;
26206 if (params == NULL_TREE)
26208 cp_parser_error (parser, "objective-c++ method declaration is expected");
26209 return error_mark_node;
26211 return params;
26214 /* Parse the non-keyword Objective-C params. */
26216 static tree
26217 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26218 tree* attributes)
26220 tree params = make_node (TREE_LIST);
26221 cp_token *token = cp_lexer_peek_token (parser->lexer);
26222 *ellipsisp = false; /* Initially, assume no ellipsis. */
26224 while (token->type == CPP_COMMA)
26226 cp_parameter_declarator *parmdecl;
26227 tree parm;
26229 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26230 token = cp_lexer_peek_token (parser->lexer);
26232 if (token->type == CPP_ELLIPSIS)
26234 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26235 *ellipsisp = true;
26236 token = cp_lexer_peek_token (parser->lexer);
26237 break;
26240 /* TODO: parse attributes for tail parameters. */
26241 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26242 parm = grokdeclarator (parmdecl->declarator,
26243 &parmdecl->decl_specifiers,
26244 PARM, /*initialized=*/0,
26245 /*attrlist=*/NULL);
26247 chainon (params, build_tree_list (NULL_TREE, parm));
26248 token = cp_lexer_peek_token (parser->lexer);
26251 /* We allow tail attributes for the method. */
26252 if (token->keyword == RID_ATTRIBUTE)
26254 if (*attributes == NULL_TREE)
26256 *attributes = cp_parser_attributes_opt (parser);
26257 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26258 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26259 return params;
26261 else
26262 /* We have an error, but parse the attributes, so that we can
26263 carry on. */
26264 *attributes = cp_parser_attributes_opt (parser);
26266 cp_parser_error (parser,
26267 "method attributes must be specified at the end");
26268 return error_mark_node;
26271 return params;
26274 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26276 static void
26277 cp_parser_objc_interstitial_code (cp_parser* parser)
26279 cp_token *token = cp_lexer_peek_token (parser->lexer);
26281 /* If the next token is `extern' and the following token is a string
26282 literal, then we have a linkage specification. */
26283 if (token->keyword == RID_EXTERN
26284 && cp_parser_is_pure_string_literal
26285 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26286 cp_parser_linkage_specification (parser);
26287 /* Handle #pragma, if any. */
26288 else if (token->type == CPP_PRAGMA)
26289 cp_parser_pragma (parser, pragma_objc_icode);
26290 /* Allow stray semicolons. */
26291 else if (token->type == CPP_SEMICOLON)
26292 cp_lexer_consume_token (parser->lexer);
26293 /* Mark methods as optional or required, when building protocols. */
26294 else if (token->keyword == RID_AT_OPTIONAL)
26296 cp_lexer_consume_token (parser->lexer);
26297 objc_set_method_opt (true);
26299 else if (token->keyword == RID_AT_REQUIRED)
26301 cp_lexer_consume_token (parser->lexer);
26302 objc_set_method_opt (false);
26304 else if (token->keyword == RID_NAMESPACE)
26305 cp_parser_namespace_definition (parser);
26306 /* Other stray characters must generate errors. */
26307 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26309 cp_lexer_consume_token (parser->lexer);
26310 error ("stray %qs between Objective-C++ methods",
26311 token->type == CPP_OPEN_BRACE ? "{" : "}");
26313 /* Finally, try to parse a block-declaration, or a function-definition. */
26314 else
26315 cp_parser_block_declaration (parser, /*statement_p=*/false);
26318 /* Parse a method signature. */
26320 static tree
26321 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26323 tree rettype, kwdparms, optparms;
26324 bool ellipsis = false;
26325 bool is_class_method;
26327 is_class_method = cp_parser_objc_method_type (parser);
26328 rettype = cp_parser_objc_typename (parser);
26329 *attributes = NULL_TREE;
26330 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26331 if (kwdparms == error_mark_node)
26332 return error_mark_node;
26333 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26334 if (optparms == error_mark_node)
26335 return error_mark_node;
26337 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26340 static bool
26341 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26343 tree tattr;
26344 cp_lexer_save_tokens (parser->lexer);
26345 tattr = cp_parser_attributes_opt (parser);
26346 gcc_assert (tattr) ;
26348 /* If the attributes are followed by a method introducer, this is not allowed.
26349 Dump the attributes and flag the situation. */
26350 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26351 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26352 return true;
26354 /* Otherwise, the attributes introduce some interstitial code, possibly so
26355 rewind to allow that check. */
26356 cp_lexer_rollback_tokens (parser->lexer);
26357 return false;
26360 /* Parse an Objective-C method prototype list. */
26362 static void
26363 cp_parser_objc_method_prototype_list (cp_parser* parser)
26365 cp_token *token = cp_lexer_peek_token (parser->lexer);
26367 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26369 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26371 tree attributes, sig;
26372 bool is_class_method;
26373 if (token->type == CPP_PLUS)
26374 is_class_method = true;
26375 else
26376 is_class_method = false;
26377 sig = cp_parser_objc_method_signature (parser, &attributes);
26378 if (sig == error_mark_node)
26380 cp_parser_skip_to_end_of_block_or_statement (parser);
26381 token = cp_lexer_peek_token (parser->lexer);
26382 continue;
26384 objc_add_method_declaration (is_class_method, sig, attributes);
26385 cp_parser_consume_semicolon_at_end_of_statement (parser);
26387 else if (token->keyword == RID_AT_PROPERTY)
26388 cp_parser_objc_at_property_declaration (parser);
26389 else if (token->keyword == RID_ATTRIBUTE
26390 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26391 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26392 OPT_Wattributes,
26393 "prefix attributes are ignored for methods");
26394 else
26395 /* Allow for interspersed non-ObjC++ code. */
26396 cp_parser_objc_interstitial_code (parser);
26398 token = cp_lexer_peek_token (parser->lexer);
26401 if (token->type != CPP_EOF)
26402 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26403 else
26404 cp_parser_error (parser, "expected %<@end%>");
26406 objc_finish_interface ();
26409 /* Parse an Objective-C method definition list. */
26411 static void
26412 cp_parser_objc_method_definition_list (cp_parser* parser)
26414 cp_token *token = cp_lexer_peek_token (parser->lexer);
26416 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26418 tree meth;
26420 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26422 cp_token *ptk;
26423 tree sig, attribute;
26424 bool is_class_method;
26425 if (token->type == CPP_PLUS)
26426 is_class_method = true;
26427 else
26428 is_class_method = false;
26429 push_deferring_access_checks (dk_deferred);
26430 sig = cp_parser_objc_method_signature (parser, &attribute);
26431 if (sig == error_mark_node)
26433 cp_parser_skip_to_end_of_block_or_statement (parser);
26434 token = cp_lexer_peek_token (parser->lexer);
26435 continue;
26437 objc_start_method_definition (is_class_method, sig, attribute,
26438 NULL_TREE);
26440 /* For historical reasons, we accept an optional semicolon. */
26441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26442 cp_lexer_consume_token (parser->lexer);
26444 ptk = cp_lexer_peek_token (parser->lexer);
26445 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26446 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26448 perform_deferred_access_checks (tf_warning_or_error);
26449 stop_deferring_access_checks ();
26450 meth = cp_parser_function_definition_after_declarator (parser,
26451 false);
26452 pop_deferring_access_checks ();
26453 objc_finish_method_definition (meth);
26456 /* The following case will be removed once @synthesize is
26457 completely implemented. */
26458 else if (token->keyword == RID_AT_PROPERTY)
26459 cp_parser_objc_at_property_declaration (parser);
26460 else if (token->keyword == RID_AT_SYNTHESIZE)
26461 cp_parser_objc_at_synthesize_declaration (parser);
26462 else if (token->keyword == RID_AT_DYNAMIC)
26463 cp_parser_objc_at_dynamic_declaration (parser);
26464 else if (token->keyword == RID_ATTRIBUTE
26465 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26466 warning_at (token->location, OPT_Wattributes,
26467 "prefix attributes are ignored for methods");
26468 else
26469 /* Allow for interspersed non-ObjC++ code. */
26470 cp_parser_objc_interstitial_code (parser);
26472 token = cp_lexer_peek_token (parser->lexer);
26475 if (token->type != CPP_EOF)
26476 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26477 else
26478 cp_parser_error (parser, "expected %<@end%>");
26480 objc_finish_implementation ();
26483 /* Parse Objective-C ivars. */
26485 static void
26486 cp_parser_objc_class_ivars (cp_parser* parser)
26488 cp_token *token = cp_lexer_peek_token (parser->lexer);
26490 if (token->type != CPP_OPEN_BRACE)
26491 return; /* No ivars specified. */
26493 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26494 token = cp_lexer_peek_token (parser->lexer);
26496 while (token->type != CPP_CLOSE_BRACE
26497 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26499 cp_decl_specifier_seq declspecs;
26500 int decl_class_or_enum_p;
26501 tree prefix_attributes;
26503 cp_parser_objc_visibility_spec (parser);
26505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26506 break;
26508 cp_parser_decl_specifier_seq (parser,
26509 CP_PARSER_FLAGS_OPTIONAL,
26510 &declspecs,
26511 &decl_class_or_enum_p);
26513 /* auto, register, static, extern, mutable. */
26514 if (declspecs.storage_class != sc_none)
26516 cp_parser_error (parser, "invalid type for instance variable");
26517 declspecs.storage_class = sc_none;
26520 /* thread_local. */
26521 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26523 cp_parser_error (parser, "invalid type for instance variable");
26524 declspecs.locations[ds_thread] = 0;
26527 /* typedef. */
26528 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26530 cp_parser_error (parser, "invalid type for instance variable");
26531 declspecs.locations[ds_typedef] = 0;
26534 prefix_attributes = declspecs.attributes;
26535 declspecs.attributes = NULL_TREE;
26537 /* Keep going until we hit the `;' at the end of the
26538 declaration. */
26539 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26541 tree width = NULL_TREE, attributes, first_attribute, decl;
26542 cp_declarator *declarator = NULL;
26543 int ctor_dtor_or_conv_p;
26545 /* Check for a (possibly unnamed) bitfield declaration. */
26546 token = cp_lexer_peek_token (parser->lexer);
26547 if (token->type == CPP_COLON)
26548 goto eat_colon;
26550 if (token->type == CPP_NAME
26551 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26552 == CPP_COLON))
26554 /* Get the name of the bitfield. */
26555 declarator = make_id_declarator (NULL_TREE,
26556 cp_parser_identifier (parser),
26557 sfk_none);
26559 eat_colon:
26560 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26561 /* Get the width of the bitfield. */
26562 width
26563 = cp_parser_constant_expression (parser);
26565 else
26567 /* Parse the declarator. */
26568 declarator
26569 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26570 &ctor_dtor_or_conv_p,
26571 /*parenthesized_p=*/NULL,
26572 /*member_p=*/false,
26573 /*friend_p=*/false);
26576 /* Look for attributes that apply to the ivar. */
26577 attributes = cp_parser_attributes_opt (parser);
26578 /* Remember which attributes are prefix attributes and
26579 which are not. */
26580 first_attribute = attributes;
26581 /* Combine the attributes. */
26582 attributes = chainon (prefix_attributes, attributes);
26584 if (width)
26585 /* Create the bitfield declaration. */
26586 decl = grokbitfield (declarator, &declspecs,
26587 width,
26588 attributes);
26589 else
26590 decl = grokfield (declarator, &declspecs,
26591 NULL_TREE, /*init_const_expr_p=*/false,
26592 NULL_TREE, attributes);
26594 /* Add the instance variable. */
26595 if (decl != error_mark_node && decl != NULL_TREE)
26596 objc_add_instance_variable (decl);
26598 /* Reset PREFIX_ATTRIBUTES. */
26599 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26600 attributes = TREE_CHAIN (attributes);
26601 if (attributes)
26602 TREE_CHAIN (attributes) = NULL_TREE;
26604 token = cp_lexer_peek_token (parser->lexer);
26606 if (token->type == CPP_COMMA)
26608 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26609 continue;
26611 break;
26614 cp_parser_consume_semicolon_at_end_of_statement (parser);
26615 token = cp_lexer_peek_token (parser->lexer);
26618 if (token->keyword == RID_AT_END)
26619 cp_parser_error (parser, "expected %<}%>");
26621 /* Do not consume the RID_AT_END, so it will be read again as terminating
26622 the @interface of @implementation. */
26623 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26624 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26626 /* For historical reasons, we accept an optional semicolon. */
26627 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26628 cp_lexer_consume_token (parser->lexer);
26631 /* Parse an Objective-C protocol declaration. */
26633 static void
26634 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26636 tree proto, protorefs;
26637 cp_token *tok;
26639 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26642 tok = cp_lexer_peek_token (parser->lexer);
26643 error_at (tok->location, "identifier expected after %<@protocol%>");
26644 cp_parser_consume_semicolon_at_end_of_statement (parser);
26645 return;
26648 /* See if we have a forward declaration or a definition. */
26649 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26651 /* Try a forward declaration first. */
26652 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26654 while (true)
26656 tree id;
26658 id = cp_parser_identifier (parser);
26659 if (id == error_mark_node)
26660 break;
26662 objc_declare_protocol (id, attributes);
26664 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26665 cp_lexer_consume_token (parser->lexer);
26666 else
26667 break;
26669 cp_parser_consume_semicolon_at_end_of_statement (parser);
26672 /* Ok, we got a full-fledged definition (or at least should). */
26673 else
26675 proto = cp_parser_identifier (parser);
26676 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26677 objc_start_protocol (proto, protorefs, attributes);
26678 cp_parser_objc_method_prototype_list (parser);
26682 /* Parse an Objective-C superclass or category. */
26684 static void
26685 cp_parser_objc_superclass_or_category (cp_parser *parser,
26686 bool iface_p,
26687 tree *super,
26688 tree *categ, bool *is_class_extension)
26690 cp_token *next = cp_lexer_peek_token (parser->lexer);
26692 *super = *categ = NULL_TREE;
26693 *is_class_extension = false;
26694 if (next->type == CPP_COLON)
26696 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26697 *super = cp_parser_identifier (parser);
26699 else if (next->type == CPP_OPEN_PAREN)
26701 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26703 /* If there is no category name, and this is an @interface, we
26704 have a class extension. */
26705 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26707 *categ = NULL_TREE;
26708 *is_class_extension = true;
26710 else
26711 *categ = cp_parser_identifier (parser);
26713 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26717 /* Parse an Objective-C class interface. */
26719 static void
26720 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26722 tree name, super, categ, protos;
26723 bool is_class_extension;
26725 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26726 name = cp_parser_identifier (parser);
26727 if (name == error_mark_node)
26729 /* It's hard to recover because even if valid @interface stuff
26730 is to follow, we can't compile it (or validate it) if we
26731 don't even know which class it refers to. Let's assume this
26732 was a stray '@interface' token in the stream and skip it.
26734 return;
26736 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26737 &is_class_extension);
26738 protos = cp_parser_objc_protocol_refs_opt (parser);
26740 /* We have either a class or a category on our hands. */
26741 if (categ || is_class_extension)
26742 objc_start_category_interface (name, categ, protos, attributes);
26743 else
26745 objc_start_class_interface (name, super, protos, attributes);
26746 /* Handle instance variable declarations, if any. */
26747 cp_parser_objc_class_ivars (parser);
26748 objc_continue_interface ();
26751 cp_parser_objc_method_prototype_list (parser);
26754 /* Parse an Objective-C class implementation. */
26756 static void
26757 cp_parser_objc_class_implementation (cp_parser* parser)
26759 tree name, super, categ;
26760 bool is_class_extension;
26762 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26763 name = cp_parser_identifier (parser);
26764 if (name == error_mark_node)
26766 /* It's hard to recover because even if valid @implementation
26767 stuff is to follow, we can't compile it (or validate it) if
26768 we don't even know which class it refers to. Let's assume
26769 this was a stray '@implementation' token in the stream and
26770 skip it.
26772 return;
26774 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26775 &is_class_extension);
26777 /* We have either a class or a category on our hands. */
26778 if (categ)
26779 objc_start_category_implementation (name, categ);
26780 else
26782 objc_start_class_implementation (name, super);
26783 /* Handle instance variable declarations, if any. */
26784 cp_parser_objc_class_ivars (parser);
26785 objc_continue_implementation ();
26788 cp_parser_objc_method_definition_list (parser);
26791 /* Consume the @end token and finish off the implementation. */
26793 static void
26794 cp_parser_objc_end_implementation (cp_parser* parser)
26796 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26797 objc_finish_implementation ();
26800 /* Parse an Objective-C declaration. */
26802 static void
26803 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26805 /* Try to figure out what kind of declaration is present. */
26806 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26808 if (attributes)
26809 switch (kwd->keyword)
26811 case RID_AT_ALIAS:
26812 case RID_AT_CLASS:
26813 case RID_AT_END:
26814 error_at (kwd->location, "attributes may not be specified before"
26815 " the %<@%D%> Objective-C++ keyword",
26816 kwd->u.value);
26817 attributes = NULL;
26818 break;
26819 case RID_AT_IMPLEMENTATION:
26820 warning_at (kwd->location, OPT_Wattributes,
26821 "prefix attributes are ignored before %<@%D%>",
26822 kwd->u.value);
26823 attributes = NULL;
26824 default:
26825 break;
26828 switch (kwd->keyword)
26830 case RID_AT_ALIAS:
26831 cp_parser_objc_alias_declaration (parser);
26832 break;
26833 case RID_AT_CLASS:
26834 cp_parser_objc_class_declaration (parser);
26835 break;
26836 case RID_AT_PROTOCOL:
26837 cp_parser_objc_protocol_declaration (parser, attributes);
26838 break;
26839 case RID_AT_INTERFACE:
26840 cp_parser_objc_class_interface (parser, attributes);
26841 break;
26842 case RID_AT_IMPLEMENTATION:
26843 cp_parser_objc_class_implementation (parser);
26844 break;
26845 case RID_AT_END:
26846 cp_parser_objc_end_implementation (parser);
26847 break;
26848 default:
26849 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26850 kwd->u.value);
26851 cp_parser_skip_to_end_of_block_or_statement (parser);
26855 /* Parse an Objective-C try-catch-finally statement.
26857 objc-try-catch-finally-stmt:
26858 @try compound-statement objc-catch-clause-seq [opt]
26859 objc-finally-clause [opt]
26861 objc-catch-clause-seq:
26862 objc-catch-clause objc-catch-clause-seq [opt]
26864 objc-catch-clause:
26865 @catch ( objc-exception-declaration ) compound-statement
26867 objc-finally-clause:
26868 @finally compound-statement
26870 objc-exception-declaration:
26871 parameter-declaration
26872 '...'
26874 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26876 Returns NULL_TREE.
26878 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26879 for C. Keep them in sync. */
26881 static tree
26882 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26884 location_t location;
26885 tree stmt;
26887 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26888 location = cp_lexer_peek_token (parser->lexer)->location;
26889 objc_maybe_warn_exceptions (location);
26890 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26891 node, lest it get absorbed into the surrounding block. */
26892 stmt = push_stmt_list ();
26893 cp_parser_compound_statement (parser, NULL, false, false);
26894 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26896 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26898 cp_parameter_declarator *parm;
26899 tree parameter_declaration = error_mark_node;
26900 bool seen_open_paren = false;
26902 cp_lexer_consume_token (parser->lexer);
26903 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26904 seen_open_paren = true;
26905 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26907 /* We have "@catch (...)" (where the '...' are literally
26908 what is in the code). Skip the '...'.
26909 parameter_declaration is set to NULL_TREE, and
26910 objc_being_catch_clauses() knows that that means
26911 '...'. */
26912 cp_lexer_consume_token (parser->lexer);
26913 parameter_declaration = NULL_TREE;
26915 else
26917 /* We have "@catch (NSException *exception)" or something
26918 like that. Parse the parameter declaration. */
26919 parm = cp_parser_parameter_declaration (parser, false, NULL);
26920 if (parm == NULL)
26921 parameter_declaration = error_mark_node;
26922 else
26923 parameter_declaration = grokdeclarator (parm->declarator,
26924 &parm->decl_specifiers,
26925 PARM, /*initialized=*/0,
26926 /*attrlist=*/NULL);
26928 if (seen_open_paren)
26929 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26930 else
26932 /* If there was no open parenthesis, we are recovering from
26933 an error, and we are trying to figure out what mistake
26934 the user has made. */
26936 /* If there is an immediate closing parenthesis, the user
26937 probably forgot the opening one (ie, they typed "@catch
26938 NSException *e)". Parse the closing parenthesis and keep
26939 going. */
26940 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26941 cp_lexer_consume_token (parser->lexer);
26943 /* If these is no immediate closing parenthesis, the user
26944 probably doesn't know that parenthesis are required at
26945 all (ie, they typed "@catch NSException *e"). So, just
26946 forget about the closing parenthesis and keep going. */
26948 objc_begin_catch_clause (parameter_declaration);
26949 cp_parser_compound_statement (parser, NULL, false, false);
26950 objc_finish_catch_clause ();
26952 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26954 cp_lexer_consume_token (parser->lexer);
26955 location = cp_lexer_peek_token (parser->lexer)->location;
26956 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26957 node, lest it get absorbed into the surrounding block. */
26958 stmt = push_stmt_list ();
26959 cp_parser_compound_statement (parser, NULL, false, false);
26960 objc_build_finally_clause (location, pop_stmt_list (stmt));
26963 return objc_finish_try_stmt ();
26966 /* Parse an Objective-C synchronized statement.
26968 objc-synchronized-stmt:
26969 @synchronized ( expression ) compound-statement
26971 Returns NULL_TREE. */
26973 static tree
26974 cp_parser_objc_synchronized_statement (cp_parser *parser)
26976 location_t location;
26977 tree lock, stmt;
26979 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26981 location = cp_lexer_peek_token (parser->lexer)->location;
26982 objc_maybe_warn_exceptions (location);
26983 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26984 lock = cp_parser_expression (parser);
26985 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26987 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26988 node, lest it get absorbed into the surrounding block. */
26989 stmt = push_stmt_list ();
26990 cp_parser_compound_statement (parser, NULL, false, false);
26992 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26995 /* Parse an Objective-C throw statement.
26997 objc-throw-stmt:
26998 @throw assignment-expression [opt] ;
27000 Returns a constructed '@throw' statement. */
27002 static tree
27003 cp_parser_objc_throw_statement (cp_parser *parser)
27005 tree expr = NULL_TREE;
27006 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27008 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27011 expr = cp_parser_expression (parser);
27013 cp_parser_consume_semicolon_at_end_of_statement (parser);
27015 return objc_build_throw_stmt (loc, expr);
27018 /* Parse an Objective-C statement. */
27020 static tree
27021 cp_parser_objc_statement (cp_parser * parser)
27023 /* Try to figure out what kind of declaration is present. */
27024 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27026 switch (kwd->keyword)
27028 case RID_AT_TRY:
27029 return cp_parser_objc_try_catch_finally_statement (parser);
27030 case RID_AT_SYNCHRONIZED:
27031 return cp_parser_objc_synchronized_statement (parser);
27032 case RID_AT_THROW:
27033 return cp_parser_objc_throw_statement (parser);
27034 default:
27035 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27036 kwd->u.value);
27037 cp_parser_skip_to_end_of_block_or_statement (parser);
27040 return error_mark_node;
27043 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27044 look ahead to see if an objc keyword follows the attributes. This
27045 is to detect the use of prefix attributes on ObjC @interface and
27046 @protocol. */
27048 static bool
27049 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27051 cp_lexer_save_tokens (parser->lexer);
27052 *attrib = cp_parser_attributes_opt (parser);
27053 gcc_assert (*attrib);
27054 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27056 cp_lexer_commit_tokens (parser->lexer);
27057 return true;
27059 cp_lexer_rollback_tokens (parser->lexer);
27060 return false;
27063 /* This routine is a minimal replacement for
27064 c_parser_struct_declaration () used when parsing the list of
27065 types/names or ObjC++ properties. For example, when parsing the
27066 code
27068 @property (readonly) int a, b, c;
27070 this function is responsible for parsing "int a, int b, int c" and
27071 returning the declarations as CHAIN of DECLs.
27073 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27074 similar parsing. */
27075 static tree
27076 cp_parser_objc_struct_declaration (cp_parser *parser)
27078 tree decls = NULL_TREE;
27079 cp_decl_specifier_seq declspecs;
27080 int decl_class_or_enum_p;
27081 tree prefix_attributes;
27083 cp_parser_decl_specifier_seq (parser,
27084 CP_PARSER_FLAGS_NONE,
27085 &declspecs,
27086 &decl_class_or_enum_p);
27088 if (declspecs.type == error_mark_node)
27089 return error_mark_node;
27091 /* auto, register, static, extern, mutable. */
27092 if (declspecs.storage_class != sc_none)
27094 cp_parser_error (parser, "invalid type for property");
27095 declspecs.storage_class = sc_none;
27098 /* thread_local. */
27099 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27101 cp_parser_error (parser, "invalid type for property");
27102 declspecs.locations[ds_thread] = 0;
27105 /* typedef. */
27106 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27108 cp_parser_error (parser, "invalid type for property");
27109 declspecs.locations[ds_typedef] = 0;
27112 prefix_attributes = declspecs.attributes;
27113 declspecs.attributes = NULL_TREE;
27115 /* Keep going until we hit the `;' at the end of the declaration. */
27116 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27118 tree attributes, first_attribute, decl;
27119 cp_declarator *declarator;
27120 cp_token *token;
27122 /* Parse the declarator. */
27123 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27124 NULL, NULL, false, false);
27126 /* Look for attributes that apply to the ivar. */
27127 attributes = cp_parser_attributes_opt (parser);
27128 /* Remember which attributes are prefix attributes and
27129 which are not. */
27130 first_attribute = attributes;
27131 /* Combine the attributes. */
27132 attributes = chainon (prefix_attributes, attributes);
27134 decl = grokfield (declarator, &declspecs,
27135 NULL_TREE, /*init_const_expr_p=*/false,
27136 NULL_TREE, attributes);
27138 if (decl == error_mark_node || decl == NULL_TREE)
27139 return error_mark_node;
27141 /* Reset PREFIX_ATTRIBUTES. */
27142 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27143 attributes = TREE_CHAIN (attributes);
27144 if (attributes)
27145 TREE_CHAIN (attributes) = NULL_TREE;
27147 DECL_CHAIN (decl) = decls;
27148 decls = decl;
27150 token = cp_lexer_peek_token (parser->lexer);
27151 if (token->type == CPP_COMMA)
27153 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27154 continue;
27156 else
27157 break;
27159 return decls;
27162 /* Parse an Objective-C @property declaration. The syntax is:
27164 objc-property-declaration:
27165 '@property' objc-property-attributes[opt] struct-declaration ;
27167 objc-property-attributes:
27168 '(' objc-property-attribute-list ')'
27170 objc-property-attribute-list:
27171 objc-property-attribute
27172 objc-property-attribute-list, objc-property-attribute
27174 objc-property-attribute
27175 'getter' = identifier
27176 'setter' = identifier
27177 'readonly'
27178 'readwrite'
27179 'assign'
27180 'retain'
27181 'copy'
27182 'nonatomic'
27184 For example:
27185 @property NSString *name;
27186 @property (readonly) id object;
27187 @property (retain, nonatomic, getter=getTheName) id name;
27188 @property int a, b, c;
27190 PS: This function is identical to
27191 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27192 static void
27193 cp_parser_objc_at_property_declaration (cp_parser *parser)
27195 /* The following variables hold the attributes of the properties as
27196 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27197 seen. When we see an attribute, we set them to 'true' (if they
27198 are boolean properties) or to the identifier (if they have an
27199 argument, ie, for getter and setter). Note that here we only
27200 parse the list of attributes, check the syntax and accumulate the
27201 attributes that we find. objc_add_property_declaration() will
27202 then process the information. */
27203 bool property_assign = false;
27204 bool property_copy = false;
27205 tree property_getter_ident = NULL_TREE;
27206 bool property_nonatomic = false;
27207 bool property_readonly = false;
27208 bool property_readwrite = false;
27209 bool property_retain = false;
27210 tree property_setter_ident = NULL_TREE;
27212 /* 'properties' is the list of properties that we read. Usually a
27213 single one, but maybe more (eg, in "@property int a, b, c;" there
27214 are three). */
27215 tree properties;
27216 location_t loc;
27218 loc = cp_lexer_peek_token (parser->lexer)->location;
27220 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27222 /* Parse the optional attribute list... */
27223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27225 /* Eat the '('. */
27226 cp_lexer_consume_token (parser->lexer);
27228 while (true)
27230 bool syntax_error = false;
27231 cp_token *token = cp_lexer_peek_token (parser->lexer);
27232 enum rid keyword;
27234 if (token->type != CPP_NAME)
27236 cp_parser_error (parser, "expected identifier");
27237 break;
27239 keyword = C_RID_CODE (token->u.value);
27240 cp_lexer_consume_token (parser->lexer);
27241 switch (keyword)
27243 case RID_ASSIGN: property_assign = true; break;
27244 case RID_COPY: property_copy = true; break;
27245 case RID_NONATOMIC: property_nonatomic = true; break;
27246 case RID_READONLY: property_readonly = true; break;
27247 case RID_READWRITE: property_readwrite = true; break;
27248 case RID_RETAIN: property_retain = true; break;
27250 case RID_GETTER:
27251 case RID_SETTER:
27252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27254 if (keyword == RID_GETTER)
27255 cp_parser_error (parser,
27256 "missing %<=%> (after %<getter%> attribute)");
27257 else
27258 cp_parser_error (parser,
27259 "missing %<=%> (after %<setter%> attribute)");
27260 syntax_error = true;
27261 break;
27263 cp_lexer_consume_token (parser->lexer); /* eat the = */
27264 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27266 cp_parser_error (parser, "expected identifier");
27267 syntax_error = true;
27268 break;
27270 if (keyword == RID_SETTER)
27272 if (property_setter_ident != NULL_TREE)
27274 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27275 cp_lexer_consume_token (parser->lexer);
27277 else
27278 property_setter_ident = cp_parser_objc_selector (parser);
27279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27280 cp_parser_error (parser, "setter name must terminate with %<:%>");
27281 else
27282 cp_lexer_consume_token (parser->lexer);
27284 else
27286 if (property_getter_ident != NULL_TREE)
27288 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27289 cp_lexer_consume_token (parser->lexer);
27291 else
27292 property_getter_ident = cp_parser_objc_selector (parser);
27294 break;
27295 default:
27296 cp_parser_error (parser, "unknown property attribute");
27297 syntax_error = true;
27298 break;
27301 if (syntax_error)
27302 break;
27304 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27305 cp_lexer_consume_token (parser->lexer);
27306 else
27307 break;
27310 /* FIXME: "@property (setter, assign);" will generate a spurious
27311 "error: expected ‘)’ before ‘,’ token". This is because
27312 cp_parser_require, unlike the C counterpart, will produce an
27313 error even if we are in error recovery. */
27314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27316 cp_parser_skip_to_closing_parenthesis (parser,
27317 /*recovering=*/true,
27318 /*or_comma=*/false,
27319 /*consume_paren=*/true);
27323 /* ... and the property declaration(s). */
27324 properties = cp_parser_objc_struct_declaration (parser);
27326 if (properties == error_mark_node)
27328 cp_parser_skip_to_end_of_statement (parser);
27329 /* If the next token is now a `;', consume it. */
27330 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27331 cp_lexer_consume_token (parser->lexer);
27332 return;
27335 if (properties == NULL_TREE)
27336 cp_parser_error (parser, "expected identifier");
27337 else
27339 /* Comma-separated properties are chained together in
27340 reverse order; add them one by one. */
27341 properties = nreverse (properties);
27343 for (; properties; properties = TREE_CHAIN (properties))
27344 objc_add_property_declaration (loc, copy_node (properties),
27345 property_readonly, property_readwrite,
27346 property_assign, property_retain,
27347 property_copy, property_nonatomic,
27348 property_getter_ident, property_setter_ident);
27351 cp_parser_consume_semicolon_at_end_of_statement (parser);
27354 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27356 objc-synthesize-declaration:
27357 @synthesize objc-synthesize-identifier-list ;
27359 objc-synthesize-identifier-list:
27360 objc-synthesize-identifier
27361 objc-synthesize-identifier-list, objc-synthesize-identifier
27363 objc-synthesize-identifier
27364 identifier
27365 identifier = identifier
27367 For example:
27368 @synthesize MyProperty;
27369 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27371 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27372 for C. Keep them in sync.
27374 static void
27375 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27377 tree list = NULL_TREE;
27378 location_t loc;
27379 loc = cp_lexer_peek_token (parser->lexer)->location;
27381 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27382 while (true)
27384 tree property, ivar;
27385 property = cp_parser_identifier (parser);
27386 if (property == error_mark_node)
27388 cp_parser_consume_semicolon_at_end_of_statement (parser);
27389 return;
27391 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27393 cp_lexer_consume_token (parser->lexer);
27394 ivar = cp_parser_identifier (parser);
27395 if (ivar == error_mark_node)
27397 cp_parser_consume_semicolon_at_end_of_statement (parser);
27398 return;
27401 else
27402 ivar = NULL_TREE;
27403 list = chainon (list, build_tree_list (ivar, property));
27404 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27405 cp_lexer_consume_token (parser->lexer);
27406 else
27407 break;
27409 cp_parser_consume_semicolon_at_end_of_statement (parser);
27410 objc_add_synthesize_declaration (loc, list);
27413 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27415 objc-dynamic-declaration:
27416 @dynamic identifier-list ;
27418 For example:
27419 @dynamic MyProperty;
27420 @dynamic MyProperty, AnotherProperty;
27422 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27423 for C. Keep them in sync.
27425 static void
27426 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27428 tree list = NULL_TREE;
27429 location_t loc;
27430 loc = cp_lexer_peek_token (parser->lexer)->location;
27432 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27433 while (true)
27435 tree property;
27436 property = cp_parser_identifier (parser);
27437 if (property == error_mark_node)
27439 cp_parser_consume_semicolon_at_end_of_statement (parser);
27440 return;
27442 list = chainon (list, build_tree_list (NULL, property));
27443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27444 cp_lexer_consume_token (parser->lexer);
27445 else
27446 break;
27448 cp_parser_consume_semicolon_at_end_of_statement (parser);
27449 objc_add_dynamic_declaration (loc, list);
27453 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27455 /* Returns name of the next clause.
27456 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27457 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27458 returned and the token is consumed. */
27460 static pragma_omp_clause
27461 cp_parser_omp_clause_name (cp_parser *parser)
27463 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27466 result = PRAGMA_OMP_CLAUSE_IF;
27467 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27468 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27469 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27470 result = PRAGMA_OACC_CLAUSE_DELETE;
27471 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27472 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27473 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27474 result = PRAGMA_OMP_CLAUSE_FOR;
27475 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27478 const char *p = IDENTIFIER_POINTER (id);
27480 switch (p[0])
27482 case 'a':
27483 if (!strcmp ("aligned", p))
27484 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27485 else if (!strcmp ("async", p))
27486 result = PRAGMA_OACC_CLAUSE_ASYNC;
27487 break;
27488 case 'c':
27489 if (!strcmp ("collapse", p))
27490 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27491 else if (!strcmp ("copy", p))
27492 result = PRAGMA_OACC_CLAUSE_COPY;
27493 else if (!strcmp ("copyin", p))
27494 result = PRAGMA_OMP_CLAUSE_COPYIN;
27495 else if (!strcmp ("copyout", p))
27496 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27497 else if (!strcmp ("copyprivate", p))
27498 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27499 else if (!strcmp ("create", p))
27500 result = PRAGMA_OACC_CLAUSE_CREATE;
27501 break;
27502 case 'd':
27503 if (!strcmp ("depend", p))
27504 result = PRAGMA_OMP_CLAUSE_DEPEND;
27505 else if (!strcmp ("device", p))
27506 result = PRAGMA_OMP_CLAUSE_DEVICE;
27507 else if (!strcmp ("deviceptr", p))
27508 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27509 else if (!strcmp ("dist_schedule", p))
27510 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27511 break;
27512 case 'f':
27513 if (!strcmp ("final", p))
27514 result = PRAGMA_OMP_CLAUSE_FINAL;
27515 else if (!strcmp ("firstprivate", p))
27516 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27517 else if (!strcmp ("from", p))
27518 result = PRAGMA_OMP_CLAUSE_FROM;
27519 break;
27520 case 'h':
27521 if (!strcmp ("host", p))
27522 result = PRAGMA_OACC_CLAUSE_HOST;
27523 break;
27524 case 'i':
27525 if (!strcmp ("inbranch", p))
27526 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27527 break;
27528 case 'l':
27529 if (!strcmp ("lastprivate", p))
27530 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27531 else if (!strcmp ("linear", p))
27532 result = PRAGMA_OMP_CLAUSE_LINEAR;
27533 break;
27534 case 'm':
27535 if (!strcmp ("map", p))
27536 result = PRAGMA_OMP_CLAUSE_MAP;
27537 else if (!strcmp ("mergeable", p))
27538 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27539 else if (flag_cilkplus && !strcmp ("mask", p))
27540 result = PRAGMA_CILK_CLAUSE_MASK;
27541 break;
27542 case 'n':
27543 if (!strcmp ("notinbranch", p))
27544 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27545 else if (!strcmp ("nowait", p))
27546 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27547 else if (flag_cilkplus && !strcmp ("nomask", p))
27548 result = PRAGMA_CILK_CLAUSE_NOMASK;
27549 else if (!strcmp ("num_gangs", p))
27550 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27551 else if (!strcmp ("num_teams", p))
27552 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27553 else if (!strcmp ("num_threads", p))
27554 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27555 else if (!strcmp ("num_workers", p))
27556 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27557 break;
27558 case 'o':
27559 if (!strcmp ("ordered", p))
27560 result = PRAGMA_OMP_CLAUSE_ORDERED;
27561 break;
27562 case 'p':
27563 if (!strcmp ("parallel", p))
27564 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27565 else if (!strcmp ("present", p))
27566 result = PRAGMA_OACC_CLAUSE_PRESENT;
27567 else if (!strcmp ("present_or_copy", p)
27568 || !strcmp ("pcopy", p))
27569 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27570 else if (!strcmp ("present_or_copyin", p)
27571 || !strcmp ("pcopyin", p))
27572 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27573 else if (!strcmp ("present_or_copyout", p)
27574 || !strcmp ("pcopyout", p))
27575 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27576 else if (!strcmp ("present_or_create", p)
27577 || !strcmp ("pcreate", p))
27578 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27579 else if (!strcmp ("private", p))
27580 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27581 else if (!strcmp ("proc_bind", p))
27582 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27583 break;
27584 case 'r':
27585 if (!strcmp ("reduction", p))
27586 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27587 break;
27588 case 's':
27589 if (!strcmp ("safelen", p))
27590 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27591 else if (!strcmp ("schedule", p))
27592 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27593 else if (!strcmp ("sections", p))
27594 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27595 else if (!strcmp ("self", p))
27596 result = PRAGMA_OACC_CLAUSE_SELF;
27597 else if (!strcmp ("shared", p))
27598 result = PRAGMA_OMP_CLAUSE_SHARED;
27599 else if (!strcmp ("simdlen", p))
27600 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27601 break;
27602 case 't':
27603 if (!strcmp ("taskgroup", p))
27604 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27605 else if (!strcmp ("thread_limit", p))
27606 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27607 else if (!strcmp ("to", p))
27608 result = PRAGMA_OMP_CLAUSE_TO;
27609 break;
27610 case 'u':
27611 if (!strcmp ("uniform", p))
27612 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27613 else if (!strcmp ("untied", p))
27614 result = PRAGMA_OMP_CLAUSE_UNTIED;
27615 break;
27616 case 'v':
27617 if (!strcmp ("vector_length", p))
27618 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27619 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27620 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27621 break;
27622 case 'w':
27623 if (!strcmp ("wait", p))
27624 result = PRAGMA_OACC_CLAUSE_WAIT;
27625 break;
27629 if (result != PRAGMA_OMP_CLAUSE_NONE)
27630 cp_lexer_consume_token (parser->lexer);
27632 return result;
27635 /* Validate that a clause of the given type does not already exist. */
27637 static void
27638 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27639 const char *name, location_t location)
27641 tree c;
27643 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27644 if (OMP_CLAUSE_CODE (c) == code)
27646 error_at (location, "too many %qs clauses", name);
27647 break;
27651 /* OpenMP 2.5:
27652 variable-list:
27653 identifier
27654 variable-list , identifier
27656 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27657 colon). An opening parenthesis will have been consumed by the caller.
27659 If KIND is nonzero, create the appropriate node and install the decl
27660 in OMP_CLAUSE_DECL and add the node to the head of the list.
27662 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27663 return the list created.
27665 COLON can be NULL if only closing parenthesis should end the list,
27666 or pointer to bool which will receive false if the list is terminated
27667 by closing parenthesis or true if the list is terminated by colon. */
27669 static tree
27670 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27671 tree list, bool *colon)
27673 cp_token *token;
27674 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27675 if (colon)
27677 parser->colon_corrects_to_scope_p = false;
27678 *colon = false;
27680 while (1)
27682 tree name, decl;
27684 token = cp_lexer_peek_token (parser->lexer);
27685 name = cp_parser_id_expression (parser, /*template_p=*/false,
27686 /*check_dependency_p=*/true,
27687 /*template_p=*/NULL,
27688 /*declarator_p=*/false,
27689 /*optional_p=*/false);
27690 if (name == error_mark_node)
27691 goto skip_comma;
27693 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27694 if (decl == error_mark_node)
27695 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27696 token->location);
27697 else if (kind != 0)
27699 switch (kind)
27701 case OMP_CLAUSE__CACHE_:
27702 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27704 error_at (token->location, "expected %<[%>");
27705 decl = error_mark_node;
27706 break;
27708 /* FALL THROUGH. */
27709 case OMP_CLAUSE_MAP:
27710 case OMP_CLAUSE_FROM:
27711 case OMP_CLAUSE_TO:
27712 case OMP_CLAUSE_DEPEND:
27713 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27715 tree low_bound = NULL_TREE, length = NULL_TREE;
27717 parser->colon_corrects_to_scope_p = false;
27718 cp_lexer_consume_token (parser->lexer);
27719 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27720 low_bound = cp_parser_expression (parser);
27721 if (!colon)
27722 parser->colon_corrects_to_scope_p
27723 = saved_colon_corrects_to_scope_p;
27724 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27725 length = integer_one_node;
27726 else
27728 /* Look for `:'. */
27729 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27730 goto skip_comma;
27731 if (!cp_lexer_next_token_is (parser->lexer,
27732 CPP_CLOSE_SQUARE))
27733 length = cp_parser_expression (parser);
27735 /* Look for the closing `]'. */
27736 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27737 RT_CLOSE_SQUARE))
27738 goto skip_comma;
27740 if (kind == OMP_CLAUSE__CACHE_)
27742 if (TREE_CODE (low_bound) != INTEGER_CST
27743 && !TREE_READONLY (low_bound))
27745 error_at (token->location,
27746 "%qD is not a constant", low_bound);
27747 decl = error_mark_node;
27750 if (TREE_CODE (length) != INTEGER_CST
27751 && !TREE_READONLY (length))
27753 error_at (token->location,
27754 "%qD is not a constant", length);
27755 decl = error_mark_node;
27759 decl = tree_cons (low_bound, length, decl);
27761 break;
27762 default:
27763 break;
27766 tree u = build_omp_clause (token->location, kind);
27767 OMP_CLAUSE_DECL (u) = decl;
27768 OMP_CLAUSE_CHAIN (u) = list;
27769 list = u;
27771 else
27772 list = tree_cons (decl, NULL_TREE, list);
27774 get_comma:
27775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27776 break;
27777 cp_lexer_consume_token (parser->lexer);
27780 if (colon)
27781 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27783 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27785 *colon = true;
27786 cp_parser_require (parser, CPP_COLON, RT_COLON);
27787 return list;
27790 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27792 int ending;
27794 /* Try to resync to an unnested comma. Copied from
27795 cp_parser_parenthesized_expression_list. */
27796 skip_comma:
27797 if (colon)
27798 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27799 ending = cp_parser_skip_to_closing_parenthesis (parser,
27800 /*recovering=*/true,
27801 /*or_comma=*/true,
27802 /*consume_paren=*/true);
27803 if (ending < 0)
27804 goto get_comma;
27807 return list;
27810 /* Similarly, but expect leading and trailing parenthesis. This is a very
27811 common case for omp clauses. */
27813 static tree
27814 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27816 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27817 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27818 return list;
27821 /* OpenACC 2.0:
27822 copy ( variable-list )
27823 copyin ( variable-list )
27824 copyout ( variable-list )
27825 create ( variable-list )
27826 delete ( variable-list )
27827 present ( variable-list )
27828 present_or_copy ( variable-list )
27829 pcopy ( variable-list )
27830 present_or_copyin ( variable-list )
27831 pcopyin ( variable-list )
27832 present_or_copyout ( variable-list )
27833 pcopyout ( variable-list )
27834 present_or_create ( variable-list )
27835 pcreate ( variable-list ) */
27837 static tree
27838 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27839 tree list)
27841 enum omp_clause_map_kind kind;
27842 switch (c_kind)
27844 case PRAGMA_OACC_CLAUSE_COPY:
27845 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
27846 break;
27847 case PRAGMA_OACC_CLAUSE_COPYIN:
27848 kind = OMP_CLAUSE_MAP_FORCE_TO;
27849 break;
27850 case PRAGMA_OACC_CLAUSE_COPYOUT:
27851 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27852 break;
27853 case PRAGMA_OACC_CLAUSE_CREATE:
27854 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
27855 break;
27856 case PRAGMA_OACC_CLAUSE_DELETE:
27857 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
27858 break;
27859 case PRAGMA_OACC_CLAUSE_DEVICE:
27860 kind = OMP_CLAUSE_MAP_FORCE_TO;
27861 break;
27862 case PRAGMA_OACC_CLAUSE_HOST:
27863 case PRAGMA_OACC_CLAUSE_SELF:
27864 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27865 break;
27866 case PRAGMA_OACC_CLAUSE_PRESENT:
27867 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
27868 break;
27869 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27870 kind = OMP_CLAUSE_MAP_TOFROM;
27871 break;
27872 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27873 kind = OMP_CLAUSE_MAP_TO;
27874 break;
27875 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27876 kind = OMP_CLAUSE_MAP_FROM;
27877 break;
27878 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27879 kind = OMP_CLAUSE_MAP_ALLOC;
27880 break;
27881 default:
27882 gcc_unreachable ();
27884 tree nl, c;
27885 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27887 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27888 OMP_CLAUSE_MAP_KIND (c) = kind;
27890 return nl;
27893 /* OpenACC 2.0:
27894 deviceptr ( variable-list ) */
27896 static tree
27897 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27899 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27900 tree vars, t;
27902 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27903 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27904 variable-list must only allow for pointer variables. */
27905 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27906 for (t = vars; t; t = TREE_CHAIN (t))
27908 tree v = TREE_PURPOSE (t);
27910 /* FIXME diagnostics: Ideally we should keep individual
27911 locations for all the variables in the var list to make the
27912 following errors more precise. Perhaps
27913 c_parser_omp_var_list_parens should construct a list of
27914 locations to go along with the var list. */
27916 if (TREE_CODE (v) != VAR_DECL)
27917 error_at (loc, "%qD is not a variable", v);
27918 else if (TREE_TYPE (v) == error_mark_node)
27920 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27921 error_at (loc, "%qD is not a pointer variable", v);
27923 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27924 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
27925 OMP_CLAUSE_DECL (u) = v;
27926 OMP_CLAUSE_CHAIN (u) = list;
27927 list = u;
27930 return list;
27933 /* OpenACC:
27934 vector_length ( expression ) */
27936 static tree
27937 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
27939 tree t, c;
27940 location_t location = cp_lexer_peek_token (parser->lexer)->location;
27941 bool error = false;
27943 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27944 return list;
27946 t = cp_parser_condition (parser);
27947 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
27949 error_at (location, "expected positive integer expression");
27950 error = true;
27953 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27956 /*or_comma=*/false,
27957 /*consume_paren=*/true);
27958 return list;
27961 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
27962 location);
27964 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
27965 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
27966 OMP_CLAUSE_CHAIN (c) = list;
27967 list = c;
27969 return list;
27972 /* OpenACC 2.0
27973 Parse wait clause or directive parameters. */
27975 static tree
27976 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
27978 vec<tree, va_gc> *args;
27979 tree t, args_tree;
27981 args = cp_parser_parenthesized_expression_list (parser, non_attr,
27982 /*cast_p=*/false,
27983 /*allow_expansion_p=*/true,
27984 /*non_constant_p=*/NULL);
27986 if (args == NULL || args->length () == 0)
27988 cp_parser_error (parser, "expected integer expression before ')'");
27989 if (args != NULL)
27990 release_tree_vector (args);
27991 return list;
27994 args_tree = build_tree_list_vec (args);
27996 release_tree_vector (args);
27998 for (t = args_tree; t; t = TREE_CHAIN (t))
28000 tree targ = TREE_VALUE (t);
28002 if (targ != error_mark_node)
28004 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28005 error ("%<wait%> expression must be integral");
28006 else
28008 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28010 mark_rvalue_use (targ);
28011 OMP_CLAUSE_DECL (c) = targ;
28012 OMP_CLAUSE_CHAIN (c) = list;
28013 list = c;
28018 return list;
28021 /* OpenACC:
28022 wait ( int-expr-list ) */
28024 static tree
28025 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28027 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28029 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28030 return list;
28032 list = cp_parser_oacc_wait_list (parser, location, list);
28034 return list;
28037 /* OpenMP 3.0:
28038 collapse ( constant-expression ) */
28040 static tree
28041 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28043 tree c, num;
28044 location_t loc;
28045 HOST_WIDE_INT n;
28047 loc = cp_lexer_peek_token (parser->lexer)->location;
28048 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28049 return list;
28051 num = cp_parser_constant_expression (parser);
28053 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28055 /*or_comma=*/false,
28056 /*consume_paren=*/true);
28058 if (num == error_mark_node)
28059 return list;
28060 num = fold_non_dependent_expr (num);
28061 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28062 || !tree_fits_shwi_p (num)
28063 || (n = tree_to_shwi (num)) <= 0
28064 || (int) n != n)
28066 error_at (loc, "collapse argument needs positive constant integer expression");
28067 return list;
28070 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28071 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28072 OMP_CLAUSE_CHAIN (c) = list;
28073 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28075 return c;
28078 /* OpenMP 2.5:
28079 default ( shared | none ) */
28081 static tree
28082 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28084 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28085 tree c;
28087 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28088 return list;
28089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28091 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28092 const char *p = IDENTIFIER_POINTER (id);
28094 switch (p[0])
28096 case 'n':
28097 if (strcmp ("none", p) != 0)
28098 goto invalid_kind;
28099 kind = OMP_CLAUSE_DEFAULT_NONE;
28100 break;
28102 case 's':
28103 if (strcmp ("shared", p) != 0)
28104 goto invalid_kind;
28105 kind = OMP_CLAUSE_DEFAULT_SHARED;
28106 break;
28108 default:
28109 goto invalid_kind;
28112 cp_lexer_consume_token (parser->lexer);
28114 else
28116 invalid_kind:
28117 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28120 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28122 /*or_comma=*/false,
28123 /*consume_paren=*/true);
28125 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28126 return list;
28128 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28129 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28130 OMP_CLAUSE_CHAIN (c) = list;
28131 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28133 return c;
28136 /* OpenMP 3.1:
28137 final ( expression ) */
28139 static tree
28140 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28142 tree t, c;
28144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28145 return list;
28147 t = cp_parser_condition (parser);
28149 if (t == error_mark_node
28150 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28152 /*or_comma=*/false,
28153 /*consume_paren=*/true);
28155 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28157 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28158 OMP_CLAUSE_FINAL_EXPR (c) = t;
28159 OMP_CLAUSE_CHAIN (c) = list;
28161 return c;
28164 /* OpenMP 2.5:
28165 if ( expression ) */
28167 static tree
28168 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28170 tree t, c;
28172 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28173 return list;
28175 t = cp_parser_condition (parser);
28177 if (t == error_mark_node
28178 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28180 /*or_comma=*/false,
28181 /*consume_paren=*/true);
28183 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28185 c = build_omp_clause (location, OMP_CLAUSE_IF);
28186 OMP_CLAUSE_IF_EXPR (c) = t;
28187 OMP_CLAUSE_CHAIN (c) = list;
28189 return c;
28192 /* OpenMP 3.1:
28193 mergeable */
28195 static tree
28196 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28197 tree list, location_t location)
28199 tree c;
28201 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28202 location);
28204 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28205 OMP_CLAUSE_CHAIN (c) = list;
28206 return c;
28209 /* OpenMP 2.5:
28210 nowait */
28212 static tree
28213 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28214 tree list, location_t location)
28216 tree c;
28218 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28220 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28221 OMP_CLAUSE_CHAIN (c) = list;
28222 return c;
28225 /* OpenACC:
28226 num_gangs ( expression ) */
28228 static tree
28229 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28231 tree t, c;
28232 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28235 return list;
28237 t = cp_parser_condition (parser);
28239 if (t == error_mark_node
28240 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28242 /*or_comma=*/false,
28243 /*consume_paren=*/true);
28245 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28247 error_at (location, "expected positive integer expression");
28248 return list;
28251 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28253 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28254 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28255 OMP_CLAUSE_CHAIN (c) = list;
28256 list = c;
28258 return list;
28261 /* OpenMP 2.5:
28262 num_threads ( expression ) */
28264 static tree
28265 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28266 location_t location)
28268 tree t, c;
28270 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28271 return list;
28273 t = cp_parser_expression (parser);
28275 if (t == error_mark_node
28276 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28278 /*or_comma=*/false,
28279 /*consume_paren=*/true);
28281 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28282 "num_threads", location);
28284 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28285 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28286 OMP_CLAUSE_CHAIN (c) = list;
28288 return c;
28291 /* OpenACC:
28292 num_workers ( expression ) */
28294 static tree
28295 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28297 tree t, c;
28298 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28300 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28301 return list;
28303 t = cp_parser_condition (parser);
28305 if (t == error_mark_node
28306 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28307 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28308 /*or_comma=*/false,
28309 /*consume_paren=*/true);
28311 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28313 error_at (location, "expected positive integer expression");
28314 return list;
28317 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28318 location);
28320 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28321 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28322 OMP_CLAUSE_CHAIN (c) = list;
28323 list = c;
28325 return list;
28328 /* OpenMP 2.5:
28329 ordered */
28331 static tree
28332 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28333 tree list, location_t location)
28335 tree c;
28337 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28338 "ordered", location);
28340 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28341 OMP_CLAUSE_CHAIN (c) = list;
28342 return c;
28345 /* OpenMP 2.5:
28346 reduction ( reduction-operator : variable-list )
28348 reduction-operator:
28349 One of: + * - & ^ | && ||
28351 OpenMP 3.1:
28353 reduction-operator:
28354 One of: + * - & ^ | && || min max
28356 OpenMP 4.0:
28358 reduction-operator:
28359 One of: + * - & ^ | && ||
28360 id-expression */
28362 static tree
28363 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28365 enum tree_code code = ERROR_MARK;
28366 tree nlist, c, id = NULL_TREE;
28368 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28369 return list;
28371 switch (cp_lexer_peek_token (parser->lexer)->type)
28373 case CPP_PLUS: code = PLUS_EXPR; break;
28374 case CPP_MULT: code = MULT_EXPR; break;
28375 case CPP_MINUS: code = MINUS_EXPR; break;
28376 case CPP_AND: code = BIT_AND_EXPR; break;
28377 case CPP_XOR: code = BIT_XOR_EXPR; break;
28378 case CPP_OR: code = BIT_IOR_EXPR; break;
28379 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28380 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28381 default: break;
28384 if (code != ERROR_MARK)
28385 cp_lexer_consume_token (parser->lexer);
28386 else
28388 bool saved_colon_corrects_to_scope_p;
28389 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28390 parser->colon_corrects_to_scope_p = false;
28391 id = cp_parser_id_expression (parser, /*template_p=*/false,
28392 /*check_dependency_p=*/true,
28393 /*template_p=*/NULL,
28394 /*declarator_p=*/false,
28395 /*optional_p=*/false);
28396 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28397 if (identifier_p (id))
28399 const char *p = IDENTIFIER_POINTER (id);
28401 if (strcmp (p, "min") == 0)
28402 code = MIN_EXPR;
28403 else if (strcmp (p, "max") == 0)
28404 code = MAX_EXPR;
28405 else if (id == ansi_opname (PLUS_EXPR))
28406 code = PLUS_EXPR;
28407 else if (id == ansi_opname (MULT_EXPR))
28408 code = MULT_EXPR;
28409 else if (id == ansi_opname (MINUS_EXPR))
28410 code = MINUS_EXPR;
28411 else if (id == ansi_opname (BIT_AND_EXPR))
28412 code = BIT_AND_EXPR;
28413 else if (id == ansi_opname (BIT_IOR_EXPR))
28414 code = BIT_IOR_EXPR;
28415 else if (id == ansi_opname (BIT_XOR_EXPR))
28416 code = BIT_XOR_EXPR;
28417 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28418 code = TRUTH_ANDIF_EXPR;
28419 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28420 code = TRUTH_ORIF_EXPR;
28421 id = omp_reduction_id (code, id, NULL_TREE);
28422 tree scope = parser->scope;
28423 if (scope)
28424 id = build_qualified_name (NULL_TREE, scope, id, false);
28425 parser->scope = NULL_TREE;
28426 parser->qualifying_scope = NULL_TREE;
28427 parser->object_scope = NULL_TREE;
28429 else
28431 error ("invalid reduction-identifier");
28432 resync_fail:
28433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28434 /*or_comma=*/false,
28435 /*consume_paren=*/true);
28436 return list;
28440 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28441 goto resync_fail;
28443 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28444 NULL);
28445 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28447 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28448 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28451 return nlist;
28454 /* OpenMP 2.5:
28455 schedule ( schedule-kind )
28456 schedule ( schedule-kind , expression )
28458 schedule-kind:
28459 static | dynamic | guided | runtime | auto */
28461 static tree
28462 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28464 tree c, t;
28466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28467 return list;
28469 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28474 const char *p = IDENTIFIER_POINTER (id);
28476 switch (p[0])
28478 case 'd':
28479 if (strcmp ("dynamic", p) != 0)
28480 goto invalid_kind;
28481 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28482 break;
28484 case 'g':
28485 if (strcmp ("guided", p) != 0)
28486 goto invalid_kind;
28487 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28488 break;
28490 case 'r':
28491 if (strcmp ("runtime", p) != 0)
28492 goto invalid_kind;
28493 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28494 break;
28496 default:
28497 goto invalid_kind;
28500 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28501 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28502 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28503 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28504 else
28505 goto invalid_kind;
28506 cp_lexer_consume_token (parser->lexer);
28508 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28510 cp_token *token;
28511 cp_lexer_consume_token (parser->lexer);
28513 token = cp_lexer_peek_token (parser->lexer);
28514 t = cp_parser_assignment_expression (parser);
28516 if (t == error_mark_node)
28517 goto resync_fail;
28518 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28519 error_at (token->location, "schedule %<runtime%> does not take "
28520 "a %<chunk_size%> parameter");
28521 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28522 error_at (token->location, "schedule %<auto%> does not take "
28523 "a %<chunk_size%> parameter");
28524 else
28525 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28528 goto resync_fail;
28530 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28531 goto resync_fail;
28533 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28534 OMP_CLAUSE_CHAIN (c) = list;
28535 return c;
28537 invalid_kind:
28538 cp_parser_error (parser, "invalid schedule kind");
28539 resync_fail:
28540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28541 /*or_comma=*/false,
28542 /*consume_paren=*/true);
28543 return list;
28546 /* OpenMP 3.0:
28547 untied */
28549 static tree
28550 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28551 tree list, location_t location)
28553 tree c;
28555 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28557 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28558 OMP_CLAUSE_CHAIN (c) = list;
28559 return c;
28562 /* OpenMP 4.0:
28563 inbranch
28564 notinbranch */
28566 static tree
28567 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28568 tree list, location_t location)
28570 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28571 tree c = build_omp_clause (location, code);
28572 OMP_CLAUSE_CHAIN (c) = list;
28573 return c;
28576 /* OpenMP 4.0:
28577 parallel
28579 sections
28580 taskgroup */
28582 static tree
28583 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28584 enum omp_clause_code code,
28585 tree list, location_t location)
28587 tree c = build_omp_clause (location, code);
28588 OMP_CLAUSE_CHAIN (c) = list;
28589 return c;
28592 /* OpenMP 4.0:
28593 num_teams ( expression ) */
28595 static tree
28596 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28597 location_t location)
28599 tree t, c;
28601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28602 return list;
28604 t = cp_parser_expression (parser);
28606 if (t == error_mark_node
28607 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28608 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28609 /*or_comma=*/false,
28610 /*consume_paren=*/true);
28612 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28613 "num_teams", location);
28615 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28616 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28617 OMP_CLAUSE_CHAIN (c) = list;
28619 return c;
28622 /* OpenMP 4.0:
28623 thread_limit ( expression ) */
28625 static tree
28626 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28627 location_t location)
28629 tree t, c;
28631 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28632 return list;
28634 t = cp_parser_expression (parser);
28636 if (t == error_mark_node
28637 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28638 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28639 /*or_comma=*/false,
28640 /*consume_paren=*/true);
28642 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28643 "thread_limit", location);
28645 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28646 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28647 OMP_CLAUSE_CHAIN (c) = list;
28649 return c;
28652 /* OpenMP 4.0:
28653 aligned ( variable-list )
28654 aligned ( variable-list : constant-expression ) */
28656 static tree
28657 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28659 tree nlist, c, alignment = NULL_TREE;
28660 bool colon;
28662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28663 return list;
28665 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28666 &colon);
28668 if (colon)
28670 alignment = cp_parser_constant_expression (parser);
28672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28674 /*or_comma=*/false,
28675 /*consume_paren=*/true);
28677 if (alignment == error_mark_node)
28678 alignment = NULL_TREE;
28681 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28682 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28684 return nlist;
28687 /* OpenMP 4.0:
28688 linear ( variable-list )
28689 linear ( variable-list : expression ) */
28691 static tree
28692 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28693 bool is_cilk_simd_fn)
28695 tree nlist, c, step = integer_one_node;
28696 bool colon;
28698 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28699 return list;
28701 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28702 &colon);
28704 if (colon)
28706 step = cp_parser_expression (parser);
28708 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28710 sorry ("using parameters for %<linear%> step is not supported yet");
28711 step = integer_one_node;
28713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28715 /*or_comma=*/false,
28716 /*consume_paren=*/true);
28718 if (step == error_mark_node)
28719 return list;
28722 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28723 OMP_CLAUSE_LINEAR_STEP (c) = step;
28725 return nlist;
28728 /* OpenMP 4.0:
28729 safelen ( constant-expression ) */
28731 static tree
28732 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28733 location_t location)
28735 tree t, c;
28737 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28738 return list;
28740 t = cp_parser_constant_expression (parser);
28742 if (t == error_mark_node
28743 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28745 /*or_comma=*/false,
28746 /*consume_paren=*/true);
28748 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28750 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28751 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28752 OMP_CLAUSE_CHAIN (c) = list;
28754 return c;
28757 /* OpenMP 4.0:
28758 simdlen ( constant-expression ) */
28760 static tree
28761 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28762 location_t location)
28764 tree t, c;
28766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28767 return list;
28769 t = cp_parser_constant_expression (parser);
28771 if (t == error_mark_node
28772 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28774 /*or_comma=*/false,
28775 /*consume_paren=*/true);
28777 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28779 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28780 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28781 OMP_CLAUSE_CHAIN (c) = list;
28783 return c;
28786 /* OpenMP 4.0:
28787 depend ( depend-kind : variable-list )
28789 depend-kind:
28790 in | out | inout */
28792 static tree
28793 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28795 tree nlist, c;
28796 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28798 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28799 return list;
28801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28804 const char *p = IDENTIFIER_POINTER (id);
28806 if (strcmp ("in", p) == 0)
28807 kind = OMP_CLAUSE_DEPEND_IN;
28808 else if (strcmp ("inout", p) == 0)
28809 kind = OMP_CLAUSE_DEPEND_INOUT;
28810 else if (strcmp ("out", p) == 0)
28811 kind = OMP_CLAUSE_DEPEND_OUT;
28812 else
28813 goto invalid_kind;
28815 else
28816 goto invalid_kind;
28818 cp_lexer_consume_token (parser->lexer);
28819 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28820 goto resync_fail;
28822 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28823 NULL);
28825 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28826 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28828 return nlist;
28830 invalid_kind:
28831 cp_parser_error (parser, "invalid depend kind");
28832 resync_fail:
28833 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28834 /*or_comma=*/false,
28835 /*consume_paren=*/true);
28836 return list;
28839 /* OpenMP 4.0:
28840 map ( map-kind : variable-list )
28841 map ( variable-list )
28843 map-kind:
28844 alloc | to | from | tofrom */
28846 static tree
28847 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28849 tree nlist, c;
28850 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28852 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28853 return list;
28855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28856 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28859 const char *p = IDENTIFIER_POINTER (id);
28861 if (strcmp ("alloc", p) == 0)
28862 kind = OMP_CLAUSE_MAP_ALLOC;
28863 else if (strcmp ("to", p) == 0)
28864 kind = OMP_CLAUSE_MAP_TO;
28865 else if (strcmp ("from", p) == 0)
28866 kind = OMP_CLAUSE_MAP_FROM;
28867 else if (strcmp ("tofrom", p) == 0)
28868 kind = OMP_CLAUSE_MAP_TOFROM;
28869 else
28871 cp_parser_error (parser, "invalid map kind");
28872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28873 /*or_comma=*/false,
28874 /*consume_paren=*/true);
28875 return list;
28877 cp_lexer_consume_token (parser->lexer);
28878 cp_lexer_consume_token (parser->lexer);
28881 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28882 NULL);
28884 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28885 OMP_CLAUSE_MAP_KIND (c) = kind;
28887 return nlist;
28890 /* OpenMP 4.0:
28891 device ( expression ) */
28893 static tree
28894 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28895 location_t location)
28897 tree t, c;
28899 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28900 return list;
28902 t = cp_parser_expression (parser);
28904 if (t == error_mark_node
28905 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28907 /*or_comma=*/false,
28908 /*consume_paren=*/true);
28910 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28911 "device", location);
28913 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28914 OMP_CLAUSE_DEVICE_ID (c) = t;
28915 OMP_CLAUSE_CHAIN (c) = list;
28917 return c;
28920 /* OpenMP 4.0:
28921 dist_schedule ( static )
28922 dist_schedule ( static , expression ) */
28924 static tree
28925 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28926 location_t location)
28928 tree c, t;
28930 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28931 return list;
28933 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28935 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28936 goto invalid_kind;
28937 cp_lexer_consume_token (parser->lexer);
28939 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28941 cp_lexer_consume_token (parser->lexer);
28943 t = cp_parser_assignment_expression (parser);
28945 if (t == error_mark_node)
28946 goto resync_fail;
28947 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28950 goto resync_fail;
28952 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28953 goto resync_fail;
28955 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28956 location);
28957 OMP_CLAUSE_CHAIN (c) = list;
28958 return c;
28960 invalid_kind:
28961 cp_parser_error (parser, "invalid dist_schedule kind");
28962 resync_fail:
28963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28964 /*or_comma=*/false,
28965 /*consume_paren=*/true);
28966 return list;
28969 /* OpenMP 4.0:
28970 proc_bind ( proc-bind-kind )
28972 proc-bind-kind:
28973 master | close | spread */
28975 static tree
28976 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28977 location_t location)
28979 tree c;
28980 enum omp_clause_proc_bind_kind kind;
28982 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28983 return list;
28985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28987 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28988 const char *p = IDENTIFIER_POINTER (id);
28990 if (strcmp ("master", p) == 0)
28991 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28992 else if (strcmp ("close", p) == 0)
28993 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28994 else if (strcmp ("spread", p) == 0)
28995 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28996 else
28997 goto invalid_kind;
28999 else
29000 goto invalid_kind;
29002 cp_lexer_consume_token (parser->lexer);
29003 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29004 goto resync_fail;
29006 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29007 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29008 location);
29009 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29010 OMP_CLAUSE_CHAIN (c) = list;
29011 return c;
29013 invalid_kind:
29014 cp_parser_error (parser, "invalid depend kind");
29015 resync_fail:
29016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29017 /*or_comma=*/false,
29018 /*consume_paren=*/true);
29019 return list;
29022 /* OpenACC:
29023 async [( int-expr )] */
29025 static tree
29026 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29028 tree c, t;
29029 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29031 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29033 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29035 cp_lexer_consume_token (parser->lexer);
29037 t = cp_parser_expression (parser);
29038 if (t == error_mark_node
29039 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29041 /*or_comma=*/false,
29042 /*consume_paren=*/true);
29045 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29047 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29048 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29049 OMP_CLAUSE_CHAIN (c) = list;
29050 list = c;
29052 return list;
29055 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29056 is a bitmask in MASK. Return the list of clauses found. */
29058 static tree
29059 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29060 const char *where, cp_token *pragma_tok,
29061 bool finish_p = true)
29063 tree clauses = NULL;
29064 bool first = true;
29066 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29068 location_t here;
29069 pragma_omp_clause c_kind;
29070 const char *c_name;
29071 tree prev = clauses;
29073 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29074 cp_lexer_consume_token (parser->lexer);
29076 here = cp_lexer_peek_token (parser->lexer)->location;
29077 c_kind = cp_parser_omp_clause_name (parser);
29079 switch (c_kind)
29081 case PRAGMA_OACC_CLAUSE_ASYNC:
29082 clauses = cp_parser_oacc_clause_async (parser, clauses);
29083 c_name = "async";
29084 break;
29085 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29086 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29087 c_name = "collapse";
29088 break;
29089 case PRAGMA_OACC_CLAUSE_COPY:
29090 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29091 c_name = "copy";
29092 break;
29093 case PRAGMA_OACC_CLAUSE_COPYIN:
29094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29095 c_name = "copyin";
29096 break;
29097 case PRAGMA_OACC_CLAUSE_COPYOUT:
29098 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29099 c_name = "copyout";
29100 break;
29101 case PRAGMA_OACC_CLAUSE_CREATE:
29102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29103 c_name = "create";
29104 break;
29105 case PRAGMA_OACC_CLAUSE_DELETE:
29106 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29107 c_name = "delete";
29108 break;
29109 case PRAGMA_OACC_CLAUSE_DEVICE:
29110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29111 c_name = "device";
29112 break;
29113 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29114 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29115 c_name = "deviceptr";
29116 break;
29117 case PRAGMA_OACC_CLAUSE_HOST:
29118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29119 c_name = "host";
29120 break;
29121 case PRAGMA_OACC_CLAUSE_IF:
29122 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29123 c_name = "if";
29124 break;
29125 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29126 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29127 c_name = "num_gangs";
29128 break;
29129 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29130 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29131 c_name = "num_workers";
29132 break;
29133 case PRAGMA_OACC_CLAUSE_PRESENT:
29134 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29135 c_name = "present";
29136 break;
29137 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29138 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29139 c_name = "present_or_copy";
29140 break;
29141 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29142 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29143 c_name = "present_or_copyin";
29144 break;
29145 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29146 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29147 c_name = "present_or_copyout";
29148 break;
29149 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29150 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29151 c_name = "present_or_create";
29152 break;
29153 case PRAGMA_OACC_CLAUSE_REDUCTION:
29154 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29155 c_name = "reduction";
29156 break;
29157 case PRAGMA_OACC_CLAUSE_SELF:
29158 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29159 c_name = "self";
29160 break;
29161 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29162 clauses =
29163 cp_parser_oacc_clause_vector_length (parser, clauses);
29164 c_name = "vector_length";
29165 break;
29166 case PRAGMA_OACC_CLAUSE_WAIT:
29167 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29168 c_name = "wait";
29169 break;
29170 default:
29171 cp_parser_error (parser, "expected clause");
29172 goto saw_error;
29175 first = false;
29177 if (((mask >> c_kind) & 1) == 0)
29179 /* Remove the invalid clause(s) from the list to avoid
29180 confusing the rest of the compiler. */
29181 clauses = prev;
29182 error_at (here, "%qs is not valid for %qs", c_name, where);
29186 saw_error:
29187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29189 if (finish_p)
29190 return finish_omp_clauses (clauses);
29192 return clauses;
29195 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29196 is a bitmask in MASK. Return the list of clauses found; the result
29197 of clause default goes in *pdefault. */
29199 static tree
29200 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29201 const char *where, cp_token *pragma_tok,
29202 bool finish_p = true)
29204 tree clauses = NULL;
29205 bool first = true;
29206 cp_token *token = NULL;
29207 bool cilk_simd_fn = false;
29209 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29211 pragma_omp_clause c_kind;
29212 const char *c_name;
29213 tree prev = clauses;
29215 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29216 cp_lexer_consume_token (parser->lexer);
29218 token = cp_lexer_peek_token (parser->lexer);
29219 c_kind = cp_parser_omp_clause_name (parser);
29221 switch (c_kind)
29223 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29224 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29225 token->location);
29226 c_name = "collapse";
29227 break;
29228 case PRAGMA_OMP_CLAUSE_COPYIN:
29229 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29230 c_name = "copyin";
29231 break;
29232 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29233 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29234 clauses);
29235 c_name = "copyprivate";
29236 break;
29237 case PRAGMA_OMP_CLAUSE_DEFAULT:
29238 clauses = cp_parser_omp_clause_default (parser, clauses,
29239 token->location);
29240 c_name = "default";
29241 break;
29242 case PRAGMA_OMP_CLAUSE_FINAL:
29243 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29244 c_name = "final";
29245 break;
29246 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29247 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29248 clauses);
29249 c_name = "firstprivate";
29250 break;
29251 case PRAGMA_OMP_CLAUSE_IF:
29252 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29253 c_name = "if";
29254 break;
29255 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29256 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29257 clauses);
29258 c_name = "lastprivate";
29259 break;
29260 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29261 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29262 token->location);
29263 c_name = "mergeable";
29264 break;
29265 case PRAGMA_OMP_CLAUSE_NOWAIT:
29266 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29267 c_name = "nowait";
29268 break;
29269 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29270 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29271 token->location);
29272 c_name = "num_threads";
29273 break;
29274 case PRAGMA_OMP_CLAUSE_ORDERED:
29275 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29276 token->location);
29277 c_name = "ordered";
29278 break;
29279 case PRAGMA_OMP_CLAUSE_PRIVATE:
29280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29281 clauses);
29282 c_name = "private";
29283 break;
29284 case PRAGMA_OMP_CLAUSE_REDUCTION:
29285 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29286 c_name = "reduction";
29287 break;
29288 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29289 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29290 token->location);
29291 c_name = "schedule";
29292 break;
29293 case PRAGMA_OMP_CLAUSE_SHARED:
29294 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29295 clauses);
29296 c_name = "shared";
29297 break;
29298 case PRAGMA_OMP_CLAUSE_UNTIED:
29299 clauses = cp_parser_omp_clause_untied (parser, clauses,
29300 token->location);
29301 c_name = "untied";
29302 break;
29303 case PRAGMA_OMP_CLAUSE_INBRANCH:
29304 case PRAGMA_CILK_CLAUSE_MASK:
29305 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29306 clauses, token->location);
29307 c_name = "inbranch";
29308 break;
29309 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29310 case PRAGMA_CILK_CLAUSE_NOMASK:
29311 clauses = cp_parser_omp_clause_branch (parser,
29312 OMP_CLAUSE_NOTINBRANCH,
29313 clauses, token->location);
29314 c_name = "notinbranch";
29315 break;
29316 case PRAGMA_OMP_CLAUSE_PARALLEL:
29317 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29318 clauses, token->location);
29319 c_name = "parallel";
29320 if (!first)
29322 clause_not_first:
29323 error_at (token->location, "%qs must be the first clause of %qs",
29324 c_name, where);
29325 clauses = prev;
29327 break;
29328 case PRAGMA_OMP_CLAUSE_FOR:
29329 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29330 clauses, token->location);
29331 c_name = "for";
29332 if (!first)
29333 goto clause_not_first;
29334 break;
29335 case PRAGMA_OMP_CLAUSE_SECTIONS:
29336 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29337 clauses, token->location);
29338 c_name = "sections";
29339 if (!first)
29340 goto clause_not_first;
29341 break;
29342 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29343 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29344 clauses, token->location);
29345 c_name = "taskgroup";
29346 if (!first)
29347 goto clause_not_first;
29348 break;
29349 case PRAGMA_OMP_CLAUSE_TO:
29350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29351 clauses);
29352 c_name = "to";
29353 break;
29354 case PRAGMA_OMP_CLAUSE_FROM:
29355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29356 clauses);
29357 c_name = "from";
29358 break;
29359 case PRAGMA_OMP_CLAUSE_UNIFORM:
29360 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29361 clauses);
29362 c_name = "uniform";
29363 break;
29364 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29365 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29366 token->location);
29367 c_name = "num_teams";
29368 break;
29369 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29370 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29371 token->location);
29372 c_name = "thread_limit";
29373 break;
29374 case PRAGMA_OMP_CLAUSE_ALIGNED:
29375 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29376 c_name = "aligned";
29377 break;
29378 case PRAGMA_OMP_CLAUSE_LINEAR:
29379 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29380 cilk_simd_fn = true;
29381 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29382 c_name = "linear";
29383 break;
29384 case PRAGMA_OMP_CLAUSE_DEPEND:
29385 clauses = cp_parser_omp_clause_depend (parser, clauses);
29386 c_name = "depend";
29387 break;
29388 case PRAGMA_OMP_CLAUSE_MAP:
29389 clauses = cp_parser_omp_clause_map (parser, clauses);
29390 c_name = "map";
29391 break;
29392 case PRAGMA_OMP_CLAUSE_DEVICE:
29393 clauses = cp_parser_omp_clause_device (parser, clauses,
29394 token->location);
29395 c_name = "device";
29396 break;
29397 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29398 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29399 token->location);
29400 c_name = "dist_schedule";
29401 break;
29402 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29403 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29404 token->location);
29405 c_name = "proc_bind";
29406 break;
29407 case PRAGMA_OMP_CLAUSE_SAFELEN:
29408 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29409 token->location);
29410 c_name = "safelen";
29411 break;
29412 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29413 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29414 token->location);
29415 c_name = "simdlen";
29416 break;
29417 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29418 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29419 c_name = "simdlen";
29420 break;
29421 default:
29422 cp_parser_error (parser, "expected clause");
29423 goto saw_error;
29426 first = false;
29428 if (((mask >> c_kind) & 1) == 0)
29430 /* Remove the invalid clause(s) from the list to avoid
29431 confusing the rest of the compiler. */
29432 clauses = prev;
29433 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29436 saw_error:
29437 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29438 no reason to skip to the end. */
29439 if (!(flag_cilkplus && pragma_tok == NULL))
29440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29441 if (finish_p)
29442 return finish_omp_clauses (clauses);
29443 return clauses;
29446 /* OpenMP 2.5:
29447 structured-block:
29448 statement
29450 In practice, we're also interested in adding the statement to an
29451 outer node. So it is convenient if we work around the fact that
29452 cp_parser_statement calls add_stmt. */
29454 static unsigned
29455 cp_parser_begin_omp_structured_block (cp_parser *parser)
29457 unsigned save = parser->in_statement;
29459 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29460 This preserves the "not within loop or switch" style error messages
29461 for nonsense cases like
29462 void foo() {
29463 #pragma omp single
29464 break;
29467 if (parser->in_statement)
29468 parser->in_statement = IN_OMP_BLOCK;
29470 return save;
29473 static void
29474 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29476 parser->in_statement = save;
29479 static tree
29480 cp_parser_omp_structured_block (cp_parser *parser)
29482 tree stmt = begin_omp_structured_block ();
29483 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29485 cp_parser_statement (parser, NULL_TREE, false, NULL);
29487 cp_parser_end_omp_structured_block (parser, save);
29488 return finish_omp_structured_block (stmt);
29491 /* OpenMP 2.5:
29492 # pragma omp atomic new-line
29493 expression-stmt
29495 expression-stmt:
29496 x binop= expr | x++ | ++x | x-- | --x
29497 binop:
29498 +, *, -, /, &, ^, |, <<, >>
29500 where x is an lvalue expression with scalar type.
29502 OpenMP 3.1:
29503 # pragma omp atomic new-line
29504 update-stmt
29506 # pragma omp atomic read new-line
29507 read-stmt
29509 # pragma omp atomic write new-line
29510 write-stmt
29512 # pragma omp atomic update new-line
29513 update-stmt
29515 # pragma omp atomic capture new-line
29516 capture-stmt
29518 # pragma omp atomic capture new-line
29519 capture-block
29521 read-stmt:
29522 v = x
29523 write-stmt:
29524 x = expr
29525 update-stmt:
29526 expression-stmt | x = x binop expr
29527 capture-stmt:
29528 v = expression-stmt
29529 capture-block:
29530 { v = x; update-stmt; } | { update-stmt; v = x; }
29532 OpenMP 4.0:
29533 update-stmt:
29534 expression-stmt | x = x binop expr | x = expr binop x
29535 capture-stmt:
29536 v = update-stmt
29537 capture-block:
29538 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29540 where x and v are lvalue expressions with scalar type. */
29542 static void
29543 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29545 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29546 tree rhs1 = NULL_TREE, orig_lhs;
29547 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29548 bool structured_block = false;
29549 bool seq_cst = false;
29551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29554 const char *p = IDENTIFIER_POINTER (id);
29556 if (!strcmp (p, "seq_cst"))
29558 seq_cst = true;
29559 cp_lexer_consume_token (parser->lexer);
29560 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29561 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29562 cp_lexer_consume_token (parser->lexer);
29565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29568 const char *p = IDENTIFIER_POINTER (id);
29570 if (!strcmp (p, "read"))
29571 code = OMP_ATOMIC_READ;
29572 else if (!strcmp (p, "write"))
29573 code = NOP_EXPR;
29574 else if (!strcmp (p, "update"))
29575 code = OMP_ATOMIC;
29576 else if (!strcmp (p, "capture"))
29577 code = OMP_ATOMIC_CAPTURE_NEW;
29578 else
29579 p = NULL;
29580 if (p)
29581 cp_lexer_consume_token (parser->lexer);
29583 if (!seq_cst)
29585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29587 cp_lexer_consume_token (parser->lexer);
29589 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29591 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29592 const char *p = IDENTIFIER_POINTER (id);
29594 if (!strcmp (p, "seq_cst"))
29596 seq_cst = true;
29597 cp_lexer_consume_token (parser->lexer);
29601 cp_parser_require_pragma_eol (parser, pragma_tok);
29603 switch (code)
29605 case OMP_ATOMIC_READ:
29606 case NOP_EXPR: /* atomic write */
29607 v = cp_parser_unary_expression (parser);
29608 if (v == error_mark_node)
29609 goto saw_error;
29610 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29611 goto saw_error;
29612 if (code == NOP_EXPR)
29613 lhs = cp_parser_expression (parser);
29614 else
29615 lhs = cp_parser_unary_expression (parser);
29616 if (lhs == error_mark_node)
29617 goto saw_error;
29618 if (code == NOP_EXPR)
29620 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29621 opcode. */
29622 code = OMP_ATOMIC;
29623 rhs = lhs;
29624 lhs = v;
29625 v = NULL_TREE;
29627 goto done;
29628 case OMP_ATOMIC_CAPTURE_NEW:
29629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29631 cp_lexer_consume_token (parser->lexer);
29632 structured_block = true;
29634 else
29636 v = cp_parser_unary_expression (parser);
29637 if (v == error_mark_node)
29638 goto saw_error;
29639 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29640 goto saw_error;
29642 default:
29643 break;
29646 restart:
29647 lhs = cp_parser_unary_expression (parser);
29648 orig_lhs = lhs;
29649 switch (TREE_CODE (lhs))
29651 case ERROR_MARK:
29652 goto saw_error;
29654 case POSTINCREMENT_EXPR:
29655 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29656 code = OMP_ATOMIC_CAPTURE_OLD;
29657 /* FALLTHROUGH */
29658 case PREINCREMENT_EXPR:
29659 lhs = TREE_OPERAND (lhs, 0);
29660 opcode = PLUS_EXPR;
29661 rhs = integer_one_node;
29662 break;
29664 case POSTDECREMENT_EXPR:
29665 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29666 code = OMP_ATOMIC_CAPTURE_OLD;
29667 /* FALLTHROUGH */
29668 case PREDECREMENT_EXPR:
29669 lhs = TREE_OPERAND (lhs, 0);
29670 opcode = MINUS_EXPR;
29671 rhs = integer_one_node;
29672 break;
29674 case COMPOUND_EXPR:
29675 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29676 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29677 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29678 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29680 (TREE_OPERAND (lhs, 1), 0), 0)))
29681 == BOOLEAN_TYPE)
29682 /* Undo effects of boolean_increment for post {in,de}crement. */
29683 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29684 /* FALLTHRU */
29685 case MODIFY_EXPR:
29686 if (TREE_CODE (lhs) == MODIFY_EXPR
29687 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29689 /* Undo effects of boolean_increment. */
29690 if (integer_onep (TREE_OPERAND (lhs, 1)))
29692 /* This is pre or post increment. */
29693 rhs = TREE_OPERAND (lhs, 1);
29694 lhs = TREE_OPERAND (lhs, 0);
29695 opcode = NOP_EXPR;
29696 if (code == OMP_ATOMIC_CAPTURE_NEW
29697 && !structured_block
29698 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29699 code = OMP_ATOMIC_CAPTURE_OLD;
29700 break;
29703 /* FALLTHRU */
29704 default:
29705 switch (cp_lexer_peek_token (parser->lexer)->type)
29707 case CPP_MULT_EQ:
29708 opcode = MULT_EXPR;
29709 break;
29710 case CPP_DIV_EQ:
29711 opcode = TRUNC_DIV_EXPR;
29712 break;
29713 case CPP_PLUS_EQ:
29714 opcode = PLUS_EXPR;
29715 break;
29716 case CPP_MINUS_EQ:
29717 opcode = MINUS_EXPR;
29718 break;
29719 case CPP_LSHIFT_EQ:
29720 opcode = LSHIFT_EXPR;
29721 break;
29722 case CPP_RSHIFT_EQ:
29723 opcode = RSHIFT_EXPR;
29724 break;
29725 case CPP_AND_EQ:
29726 opcode = BIT_AND_EXPR;
29727 break;
29728 case CPP_OR_EQ:
29729 opcode = BIT_IOR_EXPR;
29730 break;
29731 case CPP_XOR_EQ:
29732 opcode = BIT_XOR_EXPR;
29733 break;
29734 case CPP_EQ:
29735 enum cp_parser_prec oprec;
29736 cp_token *token;
29737 cp_lexer_consume_token (parser->lexer);
29738 cp_parser_parse_tentatively (parser);
29739 rhs1 = cp_parser_simple_cast_expression (parser);
29740 if (rhs1 == error_mark_node)
29742 cp_parser_abort_tentative_parse (parser);
29743 cp_parser_simple_cast_expression (parser);
29744 goto saw_error;
29746 token = cp_lexer_peek_token (parser->lexer);
29747 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29749 cp_parser_abort_tentative_parse (parser);
29750 cp_parser_parse_tentatively (parser);
29751 rhs = cp_parser_binary_expression (parser, false, true,
29752 PREC_NOT_OPERATOR, NULL);
29753 if (rhs == error_mark_node)
29755 cp_parser_abort_tentative_parse (parser);
29756 cp_parser_binary_expression (parser, false, true,
29757 PREC_NOT_OPERATOR, NULL);
29758 goto saw_error;
29760 switch (TREE_CODE (rhs))
29762 case MULT_EXPR:
29763 case TRUNC_DIV_EXPR:
29764 case PLUS_EXPR:
29765 case MINUS_EXPR:
29766 case LSHIFT_EXPR:
29767 case RSHIFT_EXPR:
29768 case BIT_AND_EXPR:
29769 case BIT_IOR_EXPR:
29770 case BIT_XOR_EXPR:
29771 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29773 if (cp_parser_parse_definitely (parser))
29775 opcode = TREE_CODE (rhs);
29776 rhs1 = TREE_OPERAND (rhs, 0);
29777 rhs = TREE_OPERAND (rhs, 1);
29778 goto stmt_done;
29780 else
29781 goto saw_error;
29783 break;
29784 default:
29785 break;
29787 cp_parser_abort_tentative_parse (parser);
29788 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29790 rhs = cp_parser_expression (parser);
29791 if (rhs == error_mark_node)
29792 goto saw_error;
29793 opcode = NOP_EXPR;
29794 rhs1 = NULL_TREE;
29795 goto stmt_done;
29797 cp_parser_error (parser,
29798 "invalid form of %<#pragma omp atomic%>");
29799 goto saw_error;
29801 if (!cp_parser_parse_definitely (parser))
29802 goto saw_error;
29803 switch (token->type)
29805 case CPP_SEMICOLON:
29806 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29808 code = OMP_ATOMIC_CAPTURE_OLD;
29809 v = lhs;
29810 lhs = NULL_TREE;
29811 lhs1 = rhs1;
29812 rhs1 = NULL_TREE;
29813 cp_lexer_consume_token (parser->lexer);
29814 goto restart;
29816 else if (structured_block)
29818 opcode = NOP_EXPR;
29819 rhs = rhs1;
29820 rhs1 = NULL_TREE;
29821 goto stmt_done;
29823 cp_parser_error (parser,
29824 "invalid form of %<#pragma omp atomic%>");
29825 goto saw_error;
29826 case CPP_MULT:
29827 opcode = MULT_EXPR;
29828 break;
29829 case CPP_DIV:
29830 opcode = TRUNC_DIV_EXPR;
29831 break;
29832 case CPP_PLUS:
29833 opcode = PLUS_EXPR;
29834 break;
29835 case CPP_MINUS:
29836 opcode = MINUS_EXPR;
29837 break;
29838 case CPP_LSHIFT:
29839 opcode = LSHIFT_EXPR;
29840 break;
29841 case CPP_RSHIFT:
29842 opcode = RSHIFT_EXPR;
29843 break;
29844 case CPP_AND:
29845 opcode = BIT_AND_EXPR;
29846 break;
29847 case CPP_OR:
29848 opcode = BIT_IOR_EXPR;
29849 break;
29850 case CPP_XOR:
29851 opcode = BIT_XOR_EXPR;
29852 break;
29853 default:
29854 cp_parser_error (parser,
29855 "invalid operator for %<#pragma omp atomic%>");
29856 goto saw_error;
29858 oprec = TOKEN_PRECEDENCE (token);
29859 gcc_assert (oprec != PREC_NOT_OPERATOR);
29860 if (commutative_tree_code (opcode))
29861 oprec = (enum cp_parser_prec) (oprec - 1);
29862 cp_lexer_consume_token (parser->lexer);
29863 rhs = cp_parser_binary_expression (parser, false, false,
29864 oprec, NULL);
29865 if (rhs == error_mark_node)
29866 goto saw_error;
29867 goto stmt_done;
29868 /* FALLTHROUGH */
29869 default:
29870 cp_parser_error (parser,
29871 "invalid operator for %<#pragma omp atomic%>");
29872 goto saw_error;
29874 cp_lexer_consume_token (parser->lexer);
29876 rhs = cp_parser_expression (parser);
29877 if (rhs == error_mark_node)
29878 goto saw_error;
29879 break;
29881 stmt_done:
29882 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29884 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29885 goto saw_error;
29886 v = cp_parser_unary_expression (parser);
29887 if (v == error_mark_node)
29888 goto saw_error;
29889 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29890 goto saw_error;
29891 lhs1 = cp_parser_unary_expression (parser);
29892 if (lhs1 == error_mark_node)
29893 goto saw_error;
29895 if (structured_block)
29897 cp_parser_consume_semicolon_at_end_of_statement (parser);
29898 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29900 done:
29901 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29902 if (!structured_block)
29903 cp_parser_consume_semicolon_at_end_of_statement (parser);
29904 return;
29906 saw_error:
29907 cp_parser_skip_to_end_of_block_or_statement (parser);
29908 if (structured_block)
29910 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29911 cp_lexer_consume_token (parser->lexer);
29912 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29914 cp_parser_skip_to_end_of_block_or_statement (parser);
29915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29916 cp_lexer_consume_token (parser->lexer);
29922 /* OpenMP 2.5:
29923 # pragma omp barrier new-line */
29925 static void
29926 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29928 cp_parser_require_pragma_eol (parser, pragma_tok);
29929 finish_omp_barrier ();
29932 /* OpenMP 2.5:
29933 # pragma omp critical [(name)] new-line
29934 structured-block */
29936 static tree
29937 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29939 tree stmt, name = NULL;
29941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29943 cp_lexer_consume_token (parser->lexer);
29945 name = cp_parser_identifier (parser);
29947 if (name == error_mark_node
29948 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29950 /*or_comma=*/false,
29951 /*consume_paren=*/true);
29952 if (name == error_mark_node)
29953 name = NULL;
29955 cp_parser_require_pragma_eol (parser, pragma_tok);
29957 stmt = cp_parser_omp_structured_block (parser);
29958 return c_finish_omp_critical (input_location, stmt, name);
29961 /* OpenMP 2.5:
29962 # pragma omp flush flush-vars[opt] new-line
29964 flush-vars:
29965 ( variable-list ) */
29967 static void
29968 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29970 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29971 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29972 cp_parser_require_pragma_eol (parser, pragma_tok);
29974 finish_omp_flush ();
29977 /* Helper function, to parse omp for increment expression. */
29979 static tree
29980 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29982 tree cond = cp_parser_binary_expression (parser, false, true,
29983 PREC_NOT_OPERATOR, NULL);
29984 if (cond == error_mark_node
29985 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29987 cp_parser_skip_to_end_of_statement (parser);
29988 return error_mark_node;
29991 switch (TREE_CODE (cond))
29993 case GT_EXPR:
29994 case GE_EXPR:
29995 case LT_EXPR:
29996 case LE_EXPR:
29997 break;
29998 case NE_EXPR:
29999 if (code == CILK_SIMD || code == CILK_FOR)
30000 break;
30001 /* Fall through: OpenMP disallows NE_EXPR. */
30002 default:
30003 return error_mark_node;
30006 /* If decl is an iterator, preserve LHS and RHS of the relational
30007 expr until finish_omp_for. */
30008 if (decl
30009 && (type_dependent_expression_p (decl)
30010 || CLASS_TYPE_P (TREE_TYPE (decl))))
30011 return cond;
30013 return build_x_binary_op (input_location, TREE_CODE (cond),
30014 TREE_OPERAND (cond, 0), ERROR_MARK,
30015 TREE_OPERAND (cond, 1), ERROR_MARK,
30016 /*overload=*/NULL, tf_warning_or_error);
30019 /* Helper function, to parse omp for increment expression. */
30021 static tree
30022 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30024 cp_token *token = cp_lexer_peek_token (parser->lexer);
30025 enum tree_code op;
30026 tree lhs, rhs;
30027 cp_id_kind idk;
30028 bool decl_first;
30030 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30032 op = (token->type == CPP_PLUS_PLUS
30033 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30034 cp_lexer_consume_token (parser->lexer);
30035 lhs = cp_parser_simple_cast_expression (parser);
30036 if (lhs != decl)
30037 return error_mark_node;
30038 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30041 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30042 if (lhs != decl)
30043 return error_mark_node;
30045 token = cp_lexer_peek_token (parser->lexer);
30046 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30048 op = (token->type == CPP_PLUS_PLUS
30049 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30050 cp_lexer_consume_token (parser->lexer);
30051 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30054 op = cp_parser_assignment_operator_opt (parser);
30055 if (op == ERROR_MARK)
30056 return error_mark_node;
30058 if (op != NOP_EXPR)
30060 rhs = cp_parser_assignment_expression (parser);
30061 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30062 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30065 lhs = cp_parser_binary_expression (parser, false, false,
30066 PREC_ADDITIVE_EXPRESSION, NULL);
30067 token = cp_lexer_peek_token (parser->lexer);
30068 decl_first = lhs == decl;
30069 if (decl_first)
30070 lhs = NULL_TREE;
30071 if (token->type != CPP_PLUS
30072 && token->type != CPP_MINUS)
30073 return error_mark_node;
30077 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30078 cp_lexer_consume_token (parser->lexer);
30079 rhs = cp_parser_binary_expression (parser, false, false,
30080 PREC_ADDITIVE_EXPRESSION, NULL);
30081 token = cp_lexer_peek_token (parser->lexer);
30082 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30084 if (lhs == NULL_TREE)
30086 if (op == PLUS_EXPR)
30087 lhs = rhs;
30088 else
30089 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30090 tf_warning_or_error);
30092 else
30093 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30094 ERROR_MARK, NULL, tf_warning_or_error);
30097 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30099 if (!decl_first)
30101 if (rhs != decl || op == MINUS_EXPR)
30102 return error_mark_node;
30103 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30105 else
30106 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30108 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30111 /* Parse the initialization statement of either an OpenMP for loop or
30112 a Cilk Plus for loop.
30114 Return true if the resulting construct should have an
30115 OMP_CLAUSE_PRIVATE added to it. */
30117 static bool
30118 cp_parser_omp_for_loop_init (cp_parser *parser,
30119 enum tree_code code,
30120 tree &this_pre_body,
30121 vec<tree, va_gc> *for_block,
30122 tree &init,
30123 tree &decl,
30124 tree &real_decl)
30126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30127 return false;
30129 bool add_private_clause = false;
30131 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30133 init-expr:
30134 var = lb
30135 integer-type var = lb
30136 random-access-iterator-type var = lb
30137 pointer-type var = lb
30139 cp_decl_specifier_seq type_specifiers;
30141 /* First, try to parse as an initialized declaration. See
30142 cp_parser_condition, from whence the bulk of this is copied. */
30144 cp_parser_parse_tentatively (parser);
30145 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30146 /*is_trailing_return=*/false,
30147 &type_specifiers);
30148 if (cp_parser_parse_definitely (parser))
30150 /* If parsing a type specifier seq succeeded, then this
30151 MUST be a initialized declaration. */
30152 tree asm_specification, attributes;
30153 cp_declarator *declarator;
30155 declarator = cp_parser_declarator (parser,
30156 CP_PARSER_DECLARATOR_NAMED,
30157 /*ctor_dtor_or_conv_p=*/NULL,
30158 /*parenthesized_p=*/NULL,
30159 /*member_p=*/false,
30160 /*friend_p=*/false);
30161 attributes = cp_parser_attributes_opt (parser);
30162 asm_specification = cp_parser_asm_specification_opt (parser);
30164 if (declarator == cp_error_declarator)
30165 cp_parser_skip_to_end_of_statement (parser);
30167 else
30169 tree pushed_scope, auto_node;
30171 decl = start_decl (declarator, &type_specifiers,
30172 SD_INITIALIZED, attributes,
30173 /*prefix_attributes=*/NULL_TREE,
30174 &pushed_scope);
30176 auto_node = type_uses_auto (TREE_TYPE (decl));
30177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30179 if (cp_lexer_next_token_is (parser->lexer,
30180 CPP_OPEN_PAREN))
30182 if (code != CILK_SIMD && code != CILK_FOR)
30183 error ("parenthesized initialization is not allowed in "
30184 "OpenMP %<for%> loop");
30185 else
30186 error ("parenthesized initialization is "
30187 "not allowed in for-loop");
30189 else
30190 /* Trigger an error. */
30191 cp_parser_require (parser, CPP_EQ, RT_EQ);
30193 init = error_mark_node;
30194 cp_parser_skip_to_end_of_statement (parser);
30196 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30197 || type_dependent_expression_p (decl)
30198 || auto_node)
30200 bool is_direct_init, is_non_constant_init;
30202 init = cp_parser_initializer (parser,
30203 &is_direct_init,
30204 &is_non_constant_init);
30206 if (auto_node)
30208 TREE_TYPE (decl)
30209 = do_auto_deduction (TREE_TYPE (decl), init,
30210 auto_node);
30212 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30213 && !type_dependent_expression_p (decl))
30214 goto non_class;
30217 cp_finish_decl (decl, init, !is_non_constant_init,
30218 asm_specification,
30219 LOOKUP_ONLYCONVERTING);
30220 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30222 vec_safe_push (for_block, this_pre_body);
30223 init = NULL_TREE;
30225 else
30226 init = pop_stmt_list (this_pre_body);
30227 this_pre_body = NULL_TREE;
30229 else
30231 /* Consume '='. */
30232 cp_lexer_consume_token (parser->lexer);
30233 init = cp_parser_assignment_expression (parser);
30235 non_class:
30236 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30237 init = error_mark_node;
30238 else
30239 cp_finish_decl (decl, NULL_TREE,
30240 /*init_const_expr_p=*/false,
30241 asm_specification,
30242 LOOKUP_ONLYCONVERTING);
30245 if (pushed_scope)
30246 pop_scope (pushed_scope);
30249 else
30251 cp_id_kind idk;
30252 /* If parsing a type specifier sequence failed, then
30253 this MUST be a simple expression. */
30254 if (code == CILK_FOR)
30255 error ("%<_Cilk_for%> allows expression instead of declaration only "
30256 "in C, not in C++");
30257 cp_parser_parse_tentatively (parser);
30258 decl = cp_parser_primary_expression (parser, false, false,
30259 false, &idk);
30260 if (!cp_parser_error_occurred (parser)
30261 && decl
30262 && DECL_P (decl)
30263 && CLASS_TYPE_P (TREE_TYPE (decl)))
30265 tree rhs;
30267 cp_parser_parse_definitely (parser);
30268 cp_parser_require (parser, CPP_EQ, RT_EQ);
30269 rhs = cp_parser_assignment_expression (parser);
30270 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30271 decl, NOP_EXPR,
30272 rhs,
30273 tf_warning_or_error));
30274 add_private_clause = true;
30276 else
30278 decl = NULL;
30279 cp_parser_abort_tentative_parse (parser);
30280 init = cp_parser_expression (parser);
30281 if (init)
30283 if (TREE_CODE (init) == MODIFY_EXPR
30284 || TREE_CODE (init) == MODOP_EXPR)
30285 real_decl = TREE_OPERAND (init, 0);
30289 return add_private_clause;
30292 /* Parse the restricted form of the for statement allowed by OpenMP. */
30294 static tree
30295 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30296 tree *cclauses)
30298 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30299 tree real_decl, initv, condv, incrv, declv;
30300 tree this_pre_body, cl;
30301 location_t loc_first;
30302 bool collapse_err = false;
30303 int i, collapse = 1, nbraces = 0;
30304 vec<tree, va_gc> *for_block = make_tree_vector ();
30306 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30307 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30308 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30310 gcc_assert (collapse >= 1);
30312 declv = make_tree_vec (collapse);
30313 initv = make_tree_vec (collapse);
30314 condv = make_tree_vec (collapse);
30315 incrv = make_tree_vec (collapse);
30317 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30319 for (i = 0; i < collapse; i++)
30321 int bracecount = 0;
30322 bool add_private_clause = false;
30323 location_t loc;
30325 if (code != CILK_FOR
30326 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30328 cp_parser_error (parser, "for statement expected");
30329 return NULL;
30331 if (code == CILK_FOR
30332 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30334 cp_parser_error (parser, "_Cilk_for statement expected");
30335 return NULL;
30337 loc = cp_lexer_consume_token (parser->lexer)->location;
30339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30340 return NULL;
30342 init = decl = real_decl = NULL;
30343 this_pre_body = push_stmt_list ();
30345 add_private_clause
30346 |= cp_parser_omp_for_loop_init (parser, code,
30347 this_pre_body, for_block,
30348 init, decl, real_decl);
30350 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30351 if (this_pre_body)
30353 this_pre_body = pop_stmt_list (this_pre_body);
30354 if (pre_body)
30356 tree t = pre_body;
30357 pre_body = push_stmt_list ();
30358 add_stmt (t);
30359 add_stmt (this_pre_body);
30360 pre_body = pop_stmt_list (pre_body);
30362 else
30363 pre_body = this_pre_body;
30366 if (decl)
30367 real_decl = decl;
30368 if (cclauses != NULL
30369 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30370 && real_decl != NULL_TREE)
30372 tree *c;
30373 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30374 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30375 && OMP_CLAUSE_DECL (*c) == real_decl)
30377 error_at (loc, "iteration variable %qD"
30378 " should not be firstprivate", real_decl);
30379 *c = OMP_CLAUSE_CHAIN (*c);
30381 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30382 && OMP_CLAUSE_DECL (*c) == real_decl)
30384 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30385 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30386 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30387 OMP_CLAUSE_DECL (l) = real_decl;
30388 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30389 if (code == OMP_SIMD)
30391 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30392 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30394 else
30396 OMP_CLAUSE_CHAIN (l) = clauses;
30397 clauses = l;
30399 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30400 CP_OMP_CLAUSE_INFO (*c) = NULL;
30401 add_private_clause = false;
30403 else
30405 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30406 && OMP_CLAUSE_DECL (*c) == real_decl)
30407 add_private_clause = false;
30408 c = &OMP_CLAUSE_CHAIN (*c);
30412 if (add_private_clause)
30414 tree c;
30415 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30417 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30419 && OMP_CLAUSE_DECL (c) == decl)
30420 break;
30421 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30422 && OMP_CLAUSE_DECL (c) == decl)
30423 error_at (loc, "iteration variable %qD "
30424 "should not be firstprivate",
30425 decl);
30426 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30427 && OMP_CLAUSE_DECL (c) == decl)
30428 error_at (loc, "iteration variable %qD should not be reduction",
30429 decl);
30431 if (c == NULL)
30433 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30434 OMP_CLAUSE_DECL (c) = decl;
30435 c = finish_omp_clauses (c);
30436 if (c)
30438 OMP_CLAUSE_CHAIN (c) = clauses;
30439 clauses = c;
30444 cond = NULL;
30445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30446 cond = cp_parser_omp_for_cond (parser, decl, code);
30447 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30449 incr = NULL;
30450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30452 /* If decl is an iterator, preserve the operator on decl
30453 until finish_omp_for. */
30454 if (real_decl
30455 && ((processing_template_decl
30456 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30457 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30458 incr = cp_parser_omp_for_incr (parser, real_decl);
30459 else
30460 incr = cp_parser_expression (parser);
30461 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30462 SET_EXPR_LOCATION (incr, input_location);
30465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30467 /*or_comma=*/false,
30468 /*consume_paren=*/true);
30470 TREE_VEC_ELT (declv, i) = decl;
30471 TREE_VEC_ELT (initv, i) = init;
30472 TREE_VEC_ELT (condv, i) = cond;
30473 TREE_VEC_ELT (incrv, i) = incr;
30475 if (i == collapse - 1)
30476 break;
30478 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30479 in between the collapsed for loops to be still considered perfectly
30480 nested. Hopefully the final version clarifies this.
30481 For now handle (multiple) {'s and empty statements. */
30482 cp_parser_parse_tentatively (parser);
30485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30486 break;
30487 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30489 cp_lexer_consume_token (parser->lexer);
30490 bracecount++;
30492 else if (bracecount
30493 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30494 cp_lexer_consume_token (parser->lexer);
30495 else
30497 loc = cp_lexer_peek_token (parser->lexer)->location;
30498 error_at (loc, "not enough collapsed for loops");
30499 collapse_err = true;
30500 cp_parser_abort_tentative_parse (parser);
30501 declv = NULL_TREE;
30502 break;
30505 while (1);
30507 if (declv)
30509 cp_parser_parse_definitely (parser);
30510 nbraces += bracecount;
30514 /* Note that we saved the original contents of this flag when we entered
30515 the structured block, and so we don't need to re-save it here. */
30516 if (code == CILK_SIMD || code == CILK_FOR)
30517 parser->in_statement = IN_CILK_SIMD_FOR;
30518 else
30519 parser->in_statement = IN_OMP_FOR;
30521 /* Note that the grammar doesn't call for a structured block here,
30522 though the loop as a whole is a structured block. */
30523 body = push_stmt_list ();
30524 cp_parser_statement (parser, NULL_TREE, false, NULL);
30525 body = pop_stmt_list (body);
30527 if (declv == NULL_TREE)
30528 ret = NULL_TREE;
30529 else
30530 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30531 pre_body, clauses);
30533 while (nbraces)
30535 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30537 cp_lexer_consume_token (parser->lexer);
30538 nbraces--;
30540 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30541 cp_lexer_consume_token (parser->lexer);
30542 else
30544 if (!collapse_err)
30546 error_at (cp_lexer_peek_token (parser->lexer)->location,
30547 "collapsed loops not perfectly nested");
30549 collapse_err = true;
30550 cp_parser_statement_seq_opt (parser, NULL);
30551 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30552 break;
30556 while (!for_block->is_empty ())
30557 add_stmt (pop_stmt_list (for_block->pop ()));
30558 release_tree_vector (for_block);
30560 return ret;
30563 /* Helper function for OpenMP parsing, split clauses and call
30564 finish_omp_clauses on each of the set of clauses afterwards. */
30566 static void
30567 cp_omp_split_clauses (location_t loc, enum tree_code code,
30568 omp_clause_mask mask, tree clauses, tree *cclauses)
30570 int i;
30571 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30572 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30573 if (cclauses[i])
30574 cclauses[i] = finish_omp_clauses (cclauses[i]);
30577 /* OpenMP 4.0:
30578 #pragma omp simd simd-clause[optseq] new-line
30579 for-loop */
30581 #define OMP_SIMD_CLAUSE_MASK \
30582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30590 static tree
30591 cp_parser_omp_simd (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, " simd");
30599 mask |= OMP_SIMD_CLAUSE_MASK;
30600 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30602 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30603 cclauses == NULL);
30604 if (cclauses)
30606 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30607 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30610 sb = begin_omp_structured_block ();
30611 save = cp_parser_begin_omp_structured_block (parser);
30613 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30615 cp_parser_end_omp_structured_block (parser, save);
30616 add_stmt (finish_omp_structured_block (sb));
30618 return ret;
30621 /* OpenMP 2.5:
30622 #pragma omp for for-clause[optseq] new-line
30623 for-loop
30625 OpenMP 4.0:
30626 #pragma omp for simd for-simd-clause[optseq] new-line
30627 for-loop */
30629 #define OMP_FOR_CLAUSE_MASK \
30630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30639 static tree
30640 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30641 char *p_name, omp_clause_mask mask, tree *cclauses)
30643 tree clauses, sb, ret;
30644 unsigned int save;
30645 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30647 strcat (p_name, " for");
30648 mask |= OMP_FOR_CLAUSE_MASK;
30649 if (cclauses)
30650 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30655 const char *p = IDENTIFIER_POINTER (id);
30657 if (strcmp (p, "simd") == 0)
30659 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30660 if (cclauses == NULL)
30661 cclauses = cclauses_buf;
30663 cp_lexer_consume_token (parser->lexer);
30664 if (!flag_openmp) /* flag_openmp_simd */
30665 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30666 cclauses);
30667 sb = begin_omp_structured_block ();
30668 save = cp_parser_begin_omp_structured_block (parser);
30669 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30670 cclauses);
30671 cp_parser_end_omp_structured_block (parser, save);
30672 tree body = finish_omp_structured_block (sb);
30673 if (ret == NULL)
30674 return ret;
30675 ret = make_node (OMP_FOR);
30676 TREE_TYPE (ret) = void_type_node;
30677 OMP_FOR_BODY (ret) = body;
30678 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30679 SET_EXPR_LOCATION (ret, loc);
30680 add_stmt (ret);
30681 return ret;
30684 if (!flag_openmp) /* flag_openmp_simd */
30686 cp_parser_require_pragma_eol (parser, pragma_tok);
30687 return NULL_TREE;
30690 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30691 cclauses == NULL);
30692 if (cclauses)
30694 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30695 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30698 sb = begin_omp_structured_block ();
30699 save = cp_parser_begin_omp_structured_block (parser);
30701 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30703 cp_parser_end_omp_structured_block (parser, save);
30704 add_stmt (finish_omp_structured_block (sb));
30706 return ret;
30709 /* OpenMP 2.5:
30710 # pragma omp master new-line
30711 structured-block */
30713 static tree
30714 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30716 cp_parser_require_pragma_eol (parser, pragma_tok);
30717 return c_finish_omp_master (input_location,
30718 cp_parser_omp_structured_block (parser));
30721 /* OpenMP 2.5:
30722 # pragma omp ordered new-line
30723 structured-block */
30725 static tree
30726 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30728 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30729 cp_parser_require_pragma_eol (parser, pragma_tok);
30730 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30733 /* OpenMP 2.5:
30735 section-scope:
30736 { section-sequence }
30738 section-sequence:
30739 section-directive[opt] structured-block
30740 section-sequence section-directive structured-block */
30742 static tree
30743 cp_parser_omp_sections_scope (cp_parser *parser)
30745 tree stmt, substmt;
30746 bool error_suppress = false;
30747 cp_token *tok;
30749 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30750 return NULL_TREE;
30752 stmt = push_stmt_list ();
30754 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30756 substmt = cp_parser_omp_structured_block (parser);
30757 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30758 add_stmt (substmt);
30761 while (1)
30763 tok = cp_lexer_peek_token (parser->lexer);
30764 if (tok->type == CPP_CLOSE_BRACE)
30765 break;
30766 if (tok->type == CPP_EOF)
30767 break;
30769 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30771 cp_lexer_consume_token (parser->lexer);
30772 cp_parser_require_pragma_eol (parser, tok);
30773 error_suppress = false;
30775 else if (!error_suppress)
30777 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30778 error_suppress = true;
30781 substmt = cp_parser_omp_structured_block (parser);
30782 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30783 add_stmt (substmt);
30785 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30787 substmt = pop_stmt_list (stmt);
30789 stmt = make_node (OMP_SECTIONS);
30790 TREE_TYPE (stmt) = void_type_node;
30791 OMP_SECTIONS_BODY (stmt) = substmt;
30793 add_stmt (stmt);
30794 return stmt;
30797 /* OpenMP 2.5:
30798 # pragma omp sections sections-clause[optseq] newline
30799 sections-scope */
30801 #define OMP_SECTIONS_CLAUSE_MASK \
30802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30808 static tree
30809 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30810 char *p_name, omp_clause_mask mask, tree *cclauses)
30812 tree clauses, ret;
30813 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30815 strcat (p_name, " sections");
30816 mask |= OMP_SECTIONS_CLAUSE_MASK;
30817 if (cclauses)
30818 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30820 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30821 cclauses == NULL);
30822 if (cclauses)
30824 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30825 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30828 ret = cp_parser_omp_sections_scope (parser);
30829 if (ret)
30830 OMP_SECTIONS_CLAUSES (ret) = clauses;
30832 return ret;
30835 /* OpenMP 2.5:
30836 # pragma omp parallel parallel-clause[optseq] new-line
30837 structured-block
30838 # pragma omp parallel for parallel-for-clause[optseq] new-line
30839 structured-block
30840 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30841 structured-block
30843 OpenMP 4.0:
30844 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30845 structured-block */
30847 #define OMP_PARALLEL_CLAUSE_MASK \
30848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30858 static tree
30859 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30860 char *p_name, omp_clause_mask mask, tree *cclauses)
30862 tree stmt, clauses, block;
30863 unsigned int save;
30864 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30866 strcat (p_name, " parallel");
30867 mask |= OMP_PARALLEL_CLAUSE_MASK;
30869 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30871 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30872 if (cclauses == NULL)
30873 cclauses = cclauses_buf;
30875 cp_lexer_consume_token (parser->lexer);
30876 if (!flag_openmp) /* flag_openmp_simd */
30877 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30878 block = begin_omp_parallel ();
30879 save = cp_parser_begin_omp_structured_block (parser);
30880 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30881 cp_parser_end_omp_structured_block (parser, save);
30882 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30883 block);
30884 if (ret == NULL_TREE)
30885 return ret;
30886 OMP_PARALLEL_COMBINED (stmt) = 1;
30887 return stmt;
30889 else if (cclauses)
30891 error_at (loc, "expected %<for%> after %qs", p_name);
30892 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30893 return NULL_TREE;
30895 else if (!flag_openmp) /* flag_openmp_simd */
30897 cp_parser_require_pragma_eol (parser, pragma_tok);
30898 return NULL_TREE;
30900 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30903 const char *p = IDENTIFIER_POINTER (id);
30904 if (strcmp (p, "sections") == 0)
30906 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30907 cclauses = cclauses_buf;
30909 cp_lexer_consume_token (parser->lexer);
30910 block = begin_omp_parallel ();
30911 save = cp_parser_begin_omp_structured_block (parser);
30912 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30913 cp_parser_end_omp_structured_block (parser, save);
30914 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30915 block);
30916 OMP_PARALLEL_COMBINED (stmt) = 1;
30917 return stmt;
30921 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30923 block = begin_omp_parallel ();
30924 save = cp_parser_begin_omp_structured_block (parser);
30925 cp_parser_statement (parser, NULL_TREE, false, NULL);
30926 cp_parser_end_omp_structured_block (parser, save);
30927 stmt = finish_omp_parallel (clauses, block);
30928 return stmt;
30931 /* OpenMP 2.5:
30932 # pragma omp single single-clause[optseq] new-line
30933 structured-block */
30935 #define OMP_SINGLE_CLAUSE_MASK \
30936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30941 static tree
30942 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30944 tree stmt = make_node (OMP_SINGLE);
30945 TREE_TYPE (stmt) = void_type_node;
30947 OMP_SINGLE_CLAUSES (stmt)
30948 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30949 "#pragma omp single", pragma_tok);
30950 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30952 return add_stmt (stmt);
30955 /* OpenMP 3.0:
30956 # pragma omp task task-clause[optseq] new-line
30957 structured-block */
30959 #define OMP_TASK_CLAUSE_MASK \
30960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30970 static tree
30971 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30973 tree clauses, block;
30974 unsigned int save;
30976 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30977 "#pragma omp task", pragma_tok);
30978 block = begin_omp_task ();
30979 save = cp_parser_begin_omp_structured_block (parser);
30980 cp_parser_statement (parser, NULL_TREE, false, NULL);
30981 cp_parser_end_omp_structured_block (parser, save);
30982 return finish_omp_task (clauses, block);
30985 /* OpenMP 3.0:
30986 # pragma omp taskwait new-line */
30988 static void
30989 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30991 cp_parser_require_pragma_eol (parser, pragma_tok);
30992 finish_omp_taskwait ();
30995 /* OpenMP 3.1:
30996 # pragma omp taskyield new-line */
30998 static void
30999 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31001 cp_parser_require_pragma_eol (parser, pragma_tok);
31002 finish_omp_taskyield ();
31005 /* OpenMP 4.0:
31006 # pragma omp taskgroup new-line
31007 structured-block */
31009 static tree
31010 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31012 cp_parser_require_pragma_eol (parser, pragma_tok);
31013 return c_finish_omp_taskgroup (input_location,
31014 cp_parser_omp_structured_block (parser));
31018 /* OpenMP 2.5:
31019 # pragma omp threadprivate (variable-list) */
31021 static void
31022 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31024 tree vars;
31026 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31027 cp_parser_require_pragma_eol (parser, pragma_tok);
31029 finish_omp_threadprivate (vars);
31032 /* OpenMP 4.0:
31033 # pragma omp cancel cancel-clause[optseq] new-line */
31035 #define OMP_CANCEL_CLAUSE_MASK \
31036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31042 static void
31043 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31045 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31046 "#pragma omp cancel", pragma_tok);
31047 finish_omp_cancel (clauses);
31050 /* OpenMP 4.0:
31051 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31053 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31059 static void
31060 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31062 tree clauses;
31063 bool point_seen = false;
31065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31067 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31068 const char *p = IDENTIFIER_POINTER (id);
31070 if (strcmp (p, "point") == 0)
31072 cp_lexer_consume_token (parser->lexer);
31073 point_seen = true;
31076 if (!point_seen)
31078 cp_parser_error (parser, "expected %<point%>");
31079 cp_parser_require_pragma_eol (parser, pragma_tok);
31080 return;
31083 clauses = cp_parser_omp_all_clauses (parser,
31084 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31085 "#pragma omp cancellation point",
31086 pragma_tok);
31087 finish_omp_cancellation_point (clauses);
31090 /* OpenMP 4.0:
31091 #pragma omp distribute distribute-clause[optseq] new-line
31092 for-loop */
31094 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31100 static tree
31101 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31102 char *p_name, omp_clause_mask mask, tree *cclauses)
31104 tree clauses, sb, ret;
31105 unsigned int save;
31106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31108 strcat (p_name, " distribute");
31109 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31114 const char *p = IDENTIFIER_POINTER (id);
31115 bool simd = false;
31116 bool parallel = false;
31118 if (strcmp (p, "simd") == 0)
31119 simd = true;
31120 else
31121 parallel = strcmp (p, "parallel") == 0;
31122 if (parallel || simd)
31124 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31125 if (cclauses == NULL)
31126 cclauses = cclauses_buf;
31127 cp_lexer_consume_token (parser->lexer);
31128 if (!flag_openmp) /* flag_openmp_simd */
31130 if (simd)
31131 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31132 cclauses);
31133 else
31134 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31135 cclauses);
31137 sb = begin_omp_structured_block ();
31138 save = cp_parser_begin_omp_structured_block (parser);
31139 if (simd)
31140 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31141 cclauses);
31142 else
31143 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31144 cclauses);
31145 cp_parser_end_omp_structured_block (parser, save);
31146 tree body = finish_omp_structured_block (sb);
31147 if (ret == NULL)
31148 return ret;
31149 ret = make_node (OMP_DISTRIBUTE);
31150 TREE_TYPE (ret) = void_type_node;
31151 OMP_FOR_BODY (ret) = body;
31152 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31153 SET_EXPR_LOCATION (ret, loc);
31154 add_stmt (ret);
31155 return ret;
31158 if (!flag_openmp) /* flag_openmp_simd */
31160 cp_parser_require_pragma_eol (parser, pragma_tok);
31161 return NULL_TREE;
31164 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31165 cclauses == NULL);
31166 if (cclauses)
31168 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31169 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31172 sb = begin_omp_structured_block ();
31173 save = cp_parser_begin_omp_structured_block (parser);
31175 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31177 cp_parser_end_omp_structured_block (parser, save);
31178 add_stmt (finish_omp_structured_block (sb));
31180 return ret;
31183 /* OpenMP 4.0:
31184 # pragma omp teams teams-clause[optseq] new-line
31185 structured-block */
31187 #define OMP_TEAMS_CLAUSE_MASK \
31188 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31196 static tree
31197 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31198 char *p_name, omp_clause_mask mask, tree *cclauses)
31200 tree clauses, sb, ret;
31201 unsigned int save;
31202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31204 strcat (p_name, " teams");
31205 mask |= OMP_TEAMS_CLAUSE_MASK;
31207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31210 const char *p = IDENTIFIER_POINTER (id);
31211 if (strcmp (p, "distribute") == 0)
31213 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31214 if (cclauses == NULL)
31215 cclauses = cclauses_buf;
31217 cp_lexer_consume_token (parser->lexer);
31218 if (!flag_openmp) /* flag_openmp_simd */
31219 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31220 cclauses);
31221 sb = begin_omp_structured_block ();
31222 save = cp_parser_begin_omp_structured_block (parser);
31223 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31224 cclauses);
31225 cp_parser_end_omp_structured_block (parser, save);
31226 tree body = finish_omp_structured_block (sb);
31227 if (ret == NULL)
31228 return ret;
31229 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31230 ret = make_node (OMP_TEAMS);
31231 TREE_TYPE (ret) = void_type_node;
31232 OMP_TEAMS_CLAUSES (ret) = clauses;
31233 OMP_TEAMS_BODY (ret) = body;
31234 return add_stmt (ret);
31237 if (!flag_openmp) /* flag_openmp_simd */
31239 cp_parser_require_pragma_eol (parser, pragma_tok);
31240 return NULL_TREE;
31243 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31244 cclauses == NULL);
31245 if (cclauses)
31247 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31248 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31251 tree stmt = make_node (OMP_TEAMS);
31252 TREE_TYPE (stmt) = void_type_node;
31253 OMP_TEAMS_CLAUSES (stmt) = clauses;
31254 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31256 return add_stmt (stmt);
31259 /* OpenMP 4.0:
31260 # pragma omp target data target-data-clause[optseq] new-line
31261 structured-block */
31263 #define OMP_TARGET_DATA_CLAUSE_MASK \
31264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31268 static tree
31269 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31271 tree stmt = make_node (OMP_TARGET_DATA);
31272 TREE_TYPE (stmt) = void_type_node;
31274 OMP_TARGET_DATA_CLAUSES (stmt)
31275 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31276 "#pragma omp target data", pragma_tok);
31277 keep_next_level (true);
31278 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31280 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31281 return add_stmt (stmt);
31284 /* OpenMP 4.0:
31285 # pragma omp target update target-update-clause[optseq] new-line */
31287 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31293 static bool
31294 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31295 enum pragma_context context)
31297 if (context == pragma_stmt)
31299 error_at (pragma_tok->location,
31300 "%<#pragma omp target update%> may only be "
31301 "used in compound statements");
31302 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31303 return false;
31306 tree clauses
31307 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31308 "#pragma omp target update", pragma_tok);
31309 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31310 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31312 error_at (pragma_tok->location,
31313 "%<#pragma omp target update must contain at least one "
31314 "%<from%> or %<to%> clauses");
31315 return false;
31318 tree stmt = make_node (OMP_TARGET_UPDATE);
31319 TREE_TYPE (stmt) = void_type_node;
31320 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31321 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31322 add_stmt (stmt);
31323 return false;
31326 /* OpenMP 4.0:
31327 # pragma omp target target-clause[optseq] new-line
31328 structured-block */
31330 #define OMP_TARGET_CLAUSE_MASK \
31331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31335 static bool
31336 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31337 enum pragma_context context)
31339 if (context != pragma_stmt && context != pragma_compound)
31341 cp_parser_error (parser, "expected declaration specifiers");
31342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31343 return false;
31346 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31348 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31349 const char *p = IDENTIFIER_POINTER (id);
31351 if (strcmp (p, "teams") == 0)
31353 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31354 char p_name[sizeof ("#pragma omp target teams distribute "
31355 "parallel for simd")];
31357 cp_lexer_consume_token (parser->lexer);
31358 strcpy (p_name, "#pragma omp target");
31359 if (!flag_openmp) /* flag_openmp_simd */
31361 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31362 OMP_TARGET_CLAUSE_MASK,
31363 cclauses);
31364 return stmt != NULL_TREE;
31366 keep_next_level (true);
31367 tree sb = begin_omp_structured_block ();
31368 unsigned save = cp_parser_begin_omp_structured_block (parser);
31369 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31370 OMP_TARGET_CLAUSE_MASK, cclauses);
31371 cp_parser_end_omp_structured_block (parser, save);
31372 tree body = finish_omp_structured_block (sb);
31373 if (ret == NULL_TREE)
31374 return false;
31375 tree stmt = make_node (OMP_TARGET);
31376 TREE_TYPE (stmt) = void_type_node;
31377 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31378 OMP_TARGET_BODY (stmt) = body;
31379 add_stmt (stmt);
31380 return true;
31382 else if (!flag_openmp) /* flag_openmp_simd */
31384 cp_parser_require_pragma_eol (parser, pragma_tok);
31385 return false;
31387 else if (strcmp (p, "data") == 0)
31389 cp_lexer_consume_token (parser->lexer);
31390 cp_parser_omp_target_data (parser, pragma_tok);
31391 return true;
31393 else if (strcmp (p, "update") == 0)
31395 cp_lexer_consume_token (parser->lexer);
31396 return cp_parser_omp_target_update (parser, pragma_tok, context);
31400 tree stmt = make_node (OMP_TARGET);
31401 TREE_TYPE (stmt) = void_type_node;
31403 OMP_TARGET_CLAUSES (stmt)
31404 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31405 "#pragma omp target", pragma_tok);
31406 keep_next_level (true);
31407 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31409 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31410 add_stmt (stmt);
31411 return true;
31414 /* OpenACC 2.0:
31415 # pragma acc cache (variable-list) new-line
31418 static tree
31419 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31421 tree stmt, clauses;
31423 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31424 clauses = finish_omp_clauses (clauses);
31426 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31428 stmt = make_node (OACC_CACHE);
31429 TREE_TYPE (stmt) = void_type_node;
31430 OACC_CACHE_CLAUSES (stmt) = clauses;
31431 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31432 add_stmt (stmt);
31434 return stmt;
31437 /* OpenACC 2.0:
31438 # pragma acc data oacc-data-clause[optseq] new-line
31439 structured-block */
31441 #define OACC_DATA_CLAUSE_MASK \
31442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31454 static tree
31455 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31457 tree stmt, clauses, block;
31458 unsigned int save;
31460 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31461 "#pragma acc data", pragma_tok);
31463 block = begin_omp_parallel ();
31464 save = cp_parser_begin_omp_structured_block (parser);
31465 cp_parser_statement (parser, NULL_TREE, false, NULL);
31466 cp_parser_end_omp_structured_block (parser, save);
31467 stmt = finish_oacc_data (clauses, block);
31468 return stmt;
31471 /* OpenACC 2.0:
31472 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31476 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31478 LOC is the location of the #pragma token.
31481 #define OACC_ENTER_DATA_CLAUSE_MASK \
31482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31490 #define OACC_EXIT_DATA_CLAUSE_MASK \
31491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31497 static tree
31498 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31499 bool enter)
31501 tree stmt, clauses;
31503 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31504 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31506 cp_parser_error (parser, enter
31507 ? "expected %<data%> in %<#pragma acc enter data%>"
31508 : "expected %<data%> in %<#pragma acc exit data%>");
31509 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31510 return NULL_TREE;
31513 const char *p =
31514 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31515 if (strcmp (p, "data") != 0)
31517 cp_parser_error (parser, "invalid pragma");
31518 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31519 return NULL_TREE;
31522 cp_lexer_consume_token (parser->lexer);
31524 if (enter)
31525 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31526 "#pragma acc enter data", pragma_tok);
31527 else
31528 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31529 "#pragma acc exit data", pragma_tok);
31531 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31533 error_at (pragma_tok->location,
31534 "%<#pragma acc enter data%> has no data movement clause");
31535 return NULL_TREE;
31538 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31539 TREE_TYPE (stmt) = void_type_node;
31540 if (enter)
31541 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31542 else
31543 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31544 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31545 add_stmt (stmt);
31546 return stmt;
31549 /* OpenACC 2.0:
31550 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31551 structured-block */
31553 #define OACC_KERNELS_CLAUSE_MASK \
31554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31568 static tree
31569 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31571 tree stmt, clauses, block;
31572 unsigned int save;
31574 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31575 "#pragma acc kernels", pragma_tok);
31577 block = begin_omp_parallel ();
31578 save = cp_parser_begin_omp_structured_block (parser);
31579 cp_parser_statement (parser, NULL_TREE, false, NULL);
31580 cp_parser_end_omp_structured_block (parser, save);
31581 stmt = finish_oacc_kernels (clauses, block);
31582 return stmt;
31585 /* OpenACC 2.0:
31586 # pragma acc loop oacc-loop-clause[optseq] new-line
31587 structured-block */
31589 #define OACC_LOOP_CLAUSE_MASK \
31590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31593 static tree
31594 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31596 tree stmt, clauses, block;
31597 int save;
31599 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31600 "#pragma acc loop", pragma_tok);
31602 block = begin_omp_structured_block ();
31603 save = cp_parser_begin_omp_structured_block (parser);
31604 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31605 cp_parser_end_omp_structured_block (parser, save);
31606 add_stmt (finish_omp_structured_block (block));
31607 return stmt;
31610 /* OpenACC 2.0:
31611 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31612 structured-block */
31614 #define OACC_PARALLEL_CLAUSE_MASK \
31615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31633 static tree
31634 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31636 tree stmt, clauses, block;
31637 unsigned int save;
31639 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31640 "#pragma acc parallel", pragma_tok);
31642 block = begin_omp_parallel ();
31643 save = cp_parser_begin_omp_structured_block (parser);
31644 cp_parser_statement (parser, NULL_TREE, false, NULL);
31645 cp_parser_end_omp_structured_block (parser, save);
31646 stmt = finish_oacc_parallel (clauses, block);
31647 return stmt;
31650 /* OpenACC 2.0:
31651 # pragma acc update oacc-update-clause[optseq] new-line
31654 #define OACC_UPDATE_CLAUSE_MASK \
31655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31662 static tree
31663 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31665 tree stmt, clauses;
31667 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31668 "#pragma acc update", pragma_tok);
31670 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31672 error_at (pragma_tok->location,
31673 "%<#pragma acc update%> must contain at least one "
31674 "%<device%> or %<host/self%> clause");
31675 return NULL_TREE;
31678 stmt = make_node (OACC_UPDATE);
31679 TREE_TYPE (stmt) = void_type_node;
31680 OACC_UPDATE_CLAUSES (stmt) = clauses;
31681 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31682 add_stmt (stmt);
31683 return stmt;
31686 /* OpenACC 2.0:
31687 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31689 LOC is the location of the #pragma token.
31692 #define OACC_WAIT_CLAUSE_MASK \
31693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31695 static tree
31696 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31698 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31699 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31701 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31702 list = cp_parser_oacc_wait_list (parser, loc, list);
31704 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31705 "#pragma acc wait", pragma_tok);
31707 stmt = c_finish_oacc_wait (loc, list, clauses);
31709 return stmt;
31712 /* OpenMP 4.0:
31713 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31715 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31723 static void
31724 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31725 enum pragma_context context)
31727 bool first_p = parser->omp_declare_simd == NULL;
31728 cp_omp_declare_simd_data data;
31729 if (first_p)
31731 data.error_seen = false;
31732 data.fndecl_seen = false;
31733 data.tokens = vNULL;
31734 parser->omp_declare_simd = &data;
31736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31737 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31738 cp_lexer_consume_token (parser->lexer);
31739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31740 parser->omp_declare_simd->error_seen = true;
31741 cp_parser_require_pragma_eol (parser, pragma_tok);
31742 struct cp_token_cache *cp
31743 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31744 parser->omp_declare_simd->tokens.safe_push (cp);
31745 if (first_p)
31747 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31748 cp_parser_pragma (parser, context);
31749 switch (context)
31751 case pragma_external:
31752 cp_parser_declaration (parser);
31753 break;
31754 case pragma_member:
31755 cp_parser_member_declaration (parser);
31756 break;
31757 case pragma_objc_icode:
31758 cp_parser_block_declaration (parser, /*statement_p=*/false);
31759 break;
31760 default:
31761 cp_parser_declaration_statement (parser);
31762 break;
31764 if (parser->omp_declare_simd
31765 && !parser->omp_declare_simd->error_seen
31766 && !parser->omp_declare_simd->fndecl_seen)
31767 error_at (pragma_tok->location,
31768 "%<#pragma omp declare simd%> not immediately followed by "
31769 "function declaration or definition");
31770 data.tokens.release ();
31771 parser->omp_declare_simd = NULL;
31775 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31776 This function is modelled similar to the late parsing of omp declare
31777 simd. */
31779 static tree
31780 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31782 struct cp_token_cache *ce;
31783 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31784 int ii = 0;
31786 if (parser->omp_declare_simd != NULL)
31788 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31789 " marked as a Cilk Plus SIMD-enabled function");
31790 XDELETE (parser->cilk_simd_fn_info);
31791 parser->cilk_simd_fn_info = NULL;
31792 return attrs;
31794 if (!info->error_seen && info->fndecl_seen)
31796 error ("vector attribute not immediately followed by a single function"
31797 " declaration or definition");
31798 info->error_seen = true;
31800 if (info->error_seen)
31801 return attrs;
31803 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31805 tree c, cl;
31807 cp_parser_push_lexer_for_tokens (parser, ce);
31808 parser->lexer->in_pragma = true;
31809 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31810 "SIMD-enabled functions attribute",
31811 NULL);
31812 cp_parser_pop_lexer (parser);
31813 if (cl)
31814 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31816 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31817 TREE_CHAIN (c) = attrs;
31818 attrs = c;
31820 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31821 TREE_CHAIN (c) = attrs;
31822 if (processing_template_decl)
31823 ATTR_IS_DEPENDENT (c) = 1;
31824 attrs = c;
31826 info->fndecl_seen = true;
31827 XDELETE (parser->cilk_simd_fn_info);
31828 parser->cilk_simd_fn_info = NULL;
31829 return attrs;
31832 /* Finalize #pragma omp declare simd clauses after direct declarator has
31833 been parsed, and put that into "omp declare simd" attribute. */
31835 static tree
31836 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31838 struct cp_token_cache *ce;
31839 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31840 int i;
31842 if (!data->error_seen && data->fndecl_seen)
31844 error ("%<#pragma omp declare simd%> not immediately followed by "
31845 "a single function declaration or definition");
31846 data->error_seen = true;
31847 return attrs;
31849 if (data->error_seen)
31850 return attrs;
31852 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31854 tree c, cl;
31856 cp_parser_push_lexer_for_tokens (parser, ce);
31857 parser->lexer->in_pragma = true;
31858 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31859 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31860 cp_lexer_consume_token (parser->lexer);
31861 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31862 "#pragma omp declare simd", pragma_tok);
31863 cp_parser_pop_lexer (parser);
31864 if (cl)
31865 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31866 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31867 TREE_CHAIN (c) = attrs;
31868 if (processing_template_decl)
31869 ATTR_IS_DEPENDENT (c) = 1;
31870 attrs = c;
31873 data->fndecl_seen = true;
31874 return attrs;
31878 /* OpenMP 4.0:
31879 # pragma omp declare target new-line
31880 declarations and definitions
31881 # pragma omp end declare target new-line */
31883 static void
31884 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31887 scope_chain->omp_declare_target_attribute++;
31890 static void
31891 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31893 const char *p = "";
31894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31896 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31897 p = IDENTIFIER_POINTER (id);
31899 if (strcmp (p, "declare") == 0)
31901 cp_lexer_consume_token (parser->lexer);
31902 p = "";
31903 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31905 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31906 p = IDENTIFIER_POINTER (id);
31908 if (strcmp (p, "target") == 0)
31909 cp_lexer_consume_token (parser->lexer);
31910 else
31912 cp_parser_error (parser, "expected %<target%>");
31913 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31914 return;
31917 else
31919 cp_parser_error (parser, "expected %<declare%>");
31920 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31921 return;
31923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31924 if (!scope_chain->omp_declare_target_attribute)
31925 error_at (pragma_tok->location,
31926 "%<#pragma omp end declare target%> without corresponding "
31927 "%<#pragma omp declare target%>");
31928 else
31929 scope_chain->omp_declare_target_attribute--;
31932 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31933 expression and optional initializer clause of
31934 #pragma omp declare reduction. We store the expression(s) as
31935 either 3, 6 or 7 special statements inside of the artificial function's
31936 body. The first two statements are DECL_EXPRs for the artificial
31937 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31938 expression that uses those variables.
31939 If there was any INITIALIZER clause, this is followed by further statements,
31940 the fourth and fifth statements are DECL_EXPRs for the artificial
31941 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31942 constructor variant (first token after open paren is not omp_priv),
31943 then the sixth statement is a statement with the function call expression
31944 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31945 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31946 to initialize the OMP_PRIV artificial variable and there is seventh
31947 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31949 static bool
31950 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31952 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31953 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31954 type = TREE_TYPE (type);
31955 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31956 DECL_ARTIFICIAL (omp_out) = 1;
31957 pushdecl (omp_out);
31958 add_decl_expr (omp_out);
31959 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31960 DECL_ARTIFICIAL (omp_in) = 1;
31961 pushdecl (omp_in);
31962 add_decl_expr (omp_in);
31963 tree combiner;
31964 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31966 keep_next_level (true);
31967 tree block = begin_omp_structured_block ();
31968 combiner = cp_parser_expression (parser);
31969 finish_expr_stmt (combiner);
31970 block = finish_omp_structured_block (block);
31971 add_stmt (block);
31973 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31974 return false;
31976 const char *p = "";
31977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31980 p = IDENTIFIER_POINTER (id);
31983 if (strcmp (p, "initializer") == 0)
31985 cp_lexer_consume_token (parser->lexer);
31986 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31987 return false;
31989 p = "";
31990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31992 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31993 p = IDENTIFIER_POINTER (id);
31996 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31997 DECL_ARTIFICIAL (omp_priv) = 1;
31998 pushdecl (omp_priv);
31999 add_decl_expr (omp_priv);
32000 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32001 DECL_ARTIFICIAL (omp_orig) = 1;
32002 pushdecl (omp_orig);
32003 add_decl_expr (omp_orig);
32005 keep_next_level (true);
32006 block = begin_omp_structured_block ();
32008 bool ctor = false;
32009 if (strcmp (p, "omp_priv") == 0)
32011 bool is_direct_init, is_non_constant_init;
32012 ctor = true;
32013 cp_lexer_consume_token (parser->lexer);
32014 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32015 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32016 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32017 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32018 == CPP_CLOSE_PAREN
32019 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32020 == CPP_CLOSE_PAREN))
32022 finish_omp_structured_block (block);
32023 error ("invalid initializer clause");
32024 return false;
32026 initializer = cp_parser_initializer (parser, &is_direct_init,
32027 &is_non_constant_init);
32028 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32029 NULL_TREE, LOOKUP_ONLYCONVERTING);
32031 else
32033 cp_parser_parse_tentatively (parser);
32034 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32035 /*check_dependency_p=*/true,
32036 /*template_p=*/NULL,
32037 /*declarator_p=*/false,
32038 /*optional_p=*/false);
32039 vec<tree, va_gc> *args;
32040 if (fn_name == error_mark_node
32041 || cp_parser_error_occurred (parser)
32042 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32043 || ((args = cp_parser_parenthesized_expression_list
32044 (parser, non_attr, /*cast_p=*/false,
32045 /*allow_expansion_p=*/true,
32046 /*non_constant_p=*/NULL)),
32047 cp_parser_error_occurred (parser)))
32049 finish_omp_structured_block (block);
32050 cp_parser_abort_tentative_parse (parser);
32051 cp_parser_error (parser, "expected id-expression (arguments)");
32052 return false;
32054 unsigned int i;
32055 tree arg;
32056 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32057 if (arg == omp_priv
32058 || (TREE_CODE (arg) == ADDR_EXPR
32059 && TREE_OPERAND (arg, 0) == omp_priv))
32060 break;
32061 cp_parser_abort_tentative_parse (parser);
32062 if (arg == NULL_TREE)
32063 error ("one of the initializer call arguments should be %<omp_priv%>"
32064 " or %<&omp_priv%>");
32065 initializer = cp_parser_postfix_expression (parser, false, false, false,
32066 false, NULL);
32067 finish_expr_stmt (initializer);
32070 block = finish_omp_structured_block (block);
32071 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32072 add_stmt (block);
32074 if (ctor)
32075 add_decl_expr (omp_orig);
32077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32078 return false;
32081 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32082 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32084 return true;
32087 /* OpenMP 4.0
32088 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32089 initializer-clause[opt] new-line
32091 initializer-clause:
32092 initializer (omp_priv initializer)
32093 initializer (function-name (argument-list)) */
32095 static void
32096 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32097 enum pragma_context)
32099 auto_vec<tree> types;
32100 enum tree_code reduc_code = ERROR_MARK;
32101 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32102 unsigned int i;
32103 cp_token *first_token;
32104 cp_token_cache *cp;
32105 int errs;
32106 void *p;
32108 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32109 p = obstack_alloc (&declarator_obstack, 0);
32111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32112 goto fail;
32114 switch (cp_lexer_peek_token (parser->lexer)->type)
32116 case CPP_PLUS:
32117 reduc_code = PLUS_EXPR;
32118 break;
32119 case CPP_MULT:
32120 reduc_code = MULT_EXPR;
32121 break;
32122 case CPP_MINUS:
32123 reduc_code = MINUS_EXPR;
32124 break;
32125 case CPP_AND:
32126 reduc_code = BIT_AND_EXPR;
32127 break;
32128 case CPP_XOR:
32129 reduc_code = BIT_XOR_EXPR;
32130 break;
32131 case CPP_OR:
32132 reduc_code = BIT_IOR_EXPR;
32133 break;
32134 case CPP_AND_AND:
32135 reduc_code = TRUTH_ANDIF_EXPR;
32136 break;
32137 case CPP_OR_OR:
32138 reduc_code = TRUTH_ORIF_EXPR;
32139 break;
32140 case CPP_NAME:
32141 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32142 break;
32143 default:
32144 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32145 "%<|%>, %<&&%>, %<||%> or identifier");
32146 goto fail;
32149 if (reduc_code != ERROR_MARK)
32150 cp_lexer_consume_token (parser->lexer);
32152 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32153 if (reduc_id == error_mark_node)
32154 goto fail;
32156 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32157 goto fail;
32159 /* Types may not be defined in declare reduction type list. */
32160 const char *saved_message;
32161 saved_message = parser->type_definition_forbidden_message;
32162 parser->type_definition_forbidden_message
32163 = G_("types may not be defined in declare reduction type list");
32164 bool saved_colon_corrects_to_scope_p;
32165 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32166 parser->colon_corrects_to_scope_p = false;
32167 bool saved_colon_doesnt_start_class_def_p;
32168 saved_colon_doesnt_start_class_def_p
32169 = parser->colon_doesnt_start_class_def_p;
32170 parser->colon_doesnt_start_class_def_p = true;
32172 while (true)
32174 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32175 type = cp_parser_type_id (parser);
32176 if (type == error_mark_node)
32178 else if (ARITHMETIC_TYPE_P (type)
32179 && (orig_reduc_id == NULL_TREE
32180 || (TREE_CODE (type) != COMPLEX_TYPE
32181 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32182 "min") == 0
32183 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32184 "max") == 0))))
32185 error_at (loc, "predeclared arithmetic type %qT in "
32186 "%<#pragma omp declare reduction%>", type);
32187 else if (TREE_CODE (type) == FUNCTION_TYPE
32188 || TREE_CODE (type) == METHOD_TYPE
32189 || TREE_CODE (type) == ARRAY_TYPE)
32190 error_at (loc, "function or array type %qT in "
32191 "%<#pragma omp declare reduction%>", type);
32192 else if (TREE_CODE (type) == REFERENCE_TYPE)
32193 error_at (loc, "reference type %qT in "
32194 "%<#pragma omp declare reduction%>", type);
32195 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32196 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32197 "%<#pragma omp declare reduction%>", type);
32198 else
32199 types.safe_push (type);
32201 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32202 cp_lexer_consume_token (parser->lexer);
32203 else
32204 break;
32207 /* Restore the saved message. */
32208 parser->type_definition_forbidden_message = saved_message;
32209 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32210 parser->colon_doesnt_start_class_def_p
32211 = saved_colon_doesnt_start_class_def_p;
32213 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32214 || types.is_empty ())
32216 fail:
32217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32218 goto done;
32221 first_token = cp_lexer_peek_token (parser->lexer);
32222 cp = NULL;
32223 errs = errorcount;
32224 FOR_EACH_VEC_ELT (types, i, type)
32226 tree fntype
32227 = build_function_type_list (void_type_node,
32228 cp_build_reference_type (type, false),
32229 NULL_TREE);
32230 tree this_reduc_id = reduc_id;
32231 if (!dependent_type_p (type))
32232 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32233 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32234 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32235 DECL_ARTIFICIAL (fndecl) = 1;
32236 DECL_EXTERNAL (fndecl) = 1;
32237 DECL_DECLARED_INLINE_P (fndecl) = 1;
32238 DECL_IGNORED_P (fndecl) = 1;
32239 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32240 DECL_ATTRIBUTES (fndecl)
32241 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32242 DECL_ATTRIBUTES (fndecl));
32243 if (processing_template_decl)
32244 fndecl = push_template_decl (fndecl);
32245 bool block_scope = false;
32246 tree block = NULL_TREE;
32247 if (current_function_decl)
32249 block_scope = true;
32250 DECL_CONTEXT (fndecl) = global_namespace;
32251 if (!processing_template_decl)
32252 pushdecl (fndecl);
32254 else if (current_class_type)
32256 if (cp == NULL)
32258 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32259 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32260 cp_lexer_consume_token (parser->lexer);
32261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32262 goto fail;
32263 cp = cp_token_cache_new (first_token,
32264 cp_lexer_peek_nth_token (parser->lexer,
32265 2));
32267 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32268 finish_member_declaration (fndecl);
32269 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32270 DECL_PENDING_INLINE_P (fndecl) = 1;
32271 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32272 continue;
32274 else
32276 DECL_CONTEXT (fndecl) = current_namespace;
32277 pushdecl (fndecl);
32279 if (!block_scope)
32280 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32281 else
32282 block = begin_omp_structured_block ();
32283 if (cp)
32285 cp_parser_push_lexer_for_tokens (parser, cp);
32286 parser->lexer->in_pragma = true;
32288 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32290 if (!block_scope)
32291 finish_function (0);
32292 else
32293 DECL_CONTEXT (fndecl) = current_function_decl;
32294 if (cp)
32295 cp_parser_pop_lexer (parser);
32296 goto fail;
32298 if (cp)
32299 cp_parser_pop_lexer (parser);
32300 if (!block_scope)
32301 finish_function (0);
32302 else
32304 DECL_CONTEXT (fndecl) = current_function_decl;
32305 block = finish_omp_structured_block (block);
32306 if (TREE_CODE (block) == BIND_EXPR)
32307 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32308 else if (TREE_CODE (block) == STATEMENT_LIST)
32309 DECL_SAVED_TREE (fndecl) = block;
32310 if (processing_template_decl)
32311 add_decl_expr (fndecl);
32313 cp_check_omp_declare_reduction (fndecl);
32314 if (cp == NULL && types.length () > 1)
32315 cp = cp_token_cache_new (first_token,
32316 cp_lexer_peek_nth_token (parser->lexer, 2));
32317 if (errs != errorcount)
32318 break;
32321 cp_parser_require_pragma_eol (parser, pragma_tok);
32323 done:
32324 /* Free any declarators allocated. */
32325 obstack_free (&declarator_obstack, p);
32328 /* OpenMP 4.0
32329 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32330 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32331 initializer-clause[opt] new-line
32332 #pragma omp declare target new-line */
32334 static void
32335 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32336 enum pragma_context context)
32338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32340 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32341 const char *p = IDENTIFIER_POINTER (id);
32343 if (strcmp (p, "simd") == 0)
32345 cp_lexer_consume_token (parser->lexer);
32346 cp_parser_omp_declare_simd (parser, pragma_tok,
32347 context);
32348 return;
32350 cp_ensure_no_omp_declare_simd (parser);
32351 if (strcmp (p, "reduction") == 0)
32353 cp_lexer_consume_token (parser->lexer);
32354 cp_parser_omp_declare_reduction (parser, pragma_tok,
32355 context);
32356 return;
32358 if (!flag_openmp) /* flag_openmp_simd */
32360 cp_parser_require_pragma_eol (parser, pragma_tok);
32361 return;
32363 if (strcmp (p, "target") == 0)
32365 cp_lexer_consume_token (parser->lexer);
32366 cp_parser_omp_declare_target (parser, pragma_tok);
32367 return;
32370 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32371 "or %<target%>");
32372 cp_parser_require_pragma_eol (parser, pragma_tok);
32375 /* Main entry point to OpenMP statement pragmas. */
32377 static void
32378 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32380 tree stmt;
32381 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32382 omp_clause_mask mask (0);
32384 switch (pragma_tok->pragma_kind)
32386 case PRAGMA_OACC_CACHE:
32387 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32388 break;
32389 case PRAGMA_OACC_DATA:
32390 stmt = cp_parser_oacc_data (parser, pragma_tok);
32391 break;
32392 case PRAGMA_OACC_ENTER_DATA:
32393 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32394 break;
32395 case PRAGMA_OACC_EXIT_DATA:
32396 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32397 break;
32398 case PRAGMA_OACC_KERNELS:
32399 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32400 break;
32401 case PRAGMA_OACC_LOOP:
32402 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32403 break;
32404 case PRAGMA_OACC_PARALLEL:
32405 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32406 break;
32407 case PRAGMA_OACC_UPDATE:
32408 stmt = cp_parser_oacc_update (parser, pragma_tok);
32409 break;
32410 case PRAGMA_OACC_WAIT:
32411 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32412 break;
32413 case PRAGMA_OMP_ATOMIC:
32414 cp_parser_omp_atomic (parser, pragma_tok);
32415 return;
32416 case PRAGMA_OMP_CRITICAL:
32417 stmt = cp_parser_omp_critical (parser, pragma_tok);
32418 break;
32419 case PRAGMA_OMP_DISTRIBUTE:
32420 strcpy (p_name, "#pragma omp");
32421 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32422 break;
32423 case PRAGMA_OMP_FOR:
32424 strcpy (p_name, "#pragma omp");
32425 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32426 break;
32427 case PRAGMA_OMP_MASTER:
32428 stmt = cp_parser_omp_master (parser, pragma_tok);
32429 break;
32430 case PRAGMA_OMP_ORDERED:
32431 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32432 break;
32433 case PRAGMA_OMP_PARALLEL:
32434 strcpy (p_name, "#pragma omp");
32435 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32436 break;
32437 case PRAGMA_OMP_SECTIONS:
32438 strcpy (p_name, "#pragma omp");
32439 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32440 break;
32441 case PRAGMA_OMP_SIMD:
32442 strcpy (p_name, "#pragma omp");
32443 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32444 break;
32445 case PRAGMA_OMP_SINGLE:
32446 stmt = cp_parser_omp_single (parser, pragma_tok);
32447 break;
32448 case PRAGMA_OMP_TASK:
32449 stmt = cp_parser_omp_task (parser, pragma_tok);
32450 break;
32451 case PRAGMA_OMP_TASKGROUP:
32452 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32453 break;
32454 case PRAGMA_OMP_TEAMS:
32455 strcpy (p_name, "#pragma omp");
32456 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32457 break;
32458 default:
32459 gcc_unreachable ();
32462 if (stmt)
32463 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32466 /* Transactional Memory parsing routines. */
32468 /* Parse a transaction attribute.
32470 txn-attribute:
32471 attribute
32472 [ [ identifier ] ]
32474 ??? Simplify this when C++0x bracket attributes are
32475 implemented properly. */
32477 static tree
32478 cp_parser_txn_attribute_opt (cp_parser *parser)
32480 cp_token *token;
32481 tree attr_name, attr = NULL;
32483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32484 return cp_parser_attributes_opt (parser);
32486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32487 return NULL_TREE;
32488 cp_lexer_consume_token (parser->lexer);
32489 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32490 goto error1;
32492 token = cp_lexer_peek_token (parser->lexer);
32493 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32495 token = cp_lexer_consume_token (parser->lexer);
32497 attr_name = (token->type == CPP_KEYWORD
32498 /* For keywords, use the canonical spelling,
32499 not the parsed identifier. */
32500 ? ridpointers[(int) token->keyword]
32501 : token->u.value);
32502 attr = build_tree_list (attr_name, NULL_TREE);
32504 else
32505 cp_parser_error (parser, "expected identifier");
32507 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32508 error1:
32509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32510 return attr;
32513 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32515 transaction-statement:
32516 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32517 compound-statement
32518 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32521 static tree
32522 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32524 unsigned char old_in = parser->in_transaction;
32525 unsigned char this_in = 1, new_in;
32526 cp_token *token;
32527 tree stmt, attrs, noex;
32529 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32530 || keyword == RID_TRANSACTION_RELAXED);
32531 token = cp_parser_require_keyword (parser, keyword,
32532 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32533 : RT_TRANSACTION_RELAXED));
32534 gcc_assert (token != NULL);
32536 if (keyword == RID_TRANSACTION_RELAXED)
32537 this_in |= TM_STMT_ATTR_RELAXED;
32538 else
32540 attrs = cp_parser_txn_attribute_opt (parser);
32541 if (attrs)
32542 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32545 /* Parse a noexcept specification. */
32546 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32548 /* Keep track if we're in the lexical scope of an outer transaction. */
32549 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32551 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32553 parser->in_transaction = new_in;
32554 cp_parser_compound_statement (parser, NULL, false, false);
32555 parser->in_transaction = old_in;
32557 finish_transaction_stmt (stmt, NULL, this_in, noex);
32559 return stmt;
32562 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32564 transaction-expression:
32565 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32566 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32569 static tree
32570 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32572 unsigned char old_in = parser->in_transaction;
32573 unsigned char this_in = 1;
32574 cp_token *token;
32575 tree expr, noex;
32576 bool noex_expr;
32578 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32579 || keyword == RID_TRANSACTION_RELAXED);
32581 if (!flag_tm)
32582 error (keyword == RID_TRANSACTION_RELAXED
32583 ? G_("%<__transaction_relaxed%> without transactional memory "
32584 "support enabled")
32585 : G_("%<__transaction_atomic%> without transactional memory "
32586 "support enabled"));
32588 token = cp_parser_require_keyword (parser, keyword,
32589 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32590 : RT_TRANSACTION_RELAXED));
32591 gcc_assert (token != NULL);
32593 if (keyword == RID_TRANSACTION_RELAXED)
32594 this_in |= TM_STMT_ATTR_RELAXED;
32596 /* Set this early. This might mean that we allow transaction_cancel in
32597 an expression that we find out later actually has to be a constexpr.
32598 However, we expect that cxx_constant_value will be able to deal with
32599 this; also, if the noexcept has no constexpr, then what we parse next
32600 really is a transaction's body. */
32601 parser->in_transaction = this_in;
32603 /* Parse a noexcept specification. */
32604 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32605 true);
32607 if (!noex || !noex_expr
32608 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32610 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32612 expr = cp_parser_expression (parser);
32613 expr = finish_parenthesized_expr (expr);
32615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32617 else
32619 /* The only expression that is available got parsed for the noexcept
32620 already. noexcept is true then. */
32621 expr = noex;
32622 noex = boolean_true_node;
32625 expr = build_transaction_expr (token->location, expr, this_in, noex);
32626 parser->in_transaction = old_in;
32628 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32629 return error_mark_node;
32631 return (flag_tm ? expr : error_mark_node);
32634 /* Parse a function-transaction-block.
32636 function-transaction-block:
32637 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32638 function-body
32639 __transaction_atomic txn-attribute[opt] function-try-block
32640 __transaction_relaxed ctor-initializer[opt] function-body
32641 __transaction_relaxed function-try-block
32644 static bool
32645 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32647 unsigned char old_in = parser->in_transaction;
32648 unsigned char new_in = 1;
32649 tree compound_stmt, stmt, attrs;
32650 bool ctor_initializer_p;
32651 cp_token *token;
32653 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32654 || keyword == RID_TRANSACTION_RELAXED);
32655 token = cp_parser_require_keyword (parser, keyword,
32656 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32657 : RT_TRANSACTION_RELAXED));
32658 gcc_assert (token != NULL);
32660 if (keyword == RID_TRANSACTION_RELAXED)
32661 new_in |= TM_STMT_ATTR_RELAXED;
32662 else
32664 attrs = cp_parser_txn_attribute_opt (parser);
32665 if (attrs)
32666 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32669 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32671 parser->in_transaction = new_in;
32673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32674 ctor_initializer_p = cp_parser_function_try_block (parser);
32675 else
32676 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32677 (parser, /*in_function_try_block=*/false);
32679 parser->in_transaction = old_in;
32681 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32683 return ctor_initializer_p;
32686 /* Parse a __transaction_cancel statement.
32688 cancel-statement:
32689 __transaction_cancel txn-attribute[opt] ;
32690 __transaction_cancel txn-attribute[opt] throw-expression ;
32692 ??? Cancel and throw is not yet implemented. */
32694 static tree
32695 cp_parser_transaction_cancel (cp_parser *parser)
32697 cp_token *token;
32698 bool is_outer = false;
32699 tree stmt, attrs;
32701 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32702 RT_TRANSACTION_CANCEL);
32703 gcc_assert (token != NULL);
32705 attrs = cp_parser_txn_attribute_opt (parser);
32706 if (attrs)
32707 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32709 /* ??? Parse cancel-and-throw here. */
32711 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32713 if (!flag_tm)
32715 error_at (token->location, "%<__transaction_cancel%> without "
32716 "transactional memory support enabled");
32717 return error_mark_node;
32719 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32721 error_at (token->location, "%<__transaction_cancel%> within a "
32722 "%<__transaction_relaxed%>");
32723 return error_mark_node;
32725 else if (is_outer)
32727 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32728 && !is_tm_may_cancel_outer (current_function_decl))
32730 error_at (token->location, "outer %<__transaction_cancel%> not "
32731 "within outer %<__transaction_atomic%>");
32732 error_at (token->location,
32733 " or a %<transaction_may_cancel_outer%> function");
32734 return error_mark_node;
32737 else if (parser->in_transaction == 0)
32739 error_at (token->location, "%<__transaction_cancel%> not within "
32740 "%<__transaction_atomic%>");
32741 return error_mark_node;
32744 stmt = build_tm_abort_call (token->location, is_outer);
32745 add_stmt (stmt);
32747 return stmt;
32750 /* The parser. */
32752 static GTY (()) cp_parser *the_parser;
32755 /* Special handling for the first token or line in the file. The first
32756 thing in the file might be #pragma GCC pch_preprocess, which loads a
32757 PCH file, which is a GC collection point. So we need to handle this
32758 first pragma without benefit of an existing lexer structure.
32760 Always returns one token to the caller in *FIRST_TOKEN. This is
32761 either the true first token of the file, or the first token after
32762 the initial pragma. */
32764 static void
32765 cp_parser_initial_pragma (cp_token *first_token)
32767 tree name = NULL;
32769 cp_lexer_get_preprocessor_token (NULL, first_token);
32770 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32771 return;
32773 cp_lexer_get_preprocessor_token (NULL, first_token);
32774 if (first_token->type == CPP_STRING)
32776 name = first_token->u.value;
32778 cp_lexer_get_preprocessor_token (NULL, first_token);
32779 if (first_token->type != CPP_PRAGMA_EOL)
32780 error_at (first_token->location,
32781 "junk at end of %<#pragma GCC pch_preprocess%>");
32783 else
32784 error_at (first_token->location, "expected string literal");
32786 /* Skip to the end of the pragma. */
32787 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32788 cp_lexer_get_preprocessor_token (NULL, first_token);
32790 /* Now actually load the PCH file. */
32791 if (name)
32792 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32794 /* Read one more token to return to our caller. We have to do this
32795 after reading the PCH file in, since its pointers have to be
32796 live. */
32797 cp_lexer_get_preprocessor_token (NULL, first_token);
32800 /* Parses the grainsize pragma for the _Cilk_for statement.
32801 Syntax:
32802 #pragma cilk grainsize = <VALUE>. */
32804 static void
32805 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32807 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32809 tree exp = cp_parser_binary_expression (parser, false, false,
32810 PREC_NOT_OPERATOR, NULL);
32811 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32812 if (!exp || exp == error_mark_node)
32814 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32815 return;
32818 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32820 cp_parser_cilk_for (parser, exp);
32821 else
32822 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32823 "%<#pragma cilk grainsize%> is not followed by "
32824 "%<_Cilk_for%>");
32825 return;
32827 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32830 /* Normal parsing of a pragma token. Here we can (and must) use the
32831 regular lexer. */
32833 static bool
32834 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32836 cp_token *pragma_tok;
32837 unsigned int id;
32839 pragma_tok = cp_lexer_consume_token (parser->lexer);
32840 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32841 parser->lexer->in_pragma = true;
32843 id = pragma_tok->pragma_kind;
32844 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32845 cp_ensure_no_omp_declare_simd (parser);
32846 switch (id)
32848 case PRAGMA_GCC_PCH_PREPROCESS:
32849 error_at (pragma_tok->location,
32850 "%<#pragma GCC pch_preprocess%> must be first");
32851 break;
32853 case PRAGMA_OMP_BARRIER:
32854 switch (context)
32856 case pragma_compound:
32857 cp_parser_omp_barrier (parser, pragma_tok);
32858 return false;
32859 case pragma_stmt:
32860 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32861 "used in compound statements");
32862 break;
32863 default:
32864 goto bad_stmt;
32866 break;
32868 case PRAGMA_OMP_FLUSH:
32869 switch (context)
32871 case pragma_compound:
32872 cp_parser_omp_flush (parser, pragma_tok);
32873 return false;
32874 case pragma_stmt:
32875 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32876 "used in compound statements");
32877 break;
32878 default:
32879 goto bad_stmt;
32881 break;
32883 case PRAGMA_OMP_TASKWAIT:
32884 switch (context)
32886 case pragma_compound:
32887 cp_parser_omp_taskwait (parser, pragma_tok);
32888 return false;
32889 case pragma_stmt:
32890 error_at (pragma_tok->location,
32891 "%<#pragma omp taskwait%> may only be "
32892 "used in compound statements");
32893 break;
32894 default:
32895 goto bad_stmt;
32897 break;
32899 case PRAGMA_OMP_TASKYIELD:
32900 switch (context)
32902 case pragma_compound:
32903 cp_parser_omp_taskyield (parser, pragma_tok);
32904 return false;
32905 case pragma_stmt:
32906 error_at (pragma_tok->location,
32907 "%<#pragma omp taskyield%> may only be "
32908 "used in compound statements");
32909 break;
32910 default:
32911 goto bad_stmt;
32913 break;
32915 case PRAGMA_OMP_CANCEL:
32916 switch (context)
32918 case pragma_compound:
32919 cp_parser_omp_cancel (parser, pragma_tok);
32920 return false;
32921 case pragma_stmt:
32922 error_at (pragma_tok->location,
32923 "%<#pragma omp cancel%> may only be "
32924 "used in compound statements");
32925 break;
32926 default:
32927 goto bad_stmt;
32929 break;
32931 case PRAGMA_OMP_CANCELLATION_POINT:
32932 switch (context)
32934 case pragma_compound:
32935 cp_parser_omp_cancellation_point (parser, pragma_tok);
32936 return false;
32937 case pragma_stmt:
32938 error_at (pragma_tok->location,
32939 "%<#pragma omp cancellation point%> may only be "
32940 "used in compound statements");
32941 break;
32942 default:
32943 goto bad_stmt;
32945 break;
32947 case PRAGMA_OMP_THREADPRIVATE:
32948 cp_parser_omp_threadprivate (parser, pragma_tok);
32949 return false;
32951 case PRAGMA_OMP_DECLARE_REDUCTION:
32952 cp_parser_omp_declare (parser, pragma_tok, context);
32953 return false;
32955 case PRAGMA_OACC_CACHE:
32956 case PRAGMA_OACC_DATA:
32957 case PRAGMA_OACC_ENTER_DATA:
32958 case PRAGMA_OACC_EXIT_DATA:
32959 case PRAGMA_OACC_KERNELS:
32960 case PRAGMA_OACC_PARALLEL:
32961 case PRAGMA_OACC_LOOP:
32962 case PRAGMA_OACC_UPDATE:
32963 case PRAGMA_OACC_WAIT:
32964 case PRAGMA_OMP_ATOMIC:
32965 case PRAGMA_OMP_CRITICAL:
32966 case PRAGMA_OMP_DISTRIBUTE:
32967 case PRAGMA_OMP_FOR:
32968 case PRAGMA_OMP_MASTER:
32969 case PRAGMA_OMP_ORDERED:
32970 case PRAGMA_OMP_PARALLEL:
32971 case PRAGMA_OMP_SECTIONS:
32972 case PRAGMA_OMP_SIMD:
32973 case PRAGMA_OMP_SINGLE:
32974 case PRAGMA_OMP_TASK:
32975 case PRAGMA_OMP_TASKGROUP:
32976 case PRAGMA_OMP_TEAMS:
32977 if (context != pragma_stmt && context != pragma_compound)
32978 goto bad_stmt;
32979 cp_parser_omp_construct (parser, pragma_tok);
32980 return true;
32982 case PRAGMA_OMP_TARGET:
32983 return cp_parser_omp_target (parser, pragma_tok, context);
32985 case PRAGMA_OMP_END_DECLARE_TARGET:
32986 cp_parser_omp_end_declare_target (parser, pragma_tok);
32987 return false;
32989 case PRAGMA_OMP_SECTION:
32990 error_at (pragma_tok->location,
32991 "%<#pragma omp section%> may only be used in "
32992 "%<#pragma omp sections%> construct");
32993 break;
32995 case PRAGMA_IVDEP:
32997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32998 cp_token *tok;
32999 tok = cp_lexer_peek_token (the_parser->lexer);
33000 if (tok->type != CPP_KEYWORD
33001 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33002 && tok->keyword != RID_DO))
33004 cp_parser_error (parser, "for, while or do statement expected");
33005 return false;
33007 cp_parser_iteration_statement (parser, true);
33008 return true;
33011 case PRAGMA_CILK_SIMD:
33012 if (context == pragma_external)
33014 error_at (pragma_tok->location,
33015 "%<#pragma simd%> must be inside a function");
33016 break;
33018 cp_parser_cilk_simd (parser, pragma_tok);
33019 return true;
33021 case PRAGMA_CILK_GRAINSIZE:
33022 if (context == pragma_external)
33024 error_at (pragma_tok->location,
33025 "%<#pragma cilk grainsize%> must be inside a function");
33026 break;
33029 /* Ignore the pragma if Cilk Plus is not enabled. */
33030 if (flag_cilkplus)
33032 cp_parser_cilk_grainsize (parser, pragma_tok);
33033 return true;
33035 else
33037 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33038 "%<#pragma cilk grainsize%>");
33039 break;
33042 default:
33043 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33044 c_invoke_pragma_handler (id);
33045 break;
33047 bad_stmt:
33048 cp_parser_error (parser, "expected declaration specifiers");
33049 break;
33052 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33053 return false;
33056 /* The interface the pragma parsers have to the lexer. */
33058 enum cpp_ttype
33059 pragma_lex (tree *value)
33061 cp_token *tok;
33062 enum cpp_ttype ret;
33064 tok = cp_lexer_peek_token (the_parser->lexer);
33066 ret = tok->type;
33067 *value = tok->u.value;
33069 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33070 ret = CPP_EOF;
33071 else if (ret == CPP_STRING)
33072 *value = cp_parser_string_literal (the_parser, false, false);
33073 else
33075 cp_lexer_consume_token (the_parser->lexer);
33076 if (ret == CPP_KEYWORD)
33077 ret = CPP_NAME;
33080 return ret;
33084 /* External interface. */
33086 /* Parse one entire translation unit. */
33088 void
33089 c_parse_file (void)
33091 static bool already_called = false;
33093 if (already_called)
33094 fatal_error ("inter-module optimizations not implemented for C++");
33095 already_called = true;
33097 the_parser = cp_parser_new ();
33098 push_deferring_access_checks (flag_access_control
33099 ? dk_no_deferred : dk_no_check);
33100 cp_parser_translation_unit (the_parser);
33101 the_parser = NULL;
33104 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33105 vectorlength clause:
33106 Syntax:
33107 vectorlength ( constant-expression ) */
33109 static tree
33110 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33111 bool is_simd_fn)
33113 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33114 tree expr;
33115 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33116 safelen clause. Thus, vectorlength is represented as OMP 4.0
33117 safelen. For SIMD-enabled function it is represented by OMP 4.0
33118 simdlen. */
33119 if (!is_simd_fn)
33120 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33121 loc);
33122 else
33123 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33124 loc);
33126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33127 return error_mark_node;
33129 expr = cp_parser_constant_expression (parser);
33130 expr = maybe_constant_value (expr);
33132 /* If expr == error_mark_node, then don't emit any errors nor
33133 create a clause. if any of the above functions returns
33134 error mark node then they would have emitted an error message. */
33135 if (expr == error_mark_node)
33137 else if (!TREE_TYPE (expr)
33138 || !TREE_CONSTANT (expr)
33139 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33140 error_at (loc, "vectorlength must be an integer constant");
33141 else if (TREE_CONSTANT (expr)
33142 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33143 error_at (loc, "vectorlength must be a power of 2");
33144 else
33146 tree c;
33147 if (!is_simd_fn)
33149 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33150 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33151 OMP_CLAUSE_CHAIN (c) = clauses;
33152 clauses = c;
33154 else
33156 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33157 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33158 OMP_CLAUSE_CHAIN (c) = clauses;
33159 clauses = c;
33163 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33164 return error_mark_node;
33165 return clauses;
33168 /* Handles the Cilk Plus #pragma simd linear clause.
33169 Syntax:
33170 linear ( simd-linear-variable-list )
33172 simd-linear-variable-list:
33173 simd-linear-variable
33174 simd-linear-variable-list , simd-linear-variable
33176 simd-linear-variable:
33177 id-expression
33178 id-expression : simd-linear-step
33180 simd-linear-step:
33181 conditional-expression */
33183 static tree
33184 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33189 return clauses;
33190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33192 cp_parser_error (parser, "expected identifier");
33193 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33194 return error_mark_node;
33197 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33198 parser->colon_corrects_to_scope_p = false;
33199 while (1)
33201 cp_token *token = cp_lexer_peek_token (parser->lexer);
33202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33204 cp_parser_error (parser, "expected variable-name");
33205 clauses = error_mark_node;
33206 break;
33209 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33210 false, false);
33211 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33212 token->location);
33213 if (decl == error_mark_node)
33215 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33216 token->location);
33217 clauses = error_mark_node;
33219 else
33221 tree e = NULL_TREE;
33222 tree step_size = integer_one_node;
33224 /* If present, parse the linear step. Otherwise, assume the default
33225 value of 1. */
33226 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33228 cp_lexer_consume_token (parser->lexer);
33230 e = cp_parser_assignment_expression (parser);
33231 e = maybe_constant_value (e);
33233 if (e == error_mark_node)
33235 /* If an error has occurred, then the whole pragma is
33236 considered ill-formed. Thus, no reason to keep
33237 parsing. */
33238 clauses = error_mark_node;
33239 break;
33241 else if (type_dependent_expression_p (e)
33242 || value_dependent_expression_p (e)
33243 || (TREE_TYPE (e)
33244 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33245 && (TREE_CONSTANT (e)
33246 || DECL_P (e))))
33247 step_size = e;
33248 else
33249 cp_parser_error (parser,
33250 "step size must be an integer constant "
33251 "expression or an integer variable");
33254 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33255 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33256 OMP_CLAUSE_DECL (l) = decl;
33257 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33258 OMP_CLAUSE_CHAIN (l) = clauses;
33259 clauses = l;
33261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33262 cp_lexer_consume_token (parser->lexer);
33263 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33264 break;
33265 else
33267 error_at (cp_lexer_peek_token (parser->lexer)->location,
33268 "expected %<,%> or %<)%> after %qE", decl);
33269 clauses = error_mark_node;
33270 break;
33273 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33274 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33275 return clauses;
33278 /* Returns the name of the next clause. If the clause is not
33279 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33280 token is not consumed. Otherwise, the appropriate enum from the
33281 pragma_simd_clause is returned and the token is consumed. */
33283 static pragma_omp_clause
33284 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33286 pragma_omp_clause clause_type;
33287 cp_token *token = cp_lexer_peek_token (parser->lexer);
33289 if (token->keyword == RID_PRIVATE)
33290 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33291 else if (!token->u.value || token->type != CPP_NAME)
33292 return PRAGMA_CILK_CLAUSE_NONE;
33293 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33294 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33295 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33296 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33297 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33298 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33299 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33300 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33301 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33302 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33303 else
33304 return PRAGMA_CILK_CLAUSE_NONE;
33306 cp_lexer_consume_token (parser->lexer);
33307 return clause_type;
33310 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33312 static tree
33313 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33315 tree clauses = NULL_TREE;
33317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33318 && clauses != error_mark_node)
33320 pragma_omp_clause c_kind;
33321 c_kind = cp_parser_cilk_simd_clause_name (parser);
33322 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33323 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33324 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33325 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33326 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33327 /* Use the OpenMP 4.0 equivalent function. */
33328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33329 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33330 /* Use the OpenMP 4.0 equivalent function. */
33331 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33332 clauses);
33333 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33334 /* Use the OMP 4.0 equivalent function. */
33335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33336 clauses);
33337 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33338 /* Use the OMP 4.0 equivalent function. */
33339 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33340 else
33342 clauses = error_mark_node;
33343 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33344 break;
33348 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33350 if (clauses == error_mark_node)
33351 return error_mark_node;
33352 else
33353 return c_finish_cilk_clauses (clauses);
33356 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33358 static void
33359 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33361 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33363 if (clauses == error_mark_node)
33364 return;
33366 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33368 error_at (cp_lexer_peek_token (parser->lexer)->location,
33369 "for statement expected");
33370 return;
33373 tree sb = begin_omp_structured_block ();
33374 int save = cp_parser_begin_omp_structured_block (parser);
33375 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33376 if (ret)
33377 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33378 cp_parser_end_omp_structured_block (parser, save);
33379 add_stmt (finish_omp_structured_block (sb));
33382 /* Main entry-point for parsing Cilk Plus _Cilk_for
33383 loops. The return value is error_mark_node
33384 when errors happen and CILK_FOR tree on success. */
33386 static tree
33387 cp_parser_cilk_for (cp_parser *parser, tree grain)
33389 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33390 gcc_unreachable ();
33392 tree sb = begin_omp_structured_block ();
33393 int save = cp_parser_begin_omp_structured_block (parser);
33395 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33396 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33397 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33398 clauses = finish_omp_clauses (clauses);
33400 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33401 if (ret)
33402 cpp_validate_cilk_plus_loop (ret);
33403 else
33404 ret = error_mark_node;
33406 cp_parser_end_omp_structured_block (parser, save);
33407 add_stmt (finish_omp_structured_block (sb));
33408 return ret;
33411 /* Create an identifier for a generic parameter type (a synthesized
33412 template parameter implied by `auto' or a concept identifier). */
33414 static GTY(()) int generic_parm_count;
33415 static tree
33416 make_generic_type_name ()
33418 char buf[32];
33419 sprintf (buf, "auto:%d", ++generic_parm_count);
33420 return get_identifier (buf);
33423 /* Predicate that behaves as is_auto_or_concept but matches the parent
33424 node of the generic type rather than the generic type itself. This
33425 allows for type transformation in add_implicit_template_parms. */
33427 static inline bool
33428 tree_type_is_auto_or_concept (const_tree t)
33430 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33433 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33434 (creating a new template parameter list if necessary). Returns the newly
33435 created template type parm. */
33437 tree
33438 synthesize_implicit_template_parm (cp_parser *parser)
33440 gcc_assert (current_binding_level->kind == sk_function_parms);
33442 /* We are either continuing a function template that already contains implicit
33443 template parameters, creating a new fully-implicit function template, or
33444 extending an existing explicit function template with implicit template
33445 parameters. */
33447 cp_binding_level *const entry_scope = current_binding_level;
33449 bool become_template = false;
33450 cp_binding_level *parent_scope = 0;
33452 if (parser->implicit_template_scope)
33454 gcc_assert (parser->implicit_template_parms);
33456 current_binding_level = parser->implicit_template_scope;
33458 else
33460 /* Roll back to the existing template parameter scope (in the case of
33461 extending an explicit function template) or introduce a new template
33462 parameter scope ahead of the function parameter scope (or class scope
33463 in the case of out-of-line member definitions). The function scope is
33464 added back after template parameter synthesis below. */
33466 cp_binding_level *scope = entry_scope;
33468 while (scope->kind == sk_function_parms)
33470 parent_scope = scope;
33471 scope = scope->level_chain;
33473 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33475 /* If not defining a class, then any class scope is a scope level in
33476 an out-of-line member definition. In this case simply wind back
33477 beyond the first such scope to inject the template parameter list.
33478 Otherwise wind back to the class being defined. The latter can
33479 occur in class member friend declarations such as:
33481 class A {
33482 void foo (auto);
33484 class B {
33485 friend void A::foo (auto);
33488 The template parameter list synthesized for the friend declaration
33489 must be injected in the scope of 'B'. This can also occur in
33490 erroneous cases such as:
33492 struct A {
33493 struct B {
33494 void foo (auto);
33496 void B::foo (auto) {}
33499 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33500 but, nevertheless, the template parameter list synthesized for the
33501 declarator should be injected into the scope of 'A' as if the
33502 ill-formed template was specified explicitly. */
33504 while (scope->kind == sk_class && !scope->defining_class_p)
33506 parent_scope = scope;
33507 scope = scope->level_chain;
33511 current_binding_level = scope;
33513 if (scope->kind != sk_template_parms
33514 || !function_being_declared_is_template_p (parser))
33516 /* Introduce a new template parameter list for implicit template
33517 parameters. */
33519 become_template = true;
33521 parser->implicit_template_scope
33522 = begin_scope (sk_template_parms, NULL);
33524 ++processing_template_decl;
33526 parser->fully_implicit_function_template_p = true;
33527 ++parser->num_template_parameter_lists;
33529 else
33531 /* Synthesize implicit template parameters at the end of the explicit
33532 template parameter list. */
33534 gcc_assert (current_template_parms);
33536 parser->implicit_template_scope = scope;
33538 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33539 parser->implicit_template_parms
33540 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33544 /* Synthesize a new template parameter and track the current template
33545 parameter chain with implicit_template_parms. */
33547 tree synth_id = make_generic_type_name ();
33548 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33549 synth_id);
33550 tree new_parm
33551 = process_template_parm (parser->implicit_template_parms,
33552 input_location,
33553 build_tree_list (NULL_TREE, synth_tmpl_parm),
33554 /*non_type=*/false,
33555 /*param_pack=*/false);
33558 if (parser->implicit_template_parms)
33559 parser->implicit_template_parms
33560 = TREE_CHAIN (parser->implicit_template_parms);
33561 else
33562 parser->implicit_template_parms = new_parm;
33564 tree new_type = TREE_TYPE (getdecls ());
33566 /* If creating a fully implicit function template, start the new implicit
33567 template parameter list with this synthesized type, otherwise grow the
33568 current template parameter list. */
33570 if (become_template)
33572 parent_scope->level_chain = current_binding_level;
33574 tree new_parms = make_tree_vec (1);
33575 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33576 current_template_parms = tree_cons (size_int (processing_template_decl),
33577 new_parms, current_template_parms);
33579 else
33581 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33582 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33583 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33584 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33587 current_binding_level = entry_scope;
33589 return new_type;
33592 /* Finish the declaration of a fully implicit function template. Such a
33593 template has no explicit template parameter list so has not been through the
33594 normal template head and tail processing. synthesize_implicit_template_parm
33595 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33596 provided if the declaration is a class member such that its template
33597 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33598 form is returned. Otherwise NULL_TREE is returned. */
33600 tree
33601 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33603 gcc_assert (parser->fully_implicit_function_template_p);
33605 if (member_decl_opt && member_decl_opt != error_mark_node
33606 && DECL_VIRTUAL_P (member_decl_opt))
33608 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33609 "implicit templates may not be %<virtual%>");
33610 DECL_VIRTUAL_P (member_decl_opt) = false;
33613 if (member_decl_opt)
33614 member_decl_opt = finish_member_template_decl (member_decl_opt);
33615 end_template_decl ();
33617 parser->fully_implicit_function_template_p = false;
33618 --parser->num_template_parameter_lists;
33620 return member_decl_opt;
33623 #include "gt-cp-parser.h"