2014-06-11 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / parser.c
blob057b7de3faaafced0dc5df2fde2d1517d5c3c1a3
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 "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
50 namespace {
51 // A helper function. Returns the object pointed to by P
52 // and sets P to NULL.
53 template<typename T>
54 inline T* release(T*& p)
56 T* q = p;
57 p = NULL;
58 return q;
60 } // namespace
63 /* The lexer. */
65 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
66 and c-lex.c) and the C++ parser. */
68 static cp_token eof_token =
70 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
73 /* The various kinds of non integral constant we encounter. */
74 typedef enum non_integral_constant {
75 NIC_NONE,
76 /* floating-point literal */
77 NIC_FLOAT,
78 /* %<this%> */
79 NIC_THIS,
80 /* %<__FUNCTION__%> */
81 NIC_FUNC_NAME,
82 /* %<__PRETTY_FUNCTION__%> */
83 NIC_PRETTY_FUNC,
84 /* %<__func__%> */
85 NIC_C99_FUNC,
86 /* "%<va_arg%> */
87 NIC_VA_ARG,
88 /* a cast */
89 NIC_CAST,
90 /* %<typeid%> operator */
91 NIC_TYPEID,
92 /* non-constant compound literals */
93 NIC_NCC,
94 /* a function call */
95 NIC_FUNC_CALL,
96 /* an increment */
97 NIC_INC,
98 /* an decrement */
99 NIC_DEC,
100 /* an array reference */
101 NIC_ARRAY_REF,
102 /* %<->%> */
103 NIC_ARROW,
104 /* %<.%> */
105 NIC_POINT,
106 /* the address of a label */
107 NIC_ADDR_LABEL,
108 /* %<*%> */
109 NIC_STAR,
110 /* %<&%> */
111 NIC_ADDR,
112 /* %<++%> */
113 NIC_PREINCREMENT,
114 /* %<--%> */
115 NIC_PREDECREMENT,
116 /* %<new%> */
117 NIC_NEW,
118 /* %<delete%> */
119 NIC_DEL,
120 /* calls to overloaded operators */
121 NIC_OVERLOADED,
122 /* an assignment */
123 NIC_ASSIGNMENT,
124 /* a comma operator */
125 NIC_COMMA,
126 /* a call to a constructor */
127 NIC_CONSTRUCTOR,
128 /* a transaction expression */
129 NIC_TRANSACTION
130 } non_integral_constant;
132 /* The various kinds of errors about name-lookup failing. */
133 typedef enum name_lookup_error {
134 /* NULL */
135 NLE_NULL,
136 /* is not a type */
137 NLE_TYPE,
138 /* is not a class or namespace */
139 NLE_CXX98,
140 /* is not a class, namespace, or enumeration */
141 NLE_NOT_CXX98
142 } name_lookup_error;
144 /* The various kinds of required token */
145 typedef enum required_token {
146 RT_NONE,
147 RT_SEMICOLON, /* ';' */
148 RT_OPEN_PAREN, /* '(' */
149 RT_CLOSE_BRACE, /* '}' */
150 RT_OPEN_BRACE, /* '{' */
151 RT_CLOSE_SQUARE, /* ']' */
152 RT_OPEN_SQUARE, /* '[' */
153 RT_COMMA, /* ',' */
154 RT_SCOPE, /* '::' */
155 RT_LESS, /* '<' */
156 RT_GREATER, /* '>' */
157 RT_EQ, /* '=' */
158 RT_ELLIPSIS, /* '...' */
159 RT_MULT, /* '*' */
160 RT_COMPL, /* '~' */
161 RT_COLON, /* ':' */
162 RT_COLON_SCOPE, /* ':' or '::' */
163 RT_CLOSE_PAREN, /* ')' */
164 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
165 RT_PRAGMA_EOL, /* end of line */
166 RT_NAME, /* identifier */
168 /* The type is CPP_KEYWORD */
169 RT_NEW, /* new */
170 RT_DELETE, /* delete */
171 RT_RETURN, /* return */
172 RT_WHILE, /* while */
173 RT_EXTERN, /* extern */
174 RT_STATIC_ASSERT, /* static_assert */
175 RT_DECLTYPE, /* decltype */
176 RT_OPERATOR, /* operator */
177 RT_CLASS, /* class */
178 RT_TEMPLATE, /* template */
179 RT_NAMESPACE, /* namespace */
180 RT_USING, /* using */
181 RT_ASM, /* asm */
182 RT_TRY, /* try */
183 RT_CATCH, /* catch */
184 RT_THROW, /* throw */
185 RT_LABEL, /* __label__ */
186 RT_AT_TRY, /* @try */
187 RT_AT_SYNCHRONIZED, /* @synchronized */
188 RT_AT_THROW, /* @throw */
190 RT_SELECT, /* selection-statement */
191 RT_INTERATION, /* iteration-statement */
192 RT_JUMP, /* jump-statement */
193 RT_CLASS_KEY, /* class-key */
194 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
195 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
196 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
197 RT_TRANSACTION_CANCEL /* __transaction_cancel */
198 } required_token;
200 /* Prototypes. */
202 static cp_lexer *cp_lexer_new_main
203 (void);
204 static cp_lexer *cp_lexer_new_from_tokens
205 (cp_token_cache *tokens);
206 static void cp_lexer_destroy
207 (cp_lexer *);
208 static int cp_lexer_saving_tokens
209 (const cp_lexer *);
210 static cp_token *cp_lexer_token_at
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_get_preprocessor_token
213 (cp_lexer *, cp_token *);
214 static inline cp_token *cp_lexer_peek_token
215 (cp_lexer *);
216 static cp_token *cp_lexer_peek_nth_token
217 (cp_lexer *, size_t);
218 static inline bool cp_lexer_next_token_is
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_not
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_keyword
223 (cp_lexer *, enum rid);
224 static cp_token *cp_lexer_consume_token
225 (cp_lexer *);
226 static void cp_lexer_purge_token
227 (cp_lexer *);
228 static void cp_lexer_purge_tokens_after
229 (cp_lexer *, cp_token_position);
230 static void cp_lexer_save_tokens
231 (cp_lexer *);
232 static void cp_lexer_commit_tokens
233 (cp_lexer *);
234 static void cp_lexer_rollback_tokens
235 (cp_lexer *);
236 static void cp_lexer_print_token
237 (FILE *, cp_token *);
238 static inline bool cp_lexer_debugging_p
239 (cp_lexer *);
240 static void cp_lexer_start_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static void cp_lexer_stop_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static cp_token_cache *cp_token_cache_new
246 (cp_token *, cp_token *);
248 static void cp_parser_initial_pragma
249 (cp_token *);
251 static tree cp_literal_operator_id
252 (const char *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *);
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_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
911 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
914 /* Return true if the next token is not the indicated KEYWORD. */
916 static inline bool
917 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
919 return cp_lexer_peek_token (lexer)->keyword != keyword;
922 /* Return true if the next token is a keyword for a decl-specifier. */
924 static bool
925 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
927 cp_token *token;
929 token = cp_lexer_peek_token (lexer);
930 switch (token->keyword)
932 /* auto specifier: storage-class-specifier in C++,
933 simple-type-specifier in C++0x. */
934 case RID_AUTO:
935 /* Storage classes. */
936 case RID_REGISTER:
937 case RID_STATIC:
938 case RID_EXTERN:
939 case RID_MUTABLE:
940 case RID_THREAD:
941 /* Elaborated type specifiers. */
942 case RID_ENUM:
943 case RID_CLASS:
944 case RID_STRUCT:
945 case RID_UNION:
946 case RID_TYPENAME:
947 /* Simple type specifiers. */
948 case RID_CHAR:
949 case RID_CHAR16:
950 case RID_CHAR32:
951 case RID_WCHAR:
952 case RID_BOOL:
953 case RID_SHORT:
954 case RID_INT:
955 case RID_LONG:
956 case RID_INT128:
957 case RID_SIGNED:
958 case RID_UNSIGNED:
959 case RID_FLOAT:
960 case RID_DOUBLE:
961 case RID_VOID:
962 /* GNU extensions. */
963 case RID_ATTRIBUTE:
964 case RID_TYPEOF:
965 /* C++0x extensions. */
966 case RID_DECLTYPE:
967 case RID_UNDERLYING_TYPE:
968 return true;
970 default:
971 return false;
975 /* Returns TRUE iff the token T begins a decltype type. */
977 static bool
978 token_is_decltype (cp_token *t)
980 return (t->keyword == RID_DECLTYPE
981 || t->type == CPP_DECLTYPE);
984 /* Returns TRUE iff the next token begins a decltype type. */
986 static bool
987 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
989 cp_token *t = cp_lexer_peek_token (lexer);
990 return token_is_decltype (t);
993 /* Return a pointer to the Nth token in the token stream. If N is 1,
994 then this is precisely equivalent to cp_lexer_peek_token (except
995 that it is not inline). One would like to disallow that case, but
996 there is one case (cp_parser_nth_token_starts_template_id) where
997 the caller passes a variable for N and it might be 1. */
999 static cp_token *
1000 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1002 cp_token *token;
1004 /* N is 1-based, not zero-based. */
1005 gcc_assert (n > 0);
1007 if (cp_lexer_debugging_p (lexer))
1008 fprintf (cp_lexer_debug_stream,
1009 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1011 --n;
1012 token = lexer->next_token;
1013 gcc_assert (!n || token != &eof_token);
1014 while (n != 0)
1016 ++token;
1017 if (token == lexer->last_token)
1019 token = &eof_token;
1020 break;
1023 if (!token->purged_p)
1024 --n;
1027 if (cp_lexer_debugging_p (lexer))
1029 cp_lexer_print_token (cp_lexer_debug_stream, token);
1030 putc ('\n', cp_lexer_debug_stream);
1033 return token;
1036 /* Return the next token, and advance the lexer's next_token pointer
1037 to point to the next non-purged token. */
1039 static cp_token *
1040 cp_lexer_consume_token (cp_lexer* lexer)
1042 cp_token *token = lexer->next_token;
1044 gcc_assert (token != &eof_token);
1045 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1049 lexer->next_token++;
1050 if (lexer->next_token == lexer->last_token)
1052 lexer->next_token = &eof_token;
1053 break;
1057 while (lexer->next_token->purged_p);
1059 cp_lexer_set_source_position_from_token (token);
1061 /* Provide debugging output. */
1062 if (cp_lexer_debugging_p (lexer))
1064 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1069 return token;
1072 /* Permanently remove the next token from the token stream, and
1073 advance the next_token pointer to refer to the next non-purged
1074 token. */
1076 static void
1077 cp_lexer_purge_token (cp_lexer *lexer)
1079 cp_token *tok = lexer->next_token;
1081 gcc_assert (tok != &eof_token);
1082 tok->purged_p = true;
1083 tok->location = UNKNOWN_LOCATION;
1084 tok->u.value = NULL_TREE;
1085 tok->keyword = RID_MAX;
1089 tok++;
1090 if (tok == lexer->last_token)
1092 tok = &eof_token;
1093 break;
1096 while (tok->purged_p);
1097 lexer->next_token = tok;
1100 /* Permanently remove all tokens after TOK, up to, but not
1101 including, the token that will be returned next by
1102 cp_lexer_peek_token. */
1104 static void
1105 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1107 cp_token *peek = lexer->next_token;
1109 if (peek == &eof_token)
1110 peek = lexer->last_token;
1112 gcc_assert (tok < peek);
1114 for ( tok += 1; tok != peek; tok += 1)
1116 tok->purged_p = true;
1117 tok->location = UNKNOWN_LOCATION;
1118 tok->u.value = NULL_TREE;
1119 tok->keyword = RID_MAX;
1123 /* Begin saving tokens. All tokens consumed after this point will be
1124 preserved. */
1126 static void
1127 cp_lexer_save_tokens (cp_lexer* lexer)
1129 /* Provide debugging output. */
1130 if (cp_lexer_debugging_p (lexer))
1131 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1133 lexer->saved_tokens.safe_push (lexer->next_token);
1136 /* Commit to the portion of the token stream most recently saved. */
1138 static void
1139 cp_lexer_commit_tokens (cp_lexer* lexer)
1141 /* Provide debugging output. */
1142 if (cp_lexer_debugging_p (lexer))
1143 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1145 lexer->saved_tokens.pop ();
1148 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1149 to the token stream. Stop saving tokens. */
1151 static void
1152 cp_lexer_rollback_tokens (cp_lexer* lexer)
1154 /* Provide debugging output. */
1155 if (cp_lexer_debugging_p (lexer))
1156 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1158 lexer->next_token = lexer->saved_tokens.pop ();
1161 /* Print a representation of the TOKEN on the STREAM. */
1163 static void
1164 cp_lexer_print_token (FILE * stream, cp_token *token)
1166 /* We don't use cpp_type2name here because the parser defines
1167 a few tokens of its own. */
1168 static const char *const token_names[] = {
1169 /* cpplib-defined token types */
1170 #define OP(e, s) #e,
1171 #define TK(e, s) #e,
1172 TTYPE_TABLE
1173 #undef OP
1174 #undef TK
1175 /* C++ parser token types - see "Manifest constants", above. */
1176 "KEYWORD",
1177 "TEMPLATE_ID",
1178 "NESTED_NAME_SPECIFIER",
1181 /* For some tokens, print the associated data. */
1182 switch (token->type)
1184 case CPP_KEYWORD:
1185 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1186 For example, `struct' is mapped to an INTEGER_CST. */
1187 if (!identifier_p (token->u.value))
1188 break;
1189 /* else fall through */
1190 case CPP_NAME:
1191 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1192 break;
1194 case CPP_STRING:
1195 case CPP_STRING16:
1196 case CPP_STRING32:
1197 case CPP_WSTRING:
1198 case CPP_UTF8STRING:
1199 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1200 break;
1202 case CPP_NUMBER:
1203 print_generic_expr (stream, token->u.value, 0);
1204 break;
1206 default:
1207 /* If we have a name for the token, print it out. Otherwise, we
1208 simply give the numeric code. */
1209 if (token->type < ARRAY_SIZE(token_names))
1210 fputs (token_names[token->type], stream);
1211 else
1212 fprintf (stream, "[%d]", token->type);
1213 break;
1217 DEBUG_FUNCTION void
1218 debug (cp_token &ref)
1220 cp_lexer_print_token (stderr, &ref);
1221 fprintf (stderr, "\n");
1224 DEBUG_FUNCTION void
1225 debug (cp_token *ptr)
1227 if (ptr)
1228 debug (*ptr);
1229 else
1230 fprintf (stderr, "<nil>\n");
1234 /* Start emitting debugging information. */
1236 static void
1237 cp_lexer_start_debugging (cp_lexer* lexer)
1239 lexer->debugging_p = true;
1240 cp_lexer_debug_stream = stderr;
1243 /* Stop emitting debugging information. */
1245 static void
1246 cp_lexer_stop_debugging (cp_lexer* lexer)
1248 lexer->debugging_p = false;
1249 cp_lexer_debug_stream = NULL;
1252 /* Create a new cp_token_cache, representing a range of tokens. */
1254 static cp_token_cache *
1255 cp_token_cache_new (cp_token *first, cp_token *last)
1257 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1258 cache->first = first;
1259 cache->last = last;
1260 return cache;
1263 /* Diagnose if #pragma omp declare simd isn't followed immediately
1264 by function declaration or definition. */
1266 static inline void
1267 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1269 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1271 error ("%<#pragma omp declare simd%> not immediately followed by "
1272 "function declaration or definition");
1273 parser->omp_declare_simd = NULL;
1277 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1278 and put that into "omp declare simd" attribute. */
1280 static inline void
1281 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1283 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1285 if (fndecl == error_mark_node)
1287 parser->omp_declare_simd = NULL;
1288 return;
1290 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1292 cp_ensure_no_omp_declare_simd (parser);
1293 return;
1298 /* Decl-specifiers. */
1300 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1302 static void
1303 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1305 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1308 /* Declarators. */
1310 /* Nothing other than the parser should be creating declarators;
1311 declarators are a semi-syntactic representation of C++ entities.
1312 Other parts of the front end that need to create entities (like
1313 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1315 static cp_declarator *make_call_declarator
1316 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1317 static cp_declarator *make_array_declarator
1318 (cp_declarator *, tree);
1319 static cp_declarator *make_pointer_declarator
1320 (cp_cv_quals, cp_declarator *, tree);
1321 static cp_declarator *make_reference_declarator
1322 (cp_cv_quals, cp_declarator *, bool, tree);
1323 static cp_parameter_declarator *make_parameter_declarator
1324 (cp_decl_specifier_seq *, cp_declarator *, tree);
1325 static cp_declarator *make_ptrmem_declarator
1326 (cp_cv_quals, tree, cp_declarator *, tree);
1328 /* An erroneous declarator. */
1329 static cp_declarator *cp_error_declarator;
1331 /* The obstack on which declarators and related data structures are
1332 allocated. */
1333 static struct obstack declarator_obstack;
1335 /* Alloc BYTES from the declarator memory pool. */
1337 static inline void *
1338 alloc_declarator (size_t bytes)
1340 return obstack_alloc (&declarator_obstack, bytes);
1343 /* Allocate a declarator of the indicated KIND. Clear fields that are
1344 common to all declarators. */
1346 static cp_declarator *
1347 make_declarator (cp_declarator_kind kind)
1349 cp_declarator *declarator;
1351 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1352 declarator->kind = kind;
1353 declarator->attributes = NULL_TREE;
1354 declarator->std_attributes = NULL_TREE;
1355 declarator->declarator = NULL;
1356 declarator->parameter_pack_p = false;
1357 declarator->id_loc = UNKNOWN_LOCATION;
1359 return declarator;
1362 /* Make a declarator for a generalized identifier. If
1363 QUALIFYING_SCOPE is non-NULL, the identifier is
1364 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1365 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1366 is, if any. */
1368 static cp_declarator *
1369 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1370 special_function_kind sfk)
1372 cp_declarator *declarator;
1374 /* It is valid to write:
1376 class C { void f(); };
1377 typedef C D;
1378 void D::f();
1380 The standard is not clear about whether `typedef const C D' is
1381 legal; as of 2002-09-15 the committee is considering that
1382 question. EDG 3.0 allows that syntax. Therefore, we do as
1383 well. */
1384 if (qualifying_scope && TYPE_P (qualifying_scope))
1385 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1387 gcc_assert (identifier_p (unqualified_name)
1388 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1389 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1391 declarator = make_declarator (cdk_id);
1392 declarator->u.id.qualifying_scope = qualifying_scope;
1393 declarator->u.id.unqualified_name = unqualified_name;
1394 declarator->u.id.sfk = sfk;
1396 return declarator;
1399 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1400 of modifiers such as const or volatile to apply to the pointer
1401 type, represented as identifiers. ATTRIBUTES represent the attributes that
1402 appertain to the pointer or reference. */
1404 cp_declarator *
1405 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1406 tree attributes)
1408 cp_declarator *declarator;
1410 declarator = make_declarator (cdk_pointer);
1411 declarator->declarator = target;
1412 declarator->u.pointer.qualifiers = cv_qualifiers;
1413 declarator->u.pointer.class_type = NULL_TREE;
1414 if (target)
1416 declarator->id_loc = target->id_loc;
1417 declarator->parameter_pack_p = target->parameter_pack_p;
1418 target->parameter_pack_p = false;
1420 else
1421 declarator->parameter_pack_p = false;
1423 declarator->std_attributes = attributes;
1425 return declarator;
1428 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1429 represent the attributes that appertain to the pointer or
1430 reference. */
1432 cp_declarator *
1433 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1434 bool rvalue_ref, tree attributes)
1436 cp_declarator *declarator;
1438 declarator = make_declarator (cdk_reference);
1439 declarator->declarator = target;
1440 declarator->u.reference.qualifiers = cv_qualifiers;
1441 declarator->u.reference.rvalue_ref = rvalue_ref;
1442 if (target)
1444 declarator->id_loc = target->id_loc;
1445 declarator->parameter_pack_p = target->parameter_pack_p;
1446 target->parameter_pack_p = false;
1448 else
1449 declarator->parameter_pack_p = false;
1451 declarator->std_attributes = attributes;
1453 return declarator;
1456 /* Like make_pointer_declarator -- but for a pointer to a non-static
1457 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1458 appertain to the pointer or reference. */
1460 cp_declarator *
1461 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1462 cp_declarator *pointee,
1463 tree attributes)
1465 cp_declarator *declarator;
1467 declarator = make_declarator (cdk_ptrmem);
1468 declarator->declarator = pointee;
1469 declarator->u.pointer.qualifiers = cv_qualifiers;
1470 declarator->u.pointer.class_type = class_type;
1472 if (pointee)
1474 declarator->parameter_pack_p = pointee->parameter_pack_p;
1475 pointee->parameter_pack_p = false;
1477 else
1478 declarator->parameter_pack_p = false;
1480 declarator->std_attributes = attributes;
1482 return declarator;
1485 /* Make a declarator for the function given by TARGET, with the
1486 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1487 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1488 indicates what exceptions can be thrown. */
1490 cp_declarator *
1491 make_call_declarator (cp_declarator *target,
1492 tree parms,
1493 cp_cv_quals cv_qualifiers,
1494 cp_virt_specifiers virt_specifiers,
1495 cp_ref_qualifier ref_qualifier,
1496 tree exception_specification,
1497 tree late_return_type)
1499 cp_declarator *declarator;
1501 declarator = make_declarator (cdk_function);
1502 declarator->declarator = target;
1503 declarator->u.function.parameters = parms;
1504 declarator->u.function.qualifiers = cv_qualifiers;
1505 declarator->u.function.virt_specifiers = virt_specifiers;
1506 declarator->u.function.ref_qualifier = ref_qualifier;
1507 declarator->u.function.exception_specification = exception_specification;
1508 declarator->u.function.late_return_type = late_return_type;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 return declarator;
1521 /* Make a declarator for an array of BOUNDS elements, each of which is
1522 defined by ELEMENT. */
1524 cp_declarator *
1525 make_array_declarator (cp_declarator *element, tree bounds)
1527 cp_declarator *declarator;
1529 declarator = make_declarator (cdk_array);
1530 declarator->declarator = element;
1531 declarator->u.array.bounds = bounds;
1532 if (element)
1534 declarator->id_loc = element->id_loc;
1535 declarator->parameter_pack_p = element->parameter_pack_p;
1536 element->parameter_pack_p = false;
1538 else
1539 declarator->parameter_pack_p = false;
1541 return declarator;
1544 /* Determine whether the declarator we've seen so far can be a
1545 parameter pack, when followed by an ellipsis. */
1546 static bool
1547 declarator_can_be_parameter_pack (cp_declarator *declarator)
1549 /* Search for a declarator name, or any other declarator that goes
1550 after the point where the ellipsis could appear in a parameter
1551 pack. If we find any of these, then this declarator can not be
1552 made into a parameter pack. */
1553 bool found = false;
1554 while (declarator && !found)
1556 switch ((int)declarator->kind)
1558 case cdk_id:
1559 case cdk_array:
1560 found = true;
1561 break;
1563 case cdk_error:
1564 return true;
1566 default:
1567 declarator = declarator->declarator;
1568 break;
1572 return !found;
1575 cp_parameter_declarator *no_parameters;
1577 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1578 DECLARATOR and DEFAULT_ARGUMENT. */
1580 cp_parameter_declarator *
1581 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1582 cp_declarator *declarator,
1583 tree default_argument)
1585 cp_parameter_declarator *parameter;
1587 parameter = ((cp_parameter_declarator *)
1588 alloc_declarator (sizeof (cp_parameter_declarator)));
1589 parameter->next = NULL;
1590 if (decl_specifiers)
1591 parameter->decl_specifiers = *decl_specifiers;
1592 else
1593 clear_decl_specs (&parameter->decl_specifiers);
1594 parameter->declarator = declarator;
1595 parameter->default_argument = default_argument;
1596 parameter->ellipsis_p = false;
1598 return parameter;
1601 /* Returns true iff DECLARATOR is a declaration for a function. */
1603 static bool
1604 function_declarator_p (const cp_declarator *declarator)
1606 while (declarator)
1608 if (declarator->kind == cdk_function
1609 && declarator->declarator->kind == cdk_id)
1610 return true;
1611 if (declarator->kind == cdk_id
1612 || declarator->kind == cdk_error)
1613 return false;
1614 declarator = declarator->declarator;
1616 return false;
1619 /* The parser. */
1621 /* Overview
1622 --------
1624 A cp_parser parses the token stream as specified by the C++
1625 grammar. Its job is purely parsing, not semantic analysis. For
1626 example, the parser breaks the token stream into declarators,
1627 expressions, statements, and other similar syntactic constructs.
1628 It does not check that the types of the expressions on either side
1629 of an assignment-statement are compatible, or that a function is
1630 not declared with a parameter of type `void'.
1632 The parser invokes routines elsewhere in the compiler to perform
1633 semantic analysis and to build up the abstract syntax tree for the
1634 code processed.
1636 The parser (and the template instantiation code, which is, in a
1637 way, a close relative of parsing) are the only parts of the
1638 compiler that should be calling push_scope and pop_scope, or
1639 related functions. The parser (and template instantiation code)
1640 keeps track of what scope is presently active; everything else
1641 should simply honor that. (The code that generates static
1642 initializers may also need to set the scope, in order to check
1643 access control correctly when emitting the initializers.)
1645 Methodology
1646 -----------
1648 The parser is of the standard recursive-descent variety. Upcoming
1649 tokens in the token stream are examined in order to determine which
1650 production to use when parsing a non-terminal. Some C++ constructs
1651 require arbitrary look ahead to disambiguate. For example, it is
1652 impossible, in the general case, to tell whether a statement is an
1653 expression or declaration without scanning the entire statement.
1654 Therefore, the parser is capable of "parsing tentatively." When the
1655 parser is not sure what construct comes next, it enters this mode.
1656 Then, while we attempt to parse the construct, the parser queues up
1657 error messages, rather than issuing them immediately, and saves the
1658 tokens it consumes. If the construct is parsed successfully, the
1659 parser "commits", i.e., it issues any queued error messages and
1660 the tokens that were being preserved are permanently discarded.
1661 If, however, the construct is not parsed successfully, the parser
1662 rolls back its state completely so that it can resume parsing using
1663 a different alternative.
1665 Future Improvements
1666 -------------------
1668 The performance of the parser could probably be improved substantially.
1669 We could often eliminate the need to parse tentatively by looking ahead
1670 a little bit. In some places, this approach might not entirely eliminate
1671 the need to parse tentatively, but it might still speed up the average
1672 case. */
1674 /* Flags that are passed to some parsing functions. These values can
1675 be bitwise-ored together. */
1677 enum
1679 /* No flags. */
1680 CP_PARSER_FLAGS_NONE = 0x0,
1681 /* The construct is optional. If it is not present, then no error
1682 should be issued. */
1683 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1684 /* When parsing a type-specifier, treat user-defined type-names
1685 as non-type identifiers. */
1686 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1687 /* When parsing a type-specifier, do not try to parse a class-specifier
1688 or enum-specifier. */
1689 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1690 /* When parsing a decl-specifier-seq, only allow type-specifier or
1691 constexpr. */
1692 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1695 /* This type is used for parameters and variables which hold
1696 combinations of the above flags. */
1697 typedef int cp_parser_flags;
1699 /* The different kinds of declarators we want to parse. */
1701 typedef enum cp_parser_declarator_kind
1703 /* We want an abstract declarator. */
1704 CP_PARSER_DECLARATOR_ABSTRACT,
1705 /* We want a named declarator. */
1706 CP_PARSER_DECLARATOR_NAMED,
1707 /* We don't mind, but the name must be an unqualified-id. */
1708 CP_PARSER_DECLARATOR_EITHER
1709 } cp_parser_declarator_kind;
1711 /* The precedence values used to parse binary expressions. The minimum value
1712 of PREC must be 1, because zero is reserved to quickly discriminate
1713 binary operators from other tokens. */
1715 enum cp_parser_prec
1717 PREC_NOT_OPERATOR,
1718 PREC_LOGICAL_OR_EXPRESSION,
1719 PREC_LOGICAL_AND_EXPRESSION,
1720 PREC_INCLUSIVE_OR_EXPRESSION,
1721 PREC_EXCLUSIVE_OR_EXPRESSION,
1722 PREC_AND_EXPRESSION,
1723 PREC_EQUALITY_EXPRESSION,
1724 PREC_RELATIONAL_EXPRESSION,
1725 PREC_SHIFT_EXPRESSION,
1726 PREC_ADDITIVE_EXPRESSION,
1727 PREC_MULTIPLICATIVE_EXPRESSION,
1728 PREC_PM_EXPRESSION,
1729 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1732 /* A mapping from a token type to a corresponding tree node type, with a
1733 precedence value. */
1735 typedef struct cp_parser_binary_operations_map_node
1737 /* The token type. */
1738 enum cpp_ttype token_type;
1739 /* The corresponding tree code. */
1740 enum tree_code tree_type;
1741 /* The precedence of this operator. */
1742 enum cp_parser_prec prec;
1743 } cp_parser_binary_operations_map_node;
1745 typedef struct cp_parser_expression_stack_entry
1747 /* Left hand side of the binary operation we are currently
1748 parsing. */
1749 tree lhs;
1750 /* Original tree code for left hand side, if it was a binary
1751 expression itself (used for -Wparentheses). */
1752 enum tree_code lhs_type;
1753 /* Tree code for the binary operation we are parsing. */
1754 enum tree_code tree_type;
1755 /* Precedence of the binary operation we are parsing. */
1756 enum cp_parser_prec prec;
1757 /* Location of the binary operation we are parsing. */
1758 location_t loc;
1759 } cp_parser_expression_stack_entry;
1761 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1762 entries because precedence levels on the stack are monotonically
1763 increasing. */
1764 typedef struct cp_parser_expression_stack_entry
1765 cp_parser_expression_stack[NUM_PREC_VALUES];
1767 /* Prototypes. */
1769 /* Constructors and destructors. */
1771 static cp_parser_context *cp_parser_context_new
1772 (cp_parser_context *);
1774 /* Class variables. */
1776 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1778 /* The operator-precedence table used by cp_parser_binary_expression.
1779 Transformed into an associative array (binops_by_token) by
1780 cp_parser_new. */
1782 static const cp_parser_binary_operations_map_node binops[] = {
1783 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1784 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1786 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1787 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1788 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1790 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1791 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1793 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1794 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1796 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1797 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1798 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1799 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1801 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1802 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1804 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1806 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1808 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1810 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1812 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1815 /* The same as binops, but initialized by cp_parser_new so that
1816 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1817 for speed. */
1818 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1820 /* Constructors and destructors. */
1822 /* Construct a new context. The context below this one on the stack
1823 is given by NEXT. */
1825 static cp_parser_context *
1826 cp_parser_context_new (cp_parser_context* next)
1828 cp_parser_context *context;
1830 /* Allocate the storage. */
1831 if (cp_parser_context_free_list != NULL)
1833 /* Pull the first entry from the free list. */
1834 context = cp_parser_context_free_list;
1835 cp_parser_context_free_list = context->next;
1836 memset (context, 0, sizeof (*context));
1838 else
1839 context = ggc_cleared_alloc<cp_parser_context> ();
1841 /* No errors have occurred yet in this context. */
1842 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1843 /* If this is not the bottommost context, copy information that we
1844 need from the previous context. */
1845 if (next)
1847 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1848 expression, then we are parsing one in this context, too. */
1849 context->object_type = next->object_type;
1850 /* Thread the stack. */
1851 context->next = next;
1854 return context;
1857 /* Managing the unparsed function queues. */
1859 #define unparsed_funs_with_default_args \
1860 parser->unparsed_queues->last ().funs_with_default_args
1861 #define unparsed_funs_with_definitions \
1862 parser->unparsed_queues->last ().funs_with_definitions
1863 #define unparsed_nsdmis \
1864 parser->unparsed_queues->last ().nsdmis
1865 #define unparsed_classes \
1866 parser->unparsed_queues->last ().classes
1868 static void
1869 push_unparsed_function_queues (cp_parser *parser)
1871 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1872 vec_safe_push (parser->unparsed_queues, e);
1875 static void
1876 pop_unparsed_function_queues (cp_parser *parser)
1878 release_tree_vector (unparsed_funs_with_definitions);
1879 parser->unparsed_queues->pop ();
1882 /* Prototypes. */
1884 /* Constructors and destructors. */
1886 static cp_parser *cp_parser_new
1887 (void);
1889 /* Routines to parse various constructs.
1891 Those that return `tree' will return the error_mark_node (rather
1892 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1893 Sometimes, they will return an ordinary node if error-recovery was
1894 attempted, even though a parse error occurred. So, to check
1895 whether or not a parse error occurred, you should always use
1896 cp_parser_error_occurred. If the construct is optional (indicated
1897 either by an `_opt' in the name of the function that does the
1898 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1899 the construct is not present. */
1901 /* Lexical conventions [gram.lex] */
1903 static tree cp_parser_identifier
1904 (cp_parser *);
1905 static tree cp_parser_string_literal
1906 (cp_parser *, bool, bool);
1907 static tree cp_parser_userdef_char_literal
1908 (cp_parser *);
1909 static tree cp_parser_userdef_string_literal
1910 (cp_token *);
1911 static tree cp_parser_userdef_numeric_literal
1912 (cp_parser *);
1914 /* Basic concepts [gram.basic] */
1916 static bool cp_parser_translation_unit
1917 (cp_parser *);
1919 /* Expressions [gram.expr] */
1921 static tree cp_parser_primary_expression
1922 (cp_parser *, bool, bool, bool, cp_id_kind *);
1923 static tree cp_parser_id_expression
1924 (cp_parser *, bool, bool, bool *, bool, bool);
1925 static tree cp_parser_unqualified_id
1926 (cp_parser *, bool, bool, bool, bool);
1927 static tree cp_parser_nested_name_specifier_opt
1928 (cp_parser *, bool, bool, bool, bool);
1929 static tree cp_parser_nested_name_specifier
1930 (cp_parser *, bool, bool, bool, bool);
1931 static tree cp_parser_qualifying_entity
1932 (cp_parser *, bool, bool, bool, bool, bool);
1933 static tree cp_parser_postfix_expression
1934 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1935 static tree cp_parser_postfix_open_square_expression
1936 (cp_parser *, tree, bool, bool);
1937 static tree cp_parser_postfix_dot_deref_expression
1938 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1939 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1940 (cp_parser *, int, bool, bool, bool *);
1941 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1942 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1943 static void cp_parser_pseudo_destructor_name
1944 (cp_parser *, tree, tree *, tree *);
1945 static tree cp_parser_unary_expression
1946 (cp_parser *, bool, bool, cp_id_kind *);
1947 static enum tree_code cp_parser_unary_operator
1948 (cp_token *);
1949 static tree cp_parser_new_expression
1950 (cp_parser *);
1951 static vec<tree, va_gc> *cp_parser_new_placement
1952 (cp_parser *);
1953 static tree cp_parser_new_type_id
1954 (cp_parser *, tree *);
1955 static cp_declarator *cp_parser_new_declarator_opt
1956 (cp_parser *);
1957 static cp_declarator *cp_parser_direct_new_declarator
1958 (cp_parser *);
1959 static vec<tree, va_gc> *cp_parser_new_initializer
1960 (cp_parser *);
1961 static tree cp_parser_delete_expression
1962 (cp_parser *);
1963 static tree cp_parser_cast_expression
1964 (cp_parser *, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_binary_expression
1966 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1967 static tree cp_parser_question_colon_clause
1968 (cp_parser *, tree);
1969 static tree cp_parser_assignment_expression
1970 (cp_parser *, bool, cp_id_kind *);
1971 static enum tree_code cp_parser_assignment_operator_opt
1972 (cp_parser *);
1973 static tree cp_parser_expression
1974 (cp_parser *, bool, cp_id_kind *);
1975 static tree cp_parser_expression
1976 (cp_parser *, bool, bool, cp_id_kind *);
1977 static tree cp_parser_constant_expression
1978 (cp_parser *, bool, bool *);
1979 static tree cp_parser_builtin_offsetof
1980 (cp_parser *);
1981 static tree cp_parser_lambda_expression
1982 (cp_parser *);
1983 static void cp_parser_lambda_introducer
1984 (cp_parser *, tree);
1985 static bool cp_parser_lambda_declarator_opt
1986 (cp_parser *, tree);
1987 static void cp_parser_lambda_body
1988 (cp_parser *, tree);
1990 /* Statements [gram.stmt.stmt] */
1992 static void cp_parser_statement
1993 (cp_parser *, tree, bool, bool *);
1994 static void cp_parser_label_for_labeled_statement
1995 (cp_parser *, tree);
1996 static tree cp_parser_expression_statement
1997 (cp_parser *, tree);
1998 static tree cp_parser_compound_statement
1999 (cp_parser *, tree, bool, bool);
2000 static void cp_parser_statement_seq_opt
2001 (cp_parser *, tree);
2002 static tree cp_parser_selection_statement
2003 (cp_parser *, bool *);
2004 static tree cp_parser_condition
2005 (cp_parser *);
2006 static tree cp_parser_iteration_statement
2007 (cp_parser *, bool);
2008 static bool cp_parser_for_init_statement
2009 (cp_parser *, tree *decl);
2010 static tree cp_parser_for
2011 (cp_parser *, bool);
2012 static tree cp_parser_c_for
2013 (cp_parser *, tree, tree, bool);
2014 static tree cp_parser_range_for
2015 (cp_parser *, tree, tree, tree, bool);
2016 static void do_range_for_auto_deduction
2017 (tree, tree);
2018 static tree cp_parser_perform_range_for_lookup
2019 (tree, tree *, tree *);
2020 static tree cp_parser_range_for_member_function
2021 (tree, tree);
2022 static tree cp_parser_jump_statement
2023 (cp_parser *);
2024 static void cp_parser_declaration_statement
2025 (cp_parser *);
2027 static tree cp_parser_implicitly_scoped_statement
2028 (cp_parser *, bool *);
2029 static void cp_parser_already_scoped_statement
2030 (cp_parser *);
2032 /* Declarations [gram.dcl.dcl] */
2034 static void cp_parser_declaration_seq_opt
2035 (cp_parser *);
2036 static void cp_parser_declaration
2037 (cp_parser *);
2038 static void cp_parser_block_declaration
2039 (cp_parser *, bool);
2040 static void cp_parser_simple_declaration
2041 (cp_parser *, bool, tree *);
2042 static void cp_parser_decl_specifier_seq
2043 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2044 static tree cp_parser_storage_class_specifier_opt
2045 (cp_parser *);
2046 static tree cp_parser_function_specifier_opt
2047 (cp_parser *, cp_decl_specifier_seq *);
2048 static tree cp_parser_type_specifier
2049 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2050 int *, bool *);
2051 static tree cp_parser_simple_type_specifier
2052 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2053 static tree cp_parser_type_name
2054 (cp_parser *);
2055 static tree cp_parser_nonclass_name
2056 (cp_parser* parser);
2057 static tree cp_parser_elaborated_type_specifier
2058 (cp_parser *, bool, bool);
2059 static tree cp_parser_enum_specifier
2060 (cp_parser *);
2061 static void cp_parser_enumerator_list
2062 (cp_parser *, tree);
2063 static void cp_parser_enumerator_definition
2064 (cp_parser *, tree);
2065 static tree cp_parser_namespace_name
2066 (cp_parser *);
2067 static void cp_parser_namespace_definition
2068 (cp_parser *);
2069 static void cp_parser_namespace_body
2070 (cp_parser *);
2071 static tree cp_parser_qualified_namespace_specifier
2072 (cp_parser *);
2073 static void cp_parser_namespace_alias_definition
2074 (cp_parser *);
2075 static bool cp_parser_using_declaration
2076 (cp_parser *, bool);
2077 static void cp_parser_using_directive
2078 (cp_parser *);
2079 static tree cp_parser_alias_declaration
2080 (cp_parser *);
2081 static void cp_parser_asm_definition
2082 (cp_parser *);
2083 static void cp_parser_linkage_specification
2084 (cp_parser *);
2085 static void cp_parser_static_assert
2086 (cp_parser *, bool);
2087 static tree cp_parser_decltype
2088 (cp_parser *);
2090 /* Declarators [gram.dcl.decl] */
2092 static tree cp_parser_init_declarator
2093 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2094 static cp_declarator *cp_parser_declarator
2095 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2096 static cp_declarator *cp_parser_direct_declarator
2097 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2098 static enum tree_code cp_parser_ptr_operator
2099 (cp_parser *, tree *, cp_cv_quals *, tree *);
2100 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2101 (cp_parser *);
2102 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2103 (cp_parser *);
2104 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2105 (cp_parser *);
2106 static tree cp_parser_late_return_type_opt
2107 (cp_parser *, cp_declarator *, cp_cv_quals);
2108 static tree cp_parser_declarator_id
2109 (cp_parser *, bool);
2110 static tree cp_parser_type_id
2111 (cp_parser *);
2112 static tree cp_parser_template_type_arg
2113 (cp_parser *);
2114 static tree cp_parser_trailing_type_id (cp_parser *);
2115 static tree cp_parser_type_id_1
2116 (cp_parser *, bool, bool);
2117 static void cp_parser_type_specifier_seq
2118 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2119 static tree cp_parser_parameter_declaration_clause
2120 (cp_parser *);
2121 static tree cp_parser_parameter_declaration_list
2122 (cp_parser *, bool *);
2123 static cp_parameter_declarator *cp_parser_parameter_declaration
2124 (cp_parser *, bool, bool *);
2125 static tree cp_parser_default_argument
2126 (cp_parser *, bool);
2127 static void cp_parser_function_body
2128 (cp_parser *, bool);
2129 static tree cp_parser_initializer
2130 (cp_parser *, bool *, bool *);
2131 static tree cp_parser_initializer_clause
2132 (cp_parser *, bool *);
2133 static tree cp_parser_braced_list
2134 (cp_parser*, bool*);
2135 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2136 (cp_parser *, bool *);
2138 static bool cp_parser_ctor_initializer_opt_and_function_body
2139 (cp_parser *, bool);
2141 static tree cp_parser_late_parsing_omp_declare_simd
2142 (cp_parser *, tree);
2144 static tree cp_parser_late_parsing_cilk_simd_fn_info
2145 (cp_parser *, tree);
2147 static tree synthesize_implicit_template_parm
2148 (cp_parser *, tree);
2149 static tree finish_fully_implicit_template
2150 (cp_parser *, tree);
2152 /* Classes [gram.class] */
2154 static tree cp_parser_class_name
2155 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2156 static tree cp_parser_class_specifier
2157 (cp_parser *);
2158 static tree cp_parser_class_head
2159 (cp_parser *, bool *);
2160 static enum tag_types cp_parser_class_key
2161 (cp_parser *);
2162 static void cp_parser_member_specification_opt
2163 (cp_parser *);
2164 static void cp_parser_member_declaration
2165 (cp_parser *);
2166 static tree cp_parser_pure_specifier
2167 (cp_parser *);
2168 static tree cp_parser_constant_initializer
2169 (cp_parser *);
2171 /* Derived classes [gram.class.derived] */
2173 static tree cp_parser_base_clause
2174 (cp_parser *);
2175 static tree cp_parser_base_specifier
2176 (cp_parser *);
2178 /* Special member functions [gram.special] */
2180 static tree cp_parser_conversion_function_id
2181 (cp_parser *);
2182 static tree cp_parser_conversion_type_id
2183 (cp_parser *);
2184 static cp_declarator *cp_parser_conversion_declarator_opt
2185 (cp_parser *);
2186 static bool cp_parser_ctor_initializer_opt
2187 (cp_parser *);
2188 static void cp_parser_mem_initializer_list
2189 (cp_parser *);
2190 static tree cp_parser_mem_initializer
2191 (cp_parser *);
2192 static tree cp_parser_mem_initializer_id
2193 (cp_parser *);
2195 /* Overloading [gram.over] */
2197 static tree cp_parser_operator_function_id
2198 (cp_parser *);
2199 static tree cp_parser_operator
2200 (cp_parser *);
2202 /* Templates [gram.temp] */
2204 static void cp_parser_template_declaration
2205 (cp_parser *, bool);
2206 static tree cp_parser_template_parameter_list
2207 (cp_parser *);
2208 static tree cp_parser_template_parameter
2209 (cp_parser *, bool *, bool *);
2210 static tree cp_parser_type_parameter
2211 (cp_parser *, bool *);
2212 static tree cp_parser_template_id
2213 (cp_parser *, bool, bool, enum tag_types, bool);
2214 static tree cp_parser_template_name
2215 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2216 static tree cp_parser_template_argument_list
2217 (cp_parser *);
2218 static tree cp_parser_template_argument
2219 (cp_parser *);
2220 static void cp_parser_explicit_instantiation
2221 (cp_parser *);
2222 static void cp_parser_explicit_specialization
2223 (cp_parser *);
2225 /* Exception handling [gram.exception] */
2227 static tree cp_parser_try_block
2228 (cp_parser *);
2229 static bool cp_parser_function_try_block
2230 (cp_parser *);
2231 static void cp_parser_handler_seq
2232 (cp_parser *);
2233 static void cp_parser_handler
2234 (cp_parser *);
2235 static tree cp_parser_exception_declaration
2236 (cp_parser *);
2237 static tree cp_parser_throw_expression
2238 (cp_parser *);
2239 static tree cp_parser_exception_specification_opt
2240 (cp_parser *);
2241 static tree cp_parser_type_id_list
2242 (cp_parser *);
2244 /* GNU Extensions */
2246 static tree cp_parser_asm_specification_opt
2247 (cp_parser *);
2248 static tree cp_parser_asm_operand_list
2249 (cp_parser *);
2250 static tree cp_parser_asm_clobber_list
2251 (cp_parser *);
2252 static tree cp_parser_asm_label_list
2253 (cp_parser *);
2254 static bool cp_next_tokens_can_be_attribute_p
2255 (cp_parser *);
2256 static bool cp_next_tokens_can_be_gnu_attribute_p
2257 (cp_parser *);
2258 static bool cp_next_tokens_can_be_std_attribute_p
2259 (cp_parser *);
2260 static bool cp_nth_tokens_can_be_std_attribute_p
2261 (cp_parser *, size_t);
2262 static bool cp_nth_tokens_can_be_gnu_attribute_p
2263 (cp_parser *, size_t);
2264 static bool cp_nth_tokens_can_be_attribute_p
2265 (cp_parser *, size_t);
2266 static tree cp_parser_attributes_opt
2267 (cp_parser *);
2268 static tree cp_parser_gnu_attributes_opt
2269 (cp_parser *);
2270 static tree cp_parser_gnu_attribute_list
2271 (cp_parser *);
2272 static tree cp_parser_std_attribute
2273 (cp_parser *);
2274 static tree cp_parser_std_attribute_spec
2275 (cp_parser *);
2276 static tree cp_parser_std_attribute_spec_seq
2277 (cp_parser *);
2278 static bool cp_parser_extension_opt
2279 (cp_parser *, int *);
2280 static void cp_parser_label_declaration
2281 (cp_parser *);
2283 /* Concept Extensions */
2285 static tree cp_parser_requires_clause
2286 (cp_parser *);
2287 static tree cp_parser_requires_clause_opt
2288 (cp_parser *);
2289 static tree cp_parser_requires_expression
2290 (cp_parser *);
2291 static tree cp_parser_requirement_parameter_list
2292 (cp_parser *);
2293 static tree cp_parser_requirement_body
2294 (cp_parser *);
2295 static tree cp_parser_requirement_list
2296 (cp_parser *);
2297 static tree cp_parser_requirement
2298 (cp_parser *);
2299 static tree cp_parser_simple_requirement
2300 (cp_parser *);
2301 static tree cp_parser_compound_requirement
2302 (cp_parser *);
2303 static tree cp_parser_type_requirement
2304 (cp_parser *);
2305 static tree cp_parser_nested_requirement
2306 (cp_parser *);
2307 static tree cp_parser_constexpr_constraint_spec
2308 (cp_parser *, tree);
2309 static tree cp_parser_noexcept_constraint_spec
2310 (cp_parser *, tree);
2311 static tree cp_parser_constraint_spec
2312 (cp_parser *, tree);
2313 static tree cp_parser_constraint_specifier_seq
2314 (cp_parser *, tree);
2316 /* Transactional Memory Extensions */
2318 static tree cp_parser_transaction
2319 (cp_parser *, enum rid);
2320 static tree cp_parser_transaction_expression
2321 (cp_parser *, enum rid);
2322 static bool cp_parser_function_transaction
2323 (cp_parser *, enum rid);
2324 static tree cp_parser_transaction_cancel
2325 (cp_parser *);
2327 enum pragma_context {
2328 pragma_external,
2329 pragma_member,
2330 pragma_objc_icode,
2331 pragma_stmt,
2332 pragma_compound
2334 static bool cp_parser_pragma
2335 (cp_parser *, enum pragma_context);
2337 /* Objective-C++ Productions */
2339 static tree cp_parser_objc_message_receiver
2340 (cp_parser *);
2341 static tree cp_parser_objc_message_args
2342 (cp_parser *);
2343 static tree cp_parser_objc_message_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_encode_expression
2346 (cp_parser *);
2347 static tree cp_parser_objc_defs_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_protocol_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_selector_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_expression
2354 (cp_parser *);
2355 static bool cp_parser_objc_selector_p
2356 (enum cpp_ttype);
2357 static tree cp_parser_objc_selector
2358 (cp_parser *);
2359 static tree cp_parser_objc_protocol_refs_opt
2360 (cp_parser *);
2361 static void cp_parser_objc_declaration
2362 (cp_parser *, tree);
2363 static tree cp_parser_objc_statement
2364 (cp_parser *);
2365 static bool cp_parser_objc_valid_prefix_attributes
2366 (cp_parser *, tree *);
2367 static void cp_parser_objc_at_property_declaration
2368 (cp_parser *) ;
2369 static void cp_parser_objc_at_synthesize_declaration
2370 (cp_parser *) ;
2371 static void cp_parser_objc_at_dynamic_declaration
2372 (cp_parser *) ;
2373 static tree cp_parser_objc_struct_declaration
2374 (cp_parser *) ;
2376 /* Utility Routines */
2378 static tree cp_parser_lookup_name
2379 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2380 static tree cp_parser_lookup_name_simple
2381 (cp_parser *, tree, location_t);
2382 static tree cp_parser_maybe_treat_template_as_class
2383 (tree, bool);
2384 static bool cp_parser_check_declarator_template_parameters
2385 (cp_parser *, cp_declarator *, location_t);
2386 static bool cp_parser_check_template_parameters
2387 (cp_parser *, unsigned, location_t, cp_declarator *);
2388 static tree cp_parser_simple_cast_expression
2389 (cp_parser *);
2390 static tree cp_parser_global_scope_opt
2391 (cp_parser *, bool);
2392 static bool cp_parser_constructor_declarator_p
2393 (cp_parser *, bool);
2394 static tree cp_parser_function_definition_from_specifiers_and_declarator
2395 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2396 static tree cp_parser_function_definition_after_declarator
2397 (cp_parser *, bool);
2398 static void cp_parser_template_declaration_after_export
2399 (cp_parser *, bool);
2400 static void cp_parser_perform_template_parameter_access_checks
2401 (vec<deferred_access_check, va_gc> *);
2402 static tree cp_parser_single_declaration
2403 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2404 static tree cp_parser_functional_cast
2405 (cp_parser *, tree);
2406 static tree cp_parser_save_member_function_body
2407 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2408 static tree cp_parser_save_nsdmi
2409 (cp_parser *);
2410 static tree cp_parser_enclosed_template_argument_list
2411 (cp_parser *);
2412 static void cp_parser_save_default_args
2413 (cp_parser *, tree);
2414 static void cp_parser_late_parsing_for_member
2415 (cp_parser *, tree);
2416 static tree cp_parser_late_parse_one_default_arg
2417 (cp_parser *, tree, tree, tree);
2418 static void cp_parser_late_parsing_nsdmi
2419 (cp_parser *, tree);
2420 static void cp_parser_late_parsing_default_args
2421 (cp_parser *, tree);
2422 static tree cp_parser_sizeof_operand
2423 (cp_parser *, enum rid);
2424 static tree cp_parser_trait_expr
2425 (cp_parser *, enum rid);
2426 static bool cp_parser_declares_only_class_p
2427 (cp_parser *);
2428 static void cp_parser_set_storage_class
2429 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2430 static void cp_parser_set_decl_spec_type
2431 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2432 static void set_and_check_decl_spec_loc
2433 (cp_decl_specifier_seq *decl_specs,
2434 cp_decl_spec ds, cp_token *);
2435 static bool cp_parser_friend_p
2436 (const cp_decl_specifier_seq *);
2437 static void cp_parser_required_error
2438 (cp_parser *, required_token, bool);
2439 static cp_token *cp_parser_require
2440 (cp_parser *, enum cpp_ttype, required_token);
2441 static cp_token *cp_parser_require_keyword
2442 (cp_parser *, enum rid, required_token);
2443 static bool cp_parser_token_starts_function_definition_p
2444 (cp_token *);
2445 static bool cp_parser_next_token_starts_class_definition_p
2446 (cp_parser *);
2447 static bool cp_parser_next_token_ends_template_argument_p
2448 (cp_parser *);
2449 static bool cp_parser_nth_token_starts_template_argument_list_p
2450 (cp_parser *, size_t);
2451 static enum tag_types cp_parser_token_is_class_key
2452 (cp_token *);
2453 static void cp_parser_check_class_key
2454 (enum tag_types, tree type);
2455 static void cp_parser_check_access_in_redeclaration
2456 (tree type, location_t location);
2457 static bool cp_parser_optional_template_keyword
2458 (cp_parser *);
2459 static void cp_parser_pre_parsed_nested_name_specifier
2460 (cp_parser *);
2461 static bool cp_parser_cache_group
2462 (cp_parser *, enum cpp_ttype, unsigned);
2463 static tree cp_parser_cache_defarg
2464 (cp_parser *parser, bool nsdmi);
2465 static void cp_parser_parse_tentatively
2466 (cp_parser *);
2467 static void cp_parser_commit_to_tentative_parse
2468 (cp_parser *);
2469 static void cp_parser_commit_to_topmost_tentative_parse
2470 (cp_parser *);
2471 static void cp_parser_abort_tentative_parse
2472 (cp_parser *);
2473 static bool cp_parser_parse_definitely
2474 (cp_parser *);
2475 static inline bool cp_parser_parsing_tentatively
2476 (cp_parser *);
2477 static bool cp_parser_uncommitted_to_tentative_parse_p
2478 (cp_parser *);
2479 static void cp_parser_error
2480 (cp_parser *, const char *);
2481 static void cp_parser_name_lookup_error
2482 (cp_parser *, tree, tree, name_lookup_error, location_t);
2483 static bool cp_parser_simulate_error
2484 (cp_parser *);
2485 static bool cp_parser_check_type_definition
2486 (cp_parser *);
2487 static void cp_parser_check_for_definition_in_return_type
2488 (cp_declarator *, tree, location_t type_location);
2489 static void cp_parser_check_for_invalid_template_id
2490 (cp_parser *, tree, enum tag_types, location_t location);
2491 static bool cp_parser_non_integral_constant_expression
2492 (cp_parser *, non_integral_constant);
2493 static void cp_parser_diagnose_invalid_type_name
2494 (cp_parser *, tree, tree, location_t);
2495 static bool cp_parser_parse_and_diagnose_invalid_type_name
2496 (cp_parser *);
2497 static int cp_parser_skip_to_closing_parenthesis
2498 (cp_parser *, bool, bool, bool);
2499 static void cp_parser_skip_to_end_of_statement
2500 (cp_parser *);
2501 static void cp_parser_consume_semicolon_at_end_of_statement
2502 (cp_parser *);
2503 static void cp_parser_skip_to_end_of_block_or_statement
2504 (cp_parser *);
2505 static bool cp_parser_skip_to_closing_brace
2506 (cp_parser *);
2507 static void cp_parser_skip_to_end_of_template_parameter_list
2508 (cp_parser *);
2509 static void cp_parser_skip_to_pragma_eol
2510 (cp_parser*, cp_token *);
2511 static bool cp_parser_error_occurred
2512 (cp_parser *);
2513 static bool cp_parser_allow_gnu_extensions_p
2514 (cp_parser *);
2515 static bool cp_parser_is_pure_string_literal
2516 (cp_token *);
2517 static bool cp_parser_is_string_literal
2518 (cp_token *);
2519 static bool cp_parser_is_keyword
2520 (cp_token *, enum rid);
2521 static tree cp_parser_make_typename_type
2522 (cp_parser *, tree, tree, location_t location);
2523 static cp_declarator * cp_parser_make_indirect_declarator
2524 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2528 // -------------------------------------------------------------------------- //
2529 // Unevaluated Operand Guard
2531 // Implementation of an RAII helper for unevaluated operand parsing.
2532 cp_unevaluated::cp_unevaluated ()
2534 ++cp_unevaluated_operand;
2535 ++c_inhibit_evaluation_warnings;
2538 cp_unevaluated::~cp_unevaluated ()
2540 --c_inhibit_evaluation_warnings;
2541 --cp_unevaluated_operand;
2544 // -------------------------------------------------------------------------- //
2545 // Tentative Parsing
2547 /* Returns nonzero if we are parsing tentatively. */
2549 static inline bool
2550 cp_parser_parsing_tentatively (cp_parser* parser)
2552 return parser->context->next != NULL;
2555 /* Returns nonzero if TOKEN is a string literal. */
2557 static bool
2558 cp_parser_is_pure_string_literal (cp_token* token)
2560 return (token->type == CPP_STRING ||
2561 token->type == CPP_STRING16 ||
2562 token->type == CPP_STRING32 ||
2563 token->type == CPP_WSTRING ||
2564 token->type == CPP_UTF8STRING);
2567 /* Returns nonzero if TOKEN is a string literal
2568 of a user-defined string literal. */
2570 static bool
2571 cp_parser_is_string_literal (cp_token* token)
2573 return (cp_parser_is_pure_string_literal (token) ||
2574 token->type == CPP_STRING_USERDEF ||
2575 token->type == CPP_STRING16_USERDEF ||
2576 token->type == CPP_STRING32_USERDEF ||
2577 token->type == CPP_WSTRING_USERDEF ||
2578 token->type == CPP_UTF8STRING_USERDEF);
2581 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2583 static bool
2584 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2586 return token->keyword == keyword;
2589 /* If not parsing tentatively, issue a diagnostic of the form
2590 FILE:LINE: MESSAGE before TOKEN
2591 where TOKEN is the next token in the input stream. MESSAGE
2592 (specified by the caller) is usually of the form "expected
2593 OTHER-TOKEN". */
2595 static void
2596 cp_parser_error (cp_parser* parser, const char* gmsgid)
2598 if (!cp_parser_simulate_error (parser))
2600 cp_token *token = cp_lexer_peek_token (parser->lexer);
2601 /* This diagnostic makes more sense if it is tagged to the line
2602 of the token we just peeked at. */
2603 cp_lexer_set_source_position_from_token (token);
2605 if (token->type == CPP_PRAGMA)
2607 error_at (token->location,
2608 "%<#pragma%> is not allowed here");
2609 cp_parser_skip_to_pragma_eol (parser, token);
2610 return;
2613 c_parse_error (gmsgid,
2614 /* Because c_parser_error does not understand
2615 CPP_KEYWORD, keywords are treated like
2616 identifiers. */
2617 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2618 token->u.value, token->flags);
2622 /* Issue an error about name-lookup failing. NAME is the
2623 IDENTIFIER_NODE DECL is the result of
2624 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2625 the thing that we hoped to find. */
2627 static void
2628 cp_parser_name_lookup_error (cp_parser* parser,
2629 tree name,
2630 tree decl,
2631 name_lookup_error desired,
2632 location_t location)
2634 /* If name lookup completely failed, tell the user that NAME was not
2635 declared. */
2636 if (decl == error_mark_node)
2638 if (parser->scope && parser->scope != global_namespace)
2639 error_at (location, "%<%E::%E%> has not been declared",
2640 parser->scope, name);
2641 else if (parser->scope == global_namespace)
2642 error_at (location, "%<::%E%> has not been declared", name);
2643 else if (parser->object_scope
2644 && !CLASS_TYPE_P (parser->object_scope))
2645 error_at (location, "request for member %qE in non-class type %qT",
2646 name, parser->object_scope);
2647 else if (parser->object_scope)
2648 error_at (location, "%<%T::%E%> has not been declared",
2649 parser->object_scope, name);
2650 else
2651 error_at (location, "%qE has not been declared", name);
2653 else if (parser->scope && parser->scope != global_namespace)
2655 switch (desired)
2657 case NLE_TYPE:
2658 error_at (location, "%<%E::%E%> is not a type",
2659 parser->scope, name);
2660 break;
2661 case NLE_CXX98:
2662 error_at (location, "%<%E::%E%> is not a class or namespace",
2663 parser->scope, name);
2664 break;
2665 case NLE_NOT_CXX98:
2666 error_at (location,
2667 "%<%E::%E%> is not a class, namespace, or enumeration",
2668 parser->scope, name);
2669 break;
2670 default:
2671 gcc_unreachable ();
2675 else if (parser->scope == global_namespace)
2677 switch (desired)
2679 case NLE_TYPE:
2680 error_at (location, "%<::%E%> is not a type", name);
2681 break;
2682 case NLE_CXX98:
2683 error_at (location, "%<::%E%> is not a class or namespace", name);
2684 break;
2685 case NLE_NOT_CXX98:
2686 error_at (location,
2687 "%<::%E%> is not a class, namespace, or enumeration",
2688 name);
2689 break;
2690 default:
2691 gcc_unreachable ();
2694 else
2696 switch (desired)
2698 case NLE_TYPE:
2699 error_at (location, "%qE is not a type", name);
2700 break;
2701 case NLE_CXX98:
2702 error_at (location, "%qE is not a class or namespace", name);
2703 break;
2704 case NLE_NOT_CXX98:
2705 error_at (location,
2706 "%qE is not a class, namespace, or enumeration", name);
2707 break;
2708 default:
2709 gcc_unreachable ();
2714 /* If we are parsing tentatively, remember that an error has occurred
2715 during this tentative parse. Returns true if the error was
2716 simulated; false if a message should be issued by the caller. */
2718 static bool
2719 cp_parser_simulate_error (cp_parser* parser)
2721 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2723 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2724 return true;
2726 return false;
2729 /* This function is called when a type is defined. If type
2730 definitions are forbidden at this point, an error message is
2731 issued. */
2733 static bool
2734 cp_parser_check_type_definition (cp_parser* parser)
2736 /* If types are forbidden here, issue a message. */
2737 if (parser->type_definition_forbidden_message)
2739 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2740 in the message need to be interpreted. */
2741 error (parser->type_definition_forbidden_message);
2742 return false;
2744 return true;
2747 /* This function is called when the DECLARATOR is processed. The TYPE
2748 was a type defined in the decl-specifiers. If it is invalid to
2749 define a type in the decl-specifiers for DECLARATOR, an error is
2750 issued. TYPE_LOCATION is the location of TYPE and is used
2751 for error reporting. */
2753 static void
2754 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2755 tree type, location_t type_location)
2757 /* [dcl.fct] forbids type definitions in return types.
2758 Unfortunately, it's not easy to know whether or not we are
2759 processing a return type until after the fact. */
2760 while (declarator
2761 && (declarator->kind == cdk_pointer
2762 || declarator->kind == cdk_reference
2763 || declarator->kind == cdk_ptrmem))
2764 declarator = declarator->declarator;
2765 if (declarator
2766 && declarator->kind == cdk_function)
2768 error_at (type_location,
2769 "new types may not be defined in a return type");
2770 inform (type_location,
2771 "(perhaps a semicolon is missing after the definition of %qT)",
2772 type);
2776 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2777 "<" in any valid C++ program. If the next token is indeed "<",
2778 issue a message warning the user about what appears to be an
2779 invalid attempt to form a template-id. LOCATION is the location
2780 of the type-specifier (TYPE) */
2782 static void
2783 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2784 tree type,
2785 enum tag_types tag_type,
2786 location_t location)
2788 cp_token_position start = 0;
2790 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2792 if (TYPE_P (type))
2793 error_at (location, "%qT is not a template", type);
2794 else if (identifier_p (type))
2796 if (tag_type != none_type)
2797 error_at (location, "%qE is not a class template", type);
2798 else
2799 error_at (location, "%qE is not a template", type);
2801 else
2802 error_at (location, "invalid template-id");
2803 /* Remember the location of the invalid "<". */
2804 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2805 start = cp_lexer_token_position (parser->lexer, true);
2806 /* Consume the "<". */
2807 cp_lexer_consume_token (parser->lexer);
2808 /* Parse the template arguments. */
2809 cp_parser_enclosed_template_argument_list (parser);
2810 /* Permanently remove the invalid template arguments so that
2811 this error message is not issued again. */
2812 if (start)
2813 cp_lexer_purge_tokens_after (parser->lexer, start);
2817 /* If parsing an integral constant-expression, issue an error message
2818 about the fact that THING appeared and return true. Otherwise,
2819 return false. In either case, set
2820 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2822 static bool
2823 cp_parser_non_integral_constant_expression (cp_parser *parser,
2824 non_integral_constant thing)
2826 parser->non_integral_constant_expression_p = true;
2827 if (parser->integral_constant_expression_p)
2829 if (!parser->allow_non_integral_constant_expression_p)
2831 const char *msg = NULL;
2832 switch (thing)
2834 case NIC_FLOAT:
2835 error ("floating-point literal "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_CAST:
2839 error ("a cast to a type other than an integral or "
2840 "enumeration type cannot appear in a "
2841 "constant-expression");
2842 return true;
2843 case NIC_TYPEID:
2844 error ("%<typeid%> operator "
2845 "cannot appear in a constant-expression");
2846 return true;
2847 case NIC_NCC:
2848 error ("non-constant compound literals "
2849 "cannot appear in a constant-expression");
2850 return true;
2851 case NIC_FUNC_CALL:
2852 error ("a function call "
2853 "cannot appear in a constant-expression");
2854 return true;
2855 case NIC_INC:
2856 error ("an increment "
2857 "cannot appear in a constant-expression");
2858 return true;
2859 case NIC_DEC:
2860 error ("an decrement "
2861 "cannot appear in a constant-expression");
2862 return true;
2863 case NIC_ARRAY_REF:
2864 error ("an array reference "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_ADDR_LABEL:
2868 error ("the address of a label "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_OVERLOADED:
2872 error ("calls to overloaded operators "
2873 "cannot appear in a constant-expression");
2874 return true;
2875 case NIC_ASSIGNMENT:
2876 error ("an assignment cannot appear in a constant-expression");
2877 return true;
2878 case NIC_COMMA:
2879 error ("a comma operator "
2880 "cannot appear in a constant-expression");
2881 return true;
2882 case NIC_CONSTRUCTOR:
2883 error ("a call to a constructor "
2884 "cannot appear in a constant-expression");
2885 return true;
2886 case NIC_TRANSACTION:
2887 error ("a transaction expression "
2888 "cannot appear in a constant-expression");
2889 return true;
2890 case NIC_THIS:
2891 msg = "this";
2892 break;
2893 case NIC_FUNC_NAME:
2894 msg = "__FUNCTION__";
2895 break;
2896 case NIC_PRETTY_FUNC:
2897 msg = "__PRETTY_FUNCTION__";
2898 break;
2899 case NIC_C99_FUNC:
2900 msg = "__func__";
2901 break;
2902 case NIC_VA_ARG:
2903 msg = "va_arg";
2904 break;
2905 case NIC_ARROW:
2906 msg = "->";
2907 break;
2908 case NIC_POINT:
2909 msg = ".";
2910 break;
2911 case NIC_STAR:
2912 msg = "*";
2913 break;
2914 case NIC_ADDR:
2915 msg = "&";
2916 break;
2917 case NIC_PREINCREMENT:
2918 msg = "++";
2919 break;
2920 case NIC_PREDECREMENT:
2921 msg = "--";
2922 break;
2923 case NIC_NEW:
2924 msg = "new";
2925 break;
2926 case NIC_DEL:
2927 msg = "delete";
2928 break;
2929 default:
2930 gcc_unreachable ();
2932 if (msg)
2933 error ("%qs cannot appear in a constant-expression", msg);
2934 return true;
2937 return false;
2940 /* Emit a diagnostic for an invalid type name. SCOPE is the
2941 qualifying scope (or NULL, if none) for ID. This function commits
2942 to the current active tentative parse, if any. (Otherwise, the
2943 problematic construct might be encountered again later, resulting
2944 in duplicate error messages.) LOCATION is the location of ID. */
2946 static void
2947 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2948 tree scope, tree id,
2949 location_t location)
2951 tree decl, old_scope, ambiguous_decls;
2952 cp_parser_commit_to_tentative_parse (parser);
2953 /* Try to lookup the identifier. */
2954 old_scope = parser->scope;
2955 parser->scope = scope;
2956 decl = cp_parser_lookup_name (parser, id, none_type,
2957 /*is_template=*/false,
2958 /*is_namespace=*/false,
2959 /*check_dependency=*/true,
2960 &ambiguous_decls, location);
2961 parser->scope = old_scope;
2962 if (ambiguous_decls)
2963 /* If the lookup was ambiguous, an error will already have
2964 been issued. */
2965 return;
2966 /* If the lookup found a template-name, it means that the user forgot
2967 to specify an argument list. Emit a useful error message. */
2968 if (TREE_CODE (decl) == TEMPLATE_DECL)
2969 error_at (location,
2970 "invalid use of template-name %qE without an argument list",
2971 decl);
2972 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2973 error_at (location, "invalid use of destructor %qD as a type", id);
2974 else if (TREE_CODE (decl) == TYPE_DECL)
2975 /* Something like 'unsigned A a;' */
2976 error_at (location, "invalid combination of multiple type-specifiers");
2977 else if (!parser->scope)
2979 /* Issue an error message. */
2980 error_at (location, "%qE does not name a type", id);
2981 /* If we're in a template class, it's possible that the user was
2982 referring to a type from a base class. For example:
2984 template <typename T> struct A { typedef T X; };
2985 template <typename T> struct B : public A<T> { X x; };
2987 The user should have said "typename A<T>::X". */
2988 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2989 inform (location, "C++11 %<constexpr%> only available with "
2990 "-std=c++11 or -std=gnu++11");
2991 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2992 inform (location, "C++11 %<noexcept%> only available with "
2993 "-std=c++11 or -std=gnu++11");
2994 else if (cxx_dialect < cxx11
2995 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2996 inform (location, "C++11 %<thread_local%> only available with "
2997 "-std=c++11 or -std=gnu++11");
2998 else if (processing_template_decl && current_class_type
2999 && TYPE_BINFO (current_class_type))
3001 tree b;
3003 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3005 b = TREE_CHAIN (b))
3007 tree base_type = BINFO_TYPE (b);
3008 if (CLASS_TYPE_P (base_type)
3009 && dependent_type_p (base_type))
3011 tree field;
3012 /* Go from a particular instantiation of the
3013 template (which will have an empty TYPE_FIELDs),
3014 to the main version. */
3015 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3016 for (field = TYPE_FIELDS (base_type);
3017 field;
3018 field = DECL_CHAIN (field))
3019 if (TREE_CODE (field) == TYPE_DECL
3020 && DECL_NAME (field) == id)
3022 inform (location,
3023 "(perhaps %<typename %T::%E%> was intended)",
3024 BINFO_TYPE (b), id);
3025 break;
3027 if (field)
3028 break;
3033 /* Here we diagnose qualified-ids where the scope is actually correct,
3034 but the identifier does not resolve to a valid type name. */
3035 else if (parser->scope != error_mark_node)
3037 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3040 error_at (location_of (id),
3041 "%qE in namespace %qE does not name a template type",
3042 id, parser->scope);
3043 else
3044 error_at (location_of (id),
3045 "%qE in namespace %qE does not name a type",
3046 id, parser->scope);
3048 else if (CLASS_TYPE_P (parser->scope)
3049 && constructor_name_p (id, parser->scope))
3051 /* A<T>::A<T>() */
3052 error_at (location, "%<%T::%E%> names the constructor, not"
3053 " the type", parser->scope, id);
3054 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3055 error_at (location, "and %qT has no template constructors",
3056 parser->scope);
3058 else if (TYPE_P (parser->scope)
3059 && dependent_scope_p (parser->scope))
3060 error_at (location, "need %<typename%> before %<%T::%E%> because "
3061 "%qT is a dependent scope",
3062 parser->scope, id, parser->scope);
3063 else if (TYPE_P (parser->scope))
3065 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3066 error_at (location_of (id),
3067 "%qE in %q#T does not name a template type",
3068 id, parser->scope);
3069 else
3070 error_at (location_of (id),
3071 "%qE in %q#T does not name a type",
3072 id, parser->scope);
3074 else
3075 gcc_unreachable ();
3079 /* Check for a common situation where a type-name should be present,
3080 but is not, and issue a sensible error message. Returns true if an
3081 invalid type-name was detected.
3083 The situation handled by this function are variable declarations of the
3084 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3085 Usually, `ID' should name a type, but if we got here it means that it
3086 does not. We try to emit the best possible error message depending on
3087 how exactly the id-expression looks like. */
3089 static bool
3090 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3092 tree id;
3093 cp_token *token = cp_lexer_peek_token (parser->lexer);
3095 /* Avoid duplicate error about ambiguous lookup. */
3096 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3098 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3099 if (next->type == CPP_NAME && next->error_reported)
3100 goto out;
3103 cp_parser_parse_tentatively (parser);
3104 id = cp_parser_id_expression (parser,
3105 /*template_keyword_p=*/false,
3106 /*check_dependency_p=*/true,
3107 /*template_p=*/NULL,
3108 /*declarator_p=*/true,
3109 /*optional_p=*/false);
3110 /* If the next token is a (, this is a function with no explicit return
3111 type, i.e. constructor, destructor or conversion op. */
3112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3113 || TREE_CODE (id) == TYPE_DECL)
3115 cp_parser_abort_tentative_parse (parser);
3116 return false;
3118 if (!cp_parser_parse_definitely (parser))
3119 return false;
3121 /* Emit a diagnostic for the invalid type. */
3122 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3123 id, token->location);
3124 out:
3125 /* If we aren't in the middle of a declarator (i.e. in a
3126 parameter-declaration-clause), skip to the end of the declaration;
3127 there's no point in trying to process it. */
3128 if (!parser->in_declarator_p)
3129 cp_parser_skip_to_end_of_block_or_statement (parser);
3130 return true;
3133 /* Consume tokens up to, and including, the next non-nested closing `)'.
3134 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3135 are doing error recovery. Returns -1 if OR_COMMA is true and we
3136 found an unnested comma. */
3138 static int
3139 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3140 bool recovering,
3141 bool or_comma,
3142 bool consume_paren)
3144 unsigned paren_depth = 0;
3145 unsigned brace_depth = 0;
3146 unsigned square_depth = 0;
3148 if (recovering && !or_comma
3149 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3150 return 0;
3152 while (true)
3154 cp_token * token = cp_lexer_peek_token (parser->lexer);
3156 switch (token->type)
3158 case CPP_EOF:
3159 case CPP_PRAGMA_EOL:
3160 /* If we've run out of tokens, then there is no closing `)'. */
3161 return 0;
3163 /* This is good for lambda expression capture-lists. */
3164 case CPP_OPEN_SQUARE:
3165 ++square_depth;
3166 break;
3167 case CPP_CLOSE_SQUARE:
3168 if (!square_depth--)
3169 return 0;
3170 break;
3172 case CPP_SEMICOLON:
3173 /* This matches the processing in skip_to_end_of_statement. */
3174 if (!brace_depth)
3175 return 0;
3176 break;
3178 case CPP_OPEN_BRACE:
3179 ++brace_depth;
3180 break;
3181 case CPP_CLOSE_BRACE:
3182 if (!brace_depth--)
3183 return 0;
3184 break;
3186 case CPP_COMMA:
3187 if (recovering && or_comma && !brace_depth && !paren_depth
3188 && !square_depth)
3189 return -1;
3190 break;
3192 case CPP_OPEN_PAREN:
3193 if (!brace_depth)
3194 ++paren_depth;
3195 break;
3197 case CPP_CLOSE_PAREN:
3198 if (!brace_depth && !paren_depth--)
3200 if (consume_paren)
3201 cp_lexer_consume_token (parser->lexer);
3202 return 1;
3204 break;
3206 default:
3207 break;
3210 /* Consume the token. */
3211 cp_lexer_consume_token (parser->lexer);
3215 /* Consume tokens until we reach the end of the current statement.
3216 Normally, that will be just before consuming a `;'. However, if a
3217 non-nested `}' comes first, then we stop before consuming that. */
3219 static void
3220 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3222 unsigned nesting_depth = 0;
3224 /* Unwind generic function template scope if necessary. */
3225 if (parser->fully_implicit_function_template_p)
3226 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3228 while (true)
3230 cp_token *token = cp_lexer_peek_token (parser->lexer);
3232 switch (token->type)
3234 case CPP_EOF:
3235 case CPP_PRAGMA_EOL:
3236 /* If we've run out of tokens, stop. */
3237 return;
3239 case CPP_SEMICOLON:
3240 /* If the next token is a `;', we have reached the end of the
3241 statement. */
3242 if (!nesting_depth)
3243 return;
3244 break;
3246 case CPP_CLOSE_BRACE:
3247 /* If this is a non-nested '}', stop before consuming it.
3248 That way, when confronted with something like:
3250 { 3 + }
3252 we stop before consuming the closing '}', even though we
3253 have not yet reached a `;'. */
3254 if (nesting_depth == 0)
3255 return;
3257 /* If it is the closing '}' for a block that we have
3258 scanned, stop -- but only after consuming the token.
3259 That way given:
3261 void f g () { ... }
3262 typedef int I;
3264 we will stop after the body of the erroneously declared
3265 function, but before consuming the following `typedef'
3266 declaration. */
3267 if (--nesting_depth == 0)
3269 cp_lexer_consume_token (parser->lexer);
3270 return;
3273 case CPP_OPEN_BRACE:
3274 ++nesting_depth;
3275 break;
3277 default:
3278 break;
3281 /* Consume the token. */
3282 cp_lexer_consume_token (parser->lexer);
3286 /* This function is called at the end of a statement or declaration.
3287 If the next token is a semicolon, it is consumed; otherwise, error
3288 recovery is attempted. */
3290 static void
3291 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3293 /* Look for the trailing `;'. */
3294 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3296 /* If there is additional (erroneous) input, skip to the end of
3297 the statement. */
3298 cp_parser_skip_to_end_of_statement (parser);
3299 /* If the next token is now a `;', consume it. */
3300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3301 cp_lexer_consume_token (parser->lexer);
3305 /* Skip tokens until we have consumed an entire block, or until we
3306 have consumed a non-nested `;'. */
3308 static void
3309 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3311 int nesting_depth = 0;
3313 /* Unwind generic function template scope if necessary. */
3314 if (parser->fully_implicit_function_template_p)
3315 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3317 while (nesting_depth >= 0)
3319 cp_token *token = cp_lexer_peek_token (parser->lexer);
3321 switch (token->type)
3323 case CPP_EOF:
3324 case CPP_PRAGMA_EOL:
3325 /* If we've run out of tokens, stop. */
3326 return;
3328 case CPP_SEMICOLON:
3329 /* Stop if this is an unnested ';'. */
3330 if (!nesting_depth)
3331 nesting_depth = -1;
3332 break;
3334 case CPP_CLOSE_BRACE:
3335 /* Stop if this is an unnested '}', or closes the outermost
3336 nesting level. */
3337 nesting_depth--;
3338 if (nesting_depth < 0)
3339 return;
3340 if (!nesting_depth)
3341 nesting_depth = -1;
3342 break;
3344 case CPP_OPEN_BRACE:
3345 /* Nest. */
3346 nesting_depth++;
3347 break;
3349 default:
3350 break;
3353 /* Consume the token. */
3354 cp_lexer_consume_token (parser->lexer);
3358 /* Skip tokens until a non-nested closing curly brace is the next
3359 token, or there are no more tokens. Return true in the first case,
3360 false otherwise. */
3362 static bool
3363 cp_parser_skip_to_closing_brace (cp_parser *parser)
3365 unsigned nesting_depth = 0;
3367 while (true)
3369 cp_token *token = cp_lexer_peek_token (parser->lexer);
3371 switch (token->type)
3373 case CPP_EOF:
3374 case CPP_PRAGMA_EOL:
3375 /* If we've run out of tokens, stop. */
3376 return false;
3378 case CPP_CLOSE_BRACE:
3379 /* If the next token is a non-nested `}', then we have reached
3380 the end of the current block. */
3381 if (nesting_depth-- == 0)
3382 return true;
3383 break;
3385 case CPP_OPEN_BRACE:
3386 /* If it the next token is a `{', then we are entering a new
3387 block. Consume the entire block. */
3388 ++nesting_depth;
3389 break;
3391 default:
3392 break;
3395 /* Consume the token. */
3396 cp_lexer_consume_token (parser->lexer);
3400 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3401 parameter is the PRAGMA token, allowing us to purge the entire pragma
3402 sequence. */
3404 static void
3405 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3407 cp_token *token;
3409 parser->lexer->in_pragma = false;
3412 token = cp_lexer_consume_token (parser->lexer);
3413 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3415 /* Ensure that the pragma is not parsed again. */
3416 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3419 /* Require pragma end of line, resyncing with it as necessary. The
3420 arguments are as for cp_parser_skip_to_pragma_eol. */
3422 static void
3423 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3425 parser->lexer->in_pragma = false;
3426 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3427 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3430 /* This is a simple wrapper around make_typename_type. When the id is
3431 an unresolved identifier node, we can provide a superior diagnostic
3432 using cp_parser_diagnose_invalid_type_name. */
3434 static tree
3435 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3436 tree id, location_t id_location)
3438 tree result;
3439 if (identifier_p (id))
3441 result = make_typename_type (scope, id, typename_type,
3442 /*complain=*/tf_none);
3443 if (result == error_mark_node)
3444 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3445 return result;
3447 return make_typename_type (scope, id, typename_type, tf_error);
3450 /* This is a wrapper around the
3451 make_{pointer,ptrmem,reference}_declarator functions that decides
3452 which one to call based on the CODE and CLASS_TYPE arguments. The
3453 CODE argument should be one of the values returned by
3454 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3455 appertain to the pointer or reference. */
3457 static cp_declarator *
3458 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3459 cp_cv_quals cv_qualifiers,
3460 cp_declarator *target,
3461 tree attributes)
3463 if (code == ERROR_MARK)
3464 return cp_error_declarator;
3466 if (code == INDIRECT_REF)
3467 if (class_type == NULL_TREE)
3468 return make_pointer_declarator (cv_qualifiers, target, attributes);
3469 else
3470 return make_ptrmem_declarator (cv_qualifiers, class_type,
3471 target, attributes);
3472 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3473 return make_reference_declarator (cv_qualifiers, target,
3474 false, attributes);
3475 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3476 return make_reference_declarator (cv_qualifiers, target,
3477 true, attributes);
3478 gcc_unreachable ();
3481 /* Create a new C++ parser. */
3483 static cp_parser *
3484 cp_parser_new (void)
3486 cp_parser *parser;
3487 cp_lexer *lexer;
3488 unsigned i;
3490 /* cp_lexer_new_main is called before doing GC allocation because
3491 cp_lexer_new_main might load a PCH file. */
3492 lexer = cp_lexer_new_main ();
3494 /* Initialize the binops_by_token so that we can get the tree
3495 directly from the token. */
3496 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3497 binops_by_token[binops[i].token_type] = binops[i];
3499 parser = ggc_cleared_alloc<cp_parser> ();
3500 parser->lexer = lexer;
3501 parser->context = cp_parser_context_new (NULL);
3503 /* For now, we always accept GNU extensions. */
3504 parser->allow_gnu_extensions_p = 1;
3506 /* The `>' token is a greater-than operator, not the end of a
3507 template-id. */
3508 parser->greater_than_is_operator_p = true;
3510 parser->default_arg_ok_p = true;
3512 /* We are not parsing a constant-expression. */
3513 parser->integral_constant_expression_p = false;
3514 parser->allow_non_integral_constant_expression_p = false;
3515 parser->non_integral_constant_expression_p = false;
3517 /* Local variable names are not forbidden. */
3518 parser->local_variables_forbidden_p = false;
3520 /* We are not processing an `extern "C"' declaration. */
3521 parser->in_unbraced_linkage_specification_p = false;
3523 /* We are not processing a declarator. */
3524 parser->in_declarator_p = false;
3526 /* We are not processing a template-argument-list. */
3527 parser->in_template_argument_list_p = false;
3529 /* We are not in an iteration statement. */
3530 parser->in_statement = 0;
3532 /* We are not in a switch statement. */
3533 parser->in_switch_statement_p = false;
3535 /* We are not parsing a type-id inside an expression. */
3536 parser->in_type_id_in_expr_p = false;
3538 /* Declarations aren't implicitly extern "C". */
3539 parser->implicit_extern_c = false;
3541 /* String literals should be translated to the execution character set. */
3542 parser->translate_strings_p = true;
3544 /* We are not parsing a function body. */
3545 parser->in_function_body = false;
3547 /* We can correct until told otherwise. */
3548 parser->colon_corrects_to_scope_p = true;
3550 /* The unparsed function queue is empty. */
3551 push_unparsed_function_queues (parser);
3553 /* There are no classes being defined. */
3554 parser->num_classes_being_defined = 0;
3556 /* No template parameters apply. */
3557 parser->num_template_parameter_lists = 0;
3559 /* Not declaring an implicit function template. */
3560 parser->auto_is_implicit_function_template_parm_p = false;
3561 parser->fully_implicit_function_template_p = false;
3562 parser->implicit_template_parms = 0;
3563 parser->implicit_template_scope = 0;
3565 return parser;
3568 /* Create a cp_lexer structure which will emit the tokens in CACHE
3569 and push it onto the parser's lexer stack. This is used for delayed
3570 parsing of in-class method bodies and default arguments, and should
3571 not be confused with tentative parsing. */
3572 static void
3573 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3575 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3576 lexer->next = parser->lexer;
3577 parser->lexer = lexer;
3579 /* Move the current source position to that of the first token in the
3580 new lexer. */
3581 cp_lexer_set_source_position_from_token (lexer->next_token);
3584 /* Pop the top lexer off the parser stack. This is never used for the
3585 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3586 static void
3587 cp_parser_pop_lexer (cp_parser *parser)
3589 cp_lexer *lexer = parser->lexer;
3590 parser->lexer = lexer->next;
3591 cp_lexer_destroy (lexer);
3593 /* Put the current source position back where it was before this
3594 lexer was pushed. */
3595 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3598 /* Lexical conventions [gram.lex] */
3600 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3601 identifier. */
3603 static tree
3604 cp_parser_identifier (cp_parser* parser)
3606 cp_token *token;
3608 /* Look for the identifier. */
3609 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3610 /* Return the value. */
3611 return token ? token->u.value : error_mark_node;
3614 /* Parse a sequence of adjacent string constants. Returns a
3615 TREE_STRING representing the combined, nul-terminated string
3616 constant. If TRANSLATE is true, translate the string to the
3617 execution character set. If WIDE_OK is true, a wide string is
3618 invalid here.
3620 C++98 [lex.string] says that if a narrow string literal token is
3621 adjacent to a wide string literal token, the behavior is undefined.
3622 However, C99 6.4.5p4 says that this results in a wide string literal.
3623 We follow C99 here, for consistency with the C front end.
3625 This code is largely lifted from lex_string() in c-lex.c.
3627 FUTURE: ObjC++ will need to handle @-strings here. */
3628 static tree
3629 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3631 tree value;
3632 size_t count;
3633 struct obstack str_ob;
3634 cpp_string str, istr, *strs;
3635 cp_token *tok;
3636 enum cpp_ttype type, curr_type;
3637 int have_suffix_p = 0;
3638 tree string_tree;
3639 tree suffix_id = NULL_TREE;
3640 bool curr_tok_is_userdef_p = false;
3642 tok = cp_lexer_peek_token (parser->lexer);
3643 if (!cp_parser_is_string_literal (tok))
3645 cp_parser_error (parser, "expected string-literal");
3646 return error_mark_node;
3649 if (cpp_userdef_string_p (tok->type))
3651 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3652 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 curr_tok_is_userdef_p = true;
3655 else
3657 string_tree = tok->u.value;
3658 curr_type = tok->type;
3660 type = curr_type;
3662 /* Try to avoid the overhead of creating and destroying an obstack
3663 for the common case of just one string. */
3664 if (!cp_parser_is_string_literal
3665 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3667 cp_lexer_consume_token (parser->lexer);
3669 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3670 str.len = TREE_STRING_LENGTH (string_tree);
3671 count = 1;
3673 if (curr_tok_is_userdef_p)
3675 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3676 have_suffix_p = 1;
3677 curr_type = cpp_userdef_string_remove_type (tok->type);
3679 else
3680 curr_type = tok->type;
3682 strs = &str;
3684 else
3686 gcc_obstack_init (&str_ob);
3687 count = 0;
3691 cp_lexer_consume_token (parser->lexer);
3692 count++;
3693 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3694 str.len = TREE_STRING_LENGTH (string_tree);
3696 if (curr_tok_is_userdef_p)
3698 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3699 if (have_suffix_p == 0)
3701 suffix_id = curr_suffix_id;
3702 have_suffix_p = 1;
3704 else if (have_suffix_p == 1
3705 && curr_suffix_id != suffix_id)
3707 error ("inconsistent user-defined literal suffixes"
3708 " %qD and %qD in string literal",
3709 suffix_id, curr_suffix_id);
3710 have_suffix_p = -1;
3712 curr_type = cpp_userdef_string_remove_type (tok->type);
3714 else
3715 curr_type = tok->type;
3717 if (type != curr_type)
3719 if (type == CPP_STRING)
3720 type = curr_type;
3721 else if (curr_type != CPP_STRING)
3722 error_at (tok->location,
3723 "unsupported non-standard concatenation "
3724 "of string literals");
3727 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3729 tok = cp_lexer_peek_token (parser->lexer);
3730 if (cpp_userdef_string_p (tok->type))
3732 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3733 curr_type = cpp_userdef_string_remove_type (tok->type);
3734 curr_tok_is_userdef_p = true;
3736 else
3738 string_tree = tok->u.value;
3739 curr_type = tok->type;
3740 curr_tok_is_userdef_p = false;
3743 while (cp_parser_is_string_literal (tok));
3745 strs = (cpp_string *) obstack_finish (&str_ob);
3748 if (type != CPP_STRING && !wide_ok)
3750 cp_parser_error (parser, "a wide string is invalid in this context");
3751 type = CPP_STRING;
3754 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3755 (parse_in, strs, count, &istr, type))
3757 value = build_string (istr.len, (const char *)istr.text);
3758 free (CONST_CAST (unsigned char *, istr.text));
3760 switch (type)
3762 default:
3763 case CPP_STRING:
3764 case CPP_UTF8STRING:
3765 TREE_TYPE (value) = char_array_type_node;
3766 break;
3767 case CPP_STRING16:
3768 TREE_TYPE (value) = char16_array_type_node;
3769 break;
3770 case CPP_STRING32:
3771 TREE_TYPE (value) = char32_array_type_node;
3772 break;
3773 case CPP_WSTRING:
3774 TREE_TYPE (value) = wchar_array_type_node;
3775 break;
3778 value = fix_string_type (value);
3780 if (have_suffix_p)
3782 tree literal = build_userdef_literal (suffix_id, value,
3783 OT_NONE, NULL_TREE);
3784 tok->u.value = literal;
3785 return cp_parser_userdef_string_literal (tok);
3788 else
3789 /* cpp_interpret_string has issued an error. */
3790 value = error_mark_node;
3792 if (count > 1)
3793 obstack_free (&str_ob, 0);
3795 return value;
3798 /* Look up a literal operator with the name and the exact arguments. */
3800 static tree
3801 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3803 tree decl, fns;
3804 decl = lookup_name (name);
3805 if (!decl || !is_overloaded_fn (decl))
3806 return error_mark_node;
3808 for (fns = decl; fns; fns = OVL_NEXT (fns))
3810 unsigned int ix;
3811 bool found = true;
3812 tree fn = OVL_CURRENT (fns);
3813 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3814 if (parmtypes != NULL_TREE)
3816 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3817 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3819 tree tparm = TREE_VALUE (parmtypes);
3820 tree targ = TREE_TYPE ((*args)[ix]);
3821 bool ptr = TYPE_PTR_P (tparm);
3822 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3823 if ((ptr || arr || !same_type_p (tparm, targ))
3824 && (!ptr || !arr
3825 || !same_type_p (TREE_TYPE (tparm),
3826 TREE_TYPE (targ))))
3827 found = false;
3829 if (found
3830 && ix == vec_safe_length (args)
3831 /* May be this should be sufficient_parms_p instead,
3832 depending on how exactly should user-defined literals
3833 work in presence of default arguments on the literal
3834 operator parameters. */
3835 && parmtypes == void_list_node)
3836 return fn;
3840 return error_mark_node;
3843 /* Parse a user-defined char constant. Returns a call to a user-defined
3844 literal operator taking the character as an argument. */
3846 static tree
3847 cp_parser_userdef_char_literal (cp_parser *parser)
3849 cp_token *token = cp_lexer_consume_token (parser->lexer);
3850 tree literal = token->u.value;
3851 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3852 tree value = USERDEF_LITERAL_VALUE (literal);
3853 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3854 tree decl, result;
3856 /* Build up a call to the user-defined operator */
3857 /* Lookup the name we got back from the id-expression. */
3858 vec<tree, va_gc> *args = make_tree_vector ();
3859 vec_safe_push (args, value);
3860 decl = lookup_literal_operator (name, args);
3861 if (!decl || decl == error_mark_node)
3863 error ("unable to find character literal operator %qD with %qT argument",
3864 name, TREE_TYPE (value));
3865 release_tree_vector (args);
3866 return error_mark_node;
3868 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3869 release_tree_vector (args);
3870 if (result != error_mark_node)
3871 return result;
3873 error ("unable to find character literal operator %qD with %qT argument",
3874 name, TREE_TYPE (value));
3875 return error_mark_node;
3878 /* A subroutine of cp_parser_userdef_numeric_literal to
3879 create a char... template parameter pack from a string node. */
3881 static tree
3882 make_char_string_pack (tree value)
3884 tree charvec;
3885 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3886 const char *str = TREE_STRING_POINTER (value);
3887 int i, len = TREE_STRING_LENGTH (value) - 1;
3888 tree argvec = make_tree_vec (1);
3890 /* Fill in CHARVEC with all of the parameters. */
3891 charvec = make_tree_vec (len);
3892 for (i = 0; i < len; ++i)
3893 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3895 /* Build the argument packs. */
3896 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3897 TREE_TYPE (argpack) = char_type_node;
3899 TREE_VEC_ELT (argvec, 0) = argpack;
3901 return argvec;
3904 /* A subroutine of cp_parser_userdef_numeric_literal to
3905 create a char... template parameter pack from a string node. */
3907 static tree
3908 make_string_pack (tree value)
3910 tree charvec;
3911 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3912 const unsigned char *str
3913 = (const unsigned char *) TREE_STRING_POINTER (value);
3914 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3915 int len = TREE_STRING_LENGTH (value) / sz - 1;
3916 tree argvec = make_tree_vec (2);
3918 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3919 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3921 /* First template parm is character type. */
3922 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3924 /* Fill in CHARVEC with all of the parameters. */
3925 charvec = make_tree_vec (len);
3926 for (int i = 0; i < len; ++i)
3927 TREE_VEC_ELT (charvec, i)
3928 = double_int_to_tree (str_char_type_node,
3929 double_int::from_buffer (str + i * sz, sz));
3931 /* Build the argument packs. */
3932 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3933 TREE_TYPE (argpack) = str_char_type_node;
3935 TREE_VEC_ELT (argvec, 1) = argpack;
3937 return argvec;
3940 /* Parse a user-defined numeric constant. returns a call to a user-defined
3941 literal operator. */
3943 static tree
3944 cp_parser_userdef_numeric_literal (cp_parser *parser)
3946 cp_token *token = cp_lexer_consume_token (parser->lexer);
3947 tree literal = token->u.value;
3948 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3949 tree value = USERDEF_LITERAL_VALUE (literal);
3950 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3951 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3952 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3953 tree decl, result;
3954 vec<tree, va_gc> *args;
3956 /* Look for a literal operator taking the exact type of numeric argument
3957 as the literal value. */
3958 args = make_tree_vector ();
3959 vec_safe_push (args, value);
3960 decl = lookup_literal_operator (name, args);
3961 if (decl && decl != error_mark_node)
3963 result = finish_call_expr (decl, &args, false, true, tf_none);
3964 if (result != error_mark_node)
3966 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3967 warning_at (token->location, OPT_Woverflow,
3968 "integer literal exceeds range of %qT type",
3969 long_long_unsigned_type_node);
3970 else
3972 if (overflow > 0)
3973 warning_at (token->location, OPT_Woverflow,
3974 "floating literal exceeds range of %qT type",
3975 long_double_type_node);
3976 else if (overflow < 0)
3977 warning_at (token->location, OPT_Woverflow,
3978 "floating literal truncated to zero");
3980 release_tree_vector (args);
3981 return result;
3984 release_tree_vector (args);
3986 /* If the numeric argument didn't work, look for a raw literal
3987 operator taking a const char* argument consisting of the number
3988 in string format. */
3989 args = make_tree_vector ();
3990 vec_safe_push (args, num_string);
3991 decl = lookup_literal_operator (name, args);
3992 if (decl && decl != error_mark_node)
3994 result = finish_call_expr (decl, &args, false, true, tf_none);
3995 if (result != error_mark_node)
3997 release_tree_vector (args);
3998 return result;
4001 release_tree_vector (args);
4003 /* If the raw literal didn't work, look for a non-type template
4004 function with parameter pack char.... Call the function with
4005 template parameter characters representing the number. */
4006 args = make_tree_vector ();
4007 decl = lookup_literal_operator (name, args);
4008 if (decl && decl != error_mark_node)
4010 tree tmpl_args = make_char_string_pack (num_string);
4011 decl = lookup_template_function (decl, tmpl_args);
4012 result = finish_call_expr (decl, &args, false, true, tf_none);
4013 if (result != error_mark_node)
4015 release_tree_vector (args);
4016 return result;
4019 release_tree_vector (args);
4021 error ("unable to find numeric literal operator %qD", name);
4022 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4023 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4024 "to enable more built-in suffixes");
4025 return error_mark_node;
4028 /* Parse a user-defined string constant. Returns a call to a user-defined
4029 literal operator taking a character pointer and the length of the string
4030 as arguments. */
4032 static tree
4033 cp_parser_userdef_string_literal (cp_token *token)
4035 tree literal = token->u.value;
4036 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4037 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4038 tree value = USERDEF_LITERAL_VALUE (literal);
4039 int len = TREE_STRING_LENGTH (value)
4040 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4041 tree decl, result;
4042 vec<tree, va_gc> *args;
4044 /* Look for a template function with typename parameter CharT
4045 and parameter pack CharT... Call the function with
4046 template parameter characters representing the string. */
4047 args = make_tree_vector ();
4048 decl = lookup_literal_operator (name, args);
4049 if (decl && decl != error_mark_node)
4051 tree tmpl_args = make_string_pack (value);
4052 decl = lookup_template_function (decl, tmpl_args);
4053 result = finish_call_expr (decl, &args, false, true, tf_none);
4054 if (result != error_mark_node)
4056 release_tree_vector (args);
4057 return result;
4060 release_tree_vector (args);
4062 /* Build up a call to the user-defined operator */
4063 /* Lookup the name we got back from the id-expression. */
4064 args = make_tree_vector ();
4065 vec_safe_push (args, value);
4066 vec_safe_push (args, build_int_cst (size_type_node, len));
4067 decl = lookup_name (name);
4068 if (!decl || decl == error_mark_node)
4070 error ("unable to find string literal operator %qD", name);
4071 release_tree_vector (args);
4072 return error_mark_node;
4074 result = finish_call_expr (decl, &args, false, true, tf_none);
4075 release_tree_vector (args);
4076 if (result != error_mark_node)
4077 return result;
4079 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4080 name, TREE_TYPE (value), size_type_node);
4081 return error_mark_node;
4085 /* Basic concepts [gram.basic] */
4087 /* Parse a translation-unit.
4089 translation-unit:
4090 declaration-seq [opt]
4092 Returns TRUE if all went well. */
4094 static bool
4095 cp_parser_translation_unit (cp_parser* parser)
4097 /* The address of the first non-permanent object on the declarator
4098 obstack. */
4099 static void *declarator_obstack_base;
4101 bool success;
4103 /* Create the declarator obstack, if necessary. */
4104 if (!cp_error_declarator)
4106 gcc_obstack_init (&declarator_obstack);
4107 /* Create the error declarator. */
4108 cp_error_declarator = make_declarator (cdk_error);
4109 /* Create the empty parameter list. */
4110 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4111 /* Remember where the base of the declarator obstack lies. */
4112 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4115 cp_parser_declaration_seq_opt (parser);
4117 /* If there are no tokens left then all went well. */
4118 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4120 /* Get rid of the token array; we don't need it any more. */
4121 cp_lexer_destroy (parser->lexer);
4122 parser->lexer = NULL;
4124 /* This file might have been a context that's implicitly extern
4125 "C". If so, pop the lang context. (Only relevant for PCH.) */
4126 if (parser->implicit_extern_c)
4128 pop_lang_context ();
4129 parser->implicit_extern_c = false;
4132 /* Finish up. */
4133 finish_translation_unit ();
4135 success = true;
4137 else
4139 cp_parser_error (parser, "expected declaration");
4140 success = false;
4143 /* Make sure the declarator obstack was fully cleaned up. */
4144 gcc_assert (obstack_next_free (&declarator_obstack)
4145 == declarator_obstack_base);
4147 /* All went well. */
4148 return success;
4151 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4152 decltype context. */
4154 static inline tsubst_flags_t
4155 complain_flags (bool decltype_p)
4157 tsubst_flags_t complain = tf_warning_or_error;
4158 if (decltype_p)
4159 complain |= tf_decltype;
4160 return complain;
4163 /* Expressions [gram.expr] */
4165 /* Parse a primary-expression.
4167 primary-expression:
4168 literal
4169 this
4170 ( expression )
4171 id-expression
4173 GNU Extensions:
4175 primary-expression:
4176 ( compound-statement )
4177 __builtin_va_arg ( assignment-expression , type-id )
4178 __builtin_offsetof ( type-id , offsetof-expression )
4180 C++ Extensions:
4181 __has_nothrow_assign ( type-id )
4182 __has_nothrow_constructor ( type-id )
4183 __has_nothrow_copy ( type-id )
4184 __has_trivial_assign ( type-id )
4185 __has_trivial_constructor ( type-id )
4186 __has_trivial_copy ( type-id )
4187 __has_trivial_destructor ( type-id )
4188 __has_virtual_destructor ( type-id )
4189 __is_abstract ( type-id )
4190 __is_base_of ( type-id , type-id )
4191 __is_class ( type-id )
4192 __is_convertible_to ( type-id , type-id )
4193 __is_empty ( type-id )
4194 __is_enum ( type-id )
4195 __is_final ( type-id )
4196 __is_literal_type ( type-id )
4197 __is_pod ( type-id )
4198 __is_polymorphic ( type-id )
4199 __is_std_layout ( type-id )
4200 __is_trivial ( type-id )
4201 __is_union ( type-id )
4203 Objective-C++ Extension:
4205 primary-expression:
4206 objc-expression
4208 literal:
4209 __null
4211 ADDRESS_P is true iff this expression was immediately preceded by
4212 "&" and therefore might denote a pointer-to-member. CAST_P is true
4213 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4214 true iff this expression is a template argument.
4216 Returns a representation of the expression. Upon return, *IDK
4217 indicates what kind of id-expression (if any) was present. */
4219 static tree
4220 cp_parser_primary_expression (cp_parser *parser,
4221 bool address_p,
4222 bool cast_p,
4223 bool template_arg_p,
4224 bool decltype_p,
4225 cp_id_kind *idk)
4227 cp_token *token = NULL;
4229 /* Assume the primary expression is not an id-expression. */
4230 *idk = CP_ID_KIND_NONE;
4232 /* Peek at the next token. */
4233 token = cp_lexer_peek_token (parser->lexer);
4234 switch (token->type)
4236 /* literal:
4237 integer-literal
4238 character-literal
4239 floating-literal
4240 string-literal
4241 boolean-literal
4242 pointer-literal
4243 user-defined-literal */
4244 case CPP_CHAR:
4245 case CPP_CHAR16:
4246 case CPP_CHAR32:
4247 case CPP_WCHAR:
4248 case CPP_NUMBER:
4249 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4250 return cp_parser_userdef_numeric_literal (parser);
4251 token = cp_lexer_consume_token (parser->lexer);
4252 if (TREE_CODE (token->u.value) == FIXED_CST)
4254 error_at (token->location,
4255 "fixed-point types not supported in C++");
4256 return error_mark_node;
4258 /* Floating-point literals are only allowed in an integral
4259 constant expression if they are cast to an integral or
4260 enumeration type. */
4261 if (TREE_CODE (token->u.value) == REAL_CST
4262 && parser->integral_constant_expression_p
4263 && pedantic)
4265 /* CAST_P will be set even in invalid code like "int(2.7 +
4266 ...)". Therefore, we have to check that the next token
4267 is sure to end the cast. */
4268 if (cast_p)
4270 cp_token *next_token;
4272 next_token = cp_lexer_peek_token (parser->lexer);
4273 if (/* The comma at the end of an
4274 enumerator-definition. */
4275 next_token->type != CPP_COMMA
4276 /* The curly brace at the end of an enum-specifier. */
4277 && next_token->type != CPP_CLOSE_BRACE
4278 /* The end of a statement. */
4279 && next_token->type != CPP_SEMICOLON
4280 /* The end of the cast-expression. */
4281 && next_token->type != CPP_CLOSE_PAREN
4282 /* The end of an array bound. */
4283 && next_token->type != CPP_CLOSE_SQUARE
4284 /* The closing ">" in a template-argument-list. */
4285 && (next_token->type != CPP_GREATER
4286 || parser->greater_than_is_operator_p)
4287 /* C++0x only: A ">>" treated like two ">" tokens,
4288 in a template-argument-list. */
4289 && (next_token->type != CPP_RSHIFT
4290 || (cxx_dialect == cxx98)
4291 || parser->greater_than_is_operator_p))
4292 cast_p = false;
4295 /* If we are within a cast, then the constraint that the
4296 cast is to an integral or enumeration type will be
4297 checked at that point. If we are not within a cast, then
4298 this code is invalid. */
4299 if (!cast_p)
4300 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4302 return token->u.value;
4304 case CPP_CHAR_USERDEF:
4305 case CPP_CHAR16_USERDEF:
4306 case CPP_CHAR32_USERDEF:
4307 case CPP_WCHAR_USERDEF:
4308 return cp_parser_userdef_char_literal (parser);
4310 case CPP_STRING:
4311 case CPP_STRING16:
4312 case CPP_STRING32:
4313 case CPP_WSTRING:
4314 case CPP_UTF8STRING:
4315 case CPP_STRING_USERDEF:
4316 case CPP_STRING16_USERDEF:
4317 case CPP_STRING32_USERDEF:
4318 case CPP_WSTRING_USERDEF:
4319 case CPP_UTF8STRING_USERDEF:
4320 /* ??? Should wide strings be allowed when parser->translate_strings_p
4321 is false (i.e. in attributes)? If not, we can kill the third
4322 argument to cp_parser_string_literal. */
4323 return cp_parser_string_literal (parser,
4324 parser->translate_strings_p,
4325 true);
4327 case CPP_OPEN_PAREN:
4329 tree expr;
4330 bool saved_greater_than_is_operator_p;
4332 /* Consume the `('. */
4333 cp_lexer_consume_token (parser->lexer);
4334 /* Within a parenthesized expression, a `>' token is always
4335 the greater-than operator. */
4336 saved_greater_than_is_operator_p
4337 = parser->greater_than_is_operator_p;
4338 parser->greater_than_is_operator_p = true;
4339 /* If we see `( { ' then we are looking at the beginning of
4340 a GNU statement-expression. */
4341 if (cp_parser_allow_gnu_extensions_p (parser)
4342 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4344 /* Statement-expressions are not allowed by the standard. */
4345 pedwarn (token->location, OPT_Wpedantic,
4346 "ISO C++ forbids braced-groups within expressions");
4348 /* And they're not allowed outside of a function-body; you
4349 cannot, for example, write:
4351 int i = ({ int j = 3; j + 1; });
4353 at class or namespace scope. */
4354 if (!parser->in_function_body
4355 || parser->in_template_argument_list_p)
4357 error_at (token->location,
4358 "statement-expressions are not allowed outside "
4359 "functions nor in template-argument lists");
4360 cp_parser_skip_to_end_of_block_or_statement (parser);
4361 expr = error_mark_node;
4363 else
4365 /* Start the statement-expression. */
4366 expr = begin_stmt_expr ();
4367 /* Parse the compound-statement. */
4368 cp_parser_compound_statement (parser, expr, false, false);
4369 /* Finish up. */
4370 expr = finish_stmt_expr (expr, false);
4373 else
4375 /* Parse the parenthesized expression. */
4376 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4377 /* Let the front end know that this expression was
4378 enclosed in parentheses. This matters in case, for
4379 example, the expression is of the form `A::B', since
4380 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4381 not. */
4382 expr = finish_parenthesized_expr (expr);
4383 /* DR 705: Wrapping an unqualified name in parentheses
4384 suppresses arg-dependent lookup. We want to pass back
4385 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4386 (c++/37862), but none of the others. */
4387 if (*idk != CP_ID_KIND_QUALIFIED)
4388 *idk = CP_ID_KIND_NONE;
4390 /* The `>' token might be the end of a template-id or
4391 template-parameter-list now. */
4392 parser->greater_than_is_operator_p
4393 = saved_greater_than_is_operator_p;
4394 /* Consume the `)'. */
4395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4396 cp_parser_skip_to_end_of_statement (parser);
4398 return expr;
4401 case CPP_OPEN_SQUARE:
4402 if (c_dialect_objc ())
4403 /* We have an Objective-C++ message. */
4404 return cp_parser_objc_expression (parser);
4406 tree lam = cp_parser_lambda_expression (parser);
4407 /* Don't warn about a failed tentative parse. */
4408 if (cp_parser_error_occurred (parser))
4409 return error_mark_node;
4410 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4411 return lam;
4414 case CPP_OBJC_STRING:
4415 if (c_dialect_objc ())
4416 /* We have an Objective-C++ string literal. */
4417 return cp_parser_objc_expression (parser);
4418 cp_parser_error (parser, "expected primary-expression");
4419 return error_mark_node;
4421 case CPP_KEYWORD:
4422 switch (token->keyword)
4424 /* These two are the boolean literals. */
4425 case RID_TRUE:
4426 cp_lexer_consume_token (parser->lexer);
4427 return boolean_true_node;
4428 case RID_FALSE:
4429 cp_lexer_consume_token (parser->lexer);
4430 return boolean_false_node;
4432 /* The `__null' literal. */
4433 case RID_NULL:
4434 cp_lexer_consume_token (parser->lexer);
4435 return null_node;
4437 /* The `nullptr' literal. */
4438 case RID_NULLPTR:
4439 cp_lexer_consume_token (parser->lexer);
4440 return nullptr_node;
4442 /* Recognize the `this' keyword. */
4443 case RID_THIS:
4444 cp_lexer_consume_token (parser->lexer);
4445 if (parser->local_variables_forbidden_p)
4447 error_at (token->location,
4448 "%<this%> may not be used in this context");
4449 return error_mark_node;
4451 /* Pointers cannot appear in constant-expressions. */
4452 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4453 return error_mark_node;
4454 return finish_this_expr ();
4456 /* The `operator' keyword can be the beginning of an
4457 id-expression. */
4458 case RID_OPERATOR:
4459 goto id_expression;
4461 case RID_FUNCTION_NAME:
4462 case RID_PRETTY_FUNCTION_NAME:
4463 case RID_C99_FUNCTION_NAME:
4465 non_integral_constant name;
4467 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4468 __func__ are the names of variables -- but they are
4469 treated specially. Therefore, they are handled here,
4470 rather than relying on the generic id-expression logic
4471 below. Grammatically, these names are id-expressions.
4473 Consume the token. */
4474 token = cp_lexer_consume_token (parser->lexer);
4476 switch (token->keyword)
4478 case RID_FUNCTION_NAME:
4479 name = NIC_FUNC_NAME;
4480 break;
4481 case RID_PRETTY_FUNCTION_NAME:
4482 name = NIC_PRETTY_FUNC;
4483 break;
4484 case RID_C99_FUNCTION_NAME:
4485 name = NIC_C99_FUNC;
4486 break;
4487 default:
4488 gcc_unreachable ();
4491 if (cp_parser_non_integral_constant_expression (parser, name))
4492 return error_mark_node;
4494 /* Look up the name. */
4495 return finish_fname (token->u.value);
4498 case RID_VA_ARG:
4500 tree expression;
4501 tree type;
4502 source_location type_location;
4504 /* The `__builtin_va_arg' construct is used to handle
4505 `va_arg'. Consume the `__builtin_va_arg' token. */
4506 cp_lexer_consume_token (parser->lexer);
4507 /* Look for the opening `('. */
4508 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4509 /* Now, parse the assignment-expression. */
4510 expression = cp_parser_assignment_expression (parser,
4511 /*cast_p=*/false, NULL);
4512 /* Look for the `,'. */
4513 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4514 type_location = cp_lexer_peek_token (parser->lexer)->location;
4515 /* Parse the type-id. */
4516 type = cp_parser_type_id (parser);
4517 /* Look for the closing `)'. */
4518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4519 /* Using `va_arg' in a constant-expression is not
4520 allowed. */
4521 if (cp_parser_non_integral_constant_expression (parser,
4522 NIC_VA_ARG))
4523 return error_mark_node;
4524 return build_x_va_arg (type_location, expression, type);
4527 case RID_OFFSETOF:
4528 return cp_parser_builtin_offsetof (parser);
4530 case RID_HAS_NOTHROW_ASSIGN:
4531 case RID_HAS_NOTHROW_CONSTRUCTOR:
4532 case RID_HAS_NOTHROW_COPY:
4533 case RID_HAS_TRIVIAL_ASSIGN:
4534 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4535 case RID_HAS_TRIVIAL_COPY:
4536 case RID_HAS_TRIVIAL_DESTRUCTOR:
4537 case RID_HAS_VIRTUAL_DESTRUCTOR:
4538 case RID_IS_ABSTRACT:
4539 case RID_IS_BASE_OF:
4540 case RID_IS_CLASS:
4541 case RID_IS_CONVERTIBLE_TO:
4542 case RID_IS_EMPTY:
4543 case RID_IS_ENUM:
4544 case RID_IS_FINAL:
4545 case RID_IS_LITERAL_TYPE:
4546 case RID_IS_POD:
4547 case RID_IS_POLYMORPHIC:
4548 case RID_IS_SAME_AS:
4549 case RID_IS_STD_LAYOUT:
4550 case RID_IS_TRIVIAL:
4551 case RID_IS_UNION:
4552 return cp_parser_trait_expr (parser, token->keyword);
4554 // C++ concepts
4555 case RID_REQUIRES:
4556 return cp_parser_requires_expression (parser);
4558 /* Objective-C++ expressions. */
4559 case RID_AT_ENCODE:
4560 case RID_AT_PROTOCOL:
4561 case RID_AT_SELECTOR:
4562 return cp_parser_objc_expression (parser);
4564 case RID_TEMPLATE:
4565 if (parser->in_function_body
4566 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4567 == CPP_LESS))
4569 error_at (token->location,
4570 "a template declaration cannot appear at block scope");
4571 cp_parser_skip_to_end_of_block_or_statement (parser);
4572 return error_mark_node;
4574 default:
4575 cp_parser_error (parser, "expected primary-expression");
4576 return error_mark_node;
4579 /* An id-expression can start with either an identifier, a
4580 `::' as the beginning of a qualified-id, or the "operator"
4581 keyword. */
4582 case CPP_NAME:
4583 case CPP_SCOPE:
4584 case CPP_TEMPLATE_ID:
4585 case CPP_NESTED_NAME_SPECIFIER:
4587 tree id_expression;
4588 tree decl;
4589 const char *error_msg;
4590 bool template_p;
4591 bool done;
4592 cp_token *id_expr_token;
4594 id_expression:
4595 /* Parse the id-expression. */
4596 id_expression
4597 = cp_parser_id_expression (parser,
4598 /*template_keyword_p=*/false,
4599 /*check_dependency_p=*/true,
4600 &template_p,
4601 /*declarator_p=*/false,
4602 /*optional_p=*/false);
4603 if (id_expression == error_mark_node)
4604 return error_mark_node;
4605 id_expr_token = token;
4606 token = cp_lexer_peek_token (parser->lexer);
4607 done = (token->type != CPP_OPEN_SQUARE
4608 && token->type != CPP_OPEN_PAREN
4609 && token->type != CPP_DOT
4610 && token->type != CPP_DEREF
4611 && token->type != CPP_PLUS_PLUS
4612 && token->type != CPP_MINUS_MINUS);
4613 /* If we have a template-id, then no further lookup is
4614 required. If the template-id was for a template-class, we
4615 will sometimes have a TYPE_DECL at this point. */
4616 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4617 || TREE_CODE (id_expression) == TYPE_DECL)
4618 decl = id_expression;
4619 /* Look up the name. */
4620 else
4622 tree ambiguous_decls;
4624 /* If we already know that this lookup is ambiguous, then
4625 we've already issued an error message; there's no reason
4626 to check again. */
4627 if (id_expr_token->type == CPP_NAME
4628 && id_expr_token->error_reported)
4630 cp_parser_simulate_error (parser);
4631 return error_mark_node;
4634 decl = cp_parser_lookup_name (parser, id_expression,
4635 none_type,
4636 template_p,
4637 /*is_namespace=*/false,
4638 /*check_dependency=*/true,
4639 &ambiguous_decls,
4640 id_expr_token->location);
4641 /* If the lookup was ambiguous, an error will already have
4642 been issued. */
4643 if (ambiguous_decls)
4644 return error_mark_node;
4646 /* In Objective-C++, we may have an Objective-C 2.0
4647 dot-syntax for classes here. */
4648 if (c_dialect_objc ()
4649 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4650 && TREE_CODE (decl) == TYPE_DECL
4651 && objc_is_class_name (decl))
4653 tree component;
4654 cp_lexer_consume_token (parser->lexer);
4655 component = cp_parser_identifier (parser);
4656 if (component == error_mark_node)
4657 return error_mark_node;
4659 return objc_build_class_component_ref (id_expression, component);
4662 /* In Objective-C++, an instance variable (ivar) may be preferred
4663 to whatever cp_parser_lookup_name() found. */
4664 decl = objc_lookup_ivar (decl, id_expression);
4666 /* If name lookup gives us a SCOPE_REF, then the
4667 qualifying scope was dependent. */
4668 if (TREE_CODE (decl) == SCOPE_REF)
4670 /* At this point, we do not know if DECL is a valid
4671 integral constant expression. We assume that it is
4672 in fact such an expression, so that code like:
4674 template <int N> struct A {
4675 int a[B<N>::i];
4678 is accepted. At template-instantiation time, we
4679 will check that B<N>::i is actually a constant. */
4680 return decl;
4682 /* Check to see if DECL is a local variable in a context
4683 where that is forbidden. */
4684 if (parser->local_variables_forbidden_p
4685 && local_variable_p (decl))
4687 /* It might be that we only found DECL because we are
4688 trying to be generous with pre-ISO scoping rules.
4689 For example, consider:
4691 int i;
4692 void g() {
4693 for (int i = 0; i < 10; ++i) {}
4694 extern void f(int j = i);
4697 Here, name look up will originally find the out
4698 of scope `i'. We need to issue a warning message,
4699 but then use the global `i'. */
4700 decl = check_for_out_of_scope_variable (decl);
4701 if (local_variable_p (decl))
4703 error_at (id_expr_token->location,
4704 "local variable %qD may not appear in this context",
4705 decl);
4706 return error_mark_node;
4711 decl = (finish_id_expression
4712 (id_expression, decl, parser->scope,
4713 idk,
4714 parser->integral_constant_expression_p,
4715 parser->allow_non_integral_constant_expression_p,
4716 &parser->non_integral_constant_expression_p,
4717 template_p, done, address_p,
4718 template_arg_p,
4719 &error_msg,
4720 id_expr_token->location));
4721 if (error_msg)
4722 cp_parser_error (parser, error_msg);
4723 return decl;
4726 /* Anything else is an error. */
4727 default:
4728 cp_parser_error (parser, "expected primary-expression");
4729 return error_mark_node;
4733 static inline tree
4734 cp_parser_primary_expression (cp_parser *parser,
4735 bool address_p,
4736 bool cast_p,
4737 bool template_arg_p,
4738 cp_id_kind *idk)
4740 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4741 /*decltype*/false, idk);
4744 /* Parse an id-expression.
4746 id-expression:
4747 unqualified-id
4748 qualified-id
4750 qualified-id:
4751 :: [opt] nested-name-specifier template [opt] unqualified-id
4752 :: identifier
4753 :: operator-function-id
4754 :: template-id
4756 Return a representation of the unqualified portion of the
4757 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4758 a `::' or nested-name-specifier.
4760 Often, if the id-expression was a qualified-id, the caller will
4761 want to make a SCOPE_REF to represent the qualified-id. This
4762 function does not do this in order to avoid wastefully creating
4763 SCOPE_REFs when they are not required.
4765 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4766 `template' keyword.
4768 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4769 uninstantiated templates.
4771 If *TEMPLATE_P is non-NULL, it is set to true iff the
4772 `template' keyword is used to explicitly indicate that the entity
4773 named is a template.
4775 If DECLARATOR_P is true, the id-expression is appearing as part of
4776 a declarator, rather than as part of an expression. */
4778 static tree
4779 cp_parser_id_expression (cp_parser *parser,
4780 bool template_keyword_p,
4781 bool check_dependency_p,
4782 bool *template_p,
4783 bool declarator_p,
4784 bool optional_p)
4786 bool global_scope_p;
4787 bool nested_name_specifier_p;
4789 /* Assume the `template' keyword was not used. */
4790 if (template_p)
4791 *template_p = template_keyword_p;
4793 /* Look for the optional `::' operator. */
4794 global_scope_p
4795 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4796 != NULL_TREE);
4797 /* Look for the optional nested-name-specifier. */
4798 nested_name_specifier_p
4799 = (cp_parser_nested_name_specifier_opt (parser,
4800 /*typename_keyword_p=*/false,
4801 check_dependency_p,
4802 /*type_p=*/false,
4803 declarator_p)
4804 != NULL_TREE);
4805 /* If there is a nested-name-specifier, then we are looking at
4806 the first qualified-id production. */
4807 if (nested_name_specifier_p)
4809 tree saved_scope;
4810 tree saved_object_scope;
4811 tree saved_qualifying_scope;
4812 tree unqualified_id;
4813 bool is_template;
4815 /* See if the next token is the `template' keyword. */
4816 if (!template_p)
4817 template_p = &is_template;
4818 *template_p = cp_parser_optional_template_keyword (parser);
4819 /* Name lookup we do during the processing of the
4820 unqualified-id might obliterate SCOPE. */
4821 saved_scope = parser->scope;
4822 saved_object_scope = parser->object_scope;
4823 saved_qualifying_scope = parser->qualifying_scope;
4824 /* Process the final unqualified-id. */
4825 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4826 check_dependency_p,
4827 declarator_p,
4828 /*optional_p=*/false);
4829 /* Restore the SAVED_SCOPE for our caller. */
4830 parser->scope = saved_scope;
4831 parser->object_scope = saved_object_scope;
4832 parser->qualifying_scope = saved_qualifying_scope;
4834 return unqualified_id;
4836 /* Otherwise, if we are in global scope, then we are looking at one
4837 of the other qualified-id productions. */
4838 else if (global_scope_p)
4840 cp_token *token;
4841 tree id;
4843 /* Peek at the next token. */
4844 token = cp_lexer_peek_token (parser->lexer);
4846 /* If it's an identifier, and the next token is not a "<", then
4847 we can avoid the template-id case. This is an optimization
4848 for this common case. */
4849 if (token->type == CPP_NAME
4850 && !cp_parser_nth_token_starts_template_argument_list_p
4851 (parser, 2))
4852 return cp_parser_identifier (parser);
4854 cp_parser_parse_tentatively (parser);
4855 /* Try a template-id. */
4856 id = cp_parser_template_id (parser,
4857 /*template_keyword_p=*/false,
4858 /*check_dependency_p=*/true,
4859 none_type,
4860 declarator_p);
4861 /* If that worked, we're done. */
4862 if (cp_parser_parse_definitely (parser))
4863 return id;
4865 /* Peek at the next token. (Changes in the token buffer may
4866 have invalidated the pointer obtained above.) */
4867 token = cp_lexer_peek_token (parser->lexer);
4869 switch (token->type)
4871 case CPP_NAME:
4872 return cp_parser_identifier (parser);
4874 case CPP_KEYWORD:
4875 if (token->keyword == RID_OPERATOR)
4876 return cp_parser_operator_function_id (parser);
4877 /* Fall through. */
4879 default:
4880 cp_parser_error (parser, "expected id-expression");
4881 return error_mark_node;
4884 else
4885 return cp_parser_unqualified_id (parser, template_keyword_p,
4886 /*check_dependency_p=*/true,
4887 declarator_p,
4888 optional_p);
4891 /* Parse an unqualified-id.
4893 unqualified-id:
4894 identifier
4895 operator-function-id
4896 conversion-function-id
4897 ~ class-name
4898 template-id
4900 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4901 keyword, in a construct like `A::template ...'.
4903 Returns a representation of unqualified-id. For the `identifier'
4904 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4905 production a BIT_NOT_EXPR is returned; the operand of the
4906 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4907 other productions, see the documentation accompanying the
4908 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4909 names are looked up in uninstantiated templates. If DECLARATOR_P
4910 is true, the unqualified-id is appearing as part of a declarator,
4911 rather than as part of an expression. */
4913 static tree
4914 cp_parser_unqualified_id (cp_parser* parser,
4915 bool template_keyword_p,
4916 bool check_dependency_p,
4917 bool declarator_p,
4918 bool optional_p)
4920 cp_token *token;
4922 /* Peek at the next token. */
4923 token = cp_lexer_peek_token (parser->lexer);
4925 switch (token->type)
4927 case CPP_NAME:
4929 tree id;
4931 /* We don't know yet whether or not this will be a
4932 template-id. */
4933 cp_parser_parse_tentatively (parser);
4934 /* Try a template-id. */
4935 id = cp_parser_template_id (parser, template_keyword_p,
4936 check_dependency_p,
4937 none_type,
4938 declarator_p);
4939 /* If it worked, we're done. */
4940 if (cp_parser_parse_definitely (parser))
4941 return id;
4942 /* Otherwise, it's an ordinary identifier. */
4943 return cp_parser_identifier (parser);
4946 case CPP_TEMPLATE_ID:
4947 return cp_parser_template_id (parser, template_keyword_p,
4948 check_dependency_p,
4949 none_type,
4950 declarator_p);
4952 case CPP_COMPL:
4954 tree type_decl;
4955 tree qualifying_scope;
4956 tree object_scope;
4957 tree scope;
4958 bool done;
4960 /* Consume the `~' token. */
4961 cp_lexer_consume_token (parser->lexer);
4962 /* Parse the class-name. The standard, as written, seems to
4963 say that:
4965 template <typename T> struct S { ~S (); };
4966 template <typename T> S<T>::~S() {}
4968 is invalid, since `~' must be followed by a class-name, but
4969 `S<T>' is dependent, and so not known to be a class.
4970 That's not right; we need to look in uninstantiated
4971 templates. A further complication arises from:
4973 template <typename T> void f(T t) {
4974 t.T::~T();
4977 Here, it is not possible to look up `T' in the scope of `T'
4978 itself. We must look in both the current scope, and the
4979 scope of the containing complete expression.
4981 Yet another issue is:
4983 struct S {
4984 int S;
4985 ~S();
4988 S::~S() {}
4990 The standard does not seem to say that the `S' in `~S'
4991 should refer to the type `S' and not the data member
4992 `S::S'. */
4994 /* DR 244 says that we look up the name after the "~" in the
4995 same scope as we looked up the qualifying name. That idea
4996 isn't fully worked out; it's more complicated than that. */
4997 scope = parser->scope;
4998 object_scope = parser->object_scope;
4999 qualifying_scope = parser->qualifying_scope;
5001 /* Check for invalid scopes. */
5002 if (scope == error_mark_node)
5004 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5005 cp_lexer_consume_token (parser->lexer);
5006 return error_mark_node;
5008 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5010 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5011 error_at (token->location,
5012 "scope %qT before %<~%> is not a class-name",
5013 scope);
5014 cp_parser_simulate_error (parser);
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5016 cp_lexer_consume_token (parser->lexer);
5017 return error_mark_node;
5019 gcc_assert (!scope || TYPE_P (scope));
5021 /* If the name is of the form "X::~X" it's OK even if X is a
5022 typedef. */
5023 token = cp_lexer_peek_token (parser->lexer);
5024 if (scope
5025 && token->type == CPP_NAME
5026 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5027 != CPP_LESS)
5028 && (token->u.value == TYPE_IDENTIFIER (scope)
5029 || (CLASS_TYPE_P (scope)
5030 && constructor_name_p (token->u.value, scope))))
5032 cp_lexer_consume_token (parser->lexer);
5033 return build_nt (BIT_NOT_EXPR, scope);
5036 /* ~auto means the destructor of whatever the object is. */
5037 if (cp_parser_is_keyword (token, RID_AUTO))
5039 if (cxx_dialect < cxx1y)
5040 pedwarn (input_location, 0,
5041 "%<~auto%> only available with "
5042 "-std=c++1y or -std=gnu++1y");
5043 cp_lexer_consume_token (parser->lexer);
5044 return build_nt (BIT_NOT_EXPR, make_auto ());
5047 /* If there was an explicit qualification (S::~T), first look
5048 in the scope given by the qualification (i.e., S).
5050 Note: in the calls to cp_parser_class_name below we pass
5051 typename_type so that lookup finds the injected-class-name
5052 rather than the constructor. */
5053 done = false;
5054 type_decl = NULL_TREE;
5055 if (scope)
5057 cp_parser_parse_tentatively (parser);
5058 type_decl = cp_parser_class_name (parser,
5059 /*typename_keyword_p=*/false,
5060 /*template_keyword_p=*/false,
5061 typename_type,
5062 /*check_dependency=*/false,
5063 /*class_head_p=*/false,
5064 declarator_p);
5065 if (cp_parser_parse_definitely (parser))
5066 done = true;
5068 /* In "N::S::~S", look in "N" as well. */
5069 if (!done && scope && qualifying_scope)
5071 cp_parser_parse_tentatively (parser);
5072 parser->scope = qualifying_scope;
5073 parser->object_scope = NULL_TREE;
5074 parser->qualifying_scope = NULL_TREE;
5075 type_decl
5076 = cp_parser_class_name (parser,
5077 /*typename_keyword_p=*/false,
5078 /*template_keyword_p=*/false,
5079 typename_type,
5080 /*check_dependency=*/false,
5081 /*class_head_p=*/false,
5082 declarator_p);
5083 if (cp_parser_parse_definitely (parser))
5084 done = true;
5086 /* In "p->S::~T", look in the scope given by "*p" as well. */
5087 else if (!done && object_scope)
5089 cp_parser_parse_tentatively (parser);
5090 parser->scope = object_scope;
5091 parser->object_scope = NULL_TREE;
5092 parser->qualifying_scope = NULL_TREE;
5093 type_decl
5094 = cp_parser_class_name (parser,
5095 /*typename_keyword_p=*/false,
5096 /*template_keyword_p=*/false,
5097 typename_type,
5098 /*check_dependency=*/false,
5099 /*class_head_p=*/false,
5100 declarator_p);
5101 if (cp_parser_parse_definitely (parser))
5102 done = true;
5104 /* Look in the surrounding context. */
5105 if (!done)
5107 parser->scope = NULL_TREE;
5108 parser->object_scope = NULL_TREE;
5109 parser->qualifying_scope = NULL_TREE;
5110 if (processing_template_decl)
5111 cp_parser_parse_tentatively (parser);
5112 type_decl
5113 = cp_parser_class_name (parser,
5114 /*typename_keyword_p=*/false,
5115 /*template_keyword_p=*/false,
5116 typename_type,
5117 /*check_dependency=*/false,
5118 /*class_head_p=*/false,
5119 declarator_p);
5120 if (processing_template_decl
5121 && ! cp_parser_parse_definitely (parser))
5123 /* We couldn't find a type with this name, so just accept
5124 it and check for a match at instantiation time. */
5125 type_decl = cp_parser_identifier (parser);
5126 if (type_decl != error_mark_node)
5127 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5128 return type_decl;
5131 /* If an error occurred, assume that the name of the
5132 destructor is the same as the name of the qualifying
5133 class. That allows us to keep parsing after running
5134 into ill-formed destructor names. */
5135 if (type_decl == error_mark_node && scope)
5136 return build_nt (BIT_NOT_EXPR, scope);
5137 else if (type_decl == error_mark_node)
5138 return error_mark_node;
5140 /* Check that destructor name and scope match. */
5141 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5144 error_at (token->location,
5145 "declaration of %<~%T%> as member of %qT",
5146 type_decl, scope);
5147 cp_parser_simulate_error (parser);
5148 return error_mark_node;
5151 /* [class.dtor]
5153 A typedef-name that names a class shall not be used as the
5154 identifier in the declarator for a destructor declaration. */
5155 if (declarator_p
5156 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5157 && !DECL_SELF_REFERENCE_P (type_decl)
5158 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5159 error_at (token->location,
5160 "typedef-name %qD used as destructor declarator",
5161 type_decl);
5163 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5166 case CPP_KEYWORD:
5167 if (token->keyword == RID_OPERATOR)
5169 tree id;
5171 /* This could be a template-id, so we try that first. */
5172 cp_parser_parse_tentatively (parser);
5173 /* Try a template-id. */
5174 id = cp_parser_template_id (parser, template_keyword_p,
5175 /*check_dependency_p=*/true,
5176 none_type,
5177 declarator_p);
5178 /* If that worked, we're done. */
5179 if (cp_parser_parse_definitely (parser))
5180 return id;
5181 /* We still don't know whether we're looking at an
5182 operator-function-id or a conversion-function-id. */
5183 cp_parser_parse_tentatively (parser);
5184 /* Try an operator-function-id. */
5185 id = cp_parser_operator_function_id (parser);
5186 /* If that didn't work, try a conversion-function-id. */
5187 if (!cp_parser_parse_definitely (parser))
5188 id = cp_parser_conversion_function_id (parser);
5189 else if (UDLIT_OPER_P (id))
5191 /* 17.6.3.3.5 */
5192 const char *name = UDLIT_OP_SUFFIX (id);
5193 if (name[0] != '_' && !in_system_header_at (input_location)
5194 && declarator_p)
5195 warning (0, "literal operator suffixes not preceded by %<_%>"
5196 " are reserved for future standardization");
5199 return id;
5201 /* Fall through. */
5203 default:
5204 if (optional_p)
5205 return NULL_TREE;
5206 cp_parser_error (parser, "expected unqualified-id");
5207 return error_mark_node;
5211 /* Parse an (optional) nested-name-specifier.
5213 nested-name-specifier: [C++98]
5214 class-or-namespace-name :: nested-name-specifier [opt]
5215 class-or-namespace-name :: template nested-name-specifier [opt]
5217 nested-name-specifier: [C++0x]
5218 type-name ::
5219 namespace-name ::
5220 nested-name-specifier identifier ::
5221 nested-name-specifier template [opt] simple-template-id ::
5223 PARSER->SCOPE should be set appropriately before this function is
5224 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5225 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5226 in name lookups.
5228 Sets PARSER->SCOPE to the class (TYPE) or namespace
5229 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5230 it unchanged if there is no nested-name-specifier. Returns the new
5231 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5233 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5234 part of a declaration and/or decl-specifier. */
5236 static tree
5237 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5238 bool typename_keyword_p,
5239 bool check_dependency_p,
5240 bool type_p,
5241 bool is_declaration)
5243 bool success = false;
5244 cp_token_position start = 0;
5245 cp_token *token;
5247 /* Remember where the nested-name-specifier starts. */
5248 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5250 start = cp_lexer_token_position (parser->lexer, false);
5251 push_deferring_access_checks (dk_deferred);
5254 while (true)
5256 tree new_scope;
5257 tree old_scope;
5258 tree saved_qualifying_scope;
5259 bool template_keyword_p;
5261 /* Spot cases that cannot be the beginning of a
5262 nested-name-specifier. */
5263 token = cp_lexer_peek_token (parser->lexer);
5265 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5266 the already parsed nested-name-specifier. */
5267 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5269 /* Grab the nested-name-specifier and continue the loop. */
5270 cp_parser_pre_parsed_nested_name_specifier (parser);
5271 /* If we originally encountered this nested-name-specifier
5272 with IS_DECLARATION set to false, we will not have
5273 resolved TYPENAME_TYPEs, so we must do so here. */
5274 if (is_declaration
5275 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5277 new_scope = resolve_typename_type (parser->scope,
5278 /*only_current_p=*/false);
5279 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5280 parser->scope = new_scope;
5282 success = true;
5283 continue;
5286 /* Spot cases that cannot be the beginning of a
5287 nested-name-specifier. On the second and subsequent times
5288 through the loop, we look for the `template' keyword. */
5289 if (success && token->keyword == RID_TEMPLATE)
5291 /* A template-id can start a nested-name-specifier. */
5292 else if (token->type == CPP_TEMPLATE_ID)
5294 /* DR 743: decltype can be used in a nested-name-specifier. */
5295 else if (token_is_decltype (token))
5297 else
5299 /* If the next token is not an identifier, then it is
5300 definitely not a type-name or namespace-name. */
5301 if (token->type != CPP_NAME)
5302 break;
5303 /* If the following token is neither a `<' (to begin a
5304 template-id), nor a `::', then we are not looking at a
5305 nested-name-specifier. */
5306 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5308 if (token->type == CPP_COLON
5309 && parser->colon_corrects_to_scope_p
5310 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5312 error_at (token->location,
5313 "found %<:%> in nested-name-specifier, expected %<::%>");
5314 token->type = CPP_SCOPE;
5317 if (token->type != CPP_SCOPE
5318 && !cp_parser_nth_token_starts_template_argument_list_p
5319 (parser, 2))
5320 break;
5323 /* The nested-name-specifier is optional, so we parse
5324 tentatively. */
5325 cp_parser_parse_tentatively (parser);
5327 /* Look for the optional `template' keyword, if this isn't the
5328 first time through the loop. */
5329 if (success)
5330 template_keyword_p = cp_parser_optional_template_keyword (parser);
5331 else
5332 template_keyword_p = false;
5334 /* Save the old scope since the name lookup we are about to do
5335 might destroy it. */
5336 old_scope = parser->scope;
5337 saved_qualifying_scope = parser->qualifying_scope;
5338 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5339 look up names in "X<T>::I" in order to determine that "Y" is
5340 a template. So, if we have a typename at this point, we make
5341 an effort to look through it. */
5342 if (is_declaration
5343 && !typename_keyword_p
5344 && parser->scope
5345 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5346 parser->scope = resolve_typename_type (parser->scope,
5347 /*only_current_p=*/false);
5348 /* Parse the qualifying entity. */
5349 new_scope
5350 = cp_parser_qualifying_entity (parser,
5351 typename_keyword_p,
5352 template_keyword_p,
5353 check_dependency_p,
5354 type_p,
5355 is_declaration);
5356 /* Look for the `::' token. */
5357 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5359 /* If we found what we wanted, we keep going; otherwise, we're
5360 done. */
5361 if (!cp_parser_parse_definitely (parser))
5363 bool error_p = false;
5365 /* Restore the OLD_SCOPE since it was valid before the
5366 failed attempt at finding the last
5367 class-or-namespace-name. */
5368 parser->scope = old_scope;
5369 parser->qualifying_scope = saved_qualifying_scope;
5371 /* If the next token is a decltype, and the one after that is a
5372 `::', then the decltype has failed to resolve to a class or
5373 enumeration type. Give this error even when parsing
5374 tentatively since it can't possibly be valid--and we're going
5375 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5376 won't get another chance.*/
5377 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5378 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5379 == CPP_SCOPE))
5381 token = cp_lexer_consume_token (parser->lexer);
5382 error_at (token->location, "decltype evaluates to %qT, "
5383 "which is not a class or enumeration type",
5384 token->u.value);
5385 parser->scope = error_mark_node;
5386 error_p = true;
5387 /* As below. */
5388 success = true;
5389 cp_lexer_consume_token (parser->lexer);
5392 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5393 break;
5394 /* If the next token is an identifier, and the one after
5395 that is a `::', then any valid interpretation would have
5396 found a class-or-namespace-name. */
5397 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5398 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5399 == CPP_SCOPE)
5400 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5401 != CPP_COMPL))
5403 token = cp_lexer_consume_token (parser->lexer);
5404 if (!error_p)
5406 if (!token->error_reported)
5408 tree decl;
5409 tree ambiguous_decls;
5411 decl = cp_parser_lookup_name (parser, token->u.value,
5412 none_type,
5413 /*is_template=*/false,
5414 /*is_namespace=*/false,
5415 /*check_dependency=*/true,
5416 &ambiguous_decls,
5417 token->location);
5418 if (TREE_CODE (decl) == TEMPLATE_DECL)
5419 error_at (token->location,
5420 "%qD used without template parameters",
5421 decl);
5422 else if (ambiguous_decls)
5424 // cp_parser_lookup_name has the same diagnostic,
5425 // thus make sure to emit it at most once.
5426 if (cp_parser_uncommitted_to_tentative_parse_p
5427 (parser))
5429 error_at (token->location,
5430 "reference to %qD is ambiguous",
5431 token->u.value);
5432 print_candidates (ambiguous_decls);
5434 decl = error_mark_node;
5436 else
5438 if (cxx_dialect != cxx98)
5439 cp_parser_name_lookup_error
5440 (parser, token->u.value, decl, NLE_NOT_CXX98,
5441 token->location);
5442 else
5443 cp_parser_name_lookup_error
5444 (parser, token->u.value, decl, NLE_CXX98,
5445 token->location);
5448 parser->scope = error_mark_node;
5449 error_p = true;
5450 /* Treat this as a successful nested-name-specifier
5451 due to:
5453 [basic.lookup.qual]
5455 If the name found is not a class-name (clause
5456 _class_) or namespace-name (_namespace.def_), the
5457 program is ill-formed. */
5458 success = true;
5460 cp_lexer_consume_token (parser->lexer);
5462 break;
5464 /* We've found one valid nested-name-specifier. */
5465 success = true;
5466 /* Name lookup always gives us a DECL. */
5467 if (TREE_CODE (new_scope) == TYPE_DECL)
5468 new_scope = TREE_TYPE (new_scope);
5469 /* Uses of "template" must be followed by actual templates. */
5470 if (template_keyword_p
5471 && !(CLASS_TYPE_P (new_scope)
5472 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5473 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5474 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5475 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5476 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5477 == TEMPLATE_ID_EXPR)))
5478 permerror (input_location, TYPE_P (new_scope)
5479 ? G_("%qT is not a template")
5480 : G_("%qD is not a template"),
5481 new_scope);
5482 /* If it is a class scope, try to complete it; we are about to
5483 be looking up names inside the class. */
5484 if (TYPE_P (new_scope)
5485 /* Since checking types for dependency can be expensive,
5486 avoid doing it if the type is already complete. */
5487 && !COMPLETE_TYPE_P (new_scope)
5488 /* Do not try to complete dependent types. */
5489 && !dependent_type_p (new_scope))
5491 new_scope = complete_type (new_scope);
5492 /* If it is a typedef to current class, use the current
5493 class instead, as the typedef won't have any names inside
5494 it yet. */
5495 if (!COMPLETE_TYPE_P (new_scope)
5496 && currently_open_class (new_scope))
5497 new_scope = TYPE_MAIN_VARIANT (new_scope);
5499 /* Make sure we look in the right scope the next time through
5500 the loop. */
5501 parser->scope = new_scope;
5504 /* If parsing tentatively, replace the sequence of tokens that makes
5505 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5506 token. That way, should we re-parse the token stream, we will
5507 not have to repeat the effort required to do the parse, nor will
5508 we issue duplicate error messages. */
5509 if (success && start)
5511 cp_token *token;
5513 token = cp_lexer_token_at (parser->lexer, start);
5514 /* Reset the contents of the START token. */
5515 token->type = CPP_NESTED_NAME_SPECIFIER;
5516 /* Retrieve any deferred checks. Do not pop this access checks yet
5517 so the memory will not be reclaimed during token replacing below. */
5518 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5519 token->u.tree_check_value->value = parser->scope;
5520 token->u.tree_check_value->checks = get_deferred_access_checks ();
5521 token->u.tree_check_value->qualifying_scope =
5522 parser->qualifying_scope;
5523 token->keyword = RID_MAX;
5525 /* Purge all subsequent tokens. */
5526 cp_lexer_purge_tokens_after (parser->lexer, start);
5529 if (start)
5530 pop_to_parent_deferring_access_checks ();
5532 return success ? parser->scope : NULL_TREE;
5535 /* Parse a nested-name-specifier. See
5536 cp_parser_nested_name_specifier_opt for details. This function
5537 behaves identically, except that it will an issue an error if no
5538 nested-name-specifier is present. */
5540 static tree
5541 cp_parser_nested_name_specifier (cp_parser *parser,
5542 bool typename_keyword_p,
5543 bool check_dependency_p,
5544 bool type_p,
5545 bool is_declaration)
5547 tree scope;
5549 /* Look for the nested-name-specifier. */
5550 scope = cp_parser_nested_name_specifier_opt (parser,
5551 typename_keyword_p,
5552 check_dependency_p,
5553 type_p,
5554 is_declaration);
5555 /* If it was not present, issue an error message. */
5556 if (!scope)
5558 cp_parser_error (parser, "expected nested-name-specifier");
5559 parser->scope = NULL_TREE;
5562 return scope;
5565 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5566 this is either a class-name or a namespace-name (which corresponds
5567 to the class-or-namespace-name production in the grammar). For
5568 C++0x, it can also be a type-name that refers to an enumeration
5569 type or a simple-template-id.
5571 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5572 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5573 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5574 TYPE_P is TRUE iff the next name should be taken as a class-name,
5575 even the same name is declared to be another entity in the same
5576 scope.
5578 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5579 specified by the class-or-namespace-name. If neither is found the
5580 ERROR_MARK_NODE is returned. */
5582 static tree
5583 cp_parser_qualifying_entity (cp_parser *parser,
5584 bool typename_keyword_p,
5585 bool template_keyword_p,
5586 bool check_dependency_p,
5587 bool type_p,
5588 bool is_declaration)
5590 tree saved_scope;
5591 tree saved_qualifying_scope;
5592 tree saved_object_scope;
5593 tree scope;
5594 bool only_class_p;
5595 bool successful_parse_p;
5597 /* DR 743: decltype can appear in a nested-name-specifier. */
5598 if (cp_lexer_next_token_is_decltype (parser->lexer))
5600 scope = cp_parser_decltype (parser);
5601 if (TREE_CODE (scope) != ENUMERAL_TYPE
5602 && !MAYBE_CLASS_TYPE_P (scope))
5604 cp_parser_simulate_error (parser);
5605 return error_mark_node;
5607 if (TYPE_NAME (scope))
5608 scope = TYPE_NAME (scope);
5609 return scope;
5612 /* Before we try to parse the class-name, we must save away the
5613 current PARSER->SCOPE since cp_parser_class_name will destroy
5614 it. */
5615 saved_scope = parser->scope;
5616 saved_qualifying_scope = parser->qualifying_scope;
5617 saved_object_scope = parser->object_scope;
5618 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5619 there is no need to look for a namespace-name. */
5620 only_class_p = template_keyword_p
5621 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5622 if (!only_class_p)
5623 cp_parser_parse_tentatively (parser);
5624 scope = cp_parser_class_name (parser,
5625 typename_keyword_p,
5626 template_keyword_p,
5627 type_p ? class_type : none_type,
5628 check_dependency_p,
5629 /*class_head_p=*/false,
5630 is_declaration);
5631 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5632 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5633 if (!only_class_p
5634 && cxx_dialect != cxx98
5635 && !successful_parse_p)
5637 /* Restore the saved scope. */
5638 parser->scope = saved_scope;
5639 parser->qualifying_scope = saved_qualifying_scope;
5640 parser->object_scope = saved_object_scope;
5642 /* Parse tentatively. */
5643 cp_parser_parse_tentatively (parser);
5645 /* Parse a type-name */
5646 scope = cp_parser_type_name (parser);
5648 /* "If the name found does not designate a namespace or a class,
5649 enumeration, or dependent type, the program is ill-formed."
5651 We cover classes and dependent types above and namespaces below,
5652 so this code is only looking for enums. */
5653 if (!scope || TREE_CODE (scope) != TYPE_DECL
5654 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5655 cp_parser_simulate_error (parser);
5657 successful_parse_p = cp_parser_parse_definitely (parser);
5659 /* If that didn't work, try for a namespace-name. */
5660 if (!only_class_p && !successful_parse_p)
5662 /* Restore the saved scope. */
5663 parser->scope = saved_scope;
5664 parser->qualifying_scope = saved_qualifying_scope;
5665 parser->object_scope = saved_object_scope;
5666 /* If we are not looking at an identifier followed by the scope
5667 resolution operator, then this is not part of a
5668 nested-name-specifier. (Note that this function is only used
5669 to parse the components of a nested-name-specifier.) */
5670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5671 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5672 return error_mark_node;
5673 scope = cp_parser_namespace_name (parser);
5676 return scope;
5679 /* Parse a postfix-expression.
5681 postfix-expression:
5682 primary-expression
5683 postfix-expression [ expression ]
5684 postfix-expression ( expression-list [opt] )
5685 simple-type-specifier ( expression-list [opt] )
5686 typename :: [opt] nested-name-specifier identifier
5687 ( expression-list [opt] )
5688 typename :: [opt] nested-name-specifier template [opt] template-id
5689 ( expression-list [opt] )
5690 postfix-expression . template [opt] id-expression
5691 postfix-expression -> template [opt] id-expression
5692 postfix-expression . pseudo-destructor-name
5693 postfix-expression -> pseudo-destructor-name
5694 postfix-expression ++
5695 postfix-expression --
5696 dynamic_cast < type-id > ( expression )
5697 static_cast < type-id > ( expression )
5698 reinterpret_cast < type-id > ( expression )
5699 const_cast < type-id > ( expression )
5700 typeid ( expression )
5701 typeid ( type-id )
5703 GNU Extension:
5705 postfix-expression:
5706 ( type-id ) { initializer-list , [opt] }
5708 This extension is a GNU version of the C99 compound-literal
5709 construct. (The C99 grammar uses `type-name' instead of `type-id',
5710 but they are essentially the same concept.)
5712 If ADDRESS_P is true, the postfix expression is the operand of the
5713 `&' operator. CAST_P is true if this expression is the target of a
5714 cast.
5716 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5717 class member access expressions [expr.ref].
5719 Returns a representation of the expression. */
5721 static tree
5722 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5723 bool member_access_only_p, bool decltype_p,
5724 cp_id_kind * pidk_return)
5726 cp_token *token;
5727 location_t loc;
5728 enum rid keyword;
5729 cp_id_kind idk = CP_ID_KIND_NONE;
5730 tree postfix_expression = NULL_TREE;
5731 bool is_member_access = false;
5732 int saved_in_statement = -1;
5734 /* Peek at the next token. */
5735 token = cp_lexer_peek_token (parser->lexer);
5736 loc = token->location;
5737 /* Some of the productions are determined by keywords. */
5738 keyword = token->keyword;
5739 switch (keyword)
5741 case RID_DYNCAST:
5742 case RID_STATCAST:
5743 case RID_REINTCAST:
5744 case RID_CONSTCAST:
5746 tree type;
5747 tree expression;
5748 const char *saved_message;
5749 bool saved_in_type_id_in_expr_p;
5751 /* All of these can be handled in the same way from the point
5752 of view of parsing. Begin by consuming the token
5753 identifying the cast. */
5754 cp_lexer_consume_token (parser->lexer);
5756 /* New types cannot be defined in the cast. */
5757 saved_message = parser->type_definition_forbidden_message;
5758 parser->type_definition_forbidden_message
5759 = G_("types may not be defined in casts");
5761 /* Look for the opening `<'. */
5762 cp_parser_require (parser, CPP_LESS, RT_LESS);
5763 /* Parse the type to which we are casting. */
5764 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5765 parser->in_type_id_in_expr_p = true;
5766 type = cp_parser_type_id (parser);
5767 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5768 /* Look for the closing `>'. */
5769 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5770 /* Restore the old message. */
5771 parser->type_definition_forbidden_message = saved_message;
5773 bool saved_greater_than_is_operator_p
5774 = parser->greater_than_is_operator_p;
5775 parser->greater_than_is_operator_p = true;
5777 /* And the expression which is being cast. */
5778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5779 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5780 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5782 parser->greater_than_is_operator_p
5783 = saved_greater_than_is_operator_p;
5785 /* Only type conversions to integral or enumeration types
5786 can be used in constant-expressions. */
5787 if (!cast_valid_in_integral_constant_expression_p (type)
5788 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5789 return error_mark_node;
5791 switch (keyword)
5793 case RID_DYNCAST:
5794 postfix_expression
5795 = build_dynamic_cast (type, expression, tf_warning_or_error);
5796 break;
5797 case RID_STATCAST:
5798 postfix_expression
5799 = build_static_cast (type, expression, tf_warning_or_error);
5800 break;
5801 case RID_REINTCAST:
5802 postfix_expression
5803 = build_reinterpret_cast (type, expression,
5804 tf_warning_or_error);
5805 break;
5806 case RID_CONSTCAST:
5807 postfix_expression
5808 = build_const_cast (type, expression, tf_warning_or_error);
5809 break;
5810 default:
5811 gcc_unreachable ();
5814 break;
5816 case RID_TYPEID:
5818 tree type;
5819 const char *saved_message;
5820 bool saved_in_type_id_in_expr_p;
5822 /* Consume the `typeid' token. */
5823 cp_lexer_consume_token (parser->lexer);
5824 /* Look for the `(' token. */
5825 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5826 /* Types cannot be defined in a `typeid' expression. */
5827 saved_message = parser->type_definition_forbidden_message;
5828 parser->type_definition_forbidden_message
5829 = G_("types may not be defined in a %<typeid%> expression");
5830 /* We can't be sure yet whether we're looking at a type-id or an
5831 expression. */
5832 cp_parser_parse_tentatively (parser);
5833 /* Try a type-id first. */
5834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5835 parser->in_type_id_in_expr_p = true;
5836 type = cp_parser_type_id (parser);
5837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5838 /* Look for the `)' token. Otherwise, we can't be sure that
5839 we're not looking at an expression: consider `typeid (int
5840 (3))', for example. */
5841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5842 /* If all went well, simply lookup the type-id. */
5843 if (cp_parser_parse_definitely (parser))
5844 postfix_expression = get_typeid (type, tf_warning_or_error);
5845 /* Otherwise, fall back to the expression variant. */
5846 else
5848 tree expression;
5850 /* Look for an expression. */
5851 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5852 /* Compute its typeid. */
5853 postfix_expression = build_typeid (expression, tf_warning_or_error);
5854 /* Look for the `)' token. */
5855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5857 /* Restore the saved message. */
5858 parser->type_definition_forbidden_message = saved_message;
5859 /* `typeid' may not appear in an integral constant expression. */
5860 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5861 return error_mark_node;
5863 break;
5865 case RID_TYPENAME:
5867 tree type;
5868 /* The syntax permitted here is the same permitted for an
5869 elaborated-type-specifier. */
5870 type = cp_parser_elaborated_type_specifier (parser,
5871 /*is_friend=*/false,
5872 /*is_declaration=*/false);
5873 postfix_expression = cp_parser_functional_cast (parser, type);
5875 break;
5877 case RID_CILK_SPAWN:
5879 cp_lexer_consume_token (parser->lexer);
5880 token = cp_lexer_peek_token (parser->lexer);
5881 if (token->type == CPP_SEMICOLON)
5883 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5884 "an expression");
5885 postfix_expression = error_mark_node;
5886 break;
5888 else if (!current_function_decl)
5890 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5891 "inside a function");
5892 postfix_expression = error_mark_node;
5893 break;
5895 else
5897 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5898 saved_in_statement = parser->in_statement;
5899 parser->in_statement |= IN_CILK_SPAWN;
5901 cfun->calls_cilk_spawn = 1;
5902 postfix_expression =
5903 cp_parser_postfix_expression (parser, false, false,
5904 false, false, &idk);
5905 if (!flag_cilkplus)
5907 error_at (token->location, "-fcilkplus must be enabled to use"
5908 " %<_Cilk_spawn%>");
5909 cfun->calls_cilk_spawn = 0;
5911 else if (saved_in_statement & IN_CILK_SPAWN)
5913 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5914 "are not permitted");
5915 postfix_expression = error_mark_node;
5916 cfun->calls_cilk_spawn = 0;
5918 else
5920 postfix_expression = build_cilk_spawn (token->location,
5921 postfix_expression);
5922 if (postfix_expression != error_mark_node)
5923 SET_EXPR_LOCATION (postfix_expression, input_location);
5924 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5926 break;
5929 case RID_BUILTIN_SHUFFLE:
5931 vec<tree, va_gc> *vec;
5932 unsigned int i;
5933 tree p;
5935 cp_lexer_consume_token (parser->lexer);
5936 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5937 /*cast_p=*/false, /*allow_expansion_p=*/true,
5938 /*non_constant_p=*/NULL);
5939 if (vec == NULL)
5940 return error_mark_node;
5942 FOR_EACH_VEC_ELT (*vec, i, p)
5943 mark_exp_read (p);
5945 if (vec->length () == 2)
5946 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5947 tf_warning_or_error);
5948 else if (vec->length () == 3)
5949 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5950 tf_warning_or_error);
5951 else
5953 error_at (loc, "wrong number of arguments to "
5954 "%<__builtin_shuffle%>");
5955 return error_mark_node;
5957 break;
5960 default:
5962 tree type;
5964 /* If the next thing is a simple-type-specifier, we may be
5965 looking at a functional cast. We could also be looking at
5966 an id-expression. So, we try the functional cast, and if
5967 that doesn't work we fall back to the primary-expression. */
5968 cp_parser_parse_tentatively (parser);
5969 /* Look for the simple-type-specifier. */
5970 type = cp_parser_simple_type_specifier (parser,
5971 /*decl_specs=*/NULL,
5972 CP_PARSER_FLAGS_NONE);
5973 /* Parse the cast itself. */
5974 if (!cp_parser_error_occurred (parser))
5975 postfix_expression
5976 = cp_parser_functional_cast (parser, type);
5977 /* If that worked, we're done. */
5978 if (cp_parser_parse_definitely (parser))
5979 break;
5981 /* If the functional-cast didn't work out, try a
5982 compound-literal. */
5983 if (cp_parser_allow_gnu_extensions_p (parser)
5984 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5986 tree initializer = NULL_TREE;
5987 bool compound_literal_p;
5989 cp_parser_parse_tentatively (parser);
5990 /* Consume the `('. */
5991 cp_lexer_consume_token (parser->lexer);
5993 /* Avoid calling cp_parser_type_id pointlessly, see comment
5994 in cp_parser_cast_expression about c++/29234. */
5995 cp_lexer_save_tokens (parser->lexer);
5997 compound_literal_p
5998 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5999 /*consume_paren=*/true)
6000 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6002 /* Roll back the tokens we skipped. */
6003 cp_lexer_rollback_tokens (parser->lexer);
6005 if (!compound_literal_p)
6006 cp_parser_simulate_error (parser);
6007 else
6009 /* Parse the type. */
6010 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6011 parser->in_type_id_in_expr_p = true;
6012 type = cp_parser_type_id (parser);
6013 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6014 /* Look for the `)'. */
6015 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6018 /* If things aren't going well, there's no need to
6019 keep going. */
6020 if (!cp_parser_error_occurred (parser))
6022 bool non_constant_p;
6023 /* Parse the brace-enclosed initializer list. */
6024 initializer = cp_parser_braced_list (parser,
6025 &non_constant_p);
6027 /* If that worked, we're definitely looking at a
6028 compound-literal expression. */
6029 if (cp_parser_parse_definitely (parser))
6031 /* Warn the user that a compound literal is not
6032 allowed in standard C++. */
6033 pedwarn (input_location, OPT_Wpedantic,
6034 "ISO C++ forbids compound-literals");
6035 /* For simplicity, we disallow compound literals in
6036 constant-expressions. We could
6037 allow compound literals of integer type, whose
6038 initializer was a constant, in constant
6039 expressions. Permitting that usage, as a further
6040 extension, would not change the meaning of any
6041 currently accepted programs. (Of course, as
6042 compound literals are not part of ISO C++, the
6043 standard has nothing to say.) */
6044 if (cp_parser_non_integral_constant_expression (parser,
6045 NIC_NCC))
6047 postfix_expression = error_mark_node;
6048 break;
6050 /* Form the representation of the compound-literal. */
6051 postfix_expression
6052 = finish_compound_literal (type, initializer,
6053 tf_warning_or_error);
6054 break;
6058 /* It must be a primary-expression. */
6059 postfix_expression
6060 = cp_parser_primary_expression (parser, address_p, cast_p,
6061 /*template_arg_p=*/false,
6062 decltype_p,
6063 &idk);
6065 break;
6068 /* Note that we don't need to worry about calling build_cplus_new on a
6069 class-valued CALL_EXPR in decltype when it isn't the end of the
6070 postfix-expression; unary_complex_lvalue will take care of that for
6071 all these cases. */
6073 /* Keep looping until the postfix-expression is complete. */
6074 while (true)
6076 if (idk == CP_ID_KIND_UNQUALIFIED
6077 && identifier_p (postfix_expression)
6078 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6079 /* It is not a Koenig lookup function call. */
6080 postfix_expression
6081 = unqualified_name_lookup_error (postfix_expression);
6083 /* Peek at the next token. */
6084 token = cp_lexer_peek_token (parser->lexer);
6086 switch (token->type)
6088 case CPP_OPEN_SQUARE:
6089 if (cp_next_tokens_can_be_std_attribute_p (parser))
6091 cp_parser_error (parser,
6092 "two consecutive %<[%> shall "
6093 "only introduce an attribute");
6094 return error_mark_node;
6096 postfix_expression
6097 = cp_parser_postfix_open_square_expression (parser,
6098 postfix_expression,
6099 false,
6100 decltype_p);
6101 idk = CP_ID_KIND_NONE;
6102 is_member_access = false;
6103 break;
6105 case CPP_OPEN_PAREN:
6106 /* postfix-expression ( expression-list [opt] ) */
6108 bool koenig_p;
6109 bool is_builtin_constant_p;
6110 bool saved_integral_constant_expression_p = false;
6111 bool saved_non_integral_constant_expression_p = false;
6112 tsubst_flags_t complain = complain_flags (decltype_p);
6113 vec<tree, va_gc> *args;
6115 is_member_access = false;
6117 is_builtin_constant_p
6118 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6119 if (is_builtin_constant_p)
6121 /* The whole point of __builtin_constant_p is to allow
6122 non-constant expressions to appear as arguments. */
6123 saved_integral_constant_expression_p
6124 = parser->integral_constant_expression_p;
6125 saved_non_integral_constant_expression_p
6126 = parser->non_integral_constant_expression_p;
6127 parser->integral_constant_expression_p = false;
6129 args = (cp_parser_parenthesized_expression_list
6130 (parser, non_attr,
6131 /*cast_p=*/false, /*allow_expansion_p=*/true,
6132 /*non_constant_p=*/NULL));
6133 if (is_builtin_constant_p)
6135 parser->integral_constant_expression_p
6136 = saved_integral_constant_expression_p;
6137 parser->non_integral_constant_expression_p
6138 = saved_non_integral_constant_expression_p;
6141 if (args == NULL)
6143 postfix_expression = error_mark_node;
6144 break;
6147 /* Function calls are not permitted in
6148 constant-expressions. */
6149 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6150 && cp_parser_non_integral_constant_expression (parser,
6151 NIC_FUNC_CALL))
6153 postfix_expression = error_mark_node;
6154 release_tree_vector (args);
6155 break;
6158 koenig_p = false;
6159 if (idk == CP_ID_KIND_UNQUALIFIED
6160 || idk == CP_ID_KIND_TEMPLATE_ID)
6162 if (identifier_p (postfix_expression))
6164 if (!args->is_empty ())
6166 koenig_p = true;
6167 if (!any_type_dependent_arguments_p (args))
6168 postfix_expression
6169 = perform_koenig_lookup (postfix_expression, args,
6170 complain);
6172 else
6173 postfix_expression
6174 = unqualified_fn_lookup_error (postfix_expression);
6176 /* We do not perform argument-dependent lookup if
6177 normal lookup finds a non-function, in accordance
6178 with the expected resolution of DR 218. */
6179 else if (!args->is_empty ()
6180 && is_overloaded_fn (postfix_expression))
6182 tree fn = get_first_fn (postfix_expression);
6183 fn = STRIP_TEMPLATE (fn);
6185 /* Do not do argument dependent lookup if regular
6186 lookup finds a member function or a block-scope
6187 function declaration. [basic.lookup.argdep]/3 */
6188 if (!DECL_FUNCTION_MEMBER_P (fn)
6189 && !DECL_LOCAL_FUNCTION_P (fn))
6191 koenig_p = true;
6192 if (!any_type_dependent_arguments_p (args))
6193 postfix_expression
6194 = perform_koenig_lookup (postfix_expression, args,
6195 complain);
6200 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6202 tree instance = TREE_OPERAND (postfix_expression, 0);
6203 tree fn = TREE_OPERAND (postfix_expression, 1);
6205 if (processing_template_decl
6206 && (type_dependent_expression_p (instance)
6207 || (!BASELINK_P (fn)
6208 && TREE_CODE (fn) != FIELD_DECL)
6209 || type_dependent_expression_p (fn)
6210 || any_type_dependent_arguments_p (args)))
6212 postfix_expression
6213 = build_nt_call_vec (postfix_expression, args);
6214 release_tree_vector (args);
6215 break;
6218 if (BASELINK_P (fn))
6220 postfix_expression
6221 = (build_new_method_call
6222 (instance, fn, &args, NULL_TREE,
6223 (idk == CP_ID_KIND_QUALIFIED
6224 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6225 : LOOKUP_NORMAL),
6226 /*fn_p=*/NULL,
6227 complain));
6229 else
6230 postfix_expression
6231 = finish_call_expr (postfix_expression, &args,
6232 /*disallow_virtual=*/false,
6233 /*koenig_p=*/false,
6234 complain);
6236 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6237 || TREE_CODE (postfix_expression) == MEMBER_REF
6238 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6239 postfix_expression = (build_offset_ref_call_from_tree
6240 (postfix_expression, &args,
6241 complain));
6242 else if (idk == CP_ID_KIND_QUALIFIED)
6243 /* A call to a static class member, or a namespace-scope
6244 function. */
6245 postfix_expression
6246 = finish_call_expr (postfix_expression, &args,
6247 /*disallow_virtual=*/true,
6248 koenig_p,
6249 complain);
6250 else
6251 /* All other function calls. */
6252 postfix_expression
6253 = finish_call_expr (postfix_expression, &args,
6254 /*disallow_virtual=*/false,
6255 koenig_p,
6256 complain);
6258 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6259 idk = CP_ID_KIND_NONE;
6261 release_tree_vector (args);
6263 break;
6265 case CPP_DOT:
6266 case CPP_DEREF:
6267 /* postfix-expression . template [opt] id-expression
6268 postfix-expression . pseudo-destructor-name
6269 postfix-expression -> template [opt] id-expression
6270 postfix-expression -> pseudo-destructor-name */
6272 /* Consume the `.' or `->' operator. */
6273 cp_lexer_consume_token (parser->lexer);
6275 postfix_expression
6276 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6277 postfix_expression,
6278 false, &idk, loc);
6280 is_member_access = true;
6281 break;
6283 case CPP_PLUS_PLUS:
6284 /* postfix-expression ++ */
6285 /* Consume the `++' token. */
6286 cp_lexer_consume_token (parser->lexer);
6287 /* Generate a representation for the complete expression. */
6288 postfix_expression
6289 = finish_increment_expr (postfix_expression,
6290 POSTINCREMENT_EXPR);
6291 /* Increments may not appear in constant-expressions. */
6292 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6293 postfix_expression = error_mark_node;
6294 idk = CP_ID_KIND_NONE;
6295 is_member_access = false;
6296 break;
6298 case CPP_MINUS_MINUS:
6299 /* postfix-expression -- */
6300 /* Consume the `--' token. */
6301 cp_lexer_consume_token (parser->lexer);
6302 /* Generate a representation for the complete expression. */
6303 postfix_expression
6304 = finish_increment_expr (postfix_expression,
6305 POSTDECREMENT_EXPR);
6306 /* Decrements may not appear in constant-expressions. */
6307 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6308 postfix_expression = error_mark_node;
6309 idk = CP_ID_KIND_NONE;
6310 is_member_access = false;
6311 break;
6313 default:
6314 if (pidk_return != NULL)
6315 * pidk_return = idk;
6316 if (member_access_only_p)
6317 return is_member_access? postfix_expression : error_mark_node;
6318 else
6319 return postfix_expression;
6323 /* We should never get here. */
6324 gcc_unreachable ();
6325 return error_mark_node;
6328 /* This function parses Cilk Plus array notations. If a normal array expr. is
6329 parsed then the array index is passed back to the caller through *INIT_INDEX
6330 and the function returns a NULL_TREE. If array notation expr. is parsed,
6331 then *INIT_INDEX is ignored by the caller and the function returns
6332 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6333 error_mark_node. */
6335 static tree
6336 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6337 tree array_value)
6339 cp_token *token = NULL;
6340 tree length_index, stride = NULL_TREE, value_tree, array_type;
6341 if (!array_value || array_value == error_mark_node)
6343 cp_parser_skip_to_end_of_statement (parser);
6344 return error_mark_node;
6347 array_type = TREE_TYPE (array_value);
6349 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6350 parser->colon_corrects_to_scope_p = false;
6351 token = cp_lexer_peek_token (parser->lexer);
6353 if (!token)
6355 cp_parser_error (parser, "expected %<:%> or numeral");
6356 return error_mark_node;
6358 else if (token->type == CPP_COLON)
6360 /* Consume the ':'. */
6361 cp_lexer_consume_token (parser->lexer);
6363 /* If we are here, then we have a case like this A[:]. */
6364 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6366 cp_parser_error (parser, "expected %<]%>");
6367 cp_parser_skip_to_end_of_statement (parser);
6368 return error_mark_node;
6370 *init_index = NULL_TREE;
6371 stride = NULL_TREE;
6372 length_index = NULL_TREE;
6374 else
6376 /* If we are here, then there are three valid possibilities:
6377 1. ARRAY [ EXP ]
6378 2. ARRAY [ EXP : EXP ]
6379 3. ARRAY [ EXP : EXP : EXP ] */
6381 *init_index = cp_parser_expression (parser, false, NULL);
6382 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6384 /* This indicates that we have a normal array expression. */
6385 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6386 return NULL_TREE;
6389 /* Consume the ':'. */
6390 cp_lexer_consume_token (parser->lexer);
6391 length_index = cp_parser_expression (parser, false, NULL);
6392 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6394 cp_lexer_consume_token (parser->lexer);
6395 stride = cp_parser_expression (parser, false, NULL);
6398 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6400 if (*init_index == error_mark_node || length_index == error_mark_node
6401 || stride == error_mark_node)
6403 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6404 cp_lexer_consume_token (parser->lexer);
6405 return error_mark_node;
6407 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6409 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6410 length_index, stride, array_type);
6411 return value_tree;
6414 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6415 by cp_parser_builtin_offsetof. We're looking for
6417 postfix-expression [ expression ]
6418 postfix-expression [ braced-init-list ] (C++11)
6420 FOR_OFFSETOF is set if we're being called in that context, which
6421 changes how we deal with integer constant expressions. */
6423 static tree
6424 cp_parser_postfix_open_square_expression (cp_parser *parser,
6425 tree postfix_expression,
6426 bool for_offsetof,
6427 bool decltype_p)
6429 tree index = NULL_TREE;
6430 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6431 bool saved_greater_than_is_operator_p;
6433 /* Consume the `[' token. */
6434 cp_lexer_consume_token (parser->lexer);
6436 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6437 parser->greater_than_is_operator_p = true;
6439 /* Parse the index expression. */
6440 /* ??? For offsetof, there is a question of what to allow here. If
6441 offsetof is not being used in an integral constant expression context,
6442 then we *could* get the right answer by computing the value at runtime.
6443 If we are in an integral constant expression context, then we might
6444 could accept any constant expression; hard to say without analysis.
6445 Rather than open the barn door too wide right away, allow only integer
6446 constant expressions here. */
6447 if (for_offsetof)
6448 index = cp_parser_constant_expression (parser, false, NULL);
6449 else
6451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6453 bool expr_nonconst_p;
6454 cp_lexer_set_source_position (parser->lexer);
6455 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6456 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6457 if (flag_cilkplus
6458 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6460 error_at (cp_lexer_peek_token (parser->lexer)->location,
6461 "braced list index is not allowed with array "
6462 "notation");
6463 cp_parser_skip_to_end_of_statement (parser);
6464 return error_mark_node;
6467 else if (flag_cilkplus)
6469 /* Here are have these two options:
6470 ARRAY[EXP : EXP] - Array notation expr with default
6471 stride of 1.
6472 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6473 stride. */
6474 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6475 postfix_expression);
6476 if (an_exp)
6477 return an_exp;
6479 else
6480 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6483 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6485 /* Look for the closing `]'. */
6486 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6488 /* Build the ARRAY_REF. */
6489 postfix_expression = grok_array_decl (loc, postfix_expression,
6490 index, decltype_p);
6492 /* When not doing offsetof, array references are not permitted in
6493 constant-expressions. */
6494 if (!for_offsetof
6495 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6496 postfix_expression = error_mark_node;
6498 return postfix_expression;
6501 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6502 by cp_parser_builtin_offsetof. We're looking for
6504 postfix-expression . template [opt] id-expression
6505 postfix-expression . pseudo-destructor-name
6506 postfix-expression -> template [opt] id-expression
6507 postfix-expression -> pseudo-destructor-name
6509 FOR_OFFSETOF is set if we're being called in that context. That sorta
6510 limits what of the above we'll actually accept, but nevermind.
6511 TOKEN_TYPE is the "." or "->" token, which will already have been
6512 removed from the stream. */
6514 static tree
6515 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6516 enum cpp_ttype token_type,
6517 tree postfix_expression,
6518 bool for_offsetof, cp_id_kind *idk,
6519 location_t location)
6521 tree name;
6522 bool dependent_p;
6523 bool pseudo_destructor_p;
6524 tree scope = NULL_TREE;
6526 /* If this is a `->' operator, dereference the pointer. */
6527 if (token_type == CPP_DEREF)
6528 postfix_expression = build_x_arrow (location, postfix_expression,
6529 tf_warning_or_error);
6530 /* Check to see whether or not the expression is type-dependent. */
6531 dependent_p = type_dependent_expression_p (postfix_expression);
6532 /* The identifier following the `->' or `.' is not qualified. */
6533 parser->scope = NULL_TREE;
6534 parser->qualifying_scope = NULL_TREE;
6535 parser->object_scope = NULL_TREE;
6536 *idk = CP_ID_KIND_NONE;
6538 /* Enter the scope corresponding to the type of the object
6539 given by the POSTFIX_EXPRESSION. */
6540 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6542 scope = TREE_TYPE (postfix_expression);
6543 /* According to the standard, no expression should ever have
6544 reference type. Unfortunately, we do not currently match
6545 the standard in this respect in that our internal representation
6546 of an expression may have reference type even when the standard
6547 says it does not. Therefore, we have to manually obtain the
6548 underlying type here. */
6549 scope = non_reference (scope);
6550 /* The type of the POSTFIX_EXPRESSION must be complete. */
6551 if (scope == unknown_type_node)
6553 error_at (location, "%qE does not have class type",
6554 postfix_expression);
6555 scope = NULL_TREE;
6557 /* Unlike the object expression in other contexts, *this is not
6558 required to be of complete type for purposes of class member
6559 access (5.2.5) outside the member function body. */
6560 else if (postfix_expression != current_class_ref
6561 && !(processing_template_decl && scope == current_class_type))
6562 scope = complete_type_or_else (scope, NULL_TREE);
6563 /* Let the name lookup machinery know that we are processing a
6564 class member access expression. */
6565 parser->context->object_type = scope;
6566 /* If something went wrong, we want to be able to discern that case,
6567 as opposed to the case where there was no SCOPE due to the type
6568 of expression being dependent. */
6569 if (!scope)
6570 scope = error_mark_node;
6571 /* If the SCOPE was erroneous, make the various semantic analysis
6572 functions exit quickly -- and without issuing additional error
6573 messages. */
6574 if (scope == error_mark_node)
6575 postfix_expression = error_mark_node;
6578 /* Assume this expression is not a pseudo-destructor access. */
6579 pseudo_destructor_p = false;
6581 /* If the SCOPE is a scalar type, then, if this is a valid program,
6582 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6583 is type dependent, it can be pseudo-destructor-name or something else.
6584 Try to parse it as pseudo-destructor-name first. */
6585 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6587 tree s;
6588 tree type;
6590 cp_parser_parse_tentatively (parser);
6591 /* Parse the pseudo-destructor-name. */
6592 s = NULL_TREE;
6593 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6594 &s, &type);
6595 if (dependent_p
6596 && (cp_parser_error_occurred (parser)
6597 || !SCALAR_TYPE_P (type)))
6598 cp_parser_abort_tentative_parse (parser);
6599 else if (cp_parser_parse_definitely (parser))
6601 pseudo_destructor_p = true;
6602 postfix_expression
6603 = finish_pseudo_destructor_expr (postfix_expression,
6604 s, type, location);
6608 if (!pseudo_destructor_p)
6610 /* If the SCOPE is not a scalar type, we are looking at an
6611 ordinary class member access expression, rather than a
6612 pseudo-destructor-name. */
6613 bool template_p;
6614 cp_token *token = cp_lexer_peek_token (parser->lexer);
6615 /* Parse the id-expression. */
6616 name = (cp_parser_id_expression
6617 (parser,
6618 cp_parser_optional_template_keyword (parser),
6619 /*check_dependency_p=*/true,
6620 &template_p,
6621 /*declarator_p=*/false,
6622 /*optional_p=*/false));
6623 /* In general, build a SCOPE_REF if the member name is qualified.
6624 However, if the name was not dependent and has already been
6625 resolved; there is no need to build the SCOPE_REF. For example;
6627 struct X { void f(); };
6628 template <typename T> void f(T* t) { t->X::f(); }
6630 Even though "t" is dependent, "X::f" is not and has been resolved
6631 to a BASELINK; there is no need to include scope information. */
6633 /* But we do need to remember that there was an explicit scope for
6634 virtual function calls. */
6635 if (parser->scope)
6636 *idk = CP_ID_KIND_QUALIFIED;
6638 /* If the name is a template-id that names a type, we will get a
6639 TYPE_DECL here. That is invalid code. */
6640 if (TREE_CODE (name) == TYPE_DECL)
6642 error_at (token->location, "invalid use of %qD", name);
6643 postfix_expression = error_mark_node;
6645 else
6647 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6649 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6651 error_at (token->location, "%<%D::%D%> is not a class member",
6652 parser->scope, name);
6653 postfix_expression = error_mark_node;
6655 else
6656 name = build_qualified_name (/*type=*/NULL_TREE,
6657 parser->scope,
6658 name,
6659 template_p);
6660 parser->scope = NULL_TREE;
6661 parser->qualifying_scope = NULL_TREE;
6662 parser->object_scope = NULL_TREE;
6664 if (parser->scope && name && BASELINK_P (name))
6665 adjust_result_of_qualified_name_lookup
6666 (name, parser->scope, scope);
6667 postfix_expression
6668 = finish_class_member_access_expr (postfix_expression, name,
6669 template_p,
6670 tf_warning_or_error);
6674 /* We no longer need to look up names in the scope of the object on
6675 the left-hand side of the `.' or `->' operator. */
6676 parser->context->object_type = NULL_TREE;
6678 /* Outside of offsetof, these operators may not appear in
6679 constant-expressions. */
6680 if (!for_offsetof
6681 && (cp_parser_non_integral_constant_expression
6682 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6683 postfix_expression = error_mark_node;
6685 return postfix_expression;
6688 /* Parse a parenthesized expression-list.
6690 expression-list:
6691 assignment-expression
6692 expression-list, assignment-expression
6694 attribute-list:
6695 expression-list
6696 identifier
6697 identifier, expression-list
6699 CAST_P is true if this expression is the target of a cast.
6701 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6702 argument pack.
6704 Returns a vector of trees. Each element is a representation of an
6705 assignment-expression. NULL is returned if the ( and or ) are
6706 missing. An empty, but allocated, vector is returned on no
6707 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6708 if we are parsing an attribute list for an attribute that wants a
6709 plain identifier argument, normal_attr for an attribute that wants
6710 an expression, or non_attr if we aren't parsing an attribute list. If
6711 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6712 not all of the expressions in the list were constant. */
6714 static vec<tree, va_gc> *
6715 cp_parser_parenthesized_expression_list (cp_parser* parser,
6716 int is_attribute_list,
6717 bool cast_p,
6718 bool allow_expansion_p,
6719 bool *non_constant_p)
6721 vec<tree, va_gc> *expression_list;
6722 bool fold_expr_p = is_attribute_list != non_attr;
6723 tree identifier = NULL_TREE;
6724 bool saved_greater_than_is_operator_p;
6726 /* Assume all the expressions will be constant. */
6727 if (non_constant_p)
6728 *non_constant_p = false;
6730 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6731 return NULL;
6733 expression_list = make_tree_vector ();
6735 /* Within a parenthesized expression, a `>' token is always
6736 the greater-than operator. */
6737 saved_greater_than_is_operator_p
6738 = parser->greater_than_is_operator_p;
6739 parser->greater_than_is_operator_p = true;
6741 /* Consume expressions until there are no more. */
6742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6743 while (true)
6745 tree expr;
6747 /* At the beginning of attribute lists, check to see if the
6748 next token is an identifier. */
6749 if (is_attribute_list == id_attr
6750 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6752 cp_token *token;
6754 /* Consume the identifier. */
6755 token = cp_lexer_consume_token (parser->lexer);
6756 /* Save the identifier. */
6757 identifier = token->u.value;
6759 else
6761 bool expr_non_constant_p;
6763 /* Parse the next assignment-expression. */
6764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6766 /* A braced-init-list. */
6767 cp_lexer_set_source_position (parser->lexer);
6768 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6769 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6770 if (non_constant_p && expr_non_constant_p)
6771 *non_constant_p = true;
6773 else if (non_constant_p)
6775 expr = (cp_parser_constant_expression
6776 (parser, /*allow_non_constant_p=*/true,
6777 &expr_non_constant_p));
6778 if (expr_non_constant_p)
6779 *non_constant_p = true;
6781 else
6782 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6784 if (fold_expr_p)
6785 expr = fold_non_dependent_expr (expr);
6787 /* If we have an ellipsis, then this is an expression
6788 expansion. */
6789 if (allow_expansion_p
6790 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6792 /* Consume the `...'. */
6793 cp_lexer_consume_token (parser->lexer);
6795 /* Build the argument pack. */
6796 expr = make_pack_expansion (expr);
6799 /* Add it to the list. We add error_mark_node
6800 expressions to the list, so that we can still tell if
6801 the correct form for a parenthesized expression-list
6802 is found. That gives better errors. */
6803 vec_safe_push (expression_list, expr);
6805 if (expr == error_mark_node)
6806 goto skip_comma;
6809 /* After the first item, attribute lists look the same as
6810 expression lists. */
6811 is_attribute_list = non_attr;
6813 get_comma:;
6814 /* If the next token isn't a `,', then we are done. */
6815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6816 break;
6818 /* Otherwise, consume the `,' and keep going. */
6819 cp_lexer_consume_token (parser->lexer);
6822 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6824 int ending;
6826 skip_comma:;
6827 /* We try and resync to an unnested comma, as that will give the
6828 user better diagnostics. */
6829 ending = cp_parser_skip_to_closing_parenthesis (parser,
6830 /*recovering=*/true,
6831 /*or_comma=*/true,
6832 /*consume_paren=*/true);
6833 if (ending < 0)
6834 goto get_comma;
6835 if (!ending)
6837 parser->greater_than_is_operator_p
6838 = saved_greater_than_is_operator_p;
6839 return NULL;
6843 parser->greater_than_is_operator_p
6844 = saved_greater_than_is_operator_p;
6846 if (identifier)
6847 vec_safe_insert (expression_list, 0, identifier);
6849 return expression_list;
6852 /* Parse a pseudo-destructor-name.
6854 pseudo-destructor-name:
6855 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6856 :: [opt] nested-name-specifier template template-id :: ~ type-name
6857 :: [opt] nested-name-specifier [opt] ~ type-name
6859 If either of the first two productions is used, sets *SCOPE to the
6860 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6861 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6862 or ERROR_MARK_NODE if the parse fails. */
6864 static void
6865 cp_parser_pseudo_destructor_name (cp_parser* parser,
6866 tree object,
6867 tree* scope,
6868 tree* type)
6870 bool nested_name_specifier_p;
6872 /* Handle ~auto. */
6873 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6874 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6875 && !type_dependent_expression_p (object))
6877 if (cxx_dialect < cxx1y)
6878 pedwarn (input_location, 0,
6879 "%<~auto%> only available with "
6880 "-std=c++1y or -std=gnu++1y");
6881 cp_lexer_consume_token (parser->lexer);
6882 cp_lexer_consume_token (parser->lexer);
6883 *scope = NULL_TREE;
6884 *type = TREE_TYPE (object);
6885 return;
6888 /* Assume that things will not work out. */
6889 *type = error_mark_node;
6891 /* Look for the optional `::' operator. */
6892 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6893 /* Look for the optional nested-name-specifier. */
6894 nested_name_specifier_p
6895 = (cp_parser_nested_name_specifier_opt (parser,
6896 /*typename_keyword_p=*/false,
6897 /*check_dependency_p=*/true,
6898 /*type_p=*/false,
6899 /*is_declaration=*/false)
6900 != NULL_TREE);
6901 /* Now, if we saw a nested-name-specifier, we might be doing the
6902 second production. */
6903 if (nested_name_specifier_p
6904 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6906 /* Consume the `template' keyword. */
6907 cp_lexer_consume_token (parser->lexer);
6908 /* Parse the template-id. */
6909 cp_parser_template_id (parser,
6910 /*template_keyword_p=*/true,
6911 /*check_dependency_p=*/false,
6912 class_type,
6913 /*is_declaration=*/true);
6914 /* Look for the `::' token. */
6915 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6917 /* If the next token is not a `~', then there might be some
6918 additional qualification. */
6919 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6921 /* At this point, we're looking for "type-name :: ~". The type-name
6922 must not be a class-name, since this is a pseudo-destructor. So,
6923 it must be either an enum-name, or a typedef-name -- both of which
6924 are just identifiers. So, we peek ahead to check that the "::"
6925 and "~" tokens are present; if they are not, then we can avoid
6926 calling type_name. */
6927 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6928 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6929 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6931 cp_parser_error (parser, "non-scalar type");
6932 return;
6935 /* Look for the type-name. */
6936 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6937 if (*scope == error_mark_node)
6938 return;
6940 /* Look for the `::' token. */
6941 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6943 else
6944 *scope = NULL_TREE;
6946 /* Look for the `~'. */
6947 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6949 /* Once we see the ~, this has to be a pseudo-destructor. */
6950 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6951 cp_parser_commit_to_topmost_tentative_parse (parser);
6953 /* Look for the type-name again. We are not responsible for
6954 checking that it matches the first type-name. */
6955 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6958 /* Parse a unary-expression.
6960 unary-expression:
6961 postfix-expression
6962 ++ cast-expression
6963 -- cast-expression
6964 unary-operator cast-expression
6965 sizeof unary-expression
6966 sizeof ( type-id )
6967 alignof ( type-id ) [C++0x]
6968 new-expression
6969 delete-expression
6971 GNU Extensions:
6973 unary-expression:
6974 __extension__ cast-expression
6975 __alignof__ unary-expression
6976 __alignof__ ( type-id )
6977 alignof unary-expression [C++0x]
6978 __real__ cast-expression
6979 __imag__ cast-expression
6980 && identifier
6981 sizeof ( type-id ) { initializer-list , [opt] }
6982 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6983 __alignof__ ( type-id ) { initializer-list , [opt] }
6985 ADDRESS_P is true iff the unary-expression is appearing as the
6986 operand of the `&' operator. CAST_P is true if this expression is
6987 the target of a cast.
6989 Returns a representation of the expression. */
6991 static tree
6992 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6993 bool decltype_p, cp_id_kind * pidk)
6995 cp_token *token;
6996 enum tree_code unary_operator;
6998 /* Peek at the next token. */
6999 token = cp_lexer_peek_token (parser->lexer);
7000 /* Some keywords give away the kind of expression. */
7001 if (token->type == CPP_KEYWORD)
7003 enum rid keyword = token->keyword;
7005 switch (keyword)
7007 case RID_ALIGNOF:
7008 case RID_SIZEOF:
7010 tree operand, ret;
7011 enum tree_code op;
7012 location_t first_loc;
7014 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7015 /* Consume the token. */
7016 cp_lexer_consume_token (parser->lexer);
7017 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7018 /* Parse the operand. */
7019 operand = cp_parser_sizeof_operand (parser, keyword);
7021 if (TYPE_P (operand))
7022 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7023 else
7025 /* ISO C++ defines alignof only with types, not with
7026 expressions. So pedwarn if alignof is used with a non-
7027 type expression. However, __alignof__ is ok. */
7028 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7029 pedwarn (token->location, OPT_Wpedantic,
7030 "ISO C++ does not allow %<alignof%> "
7031 "with a non-type");
7033 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7035 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7036 SIZEOF_EXPR with the original operand. */
7037 if (op == SIZEOF_EXPR && ret != error_mark_node)
7039 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7041 if (!processing_template_decl && TYPE_P (operand))
7043 ret = build_min (SIZEOF_EXPR, size_type_node,
7044 build1 (NOP_EXPR, operand,
7045 error_mark_node));
7046 SIZEOF_EXPR_TYPE_P (ret) = 1;
7048 else
7049 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7050 TREE_SIDE_EFFECTS (ret) = 0;
7051 TREE_READONLY (ret) = 1;
7053 SET_EXPR_LOCATION (ret, first_loc);
7055 return ret;
7058 case RID_NEW:
7059 return cp_parser_new_expression (parser);
7061 case RID_DELETE:
7062 return cp_parser_delete_expression (parser);
7064 case RID_EXTENSION:
7066 /* The saved value of the PEDANTIC flag. */
7067 int saved_pedantic;
7068 tree expr;
7070 /* Save away the PEDANTIC flag. */
7071 cp_parser_extension_opt (parser, &saved_pedantic);
7072 /* Parse the cast-expression. */
7073 expr = cp_parser_simple_cast_expression (parser);
7074 /* Restore the PEDANTIC flag. */
7075 pedantic = saved_pedantic;
7077 return expr;
7080 case RID_REALPART:
7081 case RID_IMAGPART:
7083 tree expression;
7085 /* Consume the `__real__' or `__imag__' token. */
7086 cp_lexer_consume_token (parser->lexer);
7087 /* Parse the cast-expression. */
7088 expression = cp_parser_simple_cast_expression (parser);
7089 /* Create the complete representation. */
7090 return build_x_unary_op (token->location,
7091 (keyword == RID_REALPART
7092 ? REALPART_EXPR : IMAGPART_EXPR),
7093 expression,
7094 tf_warning_or_error);
7096 break;
7098 case RID_TRANSACTION_ATOMIC:
7099 case RID_TRANSACTION_RELAXED:
7100 return cp_parser_transaction_expression (parser, keyword);
7102 case RID_NOEXCEPT:
7104 tree expr;
7105 const char *saved_message;
7106 bool saved_integral_constant_expression_p;
7107 bool saved_non_integral_constant_expression_p;
7108 bool saved_greater_than_is_operator_p;
7110 cp_lexer_consume_token (parser->lexer);
7111 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7113 saved_message = parser->type_definition_forbidden_message;
7114 parser->type_definition_forbidden_message
7115 = G_("types may not be defined in %<noexcept%> expressions");
7117 saved_integral_constant_expression_p
7118 = parser->integral_constant_expression_p;
7119 saved_non_integral_constant_expression_p
7120 = parser->non_integral_constant_expression_p;
7121 parser->integral_constant_expression_p = false;
7123 saved_greater_than_is_operator_p
7124 = parser->greater_than_is_operator_p;
7125 parser->greater_than_is_operator_p = true;
7127 ++cp_unevaluated_operand;
7128 ++c_inhibit_evaluation_warnings;
7129 expr = cp_parser_expression (parser, false, NULL);
7130 --c_inhibit_evaluation_warnings;
7131 --cp_unevaluated_operand;
7133 parser->greater_than_is_operator_p
7134 = saved_greater_than_is_operator_p;
7136 parser->integral_constant_expression_p
7137 = saved_integral_constant_expression_p;
7138 parser->non_integral_constant_expression_p
7139 = saved_non_integral_constant_expression_p;
7141 parser->type_definition_forbidden_message = saved_message;
7143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7144 return finish_noexcept_expr (expr, tf_warning_or_error);
7147 default:
7148 break;
7152 /* Look for the `:: new' and `:: delete', which also signal the
7153 beginning of a new-expression, or delete-expression,
7154 respectively. If the next token is `::', then it might be one of
7155 these. */
7156 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7158 enum rid keyword;
7160 /* See if the token after the `::' is one of the keywords in
7161 which we're interested. */
7162 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7163 /* If it's `new', we have a new-expression. */
7164 if (keyword == RID_NEW)
7165 return cp_parser_new_expression (parser);
7166 /* Similarly, for `delete'. */
7167 else if (keyword == RID_DELETE)
7168 return cp_parser_delete_expression (parser);
7171 /* Look for a unary operator. */
7172 unary_operator = cp_parser_unary_operator (token);
7173 /* The `++' and `--' operators can be handled similarly, even though
7174 they are not technically unary-operators in the grammar. */
7175 if (unary_operator == ERROR_MARK)
7177 if (token->type == CPP_PLUS_PLUS)
7178 unary_operator = PREINCREMENT_EXPR;
7179 else if (token->type == CPP_MINUS_MINUS)
7180 unary_operator = PREDECREMENT_EXPR;
7181 /* Handle the GNU address-of-label extension. */
7182 else if (cp_parser_allow_gnu_extensions_p (parser)
7183 && token->type == CPP_AND_AND)
7185 tree identifier;
7186 tree expression;
7187 location_t loc = token->location;
7189 /* Consume the '&&' token. */
7190 cp_lexer_consume_token (parser->lexer);
7191 /* Look for the identifier. */
7192 identifier = cp_parser_identifier (parser);
7193 /* Create an expression representing the address. */
7194 expression = finish_label_address_expr (identifier, loc);
7195 if (cp_parser_non_integral_constant_expression (parser,
7196 NIC_ADDR_LABEL))
7197 expression = error_mark_node;
7198 return expression;
7201 if (unary_operator != ERROR_MARK)
7203 tree cast_expression;
7204 tree expression = error_mark_node;
7205 non_integral_constant non_constant_p = NIC_NONE;
7206 location_t loc = token->location;
7207 tsubst_flags_t complain = complain_flags (decltype_p);
7209 /* Consume the operator token. */
7210 token = cp_lexer_consume_token (parser->lexer);
7211 /* Parse the cast-expression. */
7212 cast_expression
7213 = cp_parser_cast_expression (parser,
7214 unary_operator == ADDR_EXPR,
7215 /*cast_p=*/false,
7216 /*decltype*/false,
7217 pidk);
7218 /* Now, build an appropriate representation. */
7219 switch (unary_operator)
7221 case INDIRECT_REF:
7222 non_constant_p = NIC_STAR;
7223 expression = build_x_indirect_ref (loc, cast_expression,
7224 RO_UNARY_STAR,
7225 complain);
7226 break;
7228 case ADDR_EXPR:
7229 non_constant_p = NIC_ADDR;
7230 /* Fall through. */
7231 case BIT_NOT_EXPR:
7232 expression = build_x_unary_op (loc, unary_operator,
7233 cast_expression,
7234 complain);
7235 break;
7237 case PREINCREMENT_EXPR:
7238 case PREDECREMENT_EXPR:
7239 non_constant_p = unary_operator == PREINCREMENT_EXPR
7240 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7241 /* Fall through. */
7242 case UNARY_PLUS_EXPR:
7243 case NEGATE_EXPR:
7244 case TRUTH_NOT_EXPR:
7245 expression = finish_unary_op_expr (loc, unary_operator,
7246 cast_expression, complain);
7247 break;
7249 default:
7250 gcc_unreachable ();
7253 if (non_constant_p != NIC_NONE
7254 && cp_parser_non_integral_constant_expression (parser,
7255 non_constant_p))
7256 expression = error_mark_node;
7258 return expression;
7261 return cp_parser_postfix_expression (parser, address_p, cast_p,
7262 /*member_access_only_p=*/false,
7263 decltype_p,
7264 pidk);
7267 static inline tree
7268 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7269 cp_id_kind * pidk)
7271 return cp_parser_unary_expression (parser, address_p, cast_p,
7272 /*decltype*/false, pidk);
7275 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7276 unary-operator, the corresponding tree code is returned. */
7278 static enum tree_code
7279 cp_parser_unary_operator (cp_token* token)
7281 switch (token->type)
7283 case CPP_MULT:
7284 return INDIRECT_REF;
7286 case CPP_AND:
7287 return ADDR_EXPR;
7289 case CPP_PLUS:
7290 return UNARY_PLUS_EXPR;
7292 case CPP_MINUS:
7293 return NEGATE_EXPR;
7295 case CPP_NOT:
7296 return TRUTH_NOT_EXPR;
7298 case CPP_COMPL:
7299 return BIT_NOT_EXPR;
7301 default:
7302 return ERROR_MARK;
7306 /* Parse a new-expression.
7308 new-expression:
7309 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7310 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7312 Returns a representation of the expression. */
7314 static tree
7315 cp_parser_new_expression (cp_parser* parser)
7317 bool global_scope_p;
7318 vec<tree, va_gc> *placement;
7319 tree type;
7320 vec<tree, va_gc> *initializer;
7321 tree nelts = NULL_TREE;
7322 tree ret;
7324 /* Look for the optional `::' operator. */
7325 global_scope_p
7326 = (cp_parser_global_scope_opt (parser,
7327 /*current_scope_valid_p=*/false)
7328 != NULL_TREE);
7329 /* Look for the `new' operator. */
7330 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7331 /* There's no easy way to tell a new-placement from the
7332 `( type-id )' construct. */
7333 cp_parser_parse_tentatively (parser);
7334 /* Look for a new-placement. */
7335 placement = cp_parser_new_placement (parser);
7336 /* If that didn't work out, there's no new-placement. */
7337 if (!cp_parser_parse_definitely (parser))
7339 if (placement != NULL)
7340 release_tree_vector (placement);
7341 placement = NULL;
7344 /* If the next token is a `(', then we have a parenthesized
7345 type-id. */
7346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7348 cp_token *token;
7349 const char *saved_message = parser->type_definition_forbidden_message;
7351 /* Consume the `('. */
7352 cp_lexer_consume_token (parser->lexer);
7354 /* Parse the type-id. */
7355 parser->type_definition_forbidden_message
7356 = G_("types may not be defined in a new-expression");
7357 type = cp_parser_type_id (parser);
7358 parser->type_definition_forbidden_message = saved_message;
7360 /* Look for the closing `)'. */
7361 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7362 token = cp_lexer_peek_token (parser->lexer);
7363 /* There should not be a direct-new-declarator in this production,
7364 but GCC used to allowed this, so we check and emit a sensible error
7365 message for this case. */
7366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7368 error_at (token->location,
7369 "array bound forbidden after parenthesized type-id");
7370 inform (token->location,
7371 "try removing the parentheses around the type-id");
7372 cp_parser_direct_new_declarator (parser);
7375 /* Otherwise, there must be a new-type-id. */
7376 else
7377 type = cp_parser_new_type_id (parser, &nelts);
7379 /* If the next token is a `(' or '{', then we have a new-initializer. */
7380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7381 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7382 initializer = cp_parser_new_initializer (parser);
7383 else
7384 initializer = NULL;
7386 /* A new-expression may not appear in an integral constant
7387 expression. */
7388 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7389 ret = error_mark_node;
7390 else
7392 /* Create a representation of the new-expression. */
7393 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7394 tf_warning_or_error);
7397 if (placement != NULL)
7398 release_tree_vector (placement);
7399 if (initializer != NULL)
7400 release_tree_vector (initializer);
7402 return ret;
7405 /* Parse a new-placement.
7407 new-placement:
7408 ( expression-list )
7410 Returns the same representation as for an expression-list. */
7412 static vec<tree, va_gc> *
7413 cp_parser_new_placement (cp_parser* parser)
7415 vec<tree, va_gc> *expression_list;
7417 /* Parse the expression-list. */
7418 expression_list = (cp_parser_parenthesized_expression_list
7419 (parser, non_attr, /*cast_p=*/false,
7420 /*allow_expansion_p=*/true,
7421 /*non_constant_p=*/NULL));
7423 return expression_list;
7426 /* Parse a new-type-id.
7428 new-type-id:
7429 type-specifier-seq new-declarator [opt]
7431 Returns the TYPE allocated. If the new-type-id indicates an array
7432 type, *NELTS is set to the number of elements in the last array
7433 bound; the TYPE will not include the last array bound. */
7435 static tree
7436 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7438 cp_decl_specifier_seq type_specifier_seq;
7439 cp_declarator *new_declarator;
7440 cp_declarator *declarator;
7441 cp_declarator *outer_declarator;
7442 const char *saved_message;
7444 /* The type-specifier sequence must not contain type definitions.
7445 (It cannot contain declarations of new types either, but if they
7446 are not definitions we will catch that because they are not
7447 complete.) */
7448 saved_message = parser->type_definition_forbidden_message;
7449 parser->type_definition_forbidden_message
7450 = G_("types may not be defined in a new-type-id");
7451 /* Parse the type-specifier-seq. */
7452 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7453 /*is_trailing_return=*/false,
7454 &type_specifier_seq);
7455 /* Restore the old message. */
7456 parser->type_definition_forbidden_message = saved_message;
7458 if (type_specifier_seq.type == error_mark_node)
7459 return error_mark_node;
7461 /* Parse the new-declarator. */
7462 new_declarator = cp_parser_new_declarator_opt (parser);
7464 /* Determine the number of elements in the last array dimension, if
7465 any. */
7466 *nelts = NULL_TREE;
7467 /* Skip down to the last array dimension. */
7468 declarator = new_declarator;
7469 outer_declarator = NULL;
7470 while (declarator && (declarator->kind == cdk_pointer
7471 || declarator->kind == cdk_ptrmem))
7473 outer_declarator = declarator;
7474 declarator = declarator->declarator;
7476 while (declarator
7477 && declarator->kind == cdk_array
7478 && declarator->declarator
7479 && declarator->declarator->kind == cdk_array)
7481 outer_declarator = declarator;
7482 declarator = declarator->declarator;
7485 if (declarator && declarator->kind == cdk_array)
7487 *nelts = declarator->u.array.bounds;
7488 if (*nelts == error_mark_node)
7489 *nelts = integer_one_node;
7491 if (outer_declarator)
7492 outer_declarator->declarator = declarator->declarator;
7493 else
7494 new_declarator = NULL;
7497 return groktypename (&type_specifier_seq, new_declarator, false);
7500 /* Parse an (optional) new-declarator.
7502 new-declarator:
7503 ptr-operator new-declarator [opt]
7504 direct-new-declarator
7506 Returns the declarator. */
7508 static cp_declarator *
7509 cp_parser_new_declarator_opt (cp_parser* parser)
7511 enum tree_code code;
7512 tree type, std_attributes = NULL_TREE;
7513 cp_cv_quals cv_quals;
7515 /* We don't know if there's a ptr-operator next, or not. */
7516 cp_parser_parse_tentatively (parser);
7517 /* Look for a ptr-operator. */
7518 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7519 /* If that worked, look for more new-declarators. */
7520 if (cp_parser_parse_definitely (parser))
7522 cp_declarator *declarator;
7524 /* Parse another optional declarator. */
7525 declarator = cp_parser_new_declarator_opt (parser);
7527 declarator = cp_parser_make_indirect_declarator
7528 (code, type, cv_quals, declarator, std_attributes);
7530 return declarator;
7533 /* If the next token is a `[', there is a direct-new-declarator. */
7534 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7535 return cp_parser_direct_new_declarator (parser);
7537 return NULL;
7540 /* Parse a direct-new-declarator.
7542 direct-new-declarator:
7543 [ expression ]
7544 direct-new-declarator [constant-expression]
7548 static cp_declarator *
7549 cp_parser_direct_new_declarator (cp_parser* parser)
7551 cp_declarator *declarator = NULL;
7553 while (true)
7555 tree expression;
7556 cp_token *token;
7558 /* Look for the opening `['. */
7559 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7561 token = cp_lexer_peek_token (parser->lexer);
7562 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7563 /* The standard requires that the expression have integral
7564 type. DR 74 adds enumeration types. We believe that the
7565 real intent is that these expressions be handled like the
7566 expression in a `switch' condition, which also allows
7567 classes with a single conversion to integral or
7568 enumeration type. */
7569 if (!processing_template_decl)
7571 expression
7572 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7573 expression,
7574 /*complain=*/true);
7575 if (!expression)
7577 error_at (token->location,
7578 "expression in new-declarator must have integral "
7579 "or enumeration type");
7580 expression = error_mark_node;
7584 /* Look for the closing `]'. */
7585 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7587 /* Add this bound to the declarator. */
7588 declarator = make_array_declarator (declarator, expression);
7590 /* If the next token is not a `[', then there are no more
7591 bounds. */
7592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7593 break;
7596 return declarator;
7599 /* Parse a new-initializer.
7601 new-initializer:
7602 ( expression-list [opt] )
7603 braced-init-list
7605 Returns a representation of the expression-list. */
7607 static vec<tree, va_gc> *
7608 cp_parser_new_initializer (cp_parser* parser)
7610 vec<tree, va_gc> *expression_list;
7612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7614 tree t;
7615 bool expr_non_constant_p;
7616 cp_lexer_set_source_position (parser->lexer);
7617 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7618 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7619 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7620 expression_list = make_tree_vector_single (t);
7622 else
7623 expression_list = (cp_parser_parenthesized_expression_list
7624 (parser, non_attr, /*cast_p=*/false,
7625 /*allow_expansion_p=*/true,
7626 /*non_constant_p=*/NULL));
7628 return expression_list;
7631 /* Parse a delete-expression.
7633 delete-expression:
7634 :: [opt] delete cast-expression
7635 :: [opt] delete [ ] cast-expression
7637 Returns a representation of the expression. */
7639 static tree
7640 cp_parser_delete_expression (cp_parser* parser)
7642 bool global_scope_p;
7643 bool array_p;
7644 tree expression;
7646 /* Look for the optional `::' operator. */
7647 global_scope_p
7648 = (cp_parser_global_scope_opt (parser,
7649 /*current_scope_valid_p=*/false)
7650 != NULL_TREE);
7651 /* Look for the `delete' keyword. */
7652 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7653 /* See if the array syntax is in use. */
7654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7656 /* Consume the `[' token. */
7657 cp_lexer_consume_token (parser->lexer);
7658 /* Look for the `]' token. */
7659 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7660 /* Remember that this is the `[]' construct. */
7661 array_p = true;
7663 else
7664 array_p = false;
7666 /* Parse the cast-expression. */
7667 expression = cp_parser_simple_cast_expression (parser);
7669 /* A delete-expression may not appear in an integral constant
7670 expression. */
7671 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7672 return error_mark_node;
7674 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7675 tf_warning_or_error);
7678 /* Returns true if TOKEN may start a cast-expression and false
7679 otherwise. */
7681 static bool
7682 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7684 cp_token *token = cp_lexer_peek_token (parser->lexer);
7685 switch (token->type)
7687 case CPP_COMMA:
7688 case CPP_SEMICOLON:
7689 case CPP_QUERY:
7690 case CPP_COLON:
7691 case CPP_CLOSE_SQUARE:
7692 case CPP_CLOSE_PAREN:
7693 case CPP_CLOSE_BRACE:
7694 case CPP_OPEN_BRACE:
7695 case CPP_DOT:
7696 case CPP_DOT_STAR:
7697 case CPP_DEREF:
7698 case CPP_DEREF_STAR:
7699 case CPP_DIV:
7700 case CPP_MOD:
7701 case CPP_LSHIFT:
7702 case CPP_RSHIFT:
7703 case CPP_LESS:
7704 case CPP_GREATER:
7705 case CPP_LESS_EQ:
7706 case CPP_GREATER_EQ:
7707 case CPP_EQ_EQ:
7708 case CPP_NOT_EQ:
7709 case CPP_EQ:
7710 case CPP_MULT_EQ:
7711 case CPP_DIV_EQ:
7712 case CPP_MOD_EQ:
7713 case CPP_PLUS_EQ:
7714 case CPP_MINUS_EQ:
7715 case CPP_RSHIFT_EQ:
7716 case CPP_LSHIFT_EQ:
7717 case CPP_AND_EQ:
7718 case CPP_XOR_EQ:
7719 case CPP_OR_EQ:
7720 case CPP_XOR:
7721 case CPP_OR:
7722 case CPP_OR_OR:
7723 case CPP_EOF:
7724 return false;
7726 case CPP_OPEN_PAREN:
7727 /* In ((type ()) () the last () isn't a valid cast-expression,
7728 so the whole must be parsed as postfix-expression. */
7729 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7730 != CPP_CLOSE_PAREN;
7732 /* '[' may start a primary-expression in obj-c++. */
7733 case CPP_OPEN_SQUARE:
7734 return c_dialect_objc ();
7736 default:
7737 return true;
7741 /* Parse a cast-expression.
7743 cast-expression:
7744 unary-expression
7745 ( type-id ) cast-expression
7747 ADDRESS_P is true iff the unary-expression is appearing as the
7748 operand of the `&' operator. CAST_P is true if this expression is
7749 the target of a cast.
7751 Returns a representation of the expression. */
7753 static tree
7754 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7755 bool decltype_p, cp_id_kind * pidk)
7757 /* If it's a `(', then we might be looking at a cast. */
7758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7760 tree type = NULL_TREE;
7761 tree expr = NULL_TREE;
7762 bool cast_expression_p;
7763 const char *saved_message;
7765 /* There's no way to know yet whether or not this is a cast.
7766 For example, `(int (3))' is a unary-expression, while `(int)
7767 3' is a cast. So, we resort to parsing tentatively. */
7768 cp_parser_parse_tentatively (parser);
7769 /* Types may not be defined in a cast. */
7770 saved_message = parser->type_definition_forbidden_message;
7771 parser->type_definition_forbidden_message
7772 = G_("types may not be defined in casts");
7773 /* Consume the `('. */
7774 cp_lexer_consume_token (parser->lexer);
7775 /* A very tricky bit is that `(struct S) { 3 }' is a
7776 compound-literal (which we permit in C++ as an extension).
7777 But, that construct is not a cast-expression -- it is a
7778 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7779 is legal; if the compound-literal were a cast-expression,
7780 you'd need an extra set of parentheses.) But, if we parse
7781 the type-id, and it happens to be a class-specifier, then we
7782 will commit to the parse at that point, because we cannot
7783 undo the action that is done when creating a new class. So,
7784 then we cannot back up and do a postfix-expression.
7785 Another tricky case is the following (c++/29234):
7787 struct S { void operator () (); };
7789 void foo ()
7791 ( S()() );
7794 As a type-id we parse the parenthesized S()() as a function
7795 returning a function, groktypename complains and we cannot
7796 back up in this case either.
7798 Therefore, we scan ahead to the closing `)', and check to see
7799 if the tokens after the `)' can start a cast-expression. Otherwise
7800 we are dealing with an unary-expression, a postfix-expression
7801 or something else.
7803 Save tokens so that we can put them back. */
7804 cp_lexer_save_tokens (parser->lexer);
7806 /* We may be looking at a cast-expression. */
7807 cast_expression_p
7808 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7809 /*consume_paren=*/true)
7810 && cp_parser_tokens_start_cast_expression (parser));
7812 /* Roll back the tokens we skipped. */
7813 cp_lexer_rollback_tokens (parser->lexer);
7814 /* If we aren't looking at a cast-expression, simulate an error so
7815 that the call to cp_parser_parse_definitely below will fail. */
7816 if (!cast_expression_p)
7817 cp_parser_simulate_error (parser);
7818 else
7820 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7821 parser->in_type_id_in_expr_p = true;
7822 /* Look for the type-id. */
7823 type = cp_parser_type_id (parser);
7824 /* Look for the closing `)'. */
7825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7826 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7829 /* Restore the saved message. */
7830 parser->type_definition_forbidden_message = saved_message;
7832 /* At this point this can only be either a cast or a
7833 parenthesized ctor such as `(T ())' that looks like a cast to
7834 function returning T. */
7835 if (!cp_parser_error_occurred (parser))
7837 cp_parser_parse_definitely (parser);
7838 expr = cp_parser_cast_expression (parser,
7839 /*address_p=*/false,
7840 /*cast_p=*/true,
7841 /*decltype_p=*/false,
7842 pidk);
7844 /* Warn about old-style casts, if so requested. */
7845 if (warn_old_style_cast
7846 && !in_system_header_at (input_location)
7847 && !VOID_TYPE_P (type)
7848 && current_lang_name != lang_name_c)
7849 warning (OPT_Wold_style_cast, "use of old-style cast");
7851 /* Only type conversions to integral or enumeration types
7852 can be used in constant-expressions. */
7853 if (!cast_valid_in_integral_constant_expression_p (type)
7854 && cp_parser_non_integral_constant_expression (parser,
7855 NIC_CAST))
7856 return error_mark_node;
7858 /* Perform the cast. */
7859 expr = build_c_cast (input_location, type, expr);
7860 return expr;
7862 else
7863 cp_parser_abort_tentative_parse (parser);
7866 /* If we get here, then it's not a cast, so it must be a
7867 unary-expression. */
7868 return cp_parser_unary_expression (parser, address_p, cast_p,
7869 decltype_p, pidk);
7872 /* Parse a binary expression of the general form:
7874 pm-expression:
7875 cast-expression
7876 pm-expression .* cast-expression
7877 pm-expression ->* cast-expression
7879 multiplicative-expression:
7880 pm-expression
7881 multiplicative-expression * pm-expression
7882 multiplicative-expression / pm-expression
7883 multiplicative-expression % pm-expression
7885 additive-expression:
7886 multiplicative-expression
7887 additive-expression + multiplicative-expression
7888 additive-expression - multiplicative-expression
7890 shift-expression:
7891 additive-expression
7892 shift-expression << additive-expression
7893 shift-expression >> additive-expression
7895 relational-expression:
7896 shift-expression
7897 relational-expression < shift-expression
7898 relational-expression > shift-expression
7899 relational-expression <= shift-expression
7900 relational-expression >= shift-expression
7902 GNU Extension:
7904 relational-expression:
7905 relational-expression <? shift-expression
7906 relational-expression >? shift-expression
7908 equality-expression:
7909 relational-expression
7910 equality-expression == relational-expression
7911 equality-expression != relational-expression
7913 and-expression:
7914 equality-expression
7915 and-expression & equality-expression
7917 exclusive-or-expression:
7918 and-expression
7919 exclusive-or-expression ^ and-expression
7921 inclusive-or-expression:
7922 exclusive-or-expression
7923 inclusive-or-expression | exclusive-or-expression
7925 logical-and-expression:
7926 inclusive-or-expression
7927 logical-and-expression && inclusive-or-expression
7929 logical-or-expression:
7930 logical-and-expression
7931 logical-or-expression || logical-and-expression
7933 All these are implemented with a single function like:
7935 binary-expression:
7936 simple-cast-expression
7937 binary-expression <token> binary-expression
7939 CAST_P is true if this expression is the target of a cast.
7941 The binops_by_token map is used to get the tree codes for each <token> type.
7942 binary-expressions are associated according to a precedence table. */
7944 #define TOKEN_PRECEDENCE(token) \
7945 (((token->type == CPP_GREATER \
7946 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7947 && !parser->greater_than_is_operator_p) \
7948 ? PREC_NOT_OPERATOR \
7949 : binops_by_token[token->type].prec)
7951 static tree
7952 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7953 bool no_toplevel_fold_p,
7954 bool decltype_p,
7955 enum cp_parser_prec prec,
7956 cp_id_kind * pidk)
7958 cp_parser_expression_stack stack;
7959 cp_parser_expression_stack_entry *sp = &stack[0];
7960 cp_parser_expression_stack_entry current;
7961 tree rhs;
7962 cp_token *token;
7963 enum tree_code rhs_type;
7964 enum cp_parser_prec new_prec, lookahead_prec;
7965 tree overload;
7966 bool parenthesized_not_lhs_warn
7967 = cp_lexer_next_token_is (parser->lexer, CPP_NOT);
7969 /* Parse the first expression. */
7970 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7971 cast_p, decltype_p, pidk);
7972 current.lhs_type = ERROR_MARK;
7973 current.prec = prec;
7975 if (cp_parser_error_occurred (parser))
7976 return error_mark_node;
7978 for (;;)
7980 /* Get an operator token. */
7981 token = cp_lexer_peek_token (parser->lexer);
7983 if (warn_cxx0x_compat
7984 && token->type == CPP_RSHIFT
7985 && !parser->greater_than_is_operator_p)
7987 if (warning_at (token->location, OPT_Wc__0x_compat,
7988 "%<>>%> operator is treated"
7989 " as two right angle brackets in C++11"))
7990 inform (token->location,
7991 "suggest parentheses around %<>>%> expression");
7994 new_prec = TOKEN_PRECEDENCE (token);
7996 /* Popping an entry off the stack means we completed a subexpression:
7997 - either we found a token which is not an operator (`>' where it is not
7998 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7999 will happen repeatedly;
8000 - or, we found an operator which has lower priority. This is the case
8001 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8002 parsing `3 * 4'. */
8003 if (new_prec <= current.prec)
8005 if (sp == stack)
8006 break;
8007 else
8008 goto pop;
8011 get_rhs:
8012 current.tree_type = binops_by_token[token->type].tree_type;
8013 current.loc = token->location;
8015 /* We used the operator token. */
8016 cp_lexer_consume_token (parser->lexer);
8018 /* For "false && x" or "true || x", x will never be executed;
8019 disable warnings while evaluating it. */
8020 if (current.tree_type == TRUTH_ANDIF_EXPR)
8021 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8022 else if (current.tree_type == TRUTH_ORIF_EXPR)
8023 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8025 /* Extract another operand. It may be the RHS of this expression
8026 or the LHS of a new, higher priority expression. */
8027 rhs = cp_parser_simple_cast_expression (parser);
8028 rhs_type = ERROR_MARK;
8030 /* Get another operator token. Look up its precedence to avoid
8031 building a useless (immediately popped) stack entry for common
8032 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8033 token = cp_lexer_peek_token (parser->lexer);
8034 lookahead_prec = TOKEN_PRECEDENCE (token);
8035 if (lookahead_prec > new_prec)
8037 /* ... and prepare to parse the RHS of the new, higher priority
8038 expression. Since precedence levels on the stack are
8039 monotonically increasing, we do not have to care about
8040 stack overflows. */
8041 *sp = current;
8042 ++sp;
8043 current.lhs = rhs;
8044 current.lhs_type = rhs_type;
8045 current.prec = new_prec;
8046 new_prec = lookahead_prec;
8047 goto get_rhs;
8049 pop:
8050 lookahead_prec = new_prec;
8051 /* If the stack is not empty, we have parsed into LHS the right side
8052 (`4' in the example above) of an expression we had suspended.
8053 We can use the information on the stack to recover the LHS (`3')
8054 from the stack together with the tree code (`MULT_EXPR'), and
8055 the precedence of the higher level subexpression
8056 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8057 which will be used to actually build the additive expression. */
8058 rhs = current.lhs;
8059 rhs_type = current.lhs_type;
8060 --sp;
8061 current = *sp;
8064 /* Undo the disabling of warnings done above. */
8065 if (current.tree_type == TRUTH_ANDIF_EXPR)
8066 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8067 else if (current.tree_type == TRUTH_ORIF_EXPR)
8068 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8070 if (warn_logical_not_paren
8071 && parenthesized_not_lhs_warn)
8072 warn_logical_not_parentheses (current.loc, current.tree_type,
8073 TREE_OPERAND (current.lhs, 0), rhs);
8075 overload = NULL;
8076 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8077 ERROR_MARK for everything that is not a binary expression.
8078 This makes warn_about_parentheses miss some warnings that
8079 involve unary operators. For unary expressions we should
8080 pass the correct tree_code unless the unary expression was
8081 surrounded by parentheses.
8083 if (no_toplevel_fold_p
8084 && lookahead_prec <= current.prec
8085 && sp == stack)
8086 current.lhs = build2 (current.tree_type,
8087 TREE_CODE_CLASS (current.tree_type)
8088 == tcc_comparison
8089 ? boolean_type_node : TREE_TYPE (current.lhs),
8090 current.lhs, rhs);
8091 else
8092 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8093 current.lhs, current.lhs_type,
8094 rhs, rhs_type, &overload,
8095 complain_flags (decltype_p));
8096 current.lhs_type = current.tree_type;
8097 if (EXPR_P (current.lhs))
8098 SET_EXPR_LOCATION (current.lhs, current.loc);
8100 /* If the binary operator required the use of an overloaded operator,
8101 then this expression cannot be an integral constant-expression.
8102 An overloaded operator can be used even if both operands are
8103 otherwise permissible in an integral constant-expression if at
8104 least one of the operands is of enumeration type. */
8106 if (overload
8107 && cp_parser_non_integral_constant_expression (parser,
8108 NIC_OVERLOADED))
8109 return error_mark_node;
8112 return current.lhs;
8115 static tree
8116 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8117 bool no_toplevel_fold_p,
8118 enum cp_parser_prec prec,
8119 cp_id_kind * pidk)
8121 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8122 /*decltype*/false, prec, pidk);
8125 /* Parse the `? expression : assignment-expression' part of a
8126 conditional-expression. The LOGICAL_OR_EXPR is the
8127 logical-or-expression that started the conditional-expression.
8128 Returns a representation of the entire conditional-expression.
8130 This routine is used by cp_parser_assignment_expression.
8132 ? expression : assignment-expression
8134 GNU Extensions:
8136 ? : assignment-expression */
8138 static tree
8139 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8141 tree expr;
8142 tree assignment_expr;
8143 struct cp_token *token;
8144 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8146 /* Consume the `?' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 token = cp_lexer_peek_token (parser->lexer);
8149 if (cp_parser_allow_gnu_extensions_p (parser)
8150 && token->type == CPP_COLON)
8152 pedwarn (token->location, OPT_Wpedantic,
8153 "ISO C++ does not allow ?: with omitted middle operand");
8154 /* Implicit true clause. */
8155 expr = NULL_TREE;
8156 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8157 warn_for_omitted_condop (token->location, logical_or_expr);
8159 else
8161 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8162 parser->colon_corrects_to_scope_p = false;
8163 /* Parse the expression. */
8164 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8165 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8166 c_inhibit_evaluation_warnings +=
8167 ((logical_or_expr == truthvalue_true_node)
8168 - (logical_or_expr == truthvalue_false_node));
8169 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8172 /* The next token should be a `:'. */
8173 cp_parser_require (parser, CPP_COLON, RT_COLON);
8174 /* Parse the assignment-expression. */
8175 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8176 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8178 /* Build the conditional-expression. */
8179 return build_x_conditional_expr (loc, logical_or_expr,
8180 expr,
8181 assignment_expr,
8182 tf_warning_or_error);
8185 /* Parse an assignment-expression.
8187 assignment-expression:
8188 conditional-expression
8189 logical-or-expression assignment-operator assignment_expression
8190 throw-expression
8192 CAST_P is true if this expression is the target of a cast.
8193 DECLTYPE_P is true if this expression is the operand of decltype.
8195 Returns a representation for the expression. */
8197 static tree
8198 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8199 bool decltype_p, cp_id_kind * pidk)
8201 tree expr;
8203 /* If the next token is the `throw' keyword, then we're looking at
8204 a throw-expression. */
8205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8206 expr = cp_parser_throw_expression (parser);
8207 /* Otherwise, it must be that we are looking at a
8208 logical-or-expression. */
8209 else
8211 /* Parse the binary expressions (logical-or-expression). */
8212 expr = cp_parser_binary_expression (parser, cast_p, false,
8213 decltype_p,
8214 PREC_NOT_OPERATOR, pidk);
8215 /* If the next token is a `?' then we're actually looking at a
8216 conditional-expression. */
8217 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8218 return cp_parser_question_colon_clause (parser, expr);
8219 else
8221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8223 /* If it's an assignment-operator, we're using the second
8224 production. */
8225 enum tree_code assignment_operator
8226 = cp_parser_assignment_operator_opt (parser);
8227 if (assignment_operator != ERROR_MARK)
8229 bool non_constant_p;
8230 location_t saved_input_location;
8232 /* Parse the right-hand side of the assignment. */
8233 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8235 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8236 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8238 /* An assignment may not appear in a
8239 constant-expression. */
8240 if (cp_parser_non_integral_constant_expression (parser,
8241 NIC_ASSIGNMENT))
8242 return error_mark_node;
8243 /* Build the assignment expression. Its default
8244 location is the location of the '=' token. */
8245 saved_input_location = input_location;
8246 input_location = loc;
8247 expr = build_x_modify_expr (loc, expr,
8248 assignment_operator,
8249 rhs,
8250 complain_flags (decltype_p));
8251 input_location = saved_input_location;
8256 return expr;
8259 static tree
8260 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8261 cp_id_kind * pidk)
8263 return cp_parser_assignment_expression (parser, cast_p,
8264 /*decltype*/false, pidk);
8267 /* Parse an (optional) assignment-operator.
8269 assignment-operator: one of
8270 = *= /= %= += -= >>= <<= &= ^= |=
8272 GNU Extension:
8274 assignment-operator: one of
8275 <?= >?=
8277 If the next token is an assignment operator, the corresponding tree
8278 code is returned, and the token is consumed. For example, for
8279 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8280 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8281 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8282 operator, ERROR_MARK is returned. */
8284 static enum tree_code
8285 cp_parser_assignment_operator_opt (cp_parser* parser)
8287 enum tree_code op;
8288 cp_token *token;
8290 /* Peek at the next token. */
8291 token = cp_lexer_peek_token (parser->lexer);
8293 switch (token->type)
8295 case CPP_EQ:
8296 op = NOP_EXPR;
8297 break;
8299 case CPP_MULT_EQ:
8300 op = MULT_EXPR;
8301 break;
8303 case CPP_DIV_EQ:
8304 op = TRUNC_DIV_EXPR;
8305 break;
8307 case CPP_MOD_EQ:
8308 op = TRUNC_MOD_EXPR;
8309 break;
8311 case CPP_PLUS_EQ:
8312 op = PLUS_EXPR;
8313 break;
8315 case CPP_MINUS_EQ:
8316 op = MINUS_EXPR;
8317 break;
8319 case CPP_RSHIFT_EQ:
8320 op = RSHIFT_EXPR;
8321 break;
8323 case CPP_LSHIFT_EQ:
8324 op = LSHIFT_EXPR;
8325 break;
8327 case CPP_AND_EQ:
8328 op = BIT_AND_EXPR;
8329 break;
8331 case CPP_XOR_EQ:
8332 op = BIT_XOR_EXPR;
8333 break;
8335 case CPP_OR_EQ:
8336 op = BIT_IOR_EXPR;
8337 break;
8339 default:
8340 /* Nothing else is an assignment operator. */
8341 op = ERROR_MARK;
8344 /* If it was an assignment operator, consume it. */
8345 if (op != ERROR_MARK)
8346 cp_lexer_consume_token (parser->lexer);
8348 return op;
8351 /* Parse an expression.
8353 expression:
8354 assignment-expression
8355 expression , assignment-expression
8357 CAST_P is true if this expression is the target of a cast.
8358 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8359 except possibly parenthesized or on the RHS of a comma (N3276).
8361 Returns a representation of the expression. */
8363 static tree
8364 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8365 cp_id_kind * pidk)
8367 tree expression = NULL_TREE;
8368 location_t loc = UNKNOWN_LOCATION;
8370 while (true)
8372 tree assignment_expression;
8374 /* Parse the next assignment-expression. */
8375 assignment_expression
8376 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8378 /* We don't create a temporary for a call that is the immediate operand
8379 of decltype or on the RHS of a comma. But when we see a comma, we
8380 need to create a temporary for a call on the LHS. */
8381 if (decltype_p && !processing_template_decl
8382 && TREE_CODE (assignment_expression) == CALL_EXPR
8383 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8384 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8385 assignment_expression
8386 = build_cplus_new (TREE_TYPE (assignment_expression),
8387 assignment_expression, tf_warning_or_error);
8389 /* If this is the first assignment-expression, we can just
8390 save it away. */
8391 if (!expression)
8392 expression = assignment_expression;
8393 else
8394 expression = build_x_compound_expr (loc, expression,
8395 assignment_expression,
8396 complain_flags (decltype_p));
8397 /* If the next token is not a comma, then we are done with the
8398 expression. */
8399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8400 break;
8401 /* Consume the `,'. */
8402 loc = cp_lexer_peek_token (parser->lexer)->location;
8403 cp_lexer_consume_token (parser->lexer);
8404 /* A comma operator cannot appear in a constant-expression. */
8405 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8406 expression = error_mark_node;
8409 return expression;
8412 static inline tree
8413 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8415 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8418 /* Parse a constant-expression.
8420 constant-expression:
8421 conditional-expression
8423 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8424 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8425 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8426 is false, NON_CONSTANT_P should be NULL. */
8428 static tree
8429 cp_parser_constant_expression (cp_parser* parser,
8430 bool allow_non_constant_p,
8431 bool *non_constant_p)
8433 bool saved_integral_constant_expression_p;
8434 bool saved_allow_non_integral_constant_expression_p;
8435 bool saved_non_integral_constant_expression_p;
8436 tree expression;
8438 /* It might seem that we could simply parse the
8439 conditional-expression, and then check to see if it were
8440 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8441 one that the compiler can figure out is constant, possibly after
8442 doing some simplifications or optimizations. The standard has a
8443 precise definition of constant-expression, and we must honor
8444 that, even though it is somewhat more restrictive.
8446 For example:
8448 int i[(2, 3)];
8450 is not a legal declaration, because `(2, 3)' is not a
8451 constant-expression. The `,' operator is forbidden in a
8452 constant-expression. However, GCC's constant-folding machinery
8453 will fold this operation to an INTEGER_CST for `3'. */
8455 /* Save the old settings. */
8456 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8457 saved_allow_non_integral_constant_expression_p
8458 = parser->allow_non_integral_constant_expression_p;
8459 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8460 /* We are now parsing a constant-expression. */
8461 parser->integral_constant_expression_p = true;
8462 parser->allow_non_integral_constant_expression_p
8463 = (allow_non_constant_p || cxx_dialect >= cxx11);
8464 parser->non_integral_constant_expression_p = false;
8465 /* Although the grammar says "conditional-expression", we parse an
8466 "assignment-expression", which also permits "throw-expression"
8467 and the use of assignment operators. In the case that
8468 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8469 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8470 actually essential that we look for an assignment-expression.
8471 For example, cp_parser_initializer_clauses uses this function to
8472 determine whether a particular assignment-expression is in fact
8473 constant. */
8474 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8475 /* Restore the old settings. */
8476 parser->integral_constant_expression_p
8477 = saved_integral_constant_expression_p;
8478 parser->allow_non_integral_constant_expression_p
8479 = saved_allow_non_integral_constant_expression_p;
8480 if (cxx_dialect >= cxx11)
8482 /* Require an rvalue constant expression here; that's what our
8483 callers expect. Reference constant expressions are handled
8484 separately in e.g. cp_parser_template_argument. */
8485 bool is_const = potential_rvalue_constant_expression (expression);
8486 parser->non_integral_constant_expression_p = !is_const;
8487 if (!is_const && !allow_non_constant_p)
8488 require_potential_rvalue_constant_expression (expression);
8490 if (allow_non_constant_p)
8491 *non_constant_p = parser->non_integral_constant_expression_p;
8492 parser->non_integral_constant_expression_p
8493 = saved_non_integral_constant_expression_p;
8495 return expression;
8498 /* Parse __builtin_offsetof.
8500 offsetof-expression:
8501 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8503 offsetof-member-designator:
8504 id-expression
8505 | offsetof-member-designator "." id-expression
8506 | offsetof-member-designator "[" expression "]"
8507 | offsetof-member-designator "->" id-expression */
8509 static tree
8510 cp_parser_builtin_offsetof (cp_parser *parser)
8512 int save_ice_p, save_non_ice_p;
8513 tree type, expr;
8514 cp_id_kind dummy;
8515 cp_token *token;
8517 /* We're about to accept non-integral-constant things, but will
8518 definitely yield an integral constant expression. Save and
8519 restore these values around our local parsing. */
8520 save_ice_p = parser->integral_constant_expression_p;
8521 save_non_ice_p = parser->non_integral_constant_expression_p;
8523 /* Consume the "__builtin_offsetof" token. */
8524 cp_lexer_consume_token (parser->lexer);
8525 /* Consume the opening `('. */
8526 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8527 /* Parse the type-id. */
8528 type = cp_parser_type_id (parser);
8529 /* Look for the `,'. */
8530 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8531 token = cp_lexer_peek_token (parser->lexer);
8533 /* Build the (type *)null that begins the traditional offsetof macro. */
8534 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8535 tf_warning_or_error);
8537 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8538 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8539 true, &dummy, token->location);
8540 while (true)
8542 token = cp_lexer_peek_token (parser->lexer);
8543 switch (token->type)
8545 case CPP_OPEN_SQUARE:
8546 /* offsetof-member-designator "[" expression "]" */
8547 expr = cp_parser_postfix_open_square_expression (parser, expr,
8548 true, false);
8549 break;
8551 case CPP_DEREF:
8552 /* offsetof-member-designator "->" identifier */
8553 expr = grok_array_decl (token->location, expr,
8554 integer_zero_node, false);
8555 /* FALLTHRU */
8557 case CPP_DOT:
8558 /* offsetof-member-designator "." identifier */
8559 cp_lexer_consume_token (parser->lexer);
8560 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8561 expr, true, &dummy,
8562 token->location);
8563 break;
8565 case CPP_CLOSE_PAREN:
8566 /* Consume the ")" token. */
8567 cp_lexer_consume_token (parser->lexer);
8568 goto success;
8570 default:
8571 /* Error. We know the following require will fail, but
8572 that gives the proper error message. */
8573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8574 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8575 expr = error_mark_node;
8576 goto failure;
8580 success:
8581 /* If we're processing a template, we can't finish the semantics yet.
8582 Otherwise we can fold the entire expression now. */
8583 if (processing_template_decl)
8584 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8585 else
8586 expr = finish_offsetof (expr);
8588 failure:
8589 parser->integral_constant_expression_p = save_ice_p;
8590 parser->non_integral_constant_expression_p = save_non_ice_p;
8592 return expr;
8595 /* Parse a trait expression.
8597 Returns a representation of the expression, the underlying type
8598 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8600 static tree
8601 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8603 cp_trait_kind kind;
8604 tree type1, type2 = NULL_TREE;
8605 bool binary = false;
8606 cp_decl_specifier_seq decl_specs;
8608 switch (keyword)
8610 case RID_HAS_NOTHROW_ASSIGN:
8611 kind = CPTK_HAS_NOTHROW_ASSIGN;
8612 break;
8613 case RID_HAS_NOTHROW_CONSTRUCTOR:
8614 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8615 break;
8616 case RID_HAS_NOTHROW_COPY:
8617 kind = CPTK_HAS_NOTHROW_COPY;
8618 break;
8619 case RID_HAS_TRIVIAL_ASSIGN:
8620 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8621 break;
8622 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8623 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8624 break;
8625 case RID_HAS_TRIVIAL_COPY:
8626 kind = CPTK_HAS_TRIVIAL_COPY;
8627 break;
8628 case RID_HAS_TRIVIAL_DESTRUCTOR:
8629 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8630 break;
8631 case RID_HAS_VIRTUAL_DESTRUCTOR:
8632 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8633 break;
8634 case RID_IS_ABSTRACT:
8635 kind = CPTK_IS_ABSTRACT;
8636 break;
8637 case RID_IS_BASE_OF:
8638 kind = CPTK_IS_BASE_OF;
8639 binary = true;
8640 break;
8641 case RID_IS_CLASS:
8642 kind = CPTK_IS_CLASS;
8643 break;
8644 case RID_IS_CONVERTIBLE_TO:
8645 kind = CPTK_IS_CONVERTIBLE_TO;
8646 binary = true;
8647 break;
8648 case RID_IS_EMPTY:
8649 kind = CPTK_IS_EMPTY;
8650 break;
8651 case RID_IS_ENUM:
8652 kind = CPTK_IS_ENUM;
8653 break;
8654 case RID_IS_FINAL:
8655 kind = CPTK_IS_FINAL;
8656 break;
8657 case RID_IS_LITERAL_TYPE:
8658 kind = CPTK_IS_LITERAL_TYPE;
8659 break;
8660 case RID_IS_POD:
8661 kind = CPTK_IS_POD;
8662 break;
8663 case RID_IS_POLYMORPHIC:
8664 kind = CPTK_IS_POLYMORPHIC;
8665 break;
8666 case RID_IS_SAME_AS:
8667 kind = CPTK_IS_SAME_AS;
8668 binary = true;
8669 break;
8670 case RID_IS_STD_LAYOUT:
8671 kind = CPTK_IS_STD_LAYOUT;
8672 break;
8673 case RID_IS_TRIVIAL:
8674 kind = CPTK_IS_TRIVIAL;
8675 break;
8676 case RID_IS_UNION:
8677 kind = CPTK_IS_UNION;
8678 break;
8679 case RID_UNDERLYING_TYPE:
8680 kind = CPTK_UNDERLYING_TYPE;
8681 break;
8682 case RID_BASES:
8683 kind = CPTK_BASES;
8684 break;
8685 case RID_DIRECT_BASES:
8686 kind = CPTK_DIRECT_BASES;
8687 break;
8688 default:
8689 gcc_unreachable ();
8692 /* Consume the token. */
8693 cp_lexer_consume_token (parser->lexer);
8695 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8697 type1 = cp_parser_type_id (parser);
8699 if (type1 == error_mark_node)
8700 return error_mark_node;
8702 /* Build a trivial decl-specifier-seq. */
8703 clear_decl_specs (&decl_specs);
8704 decl_specs.type = type1;
8706 /* Call grokdeclarator to figure out what type this is. */
8707 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8708 /*initialized=*/0, /*attrlist=*/NULL);
8710 if (binary)
8712 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8714 type2 = cp_parser_type_id (parser);
8716 if (type2 == error_mark_node)
8717 return error_mark_node;
8719 /* Build a trivial decl-specifier-seq. */
8720 clear_decl_specs (&decl_specs);
8721 decl_specs.type = type2;
8723 /* Call grokdeclarator to figure out what type this is. */
8724 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8725 /*initialized=*/0, /*attrlist=*/NULL);
8728 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8730 /* Complete the trait expression, which may mean either processing
8731 the trait expr now or saving it for template instantiation. */
8732 switch(kind)
8734 case CPTK_UNDERLYING_TYPE:
8735 return finish_underlying_type (type1);
8736 case CPTK_BASES:
8737 return finish_bases (type1, false);
8738 case CPTK_DIRECT_BASES:
8739 return finish_bases (type1, true);
8740 default:
8741 return finish_trait_expr (kind, type1, type2);
8745 /* Lambdas that appear in variable initializer or default argument scope
8746 get that in their mangling, so we need to record it. We might as well
8747 use the count for function and namespace scopes as well. */
8748 static GTY(()) tree lambda_scope;
8749 static GTY(()) int lambda_count;
8750 typedef struct GTY(()) tree_int
8752 tree t;
8753 int i;
8754 } tree_int;
8755 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8757 static void
8758 start_lambda_scope (tree decl)
8760 tree_int ti;
8761 gcc_assert (decl);
8762 /* Once we're inside a function, we ignore other scopes and just push
8763 the function again so that popping works properly. */
8764 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8765 decl = current_function_decl;
8766 ti.t = lambda_scope;
8767 ti.i = lambda_count;
8768 vec_safe_push (lambda_scope_stack, ti);
8769 if (lambda_scope != decl)
8771 /* Don't reset the count if we're still in the same function. */
8772 lambda_scope = decl;
8773 lambda_count = 0;
8777 static void
8778 record_lambda_scope (tree lambda)
8780 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8781 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8784 static void
8785 finish_lambda_scope (void)
8787 tree_int *p = &lambda_scope_stack->last ();
8788 if (lambda_scope != p->t)
8790 lambda_scope = p->t;
8791 lambda_count = p->i;
8793 lambda_scope_stack->pop ();
8796 /* Parse a lambda expression.
8798 lambda-expression:
8799 lambda-introducer lambda-declarator [opt] compound-statement
8801 Returns a representation of the expression. */
8803 static tree
8804 cp_parser_lambda_expression (cp_parser* parser)
8806 tree lambda_expr = build_lambda_expr ();
8807 tree type;
8808 bool ok = true;
8809 cp_token *token = cp_lexer_peek_token (parser->lexer);
8811 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8813 if (cp_unevaluated_operand)
8815 if (!token->error_reported)
8817 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8818 "lambda-expression in unevaluated context");
8819 token->error_reported = true;
8821 ok = false;
8824 /* We may be in the middle of deferred access check. Disable
8825 it now. */
8826 push_deferring_access_checks (dk_no_deferred);
8828 cp_parser_lambda_introducer (parser, lambda_expr);
8830 type = begin_lambda_type (lambda_expr);
8831 if (type == error_mark_node)
8832 return error_mark_node;
8834 record_lambda_scope (lambda_expr);
8836 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8837 determine_visibility (TYPE_NAME (type));
8839 /* Now that we've started the type, add the capture fields for any
8840 explicit captures. */
8841 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8844 /* Inside the class, surrounding template-parameter-lists do not apply. */
8845 unsigned int saved_num_template_parameter_lists
8846 = parser->num_template_parameter_lists;
8847 unsigned char in_statement = parser->in_statement;
8848 bool in_switch_statement_p = parser->in_switch_statement_p;
8849 bool fully_implicit_function_template_p
8850 = parser->fully_implicit_function_template_p;
8851 tree implicit_template_parms = parser->implicit_template_parms;
8852 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8853 bool auto_is_implicit_function_template_parm_p
8854 = parser->auto_is_implicit_function_template_parm_p;
8856 parser->num_template_parameter_lists = 0;
8857 parser->in_statement = 0;
8858 parser->in_switch_statement_p = false;
8859 parser->fully_implicit_function_template_p = false;
8860 parser->implicit_template_parms = 0;
8861 parser->implicit_template_scope = 0;
8862 parser->auto_is_implicit_function_template_parm_p = false;
8864 /* By virtue of defining a local class, a lambda expression has access to
8865 the private variables of enclosing classes. */
8867 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8869 if (ok)
8870 cp_parser_lambda_body (parser, lambda_expr);
8871 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8873 if (cp_parser_skip_to_closing_brace (parser))
8874 cp_lexer_consume_token (parser->lexer);
8877 /* The capture list was built up in reverse order; fix that now. */
8878 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8879 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8881 if (ok)
8882 maybe_add_lambda_conv_op (type);
8884 type = finish_struct (type, /*attributes=*/NULL_TREE);
8886 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8887 parser->in_statement = in_statement;
8888 parser->in_switch_statement_p = in_switch_statement_p;
8889 parser->fully_implicit_function_template_p
8890 = fully_implicit_function_template_p;
8891 parser->implicit_template_parms = implicit_template_parms;
8892 parser->implicit_template_scope = implicit_template_scope;
8893 parser->auto_is_implicit_function_template_parm_p
8894 = auto_is_implicit_function_template_parm_p;
8897 pop_deferring_access_checks ();
8899 /* This field is only used during parsing of the lambda. */
8900 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8902 /* This lambda shouldn't have any proxies left at this point. */
8903 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8904 /* And now that we're done, push proxies for an enclosing lambda. */
8905 insert_pending_capture_proxies ();
8907 if (ok)
8908 return build_lambda_object (lambda_expr);
8909 else
8910 return error_mark_node;
8913 /* Parse the beginning of a lambda expression.
8915 lambda-introducer:
8916 [ lambda-capture [opt] ]
8918 LAMBDA_EXPR is the current representation of the lambda expression. */
8920 static void
8921 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8923 /* Need commas after the first capture. */
8924 bool first = true;
8926 /* Eat the leading `['. */
8927 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8929 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8930 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8931 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8932 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8933 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8934 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8936 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8938 cp_lexer_consume_token (parser->lexer);
8939 first = false;
8942 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8944 cp_token* capture_token;
8945 tree capture_id;
8946 tree capture_init_expr;
8947 cp_id_kind idk = CP_ID_KIND_NONE;
8948 bool explicit_init_p = false;
8950 enum capture_kind_type
8952 BY_COPY,
8953 BY_REFERENCE
8955 enum capture_kind_type capture_kind = BY_COPY;
8957 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8959 error ("expected end of capture-list");
8960 return;
8963 if (first)
8964 first = false;
8965 else
8966 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8968 /* Possibly capture `this'. */
8969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8971 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8972 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8973 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8974 "with by-copy capture default");
8975 cp_lexer_consume_token (parser->lexer);
8976 add_capture (lambda_expr,
8977 /*id=*/this_identifier,
8978 /*initializer=*/finish_this_expr(),
8979 /*by_reference_p=*/false,
8980 explicit_init_p);
8981 continue;
8984 /* Remember whether we want to capture as a reference or not. */
8985 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8987 capture_kind = BY_REFERENCE;
8988 cp_lexer_consume_token (parser->lexer);
8991 /* Get the identifier. */
8992 capture_token = cp_lexer_peek_token (parser->lexer);
8993 capture_id = cp_parser_identifier (parser);
8995 if (capture_id == error_mark_node)
8996 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8997 delimiters, but I modified this to stop on unnested ']' as well. It
8998 was already changed to stop on unnested '}', so the
8999 "closing_parenthesis" name is no more misleading with my change. */
9001 cp_parser_skip_to_closing_parenthesis (parser,
9002 /*recovering=*/true,
9003 /*or_comma=*/true,
9004 /*consume_paren=*/true);
9005 break;
9008 /* Find the initializer for this capture. */
9009 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9010 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9011 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9013 bool direct, non_constant;
9014 /* An explicit initializer exists. */
9015 if (cxx_dialect < cxx1y)
9016 pedwarn (input_location, 0,
9017 "lambda capture initializers "
9018 "only available with -std=c++1y or -std=gnu++1y");
9019 capture_init_expr = cp_parser_initializer (parser, &direct,
9020 &non_constant);
9021 explicit_init_p = true;
9022 if (capture_init_expr == NULL_TREE)
9024 error ("empty initializer for lambda init-capture");
9025 capture_init_expr = error_mark_node;
9028 else
9030 const char* error_msg;
9032 /* Turn the identifier into an id-expression. */
9033 capture_init_expr
9034 = cp_parser_lookup_name_simple (parser, capture_id,
9035 capture_token->location);
9037 if (capture_init_expr == error_mark_node)
9039 unqualified_name_lookup_error (capture_id);
9040 continue;
9042 else if (DECL_P (capture_init_expr)
9043 && (!VAR_P (capture_init_expr)
9044 && TREE_CODE (capture_init_expr) != PARM_DECL))
9046 error_at (capture_token->location,
9047 "capture of non-variable %qD ",
9048 capture_init_expr);
9049 inform (0, "%q+#D declared here", capture_init_expr);
9050 continue;
9052 if (VAR_P (capture_init_expr)
9053 && decl_storage_duration (capture_init_expr) != dk_auto)
9055 if (pedwarn (capture_token->location, 0, "capture of variable "
9056 "%qD with non-automatic storage duration",
9057 capture_init_expr))
9058 inform (0, "%q+#D declared here", capture_init_expr);
9059 continue;
9062 capture_init_expr
9063 = finish_id_expression
9064 (capture_id,
9065 capture_init_expr,
9066 parser->scope,
9067 &idk,
9068 /*integral_constant_expression_p=*/false,
9069 /*allow_non_integral_constant_expression_p=*/false,
9070 /*non_integral_constant_expression_p=*/NULL,
9071 /*template_p=*/false,
9072 /*done=*/true,
9073 /*address_p=*/false,
9074 /*template_arg_p=*/false,
9075 &error_msg,
9076 capture_token->location);
9078 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9080 cp_lexer_consume_token (parser->lexer);
9081 capture_init_expr = make_pack_expansion (capture_init_expr);
9083 else
9084 check_for_bare_parameter_packs (capture_init_expr);
9087 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9088 && !explicit_init_p)
9090 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9091 && capture_kind == BY_COPY)
9092 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9093 "of %qD redundant with by-copy capture default",
9094 capture_id);
9095 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9096 && capture_kind == BY_REFERENCE)
9097 pedwarn (capture_token->location, 0, "explicit by-reference "
9098 "capture of %qD redundant with by-reference capture "
9099 "default", capture_id);
9102 add_capture (lambda_expr,
9103 capture_id,
9104 capture_init_expr,
9105 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9106 explicit_init_p);
9109 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9112 /* Parse the (optional) middle of a lambda expression.
9114 lambda-declarator:
9115 < template-parameter-list [opt] >
9116 ( parameter-declaration-clause [opt] )
9117 attribute-specifier [opt]
9118 mutable [opt]
9119 exception-specification [opt]
9120 lambda-return-type-clause [opt]
9122 LAMBDA_EXPR is the current representation of the lambda expression. */
9124 static bool
9125 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9127 /* 5.1.1.4 of the standard says:
9128 If a lambda-expression does not include a lambda-declarator, it is as if
9129 the lambda-declarator were ().
9130 This means an empty parameter list, no attributes, and no exception
9131 specification. */
9132 tree param_list = void_list_node;
9133 tree attributes = NULL_TREE;
9134 tree exception_spec = NULL_TREE;
9135 tree template_param_list = NULL_TREE;
9137 /* The template-parameter-list is optional, but must begin with
9138 an opening angle if present. */
9139 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9141 if (cxx_dialect < cxx1y)
9142 pedwarn (parser->lexer->next_token->location, 0,
9143 "lambda templates are only available with "
9144 "-std=c++1y or -std=gnu++1y");
9146 cp_lexer_consume_token (parser->lexer);
9148 template_param_list = cp_parser_template_parameter_list (parser);
9150 cp_parser_skip_to_end_of_template_parameter_list (parser);
9152 /* We just processed one more parameter list. */
9153 ++parser->num_template_parameter_lists;
9156 /* The parameter-declaration-clause is optional (unless
9157 template-parameter-list was given), but must begin with an
9158 opening parenthesis if present. */
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9161 cp_lexer_consume_token (parser->lexer);
9163 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9165 /* Parse parameters. */
9166 param_list = cp_parser_parameter_declaration_clause (parser);
9168 /* Default arguments shall not be specified in the
9169 parameter-declaration-clause of a lambda-declarator. */
9170 for (tree t = param_list; t; t = TREE_CHAIN (t))
9171 if (TREE_PURPOSE (t))
9172 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9173 "default argument specified for lambda parameter");
9175 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9177 attributes = cp_parser_attributes_opt (parser);
9179 /* Parse optional `mutable' keyword. */
9180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9182 cp_lexer_consume_token (parser->lexer);
9183 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9186 /* Parse optional exception specification. */
9187 exception_spec = cp_parser_exception_specification_opt (parser);
9189 /* Parse optional trailing return type. */
9190 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9192 cp_lexer_consume_token (parser->lexer);
9193 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9194 = cp_parser_trailing_type_id (parser);
9197 /* The function parameters must be in scope all the way until after the
9198 trailing-return-type in case of decltype. */
9199 pop_bindings_and_leave_scope ();
9201 else if (template_param_list != NULL_TREE) // generate diagnostic
9202 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9204 /* Create the function call operator.
9206 Messing with declarators like this is no uglier than building up the
9207 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9208 other code. */
9210 cp_decl_specifier_seq return_type_specs;
9211 cp_declarator* declarator;
9212 tree fco;
9213 int quals;
9214 void *p;
9216 clear_decl_specs (&return_type_specs);
9217 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9218 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9219 else
9220 /* Maybe we will deduce the return type later. */
9221 return_type_specs.type = make_auto ();
9223 p = obstack_alloc (&declarator_obstack, 0);
9225 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9226 sfk_none);
9228 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9229 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9230 declarator = make_call_declarator (declarator, param_list, quals,
9231 VIRT_SPEC_UNSPECIFIED,
9232 REF_QUAL_NONE,
9233 exception_spec,
9234 /*late_return_type=*/NULL_TREE);
9235 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9237 fco = grokmethod (&return_type_specs,
9238 declarator,
9239 attributes);
9240 if (fco != error_mark_node)
9242 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9243 DECL_ARTIFICIAL (fco) = 1;
9244 /* Give the object parameter a different name. */
9245 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9246 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9247 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9249 if (template_param_list)
9251 fco = finish_member_template_decl (fco);
9252 finish_template_decl (template_param_list);
9253 --parser->num_template_parameter_lists;
9255 else if (parser->fully_implicit_function_template_p)
9256 fco = finish_fully_implicit_template (parser, fco);
9258 finish_member_declaration (fco);
9260 obstack_free (&declarator_obstack, p);
9262 return (fco != error_mark_node);
9266 /* Parse the body of a lambda expression, which is simply
9268 compound-statement
9270 but which requires special handling.
9271 LAMBDA_EXPR is the current representation of the lambda expression. */
9273 static void
9274 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9276 bool nested = (current_function_decl != NULL_TREE);
9277 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9278 if (nested)
9279 push_function_context ();
9280 else
9281 /* Still increment function_depth so that we don't GC in the
9282 middle of an expression. */
9283 ++function_depth;
9284 /* Clear this in case we're in the middle of a default argument. */
9285 parser->local_variables_forbidden_p = false;
9287 /* Finish the function call operator
9288 - class_specifier
9289 + late_parsing_for_member
9290 + function_definition_after_declarator
9291 + ctor_initializer_opt_and_function_body */
9293 tree fco = lambda_function (lambda_expr);
9294 tree body;
9295 bool done = false;
9296 tree compound_stmt;
9297 tree cap;
9299 /* Let the front end know that we are going to be defining this
9300 function. */
9301 start_preparsed_function (fco,
9302 NULL_TREE,
9303 SF_PRE_PARSED | SF_INCLASS_INLINE);
9305 start_lambda_scope (fco);
9306 body = begin_function_body ();
9308 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9309 goto out;
9311 /* Push the proxies for any explicit captures. */
9312 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9313 cap = TREE_CHAIN (cap))
9314 build_capture_proxy (TREE_PURPOSE (cap));
9316 compound_stmt = begin_compound_stmt (0);
9318 /* 5.1.1.4 of the standard says:
9319 If a lambda-expression does not include a trailing-return-type, it
9320 is as if the trailing-return-type denotes the following type:
9321 * if the compound-statement is of the form
9322 { return attribute-specifier [opt] expression ; }
9323 the type of the returned expression after lvalue-to-rvalue
9324 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9325 (_conv.array_ 4.2), and function-to-pointer conversion
9326 (_conv.func_ 4.3);
9327 * otherwise, void. */
9329 /* In a lambda that has neither a lambda-return-type-clause
9330 nor a deducible form, errors should be reported for return statements
9331 in the body. Since we used void as the placeholder return type, parsing
9332 the body as usual will give such desired behavior. */
9333 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9334 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9335 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9337 tree expr = NULL_TREE;
9338 cp_id_kind idk = CP_ID_KIND_NONE;
9340 /* Parse tentatively in case there's more after the initial return
9341 statement. */
9342 cp_parser_parse_tentatively (parser);
9344 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9346 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9349 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9351 if (cp_parser_parse_definitely (parser))
9353 if (!processing_template_decl)
9354 apply_deduced_return_type (fco, lambda_return_type (expr));
9356 /* Will get error here if type not deduced yet. */
9357 finish_return_stmt (expr);
9359 done = true;
9363 if (!done)
9365 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9366 cp_parser_label_declaration (parser);
9367 cp_parser_statement_seq_opt (parser, NULL_TREE);
9368 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9371 finish_compound_stmt (compound_stmt);
9373 out:
9374 finish_function_body (body);
9375 finish_lambda_scope ();
9377 /* Finish the function and generate code for it if necessary. */
9378 tree fn = finish_function (/*inline*/2);
9380 /* Only expand if the call op is not a template. */
9381 if (!DECL_TEMPLATE_INFO (fco))
9382 expand_or_defer_fn (fn);
9385 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9386 if (nested)
9387 pop_function_context();
9388 else
9389 --function_depth;
9392 /* Statements [gram.stmt.stmt] */
9394 /* Parse a statement.
9396 statement:
9397 labeled-statement
9398 expression-statement
9399 compound-statement
9400 selection-statement
9401 iteration-statement
9402 jump-statement
9403 declaration-statement
9404 try-block
9406 C++11:
9408 statement:
9409 labeled-statement
9410 attribute-specifier-seq (opt) expression-statement
9411 attribute-specifier-seq (opt) compound-statement
9412 attribute-specifier-seq (opt) selection-statement
9413 attribute-specifier-seq (opt) iteration-statement
9414 attribute-specifier-seq (opt) jump-statement
9415 declaration-statement
9416 attribute-specifier-seq (opt) try-block
9418 TM Extension:
9420 statement:
9421 atomic-statement
9423 IN_COMPOUND is true when the statement is nested inside a
9424 cp_parser_compound_statement; this matters for certain pragmas.
9426 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9427 is a (possibly labeled) if statement which is not enclosed in braces
9428 and has an else clause. This is used to implement -Wparentheses. */
9430 static void
9431 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9432 bool in_compound, bool *if_p)
9434 tree statement, std_attrs = NULL_TREE;
9435 cp_token *token;
9436 location_t statement_location, attrs_location;
9438 restart:
9439 if (if_p != NULL)
9440 *if_p = false;
9441 /* There is no statement yet. */
9442 statement = NULL_TREE;
9444 cp_lexer_save_tokens (parser->lexer);
9445 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9446 if (c_dialect_objc ())
9447 /* In obj-c++, seeing '[[' might be the either the beginning of
9448 c++11 attributes, or a nested objc-message-expression. So
9449 let's parse the c++11 attributes tentatively. */
9450 cp_parser_parse_tentatively (parser);
9451 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9452 if (c_dialect_objc ())
9454 if (!cp_parser_parse_definitely (parser))
9455 std_attrs = NULL_TREE;
9458 /* Peek at the next token. */
9459 token = cp_lexer_peek_token (parser->lexer);
9460 /* Remember the location of the first token in the statement. */
9461 statement_location = token->location;
9462 /* If this is a keyword, then that will often determine what kind of
9463 statement we have. */
9464 if (token->type == CPP_KEYWORD)
9466 enum rid keyword = token->keyword;
9468 switch (keyword)
9470 case RID_CASE:
9471 case RID_DEFAULT:
9472 /* Looks like a labeled-statement with a case label.
9473 Parse the label, and then use tail recursion to parse
9474 the statement. */
9475 cp_parser_label_for_labeled_statement (parser, std_attrs);
9476 goto restart;
9478 case RID_IF:
9479 case RID_SWITCH:
9480 statement = cp_parser_selection_statement (parser, if_p);
9481 break;
9483 case RID_WHILE:
9484 case RID_DO:
9485 case RID_FOR:
9486 statement = cp_parser_iteration_statement (parser, false);
9487 break;
9489 case RID_BREAK:
9490 case RID_CONTINUE:
9491 case RID_RETURN:
9492 case RID_GOTO:
9493 statement = cp_parser_jump_statement (parser);
9494 break;
9496 case RID_CILK_SYNC:
9497 cp_lexer_consume_token (parser->lexer);
9498 if (flag_cilkplus)
9500 tree sync_expr = build_cilk_sync ();
9501 SET_EXPR_LOCATION (sync_expr,
9502 token->location);
9503 statement = finish_expr_stmt (sync_expr);
9505 else
9507 error_at (token->location, "-fcilkplus must be enabled to use"
9508 " %<_Cilk_sync%>");
9509 statement = error_mark_node;
9511 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9512 break;
9514 /* Objective-C++ exception-handling constructs. */
9515 case RID_AT_TRY:
9516 case RID_AT_CATCH:
9517 case RID_AT_FINALLY:
9518 case RID_AT_SYNCHRONIZED:
9519 case RID_AT_THROW:
9520 statement = cp_parser_objc_statement (parser);
9521 break;
9523 case RID_TRY:
9524 statement = cp_parser_try_block (parser);
9525 break;
9527 case RID_NAMESPACE:
9528 /* This must be a namespace alias definition. */
9529 cp_parser_declaration_statement (parser);
9530 return;
9532 case RID_TRANSACTION_ATOMIC:
9533 case RID_TRANSACTION_RELAXED:
9534 statement = cp_parser_transaction (parser, keyword);
9535 break;
9536 case RID_TRANSACTION_CANCEL:
9537 statement = cp_parser_transaction_cancel (parser);
9538 break;
9540 default:
9541 /* It might be a keyword like `int' that can start a
9542 declaration-statement. */
9543 break;
9546 else if (token->type == CPP_NAME)
9548 /* If the next token is a `:', then we are looking at a
9549 labeled-statement. */
9550 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9551 if (token->type == CPP_COLON)
9553 /* Looks like a labeled-statement with an ordinary label.
9554 Parse the label, and then use tail recursion to parse
9555 the statement. */
9557 cp_parser_label_for_labeled_statement (parser, std_attrs);
9558 goto restart;
9561 /* Anything that starts with a `{' must be a compound-statement. */
9562 else if (token->type == CPP_OPEN_BRACE)
9563 statement = cp_parser_compound_statement (parser, NULL, false, false);
9564 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9565 a statement all its own. */
9566 else if (token->type == CPP_PRAGMA)
9568 /* Only certain OpenMP pragmas are attached to statements, and thus
9569 are considered statements themselves. All others are not. In
9570 the context of a compound, accept the pragma as a "statement" and
9571 return so that we can check for a close brace. Otherwise we
9572 require a real statement and must go back and read one. */
9573 if (in_compound)
9574 cp_parser_pragma (parser, pragma_compound);
9575 else if (!cp_parser_pragma (parser, pragma_stmt))
9576 goto restart;
9577 return;
9579 else if (token->type == CPP_EOF)
9581 cp_parser_error (parser, "expected statement");
9582 return;
9585 /* Everything else must be a declaration-statement or an
9586 expression-statement. Try for the declaration-statement
9587 first, unless we are looking at a `;', in which case we know that
9588 we have an expression-statement. */
9589 if (!statement)
9591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9593 if (std_attrs != NULL_TREE)
9595 /* Attributes should be parsed as part of the the
9596 declaration, so let's un-parse them. */
9597 cp_lexer_rollback_tokens (parser->lexer);
9598 std_attrs = NULL_TREE;
9601 cp_parser_parse_tentatively (parser);
9602 /* Try to parse the declaration-statement. */
9603 cp_parser_declaration_statement (parser);
9604 /* If that worked, we're done. */
9605 if (cp_parser_parse_definitely (parser))
9606 return;
9608 /* Look for an expression-statement instead. */
9609 statement = cp_parser_expression_statement (parser, in_statement_expr);
9612 /* Set the line number for the statement. */
9613 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9614 SET_EXPR_LOCATION (statement, statement_location);
9616 /* Note that for now, we don't do anything with c++11 statements
9617 parsed at this level. */
9618 if (std_attrs != NULL_TREE)
9619 warning_at (attrs_location,
9620 OPT_Wattributes,
9621 "attributes at the beginning of statement are ignored");
9624 /* Parse the label for a labeled-statement, i.e.
9626 identifier :
9627 case constant-expression :
9628 default :
9630 GNU Extension:
9631 case constant-expression ... constant-expression : statement
9633 When a label is parsed without errors, the label is added to the
9634 parse tree by the finish_* functions, so this function doesn't
9635 have to return the label. */
9637 static void
9638 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9640 cp_token *token;
9641 tree label = NULL_TREE;
9642 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9644 /* The next token should be an identifier. */
9645 token = cp_lexer_peek_token (parser->lexer);
9646 if (token->type != CPP_NAME
9647 && token->type != CPP_KEYWORD)
9649 cp_parser_error (parser, "expected labeled-statement");
9650 return;
9653 parser->colon_corrects_to_scope_p = false;
9654 switch (token->keyword)
9656 case RID_CASE:
9658 tree expr, expr_hi;
9659 cp_token *ellipsis;
9661 /* Consume the `case' token. */
9662 cp_lexer_consume_token (parser->lexer);
9663 /* Parse the constant-expression. */
9664 expr = cp_parser_constant_expression (parser,
9665 /*allow_non_constant_p=*/false,
9666 NULL);
9668 ellipsis = cp_lexer_peek_token (parser->lexer);
9669 if (ellipsis->type == CPP_ELLIPSIS)
9671 /* Consume the `...' token. */
9672 cp_lexer_consume_token (parser->lexer);
9673 expr_hi =
9674 cp_parser_constant_expression (parser,
9675 /*allow_non_constant_p=*/false,
9676 NULL);
9677 /* We don't need to emit warnings here, as the common code
9678 will do this for us. */
9680 else
9681 expr_hi = NULL_TREE;
9683 if (parser->in_switch_statement_p)
9684 finish_case_label (token->location, expr, expr_hi);
9685 else
9686 error_at (token->location,
9687 "case label %qE not within a switch statement",
9688 expr);
9690 break;
9692 case RID_DEFAULT:
9693 /* Consume the `default' token. */
9694 cp_lexer_consume_token (parser->lexer);
9696 if (parser->in_switch_statement_p)
9697 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9698 else
9699 error_at (token->location, "case label not within a switch statement");
9700 break;
9702 default:
9703 /* Anything else must be an ordinary label. */
9704 label = finish_label_stmt (cp_parser_identifier (parser));
9705 break;
9708 /* Require the `:' token. */
9709 cp_parser_require (parser, CPP_COLON, RT_COLON);
9711 /* An ordinary label may optionally be followed by attributes.
9712 However, this is only permitted if the attributes are then
9713 followed by a semicolon. This is because, for backward
9714 compatibility, when parsing
9715 lab: __attribute__ ((unused)) int i;
9716 we want the attribute to attach to "i", not "lab". */
9717 if (label != NULL_TREE
9718 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9720 tree attrs;
9721 cp_parser_parse_tentatively (parser);
9722 attrs = cp_parser_gnu_attributes_opt (parser);
9723 if (attrs == NULL_TREE
9724 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9725 cp_parser_abort_tentative_parse (parser);
9726 else if (!cp_parser_parse_definitely (parser))
9728 else
9729 attributes = chainon (attributes, attrs);
9732 if (attributes != NULL_TREE)
9733 cplus_decl_attributes (&label, attributes, 0);
9735 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9738 /* Parse an expression-statement.
9740 expression-statement:
9741 expression [opt] ;
9743 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9744 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9745 indicates whether this expression-statement is part of an
9746 expression statement. */
9748 static tree
9749 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9751 tree statement = NULL_TREE;
9752 cp_token *token = cp_lexer_peek_token (parser->lexer);
9754 /* If the next token is a ';', then there is no expression
9755 statement. */
9756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9758 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9759 if (statement == error_mark_node
9760 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9762 cp_parser_skip_to_end_of_block_or_statement (parser);
9763 return error_mark_node;
9767 /* Give a helpful message for "A<T>::type t;" and the like. */
9768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9769 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9771 if (TREE_CODE (statement) == SCOPE_REF)
9772 error_at (token->location, "need %<typename%> before %qE because "
9773 "%qT is a dependent scope",
9774 statement, TREE_OPERAND (statement, 0));
9775 else if (is_overloaded_fn (statement)
9776 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9778 /* A::A a; */
9779 tree fn = get_first_fn (statement);
9780 error_at (token->location,
9781 "%<%T::%D%> names the constructor, not the type",
9782 DECL_CONTEXT (fn), DECL_NAME (fn));
9786 /* Consume the final `;'. */
9787 cp_parser_consume_semicolon_at_end_of_statement (parser);
9789 if (in_statement_expr
9790 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9791 /* This is the final expression statement of a statement
9792 expression. */
9793 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9794 else if (statement)
9795 statement = finish_expr_stmt (statement);
9797 return statement;
9800 /* Parse a compound-statement.
9802 compound-statement:
9803 { statement-seq [opt] }
9805 GNU extension:
9807 compound-statement:
9808 { label-declaration-seq [opt] statement-seq [opt] }
9810 label-declaration-seq:
9811 label-declaration
9812 label-declaration-seq label-declaration
9814 Returns a tree representing the statement. */
9816 static tree
9817 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9818 bool in_try, bool function_body)
9820 tree compound_stmt;
9822 /* Consume the `{'. */
9823 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9824 return error_mark_node;
9825 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9826 && !function_body)
9827 pedwarn (input_location, OPT_Wpedantic,
9828 "compound-statement in constexpr function");
9829 /* Begin the compound-statement. */
9830 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9831 /* If the next keyword is `__label__' we have a label declaration. */
9832 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9833 cp_parser_label_declaration (parser);
9834 /* Parse an (optional) statement-seq. */
9835 cp_parser_statement_seq_opt (parser, in_statement_expr);
9836 /* Finish the compound-statement. */
9837 finish_compound_stmt (compound_stmt);
9838 /* Consume the `}'. */
9839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9841 return compound_stmt;
9844 /* Parse an (optional) statement-seq.
9846 statement-seq:
9847 statement
9848 statement-seq [opt] statement */
9850 static void
9851 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9853 /* Scan statements until there aren't any more. */
9854 while (true)
9856 cp_token *token = cp_lexer_peek_token (parser->lexer);
9858 /* If we are looking at a `}', then we have run out of
9859 statements; the same is true if we have reached the end
9860 of file, or have stumbled upon a stray '@end'. */
9861 if (token->type == CPP_CLOSE_BRACE
9862 || token->type == CPP_EOF
9863 || token->type == CPP_PRAGMA_EOL
9864 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9865 break;
9867 /* If we are in a compound statement and find 'else' then
9868 something went wrong. */
9869 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9871 if (parser->in_statement & IN_IF_STMT)
9872 break;
9873 else
9875 token = cp_lexer_consume_token (parser->lexer);
9876 error_at (token->location, "%<else%> without a previous %<if%>");
9880 /* Parse the statement. */
9881 cp_parser_statement (parser, in_statement_expr, true, NULL);
9885 /* Parse a selection-statement.
9887 selection-statement:
9888 if ( condition ) statement
9889 if ( condition ) statement else statement
9890 switch ( condition ) statement
9892 Returns the new IF_STMT or SWITCH_STMT.
9894 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9895 is a (possibly labeled) if statement which is not enclosed in
9896 braces and has an else clause. This is used to implement
9897 -Wparentheses. */
9899 static tree
9900 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9902 cp_token *token;
9903 enum rid keyword;
9905 if (if_p != NULL)
9906 *if_p = false;
9908 /* Peek at the next token. */
9909 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9911 /* See what kind of keyword it is. */
9912 keyword = token->keyword;
9913 switch (keyword)
9915 case RID_IF:
9916 case RID_SWITCH:
9918 tree statement;
9919 tree condition;
9921 /* Look for the `('. */
9922 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9924 cp_parser_skip_to_end_of_statement (parser);
9925 return error_mark_node;
9928 /* Begin the selection-statement. */
9929 if (keyword == RID_IF)
9930 statement = begin_if_stmt ();
9931 else
9932 statement = begin_switch_stmt ();
9934 /* Parse the condition. */
9935 condition = cp_parser_condition (parser);
9936 /* Look for the `)'. */
9937 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9938 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9939 /*consume_paren=*/true);
9941 if (keyword == RID_IF)
9943 bool nested_if;
9944 unsigned char in_statement;
9946 /* Add the condition. */
9947 finish_if_stmt_cond (condition, statement);
9949 /* Parse the then-clause. */
9950 in_statement = parser->in_statement;
9951 parser->in_statement |= IN_IF_STMT;
9952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9954 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9955 add_stmt (build_empty_stmt (loc));
9956 cp_lexer_consume_token (parser->lexer);
9957 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9958 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9959 "empty body in an %<if%> statement");
9960 nested_if = false;
9962 else
9963 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9964 parser->in_statement = in_statement;
9966 finish_then_clause (statement);
9968 /* If the next token is `else', parse the else-clause. */
9969 if (cp_lexer_next_token_is_keyword (parser->lexer,
9970 RID_ELSE))
9972 /* Consume the `else' keyword. */
9973 cp_lexer_consume_token (parser->lexer);
9974 begin_else_clause (statement);
9975 /* Parse the else-clause. */
9976 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9978 location_t loc;
9979 loc = cp_lexer_peek_token (parser->lexer)->location;
9980 warning_at (loc,
9981 OPT_Wempty_body, "suggest braces around "
9982 "empty body in an %<else%> statement");
9983 add_stmt (build_empty_stmt (loc));
9984 cp_lexer_consume_token (parser->lexer);
9986 else
9987 cp_parser_implicitly_scoped_statement (parser, NULL);
9989 finish_else_clause (statement);
9991 /* If we are currently parsing a then-clause, then
9992 IF_P will not be NULL. We set it to true to
9993 indicate that this if statement has an else clause.
9994 This may trigger the Wparentheses warning below
9995 when we get back up to the parent if statement. */
9996 if (if_p != NULL)
9997 *if_p = true;
9999 else
10001 /* This if statement does not have an else clause. If
10002 NESTED_IF is true, then the then-clause is an if
10003 statement which does have an else clause. We warn
10004 about the potential ambiguity. */
10005 if (nested_if)
10006 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10007 "suggest explicit braces to avoid ambiguous"
10008 " %<else%>");
10011 /* Now we're all done with the if-statement. */
10012 finish_if_stmt (statement);
10014 else
10016 bool in_switch_statement_p;
10017 unsigned char in_statement;
10019 /* Add the condition. */
10020 finish_switch_cond (condition, statement);
10022 /* Parse the body of the switch-statement. */
10023 in_switch_statement_p = parser->in_switch_statement_p;
10024 in_statement = parser->in_statement;
10025 parser->in_switch_statement_p = true;
10026 parser->in_statement |= IN_SWITCH_STMT;
10027 cp_parser_implicitly_scoped_statement (parser, NULL);
10028 parser->in_switch_statement_p = in_switch_statement_p;
10029 parser->in_statement = in_statement;
10031 /* Now we're all done with the switch-statement. */
10032 finish_switch_stmt (statement);
10035 return statement;
10037 break;
10039 default:
10040 cp_parser_error (parser, "expected selection-statement");
10041 return error_mark_node;
10045 /* Parse a condition.
10047 condition:
10048 expression
10049 type-specifier-seq declarator = initializer-clause
10050 type-specifier-seq declarator braced-init-list
10052 GNU Extension:
10054 condition:
10055 type-specifier-seq declarator asm-specification [opt]
10056 attributes [opt] = assignment-expression
10058 Returns the expression that should be tested. */
10060 static tree
10061 cp_parser_condition (cp_parser* parser)
10063 cp_decl_specifier_seq type_specifiers;
10064 const char *saved_message;
10065 int declares_class_or_enum;
10067 /* Try the declaration first. */
10068 cp_parser_parse_tentatively (parser);
10069 /* New types are not allowed in the type-specifier-seq for a
10070 condition. */
10071 saved_message = parser->type_definition_forbidden_message;
10072 parser->type_definition_forbidden_message
10073 = G_("types may not be defined in conditions");
10074 /* Parse the type-specifier-seq. */
10075 cp_parser_decl_specifier_seq (parser,
10076 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10077 &type_specifiers,
10078 &declares_class_or_enum);
10079 /* Restore the saved message. */
10080 parser->type_definition_forbidden_message = saved_message;
10081 /* If all is well, we might be looking at a declaration. */
10082 if (!cp_parser_error_occurred (parser))
10084 tree decl;
10085 tree asm_specification;
10086 tree attributes;
10087 cp_declarator *declarator;
10088 tree initializer = NULL_TREE;
10090 /* Parse the declarator. */
10091 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10092 /*ctor_dtor_or_conv_p=*/NULL,
10093 /*parenthesized_p=*/NULL,
10094 /*member_p=*/false);
10095 /* Parse the attributes. */
10096 attributes = cp_parser_attributes_opt (parser);
10097 /* Parse the asm-specification. */
10098 asm_specification = cp_parser_asm_specification_opt (parser);
10099 /* If the next token is not an `=' or '{', then we might still be
10100 looking at an expression. For example:
10102 if (A(a).x)
10104 looks like a decl-specifier-seq and a declarator -- but then
10105 there is no `=', so this is an expression. */
10106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10107 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10108 cp_parser_simulate_error (parser);
10110 /* If we did see an `=' or '{', then we are looking at a declaration
10111 for sure. */
10112 if (cp_parser_parse_definitely (parser))
10114 tree pushed_scope;
10115 bool non_constant_p;
10116 bool flags = LOOKUP_ONLYCONVERTING;
10118 /* Create the declaration. */
10119 decl = start_decl (declarator, &type_specifiers,
10120 /*initialized_p=*/true,
10121 attributes, /*prefix_attributes=*/NULL_TREE,
10122 &pushed_scope);
10124 /* Parse the initializer. */
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10127 initializer = cp_parser_braced_list (parser, &non_constant_p);
10128 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10129 flags = 0;
10131 else
10133 /* Consume the `='. */
10134 cp_parser_require (parser, CPP_EQ, RT_EQ);
10135 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10137 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10138 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10140 /* Process the initializer. */
10141 cp_finish_decl (decl,
10142 initializer, !non_constant_p,
10143 asm_specification,
10144 flags);
10146 if (pushed_scope)
10147 pop_scope (pushed_scope);
10149 return convert_from_reference (decl);
10152 /* If we didn't even get past the declarator successfully, we are
10153 definitely not looking at a declaration. */
10154 else
10155 cp_parser_abort_tentative_parse (parser);
10157 /* Otherwise, we are looking at an expression. */
10158 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10161 /* Parses a for-statement or range-for-statement until the closing ')',
10162 not included. */
10164 static tree
10165 cp_parser_for (cp_parser *parser, bool ivdep)
10167 tree init, scope, decl;
10168 bool is_range_for;
10170 /* Begin the for-statement. */
10171 scope = begin_for_scope (&init);
10173 /* Parse the initialization. */
10174 is_range_for = cp_parser_for_init_statement (parser, &decl);
10176 if (is_range_for)
10177 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10178 else
10179 return cp_parser_c_for (parser, scope, init, ivdep);
10182 static tree
10183 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10185 /* Normal for loop */
10186 tree condition = NULL_TREE;
10187 tree expression = NULL_TREE;
10188 tree stmt;
10190 stmt = begin_for_stmt (scope, init);
10191 /* The for-init-statement has already been parsed in
10192 cp_parser_for_init_statement, so no work is needed here. */
10193 finish_for_init_stmt (stmt);
10195 /* If there's a condition, process it. */
10196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10197 condition = cp_parser_condition (parser);
10198 else if (ivdep)
10200 cp_parser_error (parser, "missing loop condition in loop with "
10201 "%<GCC ivdep%> pragma");
10202 condition = error_mark_node;
10204 finish_for_cond (condition, stmt, ivdep);
10205 /* Look for the `;'. */
10206 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10208 /* If there's an expression, process it. */
10209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10210 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10211 finish_for_expr (expression, stmt);
10213 return stmt;
10216 /* Tries to parse a range-based for-statement:
10218 range-based-for:
10219 decl-specifier-seq declarator : expression
10221 The decl-specifier-seq declarator and the `:' are already parsed by
10222 cp_parser_for_init_statement. If processing_template_decl it returns a
10223 newly created RANGE_FOR_STMT; if not, it is converted to a
10224 regular FOR_STMT. */
10226 static tree
10227 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10228 bool ivdep)
10230 tree stmt, range_expr;
10232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10234 bool expr_non_constant_p;
10235 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10237 else
10238 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10240 /* If in template, STMT is converted to a normal for-statement
10241 at instantiation. If not, it is done just ahead. */
10242 if (processing_template_decl)
10244 if (check_for_bare_parameter_packs (range_expr))
10245 range_expr = error_mark_node;
10246 stmt = begin_range_for_stmt (scope, init);
10247 if (ivdep)
10248 RANGE_FOR_IVDEP (stmt) = 1;
10249 finish_range_for_decl (stmt, range_decl, range_expr);
10250 if (!type_dependent_expression_p (range_expr)
10251 /* do_auto_deduction doesn't mess with template init-lists. */
10252 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10253 do_range_for_auto_deduction (range_decl, range_expr);
10255 else
10257 stmt = begin_for_stmt (scope, init);
10258 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10260 return stmt;
10263 /* Subroutine of cp_convert_range_for: given the initializer expression,
10264 builds up the range temporary. */
10266 static tree
10267 build_range_temp (tree range_expr)
10269 tree range_type, range_temp;
10271 /* Find out the type deduced by the declaration
10272 `auto &&__range = range_expr'. */
10273 range_type = cp_build_reference_type (make_auto (), true);
10274 range_type = do_auto_deduction (range_type, range_expr,
10275 type_uses_auto (range_type));
10277 /* Create the __range variable. */
10278 range_temp = build_decl (input_location, VAR_DECL,
10279 get_identifier ("__for_range"), range_type);
10280 TREE_USED (range_temp) = 1;
10281 DECL_ARTIFICIAL (range_temp) = 1;
10283 return range_temp;
10286 /* Used by cp_parser_range_for in template context: we aren't going to
10287 do a full conversion yet, but we still need to resolve auto in the
10288 type of the for-range-declaration if present. This is basically
10289 a shortcut version of cp_convert_range_for. */
10291 static void
10292 do_range_for_auto_deduction (tree decl, tree range_expr)
10294 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10295 if (auto_node)
10297 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10298 range_temp = convert_from_reference (build_range_temp (range_expr));
10299 iter_type = (cp_parser_perform_range_for_lookup
10300 (range_temp, &begin_dummy, &end_dummy));
10301 if (iter_type)
10303 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10304 iter_type);
10305 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10306 tf_warning_or_error);
10307 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10308 iter_decl, auto_node);
10313 /* Converts a range-based for-statement into a normal
10314 for-statement, as per the definition.
10316 for (RANGE_DECL : RANGE_EXPR)
10317 BLOCK
10319 should be equivalent to:
10322 auto &&__range = RANGE_EXPR;
10323 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10324 __begin != __end;
10325 ++__begin)
10327 RANGE_DECL = *__begin;
10328 BLOCK
10332 If RANGE_EXPR is an array:
10333 BEGIN_EXPR = __range
10334 END_EXPR = __range + ARRAY_SIZE(__range)
10335 Else if RANGE_EXPR has a member 'begin' or 'end':
10336 BEGIN_EXPR = __range.begin()
10337 END_EXPR = __range.end()
10338 Else:
10339 BEGIN_EXPR = begin(__range)
10340 END_EXPR = end(__range);
10342 If __range has a member 'begin' but not 'end', or vice versa, we must
10343 still use the second alternative (it will surely fail, however).
10344 When calling begin()/end() in the third alternative we must use
10345 argument dependent lookup, but always considering 'std' as an associated
10346 namespace. */
10348 tree
10349 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10350 bool ivdep)
10352 tree begin, end;
10353 tree iter_type, begin_expr, end_expr;
10354 tree condition, expression;
10356 if (range_decl == error_mark_node || range_expr == error_mark_node)
10357 /* If an error happened previously do nothing or else a lot of
10358 unhelpful errors would be issued. */
10359 begin_expr = end_expr = iter_type = error_mark_node;
10360 else
10362 tree range_temp;
10364 if (TREE_CODE (range_expr) == VAR_DECL
10365 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10366 /* Can't bind a reference to an array of runtime bound. */
10367 range_temp = range_expr;
10368 else
10370 range_temp = build_range_temp (range_expr);
10371 pushdecl (range_temp);
10372 cp_finish_decl (range_temp, range_expr,
10373 /*is_constant_init*/false, NULL_TREE,
10374 LOOKUP_ONLYCONVERTING);
10375 range_temp = convert_from_reference (range_temp);
10377 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10378 &begin_expr, &end_expr);
10381 /* The new for initialization statement. */
10382 begin = build_decl (input_location, VAR_DECL,
10383 get_identifier ("__for_begin"), iter_type);
10384 TREE_USED (begin) = 1;
10385 DECL_ARTIFICIAL (begin) = 1;
10386 pushdecl (begin);
10387 cp_finish_decl (begin, begin_expr,
10388 /*is_constant_init*/false, NULL_TREE,
10389 LOOKUP_ONLYCONVERTING);
10391 end = build_decl (input_location, VAR_DECL,
10392 get_identifier ("__for_end"), iter_type);
10393 TREE_USED (end) = 1;
10394 DECL_ARTIFICIAL (end) = 1;
10395 pushdecl (end);
10396 cp_finish_decl (end, end_expr,
10397 /*is_constant_init*/false, NULL_TREE,
10398 LOOKUP_ONLYCONVERTING);
10400 finish_for_init_stmt (statement);
10402 /* The new for condition. */
10403 condition = build_x_binary_op (input_location, NE_EXPR,
10404 begin, ERROR_MARK,
10405 end, ERROR_MARK,
10406 NULL, tf_warning_or_error);
10407 finish_for_cond (condition, statement, ivdep);
10409 /* The new increment expression. */
10410 expression = finish_unary_op_expr (input_location,
10411 PREINCREMENT_EXPR, begin,
10412 tf_warning_or_error);
10413 finish_for_expr (expression, statement);
10415 /* The declaration is initialized with *__begin inside the loop body. */
10416 cp_finish_decl (range_decl,
10417 build_x_indirect_ref (input_location, begin, RO_NULL,
10418 tf_warning_or_error),
10419 /*is_constant_init*/false, NULL_TREE,
10420 LOOKUP_ONLYCONVERTING);
10422 return statement;
10425 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10426 We need to solve both at the same time because the method used
10427 depends on the existence of members begin or end.
10428 Returns the type deduced for the iterator expression. */
10430 static tree
10431 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10433 if (error_operand_p (range))
10435 *begin = *end = error_mark_node;
10436 return error_mark_node;
10439 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10441 error ("range-based %<for%> expression of type %qT "
10442 "has incomplete type", TREE_TYPE (range));
10443 *begin = *end = error_mark_node;
10444 return error_mark_node;
10446 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10448 /* If RANGE is an array, we will use pointer arithmetic. */
10449 *begin = range;
10450 *end = build_binary_op (input_location, PLUS_EXPR,
10451 range,
10452 array_type_nelts_top (TREE_TYPE (range)),
10454 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10456 else
10458 /* If it is not an array, we must do a bit of magic. */
10459 tree id_begin, id_end;
10460 tree member_begin, member_end;
10462 *begin = *end = error_mark_node;
10464 id_begin = get_identifier ("begin");
10465 id_end = get_identifier ("end");
10466 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10467 /*protect=*/2, /*want_type=*/false,
10468 tf_warning_or_error);
10469 member_end = lookup_member (TREE_TYPE (range), id_end,
10470 /*protect=*/2, /*want_type=*/false,
10471 tf_warning_or_error);
10473 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10475 /* Use the member functions. */
10476 if (member_begin != NULL_TREE)
10477 *begin = cp_parser_range_for_member_function (range, id_begin);
10478 else
10479 error ("range-based %<for%> expression of type %qT has an "
10480 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10482 if (member_end != NULL_TREE)
10483 *end = cp_parser_range_for_member_function (range, id_end);
10484 else
10485 error ("range-based %<for%> expression of type %qT has a "
10486 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10488 else
10490 /* Use global functions with ADL. */
10491 vec<tree, va_gc> *vec;
10492 vec = make_tree_vector ();
10494 vec_safe_push (vec, range);
10496 member_begin = perform_koenig_lookup (id_begin, vec,
10497 tf_warning_or_error);
10498 *begin = finish_call_expr (member_begin, &vec, false, true,
10499 tf_warning_or_error);
10500 member_end = perform_koenig_lookup (id_end, vec,
10501 tf_warning_or_error);
10502 *end = finish_call_expr (member_end, &vec, false, true,
10503 tf_warning_or_error);
10505 release_tree_vector (vec);
10508 /* Last common checks. */
10509 if (*begin == error_mark_node || *end == error_mark_node)
10511 /* If one of the expressions is an error do no more checks. */
10512 *begin = *end = error_mark_node;
10513 return error_mark_node;
10515 else if (type_dependent_expression_p (*begin)
10516 || type_dependent_expression_p (*end))
10517 /* Can happen, when, eg, in a template context, Koenig lookup
10518 can't resolve begin/end (c++/58503). */
10519 return NULL_TREE;
10520 else
10522 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10523 /* The unqualified type of the __begin and __end temporaries should
10524 be the same, as required by the multiple auto declaration. */
10525 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10526 error ("inconsistent begin/end types in range-based %<for%> "
10527 "statement: %qT and %qT",
10528 TREE_TYPE (*begin), TREE_TYPE (*end));
10529 return iter_type;
10534 /* Helper function for cp_parser_perform_range_for_lookup.
10535 Builds a tree for RANGE.IDENTIFIER(). */
10537 static tree
10538 cp_parser_range_for_member_function (tree range, tree identifier)
10540 tree member, res;
10541 vec<tree, va_gc> *vec;
10543 member = finish_class_member_access_expr (range, identifier,
10544 false, tf_warning_or_error);
10545 if (member == error_mark_node)
10546 return error_mark_node;
10548 vec = make_tree_vector ();
10549 res = finish_call_expr (member, &vec,
10550 /*disallow_virtual=*/false,
10551 /*koenig_p=*/false,
10552 tf_warning_or_error);
10553 release_tree_vector (vec);
10554 return res;
10557 /* Parse an iteration-statement.
10559 iteration-statement:
10560 while ( condition ) statement
10561 do statement while ( expression ) ;
10562 for ( for-init-statement condition [opt] ; expression [opt] )
10563 statement
10565 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10567 static tree
10568 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10570 cp_token *token;
10571 enum rid keyword;
10572 tree statement;
10573 unsigned char in_statement;
10575 /* Peek at the next token. */
10576 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10577 if (!token)
10578 return error_mark_node;
10580 /* Remember whether or not we are already within an iteration
10581 statement. */
10582 in_statement = parser->in_statement;
10584 /* See what kind of keyword it is. */
10585 keyword = token->keyword;
10586 switch (keyword)
10588 case RID_WHILE:
10590 tree condition;
10592 /* Begin the while-statement. */
10593 statement = begin_while_stmt ();
10594 /* Look for the `('. */
10595 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10596 /* Parse the condition. */
10597 condition = cp_parser_condition (parser);
10598 finish_while_stmt_cond (condition, statement, ivdep);
10599 /* Look for the `)'. */
10600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10601 /* Parse the dependent statement. */
10602 parser->in_statement = IN_ITERATION_STMT;
10603 cp_parser_already_scoped_statement (parser);
10604 parser->in_statement = in_statement;
10605 /* We're done with the while-statement. */
10606 finish_while_stmt (statement);
10608 break;
10610 case RID_DO:
10612 tree expression;
10614 /* Begin the do-statement. */
10615 statement = begin_do_stmt ();
10616 /* Parse the body of the do-statement. */
10617 parser->in_statement = IN_ITERATION_STMT;
10618 cp_parser_implicitly_scoped_statement (parser, NULL);
10619 parser->in_statement = in_statement;
10620 finish_do_body (statement);
10621 /* Look for the `while' keyword. */
10622 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10623 /* Look for the `('. */
10624 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10625 /* Parse the expression. */
10626 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10627 /* We're done with the do-statement. */
10628 finish_do_stmt (expression, statement, ivdep);
10629 /* Look for the `)'. */
10630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10631 /* Look for the `;'. */
10632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10634 break;
10636 case RID_FOR:
10638 /* Look for the `('. */
10639 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10641 statement = cp_parser_for (parser, ivdep);
10643 /* Look for the `)'. */
10644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10646 /* Parse the body of the for-statement. */
10647 parser->in_statement = IN_ITERATION_STMT;
10648 cp_parser_already_scoped_statement (parser);
10649 parser->in_statement = in_statement;
10651 /* We're done with the for-statement. */
10652 finish_for_stmt (statement);
10654 break;
10656 default:
10657 cp_parser_error (parser, "expected iteration-statement");
10658 statement = error_mark_node;
10659 break;
10662 return statement;
10665 /* Parse a for-init-statement or the declarator of a range-based-for.
10666 Returns true if a range-based-for declaration is seen.
10668 for-init-statement:
10669 expression-statement
10670 simple-declaration */
10672 static bool
10673 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10675 /* If the next token is a `;', then we have an empty
10676 expression-statement. Grammatically, this is also a
10677 simple-declaration, but an invalid one, because it does not
10678 declare anything. Therefore, if we did not handle this case
10679 specially, we would issue an error message about an invalid
10680 declaration. */
10681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10683 bool is_range_for = false;
10684 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10686 parser->colon_corrects_to_scope_p = false;
10688 /* We're going to speculatively look for a declaration, falling back
10689 to an expression, if necessary. */
10690 cp_parser_parse_tentatively (parser);
10691 /* Parse the declaration. */
10692 cp_parser_simple_declaration (parser,
10693 /*function_definition_allowed_p=*/false,
10694 decl);
10695 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10696 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10698 /* It is a range-for, consume the ':' */
10699 cp_lexer_consume_token (parser->lexer);
10700 is_range_for = true;
10701 if (cxx_dialect < cxx11)
10703 error_at (cp_lexer_peek_token (parser->lexer)->location,
10704 "range-based %<for%> loops are not allowed "
10705 "in C++98 mode");
10706 *decl = error_mark_node;
10709 else
10710 /* The ';' is not consumed yet because we told
10711 cp_parser_simple_declaration not to. */
10712 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10714 if (cp_parser_parse_definitely (parser))
10715 return is_range_for;
10716 /* If the tentative parse failed, then we shall need to look for an
10717 expression-statement. */
10719 /* If we are here, it is an expression-statement. */
10720 cp_parser_expression_statement (parser, NULL_TREE);
10721 return false;
10724 /* Parse a jump-statement.
10726 jump-statement:
10727 break ;
10728 continue ;
10729 return expression [opt] ;
10730 return braced-init-list ;
10731 goto identifier ;
10733 GNU extension:
10735 jump-statement:
10736 goto * expression ;
10738 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10740 static tree
10741 cp_parser_jump_statement (cp_parser* parser)
10743 tree statement = error_mark_node;
10744 cp_token *token;
10745 enum rid keyword;
10746 unsigned char in_statement;
10748 /* Peek at the next token. */
10749 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10750 if (!token)
10751 return error_mark_node;
10753 /* See what kind of keyword it is. */
10754 keyword = token->keyword;
10755 switch (keyword)
10757 case RID_BREAK:
10758 in_statement = parser->in_statement & ~IN_IF_STMT;
10759 switch (in_statement)
10761 case 0:
10762 error_at (token->location, "break statement not within loop or switch");
10763 break;
10764 default:
10765 gcc_assert ((in_statement & IN_SWITCH_STMT)
10766 || in_statement == IN_ITERATION_STMT);
10767 statement = finish_break_stmt ();
10768 if (in_statement == IN_ITERATION_STMT)
10769 break_maybe_infinite_loop ();
10770 break;
10771 case IN_OMP_BLOCK:
10772 error_at (token->location, "invalid exit from OpenMP structured block");
10773 break;
10774 case IN_OMP_FOR:
10775 error_at (token->location, "break statement used with OpenMP for loop");
10776 break;
10777 case IN_CILK_SIMD_FOR:
10778 error_at (token->location, "break statement used with Cilk Plus for loop");
10779 break;
10781 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10782 break;
10784 case RID_CONTINUE:
10785 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10787 case 0:
10788 error_at (token->location, "continue statement not within a loop");
10789 break;
10790 case IN_CILK_SIMD_FOR:
10791 error_at (token->location,
10792 "continue statement within %<#pragma simd%> loop body");
10793 /* Fall through. */
10794 case IN_ITERATION_STMT:
10795 case IN_OMP_FOR:
10796 statement = finish_continue_stmt ();
10797 break;
10798 case IN_OMP_BLOCK:
10799 error_at (token->location, "invalid exit from OpenMP structured block");
10800 break;
10801 default:
10802 gcc_unreachable ();
10804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10805 break;
10807 case RID_RETURN:
10809 tree expr;
10810 bool expr_non_constant_p;
10812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10814 cp_lexer_set_source_position (parser->lexer);
10815 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10816 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10818 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10819 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10820 else
10821 /* If the next token is a `;', then there is no
10822 expression. */
10823 expr = NULL_TREE;
10824 /* Build the return-statement. */
10825 statement = finish_return_stmt (expr);
10826 /* Look for the final `;'. */
10827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10829 break;
10831 case RID_GOTO:
10832 /* Create the goto-statement. */
10833 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10835 /* Issue a warning about this use of a GNU extension. */
10836 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10837 /* Consume the '*' token. */
10838 cp_lexer_consume_token (parser->lexer);
10839 /* Parse the dependent expression. */
10840 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10842 else
10843 finish_goto_stmt (cp_parser_identifier (parser));
10844 /* Look for the final `;'. */
10845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10846 break;
10848 default:
10849 cp_parser_error (parser, "expected jump-statement");
10850 break;
10853 return statement;
10856 /* Parse a declaration-statement.
10858 declaration-statement:
10859 block-declaration */
10861 static void
10862 cp_parser_declaration_statement (cp_parser* parser)
10864 void *p;
10866 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10867 p = obstack_alloc (&declarator_obstack, 0);
10869 /* Parse the block-declaration. */
10870 cp_parser_block_declaration (parser, /*statement_p=*/true);
10872 /* Free any declarators allocated. */
10873 obstack_free (&declarator_obstack, p);
10876 /* Some dependent statements (like `if (cond) statement'), are
10877 implicitly in their own scope. In other words, if the statement is
10878 a single statement (as opposed to a compound-statement), it is
10879 none-the-less treated as if it were enclosed in braces. Any
10880 declarations appearing in the dependent statement are out of scope
10881 after control passes that point. This function parses a statement,
10882 but ensures that is in its own scope, even if it is not a
10883 compound-statement.
10885 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10886 is a (possibly labeled) if statement which is not enclosed in
10887 braces and has an else clause. This is used to implement
10888 -Wparentheses.
10890 Returns the new statement. */
10892 static tree
10893 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10895 tree statement;
10897 if (if_p != NULL)
10898 *if_p = false;
10900 /* Mark if () ; with a special NOP_EXPR. */
10901 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10903 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10904 cp_lexer_consume_token (parser->lexer);
10905 statement = add_stmt (build_empty_stmt (loc));
10907 /* if a compound is opened, we simply parse the statement directly. */
10908 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10909 statement = cp_parser_compound_statement (parser, NULL, false, false);
10910 /* If the token is not a `{', then we must take special action. */
10911 else
10913 /* Create a compound-statement. */
10914 statement = begin_compound_stmt (0);
10915 /* Parse the dependent-statement. */
10916 cp_parser_statement (parser, NULL_TREE, false, if_p);
10917 /* Finish the dummy compound-statement. */
10918 finish_compound_stmt (statement);
10921 /* Return the statement. */
10922 return statement;
10925 /* For some dependent statements (like `while (cond) statement'), we
10926 have already created a scope. Therefore, even if the dependent
10927 statement is a compound-statement, we do not want to create another
10928 scope. */
10930 static void
10931 cp_parser_already_scoped_statement (cp_parser* parser)
10933 /* If the token is a `{', then we must take special action. */
10934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10935 cp_parser_statement (parser, NULL_TREE, false, NULL);
10936 else
10938 /* Avoid calling cp_parser_compound_statement, so that we
10939 don't create a new scope. Do everything else by hand. */
10940 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10941 /* If the next keyword is `__label__' we have a label declaration. */
10942 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10943 cp_parser_label_declaration (parser);
10944 /* Parse an (optional) statement-seq. */
10945 cp_parser_statement_seq_opt (parser, NULL_TREE);
10946 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10950 /* Declarations [gram.dcl.dcl] */
10952 /* Parse an optional declaration-sequence.
10954 declaration-seq:
10955 declaration
10956 declaration-seq declaration */
10958 static void
10959 cp_parser_declaration_seq_opt (cp_parser* parser)
10961 while (true)
10963 cp_token *token;
10965 token = cp_lexer_peek_token (parser->lexer);
10967 if (token->type == CPP_CLOSE_BRACE
10968 || token->type == CPP_EOF
10969 || token->type == CPP_PRAGMA_EOL)
10970 break;
10972 if (token->type == CPP_SEMICOLON)
10974 /* A declaration consisting of a single semicolon is
10975 invalid. Allow it unless we're being pedantic. */
10976 cp_lexer_consume_token (parser->lexer);
10977 if (!in_system_header_at (input_location))
10978 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10979 continue;
10982 /* If we're entering or exiting a region that's implicitly
10983 extern "C", modify the lang context appropriately. */
10984 if (!parser->implicit_extern_c && token->implicit_extern_c)
10986 push_lang_context (lang_name_c);
10987 parser->implicit_extern_c = true;
10989 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10991 pop_lang_context ();
10992 parser->implicit_extern_c = false;
10995 if (token->type == CPP_PRAGMA)
10997 /* A top-level declaration can consist solely of a #pragma.
10998 A nested declaration cannot, so this is done here and not
10999 in cp_parser_declaration. (A #pragma at block scope is
11000 handled in cp_parser_statement.) */
11001 cp_parser_pragma (parser, pragma_external);
11002 continue;
11005 /* Parse the declaration itself. */
11006 cp_parser_declaration (parser);
11010 /* Parse a declaration.
11012 declaration:
11013 block-declaration
11014 function-definition
11015 template-declaration
11016 explicit-instantiation
11017 explicit-specialization
11018 linkage-specification
11019 namespace-definition
11021 GNU extension:
11023 declaration:
11024 __extension__ declaration */
11026 static void
11027 cp_parser_declaration (cp_parser* parser)
11029 cp_token token1;
11030 cp_token token2;
11031 int saved_pedantic;
11032 void *p;
11033 tree attributes = NULL_TREE;
11035 /* Check for the `__extension__' keyword. */
11036 if (cp_parser_extension_opt (parser, &saved_pedantic))
11038 /* Parse the qualified declaration. */
11039 cp_parser_declaration (parser);
11040 /* Restore the PEDANTIC flag. */
11041 pedantic = saved_pedantic;
11043 return;
11046 /* Try to figure out what kind of declaration is present. */
11047 token1 = *cp_lexer_peek_token (parser->lexer);
11049 if (token1.type != CPP_EOF)
11050 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11051 else
11053 token2.type = CPP_EOF;
11054 token2.keyword = RID_MAX;
11057 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11058 p = obstack_alloc (&declarator_obstack, 0);
11060 /* If the next token is `extern' and the following token is a string
11061 literal, then we have a linkage specification. */
11062 if (token1.keyword == RID_EXTERN
11063 && cp_parser_is_pure_string_literal (&token2))
11064 cp_parser_linkage_specification (parser);
11065 /* If the next token is `template', then we have either a template
11066 declaration, an explicit instantiation, or an explicit
11067 specialization. */
11068 else if (token1.keyword == RID_TEMPLATE)
11070 /* `template <>' indicates a template specialization. */
11071 if (token2.type == CPP_LESS
11072 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11073 cp_parser_explicit_specialization (parser);
11074 /* `template <' indicates a template declaration. */
11075 else if (token2.type == CPP_LESS)
11076 cp_parser_template_declaration (parser, /*member_p=*/false);
11077 /* Anything else must be an explicit instantiation. */
11078 else
11079 cp_parser_explicit_instantiation (parser);
11081 /* If the next token is `export', then we have a template
11082 declaration. */
11083 else if (token1.keyword == RID_EXPORT)
11084 cp_parser_template_declaration (parser, /*member_p=*/false);
11085 /* If the next token is `extern', 'static' or 'inline' and the one
11086 after that is `template', we have a GNU extended explicit
11087 instantiation directive. */
11088 else if (cp_parser_allow_gnu_extensions_p (parser)
11089 && (token1.keyword == RID_EXTERN
11090 || token1.keyword == RID_STATIC
11091 || token1.keyword == RID_INLINE)
11092 && token2.keyword == RID_TEMPLATE)
11093 cp_parser_explicit_instantiation (parser);
11094 /* If the next token is `namespace', check for a named or unnamed
11095 namespace definition. */
11096 else if (token1.keyword == RID_NAMESPACE
11097 && (/* A named namespace definition. */
11098 (token2.type == CPP_NAME
11099 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11100 != CPP_EQ))
11101 /* An unnamed namespace definition. */
11102 || token2.type == CPP_OPEN_BRACE
11103 || token2.keyword == RID_ATTRIBUTE))
11104 cp_parser_namespace_definition (parser);
11105 /* An inline (associated) namespace definition. */
11106 else if (token1.keyword == RID_INLINE
11107 && token2.keyword == RID_NAMESPACE)
11108 cp_parser_namespace_definition (parser);
11109 /* Objective-C++ declaration/definition. */
11110 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11111 cp_parser_objc_declaration (parser, NULL_TREE);
11112 else if (c_dialect_objc ()
11113 && token1.keyword == RID_ATTRIBUTE
11114 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11115 cp_parser_objc_declaration (parser, attributes);
11116 /* We must have either a block declaration or a function
11117 definition. */
11118 else
11119 /* Try to parse a block-declaration, or a function-definition. */
11120 cp_parser_block_declaration (parser, /*statement_p=*/false);
11122 /* Free any declarators allocated. */
11123 obstack_free (&declarator_obstack, p);
11126 /* Parse a block-declaration.
11128 block-declaration:
11129 simple-declaration
11130 asm-definition
11131 namespace-alias-definition
11132 using-declaration
11133 using-directive
11135 GNU Extension:
11137 block-declaration:
11138 __extension__ block-declaration
11140 C++0x Extension:
11142 block-declaration:
11143 static_assert-declaration
11145 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11146 part of a declaration-statement. */
11148 static void
11149 cp_parser_block_declaration (cp_parser *parser,
11150 bool statement_p)
11152 cp_token *token1;
11153 int saved_pedantic;
11155 /* Check for the `__extension__' keyword. */
11156 if (cp_parser_extension_opt (parser, &saved_pedantic))
11158 /* Parse the qualified declaration. */
11159 cp_parser_block_declaration (parser, statement_p);
11160 /* Restore the PEDANTIC flag. */
11161 pedantic = saved_pedantic;
11163 return;
11166 /* Peek at the next token to figure out which kind of declaration is
11167 present. */
11168 token1 = cp_lexer_peek_token (parser->lexer);
11170 /* If the next keyword is `asm', we have an asm-definition. */
11171 if (token1->keyword == RID_ASM)
11173 if (statement_p)
11174 cp_parser_commit_to_tentative_parse (parser);
11175 cp_parser_asm_definition (parser);
11177 /* If the next keyword is `namespace', we have a
11178 namespace-alias-definition. */
11179 else if (token1->keyword == RID_NAMESPACE)
11180 cp_parser_namespace_alias_definition (parser);
11181 /* If the next keyword is `using', we have a
11182 using-declaration, a using-directive, or an alias-declaration. */
11183 else if (token1->keyword == RID_USING)
11185 cp_token *token2;
11187 if (statement_p)
11188 cp_parser_commit_to_tentative_parse (parser);
11189 /* If the token after `using' is `namespace', then we have a
11190 using-directive. */
11191 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11192 if (token2->keyword == RID_NAMESPACE)
11193 cp_parser_using_directive (parser);
11194 /* If the second token after 'using' is '=', then we have an
11195 alias-declaration. */
11196 else if (cxx_dialect >= cxx11
11197 && token2->type == CPP_NAME
11198 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11199 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11200 cp_parser_alias_declaration (parser);
11201 /* Otherwise, it's a using-declaration. */
11202 else
11203 cp_parser_using_declaration (parser,
11204 /*access_declaration_p=*/false);
11206 /* If the next keyword is `__label__' we have a misplaced label
11207 declaration. */
11208 else if (token1->keyword == RID_LABEL)
11210 cp_lexer_consume_token (parser->lexer);
11211 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11212 cp_parser_skip_to_end_of_statement (parser);
11213 /* If the next token is now a `;', consume it. */
11214 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11215 cp_lexer_consume_token (parser->lexer);
11217 /* If the next token is `static_assert' we have a static assertion. */
11218 else if (token1->keyword == RID_STATIC_ASSERT)
11219 cp_parser_static_assert (parser, /*member_p=*/false);
11220 /* Anything else must be a simple-declaration. */
11221 else
11222 cp_parser_simple_declaration (parser, !statement_p,
11223 /*maybe_range_for_decl*/NULL);
11226 /* Parse a simple-declaration.
11228 simple-declaration:
11229 decl-specifier-seq [opt] init-declarator-list [opt] ;
11231 init-declarator-list:
11232 init-declarator
11233 init-declarator-list , init-declarator
11235 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11236 function-definition as a simple-declaration.
11238 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11239 parsed declaration if it is an uninitialized single declarator not followed
11240 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11241 if present, will not be consumed. */
11243 static void
11244 cp_parser_simple_declaration (cp_parser* parser,
11245 bool function_definition_allowed_p,
11246 tree *maybe_range_for_decl)
11248 cp_decl_specifier_seq decl_specifiers;
11249 int declares_class_or_enum;
11250 bool saw_declarator;
11252 if (maybe_range_for_decl)
11253 *maybe_range_for_decl = NULL_TREE;
11255 /* Defer access checks until we know what is being declared; the
11256 checks for names appearing in the decl-specifier-seq should be
11257 done as if we were in the scope of the thing being declared. */
11258 push_deferring_access_checks (dk_deferred);
11260 /* Parse the decl-specifier-seq. We have to keep track of whether
11261 or not the decl-specifier-seq declares a named class or
11262 enumeration type, since that is the only case in which the
11263 init-declarator-list is allowed to be empty.
11265 [dcl.dcl]
11267 In a simple-declaration, the optional init-declarator-list can be
11268 omitted only when declaring a class or enumeration, that is when
11269 the decl-specifier-seq contains either a class-specifier, an
11270 elaborated-type-specifier, or an enum-specifier. */
11271 cp_parser_decl_specifier_seq (parser,
11272 CP_PARSER_FLAGS_OPTIONAL,
11273 &decl_specifiers,
11274 &declares_class_or_enum);
11275 /* We no longer need to defer access checks. */
11276 stop_deferring_access_checks ();
11278 /* In a block scope, a valid declaration must always have a
11279 decl-specifier-seq. By not trying to parse declarators, we can
11280 resolve the declaration/expression ambiguity more quickly. */
11281 if (!function_definition_allowed_p
11282 && !decl_specifiers.any_specifiers_p)
11284 cp_parser_error (parser, "expected declaration");
11285 goto done;
11288 /* If the next two tokens are both identifiers, the code is
11289 erroneous. The usual cause of this situation is code like:
11291 T t;
11293 where "T" should name a type -- but does not. */
11294 if (!decl_specifiers.any_type_specifiers_p
11295 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11297 /* If parsing tentatively, we should commit; we really are
11298 looking at a declaration. */
11299 cp_parser_commit_to_tentative_parse (parser);
11300 /* Give up. */
11301 goto done;
11304 /* If we have seen at least one decl-specifier, and the next token
11305 is not a parenthesis, then we must be looking at a declaration.
11306 (After "int (" we might be looking at a functional cast.) */
11307 if (decl_specifiers.any_specifiers_p
11308 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11309 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11310 && !cp_parser_error_occurred (parser))
11311 cp_parser_commit_to_tentative_parse (parser);
11313 /* Keep going until we hit the `;' at the end of the simple
11314 declaration. */
11315 saw_declarator = false;
11316 while (cp_lexer_next_token_is_not (parser->lexer,
11317 CPP_SEMICOLON))
11319 cp_token *token;
11320 bool function_definition_p;
11321 tree decl;
11323 if (saw_declarator)
11325 /* If we are processing next declarator, coma is expected */
11326 token = cp_lexer_peek_token (parser->lexer);
11327 gcc_assert (token->type == CPP_COMMA);
11328 cp_lexer_consume_token (parser->lexer);
11329 if (maybe_range_for_decl)
11330 *maybe_range_for_decl = error_mark_node;
11332 else
11333 saw_declarator = true;
11335 /* Parse the init-declarator. */
11336 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11337 /*checks=*/NULL,
11338 function_definition_allowed_p,
11339 /*member_p=*/false,
11340 declares_class_or_enum,
11341 &function_definition_p,
11342 maybe_range_for_decl);
11343 /* If an error occurred while parsing tentatively, exit quickly.
11344 (That usually happens when in the body of a function; each
11345 statement is treated as a declaration-statement until proven
11346 otherwise.) */
11347 if (cp_parser_error_occurred (parser))
11348 goto done;
11349 /* Handle function definitions specially. */
11350 if (function_definition_p)
11352 /* If the next token is a `,', then we are probably
11353 processing something like:
11355 void f() {}, *p;
11357 which is erroneous. */
11358 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11360 cp_token *token = cp_lexer_peek_token (parser->lexer);
11361 error_at (token->location,
11362 "mixing"
11363 " declarations and function-definitions is forbidden");
11365 /* Otherwise, we're done with the list of declarators. */
11366 else
11368 pop_deferring_access_checks ();
11369 return;
11372 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11373 *maybe_range_for_decl = decl;
11374 /* The next token should be either a `,' or a `;'. */
11375 token = cp_lexer_peek_token (parser->lexer);
11376 /* If it's a `,', there are more declarators to come. */
11377 if (token->type == CPP_COMMA)
11378 /* will be consumed next time around */;
11379 /* If it's a `;', we are done. */
11380 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11381 break;
11382 /* Anything else is an error. */
11383 else
11385 /* If we have already issued an error message we don't need
11386 to issue another one. */
11387 if (decl != error_mark_node
11388 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11389 cp_parser_error (parser, "expected %<,%> or %<;%>");
11390 /* Skip tokens until we reach the end of the statement. */
11391 cp_parser_skip_to_end_of_statement (parser);
11392 /* If the next token is now a `;', consume it. */
11393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11394 cp_lexer_consume_token (parser->lexer);
11395 goto done;
11397 /* After the first time around, a function-definition is not
11398 allowed -- even if it was OK at first. For example:
11400 int i, f() {}
11402 is not valid. */
11403 function_definition_allowed_p = false;
11406 /* Issue an error message if no declarators are present, and the
11407 decl-specifier-seq does not itself declare a class or
11408 enumeration: [dcl.dcl]/3. */
11409 if (!saw_declarator)
11411 if (cp_parser_declares_only_class_p (parser))
11413 if (!declares_class_or_enum
11414 && decl_specifiers.type
11415 && OVERLOAD_TYPE_P (decl_specifiers.type))
11416 /* Ensure an error is issued anyway when finish_decltype_type,
11417 called via cp_parser_decl_specifier_seq, returns a class or
11418 an enumeration (c++/51786). */
11419 decl_specifiers.type = NULL_TREE;
11420 shadow_tag (&decl_specifiers);
11422 /* Perform any deferred access checks. */
11423 perform_deferred_access_checks (tf_warning_or_error);
11426 /* Consume the `;'. */
11427 if (!maybe_range_for_decl)
11428 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11430 done:
11431 pop_deferring_access_checks ();
11434 /* Parse a decl-specifier-seq.
11436 decl-specifier-seq:
11437 decl-specifier-seq [opt] decl-specifier
11438 decl-specifier attribute-specifier-seq [opt] (C++11)
11440 decl-specifier:
11441 storage-class-specifier
11442 type-specifier
11443 function-specifier
11444 friend
11445 typedef
11447 GNU Extension:
11449 decl-specifier:
11450 attributes
11452 Concepts Extension:
11454 decl-specifier:
11455 concept
11458 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11460 The parser flags FLAGS is used to control type-specifier parsing.
11462 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11463 flags:
11465 1: one of the decl-specifiers is an elaborated-type-specifier
11466 (i.e., a type declaration)
11467 2: one of the decl-specifiers is an enum-specifier or a
11468 class-specifier (i.e., a type definition)
11472 static void
11473 cp_parser_decl_specifier_seq (cp_parser* parser,
11474 cp_parser_flags flags,
11475 cp_decl_specifier_seq *decl_specs,
11476 int* declares_class_or_enum)
11478 bool constructor_possible_p = !parser->in_declarator_p;
11479 bool found_decl_spec = false;
11480 cp_token *start_token = NULL;
11481 cp_decl_spec ds;
11483 /* Clear DECL_SPECS. */
11484 clear_decl_specs (decl_specs);
11486 /* Assume no class or enumeration type is declared. */
11487 *declares_class_or_enum = 0;
11489 /* Keep reading specifiers until there are no more to read. */
11490 while (true)
11492 bool constructor_p;
11493 cp_token *token;
11494 ds = ds_last;
11496 /* Peek at the next token. */
11497 token = cp_lexer_peek_token (parser->lexer);
11499 /* Save the first token of the decl spec list for error
11500 reporting. */
11501 if (!start_token)
11502 start_token = token;
11503 /* Handle attributes. */
11504 if (cp_next_tokens_can_be_attribute_p (parser))
11506 /* Parse the attributes. */
11507 tree attrs = cp_parser_attributes_opt (parser);
11509 /* In a sequence of declaration specifiers, c++11 attributes
11510 appertain to the type that precede them. In that case
11511 [dcl.spec]/1 says:
11513 The attribute-specifier-seq affects the type only for
11514 the declaration it appears in, not other declarations
11515 involving the same type.
11517 But for now let's force the user to position the
11518 attribute either at the beginning of the declaration or
11519 after the declarator-id, which would clearly mean that it
11520 applies to the declarator. */
11521 if (cxx11_attribute_p (attrs))
11523 if (!found_decl_spec)
11524 /* The c++11 attribute is at the beginning of the
11525 declaration. It appertains to the entity being
11526 declared. */;
11527 else
11529 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11531 /* This is an attribute following a
11532 class-specifier. */
11533 if (decl_specs->type_definition_p)
11534 warn_misplaced_attr_for_class_type (token->location,
11535 decl_specs->type);
11536 attrs = NULL_TREE;
11538 else
11540 decl_specs->std_attributes
11541 = chainon (decl_specs->std_attributes,
11542 attrs);
11543 if (decl_specs->locations[ds_std_attribute] == 0)
11544 decl_specs->locations[ds_std_attribute] = token->location;
11546 continue;
11550 decl_specs->attributes
11551 = chainon (decl_specs->attributes,
11552 attrs);
11553 if (decl_specs->locations[ds_attribute] == 0)
11554 decl_specs->locations[ds_attribute] = token->location;
11555 continue;
11557 /* Assume we will find a decl-specifier keyword. */
11558 found_decl_spec = true;
11559 /* If the next token is an appropriate keyword, we can simply
11560 add it to the list. */
11561 switch (token->keyword)
11563 /* decl-specifier:
11564 friend
11565 constexpr */
11566 case RID_FRIEND:
11567 if (!at_class_scope_p ())
11569 error_at (token->location, "%<friend%> used outside of class");
11570 cp_lexer_purge_token (parser->lexer);
11572 else
11574 ds = ds_friend;
11575 /* Consume the token. */
11576 cp_lexer_consume_token (parser->lexer);
11578 break;
11580 case RID_CONSTEXPR:
11581 ds = ds_constexpr;
11582 cp_lexer_consume_token (parser->lexer);
11583 break;
11585 case RID_CONCEPT:
11586 ds = ds_concept;
11587 cp_lexer_consume_token (parser->lexer);
11588 break;
11590 /* function-specifier:
11591 inline
11592 virtual
11593 explicit */
11594 case RID_INLINE:
11595 case RID_VIRTUAL:
11596 case RID_EXPLICIT:
11597 cp_parser_function_specifier_opt (parser, decl_specs);
11598 break;
11600 /* decl-specifier:
11601 typedef */
11602 case RID_TYPEDEF:
11603 ds = ds_typedef;
11604 /* Consume the token. */
11605 cp_lexer_consume_token (parser->lexer);
11606 /* A constructor declarator cannot appear in a typedef. */
11607 constructor_possible_p = false;
11608 /* The "typedef" keyword can only occur in a declaration; we
11609 may as well commit at this point. */
11610 cp_parser_commit_to_tentative_parse (parser);
11612 if (decl_specs->storage_class != sc_none)
11613 decl_specs->conflicting_specifiers_p = true;
11614 break;
11616 /* storage-class-specifier:
11617 auto
11618 register
11619 static
11620 extern
11621 mutable
11623 GNU Extension:
11624 thread */
11625 case RID_AUTO:
11626 if (cxx_dialect == cxx98)
11628 /* Consume the token. */
11629 cp_lexer_consume_token (parser->lexer);
11631 /* Complain about `auto' as a storage specifier, if
11632 we're complaining about C++0x compatibility. */
11633 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11634 " changes meaning in C++11; please remove it");
11636 /* Set the storage class anyway. */
11637 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11638 token);
11640 else
11641 /* C++0x auto type-specifier. */
11642 found_decl_spec = false;
11643 break;
11645 case RID_REGISTER:
11646 case RID_STATIC:
11647 case RID_EXTERN:
11648 case RID_MUTABLE:
11649 /* Consume the token. */
11650 cp_lexer_consume_token (parser->lexer);
11651 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11652 token);
11653 break;
11654 case RID_THREAD:
11655 /* Consume the token. */
11656 ds = ds_thread;
11657 cp_lexer_consume_token (parser->lexer);
11658 break;
11660 default:
11661 /* We did not yet find a decl-specifier yet. */
11662 found_decl_spec = false;
11663 break;
11666 if (found_decl_spec
11667 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11668 && token->keyword != RID_CONSTEXPR)
11669 error ("decl-specifier invalid in condition");
11671 if (ds != ds_last)
11672 set_and_check_decl_spec_loc (decl_specs, ds, token);
11674 /* Constructors are a special case. The `S' in `S()' is not a
11675 decl-specifier; it is the beginning of the declarator. */
11676 constructor_p
11677 = (!found_decl_spec
11678 && constructor_possible_p
11679 && (cp_parser_constructor_declarator_p
11680 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11682 /* If we don't have a DECL_SPEC yet, then we must be looking at
11683 a type-specifier. */
11684 if (!found_decl_spec && !constructor_p)
11686 int decl_spec_declares_class_or_enum;
11687 bool is_cv_qualifier;
11688 tree type_spec;
11690 type_spec
11691 = cp_parser_type_specifier (parser, flags,
11692 decl_specs,
11693 /*is_declaration=*/true,
11694 &decl_spec_declares_class_or_enum,
11695 &is_cv_qualifier);
11696 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11698 /* If this type-specifier referenced a user-defined type
11699 (a typedef, class-name, etc.), then we can't allow any
11700 more such type-specifiers henceforth.
11702 [dcl.spec]
11704 The longest sequence of decl-specifiers that could
11705 possibly be a type name is taken as the
11706 decl-specifier-seq of a declaration. The sequence shall
11707 be self-consistent as described below.
11709 [dcl.type]
11711 As a general rule, at most one type-specifier is allowed
11712 in the complete decl-specifier-seq of a declaration. The
11713 only exceptions are the following:
11715 -- const or volatile can be combined with any other
11716 type-specifier.
11718 -- signed or unsigned can be combined with char, long,
11719 short, or int.
11721 -- ..
11723 Example:
11725 typedef char* Pc;
11726 void g (const int Pc);
11728 Here, Pc is *not* part of the decl-specifier seq; it's
11729 the declarator. Therefore, once we see a type-specifier
11730 (other than a cv-qualifier), we forbid any additional
11731 user-defined types. We *do* still allow things like `int
11732 int' to be considered a decl-specifier-seq, and issue the
11733 error message later. */
11734 if (type_spec && !is_cv_qualifier)
11735 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11736 /* A constructor declarator cannot follow a type-specifier. */
11737 if (type_spec)
11739 constructor_possible_p = false;
11740 found_decl_spec = true;
11741 if (!is_cv_qualifier)
11742 decl_specs->any_type_specifiers_p = true;
11746 /* If we still do not have a DECL_SPEC, then there are no more
11747 decl-specifiers. */
11748 if (!found_decl_spec)
11749 break;
11751 decl_specs->any_specifiers_p = true;
11752 /* After we see one decl-specifier, further decl-specifiers are
11753 always optional. */
11754 flags |= CP_PARSER_FLAGS_OPTIONAL;
11757 /* Don't allow a friend specifier with a class definition. */
11758 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11759 && (*declares_class_or_enum & 2))
11760 error_at (decl_specs->locations[ds_friend],
11761 "class definition may not be declared a friend");
11764 /* Parse an (optional) storage-class-specifier.
11766 storage-class-specifier:
11767 auto
11768 register
11769 static
11770 extern
11771 mutable
11773 GNU Extension:
11775 storage-class-specifier:
11776 thread
11778 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11780 static tree
11781 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11783 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11785 case RID_AUTO:
11786 if (cxx_dialect != cxx98)
11787 return NULL_TREE;
11788 /* Fall through for C++98. */
11790 case RID_REGISTER:
11791 case RID_STATIC:
11792 case RID_EXTERN:
11793 case RID_MUTABLE:
11794 case RID_THREAD:
11795 /* Consume the token. */
11796 return cp_lexer_consume_token (parser->lexer)->u.value;
11798 default:
11799 return NULL_TREE;
11803 /* Parse an (optional) function-specifier.
11805 function-specifier:
11806 inline
11807 virtual
11808 explicit
11810 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11811 Updates DECL_SPECS, if it is non-NULL. */
11813 static tree
11814 cp_parser_function_specifier_opt (cp_parser* parser,
11815 cp_decl_specifier_seq *decl_specs)
11817 cp_token *token = cp_lexer_peek_token (parser->lexer);
11818 switch (token->keyword)
11820 case RID_INLINE:
11821 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11822 break;
11824 case RID_VIRTUAL:
11825 /* 14.5.2.3 [temp.mem]
11827 A member function template shall not be virtual. */
11828 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11829 error_at (token->location, "templates may not be %<virtual%>");
11830 else
11831 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11832 break;
11834 case RID_EXPLICIT:
11835 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11836 break;
11838 default:
11839 return NULL_TREE;
11842 /* Consume the token. */
11843 return cp_lexer_consume_token (parser->lexer)->u.value;
11846 /* Parse a linkage-specification.
11848 linkage-specification:
11849 extern string-literal { declaration-seq [opt] }
11850 extern string-literal declaration */
11852 static void
11853 cp_parser_linkage_specification (cp_parser* parser)
11855 tree linkage;
11857 /* Look for the `extern' keyword. */
11858 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11860 /* Look for the string-literal. */
11861 linkage = cp_parser_string_literal (parser, false, false);
11863 /* Transform the literal into an identifier. If the literal is a
11864 wide-character string, or contains embedded NULs, then we can't
11865 handle it as the user wants. */
11866 if (strlen (TREE_STRING_POINTER (linkage))
11867 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11869 cp_parser_error (parser, "invalid linkage-specification");
11870 /* Assume C++ linkage. */
11871 linkage = lang_name_cplusplus;
11873 else
11874 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11876 /* We're now using the new linkage. */
11877 push_lang_context (linkage);
11879 /* If the next token is a `{', then we're using the first
11880 production. */
11881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11883 cp_ensure_no_omp_declare_simd (parser);
11885 /* Consume the `{' token. */
11886 cp_lexer_consume_token (parser->lexer);
11887 /* Parse the declarations. */
11888 cp_parser_declaration_seq_opt (parser);
11889 /* Look for the closing `}'. */
11890 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11892 /* Otherwise, there's just one declaration. */
11893 else
11895 bool saved_in_unbraced_linkage_specification_p;
11897 saved_in_unbraced_linkage_specification_p
11898 = parser->in_unbraced_linkage_specification_p;
11899 parser->in_unbraced_linkage_specification_p = true;
11900 cp_parser_declaration (parser);
11901 parser->in_unbraced_linkage_specification_p
11902 = saved_in_unbraced_linkage_specification_p;
11905 /* We're done with the linkage-specification. */
11906 pop_lang_context ();
11909 /* Parse a static_assert-declaration.
11911 static_assert-declaration:
11912 static_assert ( constant-expression , string-literal ) ;
11914 If MEMBER_P, this static_assert is a class member. */
11916 static void
11917 cp_parser_static_assert(cp_parser *parser, bool member_p)
11919 tree condition;
11920 tree message;
11921 cp_token *token;
11922 location_t saved_loc;
11923 bool dummy;
11925 /* Peek at the `static_assert' token so we can keep track of exactly
11926 where the static assertion started. */
11927 token = cp_lexer_peek_token (parser->lexer);
11928 saved_loc = token->location;
11930 /* Look for the `static_assert' keyword. */
11931 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11932 RT_STATIC_ASSERT))
11933 return;
11935 /* We know we are in a static assertion; commit to any tentative
11936 parse. */
11937 if (cp_parser_parsing_tentatively (parser))
11938 cp_parser_commit_to_tentative_parse (parser);
11940 /* Parse the `(' starting the static assertion condition. */
11941 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11943 /* Parse the constant-expression. Allow a non-constant expression
11944 here in order to give better diagnostics in finish_static_assert. */
11945 condition =
11946 cp_parser_constant_expression (parser,
11947 /*allow_non_constant_p=*/true,
11948 /*non_constant_p=*/&dummy);
11950 /* Parse the separating `,'. */
11951 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11953 /* Parse the string-literal message. */
11954 message = cp_parser_string_literal (parser,
11955 /*translate=*/false,
11956 /*wide_ok=*/true);
11958 /* A `)' completes the static assertion. */
11959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11960 cp_parser_skip_to_closing_parenthesis (parser,
11961 /*recovering=*/true,
11962 /*or_comma=*/false,
11963 /*consume_paren=*/true);
11965 /* A semicolon terminates the declaration. */
11966 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11968 /* Complete the static assertion, which may mean either processing
11969 the static assert now or saving it for template instantiation. */
11970 finish_static_assert (condition, message, saved_loc, member_p);
11973 /* Parse the expression in decltype ( expression ). */
11975 static tree
11976 cp_parser_decltype_expr (cp_parser *parser,
11977 bool &id_expression_or_member_access_p)
11979 cp_token *id_expr_start_token;
11980 tree expr;
11982 /* First, try parsing an id-expression. */
11983 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11984 cp_parser_parse_tentatively (parser);
11985 expr = cp_parser_id_expression (parser,
11986 /*template_keyword_p=*/false,
11987 /*check_dependency_p=*/true,
11988 /*template_p=*/NULL,
11989 /*declarator_p=*/false,
11990 /*optional_p=*/false);
11992 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11994 bool non_integral_constant_expression_p = false;
11995 tree id_expression = expr;
11996 cp_id_kind idk;
11997 const char *error_msg;
11999 if (identifier_p (expr))
12000 /* Lookup the name we got back from the id-expression. */
12001 expr = cp_parser_lookup_name_simple (parser, expr,
12002 id_expr_start_token->location);
12004 if (expr
12005 && expr != error_mark_node
12006 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12007 && TREE_CODE (expr) != TYPE_DECL
12008 && (TREE_CODE (expr) != BIT_NOT_EXPR
12009 || !TYPE_P (TREE_OPERAND (expr, 0)))
12010 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12012 /* Complete lookup of the id-expression. */
12013 expr = (finish_id_expression
12014 (id_expression, expr, parser->scope, &idk,
12015 /*integral_constant_expression_p=*/false,
12016 /*allow_non_integral_constant_expression_p=*/true,
12017 &non_integral_constant_expression_p,
12018 /*template_p=*/false,
12019 /*done=*/true,
12020 /*address_p=*/false,
12021 /*template_arg_p=*/false,
12022 &error_msg,
12023 id_expr_start_token->location));
12025 if (expr == error_mark_node)
12026 /* We found an id-expression, but it was something that we
12027 should not have found. This is an error, not something
12028 we can recover from, so note that we found an
12029 id-expression and we'll recover as gracefully as
12030 possible. */
12031 id_expression_or_member_access_p = true;
12034 if (expr
12035 && expr != error_mark_node
12036 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12037 /* We have an id-expression. */
12038 id_expression_or_member_access_p = true;
12041 if (!id_expression_or_member_access_p)
12043 /* Abort the id-expression parse. */
12044 cp_parser_abort_tentative_parse (parser);
12046 /* Parsing tentatively, again. */
12047 cp_parser_parse_tentatively (parser);
12049 /* Parse a class member access. */
12050 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12051 /*cast_p=*/false, /*decltype*/true,
12052 /*member_access_only_p=*/true, NULL);
12054 if (expr
12055 && expr != error_mark_node
12056 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12057 /* We have an id-expression. */
12058 id_expression_or_member_access_p = true;
12061 if (id_expression_or_member_access_p)
12062 /* We have parsed the complete id-expression or member access. */
12063 cp_parser_parse_definitely (parser);
12064 else
12066 /* Abort our attempt to parse an id-expression or member access
12067 expression. */
12068 cp_parser_abort_tentative_parse (parser);
12070 /* Parse a full expression. */
12071 expr = cp_parser_expression (parser, /*cast_p=*/false,
12072 /*decltype*/true, NULL);
12075 return expr;
12078 /* Parse a `decltype' type. Returns the type.
12080 simple-type-specifier:
12081 decltype ( expression )
12082 C++14 proposal:
12083 decltype ( auto ) */
12085 static tree
12086 cp_parser_decltype (cp_parser *parser)
12088 tree expr;
12089 bool id_expression_or_member_access_p = false;
12090 const char *saved_message;
12091 bool saved_integral_constant_expression_p;
12092 bool saved_non_integral_constant_expression_p;
12093 bool saved_greater_than_is_operator_p;
12094 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12096 if (start_token->type == CPP_DECLTYPE)
12098 /* Already parsed. */
12099 cp_lexer_consume_token (parser->lexer);
12100 return start_token->u.value;
12103 /* Look for the `decltype' token. */
12104 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12105 return error_mark_node;
12107 /* Parse the opening `('. */
12108 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12109 return error_mark_node;
12111 /* decltype (auto) */
12112 if (cxx_dialect >= cxx1y
12113 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12115 cp_lexer_consume_token (parser->lexer);
12116 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12117 return error_mark_node;
12118 expr = make_decltype_auto ();
12119 AUTO_IS_DECLTYPE (expr) = true;
12120 goto rewrite;
12123 /* Types cannot be defined in a `decltype' expression. Save away the
12124 old message. */
12125 saved_message = parser->type_definition_forbidden_message;
12127 /* And create the new one. */
12128 parser->type_definition_forbidden_message
12129 = G_("types may not be defined in %<decltype%> expressions");
12131 /* The restrictions on constant-expressions do not apply inside
12132 decltype expressions. */
12133 saved_integral_constant_expression_p
12134 = parser->integral_constant_expression_p;
12135 saved_non_integral_constant_expression_p
12136 = parser->non_integral_constant_expression_p;
12137 parser->integral_constant_expression_p = false;
12139 /* Within a parenthesized expression, a `>' token is always
12140 the greater-than operator. */
12141 saved_greater_than_is_operator_p
12142 = parser->greater_than_is_operator_p;
12143 parser->greater_than_is_operator_p = true;
12145 /* Do not actually evaluate the expression. */
12146 ++cp_unevaluated_operand;
12148 /* Do not warn about problems with the expression. */
12149 ++c_inhibit_evaluation_warnings;
12151 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12153 /* Go back to evaluating expressions. */
12154 --cp_unevaluated_operand;
12155 --c_inhibit_evaluation_warnings;
12157 /* The `>' token might be the end of a template-id or
12158 template-parameter-list now. */
12159 parser->greater_than_is_operator_p
12160 = saved_greater_than_is_operator_p;
12162 /* Restore the old message and the integral constant expression
12163 flags. */
12164 parser->type_definition_forbidden_message = saved_message;
12165 parser->integral_constant_expression_p
12166 = saved_integral_constant_expression_p;
12167 parser->non_integral_constant_expression_p
12168 = saved_non_integral_constant_expression_p;
12170 /* Parse to the closing `)'. */
12171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12173 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12174 /*consume_paren=*/true);
12175 return error_mark_node;
12178 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12179 tf_warning_or_error);
12181 rewrite:
12182 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12183 it again. */
12184 start_token->type = CPP_DECLTYPE;
12185 start_token->u.value = expr;
12186 start_token->keyword = RID_MAX;
12187 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12189 return expr;
12192 /* Special member functions [gram.special] */
12194 /* Parse a conversion-function-id.
12196 conversion-function-id:
12197 operator conversion-type-id
12199 Returns an IDENTIFIER_NODE representing the operator. */
12201 static tree
12202 cp_parser_conversion_function_id (cp_parser* parser)
12204 tree type;
12205 tree saved_scope;
12206 tree saved_qualifying_scope;
12207 tree saved_object_scope;
12208 tree pushed_scope = NULL_TREE;
12210 /* Look for the `operator' token. */
12211 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12212 return error_mark_node;
12213 /* When we parse the conversion-type-id, the current scope will be
12214 reset. However, we need that information in able to look up the
12215 conversion function later, so we save it here. */
12216 saved_scope = parser->scope;
12217 saved_qualifying_scope = parser->qualifying_scope;
12218 saved_object_scope = parser->object_scope;
12219 /* We must enter the scope of the class so that the names of
12220 entities declared within the class are available in the
12221 conversion-type-id. For example, consider:
12223 struct S {
12224 typedef int I;
12225 operator I();
12228 S::operator I() { ... }
12230 In order to see that `I' is a type-name in the definition, we
12231 must be in the scope of `S'. */
12232 if (saved_scope)
12233 pushed_scope = push_scope (saved_scope);
12234 /* Parse the conversion-type-id. */
12235 type = cp_parser_conversion_type_id (parser);
12236 /* Leave the scope of the class, if any. */
12237 if (pushed_scope)
12238 pop_scope (pushed_scope);
12239 /* Restore the saved scope. */
12240 parser->scope = saved_scope;
12241 parser->qualifying_scope = saved_qualifying_scope;
12242 parser->object_scope = saved_object_scope;
12243 /* If the TYPE is invalid, indicate failure. */
12244 if (type == error_mark_node)
12245 return error_mark_node;
12246 return mangle_conv_op_name_for_type (type);
12249 /* Parse a conversion-type-id:
12251 conversion-type-id:
12252 type-specifier-seq conversion-declarator [opt]
12254 Returns the TYPE specified. */
12256 static tree
12257 cp_parser_conversion_type_id (cp_parser* parser)
12259 tree attributes;
12260 cp_decl_specifier_seq type_specifiers;
12261 cp_declarator *declarator;
12262 tree type_specified;
12263 const char *saved_message;
12265 /* Parse the attributes. */
12266 attributes = cp_parser_attributes_opt (parser);
12268 saved_message = parser->type_definition_forbidden_message;
12269 parser->type_definition_forbidden_message
12270 = G_("types may not be defined in a conversion-type-id");
12272 /* Parse the type-specifiers. */
12273 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12274 /*is_trailing_return=*/false,
12275 &type_specifiers);
12277 parser->type_definition_forbidden_message = saved_message;
12279 /* If that didn't work, stop. */
12280 if (type_specifiers.type == error_mark_node)
12281 return error_mark_node;
12282 /* Parse the conversion-declarator. */
12283 declarator = cp_parser_conversion_declarator_opt (parser);
12285 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12286 /*initialized=*/0, &attributes);
12287 if (attributes)
12288 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12290 /* Don't give this error when parsing tentatively. This happens to
12291 work because we always parse this definitively once. */
12292 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12293 && type_uses_auto (type_specified))
12295 if (cxx_dialect < cxx1y)
12297 error ("invalid use of %<auto%> in conversion operator");
12298 return error_mark_node;
12300 else if (template_parm_scope_p ())
12301 warning (0, "use of %<auto%> in member template "
12302 "conversion operator can never be deduced");
12305 return type_specified;
12308 /* Parse an (optional) conversion-declarator.
12310 conversion-declarator:
12311 ptr-operator conversion-declarator [opt]
12315 static cp_declarator *
12316 cp_parser_conversion_declarator_opt (cp_parser* parser)
12318 enum tree_code code;
12319 tree class_type, std_attributes = NULL_TREE;
12320 cp_cv_quals cv_quals;
12322 /* We don't know if there's a ptr-operator next, or not. */
12323 cp_parser_parse_tentatively (parser);
12324 /* Try the ptr-operator. */
12325 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12326 &std_attributes);
12327 /* If it worked, look for more conversion-declarators. */
12328 if (cp_parser_parse_definitely (parser))
12330 cp_declarator *declarator;
12332 /* Parse another optional declarator. */
12333 declarator = cp_parser_conversion_declarator_opt (parser);
12335 declarator = cp_parser_make_indirect_declarator
12336 (code, class_type, cv_quals, declarator, std_attributes);
12338 return declarator;
12341 return NULL;
12344 /* Parse an (optional) ctor-initializer.
12346 ctor-initializer:
12347 : mem-initializer-list
12349 Returns TRUE iff the ctor-initializer was actually present. */
12351 static bool
12352 cp_parser_ctor_initializer_opt (cp_parser* parser)
12354 /* If the next token is not a `:', then there is no
12355 ctor-initializer. */
12356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12358 /* Do default initialization of any bases and members. */
12359 if (DECL_CONSTRUCTOR_P (current_function_decl))
12360 finish_mem_initializers (NULL_TREE);
12362 return false;
12365 /* Consume the `:' token. */
12366 cp_lexer_consume_token (parser->lexer);
12367 /* And the mem-initializer-list. */
12368 cp_parser_mem_initializer_list (parser);
12370 return true;
12373 /* Parse a mem-initializer-list.
12375 mem-initializer-list:
12376 mem-initializer ... [opt]
12377 mem-initializer ... [opt] , mem-initializer-list */
12379 static void
12380 cp_parser_mem_initializer_list (cp_parser* parser)
12382 tree mem_initializer_list = NULL_TREE;
12383 tree target_ctor = error_mark_node;
12384 cp_token *token = cp_lexer_peek_token (parser->lexer);
12386 /* Let the semantic analysis code know that we are starting the
12387 mem-initializer-list. */
12388 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12389 error_at (token->location,
12390 "only constructors take member initializers");
12392 /* Loop through the list. */
12393 while (true)
12395 tree mem_initializer;
12397 token = cp_lexer_peek_token (parser->lexer);
12398 /* Parse the mem-initializer. */
12399 mem_initializer = cp_parser_mem_initializer (parser);
12400 /* If the next token is a `...', we're expanding member initializers. */
12401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12403 /* Consume the `...'. */
12404 cp_lexer_consume_token (parser->lexer);
12406 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12407 can be expanded but members cannot. */
12408 if (mem_initializer != error_mark_node
12409 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12411 error_at (token->location,
12412 "cannot expand initializer for member %<%D%>",
12413 TREE_PURPOSE (mem_initializer));
12414 mem_initializer = error_mark_node;
12417 /* Construct the pack expansion type. */
12418 if (mem_initializer != error_mark_node)
12419 mem_initializer = make_pack_expansion (mem_initializer);
12421 if (target_ctor != error_mark_node
12422 && mem_initializer != error_mark_node)
12424 error ("mem-initializer for %qD follows constructor delegation",
12425 TREE_PURPOSE (mem_initializer));
12426 mem_initializer = error_mark_node;
12428 /* Look for a target constructor. */
12429 if (mem_initializer != error_mark_node
12430 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12431 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12433 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12434 if (mem_initializer_list)
12436 error ("constructor delegation follows mem-initializer for %qD",
12437 TREE_PURPOSE (mem_initializer_list));
12438 mem_initializer = error_mark_node;
12440 target_ctor = mem_initializer;
12442 /* Add it to the list, unless it was erroneous. */
12443 if (mem_initializer != error_mark_node)
12445 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12446 mem_initializer_list = mem_initializer;
12448 /* If the next token is not a `,', we're done. */
12449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12450 break;
12451 /* Consume the `,' token. */
12452 cp_lexer_consume_token (parser->lexer);
12455 /* Perform semantic analysis. */
12456 if (DECL_CONSTRUCTOR_P (current_function_decl))
12457 finish_mem_initializers (mem_initializer_list);
12460 /* Parse a mem-initializer.
12462 mem-initializer:
12463 mem-initializer-id ( expression-list [opt] )
12464 mem-initializer-id braced-init-list
12466 GNU extension:
12468 mem-initializer:
12469 ( expression-list [opt] )
12471 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12472 class) or FIELD_DECL (for a non-static data member) to initialize;
12473 the TREE_VALUE is the expression-list. An empty initialization
12474 list is represented by void_list_node. */
12476 static tree
12477 cp_parser_mem_initializer (cp_parser* parser)
12479 tree mem_initializer_id;
12480 tree expression_list;
12481 tree member;
12482 cp_token *token = cp_lexer_peek_token (parser->lexer);
12484 /* Find out what is being initialized. */
12485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12487 permerror (token->location,
12488 "anachronistic old-style base class initializer");
12489 mem_initializer_id = NULL_TREE;
12491 else
12493 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12494 if (mem_initializer_id == error_mark_node)
12495 return mem_initializer_id;
12497 member = expand_member_init (mem_initializer_id);
12498 if (member && !DECL_P (member))
12499 in_base_initializer = 1;
12501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12503 bool expr_non_constant_p;
12504 cp_lexer_set_source_position (parser->lexer);
12505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12506 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12507 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12508 expression_list = build_tree_list (NULL_TREE, expression_list);
12510 else
12512 vec<tree, va_gc> *vec;
12513 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12514 /*cast_p=*/false,
12515 /*allow_expansion_p=*/true,
12516 /*non_constant_p=*/NULL);
12517 if (vec == NULL)
12518 return error_mark_node;
12519 expression_list = build_tree_list_vec (vec);
12520 release_tree_vector (vec);
12523 if (expression_list == error_mark_node)
12524 return error_mark_node;
12525 if (!expression_list)
12526 expression_list = void_type_node;
12528 in_base_initializer = 0;
12530 return member ? build_tree_list (member, expression_list) : error_mark_node;
12533 /* Parse a mem-initializer-id.
12535 mem-initializer-id:
12536 :: [opt] nested-name-specifier [opt] class-name
12537 identifier
12539 Returns a TYPE indicating the class to be initializer for the first
12540 production. Returns an IDENTIFIER_NODE indicating the data member
12541 to be initialized for the second production. */
12543 static tree
12544 cp_parser_mem_initializer_id (cp_parser* parser)
12546 bool global_scope_p;
12547 bool nested_name_specifier_p;
12548 bool template_p = false;
12549 tree id;
12551 cp_token *token = cp_lexer_peek_token (parser->lexer);
12553 /* `typename' is not allowed in this context ([temp.res]). */
12554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12556 error_at (token->location,
12557 "keyword %<typename%> not allowed in this context (a qualified "
12558 "member initializer is implicitly a type)");
12559 cp_lexer_consume_token (parser->lexer);
12561 /* Look for the optional `::' operator. */
12562 global_scope_p
12563 = (cp_parser_global_scope_opt (parser,
12564 /*current_scope_valid_p=*/false)
12565 != NULL_TREE);
12566 /* Look for the optional nested-name-specifier. The simplest way to
12567 implement:
12569 [temp.res]
12571 The keyword `typename' is not permitted in a base-specifier or
12572 mem-initializer; in these contexts a qualified name that
12573 depends on a template-parameter is implicitly assumed to be a
12574 type name.
12576 is to assume that we have seen the `typename' keyword at this
12577 point. */
12578 nested_name_specifier_p
12579 = (cp_parser_nested_name_specifier_opt (parser,
12580 /*typename_keyword_p=*/true,
12581 /*check_dependency_p=*/true,
12582 /*type_p=*/true,
12583 /*is_declaration=*/true)
12584 != NULL_TREE);
12585 if (nested_name_specifier_p)
12586 template_p = cp_parser_optional_template_keyword (parser);
12587 /* If there is a `::' operator or a nested-name-specifier, then we
12588 are definitely looking for a class-name. */
12589 if (global_scope_p || nested_name_specifier_p)
12590 return cp_parser_class_name (parser,
12591 /*typename_keyword_p=*/true,
12592 /*template_keyword_p=*/template_p,
12593 typename_type,
12594 /*check_dependency_p=*/true,
12595 /*class_head_p=*/false,
12596 /*is_declaration=*/true);
12597 /* Otherwise, we could also be looking for an ordinary identifier. */
12598 cp_parser_parse_tentatively (parser);
12599 /* Try a class-name. */
12600 id = cp_parser_class_name (parser,
12601 /*typename_keyword_p=*/true,
12602 /*template_keyword_p=*/false,
12603 none_type,
12604 /*check_dependency_p=*/true,
12605 /*class_head_p=*/false,
12606 /*is_declaration=*/true);
12607 /* If we found one, we're done. */
12608 if (cp_parser_parse_definitely (parser))
12609 return id;
12610 /* Otherwise, look for an ordinary identifier. */
12611 return cp_parser_identifier (parser);
12614 /* Overloading [gram.over] */
12616 /* Parse an operator-function-id.
12618 operator-function-id:
12619 operator operator
12621 Returns an IDENTIFIER_NODE for the operator which is a
12622 human-readable spelling of the identifier, e.g., `operator +'. */
12624 static tree
12625 cp_parser_operator_function_id (cp_parser* parser)
12627 /* Look for the `operator' keyword. */
12628 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12629 return error_mark_node;
12630 /* And then the name of the operator itself. */
12631 return cp_parser_operator (parser);
12634 /* Return an identifier node for a user-defined literal operator.
12635 The suffix identifier is chained to the operator name identifier. */
12637 static tree
12638 cp_literal_operator_id (const char* name)
12640 tree identifier;
12641 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12642 + strlen (name) + 10);
12643 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12644 identifier = get_identifier (buffer);
12646 return identifier;
12649 /* Parse an operator.
12651 operator:
12652 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12653 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12654 || ++ -- , ->* -> () []
12656 GNU Extensions:
12658 operator:
12659 <? >? <?= >?=
12661 Returns an IDENTIFIER_NODE for the operator which is a
12662 human-readable spelling of the identifier, e.g., `operator +'. */
12664 static tree
12665 cp_parser_operator (cp_parser* parser)
12667 tree id = NULL_TREE;
12668 cp_token *token;
12669 bool bad_encoding_prefix = false;
12671 /* Peek at the next token. */
12672 token = cp_lexer_peek_token (parser->lexer);
12673 /* Figure out which operator we have. */
12674 switch (token->type)
12676 case CPP_KEYWORD:
12678 enum tree_code op;
12680 /* The keyword should be either `new' or `delete'. */
12681 if (token->keyword == RID_NEW)
12682 op = NEW_EXPR;
12683 else if (token->keyword == RID_DELETE)
12684 op = DELETE_EXPR;
12685 else
12686 break;
12688 /* Consume the `new' or `delete' token. */
12689 cp_lexer_consume_token (parser->lexer);
12691 /* Peek at the next token. */
12692 token = cp_lexer_peek_token (parser->lexer);
12693 /* If it's a `[' token then this is the array variant of the
12694 operator. */
12695 if (token->type == CPP_OPEN_SQUARE)
12697 /* Consume the `[' token. */
12698 cp_lexer_consume_token (parser->lexer);
12699 /* Look for the `]' token. */
12700 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12701 id = ansi_opname (op == NEW_EXPR
12702 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12704 /* Otherwise, we have the non-array variant. */
12705 else
12706 id = ansi_opname (op);
12708 return id;
12711 case CPP_PLUS:
12712 id = ansi_opname (PLUS_EXPR);
12713 break;
12715 case CPP_MINUS:
12716 id = ansi_opname (MINUS_EXPR);
12717 break;
12719 case CPP_MULT:
12720 id = ansi_opname (MULT_EXPR);
12721 break;
12723 case CPP_DIV:
12724 id = ansi_opname (TRUNC_DIV_EXPR);
12725 break;
12727 case CPP_MOD:
12728 id = ansi_opname (TRUNC_MOD_EXPR);
12729 break;
12731 case CPP_XOR:
12732 id = ansi_opname (BIT_XOR_EXPR);
12733 break;
12735 case CPP_AND:
12736 id = ansi_opname (BIT_AND_EXPR);
12737 break;
12739 case CPP_OR:
12740 id = ansi_opname (BIT_IOR_EXPR);
12741 break;
12743 case CPP_COMPL:
12744 id = ansi_opname (BIT_NOT_EXPR);
12745 break;
12747 case CPP_NOT:
12748 id = ansi_opname (TRUTH_NOT_EXPR);
12749 break;
12751 case CPP_EQ:
12752 id = ansi_assopname (NOP_EXPR);
12753 break;
12755 case CPP_LESS:
12756 id = ansi_opname (LT_EXPR);
12757 break;
12759 case CPP_GREATER:
12760 id = ansi_opname (GT_EXPR);
12761 break;
12763 case CPP_PLUS_EQ:
12764 id = ansi_assopname (PLUS_EXPR);
12765 break;
12767 case CPP_MINUS_EQ:
12768 id = ansi_assopname (MINUS_EXPR);
12769 break;
12771 case CPP_MULT_EQ:
12772 id = ansi_assopname (MULT_EXPR);
12773 break;
12775 case CPP_DIV_EQ:
12776 id = ansi_assopname (TRUNC_DIV_EXPR);
12777 break;
12779 case CPP_MOD_EQ:
12780 id = ansi_assopname (TRUNC_MOD_EXPR);
12781 break;
12783 case CPP_XOR_EQ:
12784 id = ansi_assopname (BIT_XOR_EXPR);
12785 break;
12787 case CPP_AND_EQ:
12788 id = ansi_assopname (BIT_AND_EXPR);
12789 break;
12791 case CPP_OR_EQ:
12792 id = ansi_assopname (BIT_IOR_EXPR);
12793 break;
12795 case CPP_LSHIFT:
12796 id = ansi_opname (LSHIFT_EXPR);
12797 break;
12799 case CPP_RSHIFT:
12800 id = ansi_opname (RSHIFT_EXPR);
12801 break;
12803 case CPP_LSHIFT_EQ:
12804 id = ansi_assopname (LSHIFT_EXPR);
12805 break;
12807 case CPP_RSHIFT_EQ:
12808 id = ansi_assopname (RSHIFT_EXPR);
12809 break;
12811 case CPP_EQ_EQ:
12812 id = ansi_opname (EQ_EXPR);
12813 break;
12815 case CPP_NOT_EQ:
12816 id = ansi_opname (NE_EXPR);
12817 break;
12819 case CPP_LESS_EQ:
12820 id = ansi_opname (LE_EXPR);
12821 break;
12823 case CPP_GREATER_EQ:
12824 id = ansi_opname (GE_EXPR);
12825 break;
12827 case CPP_AND_AND:
12828 id = ansi_opname (TRUTH_ANDIF_EXPR);
12829 break;
12831 case CPP_OR_OR:
12832 id = ansi_opname (TRUTH_ORIF_EXPR);
12833 break;
12835 case CPP_PLUS_PLUS:
12836 id = ansi_opname (POSTINCREMENT_EXPR);
12837 break;
12839 case CPP_MINUS_MINUS:
12840 id = ansi_opname (PREDECREMENT_EXPR);
12841 break;
12843 case CPP_COMMA:
12844 id = ansi_opname (COMPOUND_EXPR);
12845 break;
12847 case CPP_DEREF_STAR:
12848 id = ansi_opname (MEMBER_REF);
12849 break;
12851 case CPP_DEREF:
12852 id = ansi_opname (COMPONENT_REF);
12853 break;
12855 case CPP_OPEN_PAREN:
12856 /* Consume the `('. */
12857 cp_lexer_consume_token (parser->lexer);
12858 /* Look for the matching `)'. */
12859 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12860 return ansi_opname (CALL_EXPR);
12862 case CPP_OPEN_SQUARE:
12863 /* Consume the `['. */
12864 cp_lexer_consume_token (parser->lexer);
12865 /* Look for the matching `]'. */
12866 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12867 return ansi_opname (ARRAY_REF);
12869 case CPP_WSTRING:
12870 case CPP_STRING16:
12871 case CPP_STRING32:
12872 case CPP_UTF8STRING:
12873 bad_encoding_prefix = true;
12874 /* Fall through. */
12876 case CPP_STRING:
12877 if (cxx_dialect == cxx98)
12878 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12879 if (bad_encoding_prefix)
12881 error ("invalid encoding prefix in literal operator");
12882 return error_mark_node;
12884 if (TREE_STRING_LENGTH (token->u.value) > 2)
12886 error ("expected empty string after %<operator%> keyword");
12887 return error_mark_node;
12889 /* Consume the string. */
12890 cp_lexer_consume_token (parser->lexer);
12891 /* Look for the suffix identifier. */
12892 token = cp_lexer_peek_token (parser->lexer);
12893 if (token->type == CPP_NAME)
12895 id = cp_parser_identifier (parser);
12896 if (id != error_mark_node)
12898 const char *name = IDENTIFIER_POINTER (id);
12899 return cp_literal_operator_id (name);
12902 else if (token->type == CPP_KEYWORD)
12904 error ("unexpected keyword;"
12905 " remove space between quotes and suffix identifier");
12906 return error_mark_node;
12908 else
12910 error ("expected suffix identifier");
12911 return error_mark_node;
12914 case CPP_WSTRING_USERDEF:
12915 case CPP_STRING16_USERDEF:
12916 case CPP_STRING32_USERDEF:
12917 case CPP_UTF8STRING_USERDEF:
12918 bad_encoding_prefix = true;
12919 /* Fall through. */
12921 case CPP_STRING_USERDEF:
12922 if (cxx_dialect == cxx98)
12923 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12924 if (bad_encoding_prefix)
12926 error ("invalid encoding prefix in literal operator");
12927 return error_mark_node;
12930 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12931 if (TREE_STRING_LENGTH (string_tree) > 2)
12933 error ("expected empty string after %<operator%> keyword");
12934 return error_mark_node;
12936 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12937 /* Consume the user-defined string literal. */
12938 cp_lexer_consume_token (parser->lexer);
12939 if (id != error_mark_node)
12941 const char *name = IDENTIFIER_POINTER (id);
12942 return cp_literal_operator_id (name);
12944 else
12945 return error_mark_node;
12948 default:
12949 /* Anything else is an error. */
12950 break;
12953 /* If we have selected an identifier, we need to consume the
12954 operator token. */
12955 if (id)
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Otherwise, no valid operator name was present. */
12958 else
12960 cp_parser_error (parser, "expected operator");
12961 id = error_mark_node;
12964 return id;
12967 /* Parse a template-declaration.
12969 template-declaration:
12970 export [opt] template < template-parameter-list > declaration
12972 If MEMBER_P is TRUE, this template-declaration occurs within a
12973 class-specifier.
12975 The grammar rule given by the standard isn't correct. What
12976 is really meant is:
12978 template-declaration:
12979 export [opt] template-parameter-list-seq
12980 decl-specifier-seq [opt] init-declarator [opt] ;
12981 export [opt] template-parameter-list-seq
12982 function-definition
12984 template-parameter-list-seq:
12985 template-parameter-list-seq [opt]
12986 template < template-parameter-list >
12988 Concept Extensions:
12990 template-parameter-list-seq:
12991 template < template-parameter-list > template-requirement [opt]
12993 template-requirement:
12994 requires logical-or-expression
12997 static void
12998 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13000 /* Check for `export'. */
13001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13003 /* Consume the `export' token. */
13004 cp_lexer_consume_token (parser->lexer);
13005 /* Warn that we do not support `export'. */
13006 warning (0, "keyword %<export%> not implemented, and will be ignored");
13009 cp_parser_template_declaration_after_export (parser, member_p);
13012 /* Parse a template-parameter-list.
13014 template-parameter-list:
13015 template-parameter
13016 template-parameter-list , template-parameter
13018 Returns a TREE_LIST. Each node represents a template parameter.
13019 The nodes are connected via their TREE_CHAINs. */
13021 static tree
13022 cp_parser_template_parameter_list (cp_parser* parser)
13024 tree parameter_list = NULL_TREE;
13026 begin_template_parm_list ();
13028 /* The loop below parses the template parms. We first need to know
13029 the total number of template parms to be able to compute proper
13030 canonical types of each dependent type. So after the loop, when
13031 we know the total number of template parms,
13032 end_template_parm_list computes the proper canonical types and
13033 fixes up the dependent types accordingly. */
13034 while (true)
13036 tree parameter;
13037 bool is_non_type;
13038 bool is_parameter_pack;
13039 location_t parm_loc;
13041 /* Parse the template-parameter. */
13042 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13043 parameter = cp_parser_template_parameter (parser,
13044 &is_non_type,
13045 &is_parameter_pack);
13046 /* Add it to the list. */
13047 if (parameter != error_mark_node)
13048 parameter_list = process_template_parm (parameter_list,
13049 parm_loc,
13050 parameter,
13051 is_non_type,
13052 is_parameter_pack);
13053 else
13055 tree err_parm = build_tree_list (parameter, parameter);
13056 parameter_list = chainon (parameter_list, err_parm);
13059 /* If the next token is not a `,', we're done. */
13060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13061 break;
13062 /* Otherwise, consume the `,' token. */
13063 cp_lexer_consume_token (parser->lexer);
13066 return end_template_parm_list (parameter_list);
13069 // Returns true if PARM declares a constrained-parameter.
13070 static inline bool
13071 cp_is_constrained_parameter (cp_parameter_declarator *parm)
13073 gcc_assert (parm);
13074 tree decl = parm->decl_specifiers.type;
13075 return (decl
13076 && TREE_CODE (decl) == TYPE_DECL
13077 && DECL_INITIAL (decl)
13078 && DECL_SIZE_UNIT (decl)
13079 && TREE_CODE (DECL_SIZE_UNIT (decl)) == FUNCTION_DECL);
13083 // Check that the type parameter is only a declarator-id, and that its
13084 // type is not cv-qualified.
13085 bool
13086 cp_check_constrained_type_parm (cp_parser *parser,
13087 cp_parameter_declarator *parm)
13089 // Don't ptr, ref, function, or array declarators for a constrained type
13090 // or templtae template parameter.
13091 if (parm->declarator->kind != cdk_id)
13093 cp_parser_error (parser, "invalid constrained type parameter");
13094 return false;
13097 // Don't allow cv-qualified type parameters.
13098 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const) ||
13099 decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13101 cp_parser_error (parser, "cv-qualified type parameter");
13102 return false;
13105 return true;
13108 // Finish parsing/processing a template type parameter and chekcing
13109 // various restrictions.
13110 static inline tree
13111 cp_constrained_type_template_parm (cp_parser *parser,
13112 tree id,
13113 cp_parameter_declarator* parmdecl)
13115 if (cp_check_constrained_type_parm (parser, parmdecl))
13116 return finish_template_type_parm (class_type_node, id);
13117 else
13118 return error_mark_node;
13121 // Finish parsing/processing a template template parameter by borrowing
13122 // the template parameter list from the prototype parameter.
13123 static tree
13124 cp_constrained_template_template_parm (cp_parser *parser,
13125 tree proto,
13126 tree id,
13127 cp_parameter_declarator *parmdecl)
13129 if (!cp_check_constrained_type_parm (parser, parmdecl))
13130 return error_mark_node;
13132 // FIXME: This should probably be copied, and we may need to adjust
13133 // the template parameter depths.
13134 tree saved_parms = current_template_parms;
13135 begin_template_parm_list ();
13136 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13137 end_template_parm_list ();
13139 tree parm = finish_template_template_parm (class_type_node, id);
13140 current_template_parms = saved_parms;
13141 return parm;
13144 // Create a new non-type template parameter from the given PARM declarator.
13145 static tree
13146 cp_constrained_non_type_template_parm (bool *is_non_type,
13147 cp_parameter_declarator *parm)
13149 *is_non_type = true;
13150 cp_declarator *decl = parm->declarator;
13151 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13152 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13153 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13156 // Build a constrained template parameter based on the PARMDECL
13157 // declarator. The type of PARMDECL is the constrained type, which
13158 // refers to the prototype template parameter that ultimately
13159 // specifies the type of the declared parameter.
13160 static tree
13161 cp_finish_constrained_parameter (cp_parser *parser,
13162 cp_parameter_declarator *parmdecl,
13163 bool *is_non_type,
13164 bool *is_parameter_pack)
13166 tree decl = parmdecl->decl_specifiers.type;
13167 tree id = parmdecl->declarator->u.id.unqualified_name;
13168 tree def = parmdecl->default_argument;
13169 tree proto = DECL_INITIAL (decl);
13171 // Remember if the user declared this as a parameter pack and
13172 // erase that flag on the annotation. Template packs are dealt
13173 // with separately.
13174 bool is_pack = parmdecl->declarator->parameter_pack_p;
13175 if (is_pack)
13176 parmdecl->declarator->parameter_pack_p = false;
13178 // Is the prototype a parameter pack? If so, but the declaration
13179 // does not include "...", then emit an error.
13180 bool is_variadic = template_parameter_pack_p (proto);
13181 if (is_variadic && !is_pack)
13182 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13184 // The prototype is a template parameter pack, then the resulting
13185 // parameter also needs to be a pack.
13186 if (is_pack || is_variadic)
13187 *is_parameter_pack = true;
13189 // Build the parameter. Return an error if the declarator
13190 // was invalid.
13191 tree parm;
13192 if (TREE_CODE (proto) == TYPE_DECL)
13193 parm = cp_constrained_type_template_parm (parser, id, parmdecl);
13194 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13195 parm = cp_constrained_template_template_parm (parser, proto, id, parmdecl);
13196 else
13197 parm = cp_constrained_non_type_template_parm (is_non_type, parmdecl);
13198 if (parm == error_mark_node)
13199 return error_mark_node;
13201 // Finish the parameter decl and create a node attaching the
13202 // default argument and constraint.
13203 parm = build_tree_list (def, parm);
13204 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13205 return parm;
13209 /* Parse a template-parameter.
13211 template-parameter:
13212 type-parameter
13213 parameter-declaration
13215 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13216 the parameter. The TREE_PURPOSE is the default value, if any.
13217 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13218 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13219 set to true iff this parameter is a parameter pack. */
13221 static tree
13222 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13223 bool *is_parameter_pack)
13225 cp_token *token;
13226 cp_parameter_declarator *parameter_declarator;
13227 cp_declarator *id_declarator;
13228 tree parm;
13230 /* Assume it is a type parameter or a template parameter. */
13231 *is_non_type = false;
13232 /* Assume it not a parameter pack. */
13233 *is_parameter_pack = false;
13234 /* Peek at the next token. */
13235 token = cp_lexer_peek_token (parser->lexer);
13236 /* If it is `class' or `template', we have a type-parameter. */
13237 if (token->keyword == RID_TEMPLATE)
13238 return cp_parser_type_parameter (parser, is_parameter_pack);
13239 /* If it is `class' or `typename' we do not know yet whether it is a
13240 type parameter or a non-type parameter. Consider:
13242 template <typename T, typename T::X X> ...
13246 template <class C, class D*> ...
13248 Here, the first parameter is a type parameter, and the second is
13249 a non-type parameter. We can tell by looking at the token after
13250 the identifier -- if it is a `,', `=', or `>' then we have a type
13251 parameter. */
13252 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13254 /* Peek at the token after `class' or `typename'. */
13255 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13256 /* If it's an ellipsis, we have a template type parameter
13257 pack. */
13258 if (token->type == CPP_ELLIPSIS)
13259 return cp_parser_type_parameter (parser, is_parameter_pack);
13260 /* If it's an identifier, skip it. */
13261 if (token->type == CPP_NAME)
13262 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13263 /* Now, see if the token looks like the end of a template
13264 parameter. */
13265 if (token->type == CPP_COMMA
13266 || token->type == CPP_EQ
13267 || token->type == CPP_GREATER)
13268 return cp_parser_type_parameter (parser, is_parameter_pack);
13271 /* Otherwise, it is a non-type parameter.
13273 [temp.param]
13275 When parsing a default template-argument for a non-type
13276 template-parameter, the first non-nested `>' is taken as the end
13277 of the template parameter-list rather than a greater-than
13278 operator. */
13279 parameter_declarator
13280 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13281 /*parenthesized_p=*/NULL);
13283 if (!parameter_declarator)
13284 return error_mark_node;
13286 // The parameter may have been constrained.
13287 if (cp_is_constrained_parameter (parameter_declarator))
13288 return cp_finish_constrained_parameter (parser,
13289 parameter_declarator,
13290 is_non_type,
13291 is_parameter_pack);
13293 // Now we're sure that the parameter is a non-type parameter.
13294 *is_non_type = true;
13296 /* If the parameter declaration is marked as a parameter pack, set
13297 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13298 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13299 grokdeclarator. */
13300 if (parameter_declarator->declarator
13301 && parameter_declarator->declarator->parameter_pack_p)
13303 *is_parameter_pack = true;
13304 parameter_declarator->declarator->parameter_pack_p = false;
13307 if (parameter_declarator->default_argument)
13309 /* Can happen in some cases of erroneous input (c++/34892). */
13310 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13311 /* Consume the `...' for better error recovery. */
13312 cp_lexer_consume_token (parser->lexer);
13314 /* If the next token is an ellipsis, and we don't already have it
13315 marked as a parameter pack, then we have a parameter pack (that
13316 has no declarator). */
13317 else if (!*is_parameter_pack
13318 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13319 && (declarator_can_be_parameter_pack
13320 (parameter_declarator->declarator)))
13322 /* Consume the `...'. */
13323 cp_lexer_consume_token (parser->lexer);
13324 maybe_warn_variadic_templates ();
13326 *is_parameter_pack = true;
13328 /* We might end up with a pack expansion as the type of the non-type
13329 template parameter, in which case this is a non-type template
13330 parameter pack. */
13331 else if (parameter_declarator->decl_specifiers.type
13332 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13334 *is_parameter_pack = true;
13335 parameter_declarator->decl_specifiers.type =
13336 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13339 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13341 /* Parameter packs cannot have default arguments. However, a
13342 user may try to do so, so we'll parse them and give an
13343 appropriate diagnostic here. */
13345 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13347 /* Find the name of the parameter pack. */
13348 id_declarator = parameter_declarator->declarator;
13349 while (id_declarator && id_declarator->kind != cdk_id)
13350 id_declarator = id_declarator->declarator;
13352 if (id_declarator && id_declarator->kind == cdk_id)
13353 error_at (start_token->location,
13354 "template parameter pack %qD cannot have a default argument",
13355 id_declarator->u.id.unqualified_name);
13356 else
13357 error_at (start_token->location,
13358 "template parameter pack cannot have a default argument");
13360 /* Parse the default argument, but throw away the result. */
13361 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13364 parm = grokdeclarator (parameter_declarator->declarator,
13365 &parameter_declarator->decl_specifiers,
13366 TPARM, /*initialized=*/0,
13367 /*attrlist=*/NULL);
13368 if (parm == error_mark_node)
13369 return error_mark_node;
13371 return build_tree_list (parameter_declarator->default_argument, parm);
13374 /* Parse a type-parameter.
13376 type-parameter:
13377 class identifier [opt]
13378 class identifier [opt] = type-id
13379 typename identifier [opt]
13380 typename identifier [opt] = type-id
13381 template < template-parameter-list > class identifier [opt]
13382 template < template-parameter-list > class identifier [opt]
13383 = id-expression
13385 GNU Extension (variadic templates):
13387 type-parameter:
13388 class ... identifier [opt]
13389 typename ... identifier [opt]
13391 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13392 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13393 the declaration of the parameter.
13395 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13397 static tree
13398 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13400 cp_token *token;
13401 tree parameter;
13403 /* Look for a keyword to tell us what kind of parameter this is. */
13404 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13405 if (!token)
13406 return error_mark_node;
13408 switch (token->keyword)
13410 case RID_CLASS:
13411 case RID_TYPENAME:
13413 tree identifier;
13414 tree default_argument;
13416 /* If the next token is an ellipsis, we have a template
13417 argument pack. */
13418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13420 /* Consume the `...' token. */
13421 cp_lexer_consume_token (parser->lexer);
13422 maybe_warn_variadic_templates ();
13424 *is_parameter_pack = true;
13427 /* If the next token is an identifier, then it names the
13428 parameter. */
13429 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13430 identifier = cp_parser_identifier (parser);
13431 else
13432 identifier = NULL_TREE;
13434 /* Create the parameter. */
13435 parameter = finish_template_type_parm (class_type_node, identifier);
13437 /* If the next token is an `=', we have a default argument. */
13438 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13440 /* Consume the `=' token. */
13441 cp_lexer_consume_token (parser->lexer);
13442 /* Parse the default-argument. */
13443 push_deferring_access_checks (dk_no_deferred);
13444 default_argument = cp_parser_type_id (parser);
13446 /* Template parameter packs cannot have default
13447 arguments. */
13448 if (*is_parameter_pack)
13450 if (identifier)
13451 error_at (token->location,
13452 "template parameter pack %qD cannot have a "
13453 "default argument", identifier);
13454 else
13455 error_at (token->location,
13456 "template parameter packs cannot have "
13457 "default arguments");
13458 default_argument = NULL_TREE;
13460 pop_deferring_access_checks ();
13462 else
13463 default_argument = NULL_TREE;
13465 /* Create the combined representation of the parameter and the
13466 default argument. */
13467 parameter = build_tree_list (default_argument, parameter);
13469 break;
13471 case RID_TEMPLATE:
13473 tree identifier;
13474 tree default_argument;
13476 // Save the current requirements before parsing the
13477 // template parameter list.
13478 tree saved_template_reqs = release (current_template_reqs);
13480 /* Look for the `<'. */
13481 cp_parser_require (parser, CPP_LESS, RT_LESS);
13482 /* Parse the template-parameter-list. */
13483 cp_parser_template_parameter_list (parser);
13484 /* Look for the `>'. */
13485 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13487 // If template requirements are present, parse them.
13488 if (flag_concepts)
13490 tree reqs = get_shorthand_requirements (current_template_parms);
13491 if (tree r = cp_parser_requires_clause_opt (parser))
13492 reqs = conjoin_requirements (reqs, r);
13493 current_template_reqs = finish_template_requirements (reqs);
13495 // Attach the constraints to the parameter list.
13496 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
13497 = current_template_reqs;
13500 /* Look for the `class' keyword. */
13501 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13502 /* If the next token is an ellipsis, we have a template
13503 argument pack. */
13504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13506 /* Consume the `...' token. */
13507 cp_lexer_consume_token (parser->lexer);
13508 maybe_warn_variadic_templates ();
13510 *is_parameter_pack = true;
13512 /* If the next token is an `=', then there is a
13513 default-argument. If the next token is a `>', we are at
13514 the end of the parameter-list. If the next token is a `,',
13515 then we are at the end of this parameter. */
13516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13517 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13518 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13520 identifier = cp_parser_identifier (parser);
13521 /* Treat invalid names as if the parameter were nameless. */
13522 if (identifier == error_mark_node)
13523 identifier = NULL_TREE;
13525 else
13526 identifier = NULL_TREE;
13528 /* Create the template parameter. */
13529 parameter = finish_template_template_parm (class_type_node,
13530 identifier);
13532 // Restore the saved constraints.
13533 current_template_reqs = saved_template_reqs;
13535 /* If the next token is an `=', then there is a
13536 default-argument. */
13537 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13539 bool is_template;
13541 /* Consume the `='. */
13542 cp_lexer_consume_token (parser->lexer);
13543 /* Parse the id-expression. */
13544 push_deferring_access_checks (dk_no_deferred);
13545 /* save token before parsing the id-expression, for error
13546 reporting */
13547 token = cp_lexer_peek_token (parser->lexer);
13548 default_argument
13549 = cp_parser_id_expression (parser,
13550 /*template_keyword_p=*/false,
13551 /*check_dependency_p=*/true,
13552 /*template_p=*/&is_template,
13553 /*declarator_p=*/false,
13554 /*optional_p=*/false);
13555 if (TREE_CODE (default_argument) == TYPE_DECL)
13556 /* If the id-expression was a template-id that refers to
13557 a template-class, we already have the declaration here,
13558 so no further lookup is needed. */
13560 else
13561 /* Look up the name. */
13562 default_argument
13563 = cp_parser_lookup_name (parser, default_argument,
13564 none_type,
13565 /*is_template=*/is_template,
13566 /*is_namespace=*/false,
13567 /*check_dependency=*/true,
13568 /*ambiguous_decls=*/NULL,
13569 token->location);
13570 /* See if the default argument is valid. */
13571 default_argument
13572 = check_template_template_default_arg (default_argument);
13574 /* Template parameter packs cannot have default
13575 arguments. */
13576 if (*is_parameter_pack)
13578 if (identifier)
13579 error_at (token->location,
13580 "template parameter pack %qD cannot "
13581 "have a default argument",
13582 identifier);
13583 else
13584 error_at (token->location, "template parameter packs cannot "
13585 "have default arguments");
13586 default_argument = NULL_TREE;
13588 pop_deferring_access_checks ();
13590 else
13591 default_argument = NULL_TREE;
13593 /* Create the combined representation of the parameter and the
13594 default argument. */
13595 parameter = build_tree_list (default_argument, parameter);
13597 break;
13599 default:
13600 gcc_unreachable ();
13601 break;
13604 return parameter;
13607 /* Parse a template-id.
13609 template-id:
13610 template-name < template-argument-list [opt] >
13612 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13613 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13614 returned. Otherwise, if the template-name names a function, or set
13615 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13616 names a class, returns a TYPE_DECL for the specialization.
13618 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13619 uninstantiated templates. */
13621 static tree
13622 cp_parser_template_id (cp_parser *parser,
13623 bool template_keyword_p,
13624 bool check_dependency_p,
13625 enum tag_types tag_type,
13626 bool is_declaration)
13628 int i;
13629 tree templ;
13630 tree arguments;
13631 tree template_id;
13632 cp_token_position start_of_id = 0;
13633 deferred_access_check *chk;
13634 vec<deferred_access_check, va_gc> *access_check;
13635 cp_token *next_token = NULL, *next_token_2 = NULL;
13636 bool is_identifier;
13638 /* If the next token corresponds to a template-id, there is no need
13639 to reparse it. */
13640 next_token = cp_lexer_peek_token (parser->lexer);
13641 if (next_token->type == CPP_TEMPLATE_ID)
13643 struct tree_check *check_value;
13645 /* Get the stored value. */
13646 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13647 /* Perform any access checks that were deferred. */
13648 access_check = check_value->checks;
13649 if (access_check)
13651 FOR_EACH_VEC_ELT (*access_check, i, chk)
13652 perform_or_defer_access_check (chk->binfo,
13653 chk->decl,
13654 chk->diag_decl,
13655 tf_warning_or_error);
13657 /* Return the stored value. */
13658 return check_value->value;
13661 /* Avoid performing name lookup if there is no possibility of
13662 finding a template-id. */
13663 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13664 || (next_token->type == CPP_NAME
13665 && !cp_parser_nth_token_starts_template_argument_list_p
13666 (parser, 2)))
13668 cp_parser_error (parser, "expected template-id");
13669 return error_mark_node;
13672 /* Remember where the template-id starts. */
13673 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13674 start_of_id = cp_lexer_token_position (parser->lexer, false);
13676 push_deferring_access_checks (dk_deferred);
13678 /* Parse the template-name. */
13679 is_identifier = false;
13680 templ = cp_parser_template_name (parser, template_keyword_p,
13681 check_dependency_p,
13682 is_declaration,
13683 tag_type,
13684 &is_identifier);
13685 if (templ == error_mark_node || is_identifier)
13687 pop_deferring_access_checks ();
13688 return templ;
13691 /* If we find the sequence `[:' after a template-name, it's probably
13692 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13693 parse correctly the argument list. */
13694 next_token = cp_lexer_peek_token (parser->lexer);
13695 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13696 if (next_token->type == CPP_OPEN_SQUARE
13697 && next_token->flags & DIGRAPH
13698 && next_token_2->type == CPP_COLON
13699 && !(next_token_2->flags & PREV_WHITE))
13701 cp_parser_parse_tentatively (parser);
13702 /* Change `:' into `::'. */
13703 next_token_2->type = CPP_SCOPE;
13704 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13705 CPP_LESS. */
13706 cp_lexer_consume_token (parser->lexer);
13708 /* Parse the arguments. */
13709 arguments = cp_parser_enclosed_template_argument_list (parser);
13710 if (!cp_parser_parse_definitely (parser))
13712 /* If we couldn't parse an argument list, then we revert our changes
13713 and return simply an error. Maybe this is not a template-id
13714 after all. */
13715 next_token_2->type = CPP_COLON;
13716 cp_parser_error (parser, "expected %<<%>");
13717 pop_deferring_access_checks ();
13718 return error_mark_node;
13720 /* Otherwise, emit an error about the invalid digraph, but continue
13721 parsing because we got our argument list. */
13722 if (permerror (next_token->location,
13723 "%<<::%> cannot begin a template-argument list"))
13725 static bool hint = false;
13726 inform (next_token->location,
13727 "%<<:%> is an alternate spelling for %<[%>."
13728 " Insert whitespace between %<<%> and %<::%>");
13729 if (!hint && !flag_permissive)
13731 inform (next_token->location, "(if you use %<-fpermissive%> "
13732 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13733 "accept your code)");
13734 hint = true;
13738 else
13740 /* Look for the `<' that starts the template-argument-list. */
13741 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13743 pop_deferring_access_checks ();
13744 return error_mark_node;
13746 /* Parse the arguments. */
13747 arguments = cp_parser_enclosed_template_argument_list (parser);
13750 /* Build a representation of the specialization. */
13751 if (identifier_p (templ))
13752 template_id = build_min_nt_loc (next_token->location,
13753 TEMPLATE_ID_EXPR,
13754 templ, arguments);
13755 else if (DECL_TYPE_TEMPLATE_P (templ)
13756 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13758 bool entering_scope;
13759 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13760 template (rather than some instantiation thereof) only if
13761 is not nested within some other construct. For example, in
13762 "template <typename T> void f(T) { A<T>::", A<T> is just an
13763 instantiation of A. */
13764 entering_scope = (template_parm_scope_p ()
13765 && cp_lexer_next_token_is (parser->lexer,
13766 CPP_SCOPE));
13767 template_id
13768 = finish_template_type (templ, arguments, entering_scope);
13770 else
13772 /* If it's not a class-template or a template-template, it should be
13773 a function-template. */
13774 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13775 || TREE_CODE (templ) == OVERLOAD
13776 || BASELINK_P (templ)));
13778 template_id = lookup_template_function (templ, arguments);
13781 /* If parsing tentatively, replace the sequence of tokens that makes
13782 up the template-id with a CPP_TEMPLATE_ID token. That way,
13783 should we re-parse the token stream, we will not have to repeat
13784 the effort required to do the parse, nor will we issue duplicate
13785 error messages about problems during instantiation of the
13786 template. */
13787 if (start_of_id
13788 /* Don't do this if we had a parse error in a declarator; re-parsing
13789 might succeed if a name changes meaning (60361). */
13790 && !(cp_parser_error_occurred (parser)
13791 && cp_parser_parsing_tentatively (parser)
13792 && parser->in_declarator_p))
13794 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13796 /* Reset the contents of the START_OF_ID token. */
13797 token->type = CPP_TEMPLATE_ID;
13798 /* Retrieve any deferred checks. Do not pop this access checks yet
13799 so the memory will not be reclaimed during token replacing below. */
13800 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13801 token->u.tree_check_value->value = template_id;
13802 token->u.tree_check_value->checks = get_deferred_access_checks ();
13803 token->keyword = RID_MAX;
13805 /* Purge all subsequent tokens. */
13806 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13808 /* ??? Can we actually assume that, if template_id ==
13809 error_mark_node, we will have issued a diagnostic to the
13810 user, as opposed to simply marking the tentative parse as
13811 failed? */
13812 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13813 error_at (token->location, "parse error in template argument list");
13816 pop_to_parent_deferring_access_checks ();
13817 return template_id;
13820 /* Parse a template-name.
13822 template-name:
13823 identifier
13825 The standard should actually say:
13827 template-name:
13828 identifier
13829 operator-function-id
13831 A defect report has been filed about this issue.
13833 A conversion-function-id cannot be a template name because they cannot
13834 be part of a template-id. In fact, looking at this code:
13836 a.operator K<int>()
13838 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13839 It is impossible to call a templated conversion-function-id with an
13840 explicit argument list, since the only allowed template parameter is
13841 the type to which it is converting.
13843 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13844 `template' keyword, in a construction like:
13846 T::template f<3>()
13848 In that case `f' is taken to be a template-name, even though there
13849 is no way of knowing for sure.
13851 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13852 name refers to a set of overloaded functions, at least one of which
13853 is a template, or an IDENTIFIER_NODE with the name of the template,
13854 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13855 names are looked up inside uninstantiated templates. */
13857 static tree
13858 cp_parser_template_name (cp_parser* parser,
13859 bool template_keyword_p,
13860 bool check_dependency_p,
13861 bool is_declaration,
13862 enum tag_types tag_type,
13863 bool *is_identifier)
13865 tree identifier;
13866 tree decl;
13867 tree fns;
13868 cp_token *token = cp_lexer_peek_token (parser->lexer);
13870 /* If the next token is `operator', then we have either an
13871 operator-function-id or a conversion-function-id. */
13872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13874 /* We don't know whether we're looking at an
13875 operator-function-id or a conversion-function-id. */
13876 cp_parser_parse_tentatively (parser);
13877 /* Try an operator-function-id. */
13878 identifier = cp_parser_operator_function_id (parser);
13879 /* If that didn't work, try a conversion-function-id. */
13880 if (!cp_parser_parse_definitely (parser))
13882 cp_parser_error (parser, "expected template-name");
13883 return error_mark_node;
13886 /* Look for the identifier. */
13887 else
13888 identifier = cp_parser_identifier (parser);
13890 /* If we didn't find an identifier, we don't have a template-id. */
13891 if (identifier == error_mark_node)
13892 return error_mark_node;
13894 /* If the name immediately followed the `template' keyword, then it
13895 is a template-name. However, if the next token is not `<', then
13896 we do not treat it as a template-name, since it is not being used
13897 as part of a template-id. This enables us to handle constructs
13898 like:
13900 template <typename T> struct S { S(); };
13901 template <typename T> S<T>::S();
13903 correctly. We would treat `S' as a template -- if it were `S<T>'
13904 -- but we do not if there is no `<'. */
13906 if (processing_template_decl
13907 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13909 /* In a declaration, in a dependent context, we pretend that the
13910 "template" keyword was present in order to improve error
13911 recovery. For example, given:
13913 template <typename T> void f(T::X<int>);
13915 we want to treat "X<int>" as a template-id. */
13916 if (is_declaration
13917 && !template_keyword_p
13918 && parser->scope && TYPE_P (parser->scope)
13919 && check_dependency_p
13920 && dependent_scope_p (parser->scope)
13921 /* Do not do this for dtors (or ctors), since they never
13922 need the template keyword before their name. */
13923 && !constructor_name_p (identifier, parser->scope))
13925 cp_token_position start = 0;
13927 /* Explain what went wrong. */
13928 error_at (token->location, "non-template %qD used as template",
13929 identifier);
13930 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13931 parser->scope, identifier);
13932 /* If parsing tentatively, find the location of the "<" token. */
13933 if (cp_parser_simulate_error (parser))
13934 start = cp_lexer_token_position (parser->lexer, true);
13935 /* Parse the template arguments so that we can issue error
13936 messages about them. */
13937 cp_lexer_consume_token (parser->lexer);
13938 cp_parser_enclosed_template_argument_list (parser);
13939 /* Skip tokens until we find a good place from which to
13940 continue parsing. */
13941 cp_parser_skip_to_closing_parenthesis (parser,
13942 /*recovering=*/true,
13943 /*or_comma=*/true,
13944 /*consume_paren=*/false);
13945 /* If parsing tentatively, permanently remove the
13946 template argument list. That will prevent duplicate
13947 error messages from being issued about the missing
13948 "template" keyword. */
13949 if (start)
13950 cp_lexer_purge_tokens_after (parser->lexer, start);
13951 if (is_identifier)
13952 *is_identifier = true;
13953 return identifier;
13956 /* If the "template" keyword is present, then there is generally
13957 no point in doing name-lookup, so we just return IDENTIFIER.
13958 But, if the qualifying scope is non-dependent then we can
13959 (and must) do name-lookup normally. */
13960 if (template_keyword_p
13961 && (!parser->scope
13962 || (TYPE_P (parser->scope)
13963 && dependent_type_p (parser->scope))))
13964 return identifier;
13967 /* Look up the name. */
13968 decl = cp_parser_lookup_name (parser, identifier,
13969 tag_type,
13970 /*is_template=*/true,
13971 /*is_namespace=*/false,
13972 check_dependency_p,
13973 /*ambiguous_decls=*/NULL,
13974 token->location);
13976 /* If DECL is a template, then the name was a template-name. */
13977 if (TREE_CODE (decl) == TEMPLATE_DECL)
13979 else
13981 tree fn = NULL_TREE;
13983 /* The standard does not explicitly indicate whether a name that
13984 names a set of overloaded declarations, some of which are
13985 templates, is a template-name. However, such a name should
13986 be a template-name; otherwise, there is no way to form a
13987 template-id for the overloaded templates. */
13988 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13989 if (TREE_CODE (fns) == OVERLOAD)
13990 for (fn = fns; fn; fn = OVL_NEXT (fn))
13991 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13992 break;
13994 if (!fn)
13996 /* The name does not name a template. */
13997 cp_parser_error (parser, "expected template-name");
13998 return error_mark_node;
14002 /* If DECL is dependent, and refers to a function, then just return
14003 its name; we will look it up again during template instantiation. */
14004 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14006 tree scope = ovl_scope (decl);
14007 if (TYPE_P (scope) && dependent_type_p (scope))
14008 return identifier;
14011 return decl;
14014 /* Parse a template-argument-list.
14016 template-argument-list:
14017 template-argument ... [opt]
14018 template-argument-list , template-argument ... [opt]
14020 Returns a TREE_VEC containing the arguments. */
14022 static tree
14023 cp_parser_template_argument_list (cp_parser* parser)
14025 tree fixed_args[10];
14026 unsigned n_args = 0;
14027 unsigned alloced = 10;
14028 tree *arg_ary = fixed_args;
14029 tree vec;
14030 bool saved_in_template_argument_list_p;
14031 bool saved_ice_p;
14032 bool saved_non_ice_p;
14034 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14035 parser->in_template_argument_list_p = true;
14036 /* Even if the template-id appears in an integral
14037 constant-expression, the contents of the argument list do
14038 not. */
14039 saved_ice_p = parser->integral_constant_expression_p;
14040 parser->integral_constant_expression_p = false;
14041 saved_non_ice_p = parser->non_integral_constant_expression_p;
14042 parser->non_integral_constant_expression_p = false;
14044 /* Parse the arguments. */
14047 tree argument;
14049 if (n_args)
14050 /* Consume the comma. */
14051 cp_lexer_consume_token (parser->lexer);
14053 /* Parse the template-argument. */
14054 argument = cp_parser_template_argument (parser);
14056 /* If the next token is an ellipsis, we're expanding a template
14057 argument pack. */
14058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14060 if (argument == error_mark_node)
14062 cp_token *token = cp_lexer_peek_token (parser->lexer);
14063 error_at (token->location,
14064 "expected parameter pack before %<...%>");
14066 /* Consume the `...' token. */
14067 cp_lexer_consume_token (parser->lexer);
14069 /* Make the argument into a TYPE_PACK_EXPANSION or
14070 EXPR_PACK_EXPANSION. */
14071 argument = make_pack_expansion (argument);
14074 if (n_args == alloced)
14076 alloced *= 2;
14078 if (arg_ary == fixed_args)
14080 arg_ary = XNEWVEC (tree, alloced);
14081 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14083 else
14084 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14086 arg_ary[n_args++] = argument;
14088 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14090 vec = make_tree_vec (n_args);
14092 while (n_args--)
14093 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14095 if (arg_ary != fixed_args)
14096 free (arg_ary);
14097 parser->non_integral_constant_expression_p = saved_non_ice_p;
14098 parser->integral_constant_expression_p = saved_ice_p;
14099 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14100 #ifdef ENABLE_CHECKING
14101 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14102 #endif
14103 return vec;
14106 /* Parse a template-argument.
14108 template-argument:
14109 assignment-expression
14110 type-id
14111 id-expression
14113 The representation is that of an assignment-expression, type-id, or
14114 id-expression -- except that the qualified id-expression is
14115 evaluated, so that the value returned is either a DECL or an
14116 OVERLOAD.
14118 Although the standard says "assignment-expression", it forbids
14119 throw-expressions or assignments in the template argument.
14120 Therefore, we use "conditional-expression" instead. */
14122 static tree
14123 cp_parser_template_argument (cp_parser* parser)
14125 tree argument;
14126 bool template_p;
14127 bool address_p;
14128 bool maybe_type_id = false;
14129 cp_token *token = NULL, *argument_start_token = NULL;
14130 location_t loc = 0;
14131 cp_id_kind idk;
14133 /* There's really no way to know what we're looking at, so we just
14134 try each alternative in order.
14136 [temp.arg]
14138 In a template-argument, an ambiguity between a type-id and an
14139 expression is resolved to a type-id, regardless of the form of
14140 the corresponding template-parameter.
14142 Therefore, we try a type-id first. */
14143 cp_parser_parse_tentatively (parser);
14144 argument = cp_parser_template_type_arg (parser);
14145 /* If there was no error parsing the type-id but the next token is a
14146 '>>', our behavior depends on which dialect of C++ we're
14147 parsing. In C++98, we probably found a typo for '> >'. But there
14148 are type-id which are also valid expressions. For instance:
14150 struct X { int operator >> (int); };
14151 template <int V> struct Foo {};
14152 Foo<X () >> 5> r;
14154 Here 'X()' is a valid type-id of a function type, but the user just
14155 wanted to write the expression "X() >> 5". Thus, we remember that we
14156 found a valid type-id, but we still try to parse the argument as an
14157 expression to see what happens.
14159 In C++0x, the '>>' will be considered two separate '>'
14160 tokens. */
14161 if (!cp_parser_error_occurred (parser)
14162 && cxx_dialect == cxx98
14163 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14165 maybe_type_id = true;
14166 cp_parser_abort_tentative_parse (parser);
14168 else
14170 /* If the next token isn't a `,' or a `>', then this argument wasn't
14171 really finished. This means that the argument is not a valid
14172 type-id. */
14173 if (!cp_parser_next_token_ends_template_argument_p (parser))
14174 cp_parser_error (parser, "expected template-argument");
14175 /* If that worked, we're done. */
14176 if (cp_parser_parse_definitely (parser))
14177 return argument;
14179 /* We're still not sure what the argument will be. */
14180 cp_parser_parse_tentatively (parser);
14181 /* Try a template. */
14182 argument_start_token = cp_lexer_peek_token (parser->lexer);
14183 argument = cp_parser_id_expression (parser,
14184 /*template_keyword_p=*/false,
14185 /*check_dependency_p=*/true,
14186 &template_p,
14187 /*declarator_p=*/false,
14188 /*optional_p=*/false);
14189 /* If the next token isn't a `,' or a `>', then this argument wasn't
14190 really finished. */
14191 if (!cp_parser_next_token_ends_template_argument_p (parser))
14192 cp_parser_error (parser, "expected template-argument");
14193 if (!cp_parser_error_occurred (parser))
14195 /* Figure out what is being referred to. If the id-expression
14196 was for a class template specialization, then we will have a
14197 TYPE_DECL at this point. There is no need to do name lookup
14198 at this point in that case. */
14199 if (TREE_CODE (argument) != TYPE_DECL)
14200 argument = cp_parser_lookup_name (parser, argument,
14201 none_type,
14202 /*is_template=*/template_p,
14203 /*is_namespace=*/false,
14204 /*check_dependency=*/true,
14205 /*ambiguous_decls=*/NULL,
14206 argument_start_token->location);
14207 if (TREE_CODE (argument) != TEMPLATE_DECL
14208 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14209 cp_parser_error (parser, "expected template-name");
14211 if (cp_parser_parse_definitely (parser))
14212 return argument;
14213 /* It must be a non-type argument. There permitted cases are given
14214 in [temp.arg.nontype]:
14216 -- an integral constant-expression of integral or enumeration
14217 type; or
14219 -- the name of a non-type template-parameter; or
14221 -- the name of an object or function with external linkage...
14223 -- the address of an object or function with external linkage...
14225 -- a pointer to member... */
14226 /* Look for a non-type template parameter. */
14227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14229 cp_parser_parse_tentatively (parser);
14230 argument = cp_parser_primary_expression (parser,
14231 /*address_p=*/false,
14232 /*cast_p=*/false,
14233 /*template_arg_p=*/true,
14234 &idk);
14235 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14236 || !cp_parser_next_token_ends_template_argument_p (parser))
14237 cp_parser_simulate_error (parser);
14238 if (cp_parser_parse_definitely (parser))
14239 return argument;
14242 /* If the next token is "&", the argument must be the address of an
14243 object or function with external linkage. */
14244 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14245 if (address_p)
14247 loc = cp_lexer_peek_token (parser->lexer)->location;
14248 cp_lexer_consume_token (parser->lexer);
14250 /* See if we might have an id-expression. */
14251 token = cp_lexer_peek_token (parser->lexer);
14252 if (token->type == CPP_NAME
14253 || token->keyword == RID_OPERATOR
14254 || token->type == CPP_SCOPE
14255 || token->type == CPP_TEMPLATE_ID
14256 || token->type == CPP_NESTED_NAME_SPECIFIER)
14258 cp_parser_parse_tentatively (parser);
14259 argument = cp_parser_primary_expression (parser,
14260 address_p,
14261 /*cast_p=*/false,
14262 /*template_arg_p=*/true,
14263 &idk);
14264 if (cp_parser_error_occurred (parser)
14265 || !cp_parser_next_token_ends_template_argument_p (parser))
14266 cp_parser_abort_tentative_parse (parser);
14267 else
14269 tree probe;
14271 if (INDIRECT_REF_P (argument))
14273 /* Strip the dereference temporarily. */
14274 gcc_assert (REFERENCE_REF_P (argument));
14275 argument = TREE_OPERAND (argument, 0);
14278 /* If we're in a template, we represent a qualified-id referring
14279 to a static data member as a SCOPE_REF even if the scope isn't
14280 dependent so that we can check access control later. */
14281 probe = argument;
14282 if (TREE_CODE (probe) == SCOPE_REF)
14283 probe = TREE_OPERAND (probe, 1);
14284 if (VAR_P (probe))
14286 /* A variable without external linkage might still be a
14287 valid constant-expression, so no error is issued here
14288 if the external-linkage check fails. */
14289 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14290 cp_parser_simulate_error (parser);
14292 else if (is_overloaded_fn (argument))
14293 /* All overloaded functions are allowed; if the external
14294 linkage test does not pass, an error will be issued
14295 later. */
14297 else if (address_p
14298 && (TREE_CODE (argument) == OFFSET_REF
14299 || TREE_CODE (argument) == SCOPE_REF))
14300 /* A pointer-to-member. */
14302 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14304 else
14305 cp_parser_simulate_error (parser);
14307 if (cp_parser_parse_definitely (parser))
14309 if (address_p)
14310 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14311 tf_warning_or_error);
14312 else
14313 argument = convert_from_reference (argument);
14314 return argument;
14318 /* If the argument started with "&", there are no other valid
14319 alternatives at this point. */
14320 if (address_p)
14322 cp_parser_error (parser, "invalid non-type template argument");
14323 return error_mark_node;
14326 /* If the argument wasn't successfully parsed as a type-id followed
14327 by '>>', the argument can only be a constant expression now.
14328 Otherwise, we try parsing the constant-expression tentatively,
14329 because the argument could really be a type-id. */
14330 if (maybe_type_id)
14331 cp_parser_parse_tentatively (parser);
14332 argument = cp_parser_constant_expression (parser,
14333 /*allow_non_constant_p=*/false,
14334 /*non_constant_p=*/NULL);
14335 if (!maybe_type_id)
14336 return argument;
14337 if (!cp_parser_next_token_ends_template_argument_p (parser))
14338 cp_parser_error (parser, "expected template-argument");
14339 if (cp_parser_parse_definitely (parser))
14340 return argument;
14341 /* We did our best to parse the argument as a non type-id, but that
14342 was the only alternative that matched (albeit with a '>' after
14343 it). We can assume it's just a typo from the user, and a
14344 diagnostic will then be issued. */
14345 return cp_parser_template_type_arg (parser);
14348 /* Parse an explicit-instantiation.
14350 explicit-instantiation:
14351 template declaration
14353 Although the standard says `declaration', what it really means is:
14355 explicit-instantiation:
14356 template decl-specifier-seq [opt] declarator [opt] ;
14358 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14359 supposed to be allowed. A defect report has been filed about this
14360 issue.
14362 GNU Extension:
14364 explicit-instantiation:
14365 storage-class-specifier template
14366 decl-specifier-seq [opt] declarator [opt] ;
14367 function-specifier template
14368 decl-specifier-seq [opt] declarator [opt] ; */
14370 static void
14371 cp_parser_explicit_instantiation (cp_parser* parser)
14373 int declares_class_or_enum;
14374 cp_decl_specifier_seq decl_specifiers;
14375 tree extension_specifier = NULL_TREE;
14377 timevar_push (TV_TEMPLATE_INST);
14379 /* Look for an (optional) storage-class-specifier or
14380 function-specifier. */
14381 if (cp_parser_allow_gnu_extensions_p (parser))
14383 extension_specifier
14384 = cp_parser_storage_class_specifier_opt (parser);
14385 if (!extension_specifier)
14386 extension_specifier
14387 = cp_parser_function_specifier_opt (parser,
14388 /*decl_specs=*/NULL);
14391 /* Look for the `template' keyword. */
14392 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14393 /* Let the front end know that we are processing an explicit
14394 instantiation. */
14395 begin_explicit_instantiation ();
14396 /* [temp.explicit] says that we are supposed to ignore access
14397 control while processing explicit instantiation directives. */
14398 push_deferring_access_checks (dk_no_check);
14399 /* Parse a decl-specifier-seq. */
14400 cp_parser_decl_specifier_seq (parser,
14401 CP_PARSER_FLAGS_OPTIONAL,
14402 &decl_specifiers,
14403 &declares_class_or_enum);
14404 /* If there was exactly one decl-specifier, and it declared a class,
14405 and there's no declarator, then we have an explicit type
14406 instantiation. */
14407 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14409 tree type;
14411 type = check_tag_decl (&decl_specifiers,
14412 /*explicit_type_instantiation_p=*/true);
14413 /* Turn access control back on for names used during
14414 template instantiation. */
14415 pop_deferring_access_checks ();
14416 if (type)
14417 do_type_instantiation (type, extension_specifier,
14418 /*complain=*/tf_error);
14420 else
14422 cp_declarator *declarator;
14423 tree decl;
14425 /* Parse the declarator. */
14426 declarator
14427 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14428 /*ctor_dtor_or_conv_p=*/NULL,
14429 /*parenthesized_p=*/NULL,
14430 /*member_p=*/false);
14431 if (declares_class_or_enum & 2)
14432 cp_parser_check_for_definition_in_return_type (declarator,
14433 decl_specifiers.type,
14434 decl_specifiers.locations[ds_type_spec]);
14435 if (declarator != cp_error_declarator)
14437 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14438 permerror (decl_specifiers.locations[ds_inline],
14439 "explicit instantiation shall not use"
14440 " %<inline%> specifier");
14441 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14442 permerror (decl_specifiers.locations[ds_constexpr],
14443 "explicit instantiation shall not use"
14444 " %<constexpr%> specifier");
14446 decl = grokdeclarator (declarator, &decl_specifiers,
14447 NORMAL, 0, &decl_specifiers.attributes);
14448 /* Turn access control back on for names used during
14449 template instantiation. */
14450 pop_deferring_access_checks ();
14451 /* Do the explicit instantiation. */
14452 do_decl_instantiation (decl, extension_specifier);
14454 else
14456 pop_deferring_access_checks ();
14457 /* Skip the body of the explicit instantiation. */
14458 cp_parser_skip_to_end_of_statement (parser);
14461 /* We're done with the instantiation. */
14462 end_explicit_instantiation ();
14464 cp_parser_consume_semicolon_at_end_of_statement (parser);
14466 timevar_pop (TV_TEMPLATE_INST);
14469 /* Parse an explicit-specialization.
14471 explicit-specialization:
14472 template < > declaration
14474 Although the standard says `declaration', what it really means is:
14476 explicit-specialization:
14477 template <> decl-specifier [opt] init-declarator [opt] ;
14478 template <> function-definition
14479 template <> explicit-specialization
14480 template <> template-declaration */
14482 static void
14483 cp_parser_explicit_specialization (cp_parser* parser)
14485 bool need_lang_pop;
14486 cp_token *token = cp_lexer_peek_token (parser->lexer);
14488 /* Look for the `template' keyword. */
14489 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14490 /* Look for the `<'. */
14491 cp_parser_require (parser, CPP_LESS, RT_LESS);
14492 /* Look for the `>'. */
14493 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14494 /* We have processed another parameter list. */
14495 ++parser->num_template_parameter_lists;
14496 /* [temp]
14498 A template ... explicit specialization ... shall not have C
14499 linkage. */
14500 if (current_lang_name == lang_name_c)
14502 error_at (token->location, "template specialization with C linkage");
14503 /* Give it C++ linkage to avoid confusing other parts of the
14504 front end. */
14505 push_lang_context (lang_name_cplusplus);
14506 need_lang_pop = true;
14508 else
14509 need_lang_pop = false;
14510 /* Let the front end know that we are beginning a specialization. */
14511 if (!begin_specialization ())
14513 end_specialization ();
14514 return;
14517 /* If the next keyword is `template', we need to figure out whether
14518 or not we're looking a template-declaration. */
14519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14521 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14522 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14523 cp_parser_template_declaration_after_export (parser,
14524 /*member_p=*/false);
14525 else
14526 cp_parser_explicit_specialization (parser);
14528 else
14529 /* Parse the dependent declaration. */
14530 cp_parser_single_declaration (parser,
14531 /*checks=*/NULL,
14532 /*member_p=*/false,
14533 /*explicit_specialization_p=*/true,
14534 /*friend_p=*/NULL);
14535 /* We're done with the specialization. */
14536 end_specialization ();
14537 /* For the erroneous case of a template with C linkage, we pushed an
14538 implicit C++ linkage scope; exit that scope now. */
14539 if (need_lang_pop)
14540 pop_lang_context ();
14541 /* We're done with this parameter list. */
14542 --parser->num_template_parameter_lists;
14545 /* Parse a type-specifier.
14547 type-specifier:
14548 simple-type-specifier
14549 class-specifier
14550 enum-specifier
14551 elaborated-type-specifier
14552 cv-qualifier
14554 GNU Extension:
14556 type-specifier:
14557 __complex__
14559 Returns a representation of the type-specifier. For a
14560 class-specifier, enum-specifier, or elaborated-type-specifier, a
14561 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14563 The parser flags FLAGS is used to control type-specifier parsing.
14565 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14566 in a decl-specifier-seq.
14568 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14569 class-specifier, enum-specifier, or elaborated-type-specifier, then
14570 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14571 if a type is declared; 2 if it is defined. Otherwise, it is set to
14572 zero.
14574 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14575 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14576 is set to FALSE. */
14578 static tree
14579 cp_parser_type_specifier (cp_parser* parser,
14580 cp_parser_flags flags,
14581 cp_decl_specifier_seq *decl_specs,
14582 bool is_declaration,
14583 int* declares_class_or_enum,
14584 bool* is_cv_qualifier)
14586 tree type_spec = NULL_TREE;
14587 cp_token *token;
14588 enum rid keyword;
14589 cp_decl_spec ds = ds_last;
14591 /* Assume this type-specifier does not declare a new type. */
14592 if (declares_class_or_enum)
14593 *declares_class_or_enum = 0;
14594 /* And that it does not specify a cv-qualifier. */
14595 if (is_cv_qualifier)
14596 *is_cv_qualifier = false;
14597 /* Peek at the next token. */
14598 token = cp_lexer_peek_token (parser->lexer);
14600 /* If we're looking at a keyword, we can use that to guide the
14601 production we choose. */
14602 keyword = token->keyword;
14603 switch (keyword)
14605 case RID_ENUM:
14606 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14607 goto elaborated_type_specifier;
14609 /* Look for the enum-specifier. */
14610 type_spec = cp_parser_enum_specifier (parser);
14611 /* If that worked, we're done. */
14612 if (type_spec)
14614 if (declares_class_or_enum)
14615 *declares_class_or_enum = 2;
14616 if (decl_specs)
14617 cp_parser_set_decl_spec_type (decl_specs,
14618 type_spec,
14619 token,
14620 /*type_definition_p=*/true);
14621 return type_spec;
14623 else
14624 goto elaborated_type_specifier;
14626 /* Any of these indicate either a class-specifier, or an
14627 elaborated-type-specifier. */
14628 case RID_CLASS:
14629 case RID_STRUCT:
14630 case RID_UNION:
14631 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14632 goto elaborated_type_specifier;
14634 /* Parse tentatively so that we can back up if we don't find a
14635 class-specifier. */
14636 cp_parser_parse_tentatively (parser);
14637 /* Look for the class-specifier. */
14638 type_spec = cp_parser_class_specifier (parser);
14639 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14640 /* If that worked, we're done. */
14641 if (cp_parser_parse_definitely (parser))
14643 if (declares_class_or_enum)
14644 *declares_class_or_enum = 2;
14645 if (decl_specs)
14646 cp_parser_set_decl_spec_type (decl_specs,
14647 type_spec,
14648 token,
14649 /*type_definition_p=*/true);
14650 return type_spec;
14653 /* Fall through. */
14654 elaborated_type_specifier:
14655 /* We're declaring (not defining) a class or enum. */
14656 if (declares_class_or_enum)
14657 *declares_class_or_enum = 1;
14659 /* Fall through. */
14660 case RID_TYPENAME:
14661 /* Look for an elaborated-type-specifier. */
14662 type_spec
14663 = (cp_parser_elaborated_type_specifier
14664 (parser,
14665 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14666 is_declaration));
14667 if (decl_specs)
14668 cp_parser_set_decl_spec_type (decl_specs,
14669 type_spec,
14670 token,
14671 /*type_definition_p=*/false);
14672 return type_spec;
14674 case RID_CONST:
14675 ds = ds_const;
14676 if (is_cv_qualifier)
14677 *is_cv_qualifier = true;
14678 break;
14680 case RID_VOLATILE:
14681 ds = ds_volatile;
14682 if (is_cv_qualifier)
14683 *is_cv_qualifier = true;
14684 break;
14686 case RID_RESTRICT:
14687 ds = ds_restrict;
14688 if (is_cv_qualifier)
14689 *is_cv_qualifier = true;
14690 break;
14692 case RID_COMPLEX:
14693 /* The `__complex__' keyword is a GNU extension. */
14694 ds = ds_complex;
14695 break;
14697 default:
14698 break;
14701 /* Handle simple keywords. */
14702 if (ds != ds_last)
14704 if (decl_specs)
14706 set_and_check_decl_spec_loc (decl_specs, ds, token);
14707 decl_specs->any_specifiers_p = true;
14709 return cp_lexer_consume_token (parser->lexer)->u.value;
14712 /* If we do not already have a type-specifier, assume we are looking
14713 at a simple-type-specifier. */
14714 type_spec = cp_parser_simple_type_specifier (parser,
14715 decl_specs,
14716 flags);
14718 /* If we didn't find a type-specifier, and a type-specifier was not
14719 optional in this context, issue an error message. */
14720 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14722 cp_parser_error (parser, "expected type specifier");
14723 return error_mark_node;
14726 return type_spec;
14729 /* Parse a simple-type-specifier.
14731 simple-type-specifier:
14732 :: [opt] nested-name-specifier [opt] type-name
14733 :: [opt] nested-name-specifier template template-id
14734 char
14735 wchar_t
14736 bool
14737 short
14739 long
14740 signed
14741 unsigned
14742 float
14743 double
14744 void
14746 C++0x Extension:
14748 simple-type-specifier:
14749 auto
14750 decltype ( expression )
14751 char16_t
14752 char32_t
14753 __underlying_type ( type-id )
14755 GNU Extension:
14757 simple-type-specifier:
14758 __int128
14759 __typeof__ unary-expression
14760 __typeof__ ( type-id )
14761 __typeof__ ( type-id ) { initializer-list , [opt] }
14763 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14764 appropriately updated. */
14766 static tree
14767 cp_parser_simple_type_specifier (cp_parser* parser,
14768 cp_decl_specifier_seq *decl_specs,
14769 cp_parser_flags flags)
14771 tree type = NULL_TREE;
14772 cp_token *token;
14774 /* Peek at the next token. */
14775 token = cp_lexer_peek_token (parser->lexer);
14777 /* If we're looking at a keyword, things are easy. */
14778 switch (token->keyword)
14780 case RID_CHAR:
14781 if (decl_specs)
14782 decl_specs->explicit_char_p = true;
14783 type = char_type_node;
14784 break;
14785 case RID_CHAR16:
14786 type = char16_type_node;
14787 break;
14788 case RID_CHAR32:
14789 type = char32_type_node;
14790 break;
14791 case RID_WCHAR:
14792 type = wchar_type_node;
14793 break;
14794 case RID_BOOL:
14795 type = boolean_type_node;
14796 break;
14797 case RID_SHORT:
14798 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14799 type = short_integer_type_node;
14800 break;
14801 case RID_INT:
14802 if (decl_specs)
14803 decl_specs->explicit_int_p = true;
14804 type = integer_type_node;
14805 break;
14806 case RID_INT128:
14807 if (!int128_integer_type_node)
14808 break;
14809 if (decl_specs)
14810 decl_specs->explicit_int128_p = true;
14811 type = int128_integer_type_node;
14812 break;
14813 case RID_LONG:
14814 if (decl_specs)
14815 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14816 type = long_integer_type_node;
14817 break;
14818 case RID_SIGNED:
14819 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14820 type = integer_type_node;
14821 break;
14822 case RID_UNSIGNED:
14823 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14824 type = unsigned_type_node;
14825 break;
14826 case RID_FLOAT:
14827 type = float_type_node;
14828 break;
14829 case RID_DOUBLE:
14830 type = double_type_node;
14831 break;
14832 case RID_VOID:
14833 type = void_type_node;
14834 break;
14836 case RID_AUTO:
14837 maybe_warn_cpp0x (CPP0X_AUTO);
14838 if (parser->auto_is_implicit_function_template_parm_p)
14840 type = synthesize_implicit_template_parm (parser, NULL_TREE);
14842 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14844 if (cxx_dialect < cxx1y)
14845 pedwarn (location_of (type), 0,
14846 "use of %<auto%> in lambda parameter declaration "
14847 "only available with "
14848 "-std=c++1y or -std=gnu++1y");
14850 else if (cxx_dialect < cxx1y)
14851 pedwarn (location_of (type), 0,
14852 "use of %<auto%> in parameter declaration "
14853 "only available with "
14854 "-std=c++1y or -std=gnu++1y");
14855 else
14856 pedwarn (location_of (type), OPT_Wpedantic,
14857 "ISO C++ forbids use of %<auto%> in parameter "
14858 "declaration");
14860 else
14861 type = make_auto ();
14862 break;
14864 case RID_DECLTYPE:
14865 /* Since DR 743, decltype can either be a simple-type-specifier by
14866 itself or begin a nested-name-specifier. Parsing it will replace
14867 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14868 handling below decide what to do. */
14869 cp_parser_decltype (parser);
14870 cp_lexer_set_token_position (parser->lexer, token);
14871 break;
14873 case RID_TYPEOF:
14874 /* Consume the `typeof' token. */
14875 cp_lexer_consume_token (parser->lexer);
14876 /* Parse the operand to `typeof'. */
14877 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14878 /* If it is not already a TYPE, take its type. */
14879 if (!TYPE_P (type))
14880 type = finish_typeof (type);
14882 if (decl_specs)
14883 cp_parser_set_decl_spec_type (decl_specs, type,
14884 token,
14885 /*type_definition_p=*/false);
14887 return type;
14889 case RID_UNDERLYING_TYPE:
14890 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14891 if (decl_specs)
14892 cp_parser_set_decl_spec_type (decl_specs, type,
14893 token,
14894 /*type_definition_p=*/false);
14896 return type;
14898 case RID_BASES:
14899 case RID_DIRECT_BASES:
14900 type = cp_parser_trait_expr (parser, token->keyword);
14901 if (decl_specs)
14902 cp_parser_set_decl_spec_type (decl_specs, type,
14903 token,
14904 /*type_definition_p=*/false);
14905 return type;
14906 default:
14907 break;
14910 /* If token is an already-parsed decltype not followed by ::,
14911 it's a simple-type-specifier. */
14912 if (token->type == CPP_DECLTYPE
14913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14915 type = token->u.value;
14916 if (decl_specs)
14917 cp_parser_set_decl_spec_type (decl_specs, type,
14918 token,
14919 /*type_definition_p=*/false);
14920 cp_lexer_consume_token (parser->lexer);
14921 return type;
14924 /* If the type-specifier was for a built-in type, we're done. */
14925 if (type)
14927 /* Record the type. */
14928 if (decl_specs
14929 && (token->keyword != RID_SIGNED
14930 && token->keyword != RID_UNSIGNED
14931 && token->keyword != RID_SHORT
14932 && token->keyword != RID_LONG))
14933 cp_parser_set_decl_spec_type (decl_specs,
14934 type,
14935 token,
14936 /*type_definition_p=*/false);
14937 if (decl_specs)
14938 decl_specs->any_specifiers_p = true;
14940 /* Consume the token. */
14941 cp_lexer_consume_token (parser->lexer);
14943 /* There is no valid C++ program where a non-template type is
14944 followed by a "<". That usually indicates that the user thought
14945 that the type was a template. */
14946 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14947 token->location);
14949 return TYPE_NAME (type);
14952 /* The type-specifier must be a user-defined type. */
14953 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14955 bool qualified_p;
14956 bool global_p;
14958 /* Don't gobble tokens or issue error messages if this is an
14959 optional type-specifier. */
14960 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14961 cp_parser_parse_tentatively (parser);
14963 /* Look for the optional `::' operator. */
14964 global_p
14965 = (cp_parser_global_scope_opt (parser,
14966 /*current_scope_valid_p=*/false)
14967 != NULL_TREE);
14968 /* Look for the nested-name specifier. */
14969 qualified_p
14970 = (cp_parser_nested_name_specifier_opt (parser,
14971 /*typename_keyword_p=*/false,
14972 /*check_dependency_p=*/true,
14973 /*type_p=*/false,
14974 /*is_declaration=*/false)
14975 != NULL_TREE);
14976 token = cp_lexer_peek_token (parser->lexer);
14977 /* If we have seen a nested-name-specifier, and the next token
14978 is `template', then we are using the template-id production. */
14979 if (parser->scope
14980 && cp_parser_optional_template_keyword (parser))
14982 /* Look for the template-id. */
14983 type = cp_parser_template_id (parser,
14984 /*template_keyword_p=*/true,
14985 /*check_dependency_p=*/true,
14986 none_type,
14987 /*is_declaration=*/false);
14988 /* If the template-id did not name a type, we are out of
14989 luck. */
14990 if (TREE_CODE (type) != TYPE_DECL)
14992 cp_parser_error (parser, "expected template-id for type");
14993 type = NULL_TREE;
14996 /* Otherwise, look for a type-name. */
14997 else
14998 type = cp_parser_type_name (parser);
14999 /* Keep track of all name-lookups performed in class scopes. */
15000 if (type
15001 && !global_p
15002 && !qualified_p
15003 && TREE_CODE (type) == TYPE_DECL
15004 && identifier_p (DECL_NAME (type)))
15005 maybe_note_name_used_in_class (DECL_NAME (type), type);
15006 /* If it didn't work out, we don't have a TYPE. */
15007 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15008 && !cp_parser_parse_definitely (parser))
15009 type = NULL_TREE;
15010 if (type && decl_specs)
15011 cp_parser_set_decl_spec_type (decl_specs, type,
15012 token,
15013 /*type_definition_p=*/false);
15016 /* If we didn't get a type-name, issue an error message. */
15017 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15019 cp_parser_error (parser, "expected type-name");
15020 return error_mark_node;
15023 if (type && type != error_mark_node)
15025 /* See if TYPE is an Objective-C type, and if so, parse and
15026 accept any protocol references following it. Do this before
15027 the cp_parser_check_for_invalid_template_id() call, because
15028 Objective-C types can be followed by '<...>' which would
15029 enclose protocol names rather than template arguments, and so
15030 everything is fine. */
15031 if (c_dialect_objc () && !parser->scope
15032 && (objc_is_id (type) || objc_is_class_name (type)))
15034 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15035 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15037 /* Clobber the "unqualified" type previously entered into
15038 DECL_SPECS with the new, improved protocol-qualified version. */
15039 if (decl_specs)
15040 decl_specs->type = qual_type;
15042 return qual_type;
15045 /* There is no valid C++ program where a non-template type is
15046 followed by a "<". That usually indicates that the user
15047 thought that the type was a template. */
15048 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15049 none_type,
15050 token->location);
15053 return type;
15056 /* Parse a type-name.
15058 type-name:
15059 class-name
15060 enum-name
15061 typedef-name
15062 simple-template-id [in c++0x]
15064 enum-name:
15065 identifier
15067 typedef-name:
15068 identifier
15070 Concepts:
15072 type-name:
15073 concept-name
15075 concept-name:
15076 identifier
15078 Returns a TYPE_DECL for the type. */
15080 static tree
15081 cp_parser_type_name (cp_parser* parser)
15083 tree type_decl;
15085 /* We can't know yet whether it is a class-name or not. */
15086 cp_parser_parse_tentatively (parser);
15087 /* Try a class-name. */
15088 type_decl = cp_parser_class_name (parser,
15089 /*typename_keyword_p=*/false,
15090 /*template_keyword_p=*/false,
15091 none_type,
15092 /*check_dependency_p=*/true,
15093 /*class_head_p=*/false,
15094 /*is_declaration=*/false);
15095 /* If it's not a class-name, keep looking. */
15096 if (!cp_parser_parse_definitely (parser))
15098 if (cxx_dialect < cxx11)
15099 /* It must be a typedef-name or an enum-name. */
15100 return cp_parser_nonclass_name (parser);
15102 cp_parser_parse_tentatively (parser);
15103 /* It is either a simple-template-id representing an
15104 instantiation of an alias template... */
15105 type_decl = cp_parser_template_id (parser,
15106 /*template_keyword_p=*/false,
15107 /*check_dependency_p=*/true,
15108 none_type,
15109 /*is_declaration=*/false);
15110 /* Note that this must be an instantiation of an alias template
15111 because [temp.names]/6 says:
15113 A template-id that names an alias template specialization
15114 is a type-name.
15116 Whereas [temp.names]/7 says:
15118 A simple-template-id that names a class template
15119 specialization is a class-name. */
15120 if (type_decl != NULL_TREE
15121 && TREE_CODE (type_decl) == TYPE_DECL
15122 && TYPE_DECL_ALIAS_P (type_decl))
15123 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15124 else
15125 cp_parser_simulate_error (parser);
15127 if (!cp_parser_parse_definitely (parser))
15128 /* ... Or a typedef-name or an enum-name. */
15129 return cp_parser_nonclass_name (parser);
15132 return type_decl;
15137 // If DECL refers to a concept, return a TYPE_DECL representing the result
15138 // of using the constrained type specifier in the current context.
15140 // DECL refers to a concept if
15141 // - it is an overload set containing a function concept taking a single
15142 // type argument, or
15143 // - it is a variable concept taking a single type argument
15146 // TODO: DECL could be a variable concept.
15147 static tree
15148 cp_check_concept_name (cp_parser* parser, tree decl)
15150 gcc_assert (TREE_CODE (decl) == OVERLOAD);
15152 // Try to build a call expression that evaluates the concept. This
15153 // can fail if the overload set refers only to non-templates.
15154 tree call = build_concept_check (decl, build_nt(PLACEHOLDER_EXPR));
15155 if (call == error_mark_node)
15156 return NULL_TREE;
15158 // Resolve the constraint check to deduce the declared parameter.
15159 tree check = resolve_constraint_check (call);
15160 if (!check)
15161 return NULL_TREE;
15163 // Get function and argument from the resolved check expression. If
15164 // the argument was a pack expansion, then get the first element
15165 // of that pack.
15166 tree fn = TREE_VALUE (check);
15167 tree arg = TREE_VEC_ELT (TREE_PURPOSE (check), 0);
15168 if (ARGUMENT_PACK_P (arg))
15169 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
15171 // Get the protyping parameter bound to the placeholder.
15172 tree proto = TREE_TYPE (arg);
15174 // In template paramteer scope, this results in a constrained parameter.
15175 // Return a descriptor of that parm.
15176 if (template_parm_scope_p () && processing_template_parmlist)
15177 return build_constrained_parameter (proto, fn);
15179 // In a parameter-declaration-clause, constrained-type specifiers
15180 // result in invented template parameters.
15181 if (parser->auto_is_implicit_function_template_parm_p)
15183 tree x = build_constrained_parameter (proto, fn);
15184 tree r = synthesize_implicit_template_parm (parser, x);
15185 return r;
15188 // A concept-name appearing in a result-type constraint is a
15189 // constrained auto. Meaning that type deduction will be applied
15190 // and the constraint checked.
15192 // TODO: Actually bind the constraint to the auto.
15193 if (parser->in_result_type_constraint_p)
15194 return make_auto();
15196 // FIXME: What other contexts accept a constrained-type-specifier?
15197 // - variable declarations
15198 // - trailing return types
15200 return NULL_TREE;
15203 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15204 or a concept-name.
15206 enum-name:
15207 identifier
15209 typedef-name:
15210 identifier
15212 concept-name:
15213 identifier
15215 Returns a TYPE_DECL for the type. */
15217 static tree
15218 cp_parser_nonclass_name (cp_parser* parser)
15220 tree type_decl;
15221 tree identifier;
15223 cp_token *token = cp_lexer_peek_token (parser->lexer);
15224 identifier = cp_parser_identifier (parser);
15225 if (identifier == error_mark_node)
15226 return error_mark_node;
15228 /* Look up the type-name. */
15229 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15230 type_decl = strip_using_decl (type_decl);
15232 // If we found an overload set, then it may refer to a concept-name.
15234 // TODO: The name could also refer to a variable template or an
15235 // introduction (if followed by '{').
15236 if (flag_concepts && TREE_CODE (type_decl) == OVERLOAD)
15238 // Determine whether the overload refers to a concept.
15239 if (tree decl = cp_check_concept_name (parser, type_decl))
15240 return decl;
15243 if (TREE_CODE (type_decl) == USING_DECL)
15245 if (!DECL_DEPENDENT_P (type_decl))
15246 type_decl = strip_using_decl (type_decl);
15247 else if (USING_DECL_TYPENAME_P (type_decl))
15249 /* We have found a type introduced by a using
15250 declaration at class scope that refers to a dependent
15251 type.
15253 using typename :: [opt] nested-name-specifier unqualified-id ;
15255 type_decl = make_typename_type (TREE_TYPE (type_decl),
15256 DECL_NAME (type_decl),
15257 typename_type, tf_error);
15258 if (type_decl != error_mark_node)
15259 type_decl = TYPE_NAME (type_decl);
15263 if (TREE_CODE (type_decl) != TYPE_DECL
15264 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15266 /* See if this is an Objective-C type. */
15267 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15268 tree type = objc_get_protocol_qualified_type (identifier, protos);
15269 if (type)
15270 type_decl = TYPE_NAME (type);
15273 /* Issue an error if we did not find a type-name. */
15274 if (TREE_CODE (type_decl) != TYPE_DECL
15275 /* In Objective-C, we have the complication that class names are
15276 normally type names and start declarations (eg, the
15277 "NSObject" in "NSObject *object;"), but can be used in an
15278 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15279 is an expression. So, a classname followed by a dot is not a
15280 valid type-name. */
15281 || (objc_is_class_name (TREE_TYPE (type_decl))
15282 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15284 if (!cp_parser_simulate_error (parser))
15285 cp_parser_name_lookup_error (parser, identifier, type_decl,
15286 NLE_TYPE, token->location);
15287 return error_mark_node;
15289 /* Remember that the name was used in the definition of the
15290 current class so that we can check later to see if the
15291 meaning would have been different after the class was
15292 entirely defined. */
15293 else if (type_decl != error_mark_node
15294 && !parser->scope)
15295 maybe_note_name_used_in_class (identifier, type_decl);
15297 return type_decl;
15300 /* Parse an elaborated-type-specifier. Note that the grammar given
15301 here incorporates the resolution to DR68.
15303 elaborated-type-specifier:
15304 class-key :: [opt] nested-name-specifier [opt] identifier
15305 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15306 enum-key :: [opt] nested-name-specifier [opt] identifier
15307 typename :: [opt] nested-name-specifier identifier
15308 typename :: [opt] nested-name-specifier template [opt]
15309 template-id
15311 GNU extension:
15313 elaborated-type-specifier:
15314 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15315 class-key attributes :: [opt] nested-name-specifier [opt]
15316 template [opt] template-id
15317 enum attributes :: [opt] nested-name-specifier [opt] identifier
15319 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15320 declared `friend'. If IS_DECLARATION is TRUE, then this
15321 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15322 something is being declared.
15324 Returns the TYPE specified. */
15326 static tree
15327 cp_parser_elaborated_type_specifier (cp_parser* parser,
15328 bool is_friend,
15329 bool is_declaration)
15331 enum tag_types tag_type;
15332 tree identifier;
15333 tree type = NULL_TREE;
15334 tree attributes = NULL_TREE;
15335 tree globalscope;
15336 cp_token *token = NULL;
15338 /* See if we're looking at the `enum' keyword. */
15339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15341 /* Consume the `enum' token. */
15342 cp_lexer_consume_token (parser->lexer);
15343 /* Remember that it's an enumeration type. */
15344 tag_type = enum_type;
15345 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15346 enums) is used here. */
15347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15348 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15350 pedwarn (input_location, 0, "elaborated-type-specifier "
15351 "for a scoped enum must not use the %<%D%> keyword",
15352 cp_lexer_peek_token (parser->lexer)->u.value);
15353 /* Consume the `struct' or `class' and parse it anyway. */
15354 cp_lexer_consume_token (parser->lexer);
15356 /* Parse the attributes. */
15357 attributes = cp_parser_attributes_opt (parser);
15359 /* Or, it might be `typename'. */
15360 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15361 RID_TYPENAME))
15363 /* Consume the `typename' token. */
15364 cp_lexer_consume_token (parser->lexer);
15365 /* Remember that it's a `typename' type. */
15366 tag_type = typename_type;
15368 /* Otherwise it must be a class-key. */
15369 else
15371 tag_type = cp_parser_class_key (parser);
15372 if (tag_type == none_type)
15373 return error_mark_node;
15374 /* Parse the attributes. */
15375 attributes = cp_parser_attributes_opt (parser);
15378 /* Look for the `::' operator. */
15379 globalscope = cp_parser_global_scope_opt (parser,
15380 /*current_scope_valid_p=*/false);
15381 /* Look for the nested-name-specifier. */
15382 if (tag_type == typename_type && !globalscope)
15384 if (!cp_parser_nested_name_specifier (parser,
15385 /*typename_keyword_p=*/true,
15386 /*check_dependency_p=*/true,
15387 /*type_p=*/true,
15388 is_declaration))
15389 return error_mark_node;
15391 else
15392 /* Even though `typename' is not present, the proposed resolution
15393 to Core Issue 180 says that in `class A<T>::B', `B' should be
15394 considered a type-name, even if `A<T>' is dependent. */
15395 cp_parser_nested_name_specifier_opt (parser,
15396 /*typename_keyword_p=*/true,
15397 /*check_dependency_p=*/true,
15398 /*type_p=*/true,
15399 is_declaration);
15400 /* For everything but enumeration types, consider a template-id.
15401 For an enumeration type, consider only a plain identifier. */
15402 if (tag_type != enum_type)
15404 bool template_p = false;
15405 tree decl;
15407 /* Allow the `template' keyword. */
15408 template_p = cp_parser_optional_template_keyword (parser);
15409 /* If we didn't see `template', we don't know if there's a
15410 template-id or not. */
15411 if (!template_p)
15412 cp_parser_parse_tentatively (parser);
15413 /* Parse the template-id. */
15414 token = cp_lexer_peek_token (parser->lexer);
15415 decl = cp_parser_template_id (parser, template_p,
15416 /*check_dependency_p=*/true,
15417 tag_type,
15418 is_declaration);
15419 /* If we didn't find a template-id, look for an ordinary
15420 identifier. */
15421 if (!template_p && !cp_parser_parse_definitely (parser))
15423 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15424 in effect, then we must assume that, upon instantiation, the
15425 template will correspond to a class. */
15426 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15427 && tag_type == typename_type)
15428 type = make_typename_type (parser->scope, decl,
15429 typename_type,
15430 /*complain=*/tf_error);
15431 /* If the `typename' keyword is in effect and DECL is not a type
15432 decl, then type is non existent. */
15433 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15435 else if (TREE_CODE (decl) == TYPE_DECL)
15436 type = check_elaborated_type_specifier (tag_type, decl,
15437 /*allow_template_p=*/true);
15438 else if (decl == error_mark_node)
15439 type = error_mark_node;
15442 if (!type)
15444 token = cp_lexer_peek_token (parser->lexer);
15445 identifier = cp_parser_identifier (parser);
15447 if (identifier == error_mark_node)
15449 parser->scope = NULL_TREE;
15450 return error_mark_node;
15453 /* For a `typename', we needn't call xref_tag. */
15454 if (tag_type == typename_type
15455 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15456 return cp_parser_make_typename_type (parser, parser->scope,
15457 identifier,
15458 token->location);
15459 /* Look up a qualified name in the usual way. */
15460 if (parser->scope)
15462 tree decl;
15463 tree ambiguous_decls;
15465 decl = cp_parser_lookup_name (parser, identifier,
15466 tag_type,
15467 /*is_template=*/false,
15468 /*is_namespace=*/false,
15469 /*check_dependency=*/true,
15470 &ambiguous_decls,
15471 token->location);
15473 /* If the lookup was ambiguous, an error will already have been
15474 issued. */
15475 if (ambiguous_decls)
15476 return error_mark_node;
15478 /* If we are parsing friend declaration, DECL may be a
15479 TEMPLATE_DECL tree node here. However, we need to check
15480 whether this TEMPLATE_DECL results in valid code. Consider
15481 the following example:
15483 namespace N {
15484 template <class T> class C {};
15486 class X {
15487 template <class T> friend class N::C; // #1, valid code
15489 template <class T> class Y {
15490 friend class N::C; // #2, invalid code
15493 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15494 name lookup of `N::C'. We see that friend declaration must
15495 be template for the code to be valid. Note that
15496 processing_template_decl does not work here since it is
15497 always 1 for the above two cases. */
15499 decl = (cp_parser_maybe_treat_template_as_class
15500 (decl, /*tag_name_p=*/is_friend
15501 && parser->num_template_parameter_lists));
15503 if (TREE_CODE (decl) != TYPE_DECL)
15505 cp_parser_diagnose_invalid_type_name (parser,
15506 parser->scope,
15507 identifier,
15508 token->location);
15509 return error_mark_node;
15512 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15514 bool allow_template = (parser->num_template_parameter_lists
15515 || DECL_SELF_REFERENCE_P (decl));
15516 type = check_elaborated_type_specifier (tag_type, decl,
15517 allow_template);
15519 if (type == error_mark_node)
15520 return error_mark_node;
15523 /* Forward declarations of nested types, such as
15525 class C1::C2;
15526 class C1::C2::C3;
15528 are invalid unless all components preceding the final '::'
15529 are complete. If all enclosing types are complete, these
15530 declarations become merely pointless.
15532 Invalid forward declarations of nested types are errors
15533 caught elsewhere in parsing. Those that are pointless arrive
15534 here. */
15536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15537 && !is_friend && !processing_explicit_instantiation)
15538 warning (0, "declaration %qD does not declare anything", decl);
15540 type = TREE_TYPE (decl);
15542 else
15544 /* An elaborated-type-specifier sometimes introduces a new type and
15545 sometimes names an existing type. Normally, the rule is that it
15546 introduces a new type only if there is not an existing type of
15547 the same name already in scope. For example, given:
15549 struct S {};
15550 void f() { struct S s; }
15552 the `struct S' in the body of `f' is the same `struct S' as in
15553 the global scope; the existing definition is used. However, if
15554 there were no global declaration, this would introduce a new
15555 local class named `S'.
15557 An exception to this rule applies to the following code:
15559 namespace N { struct S; }
15561 Here, the elaborated-type-specifier names a new type
15562 unconditionally; even if there is already an `S' in the
15563 containing scope this declaration names a new type.
15564 This exception only applies if the elaborated-type-specifier
15565 forms the complete declaration:
15567 [class.name]
15569 A declaration consisting solely of `class-key identifier ;' is
15570 either a redeclaration of the name in the current scope or a
15571 forward declaration of the identifier as a class name. It
15572 introduces the name into the current scope.
15574 We are in this situation precisely when the next token is a `;'.
15576 An exception to the exception is that a `friend' declaration does
15577 *not* name a new type; i.e., given:
15579 struct S { friend struct T; };
15581 `T' is not a new type in the scope of `S'.
15583 Also, `new struct S' or `sizeof (struct S)' never results in the
15584 definition of a new type; a new type can only be declared in a
15585 declaration context. */
15587 tag_scope ts;
15588 bool template_p;
15590 if (is_friend)
15591 /* Friends have special name lookup rules. */
15592 ts = ts_within_enclosing_non_class;
15593 else if (is_declaration
15594 && cp_lexer_next_token_is (parser->lexer,
15595 CPP_SEMICOLON))
15596 /* This is a `class-key identifier ;' */
15597 ts = ts_current;
15598 else
15599 ts = ts_global;
15601 template_p =
15602 (parser->num_template_parameter_lists
15603 && (cp_parser_next_token_starts_class_definition_p (parser)
15604 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15605 /* An unqualified name was used to reference this type, so
15606 there were no qualifying templates. */
15607 if (!cp_parser_check_template_parameters (parser,
15608 /*num_templates=*/0,
15609 token->location,
15610 /*declarator=*/NULL))
15611 return error_mark_node;
15612 type = xref_tag (tag_type, identifier, ts, template_p);
15616 if (type == error_mark_node)
15617 return error_mark_node;
15619 /* Allow attributes on forward declarations of classes. */
15620 if (attributes)
15622 if (TREE_CODE (type) == TYPENAME_TYPE)
15623 warning (OPT_Wattributes,
15624 "attributes ignored on uninstantiated type");
15625 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15626 && ! processing_explicit_instantiation)
15627 warning (OPT_Wattributes,
15628 "attributes ignored on template instantiation");
15629 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15630 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15631 else
15632 warning (OPT_Wattributes,
15633 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15636 if (tag_type != enum_type)
15638 /* Indicate whether this class was declared as a `class' or as a
15639 `struct'. */
15640 if (TREE_CODE (type) == RECORD_TYPE)
15641 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15642 cp_parser_check_class_key (tag_type, type);
15645 /* A "<" cannot follow an elaborated type specifier. If that
15646 happens, the user was probably trying to form a template-id. */
15647 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15648 token->location);
15650 return type;
15653 /* Parse an enum-specifier.
15655 enum-specifier:
15656 enum-head { enumerator-list [opt] }
15657 enum-head { enumerator-list , } [C++0x]
15659 enum-head:
15660 enum-key identifier [opt] enum-base [opt]
15661 enum-key nested-name-specifier identifier enum-base [opt]
15663 enum-key:
15664 enum
15665 enum class [C++0x]
15666 enum struct [C++0x]
15668 enum-base: [C++0x]
15669 : type-specifier-seq
15671 opaque-enum-specifier:
15672 enum-key identifier enum-base [opt] ;
15674 GNU Extensions:
15675 enum-key attributes[opt] identifier [opt] enum-base [opt]
15676 { enumerator-list [opt] }attributes[opt]
15677 enum-key attributes[opt] identifier [opt] enum-base [opt]
15678 { enumerator-list, }attributes[opt] [C++0x]
15680 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15681 if the token stream isn't an enum-specifier after all. */
15683 static tree
15684 cp_parser_enum_specifier (cp_parser* parser)
15686 tree identifier;
15687 tree type = NULL_TREE;
15688 tree prev_scope;
15689 tree nested_name_specifier = NULL_TREE;
15690 tree attributes;
15691 bool scoped_enum_p = false;
15692 bool has_underlying_type = false;
15693 bool nested_being_defined = false;
15694 bool new_value_list = false;
15695 bool is_new_type = false;
15696 bool is_anonymous = false;
15697 tree underlying_type = NULL_TREE;
15698 cp_token *type_start_token = NULL;
15699 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15701 parser->colon_corrects_to_scope_p = false;
15703 /* Parse tentatively so that we can back up if we don't find a
15704 enum-specifier. */
15705 cp_parser_parse_tentatively (parser);
15707 /* Caller guarantees that the current token is 'enum', an identifier
15708 possibly follows, and the token after that is an opening brace.
15709 If we don't have an identifier, fabricate an anonymous name for
15710 the enumeration being defined. */
15711 cp_lexer_consume_token (parser->lexer);
15713 /* Parse the "class" or "struct", which indicates a scoped
15714 enumeration type in C++0x. */
15715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15716 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15718 if (cxx_dialect < cxx11)
15719 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15721 /* Consume the `struct' or `class' token. */
15722 cp_lexer_consume_token (parser->lexer);
15724 scoped_enum_p = true;
15727 attributes = cp_parser_attributes_opt (parser);
15729 /* Clear the qualification. */
15730 parser->scope = NULL_TREE;
15731 parser->qualifying_scope = NULL_TREE;
15732 parser->object_scope = NULL_TREE;
15734 /* Figure out in what scope the declaration is being placed. */
15735 prev_scope = current_scope ();
15737 type_start_token = cp_lexer_peek_token (parser->lexer);
15739 push_deferring_access_checks (dk_no_check);
15740 nested_name_specifier
15741 = cp_parser_nested_name_specifier_opt (parser,
15742 /*typename_keyword_p=*/true,
15743 /*check_dependency_p=*/false,
15744 /*type_p=*/false,
15745 /*is_declaration=*/false);
15747 if (nested_name_specifier)
15749 tree name;
15751 identifier = cp_parser_identifier (parser);
15752 name = cp_parser_lookup_name (parser, identifier,
15753 enum_type,
15754 /*is_template=*/false,
15755 /*is_namespace=*/false,
15756 /*check_dependency=*/true,
15757 /*ambiguous_decls=*/NULL,
15758 input_location);
15759 if (name && name != error_mark_node)
15761 type = TREE_TYPE (name);
15762 if (TREE_CODE (type) == TYPENAME_TYPE)
15764 /* Are template enums allowed in ISO? */
15765 if (template_parm_scope_p ())
15766 pedwarn (type_start_token->location, OPT_Wpedantic,
15767 "%qD is an enumeration template", name);
15768 /* ignore a typename reference, for it will be solved by name
15769 in start_enum. */
15770 type = NULL_TREE;
15773 else if (nested_name_specifier == error_mark_node)
15774 /* We already issued an error. */;
15775 else
15776 error_at (type_start_token->location,
15777 "%qD is not an enumerator-name", identifier);
15779 else
15781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15782 identifier = cp_parser_identifier (parser);
15783 else
15785 identifier = make_anon_name ();
15786 is_anonymous = true;
15787 if (scoped_enum_p)
15788 error_at (type_start_token->location,
15789 "anonymous scoped enum is not allowed");
15792 pop_deferring_access_checks ();
15794 /* Check for the `:' that denotes a specified underlying type in C++0x.
15795 Note that a ':' could also indicate a bitfield width, however. */
15796 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15798 cp_decl_specifier_seq type_specifiers;
15800 /* Consume the `:'. */
15801 cp_lexer_consume_token (parser->lexer);
15803 /* Parse the type-specifier-seq. */
15804 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15805 /*is_trailing_return=*/false,
15806 &type_specifiers);
15808 /* At this point this is surely not elaborated type specifier. */
15809 if (!cp_parser_parse_definitely (parser))
15810 return NULL_TREE;
15812 if (cxx_dialect < cxx11)
15813 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15815 has_underlying_type = true;
15817 /* If that didn't work, stop. */
15818 if (type_specifiers.type != error_mark_node)
15820 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15821 /*initialized=*/0, NULL);
15822 if (underlying_type == error_mark_node
15823 || check_for_bare_parameter_packs (underlying_type))
15824 underlying_type = NULL_TREE;
15828 /* Look for the `{' but don't consume it yet. */
15829 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15831 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15833 cp_parser_error (parser, "expected %<{%>");
15834 if (has_underlying_type)
15836 type = NULL_TREE;
15837 goto out;
15840 /* An opaque-enum-specifier must have a ';' here. */
15841 if ((scoped_enum_p || underlying_type)
15842 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15844 cp_parser_error (parser, "expected %<;%> or %<{%>");
15845 if (has_underlying_type)
15847 type = NULL_TREE;
15848 goto out;
15853 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15854 return NULL_TREE;
15856 if (nested_name_specifier)
15858 if (CLASS_TYPE_P (nested_name_specifier))
15860 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15861 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15862 push_scope (nested_name_specifier);
15864 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15866 push_nested_namespace (nested_name_specifier);
15870 /* Issue an error message if type-definitions are forbidden here. */
15871 if (!cp_parser_check_type_definition (parser))
15872 type = error_mark_node;
15873 else
15874 /* Create the new type. We do this before consuming the opening
15875 brace so the enum will be recorded as being on the line of its
15876 tag (or the 'enum' keyword, if there is no tag). */
15877 type = start_enum (identifier, type, underlying_type,
15878 scoped_enum_p, &is_new_type);
15880 /* If the next token is not '{' it is an opaque-enum-specifier or an
15881 elaborated-type-specifier. */
15882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15884 timevar_push (TV_PARSE_ENUM);
15885 if (nested_name_specifier
15886 && nested_name_specifier != error_mark_node)
15888 /* The following catches invalid code such as:
15889 enum class S<int>::E { A, B, C }; */
15890 if (!processing_specialization
15891 && CLASS_TYPE_P (nested_name_specifier)
15892 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15893 error_at (type_start_token->location, "cannot add an enumerator "
15894 "list to a template instantiation");
15896 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15898 error_at (type_start_token->location,
15899 "%<%T::%E%> has not been declared",
15900 TYPE_CONTEXT (nested_name_specifier),
15901 nested_name_specifier);
15902 type = error_mark_node;
15904 /* If that scope does not contain the scope in which the
15905 class was originally declared, the program is invalid. */
15906 else if (prev_scope && !is_ancestor (prev_scope,
15907 nested_name_specifier))
15909 if (at_namespace_scope_p ())
15910 error_at (type_start_token->location,
15911 "declaration of %qD in namespace %qD which does not "
15912 "enclose %qD",
15913 type, prev_scope, nested_name_specifier);
15914 else
15915 error_at (type_start_token->location,
15916 "declaration of %qD in %qD which does not "
15917 "enclose %qD",
15918 type, prev_scope, nested_name_specifier);
15919 type = error_mark_node;
15923 if (scoped_enum_p)
15924 begin_scope (sk_scoped_enum, type);
15926 /* Consume the opening brace. */
15927 cp_lexer_consume_token (parser->lexer);
15929 if (type == error_mark_node)
15930 ; /* Nothing to add */
15931 else if (OPAQUE_ENUM_P (type)
15932 || (cxx_dialect > cxx98 && processing_specialization))
15934 new_value_list = true;
15935 SET_OPAQUE_ENUM_P (type, false);
15936 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15938 else
15940 error_at (type_start_token->location,
15941 "multiple definition of %q#T", type);
15942 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15943 "previous definition here");
15944 type = error_mark_node;
15947 if (type == error_mark_node)
15948 cp_parser_skip_to_end_of_block_or_statement (parser);
15949 /* If the next token is not '}', then there are some enumerators. */
15950 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15952 if (is_anonymous && !scoped_enum_p)
15953 pedwarn (type_start_token->location, OPT_Wpedantic,
15954 "ISO C++ forbids empty anonymous enum");
15956 else
15957 cp_parser_enumerator_list (parser, type);
15959 /* Consume the final '}'. */
15960 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15962 if (scoped_enum_p)
15963 finish_scope ();
15964 timevar_pop (TV_PARSE_ENUM);
15966 else
15968 /* If a ';' follows, then it is an opaque-enum-specifier
15969 and additional restrictions apply. */
15970 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15972 if (is_anonymous)
15973 error_at (type_start_token->location,
15974 "opaque-enum-specifier without name");
15975 else if (nested_name_specifier)
15976 error_at (type_start_token->location,
15977 "opaque-enum-specifier must use a simple identifier");
15981 /* Look for trailing attributes to apply to this enumeration, and
15982 apply them if appropriate. */
15983 if (cp_parser_allow_gnu_extensions_p (parser))
15985 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15986 trailing_attr = chainon (trailing_attr, attributes);
15987 cplus_decl_attributes (&type,
15988 trailing_attr,
15989 (int) ATTR_FLAG_TYPE_IN_PLACE);
15992 /* Finish up the enumeration. */
15993 if (type != error_mark_node)
15995 if (new_value_list)
15996 finish_enum_value_list (type);
15997 if (is_new_type)
15998 finish_enum (type);
16001 if (nested_name_specifier)
16003 if (CLASS_TYPE_P (nested_name_specifier))
16005 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16006 pop_scope (nested_name_specifier);
16008 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16010 pop_nested_namespace (nested_name_specifier);
16013 out:
16014 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16015 return type;
16018 /* Parse an enumerator-list. The enumerators all have the indicated
16019 TYPE.
16021 enumerator-list:
16022 enumerator-definition
16023 enumerator-list , enumerator-definition */
16025 static void
16026 cp_parser_enumerator_list (cp_parser* parser, tree type)
16028 while (true)
16030 /* Parse an enumerator-definition. */
16031 cp_parser_enumerator_definition (parser, type);
16033 /* If the next token is not a ',', we've reached the end of
16034 the list. */
16035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16036 break;
16037 /* Otherwise, consume the `,' and keep going. */
16038 cp_lexer_consume_token (parser->lexer);
16039 /* If the next token is a `}', there is a trailing comma. */
16040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16042 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16043 pedwarn (input_location, OPT_Wpedantic,
16044 "comma at end of enumerator list");
16045 break;
16050 /* Parse an enumerator-definition. The enumerator has the indicated
16051 TYPE.
16053 enumerator-definition:
16054 enumerator
16055 enumerator = constant-expression
16057 enumerator:
16058 identifier */
16060 static void
16061 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16063 tree identifier;
16064 tree value;
16065 location_t loc;
16067 /* Save the input location because we are interested in the location
16068 of the identifier and not the location of the explicit value. */
16069 loc = cp_lexer_peek_token (parser->lexer)->location;
16071 /* Look for the identifier. */
16072 identifier = cp_parser_identifier (parser);
16073 if (identifier == error_mark_node)
16074 return;
16076 /* If the next token is an '=', then there is an explicit value. */
16077 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16079 /* Consume the `=' token. */
16080 cp_lexer_consume_token (parser->lexer);
16081 /* Parse the value. */
16082 value = cp_parser_constant_expression (parser,
16083 /*allow_non_constant_p=*/false,
16084 NULL);
16086 else
16087 value = NULL_TREE;
16089 /* If we are processing a template, make sure the initializer of the
16090 enumerator doesn't contain any bare template parameter pack. */
16091 if (check_for_bare_parameter_packs (value))
16092 value = error_mark_node;
16094 /* integral_constant_value will pull out this expression, so make sure
16095 it's folded as appropriate. */
16096 value = fold_non_dependent_expr (value);
16098 /* Create the enumerator. */
16099 build_enumerator (identifier, value, type, loc);
16102 /* Parse a namespace-name.
16104 namespace-name:
16105 original-namespace-name
16106 namespace-alias
16108 Returns the NAMESPACE_DECL for the namespace. */
16110 static tree
16111 cp_parser_namespace_name (cp_parser* parser)
16113 tree identifier;
16114 tree namespace_decl;
16116 cp_token *token = cp_lexer_peek_token (parser->lexer);
16118 /* Get the name of the namespace. */
16119 identifier = cp_parser_identifier (parser);
16120 if (identifier == error_mark_node)
16121 return error_mark_node;
16123 /* Look up the identifier in the currently active scope. Look only
16124 for namespaces, due to:
16126 [basic.lookup.udir]
16128 When looking up a namespace-name in a using-directive or alias
16129 definition, only namespace names are considered.
16131 And:
16133 [basic.lookup.qual]
16135 During the lookup of a name preceding the :: scope resolution
16136 operator, object, function, and enumerator names are ignored.
16138 (Note that cp_parser_qualifying_entity only calls this
16139 function if the token after the name is the scope resolution
16140 operator.) */
16141 namespace_decl = cp_parser_lookup_name (parser, identifier,
16142 none_type,
16143 /*is_template=*/false,
16144 /*is_namespace=*/true,
16145 /*check_dependency=*/true,
16146 /*ambiguous_decls=*/NULL,
16147 token->location);
16148 /* If it's not a namespace, issue an error. */
16149 if (namespace_decl == error_mark_node
16150 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16153 error_at (token->location, "%qD is not a namespace-name", identifier);
16154 cp_parser_error (parser, "expected namespace-name");
16155 namespace_decl = error_mark_node;
16158 return namespace_decl;
16161 /* Parse a namespace-definition.
16163 namespace-definition:
16164 named-namespace-definition
16165 unnamed-namespace-definition
16167 named-namespace-definition:
16168 original-namespace-definition
16169 extension-namespace-definition
16171 original-namespace-definition:
16172 namespace identifier { namespace-body }
16174 extension-namespace-definition:
16175 namespace original-namespace-name { namespace-body }
16177 unnamed-namespace-definition:
16178 namespace { namespace-body } */
16180 static void
16181 cp_parser_namespace_definition (cp_parser* parser)
16183 tree identifier, attribs;
16184 bool has_visibility;
16185 bool is_inline;
16187 cp_ensure_no_omp_declare_simd (parser);
16188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16190 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16191 is_inline = true;
16192 cp_lexer_consume_token (parser->lexer);
16194 else
16195 is_inline = false;
16197 /* Look for the `namespace' keyword. */
16198 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16200 /* Get the name of the namespace. We do not attempt to distinguish
16201 between an original-namespace-definition and an
16202 extension-namespace-definition at this point. The semantic
16203 analysis routines are responsible for that. */
16204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16205 identifier = cp_parser_identifier (parser);
16206 else
16207 identifier = NULL_TREE;
16209 /* Parse any specified attributes. */
16210 attribs = cp_parser_attributes_opt (parser);
16212 /* Look for the `{' to start the namespace. */
16213 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16214 /* Start the namespace. */
16215 push_namespace (identifier);
16217 /* "inline namespace" is equivalent to a stub namespace definition
16218 followed by a strong using directive. */
16219 if (is_inline)
16221 tree name_space = current_namespace;
16222 /* Set up namespace association. */
16223 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16224 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16225 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16226 /* Import the contents of the inline namespace. */
16227 pop_namespace ();
16228 do_using_directive (name_space);
16229 push_namespace (identifier);
16232 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16234 /* Parse the body of the namespace. */
16235 cp_parser_namespace_body (parser);
16237 if (has_visibility)
16238 pop_visibility (1);
16240 /* Finish the namespace. */
16241 pop_namespace ();
16242 /* Look for the final `}'. */
16243 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16246 /* Parse a namespace-body.
16248 namespace-body:
16249 declaration-seq [opt] */
16251 static void
16252 cp_parser_namespace_body (cp_parser* parser)
16254 cp_parser_declaration_seq_opt (parser);
16257 /* Parse a namespace-alias-definition.
16259 namespace-alias-definition:
16260 namespace identifier = qualified-namespace-specifier ; */
16262 static void
16263 cp_parser_namespace_alias_definition (cp_parser* parser)
16265 tree identifier;
16266 tree namespace_specifier;
16268 cp_token *token = cp_lexer_peek_token (parser->lexer);
16270 /* Look for the `namespace' keyword. */
16271 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16272 /* Look for the identifier. */
16273 identifier = cp_parser_identifier (parser);
16274 if (identifier == error_mark_node)
16275 return;
16276 /* Look for the `=' token. */
16277 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16278 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16280 error_at (token->location, "%<namespace%> definition is not allowed here");
16281 /* Skip the definition. */
16282 cp_lexer_consume_token (parser->lexer);
16283 if (cp_parser_skip_to_closing_brace (parser))
16284 cp_lexer_consume_token (parser->lexer);
16285 return;
16287 cp_parser_require (parser, CPP_EQ, RT_EQ);
16288 /* Look for the qualified-namespace-specifier. */
16289 namespace_specifier
16290 = cp_parser_qualified_namespace_specifier (parser);
16291 /* Look for the `;' token. */
16292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16294 /* Register the alias in the symbol table. */
16295 do_namespace_alias (identifier, namespace_specifier);
16298 /* Parse a qualified-namespace-specifier.
16300 qualified-namespace-specifier:
16301 :: [opt] nested-name-specifier [opt] namespace-name
16303 Returns a NAMESPACE_DECL corresponding to the specified
16304 namespace. */
16306 static tree
16307 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16309 /* Look for the optional `::'. */
16310 cp_parser_global_scope_opt (parser,
16311 /*current_scope_valid_p=*/false);
16313 /* Look for the optional nested-name-specifier. */
16314 cp_parser_nested_name_specifier_opt (parser,
16315 /*typename_keyword_p=*/false,
16316 /*check_dependency_p=*/true,
16317 /*type_p=*/false,
16318 /*is_declaration=*/true);
16320 return cp_parser_namespace_name (parser);
16323 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16324 access declaration.
16326 using-declaration:
16327 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16328 using :: unqualified-id ;
16330 access-declaration:
16331 qualified-id ;
16335 static bool
16336 cp_parser_using_declaration (cp_parser* parser,
16337 bool access_declaration_p)
16339 cp_token *token;
16340 bool typename_p = false;
16341 bool global_scope_p;
16342 tree decl;
16343 tree identifier;
16344 tree qscope;
16345 int oldcount = errorcount;
16346 cp_token *diag_token = NULL;
16348 if (access_declaration_p)
16350 diag_token = cp_lexer_peek_token (parser->lexer);
16351 cp_parser_parse_tentatively (parser);
16353 else
16355 /* Look for the `using' keyword. */
16356 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16358 /* Peek at the next token. */
16359 token = cp_lexer_peek_token (parser->lexer);
16360 /* See if it's `typename'. */
16361 if (token->keyword == RID_TYPENAME)
16363 /* Remember that we've seen it. */
16364 typename_p = true;
16365 /* Consume the `typename' token. */
16366 cp_lexer_consume_token (parser->lexer);
16370 /* Look for the optional global scope qualification. */
16371 global_scope_p
16372 = (cp_parser_global_scope_opt (parser,
16373 /*current_scope_valid_p=*/false)
16374 != NULL_TREE);
16376 /* If we saw `typename', or didn't see `::', then there must be a
16377 nested-name-specifier present. */
16378 if (typename_p || !global_scope_p)
16380 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16381 /*check_dependency_p=*/true,
16382 /*type_p=*/false,
16383 /*is_declaration=*/true);
16384 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16386 cp_parser_skip_to_end_of_block_or_statement (parser);
16387 return false;
16390 /* Otherwise, we could be in either of the two productions. In that
16391 case, treat the nested-name-specifier as optional. */
16392 else
16393 qscope = cp_parser_nested_name_specifier_opt (parser,
16394 /*typename_keyword_p=*/false,
16395 /*check_dependency_p=*/true,
16396 /*type_p=*/false,
16397 /*is_declaration=*/true);
16398 if (!qscope)
16399 qscope = global_namespace;
16401 if (access_declaration_p && cp_parser_error_occurred (parser))
16402 /* Something has already gone wrong; there's no need to parse
16403 further. Since an error has occurred, the return value of
16404 cp_parser_parse_definitely will be false, as required. */
16405 return cp_parser_parse_definitely (parser);
16407 token = cp_lexer_peek_token (parser->lexer);
16408 /* Parse the unqualified-id. */
16409 identifier = cp_parser_unqualified_id (parser,
16410 /*template_keyword_p=*/false,
16411 /*check_dependency_p=*/true,
16412 /*declarator_p=*/true,
16413 /*optional_p=*/false);
16415 if (access_declaration_p)
16417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16418 cp_parser_simulate_error (parser);
16419 if (!cp_parser_parse_definitely (parser))
16420 return false;
16423 /* The function we call to handle a using-declaration is different
16424 depending on what scope we are in. */
16425 if (qscope == error_mark_node || identifier == error_mark_node)
16427 else if (!identifier_p (identifier)
16428 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16429 /* [namespace.udecl]
16431 A using declaration shall not name a template-id. */
16432 error_at (token->location,
16433 "a template-id may not appear in a using-declaration");
16434 else
16436 if (at_class_scope_p ())
16438 /* Create the USING_DECL. */
16439 decl = do_class_using_decl (parser->scope, identifier);
16441 if (decl && typename_p)
16442 USING_DECL_TYPENAME_P (decl) = 1;
16444 if (check_for_bare_parameter_packs (decl))
16446 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16447 return false;
16449 else
16450 /* Add it to the list of members in this class. */
16451 finish_member_declaration (decl);
16453 else
16455 decl = cp_parser_lookup_name_simple (parser,
16456 identifier,
16457 token->location);
16458 if (decl == error_mark_node)
16459 cp_parser_name_lookup_error (parser, identifier,
16460 decl, NLE_NULL,
16461 token->location);
16462 else if (check_for_bare_parameter_packs (decl))
16464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16465 return false;
16467 else if (!at_namespace_scope_p ())
16468 do_local_using_decl (decl, qscope, identifier);
16469 else
16470 do_toplevel_using_decl (decl, qscope, identifier);
16474 /* Look for the final `;'. */
16475 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16477 if (access_declaration_p && errorcount == oldcount)
16478 warning_at (diag_token->location, OPT_Wdeprecated,
16479 "access declarations are deprecated "
16480 "in favour of using-declarations; "
16481 "suggestion: add the %<using%> keyword");
16483 return true;
16486 /* Parse an alias-declaration.
16488 alias-declaration:
16489 using identifier attribute-specifier-seq [opt] = type-id */
16491 static tree
16492 cp_parser_alias_declaration (cp_parser* parser)
16494 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16495 location_t id_location;
16496 cp_declarator *declarator;
16497 cp_decl_specifier_seq decl_specs;
16498 bool member_p;
16499 const char *saved_message = NULL;
16501 /* Look for the `using' keyword. */
16502 cp_token *using_token
16503 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16504 if (using_token == NULL)
16505 return error_mark_node;
16507 id_location = cp_lexer_peek_token (parser->lexer)->location;
16508 id = cp_parser_identifier (parser);
16509 if (id == error_mark_node)
16510 return error_mark_node;
16512 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16513 attributes = cp_parser_attributes_opt (parser);
16514 if (attributes == error_mark_node)
16515 return error_mark_node;
16517 cp_parser_require (parser, CPP_EQ, RT_EQ);
16519 if (cp_parser_error_occurred (parser))
16520 return error_mark_node;
16522 cp_parser_commit_to_tentative_parse (parser);
16524 /* Now we are going to parse the type-id of the declaration. */
16527 [dcl.type]/3 says:
16529 "A type-specifier-seq shall not define a class or enumeration
16530 unless it appears in the type-id of an alias-declaration (7.1.3) that
16531 is not the declaration of a template-declaration."
16533 In other words, if we currently are in an alias template, the
16534 type-id should not define a type.
16536 So let's set parser->type_definition_forbidden_message in that
16537 case; cp_parser_check_type_definition (called by
16538 cp_parser_class_specifier) will then emit an error if a type is
16539 defined in the type-id. */
16540 if (parser->num_template_parameter_lists)
16542 saved_message = parser->type_definition_forbidden_message;
16543 parser->type_definition_forbidden_message =
16544 G_("types may not be defined in alias template declarations");
16547 type = cp_parser_type_id (parser);
16549 /* Restore the error message if need be. */
16550 if (parser->num_template_parameter_lists)
16551 parser->type_definition_forbidden_message = saved_message;
16553 if (type == error_mark_node
16554 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16556 cp_parser_skip_to_end_of_block_or_statement (parser);
16557 return error_mark_node;
16560 /* A typedef-name can also be introduced by an alias-declaration. The
16561 identifier following the using keyword becomes a typedef-name. It has
16562 the same semantics as if it were introduced by the typedef
16563 specifier. In particular, it does not define a new type and it shall
16564 not appear in the type-id. */
16566 clear_decl_specs (&decl_specs);
16567 decl_specs.type = type;
16568 if (attributes != NULL_TREE)
16570 decl_specs.attributes = attributes;
16571 set_and_check_decl_spec_loc (&decl_specs,
16572 ds_attribute,
16573 attrs_token);
16575 set_and_check_decl_spec_loc (&decl_specs,
16576 ds_typedef,
16577 using_token);
16578 set_and_check_decl_spec_loc (&decl_specs,
16579 ds_alias,
16580 using_token);
16582 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16583 declarator->id_loc = id_location;
16585 member_p = at_class_scope_p ();
16586 if (member_p)
16587 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16588 NULL_TREE, attributes);
16589 else
16590 decl = start_decl (declarator, &decl_specs, 0,
16591 attributes, NULL_TREE, &pushed_scope);
16592 if (decl == error_mark_node)
16593 return decl;
16595 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16597 if (pushed_scope)
16598 pop_scope (pushed_scope);
16600 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16601 added into the symbol table; otherwise, return the TYPE_DECL. */
16602 if (DECL_LANG_SPECIFIC (decl)
16603 && DECL_TEMPLATE_INFO (decl)
16604 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16606 decl = DECL_TI_TEMPLATE (decl);
16607 if (member_p)
16608 check_member_template (decl);
16611 return decl;
16614 /* Parse a using-directive.
16616 using-directive:
16617 using namespace :: [opt] nested-name-specifier [opt]
16618 namespace-name ; */
16620 static void
16621 cp_parser_using_directive (cp_parser* parser)
16623 tree namespace_decl;
16624 tree attribs;
16626 /* Look for the `using' keyword. */
16627 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16628 /* And the `namespace' keyword. */
16629 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16630 /* Look for the optional `::' operator. */
16631 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16632 /* And the optional nested-name-specifier. */
16633 cp_parser_nested_name_specifier_opt (parser,
16634 /*typename_keyword_p=*/false,
16635 /*check_dependency_p=*/true,
16636 /*type_p=*/false,
16637 /*is_declaration=*/true);
16638 /* Get the namespace being used. */
16639 namespace_decl = cp_parser_namespace_name (parser);
16640 /* And any specified attributes. */
16641 attribs = cp_parser_attributes_opt (parser);
16642 /* Update the symbol table. */
16643 parse_using_directive (namespace_decl, attribs);
16644 /* Look for the final `;'. */
16645 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16648 /* Parse an asm-definition.
16650 asm-definition:
16651 asm ( string-literal ) ;
16653 GNU Extension:
16655 asm-definition:
16656 asm volatile [opt] ( string-literal ) ;
16657 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16658 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16659 : asm-operand-list [opt] ) ;
16660 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16661 : asm-operand-list [opt]
16662 : asm-clobber-list [opt] ) ;
16663 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16664 : asm-clobber-list [opt]
16665 : asm-goto-list ) ; */
16667 static void
16668 cp_parser_asm_definition (cp_parser* parser)
16670 tree string;
16671 tree outputs = NULL_TREE;
16672 tree inputs = NULL_TREE;
16673 tree clobbers = NULL_TREE;
16674 tree labels = NULL_TREE;
16675 tree asm_stmt;
16676 bool volatile_p = false;
16677 bool extended_p = false;
16678 bool invalid_inputs_p = false;
16679 bool invalid_outputs_p = false;
16680 bool goto_p = false;
16681 required_token missing = RT_NONE;
16683 /* Look for the `asm' keyword. */
16684 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16685 /* See if the next token is `volatile'. */
16686 if (cp_parser_allow_gnu_extensions_p (parser)
16687 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16689 /* Remember that we saw the `volatile' keyword. */
16690 volatile_p = true;
16691 /* Consume the token. */
16692 cp_lexer_consume_token (parser->lexer);
16694 if (cp_parser_allow_gnu_extensions_p (parser)
16695 && parser->in_function_body
16696 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16698 /* Remember that we saw the `goto' keyword. */
16699 goto_p = true;
16700 /* Consume the token. */
16701 cp_lexer_consume_token (parser->lexer);
16703 /* Look for the opening `('. */
16704 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16705 return;
16706 /* Look for the string. */
16707 string = cp_parser_string_literal (parser, false, false);
16708 if (string == error_mark_node)
16710 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16711 /*consume_paren=*/true);
16712 return;
16715 /* If we're allowing GNU extensions, check for the extended assembly
16716 syntax. Unfortunately, the `:' tokens need not be separated by
16717 a space in C, and so, for compatibility, we tolerate that here
16718 too. Doing that means that we have to treat the `::' operator as
16719 two `:' tokens. */
16720 if (cp_parser_allow_gnu_extensions_p (parser)
16721 && parser->in_function_body
16722 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16723 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16725 bool inputs_p = false;
16726 bool clobbers_p = false;
16727 bool labels_p = false;
16729 /* The extended syntax was used. */
16730 extended_p = true;
16732 /* Look for outputs. */
16733 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16735 /* Consume the `:'. */
16736 cp_lexer_consume_token (parser->lexer);
16737 /* Parse the output-operands. */
16738 if (cp_lexer_next_token_is_not (parser->lexer,
16739 CPP_COLON)
16740 && cp_lexer_next_token_is_not (parser->lexer,
16741 CPP_SCOPE)
16742 && cp_lexer_next_token_is_not (parser->lexer,
16743 CPP_CLOSE_PAREN)
16744 && !goto_p)
16745 outputs = cp_parser_asm_operand_list (parser);
16747 if (outputs == error_mark_node)
16748 invalid_outputs_p = true;
16750 /* If the next token is `::', there are no outputs, and the
16751 next token is the beginning of the inputs. */
16752 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16753 /* The inputs are coming next. */
16754 inputs_p = true;
16756 /* Look for inputs. */
16757 if (inputs_p
16758 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16760 /* Consume the `:' or `::'. */
16761 cp_lexer_consume_token (parser->lexer);
16762 /* Parse the output-operands. */
16763 if (cp_lexer_next_token_is_not (parser->lexer,
16764 CPP_COLON)
16765 && cp_lexer_next_token_is_not (parser->lexer,
16766 CPP_SCOPE)
16767 && cp_lexer_next_token_is_not (parser->lexer,
16768 CPP_CLOSE_PAREN))
16769 inputs = cp_parser_asm_operand_list (parser);
16771 if (inputs == error_mark_node)
16772 invalid_inputs_p = true;
16774 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16775 /* The clobbers are coming next. */
16776 clobbers_p = true;
16778 /* Look for clobbers. */
16779 if (clobbers_p
16780 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16782 clobbers_p = true;
16783 /* Consume the `:' or `::'. */
16784 cp_lexer_consume_token (parser->lexer);
16785 /* Parse the clobbers. */
16786 if (cp_lexer_next_token_is_not (parser->lexer,
16787 CPP_COLON)
16788 && cp_lexer_next_token_is_not (parser->lexer,
16789 CPP_CLOSE_PAREN))
16790 clobbers = cp_parser_asm_clobber_list (parser);
16792 else if (goto_p
16793 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16794 /* The labels are coming next. */
16795 labels_p = true;
16797 /* Look for labels. */
16798 if (labels_p
16799 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16801 labels_p = true;
16802 /* Consume the `:' or `::'. */
16803 cp_lexer_consume_token (parser->lexer);
16804 /* Parse the labels. */
16805 labels = cp_parser_asm_label_list (parser);
16808 if (goto_p && !labels_p)
16809 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16811 else if (goto_p)
16812 missing = RT_COLON_SCOPE;
16814 /* Look for the closing `)'. */
16815 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16816 missing ? missing : RT_CLOSE_PAREN))
16817 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16818 /*consume_paren=*/true);
16819 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16821 if (!invalid_inputs_p && !invalid_outputs_p)
16823 /* Create the ASM_EXPR. */
16824 if (parser->in_function_body)
16826 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16827 inputs, clobbers, labels);
16828 /* If the extended syntax was not used, mark the ASM_EXPR. */
16829 if (!extended_p)
16831 tree temp = asm_stmt;
16832 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16833 temp = TREE_OPERAND (temp, 0);
16835 ASM_INPUT_P (temp) = 1;
16838 else
16839 add_asm_node (string);
16843 // An RAII guard that manages the current template reuqirememts. The
16844 // constructor takes a boolean value, SAVE that, when true, resets
16845 // the current template requirements on construction. Otherwise, no change
16846 // is made. When the destructor is invoked, the current template requirements
16847 // are reset (made null) preventing subsequent parsing routines from using
16848 // them.
16849 struct cp_manage_requirements {
16850 bool save;
16851 tree reqs;
16853 // Construct the guard. If SAVE is true, the current requirements
16854 // saved and reset. Otherwise, no action is taken.
16855 cp_manage_requirements (bool save)
16856 : save (save), reqs (save ? release (current_template_reqs) : NULL_TREE)
16859 ~cp_manage_requirements ()
16861 current_template_reqs = reqs;
16865 // A DECLARATOR may have a trailing requires clause; it may also have
16866 // requirements introduced through the use of terse notation in the
16867 // declaration of function parameters. Returns the parsed and analyzed
16868 // template requirements.
16869 static tree
16870 cp_parser_trailing_requirements (cp_parser *parser, cp_declarator *decl)
16872 if (function_declarator_p (decl))
16874 // Get any constraints induced by the terse notaiton.
16875 tree terse_reqs = NULL_TREE;
16876 if (parser->fully_implicit_function_template_p)
16877 terse_reqs = get_shorthand_requirements (current_template_parms);
16879 // An optional requires clause can yield an additional constraint.
16880 tree explicit_reqs = cp_parser_requires_clause_opt (parser);
16882 // If requirements were specified in either the implicit
16883 // template parameter list or an explicit requires clause,
16884 // prepare to associate those with the declaration.
16885 if (tree r = conjoin_requirements (terse_reqs, explicit_reqs))
16886 current_template_reqs = finish_template_requirements (r);
16889 return current_template_reqs;
16893 /* Declarators [gram.dcl.decl] */
16895 /* Parse an init-declarator.
16897 init-declarator:
16898 declarator initializer [opt]
16900 GNU Extension:
16902 init-declarator:
16903 declarator asm-specification [opt] attributes [opt] initializer [opt]
16905 function-definition:
16906 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16907 function-body
16908 decl-specifier-seq [opt] declarator function-try-block
16910 GNU Extension:
16912 function-definition:
16913 __extension__ function-definition
16915 TM Extension:
16917 function-definition:
16918 decl-specifier-seq [opt] declarator function-transaction-block
16920 The DECL_SPECIFIERS apply to this declarator. Returns a
16921 representation of the entity declared. If MEMBER_P is TRUE, then
16922 this declarator appears in a class scope. The new DECL created by
16923 this declarator is returned.
16925 The CHECKS are access checks that should be performed once we know
16926 what entity is being declared (and, therefore, what classes have
16927 befriended it).
16929 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16930 for a function-definition here as well. If the declarator is a
16931 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16932 be TRUE upon return. By that point, the function-definition will
16933 have been completely parsed.
16935 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16936 is FALSE.
16938 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16939 parsed declaration if it is an uninitialized single declarator not followed
16940 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16941 if present, will not be consumed. If returned, this declarator will be
16942 created with SD_INITIALIZED but will not call cp_finish_decl. */
16944 static tree
16945 cp_parser_init_declarator (cp_parser* parser,
16946 cp_decl_specifier_seq *decl_specifiers,
16947 vec<deferred_access_check, va_gc> *checks,
16948 bool function_definition_allowed_p,
16949 bool member_p,
16950 int declares_class_or_enum,
16951 bool* function_definition_p,
16952 tree* maybe_range_for_decl)
16954 cp_token *token = NULL, *asm_spec_start_token = NULL,
16955 *attributes_start_token = NULL;
16956 cp_declarator *declarator;
16957 tree prefix_attributes;
16958 tree attributes = NULL;
16959 tree asm_specification;
16960 tree initializer;
16961 tree decl = NULL_TREE;
16962 tree scope;
16963 int is_initialized;
16964 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16965 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16966 "(...)". */
16967 enum cpp_ttype initialization_kind;
16968 bool is_direct_init = false;
16969 bool is_non_constant_init;
16970 int ctor_dtor_or_conv_p;
16971 bool friend_p;
16972 tree pushed_scope = NULL_TREE;
16973 bool range_for_decl_p = false;
16974 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16976 /* Gather the attributes that were provided with the
16977 decl-specifiers. */
16978 prefix_attributes = decl_specifiers->attributes;
16980 /* Assume that this is not the declarator for a function
16981 definition. */
16982 if (function_definition_p)
16983 *function_definition_p = false;
16985 /* Default arguments are only permitted for function parameters. */
16986 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16987 parser->default_arg_ok_p = false;
16989 /* Defer access checks while parsing the declarator; we cannot know
16990 what names are accessible until we know what is being
16991 declared. */
16992 resume_deferring_access_checks ();
16994 /* Parse the declarator. */
16995 token = cp_lexer_peek_token (parser->lexer);
16996 declarator
16997 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16998 &ctor_dtor_or_conv_p,
16999 /*parenthesized_p=*/NULL,
17000 member_p);
17001 /* Gather up the deferred checks. */
17002 stop_deferring_access_checks ();
17004 parser->default_arg_ok_p = saved_default_arg_ok_p;
17006 /* If the DECLARATOR was erroneous, there's no need to go
17007 further. */
17008 if (declarator == cp_error_declarator)
17009 return error_mark_node;
17011 /* Check that the number of template-parameter-lists is OK. */
17012 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17013 token->location))
17014 return error_mark_node;
17016 if (declares_class_or_enum & 2)
17017 cp_parser_check_for_definition_in_return_type (declarator,
17018 decl_specifiers->type,
17019 decl_specifiers->locations[ds_type_spec]);
17021 /* Figure out what scope the entity declared by the DECLARATOR is
17022 located in. `grokdeclarator' sometimes changes the scope, so
17023 we compute it now. */
17024 scope = get_scope_of_declarator (declarator);
17026 /* Perform any lookups in the declared type which were thought to be
17027 dependent, but are not in the scope of the declarator. */
17028 decl_specifiers->type
17029 = maybe_update_decl_type (decl_specifiers->type, scope);
17031 /* If we're allowing GNU extensions, look for an
17032 asm-specification. */
17033 if (cp_parser_allow_gnu_extensions_p (parser))
17035 /* Look for an asm-specification. */
17036 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17037 asm_specification = cp_parser_asm_specification_opt (parser);
17039 else
17040 asm_specification = NULL_TREE;
17042 /* Look for attributes. */
17043 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17044 attributes = cp_parser_attributes_opt (parser);
17047 // If scope is non-null, then we're parsing a declarator with
17048 // a nested-name specifier. It's possible that some component of
17049 // that specifier is bringing template contsraints with it, which
17050 // we need to suppress for for the time being. For example:
17052 // template<typename T>
17053 // requires C<T>()
17054 // void S<T>::f() requires D<T>() { ... }
17056 // At the point we parse the 2nd requires clause, the previous the
17057 // current constraints will have been used to resolve the enclosing
17058 // class S<T>. The D<T>() requirement applies only to the definition
17059 // of f and do not include C<T>().
17061 // Save requirements, resetting them if the scope was established.
17062 cp_manage_requirements saved_requirements (scope != NULL_TREE);
17064 // TODO: Check that a declaration does not have 2 requires clauses.
17066 // Parse an optional trailing requires clause for the parsed declarator.
17067 if (flag_concepts)
17068 cp_parser_trailing_requirements (parser, declarator);
17070 /* Peek at the next token. */
17071 token = cp_lexer_peek_token (parser->lexer);
17073 if (function_declarator_p (declarator))
17075 /* Check to see if the token indicates the start of a
17076 function-definition. */
17077 if (cp_parser_token_starts_function_definition_p (token))
17079 if (!function_definition_allowed_p)
17081 /* If a function-definition should not appear here, issue an
17082 error message. */
17083 cp_parser_error (parser,
17084 "a function-definition is not allowed here");
17085 return error_mark_node;
17088 location_t func_brace_location
17089 = cp_lexer_peek_token (parser->lexer)->location;
17091 /* Neither attributes nor an asm-specification are allowed
17092 on a function-definition. */
17093 if (asm_specification)
17094 error_at (asm_spec_start_token->location,
17095 "an asm-specification is not allowed "
17096 "on a function-definition");
17097 if (attributes)
17098 error_at (attributes_start_token->location,
17099 "attributes are not allowed "
17100 "on a function-definition");
17101 /* This is a function-definition. */
17102 *function_definition_p = true;
17104 /* Parse the function definition. */
17105 if (member_p)
17106 decl = cp_parser_save_member_function_body (parser,
17107 decl_specifiers,
17108 declarator,
17109 prefix_attributes);
17110 else
17111 decl =
17112 (cp_parser_function_definition_from_specifiers_and_declarator
17113 (parser, decl_specifiers, prefix_attributes, declarator));
17115 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17117 /* This is where the prologue starts... */
17118 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17119 = func_brace_location;
17122 return decl;
17126 /* [dcl.dcl]
17128 Only in function declarations for constructors, destructors, and
17129 type conversions can the decl-specifier-seq be omitted.
17131 We explicitly postpone this check past the point where we handle
17132 function-definitions because we tolerate function-definitions
17133 that are missing their return types in some modes. */
17134 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17136 cp_parser_error (parser,
17137 "expected constructor, destructor, or type conversion");
17138 return error_mark_node;
17141 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17142 if (token->type == CPP_EQ
17143 || token->type == CPP_OPEN_PAREN
17144 || token->type == CPP_OPEN_BRACE)
17146 is_initialized = SD_INITIALIZED;
17147 initialization_kind = token->type;
17148 if (maybe_range_for_decl)
17149 *maybe_range_for_decl = error_mark_node;
17151 if (token->type == CPP_EQ
17152 && function_declarator_p (declarator))
17154 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17155 if (t2->keyword == RID_DEFAULT)
17156 is_initialized = SD_DEFAULTED;
17157 else if (t2->keyword == RID_DELETE)
17158 is_initialized = SD_DELETED;
17161 else
17163 /* If the init-declarator isn't initialized and isn't followed by a
17164 `,' or `;', it's not a valid init-declarator. */
17165 if (token->type != CPP_COMMA
17166 && token->type != CPP_SEMICOLON)
17168 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17169 range_for_decl_p = true;
17170 else
17172 cp_parser_error (parser, "expected initializer");
17173 return error_mark_node;
17176 is_initialized = SD_UNINITIALIZED;
17177 initialization_kind = CPP_EOF;
17180 /* Because start_decl has side-effects, we should only call it if we
17181 know we're going ahead. By this point, we know that we cannot
17182 possibly be looking at any other construct. */
17183 cp_parser_commit_to_tentative_parse (parser);
17185 /* If the decl specifiers were bad, issue an error now that we're
17186 sure this was intended to be a declarator. Then continue
17187 declaring the variable(s), as int, to try to cut down on further
17188 errors. */
17189 if (decl_specifiers->any_specifiers_p
17190 && decl_specifiers->type == error_mark_node)
17192 cp_parser_error (parser, "invalid type in declaration");
17193 decl_specifiers->type = integer_type_node;
17196 /* Check to see whether or not this declaration is a friend. */
17197 friend_p = cp_parser_friend_p (decl_specifiers);
17199 /* Enter the newly declared entry in the symbol table. If we're
17200 processing a declaration in a class-specifier, we wait until
17201 after processing the initializer. */
17202 if (!member_p)
17204 if (parser->in_unbraced_linkage_specification_p)
17205 decl_specifiers->storage_class = sc_extern;
17206 decl = start_decl (declarator, decl_specifiers,
17207 range_for_decl_p? SD_INITIALIZED : is_initialized,
17208 attributes, prefix_attributes, &pushed_scope);
17209 cp_finalize_omp_declare_simd (parser, decl);
17210 /* Adjust location of decl if declarator->id_loc is more appropriate:
17211 set, and decl wasn't merged with another decl, in which case its
17212 location would be different from input_location, and more accurate. */
17213 if (DECL_P (decl)
17214 && declarator->id_loc != UNKNOWN_LOCATION
17215 && DECL_SOURCE_LOCATION (decl) == input_location)
17216 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17218 else if (scope)
17219 /* Enter the SCOPE. That way unqualified names appearing in the
17220 initializer will be looked up in SCOPE. */
17221 pushed_scope = push_scope (scope);
17223 /* Perform deferred access control checks, now that we know in which
17224 SCOPE the declared entity resides. */
17225 if (!member_p && decl)
17227 tree saved_current_function_decl = NULL_TREE;
17229 /* If the entity being declared is a function, pretend that we
17230 are in its scope. If it is a `friend', it may have access to
17231 things that would not otherwise be accessible. */
17232 if (TREE_CODE (decl) == FUNCTION_DECL)
17234 saved_current_function_decl = current_function_decl;
17235 current_function_decl = decl;
17238 /* Perform access checks for template parameters. */
17239 cp_parser_perform_template_parameter_access_checks (checks);
17241 /* Perform the access control checks for the declarator and the
17242 decl-specifiers. */
17243 perform_deferred_access_checks (tf_warning_or_error);
17245 /* Restore the saved value. */
17246 if (TREE_CODE (decl) == FUNCTION_DECL)
17247 current_function_decl = saved_current_function_decl;
17250 /* Parse the initializer. */
17251 initializer = NULL_TREE;
17252 is_direct_init = false;
17253 is_non_constant_init = true;
17254 if (is_initialized)
17256 if (function_declarator_p (declarator))
17258 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17259 if (initialization_kind == CPP_EQ)
17260 initializer = cp_parser_pure_specifier (parser);
17261 else
17263 /* If the declaration was erroneous, we don't really
17264 know what the user intended, so just silently
17265 consume the initializer. */
17266 if (decl != error_mark_node)
17267 error_at (initializer_start_token->location,
17268 "initializer provided for function");
17269 cp_parser_skip_to_closing_parenthesis (parser,
17270 /*recovering=*/true,
17271 /*or_comma=*/false,
17272 /*consume_paren=*/true);
17275 else
17277 /* We want to record the extra mangling scope for in-class
17278 initializers of class members and initializers of static data
17279 member templates. The former involves deferring
17280 parsing of the initializer until end of class as with default
17281 arguments. So right here we only handle the latter. */
17282 if (!member_p && processing_template_decl)
17283 start_lambda_scope (decl);
17284 initializer = cp_parser_initializer (parser,
17285 &is_direct_init,
17286 &is_non_constant_init);
17287 if (!member_p && processing_template_decl)
17288 finish_lambda_scope ();
17289 if (initializer == error_mark_node)
17290 cp_parser_skip_to_end_of_statement (parser);
17294 /* The old parser allows attributes to appear after a parenthesized
17295 initializer. Mark Mitchell proposed removing this functionality
17296 on the GCC mailing lists on 2002-08-13. This parser accepts the
17297 attributes -- but ignores them. */
17298 if (cp_parser_allow_gnu_extensions_p (parser)
17299 && initialization_kind == CPP_OPEN_PAREN)
17300 if (cp_parser_attributes_opt (parser))
17301 warning (OPT_Wattributes,
17302 "attributes after parenthesized initializer ignored");
17304 /* A non-template declaration involving a function parameter list containing
17305 an implicit template parameter will have been made into a template. If it
17306 turns out that the resulting declaration is not an actual function then
17307 finish the template declaration here. An error message will already have
17308 been issued. */
17309 if (parser->fully_implicit_function_template_p)
17310 if (!function_declarator_p (declarator))
17312 if (pushed_scope)
17314 pop_scope (pushed_scope);
17315 pushed_scope = 0;
17317 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17320 /* For an in-class declaration, use `grokfield' to create the
17321 declaration. */
17322 if (member_p)
17324 if (pushed_scope)
17326 pop_scope (pushed_scope);
17327 pushed_scope = NULL_TREE;
17329 decl = grokfield (declarator, decl_specifiers,
17330 initializer, !is_non_constant_init,
17331 /*asmspec=*/NULL_TREE,
17332 chainon (attributes, prefix_attributes));
17333 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17334 cp_parser_save_default_args (parser, decl);
17335 cp_finalize_omp_declare_simd (parser, decl);
17338 /* Finish processing the declaration. But, skip member
17339 declarations. */
17340 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17342 cp_finish_decl (decl,
17343 initializer, !is_non_constant_init,
17344 asm_specification,
17345 /* If the initializer is in parentheses, then this is
17346 a direct-initialization, which means that an
17347 `explicit' constructor is OK. Otherwise, an
17348 `explicit' constructor cannot be used. */
17349 ((is_direct_init || !is_initialized)
17350 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17352 else if ((cxx_dialect != cxx98) && friend_p
17353 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17354 /* Core issue #226 (C++0x only): A default template-argument
17355 shall not be specified in a friend class template
17356 declaration. */
17357 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17358 /*is_partial=*/false, /*is_friend_decl=*/1);
17360 if (!friend_p && pushed_scope)
17361 pop_scope (pushed_scope);
17363 if (function_declarator_p (declarator)
17364 && parser->fully_implicit_function_template_p)
17366 if (member_p)
17367 decl = finish_fully_implicit_template (parser, decl);
17368 else
17369 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17372 return decl;
17375 /* Parse a declarator.
17377 declarator:
17378 direct-declarator
17379 ptr-operator declarator
17381 abstract-declarator:
17382 ptr-operator abstract-declarator [opt]
17383 direct-abstract-declarator
17385 GNU Extensions:
17387 declarator:
17388 attributes [opt] direct-declarator
17389 attributes [opt] ptr-operator declarator
17391 abstract-declarator:
17392 attributes [opt] ptr-operator abstract-declarator [opt]
17393 attributes [opt] direct-abstract-declarator
17395 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17396 detect constructor, destructor or conversion operators. It is set
17397 to -1 if the declarator is a name, and +1 if it is a
17398 function. Otherwise it is set to zero. Usually you just want to
17399 test for >0, but internally the negative value is used.
17401 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17402 a decl-specifier-seq unless it declares a constructor, destructor,
17403 or conversion. It might seem that we could check this condition in
17404 semantic analysis, rather than parsing, but that makes it difficult
17405 to handle something like `f()'. We want to notice that there are
17406 no decl-specifiers, and therefore realize that this is an
17407 expression, not a declaration.)
17409 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17410 the declarator is a direct-declarator of the form "(...)".
17412 MEMBER_P is true iff this declarator is a member-declarator. */
17414 static cp_declarator *
17415 cp_parser_declarator (cp_parser* parser,
17416 cp_parser_declarator_kind dcl_kind,
17417 int* ctor_dtor_or_conv_p,
17418 bool* parenthesized_p,
17419 bool member_p)
17421 cp_declarator *declarator;
17422 enum tree_code code;
17423 cp_cv_quals cv_quals;
17424 tree class_type;
17425 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17427 /* Assume this is not a constructor, destructor, or type-conversion
17428 operator. */
17429 if (ctor_dtor_or_conv_p)
17430 *ctor_dtor_or_conv_p = 0;
17432 if (cp_parser_allow_gnu_extensions_p (parser))
17433 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17435 /* Check for the ptr-operator production. */
17436 cp_parser_parse_tentatively (parser);
17437 /* Parse the ptr-operator. */
17438 code = cp_parser_ptr_operator (parser,
17439 &class_type,
17440 &cv_quals,
17441 &std_attributes);
17443 /* If that worked, then we have a ptr-operator. */
17444 if (cp_parser_parse_definitely (parser))
17446 /* If a ptr-operator was found, then this declarator was not
17447 parenthesized. */
17448 if (parenthesized_p)
17449 *parenthesized_p = true;
17450 /* The dependent declarator is optional if we are parsing an
17451 abstract-declarator. */
17452 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17453 cp_parser_parse_tentatively (parser);
17455 /* Parse the dependent declarator. */
17456 declarator = cp_parser_declarator (parser, dcl_kind,
17457 /*ctor_dtor_or_conv_p=*/NULL,
17458 /*parenthesized_p=*/NULL,
17459 /*member_p=*/false);
17461 /* If we are parsing an abstract-declarator, we must handle the
17462 case where the dependent declarator is absent. */
17463 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17464 && !cp_parser_parse_definitely (parser))
17465 declarator = NULL;
17467 declarator = cp_parser_make_indirect_declarator
17468 (code, class_type, cv_quals, declarator, std_attributes);
17470 /* Everything else is a direct-declarator. */
17471 else
17473 if (parenthesized_p)
17474 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17475 CPP_OPEN_PAREN);
17476 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17477 ctor_dtor_or_conv_p,
17478 member_p);
17481 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17482 declarator->attributes = gnu_attributes;
17484 return declarator;
17487 /* Parse a direct-declarator or direct-abstract-declarator.
17489 direct-declarator:
17490 declarator-id
17491 direct-declarator ( parameter-declaration-clause )
17492 cv-qualifier-seq [opt]
17493 ref-qualifier [opt]
17494 exception-specification [opt]
17495 direct-declarator [ constant-expression [opt] ]
17496 ( declarator )
17498 direct-abstract-declarator:
17499 direct-abstract-declarator [opt]
17500 ( parameter-declaration-clause )
17501 cv-qualifier-seq [opt]
17502 ref-qualifier [opt]
17503 exception-specification [opt]
17504 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17505 ( abstract-declarator )
17507 Returns a representation of the declarator. DCL_KIND is
17508 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17509 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17510 we are parsing a direct-declarator. It is
17511 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17512 of ambiguity we prefer an abstract declarator, as per
17513 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17514 cp_parser_declarator. */
17516 static cp_declarator *
17517 cp_parser_direct_declarator (cp_parser* parser,
17518 cp_parser_declarator_kind dcl_kind,
17519 int* ctor_dtor_or_conv_p,
17520 bool member_p)
17522 cp_token *token;
17523 cp_declarator *declarator = NULL;
17524 tree scope = NULL_TREE;
17525 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17526 bool saved_in_declarator_p = parser->in_declarator_p;
17527 bool first = true;
17528 tree pushed_scope = NULL_TREE;
17530 while (true)
17532 /* Peek at the next token. */
17533 token = cp_lexer_peek_token (parser->lexer);
17534 if (token->type == CPP_OPEN_PAREN)
17536 /* This is either a parameter-declaration-clause, or a
17537 parenthesized declarator. When we know we are parsing a
17538 named declarator, it must be a parenthesized declarator
17539 if FIRST is true. For instance, `(int)' is a
17540 parameter-declaration-clause, with an omitted
17541 direct-abstract-declarator. But `((*))', is a
17542 parenthesized abstract declarator. Finally, when T is a
17543 template parameter `(T)' is a
17544 parameter-declaration-clause, and not a parenthesized
17545 named declarator.
17547 We first try and parse a parameter-declaration-clause,
17548 and then try a nested declarator (if FIRST is true).
17550 It is not an error for it not to be a
17551 parameter-declaration-clause, even when FIRST is
17552 false. Consider,
17554 int i (int);
17555 int i (3);
17557 The first is the declaration of a function while the
17558 second is the definition of a variable, including its
17559 initializer.
17561 Having seen only the parenthesis, we cannot know which of
17562 these two alternatives should be selected. Even more
17563 complex are examples like:
17565 int i (int (a));
17566 int i (int (3));
17568 The former is a function-declaration; the latter is a
17569 variable initialization.
17571 Thus again, we try a parameter-declaration-clause, and if
17572 that fails, we back out and return. */
17574 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17576 tree params;
17577 bool is_declarator = false;
17579 /* In a member-declarator, the only valid interpretation
17580 of a parenthesis is the start of a
17581 parameter-declaration-clause. (It is invalid to
17582 initialize a static data member with a parenthesized
17583 initializer; only the "=" form of initialization is
17584 permitted.) */
17585 if (!member_p)
17586 cp_parser_parse_tentatively (parser);
17588 /* Consume the `('. */
17589 cp_lexer_consume_token (parser->lexer);
17590 if (first)
17592 /* If this is going to be an abstract declarator, we're
17593 in a declarator and we can't have default args. */
17594 parser->default_arg_ok_p = false;
17595 parser->in_declarator_p = true;
17598 begin_scope (sk_function_parms, NULL_TREE);
17600 /* Parse the parameter-declaration-clause. */
17601 params = cp_parser_parameter_declaration_clause (parser);
17603 /* Consume the `)'. */
17604 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17606 /* If all went well, parse the cv-qualifier-seq,
17607 ref-qualifier and the exception-specification. */
17608 if (member_p || cp_parser_parse_definitely (parser))
17610 cp_cv_quals cv_quals;
17611 cp_virt_specifiers virt_specifiers;
17612 cp_ref_qualifier ref_qual;
17613 tree exception_specification;
17614 tree late_return;
17615 tree attrs;
17616 bool memfn = (member_p || (pushed_scope
17617 && CLASS_TYPE_P (pushed_scope)));
17619 is_declarator = true;
17621 if (ctor_dtor_or_conv_p)
17622 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17623 first = false;
17625 /* Parse the cv-qualifier-seq. */
17626 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17627 /* Parse the ref-qualifier. */
17628 ref_qual = cp_parser_ref_qualifier_opt (parser);
17629 /* And the exception-specification. */
17630 exception_specification
17631 = cp_parser_exception_specification_opt (parser);
17633 attrs = cp_parser_std_attribute_spec_seq (parser);
17635 /* In here, we handle cases where attribute is used after
17636 the function declaration. For example:
17637 void func (int x) __attribute__((vector(..))); */
17638 if (flag_cilkplus
17639 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17641 cp_parser_parse_tentatively (parser);
17642 tree attr = cp_parser_gnu_attributes_opt (parser);
17643 if (cp_lexer_next_token_is_not (parser->lexer,
17644 CPP_SEMICOLON)
17645 && cp_lexer_next_token_is_not (parser->lexer,
17646 CPP_OPEN_BRACE))
17647 cp_parser_abort_tentative_parse (parser);
17648 else if (!cp_parser_parse_definitely (parser))
17650 else
17651 attrs = chainon (attr, attrs);
17653 late_return = (cp_parser_late_return_type_opt
17654 (parser, declarator,
17655 memfn ? cv_quals : -1));
17658 /* Parse the virt-specifier-seq. */
17659 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17661 /* Create the function-declarator. */
17662 declarator = make_call_declarator (declarator,
17663 params,
17664 cv_quals,
17665 virt_specifiers,
17666 ref_qual,
17667 exception_specification,
17668 late_return);
17669 declarator->std_attributes = attrs;
17670 /* Any subsequent parameter lists are to do with
17671 return type, so are not those of the declared
17672 function. */
17673 parser->default_arg_ok_p = false;
17676 /* Remove the function parms from scope. */
17677 pop_bindings_and_leave_scope ();
17679 if (is_declarator)
17680 /* Repeat the main loop. */
17681 continue;
17684 /* If this is the first, we can try a parenthesized
17685 declarator. */
17686 if (first)
17688 bool saved_in_type_id_in_expr_p;
17690 parser->default_arg_ok_p = saved_default_arg_ok_p;
17691 parser->in_declarator_p = saved_in_declarator_p;
17693 /* Consume the `('. */
17694 cp_lexer_consume_token (parser->lexer);
17695 /* Parse the nested declarator. */
17696 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17697 parser->in_type_id_in_expr_p = true;
17698 declarator
17699 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17700 /*parenthesized_p=*/NULL,
17701 member_p);
17702 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17703 first = false;
17704 /* Expect a `)'. */
17705 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17706 declarator = cp_error_declarator;
17707 if (declarator == cp_error_declarator)
17708 break;
17710 goto handle_declarator;
17712 /* Otherwise, we must be done. */
17713 else
17714 break;
17716 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17717 && token->type == CPP_OPEN_SQUARE
17718 && !cp_next_tokens_can_be_attribute_p (parser))
17720 /* Parse an array-declarator. */
17721 tree bounds, attrs;
17723 if (ctor_dtor_or_conv_p)
17724 *ctor_dtor_or_conv_p = 0;
17726 first = false;
17727 parser->default_arg_ok_p = false;
17728 parser->in_declarator_p = true;
17729 /* Consume the `['. */
17730 cp_lexer_consume_token (parser->lexer);
17731 /* Peek at the next token. */
17732 token = cp_lexer_peek_token (parser->lexer);
17733 /* If the next token is `]', then there is no
17734 constant-expression. */
17735 if (token->type != CPP_CLOSE_SQUARE)
17737 bool non_constant_p;
17738 bounds
17739 = cp_parser_constant_expression (parser,
17740 /*allow_non_constant=*/true,
17741 &non_constant_p);
17742 if (!non_constant_p)
17743 /* OK */;
17744 else if (error_operand_p (bounds))
17745 /* Already gave an error. */;
17746 else if (!parser->in_function_body
17747 || current_binding_level->kind == sk_function_parms)
17749 /* Normally, the array bound must be an integral constant
17750 expression. However, as an extension, we allow VLAs
17751 in function scopes as long as they aren't part of a
17752 parameter declaration. */
17753 cp_parser_error (parser,
17754 "array bound is not an integer constant");
17755 bounds = error_mark_node;
17757 else if (processing_template_decl
17758 && !type_dependent_expression_p (bounds))
17760 /* Remember this wasn't a constant-expression. */
17761 bounds = build_nop (TREE_TYPE (bounds), bounds);
17762 TREE_SIDE_EFFECTS (bounds) = 1;
17765 else
17766 bounds = NULL_TREE;
17767 /* Look for the closing `]'. */
17768 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17770 declarator = cp_error_declarator;
17771 break;
17774 attrs = cp_parser_std_attribute_spec_seq (parser);
17775 declarator = make_array_declarator (declarator, bounds);
17776 declarator->std_attributes = attrs;
17778 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17781 tree qualifying_scope;
17782 tree unqualified_name;
17783 tree attrs;
17784 special_function_kind sfk;
17785 bool abstract_ok;
17786 bool pack_expansion_p = false;
17787 cp_token *declarator_id_start_token;
17789 /* Parse a declarator-id */
17790 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17791 if (abstract_ok)
17793 cp_parser_parse_tentatively (parser);
17795 /* If we see an ellipsis, we should be looking at a
17796 parameter pack. */
17797 if (token->type == CPP_ELLIPSIS)
17799 /* Consume the `...' */
17800 cp_lexer_consume_token (parser->lexer);
17802 pack_expansion_p = true;
17806 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17807 unqualified_name
17808 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17809 qualifying_scope = parser->scope;
17810 if (abstract_ok)
17812 bool okay = false;
17814 if (!unqualified_name && pack_expansion_p)
17816 /* Check whether an error occurred. */
17817 okay = !cp_parser_error_occurred (parser);
17819 /* We already consumed the ellipsis to mark a
17820 parameter pack, but we have no way to report it,
17821 so abort the tentative parse. We will be exiting
17822 immediately anyway. */
17823 cp_parser_abort_tentative_parse (parser);
17825 else
17826 okay = cp_parser_parse_definitely (parser);
17828 if (!okay)
17829 unqualified_name = error_mark_node;
17830 else if (unqualified_name
17831 && (qualifying_scope
17832 || (!identifier_p (unqualified_name))))
17834 cp_parser_error (parser, "expected unqualified-id");
17835 unqualified_name = error_mark_node;
17839 if (!unqualified_name)
17840 return NULL;
17841 if (unqualified_name == error_mark_node)
17843 declarator = cp_error_declarator;
17844 pack_expansion_p = false;
17845 declarator->parameter_pack_p = false;
17846 break;
17849 attrs = cp_parser_std_attribute_spec_seq (parser);
17851 if (qualifying_scope && at_namespace_scope_p ()
17852 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17854 /* In the declaration of a member of a template class
17855 outside of the class itself, the SCOPE will sometimes
17856 be a TYPENAME_TYPE. For example, given:
17858 template <typename T>
17859 int S<T>::R::i = 3;
17861 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17862 this context, we must resolve S<T>::R to an ordinary
17863 type, rather than a typename type.
17865 The reason we normally avoid resolving TYPENAME_TYPEs
17866 is that a specialization of `S' might render
17867 `S<T>::R' not a type. However, if `S' is
17868 specialized, then this `i' will not be used, so there
17869 is no harm in resolving the types here. */
17870 tree type;
17872 /* Resolve the TYPENAME_TYPE. */
17873 type = resolve_typename_type (qualifying_scope,
17874 /*only_current_p=*/false);
17875 /* If that failed, the declarator is invalid. */
17876 if (TREE_CODE (type) == TYPENAME_TYPE)
17878 if (typedef_variant_p (type))
17879 error_at (declarator_id_start_token->location,
17880 "cannot define member of dependent typedef "
17881 "%qT", type);
17882 else
17883 error_at (declarator_id_start_token->location,
17884 "%<%T::%E%> is not a type",
17885 TYPE_CONTEXT (qualifying_scope),
17886 TYPE_IDENTIFIER (qualifying_scope));
17888 qualifying_scope = type;
17891 sfk = sfk_none;
17893 if (unqualified_name)
17895 tree class_type;
17897 if (qualifying_scope
17898 && CLASS_TYPE_P (qualifying_scope))
17899 class_type = qualifying_scope;
17900 else
17901 class_type = current_class_type;
17903 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17905 tree name_type = TREE_TYPE (unqualified_name);
17906 if (class_type && same_type_p (name_type, class_type))
17908 if (qualifying_scope
17909 && CLASSTYPE_USE_TEMPLATE (name_type))
17911 error_at (declarator_id_start_token->location,
17912 "invalid use of constructor as a template");
17913 inform (declarator_id_start_token->location,
17914 "use %<%T::%D%> instead of %<%T::%D%> to "
17915 "name the constructor in a qualified name",
17916 class_type,
17917 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17918 class_type, name_type);
17919 declarator = cp_error_declarator;
17920 break;
17922 else
17923 unqualified_name = constructor_name (class_type);
17925 else
17927 /* We do not attempt to print the declarator
17928 here because we do not have enough
17929 information about its original syntactic
17930 form. */
17931 cp_parser_error (parser, "invalid declarator");
17932 declarator = cp_error_declarator;
17933 break;
17937 if (class_type)
17939 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17940 sfk = sfk_destructor;
17941 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17942 sfk = sfk_conversion;
17943 else if (/* There's no way to declare a constructor
17944 for an anonymous type, even if the type
17945 got a name for linkage purposes. */
17946 !TYPE_WAS_ANONYMOUS (class_type)
17947 && constructor_name_p (unqualified_name,
17948 class_type))
17950 unqualified_name = constructor_name (class_type);
17951 sfk = sfk_constructor;
17953 else if (is_overloaded_fn (unqualified_name)
17954 && DECL_CONSTRUCTOR_P (get_first_fn
17955 (unqualified_name)))
17956 sfk = sfk_constructor;
17958 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17959 *ctor_dtor_or_conv_p = -1;
17962 declarator = make_id_declarator (qualifying_scope,
17963 unqualified_name,
17964 sfk);
17965 declarator->std_attributes = attrs;
17966 declarator->id_loc = token->location;
17967 declarator->parameter_pack_p = pack_expansion_p;
17969 if (pack_expansion_p)
17970 maybe_warn_variadic_templates ();
17973 handle_declarator:;
17974 scope = get_scope_of_declarator (declarator);
17975 if (scope)
17977 /* Any names that appear after the declarator-id for a
17978 member are looked up in the containing scope. */
17979 if (at_function_scope_p ())
17981 /* But declarations with qualified-ids can't appear in a
17982 function. */
17983 cp_parser_error (parser, "qualified-id in declaration");
17984 declarator = cp_error_declarator;
17985 break;
17987 pushed_scope = push_scope (scope);
17989 parser->in_declarator_p = true;
17990 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17991 || (declarator && declarator->kind == cdk_id))
17992 /* Default args are only allowed on function
17993 declarations. */
17994 parser->default_arg_ok_p = saved_default_arg_ok_p;
17995 else
17996 parser->default_arg_ok_p = false;
17998 first = false;
18000 /* We're done. */
18001 else
18002 break;
18005 /* For an abstract declarator, we might wind up with nothing at this
18006 point. That's an error; the declarator is not optional. */
18007 if (!declarator)
18008 cp_parser_error (parser, "expected declarator");
18010 /* If we entered a scope, we must exit it now. */
18011 if (pushed_scope)
18012 pop_scope (pushed_scope);
18014 parser->default_arg_ok_p = saved_default_arg_ok_p;
18015 parser->in_declarator_p = saved_in_declarator_p;
18017 return declarator;
18020 /* Parse a ptr-operator.
18022 ptr-operator:
18023 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18024 * cv-qualifier-seq [opt]
18026 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18027 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18029 GNU Extension:
18031 ptr-operator:
18032 & cv-qualifier-seq [opt]
18034 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18035 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18036 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18037 filled in with the TYPE containing the member. *CV_QUALS is
18038 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18039 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18040 Note that the tree codes returned by this function have nothing
18041 to do with the types of trees that will be eventually be created
18042 to represent the pointer or reference type being parsed. They are
18043 just constants with suggestive names. */
18044 static enum tree_code
18045 cp_parser_ptr_operator (cp_parser* parser,
18046 tree* type,
18047 cp_cv_quals *cv_quals,
18048 tree *attributes)
18050 enum tree_code code = ERROR_MARK;
18051 cp_token *token;
18052 tree attrs = NULL_TREE;
18054 /* Assume that it's not a pointer-to-member. */
18055 *type = NULL_TREE;
18056 /* And that there are no cv-qualifiers. */
18057 *cv_quals = TYPE_UNQUALIFIED;
18059 /* Peek at the next token. */
18060 token = cp_lexer_peek_token (parser->lexer);
18062 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18063 if (token->type == CPP_MULT)
18064 code = INDIRECT_REF;
18065 else if (token->type == CPP_AND)
18066 code = ADDR_EXPR;
18067 else if ((cxx_dialect != cxx98) &&
18068 token->type == CPP_AND_AND) /* C++0x only */
18069 code = NON_LVALUE_EXPR;
18071 if (code != ERROR_MARK)
18073 /* Consume the `*', `&' or `&&'. */
18074 cp_lexer_consume_token (parser->lexer);
18076 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18077 `&', if we are allowing GNU extensions. (The only qualifier
18078 that can legally appear after `&' is `restrict', but that is
18079 enforced during semantic analysis. */
18080 if (code == INDIRECT_REF
18081 || cp_parser_allow_gnu_extensions_p (parser))
18082 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18084 attrs = cp_parser_std_attribute_spec_seq (parser);
18085 if (attributes != NULL)
18086 *attributes = attrs;
18088 else
18090 /* Try the pointer-to-member case. */
18091 cp_parser_parse_tentatively (parser);
18092 /* Look for the optional `::' operator. */
18093 cp_parser_global_scope_opt (parser,
18094 /*current_scope_valid_p=*/false);
18095 /* Look for the nested-name specifier. */
18096 token = cp_lexer_peek_token (parser->lexer);
18097 cp_parser_nested_name_specifier (parser,
18098 /*typename_keyword_p=*/false,
18099 /*check_dependency_p=*/true,
18100 /*type_p=*/false,
18101 /*is_declaration=*/false);
18102 /* If we found it, and the next token is a `*', then we are
18103 indeed looking at a pointer-to-member operator. */
18104 if (!cp_parser_error_occurred (parser)
18105 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18107 /* Indicate that the `*' operator was used. */
18108 code = INDIRECT_REF;
18110 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18111 error_at (token->location, "%qD is a namespace", parser->scope);
18112 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18113 error_at (token->location, "cannot form pointer to member of "
18114 "non-class %q#T", parser->scope);
18115 else
18117 /* The type of which the member is a member is given by the
18118 current SCOPE. */
18119 *type = parser->scope;
18120 /* The next name will not be qualified. */
18121 parser->scope = NULL_TREE;
18122 parser->qualifying_scope = NULL_TREE;
18123 parser->object_scope = NULL_TREE;
18124 /* Look for optional c++11 attributes. */
18125 attrs = cp_parser_std_attribute_spec_seq (parser);
18126 if (attributes != NULL)
18127 *attributes = attrs;
18128 /* Look for the optional cv-qualifier-seq. */
18129 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18132 /* If that didn't work we don't have a ptr-operator. */
18133 if (!cp_parser_parse_definitely (parser))
18134 cp_parser_error (parser, "expected ptr-operator");
18137 return code;
18140 /* Parse an (optional) cv-qualifier-seq.
18142 cv-qualifier-seq:
18143 cv-qualifier cv-qualifier-seq [opt]
18145 cv-qualifier:
18146 const
18147 volatile
18149 GNU Extension:
18151 cv-qualifier:
18152 __restrict__
18154 Returns a bitmask representing the cv-qualifiers. */
18156 static cp_cv_quals
18157 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18159 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18161 while (true)
18163 cp_token *token;
18164 cp_cv_quals cv_qualifier;
18166 /* Peek at the next token. */
18167 token = cp_lexer_peek_token (parser->lexer);
18168 /* See if it's a cv-qualifier. */
18169 switch (token->keyword)
18171 case RID_CONST:
18172 cv_qualifier = TYPE_QUAL_CONST;
18173 break;
18175 case RID_VOLATILE:
18176 cv_qualifier = TYPE_QUAL_VOLATILE;
18177 break;
18179 case RID_RESTRICT:
18180 cv_qualifier = TYPE_QUAL_RESTRICT;
18181 break;
18183 default:
18184 cv_qualifier = TYPE_UNQUALIFIED;
18185 break;
18188 if (!cv_qualifier)
18189 break;
18191 if (cv_quals & cv_qualifier)
18193 error_at (token->location, "duplicate cv-qualifier");
18194 cp_lexer_purge_token (parser->lexer);
18196 else
18198 cp_lexer_consume_token (parser->lexer);
18199 cv_quals |= cv_qualifier;
18203 return cv_quals;
18206 /* Parse an (optional) ref-qualifier
18208 ref-qualifier:
18212 Returns cp_ref_qualifier representing ref-qualifier. */
18214 static cp_ref_qualifier
18215 cp_parser_ref_qualifier_opt (cp_parser* parser)
18217 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18219 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18220 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18221 return ref_qual;
18223 while (true)
18225 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18226 cp_token *token = cp_lexer_peek_token (parser->lexer);
18228 switch (token->type)
18230 case CPP_AND:
18231 curr_ref_qual = REF_QUAL_LVALUE;
18232 break;
18234 case CPP_AND_AND:
18235 curr_ref_qual = REF_QUAL_RVALUE;
18236 break;
18238 default:
18239 curr_ref_qual = REF_QUAL_NONE;
18240 break;
18243 if (!curr_ref_qual)
18244 break;
18245 else if (ref_qual)
18247 error_at (token->location, "multiple ref-qualifiers");
18248 cp_lexer_purge_token (parser->lexer);
18250 else
18252 ref_qual = curr_ref_qual;
18253 cp_lexer_consume_token (parser->lexer);
18257 return ref_qual;
18260 /* Parse an (optional) virt-specifier-seq.
18262 virt-specifier-seq:
18263 virt-specifier virt-specifier-seq [opt]
18265 virt-specifier:
18266 override
18267 final
18269 Returns a bitmask representing the virt-specifiers. */
18271 static cp_virt_specifiers
18272 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18274 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18276 while (true)
18278 cp_token *token;
18279 cp_virt_specifiers virt_specifier;
18281 /* Peek at the next token. */
18282 token = cp_lexer_peek_token (parser->lexer);
18283 /* See if it's a virt-specifier-qualifier. */
18284 if (token->type != CPP_NAME)
18285 break;
18286 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18288 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18289 virt_specifier = VIRT_SPEC_OVERRIDE;
18291 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18293 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18294 virt_specifier = VIRT_SPEC_FINAL;
18296 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18298 virt_specifier = VIRT_SPEC_FINAL;
18300 else
18301 break;
18303 if (virt_specifiers & virt_specifier)
18305 error_at (token->location, "duplicate virt-specifier");
18306 cp_lexer_purge_token (parser->lexer);
18308 else
18310 cp_lexer_consume_token (parser->lexer);
18311 virt_specifiers |= virt_specifier;
18314 return virt_specifiers;
18317 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18318 is in scope even though it isn't real. */
18320 void
18321 inject_this_parameter (tree ctype, cp_cv_quals quals)
18323 tree this_parm;
18325 if (current_class_ptr)
18327 /* We don't clear this between NSDMIs. Is it already what we want? */
18328 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18329 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18330 && cp_type_quals (type) == quals)
18331 return;
18334 this_parm = build_this_parm (ctype, quals);
18335 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18336 current_class_ptr = NULL_TREE;
18337 current_class_ref
18338 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18339 current_class_ptr = this_parm;
18342 /* Return true iff our current scope is a non-static data member
18343 initializer. */
18345 bool
18346 parsing_nsdmi (void)
18348 /* We recognize NSDMI context by the context-less 'this' pointer set up
18349 by the function above. */
18350 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18351 return true;
18352 return false;
18355 /* Parse a late-specified return type, if any. This is not a separate
18356 non-terminal, but part of a function declarator, which looks like
18358 -> trailing-type-specifier-seq abstract-declarator(opt)
18360 Returns the type indicated by the type-id.
18362 In addition to this this parses any queued up omp declare simd
18363 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18365 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18366 function. */
18368 static tree
18369 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18370 cp_cv_quals quals)
18372 cp_token *token;
18373 tree type = NULL_TREE;
18374 bool declare_simd_p = (parser->omp_declare_simd
18375 && declarator
18376 && declarator->kind == cdk_id);
18378 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18379 && declarator && declarator->kind == cdk_id);
18381 /* Peek at the next token. */
18382 token = cp_lexer_peek_token (parser->lexer);
18383 /* A late-specified return type is indicated by an initial '->'. */
18384 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18385 return NULL_TREE;
18387 tree save_ccp = current_class_ptr;
18388 tree save_ccr = current_class_ref;
18389 if (quals >= 0)
18391 /* DR 1207: 'this' is in scope in the trailing return type. */
18392 inject_this_parameter (current_class_type, quals);
18395 if (token->type == CPP_DEREF)
18397 /* Consume the ->. */
18398 cp_lexer_consume_token (parser->lexer);
18399 type = cp_parser_trailing_type_id (parser);
18402 if (cilk_simd_fn_vector_p)
18403 declarator->std_attributes
18404 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18405 declarator->std_attributes);
18406 if (declare_simd_p)
18407 declarator->std_attributes
18408 = cp_parser_late_parsing_omp_declare_simd (parser,
18409 declarator->std_attributes);
18411 if (quals >= 0)
18413 current_class_ptr = save_ccp;
18414 current_class_ref = save_ccr;
18417 return type;
18420 /* Parse a declarator-id.
18422 declarator-id:
18423 id-expression
18424 :: [opt] nested-name-specifier [opt] type-name
18426 In the `id-expression' case, the value returned is as for
18427 cp_parser_id_expression if the id-expression was an unqualified-id.
18428 If the id-expression was a qualified-id, then a SCOPE_REF is
18429 returned. The first operand is the scope (either a NAMESPACE_DECL
18430 or TREE_TYPE), but the second is still just a representation of an
18431 unqualified-id. */
18433 static tree
18434 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18436 tree id;
18437 /* The expression must be an id-expression. Assume that qualified
18438 names are the names of types so that:
18440 template <class T>
18441 int S<T>::R::i = 3;
18443 will work; we must treat `S<T>::R' as the name of a type.
18444 Similarly, assume that qualified names are templates, where
18445 required, so that:
18447 template <class T>
18448 int S<T>::R<T>::i = 3;
18450 will work, too. */
18451 id = cp_parser_id_expression (parser,
18452 /*template_keyword_p=*/false,
18453 /*check_dependency_p=*/false,
18454 /*template_p=*/NULL,
18455 /*declarator_p=*/true,
18456 optional_p);
18457 if (id && BASELINK_P (id))
18458 id = BASELINK_FUNCTIONS (id);
18459 return id;
18462 /* Parse a type-id.
18464 type-id:
18465 type-specifier-seq abstract-declarator [opt]
18467 Returns the TYPE specified. */
18469 static tree
18470 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18471 bool is_trailing_return)
18473 cp_decl_specifier_seq type_specifier_seq;
18474 cp_declarator *abstract_declarator;
18476 /* Parse the type-specifier-seq. */
18477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18478 is_trailing_return,
18479 &type_specifier_seq);
18480 if (type_specifier_seq.type == error_mark_node)
18481 return error_mark_node;
18483 /* There might or might not be an abstract declarator. */
18484 cp_parser_parse_tentatively (parser);
18485 /* Look for the declarator. */
18486 abstract_declarator
18487 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18488 /*parenthesized_p=*/NULL,
18489 /*member_p=*/false);
18490 /* Check to see if there really was a declarator. */
18491 if (!cp_parser_parse_definitely (parser))
18492 abstract_declarator = NULL;
18494 if (type_specifier_seq.type
18495 /* None of the valid uses of 'auto' in C++14 involve the type-id
18496 nonterminal, but it is valid in a trailing-return-type. */
18497 && !(cxx_dialect >= cxx1y && is_trailing_return)
18498 && type_uses_auto (type_specifier_seq.type))
18500 /* A type-id with type 'auto' is only ok if the abstract declarator
18501 is a function declarator with a late-specified return type.
18503 A type-id with 'auto' is also valid in a trailing-return-type
18504 in a compound-requirment. */
18505 if (abstract_declarator
18506 && abstract_declarator->kind == cdk_function
18507 && abstract_declarator->u.function.late_return_type)
18508 /* OK */;
18509 else if (parser->in_result_type_constraint_p)
18510 /* OK */;
18511 else
18513 error ("invalid use of %<auto%>");
18514 return error_mark_node;
18518 return groktypename (&type_specifier_seq, abstract_declarator,
18519 is_template_arg);
18522 static tree cp_parser_type_id (cp_parser *parser)
18524 return cp_parser_type_id_1 (parser, false, false);
18527 static tree cp_parser_template_type_arg (cp_parser *parser)
18529 tree r;
18530 const char *saved_message = parser->type_definition_forbidden_message;
18531 parser->type_definition_forbidden_message
18532 = G_("types may not be defined in template arguments");
18533 r = cp_parser_type_id_1 (parser, true, false);
18534 parser->type_definition_forbidden_message = saved_message;
18535 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18537 error ("invalid use of %<auto%> in template argument");
18538 r = error_mark_node;
18540 return r;
18543 static tree cp_parser_trailing_type_id (cp_parser *parser)
18545 return cp_parser_type_id_1 (parser, false, true);
18548 /* Parse a type-specifier-seq.
18550 type-specifier-seq:
18551 type-specifier type-specifier-seq [opt]
18553 GNU extension:
18555 type-specifier-seq:
18556 attributes type-specifier-seq [opt]
18558 If IS_DECLARATION is true, we are at the start of a "condition" or
18559 exception-declaration, so we might be followed by a declarator-id.
18561 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18562 i.e. we've just seen "->".
18564 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18566 static void
18567 cp_parser_type_specifier_seq (cp_parser* parser,
18568 bool is_declaration,
18569 bool is_trailing_return,
18570 cp_decl_specifier_seq *type_specifier_seq)
18572 bool seen_type_specifier = false;
18573 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18574 cp_token *start_token = NULL;
18576 /* Clear the TYPE_SPECIFIER_SEQ. */
18577 clear_decl_specs (type_specifier_seq);
18579 /* In the context of a trailing return type, enum E { } is an
18580 elaborated-type-specifier followed by a function-body, not an
18581 enum-specifier. */
18582 if (is_trailing_return)
18583 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18585 /* Parse the type-specifiers and attributes. */
18586 while (true)
18588 tree type_specifier;
18589 bool is_cv_qualifier;
18591 /* Check for attributes first. */
18592 if (cp_next_tokens_can_be_attribute_p (parser))
18594 type_specifier_seq->attributes =
18595 chainon (type_specifier_seq->attributes,
18596 cp_parser_attributes_opt (parser));
18597 continue;
18600 /* record the token of the beginning of the type specifier seq,
18601 for error reporting purposes*/
18602 if (!start_token)
18603 start_token = cp_lexer_peek_token (parser->lexer);
18605 /* Look for the type-specifier. */
18606 type_specifier = cp_parser_type_specifier (parser,
18607 flags,
18608 type_specifier_seq,
18609 /*is_declaration=*/false,
18610 NULL,
18611 &is_cv_qualifier);
18612 if (!type_specifier)
18614 /* If the first type-specifier could not be found, this is not a
18615 type-specifier-seq at all. */
18616 if (!seen_type_specifier)
18618 /* Set in_declarator_p to avoid skipping to the semicolon. */
18619 int in_decl = parser->in_declarator_p;
18620 parser->in_declarator_p = true;
18622 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18623 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18624 cp_parser_error (parser, "expected type-specifier");
18626 parser->in_declarator_p = in_decl;
18628 type_specifier_seq->type = error_mark_node;
18629 return;
18631 /* If subsequent type-specifiers could not be found, the
18632 type-specifier-seq is complete. */
18633 break;
18636 seen_type_specifier = true;
18637 /* The standard says that a condition can be:
18639 type-specifier-seq declarator = assignment-expression
18641 However, given:
18643 struct S {};
18644 if (int S = ...)
18646 we should treat the "S" as a declarator, not as a
18647 type-specifier. The standard doesn't say that explicitly for
18648 type-specifier-seq, but it does say that for
18649 decl-specifier-seq in an ordinary declaration. Perhaps it
18650 would be clearer just to allow a decl-specifier-seq here, and
18651 then add a semantic restriction that if any decl-specifiers
18652 that are not type-specifiers appear, the program is invalid. */
18653 if (is_declaration && !is_cv_qualifier)
18654 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18658 /* Return whether the function currently being declared has an associated
18659 template parameter list. */
18661 static bool
18662 function_being_declared_is_template_p (cp_parser* parser)
18664 if (!current_template_parms || processing_template_parmlist)
18665 return false;
18667 if (parser->implicit_template_scope)
18668 return true;
18670 if (at_class_scope_p ()
18671 && TYPE_BEING_DEFINED (current_class_type))
18672 return parser->num_template_parameter_lists != 0;
18674 return ((int) parser->num_template_parameter_lists > template_class_depth
18675 (current_class_type));
18678 /* Parse a parameter-declaration-clause.
18680 parameter-declaration-clause:
18681 parameter-declaration-list [opt] ... [opt]
18682 parameter-declaration-list , ...
18684 Returns a representation for the parameter declarations. A return
18685 value of NULL indicates a parameter-declaration-clause consisting
18686 only of an ellipsis. */
18688 static tree
18689 cp_parser_parameter_declaration_clause (cp_parser* parser)
18691 tree parameters;
18692 cp_token *token;
18693 bool ellipsis_p;
18694 bool is_error;
18696 struct cleanup {
18697 cp_parser* parser;
18698 int auto_is_implicit_function_template_parm_p;
18699 ~cleanup() {
18700 parser->auto_is_implicit_function_template_parm_p
18701 = auto_is_implicit_function_template_parm_p;
18703 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18705 (void) cleanup;
18707 if (!processing_specialization
18708 && !processing_template_parmlist
18709 && !processing_explicit_instantiation)
18710 if (!current_function_decl
18711 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18712 parser->auto_is_implicit_function_template_parm_p = true;
18714 /* Peek at the next token. */
18715 token = cp_lexer_peek_token (parser->lexer);
18716 /* Check for trivial parameter-declaration-clauses. */
18717 if (token->type == CPP_ELLIPSIS)
18719 /* Consume the `...' token. */
18720 cp_lexer_consume_token (parser->lexer);
18721 return NULL_TREE;
18723 else if (token->type == CPP_CLOSE_PAREN)
18724 /* There are no parameters. */
18726 #ifndef NO_IMPLICIT_EXTERN_C
18727 if (in_system_header_at (input_location)
18728 && current_class_type == NULL
18729 && current_lang_name == lang_name_c)
18730 return NULL_TREE;
18731 else
18732 #endif
18733 return void_list_node;
18735 /* Check for `(void)', too, which is a special case. */
18736 else if (token->keyword == RID_VOID
18737 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18738 == CPP_CLOSE_PAREN))
18740 /* Consume the `void' token. */
18741 cp_lexer_consume_token (parser->lexer);
18742 /* There are no parameters. */
18743 return void_list_node;
18746 /* Parse the parameter-declaration-list. */
18747 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18748 /* If a parse error occurred while parsing the
18749 parameter-declaration-list, then the entire
18750 parameter-declaration-clause is erroneous. */
18751 if (is_error)
18752 return NULL;
18754 /* Peek at the next token. */
18755 token = cp_lexer_peek_token (parser->lexer);
18756 /* If it's a `,', the clause should terminate with an ellipsis. */
18757 if (token->type == CPP_COMMA)
18759 /* Consume the `,'. */
18760 cp_lexer_consume_token (parser->lexer);
18761 /* Expect an ellipsis. */
18762 ellipsis_p
18763 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18765 /* It might also be `...' if the optional trailing `,' was
18766 omitted. */
18767 else if (token->type == CPP_ELLIPSIS)
18769 /* Consume the `...' token. */
18770 cp_lexer_consume_token (parser->lexer);
18771 /* And remember that we saw it. */
18772 ellipsis_p = true;
18774 else
18775 ellipsis_p = false;
18777 /* Finish the parameter list. */
18778 if (!ellipsis_p)
18779 parameters = chainon (parameters, void_list_node);
18781 return parameters;
18784 /* Parse a parameter-declaration-list.
18786 parameter-declaration-list:
18787 parameter-declaration
18788 parameter-declaration-list , parameter-declaration
18790 Returns a representation of the parameter-declaration-list, as for
18791 cp_parser_parameter_declaration_clause. However, the
18792 `void_list_node' is never appended to the list. Upon return,
18793 *IS_ERROR will be true iff an error occurred. */
18795 static tree
18796 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18798 tree parameters = NULL_TREE;
18799 tree *tail = &parameters;
18800 bool saved_in_unbraced_linkage_specification_p;
18801 int index = 0;
18803 /* Assume all will go well. */
18804 *is_error = false;
18805 /* The special considerations that apply to a function within an
18806 unbraced linkage specifications do not apply to the parameters
18807 to the function. */
18808 saved_in_unbraced_linkage_specification_p
18809 = parser->in_unbraced_linkage_specification_p;
18810 parser->in_unbraced_linkage_specification_p = false;
18812 /* Look for more parameters. */
18813 while (true)
18815 cp_parameter_declarator *parameter;
18816 tree decl = error_mark_node;
18817 bool parenthesized_p = false;
18818 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18819 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18820 (current_template_parms)) : 0);
18822 /* Parse the parameter. */
18823 parameter
18824 = cp_parser_parameter_declaration (parser,
18825 /*template_parm_p=*/false,
18826 &parenthesized_p);
18828 /* We don't know yet if the enclosing context is deprecated, so wait
18829 and warn in grokparms if appropriate. */
18830 deprecated_state = DEPRECATED_SUPPRESS;
18832 if (parameter)
18834 /* If a function parameter pack was specified and an implicit template
18835 parameter was introduced during cp_parser_parameter_declaration,
18836 change any implicit parameters introduced into packs. */
18837 if (parser->implicit_template_parms
18838 && parameter->declarator
18839 && parameter->declarator->parameter_pack_p)
18841 int latest_template_parm_idx = TREE_VEC_LENGTH
18842 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18844 if (latest_template_parm_idx != template_parm_idx)
18845 parameter->decl_specifiers.type = convert_generic_types_to_packs
18846 (parameter->decl_specifiers.type,
18847 template_parm_idx, latest_template_parm_idx);
18850 decl = grokdeclarator (parameter->declarator,
18851 &parameter->decl_specifiers,
18852 PARM,
18853 parameter->default_argument != NULL_TREE,
18854 &parameter->decl_specifiers.attributes);
18857 deprecated_state = DEPRECATED_NORMAL;
18859 /* If a parse error occurred parsing the parameter declaration,
18860 then the entire parameter-declaration-list is erroneous. */
18861 if (decl == error_mark_node)
18863 *is_error = true;
18864 parameters = error_mark_node;
18865 break;
18868 if (parameter->decl_specifiers.attributes)
18869 cplus_decl_attributes (&decl,
18870 parameter->decl_specifiers.attributes,
18872 if (DECL_NAME (decl))
18873 decl = pushdecl (decl);
18875 if (decl != error_mark_node)
18877 retrofit_lang_decl (decl);
18878 DECL_PARM_INDEX (decl) = ++index;
18879 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18882 /* Add the new parameter to the list. */
18883 *tail = build_tree_list (parameter->default_argument, decl);
18884 tail = &TREE_CHAIN (*tail);
18886 /* Peek at the next token. */
18887 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18888 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18889 /* These are for Objective-C++ */
18890 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18891 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18892 /* The parameter-declaration-list is complete. */
18893 break;
18894 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18896 cp_token *token;
18898 /* Peek at the next token. */
18899 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18900 /* If it's an ellipsis, then the list is complete. */
18901 if (token->type == CPP_ELLIPSIS)
18902 break;
18903 /* Otherwise, there must be more parameters. Consume the
18904 `,'. */
18905 cp_lexer_consume_token (parser->lexer);
18906 /* When parsing something like:
18908 int i(float f, double d)
18910 we can tell after seeing the declaration for "f" that we
18911 are not looking at an initialization of a variable "i",
18912 but rather at the declaration of a function "i".
18914 Due to the fact that the parsing of template arguments
18915 (as specified to a template-id) requires backtracking we
18916 cannot use this technique when inside a template argument
18917 list. */
18918 if (!parser->in_template_argument_list_p
18919 && !parser->in_type_id_in_expr_p
18920 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18921 /* However, a parameter-declaration of the form
18922 "float(f)" (which is a valid declaration of a
18923 parameter "f") can also be interpreted as an
18924 expression (the conversion of "f" to "float"). */
18925 && !parenthesized_p)
18926 cp_parser_commit_to_tentative_parse (parser);
18928 else
18930 cp_parser_error (parser, "expected %<,%> or %<...%>");
18931 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18932 cp_parser_skip_to_closing_parenthesis (parser,
18933 /*recovering=*/true,
18934 /*or_comma=*/false,
18935 /*consume_paren=*/false);
18936 break;
18940 parser->in_unbraced_linkage_specification_p
18941 = saved_in_unbraced_linkage_specification_p;
18943 /* Reset implicit_template_scope if we are about to leave the function
18944 parameter list that introduced it. Note that for out-of-line member
18945 definitions, there will be one or more class scopes before we get to
18946 the template parameter scope. */
18948 if (cp_binding_level *its = parser->implicit_template_scope)
18949 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18951 while (maybe_its->kind == sk_class)
18952 maybe_its = maybe_its->level_chain;
18953 if (maybe_its == its)
18955 parser->implicit_template_parms = 0;
18956 parser->implicit_template_scope = 0;
18960 return parameters;
18963 /* Parse a parameter declaration.
18965 parameter-declaration:
18966 decl-specifier-seq ... [opt] declarator
18967 decl-specifier-seq declarator = assignment-expression
18968 decl-specifier-seq ... [opt] abstract-declarator [opt]
18969 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18971 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18972 declares a template parameter. (In that case, a non-nested `>'
18973 token encountered during the parsing of the assignment-expression
18974 is not interpreted as a greater-than operator.)
18976 Returns a representation of the parameter, or NULL if an error
18977 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18978 true iff the declarator is of the form "(p)". */
18980 static cp_parameter_declarator *
18981 cp_parser_parameter_declaration (cp_parser *parser,
18982 bool template_parm_p,
18983 bool *parenthesized_p)
18985 int declares_class_or_enum;
18986 cp_decl_specifier_seq decl_specifiers;
18987 cp_declarator *declarator;
18988 tree default_argument;
18989 cp_token *token = NULL, *declarator_token_start = NULL;
18990 const char *saved_message;
18992 /* In a template parameter, `>' is not an operator.
18994 [temp.param]
18996 When parsing a default template-argument for a non-type
18997 template-parameter, the first non-nested `>' is taken as the end
18998 of the template parameter-list rather than a greater-than
18999 operator. */
19001 /* Type definitions may not appear in parameter types. */
19002 saved_message = parser->type_definition_forbidden_message;
19003 parser->type_definition_forbidden_message
19004 = G_("types may not be defined in parameter types");
19006 /* Parse the declaration-specifiers. */
19007 cp_parser_decl_specifier_seq (parser,
19008 CP_PARSER_FLAGS_NONE,
19009 &decl_specifiers,
19010 &declares_class_or_enum);
19012 /* Complain about missing 'typename' or other invalid type names. */
19013 if (!decl_specifiers.any_type_specifiers_p
19014 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19015 decl_specifiers.type = error_mark_node;
19017 /* If an error occurred, there's no reason to attempt to parse the
19018 rest of the declaration. */
19019 if (cp_parser_error_occurred (parser))
19021 parser->type_definition_forbidden_message = saved_message;
19022 return NULL;
19025 /* Peek at the next token. */
19026 token = cp_lexer_peek_token (parser->lexer);
19028 /* If the next token is a `)', `,', `=', `>', or `...', then there
19029 is no declarator. However, when variadic templates are enabled,
19030 there may be a declarator following `...'. */
19031 if (token->type == CPP_CLOSE_PAREN
19032 || token->type == CPP_COMMA
19033 || token->type == CPP_EQ
19034 || token->type == CPP_GREATER)
19036 declarator = NULL;
19037 if (parenthesized_p)
19038 *parenthesized_p = false;
19040 /* Otherwise, there should be a declarator. */
19041 else
19043 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19044 parser->default_arg_ok_p = false;
19046 /* After seeing a decl-specifier-seq, if the next token is not a
19047 "(", there is no possibility that the code is a valid
19048 expression. Therefore, if parsing tentatively, we commit at
19049 this point. */
19050 if (!parser->in_template_argument_list_p
19051 /* In an expression context, having seen:
19053 (int((char ...
19055 we cannot be sure whether we are looking at a
19056 function-type (taking a "char" as a parameter) or a cast
19057 of some object of type "char" to "int". */
19058 && !parser->in_type_id_in_expr_p
19059 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19060 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19061 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19062 cp_parser_commit_to_tentative_parse (parser);
19063 /* Parse the declarator. */
19064 declarator_token_start = token;
19065 declarator = cp_parser_declarator (parser,
19066 CP_PARSER_DECLARATOR_EITHER,
19067 /*ctor_dtor_or_conv_p=*/NULL,
19068 parenthesized_p,
19069 /*member_p=*/false);
19070 parser->default_arg_ok_p = saved_default_arg_ok_p;
19071 /* After the declarator, allow more attributes. */
19072 decl_specifiers.attributes
19073 = chainon (decl_specifiers.attributes,
19074 cp_parser_attributes_opt (parser));
19077 /* If the next token is an ellipsis, and we have not seen a
19078 declarator name, and the type of the declarator contains parameter
19079 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19080 a parameter pack expansion expression. Otherwise, leave the
19081 ellipsis for a C-style variadic function. */
19082 token = cp_lexer_peek_token (parser->lexer);
19083 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19085 tree type = decl_specifiers.type;
19087 if (type && DECL_P (type))
19088 type = TREE_TYPE (type);
19090 if (type
19091 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19092 && declarator_can_be_parameter_pack (declarator)
19093 && (!declarator || !declarator->parameter_pack_p)
19094 && uses_parameter_packs (type))
19096 /* Consume the `...'. */
19097 cp_lexer_consume_token (parser->lexer);
19098 maybe_warn_variadic_templates ();
19100 /* Build a pack expansion type */
19101 if (declarator)
19102 declarator->parameter_pack_p = true;
19103 else
19104 decl_specifiers.type = make_pack_expansion (type);
19108 /* The restriction on defining new types applies only to the type
19109 of the parameter, not to the default argument. */
19110 parser->type_definition_forbidden_message = saved_message;
19112 /* If the next token is `=', then process a default argument. */
19113 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19115 token = cp_lexer_peek_token (parser->lexer);
19116 /* If we are defining a class, then the tokens that make up the
19117 default argument must be saved and processed later. */
19118 if (!template_parm_p && at_class_scope_p ()
19119 && TYPE_BEING_DEFINED (current_class_type)
19120 && !LAMBDA_TYPE_P (current_class_type))
19121 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19122 /* Outside of a class definition, we can just parse the
19123 assignment-expression. */
19124 else
19125 default_argument
19126 = cp_parser_default_argument (parser, template_parm_p);
19128 if (!parser->default_arg_ok_p)
19130 if (flag_permissive)
19131 warning (0, "deprecated use of default argument for parameter of non-function");
19132 else
19134 error_at (token->location,
19135 "default arguments are only "
19136 "permitted for function parameters");
19137 default_argument = NULL_TREE;
19140 else if ((declarator && declarator->parameter_pack_p)
19141 || (decl_specifiers.type
19142 && PACK_EXPANSION_P (decl_specifiers.type)))
19144 /* Find the name of the parameter pack. */
19145 cp_declarator *id_declarator = declarator;
19146 while (id_declarator && id_declarator->kind != cdk_id)
19147 id_declarator = id_declarator->declarator;
19149 if (id_declarator && id_declarator->kind == cdk_id)
19150 error_at (declarator_token_start->location,
19151 template_parm_p
19152 ? G_("template parameter pack %qD "
19153 "cannot have a default argument")
19154 : G_("parameter pack %qD cannot have "
19155 "a default argument"),
19156 id_declarator->u.id.unqualified_name);
19157 else
19158 error_at (declarator_token_start->location,
19159 template_parm_p
19160 ? G_("template parameter pack cannot have "
19161 "a default argument")
19162 : G_("parameter pack cannot have a "
19163 "default argument"));
19165 default_argument = NULL_TREE;
19168 else
19169 default_argument = NULL_TREE;
19171 return make_parameter_declarator (&decl_specifiers,
19172 declarator,
19173 default_argument);
19176 /* Parse a default argument and return it.
19178 TEMPLATE_PARM_P is true if this is a default argument for a
19179 non-type template parameter. */
19180 static tree
19181 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19183 tree default_argument = NULL_TREE;
19184 bool saved_greater_than_is_operator_p;
19185 bool saved_local_variables_forbidden_p;
19186 bool non_constant_p, is_direct_init;
19188 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19189 set correctly. */
19190 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19191 parser->greater_than_is_operator_p = !template_parm_p;
19192 /* Local variable names (and the `this' keyword) may not
19193 appear in a default argument. */
19194 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19195 parser->local_variables_forbidden_p = true;
19196 /* Parse the assignment-expression. */
19197 if (template_parm_p)
19198 push_deferring_access_checks (dk_no_deferred);
19199 tree saved_class_ptr = NULL_TREE;
19200 tree saved_class_ref = NULL_TREE;
19201 /* The "this" pointer is not valid in a default argument. */
19202 if (cfun)
19204 saved_class_ptr = current_class_ptr;
19205 cp_function_chain->x_current_class_ptr = NULL_TREE;
19206 saved_class_ref = current_class_ref;
19207 cp_function_chain->x_current_class_ref = NULL_TREE;
19209 default_argument
19210 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19211 /* Restore the "this" pointer. */
19212 if (cfun)
19214 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19215 cp_function_chain->x_current_class_ref = saved_class_ref;
19217 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19218 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19219 if (template_parm_p)
19220 pop_deferring_access_checks ();
19221 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19222 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19224 return default_argument;
19227 /* Parse a function-body.
19229 function-body:
19230 compound_statement */
19232 static void
19233 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19235 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19238 /* Parse a ctor-initializer-opt followed by a function-body. Return
19239 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19240 is true we are parsing a function-try-block. */
19242 static bool
19243 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19244 bool in_function_try_block)
19246 tree body, list;
19247 bool ctor_initializer_p;
19248 const bool check_body_p =
19249 DECL_CONSTRUCTOR_P (current_function_decl)
19250 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19251 tree last = NULL;
19253 /* Begin the function body. */
19254 body = begin_function_body ();
19255 /* Parse the optional ctor-initializer. */
19256 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19258 /* If we're parsing a constexpr constructor definition, we need
19259 to check that the constructor body is indeed empty. However,
19260 before we get to cp_parser_function_body lot of junk has been
19261 generated, so we can't just check that we have an empty block.
19262 Rather we take a snapshot of the outermost block, and check whether
19263 cp_parser_function_body changed its state. */
19264 if (check_body_p)
19266 list = cur_stmt_list;
19267 if (STATEMENT_LIST_TAIL (list))
19268 last = STATEMENT_LIST_TAIL (list)->stmt;
19270 /* Parse the function-body. */
19271 cp_parser_function_body (parser, in_function_try_block);
19272 if (check_body_p)
19273 check_constexpr_ctor_body (last, list);
19274 /* Finish the function body. */
19275 finish_function_body (body);
19277 return ctor_initializer_p;
19280 /* Parse an initializer.
19282 initializer:
19283 = initializer-clause
19284 ( expression-list )
19286 Returns an expression representing the initializer. If no
19287 initializer is present, NULL_TREE is returned.
19289 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19290 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19291 set to TRUE if there is no initializer present. If there is an
19292 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19293 is set to true; otherwise it is set to false. */
19295 static tree
19296 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19297 bool* non_constant_p)
19299 cp_token *token;
19300 tree init;
19302 /* Peek at the next token. */
19303 token = cp_lexer_peek_token (parser->lexer);
19305 /* Let our caller know whether or not this initializer was
19306 parenthesized. */
19307 *is_direct_init = (token->type != CPP_EQ);
19308 /* Assume that the initializer is constant. */
19309 *non_constant_p = false;
19311 if (token->type == CPP_EQ)
19313 /* Consume the `='. */
19314 cp_lexer_consume_token (parser->lexer);
19315 /* Parse the initializer-clause. */
19316 init = cp_parser_initializer_clause (parser, non_constant_p);
19318 else if (token->type == CPP_OPEN_PAREN)
19320 vec<tree, va_gc> *vec;
19321 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19322 /*cast_p=*/false,
19323 /*allow_expansion_p=*/true,
19324 non_constant_p);
19325 if (vec == NULL)
19326 return error_mark_node;
19327 init = build_tree_list_vec (vec);
19328 release_tree_vector (vec);
19330 else if (token->type == CPP_OPEN_BRACE)
19332 cp_lexer_set_source_position (parser->lexer);
19333 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19334 init = cp_parser_braced_list (parser, non_constant_p);
19335 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19337 else
19339 /* Anything else is an error. */
19340 cp_parser_error (parser, "expected initializer");
19341 init = error_mark_node;
19344 return init;
19347 /* Parse an initializer-clause.
19349 initializer-clause:
19350 assignment-expression
19351 braced-init-list
19353 Returns an expression representing the initializer.
19355 If the `assignment-expression' production is used the value
19356 returned is simply a representation for the expression.
19358 Otherwise, calls cp_parser_braced_list. */
19360 static tree
19361 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19363 tree initializer;
19365 /* Assume the expression is constant. */
19366 *non_constant_p = false;
19368 /* If it is not a `{', then we are looking at an
19369 assignment-expression. */
19370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19372 initializer
19373 = cp_parser_constant_expression (parser,
19374 /*allow_non_constant_p=*/true,
19375 non_constant_p);
19377 else
19378 initializer = cp_parser_braced_list (parser, non_constant_p);
19380 return initializer;
19383 /* Parse a brace-enclosed initializer list.
19385 braced-init-list:
19386 { initializer-list , [opt] }
19389 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19390 the elements of the initializer-list (or NULL, if the last
19391 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19392 NULL_TREE. There is no way to detect whether or not the optional
19393 trailing `,' was provided. NON_CONSTANT_P is as for
19394 cp_parser_initializer. */
19396 static tree
19397 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19399 tree initializer;
19401 /* Consume the `{' token. */
19402 cp_lexer_consume_token (parser->lexer);
19403 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19404 initializer = make_node (CONSTRUCTOR);
19405 /* If it's not a `}', then there is a non-trivial initializer. */
19406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19408 /* Parse the initializer list. */
19409 CONSTRUCTOR_ELTS (initializer)
19410 = cp_parser_initializer_list (parser, non_constant_p);
19411 /* A trailing `,' token is allowed. */
19412 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19413 cp_lexer_consume_token (parser->lexer);
19415 else
19416 *non_constant_p = false;
19417 /* Now, there should be a trailing `}'. */
19418 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19419 TREE_TYPE (initializer) = init_list_type_node;
19420 return initializer;
19423 /* Parse an initializer-list.
19425 initializer-list:
19426 initializer-clause ... [opt]
19427 initializer-list , initializer-clause ... [opt]
19429 GNU Extension:
19431 initializer-list:
19432 designation initializer-clause ...[opt]
19433 initializer-list , designation initializer-clause ...[opt]
19435 designation:
19436 . identifier =
19437 identifier :
19438 [ constant-expression ] =
19440 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19441 for the initializer. If the INDEX of the elt is non-NULL, it is the
19442 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19443 as for cp_parser_initializer. */
19445 static vec<constructor_elt, va_gc> *
19446 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19448 vec<constructor_elt, va_gc> *v = NULL;
19450 /* Assume all of the expressions are constant. */
19451 *non_constant_p = false;
19453 /* Parse the rest of the list. */
19454 while (true)
19456 cp_token *token;
19457 tree designator;
19458 tree initializer;
19459 bool clause_non_constant_p;
19461 /* If the next token is an identifier and the following one is a
19462 colon, we are looking at the GNU designated-initializer
19463 syntax. */
19464 if (cp_parser_allow_gnu_extensions_p (parser)
19465 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19466 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19468 /* Warn the user that they are using an extension. */
19469 pedwarn (input_location, OPT_Wpedantic,
19470 "ISO C++ does not allow designated initializers");
19471 /* Consume the identifier. */
19472 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19473 /* Consume the `:'. */
19474 cp_lexer_consume_token (parser->lexer);
19476 /* Also handle the C99 syntax, '. id ='. */
19477 else if (cp_parser_allow_gnu_extensions_p (parser)
19478 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19480 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19482 /* Warn the user that they are using an extension. */
19483 pedwarn (input_location, OPT_Wpedantic,
19484 "ISO C++ does not allow C99 designated initializers");
19485 /* Consume the `.'. */
19486 cp_lexer_consume_token (parser->lexer);
19487 /* Consume the identifier. */
19488 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19489 /* Consume the `='. */
19490 cp_lexer_consume_token (parser->lexer);
19492 /* Also handle C99 array designators, '[ const ] ='. */
19493 else if (cp_parser_allow_gnu_extensions_p (parser)
19494 && !c_dialect_objc ()
19495 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19497 /* In C++11, [ could start a lambda-introducer. */
19498 bool non_const = false;
19500 cp_parser_parse_tentatively (parser);
19501 cp_lexer_consume_token (parser->lexer);
19502 designator = cp_parser_constant_expression (parser, true, &non_const);
19503 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19504 cp_parser_require (parser, CPP_EQ, RT_EQ);
19505 if (!cp_parser_parse_definitely (parser))
19506 designator = NULL_TREE;
19507 else if (non_const)
19508 require_potential_rvalue_constant_expression (designator);
19510 else
19511 designator = NULL_TREE;
19513 /* Parse the initializer. */
19514 initializer = cp_parser_initializer_clause (parser,
19515 &clause_non_constant_p);
19516 /* If any clause is non-constant, so is the entire initializer. */
19517 if (clause_non_constant_p)
19518 *non_constant_p = true;
19520 /* If we have an ellipsis, this is an initializer pack
19521 expansion. */
19522 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19524 /* Consume the `...'. */
19525 cp_lexer_consume_token (parser->lexer);
19527 /* Turn the initializer into an initializer expansion. */
19528 initializer = make_pack_expansion (initializer);
19531 /* Add it to the vector. */
19532 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19534 /* If the next token is not a comma, we have reached the end of
19535 the list. */
19536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19537 break;
19539 /* Peek at the next token. */
19540 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19541 /* If the next token is a `}', then we're still done. An
19542 initializer-clause can have a trailing `,' after the
19543 initializer-list and before the closing `}'. */
19544 if (token->type == CPP_CLOSE_BRACE)
19545 break;
19547 /* Consume the `,' token. */
19548 cp_lexer_consume_token (parser->lexer);
19551 return v;
19554 /* Classes [gram.class] */
19556 /* Parse a class-name.
19558 class-name:
19559 identifier
19560 template-id
19562 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19563 to indicate that names looked up in dependent types should be
19564 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19565 keyword has been used to indicate that the name that appears next
19566 is a template. TAG_TYPE indicates the explicit tag given before
19567 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19568 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19569 is the class being defined in a class-head.
19571 Returns the TYPE_DECL representing the class. */
19573 static tree
19574 cp_parser_class_name (cp_parser *parser,
19575 bool typename_keyword_p,
19576 bool template_keyword_p,
19577 enum tag_types tag_type,
19578 bool check_dependency_p,
19579 bool class_head_p,
19580 bool is_declaration)
19582 tree decl;
19583 tree scope;
19584 bool typename_p;
19585 cp_token *token;
19586 tree identifier = NULL_TREE;
19588 /* All class-names start with an identifier. */
19589 token = cp_lexer_peek_token (parser->lexer);
19590 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19592 cp_parser_error (parser, "expected class-name");
19593 return error_mark_node;
19596 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19597 to a template-id, so we save it here. */
19598 scope = parser->scope;
19599 if (scope == error_mark_node)
19600 return error_mark_node;
19602 /* Any name names a type if we're following the `typename' keyword
19603 in a qualified name where the enclosing scope is type-dependent. */
19604 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19605 && dependent_type_p (scope));
19606 /* Handle the common case (an identifier, but not a template-id)
19607 efficiently. */
19608 if (token->type == CPP_NAME
19609 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19611 cp_token *identifier_token;
19612 bool ambiguous_p;
19614 /* Look for the identifier. */
19615 identifier_token = cp_lexer_peek_token (parser->lexer);
19616 ambiguous_p = identifier_token->error_reported;
19617 identifier = cp_parser_identifier (parser);
19618 /* If the next token isn't an identifier, we are certainly not
19619 looking at a class-name. */
19620 if (identifier == error_mark_node)
19621 decl = error_mark_node;
19622 /* If we know this is a type-name, there's no need to look it
19623 up. */
19624 else if (typename_p)
19625 decl = identifier;
19626 else
19628 tree ambiguous_decls;
19629 /* If we already know that this lookup is ambiguous, then
19630 we've already issued an error message; there's no reason
19631 to check again. */
19632 if (ambiguous_p)
19634 cp_parser_simulate_error (parser);
19635 return error_mark_node;
19637 /* If the next token is a `::', then the name must be a type
19638 name.
19640 [basic.lookup.qual]
19642 During the lookup for a name preceding the :: scope
19643 resolution operator, object, function, and enumerator
19644 names are ignored. */
19645 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19646 tag_type = typename_type;
19647 /* Look up the name. */
19648 decl = cp_parser_lookup_name (parser, identifier,
19649 tag_type,
19650 /*is_template=*/false,
19651 /*is_namespace=*/false,
19652 check_dependency_p,
19653 &ambiguous_decls,
19654 identifier_token->location);
19655 if (ambiguous_decls)
19657 if (cp_parser_parsing_tentatively (parser))
19658 cp_parser_simulate_error (parser);
19659 return error_mark_node;
19663 else
19665 /* Try a template-id. */
19666 decl = cp_parser_template_id (parser, template_keyword_p,
19667 check_dependency_p,
19668 tag_type,
19669 is_declaration);
19670 if (decl == error_mark_node)
19671 return error_mark_node;
19674 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19676 /* If this is a typename, create a TYPENAME_TYPE. */
19677 if (typename_p && decl != error_mark_node)
19679 decl = make_typename_type (scope, decl, typename_type,
19680 /*complain=*/tf_error);
19681 if (decl != error_mark_node)
19682 decl = TYPE_NAME (decl);
19685 decl = strip_using_decl (decl);
19687 /* Check to see that it is really the name of a class. */
19688 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19689 && identifier_p (TREE_OPERAND (decl, 0))
19690 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19691 /* Situations like this:
19693 template <typename T> struct A {
19694 typename T::template X<int>::I i;
19697 are problematic. Is `T::template X<int>' a class-name? The
19698 standard does not seem to be definitive, but there is no other
19699 valid interpretation of the following `::'. Therefore, those
19700 names are considered class-names. */
19702 decl = make_typename_type (scope, decl, tag_type, tf_error);
19703 if (decl != error_mark_node)
19704 decl = TYPE_NAME (decl);
19706 else if (TREE_CODE (decl) != TYPE_DECL
19707 || TREE_TYPE (decl) == error_mark_node
19708 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19709 /* In Objective-C 2.0, a classname followed by '.' starts a
19710 dot-syntax expression, and it's not a type-name. */
19711 || (c_dialect_objc ()
19712 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19713 && objc_is_class_name (decl)))
19714 decl = error_mark_node;
19716 if (decl == error_mark_node)
19717 cp_parser_error (parser, "expected class-name");
19718 else if (identifier && !parser->scope)
19719 maybe_note_name_used_in_class (identifier, decl);
19721 return decl;
19724 /* Parse a class-specifier.
19726 class-specifier:
19727 class-head { member-specification [opt] }
19729 Returns the TREE_TYPE representing the class. */
19731 static tree
19732 cp_parser_class_specifier_1 (cp_parser* parser)
19734 tree type;
19735 tree attributes = NULL_TREE;
19736 bool nested_name_specifier_p;
19737 unsigned saved_num_template_parameter_lists;
19738 bool saved_in_function_body;
19739 unsigned char in_statement;
19740 bool in_switch_statement_p;
19741 bool saved_in_unbraced_linkage_specification_p;
19742 tree old_scope = NULL_TREE;
19743 tree scope = NULL_TREE;
19744 cp_token *closing_brace;
19746 push_deferring_access_checks (dk_no_deferred);
19748 /* Parse the class-head. */
19749 type = cp_parser_class_head (parser,
19750 &nested_name_specifier_p);
19751 /* If the class-head was a semantic disaster, skip the entire body
19752 of the class. */
19753 if (!type)
19755 cp_parser_skip_to_end_of_block_or_statement (parser);
19756 pop_deferring_access_checks ();
19757 return error_mark_node;
19760 /* Look for the `{'. */
19761 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19763 pop_deferring_access_checks ();
19764 return error_mark_node;
19767 cp_ensure_no_omp_declare_simd (parser);
19769 /* Issue an error message if type-definitions are forbidden here. */
19770 cp_parser_check_type_definition (parser);
19771 /* Remember that we are defining one more class. */
19772 ++parser->num_classes_being_defined;
19773 /* Inside the class, surrounding template-parameter-lists do not
19774 apply. */
19775 saved_num_template_parameter_lists
19776 = parser->num_template_parameter_lists;
19777 parser->num_template_parameter_lists = 0;
19778 /* We are not in a function body. */
19779 saved_in_function_body = parser->in_function_body;
19780 parser->in_function_body = false;
19781 /* Or in a loop. */
19782 in_statement = parser->in_statement;
19783 parser->in_statement = 0;
19784 /* Or in a switch. */
19785 in_switch_statement_p = parser->in_switch_statement_p;
19786 parser->in_switch_statement_p = false;
19787 /* We are not immediately inside an extern "lang" block. */
19788 saved_in_unbraced_linkage_specification_p
19789 = parser->in_unbraced_linkage_specification_p;
19790 parser->in_unbraced_linkage_specification_p = false;
19792 /* Start the class. */
19793 if (nested_name_specifier_p)
19795 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19796 old_scope = push_inner_scope (scope);
19798 type = begin_class_definition (type);
19800 if (type == error_mark_node)
19801 /* If the type is erroneous, skip the entire body of the class. */
19802 cp_parser_skip_to_closing_brace (parser);
19803 else
19804 /* Parse the member-specification. */
19805 cp_parser_member_specification_opt (parser);
19807 /* Look for the trailing `}'. */
19808 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19809 /* Look for trailing attributes to apply to this class. */
19810 if (cp_parser_allow_gnu_extensions_p (parser))
19811 attributes = cp_parser_gnu_attributes_opt (parser);
19812 if (type != error_mark_node)
19813 type = finish_struct (type, attributes);
19814 if (nested_name_specifier_p)
19815 pop_inner_scope (old_scope, scope);
19817 /* We've finished a type definition. Check for the common syntax
19818 error of forgetting a semicolon after the definition. We need to
19819 be careful, as we can't just check for not-a-semicolon and be done
19820 with it; the user might have typed:
19822 class X { } c = ...;
19823 class X { } *p = ...;
19825 and so forth. Instead, enumerate all the possible tokens that
19826 might follow this production; if we don't see one of them, then
19827 complain and silently insert the semicolon. */
19829 cp_token *token = cp_lexer_peek_token (parser->lexer);
19830 bool want_semicolon = true;
19832 if (cp_next_tokens_can_be_std_attribute_p (parser))
19833 /* Don't try to parse c++11 attributes here. As per the
19834 grammar, that should be a task for
19835 cp_parser_decl_specifier_seq. */
19836 want_semicolon = false;
19838 switch (token->type)
19840 case CPP_NAME:
19841 case CPP_SEMICOLON:
19842 case CPP_MULT:
19843 case CPP_AND:
19844 case CPP_OPEN_PAREN:
19845 case CPP_CLOSE_PAREN:
19846 case CPP_COMMA:
19847 want_semicolon = false;
19848 break;
19850 /* While it's legal for type qualifiers and storage class
19851 specifiers to follow type definitions in the grammar, only
19852 compiler testsuites contain code like that. Assume that if
19853 we see such code, then what we're really seeing is a case
19854 like:
19856 class X { }
19857 const <type> var = ...;
19861 class Y { }
19862 static <type> func (...) ...
19864 i.e. the qualifier or specifier applies to the next
19865 declaration. To do so, however, we need to look ahead one
19866 more token to see if *that* token is a type specifier.
19868 This code could be improved to handle:
19870 class Z { }
19871 static const <type> var = ...; */
19872 case CPP_KEYWORD:
19873 if (keyword_is_decl_specifier (token->keyword))
19875 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19877 /* Handling user-defined types here would be nice, but very
19878 tricky. */
19879 want_semicolon
19880 = (lookahead->type == CPP_KEYWORD
19881 && keyword_begins_type_specifier (lookahead->keyword));
19883 break;
19884 default:
19885 break;
19888 /* If we don't have a type, then something is very wrong and we
19889 shouldn't try to do anything clever. Likewise for not seeing the
19890 closing brace. */
19891 if (closing_brace && TYPE_P (type) && want_semicolon)
19893 cp_token_position prev
19894 = cp_lexer_previous_token_position (parser->lexer);
19895 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19896 location_t loc = prev_token->location;
19898 if (CLASSTYPE_DECLARED_CLASS (type))
19899 error_at (loc, "expected %<;%> after class definition");
19900 else if (TREE_CODE (type) == RECORD_TYPE)
19901 error_at (loc, "expected %<;%> after struct definition");
19902 else if (TREE_CODE (type) == UNION_TYPE)
19903 error_at (loc, "expected %<;%> after union definition");
19904 else
19905 gcc_unreachable ();
19907 /* Unget one token and smash it to look as though we encountered
19908 a semicolon in the input stream. */
19909 cp_lexer_set_token_position (parser->lexer, prev);
19910 token = cp_lexer_peek_token (parser->lexer);
19911 token->type = CPP_SEMICOLON;
19912 token->keyword = RID_MAX;
19916 /* If this class is not itself within the scope of another class,
19917 then we need to parse the bodies of all of the queued function
19918 definitions. Note that the queued functions defined in a class
19919 are not always processed immediately following the
19920 class-specifier for that class. Consider:
19922 struct A {
19923 struct B { void f() { sizeof (A); } };
19926 If `f' were processed before the processing of `A' were
19927 completed, there would be no way to compute the size of `A'.
19928 Note that the nesting we are interested in here is lexical --
19929 not the semantic nesting given by TYPE_CONTEXT. In particular,
19930 for:
19932 struct A { struct B; };
19933 struct A::B { void f() { } };
19935 there is no need to delay the parsing of `A::B::f'. */
19936 if (--parser->num_classes_being_defined == 0)
19938 tree decl;
19939 tree class_type = NULL_TREE;
19940 tree pushed_scope = NULL_TREE;
19941 unsigned ix;
19942 cp_default_arg_entry *e;
19943 tree save_ccp, save_ccr;
19945 /* In a first pass, parse default arguments to the functions.
19946 Then, in a second pass, parse the bodies of the functions.
19947 This two-phased approach handles cases like:
19949 struct S {
19950 void f() { g(); }
19951 void g(int i = 3);
19955 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19957 decl = e->decl;
19958 /* If there are default arguments that have not yet been processed,
19959 take care of them now. */
19960 if (class_type != e->class_type)
19962 if (pushed_scope)
19963 pop_scope (pushed_scope);
19964 class_type = e->class_type;
19965 pushed_scope = push_scope (class_type);
19967 /* Make sure that any template parameters are in scope. */
19968 maybe_begin_member_template_processing (decl);
19969 /* Parse the default argument expressions. */
19970 cp_parser_late_parsing_default_args (parser, decl);
19971 /* Remove any template parameters from the symbol table. */
19972 maybe_end_member_template_processing ();
19974 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19975 /* Now parse any NSDMIs. */
19976 save_ccp = current_class_ptr;
19977 save_ccr = current_class_ref;
19978 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19980 if (class_type != DECL_CONTEXT (decl))
19982 if (pushed_scope)
19983 pop_scope (pushed_scope);
19984 class_type = DECL_CONTEXT (decl);
19985 pushed_scope = push_scope (class_type);
19987 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19988 cp_parser_late_parsing_nsdmi (parser, decl);
19990 vec_safe_truncate (unparsed_nsdmis, 0);
19991 current_class_ptr = save_ccp;
19992 current_class_ref = save_ccr;
19993 if (pushed_scope)
19994 pop_scope (pushed_scope);
19996 /* Now do some post-NSDMI bookkeeping. */
19997 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19998 after_nsdmi_defaulted_late_checks (class_type);
19999 vec_safe_truncate (unparsed_classes, 0);
20000 after_nsdmi_defaulted_late_checks (type);
20002 /* Now parse the body of the functions. */
20003 if (flag_openmp)
20005 /* OpenMP UDRs need to be parsed before all other functions. */
20006 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20007 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20008 cp_parser_late_parsing_for_member (parser, decl);
20009 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20010 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20011 cp_parser_late_parsing_for_member (parser, decl);
20013 else
20014 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20015 cp_parser_late_parsing_for_member (parser, decl);
20016 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20018 else
20019 vec_safe_push (unparsed_classes, type);
20021 /* Put back any saved access checks. */
20022 pop_deferring_access_checks ();
20024 /* Restore saved state. */
20025 parser->in_switch_statement_p = in_switch_statement_p;
20026 parser->in_statement = in_statement;
20027 parser->in_function_body = saved_in_function_body;
20028 parser->num_template_parameter_lists
20029 = saved_num_template_parameter_lists;
20030 parser->in_unbraced_linkage_specification_p
20031 = saved_in_unbraced_linkage_specification_p;
20033 return type;
20036 static tree
20037 cp_parser_class_specifier (cp_parser* parser)
20039 tree ret;
20040 timevar_push (TV_PARSE_STRUCT);
20041 ret = cp_parser_class_specifier_1 (parser);
20042 timevar_pop (TV_PARSE_STRUCT);
20043 return ret;
20046 /* Parse a class-head.
20048 class-head:
20049 class-key identifier [opt] base-clause [opt]
20050 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20051 class-key nested-name-specifier [opt] template-id
20052 base-clause [opt]
20054 class-virt-specifier:
20055 final
20057 GNU Extensions:
20058 class-key attributes identifier [opt] base-clause [opt]
20059 class-key attributes nested-name-specifier identifier base-clause [opt]
20060 class-key attributes nested-name-specifier [opt] template-id
20061 base-clause [opt]
20063 Upon return BASES is initialized to the list of base classes (or
20064 NULL, if there are none) in the same form returned by
20065 cp_parser_base_clause.
20067 Returns the TYPE of the indicated class. Sets
20068 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20069 involving a nested-name-specifier was used, and FALSE otherwise.
20071 Returns error_mark_node if this is not a class-head.
20073 Returns NULL_TREE if the class-head is syntactically valid, but
20074 semantically invalid in a way that means we should skip the entire
20075 body of the class. */
20077 static tree
20078 cp_parser_class_head (cp_parser* parser,
20079 bool* nested_name_specifier_p)
20081 tree nested_name_specifier;
20082 enum tag_types class_key;
20083 tree id = NULL_TREE;
20084 tree type = NULL_TREE;
20085 tree attributes;
20086 tree bases;
20087 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20088 bool template_id_p = false;
20089 bool qualified_p = false;
20090 bool invalid_nested_name_p = false;
20091 bool invalid_explicit_specialization_p = false;
20092 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20093 tree pushed_scope = NULL_TREE;
20094 unsigned num_templates;
20095 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20096 /* Assume no nested-name-specifier will be present. */
20097 *nested_name_specifier_p = false;
20098 /* Assume no template parameter lists will be used in defining the
20099 type. */
20100 num_templates = 0;
20101 parser->colon_corrects_to_scope_p = false;
20103 /* Look for the class-key. */
20104 class_key = cp_parser_class_key (parser);
20105 if (class_key == none_type)
20106 return error_mark_node;
20108 /* Parse the attributes. */
20109 attributes = cp_parser_attributes_opt (parser);
20111 /* If the next token is `::', that is invalid -- but sometimes
20112 people do try to write:
20114 struct ::S {};
20116 Handle this gracefully by accepting the extra qualifier, and then
20117 issuing an error about it later if this really is a
20118 class-head. If it turns out just to be an elaborated type
20119 specifier, remain silent. */
20120 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20121 qualified_p = true;
20123 push_deferring_access_checks (dk_no_check);
20125 /* Determine the name of the class. Begin by looking for an
20126 optional nested-name-specifier. */
20127 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20128 nested_name_specifier
20129 = cp_parser_nested_name_specifier_opt (parser,
20130 /*typename_keyword_p=*/false,
20131 /*check_dependency_p=*/false,
20132 /*type_p=*/true,
20133 /*is_declaration=*/false);
20134 /* If there was a nested-name-specifier, then there *must* be an
20135 identifier. */
20136 if (nested_name_specifier)
20138 type_start_token = cp_lexer_peek_token (parser->lexer);
20139 /* Although the grammar says `identifier', it really means
20140 `class-name' or `template-name'. You are only allowed to
20141 define a class that has already been declared with this
20142 syntax.
20144 The proposed resolution for Core Issue 180 says that wherever
20145 you see `class T::X' you should treat `X' as a type-name.
20147 It is OK to define an inaccessible class; for example:
20149 class A { class B; };
20150 class A::B {};
20152 We do not know if we will see a class-name, or a
20153 template-name. We look for a class-name first, in case the
20154 class-name is a template-id; if we looked for the
20155 template-name first we would stop after the template-name. */
20156 cp_parser_parse_tentatively (parser);
20157 type = cp_parser_class_name (parser,
20158 /*typename_keyword_p=*/false,
20159 /*template_keyword_p=*/false,
20160 class_type,
20161 /*check_dependency_p=*/false,
20162 /*class_head_p=*/true,
20163 /*is_declaration=*/false);
20164 /* If that didn't work, ignore the nested-name-specifier. */
20165 if (!cp_parser_parse_definitely (parser))
20167 invalid_nested_name_p = true;
20168 type_start_token = cp_lexer_peek_token (parser->lexer);
20169 id = cp_parser_identifier (parser);
20170 if (id == error_mark_node)
20171 id = NULL_TREE;
20173 /* If we could not find a corresponding TYPE, treat this
20174 declaration like an unqualified declaration. */
20175 if (type == error_mark_node)
20176 nested_name_specifier = NULL_TREE;
20177 /* Otherwise, count the number of templates used in TYPE and its
20178 containing scopes. */
20179 else
20181 tree scope;
20183 for (scope = TREE_TYPE (type);
20184 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20185 scope = get_containing_scope (scope))
20186 if (TYPE_P (scope)
20187 && CLASS_TYPE_P (scope)
20188 && CLASSTYPE_TEMPLATE_INFO (scope)
20189 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20190 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20191 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20192 ++num_templates;
20195 /* Otherwise, the identifier is optional. */
20196 else
20198 /* We don't know whether what comes next is a template-id,
20199 an identifier, or nothing at all. */
20200 cp_parser_parse_tentatively (parser);
20201 /* Check for a template-id. */
20202 type_start_token = cp_lexer_peek_token (parser->lexer);
20203 id = cp_parser_template_id (parser,
20204 /*template_keyword_p=*/false,
20205 /*check_dependency_p=*/true,
20206 class_key,
20207 /*is_declaration=*/true);
20208 /* If that didn't work, it could still be an identifier. */
20209 if (!cp_parser_parse_definitely (parser))
20211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20213 type_start_token = cp_lexer_peek_token (parser->lexer);
20214 id = cp_parser_identifier (parser);
20216 else
20217 id = NULL_TREE;
20219 else
20221 template_id_p = true;
20222 ++num_templates;
20226 pop_deferring_access_checks ();
20228 if (id)
20230 cp_parser_check_for_invalid_template_id (parser, id,
20231 class_key,
20232 type_start_token->location);
20234 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20236 /* If it's not a `:' or a `{' then we can't really be looking at a
20237 class-head, since a class-head only appears as part of a
20238 class-specifier. We have to detect this situation before calling
20239 xref_tag, since that has irreversible side-effects. */
20240 if (!cp_parser_next_token_starts_class_definition_p (parser))
20242 cp_parser_error (parser, "expected %<{%> or %<:%>");
20243 type = error_mark_node;
20244 goto out;
20247 /* At this point, we're going ahead with the class-specifier, even
20248 if some other problem occurs. */
20249 cp_parser_commit_to_tentative_parse (parser);
20250 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20252 cp_parser_error (parser,
20253 "cannot specify %<override%> for a class");
20254 type = error_mark_node;
20255 goto out;
20257 /* Issue the error about the overly-qualified name now. */
20258 if (qualified_p)
20260 cp_parser_error (parser,
20261 "global qualification of class name is invalid");
20262 type = error_mark_node;
20263 goto out;
20265 else if (invalid_nested_name_p)
20267 cp_parser_error (parser,
20268 "qualified name does not name a class");
20269 type = error_mark_node;
20270 goto out;
20272 else if (nested_name_specifier)
20274 tree scope;
20276 /* Reject typedef-names in class heads. */
20277 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20279 error_at (type_start_token->location,
20280 "invalid class name in declaration of %qD",
20281 type);
20282 type = NULL_TREE;
20283 goto done;
20286 /* Figure out in what scope the declaration is being placed. */
20287 scope = current_scope ();
20288 /* If that scope does not contain the scope in which the
20289 class was originally declared, the program is invalid. */
20290 if (scope && !is_ancestor (scope, nested_name_specifier))
20292 if (at_namespace_scope_p ())
20293 error_at (type_start_token->location,
20294 "declaration of %qD in namespace %qD which does not "
20295 "enclose %qD",
20296 type, scope, nested_name_specifier);
20297 else
20298 error_at (type_start_token->location,
20299 "declaration of %qD in %qD which does not enclose %qD",
20300 type, scope, nested_name_specifier);
20301 type = NULL_TREE;
20302 goto done;
20304 /* [dcl.meaning]
20306 A declarator-id shall not be qualified except for the
20307 definition of a ... nested class outside of its class
20308 ... [or] the definition or explicit instantiation of a
20309 class member of a namespace outside of its namespace. */
20310 if (scope == nested_name_specifier)
20312 permerror (nested_name_specifier_token_start->location,
20313 "extra qualification not allowed");
20314 nested_name_specifier = NULL_TREE;
20315 num_templates = 0;
20318 /* An explicit-specialization must be preceded by "template <>". If
20319 it is not, try to recover gracefully. */
20320 if (at_namespace_scope_p ()
20321 && parser->num_template_parameter_lists == 0
20322 && template_id_p)
20324 error_at (type_start_token->location,
20325 "an explicit specialization must be preceded by %<template <>%>");
20326 invalid_explicit_specialization_p = true;
20327 /* Take the same action that would have been taken by
20328 cp_parser_explicit_specialization. */
20329 ++parser->num_template_parameter_lists;
20330 begin_specialization ();
20332 /* There must be no "return" statements between this point and the
20333 end of this function; set "type "to the correct return value and
20334 use "goto done;" to return. */
20335 /* Make sure that the right number of template parameters were
20336 present. */
20337 if (!cp_parser_check_template_parameters (parser, num_templates,
20338 type_start_token->location,
20339 /*declarator=*/NULL))
20341 /* If something went wrong, there is no point in even trying to
20342 process the class-definition. */
20343 type = NULL_TREE;
20344 goto done;
20347 /* Look up the type. */
20348 if (template_id_p)
20350 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20351 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20352 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20354 error_at (type_start_token->location,
20355 "function template %qD redeclared as a class template", id);
20356 type = error_mark_node;
20358 else
20360 type = TREE_TYPE (id);
20361 type = maybe_process_partial_specialization (type);
20363 if (nested_name_specifier)
20364 pushed_scope = push_scope (nested_name_specifier);
20366 else if (nested_name_specifier)
20368 tree class_type;
20370 /* Given:
20372 template <typename T> struct S { struct T };
20373 template <typename T> struct S<T>::T { };
20375 we will get a TYPENAME_TYPE when processing the definition of
20376 `S::T'. We need to resolve it to the actual type before we
20377 try to define it. */
20378 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20380 class_type = resolve_typename_type (TREE_TYPE (type),
20381 /*only_current_p=*/false);
20382 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20383 type = TYPE_NAME (class_type);
20384 else
20386 cp_parser_error (parser, "could not resolve typename type");
20387 type = error_mark_node;
20391 if (maybe_process_partial_specialization (TREE_TYPE (type))
20392 == error_mark_node)
20394 type = NULL_TREE;
20395 goto done;
20398 class_type = current_class_type;
20399 /* Enter the scope indicated by the nested-name-specifier. */
20400 pushed_scope = push_scope (nested_name_specifier);
20401 /* Get the canonical version of this type. */
20402 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20403 /* Call push_template_decl if it seems like we should be defining a
20404 template either from the template headers or the type we're
20405 defining, so that we diagnose both extra and missing headers. */
20406 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20407 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20408 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20409 (TREE_TYPE (type)))))
20410 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20412 type = push_template_decl (type);
20413 if (type == error_mark_node)
20415 type = NULL_TREE;
20416 goto done;
20420 type = TREE_TYPE (type);
20421 *nested_name_specifier_p = true;
20423 else /* The name is not a nested name. */
20425 /* If the class was unnamed, create a dummy name. */
20426 if (!id)
20427 id = make_anon_name ();
20428 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20429 parser->num_template_parameter_lists);
20432 /* Indicate whether this class was declared as a `class' or as a
20433 `struct'. */
20434 if (TREE_CODE (type) == RECORD_TYPE)
20435 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20436 cp_parser_check_class_key (class_key, type);
20438 /* If this type was already complete, and we see another definition,
20439 that's an error. */
20440 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20442 error_at (type_start_token->location, "redefinition of %q#T",
20443 type);
20444 error_at (type_start_token->location, "previous definition of %q+#T",
20445 type);
20446 type = NULL_TREE;
20447 goto done;
20449 else if (type == error_mark_node)
20450 type = NULL_TREE;
20452 if (type)
20454 /* Apply attributes now, before any use of the class as a template
20455 argument in its base list. */
20456 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20457 fixup_attribute_variants (type);
20460 /* We will have entered the scope containing the class; the names of
20461 base classes should be looked up in that context. For example:
20463 struct A { struct B {}; struct C; };
20464 struct A::C : B {};
20466 is valid. */
20468 /* Get the list of base-classes, if there is one. */
20469 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20471 /* PR59482: enter the class scope so that base-specifiers are looked
20472 up correctly. */
20473 if (type)
20474 pushclass (type);
20475 bases = cp_parser_base_clause (parser);
20476 /* PR59482: get out of the previously pushed class scope so that the
20477 subsequent pops pop the right thing. */
20478 if (type)
20479 popclass ();
20481 else
20482 bases = NULL_TREE;
20484 /* If we're really defining a class, process the base classes.
20485 If they're invalid, fail. */
20486 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20487 && !xref_basetypes (type, bases))
20488 type = NULL_TREE;
20490 done:
20491 /* Leave the scope given by the nested-name-specifier. We will
20492 enter the class scope itself while processing the members. */
20493 if (pushed_scope)
20494 pop_scope (pushed_scope);
20496 if (invalid_explicit_specialization_p)
20498 end_specialization ();
20499 --parser->num_template_parameter_lists;
20502 if (type)
20503 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20504 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20505 CLASSTYPE_FINAL (type) = 1;
20506 out:
20507 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20508 return type;
20511 /* Parse a class-key.
20513 class-key:
20514 class
20515 struct
20516 union
20518 Returns the kind of class-key specified, or none_type to indicate
20519 error. */
20521 static enum tag_types
20522 cp_parser_class_key (cp_parser* parser)
20524 cp_token *token;
20525 enum tag_types tag_type;
20527 /* Look for the class-key. */
20528 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20529 if (!token)
20530 return none_type;
20532 /* Check to see if the TOKEN is a class-key. */
20533 tag_type = cp_parser_token_is_class_key (token);
20534 if (!tag_type)
20535 cp_parser_error (parser, "expected class-key");
20536 return tag_type;
20539 /* Parse an (optional) member-specification.
20541 member-specification:
20542 member-declaration member-specification [opt]
20543 access-specifier : member-specification [opt] */
20545 static void
20546 cp_parser_member_specification_opt (cp_parser* parser)
20548 while (true)
20550 cp_token *token;
20551 enum rid keyword;
20553 /* Peek at the next token. */
20554 token = cp_lexer_peek_token (parser->lexer);
20555 /* If it's a `}', or EOF then we've seen all the members. */
20556 if (token->type == CPP_CLOSE_BRACE
20557 || token->type == CPP_EOF
20558 || token->type == CPP_PRAGMA_EOL)
20559 break;
20561 /* See if this token is a keyword. */
20562 keyword = token->keyword;
20563 switch (keyword)
20565 case RID_PUBLIC:
20566 case RID_PROTECTED:
20567 case RID_PRIVATE:
20568 /* Consume the access-specifier. */
20569 cp_lexer_consume_token (parser->lexer);
20570 /* Remember which access-specifier is active. */
20571 current_access_specifier = token->u.value;
20572 /* Look for the `:'. */
20573 cp_parser_require (parser, CPP_COLON, RT_COLON);
20574 break;
20576 default:
20577 /* Accept #pragmas at class scope. */
20578 if (token->type == CPP_PRAGMA)
20580 cp_parser_pragma (parser, pragma_member);
20581 break;
20584 /* Otherwise, the next construction must be a
20585 member-declaration. */
20586 cp_parser_member_declaration (parser);
20591 /* Parse a member-declaration.
20593 member-declaration:
20594 decl-specifier-seq [opt] member-declarator-list [opt] ;
20595 function-definition ; [opt]
20596 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20597 using-declaration
20598 template-declaration
20599 alias-declaration
20601 member-declarator-list:
20602 member-declarator
20603 member-declarator-list , member-declarator
20605 member-declarator:
20606 declarator pure-specifier [opt]
20607 declarator constant-initializer [opt]
20608 identifier [opt] : constant-expression
20610 GNU Extensions:
20612 member-declaration:
20613 __extension__ member-declaration
20615 member-declarator:
20616 declarator attributes [opt] pure-specifier [opt]
20617 declarator attributes [opt] constant-initializer [opt]
20618 identifier [opt] attributes [opt] : constant-expression
20620 C++0x Extensions:
20622 member-declaration:
20623 static_assert-declaration */
20625 static void
20626 cp_parser_member_declaration (cp_parser* parser)
20628 cp_decl_specifier_seq decl_specifiers;
20629 tree prefix_attributes;
20630 tree decl;
20631 int declares_class_or_enum;
20632 bool friend_p;
20633 cp_token *token = NULL;
20634 cp_token *decl_spec_token_start = NULL;
20635 cp_token *initializer_token_start = NULL;
20636 int saved_pedantic;
20637 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20639 /* Check for the `__extension__' keyword. */
20640 if (cp_parser_extension_opt (parser, &saved_pedantic))
20642 /* Recurse. */
20643 cp_parser_member_declaration (parser);
20644 /* Restore the old value of the PEDANTIC flag. */
20645 pedantic = saved_pedantic;
20647 return;
20650 /* Check for a template-declaration. */
20651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20653 /* An explicit specialization here is an error condition, and we
20654 expect the specialization handler to detect and report this. */
20655 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20656 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20657 cp_parser_explicit_specialization (parser);
20658 else
20659 cp_parser_template_declaration (parser, /*member_p=*/true);
20661 return;
20664 /* Check for a using-declaration. */
20665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20667 if (cxx_dialect < cxx11)
20669 /* Parse the using-declaration. */
20670 cp_parser_using_declaration (parser,
20671 /*access_declaration_p=*/false);
20672 return;
20674 else
20676 tree decl;
20677 bool alias_decl_expected;
20678 cp_parser_parse_tentatively (parser);
20679 decl = cp_parser_alias_declaration (parser);
20680 /* Note that if we actually see the '=' token after the
20681 identifier, cp_parser_alias_declaration commits the
20682 tentative parse. In that case, we really expects an
20683 alias-declaration. Otherwise, we expect a using
20684 declaration. */
20685 alias_decl_expected =
20686 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20687 cp_parser_parse_definitely (parser);
20689 if (alias_decl_expected)
20690 finish_member_declaration (decl);
20691 else
20692 cp_parser_using_declaration (parser,
20693 /*access_declaration_p=*/false);
20694 return;
20698 /* Check for @defs. */
20699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20701 tree ivar, member;
20702 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20703 ivar = ivar_chains;
20704 while (ivar)
20706 member = ivar;
20707 ivar = TREE_CHAIN (member);
20708 TREE_CHAIN (member) = NULL_TREE;
20709 finish_member_declaration (member);
20711 return;
20714 /* If the next token is `static_assert' we have a static assertion. */
20715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20717 cp_parser_static_assert (parser, /*member_p=*/true);
20718 return;
20721 parser->colon_corrects_to_scope_p = false;
20723 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20724 goto out;
20726 /* Parse the decl-specifier-seq. */
20727 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20728 cp_parser_decl_specifier_seq (parser,
20729 CP_PARSER_FLAGS_OPTIONAL,
20730 &decl_specifiers,
20731 &declares_class_or_enum);
20732 /* Check for an invalid type-name. */
20733 if (!decl_specifiers.any_type_specifiers_p
20734 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20735 goto out;
20736 /* If there is no declarator, then the decl-specifier-seq should
20737 specify a type. */
20738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20740 /* If there was no decl-specifier-seq, and the next token is a
20741 `;', then we have something like:
20743 struct S { ; };
20745 [class.mem]
20747 Each member-declaration shall declare at least one member
20748 name of the class. */
20749 if (!decl_specifiers.any_specifiers_p)
20751 cp_token *token = cp_lexer_peek_token (parser->lexer);
20752 if (!in_system_header_at (token->location))
20753 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20755 else
20757 tree type;
20759 /* See if this declaration is a friend. */
20760 friend_p = cp_parser_friend_p (&decl_specifiers);
20761 /* If there were decl-specifiers, check to see if there was
20762 a class-declaration. */
20763 type = check_tag_decl (&decl_specifiers,
20764 /*explicit_type_instantiation_p=*/false);
20765 /* Nested classes have already been added to the class, but
20766 a `friend' needs to be explicitly registered. */
20767 if (friend_p)
20769 /* If the `friend' keyword was present, the friend must
20770 be introduced with a class-key. */
20771 if (!declares_class_or_enum && cxx_dialect < cxx11)
20772 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20773 "in C++03 a class-key must be used "
20774 "when declaring a friend");
20775 /* In this case:
20777 template <typename T> struct A {
20778 friend struct A<T>::B;
20781 A<T>::B will be represented by a TYPENAME_TYPE, and
20782 therefore not recognized by check_tag_decl. */
20783 if (!type)
20785 type = decl_specifiers.type;
20786 if (type && TREE_CODE (type) == TYPE_DECL)
20787 type = TREE_TYPE (type);
20789 if (!type || !TYPE_P (type))
20790 error_at (decl_spec_token_start->location,
20791 "friend declaration does not name a class or "
20792 "function");
20793 else
20794 make_friend_class (current_class_type, type,
20795 /*complain=*/true);
20797 /* If there is no TYPE, an error message will already have
20798 been issued. */
20799 else if (!type || type == error_mark_node)
20801 /* An anonymous aggregate has to be handled specially; such
20802 a declaration really declares a data member (with a
20803 particular type), as opposed to a nested class. */
20804 else if (ANON_AGGR_TYPE_P (type))
20806 /* C++11 9.5/6. */
20807 if (decl_specifiers.storage_class != sc_none)
20808 error_at (decl_spec_token_start->location,
20809 "a storage class on an anonymous aggregate "
20810 "in class scope is not allowed");
20812 /* Remove constructors and such from TYPE, now that we
20813 know it is an anonymous aggregate. */
20814 fixup_anonymous_aggr (type);
20815 /* And make the corresponding data member. */
20816 decl = build_decl (decl_spec_token_start->location,
20817 FIELD_DECL, NULL_TREE, type);
20818 /* Add it to the class. */
20819 finish_member_declaration (decl);
20821 else
20822 cp_parser_check_access_in_redeclaration
20823 (TYPE_NAME (type),
20824 decl_spec_token_start->location);
20827 else
20829 bool assume_semicolon = false;
20831 /* Clear attributes from the decl_specifiers but keep them
20832 around as prefix attributes that apply them to the entity
20833 being declared. */
20834 prefix_attributes = decl_specifiers.attributes;
20835 decl_specifiers.attributes = NULL_TREE;
20837 /* See if these declarations will be friends. */
20838 friend_p = cp_parser_friend_p (&decl_specifiers);
20840 /* Keep going until we hit the `;' at the end of the
20841 declaration. */
20842 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20844 tree attributes = NULL_TREE;
20845 tree first_attribute;
20847 /* Peek at the next token. */
20848 token = cp_lexer_peek_token (parser->lexer);
20850 /* Check for a bitfield declaration. */
20851 if (token->type == CPP_COLON
20852 || (token->type == CPP_NAME
20853 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20854 == CPP_COLON))
20856 tree identifier;
20857 tree width;
20859 /* Get the name of the bitfield. Note that we cannot just
20860 check TOKEN here because it may have been invalidated by
20861 the call to cp_lexer_peek_nth_token above. */
20862 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20863 identifier = cp_parser_identifier (parser);
20864 else
20865 identifier = NULL_TREE;
20867 /* Consume the `:' token. */
20868 cp_lexer_consume_token (parser->lexer);
20869 /* Get the width of the bitfield. */
20870 width
20871 = cp_parser_constant_expression (parser,
20872 /*allow_non_constant=*/false,
20873 NULL);
20875 /* Look for attributes that apply to the bitfield. */
20876 attributes = cp_parser_attributes_opt (parser);
20877 /* Remember which attributes are prefix attributes and
20878 which are not. */
20879 first_attribute = attributes;
20880 /* Combine the attributes. */
20881 attributes = chainon (prefix_attributes, attributes);
20883 /* Create the bitfield declaration. */
20884 decl = grokbitfield (identifier
20885 ? make_id_declarator (NULL_TREE,
20886 identifier,
20887 sfk_none)
20888 : NULL,
20889 &decl_specifiers,
20890 width,
20891 attributes);
20893 else
20895 cp_declarator *declarator;
20896 tree initializer;
20897 tree asm_specification;
20898 int ctor_dtor_or_conv_p;
20900 /* Parse the declarator. */
20901 declarator
20902 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20903 &ctor_dtor_or_conv_p,
20904 /*parenthesized_p=*/NULL,
20905 /*member_p=*/true);
20907 /* If something went wrong parsing the declarator, make sure
20908 that we at least consume some tokens. */
20909 if (declarator == cp_error_declarator)
20911 /* Skip to the end of the statement. */
20912 cp_parser_skip_to_end_of_statement (parser);
20913 /* If the next token is not a semicolon, that is
20914 probably because we just skipped over the body of
20915 a function. So, we consume a semicolon if
20916 present, but do not issue an error message if it
20917 is not present. */
20918 if (cp_lexer_next_token_is (parser->lexer,
20919 CPP_SEMICOLON))
20920 cp_lexer_consume_token (parser->lexer);
20921 goto out;
20924 if (declares_class_or_enum & 2)
20925 cp_parser_check_for_definition_in_return_type
20926 (declarator, decl_specifiers.type,
20927 decl_specifiers.locations[ds_type_spec]);
20929 /* Look for an asm-specification. */
20930 asm_specification = cp_parser_asm_specification_opt (parser);
20931 /* Look for attributes that apply to the declaration. */
20932 attributes = cp_parser_attributes_opt (parser);
20933 /* Remember which attributes are prefix attributes and
20934 which are not. */
20935 first_attribute = attributes;
20936 /* Combine the attributes. */
20937 attributes = chainon (prefix_attributes, attributes);
20939 /* If it's an `=', then we have a constant-initializer or a
20940 pure-specifier. It is not correct to parse the
20941 initializer before registering the member declaration
20942 since the member declaration should be in scope while
20943 its initializer is processed. However, the rest of the
20944 front end does not yet provide an interface that allows
20945 us to handle this correctly. */
20946 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20948 /* In [class.mem]:
20950 A pure-specifier shall be used only in the declaration of
20951 a virtual function.
20953 A member-declarator can contain a constant-initializer
20954 only if it declares a static member of integral or
20955 enumeration type.
20957 Therefore, if the DECLARATOR is for a function, we look
20958 for a pure-specifier; otherwise, we look for a
20959 constant-initializer. When we call `grokfield', it will
20960 perform more stringent semantics checks. */
20961 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20962 if (function_declarator_p (declarator)
20963 || (decl_specifiers.type
20964 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20965 && declarator->kind == cdk_id
20966 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20967 == FUNCTION_TYPE)))
20968 initializer = cp_parser_pure_specifier (parser);
20969 else if (decl_specifiers.storage_class != sc_static)
20970 initializer = cp_parser_save_nsdmi (parser);
20971 else if (cxx_dialect >= cxx11)
20973 bool nonconst;
20974 /* Don't require a constant rvalue in C++11, since we
20975 might want a reference constant. We'll enforce
20976 constancy later. */
20977 cp_lexer_consume_token (parser->lexer);
20978 /* Parse the initializer. */
20979 initializer = cp_parser_initializer_clause (parser,
20980 &nonconst);
20982 else
20983 /* Parse the initializer. */
20984 initializer = cp_parser_constant_initializer (parser);
20986 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20987 && !function_declarator_p (declarator))
20989 bool x;
20990 if (decl_specifiers.storage_class != sc_static)
20991 initializer = cp_parser_save_nsdmi (parser);
20992 else
20993 initializer = cp_parser_initializer (parser, &x, &x);
20995 /* Otherwise, there is no initializer. */
20996 else
20997 initializer = NULL_TREE;
20999 // Save and reset the current template requirements. Handle
21000 // the trailing requirements, if there are any.
21001 cp_manage_requirements saved_requirements (true);
21002 if (flag_concepts)
21003 cp_parser_trailing_requirements (parser, declarator);
21005 /* See if we are probably looking at a function
21006 definition. We are certainly not looking at a
21007 member-declarator. Calling `grokfield' has
21008 side-effects, so we must not do it unless we are sure
21009 that we are looking at a member-declarator. */
21010 if (cp_parser_token_starts_function_definition_p
21011 (cp_lexer_peek_token (parser->lexer)))
21013 /* The grammar does not allow a pure-specifier to be
21014 used when a member function is defined. (It is
21015 possible that this fact is an oversight in the
21016 standard, since a pure function may be defined
21017 outside of the class-specifier. */
21018 if (initializer && initializer_token_start)
21019 error_at (initializer_token_start->location,
21020 "pure-specifier on function-definition");
21021 decl = cp_parser_save_member_function_body (parser,
21022 &decl_specifiers,
21023 declarator,
21024 attributes);
21025 if (parser->fully_implicit_function_template_p)
21026 decl = finish_fully_implicit_template (parser, decl);
21027 /* If the member was not a friend, declare it here. */
21028 if (!friend_p)
21029 finish_member_declaration (decl);
21031 if (friend_p)
21032 check_constrained_friend (decl, current_template_reqs);
21034 /* Peek at the next token. */
21035 token = cp_lexer_peek_token (parser->lexer);
21036 /* If the next token is a semicolon, consume it. */
21037 if (token->type == CPP_SEMICOLON)
21038 cp_lexer_consume_token (parser->lexer);
21040 goto out;
21042 else
21043 if (declarator->kind == cdk_function)
21044 declarator->id_loc = token->location;
21045 /* Create the declaration. */
21046 decl = grokfield (declarator, &decl_specifiers,
21047 initializer, /*init_const_expr_p=*/true,
21048 asm_specification, attributes);
21049 if (parser->fully_implicit_function_template_p)
21051 if (friend_p)
21052 finish_fully_implicit_template (parser, 0);
21053 else
21054 decl = finish_fully_implicit_template (parser, decl);
21058 cp_finalize_omp_declare_simd (parser, decl);
21060 /* Reset PREFIX_ATTRIBUTES. */
21061 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21062 attributes = TREE_CHAIN (attributes);
21063 if (attributes)
21064 TREE_CHAIN (attributes) = NULL_TREE;
21066 /* If there is any qualification still in effect, clear it
21067 now; we will be starting fresh with the next declarator. */
21068 parser->scope = NULL_TREE;
21069 parser->qualifying_scope = NULL_TREE;
21070 parser->object_scope = NULL_TREE;
21071 /* If it's a `,', then there are more declarators. */
21072 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21074 cp_lexer_consume_token (parser->lexer);
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21077 cp_token *token = cp_lexer_previous_token (parser->lexer);
21078 error_at (token->location,
21079 "stray %<,%> at end of member declaration");
21082 /* If the next token isn't a `;', then we have a parse error. */
21083 else if (cp_lexer_next_token_is_not (parser->lexer,
21084 CPP_SEMICOLON))
21086 /* The next token might be a ways away from where the
21087 actual semicolon is missing. Find the previous token
21088 and use that for our error position. */
21089 cp_token *token = cp_lexer_previous_token (parser->lexer);
21090 error_at (token->location,
21091 "expected %<;%> at end of member declaration");
21093 /* Assume that the user meant to provide a semicolon. If
21094 we were to cp_parser_skip_to_end_of_statement, we might
21095 skip to a semicolon inside a member function definition
21096 and issue nonsensical error messages. */
21097 assume_semicolon = true;
21100 if (decl)
21102 /* Add DECL to the list of members. */
21103 if (!friend_p)
21104 finish_member_declaration (decl);
21106 if (TREE_CODE (decl) == FUNCTION_DECL)
21107 cp_parser_save_default_args (parser, decl);
21108 else if (TREE_CODE (decl) == FIELD_DECL
21109 && !DECL_C_BIT_FIELD (decl)
21110 && DECL_INITIAL (decl))
21111 /* Add DECL to the queue of NSDMI to be parsed later. */
21112 vec_safe_push (unparsed_nsdmis, decl);
21115 if (assume_semicolon)
21116 goto out;
21120 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21121 out:
21122 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21125 /* Parse a pure-specifier.
21127 pure-specifier:
21130 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21131 Otherwise, ERROR_MARK_NODE is returned. */
21133 static tree
21134 cp_parser_pure_specifier (cp_parser* parser)
21136 cp_token *token;
21138 /* Look for the `=' token. */
21139 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21140 return error_mark_node;
21141 /* Look for the `0' token. */
21142 token = cp_lexer_peek_token (parser->lexer);
21144 if (token->type == CPP_EOF
21145 || token->type == CPP_PRAGMA_EOL)
21146 return error_mark_node;
21148 cp_lexer_consume_token (parser->lexer);
21150 /* Accept = default or = delete in c++0x mode. */
21151 if (token->keyword == RID_DEFAULT
21152 || token->keyword == RID_DELETE)
21154 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21155 return token->u.value;
21158 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21159 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21161 cp_parser_error (parser,
21162 "invalid pure specifier (only %<= 0%> is allowed)");
21163 cp_parser_skip_to_end_of_statement (parser);
21164 return error_mark_node;
21166 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21168 error_at (token->location, "templates may not be %<virtual%>");
21169 return error_mark_node;
21172 return integer_zero_node;
21175 /* Parse a constant-initializer.
21177 constant-initializer:
21178 = constant-expression
21180 Returns a representation of the constant-expression. */
21182 static tree
21183 cp_parser_constant_initializer (cp_parser* parser)
21185 /* Look for the `=' token. */
21186 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21187 return error_mark_node;
21189 /* It is invalid to write:
21191 struct S { static const int i = { 7 }; };
21194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21196 cp_parser_error (parser,
21197 "a brace-enclosed initializer is not allowed here");
21198 /* Consume the opening brace. */
21199 cp_lexer_consume_token (parser->lexer);
21200 /* Skip the initializer. */
21201 cp_parser_skip_to_closing_brace (parser);
21202 /* Look for the trailing `}'. */
21203 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21205 return error_mark_node;
21208 return cp_parser_constant_expression (parser,
21209 /*allow_non_constant=*/false,
21210 NULL);
21213 /* Derived classes [gram.class.derived] */
21215 /* Parse a base-clause.
21217 base-clause:
21218 : base-specifier-list
21220 base-specifier-list:
21221 base-specifier ... [opt]
21222 base-specifier-list , base-specifier ... [opt]
21224 Returns a TREE_LIST representing the base-classes, in the order in
21225 which they were declared. The representation of each node is as
21226 described by cp_parser_base_specifier.
21228 In the case that no bases are specified, this function will return
21229 NULL_TREE, not ERROR_MARK_NODE. */
21231 static tree
21232 cp_parser_base_clause (cp_parser* parser)
21234 tree bases = NULL_TREE;
21236 /* Look for the `:' that begins the list. */
21237 cp_parser_require (parser, CPP_COLON, RT_COLON);
21239 /* Scan the base-specifier-list. */
21240 while (true)
21242 cp_token *token;
21243 tree base;
21244 bool pack_expansion_p = false;
21246 /* Look for the base-specifier. */
21247 base = cp_parser_base_specifier (parser);
21248 /* Look for the (optional) ellipsis. */
21249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21251 /* Consume the `...'. */
21252 cp_lexer_consume_token (parser->lexer);
21254 pack_expansion_p = true;
21257 /* Add BASE to the front of the list. */
21258 if (base && base != error_mark_node)
21260 if (pack_expansion_p)
21261 /* Make this a pack expansion type. */
21262 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21264 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21266 TREE_CHAIN (base) = bases;
21267 bases = base;
21270 /* Peek at the next token. */
21271 token = cp_lexer_peek_token (parser->lexer);
21272 /* If it's not a comma, then the list is complete. */
21273 if (token->type != CPP_COMMA)
21274 break;
21275 /* Consume the `,'. */
21276 cp_lexer_consume_token (parser->lexer);
21279 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21280 base class had a qualified name. However, the next name that
21281 appears is certainly not qualified. */
21282 parser->scope = NULL_TREE;
21283 parser->qualifying_scope = NULL_TREE;
21284 parser->object_scope = NULL_TREE;
21286 return nreverse (bases);
21289 /* Parse a base-specifier.
21291 base-specifier:
21292 :: [opt] nested-name-specifier [opt] class-name
21293 virtual access-specifier [opt] :: [opt] nested-name-specifier
21294 [opt] class-name
21295 access-specifier virtual [opt] :: [opt] nested-name-specifier
21296 [opt] class-name
21298 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21299 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21300 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21301 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21303 static tree
21304 cp_parser_base_specifier (cp_parser* parser)
21306 cp_token *token;
21307 bool done = false;
21308 bool virtual_p = false;
21309 bool duplicate_virtual_error_issued_p = false;
21310 bool duplicate_access_error_issued_p = false;
21311 bool class_scope_p, template_p;
21312 tree access = access_default_node;
21313 tree type;
21315 /* Process the optional `virtual' and `access-specifier'. */
21316 while (!done)
21318 /* Peek at the next token. */
21319 token = cp_lexer_peek_token (parser->lexer);
21320 /* Process `virtual'. */
21321 switch (token->keyword)
21323 case RID_VIRTUAL:
21324 /* If `virtual' appears more than once, issue an error. */
21325 if (virtual_p && !duplicate_virtual_error_issued_p)
21327 cp_parser_error (parser,
21328 "%<virtual%> specified more than once in base-specified");
21329 duplicate_virtual_error_issued_p = true;
21332 virtual_p = true;
21334 /* Consume the `virtual' token. */
21335 cp_lexer_consume_token (parser->lexer);
21337 break;
21339 case RID_PUBLIC:
21340 case RID_PROTECTED:
21341 case RID_PRIVATE:
21342 /* If more than one access specifier appears, issue an
21343 error. */
21344 if (access != access_default_node
21345 && !duplicate_access_error_issued_p)
21347 cp_parser_error (parser,
21348 "more than one access specifier in base-specified");
21349 duplicate_access_error_issued_p = true;
21352 access = ridpointers[(int) token->keyword];
21354 /* Consume the access-specifier. */
21355 cp_lexer_consume_token (parser->lexer);
21357 break;
21359 default:
21360 done = true;
21361 break;
21364 /* It is not uncommon to see programs mechanically, erroneously, use
21365 the 'typename' keyword to denote (dependent) qualified types
21366 as base classes. */
21367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21369 token = cp_lexer_peek_token (parser->lexer);
21370 if (!processing_template_decl)
21371 error_at (token->location,
21372 "keyword %<typename%> not allowed outside of templates");
21373 else
21374 error_at (token->location,
21375 "keyword %<typename%> not allowed in this context "
21376 "(the base class is implicitly a type)");
21377 cp_lexer_consume_token (parser->lexer);
21380 /* Look for the optional `::' operator. */
21381 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21382 /* Look for the nested-name-specifier. The simplest way to
21383 implement:
21385 [temp.res]
21387 The keyword `typename' is not permitted in a base-specifier or
21388 mem-initializer; in these contexts a qualified name that
21389 depends on a template-parameter is implicitly assumed to be a
21390 type name.
21392 is to pretend that we have seen the `typename' keyword at this
21393 point. */
21394 cp_parser_nested_name_specifier_opt (parser,
21395 /*typename_keyword_p=*/true,
21396 /*check_dependency_p=*/true,
21397 typename_type,
21398 /*is_declaration=*/true);
21399 /* If the base class is given by a qualified name, assume that names
21400 we see are type names or templates, as appropriate. */
21401 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21402 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21404 if (!parser->scope
21405 && cp_lexer_next_token_is_decltype (parser->lexer))
21406 /* DR 950 allows decltype as a base-specifier. */
21407 type = cp_parser_decltype (parser);
21408 else
21410 /* Otherwise, look for the class-name. */
21411 type = cp_parser_class_name (parser,
21412 class_scope_p,
21413 template_p,
21414 typename_type,
21415 /*check_dependency_p=*/true,
21416 /*class_head_p=*/false,
21417 /*is_declaration=*/true);
21418 type = TREE_TYPE (type);
21421 if (type == error_mark_node)
21422 return error_mark_node;
21424 return finish_base_specifier (type, access, virtual_p);
21427 /* Exception handling [gram.exception] */
21429 /* Parse an (optional) noexcept-specification.
21431 noexcept-specification:
21432 noexcept ( constant-expression ) [opt]
21434 If no noexcept-specification is present, returns NULL_TREE.
21435 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21436 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21437 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21438 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21439 in which case a boolean condition is returned instead. */
21441 static tree
21442 cp_parser_noexcept_specification_opt (cp_parser* parser,
21443 bool require_constexpr,
21444 bool* consumed_expr,
21445 bool return_cond)
21447 cp_token *token;
21448 const char *saved_message;
21450 /* Peek at the next token. */
21451 token = cp_lexer_peek_token (parser->lexer);
21453 /* Is it a noexcept-specification? */
21454 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21456 tree expr;
21457 cp_lexer_consume_token (parser->lexer);
21459 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21461 cp_lexer_consume_token (parser->lexer);
21463 if (require_constexpr)
21465 /* Types may not be defined in an exception-specification. */
21466 saved_message = parser->type_definition_forbidden_message;
21467 parser->type_definition_forbidden_message
21468 = G_("types may not be defined in an exception-specification");
21470 expr = cp_parser_constant_expression (parser, false, NULL);
21472 /* Restore the saved message. */
21473 parser->type_definition_forbidden_message = saved_message;
21475 else
21477 expr = cp_parser_expression (parser, false, NULL);
21478 *consumed_expr = true;
21481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21483 else
21485 expr = boolean_true_node;
21486 if (!require_constexpr)
21487 *consumed_expr = false;
21490 /* We cannot build a noexcept-spec right away because this will check
21491 that expr is a constexpr. */
21492 if (!return_cond)
21493 return build_noexcept_spec (expr, tf_warning_or_error);
21494 else
21495 return expr;
21497 else
21498 return NULL_TREE;
21501 /* Parse an (optional) exception-specification.
21503 exception-specification:
21504 throw ( type-id-list [opt] )
21506 Returns a TREE_LIST representing the exception-specification. The
21507 TREE_VALUE of each node is a type. */
21509 static tree
21510 cp_parser_exception_specification_opt (cp_parser* parser)
21512 cp_token *token;
21513 tree type_id_list;
21514 const char *saved_message;
21516 /* Peek at the next token. */
21517 token = cp_lexer_peek_token (parser->lexer);
21519 /* Is it a noexcept-specification? */
21520 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21521 false);
21522 if (type_id_list != NULL_TREE)
21523 return type_id_list;
21525 /* If it's not `throw', then there's no exception-specification. */
21526 if (!cp_parser_is_keyword (token, RID_THROW))
21527 return NULL_TREE;
21529 #if 0
21530 /* Enable this once a lot of code has transitioned to noexcept? */
21531 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21532 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21533 "deprecated in C++0x; use %<noexcept%> instead");
21534 #endif
21536 /* Consume the `throw'. */
21537 cp_lexer_consume_token (parser->lexer);
21539 /* Look for the `('. */
21540 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21542 /* Peek at the next token. */
21543 token = cp_lexer_peek_token (parser->lexer);
21544 /* If it's not a `)', then there is a type-id-list. */
21545 if (token->type != CPP_CLOSE_PAREN)
21547 /* Types may not be defined in an exception-specification. */
21548 saved_message = parser->type_definition_forbidden_message;
21549 parser->type_definition_forbidden_message
21550 = G_("types may not be defined in an exception-specification");
21551 /* Parse the type-id-list. */
21552 type_id_list = cp_parser_type_id_list (parser);
21553 /* Restore the saved message. */
21554 parser->type_definition_forbidden_message = saved_message;
21556 else
21557 type_id_list = empty_except_spec;
21559 /* Look for the `)'. */
21560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21562 return type_id_list;
21565 /* Parse an (optional) type-id-list.
21567 type-id-list:
21568 type-id ... [opt]
21569 type-id-list , type-id ... [opt]
21571 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21572 in the order that the types were presented. */
21574 static tree
21575 cp_parser_type_id_list (cp_parser* parser)
21577 tree types = NULL_TREE;
21579 while (true)
21581 cp_token *token;
21582 tree type;
21584 /* Get the next type-id. */
21585 type = cp_parser_type_id (parser);
21586 /* Parse the optional ellipsis. */
21587 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21589 /* Consume the `...'. */
21590 cp_lexer_consume_token (parser->lexer);
21592 /* Turn the type into a pack expansion expression. */
21593 type = make_pack_expansion (type);
21595 /* Add it to the list. */
21596 types = add_exception_specifier (types, type, /*complain=*/1);
21597 /* Peek at the next token. */
21598 token = cp_lexer_peek_token (parser->lexer);
21599 /* If it is not a `,', we are done. */
21600 if (token->type != CPP_COMMA)
21601 break;
21602 /* Consume the `,'. */
21603 cp_lexer_consume_token (parser->lexer);
21606 return nreverse (types);
21609 /* Parse a try-block.
21611 try-block:
21612 try compound-statement handler-seq */
21614 static tree
21615 cp_parser_try_block (cp_parser* parser)
21617 tree try_block;
21619 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21620 try_block = begin_try_block ();
21621 cp_parser_compound_statement (parser, NULL, true, false);
21622 finish_try_block (try_block);
21623 cp_parser_handler_seq (parser);
21624 finish_handler_sequence (try_block);
21626 return try_block;
21629 /* Parse a function-try-block.
21631 function-try-block:
21632 try ctor-initializer [opt] function-body handler-seq */
21634 static bool
21635 cp_parser_function_try_block (cp_parser* parser)
21637 tree compound_stmt;
21638 tree try_block;
21639 bool ctor_initializer_p;
21641 /* Look for the `try' keyword. */
21642 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21643 return false;
21644 /* Let the rest of the front end know where we are. */
21645 try_block = begin_function_try_block (&compound_stmt);
21646 /* Parse the function-body. */
21647 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21648 (parser, /*in_function_try_block=*/true);
21649 /* We're done with the `try' part. */
21650 finish_function_try_block (try_block);
21651 /* Parse the handlers. */
21652 cp_parser_handler_seq (parser);
21653 /* We're done with the handlers. */
21654 finish_function_handler_sequence (try_block, compound_stmt);
21656 return ctor_initializer_p;
21659 /* Parse a handler-seq.
21661 handler-seq:
21662 handler handler-seq [opt] */
21664 static void
21665 cp_parser_handler_seq (cp_parser* parser)
21667 while (true)
21669 cp_token *token;
21671 /* Parse the handler. */
21672 cp_parser_handler (parser);
21673 /* Peek at the next token. */
21674 token = cp_lexer_peek_token (parser->lexer);
21675 /* If it's not `catch' then there are no more handlers. */
21676 if (!cp_parser_is_keyword (token, RID_CATCH))
21677 break;
21681 /* Parse a handler.
21683 handler:
21684 catch ( exception-declaration ) compound-statement */
21686 static void
21687 cp_parser_handler (cp_parser* parser)
21689 tree handler;
21690 tree declaration;
21692 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21693 handler = begin_handler ();
21694 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21695 declaration = cp_parser_exception_declaration (parser);
21696 finish_handler_parms (declaration, handler);
21697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21698 cp_parser_compound_statement (parser, NULL, false, false);
21699 finish_handler (handler);
21702 /* Parse an exception-declaration.
21704 exception-declaration:
21705 type-specifier-seq declarator
21706 type-specifier-seq abstract-declarator
21707 type-specifier-seq
21710 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21711 ellipsis variant is used. */
21713 static tree
21714 cp_parser_exception_declaration (cp_parser* parser)
21716 cp_decl_specifier_seq type_specifiers;
21717 cp_declarator *declarator;
21718 const char *saved_message;
21720 /* If it's an ellipsis, it's easy to handle. */
21721 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21723 /* Consume the `...' token. */
21724 cp_lexer_consume_token (parser->lexer);
21725 return NULL_TREE;
21728 /* Types may not be defined in exception-declarations. */
21729 saved_message = parser->type_definition_forbidden_message;
21730 parser->type_definition_forbidden_message
21731 = G_("types may not be defined in exception-declarations");
21733 /* Parse the type-specifier-seq. */
21734 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21735 /*is_trailing_return=*/false,
21736 &type_specifiers);
21737 /* If it's a `)', then there is no declarator. */
21738 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21739 declarator = NULL;
21740 else
21741 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21742 /*ctor_dtor_or_conv_p=*/NULL,
21743 /*parenthesized_p=*/NULL,
21744 /*member_p=*/false);
21746 /* Restore the saved message. */
21747 parser->type_definition_forbidden_message = saved_message;
21749 if (!type_specifiers.any_specifiers_p)
21750 return error_mark_node;
21752 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21755 /* Parse a throw-expression.
21757 throw-expression:
21758 throw assignment-expression [opt]
21760 Returns a THROW_EXPR representing the throw-expression. */
21762 static tree
21763 cp_parser_throw_expression (cp_parser* parser)
21765 tree expression;
21766 cp_token* token;
21768 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21769 token = cp_lexer_peek_token (parser->lexer);
21770 /* Figure out whether or not there is an assignment-expression
21771 following the "throw" keyword. */
21772 if (token->type == CPP_COMMA
21773 || token->type == CPP_SEMICOLON
21774 || token->type == CPP_CLOSE_PAREN
21775 || token->type == CPP_CLOSE_SQUARE
21776 || token->type == CPP_CLOSE_BRACE
21777 || token->type == CPP_COLON)
21778 expression = NULL_TREE;
21779 else
21780 expression = cp_parser_assignment_expression (parser,
21781 /*cast_p=*/false, NULL);
21783 return build_throw (expression);
21786 /* GNU Extensions */
21788 /* Parse an (optional) asm-specification.
21790 asm-specification:
21791 asm ( string-literal )
21793 If the asm-specification is present, returns a STRING_CST
21794 corresponding to the string-literal. Otherwise, returns
21795 NULL_TREE. */
21797 static tree
21798 cp_parser_asm_specification_opt (cp_parser* parser)
21800 cp_token *token;
21801 tree asm_specification;
21803 /* Peek at the next token. */
21804 token = cp_lexer_peek_token (parser->lexer);
21805 /* If the next token isn't the `asm' keyword, then there's no
21806 asm-specification. */
21807 if (!cp_parser_is_keyword (token, RID_ASM))
21808 return NULL_TREE;
21810 /* Consume the `asm' token. */
21811 cp_lexer_consume_token (parser->lexer);
21812 /* Look for the `('. */
21813 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21815 /* Look for the string-literal. */
21816 asm_specification = cp_parser_string_literal (parser, false, false);
21818 /* Look for the `)'. */
21819 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21821 return asm_specification;
21824 /* Parse an asm-operand-list.
21826 asm-operand-list:
21827 asm-operand
21828 asm-operand-list , asm-operand
21830 asm-operand:
21831 string-literal ( expression )
21832 [ string-literal ] string-literal ( expression )
21834 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21835 each node is the expression. The TREE_PURPOSE is itself a
21836 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21837 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21838 is a STRING_CST for the string literal before the parenthesis. Returns
21839 ERROR_MARK_NODE if any of the operands are invalid. */
21841 static tree
21842 cp_parser_asm_operand_list (cp_parser* parser)
21844 tree asm_operands = NULL_TREE;
21845 bool invalid_operands = false;
21847 while (true)
21849 tree string_literal;
21850 tree expression;
21851 tree name;
21853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21855 /* Consume the `[' token. */
21856 cp_lexer_consume_token (parser->lexer);
21857 /* Read the operand name. */
21858 name = cp_parser_identifier (parser);
21859 if (name != error_mark_node)
21860 name = build_string (IDENTIFIER_LENGTH (name),
21861 IDENTIFIER_POINTER (name));
21862 /* Look for the closing `]'. */
21863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21865 else
21866 name = NULL_TREE;
21867 /* Look for the string-literal. */
21868 string_literal = cp_parser_string_literal (parser, false, false);
21870 /* Look for the `('. */
21871 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21872 /* Parse the expression. */
21873 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21874 /* Look for the `)'. */
21875 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21877 if (name == error_mark_node
21878 || string_literal == error_mark_node
21879 || expression == error_mark_node)
21880 invalid_operands = true;
21882 /* Add this operand to the list. */
21883 asm_operands = tree_cons (build_tree_list (name, string_literal),
21884 expression,
21885 asm_operands);
21886 /* If the next token is not a `,', there are no more
21887 operands. */
21888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21889 break;
21890 /* Consume the `,'. */
21891 cp_lexer_consume_token (parser->lexer);
21894 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21897 /* Parse an asm-clobber-list.
21899 asm-clobber-list:
21900 string-literal
21901 asm-clobber-list , string-literal
21903 Returns a TREE_LIST, indicating the clobbers in the order that they
21904 appeared. The TREE_VALUE of each node is a STRING_CST. */
21906 static tree
21907 cp_parser_asm_clobber_list (cp_parser* parser)
21909 tree clobbers = NULL_TREE;
21911 while (true)
21913 tree string_literal;
21915 /* Look for the string literal. */
21916 string_literal = cp_parser_string_literal (parser, false, false);
21917 /* Add it to the list. */
21918 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21919 /* If the next token is not a `,', then the list is
21920 complete. */
21921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21922 break;
21923 /* Consume the `,' token. */
21924 cp_lexer_consume_token (parser->lexer);
21927 return clobbers;
21930 /* Parse an asm-label-list.
21932 asm-label-list:
21933 identifier
21934 asm-label-list , identifier
21936 Returns a TREE_LIST, indicating the labels in the order that they
21937 appeared. The TREE_VALUE of each node is a label. */
21939 static tree
21940 cp_parser_asm_label_list (cp_parser* parser)
21942 tree labels = NULL_TREE;
21944 while (true)
21946 tree identifier, label, name;
21948 /* Look for the identifier. */
21949 identifier = cp_parser_identifier (parser);
21950 if (!error_operand_p (identifier))
21952 label = lookup_label (identifier);
21953 if (TREE_CODE (label) == LABEL_DECL)
21955 TREE_USED (label) = 1;
21956 check_goto (label);
21957 name = build_string (IDENTIFIER_LENGTH (identifier),
21958 IDENTIFIER_POINTER (identifier));
21959 labels = tree_cons (name, label, labels);
21962 /* If the next token is not a `,', then the list is
21963 complete. */
21964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21965 break;
21966 /* Consume the `,' token. */
21967 cp_lexer_consume_token (parser->lexer);
21970 return nreverse (labels);
21973 /* Return TRUE iff the next tokens in the stream are possibly the
21974 beginning of a GNU extension attribute. */
21976 static bool
21977 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21979 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21982 /* Return TRUE iff the next tokens in the stream are possibly the
21983 beginning of a standard C++-11 attribute specifier. */
21985 static bool
21986 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21988 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21991 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21992 beginning of a standard C++-11 attribute specifier. */
21994 static bool
21995 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21997 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21999 return (cxx_dialect >= cxx11
22000 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22001 || (token->type == CPP_OPEN_SQUARE
22002 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22003 && token->type == CPP_OPEN_SQUARE)));
22006 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22007 beginning of a GNU extension attribute. */
22009 static bool
22010 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22012 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22014 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22017 /* Return true iff the next tokens can be the beginning of either a
22018 GNU attribute list, or a standard C++11 attribute sequence. */
22020 static bool
22021 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22023 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22024 || cp_next_tokens_can_be_std_attribute_p (parser));
22027 /* Return true iff the next Nth tokens can be the beginning of either
22028 a GNU attribute list, or a standard C++11 attribute sequence. */
22030 static bool
22031 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22033 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22034 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22037 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22038 of GNU attributes, or return NULL. */
22040 static tree
22041 cp_parser_attributes_opt (cp_parser *parser)
22043 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22044 return cp_parser_gnu_attributes_opt (parser);
22045 return cp_parser_std_attribute_spec_seq (parser);
22048 #define CILK_SIMD_FN_CLAUSE_MASK \
22049 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22050 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22051 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22052 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22053 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22055 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22056 vector [(<clauses>)] */
22058 static void
22059 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22061 bool first_p = parser->cilk_simd_fn_info == NULL;
22062 cp_token *token = v_token;
22063 if (first_p)
22065 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22066 parser->cilk_simd_fn_info->error_seen = false;
22067 parser->cilk_simd_fn_info->fndecl_seen = false;
22068 parser->cilk_simd_fn_info->tokens = vNULL;
22070 int paren_scope = 0;
22071 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22073 cp_lexer_consume_token (parser->lexer);
22074 v_token = cp_lexer_peek_token (parser->lexer);
22075 paren_scope++;
22077 while (paren_scope > 0)
22079 token = cp_lexer_peek_token (parser->lexer);
22080 if (token->type == CPP_OPEN_PAREN)
22081 paren_scope++;
22082 else if (token->type == CPP_CLOSE_PAREN)
22083 paren_scope--;
22084 /* Do not push the last ')' */
22085 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22086 cp_lexer_consume_token (parser->lexer);
22089 token->type = CPP_PRAGMA_EOL;
22090 parser->lexer->next_token = token;
22091 cp_lexer_consume_token (parser->lexer);
22093 struct cp_token_cache *cp
22094 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22095 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22098 /* Parse an (optional) series of attributes.
22100 attributes:
22101 attributes attribute
22103 attribute:
22104 __attribute__ (( attribute-list [opt] ))
22106 The return value is as for cp_parser_gnu_attribute_list. */
22108 static tree
22109 cp_parser_gnu_attributes_opt (cp_parser* parser)
22111 tree attributes = NULL_TREE;
22113 while (true)
22115 cp_token *token;
22116 tree attribute_list;
22117 bool ok = true;
22119 /* Peek at the next token. */
22120 token = cp_lexer_peek_token (parser->lexer);
22121 /* If it's not `__attribute__', then we're done. */
22122 if (token->keyword != RID_ATTRIBUTE)
22123 break;
22125 /* Consume the `__attribute__' keyword. */
22126 cp_lexer_consume_token (parser->lexer);
22127 /* Look for the two `(' tokens. */
22128 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22129 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22131 /* Peek at the next token. */
22132 token = cp_lexer_peek_token (parser->lexer);
22133 if (token->type != CPP_CLOSE_PAREN)
22134 /* Parse the attribute-list. */
22135 attribute_list = cp_parser_gnu_attribute_list (parser);
22136 else
22137 /* If the next token is a `)', then there is no attribute
22138 list. */
22139 attribute_list = NULL;
22141 /* Look for the two `)' tokens. */
22142 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22143 ok = false;
22144 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22145 ok = false;
22146 if (!ok)
22147 cp_parser_skip_to_end_of_statement (parser);
22149 /* Add these new attributes to the list. */
22150 attributes = chainon (attributes, attribute_list);
22153 return attributes;
22156 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22157 "__vector" or "__vector__." */
22159 static inline bool
22160 is_cilkplus_vector_p (tree name)
22162 if (flag_cilkplus && is_attribute_p ("vector", name))
22163 return true;
22164 return false;
22167 /* Parse a GNU attribute-list.
22169 attribute-list:
22170 attribute
22171 attribute-list , attribute
22173 attribute:
22174 identifier
22175 identifier ( identifier )
22176 identifier ( identifier , expression-list )
22177 identifier ( expression-list )
22179 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22180 to an attribute. The TREE_PURPOSE of each node is the identifier
22181 indicating which attribute is in use. The TREE_VALUE represents
22182 the arguments, if any. */
22184 static tree
22185 cp_parser_gnu_attribute_list (cp_parser* parser)
22187 tree attribute_list = NULL_TREE;
22188 bool save_translate_strings_p = parser->translate_strings_p;
22190 parser->translate_strings_p = false;
22191 while (true)
22193 cp_token *token;
22194 tree identifier;
22195 tree attribute;
22197 /* Look for the identifier. We also allow keywords here; for
22198 example `__attribute__ ((const))' is legal. */
22199 token = cp_lexer_peek_token (parser->lexer);
22200 if (token->type == CPP_NAME
22201 || token->type == CPP_KEYWORD)
22203 tree arguments = NULL_TREE;
22205 /* Consume the token, but save it since we need it for the
22206 SIMD enabled function parsing. */
22207 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22209 /* Save away the identifier that indicates which attribute
22210 this is. */
22211 identifier = (token->type == CPP_KEYWORD)
22212 /* For keywords, use the canonical spelling, not the
22213 parsed identifier. */
22214 ? ridpointers[(int) token->keyword]
22215 : id_token->u.value;
22217 attribute = build_tree_list (identifier, NULL_TREE);
22219 /* Peek at the next token. */
22220 token = cp_lexer_peek_token (parser->lexer);
22221 /* If it's an `(', then parse the attribute arguments. */
22222 if (token->type == CPP_OPEN_PAREN)
22224 vec<tree, va_gc> *vec;
22225 int attr_flag = (attribute_takes_identifier_p (identifier)
22226 ? id_attr : normal_attr);
22227 if (is_cilkplus_vector_p (identifier))
22229 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22230 continue;
22232 else
22233 vec = cp_parser_parenthesized_expression_list
22234 (parser, attr_flag, /*cast_p=*/false,
22235 /*allow_expansion_p=*/false,
22236 /*non_constant_p=*/NULL);
22237 if (vec == NULL)
22238 arguments = error_mark_node;
22239 else
22241 arguments = build_tree_list_vec (vec);
22242 release_tree_vector (vec);
22244 /* Save the arguments away. */
22245 TREE_VALUE (attribute) = arguments;
22247 else if (is_cilkplus_vector_p (identifier))
22249 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22250 continue;
22253 if (arguments != error_mark_node)
22255 /* Add this attribute to the list. */
22256 TREE_CHAIN (attribute) = attribute_list;
22257 attribute_list = attribute;
22260 token = cp_lexer_peek_token (parser->lexer);
22262 /* Now, look for more attributes. If the next token isn't a
22263 `,', we're done. */
22264 if (token->type != CPP_COMMA)
22265 break;
22267 /* Consume the comma and keep going. */
22268 cp_lexer_consume_token (parser->lexer);
22270 parser->translate_strings_p = save_translate_strings_p;
22272 /* We built up the list in reverse order. */
22273 return nreverse (attribute_list);
22276 /* Parse a standard C++11 attribute.
22278 The returned representation is a TREE_LIST which TREE_PURPOSE is
22279 the scoped name of the attribute, and the TREE_VALUE is its
22280 arguments list.
22282 Note that the scoped name of the attribute is itself a TREE_LIST
22283 which TREE_PURPOSE is the namespace of the attribute, and
22284 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22285 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22286 and which TREE_PURPOSE is directly the attribute name.
22288 Clients of the attribute code should use get_attribute_namespace
22289 and get_attribute_name to get the actual namespace and name of
22290 attributes, regardless of their being GNU or C++11 attributes.
22292 attribute:
22293 attribute-token attribute-argument-clause [opt]
22295 attribute-token:
22296 identifier
22297 attribute-scoped-token
22299 attribute-scoped-token:
22300 attribute-namespace :: identifier
22302 attribute-namespace:
22303 identifier
22305 attribute-argument-clause:
22306 ( balanced-token-seq )
22308 balanced-token-seq:
22309 balanced-token [opt]
22310 balanced-token-seq balanced-token
22312 balanced-token:
22313 ( balanced-token-seq )
22314 [ balanced-token-seq ]
22315 { balanced-token-seq }. */
22317 static tree
22318 cp_parser_std_attribute (cp_parser *parser)
22320 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22321 cp_token *token;
22323 /* First, parse name of the the attribute, a.k.a
22324 attribute-token. */
22326 token = cp_lexer_peek_token (parser->lexer);
22327 if (token->type == CPP_NAME)
22328 attr_id = token->u.value;
22329 else if (token->type == CPP_KEYWORD)
22330 attr_id = ridpointers[(int) token->keyword];
22331 else if (token->flags & NAMED_OP)
22332 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22334 if (attr_id == NULL_TREE)
22335 return NULL_TREE;
22337 cp_lexer_consume_token (parser->lexer);
22339 token = cp_lexer_peek_token (parser->lexer);
22340 if (token->type == CPP_SCOPE)
22342 /* We are seeing a scoped attribute token. */
22344 cp_lexer_consume_token (parser->lexer);
22345 attr_ns = attr_id;
22347 token = cp_lexer_consume_token (parser->lexer);
22348 if (token->type == CPP_NAME)
22349 attr_id = token->u.value;
22350 else if (token->type == CPP_KEYWORD)
22351 attr_id = ridpointers[(int) token->keyword];
22352 else
22354 error_at (token->location,
22355 "expected an identifier for the attribute name");
22356 return error_mark_node;
22358 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22359 NULL_TREE);
22360 token = cp_lexer_peek_token (parser->lexer);
22362 else
22364 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22365 NULL_TREE);
22366 /* C++11 noreturn attribute is equivalent to GNU's. */
22367 if (is_attribute_p ("noreturn", attr_id))
22368 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22369 /* C++14 deprecated attribute is equivalent to GNU's. */
22370 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
22371 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22374 /* Now parse the optional argument clause of the attribute. */
22376 if (token->type != CPP_OPEN_PAREN)
22377 return attribute;
22380 vec<tree, va_gc> *vec;
22381 int attr_flag = normal_attr;
22383 if (attr_ns == get_identifier ("gnu")
22384 && attribute_takes_identifier_p (attr_id))
22385 /* A GNU attribute that takes an identifier in parameter. */
22386 attr_flag = id_attr;
22388 vec = cp_parser_parenthesized_expression_list
22389 (parser, attr_flag, /*cast_p=*/false,
22390 /*allow_expansion_p=*/true,
22391 /*non_constant_p=*/NULL);
22392 if (vec == NULL)
22393 arguments = error_mark_node;
22394 else
22396 arguments = build_tree_list_vec (vec);
22397 release_tree_vector (vec);
22400 if (arguments == error_mark_node)
22401 attribute = error_mark_node;
22402 else
22403 TREE_VALUE (attribute) = arguments;
22406 return attribute;
22409 /* Parse a list of standard C++-11 attributes.
22411 attribute-list:
22412 attribute [opt]
22413 attribute-list , attribute[opt]
22414 attribute ...
22415 attribute-list , attribute ...
22418 static tree
22419 cp_parser_std_attribute_list (cp_parser *parser)
22421 tree attributes = NULL_TREE, attribute = NULL_TREE;
22422 cp_token *token = NULL;
22424 while (true)
22426 attribute = cp_parser_std_attribute (parser);
22427 if (attribute == error_mark_node)
22428 break;
22429 if (attribute != NULL_TREE)
22431 TREE_CHAIN (attribute) = attributes;
22432 attributes = attribute;
22434 token = cp_lexer_peek_token (parser->lexer);
22435 if (token->type != CPP_COMMA)
22436 break;
22437 cp_lexer_consume_token (parser->lexer);
22439 attributes = nreverse (attributes);
22440 return attributes;
22443 /* Parse a standard C++-11 attribute specifier.
22445 attribute-specifier:
22446 [ [ attribute-list ] ]
22447 alignment-specifier
22449 alignment-specifier:
22450 alignas ( type-id ... [opt] )
22451 alignas ( alignment-expression ... [opt] ). */
22453 static tree
22454 cp_parser_std_attribute_spec (cp_parser *parser)
22456 tree attributes = NULL_TREE;
22457 cp_token *token = cp_lexer_peek_token (parser->lexer);
22459 if (token->type == CPP_OPEN_SQUARE
22460 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22462 cp_lexer_consume_token (parser->lexer);
22463 cp_lexer_consume_token (parser->lexer);
22465 attributes = cp_parser_std_attribute_list (parser);
22467 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22468 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22469 cp_parser_skip_to_end_of_statement (parser);
22470 else
22471 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22472 when we are sure that we have actually parsed them. */
22473 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22475 else
22477 tree alignas_expr;
22479 /* Look for an alignment-specifier. */
22481 token = cp_lexer_peek_token (parser->lexer);
22483 if (token->type != CPP_KEYWORD
22484 || token->keyword != RID_ALIGNAS)
22485 return NULL_TREE;
22487 cp_lexer_consume_token (parser->lexer);
22488 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22490 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22492 cp_parser_error (parser, "expected %<(%>");
22493 return error_mark_node;
22496 cp_parser_parse_tentatively (parser);
22497 alignas_expr = cp_parser_type_id (parser);
22499 if (!cp_parser_parse_definitely (parser))
22501 gcc_assert (alignas_expr == error_mark_node
22502 || alignas_expr == NULL_TREE);
22504 alignas_expr =
22505 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22506 /**cp_id_kind=*/NULL);
22507 if (alignas_expr == error_mark_node)
22508 cp_parser_skip_to_end_of_statement (parser);
22509 if (alignas_expr == NULL_TREE
22510 || alignas_expr == error_mark_node)
22511 return alignas_expr;
22514 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22516 cp_parser_error (parser, "expected %<)%>");
22517 return error_mark_node;
22520 alignas_expr = cxx_alignas_expr (alignas_expr);
22522 /* Build the C++-11 representation of an 'aligned'
22523 attribute. */
22524 attributes =
22525 build_tree_list (build_tree_list (get_identifier ("gnu"),
22526 get_identifier ("aligned")),
22527 build_tree_list (NULL_TREE, alignas_expr));
22530 return attributes;
22533 /* Parse a standard C++-11 attribute-specifier-seq.
22535 attribute-specifier-seq:
22536 attribute-specifier-seq [opt] attribute-specifier
22539 static tree
22540 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22542 tree attr_specs = NULL;
22544 while (true)
22546 tree attr_spec = cp_parser_std_attribute_spec (parser);
22547 if (attr_spec == NULL_TREE)
22548 break;
22549 if (attr_spec == error_mark_node)
22550 return error_mark_node;
22552 TREE_CHAIN (attr_spec) = attr_specs;
22553 attr_specs = attr_spec;
22556 attr_specs = nreverse (attr_specs);
22557 return attr_specs;
22560 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22561 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22562 current value of the PEDANTIC flag, regardless of whether or not
22563 the `__extension__' keyword is present. The caller is responsible
22564 for restoring the value of the PEDANTIC flag. */
22566 static bool
22567 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22569 /* Save the old value of the PEDANTIC flag. */
22570 *saved_pedantic = pedantic;
22572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22574 /* Consume the `__extension__' token. */
22575 cp_lexer_consume_token (parser->lexer);
22576 /* We're not being pedantic while the `__extension__' keyword is
22577 in effect. */
22578 pedantic = 0;
22580 return true;
22583 return false;
22586 /* Parse a label declaration.
22588 label-declaration:
22589 __label__ label-declarator-seq ;
22591 label-declarator-seq:
22592 identifier , label-declarator-seq
22593 identifier */
22595 static void
22596 cp_parser_label_declaration (cp_parser* parser)
22598 /* Look for the `__label__' keyword. */
22599 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22601 while (true)
22603 tree identifier;
22605 /* Look for an identifier. */
22606 identifier = cp_parser_identifier (parser);
22607 /* If we failed, stop. */
22608 if (identifier == error_mark_node)
22609 break;
22610 /* Declare it as a label. */
22611 finish_label_decl (identifier);
22612 /* If the next token is a `;', stop. */
22613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22614 break;
22615 /* Look for the `,' separating the label declarations. */
22616 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22619 /* Look for the final `;'. */
22620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22623 // -------------------------------------------------------------------------- //
22624 // Requires Clause
22626 // Parse a requires clause.
22628 // requires-clause:
22629 // 'requires' logical-or-expression
22631 // The required logical-or-expression must be a constant expression.
22632 static tree
22633 cp_parser_requires_clause (cp_parser *parser)
22635 // Parse the constant expression.
22636 tree expr =
22637 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
22638 if (!require_potential_rvalue_constant_expression (expr))
22639 return error_mark_node;
22640 return expr;
22643 // Optionally parse a requires clause:
22645 // requires-clause:
22646 // 'requires' logical-or-expression
22648 // The required logical-or-expression must be a constant expression.
22649 static tree
22650 cp_parser_requires_clause_opt (cp_parser *parser)
22652 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22653 return NULL_TREE;
22654 cp_lexer_consume_token (parser->lexer);
22655 return cp_parser_requires_clause (parser);
22659 // -------------------------------------------------------------------------- //
22660 // Requires Expression
22663 // An RAII helper that provides scoped control for entering and exiting
22664 // the local scope defined by a requires expression. Name bindings introduced
22665 // within the scope are popped prior to exiting the scope.
22666 struct cp_parser_requires_expr_scope
22668 // Enter a scope of kind K belonging to the decl D.
22669 cp_parser_requires_expr_scope ()
22671 begin_scope (sk_block, NULL_TREE);
22674 ~cp_parser_requires_expr_scope ()
22676 for (tree t = current_binding_level->names; t; t = DECL_CHAIN (t))
22677 pop_binding (DECL_NAME (t), t);
22678 leave_scope ();
22682 // Parse a requires expression
22684 // requirement-expression:
22685 // 'requires' requirement-parameter-list requirement-body
22686 static tree
22687 cp_parser_requires_expression (cp_parser *parser)
22689 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
22690 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
22692 // TODO: Earlier versions of the concepts lite spec did not allow
22693 // requires expressions outside of template declarations. That
22694 // restriction was relaxed in Chicago, but it has not been implemented.
22695 if (!processing_template_decl)
22697 error_at (loc, "a requires expression cannot appear outside a template");
22698 cp_parser_skip_to_end_of_statement (parser);
22699 return error_mark_node;
22703 // TODO: Check that requires expressions are only written inside of
22704 // template declarations. They don't need to be concepts, just templates.
22706 // Parse the optional parameter list. Any local parameter declarations
22707 // are added to a new scope and are visible within the nested
22708 // requirement list.
22709 cp_parser_requires_expr_scope guard;
22710 tree parms = cp_parser_requirement_parameter_list (parser);
22711 if (parms == error_mark_node)
22712 return error_mark_node;
22714 // Parse the requirement body.
22715 tree reqs = cp_parser_requirement_body (parser);
22716 if (reqs == error_mark_node)
22717 return error_mark_node;
22719 return finish_requires_expr (parms, reqs);
22722 // Parse a parameterized requirement.
22724 // requirement-parameter-list:
22725 // '(' parameter-declaration-clause ')'
22726 static tree
22727 cp_parser_requirement_parameter_list (cp_parser *parser)
22729 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22730 return error_mark_node;
22732 // Parse the nested parameter declaration clause.
22733 tree parms = cp_parser_parameter_declaration_clause (parser);
22734 if (parms == error_mark_node)
22735 return error_mark_node;
22737 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22738 return error_mark_node;
22740 return parms;
22743 // Parse the body of a requirement.
22745 // requirement-body:
22746 // '{' requirement-list '}'
22747 static tree
22748 cp_parser_requirement_body (cp_parser *parser)
22750 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22751 return error_mark_node;
22753 tree reqs = cp_parser_requirement_list (parser);
22755 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22756 return error_mark_node;
22758 return reqs;
22761 // Parse a list of requirements.
22763 // requirement-list:
22764 // requirement
22765 // requirement-list ';' requirement[opt]
22766 static tree
22767 cp_parser_requirement_list (cp_parser *parser)
22769 tree result = NULL_TREE;
22770 while (true)
22772 tree req = cp_parser_requirement (parser);
22773 if (req == error_mark_node)
22774 return req;
22776 result = tree_cons (NULL_TREE, req, result);
22778 // If we see a semi-colon, consume it.
22779 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22780 cp_lexer_consume_token (parser->lexer);
22782 // If we've found the end of the list, stop processing
22783 // the list.
22784 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22785 break;
22788 // Reverse the order of requirements so they are analyzed in
22789 // declaration order.
22790 return nreverse (result);
22793 // Parse a syntactic requirement or type requirement.
22795 // requirement:
22796 // simple-requirement
22797 // compound-requirement
22798 // type-requirement
22799 // nested-requirement
22800 static tree
22801 cp_parser_requirement (cp_parser *parser)
22803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22804 return cp_parser_nested_requirement (parser);
22806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22807 return cp_parser_compound_requirement (parser);
22809 // Try parsing a type requirement first.
22810 cp_parser_parse_tentatively (parser);
22811 tree req = cp_parser_type_requirement (parser);
22812 if (!cp_parser_parse_definitely (parser))
22813 req = cp_parser_simple_requirement (parser);
22815 return req;
22818 // Parse a nested requirement. This is the same as a requires clause.
22820 // nested-requirement:
22821 // requires-clause
22822 static tree
22823 cp_parser_nested_requirement (cp_parser *parser)
22825 cp_lexer_consume_token (parser->lexer);
22826 tree req = cp_parser_requires_clause (parser);
22827 if (req == error_mark_node)
22828 return error_mark_node;
22829 return finish_nested_requirement (req);
22832 // Parse a simple requirement.
22834 // simple-requirement:
22835 // expression
22836 static tree
22837 cp_parser_simple_requirement (cp_parser *parser)
22839 tree expr = cp_parser_expression (parser, false, false, NULL);
22840 if (!expr || expr == error_mark_node)
22841 return error_mark_node;
22842 return finish_expr_requirement (expr);
22845 // Parse a compound requirement
22847 // compound-requirement:
22848 // '{' expression '}' trailing-constraint-specifiers
22850 // trailing-constraint-specifiers:
22851 // constraint-specifiers-seq[opt] result-type-requirement[opt]
22853 // result-type-requirement:
22854 // '->' type-id
22855 static tree
22856 cp_parser_compound_requirement (cp_parser *parser)
22858 // Parse an expression enclosed in '{ }'s.
22859 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22860 return error_mark_node;
22862 tree expr = cp_parser_expression (parser, false, false, NULL);
22863 if (!expr || expr == error_mark_node)
22864 return error_mark_node;
22866 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22867 return error_mark_node;
22869 // Parse trailing expression specifiers.
22870 tree cs = cp_parser_constraint_specifier_seq (parser, expr);
22872 // Parse the optional trailing type requirement.
22873 tree type = NULL_TREE;
22874 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
22876 cp_lexer_consume_token (parser->lexer);
22877 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
22878 parser->in_result_type_constraint_p = true;
22879 type = cp_parser_trailing_type_id (parser);
22880 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
22881 if (type == error_mark_node)
22882 return error_mark_node;
22885 return finish_expr_requirement (expr, type, cs);
22888 // Parse a type requirement
22890 // type-requirement
22891 // type-id
22892 static tree
22893 cp_parser_type_requirement (cp_parser *parser)
22895 // Try parsing the type-id
22896 tree type = cp_parser_type_id (parser);
22897 if (type == error_mark_node)
22898 return error_mark_node;
22900 // It can only be a type requirement if nothing comes after it.
22901 // For example, this:
22903 // typename T::value_type x = a;
22905 // Is not a type requirement even though it stars with a type-id.
22906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22907 return error_mark_node;
22909 return finish_type_requirement (type);
22912 // Parse an optional constexpr specifier in a constraint expression.
22913 static tree
22914 cp_parser_constexpr_constraint_spec (cp_parser *parser, tree expr)
22916 cp_lexer_consume_token (parser->lexer);
22917 return finish_constexpr_requirement (expr);
22920 // Parse an optional noexcept specifier in a constraint expression.
22921 static tree
22922 cp_parser_noexcept_constraint_spec (cp_parser *parser, tree expr)
22924 cp_lexer_consume_token (parser->lexer);
22925 return finish_noexcept_requirement (expr);
22928 static tree
22929 cp_parser_constraint_spec (cp_parser *parser, tree expr)
22931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONSTEXPR))
22932 return cp_parser_constexpr_constraint_spec (parser, expr);
22933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
22934 return cp_parser_noexcept_constraint_spec (parser, expr);
22935 return NULL_TREE;
22938 // Parse an optional expression specifier sequence.
22940 // constraint-specifier-sequence:
22941 // constexpr [opt] noexcept [opt]
22942 static tree
22943 cp_parser_constraint_specifier_seq (cp_parser *parser, tree expr)
22945 tree result = NULL_TREE;
22946 while (1)
22948 // If we can parse a constraint specifier, insert it into
22949 // the list of requirements.
22950 if (tree spec = cp_parser_constraint_spec (parser, expr))
22952 result = tree_cons (NULL_TREE, spec, result);
22953 continue;
22955 break;
22957 return result;
22960 /* Support Functions */
22962 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22963 NAME should have one of the representations used for an
22964 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22965 is returned. If PARSER->SCOPE is a dependent type, then a
22966 SCOPE_REF is returned.
22968 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22969 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22970 was formed. Abstractly, such entities should not be passed to this
22971 function, because they do not need to be looked up, but it is
22972 simpler to check for this special case here, rather than at the
22973 call-sites.
22975 In cases not explicitly covered above, this function returns a
22976 DECL, OVERLOAD, or baselink representing the result of the lookup.
22977 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22978 is returned.
22980 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22981 (e.g., "struct") that was used. In that case bindings that do not
22982 refer to types are ignored.
22984 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22985 ignored.
22987 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22988 are ignored.
22990 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22991 types.
22993 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22994 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22995 NULL_TREE otherwise. */
22997 static tree
22998 cp_parser_lookup_name (cp_parser *parser, tree name,
22999 enum tag_types tag_type,
23000 bool is_template,
23001 bool is_namespace,
23002 bool check_dependency,
23003 tree *ambiguous_decls,
23004 location_t name_location)
23006 tree decl;
23007 tree object_type = parser->context->object_type;
23009 /* Assume that the lookup will be unambiguous. */
23010 if (ambiguous_decls)
23011 *ambiguous_decls = NULL_TREE;
23013 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
23014 no longer valid. Note that if we are parsing tentatively, and
23015 the parse fails, OBJECT_TYPE will be automatically restored. */
23016 parser->context->object_type = NULL_TREE;
23018 if (name == error_mark_node)
23019 return error_mark_node;
23021 /* A template-id has already been resolved; there is no lookup to
23022 do. */
23023 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
23024 return name;
23025 if (BASELINK_P (name))
23027 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
23028 == TEMPLATE_ID_EXPR);
23029 return name;
23032 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
23033 it should already have been checked to make sure that the name
23034 used matches the type being destroyed. */
23035 if (TREE_CODE (name) == BIT_NOT_EXPR)
23037 tree type;
23039 /* Figure out to which type this destructor applies. */
23040 if (parser->scope)
23041 type = parser->scope;
23042 else if (object_type)
23043 type = object_type;
23044 else
23045 type = current_class_type;
23046 /* If that's not a class type, there is no destructor. */
23047 if (!type || !CLASS_TYPE_P (type))
23048 return error_mark_node;
23049 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
23050 lazily_declare_fn (sfk_destructor, type);
23051 if (!CLASSTYPE_DESTRUCTORS (type))
23052 return error_mark_node;
23053 /* If it was a class type, return the destructor. */
23054 return CLASSTYPE_DESTRUCTORS (type);
23057 /* By this point, the NAME should be an ordinary identifier. If
23058 the id-expression was a qualified name, the qualifying scope is
23059 stored in PARSER->SCOPE at this point. */
23060 gcc_assert (identifier_p (name));
23062 /* Perform the lookup. */
23063 if (parser->scope)
23065 bool dependent_p;
23067 if (parser->scope == error_mark_node)
23068 return error_mark_node;
23070 /* If the SCOPE is dependent, the lookup must be deferred until
23071 the template is instantiated -- unless we are explicitly
23072 looking up names in uninstantiated templates. Even then, we
23073 cannot look up the name if the scope is not a class type; it
23074 might, for example, be a template type parameter. */
23075 dependent_p = (TYPE_P (parser->scope)
23076 && dependent_scope_p (parser->scope));
23077 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
23078 && dependent_p)
23079 /* Defer lookup. */
23080 decl = error_mark_node;
23081 else
23083 tree pushed_scope = NULL_TREE;
23085 /* If PARSER->SCOPE is a dependent type, then it must be a
23086 class type, and we must not be checking dependencies;
23087 otherwise, we would have processed this lookup above. So
23088 that PARSER->SCOPE is not considered a dependent base by
23089 lookup_member, we must enter the scope here. */
23090 if (dependent_p)
23091 pushed_scope = push_scope (parser->scope);
23093 /* If the PARSER->SCOPE is a template specialization, it
23094 may be instantiated during name lookup. In that case,
23095 errors may be issued. Even if we rollback the current
23096 tentative parse, those errors are valid. */
23097 decl = lookup_qualified_name (parser->scope, name,
23098 tag_type != none_type,
23099 /*complain=*/true);
23101 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
23102 lookup result and the nested-name-specifier nominates a class C:
23103 * if the name specified after the nested-name-specifier, when
23104 looked up in C, is the injected-class-name of C (Clause 9), or
23105 * if the name specified after the nested-name-specifier is the
23106 same as the identifier or the simple-template-id's template-
23107 name in the last component of the nested-name-specifier,
23108 the name is instead considered to name the constructor of
23109 class C. [ Note: for example, the constructor is not an
23110 acceptable lookup result in an elaborated-type-specifier so
23111 the constructor would not be used in place of the
23112 injected-class-name. --end note ] Such a constructor name
23113 shall be used only in the declarator-id of a declaration that
23114 names a constructor or in a using-declaration. */
23115 if (tag_type == none_type
23116 && DECL_SELF_REFERENCE_P (decl)
23117 && same_type_p (DECL_CONTEXT (decl), parser->scope))
23118 decl = lookup_qualified_name (parser->scope, ctor_identifier,
23119 tag_type != none_type,
23120 /*complain=*/true);
23122 /* If we have a single function from a using decl, pull it out. */
23123 if (TREE_CODE (decl) == OVERLOAD
23124 && !really_overloaded_fn (decl))
23125 decl = OVL_FUNCTION (decl);
23127 if (pushed_scope)
23128 pop_scope (pushed_scope);
23131 /* If the scope is a dependent type and either we deferred lookup or
23132 we did lookup but didn't find the name, rememeber the name. */
23133 if (decl == error_mark_node && TYPE_P (parser->scope)
23134 && dependent_type_p (parser->scope))
23136 if (tag_type)
23138 tree type;
23140 /* The resolution to Core Issue 180 says that `struct
23141 A::B' should be considered a type-name, even if `A'
23142 is dependent. */
23143 type = make_typename_type (parser->scope, name, tag_type,
23144 /*complain=*/tf_error);
23145 if (type != error_mark_node)
23146 decl = TYPE_NAME (type);
23148 else if (is_template
23149 && (cp_parser_next_token_ends_template_argument_p (parser)
23150 || cp_lexer_next_token_is (parser->lexer,
23151 CPP_CLOSE_PAREN)))
23152 decl = make_unbound_class_template (parser->scope,
23153 name, NULL_TREE,
23154 /*complain=*/tf_error);
23155 else
23156 decl = build_qualified_name (/*type=*/NULL_TREE,
23157 parser->scope, name,
23158 is_template);
23160 parser->qualifying_scope = parser->scope;
23161 parser->object_scope = NULL_TREE;
23163 else if (object_type)
23165 /* Look up the name in the scope of the OBJECT_TYPE, unless the
23166 OBJECT_TYPE is not a class. */
23167 if (CLASS_TYPE_P (object_type))
23168 /* If the OBJECT_TYPE is a template specialization, it may
23169 be instantiated during name lookup. In that case, errors
23170 may be issued. Even if we rollback the current tentative
23171 parse, those errors are valid. */
23172 decl = lookup_member (object_type,
23173 name,
23174 /*protect=*/0,
23175 tag_type != none_type,
23176 tf_warning_or_error);
23177 else
23178 decl = NULL_TREE;
23180 if (!decl)
23181 /* Look it up in the enclosing context. */
23182 decl = lookup_name_real (name, tag_type != none_type,
23183 /*nonclass=*/0,
23184 /*block_p=*/true, is_namespace, 0);
23185 parser->object_scope = object_type;
23186 parser->qualifying_scope = NULL_TREE;
23188 else
23190 decl = lookup_name_real (name, tag_type != none_type,
23191 /*nonclass=*/0,
23192 /*block_p=*/true, is_namespace, 0);
23193 parser->qualifying_scope = NULL_TREE;
23194 parser->object_scope = NULL_TREE;
23197 /* If the lookup failed, let our caller know. */
23198 if (!decl || decl == error_mark_node)
23199 return error_mark_node;
23201 /* Pull out the template from an injected-class-name (or multiple). */
23202 if (is_template)
23203 decl = maybe_get_template_decl_from_type_decl (decl);
23205 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23206 if (TREE_CODE (decl) == TREE_LIST)
23208 if (ambiguous_decls)
23209 *ambiguous_decls = decl;
23210 /* The error message we have to print is too complicated for
23211 cp_parser_error, so we incorporate its actions directly. */
23212 if (!cp_parser_simulate_error (parser))
23214 error_at (name_location, "reference to %qD is ambiguous",
23215 name);
23216 print_candidates (decl);
23218 return error_mark_node;
23221 gcc_assert (DECL_P (decl)
23222 || TREE_CODE (decl) == OVERLOAD
23223 || TREE_CODE (decl) == SCOPE_REF
23224 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23225 || BASELINK_P (decl));
23227 /* If we have resolved the name of a member declaration, check to
23228 see if the declaration is accessible. When the name resolves to
23229 set of overloaded functions, accessibility is checked when
23230 overload resolution is done.
23232 During an explicit instantiation, access is not checked at all,
23233 as per [temp.explicit]. */
23234 if (DECL_P (decl))
23235 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23237 maybe_record_typedef_use (decl);
23239 return decl;
23242 /* Like cp_parser_lookup_name, but for use in the typical case where
23243 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23244 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23246 static tree
23247 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23249 return cp_parser_lookup_name (parser, name,
23250 none_type,
23251 /*is_template=*/false,
23252 /*is_namespace=*/false,
23253 /*check_dependency=*/true,
23254 /*ambiguous_decls=*/NULL,
23255 location);
23258 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23259 the current context, return the TYPE_DECL. If TAG_NAME_P is
23260 true, the DECL indicates the class being defined in a class-head,
23261 or declared in an elaborated-type-specifier.
23263 Otherwise, return DECL. */
23265 static tree
23266 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23268 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23269 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23271 struct A {
23272 template <typename T> struct B;
23275 template <typename T> struct A::B {};
23277 Similarly, in an elaborated-type-specifier:
23279 namespace N { struct X{}; }
23281 struct A {
23282 template <typename T> friend struct N::X;
23285 However, if the DECL refers to a class type, and we are in
23286 the scope of the class, then the name lookup automatically
23287 finds the TYPE_DECL created by build_self_reference rather
23288 than a TEMPLATE_DECL. For example, in:
23290 template <class T> struct S {
23291 S s;
23294 there is no need to handle such case. */
23296 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23297 return DECL_TEMPLATE_RESULT (decl);
23299 return decl;
23302 /* If too many, or too few, template-parameter lists apply to the
23303 declarator, issue an error message. Returns TRUE if all went well,
23304 and FALSE otherwise. */
23306 static bool
23307 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23308 cp_declarator *declarator,
23309 location_t declarator_location)
23311 switch (declarator->kind)
23313 case cdk_id:
23315 unsigned num_templates = 0;
23316 tree scope = declarator->u.id.qualifying_scope;
23318 if (scope)
23319 num_templates = num_template_headers_for_class (scope);
23320 else if (TREE_CODE (declarator->u.id.unqualified_name)
23321 == TEMPLATE_ID_EXPR)
23322 /* If the DECLARATOR has the form `X<y>' then it uses one
23323 additional level of template parameters. */
23324 ++num_templates;
23326 return cp_parser_check_template_parameters
23327 (parser, num_templates, declarator_location, declarator);
23330 case cdk_function:
23331 case cdk_array:
23332 case cdk_pointer:
23333 case cdk_reference:
23334 case cdk_ptrmem:
23335 return (cp_parser_check_declarator_template_parameters
23336 (parser, declarator->declarator, declarator_location));
23338 case cdk_error:
23339 return true;
23341 default:
23342 gcc_unreachable ();
23344 return false;
23347 /* NUM_TEMPLATES were used in the current declaration. If that is
23348 invalid, return FALSE and issue an error messages. Otherwise,
23349 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23350 declarator and we can print more accurate diagnostics. */
23352 static bool
23353 cp_parser_check_template_parameters (cp_parser* parser,
23354 unsigned num_templates,
23355 location_t location,
23356 cp_declarator *declarator)
23358 /* If there are the same number of template classes and parameter
23359 lists, that's OK. */
23360 if (parser->num_template_parameter_lists == num_templates)
23361 return true;
23362 /* If there are more, but only one more, then we are referring to a
23363 member template. That's OK too. */
23364 if (parser->num_template_parameter_lists == num_templates + 1)
23365 return true;
23366 /* If there are more template classes than parameter lists, we have
23367 something like:
23369 template <class T> void S<T>::R<T>::f (); */
23370 if (parser->num_template_parameter_lists < num_templates)
23372 if (declarator && !current_function_decl)
23373 error_at (location, "specializing member %<%T::%E%> "
23374 "requires %<template<>%> syntax",
23375 declarator->u.id.qualifying_scope,
23376 declarator->u.id.unqualified_name);
23377 else if (declarator)
23378 error_at (location, "invalid declaration of %<%T::%E%>",
23379 declarator->u.id.qualifying_scope,
23380 declarator->u.id.unqualified_name);
23381 else
23382 error_at (location, "too few template-parameter-lists");
23383 return false;
23385 /* Otherwise, there are too many template parameter lists. We have
23386 something like:
23388 template <class T> template <class U> void S::f(); */
23389 error_at (location, "too many template-parameter-lists");
23390 return false;
23393 /* Parse an optional `::' token indicating that the following name is
23394 from the global namespace. If so, PARSER->SCOPE is set to the
23395 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23396 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23397 Returns the new value of PARSER->SCOPE, if the `::' token is
23398 present, and NULL_TREE otherwise. */
23400 static tree
23401 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23403 cp_token *token;
23405 /* Peek at the next token. */
23406 token = cp_lexer_peek_token (parser->lexer);
23407 /* If we're looking at a `::' token then we're starting from the
23408 global namespace, not our current location. */
23409 if (token->type == CPP_SCOPE)
23411 /* Consume the `::' token. */
23412 cp_lexer_consume_token (parser->lexer);
23413 /* Set the SCOPE so that we know where to start the lookup. */
23414 parser->scope = global_namespace;
23415 parser->qualifying_scope = global_namespace;
23416 parser->object_scope = NULL_TREE;
23418 return parser->scope;
23420 else if (!current_scope_valid_p)
23422 parser->scope = NULL_TREE;
23423 parser->qualifying_scope = NULL_TREE;
23424 parser->object_scope = NULL_TREE;
23427 return NULL_TREE;
23430 /* Returns TRUE if the upcoming token sequence is the start of a
23431 constructor declarator. If FRIEND_P is true, the declarator is
23432 preceded by the `friend' specifier. */
23434 static bool
23435 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23437 bool constructor_p;
23438 bool outside_class_specifier_p;
23439 tree nested_name_specifier;
23440 cp_token *next_token;
23442 /* The common case is that this is not a constructor declarator, so
23443 try to avoid doing lots of work if at all possible. It's not
23444 valid declare a constructor at function scope. */
23445 if (parser->in_function_body)
23446 return false;
23447 /* And only certain tokens can begin a constructor declarator. */
23448 next_token = cp_lexer_peek_token (parser->lexer);
23449 if (next_token->type != CPP_NAME
23450 && next_token->type != CPP_SCOPE
23451 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23452 && next_token->type != CPP_TEMPLATE_ID)
23453 return false;
23455 /* Parse tentatively; we are going to roll back all of the tokens
23456 consumed here. */
23457 cp_parser_parse_tentatively (parser);
23458 /* Assume that we are looking at a constructor declarator. */
23459 constructor_p = true;
23461 /* Look for the optional `::' operator. */
23462 cp_parser_global_scope_opt (parser,
23463 /*current_scope_valid_p=*/false);
23464 /* Look for the nested-name-specifier. */
23465 nested_name_specifier
23466 = (cp_parser_nested_name_specifier_opt (parser,
23467 /*typename_keyword_p=*/false,
23468 /*check_dependency_p=*/false,
23469 /*type_p=*/false,
23470 /*is_declaration=*/false));
23472 outside_class_specifier_p = (!at_class_scope_p ()
23473 || !TYPE_BEING_DEFINED (current_class_type)
23474 || friend_p);
23476 /* Outside of a class-specifier, there must be a
23477 nested-name-specifier. */
23478 if (!nested_name_specifier && outside_class_specifier_p)
23479 constructor_p = false;
23480 else if (nested_name_specifier == error_mark_node)
23481 constructor_p = false;
23483 /* If we have a class scope, this is easy; DR 147 says that S::S always
23484 names the constructor, and no other qualified name could. */
23485 if (constructor_p && nested_name_specifier
23486 && CLASS_TYPE_P (nested_name_specifier))
23488 tree id = cp_parser_unqualified_id (parser,
23489 /*template_keyword_p=*/false,
23490 /*check_dependency_p=*/false,
23491 /*declarator_p=*/true,
23492 /*optional_p=*/false);
23493 if (is_overloaded_fn (id))
23494 id = DECL_NAME (get_first_fn (id));
23495 if (!constructor_name_p (id, nested_name_specifier))
23496 constructor_p = false;
23498 /* If we still think that this might be a constructor-declarator,
23499 look for a class-name. */
23500 else if (constructor_p)
23502 /* If we have:
23504 template <typename T> struct S {
23505 S();
23508 we must recognize that the nested `S' names a class. */
23509 tree type_decl;
23510 type_decl = cp_parser_class_name (parser,
23511 /*typename_keyword_p=*/false,
23512 /*template_keyword_p=*/false,
23513 none_type,
23514 /*check_dependency_p=*/false,
23515 /*class_head_p=*/false,
23516 /*is_declaration=*/false);
23517 /* If there was no class-name, then this is not a constructor.
23518 Otherwise, if we are in a class-specifier and we aren't
23519 handling a friend declaration, check that its type matches
23520 current_class_type (c++/38313). Note: error_mark_node
23521 is left alone for error recovery purposes. */
23522 constructor_p = (!cp_parser_error_occurred (parser)
23523 && (outside_class_specifier_p
23524 || type_decl == error_mark_node
23525 || same_type_p (current_class_type,
23526 TREE_TYPE (type_decl))));
23528 /* If we're still considering a constructor, we have to see a `(',
23529 to begin the parameter-declaration-clause, followed by either a
23530 `)', an `...', or a decl-specifier. We need to check for a
23531 type-specifier to avoid being fooled into thinking that:
23533 S (f) (int);
23535 is a constructor. (It is actually a function named `f' that
23536 takes one parameter (of type `int') and returns a value of type
23537 `S'. */
23538 if (constructor_p
23539 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23540 constructor_p = false;
23542 if (constructor_p
23543 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23544 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23545 /* A parameter declaration begins with a decl-specifier,
23546 which is either the "attribute" keyword, a storage class
23547 specifier, or (usually) a type-specifier. */
23548 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23550 tree type;
23551 tree pushed_scope = NULL_TREE;
23552 unsigned saved_num_template_parameter_lists;
23554 /* Names appearing in the type-specifier should be looked up
23555 in the scope of the class. */
23556 if (current_class_type)
23557 type = NULL_TREE;
23558 else
23560 type = TREE_TYPE (type_decl);
23561 if (TREE_CODE (type) == TYPENAME_TYPE)
23563 type = resolve_typename_type (type,
23564 /*only_current_p=*/false);
23565 if (TREE_CODE (type) == TYPENAME_TYPE)
23567 cp_parser_abort_tentative_parse (parser);
23568 return false;
23571 pushed_scope = push_scope (type);
23574 /* Inside the constructor parameter list, surrounding
23575 template-parameter-lists do not apply. */
23576 saved_num_template_parameter_lists
23577 = parser->num_template_parameter_lists;
23578 parser->num_template_parameter_lists = 0;
23580 /* Look for the type-specifier. */
23581 cp_parser_type_specifier (parser,
23582 CP_PARSER_FLAGS_NONE,
23583 /*decl_specs=*/NULL,
23584 /*is_declarator=*/true,
23585 /*declares_class_or_enum=*/NULL,
23586 /*is_cv_qualifier=*/NULL);
23588 parser->num_template_parameter_lists
23589 = saved_num_template_parameter_lists;
23591 /* Leave the scope of the class. */
23592 if (pushed_scope)
23593 pop_scope (pushed_scope);
23595 constructor_p = !cp_parser_error_occurred (parser);
23599 /* We did not really want to consume any tokens. */
23600 cp_parser_abort_tentative_parse (parser);
23602 return constructor_p;
23605 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23606 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23607 they must be performed once we are in the scope of the function.
23609 Returns the function defined. */
23611 static tree
23612 cp_parser_function_definition_from_specifiers_and_declarator
23613 (cp_parser* parser,
23614 cp_decl_specifier_seq *decl_specifiers,
23615 tree attributes,
23616 const cp_declarator *declarator)
23618 tree fn;
23619 bool success_p;
23621 /* Begin the function-definition. */
23622 success_p = start_function (decl_specifiers, declarator, attributes);
23624 /* The things we're about to see are not directly qualified by any
23625 template headers we've seen thus far. */
23626 reset_specialization ();
23628 /* If there were names looked up in the decl-specifier-seq that we
23629 did not check, check them now. We must wait until we are in the
23630 scope of the function to perform the checks, since the function
23631 might be a friend. */
23632 perform_deferred_access_checks (tf_warning_or_error);
23634 if (success_p)
23636 cp_finalize_omp_declare_simd (parser, current_function_decl);
23637 parser->omp_declare_simd = NULL;
23640 if (!success_p)
23642 /* Skip the entire function. */
23643 cp_parser_skip_to_end_of_block_or_statement (parser);
23644 fn = error_mark_node;
23646 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23648 /* Seen already, skip it. An error message has already been output. */
23649 cp_parser_skip_to_end_of_block_or_statement (parser);
23650 fn = current_function_decl;
23651 current_function_decl = NULL_TREE;
23652 /* If this is a function from a class, pop the nested class. */
23653 if (current_class_name)
23654 pop_nested_class ();
23656 else
23658 timevar_id_t tv;
23659 if (DECL_DECLARED_INLINE_P (current_function_decl))
23660 tv = TV_PARSE_INLINE;
23661 else
23662 tv = TV_PARSE_FUNC;
23663 timevar_push (tv);
23664 fn = cp_parser_function_definition_after_declarator (parser,
23665 /*inline_p=*/false);
23666 timevar_pop (tv);
23669 return fn;
23672 /* Parse the part of a function-definition that follows the
23673 declarator. INLINE_P is TRUE iff this function is an inline
23674 function defined within a class-specifier.
23676 Returns the function defined. */
23678 static tree
23679 cp_parser_function_definition_after_declarator (cp_parser* parser,
23680 bool inline_p)
23682 tree fn;
23683 bool ctor_initializer_p = false;
23684 bool saved_in_unbraced_linkage_specification_p;
23685 bool saved_in_function_body;
23686 unsigned saved_num_template_parameter_lists;
23687 cp_token *token;
23688 bool fully_implicit_function_template_p
23689 = parser->fully_implicit_function_template_p;
23690 parser->fully_implicit_function_template_p = false;
23691 tree implicit_template_parms
23692 = parser->implicit_template_parms;
23693 parser->implicit_template_parms = 0;
23694 cp_binding_level* implicit_template_scope
23695 = parser->implicit_template_scope;
23696 parser->implicit_template_scope = 0;
23698 saved_in_function_body = parser->in_function_body;
23699 parser->in_function_body = true;
23700 /* If the next token is `return', then the code may be trying to
23701 make use of the "named return value" extension that G++ used to
23702 support. */
23703 token = cp_lexer_peek_token (parser->lexer);
23704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23706 /* Consume the `return' keyword. */
23707 cp_lexer_consume_token (parser->lexer);
23708 /* Look for the identifier that indicates what value is to be
23709 returned. */
23710 cp_parser_identifier (parser);
23711 /* Issue an error message. */
23712 error_at (token->location,
23713 "named return values are no longer supported");
23714 /* Skip tokens until we reach the start of the function body. */
23715 while (true)
23717 cp_token *token = cp_lexer_peek_token (parser->lexer);
23718 if (token->type == CPP_OPEN_BRACE
23719 || token->type == CPP_EOF
23720 || token->type == CPP_PRAGMA_EOL)
23721 break;
23722 cp_lexer_consume_token (parser->lexer);
23725 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23726 anything declared inside `f'. */
23727 saved_in_unbraced_linkage_specification_p
23728 = parser->in_unbraced_linkage_specification_p;
23729 parser->in_unbraced_linkage_specification_p = false;
23730 /* Inside the function, surrounding template-parameter-lists do not
23731 apply. */
23732 saved_num_template_parameter_lists
23733 = parser->num_template_parameter_lists;
23734 parser->num_template_parameter_lists = 0;
23736 start_lambda_scope (current_function_decl);
23738 /* If the next token is `try', `__transaction_atomic', or
23739 `__transaction_relaxed`, then we are looking at either function-try-block
23740 or function-transaction-block. Note that all of these include the
23741 function-body. */
23742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23743 ctor_initializer_p = cp_parser_function_transaction (parser,
23744 RID_TRANSACTION_ATOMIC);
23745 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23746 RID_TRANSACTION_RELAXED))
23747 ctor_initializer_p = cp_parser_function_transaction (parser,
23748 RID_TRANSACTION_RELAXED);
23749 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23750 ctor_initializer_p = cp_parser_function_try_block (parser);
23751 else
23752 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23753 (parser, /*in_function_try_block=*/false);
23755 finish_lambda_scope ();
23757 /* Finish the function. */
23758 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23759 (inline_p ? 2 : 0));
23760 /* Generate code for it, if necessary. */
23761 expand_or_defer_fn (fn);
23762 /* Restore the saved values. */
23763 parser->in_unbraced_linkage_specification_p
23764 = saved_in_unbraced_linkage_specification_p;
23765 parser->num_template_parameter_lists
23766 = saved_num_template_parameter_lists;
23767 parser->in_function_body = saved_in_function_body;
23769 parser->fully_implicit_function_template_p
23770 = fully_implicit_function_template_p;
23771 parser->implicit_template_parms
23772 = implicit_template_parms;
23773 parser->implicit_template_scope
23774 = implicit_template_scope;
23776 if (parser->fully_implicit_function_template_p)
23777 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23779 return fn;
23782 /* Parse a template-declaration, assuming that the `export' (and
23783 `extern') keywords, if present, has already been scanned. MEMBER_P
23784 is as for cp_parser_template_declaration. */
23786 static void
23787 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23789 tree decl = NULL_TREE;
23790 vec<deferred_access_check, va_gc> *checks;
23791 tree parameter_list;
23792 bool friend_p = false;
23793 bool need_lang_pop;
23794 cp_token *token;
23796 /* Look for the `template' keyword. */
23797 token = cp_lexer_peek_token (parser->lexer);
23798 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23799 return;
23801 /* And the `<'. */
23802 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23803 return;
23804 if (at_class_scope_p () && current_function_decl)
23806 /* 14.5.2.2 [temp.mem]
23808 A local class shall not have member templates. */
23809 error_at (token->location,
23810 "invalid declaration of member template in local class");
23811 cp_parser_skip_to_end_of_block_or_statement (parser);
23812 return;
23814 /* [temp]
23816 A template ... shall not have C linkage. */
23817 if (current_lang_name == lang_name_c)
23819 error_at (token->location, "template with C linkage");
23820 /* Give it C++ linkage to avoid confusing other parts of the
23821 front end. */
23822 push_lang_context (lang_name_cplusplus);
23823 need_lang_pop = true;
23825 else
23826 need_lang_pop = false;
23828 /* We cannot perform access checks on the template parameter
23829 declarations until we know what is being declared, just as we
23830 cannot check the decl-specifier list. */
23831 push_deferring_access_checks (dk_deferred);
23833 // Save the current template requirements.
23834 tree saved_template_reqs = release (current_template_reqs);
23836 /* If the next token is `>', then we have an invalid
23837 specialization. Rather than complain about an invalid template
23838 parameter, issue an error message here. */
23839 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23841 cp_parser_error (parser, "invalid explicit specialization");
23842 begin_specialization ();
23843 parameter_list = NULL_TREE;
23845 else
23847 /* Parse the template parameters. */
23848 parameter_list = cp_parser_template_parameter_list (parser);
23851 /* Get the deferred access checks from the parameter list. These
23852 will be checked once we know what is being declared, as for a
23853 member template the checks must be performed in the scope of the
23854 class containing the member. */
23855 checks = get_deferred_access_checks ();
23857 /* Look for the `>'. */
23858 cp_parser_skip_to_end_of_template_parameter_list (parser);
23859 /* We just processed one more parameter list. */
23860 ++parser->num_template_parameter_lists;
23862 // Manage template requirements
23863 if (flag_concepts)
23865 tree reqs = get_shorthand_requirements (current_template_parms);
23866 if (tree r = cp_parser_requires_clause_opt (parser))
23867 reqs = conjoin_requirements (reqs, r);
23868 current_template_reqs = finish_template_requirements (reqs);
23870 // Attach the constraints to the template parameter list.
23871 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
23872 = current_template_reqs;
23875 /* If the next token is `template', there are more template
23876 parameters. */
23877 if (cp_lexer_next_token_is_keyword (parser->lexer,
23878 RID_TEMPLATE))
23879 cp_parser_template_declaration_after_export (parser, member_p);
23880 else if (cxx_dialect >= cxx11
23881 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23882 decl = cp_parser_alias_declaration (parser);
23883 else
23885 /* There are no access checks when parsing a template, as we do not
23886 know if a specialization will be a friend. */
23887 push_deferring_access_checks (dk_no_check);
23888 token = cp_lexer_peek_token (parser->lexer);
23889 decl = cp_parser_single_declaration (parser,
23890 checks,
23891 member_p,
23892 /*explicit_specialization_p=*/false,
23893 &friend_p);
23894 pop_deferring_access_checks ();
23896 /* If this is a member template declaration, let the front
23897 end know. */
23898 if (member_p && !friend_p && decl)
23900 if (TREE_CODE (decl) == TYPE_DECL)
23901 cp_parser_check_access_in_redeclaration (decl, token->location);
23903 decl = finish_member_template_decl (decl);
23905 else if (friend_p && decl
23906 && DECL_DECLARES_TYPE_P (decl))
23907 make_friend_class (current_class_type, TREE_TYPE (decl),
23908 /*complain=*/true);
23910 /* We are done with the current parameter list. */
23911 --parser->num_template_parameter_lists;
23913 pop_deferring_access_checks ();
23915 /* Finish up. */
23916 finish_template_decl (parameter_list);
23918 // Restore the current template requirements.
23919 current_template_reqs = saved_template_reqs;
23921 /* Check the template arguments for a literal operator template. */
23922 if (decl
23923 && DECL_DECLARES_FUNCTION_P (decl)
23924 && UDLIT_OPER_P (DECL_NAME (decl)))
23926 bool ok = true;
23927 if (parameter_list == NULL_TREE)
23928 ok = false;
23929 else
23931 int num_parms = TREE_VEC_LENGTH (parameter_list);
23932 if (num_parms == 1)
23934 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23935 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23936 if (TREE_TYPE (parm) != char_type_node
23937 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23938 ok = false;
23940 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23942 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23943 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23944 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23945 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23946 if (TREE_TYPE (parm) != TREE_TYPE (type)
23947 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23948 ok = false;
23950 else
23951 ok = false;
23953 if (!ok)
23954 error ("literal operator template %qD has invalid parameter list."
23955 " Expected non-type template argument pack <char...>"
23956 " or <typename CharT, CharT...>",
23957 decl);
23959 /* Register member declarations. */
23960 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23961 finish_member_declaration (decl);
23962 /* For the erroneous case of a template with C linkage, we pushed an
23963 implicit C++ linkage scope; exit that scope now. */
23964 if (need_lang_pop)
23965 pop_lang_context ();
23966 /* If DECL is a function template, we must return to parse it later.
23967 (Even though there is no definition, there might be default
23968 arguments that need handling.) */
23969 if (member_p && decl
23970 && DECL_DECLARES_FUNCTION_P (decl))
23971 vec_safe_push (unparsed_funs_with_definitions, decl);
23974 /* Perform the deferred access checks from a template-parameter-list.
23975 CHECKS is a TREE_LIST of access checks, as returned by
23976 get_deferred_access_checks. */
23978 static void
23979 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23981 ++processing_template_parmlist;
23982 perform_access_checks (checks, tf_warning_or_error);
23983 --processing_template_parmlist;
23986 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23987 `function-definition' sequence that follows a template header.
23988 If MEMBER_P is true, this declaration appears in a class scope.
23990 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23991 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23993 static tree
23994 cp_parser_single_declaration (cp_parser* parser,
23995 vec<deferred_access_check, va_gc> *checks,
23996 bool member_p,
23997 bool explicit_specialization_p,
23998 bool* friend_p)
24000 int declares_class_or_enum;
24001 tree decl = NULL_TREE;
24002 cp_decl_specifier_seq decl_specifiers;
24003 bool function_definition_p = false;
24004 cp_token *decl_spec_token_start;
24006 /* This function is only used when processing a template
24007 declaration. */
24008 gcc_assert (innermost_scope_kind () == sk_template_parms
24009 || innermost_scope_kind () == sk_template_spec);
24011 /* Defer access checks until we know what is being declared. */
24012 push_deferring_access_checks (dk_deferred);
24014 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
24015 alternative. */
24016 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24017 cp_parser_decl_specifier_seq (parser,
24018 CP_PARSER_FLAGS_OPTIONAL,
24019 &decl_specifiers,
24020 &declares_class_or_enum);
24021 if (friend_p)
24022 *friend_p = cp_parser_friend_p (&decl_specifiers);
24024 /* There are no template typedefs. */
24025 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
24027 error_at (decl_spec_token_start->location,
24028 "template declaration of %<typedef%>");
24029 decl = error_mark_node;
24032 /* Gather up the access checks that occurred the
24033 decl-specifier-seq. */
24034 stop_deferring_access_checks ();
24036 /* Check for the declaration of a template class. */
24037 if (declares_class_or_enum)
24039 if (cp_parser_declares_only_class_p (parser))
24041 decl = shadow_tag (&decl_specifiers);
24043 /* In this case:
24045 struct C {
24046 friend template <typename T> struct A<T>::B;
24049 A<T>::B will be represented by a TYPENAME_TYPE, and
24050 therefore not recognized by shadow_tag. */
24051 if (friend_p && *friend_p
24052 && !decl
24053 && decl_specifiers.type
24054 && TYPE_P (decl_specifiers.type))
24055 decl = decl_specifiers.type;
24057 if (decl && decl != error_mark_node)
24058 decl = TYPE_NAME (decl);
24059 else
24060 decl = error_mark_node;
24062 /* Perform access checks for template parameters. */
24063 cp_parser_perform_template_parameter_access_checks (checks);
24067 /* Complain about missing 'typename' or other invalid type names. */
24068 if (!decl_specifiers.any_type_specifiers_p
24069 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24071 /* cp_parser_parse_and_diagnose_invalid_type_name calls
24072 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
24073 the rest of this declaration. */
24074 decl = error_mark_node;
24075 goto out;
24078 /* If it's not a template class, try for a template function. If
24079 the next token is a `;', then this declaration does not declare
24080 anything. But, if there were errors in the decl-specifiers, then
24081 the error might well have come from an attempted class-specifier.
24082 In that case, there's no need to warn about a missing declarator. */
24083 if (!decl
24084 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
24085 || decl_specifiers.type != error_mark_node))
24087 decl = cp_parser_init_declarator (parser,
24088 &decl_specifiers,
24089 checks,
24090 /*function_definition_allowed_p=*/true,
24091 member_p,
24092 declares_class_or_enum,
24093 &function_definition_p,
24094 NULL);
24096 /* 7.1.1-1 [dcl.stc]
24098 A storage-class-specifier shall not be specified in an explicit
24099 specialization... */
24100 if (decl
24101 && explicit_specialization_p
24102 && decl_specifiers.storage_class != sc_none)
24104 error_at (decl_spec_token_start->location,
24105 "explicit template specialization cannot have a storage class");
24106 decl = error_mark_node;
24109 if (decl && VAR_P (decl))
24110 check_template_variable (decl);
24113 /* Look for a trailing `;' after the declaration. */
24114 if (!function_definition_p
24115 && (decl == error_mark_node
24116 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
24117 cp_parser_skip_to_end_of_block_or_statement (parser);
24119 out:
24120 pop_deferring_access_checks ();
24122 /* Clear any current qualification; whatever comes next is the start
24123 of something new. */
24124 parser->scope = NULL_TREE;
24125 parser->qualifying_scope = NULL_TREE;
24126 parser->object_scope = NULL_TREE;
24128 return decl;
24131 /* Parse a cast-expression that is not the operand of a unary "&". */
24133 static tree
24134 cp_parser_simple_cast_expression (cp_parser *parser)
24136 return cp_parser_cast_expression (parser, /*address_p=*/false,
24137 /*cast_p=*/false, /*decltype*/false, NULL);
24140 /* Parse a functional cast to TYPE. Returns an expression
24141 representing the cast. */
24143 static tree
24144 cp_parser_functional_cast (cp_parser* parser, tree type)
24146 vec<tree, va_gc> *vec;
24147 tree expression_list;
24148 tree cast;
24149 bool nonconst_p;
24151 if (!type)
24152 type = error_mark_node;
24154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24156 cp_lexer_set_source_position (parser->lexer);
24157 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24158 expression_list = cp_parser_braced_list (parser, &nonconst_p);
24159 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
24160 if (TREE_CODE (type) == TYPE_DECL)
24161 type = TREE_TYPE (type);
24162 return finish_compound_literal (type, expression_list,
24163 tf_warning_or_error);
24167 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24168 /*cast_p=*/true,
24169 /*allow_expansion_p=*/true,
24170 /*non_constant_p=*/NULL);
24171 if (vec == NULL)
24172 expression_list = error_mark_node;
24173 else
24175 expression_list = build_tree_list_vec (vec);
24176 release_tree_vector (vec);
24179 cast = build_functional_cast (type, expression_list,
24180 tf_warning_or_error);
24181 /* [expr.const]/1: In an integral constant expression "only type
24182 conversions to integral or enumeration type can be used". */
24183 if (TREE_CODE (type) == TYPE_DECL)
24184 type = TREE_TYPE (type);
24185 if (cast != error_mark_node
24186 && !cast_valid_in_integral_constant_expression_p (type)
24187 && cp_parser_non_integral_constant_expression (parser,
24188 NIC_CONSTRUCTOR))
24189 return error_mark_node;
24190 return cast;
24193 /* Save the tokens that make up the body of a member function defined
24194 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24195 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24196 specifiers applied to the declaration. Returns the FUNCTION_DECL
24197 for the member function. */
24199 static tree
24200 cp_parser_save_member_function_body (cp_parser* parser,
24201 cp_decl_specifier_seq *decl_specifiers,
24202 cp_declarator *declarator,
24203 tree attributes)
24205 cp_token *first;
24206 cp_token *last;
24207 tree fn;
24209 /* Create the FUNCTION_DECL. */
24210 fn = grokmethod (decl_specifiers, declarator, attributes);
24211 cp_finalize_omp_declare_simd (parser, fn);
24212 /* If something went badly wrong, bail out now. */
24213 if (fn == error_mark_node)
24215 /* If there's a function-body, skip it. */
24216 if (cp_parser_token_starts_function_definition_p
24217 (cp_lexer_peek_token (parser->lexer)))
24218 cp_parser_skip_to_end_of_block_or_statement (parser);
24219 return error_mark_node;
24222 /* Remember it, if there default args to post process. */
24223 cp_parser_save_default_args (parser, fn);
24225 /* Save away the tokens that make up the body of the
24226 function. */
24227 first = parser->lexer->next_token;
24228 /* Handle function try blocks. */
24229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24230 cp_lexer_consume_token (parser->lexer);
24231 /* We can have braced-init-list mem-initializers before the fn body. */
24232 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24234 cp_lexer_consume_token (parser->lexer);
24235 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24237 /* cache_group will stop after an un-nested { } pair, too. */
24238 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24239 break;
24241 /* variadic mem-inits have ... after the ')'. */
24242 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24243 cp_lexer_consume_token (parser->lexer);
24246 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24247 /* Handle function try blocks. */
24248 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24249 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24250 last = parser->lexer->next_token;
24252 /* Save away the inline definition; we will process it when the
24253 class is complete. */
24254 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24255 DECL_PENDING_INLINE_P (fn) = 1;
24257 /* We need to know that this was defined in the class, so that
24258 friend templates are handled correctly. */
24259 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24261 /* Add FN to the queue of functions to be parsed later. */
24262 vec_safe_push (unparsed_funs_with_definitions, fn);
24264 return fn;
24267 /* Save the tokens that make up the in-class initializer for a non-static
24268 data member. Returns a DEFAULT_ARG. */
24270 static tree
24271 cp_parser_save_nsdmi (cp_parser* parser)
24273 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24276 /* Parse a template-argument-list, as well as the trailing ">" (but
24277 not the opening "<"). See cp_parser_template_argument_list for the
24278 return value. */
24280 static tree
24281 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24283 tree arguments;
24284 tree saved_scope;
24285 tree saved_qualifying_scope;
24286 tree saved_object_scope;
24287 bool saved_greater_than_is_operator_p;
24288 int saved_unevaluated_operand;
24289 int saved_inhibit_evaluation_warnings;
24291 /* [temp.names]
24293 When parsing a template-id, the first non-nested `>' is taken as
24294 the end of the template-argument-list rather than a greater-than
24295 operator. */
24296 saved_greater_than_is_operator_p
24297 = parser->greater_than_is_operator_p;
24298 parser->greater_than_is_operator_p = false;
24299 /* Parsing the argument list may modify SCOPE, so we save it
24300 here. */
24301 saved_scope = parser->scope;
24302 saved_qualifying_scope = parser->qualifying_scope;
24303 saved_object_scope = parser->object_scope;
24304 /* We need to evaluate the template arguments, even though this
24305 template-id may be nested within a "sizeof". */
24306 saved_unevaluated_operand = cp_unevaluated_operand;
24307 cp_unevaluated_operand = 0;
24308 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24309 c_inhibit_evaluation_warnings = 0;
24310 /* Parse the template-argument-list itself. */
24311 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24312 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24313 arguments = NULL_TREE;
24314 else
24315 arguments = cp_parser_template_argument_list (parser);
24316 /* Look for the `>' that ends the template-argument-list. If we find
24317 a '>>' instead, it's probably just a typo. */
24318 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24320 if (cxx_dialect != cxx98)
24322 /* In C++0x, a `>>' in a template argument list or cast
24323 expression is considered to be two separate `>'
24324 tokens. So, change the current token to a `>', but don't
24325 consume it: it will be consumed later when the outer
24326 template argument list (or cast expression) is parsed.
24327 Note that this replacement of `>' for `>>' is necessary
24328 even if we are parsing tentatively: in the tentative
24329 case, after calling
24330 cp_parser_enclosed_template_argument_list we will always
24331 throw away all of the template arguments and the first
24332 closing `>', either because the template argument list
24333 was erroneous or because we are replacing those tokens
24334 with a CPP_TEMPLATE_ID token. The second `>' (which will
24335 not have been thrown away) is needed either to close an
24336 outer template argument list or to complete a new-style
24337 cast. */
24338 cp_token *token = cp_lexer_peek_token (parser->lexer);
24339 token->type = CPP_GREATER;
24341 else if (!saved_greater_than_is_operator_p)
24343 /* If we're in a nested template argument list, the '>>' has
24344 to be a typo for '> >'. We emit the error message, but we
24345 continue parsing and we push a '>' as next token, so that
24346 the argument list will be parsed correctly. Note that the
24347 global source location is still on the token before the
24348 '>>', so we need to say explicitly where we want it. */
24349 cp_token *token = cp_lexer_peek_token (parser->lexer);
24350 error_at (token->location, "%<>>%> should be %<> >%> "
24351 "within a nested template argument list");
24353 token->type = CPP_GREATER;
24355 else
24357 /* If this is not a nested template argument list, the '>>'
24358 is a typo for '>'. Emit an error message and continue.
24359 Same deal about the token location, but here we can get it
24360 right by consuming the '>>' before issuing the diagnostic. */
24361 cp_token *token = cp_lexer_consume_token (parser->lexer);
24362 error_at (token->location,
24363 "spurious %<>>%>, use %<>%> to terminate "
24364 "a template argument list");
24367 else
24368 cp_parser_skip_to_end_of_template_parameter_list (parser);
24369 /* The `>' token might be a greater-than operator again now. */
24370 parser->greater_than_is_operator_p
24371 = saved_greater_than_is_operator_p;
24372 /* Restore the SAVED_SCOPE. */
24373 parser->scope = saved_scope;
24374 parser->qualifying_scope = saved_qualifying_scope;
24375 parser->object_scope = saved_object_scope;
24376 cp_unevaluated_operand = saved_unevaluated_operand;
24377 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24379 return arguments;
24382 /* MEMBER_FUNCTION is a member function, or a friend. If default
24383 arguments, or the body of the function have not yet been parsed,
24384 parse them now. */
24386 static void
24387 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24389 timevar_push (TV_PARSE_INMETH);
24390 /* If this member is a template, get the underlying
24391 FUNCTION_DECL. */
24392 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24393 member_function = DECL_TEMPLATE_RESULT (member_function);
24395 /* There should not be any class definitions in progress at this
24396 point; the bodies of members are only parsed outside of all class
24397 definitions. */
24398 gcc_assert (parser->num_classes_being_defined == 0);
24399 /* While we're parsing the member functions we might encounter more
24400 classes. We want to handle them right away, but we don't want
24401 them getting mixed up with functions that are currently in the
24402 queue. */
24403 push_unparsed_function_queues (parser);
24405 /* Make sure that any template parameters are in scope. */
24406 maybe_begin_member_template_processing (member_function);
24408 // Restore the declaration's requirements for the parsing of
24409 // the definition.
24411 tree saved_template_reqs = release (current_template_reqs);
24412 current_template_reqs = get_constraints (member_function);
24414 /* If the body of the function has not yet been parsed, parse it
24415 now. */
24416 if (DECL_PENDING_INLINE_P (member_function))
24418 tree function_scope;
24419 cp_token_cache *tokens;
24421 /* The function is no longer pending; we are processing it. */
24422 tokens = DECL_PENDING_INLINE_INFO (member_function);
24423 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24424 DECL_PENDING_INLINE_P (member_function) = 0;
24426 /* If this is a local class, enter the scope of the containing
24427 function. */
24428 function_scope = current_function_decl;
24429 if (function_scope)
24430 push_function_context ();
24432 /* Push the body of the function onto the lexer stack. */
24433 cp_parser_push_lexer_for_tokens (parser, tokens);
24435 /* Let the front end know that we going to be defining this
24436 function. */
24437 start_preparsed_function (member_function, NULL_TREE,
24438 SF_PRE_PARSED | SF_INCLASS_INLINE);
24440 /* Don't do access checking if it is a templated function. */
24441 if (processing_template_decl)
24442 push_deferring_access_checks (dk_no_check);
24444 /* #pragma omp declare reduction needs special parsing. */
24445 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24447 parser->lexer->in_pragma = true;
24448 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24449 finish_function (/*inline*/2);
24450 cp_check_omp_declare_reduction (member_function);
24452 else
24453 /* Now, parse the body of the function. */
24454 cp_parser_function_definition_after_declarator (parser,
24455 /*inline_p=*/true);
24457 if (processing_template_decl)
24458 pop_deferring_access_checks ();
24460 /* Leave the scope of the containing function. */
24461 if (function_scope)
24462 pop_function_context ();
24463 cp_parser_pop_lexer (parser);
24466 /* Remove any template parameters from the symbol table. */
24467 maybe_end_member_template_processing ();
24469 // Restore the template requirements.
24470 current_template_reqs = saved_template_reqs;
24472 /* Restore the queue. */
24473 pop_unparsed_function_queues (parser);
24474 timevar_pop (TV_PARSE_INMETH);
24477 /* If DECL contains any default args, remember it on the unparsed
24478 functions queue. */
24480 static void
24481 cp_parser_save_default_args (cp_parser* parser, tree decl)
24483 tree probe;
24485 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24486 probe;
24487 probe = TREE_CHAIN (probe))
24488 if (TREE_PURPOSE (probe))
24490 cp_default_arg_entry entry = {current_class_type, decl};
24491 vec_safe_push (unparsed_funs_with_default_args, entry);
24492 break;
24496 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24497 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24498 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24499 from the parameter-type-list. */
24501 static tree
24502 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24503 tree default_arg, tree parmtype)
24505 cp_token_cache *tokens;
24506 tree parsed_arg;
24507 bool dummy;
24509 if (default_arg == error_mark_node)
24510 return error_mark_node;
24512 /* Push the saved tokens for the default argument onto the parser's
24513 lexer stack. */
24514 tokens = DEFARG_TOKENS (default_arg);
24515 cp_parser_push_lexer_for_tokens (parser, tokens);
24517 start_lambda_scope (decl);
24519 /* Parse the default argument. */
24520 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24521 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24522 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24524 finish_lambda_scope ();
24526 if (parsed_arg == error_mark_node)
24527 cp_parser_skip_to_end_of_statement (parser);
24529 if (!processing_template_decl)
24531 /* In a non-template class, check conversions now. In a template,
24532 we'll wait and instantiate these as needed. */
24533 if (TREE_CODE (decl) == PARM_DECL)
24534 parsed_arg = check_default_argument (parmtype, parsed_arg,
24535 tf_warning_or_error);
24536 else
24537 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24540 /* If the token stream has not been completely used up, then
24541 there was extra junk after the end of the default
24542 argument. */
24543 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24545 if (TREE_CODE (decl) == PARM_DECL)
24546 cp_parser_error (parser, "expected %<,%>");
24547 else
24548 cp_parser_error (parser, "expected %<;%>");
24551 /* Revert to the main lexer. */
24552 cp_parser_pop_lexer (parser);
24554 return parsed_arg;
24557 /* FIELD is a non-static data member with an initializer which we saved for
24558 later; parse it now. */
24560 static void
24561 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24563 tree def;
24565 maybe_begin_member_template_processing (field);
24567 push_unparsed_function_queues (parser);
24568 def = cp_parser_late_parse_one_default_arg (parser, field,
24569 DECL_INITIAL (field),
24570 NULL_TREE);
24571 pop_unparsed_function_queues (parser);
24573 maybe_end_member_template_processing ();
24575 DECL_INITIAL (field) = def;
24578 /* FN is a FUNCTION_DECL which may contains a parameter with an
24579 unparsed DEFAULT_ARG. Parse the default args now. This function
24580 assumes that the current scope is the scope in which the default
24581 argument should be processed. */
24583 static void
24584 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24586 bool saved_local_variables_forbidden_p;
24587 tree parm, parmdecl;
24589 /* While we're parsing the default args, we might (due to the
24590 statement expression extension) encounter more classes. We want
24591 to handle them right away, but we don't want them getting mixed
24592 up with default args that are currently in the queue. */
24593 push_unparsed_function_queues (parser);
24595 /* Local variable names (and the `this' keyword) may not appear
24596 in a default argument. */
24597 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24598 parser->local_variables_forbidden_p = true;
24600 push_defarg_context (fn);
24602 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24603 parmdecl = DECL_ARGUMENTS (fn);
24604 parm && parm != void_list_node;
24605 parm = TREE_CHAIN (parm),
24606 parmdecl = DECL_CHAIN (parmdecl))
24608 tree default_arg = TREE_PURPOSE (parm);
24609 tree parsed_arg;
24610 vec<tree, va_gc> *insts;
24611 tree copy;
24612 unsigned ix;
24614 if (!default_arg)
24615 continue;
24617 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24618 /* This can happen for a friend declaration for a function
24619 already declared with default arguments. */
24620 continue;
24622 parsed_arg
24623 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24624 default_arg,
24625 TREE_VALUE (parm));
24626 if (parsed_arg == error_mark_node)
24628 continue;
24631 TREE_PURPOSE (parm) = parsed_arg;
24633 /* Update any instantiations we've already created. */
24634 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24635 vec_safe_iterate (insts, ix, &copy); ix++)
24636 TREE_PURPOSE (copy) = parsed_arg;
24639 pop_defarg_context ();
24641 /* Make sure no default arg is missing. */
24642 check_default_args (fn);
24644 /* Restore the state of local_variables_forbidden_p. */
24645 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24647 /* Restore the queue. */
24648 pop_unparsed_function_queues (parser);
24651 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24653 sizeof ... ( identifier )
24655 where the 'sizeof' token has already been consumed. */
24657 static tree
24658 cp_parser_sizeof_pack (cp_parser *parser)
24660 /* Consume the `...'. */
24661 cp_lexer_consume_token (parser->lexer);
24662 maybe_warn_variadic_templates ();
24664 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24665 if (paren)
24666 cp_lexer_consume_token (parser->lexer);
24667 else
24668 permerror (cp_lexer_peek_token (parser->lexer)->location,
24669 "%<sizeof...%> argument must be surrounded by parentheses");
24671 cp_token *token = cp_lexer_peek_token (parser->lexer);
24672 tree name = cp_parser_identifier (parser);
24673 if (name == error_mark_node)
24674 return error_mark_node;
24675 /* The name is not qualified. */
24676 parser->scope = NULL_TREE;
24677 parser->qualifying_scope = NULL_TREE;
24678 parser->object_scope = NULL_TREE;
24679 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24680 if (expr == error_mark_node)
24681 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24682 token->location);
24683 if (TREE_CODE (expr) == TYPE_DECL)
24684 expr = TREE_TYPE (expr);
24685 else if (TREE_CODE (expr) == CONST_DECL)
24686 expr = DECL_INITIAL (expr);
24687 expr = make_pack_expansion (expr);
24689 if (paren)
24690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24692 return expr;
24695 /* Parse the operand of `sizeof' (or a similar operator). Returns
24696 either a TYPE or an expression, depending on the form of the
24697 input. The KEYWORD indicates which kind of expression we have
24698 encountered. */
24700 static tree
24701 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24703 tree expr = NULL_TREE;
24704 const char *saved_message;
24705 char *tmp;
24706 bool saved_integral_constant_expression_p;
24707 bool saved_non_integral_constant_expression_p;
24709 /* If it's a `...', then we are computing the length of a parameter
24710 pack. */
24711 if (keyword == RID_SIZEOF
24712 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24713 return cp_parser_sizeof_pack (parser);
24715 /* Types cannot be defined in a `sizeof' expression. Save away the
24716 old message. */
24717 saved_message = parser->type_definition_forbidden_message;
24718 /* And create the new one. */
24719 tmp = concat ("types may not be defined in %<",
24720 IDENTIFIER_POINTER (ridpointers[keyword]),
24721 "%> expressions", NULL);
24722 parser->type_definition_forbidden_message = tmp;
24724 /* The restrictions on constant-expressions do not apply inside
24725 sizeof expressions. */
24726 saved_integral_constant_expression_p
24727 = parser->integral_constant_expression_p;
24728 saved_non_integral_constant_expression_p
24729 = parser->non_integral_constant_expression_p;
24730 parser->integral_constant_expression_p = false;
24732 /* Do not actually evaluate the expression. */
24733 ++cp_unevaluated_operand;
24734 ++c_inhibit_evaluation_warnings;
24735 /* If it's a `(', then we might be looking at the type-id
24736 construction. */
24737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24739 tree type = NULL_TREE;
24740 bool compound_literal_p;
24742 /* We can't be sure yet whether we're looking at a type-id or an
24743 expression. */
24744 cp_parser_parse_tentatively (parser);
24745 /* Consume the `('. */
24746 cp_lexer_consume_token (parser->lexer);
24747 /* Note: as a GNU Extension, compound literals are considered
24748 postfix-expressions as they are in C99, so they are valid
24749 arguments to sizeof. See comment in cp_parser_cast_expression
24750 for details. */
24751 cp_lexer_save_tokens (parser->lexer);
24752 /* Skip tokens until the next token is a closing parenthesis.
24753 If we find the closing `)', and the next token is a `{', then
24754 we are looking at a compound-literal. */
24755 compound_literal_p
24756 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
24757 /*consume_paren=*/true)
24758 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
24759 /* Roll back the tokens we skipped. */
24760 cp_lexer_rollback_tokens (parser->lexer);
24761 /* If we were looking at a compound-literal, simulate an error
24762 so that the call to cp_parser_parse_definitely below will
24763 fail. */
24764 if (compound_literal_p)
24765 cp_parser_simulate_error (parser);
24766 else
24768 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24769 parser->in_type_id_in_expr_p = true;
24770 /* Look for the type-id. */
24771 type = cp_parser_type_id (parser);
24772 /* Look for the closing `)'. */
24773 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24774 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24777 /* If all went well, then we're done. */
24778 if (cp_parser_parse_definitely (parser))
24780 cp_decl_specifier_seq decl_specs;
24782 /* Build a trivial decl-specifier-seq. */
24783 clear_decl_specs (&decl_specs);
24784 decl_specs.type = type;
24786 /* Call grokdeclarator to figure out what type this is. */
24787 expr = grokdeclarator (NULL,
24788 &decl_specs,
24789 TYPENAME,
24790 /*initialized=*/0,
24791 /*attrlist=*/NULL);
24795 /* If the type-id production did not work out, then we must be
24796 looking at the unary-expression production. */
24797 if (!expr)
24798 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24799 /*cast_p=*/false, NULL);
24801 /* Go back to evaluating expressions. */
24802 --cp_unevaluated_operand;
24803 --c_inhibit_evaluation_warnings;
24805 /* Free the message we created. */
24806 free (tmp);
24807 /* And restore the old one. */
24808 parser->type_definition_forbidden_message = saved_message;
24809 parser->integral_constant_expression_p
24810 = saved_integral_constant_expression_p;
24811 parser->non_integral_constant_expression_p
24812 = saved_non_integral_constant_expression_p;
24814 return expr;
24817 /* If the current declaration has no declarator, return true. */
24819 static bool
24820 cp_parser_declares_only_class_p (cp_parser *parser)
24822 /* If the next token is a `;' or a `,' then there is no
24823 declarator. */
24824 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24825 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24828 /* Update the DECL_SPECS to reflect the storage class indicated by
24829 KEYWORD. */
24831 static void
24832 cp_parser_set_storage_class (cp_parser *parser,
24833 cp_decl_specifier_seq *decl_specs,
24834 enum rid keyword,
24835 cp_token *token)
24837 cp_storage_class storage_class;
24839 if (parser->in_unbraced_linkage_specification_p)
24841 error_at (token->location, "invalid use of %qD in linkage specification",
24842 ridpointers[keyword]);
24843 return;
24845 else if (decl_specs->storage_class != sc_none)
24847 decl_specs->conflicting_specifiers_p = true;
24848 return;
24851 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24852 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24853 && decl_specs->gnu_thread_keyword_p)
24855 pedwarn (decl_specs->locations[ds_thread], 0,
24856 "%<__thread%> before %qD", ridpointers[keyword]);
24859 switch (keyword)
24861 case RID_AUTO:
24862 storage_class = sc_auto;
24863 break;
24864 case RID_REGISTER:
24865 storage_class = sc_register;
24866 break;
24867 case RID_STATIC:
24868 storage_class = sc_static;
24869 break;
24870 case RID_EXTERN:
24871 storage_class = sc_extern;
24872 break;
24873 case RID_MUTABLE:
24874 storage_class = sc_mutable;
24875 break;
24876 default:
24877 gcc_unreachable ();
24879 decl_specs->storage_class = storage_class;
24880 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24882 /* A storage class specifier cannot be applied alongside a typedef
24883 specifier. If there is a typedef specifier present then set
24884 conflicting_specifiers_p which will trigger an error later
24885 on in grokdeclarator. */
24886 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24887 decl_specs->conflicting_specifiers_p = true;
24890 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24891 is true, the type is a class or enum definition. */
24893 static void
24894 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24895 tree type_spec,
24896 cp_token *token,
24897 bool type_definition_p)
24899 decl_specs->any_specifiers_p = true;
24901 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24902 (with, for example, in "typedef int wchar_t;") we remember that
24903 this is what happened. In system headers, we ignore these
24904 declarations so that G++ can work with system headers that are not
24905 C++-safe. */
24906 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24907 && !type_definition_p
24908 && (type_spec == boolean_type_node
24909 || type_spec == char16_type_node
24910 || type_spec == char32_type_node
24911 || type_spec == wchar_type_node)
24912 && (decl_specs->type
24913 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24914 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24915 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24916 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24918 decl_specs->redefined_builtin_type = type_spec;
24919 set_and_check_decl_spec_loc (decl_specs,
24920 ds_redefined_builtin_type_spec,
24921 token);
24922 if (!decl_specs->type)
24924 decl_specs->type = type_spec;
24925 decl_specs->type_definition_p = false;
24926 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24929 else if (decl_specs->type)
24930 decl_specs->multiple_types_p = true;
24931 else
24933 decl_specs->type = type_spec;
24934 decl_specs->type_definition_p = type_definition_p;
24935 decl_specs->redefined_builtin_type = NULL_TREE;
24936 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24940 /* True iff TOKEN is the GNU keyword __thread. */
24942 static bool
24943 token_is__thread (cp_token *token)
24945 gcc_assert (token->keyword == RID_THREAD);
24946 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24949 /* Set the location for a declarator specifier and check if it is
24950 duplicated.
24952 DECL_SPECS is the sequence of declarator specifiers onto which to
24953 set the location.
24955 DS is the single declarator specifier to set which location is to
24956 be set onto the existing sequence of declarators.
24958 LOCATION is the location for the declarator specifier to
24959 consider. */
24961 static void
24962 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24963 cp_decl_spec ds, cp_token *token)
24965 gcc_assert (ds < ds_last);
24967 if (decl_specs == NULL)
24968 return;
24970 source_location location = token->location;
24972 if (decl_specs->locations[ds] == 0)
24974 decl_specs->locations[ds] = location;
24975 if (ds == ds_thread)
24976 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24978 else
24980 if (ds == ds_long)
24982 if (decl_specs->locations[ds_long_long] != 0)
24983 error_at (location,
24984 "%<long long long%> is too long for GCC");
24985 else
24987 decl_specs->locations[ds_long_long] = location;
24988 pedwarn_cxx98 (location,
24989 OPT_Wlong_long,
24990 "ISO C++ 1998 does not support %<long long%>");
24993 else if (ds == ds_thread)
24995 bool gnu = token_is__thread (token);
24996 if (gnu != decl_specs->gnu_thread_keyword_p)
24997 error_at (location,
24998 "both %<__thread%> and %<thread_local%> specified");
24999 else
25000 error_at (location, "duplicate %qD", token->u.value);
25002 else
25004 static const char *const decl_spec_names[] = {
25005 "signed",
25006 "unsigned",
25007 "short",
25008 "long",
25009 "const",
25010 "volatile",
25011 "restrict",
25012 "inline",
25013 "virtual",
25014 "explicit",
25015 "friend",
25016 "typedef",
25017 "using",
25018 "constexpr",
25019 "__complex"
25021 error_at (location,
25022 "duplicate %qs", decl_spec_names[ds]);
25027 /* Return true iff the declarator specifier DS is present in the
25028 sequence of declarator specifiers DECL_SPECS. */
25030 bool
25031 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
25032 cp_decl_spec ds)
25034 gcc_assert (ds < ds_last);
25036 if (decl_specs == NULL)
25037 return false;
25039 return decl_specs->locations[ds] != 0;
25042 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
25043 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
25045 static bool
25046 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
25048 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
25051 /* Issue an error message indicating that TOKEN_DESC was expected.
25052 If KEYWORD is true, it indicated this function is called by
25053 cp_parser_require_keword and the required token can only be
25054 a indicated keyword. */
25056 static void
25057 cp_parser_required_error (cp_parser *parser,
25058 required_token token_desc,
25059 bool keyword)
25061 switch (token_desc)
25063 case RT_NEW:
25064 cp_parser_error (parser, "expected %<new%>");
25065 return;
25066 case RT_DELETE:
25067 cp_parser_error (parser, "expected %<delete%>");
25068 return;
25069 case RT_RETURN:
25070 cp_parser_error (parser, "expected %<return%>");
25071 return;
25072 case RT_WHILE:
25073 cp_parser_error (parser, "expected %<while%>");
25074 return;
25075 case RT_EXTERN:
25076 cp_parser_error (parser, "expected %<extern%>");
25077 return;
25078 case RT_STATIC_ASSERT:
25079 cp_parser_error (parser, "expected %<static_assert%>");
25080 return;
25081 case RT_DECLTYPE:
25082 cp_parser_error (parser, "expected %<decltype%>");
25083 return;
25084 case RT_OPERATOR:
25085 cp_parser_error (parser, "expected %<operator%>");
25086 return;
25087 case RT_CLASS:
25088 cp_parser_error (parser, "expected %<class%>");
25089 return;
25090 case RT_TEMPLATE:
25091 cp_parser_error (parser, "expected %<template%>");
25092 return;
25093 case RT_NAMESPACE:
25094 cp_parser_error (parser, "expected %<namespace%>");
25095 return;
25096 case RT_USING:
25097 cp_parser_error (parser, "expected %<using%>");
25098 return;
25099 case RT_ASM:
25100 cp_parser_error (parser, "expected %<asm%>");
25101 return;
25102 case RT_TRY:
25103 cp_parser_error (parser, "expected %<try%>");
25104 return;
25105 case RT_CATCH:
25106 cp_parser_error (parser, "expected %<catch%>");
25107 return;
25108 case RT_THROW:
25109 cp_parser_error (parser, "expected %<throw%>");
25110 return;
25111 case RT_LABEL:
25112 cp_parser_error (parser, "expected %<__label__%>");
25113 return;
25114 case RT_AT_TRY:
25115 cp_parser_error (parser, "expected %<@try%>");
25116 return;
25117 case RT_AT_SYNCHRONIZED:
25118 cp_parser_error (parser, "expected %<@synchronized%>");
25119 return;
25120 case RT_AT_THROW:
25121 cp_parser_error (parser, "expected %<@throw%>");
25122 return;
25123 case RT_TRANSACTION_ATOMIC:
25124 cp_parser_error (parser, "expected %<__transaction_atomic%>");
25125 return;
25126 case RT_TRANSACTION_RELAXED:
25127 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
25128 return;
25129 default:
25130 break;
25132 if (!keyword)
25134 switch (token_desc)
25136 case RT_SEMICOLON:
25137 cp_parser_error (parser, "expected %<;%>");
25138 return;
25139 case RT_OPEN_PAREN:
25140 cp_parser_error (parser, "expected %<(%>");
25141 return;
25142 case RT_CLOSE_BRACE:
25143 cp_parser_error (parser, "expected %<}%>");
25144 return;
25145 case RT_OPEN_BRACE:
25146 cp_parser_error (parser, "expected %<{%>");
25147 return;
25148 case RT_CLOSE_SQUARE:
25149 cp_parser_error (parser, "expected %<]%>");
25150 return;
25151 case RT_OPEN_SQUARE:
25152 cp_parser_error (parser, "expected %<[%>");
25153 return;
25154 case RT_COMMA:
25155 cp_parser_error (parser, "expected %<,%>");
25156 return;
25157 case RT_SCOPE:
25158 cp_parser_error (parser, "expected %<::%>");
25159 return;
25160 case RT_LESS:
25161 cp_parser_error (parser, "expected %<<%>");
25162 return;
25163 case RT_GREATER:
25164 cp_parser_error (parser, "expected %<>%>");
25165 return;
25166 case RT_EQ:
25167 cp_parser_error (parser, "expected %<=%>");
25168 return;
25169 case RT_ELLIPSIS:
25170 cp_parser_error (parser, "expected %<...%>");
25171 return;
25172 case RT_MULT:
25173 cp_parser_error (parser, "expected %<*%>");
25174 return;
25175 case RT_COMPL:
25176 cp_parser_error (parser, "expected %<~%>");
25177 return;
25178 case RT_COLON:
25179 cp_parser_error (parser, "expected %<:%>");
25180 return;
25181 case RT_COLON_SCOPE:
25182 cp_parser_error (parser, "expected %<:%> or %<::%>");
25183 return;
25184 case RT_CLOSE_PAREN:
25185 cp_parser_error (parser, "expected %<)%>");
25186 return;
25187 case RT_COMMA_CLOSE_PAREN:
25188 cp_parser_error (parser, "expected %<,%> or %<)%>");
25189 return;
25190 case RT_PRAGMA_EOL:
25191 cp_parser_error (parser, "expected end of line");
25192 return;
25193 case RT_NAME:
25194 cp_parser_error (parser, "expected identifier");
25195 return;
25196 case RT_SELECT:
25197 cp_parser_error (parser, "expected selection-statement");
25198 return;
25199 case RT_INTERATION:
25200 cp_parser_error (parser, "expected iteration-statement");
25201 return;
25202 case RT_JUMP:
25203 cp_parser_error (parser, "expected jump-statement");
25204 return;
25205 case RT_CLASS_KEY:
25206 cp_parser_error (parser, "expected class-key");
25207 return;
25208 case RT_CLASS_TYPENAME_TEMPLATE:
25209 cp_parser_error (parser,
25210 "expected %<class%>, %<typename%>, or %<template%>");
25211 return;
25212 default:
25213 gcc_unreachable ();
25216 else
25217 gcc_unreachable ();
25222 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25223 issue an error message indicating that TOKEN_DESC was expected.
25225 Returns the token consumed, if the token had the appropriate type.
25226 Otherwise, returns NULL. */
25228 static cp_token *
25229 cp_parser_require (cp_parser* parser,
25230 enum cpp_ttype type,
25231 required_token token_desc)
25233 if (cp_lexer_next_token_is (parser->lexer, type))
25234 return cp_lexer_consume_token (parser->lexer);
25235 else
25237 /* Output the MESSAGE -- unless we're parsing tentatively. */
25238 if (!cp_parser_simulate_error (parser))
25239 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25240 return NULL;
25244 /* An error message is produced if the next token is not '>'.
25245 All further tokens are skipped until the desired token is
25246 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25248 static void
25249 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25251 /* Current level of '< ... >'. */
25252 unsigned level = 0;
25253 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25254 unsigned nesting_depth = 0;
25256 /* Are we ready, yet? If not, issue error message. */
25257 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25258 return;
25260 /* Skip tokens until the desired token is found. */
25261 while (true)
25263 /* Peek at the next token. */
25264 switch (cp_lexer_peek_token (parser->lexer)->type)
25266 case CPP_LESS:
25267 if (!nesting_depth)
25268 ++level;
25269 break;
25271 case CPP_RSHIFT:
25272 if (cxx_dialect == cxx98)
25273 /* C++0x views the `>>' operator as two `>' tokens, but
25274 C++98 does not. */
25275 break;
25276 else if (!nesting_depth && level-- == 0)
25278 /* We've hit a `>>' where the first `>' closes the
25279 template argument list, and the second `>' is
25280 spurious. Just consume the `>>' and stop; we've
25281 already produced at least one error. */
25282 cp_lexer_consume_token (parser->lexer);
25283 return;
25285 /* Fall through for C++0x, so we handle the second `>' in
25286 the `>>'. */
25288 case CPP_GREATER:
25289 if (!nesting_depth && level-- == 0)
25291 /* We've reached the token we want, consume it and stop. */
25292 cp_lexer_consume_token (parser->lexer);
25293 return;
25295 break;
25297 case CPP_OPEN_PAREN:
25298 case CPP_OPEN_SQUARE:
25299 ++nesting_depth;
25300 break;
25302 case CPP_CLOSE_PAREN:
25303 case CPP_CLOSE_SQUARE:
25304 if (nesting_depth-- == 0)
25305 return;
25306 break;
25308 case CPP_EOF:
25309 case CPP_PRAGMA_EOL:
25310 case CPP_SEMICOLON:
25311 case CPP_OPEN_BRACE:
25312 case CPP_CLOSE_BRACE:
25313 /* The '>' was probably forgotten, don't look further. */
25314 return;
25316 default:
25317 break;
25320 /* Consume this token. */
25321 cp_lexer_consume_token (parser->lexer);
25325 /* If the next token is the indicated keyword, consume it. Otherwise,
25326 issue an error message indicating that TOKEN_DESC was expected.
25328 Returns the token consumed, if the token had the appropriate type.
25329 Otherwise, returns NULL. */
25331 static cp_token *
25332 cp_parser_require_keyword (cp_parser* parser,
25333 enum rid keyword,
25334 required_token token_desc)
25336 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25338 if (token && token->keyword != keyword)
25340 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25341 return NULL;
25344 return token;
25347 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25348 function-definition. */
25350 static bool
25351 cp_parser_token_starts_function_definition_p (cp_token* token)
25353 return (/* An ordinary function-body begins with an `{'. */
25354 token->type == CPP_OPEN_BRACE
25355 /* A ctor-initializer begins with a `:'. */
25356 || token->type == CPP_COLON
25357 /* A function-try-block begins with `try'. */
25358 || token->keyword == RID_TRY
25359 /* A function-transaction-block begins with `__transaction_atomic'
25360 or `__transaction_relaxed'. */
25361 || token->keyword == RID_TRANSACTION_ATOMIC
25362 || token->keyword == RID_TRANSACTION_RELAXED
25363 /* The named return value extension begins with `return'. */
25364 || token->keyword == RID_RETURN);
25367 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25368 definition. */
25370 static bool
25371 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25373 cp_token *token;
25375 token = cp_lexer_peek_token (parser->lexer);
25376 return (token->type == CPP_OPEN_BRACE
25377 || (token->type == CPP_COLON
25378 && !parser->colon_doesnt_start_class_def_p));
25381 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25382 C++0x) ending a template-argument. */
25384 static bool
25385 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25387 cp_token *token;
25389 token = cp_lexer_peek_token (parser->lexer);
25390 return (token->type == CPP_COMMA
25391 || token->type == CPP_GREATER
25392 || token->type == CPP_ELLIPSIS
25393 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25396 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25397 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25399 static bool
25400 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25401 size_t n)
25403 cp_token *token;
25405 token = cp_lexer_peek_nth_token (parser->lexer, n);
25406 if (token->type == CPP_LESS)
25407 return true;
25408 /* Check for the sequence `<::' in the original code. It would be lexed as
25409 `[:', where `[' is a digraph, and there is no whitespace before
25410 `:'. */
25411 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25413 cp_token *token2;
25414 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25415 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25416 return true;
25418 return false;
25421 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25422 or none_type otherwise. */
25424 static enum tag_types
25425 cp_parser_token_is_class_key (cp_token* token)
25427 switch (token->keyword)
25429 case RID_CLASS:
25430 return class_type;
25431 case RID_STRUCT:
25432 return record_type;
25433 case RID_UNION:
25434 return union_type;
25436 default:
25437 return none_type;
25441 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25443 static void
25444 cp_parser_check_class_key (enum tag_types class_key, tree type)
25446 if (type == error_mark_node)
25447 return;
25448 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25450 if (permerror (input_location, "%qs tag used in naming %q#T",
25451 class_key == union_type ? "union"
25452 : class_key == record_type ? "struct" : "class",
25453 type))
25454 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25455 "%q#T was previously declared here", type);
25459 /* Issue an error message if DECL is redeclared with different
25460 access than its original declaration [class.access.spec/3].
25461 This applies to nested classes and nested class templates.
25462 [class.mem/1]. */
25464 static void
25465 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25467 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25468 return;
25470 if ((TREE_PRIVATE (decl)
25471 != (current_access_specifier == access_private_node))
25472 || (TREE_PROTECTED (decl)
25473 != (current_access_specifier == access_protected_node)))
25474 error_at (location, "%qD redeclared with different access", decl);
25477 /* Look for the `template' keyword, as a syntactic disambiguator.
25478 Return TRUE iff it is present, in which case it will be
25479 consumed. */
25481 static bool
25482 cp_parser_optional_template_keyword (cp_parser *parser)
25484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25486 /* In C++98 the `template' keyword can only be used within templates;
25487 outside templates the parser can always figure out what is a
25488 template and what is not. In C++11, per the resolution of DR 468,
25489 `template' is allowed in cases where it is not strictly necessary. */
25490 if (!processing_template_decl
25491 && pedantic && cxx_dialect == cxx98)
25493 cp_token *token = cp_lexer_peek_token (parser->lexer);
25494 pedwarn (token->location, OPT_Wpedantic,
25495 "in C++98 %<template%> (as a disambiguator) is only "
25496 "allowed within templates");
25497 /* If this part of the token stream is rescanned, the same
25498 error message would be generated. So, we purge the token
25499 from the stream. */
25500 cp_lexer_purge_token (parser->lexer);
25501 return false;
25503 else
25505 /* Consume the `template' keyword. */
25506 cp_lexer_consume_token (parser->lexer);
25507 return true;
25510 return false;
25513 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25514 set PARSER->SCOPE, and perform other related actions. */
25516 static void
25517 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25519 int i;
25520 struct tree_check *check_value;
25521 deferred_access_check *chk;
25522 vec<deferred_access_check, va_gc> *checks;
25524 /* Get the stored value. */
25525 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25526 /* Perform any access checks that were deferred. */
25527 checks = check_value->checks;
25528 if (checks)
25530 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25531 perform_or_defer_access_check (chk->binfo,
25532 chk->decl,
25533 chk->diag_decl, tf_warning_or_error);
25535 /* Set the scope from the stored value. */
25536 parser->scope = check_value->value;
25537 parser->qualifying_scope = check_value->qualifying_scope;
25538 parser->object_scope = NULL_TREE;
25541 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25542 encounter the end of a block before what we were looking for. */
25544 static bool
25545 cp_parser_cache_group (cp_parser *parser,
25546 enum cpp_ttype end,
25547 unsigned depth)
25549 while (true)
25551 cp_token *token = cp_lexer_peek_token (parser->lexer);
25553 /* Abort a parenthesized expression if we encounter a semicolon. */
25554 if ((end == CPP_CLOSE_PAREN || depth == 0)
25555 && token->type == CPP_SEMICOLON)
25556 return true;
25557 /* If we've reached the end of the file, stop. */
25558 if (token->type == CPP_EOF
25559 || (end != CPP_PRAGMA_EOL
25560 && token->type == CPP_PRAGMA_EOL))
25561 return true;
25562 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25563 /* We've hit the end of an enclosing block, so there's been some
25564 kind of syntax error. */
25565 return true;
25567 /* Consume the token. */
25568 cp_lexer_consume_token (parser->lexer);
25569 /* See if it starts a new group. */
25570 if (token->type == CPP_OPEN_BRACE)
25572 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25573 /* In theory this should probably check end == '}', but
25574 cp_parser_save_member_function_body needs it to exit
25575 after either '}' or ')' when called with ')'. */
25576 if (depth == 0)
25577 return false;
25579 else if (token->type == CPP_OPEN_PAREN)
25581 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25582 if (depth == 0 && end == CPP_CLOSE_PAREN)
25583 return false;
25585 else if (token->type == CPP_PRAGMA)
25586 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25587 else if (token->type == end)
25588 return false;
25592 /* Like above, for caching a default argument or NSDMI. Both of these are
25593 terminated by a non-nested comma, but it can be unclear whether or not a
25594 comma is nested in a template argument list unless we do more parsing.
25595 In order to handle this ambiguity, when we encounter a ',' after a '<'
25596 we try to parse what follows as a parameter-declaration-list (in the
25597 case of a default argument) or a member-declarator (in the case of an
25598 NSDMI). If that succeeds, then we stop caching. */
25600 static tree
25601 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25603 unsigned depth = 0;
25604 int maybe_template_id = 0;
25605 cp_token *first_token;
25606 cp_token *token;
25607 tree default_argument;
25609 /* Add tokens until we have processed the entire default
25610 argument. We add the range [first_token, token). */
25611 first_token = cp_lexer_peek_token (parser->lexer);
25612 if (first_token->type == CPP_OPEN_BRACE)
25614 /* For list-initialization, this is straightforward. */
25615 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25616 token = cp_lexer_peek_token (parser->lexer);
25618 else while (true)
25620 bool done = false;
25622 /* Peek at the next token. */
25623 token = cp_lexer_peek_token (parser->lexer);
25624 /* What we do depends on what token we have. */
25625 switch (token->type)
25627 /* In valid code, a default argument must be
25628 immediately followed by a `,' `)', or `...'. */
25629 case CPP_COMMA:
25630 if (depth == 0 && maybe_template_id)
25632 /* If we've seen a '<', we might be in a
25633 template-argument-list. Until Core issue 325 is
25634 resolved, we don't know how this situation ought
25635 to be handled, so try to DTRT. We check whether
25636 what comes after the comma is a valid parameter
25637 declaration list. If it is, then the comma ends
25638 the default argument; otherwise the default
25639 argument continues. */
25640 bool error = false;
25642 /* Set ITALP so cp_parser_parameter_declaration_list
25643 doesn't decide to commit to this parse. */
25644 bool saved_italp = parser->in_template_argument_list_p;
25645 parser->in_template_argument_list_p = true;
25647 cp_parser_parse_tentatively (parser);
25648 cp_lexer_consume_token (parser->lexer);
25650 if (nsdmi)
25652 int ctor_dtor_or_conv_p;
25653 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25654 &ctor_dtor_or_conv_p,
25655 /*parenthesized_p=*/NULL,
25656 /*member_p=*/true);
25658 else
25660 begin_scope (sk_function_parms, NULL_TREE);
25661 cp_parser_parameter_declaration_list (parser, &error);
25662 pop_bindings_and_leave_scope ();
25664 if (!cp_parser_error_occurred (parser) && !error)
25665 done = true;
25666 cp_parser_abort_tentative_parse (parser);
25668 parser->in_template_argument_list_p = saved_italp;
25669 break;
25671 case CPP_CLOSE_PAREN:
25672 case CPP_ELLIPSIS:
25673 /* If we run into a non-nested `;', `}', or `]',
25674 then the code is invalid -- but the default
25675 argument is certainly over. */
25676 case CPP_SEMICOLON:
25677 case CPP_CLOSE_BRACE:
25678 case CPP_CLOSE_SQUARE:
25679 if (depth == 0
25680 /* Handle correctly int n = sizeof ... ( p ); */
25681 && token->type != CPP_ELLIPSIS)
25682 done = true;
25683 /* Update DEPTH, if necessary. */
25684 else if (token->type == CPP_CLOSE_PAREN
25685 || token->type == CPP_CLOSE_BRACE
25686 || token->type == CPP_CLOSE_SQUARE)
25687 --depth;
25688 break;
25690 case CPP_OPEN_PAREN:
25691 case CPP_OPEN_SQUARE:
25692 case CPP_OPEN_BRACE:
25693 ++depth;
25694 break;
25696 case CPP_LESS:
25697 if (depth == 0)
25698 /* This might be the comparison operator, or it might
25699 start a template argument list. */
25700 ++maybe_template_id;
25701 break;
25703 case CPP_RSHIFT:
25704 if (cxx_dialect == cxx98)
25705 break;
25706 /* Fall through for C++0x, which treats the `>>'
25707 operator like two `>' tokens in certain
25708 cases. */
25710 case CPP_GREATER:
25711 if (depth == 0)
25713 /* This might be an operator, or it might close a
25714 template argument list. But if a previous '<'
25715 started a template argument list, this will have
25716 closed it, so we can't be in one anymore. */
25717 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25718 if (maybe_template_id < 0)
25719 maybe_template_id = 0;
25721 break;
25723 /* If we run out of tokens, issue an error message. */
25724 case CPP_EOF:
25725 case CPP_PRAGMA_EOL:
25726 error_at (token->location, "file ends in default argument");
25727 done = true;
25728 break;
25730 case CPP_NAME:
25731 case CPP_SCOPE:
25732 /* In these cases, we should look for template-ids.
25733 For example, if the default argument is
25734 `X<int, double>()', we need to do name lookup to
25735 figure out whether or not `X' is a template; if
25736 so, the `,' does not end the default argument.
25738 That is not yet done. */
25739 break;
25741 default:
25742 break;
25745 /* If we've reached the end, stop. */
25746 if (done)
25747 break;
25749 /* Add the token to the token block. */
25750 token = cp_lexer_consume_token (parser->lexer);
25753 /* Create a DEFAULT_ARG to represent the unparsed default
25754 argument. */
25755 default_argument = make_node (DEFAULT_ARG);
25756 DEFARG_TOKENS (default_argument)
25757 = cp_token_cache_new (first_token, token);
25758 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25760 return default_argument;
25763 /* Begin parsing tentatively. We always save tokens while parsing
25764 tentatively so that if the tentative parsing fails we can restore the
25765 tokens. */
25767 static void
25768 cp_parser_parse_tentatively (cp_parser* parser)
25770 /* Enter a new parsing context. */
25771 parser->context = cp_parser_context_new (parser->context);
25772 /* Begin saving tokens. */
25773 cp_lexer_save_tokens (parser->lexer);
25774 /* In order to avoid repetitive access control error messages,
25775 access checks are queued up until we are no longer parsing
25776 tentatively. */
25777 push_deferring_access_checks (dk_deferred);
25780 /* Commit to the currently active tentative parse. */
25782 static void
25783 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25785 cp_parser_context *context;
25786 cp_lexer *lexer;
25788 /* Mark all of the levels as committed. */
25789 lexer = parser->lexer;
25790 for (context = parser->context; context->next; context = context->next)
25792 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25793 break;
25794 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25795 while (!cp_lexer_saving_tokens (lexer))
25796 lexer = lexer->next;
25797 cp_lexer_commit_tokens (lexer);
25801 /* Commit to the topmost currently active tentative parse.
25803 Note that this function shouldn't be called when there are
25804 irreversible side-effects while in a tentative state. For
25805 example, we shouldn't create a permanent entry in the symbol
25806 table, or issue an error message that might not apply if the
25807 tentative parse is aborted. */
25809 static void
25810 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25812 cp_parser_context *context = parser->context;
25813 cp_lexer *lexer = parser->lexer;
25815 if (context)
25817 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25818 return;
25819 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25821 while (!cp_lexer_saving_tokens (lexer))
25822 lexer = lexer->next;
25823 cp_lexer_commit_tokens (lexer);
25827 /* Abort the currently active tentative parse. All consumed tokens
25828 will be rolled back, and no diagnostics will be issued. */
25830 static void
25831 cp_parser_abort_tentative_parse (cp_parser* parser)
25833 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25834 || errorcount > 0);
25835 cp_parser_simulate_error (parser);
25836 /* Now, pretend that we want to see if the construct was
25837 successfully parsed. */
25838 cp_parser_parse_definitely (parser);
25841 /* Stop parsing tentatively. If a parse error has occurred, restore the
25842 token stream. Otherwise, commit to the tokens we have consumed.
25843 Returns true if no error occurred; false otherwise. */
25845 static bool
25846 cp_parser_parse_definitely (cp_parser* parser)
25848 bool error_occurred;
25849 cp_parser_context *context;
25851 /* Remember whether or not an error occurred, since we are about to
25852 destroy that information. */
25853 error_occurred = cp_parser_error_occurred (parser);
25854 /* Remove the topmost context from the stack. */
25855 context = parser->context;
25856 parser->context = context->next;
25857 /* If no parse errors occurred, commit to the tentative parse. */
25858 if (!error_occurred)
25860 /* Commit to the tokens read tentatively, unless that was
25861 already done. */
25862 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25863 cp_lexer_commit_tokens (parser->lexer);
25865 pop_to_parent_deferring_access_checks ();
25867 /* Otherwise, if errors occurred, roll back our state so that things
25868 are just as they were before we began the tentative parse. */
25869 else
25871 cp_lexer_rollback_tokens (parser->lexer);
25872 pop_deferring_access_checks ();
25874 /* Add the context to the front of the free list. */
25875 context->next = cp_parser_context_free_list;
25876 cp_parser_context_free_list = context;
25878 return !error_occurred;
25881 /* Returns true if we are parsing tentatively and are not committed to
25882 this tentative parse. */
25884 static bool
25885 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25887 return (cp_parser_parsing_tentatively (parser)
25888 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25891 /* Returns nonzero iff an error has occurred during the most recent
25892 tentative parse. */
25894 static bool
25895 cp_parser_error_occurred (cp_parser* parser)
25897 return (cp_parser_parsing_tentatively (parser)
25898 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25901 /* Returns nonzero if GNU extensions are allowed. */
25903 static bool
25904 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25906 return parser->allow_gnu_extensions_p;
25909 /* Objective-C++ Productions */
25912 /* Parse an Objective-C expression, which feeds into a primary-expression
25913 above.
25915 objc-expression:
25916 objc-message-expression
25917 objc-string-literal
25918 objc-encode-expression
25919 objc-protocol-expression
25920 objc-selector-expression
25922 Returns a tree representation of the expression. */
25924 static tree
25925 cp_parser_objc_expression (cp_parser* parser)
25927 /* Try to figure out what kind of declaration is present. */
25928 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25930 switch (kwd->type)
25932 case CPP_OPEN_SQUARE:
25933 return cp_parser_objc_message_expression (parser);
25935 case CPP_OBJC_STRING:
25936 kwd = cp_lexer_consume_token (parser->lexer);
25937 return objc_build_string_object (kwd->u.value);
25939 case CPP_KEYWORD:
25940 switch (kwd->keyword)
25942 case RID_AT_ENCODE:
25943 return cp_parser_objc_encode_expression (parser);
25945 case RID_AT_PROTOCOL:
25946 return cp_parser_objc_protocol_expression (parser);
25948 case RID_AT_SELECTOR:
25949 return cp_parser_objc_selector_expression (parser);
25951 default:
25952 break;
25954 default:
25955 error_at (kwd->location,
25956 "misplaced %<@%D%> Objective-C++ construct",
25957 kwd->u.value);
25958 cp_parser_skip_to_end_of_block_or_statement (parser);
25961 return error_mark_node;
25964 /* Parse an Objective-C message expression.
25966 objc-message-expression:
25967 [ objc-message-receiver objc-message-args ]
25969 Returns a representation of an Objective-C message. */
25971 static tree
25972 cp_parser_objc_message_expression (cp_parser* parser)
25974 tree receiver, messageargs;
25976 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25977 receiver = cp_parser_objc_message_receiver (parser);
25978 messageargs = cp_parser_objc_message_args (parser);
25979 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25981 return objc_build_message_expr (receiver, messageargs);
25984 /* Parse an objc-message-receiver.
25986 objc-message-receiver:
25987 expression
25988 simple-type-specifier
25990 Returns a representation of the type or expression. */
25992 static tree
25993 cp_parser_objc_message_receiver (cp_parser* parser)
25995 tree rcv;
25997 /* An Objective-C message receiver may be either (1) a type
25998 or (2) an expression. */
25999 cp_parser_parse_tentatively (parser);
26000 rcv = cp_parser_expression (parser, false, NULL);
26002 if (cp_parser_parse_definitely (parser))
26003 return rcv;
26005 rcv = cp_parser_simple_type_specifier (parser,
26006 /*decl_specs=*/NULL,
26007 CP_PARSER_FLAGS_NONE);
26009 return objc_get_class_reference (rcv);
26012 /* Parse the arguments and selectors comprising an Objective-C message.
26014 objc-message-args:
26015 objc-selector
26016 objc-selector-args
26017 objc-selector-args , objc-comma-args
26019 objc-selector-args:
26020 objc-selector [opt] : assignment-expression
26021 objc-selector-args objc-selector [opt] : assignment-expression
26023 objc-comma-args:
26024 assignment-expression
26025 objc-comma-args , assignment-expression
26027 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
26028 selector arguments and TREE_VALUE containing a list of comma
26029 arguments. */
26031 static tree
26032 cp_parser_objc_message_args (cp_parser* parser)
26034 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
26035 bool maybe_unary_selector_p = true;
26036 cp_token *token = cp_lexer_peek_token (parser->lexer);
26038 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26040 tree selector = NULL_TREE, arg;
26042 if (token->type != CPP_COLON)
26043 selector = cp_parser_objc_selector (parser);
26045 /* Detect if we have a unary selector. */
26046 if (maybe_unary_selector_p
26047 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26048 return build_tree_list (selector, NULL_TREE);
26050 maybe_unary_selector_p = false;
26051 cp_parser_require (parser, CPP_COLON, RT_COLON);
26052 arg = cp_parser_assignment_expression (parser, false, NULL);
26054 sel_args
26055 = chainon (sel_args,
26056 build_tree_list (selector, arg));
26058 token = cp_lexer_peek_token (parser->lexer);
26061 /* Handle non-selector arguments, if any. */
26062 while (token->type == CPP_COMMA)
26064 tree arg;
26066 cp_lexer_consume_token (parser->lexer);
26067 arg = cp_parser_assignment_expression (parser, false, NULL);
26069 addl_args
26070 = chainon (addl_args,
26071 build_tree_list (NULL_TREE, arg));
26073 token = cp_lexer_peek_token (parser->lexer);
26076 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
26078 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
26079 return build_tree_list (error_mark_node, error_mark_node);
26082 return build_tree_list (sel_args, addl_args);
26085 /* Parse an Objective-C encode expression.
26087 objc-encode-expression:
26088 @encode objc-typename
26090 Returns an encoded representation of the type argument. */
26092 static tree
26093 cp_parser_objc_encode_expression (cp_parser* parser)
26095 tree type;
26096 cp_token *token;
26098 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
26099 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26100 token = cp_lexer_peek_token (parser->lexer);
26101 type = complete_type (cp_parser_type_id (parser));
26102 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26104 if (!type)
26106 error_at (token->location,
26107 "%<@encode%> must specify a type as an argument");
26108 return error_mark_node;
26111 /* This happens if we find @encode(T) (where T is a template
26112 typename or something dependent on a template typename) when
26113 parsing a template. In that case, we can't compile it
26114 immediately, but we rather create an AT_ENCODE_EXPR which will
26115 need to be instantiated when the template is used.
26117 if (dependent_type_p (type))
26119 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
26120 TREE_READONLY (value) = 1;
26121 return value;
26124 return objc_build_encode_expr (type);
26127 /* Parse an Objective-C @defs expression. */
26129 static tree
26130 cp_parser_objc_defs_expression (cp_parser *parser)
26132 tree name;
26134 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
26135 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26136 name = cp_parser_identifier (parser);
26137 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26139 return objc_get_class_ivars (name);
26142 /* Parse an Objective-C protocol expression.
26144 objc-protocol-expression:
26145 @protocol ( identifier )
26147 Returns a representation of the protocol expression. */
26149 static tree
26150 cp_parser_objc_protocol_expression (cp_parser* parser)
26152 tree proto;
26154 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26155 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26156 proto = cp_parser_identifier (parser);
26157 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26159 return objc_build_protocol_expr (proto);
26162 /* Parse an Objective-C selector expression.
26164 objc-selector-expression:
26165 @selector ( objc-method-signature )
26167 objc-method-signature:
26168 objc-selector
26169 objc-selector-seq
26171 objc-selector-seq:
26172 objc-selector :
26173 objc-selector-seq objc-selector :
26175 Returns a representation of the method selector. */
26177 static tree
26178 cp_parser_objc_selector_expression (cp_parser* parser)
26180 tree sel_seq = NULL_TREE;
26181 bool maybe_unary_selector_p = true;
26182 cp_token *token;
26183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26185 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26186 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26187 token = cp_lexer_peek_token (parser->lexer);
26189 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26190 || token->type == CPP_SCOPE)
26192 tree selector = NULL_TREE;
26194 if (token->type != CPP_COLON
26195 || token->type == CPP_SCOPE)
26196 selector = cp_parser_objc_selector (parser);
26198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26199 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26201 /* Detect if we have a unary selector. */
26202 if (maybe_unary_selector_p)
26204 sel_seq = selector;
26205 goto finish_selector;
26207 else
26209 cp_parser_error (parser, "expected %<:%>");
26212 maybe_unary_selector_p = false;
26213 token = cp_lexer_consume_token (parser->lexer);
26215 if (token->type == CPP_SCOPE)
26217 sel_seq
26218 = chainon (sel_seq,
26219 build_tree_list (selector, NULL_TREE));
26220 sel_seq
26221 = chainon (sel_seq,
26222 build_tree_list (NULL_TREE, NULL_TREE));
26224 else
26225 sel_seq
26226 = chainon (sel_seq,
26227 build_tree_list (selector, NULL_TREE));
26229 token = cp_lexer_peek_token (parser->lexer);
26232 finish_selector:
26233 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26235 return objc_build_selector_expr (loc, sel_seq);
26238 /* Parse a list of identifiers.
26240 objc-identifier-list:
26241 identifier
26242 objc-identifier-list , identifier
26244 Returns a TREE_LIST of identifier nodes. */
26246 static tree
26247 cp_parser_objc_identifier_list (cp_parser* parser)
26249 tree identifier;
26250 tree list;
26251 cp_token *sep;
26253 identifier = cp_parser_identifier (parser);
26254 if (identifier == error_mark_node)
26255 return error_mark_node;
26257 list = build_tree_list (NULL_TREE, identifier);
26258 sep = cp_lexer_peek_token (parser->lexer);
26260 while (sep->type == CPP_COMMA)
26262 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26263 identifier = cp_parser_identifier (parser);
26264 if (identifier == error_mark_node)
26265 return list;
26267 list = chainon (list, build_tree_list (NULL_TREE,
26268 identifier));
26269 sep = cp_lexer_peek_token (parser->lexer);
26272 return list;
26275 /* Parse an Objective-C alias declaration.
26277 objc-alias-declaration:
26278 @compatibility_alias identifier identifier ;
26280 This function registers the alias mapping with the Objective-C front end.
26281 It returns nothing. */
26283 static void
26284 cp_parser_objc_alias_declaration (cp_parser* parser)
26286 tree alias, orig;
26288 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26289 alias = cp_parser_identifier (parser);
26290 orig = cp_parser_identifier (parser);
26291 objc_declare_alias (alias, orig);
26292 cp_parser_consume_semicolon_at_end_of_statement (parser);
26295 /* Parse an Objective-C class forward-declaration.
26297 objc-class-declaration:
26298 @class objc-identifier-list ;
26300 The function registers the forward declarations with the Objective-C
26301 front end. It returns nothing. */
26303 static void
26304 cp_parser_objc_class_declaration (cp_parser* parser)
26306 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26307 while (true)
26309 tree id;
26311 id = cp_parser_identifier (parser);
26312 if (id == error_mark_node)
26313 break;
26315 objc_declare_class (id);
26317 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26318 cp_lexer_consume_token (parser->lexer);
26319 else
26320 break;
26322 cp_parser_consume_semicolon_at_end_of_statement (parser);
26325 /* Parse a list of Objective-C protocol references.
26327 objc-protocol-refs-opt:
26328 objc-protocol-refs [opt]
26330 objc-protocol-refs:
26331 < objc-identifier-list >
26333 Returns a TREE_LIST of identifiers, if any. */
26335 static tree
26336 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26338 tree protorefs = NULL_TREE;
26340 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26342 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26343 protorefs = cp_parser_objc_identifier_list (parser);
26344 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26347 return protorefs;
26350 /* Parse a Objective-C visibility specification. */
26352 static void
26353 cp_parser_objc_visibility_spec (cp_parser* parser)
26355 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26357 switch (vis->keyword)
26359 case RID_AT_PRIVATE:
26360 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26361 break;
26362 case RID_AT_PROTECTED:
26363 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26364 break;
26365 case RID_AT_PUBLIC:
26366 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26367 break;
26368 case RID_AT_PACKAGE:
26369 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26370 break;
26371 default:
26372 return;
26375 /* Eat '@private'/'@protected'/'@public'. */
26376 cp_lexer_consume_token (parser->lexer);
26379 /* Parse an Objective-C method type. Return 'true' if it is a class
26380 (+) method, and 'false' if it is an instance (-) method. */
26382 static inline bool
26383 cp_parser_objc_method_type (cp_parser* parser)
26385 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26386 return true;
26387 else
26388 return false;
26391 /* Parse an Objective-C protocol qualifier. */
26393 static tree
26394 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26396 tree quals = NULL_TREE, node;
26397 cp_token *token = cp_lexer_peek_token (parser->lexer);
26399 node = token->u.value;
26401 while (node && identifier_p (node)
26402 && (node == ridpointers [(int) RID_IN]
26403 || node == ridpointers [(int) RID_OUT]
26404 || node == ridpointers [(int) RID_INOUT]
26405 || node == ridpointers [(int) RID_BYCOPY]
26406 || node == ridpointers [(int) RID_BYREF]
26407 || node == ridpointers [(int) RID_ONEWAY]))
26409 quals = tree_cons (NULL_TREE, node, quals);
26410 cp_lexer_consume_token (parser->lexer);
26411 token = cp_lexer_peek_token (parser->lexer);
26412 node = token->u.value;
26415 return quals;
26418 /* Parse an Objective-C typename. */
26420 static tree
26421 cp_parser_objc_typename (cp_parser* parser)
26423 tree type_name = NULL_TREE;
26425 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26427 tree proto_quals, cp_type = NULL_TREE;
26429 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26430 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26432 /* An ObjC type name may consist of just protocol qualifiers, in which
26433 case the type shall default to 'id'. */
26434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26436 cp_type = cp_parser_type_id (parser);
26438 /* If the type could not be parsed, an error has already
26439 been produced. For error recovery, behave as if it had
26440 not been specified, which will use the default type
26441 'id'. */
26442 if (cp_type == error_mark_node)
26444 cp_type = NULL_TREE;
26445 /* We need to skip to the closing parenthesis as
26446 cp_parser_type_id() does not seem to do it for
26447 us. */
26448 cp_parser_skip_to_closing_parenthesis (parser,
26449 /*recovering=*/true,
26450 /*or_comma=*/false,
26451 /*consume_paren=*/false);
26455 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26456 type_name = build_tree_list (proto_quals, cp_type);
26459 return type_name;
26462 /* Check to see if TYPE refers to an Objective-C selector name. */
26464 static bool
26465 cp_parser_objc_selector_p (enum cpp_ttype type)
26467 return (type == CPP_NAME || type == CPP_KEYWORD
26468 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26469 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26470 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26471 || type == CPP_XOR || type == CPP_XOR_EQ);
26474 /* Parse an Objective-C selector. */
26476 static tree
26477 cp_parser_objc_selector (cp_parser* parser)
26479 cp_token *token = cp_lexer_consume_token (parser->lexer);
26481 if (!cp_parser_objc_selector_p (token->type))
26483 error_at (token->location, "invalid Objective-C++ selector name");
26484 return error_mark_node;
26487 /* C++ operator names are allowed to appear in ObjC selectors. */
26488 switch (token->type)
26490 case CPP_AND_AND: return get_identifier ("and");
26491 case CPP_AND_EQ: return get_identifier ("and_eq");
26492 case CPP_AND: return get_identifier ("bitand");
26493 case CPP_OR: return get_identifier ("bitor");
26494 case CPP_COMPL: return get_identifier ("compl");
26495 case CPP_NOT: return get_identifier ("not");
26496 case CPP_NOT_EQ: return get_identifier ("not_eq");
26497 case CPP_OR_OR: return get_identifier ("or");
26498 case CPP_OR_EQ: return get_identifier ("or_eq");
26499 case CPP_XOR: return get_identifier ("xor");
26500 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26501 default: return token->u.value;
26505 /* Parse an Objective-C params list. */
26507 static tree
26508 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26510 tree params = NULL_TREE;
26511 bool maybe_unary_selector_p = true;
26512 cp_token *token = cp_lexer_peek_token (parser->lexer);
26514 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26516 tree selector = NULL_TREE, type_name, identifier;
26517 tree parm_attr = NULL_TREE;
26519 if (token->keyword == RID_ATTRIBUTE)
26520 break;
26522 if (token->type != CPP_COLON)
26523 selector = cp_parser_objc_selector (parser);
26525 /* Detect if we have a unary selector. */
26526 if (maybe_unary_selector_p
26527 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26529 params = selector; /* Might be followed by attributes. */
26530 break;
26533 maybe_unary_selector_p = false;
26534 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26536 /* Something went quite wrong. There should be a colon
26537 here, but there is not. Stop parsing parameters. */
26538 break;
26540 type_name = cp_parser_objc_typename (parser);
26541 /* New ObjC allows attributes on parameters too. */
26542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26543 parm_attr = cp_parser_attributes_opt (parser);
26544 identifier = cp_parser_identifier (parser);
26546 params
26547 = chainon (params,
26548 objc_build_keyword_decl (selector,
26549 type_name,
26550 identifier,
26551 parm_attr));
26553 token = cp_lexer_peek_token (parser->lexer);
26556 if (params == NULL_TREE)
26558 cp_parser_error (parser, "objective-c++ method declaration is expected");
26559 return error_mark_node;
26562 /* We allow tail attributes for the method. */
26563 if (token->keyword == RID_ATTRIBUTE)
26565 *attributes = cp_parser_attributes_opt (parser);
26566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26567 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26568 return params;
26569 cp_parser_error (parser,
26570 "method attributes must be specified at the end");
26571 return error_mark_node;
26574 if (params == NULL_TREE)
26576 cp_parser_error (parser, "objective-c++ method declaration is expected");
26577 return error_mark_node;
26579 return params;
26582 /* Parse the non-keyword Objective-C params. */
26584 static tree
26585 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26586 tree* attributes)
26588 tree params = make_node (TREE_LIST);
26589 cp_token *token = cp_lexer_peek_token (parser->lexer);
26590 *ellipsisp = false; /* Initially, assume no ellipsis. */
26592 while (token->type == CPP_COMMA)
26594 cp_parameter_declarator *parmdecl;
26595 tree parm;
26597 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26598 token = cp_lexer_peek_token (parser->lexer);
26600 if (token->type == CPP_ELLIPSIS)
26602 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26603 *ellipsisp = true;
26604 token = cp_lexer_peek_token (parser->lexer);
26605 break;
26608 /* TODO: parse attributes for tail parameters. */
26609 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26610 parm = grokdeclarator (parmdecl->declarator,
26611 &parmdecl->decl_specifiers,
26612 PARM, /*initialized=*/0,
26613 /*attrlist=*/NULL);
26615 chainon (params, build_tree_list (NULL_TREE, parm));
26616 token = cp_lexer_peek_token (parser->lexer);
26619 /* We allow tail attributes for the method. */
26620 if (token->keyword == RID_ATTRIBUTE)
26622 if (*attributes == NULL_TREE)
26624 *attributes = cp_parser_attributes_opt (parser);
26625 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26626 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26627 return params;
26629 else
26630 /* We have an error, but parse the attributes, so that we can
26631 carry on. */
26632 *attributes = cp_parser_attributes_opt (parser);
26634 cp_parser_error (parser,
26635 "method attributes must be specified at the end");
26636 return error_mark_node;
26639 return params;
26642 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26644 static void
26645 cp_parser_objc_interstitial_code (cp_parser* parser)
26647 cp_token *token = cp_lexer_peek_token (parser->lexer);
26649 /* If the next token is `extern' and the following token is a string
26650 literal, then we have a linkage specification. */
26651 if (token->keyword == RID_EXTERN
26652 && cp_parser_is_pure_string_literal
26653 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26654 cp_parser_linkage_specification (parser);
26655 /* Handle #pragma, if any. */
26656 else if (token->type == CPP_PRAGMA)
26657 cp_parser_pragma (parser, pragma_objc_icode);
26658 /* Allow stray semicolons. */
26659 else if (token->type == CPP_SEMICOLON)
26660 cp_lexer_consume_token (parser->lexer);
26661 /* Mark methods as optional or required, when building protocols. */
26662 else if (token->keyword == RID_AT_OPTIONAL)
26664 cp_lexer_consume_token (parser->lexer);
26665 objc_set_method_opt (true);
26667 else if (token->keyword == RID_AT_REQUIRED)
26669 cp_lexer_consume_token (parser->lexer);
26670 objc_set_method_opt (false);
26672 else if (token->keyword == RID_NAMESPACE)
26673 cp_parser_namespace_definition (parser);
26674 /* Other stray characters must generate errors. */
26675 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26677 cp_lexer_consume_token (parser->lexer);
26678 error ("stray %qs between Objective-C++ methods",
26679 token->type == CPP_OPEN_BRACE ? "{" : "}");
26681 /* Finally, try to parse a block-declaration, or a function-definition. */
26682 else
26683 cp_parser_block_declaration (parser, /*statement_p=*/false);
26686 /* Parse a method signature. */
26688 static tree
26689 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26691 tree rettype, kwdparms, optparms;
26692 bool ellipsis = false;
26693 bool is_class_method;
26695 is_class_method = cp_parser_objc_method_type (parser);
26696 rettype = cp_parser_objc_typename (parser);
26697 *attributes = NULL_TREE;
26698 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26699 if (kwdparms == error_mark_node)
26700 return error_mark_node;
26701 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26702 if (optparms == error_mark_node)
26703 return error_mark_node;
26705 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26708 static bool
26709 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26711 tree tattr;
26712 cp_lexer_save_tokens (parser->lexer);
26713 tattr = cp_parser_attributes_opt (parser);
26714 gcc_assert (tattr) ;
26716 /* If the attributes are followed by a method introducer, this is not allowed.
26717 Dump the attributes and flag the situation. */
26718 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26719 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26720 return true;
26722 /* Otherwise, the attributes introduce some interstitial code, possibly so
26723 rewind to allow that check. */
26724 cp_lexer_rollback_tokens (parser->lexer);
26725 return false;
26728 /* Parse an Objective-C method prototype list. */
26730 static void
26731 cp_parser_objc_method_prototype_list (cp_parser* parser)
26733 cp_token *token = cp_lexer_peek_token (parser->lexer);
26735 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26737 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26739 tree attributes, sig;
26740 bool is_class_method;
26741 if (token->type == CPP_PLUS)
26742 is_class_method = true;
26743 else
26744 is_class_method = false;
26745 sig = cp_parser_objc_method_signature (parser, &attributes);
26746 if (sig == error_mark_node)
26748 cp_parser_skip_to_end_of_block_or_statement (parser);
26749 token = cp_lexer_peek_token (parser->lexer);
26750 continue;
26752 objc_add_method_declaration (is_class_method, sig, attributes);
26753 cp_parser_consume_semicolon_at_end_of_statement (parser);
26755 else if (token->keyword == RID_AT_PROPERTY)
26756 cp_parser_objc_at_property_declaration (parser);
26757 else if (token->keyword == RID_ATTRIBUTE
26758 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26759 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26760 OPT_Wattributes,
26761 "prefix attributes are ignored for methods");
26762 else
26763 /* Allow for interspersed non-ObjC++ code. */
26764 cp_parser_objc_interstitial_code (parser);
26766 token = cp_lexer_peek_token (parser->lexer);
26769 if (token->type != CPP_EOF)
26770 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26771 else
26772 cp_parser_error (parser, "expected %<@end%>");
26774 objc_finish_interface ();
26777 /* Parse an Objective-C method definition list. */
26779 static void
26780 cp_parser_objc_method_definition_list (cp_parser* parser)
26782 cp_token *token = cp_lexer_peek_token (parser->lexer);
26784 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26786 tree meth;
26788 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26790 cp_token *ptk;
26791 tree sig, attribute;
26792 bool is_class_method;
26793 if (token->type == CPP_PLUS)
26794 is_class_method = true;
26795 else
26796 is_class_method = false;
26797 push_deferring_access_checks (dk_deferred);
26798 sig = cp_parser_objc_method_signature (parser, &attribute);
26799 if (sig == error_mark_node)
26801 cp_parser_skip_to_end_of_block_or_statement (parser);
26802 token = cp_lexer_peek_token (parser->lexer);
26803 continue;
26805 objc_start_method_definition (is_class_method, sig, attribute,
26806 NULL_TREE);
26808 /* For historical reasons, we accept an optional semicolon. */
26809 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26810 cp_lexer_consume_token (parser->lexer);
26812 ptk = cp_lexer_peek_token (parser->lexer);
26813 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26814 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26816 perform_deferred_access_checks (tf_warning_or_error);
26817 stop_deferring_access_checks ();
26818 meth = cp_parser_function_definition_after_declarator (parser,
26819 false);
26820 pop_deferring_access_checks ();
26821 objc_finish_method_definition (meth);
26824 /* The following case will be removed once @synthesize is
26825 completely implemented. */
26826 else if (token->keyword == RID_AT_PROPERTY)
26827 cp_parser_objc_at_property_declaration (parser);
26828 else if (token->keyword == RID_AT_SYNTHESIZE)
26829 cp_parser_objc_at_synthesize_declaration (parser);
26830 else if (token->keyword == RID_AT_DYNAMIC)
26831 cp_parser_objc_at_dynamic_declaration (parser);
26832 else if (token->keyword == RID_ATTRIBUTE
26833 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26834 warning_at (token->location, OPT_Wattributes,
26835 "prefix attributes are ignored for methods");
26836 else
26837 /* Allow for interspersed non-ObjC++ code. */
26838 cp_parser_objc_interstitial_code (parser);
26840 token = cp_lexer_peek_token (parser->lexer);
26843 if (token->type != CPP_EOF)
26844 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26845 else
26846 cp_parser_error (parser, "expected %<@end%>");
26848 objc_finish_implementation ();
26851 /* Parse Objective-C ivars. */
26853 static void
26854 cp_parser_objc_class_ivars (cp_parser* parser)
26856 cp_token *token = cp_lexer_peek_token (parser->lexer);
26858 if (token->type != CPP_OPEN_BRACE)
26859 return; /* No ivars specified. */
26861 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26862 token = cp_lexer_peek_token (parser->lexer);
26864 while (token->type != CPP_CLOSE_BRACE
26865 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26867 cp_decl_specifier_seq declspecs;
26868 int decl_class_or_enum_p;
26869 tree prefix_attributes;
26871 cp_parser_objc_visibility_spec (parser);
26873 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26874 break;
26876 cp_parser_decl_specifier_seq (parser,
26877 CP_PARSER_FLAGS_OPTIONAL,
26878 &declspecs,
26879 &decl_class_or_enum_p);
26881 /* auto, register, static, extern, mutable. */
26882 if (declspecs.storage_class != sc_none)
26884 cp_parser_error (parser, "invalid type for instance variable");
26885 declspecs.storage_class = sc_none;
26888 /* thread_local. */
26889 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26891 cp_parser_error (parser, "invalid type for instance variable");
26892 declspecs.locations[ds_thread] = 0;
26895 /* typedef. */
26896 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26898 cp_parser_error (parser, "invalid type for instance variable");
26899 declspecs.locations[ds_typedef] = 0;
26902 prefix_attributes = declspecs.attributes;
26903 declspecs.attributes = NULL_TREE;
26905 /* Keep going until we hit the `;' at the end of the
26906 declaration. */
26907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26909 tree width = NULL_TREE, attributes, first_attribute, decl;
26910 cp_declarator *declarator = NULL;
26911 int ctor_dtor_or_conv_p;
26913 /* Check for a (possibly unnamed) bitfield declaration. */
26914 token = cp_lexer_peek_token (parser->lexer);
26915 if (token->type == CPP_COLON)
26916 goto eat_colon;
26918 if (token->type == CPP_NAME
26919 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26920 == CPP_COLON))
26922 /* Get the name of the bitfield. */
26923 declarator = make_id_declarator (NULL_TREE,
26924 cp_parser_identifier (parser),
26925 sfk_none);
26927 eat_colon:
26928 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26929 /* Get the width of the bitfield. */
26930 width
26931 = cp_parser_constant_expression (parser,
26932 /*allow_non_constant=*/false,
26933 NULL);
26935 else
26937 /* Parse the declarator. */
26938 declarator
26939 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26940 &ctor_dtor_or_conv_p,
26941 /*parenthesized_p=*/NULL,
26942 /*member_p=*/false);
26945 /* Look for attributes that apply to the ivar. */
26946 attributes = cp_parser_attributes_opt (parser);
26947 /* Remember which attributes are prefix attributes and
26948 which are not. */
26949 first_attribute = attributes;
26950 /* Combine the attributes. */
26951 attributes = chainon (prefix_attributes, attributes);
26953 if (width)
26954 /* Create the bitfield declaration. */
26955 decl = grokbitfield (declarator, &declspecs,
26956 width,
26957 attributes);
26958 else
26959 decl = grokfield (declarator, &declspecs,
26960 NULL_TREE, /*init_const_expr_p=*/false,
26961 NULL_TREE, attributes);
26963 /* Add the instance variable. */
26964 if (decl != error_mark_node && decl != NULL_TREE)
26965 objc_add_instance_variable (decl);
26967 /* Reset PREFIX_ATTRIBUTES. */
26968 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26969 attributes = TREE_CHAIN (attributes);
26970 if (attributes)
26971 TREE_CHAIN (attributes) = NULL_TREE;
26973 token = cp_lexer_peek_token (parser->lexer);
26975 if (token->type == CPP_COMMA)
26977 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26978 continue;
26980 break;
26983 cp_parser_consume_semicolon_at_end_of_statement (parser);
26984 token = cp_lexer_peek_token (parser->lexer);
26987 if (token->keyword == RID_AT_END)
26988 cp_parser_error (parser, "expected %<}%>");
26990 /* Do not consume the RID_AT_END, so it will be read again as terminating
26991 the @interface of @implementation. */
26992 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26993 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26995 /* For historical reasons, we accept an optional semicolon. */
26996 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26997 cp_lexer_consume_token (parser->lexer);
27000 /* Parse an Objective-C protocol declaration. */
27002 static void
27003 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
27005 tree proto, protorefs;
27006 cp_token *tok;
27008 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
27011 tok = cp_lexer_peek_token (parser->lexer);
27012 error_at (tok->location, "identifier expected after %<@protocol%>");
27013 cp_parser_consume_semicolon_at_end_of_statement (parser);
27014 return;
27017 /* See if we have a forward declaration or a definition. */
27018 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
27020 /* Try a forward declaration first. */
27021 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
27023 while (true)
27025 tree id;
27027 id = cp_parser_identifier (parser);
27028 if (id == error_mark_node)
27029 break;
27031 objc_declare_protocol (id, attributes);
27033 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27034 cp_lexer_consume_token (parser->lexer);
27035 else
27036 break;
27038 cp_parser_consume_semicolon_at_end_of_statement (parser);
27041 /* Ok, we got a full-fledged definition (or at least should). */
27042 else
27044 proto = cp_parser_identifier (parser);
27045 protorefs = cp_parser_objc_protocol_refs_opt (parser);
27046 objc_start_protocol (proto, protorefs, attributes);
27047 cp_parser_objc_method_prototype_list (parser);
27051 /* Parse an Objective-C superclass or category. */
27053 static void
27054 cp_parser_objc_superclass_or_category (cp_parser *parser,
27055 bool iface_p,
27056 tree *super,
27057 tree *categ, bool *is_class_extension)
27059 cp_token *next = cp_lexer_peek_token (parser->lexer);
27061 *super = *categ = NULL_TREE;
27062 *is_class_extension = false;
27063 if (next->type == CPP_COLON)
27065 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27066 *super = cp_parser_identifier (parser);
27068 else if (next->type == CPP_OPEN_PAREN)
27070 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27072 /* If there is no category name, and this is an @interface, we
27073 have a class extension. */
27074 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27076 *categ = NULL_TREE;
27077 *is_class_extension = true;
27079 else
27080 *categ = cp_parser_identifier (parser);
27082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27086 /* Parse an Objective-C class interface. */
27088 static void
27089 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
27091 tree name, super, categ, protos;
27092 bool is_class_extension;
27094 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
27095 name = cp_parser_identifier (parser);
27096 if (name == error_mark_node)
27098 /* It's hard to recover because even if valid @interface stuff
27099 is to follow, we can't compile it (or validate it) if we
27100 don't even know which class it refers to. Let's assume this
27101 was a stray '@interface' token in the stream and skip it.
27103 return;
27105 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
27106 &is_class_extension);
27107 protos = cp_parser_objc_protocol_refs_opt (parser);
27109 /* We have either a class or a category on our hands. */
27110 if (categ || is_class_extension)
27111 objc_start_category_interface (name, categ, protos, attributes);
27112 else
27114 objc_start_class_interface (name, super, protos, attributes);
27115 /* Handle instance variable declarations, if any. */
27116 cp_parser_objc_class_ivars (parser);
27117 objc_continue_interface ();
27120 cp_parser_objc_method_prototype_list (parser);
27123 /* Parse an Objective-C class implementation. */
27125 static void
27126 cp_parser_objc_class_implementation (cp_parser* parser)
27128 tree name, super, categ;
27129 bool is_class_extension;
27131 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
27132 name = cp_parser_identifier (parser);
27133 if (name == error_mark_node)
27135 /* It's hard to recover because even if valid @implementation
27136 stuff is to follow, we can't compile it (or validate it) if
27137 we don't even know which class it refers to. Let's assume
27138 this was a stray '@implementation' token in the stream and
27139 skip it.
27141 return;
27143 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
27144 &is_class_extension);
27146 /* We have either a class or a category on our hands. */
27147 if (categ)
27148 objc_start_category_implementation (name, categ);
27149 else
27151 objc_start_class_implementation (name, super);
27152 /* Handle instance variable declarations, if any. */
27153 cp_parser_objc_class_ivars (parser);
27154 objc_continue_implementation ();
27157 cp_parser_objc_method_definition_list (parser);
27160 /* Consume the @end token and finish off the implementation. */
27162 static void
27163 cp_parser_objc_end_implementation (cp_parser* parser)
27165 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27166 objc_finish_implementation ();
27169 /* Parse an Objective-C declaration. */
27171 static void
27172 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27174 /* Try to figure out what kind of declaration is present. */
27175 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27177 if (attributes)
27178 switch (kwd->keyword)
27180 case RID_AT_ALIAS:
27181 case RID_AT_CLASS:
27182 case RID_AT_END:
27183 error_at (kwd->location, "attributes may not be specified before"
27184 " the %<@%D%> Objective-C++ keyword",
27185 kwd->u.value);
27186 attributes = NULL;
27187 break;
27188 case RID_AT_IMPLEMENTATION:
27189 warning_at (kwd->location, OPT_Wattributes,
27190 "prefix attributes are ignored before %<@%D%>",
27191 kwd->u.value);
27192 attributes = NULL;
27193 default:
27194 break;
27197 switch (kwd->keyword)
27199 case RID_AT_ALIAS:
27200 cp_parser_objc_alias_declaration (parser);
27201 break;
27202 case RID_AT_CLASS:
27203 cp_parser_objc_class_declaration (parser);
27204 break;
27205 case RID_AT_PROTOCOL:
27206 cp_parser_objc_protocol_declaration (parser, attributes);
27207 break;
27208 case RID_AT_INTERFACE:
27209 cp_parser_objc_class_interface (parser, attributes);
27210 break;
27211 case RID_AT_IMPLEMENTATION:
27212 cp_parser_objc_class_implementation (parser);
27213 break;
27214 case RID_AT_END:
27215 cp_parser_objc_end_implementation (parser);
27216 break;
27217 default:
27218 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27219 kwd->u.value);
27220 cp_parser_skip_to_end_of_block_or_statement (parser);
27224 /* Parse an Objective-C try-catch-finally statement.
27226 objc-try-catch-finally-stmt:
27227 @try compound-statement objc-catch-clause-seq [opt]
27228 objc-finally-clause [opt]
27230 objc-catch-clause-seq:
27231 objc-catch-clause objc-catch-clause-seq [opt]
27233 objc-catch-clause:
27234 @catch ( objc-exception-declaration ) compound-statement
27236 objc-finally-clause:
27237 @finally compound-statement
27239 objc-exception-declaration:
27240 parameter-declaration
27241 '...'
27243 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27245 Returns NULL_TREE.
27247 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27248 for C. Keep them in sync. */
27250 static tree
27251 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27253 location_t location;
27254 tree stmt;
27256 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27257 location = cp_lexer_peek_token (parser->lexer)->location;
27258 objc_maybe_warn_exceptions (location);
27259 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27260 node, lest it get absorbed into the surrounding block. */
27261 stmt = push_stmt_list ();
27262 cp_parser_compound_statement (parser, NULL, false, false);
27263 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27265 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27267 cp_parameter_declarator *parm;
27268 tree parameter_declaration = error_mark_node;
27269 bool seen_open_paren = false;
27271 cp_lexer_consume_token (parser->lexer);
27272 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27273 seen_open_paren = true;
27274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27276 /* We have "@catch (...)" (where the '...' are literally
27277 what is in the code). Skip the '...'.
27278 parameter_declaration is set to NULL_TREE, and
27279 objc_being_catch_clauses() knows that that means
27280 '...'. */
27281 cp_lexer_consume_token (parser->lexer);
27282 parameter_declaration = NULL_TREE;
27284 else
27286 /* We have "@catch (NSException *exception)" or something
27287 like that. Parse the parameter declaration. */
27288 parm = cp_parser_parameter_declaration (parser, false, NULL);
27289 if (parm == NULL)
27290 parameter_declaration = error_mark_node;
27291 else
27292 parameter_declaration = grokdeclarator (parm->declarator,
27293 &parm->decl_specifiers,
27294 PARM, /*initialized=*/0,
27295 /*attrlist=*/NULL);
27297 if (seen_open_paren)
27298 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27299 else
27301 /* If there was no open parenthesis, we are recovering from
27302 an error, and we are trying to figure out what mistake
27303 the user has made. */
27305 /* If there is an immediate closing parenthesis, the user
27306 probably forgot the opening one (ie, they typed "@catch
27307 NSException *e)". Parse the closing parenthesis and keep
27308 going. */
27309 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27310 cp_lexer_consume_token (parser->lexer);
27312 /* If these is no immediate closing parenthesis, the user
27313 probably doesn't know that parenthesis are required at
27314 all (ie, they typed "@catch NSException *e"). So, just
27315 forget about the closing parenthesis and keep going. */
27317 objc_begin_catch_clause (parameter_declaration);
27318 cp_parser_compound_statement (parser, NULL, false, false);
27319 objc_finish_catch_clause ();
27321 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27323 cp_lexer_consume_token (parser->lexer);
27324 location = cp_lexer_peek_token (parser->lexer)->location;
27325 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27326 node, lest it get absorbed into the surrounding block. */
27327 stmt = push_stmt_list ();
27328 cp_parser_compound_statement (parser, NULL, false, false);
27329 objc_build_finally_clause (location, pop_stmt_list (stmt));
27332 return objc_finish_try_stmt ();
27335 /* Parse an Objective-C synchronized statement.
27337 objc-synchronized-stmt:
27338 @synchronized ( expression ) compound-statement
27340 Returns NULL_TREE. */
27342 static tree
27343 cp_parser_objc_synchronized_statement (cp_parser *parser)
27345 location_t location;
27346 tree lock, stmt;
27348 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27350 location = cp_lexer_peek_token (parser->lexer)->location;
27351 objc_maybe_warn_exceptions (location);
27352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27353 lock = cp_parser_expression (parser, false, NULL);
27354 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27356 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27357 node, lest it get absorbed into the surrounding block. */
27358 stmt = push_stmt_list ();
27359 cp_parser_compound_statement (parser, NULL, false, false);
27361 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27364 /* Parse an Objective-C throw statement.
27366 objc-throw-stmt:
27367 @throw assignment-expression [opt] ;
27369 Returns a constructed '@throw' statement. */
27371 static tree
27372 cp_parser_objc_throw_statement (cp_parser *parser)
27374 tree expr = NULL_TREE;
27375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27377 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27380 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27382 cp_parser_consume_semicolon_at_end_of_statement (parser);
27384 return objc_build_throw_stmt (loc, expr);
27387 /* Parse an Objective-C statement. */
27389 static tree
27390 cp_parser_objc_statement (cp_parser * parser)
27392 /* Try to figure out what kind of declaration is present. */
27393 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27395 switch (kwd->keyword)
27397 case RID_AT_TRY:
27398 return cp_parser_objc_try_catch_finally_statement (parser);
27399 case RID_AT_SYNCHRONIZED:
27400 return cp_parser_objc_synchronized_statement (parser);
27401 case RID_AT_THROW:
27402 return cp_parser_objc_throw_statement (parser);
27403 default:
27404 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27405 kwd->u.value);
27406 cp_parser_skip_to_end_of_block_or_statement (parser);
27409 return error_mark_node;
27412 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27413 look ahead to see if an objc keyword follows the attributes. This
27414 is to detect the use of prefix attributes on ObjC @interface and
27415 @protocol. */
27417 static bool
27418 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27420 cp_lexer_save_tokens (parser->lexer);
27421 *attrib = cp_parser_attributes_opt (parser);
27422 gcc_assert (*attrib);
27423 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27425 cp_lexer_commit_tokens (parser->lexer);
27426 return true;
27428 cp_lexer_rollback_tokens (parser->lexer);
27429 return false;
27432 /* This routine is a minimal replacement for
27433 c_parser_struct_declaration () used when parsing the list of
27434 types/names or ObjC++ properties. For example, when parsing the
27435 code
27437 @property (readonly) int a, b, c;
27439 this function is responsible for parsing "int a, int b, int c" and
27440 returning the declarations as CHAIN of DECLs.
27442 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27443 similar parsing. */
27444 static tree
27445 cp_parser_objc_struct_declaration (cp_parser *parser)
27447 tree decls = NULL_TREE;
27448 cp_decl_specifier_seq declspecs;
27449 int decl_class_or_enum_p;
27450 tree prefix_attributes;
27452 cp_parser_decl_specifier_seq (parser,
27453 CP_PARSER_FLAGS_NONE,
27454 &declspecs,
27455 &decl_class_or_enum_p);
27457 if (declspecs.type == error_mark_node)
27458 return error_mark_node;
27460 /* auto, register, static, extern, mutable. */
27461 if (declspecs.storage_class != sc_none)
27463 cp_parser_error (parser, "invalid type for property");
27464 declspecs.storage_class = sc_none;
27467 /* thread_local. */
27468 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27470 cp_parser_error (parser, "invalid type for property");
27471 declspecs.locations[ds_thread] = 0;
27474 /* typedef. */
27475 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27477 cp_parser_error (parser, "invalid type for property");
27478 declspecs.locations[ds_typedef] = 0;
27481 prefix_attributes = declspecs.attributes;
27482 declspecs.attributes = NULL_TREE;
27484 /* Keep going until we hit the `;' at the end of the declaration. */
27485 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27487 tree attributes, first_attribute, decl;
27488 cp_declarator *declarator;
27489 cp_token *token;
27491 /* Parse the declarator. */
27492 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27493 NULL, NULL, false);
27495 /* Look for attributes that apply to the ivar. */
27496 attributes = cp_parser_attributes_opt (parser);
27497 /* Remember which attributes are prefix attributes and
27498 which are not. */
27499 first_attribute = attributes;
27500 /* Combine the attributes. */
27501 attributes = chainon (prefix_attributes, attributes);
27503 decl = grokfield (declarator, &declspecs,
27504 NULL_TREE, /*init_const_expr_p=*/false,
27505 NULL_TREE, attributes);
27507 if (decl == error_mark_node || decl == NULL_TREE)
27508 return error_mark_node;
27510 /* Reset PREFIX_ATTRIBUTES. */
27511 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27512 attributes = TREE_CHAIN (attributes);
27513 if (attributes)
27514 TREE_CHAIN (attributes) = NULL_TREE;
27516 DECL_CHAIN (decl) = decls;
27517 decls = decl;
27519 token = cp_lexer_peek_token (parser->lexer);
27520 if (token->type == CPP_COMMA)
27522 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27523 continue;
27525 else
27526 break;
27528 return decls;
27531 /* Parse an Objective-C @property declaration. The syntax is:
27533 objc-property-declaration:
27534 '@property' objc-property-attributes[opt] struct-declaration ;
27536 objc-property-attributes:
27537 '(' objc-property-attribute-list ')'
27539 objc-property-attribute-list:
27540 objc-property-attribute
27541 objc-property-attribute-list, objc-property-attribute
27543 objc-property-attribute
27544 'getter' = identifier
27545 'setter' = identifier
27546 'readonly'
27547 'readwrite'
27548 'assign'
27549 'retain'
27550 'copy'
27551 'nonatomic'
27553 For example:
27554 @property NSString *name;
27555 @property (readonly) id object;
27556 @property (retain, nonatomic, getter=getTheName) id name;
27557 @property int a, b, c;
27559 PS: This function is identical to
27560 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27561 static void
27562 cp_parser_objc_at_property_declaration (cp_parser *parser)
27564 /* The following variables hold the attributes of the properties as
27565 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27566 seen. When we see an attribute, we set them to 'true' (if they
27567 are boolean properties) or to the identifier (if they have an
27568 argument, ie, for getter and setter). Note that here we only
27569 parse the list of attributes, check the syntax and accumulate the
27570 attributes that we find. objc_add_property_declaration() will
27571 then process the information. */
27572 bool property_assign = false;
27573 bool property_copy = false;
27574 tree property_getter_ident = NULL_TREE;
27575 bool property_nonatomic = false;
27576 bool property_readonly = false;
27577 bool property_readwrite = false;
27578 bool property_retain = false;
27579 tree property_setter_ident = NULL_TREE;
27581 /* 'properties' is the list of properties that we read. Usually a
27582 single one, but maybe more (eg, in "@property int a, b, c;" there
27583 are three). */
27584 tree properties;
27585 location_t loc;
27587 loc = cp_lexer_peek_token (parser->lexer)->location;
27589 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27591 /* Parse the optional attribute list... */
27592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27594 /* Eat the '('. */
27595 cp_lexer_consume_token (parser->lexer);
27597 while (true)
27599 bool syntax_error = false;
27600 cp_token *token = cp_lexer_peek_token (parser->lexer);
27601 enum rid keyword;
27603 if (token->type != CPP_NAME)
27605 cp_parser_error (parser, "expected identifier");
27606 break;
27608 keyword = C_RID_CODE (token->u.value);
27609 cp_lexer_consume_token (parser->lexer);
27610 switch (keyword)
27612 case RID_ASSIGN: property_assign = true; break;
27613 case RID_COPY: property_copy = true; break;
27614 case RID_NONATOMIC: property_nonatomic = true; break;
27615 case RID_READONLY: property_readonly = true; break;
27616 case RID_READWRITE: property_readwrite = true; break;
27617 case RID_RETAIN: property_retain = true; break;
27619 case RID_GETTER:
27620 case RID_SETTER:
27621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27623 if (keyword == RID_GETTER)
27624 cp_parser_error (parser,
27625 "missing %<=%> (after %<getter%> attribute)");
27626 else
27627 cp_parser_error (parser,
27628 "missing %<=%> (after %<setter%> attribute)");
27629 syntax_error = true;
27630 break;
27632 cp_lexer_consume_token (parser->lexer); /* eat the = */
27633 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27635 cp_parser_error (parser, "expected identifier");
27636 syntax_error = true;
27637 break;
27639 if (keyword == RID_SETTER)
27641 if (property_setter_ident != NULL_TREE)
27643 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27644 cp_lexer_consume_token (parser->lexer);
27646 else
27647 property_setter_ident = cp_parser_objc_selector (parser);
27648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27649 cp_parser_error (parser, "setter name must terminate with %<:%>");
27650 else
27651 cp_lexer_consume_token (parser->lexer);
27653 else
27655 if (property_getter_ident != NULL_TREE)
27657 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27658 cp_lexer_consume_token (parser->lexer);
27660 else
27661 property_getter_ident = cp_parser_objc_selector (parser);
27663 break;
27664 default:
27665 cp_parser_error (parser, "unknown property attribute");
27666 syntax_error = true;
27667 break;
27670 if (syntax_error)
27671 break;
27673 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27674 cp_lexer_consume_token (parser->lexer);
27675 else
27676 break;
27679 /* FIXME: "@property (setter, assign);" will generate a spurious
27680 "error: expected ‘)’ before ‘,’ token". This is because
27681 cp_parser_require, unlike the C counterpart, will produce an
27682 error even if we are in error recovery. */
27683 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27685 cp_parser_skip_to_closing_parenthesis (parser,
27686 /*recovering=*/true,
27687 /*or_comma=*/false,
27688 /*consume_paren=*/true);
27692 /* ... and the property declaration(s). */
27693 properties = cp_parser_objc_struct_declaration (parser);
27695 if (properties == error_mark_node)
27697 cp_parser_skip_to_end_of_statement (parser);
27698 /* If the next token is now a `;', consume it. */
27699 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27700 cp_lexer_consume_token (parser->lexer);
27701 return;
27704 if (properties == NULL_TREE)
27705 cp_parser_error (parser, "expected identifier");
27706 else
27708 /* Comma-separated properties are chained together in
27709 reverse order; add them one by one. */
27710 properties = nreverse (properties);
27712 for (; properties; properties = TREE_CHAIN (properties))
27713 objc_add_property_declaration (loc, copy_node (properties),
27714 property_readonly, property_readwrite,
27715 property_assign, property_retain,
27716 property_copy, property_nonatomic,
27717 property_getter_ident, property_setter_ident);
27720 cp_parser_consume_semicolon_at_end_of_statement (parser);
27723 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27725 objc-synthesize-declaration:
27726 @synthesize objc-synthesize-identifier-list ;
27728 objc-synthesize-identifier-list:
27729 objc-synthesize-identifier
27730 objc-synthesize-identifier-list, objc-synthesize-identifier
27732 objc-synthesize-identifier
27733 identifier
27734 identifier = identifier
27736 For example:
27737 @synthesize MyProperty;
27738 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27740 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27741 for C. Keep them in sync.
27743 static void
27744 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27746 tree list = NULL_TREE;
27747 location_t loc;
27748 loc = cp_lexer_peek_token (parser->lexer)->location;
27750 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27751 while (true)
27753 tree property, ivar;
27754 property = cp_parser_identifier (parser);
27755 if (property == error_mark_node)
27757 cp_parser_consume_semicolon_at_end_of_statement (parser);
27758 return;
27760 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27762 cp_lexer_consume_token (parser->lexer);
27763 ivar = cp_parser_identifier (parser);
27764 if (ivar == error_mark_node)
27766 cp_parser_consume_semicolon_at_end_of_statement (parser);
27767 return;
27770 else
27771 ivar = NULL_TREE;
27772 list = chainon (list, build_tree_list (ivar, property));
27773 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27774 cp_lexer_consume_token (parser->lexer);
27775 else
27776 break;
27778 cp_parser_consume_semicolon_at_end_of_statement (parser);
27779 objc_add_synthesize_declaration (loc, list);
27782 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27784 objc-dynamic-declaration:
27785 @dynamic identifier-list ;
27787 For example:
27788 @dynamic MyProperty;
27789 @dynamic MyProperty, AnotherProperty;
27791 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27792 for C. Keep them in sync.
27794 static void
27795 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27797 tree list = NULL_TREE;
27798 location_t loc;
27799 loc = cp_lexer_peek_token (parser->lexer)->location;
27801 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27802 while (true)
27804 tree property;
27805 property = cp_parser_identifier (parser);
27806 if (property == error_mark_node)
27808 cp_parser_consume_semicolon_at_end_of_statement (parser);
27809 return;
27811 list = chainon (list, build_tree_list (NULL, property));
27812 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27813 cp_lexer_consume_token (parser->lexer);
27814 else
27815 break;
27817 cp_parser_consume_semicolon_at_end_of_statement (parser);
27818 objc_add_dynamic_declaration (loc, list);
27822 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27824 /* Returns name of the next clause.
27825 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27826 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27827 returned and the token is consumed. */
27829 static pragma_omp_clause
27830 cp_parser_omp_clause_name (cp_parser *parser)
27832 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27835 result = PRAGMA_OMP_CLAUSE_IF;
27836 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27837 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27838 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27839 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27840 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27841 result = PRAGMA_OMP_CLAUSE_FOR;
27842 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27845 const char *p = IDENTIFIER_POINTER (id);
27847 switch (p[0])
27849 case 'a':
27850 if (!strcmp ("aligned", p))
27851 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27852 break;
27853 case 'c':
27854 if (!strcmp ("collapse", p))
27855 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27856 else if (!strcmp ("copyin", p))
27857 result = PRAGMA_OMP_CLAUSE_COPYIN;
27858 else if (!strcmp ("copyprivate", p))
27859 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27860 break;
27861 case 'd':
27862 if (!strcmp ("depend", p))
27863 result = PRAGMA_OMP_CLAUSE_DEPEND;
27864 else if (!strcmp ("device", p))
27865 result = PRAGMA_OMP_CLAUSE_DEVICE;
27866 else if (!strcmp ("dist_schedule", p))
27867 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27868 break;
27869 case 'f':
27870 if (!strcmp ("final", p))
27871 result = PRAGMA_OMP_CLAUSE_FINAL;
27872 else if (!strcmp ("firstprivate", p))
27873 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27874 else if (!strcmp ("from", p))
27875 result = PRAGMA_OMP_CLAUSE_FROM;
27876 break;
27877 case 'i':
27878 if (!strcmp ("inbranch", p))
27879 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27880 break;
27881 case 'l':
27882 if (!strcmp ("lastprivate", p))
27883 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27884 else if (!strcmp ("linear", p))
27885 result = PRAGMA_OMP_CLAUSE_LINEAR;
27886 break;
27887 case 'm':
27888 if (!strcmp ("map", p))
27889 result = PRAGMA_OMP_CLAUSE_MAP;
27890 else if (!strcmp ("mergeable", p))
27891 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27892 else if (flag_cilkplus && !strcmp ("mask", p))
27893 result = PRAGMA_CILK_CLAUSE_MASK;
27894 break;
27895 case 'n':
27896 if (!strcmp ("notinbranch", p))
27897 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27898 else if (!strcmp ("nowait", p))
27899 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27900 else if (flag_cilkplus && !strcmp ("nomask", p))
27901 result = PRAGMA_CILK_CLAUSE_NOMASK;
27902 else if (!strcmp ("num_teams", p))
27903 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27904 else if (!strcmp ("num_threads", p))
27905 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27906 break;
27907 case 'o':
27908 if (!strcmp ("ordered", p))
27909 result = PRAGMA_OMP_CLAUSE_ORDERED;
27910 break;
27911 case 'p':
27912 if (!strcmp ("parallel", p))
27913 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27914 else if (!strcmp ("proc_bind", p))
27915 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27916 break;
27917 case 'r':
27918 if (!strcmp ("reduction", p))
27919 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27920 break;
27921 case 's':
27922 if (!strcmp ("safelen", p))
27923 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27924 else if (!strcmp ("schedule", p))
27925 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27926 else if (!strcmp ("sections", p))
27927 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27928 else if (!strcmp ("shared", p))
27929 result = PRAGMA_OMP_CLAUSE_SHARED;
27930 else if (!strcmp ("simdlen", p))
27931 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27932 break;
27933 case 't':
27934 if (!strcmp ("taskgroup", p))
27935 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27936 else if (!strcmp ("thread_limit", p))
27937 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27938 else if (!strcmp ("to", p))
27939 result = PRAGMA_OMP_CLAUSE_TO;
27940 break;
27941 case 'u':
27942 if (!strcmp ("uniform", p))
27943 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27944 else if (!strcmp ("untied", p))
27945 result = PRAGMA_OMP_CLAUSE_UNTIED;
27946 break;
27947 case 'v':
27948 if (flag_cilkplus && !strcmp ("vectorlength", p))
27949 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27950 break;
27954 if (result != PRAGMA_OMP_CLAUSE_NONE)
27955 cp_lexer_consume_token (parser->lexer);
27957 return result;
27960 /* Validate that a clause of the given type does not already exist. */
27962 static void
27963 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27964 const char *name, location_t location)
27966 tree c;
27968 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27969 if (OMP_CLAUSE_CODE (c) == code)
27971 error_at (location, "too many %qs clauses", name);
27972 break;
27976 /* OpenMP 2.5:
27977 variable-list:
27978 identifier
27979 variable-list , identifier
27981 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27982 colon). An opening parenthesis will have been consumed by the caller.
27984 If KIND is nonzero, create the appropriate node and install the decl
27985 in OMP_CLAUSE_DECL and add the node to the head of the list.
27987 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27988 return the list created.
27990 COLON can be NULL if only closing parenthesis should end the list,
27991 or pointer to bool which will receive false if the list is terminated
27992 by closing parenthesis or true if the list is terminated by colon. */
27994 static tree
27995 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27996 tree list, bool *colon)
27998 cp_token *token;
27999 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28000 if (colon)
28002 parser->colon_corrects_to_scope_p = false;
28003 *colon = false;
28005 while (1)
28007 tree name, decl;
28009 token = cp_lexer_peek_token (parser->lexer);
28010 name = cp_parser_id_expression (parser, /*template_p=*/false,
28011 /*check_dependency_p=*/true,
28012 /*template_p=*/NULL,
28013 /*declarator_p=*/false,
28014 /*optional_p=*/false);
28015 if (name == error_mark_node)
28016 goto skip_comma;
28018 decl = cp_parser_lookup_name_simple (parser, name, token->location);
28019 if (decl == error_mark_node)
28020 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
28021 token->location);
28022 else if (kind != 0)
28024 switch (kind)
28026 case OMP_CLAUSE_MAP:
28027 case OMP_CLAUSE_FROM:
28028 case OMP_CLAUSE_TO:
28029 case OMP_CLAUSE_DEPEND:
28030 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28032 tree low_bound = NULL_TREE, length = NULL_TREE;
28034 parser->colon_corrects_to_scope_p = false;
28035 cp_lexer_consume_token (parser->lexer);
28036 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28037 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
28038 NULL);
28039 if (!colon)
28040 parser->colon_corrects_to_scope_p
28041 = saved_colon_corrects_to_scope_p;
28042 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
28043 length = integer_one_node;
28044 else
28046 /* Look for `:'. */
28047 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28048 goto skip_comma;
28049 if (!cp_lexer_next_token_is (parser->lexer,
28050 CPP_CLOSE_SQUARE))
28051 length = cp_parser_expression (parser,
28052 /*cast_p=*/false,
28053 NULL);
28055 /* Look for the closing `]'. */
28056 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
28057 RT_CLOSE_SQUARE))
28058 goto skip_comma;
28059 decl = tree_cons (low_bound, length, decl);
28061 break;
28062 default:
28063 break;
28066 tree u = build_omp_clause (token->location, kind);
28067 OMP_CLAUSE_DECL (u) = decl;
28068 OMP_CLAUSE_CHAIN (u) = list;
28069 list = u;
28071 else
28072 list = tree_cons (decl, NULL_TREE, list);
28074 get_comma:
28075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28076 break;
28077 cp_lexer_consume_token (parser->lexer);
28080 if (colon)
28081 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28083 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28085 *colon = true;
28086 cp_parser_require (parser, CPP_COLON, RT_COLON);
28087 return list;
28090 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28092 int ending;
28094 /* Try to resync to an unnested comma. Copied from
28095 cp_parser_parenthesized_expression_list. */
28096 skip_comma:
28097 if (colon)
28098 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28099 ending = cp_parser_skip_to_closing_parenthesis (parser,
28100 /*recovering=*/true,
28101 /*or_comma=*/true,
28102 /*consume_paren=*/true);
28103 if (ending < 0)
28104 goto get_comma;
28107 return list;
28110 /* Similarly, but expect leading and trailing parenthesis. This is a very
28111 common case for omp clauses. */
28113 static tree
28114 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28116 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28117 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28118 return list;
28121 /* OpenMP 3.0:
28122 collapse ( constant-expression ) */
28124 static tree
28125 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28127 tree c, num;
28128 location_t loc;
28129 HOST_WIDE_INT n;
28131 loc = cp_lexer_peek_token (parser->lexer)->location;
28132 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28133 return list;
28135 num = cp_parser_constant_expression (parser, false, NULL);
28137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28139 /*or_comma=*/false,
28140 /*consume_paren=*/true);
28142 if (num == error_mark_node)
28143 return list;
28144 num = fold_non_dependent_expr (num);
28145 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28146 || !tree_fits_shwi_p (num)
28147 || (n = tree_to_shwi (num)) <= 0
28148 || (int) n != n)
28150 error_at (loc, "collapse argument needs positive constant integer expression");
28151 return list;
28154 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28155 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28156 OMP_CLAUSE_CHAIN (c) = list;
28157 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28159 return c;
28162 /* OpenMP 2.5:
28163 default ( shared | none ) */
28165 static tree
28166 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28168 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28169 tree c;
28171 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28172 return list;
28173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28175 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28176 const char *p = IDENTIFIER_POINTER (id);
28178 switch (p[0])
28180 case 'n':
28181 if (strcmp ("none", p) != 0)
28182 goto invalid_kind;
28183 kind = OMP_CLAUSE_DEFAULT_NONE;
28184 break;
28186 case 's':
28187 if (strcmp ("shared", p) != 0)
28188 goto invalid_kind;
28189 kind = OMP_CLAUSE_DEFAULT_SHARED;
28190 break;
28192 default:
28193 goto invalid_kind;
28196 cp_lexer_consume_token (parser->lexer);
28198 else
28200 invalid_kind:
28201 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28204 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28205 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28206 /*or_comma=*/false,
28207 /*consume_paren=*/true);
28209 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28210 return list;
28212 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28213 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28214 OMP_CLAUSE_CHAIN (c) = list;
28215 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28217 return c;
28220 /* OpenMP 3.1:
28221 final ( expression ) */
28223 static tree
28224 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28226 tree t, c;
28228 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28229 return list;
28231 t = cp_parser_condition (parser);
28233 if (t == error_mark_node
28234 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28235 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28236 /*or_comma=*/false,
28237 /*consume_paren=*/true);
28239 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28241 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28242 OMP_CLAUSE_FINAL_EXPR (c) = t;
28243 OMP_CLAUSE_CHAIN (c) = list;
28245 return c;
28248 /* OpenMP 2.5:
28249 if ( expression ) */
28251 static tree
28252 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28254 tree t, c;
28256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28257 return list;
28259 t = cp_parser_condition (parser);
28261 if (t == error_mark_node
28262 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28264 /*or_comma=*/false,
28265 /*consume_paren=*/true);
28267 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28269 c = build_omp_clause (location, OMP_CLAUSE_IF);
28270 OMP_CLAUSE_IF_EXPR (c) = t;
28271 OMP_CLAUSE_CHAIN (c) = list;
28273 return c;
28276 /* OpenMP 3.1:
28277 mergeable */
28279 static tree
28280 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28281 tree list, location_t location)
28283 tree c;
28285 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28286 location);
28288 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28289 OMP_CLAUSE_CHAIN (c) = list;
28290 return c;
28293 /* OpenMP 2.5:
28294 nowait */
28296 static tree
28297 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28298 tree list, location_t location)
28300 tree c;
28302 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28304 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28305 OMP_CLAUSE_CHAIN (c) = list;
28306 return c;
28309 /* OpenMP 2.5:
28310 num_threads ( expression ) */
28312 static tree
28313 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28314 location_t location)
28316 tree t, c;
28318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28319 return list;
28321 t = cp_parser_expression (parser, false, NULL);
28323 if (t == error_mark_node
28324 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28326 /*or_comma=*/false,
28327 /*consume_paren=*/true);
28329 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28330 "num_threads", location);
28332 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28333 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28334 OMP_CLAUSE_CHAIN (c) = list;
28336 return c;
28339 /* OpenMP 2.5:
28340 ordered */
28342 static tree
28343 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28344 tree list, location_t location)
28346 tree c;
28348 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28349 "ordered", location);
28351 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28352 OMP_CLAUSE_CHAIN (c) = list;
28353 return c;
28356 /* OpenMP 2.5:
28357 reduction ( reduction-operator : variable-list )
28359 reduction-operator:
28360 One of: + * - & ^ | && ||
28362 OpenMP 3.1:
28364 reduction-operator:
28365 One of: + * - & ^ | && || min max
28367 OpenMP 4.0:
28369 reduction-operator:
28370 One of: + * - & ^ | && ||
28371 id-expression */
28373 static tree
28374 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28376 enum tree_code code = ERROR_MARK;
28377 tree nlist, c, id = NULL_TREE;
28379 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28380 return list;
28382 switch (cp_lexer_peek_token (parser->lexer)->type)
28384 case CPP_PLUS: code = PLUS_EXPR; break;
28385 case CPP_MULT: code = MULT_EXPR; break;
28386 case CPP_MINUS: code = MINUS_EXPR; break;
28387 case CPP_AND: code = BIT_AND_EXPR; break;
28388 case CPP_XOR: code = BIT_XOR_EXPR; break;
28389 case CPP_OR: code = BIT_IOR_EXPR; break;
28390 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28391 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28392 default: break;
28395 if (code != ERROR_MARK)
28396 cp_lexer_consume_token (parser->lexer);
28397 else
28399 bool saved_colon_corrects_to_scope_p;
28400 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28401 parser->colon_corrects_to_scope_p = false;
28402 id = cp_parser_id_expression (parser, /*template_p=*/false,
28403 /*check_dependency_p=*/true,
28404 /*template_p=*/NULL,
28405 /*declarator_p=*/false,
28406 /*optional_p=*/false);
28407 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28408 if (identifier_p (id))
28410 const char *p = IDENTIFIER_POINTER (id);
28412 if (strcmp (p, "min") == 0)
28413 code = MIN_EXPR;
28414 else if (strcmp (p, "max") == 0)
28415 code = MAX_EXPR;
28416 else if (id == ansi_opname (PLUS_EXPR))
28417 code = PLUS_EXPR;
28418 else if (id == ansi_opname (MULT_EXPR))
28419 code = MULT_EXPR;
28420 else if (id == ansi_opname (MINUS_EXPR))
28421 code = MINUS_EXPR;
28422 else if (id == ansi_opname (BIT_AND_EXPR))
28423 code = BIT_AND_EXPR;
28424 else if (id == ansi_opname (BIT_IOR_EXPR))
28425 code = BIT_IOR_EXPR;
28426 else if (id == ansi_opname (BIT_XOR_EXPR))
28427 code = BIT_XOR_EXPR;
28428 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28429 code = TRUTH_ANDIF_EXPR;
28430 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28431 code = TRUTH_ORIF_EXPR;
28432 id = omp_reduction_id (code, id, NULL_TREE);
28433 tree scope = parser->scope;
28434 if (scope)
28435 id = build_qualified_name (NULL_TREE, scope, id, false);
28436 parser->scope = NULL_TREE;
28437 parser->qualifying_scope = NULL_TREE;
28438 parser->object_scope = NULL_TREE;
28440 else
28442 error ("invalid reduction-identifier");
28443 resync_fail:
28444 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28445 /*or_comma=*/false,
28446 /*consume_paren=*/true);
28447 return list;
28451 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28452 goto resync_fail;
28454 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28455 NULL);
28456 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28458 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28459 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28462 return nlist;
28465 /* OpenMP 2.5:
28466 schedule ( schedule-kind )
28467 schedule ( schedule-kind , expression )
28469 schedule-kind:
28470 static | dynamic | guided | runtime | auto */
28472 static tree
28473 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28475 tree c, t;
28477 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28478 return list;
28480 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28485 const char *p = IDENTIFIER_POINTER (id);
28487 switch (p[0])
28489 case 'd':
28490 if (strcmp ("dynamic", p) != 0)
28491 goto invalid_kind;
28492 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28493 break;
28495 case 'g':
28496 if (strcmp ("guided", p) != 0)
28497 goto invalid_kind;
28498 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28499 break;
28501 case 'r':
28502 if (strcmp ("runtime", p) != 0)
28503 goto invalid_kind;
28504 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28505 break;
28507 default:
28508 goto invalid_kind;
28511 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28512 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28513 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28514 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28515 else
28516 goto invalid_kind;
28517 cp_lexer_consume_token (parser->lexer);
28519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28521 cp_token *token;
28522 cp_lexer_consume_token (parser->lexer);
28524 token = cp_lexer_peek_token (parser->lexer);
28525 t = cp_parser_assignment_expression (parser, false, NULL);
28527 if (t == error_mark_node)
28528 goto resync_fail;
28529 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28530 error_at (token->location, "schedule %<runtime%> does not take "
28531 "a %<chunk_size%> parameter");
28532 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28533 error_at (token->location, "schedule %<auto%> does not take "
28534 "a %<chunk_size%> parameter");
28535 else
28536 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28538 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28539 goto resync_fail;
28541 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28542 goto resync_fail;
28544 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28545 OMP_CLAUSE_CHAIN (c) = list;
28546 return c;
28548 invalid_kind:
28549 cp_parser_error (parser, "invalid schedule kind");
28550 resync_fail:
28551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28552 /*or_comma=*/false,
28553 /*consume_paren=*/true);
28554 return list;
28557 /* OpenMP 3.0:
28558 untied */
28560 static tree
28561 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28562 tree list, location_t location)
28564 tree c;
28566 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28568 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28569 OMP_CLAUSE_CHAIN (c) = list;
28570 return c;
28573 /* OpenMP 4.0:
28574 inbranch
28575 notinbranch */
28577 static tree
28578 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28579 tree list, location_t location)
28581 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28582 tree c = build_omp_clause (location, code);
28583 OMP_CLAUSE_CHAIN (c) = list;
28584 return c;
28587 /* OpenMP 4.0:
28588 parallel
28590 sections
28591 taskgroup */
28593 static tree
28594 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28595 enum omp_clause_code code,
28596 tree list, location_t location)
28598 tree c = build_omp_clause (location, code);
28599 OMP_CLAUSE_CHAIN (c) = list;
28600 return c;
28603 /* OpenMP 4.0:
28604 num_teams ( expression ) */
28606 static tree
28607 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28608 location_t location)
28610 tree t, c;
28612 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28613 return list;
28615 t = cp_parser_expression (parser, false, NULL);
28617 if (t == error_mark_node
28618 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28620 /*or_comma=*/false,
28621 /*consume_paren=*/true);
28623 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28624 "num_teams", location);
28626 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28627 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28628 OMP_CLAUSE_CHAIN (c) = list;
28630 return c;
28633 /* OpenMP 4.0:
28634 thread_limit ( expression ) */
28636 static tree
28637 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28638 location_t location)
28640 tree t, c;
28642 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28643 return list;
28645 t = cp_parser_expression (parser, false, NULL);
28647 if (t == error_mark_node
28648 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28650 /*or_comma=*/false,
28651 /*consume_paren=*/true);
28653 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28654 "thread_limit", location);
28656 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28657 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28658 OMP_CLAUSE_CHAIN (c) = list;
28660 return c;
28663 /* OpenMP 4.0:
28664 aligned ( variable-list )
28665 aligned ( variable-list : constant-expression ) */
28667 static tree
28668 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28670 tree nlist, c, alignment = NULL_TREE;
28671 bool colon;
28673 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28674 return list;
28676 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28677 &colon);
28679 if (colon)
28681 alignment = cp_parser_constant_expression (parser, false, NULL);
28683 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28685 /*or_comma=*/false,
28686 /*consume_paren=*/true);
28688 if (alignment == error_mark_node)
28689 alignment = NULL_TREE;
28692 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28693 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28695 return nlist;
28698 /* OpenMP 4.0:
28699 linear ( variable-list )
28700 linear ( variable-list : expression ) */
28702 static tree
28703 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28704 bool is_cilk_simd_fn)
28706 tree nlist, c, step = integer_one_node;
28707 bool colon;
28709 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28710 return list;
28712 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28713 &colon);
28715 if (colon)
28717 step = cp_parser_expression (parser, false, NULL);
28719 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28721 sorry ("using parameters for %<linear%> step is not supported yet");
28722 step = integer_one_node;
28724 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28725 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28726 /*or_comma=*/false,
28727 /*consume_paren=*/true);
28729 if (step == error_mark_node)
28730 return list;
28733 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28734 OMP_CLAUSE_LINEAR_STEP (c) = step;
28736 return nlist;
28739 /* OpenMP 4.0:
28740 safelen ( constant-expression ) */
28742 static tree
28743 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28744 location_t location)
28746 tree t, c;
28748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28749 return list;
28751 t = cp_parser_constant_expression (parser, false, NULL);
28753 if (t == error_mark_node
28754 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28756 /*or_comma=*/false,
28757 /*consume_paren=*/true);
28759 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28761 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28762 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28763 OMP_CLAUSE_CHAIN (c) = list;
28765 return c;
28768 /* OpenMP 4.0:
28769 simdlen ( constant-expression ) */
28771 static tree
28772 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28773 location_t location)
28775 tree t, c;
28777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28778 return list;
28780 t = cp_parser_constant_expression (parser, false, NULL);
28782 if (t == error_mark_node
28783 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28785 /*or_comma=*/false,
28786 /*consume_paren=*/true);
28788 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28790 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28791 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28792 OMP_CLAUSE_CHAIN (c) = list;
28794 return c;
28797 /* OpenMP 4.0:
28798 depend ( depend-kind : variable-list )
28800 depend-kind:
28801 in | out | inout */
28803 static tree
28804 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28806 tree nlist, c;
28807 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28809 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28810 return list;
28812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28814 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28815 const char *p = IDENTIFIER_POINTER (id);
28817 if (strcmp ("in", p) == 0)
28818 kind = OMP_CLAUSE_DEPEND_IN;
28819 else if (strcmp ("inout", p) == 0)
28820 kind = OMP_CLAUSE_DEPEND_INOUT;
28821 else if (strcmp ("out", p) == 0)
28822 kind = OMP_CLAUSE_DEPEND_OUT;
28823 else
28824 goto invalid_kind;
28826 else
28827 goto invalid_kind;
28829 cp_lexer_consume_token (parser->lexer);
28830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28831 goto resync_fail;
28833 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28834 NULL);
28836 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28837 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28839 return nlist;
28841 invalid_kind:
28842 cp_parser_error (parser, "invalid depend kind");
28843 resync_fail:
28844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28845 /*or_comma=*/false,
28846 /*consume_paren=*/true);
28847 return list;
28850 /* OpenMP 4.0:
28851 map ( map-kind : variable-list )
28852 map ( variable-list )
28854 map-kind:
28855 alloc | to | from | tofrom */
28857 static tree
28858 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28860 tree nlist, c;
28861 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28864 return list;
28866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28867 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28870 const char *p = IDENTIFIER_POINTER (id);
28872 if (strcmp ("alloc", p) == 0)
28873 kind = OMP_CLAUSE_MAP_ALLOC;
28874 else if (strcmp ("to", p) == 0)
28875 kind = OMP_CLAUSE_MAP_TO;
28876 else if (strcmp ("from", p) == 0)
28877 kind = OMP_CLAUSE_MAP_FROM;
28878 else if (strcmp ("tofrom", p) == 0)
28879 kind = OMP_CLAUSE_MAP_TOFROM;
28880 else
28882 cp_parser_error (parser, "invalid map kind");
28883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28884 /*or_comma=*/false,
28885 /*consume_paren=*/true);
28886 return list;
28888 cp_lexer_consume_token (parser->lexer);
28889 cp_lexer_consume_token (parser->lexer);
28892 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28893 NULL);
28895 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28896 OMP_CLAUSE_MAP_KIND (c) = kind;
28898 return nlist;
28901 /* OpenMP 4.0:
28902 device ( expression ) */
28904 static tree
28905 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28906 location_t location)
28908 tree t, c;
28910 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28911 return list;
28913 t = cp_parser_expression (parser, false, NULL);
28915 if (t == error_mark_node
28916 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28918 /*or_comma=*/false,
28919 /*consume_paren=*/true);
28921 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28922 "device", location);
28924 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28925 OMP_CLAUSE_DEVICE_ID (c) = t;
28926 OMP_CLAUSE_CHAIN (c) = list;
28928 return c;
28931 /* OpenMP 4.0:
28932 dist_schedule ( static )
28933 dist_schedule ( static , expression ) */
28935 static tree
28936 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28937 location_t location)
28939 tree c, t;
28941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28942 return list;
28944 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28946 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28947 goto invalid_kind;
28948 cp_lexer_consume_token (parser->lexer);
28950 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28952 cp_lexer_consume_token (parser->lexer);
28954 t = cp_parser_assignment_expression (parser, false, NULL);
28956 if (t == error_mark_node)
28957 goto resync_fail;
28958 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28960 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28961 goto resync_fail;
28963 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28964 goto resync_fail;
28966 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28967 location);
28968 OMP_CLAUSE_CHAIN (c) = list;
28969 return c;
28971 invalid_kind:
28972 cp_parser_error (parser, "invalid dist_schedule kind");
28973 resync_fail:
28974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28975 /*or_comma=*/false,
28976 /*consume_paren=*/true);
28977 return list;
28980 /* OpenMP 4.0:
28981 proc_bind ( proc-bind-kind )
28983 proc-bind-kind:
28984 master | close | spread */
28986 static tree
28987 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28988 location_t location)
28990 tree c;
28991 enum omp_clause_proc_bind_kind kind;
28993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28994 return list;
28996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28999 const char *p = IDENTIFIER_POINTER (id);
29001 if (strcmp ("master", p) == 0)
29002 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29003 else if (strcmp ("close", p) == 0)
29004 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29005 else if (strcmp ("spread", p) == 0)
29006 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29007 else
29008 goto invalid_kind;
29010 else
29011 goto invalid_kind;
29013 cp_lexer_consume_token (parser->lexer);
29014 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29015 goto resync_fail;
29017 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29018 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29019 location);
29020 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29021 OMP_CLAUSE_CHAIN (c) = list;
29022 return c;
29024 invalid_kind:
29025 cp_parser_error (parser, "invalid depend kind");
29026 resync_fail:
29027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29028 /*or_comma=*/false,
29029 /*consume_paren=*/true);
29030 return list;
29033 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29034 is a bitmask in MASK. Return the list of clauses found; the result
29035 of clause default goes in *pdefault. */
29037 static tree
29038 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29039 const char *where, cp_token *pragma_tok,
29040 bool finish_p = true)
29042 tree clauses = NULL;
29043 bool first = true;
29044 cp_token *token = NULL;
29045 bool cilk_simd_fn = false;
29047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29049 pragma_omp_clause c_kind;
29050 const char *c_name;
29051 tree prev = clauses;
29053 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29054 cp_lexer_consume_token (parser->lexer);
29056 token = cp_lexer_peek_token (parser->lexer);
29057 c_kind = cp_parser_omp_clause_name (parser);
29059 switch (c_kind)
29061 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29062 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29063 token->location);
29064 c_name = "collapse";
29065 break;
29066 case PRAGMA_OMP_CLAUSE_COPYIN:
29067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29068 c_name = "copyin";
29069 break;
29070 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29072 clauses);
29073 c_name = "copyprivate";
29074 break;
29075 case PRAGMA_OMP_CLAUSE_DEFAULT:
29076 clauses = cp_parser_omp_clause_default (parser, clauses,
29077 token->location);
29078 c_name = "default";
29079 break;
29080 case PRAGMA_OMP_CLAUSE_FINAL:
29081 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29082 c_name = "final";
29083 break;
29084 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29085 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29086 clauses);
29087 c_name = "firstprivate";
29088 break;
29089 case PRAGMA_OMP_CLAUSE_IF:
29090 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29091 c_name = "if";
29092 break;
29093 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29094 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29095 clauses);
29096 c_name = "lastprivate";
29097 break;
29098 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29099 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29100 token->location);
29101 c_name = "mergeable";
29102 break;
29103 case PRAGMA_OMP_CLAUSE_NOWAIT:
29104 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29105 c_name = "nowait";
29106 break;
29107 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29108 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29109 token->location);
29110 c_name = "num_threads";
29111 break;
29112 case PRAGMA_OMP_CLAUSE_ORDERED:
29113 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29114 token->location);
29115 c_name = "ordered";
29116 break;
29117 case PRAGMA_OMP_CLAUSE_PRIVATE:
29118 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29119 clauses);
29120 c_name = "private";
29121 break;
29122 case PRAGMA_OMP_CLAUSE_REDUCTION:
29123 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29124 c_name = "reduction";
29125 break;
29126 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29127 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29128 token->location);
29129 c_name = "schedule";
29130 break;
29131 case PRAGMA_OMP_CLAUSE_SHARED:
29132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29133 clauses);
29134 c_name = "shared";
29135 break;
29136 case PRAGMA_OMP_CLAUSE_UNTIED:
29137 clauses = cp_parser_omp_clause_untied (parser, clauses,
29138 token->location);
29139 c_name = "untied";
29140 break;
29141 case PRAGMA_OMP_CLAUSE_INBRANCH:
29142 case PRAGMA_CILK_CLAUSE_MASK:
29143 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29144 clauses, token->location);
29145 c_name = "inbranch";
29146 break;
29147 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29148 case PRAGMA_CILK_CLAUSE_NOMASK:
29149 clauses = cp_parser_omp_clause_branch (parser,
29150 OMP_CLAUSE_NOTINBRANCH,
29151 clauses, token->location);
29152 c_name = "notinbranch";
29153 break;
29154 case PRAGMA_OMP_CLAUSE_PARALLEL:
29155 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29156 clauses, token->location);
29157 c_name = "parallel";
29158 if (!first)
29160 clause_not_first:
29161 error_at (token->location, "%qs must be the first clause of %qs",
29162 c_name, where);
29163 clauses = prev;
29165 break;
29166 case PRAGMA_OMP_CLAUSE_FOR:
29167 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29168 clauses, token->location);
29169 c_name = "for";
29170 if (!first)
29171 goto clause_not_first;
29172 break;
29173 case PRAGMA_OMP_CLAUSE_SECTIONS:
29174 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29175 clauses, token->location);
29176 c_name = "sections";
29177 if (!first)
29178 goto clause_not_first;
29179 break;
29180 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29181 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29182 clauses, token->location);
29183 c_name = "taskgroup";
29184 if (!first)
29185 goto clause_not_first;
29186 break;
29187 case PRAGMA_OMP_CLAUSE_TO:
29188 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29189 clauses);
29190 c_name = "to";
29191 break;
29192 case PRAGMA_OMP_CLAUSE_FROM:
29193 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29194 clauses);
29195 c_name = "from";
29196 break;
29197 case PRAGMA_OMP_CLAUSE_UNIFORM:
29198 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29199 clauses);
29200 c_name = "uniform";
29201 break;
29202 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29203 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29204 token->location);
29205 c_name = "num_teams";
29206 break;
29207 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29208 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29209 token->location);
29210 c_name = "thread_limit";
29211 break;
29212 case PRAGMA_OMP_CLAUSE_ALIGNED:
29213 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29214 c_name = "aligned";
29215 break;
29216 case PRAGMA_OMP_CLAUSE_LINEAR:
29217 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29218 cilk_simd_fn = true;
29219 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29220 c_name = "linear";
29221 break;
29222 case PRAGMA_OMP_CLAUSE_DEPEND:
29223 clauses = cp_parser_omp_clause_depend (parser, clauses);
29224 c_name = "depend";
29225 break;
29226 case PRAGMA_OMP_CLAUSE_MAP:
29227 clauses = cp_parser_omp_clause_map (parser, clauses);
29228 c_name = "map";
29229 break;
29230 case PRAGMA_OMP_CLAUSE_DEVICE:
29231 clauses = cp_parser_omp_clause_device (parser, clauses,
29232 token->location);
29233 c_name = "device";
29234 break;
29235 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29236 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29237 token->location);
29238 c_name = "dist_schedule";
29239 break;
29240 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29241 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29242 token->location);
29243 c_name = "proc_bind";
29244 break;
29245 case PRAGMA_OMP_CLAUSE_SAFELEN:
29246 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29247 token->location);
29248 c_name = "safelen";
29249 break;
29250 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29251 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29252 token->location);
29253 c_name = "simdlen";
29254 break;
29255 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29256 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29257 c_name = "simdlen";
29258 break;
29259 default:
29260 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29261 goto saw_error;
29264 first = false;
29266 if (((mask >> c_kind) & 1) == 0)
29268 /* Remove the invalid clause(s) from the list to avoid
29269 confusing the rest of the compiler. */
29270 clauses = prev;
29271 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29274 saw_error:
29275 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29276 no reason to skip to the end. */
29277 if (!(flag_cilkplus && pragma_tok == NULL))
29278 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29279 if (finish_p)
29280 return finish_omp_clauses (clauses);
29281 return clauses;
29284 /* OpenMP 2.5:
29285 structured-block:
29286 statement
29288 In practice, we're also interested in adding the statement to an
29289 outer node. So it is convenient if we work around the fact that
29290 cp_parser_statement calls add_stmt. */
29292 static unsigned
29293 cp_parser_begin_omp_structured_block (cp_parser *parser)
29295 unsigned save = parser->in_statement;
29297 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29298 This preserves the "not within loop or switch" style error messages
29299 for nonsense cases like
29300 void foo() {
29301 #pragma omp single
29302 break;
29305 if (parser->in_statement)
29306 parser->in_statement = IN_OMP_BLOCK;
29308 return save;
29311 static void
29312 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29314 parser->in_statement = save;
29317 static tree
29318 cp_parser_omp_structured_block (cp_parser *parser)
29320 tree stmt = begin_omp_structured_block ();
29321 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29323 cp_parser_statement (parser, NULL_TREE, false, NULL);
29325 cp_parser_end_omp_structured_block (parser, save);
29326 return finish_omp_structured_block (stmt);
29329 /* OpenMP 2.5:
29330 # pragma omp atomic new-line
29331 expression-stmt
29333 expression-stmt:
29334 x binop= expr | x++ | ++x | x-- | --x
29335 binop:
29336 +, *, -, /, &, ^, |, <<, >>
29338 where x is an lvalue expression with scalar type.
29340 OpenMP 3.1:
29341 # pragma omp atomic new-line
29342 update-stmt
29344 # pragma omp atomic read new-line
29345 read-stmt
29347 # pragma omp atomic write new-line
29348 write-stmt
29350 # pragma omp atomic update new-line
29351 update-stmt
29353 # pragma omp atomic capture new-line
29354 capture-stmt
29356 # pragma omp atomic capture new-line
29357 capture-block
29359 read-stmt:
29360 v = x
29361 write-stmt:
29362 x = expr
29363 update-stmt:
29364 expression-stmt | x = x binop expr
29365 capture-stmt:
29366 v = expression-stmt
29367 capture-block:
29368 { v = x; update-stmt; } | { update-stmt; v = x; }
29370 OpenMP 4.0:
29371 update-stmt:
29372 expression-stmt | x = x binop expr | x = expr binop x
29373 capture-stmt:
29374 v = update-stmt
29375 capture-block:
29376 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29378 where x and v are lvalue expressions with scalar type. */
29380 static void
29381 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29383 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29384 tree rhs1 = NULL_TREE, orig_lhs;
29385 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29386 bool structured_block = false;
29387 bool seq_cst = false;
29389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29392 const char *p = IDENTIFIER_POINTER (id);
29394 if (!strcmp (p, "seq_cst"))
29396 seq_cst = true;
29397 cp_lexer_consume_token (parser->lexer);
29398 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29399 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29400 cp_lexer_consume_token (parser->lexer);
29403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29405 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29406 const char *p = IDENTIFIER_POINTER (id);
29408 if (!strcmp (p, "read"))
29409 code = OMP_ATOMIC_READ;
29410 else if (!strcmp (p, "write"))
29411 code = NOP_EXPR;
29412 else if (!strcmp (p, "update"))
29413 code = OMP_ATOMIC;
29414 else if (!strcmp (p, "capture"))
29415 code = OMP_ATOMIC_CAPTURE_NEW;
29416 else
29417 p = NULL;
29418 if (p)
29419 cp_lexer_consume_token (parser->lexer);
29421 if (!seq_cst)
29423 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29424 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29425 cp_lexer_consume_token (parser->lexer);
29427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29429 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29430 const char *p = IDENTIFIER_POINTER (id);
29432 if (!strcmp (p, "seq_cst"))
29434 seq_cst = true;
29435 cp_lexer_consume_token (parser->lexer);
29439 cp_parser_require_pragma_eol (parser, pragma_tok);
29441 switch (code)
29443 case OMP_ATOMIC_READ:
29444 case NOP_EXPR: /* atomic write */
29445 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29446 /*cast_p=*/false, NULL);
29447 if (v == error_mark_node)
29448 goto saw_error;
29449 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29450 goto saw_error;
29451 if (code == NOP_EXPR)
29452 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29453 else
29454 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
29455 /*cast_p=*/false, NULL);
29456 if (lhs == error_mark_node)
29457 goto saw_error;
29458 if (code == NOP_EXPR)
29460 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29461 opcode. */
29462 code = OMP_ATOMIC;
29463 rhs = lhs;
29464 lhs = v;
29465 v = NULL_TREE;
29467 goto done;
29468 case OMP_ATOMIC_CAPTURE_NEW:
29469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29471 cp_lexer_consume_token (parser->lexer);
29472 structured_block = true;
29474 else
29476 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29477 /*cast_p=*/false, NULL);
29478 if (v == error_mark_node)
29479 goto saw_error;
29480 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29481 goto saw_error;
29483 default:
29484 break;
29487 restart:
29488 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
29489 /*cast_p=*/false, NULL);
29490 orig_lhs = lhs;
29491 switch (TREE_CODE (lhs))
29493 case ERROR_MARK:
29494 goto saw_error;
29496 case POSTINCREMENT_EXPR:
29497 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29498 code = OMP_ATOMIC_CAPTURE_OLD;
29499 /* FALLTHROUGH */
29500 case PREINCREMENT_EXPR:
29501 lhs = TREE_OPERAND (lhs, 0);
29502 opcode = PLUS_EXPR;
29503 rhs = integer_one_node;
29504 break;
29506 case POSTDECREMENT_EXPR:
29507 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29508 code = OMP_ATOMIC_CAPTURE_OLD;
29509 /* FALLTHROUGH */
29510 case PREDECREMENT_EXPR:
29511 lhs = TREE_OPERAND (lhs, 0);
29512 opcode = MINUS_EXPR;
29513 rhs = integer_one_node;
29514 break;
29516 case COMPOUND_EXPR:
29517 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29518 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29519 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29520 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29521 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29522 (TREE_OPERAND (lhs, 1), 0), 0)))
29523 == BOOLEAN_TYPE)
29524 /* Undo effects of boolean_increment for post {in,de}crement. */
29525 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29526 /* FALLTHRU */
29527 case MODIFY_EXPR:
29528 if (TREE_CODE (lhs) == MODIFY_EXPR
29529 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29531 /* Undo effects of boolean_increment. */
29532 if (integer_onep (TREE_OPERAND (lhs, 1)))
29534 /* This is pre or post increment. */
29535 rhs = TREE_OPERAND (lhs, 1);
29536 lhs = TREE_OPERAND (lhs, 0);
29537 opcode = NOP_EXPR;
29538 if (code == OMP_ATOMIC_CAPTURE_NEW
29539 && !structured_block
29540 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29541 code = OMP_ATOMIC_CAPTURE_OLD;
29542 break;
29545 /* FALLTHRU */
29546 default:
29547 switch (cp_lexer_peek_token (parser->lexer)->type)
29549 case CPP_MULT_EQ:
29550 opcode = MULT_EXPR;
29551 break;
29552 case CPP_DIV_EQ:
29553 opcode = TRUNC_DIV_EXPR;
29554 break;
29555 case CPP_PLUS_EQ:
29556 opcode = PLUS_EXPR;
29557 break;
29558 case CPP_MINUS_EQ:
29559 opcode = MINUS_EXPR;
29560 break;
29561 case CPP_LSHIFT_EQ:
29562 opcode = LSHIFT_EXPR;
29563 break;
29564 case CPP_RSHIFT_EQ:
29565 opcode = RSHIFT_EXPR;
29566 break;
29567 case CPP_AND_EQ:
29568 opcode = BIT_AND_EXPR;
29569 break;
29570 case CPP_OR_EQ:
29571 opcode = BIT_IOR_EXPR;
29572 break;
29573 case CPP_XOR_EQ:
29574 opcode = BIT_XOR_EXPR;
29575 break;
29576 case CPP_EQ:
29577 enum cp_parser_prec oprec;
29578 cp_token *token;
29579 cp_lexer_consume_token (parser->lexer);
29580 cp_parser_parse_tentatively (parser);
29581 rhs1 = cp_parser_simple_cast_expression (parser);
29582 if (rhs1 == error_mark_node)
29584 cp_parser_abort_tentative_parse (parser);
29585 cp_parser_simple_cast_expression (parser);
29586 goto saw_error;
29588 token = cp_lexer_peek_token (parser->lexer);
29589 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29591 cp_parser_abort_tentative_parse (parser);
29592 cp_parser_parse_tentatively (parser);
29593 rhs = cp_parser_binary_expression (parser, false, true,
29594 PREC_NOT_OPERATOR, NULL);
29595 if (rhs == error_mark_node)
29597 cp_parser_abort_tentative_parse (parser);
29598 cp_parser_binary_expression (parser, false, true,
29599 PREC_NOT_OPERATOR, NULL);
29600 goto saw_error;
29602 switch (TREE_CODE (rhs))
29604 case MULT_EXPR:
29605 case TRUNC_DIV_EXPR:
29606 case PLUS_EXPR:
29607 case MINUS_EXPR:
29608 case LSHIFT_EXPR:
29609 case RSHIFT_EXPR:
29610 case BIT_AND_EXPR:
29611 case BIT_IOR_EXPR:
29612 case BIT_XOR_EXPR:
29613 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29615 if (cp_parser_parse_definitely (parser))
29617 opcode = TREE_CODE (rhs);
29618 rhs1 = TREE_OPERAND (rhs, 0);
29619 rhs = TREE_OPERAND (rhs, 1);
29620 goto stmt_done;
29622 else
29623 goto saw_error;
29625 break;
29626 default:
29627 break;
29629 cp_parser_abort_tentative_parse (parser);
29630 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29632 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29633 if (rhs == error_mark_node)
29634 goto saw_error;
29635 opcode = NOP_EXPR;
29636 rhs1 = NULL_TREE;
29637 goto stmt_done;
29639 cp_parser_error (parser,
29640 "invalid form of %<#pragma omp atomic%>");
29641 goto saw_error;
29643 if (!cp_parser_parse_definitely (parser))
29644 goto saw_error;
29645 switch (token->type)
29647 case CPP_SEMICOLON:
29648 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29650 code = OMP_ATOMIC_CAPTURE_OLD;
29651 v = lhs;
29652 lhs = NULL_TREE;
29653 lhs1 = rhs1;
29654 rhs1 = NULL_TREE;
29655 cp_lexer_consume_token (parser->lexer);
29656 goto restart;
29658 else if (structured_block)
29660 opcode = NOP_EXPR;
29661 rhs = rhs1;
29662 rhs1 = NULL_TREE;
29663 goto stmt_done;
29665 cp_parser_error (parser,
29666 "invalid form of %<#pragma omp atomic%>");
29667 goto saw_error;
29668 case CPP_MULT:
29669 opcode = MULT_EXPR;
29670 break;
29671 case CPP_DIV:
29672 opcode = TRUNC_DIV_EXPR;
29673 break;
29674 case CPP_PLUS:
29675 opcode = PLUS_EXPR;
29676 break;
29677 case CPP_MINUS:
29678 opcode = MINUS_EXPR;
29679 break;
29680 case CPP_LSHIFT:
29681 opcode = LSHIFT_EXPR;
29682 break;
29683 case CPP_RSHIFT:
29684 opcode = RSHIFT_EXPR;
29685 break;
29686 case CPP_AND:
29687 opcode = BIT_AND_EXPR;
29688 break;
29689 case CPP_OR:
29690 opcode = BIT_IOR_EXPR;
29691 break;
29692 case CPP_XOR:
29693 opcode = BIT_XOR_EXPR;
29694 break;
29695 default:
29696 cp_parser_error (parser,
29697 "invalid operator for %<#pragma omp atomic%>");
29698 goto saw_error;
29700 oprec = TOKEN_PRECEDENCE (token);
29701 gcc_assert (oprec != PREC_NOT_OPERATOR);
29702 if (commutative_tree_code (opcode))
29703 oprec = (enum cp_parser_prec) (oprec - 1);
29704 cp_lexer_consume_token (parser->lexer);
29705 rhs = cp_parser_binary_expression (parser, false, false,
29706 oprec, NULL);
29707 if (rhs == error_mark_node)
29708 goto saw_error;
29709 goto stmt_done;
29710 /* FALLTHROUGH */
29711 default:
29712 cp_parser_error (parser,
29713 "invalid operator for %<#pragma omp atomic%>");
29714 goto saw_error;
29716 cp_lexer_consume_token (parser->lexer);
29718 rhs = cp_parser_expression (parser, false, NULL);
29719 if (rhs == error_mark_node)
29720 goto saw_error;
29721 break;
29723 stmt_done:
29724 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29726 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29727 goto saw_error;
29728 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29729 /*cast_p=*/false, NULL);
29730 if (v == error_mark_node)
29731 goto saw_error;
29732 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29733 goto saw_error;
29734 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29735 /*cast_p=*/false, NULL);
29736 if (lhs1 == error_mark_node)
29737 goto saw_error;
29739 if (structured_block)
29741 cp_parser_consume_semicolon_at_end_of_statement (parser);
29742 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29744 done:
29745 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29746 if (!structured_block)
29747 cp_parser_consume_semicolon_at_end_of_statement (parser);
29748 return;
29750 saw_error:
29751 cp_parser_skip_to_end_of_block_or_statement (parser);
29752 if (structured_block)
29754 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29755 cp_lexer_consume_token (parser->lexer);
29756 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29758 cp_parser_skip_to_end_of_block_or_statement (parser);
29759 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29760 cp_lexer_consume_token (parser->lexer);
29766 /* OpenMP 2.5:
29767 # pragma omp barrier new-line */
29769 static void
29770 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29772 cp_parser_require_pragma_eol (parser, pragma_tok);
29773 finish_omp_barrier ();
29776 /* OpenMP 2.5:
29777 # pragma omp critical [(name)] new-line
29778 structured-block */
29780 static tree
29781 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29783 tree stmt, name = NULL;
29785 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29787 cp_lexer_consume_token (parser->lexer);
29789 name = cp_parser_identifier (parser);
29791 if (name == error_mark_node
29792 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29793 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29794 /*or_comma=*/false,
29795 /*consume_paren=*/true);
29796 if (name == error_mark_node)
29797 name = NULL;
29799 cp_parser_require_pragma_eol (parser, pragma_tok);
29801 stmt = cp_parser_omp_structured_block (parser);
29802 return c_finish_omp_critical (input_location, stmt, name);
29805 /* OpenMP 2.5:
29806 # pragma omp flush flush-vars[opt] new-line
29808 flush-vars:
29809 ( variable-list ) */
29811 static void
29812 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29815 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29816 cp_parser_require_pragma_eol (parser, pragma_tok);
29818 finish_omp_flush ();
29821 /* Helper function, to parse omp for increment expression. */
29823 static tree
29824 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29826 tree cond = cp_parser_binary_expression (parser, false, true,
29827 PREC_NOT_OPERATOR, NULL);
29828 if (cond == error_mark_node
29829 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29831 cp_parser_skip_to_end_of_statement (parser);
29832 return error_mark_node;
29835 switch (TREE_CODE (cond))
29837 case GT_EXPR:
29838 case GE_EXPR:
29839 case LT_EXPR:
29840 case LE_EXPR:
29841 break;
29842 case NE_EXPR:
29843 if (code == CILK_SIMD)
29844 break;
29845 /* Fall through: OpenMP disallows NE_EXPR. */
29846 default:
29847 return error_mark_node;
29850 /* If decl is an iterator, preserve LHS and RHS of the relational
29851 expr until finish_omp_for. */
29852 if (decl
29853 && (type_dependent_expression_p (decl)
29854 || CLASS_TYPE_P (TREE_TYPE (decl))))
29855 return cond;
29857 return build_x_binary_op (input_location, TREE_CODE (cond),
29858 TREE_OPERAND (cond, 0), ERROR_MARK,
29859 TREE_OPERAND (cond, 1), ERROR_MARK,
29860 /*overload=*/NULL, tf_warning_or_error);
29863 /* Helper function, to parse omp for increment expression. */
29865 static tree
29866 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29868 cp_token *token = cp_lexer_peek_token (parser->lexer);
29869 enum tree_code op;
29870 tree lhs, rhs;
29871 cp_id_kind idk;
29872 bool decl_first;
29874 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29876 op = (token->type == CPP_PLUS_PLUS
29877 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29878 cp_lexer_consume_token (parser->lexer);
29879 lhs = cp_parser_simple_cast_expression (parser);
29880 if (lhs != decl)
29881 return error_mark_node;
29882 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29885 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29886 if (lhs != decl)
29887 return error_mark_node;
29889 token = cp_lexer_peek_token (parser->lexer);
29890 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29892 op = (token->type == CPP_PLUS_PLUS
29893 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29894 cp_lexer_consume_token (parser->lexer);
29895 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29898 op = cp_parser_assignment_operator_opt (parser);
29899 if (op == ERROR_MARK)
29900 return error_mark_node;
29902 if (op != NOP_EXPR)
29904 rhs = cp_parser_assignment_expression (parser, false, NULL);
29905 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29906 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29909 lhs = cp_parser_binary_expression (parser, false, false,
29910 PREC_ADDITIVE_EXPRESSION, NULL);
29911 token = cp_lexer_peek_token (parser->lexer);
29912 decl_first = lhs == decl;
29913 if (decl_first)
29914 lhs = NULL_TREE;
29915 if (token->type != CPP_PLUS
29916 && token->type != CPP_MINUS)
29917 return error_mark_node;
29921 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29922 cp_lexer_consume_token (parser->lexer);
29923 rhs = cp_parser_binary_expression (parser, false, false,
29924 PREC_ADDITIVE_EXPRESSION, NULL);
29925 token = cp_lexer_peek_token (parser->lexer);
29926 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29928 if (lhs == NULL_TREE)
29930 if (op == PLUS_EXPR)
29931 lhs = rhs;
29932 else
29933 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29934 tf_warning_or_error);
29936 else
29937 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29938 ERROR_MARK, NULL, tf_warning_or_error);
29941 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29943 if (!decl_first)
29945 if (rhs != decl || op == MINUS_EXPR)
29946 return error_mark_node;
29947 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29949 else
29950 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29952 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29955 /* Parse the initialization statement of either an OpenMP for loop or
29956 a Cilk Plus for loop.
29958 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29959 Plus.
29961 Return true if the resulting construct should have an
29962 OMP_CLAUSE_PRIVATE added to it. */
29964 static bool
29965 cp_parser_omp_for_loop_init (cp_parser *parser,
29966 bool parsing_openmp,
29967 tree &this_pre_body,
29968 vec<tree, va_gc> *for_block,
29969 tree &init,
29970 tree &decl,
29971 tree &real_decl)
29973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29974 return false;
29976 bool add_private_clause = false;
29978 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29980 init-expr:
29981 var = lb
29982 integer-type var = lb
29983 random-access-iterator-type var = lb
29984 pointer-type var = lb
29986 cp_decl_specifier_seq type_specifiers;
29988 /* First, try to parse as an initialized declaration. See
29989 cp_parser_condition, from whence the bulk of this is copied. */
29991 cp_parser_parse_tentatively (parser);
29992 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29993 /*is_trailing_return=*/false,
29994 &type_specifiers);
29995 if (cp_parser_parse_definitely (parser))
29997 /* If parsing a type specifier seq succeeded, then this
29998 MUST be a initialized declaration. */
29999 tree asm_specification, attributes;
30000 cp_declarator *declarator;
30002 declarator = cp_parser_declarator (parser,
30003 CP_PARSER_DECLARATOR_NAMED,
30004 /*ctor_dtor_or_conv_p=*/NULL,
30005 /*parenthesized_p=*/NULL,
30006 /*member_p=*/false);
30007 attributes = cp_parser_attributes_opt (parser);
30008 asm_specification = cp_parser_asm_specification_opt (parser);
30010 if (declarator == cp_error_declarator)
30011 cp_parser_skip_to_end_of_statement (parser);
30013 else
30015 tree pushed_scope, auto_node;
30017 decl = start_decl (declarator, &type_specifiers,
30018 SD_INITIALIZED, attributes,
30019 /*prefix_attributes=*/NULL_TREE,
30020 &pushed_scope);
30022 auto_node = type_uses_auto (TREE_TYPE (decl));
30023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30025 if (cp_lexer_next_token_is (parser->lexer,
30026 CPP_OPEN_PAREN))
30028 if (parsing_openmp)
30029 error ("parenthesized initialization is not allowed in "
30030 "OpenMP %<for%> loop");
30031 else
30032 error ("parenthesized initialization is "
30033 "not allowed in for-loop");
30035 else
30036 /* Trigger an error. */
30037 cp_parser_require (parser, CPP_EQ, RT_EQ);
30039 init = error_mark_node;
30040 cp_parser_skip_to_end_of_statement (parser);
30042 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30043 || type_dependent_expression_p (decl)
30044 || auto_node)
30046 bool is_direct_init, is_non_constant_init;
30048 init = cp_parser_initializer (parser,
30049 &is_direct_init,
30050 &is_non_constant_init);
30052 if (auto_node)
30054 TREE_TYPE (decl)
30055 = do_auto_deduction (TREE_TYPE (decl), init,
30056 auto_node);
30058 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30059 && !type_dependent_expression_p (decl))
30060 goto non_class;
30063 cp_finish_decl (decl, init, !is_non_constant_init,
30064 asm_specification,
30065 LOOKUP_ONLYCONVERTING);
30066 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30068 vec_safe_push (for_block, this_pre_body);
30069 init = NULL_TREE;
30071 else
30072 init = pop_stmt_list (this_pre_body);
30073 this_pre_body = NULL_TREE;
30075 else
30077 /* Consume '='. */
30078 cp_lexer_consume_token (parser->lexer);
30079 init = cp_parser_assignment_expression (parser, false, NULL);
30081 non_class:
30082 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30083 init = error_mark_node;
30084 else
30085 cp_finish_decl (decl, NULL_TREE,
30086 /*init_const_expr_p=*/false,
30087 asm_specification,
30088 LOOKUP_ONLYCONVERTING);
30091 if (pushed_scope)
30092 pop_scope (pushed_scope);
30095 else
30097 cp_id_kind idk;
30098 /* If parsing a type specifier sequence failed, then
30099 this MUST be a simple expression. */
30100 cp_parser_parse_tentatively (parser);
30101 decl = cp_parser_primary_expression (parser, false, false,
30102 false, &idk);
30103 if (!cp_parser_error_occurred (parser)
30104 && decl
30105 && DECL_P (decl)
30106 && CLASS_TYPE_P (TREE_TYPE (decl)))
30108 tree rhs;
30110 cp_parser_parse_definitely (parser);
30111 cp_parser_require (parser, CPP_EQ, RT_EQ);
30112 rhs = cp_parser_assignment_expression (parser, false, NULL);
30113 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30114 decl, NOP_EXPR,
30115 rhs,
30116 tf_warning_or_error));
30117 add_private_clause = true;
30119 else
30121 decl = NULL;
30122 cp_parser_abort_tentative_parse (parser);
30123 init = cp_parser_expression (parser, false, NULL);
30124 if (init)
30126 if (TREE_CODE (init) == MODIFY_EXPR
30127 || TREE_CODE (init) == MODOP_EXPR)
30128 real_decl = TREE_OPERAND (init, 0);
30132 return add_private_clause;
30135 /* Parse the restricted form of the for statement allowed by OpenMP. */
30137 static tree
30138 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30139 tree *cclauses)
30141 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30142 tree real_decl, initv, condv, incrv, declv;
30143 tree this_pre_body, cl;
30144 location_t loc_first;
30145 bool collapse_err = false;
30146 int i, collapse = 1, nbraces = 0;
30147 vec<tree, va_gc> *for_block = make_tree_vector ();
30149 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30150 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30151 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30153 gcc_assert (collapse >= 1);
30155 declv = make_tree_vec (collapse);
30156 initv = make_tree_vec (collapse);
30157 condv = make_tree_vec (collapse);
30158 incrv = make_tree_vec (collapse);
30160 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30162 for (i = 0; i < collapse; i++)
30164 int bracecount = 0;
30165 bool add_private_clause = false;
30166 location_t loc;
30168 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30170 cp_parser_error (parser, "for statement expected");
30171 return NULL;
30173 loc = cp_lexer_consume_token (parser->lexer)->location;
30175 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30176 return NULL;
30178 init = decl = real_decl = NULL;
30179 this_pre_body = push_stmt_list ();
30181 add_private_clause
30182 |= cp_parser_omp_for_loop_init (parser,
30183 /*parsing_openmp=*/code != CILK_SIMD,
30184 this_pre_body, for_block,
30185 init, decl, real_decl);
30187 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30188 if (this_pre_body)
30190 this_pre_body = pop_stmt_list (this_pre_body);
30191 if (pre_body)
30193 tree t = pre_body;
30194 pre_body = push_stmt_list ();
30195 add_stmt (t);
30196 add_stmt (this_pre_body);
30197 pre_body = pop_stmt_list (pre_body);
30199 else
30200 pre_body = this_pre_body;
30203 if (decl)
30204 real_decl = decl;
30205 if (cclauses != NULL
30206 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30207 && real_decl != NULL_TREE)
30209 tree *c;
30210 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30211 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30212 && OMP_CLAUSE_DECL (*c) == real_decl)
30214 error_at (loc, "iteration variable %qD"
30215 " should not be firstprivate", real_decl);
30216 *c = OMP_CLAUSE_CHAIN (*c);
30218 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30219 && OMP_CLAUSE_DECL (*c) == real_decl)
30221 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30222 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30223 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30224 OMP_CLAUSE_DECL (l) = real_decl;
30225 OMP_CLAUSE_CHAIN (l) = clauses;
30226 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30227 clauses = l;
30228 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30229 CP_OMP_CLAUSE_INFO (*c) = NULL;
30230 add_private_clause = false;
30232 else
30234 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30235 && OMP_CLAUSE_DECL (*c) == real_decl)
30236 add_private_clause = false;
30237 c = &OMP_CLAUSE_CHAIN (*c);
30241 if (add_private_clause)
30243 tree c;
30244 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30246 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30247 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30248 && OMP_CLAUSE_DECL (c) == decl)
30249 break;
30250 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30251 && OMP_CLAUSE_DECL (c) == decl)
30252 error_at (loc, "iteration variable %qD "
30253 "should not be firstprivate",
30254 decl);
30255 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30256 && OMP_CLAUSE_DECL (c) == decl)
30257 error_at (loc, "iteration variable %qD should not be reduction",
30258 decl);
30260 if (c == NULL)
30262 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30263 OMP_CLAUSE_DECL (c) = decl;
30264 c = finish_omp_clauses (c);
30265 if (c)
30267 OMP_CLAUSE_CHAIN (c) = clauses;
30268 clauses = c;
30273 cond = NULL;
30274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30275 cond = cp_parser_omp_for_cond (parser, decl, code);
30276 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30278 incr = NULL;
30279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30281 /* If decl is an iterator, preserve the operator on decl
30282 until finish_omp_for. */
30283 if (real_decl
30284 && ((processing_template_decl
30285 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30286 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30287 incr = cp_parser_omp_for_incr (parser, real_decl);
30288 else
30289 incr = cp_parser_expression (parser, false, NULL);
30290 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30291 SET_EXPR_LOCATION (incr, input_location);
30294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30295 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30296 /*or_comma=*/false,
30297 /*consume_paren=*/true);
30299 TREE_VEC_ELT (declv, i) = decl;
30300 TREE_VEC_ELT (initv, i) = init;
30301 TREE_VEC_ELT (condv, i) = cond;
30302 TREE_VEC_ELT (incrv, i) = incr;
30304 if (i == collapse - 1)
30305 break;
30307 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30308 in between the collapsed for loops to be still considered perfectly
30309 nested. Hopefully the final version clarifies this.
30310 For now handle (multiple) {'s and empty statements. */
30311 cp_parser_parse_tentatively (parser);
30314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30315 break;
30316 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30318 cp_lexer_consume_token (parser->lexer);
30319 bracecount++;
30321 else if (bracecount
30322 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30323 cp_lexer_consume_token (parser->lexer);
30324 else
30326 loc = cp_lexer_peek_token (parser->lexer)->location;
30327 error_at (loc, "not enough collapsed for loops");
30328 collapse_err = true;
30329 cp_parser_abort_tentative_parse (parser);
30330 declv = NULL_TREE;
30331 break;
30334 while (1);
30336 if (declv)
30338 cp_parser_parse_definitely (parser);
30339 nbraces += bracecount;
30343 /* Note that we saved the original contents of this flag when we entered
30344 the structured block, and so we don't need to re-save it here. */
30345 if (code == CILK_SIMD)
30346 parser->in_statement = IN_CILK_SIMD_FOR;
30347 else
30348 parser->in_statement = IN_OMP_FOR;
30350 /* Note that the grammar doesn't call for a structured block here,
30351 though the loop as a whole is a structured block. */
30352 body = push_stmt_list ();
30353 cp_parser_statement (parser, NULL_TREE, false, NULL);
30354 body = pop_stmt_list (body);
30356 if (declv == NULL_TREE)
30357 ret = NULL_TREE;
30358 else
30359 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30360 pre_body, clauses);
30362 while (nbraces)
30364 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30366 cp_lexer_consume_token (parser->lexer);
30367 nbraces--;
30369 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30370 cp_lexer_consume_token (parser->lexer);
30371 else
30373 if (!collapse_err)
30375 error_at (cp_lexer_peek_token (parser->lexer)->location,
30376 "collapsed loops not perfectly nested");
30378 collapse_err = true;
30379 cp_parser_statement_seq_opt (parser, NULL);
30380 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30381 break;
30385 while (!for_block->is_empty ())
30386 add_stmt (pop_stmt_list (for_block->pop ()));
30387 release_tree_vector (for_block);
30389 return ret;
30392 /* Helper function for OpenMP parsing, split clauses and call
30393 finish_omp_clauses on each of the set of clauses afterwards. */
30395 static void
30396 cp_omp_split_clauses (location_t loc, enum tree_code code,
30397 omp_clause_mask mask, tree clauses, tree *cclauses)
30399 int i;
30400 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30401 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30402 if (cclauses[i])
30403 cclauses[i] = finish_omp_clauses (cclauses[i]);
30406 /* OpenMP 4.0:
30407 #pragma omp simd simd-clause[optseq] new-line
30408 for-loop */
30410 #define OMP_SIMD_CLAUSE_MASK \
30411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30419 static tree
30420 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30421 char *p_name, omp_clause_mask mask, tree *cclauses)
30423 tree clauses, sb, ret;
30424 unsigned int save;
30425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30427 strcat (p_name, " simd");
30428 mask |= OMP_SIMD_CLAUSE_MASK;
30429 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30431 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30432 cclauses == NULL);
30433 if (cclauses)
30435 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30436 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30439 sb = begin_omp_structured_block ();
30440 save = cp_parser_begin_omp_structured_block (parser);
30442 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30444 cp_parser_end_omp_structured_block (parser, save);
30445 add_stmt (finish_omp_structured_block (sb));
30447 return ret;
30450 /* OpenMP 2.5:
30451 #pragma omp for for-clause[optseq] new-line
30452 for-loop
30454 OpenMP 4.0:
30455 #pragma omp for simd for-simd-clause[optseq] new-line
30456 for-loop */
30458 #define OMP_FOR_CLAUSE_MASK \
30459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30468 static tree
30469 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30470 char *p_name, omp_clause_mask mask, tree *cclauses)
30472 tree clauses, sb, ret;
30473 unsigned int save;
30474 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30476 strcat (p_name, " for");
30477 mask |= OMP_FOR_CLAUSE_MASK;
30478 if (cclauses)
30479 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30481 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30483 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30484 const char *p = IDENTIFIER_POINTER (id);
30486 if (strcmp (p, "simd") == 0)
30488 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30489 if (cclauses == NULL)
30490 cclauses = cclauses_buf;
30492 cp_lexer_consume_token (parser->lexer);
30493 if (!flag_openmp) /* flag_openmp_simd */
30494 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30495 cclauses);
30496 sb = begin_omp_structured_block ();
30497 save = cp_parser_begin_omp_structured_block (parser);
30498 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30499 cclauses);
30500 cp_parser_end_omp_structured_block (parser, save);
30501 tree body = finish_omp_structured_block (sb);
30502 if (ret == NULL)
30503 return ret;
30504 ret = make_node (OMP_FOR);
30505 TREE_TYPE (ret) = void_type_node;
30506 OMP_FOR_BODY (ret) = body;
30507 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30508 SET_EXPR_LOCATION (ret, loc);
30509 add_stmt (ret);
30510 return ret;
30513 if (!flag_openmp) /* flag_openmp_simd */
30515 cp_parser_require_pragma_eol (parser, pragma_tok);
30516 return NULL_TREE;
30519 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30520 cclauses == NULL);
30521 if (cclauses)
30523 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30524 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30527 sb = begin_omp_structured_block ();
30528 save = cp_parser_begin_omp_structured_block (parser);
30530 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30532 cp_parser_end_omp_structured_block (parser, save);
30533 add_stmt (finish_omp_structured_block (sb));
30535 return ret;
30538 /* OpenMP 2.5:
30539 # pragma omp master new-line
30540 structured-block */
30542 static tree
30543 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30545 cp_parser_require_pragma_eol (parser, pragma_tok);
30546 return c_finish_omp_master (input_location,
30547 cp_parser_omp_structured_block (parser));
30550 /* OpenMP 2.5:
30551 # pragma omp ordered new-line
30552 structured-block */
30554 static tree
30555 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30557 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30558 cp_parser_require_pragma_eol (parser, pragma_tok);
30559 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30562 /* OpenMP 2.5:
30564 section-scope:
30565 { section-sequence }
30567 section-sequence:
30568 section-directive[opt] structured-block
30569 section-sequence section-directive structured-block */
30571 static tree
30572 cp_parser_omp_sections_scope (cp_parser *parser)
30574 tree stmt, substmt;
30575 bool error_suppress = false;
30576 cp_token *tok;
30578 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30579 return NULL_TREE;
30581 stmt = push_stmt_list ();
30583 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30585 substmt = cp_parser_omp_structured_block (parser);
30586 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30587 add_stmt (substmt);
30590 while (1)
30592 tok = cp_lexer_peek_token (parser->lexer);
30593 if (tok->type == CPP_CLOSE_BRACE)
30594 break;
30595 if (tok->type == CPP_EOF)
30596 break;
30598 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30600 cp_lexer_consume_token (parser->lexer);
30601 cp_parser_require_pragma_eol (parser, tok);
30602 error_suppress = false;
30604 else if (!error_suppress)
30606 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30607 error_suppress = true;
30610 substmt = cp_parser_omp_structured_block (parser);
30611 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30612 add_stmt (substmt);
30614 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30616 substmt = pop_stmt_list (stmt);
30618 stmt = make_node (OMP_SECTIONS);
30619 TREE_TYPE (stmt) = void_type_node;
30620 OMP_SECTIONS_BODY (stmt) = substmt;
30622 add_stmt (stmt);
30623 return stmt;
30626 /* OpenMP 2.5:
30627 # pragma omp sections sections-clause[optseq] newline
30628 sections-scope */
30630 #define OMP_SECTIONS_CLAUSE_MASK \
30631 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30637 static tree
30638 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30639 char *p_name, omp_clause_mask mask, tree *cclauses)
30641 tree clauses, ret;
30642 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30644 strcat (p_name, " sections");
30645 mask |= OMP_SECTIONS_CLAUSE_MASK;
30646 if (cclauses)
30647 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30649 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30650 cclauses == NULL);
30651 if (cclauses)
30653 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30654 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30657 ret = cp_parser_omp_sections_scope (parser);
30658 if (ret)
30659 OMP_SECTIONS_CLAUSES (ret) = clauses;
30661 return ret;
30664 /* OpenMP 2.5:
30665 # pragma omp parallel parallel-clause[optseq] new-line
30666 structured-block
30667 # pragma omp parallel for parallel-for-clause[optseq] new-line
30668 structured-block
30669 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30670 structured-block
30672 OpenMP 4.0:
30673 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30674 structured-block */
30676 #define OMP_PARALLEL_CLAUSE_MASK \
30677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30687 static tree
30688 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30689 char *p_name, omp_clause_mask mask, tree *cclauses)
30691 tree stmt, clauses, block;
30692 unsigned int save;
30693 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30695 strcat (p_name, " parallel");
30696 mask |= OMP_PARALLEL_CLAUSE_MASK;
30698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30700 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30701 if (cclauses == NULL)
30702 cclauses = cclauses_buf;
30704 cp_lexer_consume_token (parser->lexer);
30705 if (!flag_openmp) /* flag_openmp_simd */
30706 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30707 block = begin_omp_parallel ();
30708 save = cp_parser_begin_omp_structured_block (parser);
30709 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30710 cp_parser_end_omp_structured_block (parser, save);
30711 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30712 block);
30713 if (ret == NULL_TREE)
30714 return ret;
30715 OMP_PARALLEL_COMBINED (stmt) = 1;
30716 return stmt;
30718 else if (cclauses)
30720 error_at (loc, "expected %<for%> after %qs", p_name);
30721 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30722 return NULL_TREE;
30724 else if (!flag_openmp) /* flag_openmp_simd */
30726 cp_parser_require_pragma_eol (parser, pragma_tok);
30727 return NULL_TREE;
30729 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30731 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30732 const char *p = IDENTIFIER_POINTER (id);
30733 if (strcmp (p, "sections") == 0)
30735 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30736 cclauses = cclauses_buf;
30738 cp_lexer_consume_token (parser->lexer);
30739 block = begin_omp_parallel ();
30740 save = cp_parser_begin_omp_structured_block (parser);
30741 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30742 cp_parser_end_omp_structured_block (parser, save);
30743 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30744 block);
30745 OMP_PARALLEL_COMBINED (stmt) = 1;
30746 return stmt;
30750 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30752 block = begin_omp_parallel ();
30753 save = cp_parser_begin_omp_structured_block (parser);
30754 cp_parser_statement (parser, NULL_TREE, false, NULL);
30755 cp_parser_end_omp_structured_block (parser, save);
30756 stmt = finish_omp_parallel (clauses, block);
30757 return stmt;
30760 /* OpenMP 2.5:
30761 # pragma omp single single-clause[optseq] new-line
30762 structured-block */
30764 #define OMP_SINGLE_CLAUSE_MASK \
30765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30770 static tree
30771 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30773 tree stmt = make_node (OMP_SINGLE);
30774 TREE_TYPE (stmt) = void_type_node;
30776 OMP_SINGLE_CLAUSES (stmt)
30777 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30778 "#pragma omp single", pragma_tok);
30779 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30781 return add_stmt (stmt);
30784 /* OpenMP 3.0:
30785 # pragma omp task task-clause[optseq] new-line
30786 structured-block */
30788 #define OMP_TASK_CLAUSE_MASK \
30789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30799 static tree
30800 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30802 tree clauses, block;
30803 unsigned int save;
30805 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30806 "#pragma omp task", pragma_tok);
30807 block = begin_omp_task ();
30808 save = cp_parser_begin_omp_structured_block (parser);
30809 cp_parser_statement (parser, NULL_TREE, false, NULL);
30810 cp_parser_end_omp_structured_block (parser, save);
30811 return finish_omp_task (clauses, block);
30814 /* OpenMP 3.0:
30815 # pragma omp taskwait new-line */
30817 static void
30818 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30820 cp_parser_require_pragma_eol (parser, pragma_tok);
30821 finish_omp_taskwait ();
30824 /* OpenMP 3.1:
30825 # pragma omp taskyield new-line */
30827 static void
30828 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30830 cp_parser_require_pragma_eol (parser, pragma_tok);
30831 finish_omp_taskyield ();
30834 /* OpenMP 4.0:
30835 # pragma omp taskgroup new-line
30836 structured-block */
30838 static tree
30839 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30841 cp_parser_require_pragma_eol (parser, pragma_tok);
30842 return c_finish_omp_taskgroup (input_location,
30843 cp_parser_omp_structured_block (parser));
30847 /* OpenMP 2.5:
30848 # pragma omp threadprivate (variable-list) */
30850 static void
30851 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30853 tree vars;
30855 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30856 cp_parser_require_pragma_eol (parser, pragma_tok);
30858 finish_omp_threadprivate (vars);
30861 /* OpenMP 4.0:
30862 # pragma omp cancel cancel-clause[optseq] new-line */
30864 #define OMP_CANCEL_CLAUSE_MASK \
30865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30871 static void
30872 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30874 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30875 "#pragma omp cancel", pragma_tok);
30876 finish_omp_cancel (clauses);
30879 /* OpenMP 4.0:
30880 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30882 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30888 static void
30889 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30891 tree clauses;
30892 bool point_seen = false;
30894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30896 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30897 const char *p = IDENTIFIER_POINTER (id);
30899 if (strcmp (p, "point") == 0)
30901 cp_lexer_consume_token (parser->lexer);
30902 point_seen = true;
30905 if (!point_seen)
30907 cp_parser_error (parser, "expected %<point%>");
30908 cp_parser_require_pragma_eol (parser, pragma_tok);
30909 return;
30912 clauses = cp_parser_omp_all_clauses (parser,
30913 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30914 "#pragma omp cancellation point",
30915 pragma_tok);
30916 finish_omp_cancellation_point (clauses);
30919 /* OpenMP 4.0:
30920 #pragma omp distribute distribute-clause[optseq] new-line
30921 for-loop */
30923 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30929 static tree
30930 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30931 char *p_name, omp_clause_mask mask, tree *cclauses)
30933 tree clauses, sb, ret;
30934 unsigned int save;
30935 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30937 strcat (p_name, " distribute");
30938 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30942 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30943 const char *p = IDENTIFIER_POINTER (id);
30944 bool simd = false;
30945 bool parallel = false;
30947 if (strcmp (p, "simd") == 0)
30948 simd = true;
30949 else
30950 parallel = strcmp (p, "parallel") == 0;
30951 if (parallel || simd)
30953 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30954 if (cclauses == NULL)
30955 cclauses = cclauses_buf;
30956 cp_lexer_consume_token (parser->lexer);
30957 if (!flag_openmp) /* flag_openmp_simd */
30959 if (simd)
30960 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30961 cclauses);
30962 else
30963 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30964 cclauses);
30966 sb = begin_omp_structured_block ();
30967 save = cp_parser_begin_omp_structured_block (parser);
30968 if (simd)
30969 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30970 cclauses);
30971 else
30972 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30973 cclauses);
30974 cp_parser_end_omp_structured_block (parser, save);
30975 tree body = finish_omp_structured_block (sb);
30976 if (ret == NULL)
30977 return ret;
30978 ret = make_node (OMP_DISTRIBUTE);
30979 TREE_TYPE (ret) = void_type_node;
30980 OMP_FOR_BODY (ret) = body;
30981 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30982 SET_EXPR_LOCATION (ret, loc);
30983 add_stmt (ret);
30984 return ret;
30987 if (!flag_openmp) /* flag_openmp_simd */
30989 cp_parser_require_pragma_eol (parser, pragma_tok);
30990 return NULL_TREE;
30993 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30994 cclauses == NULL);
30995 if (cclauses)
30997 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30998 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31001 sb = begin_omp_structured_block ();
31002 save = cp_parser_begin_omp_structured_block (parser);
31004 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31006 cp_parser_end_omp_structured_block (parser, save);
31007 add_stmt (finish_omp_structured_block (sb));
31009 return ret;
31012 /* OpenMP 4.0:
31013 # pragma omp teams teams-clause[optseq] new-line
31014 structured-block */
31016 #define OMP_TEAMS_CLAUSE_MASK \
31017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31025 static tree
31026 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31027 char *p_name, omp_clause_mask mask, tree *cclauses)
31029 tree clauses, sb, ret;
31030 unsigned int save;
31031 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31033 strcat (p_name, " teams");
31034 mask |= OMP_TEAMS_CLAUSE_MASK;
31036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31038 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31039 const char *p = IDENTIFIER_POINTER (id);
31040 if (strcmp (p, "distribute") == 0)
31042 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31043 if (cclauses == NULL)
31044 cclauses = cclauses_buf;
31046 cp_lexer_consume_token (parser->lexer);
31047 if (!flag_openmp) /* flag_openmp_simd */
31048 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31049 cclauses);
31050 sb = begin_omp_structured_block ();
31051 save = cp_parser_begin_omp_structured_block (parser);
31052 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31053 cclauses);
31054 cp_parser_end_omp_structured_block (parser, save);
31055 tree body = finish_omp_structured_block (sb);
31056 if (ret == NULL)
31057 return ret;
31058 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31059 ret = make_node (OMP_TEAMS);
31060 TREE_TYPE (ret) = void_type_node;
31061 OMP_TEAMS_CLAUSES (ret) = clauses;
31062 OMP_TEAMS_BODY (ret) = body;
31063 return add_stmt (ret);
31066 if (!flag_openmp) /* flag_openmp_simd */
31068 cp_parser_require_pragma_eol (parser, pragma_tok);
31069 return NULL_TREE;
31072 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31073 cclauses == NULL);
31074 if (cclauses)
31076 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31077 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31080 tree stmt = make_node (OMP_TEAMS);
31081 TREE_TYPE (stmt) = void_type_node;
31082 OMP_TEAMS_CLAUSES (stmt) = clauses;
31083 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31085 return add_stmt (stmt);
31088 /* OpenMP 4.0:
31089 # pragma omp target data target-data-clause[optseq] new-line
31090 structured-block */
31092 #define OMP_TARGET_DATA_CLAUSE_MASK \
31093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31097 static tree
31098 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31100 tree stmt = make_node (OMP_TARGET_DATA);
31101 TREE_TYPE (stmt) = void_type_node;
31103 OMP_TARGET_DATA_CLAUSES (stmt)
31104 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31105 "#pragma omp target data", pragma_tok);
31106 keep_next_level (true);
31107 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31109 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31110 return add_stmt (stmt);
31113 /* OpenMP 4.0:
31114 # pragma omp target update target-update-clause[optseq] new-line */
31116 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31122 static bool
31123 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31124 enum pragma_context context)
31126 if (context == pragma_stmt)
31128 error_at (pragma_tok->location,
31129 "%<#pragma omp target update%> may only be "
31130 "used in compound statements");
31131 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31132 return false;
31135 tree clauses
31136 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31137 "#pragma omp target update", pragma_tok);
31138 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31139 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31141 error_at (pragma_tok->location,
31142 "%<#pragma omp target update must contain at least one "
31143 "%<from%> or %<to%> clauses");
31144 return false;
31147 tree stmt = make_node (OMP_TARGET_UPDATE);
31148 TREE_TYPE (stmt) = void_type_node;
31149 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31150 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31151 add_stmt (stmt);
31152 return false;
31155 /* OpenMP 4.0:
31156 # pragma omp target target-clause[optseq] new-line
31157 structured-block */
31159 #define OMP_TARGET_CLAUSE_MASK \
31160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31164 static bool
31165 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31166 enum pragma_context context)
31168 if (context != pragma_stmt && context != pragma_compound)
31170 cp_parser_error (parser, "expected declaration specifiers");
31171 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31172 return false;
31175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31178 const char *p = IDENTIFIER_POINTER (id);
31180 if (strcmp (p, "teams") == 0)
31182 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31183 char p_name[sizeof ("#pragma omp target teams distribute "
31184 "parallel for simd")];
31186 cp_lexer_consume_token (parser->lexer);
31187 strcpy (p_name, "#pragma omp target");
31188 if (!flag_openmp) /* flag_openmp_simd */
31190 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31191 OMP_TARGET_CLAUSE_MASK,
31192 cclauses);
31193 return stmt != NULL_TREE;
31195 keep_next_level (true);
31196 tree sb = begin_omp_structured_block ();
31197 unsigned save = cp_parser_begin_omp_structured_block (parser);
31198 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31199 OMP_TARGET_CLAUSE_MASK, cclauses);
31200 cp_parser_end_omp_structured_block (parser, save);
31201 tree body = finish_omp_structured_block (sb);
31202 if (ret == NULL_TREE)
31203 return false;
31204 tree stmt = make_node (OMP_TARGET);
31205 TREE_TYPE (stmt) = void_type_node;
31206 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31207 OMP_TARGET_BODY (stmt) = body;
31208 add_stmt (stmt);
31209 return true;
31211 else if (!flag_openmp) /* flag_openmp_simd */
31213 cp_parser_require_pragma_eol (parser, pragma_tok);
31214 return false;
31216 else if (strcmp (p, "data") == 0)
31218 cp_lexer_consume_token (parser->lexer);
31219 cp_parser_omp_target_data (parser, pragma_tok);
31220 return true;
31222 else if (strcmp (p, "update") == 0)
31224 cp_lexer_consume_token (parser->lexer);
31225 return cp_parser_omp_target_update (parser, pragma_tok, context);
31229 tree stmt = make_node (OMP_TARGET);
31230 TREE_TYPE (stmt) = void_type_node;
31232 OMP_TARGET_CLAUSES (stmt)
31233 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31234 "#pragma omp target", pragma_tok);
31235 keep_next_level (true);
31236 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31238 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31239 add_stmt (stmt);
31240 return true;
31243 /* OpenMP 4.0:
31244 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31246 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31247 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31254 static void
31255 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31256 enum pragma_context context)
31258 bool first_p = parser->omp_declare_simd == NULL;
31259 cp_omp_declare_simd_data data;
31260 if (first_p)
31262 data.error_seen = false;
31263 data.fndecl_seen = false;
31264 data.tokens = vNULL;
31265 parser->omp_declare_simd = &data;
31267 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31268 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31269 cp_lexer_consume_token (parser->lexer);
31270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31271 parser->omp_declare_simd->error_seen = true;
31272 cp_parser_require_pragma_eol (parser, pragma_tok);
31273 struct cp_token_cache *cp
31274 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31275 parser->omp_declare_simd->tokens.safe_push (cp);
31276 if (first_p)
31278 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31279 cp_parser_pragma (parser, context);
31280 switch (context)
31282 case pragma_external:
31283 cp_parser_declaration (parser);
31284 break;
31285 case pragma_member:
31286 cp_parser_member_declaration (parser);
31287 break;
31288 case pragma_objc_icode:
31289 cp_parser_block_declaration (parser, /*statement_p=*/false);
31290 break;
31291 default:
31292 cp_parser_declaration_statement (parser);
31293 break;
31295 if (parser->omp_declare_simd
31296 && !parser->omp_declare_simd->error_seen
31297 && !parser->omp_declare_simd->fndecl_seen)
31298 error_at (pragma_tok->location,
31299 "%<#pragma omp declare simd%> not immediately followed by "
31300 "function declaration or definition");
31301 data.tokens.release ();
31302 parser->omp_declare_simd = NULL;
31306 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31307 This function is modelled similar to the late parsing of omp declare
31308 simd. */
31310 static tree
31311 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31313 struct cp_token_cache *ce;
31314 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31315 int ii = 0;
31317 if (parser->omp_declare_simd != NULL)
31319 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31320 " marked as a Cilk Plus SIMD-enabled function");
31321 XDELETE (parser->cilk_simd_fn_info);
31322 parser->cilk_simd_fn_info = NULL;
31323 return attrs;
31325 if (!info->error_seen && info->fndecl_seen)
31327 error ("vector attribute not immediately followed by a single function"
31328 " declaration or definition");
31329 info->error_seen = true;
31331 if (info->error_seen)
31332 return attrs;
31334 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31336 tree c, cl;
31338 cp_parser_push_lexer_for_tokens (parser, ce);
31339 parser->lexer->in_pragma = true;
31340 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31341 "SIMD-enabled functions attribute",
31342 NULL);
31343 cp_parser_pop_lexer (parser);
31344 if (cl)
31345 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31347 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31348 TREE_CHAIN (c) = attrs;
31349 attrs = c;
31351 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31352 TREE_CHAIN (c) = attrs;
31353 if (processing_template_decl)
31354 ATTR_IS_DEPENDENT (c) = 1;
31355 attrs = c;
31357 info->fndecl_seen = true;
31358 XDELETE (parser->cilk_simd_fn_info);
31359 parser->cilk_simd_fn_info = NULL;
31360 return attrs;
31363 /* Finalize #pragma omp declare simd clauses after direct declarator has
31364 been parsed, and put that into "omp declare simd" attribute. */
31366 static tree
31367 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31369 struct cp_token_cache *ce;
31370 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31371 int i;
31373 if (!data->error_seen && data->fndecl_seen)
31375 error ("%<#pragma omp declare simd%> not immediately followed by "
31376 "a single function declaration or definition");
31377 data->error_seen = true;
31378 return attrs;
31380 if (data->error_seen)
31381 return attrs;
31383 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31385 tree c, cl;
31387 cp_parser_push_lexer_for_tokens (parser, ce);
31388 parser->lexer->in_pragma = true;
31389 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31390 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31391 cp_lexer_consume_token (parser->lexer);
31392 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31393 "#pragma omp declare simd", pragma_tok);
31394 cp_parser_pop_lexer (parser);
31395 if (cl)
31396 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31397 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31398 TREE_CHAIN (c) = attrs;
31399 if (processing_template_decl)
31400 ATTR_IS_DEPENDENT (c) = 1;
31401 attrs = c;
31404 data->fndecl_seen = true;
31405 return attrs;
31409 /* OpenMP 4.0:
31410 # pragma omp declare target new-line
31411 declarations and definitions
31412 # pragma omp end declare target new-line */
31414 static void
31415 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31418 scope_chain->omp_declare_target_attribute++;
31421 static void
31422 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31424 const char *p = "";
31425 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31428 p = IDENTIFIER_POINTER (id);
31430 if (strcmp (p, "declare") == 0)
31432 cp_lexer_consume_token (parser->lexer);
31433 p = "";
31434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31437 p = IDENTIFIER_POINTER (id);
31439 if (strcmp (p, "target") == 0)
31440 cp_lexer_consume_token (parser->lexer);
31441 else
31443 cp_parser_error (parser, "expected %<target%>");
31444 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31445 return;
31448 else
31450 cp_parser_error (parser, "expected %<declare%>");
31451 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31452 return;
31454 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31455 if (!scope_chain->omp_declare_target_attribute)
31456 error_at (pragma_tok->location,
31457 "%<#pragma omp end declare target%> without corresponding "
31458 "%<#pragma omp declare target%>");
31459 else
31460 scope_chain->omp_declare_target_attribute--;
31463 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31464 expression and optional initializer clause of
31465 #pragma omp declare reduction. We store the expression(s) as
31466 either 3, 6 or 7 special statements inside of the artificial function's
31467 body. The first two statements are DECL_EXPRs for the artificial
31468 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31469 expression that uses those variables.
31470 If there was any INITIALIZER clause, this is followed by further statements,
31471 the fourth and fifth statements are DECL_EXPRs for the artificial
31472 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31473 constructor variant (first token after open paren is not omp_priv),
31474 then the sixth statement is a statement with the function call expression
31475 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31476 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31477 to initialize the OMP_PRIV artificial variable and there is seventh
31478 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31480 static bool
31481 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31483 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31484 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31485 type = TREE_TYPE (type);
31486 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31487 DECL_ARTIFICIAL (omp_out) = 1;
31488 pushdecl (omp_out);
31489 add_decl_expr (omp_out);
31490 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31491 DECL_ARTIFICIAL (omp_in) = 1;
31492 pushdecl (omp_in);
31493 add_decl_expr (omp_in);
31494 tree combiner;
31495 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31497 keep_next_level (true);
31498 tree block = begin_omp_structured_block ();
31499 combiner = cp_parser_expression (parser, false, NULL);
31500 finish_expr_stmt (combiner);
31501 block = finish_omp_structured_block (block);
31502 add_stmt (block);
31504 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31505 return false;
31507 const char *p = "";
31508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31511 p = IDENTIFIER_POINTER (id);
31514 if (strcmp (p, "initializer") == 0)
31516 cp_lexer_consume_token (parser->lexer);
31517 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31518 return false;
31520 p = "";
31521 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31523 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31524 p = IDENTIFIER_POINTER (id);
31527 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31528 DECL_ARTIFICIAL (omp_priv) = 1;
31529 pushdecl (omp_priv);
31530 add_decl_expr (omp_priv);
31531 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31532 DECL_ARTIFICIAL (omp_orig) = 1;
31533 pushdecl (omp_orig);
31534 add_decl_expr (omp_orig);
31536 keep_next_level (true);
31537 block = begin_omp_structured_block ();
31539 bool ctor = false;
31540 if (strcmp (p, "omp_priv") == 0)
31542 bool is_direct_init, is_non_constant_init;
31543 ctor = true;
31544 cp_lexer_consume_token (parser->lexer);
31545 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31546 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31547 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31548 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31549 == CPP_CLOSE_PAREN
31550 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31551 == CPP_CLOSE_PAREN))
31553 finish_omp_structured_block (block);
31554 error ("invalid initializer clause");
31555 return false;
31557 initializer = cp_parser_initializer (parser, &is_direct_init,
31558 &is_non_constant_init);
31559 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31560 NULL_TREE, LOOKUP_ONLYCONVERTING);
31562 else
31564 cp_parser_parse_tentatively (parser);
31565 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31566 /*check_dependency_p=*/true,
31567 /*template_p=*/NULL,
31568 /*declarator_p=*/false,
31569 /*optional_p=*/false);
31570 vec<tree, va_gc> *args;
31571 if (fn_name == error_mark_node
31572 || cp_parser_error_occurred (parser)
31573 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31574 || ((args = cp_parser_parenthesized_expression_list
31575 (parser, non_attr, /*cast_p=*/false,
31576 /*allow_expansion_p=*/true,
31577 /*non_constant_p=*/NULL)),
31578 cp_parser_error_occurred (parser)))
31580 finish_omp_structured_block (block);
31581 cp_parser_abort_tentative_parse (parser);
31582 cp_parser_error (parser, "expected id-expression (arguments)");
31583 return false;
31585 unsigned int i;
31586 tree arg;
31587 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31588 if (arg == omp_priv
31589 || (TREE_CODE (arg) == ADDR_EXPR
31590 && TREE_OPERAND (arg, 0) == omp_priv))
31591 break;
31592 cp_parser_abort_tentative_parse (parser);
31593 if (arg == NULL_TREE)
31594 error ("one of the initializer call arguments should be %<omp_priv%>"
31595 " or %<&omp_priv%>");
31596 initializer = cp_parser_postfix_expression (parser, false, false, false,
31597 false, NULL);
31598 finish_expr_stmt (initializer);
31601 block = finish_omp_structured_block (block);
31602 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31603 finish_expr_stmt (block);
31605 if (ctor)
31606 add_decl_expr (omp_orig);
31608 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31609 return false;
31612 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31613 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31615 return true;
31618 /* OpenMP 4.0
31619 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31620 initializer-clause[opt] new-line
31622 initializer-clause:
31623 initializer (omp_priv initializer)
31624 initializer (function-name (argument-list)) */
31626 static void
31627 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31628 enum pragma_context)
31630 auto_vec<tree> types;
31631 enum tree_code reduc_code = ERROR_MARK;
31632 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31633 unsigned int i;
31634 cp_token *first_token;
31635 cp_token_cache *cp;
31636 int errs;
31637 void *p;
31639 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31640 p = obstack_alloc (&declarator_obstack, 0);
31642 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31643 goto fail;
31645 switch (cp_lexer_peek_token (parser->lexer)->type)
31647 case CPP_PLUS:
31648 reduc_code = PLUS_EXPR;
31649 break;
31650 case CPP_MULT:
31651 reduc_code = MULT_EXPR;
31652 break;
31653 case CPP_MINUS:
31654 reduc_code = MINUS_EXPR;
31655 break;
31656 case CPP_AND:
31657 reduc_code = BIT_AND_EXPR;
31658 break;
31659 case CPP_XOR:
31660 reduc_code = BIT_XOR_EXPR;
31661 break;
31662 case CPP_OR:
31663 reduc_code = BIT_IOR_EXPR;
31664 break;
31665 case CPP_AND_AND:
31666 reduc_code = TRUTH_ANDIF_EXPR;
31667 break;
31668 case CPP_OR_OR:
31669 reduc_code = TRUTH_ORIF_EXPR;
31670 break;
31671 case CPP_NAME:
31672 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31673 break;
31674 default:
31675 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31676 "%<|%>, %<&&%>, %<||%> or identifier");
31677 goto fail;
31680 if (reduc_code != ERROR_MARK)
31681 cp_lexer_consume_token (parser->lexer);
31683 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31684 if (reduc_id == error_mark_node)
31685 goto fail;
31687 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31688 goto fail;
31690 /* Types may not be defined in declare reduction type list. */
31691 const char *saved_message;
31692 saved_message = parser->type_definition_forbidden_message;
31693 parser->type_definition_forbidden_message
31694 = G_("types may not be defined in declare reduction type list");
31695 bool saved_colon_corrects_to_scope_p;
31696 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31697 parser->colon_corrects_to_scope_p = false;
31698 bool saved_colon_doesnt_start_class_def_p;
31699 saved_colon_doesnt_start_class_def_p
31700 = parser->colon_doesnt_start_class_def_p;
31701 parser->colon_doesnt_start_class_def_p = true;
31703 while (true)
31705 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31706 type = cp_parser_type_id (parser);
31707 if (type == error_mark_node)
31709 else if (ARITHMETIC_TYPE_P (type)
31710 && (orig_reduc_id == NULL_TREE
31711 || (TREE_CODE (type) != COMPLEX_TYPE
31712 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31713 "min") == 0
31714 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31715 "max") == 0))))
31716 error_at (loc, "predeclared arithmetic type %qT in "
31717 "%<#pragma omp declare reduction%>", type);
31718 else if (TREE_CODE (type) == FUNCTION_TYPE
31719 || TREE_CODE (type) == METHOD_TYPE
31720 || TREE_CODE (type) == ARRAY_TYPE)
31721 error_at (loc, "function or array type %qT in "
31722 "%<#pragma omp declare reduction%>", type);
31723 else if (TREE_CODE (type) == REFERENCE_TYPE)
31724 error_at (loc, "reference type %qT in "
31725 "%<#pragma omp declare reduction%>", type);
31726 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31727 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31728 "%<#pragma omp declare reduction%>", type);
31729 else
31730 types.safe_push (type);
31732 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31733 cp_lexer_consume_token (parser->lexer);
31734 else
31735 break;
31738 /* Restore the saved message. */
31739 parser->type_definition_forbidden_message = saved_message;
31740 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31741 parser->colon_doesnt_start_class_def_p
31742 = saved_colon_doesnt_start_class_def_p;
31744 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31745 || types.is_empty ())
31747 fail:
31748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31749 goto done;
31752 first_token = cp_lexer_peek_token (parser->lexer);
31753 cp = NULL;
31754 errs = errorcount;
31755 FOR_EACH_VEC_ELT (types, i, type)
31757 tree fntype
31758 = build_function_type_list (void_type_node,
31759 cp_build_reference_type (type, false),
31760 NULL_TREE);
31761 tree this_reduc_id = reduc_id;
31762 if (!dependent_type_p (type))
31763 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31764 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31765 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31766 DECL_ARTIFICIAL (fndecl) = 1;
31767 DECL_EXTERNAL (fndecl) = 1;
31768 DECL_DECLARED_INLINE_P (fndecl) = 1;
31769 DECL_IGNORED_P (fndecl) = 1;
31770 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31771 DECL_ATTRIBUTES (fndecl)
31772 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31773 DECL_ATTRIBUTES (fndecl));
31774 if (processing_template_decl)
31775 fndecl = push_template_decl (fndecl);
31776 bool block_scope = false;
31777 tree block = NULL_TREE;
31778 if (current_function_decl)
31780 block_scope = true;
31781 DECL_CONTEXT (fndecl) = global_namespace;
31782 if (!processing_template_decl)
31783 pushdecl (fndecl);
31785 else if (current_class_type)
31787 if (cp == NULL)
31789 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31790 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31791 cp_lexer_consume_token (parser->lexer);
31792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31793 goto fail;
31794 cp = cp_token_cache_new (first_token,
31795 cp_lexer_peek_nth_token (parser->lexer,
31796 2));
31798 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31799 finish_member_declaration (fndecl);
31800 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31801 DECL_PENDING_INLINE_P (fndecl) = 1;
31802 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31803 continue;
31805 else
31807 DECL_CONTEXT (fndecl) = current_namespace;
31808 pushdecl (fndecl);
31810 if (!block_scope)
31811 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31812 else
31813 block = begin_omp_structured_block ();
31814 if (cp)
31816 cp_parser_push_lexer_for_tokens (parser, cp);
31817 parser->lexer->in_pragma = true;
31819 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31821 if (!block_scope)
31822 finish_function (0);
31823 else
31824 DECL_CONTEXT (fndecl) = current_function_decl;
31825 if (cp)
31826 cp_parser_pop_lexer (parser);
31827 goto fail;
31829 if (cp)
31830 cp_parser_pop_lexer (parser);
31831 if (!block_scope)
31832 finish_function (0);
31833 else
31835 DECL_CONTEXT (fndecl) = current_function_decl;
31836 block = finish_omp_structured_block (block);
31837 if (TREE_CODE (block) == BIND_EXPR)
31838 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31839 else if (TREE_CODE (block) == STATEMENT_LIST)
31840 DECL_SAVED_TREE (fndecl) = block;
31841 if (processing_template_decl)
31842 add_decl_expr (fndecl);
31844 cp_check_omp_declare_reduction (fndecl);
31845 if (cp == NULL && types.length () > 1)
31846 cp = cp_token_cache_new (first_token,
31847 cp_lexer_peek_nth_token (parser->lexer, 2));
31848 if (errs != errorcount)
31849 break;
31852 cp_parser_require_pragma_eol (parser, pragma_tok);
31854 done:
31855 /* Free any declarators allocated. */
31856 obstack_free (&declarator_obstack, p);
31859 /* OpenMP 4.0
31860 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31861 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31862 initializer-clause[opt] new-line
31863 #pragma omp declare target new-line */
31865 static void
31866 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31867 enum pragma_context context)
31869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31871 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31872 const char *p = IDENTIFIER_POINTER (id);
31874 if (strcmp (p, "simd") == 0)
31876 cp_lexer_consume_token (parser->lexer);
31877 cp_parser_omp_declare_simd (parser, pragma_tok,
31878 context);
31879 return;
31881 cp_ensure_no_omp_declare_simd (parser);
31882 if (strcmp (p, "reduction") == 0)
31884 cp_lexer_consume_token (parser->lexer);
31885 cp_parser_omp_declare_reduction (parser, pragma_tok,
31886 context);
31887 return;
31889 if (!flag_openmp) /* flag_openmp_simd */
31891 cp_parser_require_pragma_eol (parser, pragma_tok);
31892 return;
31894 if (strcmp (p, "target") == 0)
31896 cp_lexer_consume_token (parser->lexer);
31897 cp_parser_omp_declare_target (parser, pragma_tok);
31898 return;
31901 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31902 "or %<target%>");
31903 cp_parser_require_pragma_eol (parser, pragma_tok);
31906 /* Main entry point to OpenMP statement pragmas. */
31908 static void
31909 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31911 tree stmt;
31912 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31913 omp_clause_mask mask (0);
31915 switch (pragma_tok->pragma_kind)
31917 case PRAGMA_OMP_ATOMIC:
31918 cp_parser_omp_atomic (parser, pragma_tok);
31919 return;
31920 case PRAGMA_OMP_CRITICAL:
31921 stmt = cp_parser_omp_critical (parser, pragma_tok);
31922 break;
31923 case PRAGMA_OMP_DISTRIBUTE:
31924 strcpy (p_name, "#pragma omp");
31925 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31926 break;
31927 case PRAGMA_OMP_FOR:
31928 strcpy (p_name, "#pragma omp");
31929 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31930 break;
31931 case PRAGMA_OMP_MASTER:
31932 stmt = cp_parser_omp_master (parser, pragma_tok);
31933 break;
31934 case PRAGMA_OMP_ORDERED:
31935 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31936 break;
31937 case PRAGMA_OMP_PARALLEL:
31938 strcpy (p_name, "#pragma omp");
31939 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31940 break;
31941 case PRAGMA_OMP_SECTIONS:
31942 strcpy (p_name, "#pragma omp");
31943 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31944 break;
31945 case PRAGMA_OMP_SIMD:
31946 strcpy (p_name, "#pragma omp");
31947 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31948 break;
31949 case PRAGMA_OMP_SINGLE:
31950 stmt = cp_parser_omp_single (parser, pragma_tok);
31951 break;
31952 case PRAGMA_OMP_TASK:
31953 stmt = cp_parser_omp_task (parser, pragma_tok);
31954 break;
31955 case PRAGMA_OMP_TASKGROUP:
31956 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31957 break;
31958 case PRAGMA_OMP_TEAMS:
31959 strcpy (p_name, "#pragma omp");
31960 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31961 break;
31962 default:
31963 gcc_unreachable ();
31966 if (stmt)
31967 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31970 /* Transactional Memory parsing routines. */
31972 /* Parse a transaction attribute.
31974 txn-attribute:
31975 attribute
31976 [ [ identifier ] ]
31978 ??? Simplify this when C++0x bracket attributes are
31979 implemented properly. */
31981 static tree
31982 cp_parser_txn_attribute_opt (cp_parser *parser)
31984 cp_token *token;
31985 tree attr_name, attr = NULL;
31987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31988 return cp_parser_attributes_opt (parser);
31990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31991 return NULL_TREE;
31992 cp_lexer_consume_token (parser->lexer);
31993 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31994 goto error1;
31996 token = cp_lexer_peek_token (parser->lexer);
31997 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31999 token = cp_lexer_consume_token (parser->lexer);
32001 attr_name = (token->type == CPP_KEYWORD
32002 /* For keywords, use the canonical spelling,
32003 not the parsed identifier. */
32004 ? ridpointers[(int) token->keyword]
32005 : token->u.value);
32006 attr = build_tree_list (attr_name, NULL_TREE);
32008 else
32009 cp_parser_error (parser, "expected identifier");
32011 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32012 error1:
32013 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32014 return attr;
32017 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32019 transaction-statement:
32020 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32021 compound-statement
32022 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32025 static tree
32026 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32028 unsigned char old_in = parser->in_transaction;
32029 unsigned char this_in = 1, new_in;
32030 cp_token *token;
32031 tree stmt, attrs, noex;
32033 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32034 || keyword == RID_TRANSACTION_RELAXED);
32035 token = cp_parser_require_keyword (parser, keyword,
32036 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32037 : RT_TRANSACTION_RELAXED));
32038 gcc_assert (token != NULL);
32040 if (keyword == RID_TRANSACTION_RELAXED)
32041 this_in |= TM_STMT_ATTR_RELAXED;
32042 else
32044 attrs = cp_parser_txn_attribute_opt (parser);
32045 if (attrs)
32046 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32049 /* Parse a noexcept specification. */
32050 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32052 /* Keep track if we're in the lexical scope of an outer transaction. */
32053 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32055 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32057 parser->in_transaction = new_in;
32058 cp_parser_compound_statement (parser, NULL, false, false);
32059 parser->in_transaction = old_in;
32061 finish_transaction_stmt (stmt, NULL, this_in, noex);
32063 return stmt;
32066 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32068 transaction-expression:
32069 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32070 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32073 static tree
32074 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32076 unsigned char old_in = parser->in_transaction;
32077 unsigned char this_in = 1;
32078 cp_token *token;
32079 tree expr, noex;
32080 bool noex_expr;
32082 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32083 || keyword == RID_TRANSACTION_RELAXED);
32085 if (!flag_tm)
32086 error (keyword == RID_TRANSACTION_RELAXED
32087 ? G_("%<__transaction_relaxed%> without transactional memory "
32088 "support enabled")
32089 : G_("%<__transaction_atomic%> without transactional memory "
32090 "support enabled"));
32092 token = cp_parser_require_keyword (parser, keyword,
32093 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32094 : RT_TRANSACTION_RELAXED));
32095 gcc_assert (token != NULL);
32097 if (keyword == RID_TRANSACTION_RELAXED)
32098 this_in |= TM_STMT_ATTR_RELAXED;
32100 /* Set this early. This might mean that we allow transaction_cancel in
32101 an expression that we find out later actually has to be a constexpr.
32102 However, we expect that cxx_constant_value will be able to deal with
32103 this; also, if the noexcept has no constexpr, then what we parse next
32104 really is a transaction's body. */
32105 parser->in_transaction = this_in;
32107 /* Parse a noexcept specification. */
32108 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32109 true);
32111 if (!noex || !noex_expr
32112 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32116 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
32117 expr = finish_parenthesized_expr (expr);
32119 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32121 else
32123 /* The only expression that is available got parsed for the noexcept
32124 already. noexcept is true then. */
32125 expr = noex;
32126 noex = boolean_true_node;
32129 expr = build_transaction_expr (token->location, expr, this_in, noex);
32130 parser->in_transaction = old_in;
32132 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32133 return error_mark_node;
32135 return (flag_tm ? expr : error_mark_node);
32138 /* Parse a function-transaction-block.
32140 function-transaction-block:
32141 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32142 function-body
32143 __transaction_atomic txn-attribute[opt] function-try-block
32144 __transaction_relaxed ctor-initializer[opt] function-body
32145 __transaction_relaxed function-try-block
32148 static bool
32149 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32151 unsigned char old_in = parser->in_transaction;
32152 unsigned char new_in = 1;
32153 tree compound_stmt, stmt, attrs;
32154 bool ctor_initializer_p;
32155 cp_token *token;
32157 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32158 || keyword == RID_TRANSACTION_RELAXED);
32159 token = cp_parser_require_keyword (parser, keyword,
32160 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32161 : RT_TRANSACTION_RELAXED));
32162 gcc_assert (token != NULL);
32164 if (keyword == RID_TRANSACTION_RELAXED)
32165 new_in |= TM_STMT_ATTR_RELAXED;
32166 else
32168 attrs = cp_parser_txn_attribute_opt (parser);
32169 if (attrs)
32170 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32173 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32175 parser->in_transaction = new_in;
32177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32178 ctor_initializer_p = cp_parser_function_try_block (parser);
32179 else
32180 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32181 (parser, /*in_function_try_block=*/false);
32183 parser->in_transaction = old_in;
32185 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32187 return ctor_initializer_p;
32190 /* Parse a __transaction_cancel statement.
32192 cancel-statement:
32193 __transaction_cancel txn-attribute[opt] ;
32194 __transaction_cancel txn-attribute[opt] throw-expression ;
32196 ??? Cancel and throw is not yet implemented. */
32198 static tree
32199 cp_parser_transaction_cancel (cp_parser *parser)
32201 cp_token *token;
32202 bool is_outer = false;
32203 tree stmt, attrs;
32205 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32206 RT_TRANSACTION_CANCEL);
32207 gcc_assert (token != NULL);
32209 attrs = cp_parser_txn_attribute_opt (parser);
32210 if (attrs)
32211 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32213 /* ??? Parse cancel-and-throw here. */
32215 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32217 if (!flag_tm)
32219 error_at (token->location, "%<__transaction_cancel%> without "
32220 "transactional memory support enabled");
32221 return error_mark_node;
32223 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32225 error_at (token->location, "%<__transaction_cancel%> within a "
32226 "%<__transaction_relaxed%>");
32227 return error_mark_node;
32229 else if (is_outer)
32231 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32232 && !is_tm_may_cancel_outer (current_function_decl))
32234 error_at (token->location, "outer %<__transaction_cancel%> not "
32235 "within outer %<__transaction_atomic%>");
32236 error_at (token->location,
32237 " or a %<transaction_may_cancel_outer%> function");
32238 return error_mark_node;
32241 else if (parser->in_transaction == 0)
32243 error_at (token->location, "%<__transaction_cancel%> not within "
32244 "%<__transaction_atomic%>");
32245 return error_mark_node;
32248 stmt = build_tm_abort_call (token->location, is_outer);
32249 add_stmt (stmt);
32251 return stmt;
32254 /* The parser. */
32256 static GTY (()) cp_parser *the_parser;
32259 /* Special handling for the first token or line in the file. The first
32260 thing in the file might be #pragma GCC pch_preprocess, which loads a
32261 PCH file, which is a GC collection point. So we need to handle this
32262 first pragma without benefit of an existing lexer structure.
32264 Always returns one token to the caller in *FIRST_TOKEN. This is
32265 either the true first token of the file, or the first token after
32266 the initial pragma. */
32268 static void
32269 cp_parser_initial_pragma (cp_token *first_token)
32271 tree name = NULL;
32273 cp_lexer_get_preprocessor_token (NULL, first_token);
32274 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32275 return;
32277 cp_lexer_get_preprocessor_token (NULL, first_token);
32278 if (first_token->type == CPP_STRING)
32280 name = first_token->u.value;
32282 cp_lexer_get_preprocessor_token (NULL, first_token);
32283 if (first_token->type != CPP_PRAGMA_EOL)
32284 error_at (first_token->location,
32285 "junk at end of %<#pragma GCC pch_preprocess%>");
32287 else
32288 error_at (first_token->location, "expected string literal");
32290 /* Skip to the end of the pragma. */
32291 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32292 cp_lexer_get_preprocessor_token (NULL, first_token);
32294 /* Now actually load the PCH file. */
32295 if (name)
32296 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32298 /* Read one more token to return to our caller. We have to do this
32299 after reading the PCH file in, since its pointers have to be
32300 live. */
32301 cp_lexer_get_preprocessor_token (NULL, first_token);
32304 /* Normal parsing of a pragma token. Here we can (and must) use the
32305 regular lexer. */
32307 static bool
32308 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32310 cp_token *pragma_tok;
32311 unsigned int id;
32313 pragma_tok = cp_lexer_consume_token (parser->lexer);
32314 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32315 parser->lexer->in_pragma = true;
32317 id = pragma_tok->pragma_kind;
32318 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32319 cp_ensure_no_omp_declare_simd (parser);
32320 switch (id)
32322 case PRAGMA_GCC_PCH_PREPROCESS:
32323 error_at (pragma_tok->location,
32324 "%<#pragma GCC pch_preprocess%> must be first");
32325 break;
32327 case PRAGMA_OMP_BARRIER:
32328 switch (context)
32330 case pragma_compound:
32331 cp_parser_omp_barrier (parser, pragma_tok);
32332 return false;
32333 case pragma_stmt:
32334 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32335 "used in compound statements");
32336 break;
32337 default:
32338 goto bad_stmt;
32340 break;
32342 case PRAGMA_OMP_FLUSH:
32343 switch (context)
32345 case pragma_compound:
32346 cp_parser_omp_flush (parser, pragma_tok);
32347 return false;
32348 case pragma_stmt:
32349 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32350 "used in compound statements");
32351 break;
32352 default:
32353 goto bad_stmt;
32355 break;
32357 case PRAGMA_OMP_TASKWAIT:
32358 switch (context)
32360 case pragma_compound:
32361 cp_parser_omp_taskwait (parser, pragma_tok);
32362 return false;
32363 case pragma_stmt:
32364 error_at (pragma_tok->location,
32365 "%<#pragma omp taskwait%> may only be "
32366 "used in compound statements");
32367 break;
32368 default:
32369 goto bad_stmt;
32371 break;
32373 case PRAGMA_OMP_TASKYIELD:
32374 switch (context)
32376 case pragma_compound:
32377 cp_parser_omp_taskyield (parser, pragma_tok);
32378 return false;
32379 case pragma_stmt:
32380 error_at (pragma_tok->location,
32381 "%<#pragma omp taskyield%> may only be "
32382 "used in compound statements");
32383 break;
32384 default:
32385 goto bad_stmt;
32387 break;
32389 case PRAGMA_OMP_CANCEL:
32390 switch (context)
32392 case pragma_compound:
32393 cp_parser_omp_cancel (parser, pragma_tok);
32394 return false;
32395 case pragma_stmt:
32396 error_at (pragma_tok->location,
32397 "%<#pragma omp cancel%> may only be "
32398 "used in compound statements");
32399 break;
32400 default:
32401 goto bad_stmt;
32403 break;
32405 case PRAGMA_OMP_CANCELLATION_POINT:
32406 switch (context)
32408 case pragma_compound:
32409 cp_parser_omp_cancellation_point (parser, pragma_tok);
32410 return false;
32411 case pragma_stmt:
32412 error_at (pragma_tok->location,
32413 "%<#pragma omp cancellation point%> may only be "
32414 "used in compound statements");
32415 break;
32416 default:
32417 goto bad_stmt;
32419 break;
32421 case PRAGMA_OMP_THREADPRIVATE:
32422 cp_parser_omp_threadprivate (parser, pragma_tok);
32423 return false;
32425 case PRAGMA_OMP_DECLARE_REDUCTION:
32426 cp_parser_omp_declare (parser, pragma_tok, context);
32427 return false;
32429 case PRAGMA_OMP_ATOMIC:
32430 case PRAGMA_OMP_CRITICAL:
32431 case PRAGMA_OMP_DISTRIBUTE:
32432 case PRAGMA_OMP_FOR:
32433 case PRAGMA_OMP_MASTER:
32434 case PRAGMA_OMP_ORDERED:
32435 case PRAGMA_OMP_PARALLEL:
32436 case PRAGMA_OMP_SECTIONS:
32437 case PRAGMA_OMP_SIMD:
32438 case PRAGMA_OMP_SINGLE:
32439 case PRAGMA_OMP_TASK:
32440 case PRAGMA_OMP_TASKGROUP:
32441 case PRAGMA_OMP_TEAMS:
32442 if (context != pragma_stmt && context != pragma_compound)
32443 goto bad_stmt;
32444 cp_parser_omp_construct (parser, pragma_tok);
32445 return true;
32447 case PRAGMA_OMP_TARGET:
32448 return cp_parser_omp_target (parser, pragma_tok, context);
32450 case PRAGMA_OMP_END_DECLARE_TARGET:
32451 cp_parser_omp_end_declare_target (parser, pragma_tok);
32452 return false;
32454 case PRAGMA_OMP_SECTION:
32455 error_at (pragma_tok->location,
32456 "%<#pragma omp section%> may only be used in "
32457 "%<#pragma omp sections%> construct");
32458 break;
32460 case PRAGMA_IVDEP:
32462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32463 cp_token *tok;
32464 tok = cp_lexer_peek_token (the_parser->lexer);
32465 if (tok->type != CPP_KEYWORD
32466 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32467 && tok->keyword != RID_DO))
32469 cp_parser_error (parser, "for, while or do statement expected");
32470 return false;
32472 cp_parser_iteration_statement (parser, true);
32473 return true;
32476 case PRAGMA_CILK_SIMD:
32477 if (context == pragma_external)
32479 error_at (pragma_tok->location,
32480 "%<#pragma simd%> must be inside a function");
32481 break;
32483 cp_parser_cilk_simd (parser, pragma_tok);
32484 return true;
32486 default:
32487 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32488 c_invoke_pragma_handler (id);
32489 break;
32491 bad_stmt:
32492 cp_parser_error (parser, "expected declaration specifiers");
32493 break;
32496 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32497 return false;
32500 /* The interface the pragma parsers have to the lexer. */
32502 enum cpp_ttype
32503 pragma_lex (tree *value)
32505 cp_token *tok;
32506 enum cpp_ttype ret;
32508 tok = cp_lexer_peek_token (the_parser->lexer);
32510 ret = tok->type;
32511 *value = tok->u.value;
32513 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32514 ret = CPP_EOF;
32515 else if (ret == CPP_STRING)
32516 *value = cp_parser_string_literal (the_parser, false, false);
32517 else
32519 cp_lexer_consume_token (the_parser->lexer);
32520 if (ret == CPP_KEYWORD)
32521 ret = CPP_NAME;
32524 return ret;
32528 /* External interface. */
32530 /* Parse one entire translation unit. */
32532 void
32533 c_parse_file (void)
32535 static bool already_called = false;
32537 if (already_called)
32539 sorry ("inter-module optimizations not implemented for C++");
32540 return;
32542 already_called = true;
32544 the_parser = cp_parser_new ();
32545 push_deferring_access_checks (flag_access_control
32546 ? dk_no_deferred : dk_no_check);
32547 cp_parser_translation_unit (the_parser);
32548 the_parser = NULL;
32551 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32552 vectorlength clause:
32553 Syntax:
32554 vectorlength ( constant-expression ) */
32556 static tree
32557 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32558 bool is_simd_fn)
32560 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32561 tree expr;
32562 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32563 safelen clause. Thus, vectorlength is represented as OMP 4.0
32564 safelen. For SIMD-enabled function it is represented by OMP 4.0
32565 simdlen. */
32566 if (!is_simd_fn)
32567 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32568 loc);
32569 else
32570 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32571 loc);
32573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32574 return error_mark_node;
32576 expr = cp_parser_constant_expression (parser, false, NULL);
32577 expr = maybe_constant_value (expr);
32579 /* If expr == error_mark_node, then don't emit any errors nor
32580 create a clause. if any of the above functions returns
32581 error mark node then they would have emitted an error message. */
32582 if (expr == error_mark_node)
32584 else if (!TREE_TYPE (expr)
32585 || !TREE_CONSTANT (expr)
32586 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32587 error_at (loc, "vectorlength must be an integer constant");
32588 else if (TREE_CONSTANT (expr)
32589 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32590 error_at (loc, "vectorlength must be a power of 2");
32591 else
32593 tree c;
32594 if (!is_simd_fn)
32596 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32597 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32598 OMP_CLAUSE_CHAIN (c) = clauses;
32599 clauses = c;
32601 else
32603 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32604 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32605 OMP_CLAUSE_CHAIN (c) = clauses;
32606 clauses = c;
32610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32611 return error_mark_node;
32612 return clauses;
32615 /* Handles the Cilk Plus #pragma simd linear clause.
32616 Syntax:
32617 linear ( simd-linear-variable-list )
32619 simd-linear-variable-list:
32620 simd-linear-variable
32621 simd-linear-variable-list , simd-linear-variable
32623 simd-linear-variable:
32624 id-expression
32625 id-expression : simd-linear-step
32627 simd-linear-step:
32628 conditional-expression */
32630 static tree
32631 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32633 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32635 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32636 return clauses;
32637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32639 cp_parser_error (parser, "expected identifier");
32640 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32641 return error_mark_node;
32644 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32645 parser->colon_corrects_to_scope_p = false;
32646 while (1)
32648 cp_token *token = cp_lexer_peek_token (parser->lexer);
32649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32651 cp_parser_error (parser, "expected variable-name");
32652 clauses = error_mark_node;
32653 break;
32656 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32657 false, false);
32658 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32659 token->location);
32660 if (decl == error_mark_node)
32662 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32663 token->location);
32664 clauses = error_mark_node;
32666 else
32668 tree e = NULL_TREE;
32669 tree step_size = integer_one_node;
32671 /* If present, parse the linear step. Otherwise, assume the default
32672 value of 1. */
32673 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32675 cp_lexer_consume_token (parser->lexer);
32677 e = cp_parser_assignment_expression (parser, false, NULL);
32678 e = maybe_constant_value (e);
32680 if (e == error_mark_node)
32682 /* If an error has occurred, then the whole pragma is
32683 considered ill-formed. Thus, no reason to keep
32684 parsing. */
32685 clauses = error_mark_node;
32686 break;
32688 else if (type_dependent_expression_p (e)
32689 || value_dependent_expression_p (e)
32690 || (TREE_TYPE (e)
32691 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32692 && (TREE_CONSTANT (e)
32693 || DECL_P (e))))
32694 step_size = e;
32695 else
32696 cp_parser_error (parser,
32697 "step size must be an integer constant "
32698 "expression or an integer variable");
32701 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32702 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32703 OMP_CLAUSE_DECL (l) = decl;
32704 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32705 OMP_CLAUSE_CHAIN (l) = clauses;
32706 clauses = l;
32708 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32709 cp_lexer_consume_token (parser->lexer);
32710 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32711 break;
32712 else
32714 error_at (cp_lexer_peek_token (parser->lexer)->location,
32715 "expected %<,%> or %<)%> after %qE", decl);
32716 clauses = error_mark_node;
32717 break;
32720 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32721 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32722 return clauses;
32725 /* Returns the name of the next clause. If the clause is not
32726 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32727 token is not consumed. Otherwise, the appropriate enum from the
32728 pragma_simd_clause is returned and the token is consumed. */
32730 static pragma_omp_clause
32731 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32733 pragma_omp_clause clause_type;
32734 cp_token *token = cp_lexer_peek_token (parser->lexer);
32736 if (token->keyword == RID_PRIVATE)
32737 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32738 else if (!token->u.value || token->type != CPP_NAME)
32739 return PRAGMA_CILK_CLAUSE_NONE;
32740 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32741 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32742 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32743 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32744 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32745 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32746 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32747 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32748 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32749 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32750 else
32751 return PRAGMA_CILK_CLAUSE_NONE;
32753 cp_lexer_consume_token (parser->lexer);
32754 return clause_type;
32757 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32759 static tree
32760 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32762 tree clauses = NULL_TREE;
32764 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32765 && clauses != error_mark_node)
32767 pragma_omp_clause c_kind;
32768 c_kind = cp_parser_cilk_simd_clause_name (parser);
32769 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32770 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32771 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32772 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32773 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32774 /* Use the OpenMP 4.0 equivalent function. */
32775 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32776 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32777 /* Use the OpenMP 4.0 equivalent function. */
32778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32779 clauses);
32780 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32781 /* Use the OMP 4.0 equivalent function. */
32782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32783 clauses);
32784 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32785 /* Use the OMP 4.0 equivalent function. */
32786 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32787 else
32789 clauses = error_mark_node;
32790 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32791 break;
32795 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32797 if (clauses == error_mark_node)
32798 return error_mark_node;
32799 else
32800 return c_finish_cilk_clauses (clauses);
32803 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32805 static void
32806 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32808 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32810 if (clauses == error_mark_node)
32811 return;
32813 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32815 error_at (cp_lexer_peek_token (parser->lexer)->location,
32816 "for statement expected");
32817 return;
32820 tree sb = begin_omp_structured_block ();
32821 int save = cp_parser_begin_omp_structured_block (parser);
32822 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32823 if (ret)
32824 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32825 cp_parser_end_omp_structured_block (parser, save);
32826 add_stmt (finish_omp_structured_block (sb));
32827 return;
32830 /* Create an identifier for a generic parameter type (a synthesized
32831 template parameter implied by `auto' or a concept identifier). */
32833 static GTY(()) int generic_parm_count;
32834 static tree
32835 make_generic_type_name ()
32837 char buf[32];
32838 sprintf (buf, "auto:%d", ++generic_parm_count);
32839 return get_identifier (buf);
32842 /* Predicate that behaves as is_auto_or_concept but matches the parent
32843 node of the generic type rather than the generic type itself. This
32844 allows for type transformation in add_implicit_template_parms. */
32846 static inline bool
32847 tree_type_is_auto_or_concept (const_tree t)
32849 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32852 // Return the template decl being called or evaluated as part of the
32853 // constraint check.
32855 // TODO: This is a bit of a hack. When we finish the template parameter
32856 // the constraint is just a call expression, but we don't have the full
32857 // context that we used to build that call expression. Since we're going
32858 // to be comparing declarations, it would helpful to have that. This
32859 // means we'll have to make the TREE_TYPE of the parameter node a pair
32860 // containing the context (the TYPE_DECL) and the constraint.
32861 static tree
32862 get_concept_from_constraint (tree t)
32864 gcc_assert (TREE_CODE (t) == CALL_EXPR);
32865 tree fn = CALL_EXPR_FN (t);
32866 tree ovl = TREE_OPERAND (fn, 0);
32867 tree tmpl = OVL_FUNCTION (ovl);
32868 return DECL_TEMPLATE_RESULT (tmpl);
32871 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32872 (creating a new template parameter list if necessary). Returns the newly
32873 created template type parm. */
32875 tree
32876 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
32878 gcc_assert (current_binding_level->kind == sk_function_parms);
32880 // Before committing to modifying any scope, if we're in an implicit
32881 // template scope, and we're trying to synthesize a constrained
32882 // parameter, try to find a previous parameter with the same name.
32883 if (parser->implicit_template_scope && constr)
32885 tree t = parser->implicit_template_parms;
32886 while (t)
32888 tree c = get_concept_from_constraint (TREE_TYPE (t));
32889 if (c == DECL_SIZE_UNIT (constr))
32890 return TREE_VALUE (t);
32891 t = TREE_CHAIN (t);
32896 /* We are either continuing a function template that already contains implicit
32897 template parameters, creating a new fully-implicit function template, or
32898 extending an existing explicit function template with implicit template
32899 parameters. */
32901 cp_binding_level *const entry_scope = current_binding_level;
32903 bool become_template = false;
32904 cp_binding_level *parent_scope = 0;
32906 if (parser->implicit_template_scope)
32908 gcc_assert (parser->implicit_template_parms);
32910 current_binding_level = parser->implicit_template_scope;
32912 else
32914 /* Roll back to the existing template parameter scope (in the case of
32915 extending an explicit function template) or introduce a new template
32916 parameter scope ahead of the function parameter scope (or class scope
32917 in the case of out-of-line member definitions). The function scope is
32918 added back after template parameter synthesis below. */
32920 cp_binding_level *scope = entry_scope;
32922 while (scope->kind == sk_function_parms)
32924 parent_scope = scope;
32925 scope = scope->level_chain;
32927 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32929 /* If not defining a class, then any class scope is a scope level in
32930 an out-of-line member definition. In this case simply wind back
32931 beyond the first such scope to inject the template parameter list.
32932 Otherwise wind back to the class being defined. The latter can
32933 occur in class member friend declarations such as:
32935 class A {
32936 void foo (auto);
32938 class B {
32939 friend void A::foo (auto);
32942 The template parameter list synthesized for the friend declaration
32943 must be injected in the scope of 'B'. This can also occur in
32944 erroneous cases such as:
32946 struct A {
32947 struct B {
32948 void foo (auto);
32950 void B::foo (auto) {}
32953 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32954 but, nevertheless, the template parameter list synthesized for the
32955 declarator should be injected into the scope of 'A' as if the
32956 ill-formed template was specified explicitly. */
32958 while (scope->kind == sk_class && !scope->defining_class_p)
32960 parent_scope = scope;
32961 scope = scope->level_chain;
32965 current_binding_level = scope;
32967 if (scope->kind != sk_template_parms
32968 || !function_being_declared_is_template_p (parser))
32970 /* Introduce a new template parameter list for implicit template
32971 parameters. */
32973 become_template = true;
32975 parser->implicit_template_scope
32976 = begin_scope (sk_template_parms, NULL);
32978 ++processing_template_decl;
32980 parser->fully_implicit_function_template_p = true;
32981 ++parser->num_template_parameter_lists;
32983 else
32985 /* Synthesize implicit template parameters at the end of the explicit
32986 template parameter list. */
32988 gcc_assert (current_template_parms);
32990 parser->implicit_template_scope = scope;
32992 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32993 parser->implicit_template_parms
32994 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32998 /* Synthesize a new template parameter and track the current template
32999 parameter chain with implicit_template_parms. */
33001 tree synth_id = make_generic_type_name ();
33002 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33003 synth_id);
33005 // Attach the constraint to the parm before processing.
33006 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
33007 TREE_TYPE (node) = constr;
33009 tree new_parm
33010 = process_template_parm (parser->implicit_template_parms,
33011 input_location,
33012 node,
33013 /*non_type=*/false,
33014 /*param_pack=*/false);
33017 if (parser->implicit_template_parms)
33018 parser->implicit_template_parms
33019 = TREE_CHAIN (parser->implicit_template_parms);
33020 else
33021 parser->implicit_template_parms = new_parm;
33023 tree new_type = TREE_TYPE (getdecls ());
33025 /* If creating a fully implicit function template, start the new implicit
33026 template parameter list with this synthesized type, otherwise grow the
33027 current template parameter list. */
33029 if (become_template)
33031 parent_scope->level_chain = current_binding_level;
33033 tree new_parms = make_tree_vec (1);
33034 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33035 current_template_parms = tree_cons (size_int (processing_template_decl),
33036 new_parms, current_template_parms);
33038 else
33040 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33041 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33042 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33043 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33046 current_binding_level = entry_scope;
33048 return new_type;
33051 /* Finish the declaration of a fully implicit function template. Such a
33052 template has no explicit template parameter list so has not been through the
33053 normal template head and tail processing. synthesize_implicit_template_parm
33054 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33055 provided if the declaration is a class member such that its template
33056 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33057 form is returned. Otherwise NULL_TREE is returned. */
33059 tree
33060 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33062 gcc_assert (parser->fully_implicit_function_template_p);
33064 if (member_decl_opt && member_decl_opt != error_mark_node
33065 && DECL_VIRTUAL_P (member_decl_opt))
33067 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33068 "implicit templates may not be %<virtual%>");
33069 DECL_VIRTUAL_P (member_decl_opt) = false;
33072 if (member_decl_opt)
33073 member_decl_opt = finish_member_template_decl (member_decl_opt);
33074 end_template_decl ();
33076 parser->fully_implicit_function_template_p = false;
33077 --parser->num_template_parameter_lists;
33079 return member_decl_opt;
33082 #include "gt-cp-parser.h"