don't check in warn_logical_not_paren-check on folded result of current's lhs.
[official-gcc.git] / gcc / cp / parser.c
blob7c48f4cce3ac8a18edd60bbd10bba70ab71917f5
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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 "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "print-tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "trans-mem.h"
34 #include "cp-tree.h"
35 #include "intl.h"
36 #include "c-family/c-pragma.h"
37 #include "decl.h"
38 #include "flags.h"
39 #include "diagnostic-core.h"
40 #include "target.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "cgraph.h"
44 #include "c-family/c-common.h"
45 #include "c-family/c-objc.h"
46 #include "plugin.h"
47 #include "tree-pretty-print.h"
48 #include "parser.h"
49 #include "type-utils.h"
50 #include "omp-low.h"
51 #include "gomp-constants.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.c) and the C++ parser. */
59 static cp_token eof_token =
61 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
64 /* The various kinds of non integral constant we encounter. */
65 typedef enum non_integral_constant {
66 NIC_NONE,
67 /* floating-point literal */
68 NIC_FLOAT,
69 /* %<this%> */
70 NIC_THIS,
71 /* %<__FUNCTION__%> */
72 NIC_FUNC_NAME,
73 /* %<__PRETTY_FUNCTION__%> */
74 NIC_PRETTY_FUNC,
75 /* %<__func__%> */
76 NIC_C99_FUNC,
77 /* "%<va_arg%> */
78 NIC_VA_ARG,
79 /* a cast */
80 NIC_CAST,
81 /* %<typeid%> operator */
82 NIC_TYPEID,
83 /* non-constant compound literals */
84 NIC_NCC,
85 /* a function call */
86 NIC_FUNC_CALL,
87 /* an increment */
88 NIC_INC,
89 /* an decrement */
90 NIC_DEC,
91 /* an array reference */
92 NIC_ARRAY_REF,
93 /* %<->%> */
94 NIC_ARROW,
95 /* %<.%> */
96 NIC_POINT,
97 /* the address of a label */
98 NIC_ADDR_LABEL,
99 /* %<*%> */
100 NIC_STAR,
101 /* %<&%> */
102 NIC_ADDR,
103 /* %<++%> */
104 NIC_PREINCREMENT,
105 /* %<--%> */
106 NIC_PREDECREMENT,
107 /* %<new%> */
108 NIC_NEW,
109 /* %<delete%> */
110 NIC_DEL,
111 /* calls to overloaded operators */
112 NIC_OVERLOADED,
113 /* an assignment */
114 NIC_ASSIGNMENT,
115 /* a comma operator */
116 NIC_COMMA,
117 /* a call to a constructor */
118 NIC_CONSTRUCTOR,
119 /* a transaction expression */
120 NIC_TRANSACTION
121 } non_integral_constant;
123 /* The various kinds of errors about name-lookup failing. */
124 typedef enum name_lookup_error {
125 /* NULL */
126 NLE_NULL,
127 /* is not a type */
128 NLE_TYPE,
129 /* is not a class or namespace */
130 NLE_CXX98,
131 /* is not a class, namespace, or enumeration */
132 NLE_NOT_CXX98
133 } name_lookup_error;
135 /* The various kinds of required token */
136 typedef enum required_token {
137 RT_NONE,
138 RT_SEMICOLON, /* ';' */
139 RT_OPEN_PAREN, /* '(' */
140 RT_CLOSE_BRACE, /* '}' */
141 RT_OPEN_BRACE, /* '{' */
142 RT_CLOSE_SQUARE, /* ']' */
143 RT_OPEN_SQUARE, /* '[' */
144 RT_COMMA, /* ',' */
145 RT_SCOPE, /* '::' */
146 RT_LESS, /* '<' */
147 RT_GREATER, /* '>' */
148 RT_EQ, /* '=' */
149 RT_ELLIPSIS, /* '...' */
150 RT_MULT, /* '*' */
151 RT_COMPL, /* '~' */
152 RT_COLON, /* ':' */
153 RT_COLON_SCOPE, /* ':' or '::' */
154 RT_CLOSE_PAREN, /* ')' */
155 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
156 RT_PRAGMA_EOL, /* end of line */
157 RT_NAME, /* identifier */
159 /* The type is CPP_KEYWORD */
160 RT_NEW, /* new */
161 RT_DELETE, /* delete */
162 RT_RETURN, /* return */
163 RT_WHILE, /* while */
164 RT_EXTERN, /* extern */
165 RT_STATIC_ASSERT, /* static_assert */
166 RT_DECLTYPE, /* decltype */
167 RT_OPERATOR, /* operator */
168 RT_CLASS, /* class */
169 RT_TEMPLATE, /* template */
170 RT_NAMESPACE, /* namespace */
171 RT_USING, /* using */
172 RT_ASM, /* asm */
173 RT_TRY, /* try */
174 RT_CATCH, /* catch */
175 RT_THROW, /* throw */
176 RT_LABEL, /* __label__ */
177 RT_AT_TRY, /* @try */
178 RT_AT_SYNCHRONIZED, /* @synchronized */
179 RT_AT_THROW, /* @throw */
181 RT_SELECT, /* selection-statement */
182 RT_INTERATION, /* iteration-statement */
183 RT_JUMP, /* jump-statement */
184 RT_CLASS_KEY, /* class-key */
185 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
186 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
187 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
188 RT_TRANSACTION_CANCEL /* __transaction_cancel */
189 } required_token;
191 /* Prototypes. */
193 static cp_lexer *cp_lexer_new_main
194 (void);
195 static cp_lexer *cp_lexer_new_from_tokens
196 (cp_token_cache *tokens);
197 static void cp_lexer_destroy
198 (cp_lexer *);
199 static int cp_lexer_saving_tokens
200 (const cp_lexer *);
201 static cp_token *cp_lexer_token_at
202 (cp_lexer *, cp_token_position);
203 static void cp_lexer_get_preprocessor_token
204 (cp_lexer *, cp_token *);
205 static inline cp_token *cp_lexer_peek_token
206 (cp_lexer *);
207 static cp_token *cp_lexer_peek_nth_token
208 (cp_lexer *, size_t);
209 static inline bool cp_lexer_next_token_is
210 (cp_lexer *, enum cpp_ttype);
211 static bool cp_lexer_next_token_is_not
212 (cp_lexer *, enum cpp_ttype);
213 static bool cp_lexer_next_token_is_keyword
214 (cp_lexer *, enum rid);
215 static cp_token *cp_lexer_consume_token
216 (cp_lexer *);
217 static void cp_lexer_purge_token
218 (cp_lexer *);
219 static void cp_lexer_purge_tokens_after
220 (cp_lexer *, cp_token_position);
221 static void cp_lexer_save_tokens
222 (cp_lexer *);
223 static void cp_lexer_commit_tokens
224 (cp_lexer *);
225 static void cp_lexer_rollback_tokens
226 (cp_lexer *);
227 static void cp_lexer_print_token
228 (FILE *, cp_token *);
229 static inline bool cp_lexer_debugging_p
230 (cp_lexer *);
231 static void cp_lexer_start_debugging
232 (cp_lexer *) ATTRIBUTE_UNUSED;
233 static void cp_lexer_stop_debugging
234 (cp_lexer *) ATTRIBUTE_UNUSED;
236 static cp_token_cache *cp_token_cache_new
237 (cp_token *, cp_token *);
239 static void cp_parser_initial_pragma
240 (cp_token *);
242 static tree cp_literal_operator_id
243 (const char *);
245 static void cp_parser_cilk_simd
246 (cp_parser *, cp_token *);
247 static tree cp_parser_cilk_for
248 (cp_parser *, tree);
249 static bool cp_parser_omp_declare_reduction_exprs
250 (tree, cp_parser *);
251 static tree cp_parser_cilk_simd_vectorlength
252 (cp_parser *, tree, bool);
254 /* Manifest constants. */
255 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
256 #define CP_SAVED_TOKEN_STACK 5
258 /* Variables. */
260 /* The stream to which debugging output should be written. */
261 static FILE *cp_lexer_debug_stream;
263 /* Nonzero if we are parsing an unevaluated operand: an operand to
264 sizeof, typeof, or alignof. */
265 int cp_unevaluated_operand;
267 /* Dump up to NUM tokens in BUFFER to FILE starting with token
268 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
269 first token in BUFFER. If NUM is 0, dump all the tokens. If
270 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
271 highlighted by surrounding it in [[ ]]. */
273 static void
274 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
275 cp_token *start_token, unsigned num,
276 cp_token *curr_token)
278 unsigned i, nprinted;
279 cp_token *token;
280 bool do_print;
282 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
284 if (buffer == NULL)
285 return;
287 if (num == 0)
288 num = buffer->length ();
290 if (start_token == NULL)
291 start_token = buffer->address ();
293 if (start_token > buffer->address ())
295 cp_lexer_print_token (file, &(*buffer)[0]);
296 fprintf (file, " ... ");
299 do_print = false;
300 nprinted = 0;
301 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
303 if (token == start_token)
304 do_print = true;
306 if (!do_print)
307 continue;
309 nprinted++;
310 if (token == curr_token)
311 fprintf (file, "[[");
313 cp_lexer_print_token (file, token);
315 if (token == curr_token)
316 fprintf (file, "]]");
318 switch (token->type)
320 case CPP_SEMICOLON:
321 case CPP_OPEN_BRACE:
322 case CPP_CLOSE_BRACE:
323 case CPP_EOF:
324 fputc ('\n', file);
325 break;
327 default:
328 fputc (' ', file);
332 if (i == num && i < buffer->length ())
334 fprintf (file, " ... ");
335 cp_lexer_print_token (file, &buffer->last ());
338 fprintf (file, "\n");
342 /* Dump all tokens in BUFFER to stderr. */
344 void
345 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
347 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
350 DEBUG_FUNCTION void
351 debug (vec<cp_token, va_gc> &ref)
353 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> *ptr)
359 if (ptr)
360 debug (*ptr);
361 else
362 fprintf (stderr, "<nil>\n");
366 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
367 description for T. */
369 static void
370 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
372 if (t)
374 fprintf (file, "%s: ", desc);
375 print_node_brief (file, "", t, 0);
380 /* Dump parser context C to FILE. */
382 static void
383 cp_debug_print_context (FILE *file, cp_parser_context *c)
385 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
386 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
387 print_node_brief (file, "", c->object_type, 0);
388 fprintf (file, "}\n");
392 /* Print the stack of parsing contexts to FILE starting with FIRST. */
394 static void
395 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
397 unsigned i;
398 cp_parser_context *c;
400 fprintf (file, "Parsing context stack:\n");
401 for (i = 0, c = first; c; c = c->next, i++)
403 fprintf (file, "\t#%u: ", i);
404 cp_debug_print_context (file, c);
409 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
411 static void
412 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
414 if (flag)
415 fprintf (file, "%s: true\n", desc);
419 /* Print an unparsed function entry UF to FILE. */
421 static void
422 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
424 unsigned i;
425 cp_default_arg_entry *default_arg_fn;
426 tree fn;
428 fprintf (file, "\tFunctions with default args:\n");
429 for (i = 0;
430 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
431 i++)
433 fprintf (file, "\t\tClass type: ");
434 print_node_brief (file, "", default_arg_fn->class_type, 0);
435 fprintf (file, "\t\tDeclaration: ");
436 print_node_brief (file, "", default_arg_fn->decl, 0);
437 fprintf (file, "\n");
440 fprintf (file, "\n\tFunctions with definitions that require "
441 "post-processing\n\t\t");
442 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
444 print_node_brief (file, "", fn, 0);
445 fprintf (file, " ");
447 fprintf (file, "\n");
449 fprintf (file, "\n\tNon-static data members with initializers that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
460 /* Print the stack of unparsed member functions S to FILE. */
462 static void
463 cp_debug_print_unparsed_queues (FILE *file,
464 vec<cp_unparsed_functions_entry, va_gc> *s)
466 unsigned i;
467 cp_unparsed_functions_entry *uf;
469 fprintf (file, "Unparsed functions\n");
470 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
472 fprintf (file, "#%u:\n", i);
473 cp_debug_print_unparsed_function (file, uf);
478 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
479 the given PARSER. If FILE is NULL, the output is printed on stderr. */
481 static void
482 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
484 cp_token *next_token, *first_token, *start_token;
486 if (file == NULL)
487 file = stderr;
489 next_token = parser->lexer->next_token;
490 first_token = parser->lexer->buffer->address ();
491 start_token = (next_token > first_token + window_size / 2)
492 ? next_token - window_size / 2
493 : first_token;
494 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
495 next_token);
499 /* Dump debugging information for the given PARSER. If FILE is NULL,
500 the output is printed on stderr. */
502 void
503 cp_debug_parser (FILE *file, cp_parser *parser)
505 const size_t window_size = 20;
506 cp_token *token;
507 expanded_location eloc;
509 if (file == NULL)
510 file = stderr;
512 fprintf (file, "Parser state\n\n");
513 fprintf (file, "Number of tokens: %u\n",
514 vec_safe_length (parser->lexer->buffer));
515 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
516 cp_debug_print_tree_if_set (file, "Object scope",
517 parser->object_scope);
518 cp_debug_print_tree_if_set (file, "Qualifying scope",
519 parser->qualifying_scope);
520 cp_debug_print_context_stack (file, parser->context);
521 cp_debug_print_flag (file, "Allow GNU extensions",
522 parser->allow_gnu_extensions_p);
523 cp_debug_print_flag (file, "'>' token is greater-than",
524 parser->greater_than_is_operator_p);
525 cp_debug_print_flag (file, "Default args allowed in current "
526 "parameter list", parser->default_arg_ok_p);
527 cp_debug_print_flag (file, "Parsing integral constant-expression",
528 parser->integral_constant_expression_p);
529 cp_debug_print_flag (file, "Allow non-constant expression in current "
530 "constant-expression",
531 parser->allow_non_integral_constant_expression_p);
532 cp_debug_print_flag (file, "Seen non-constant expression",
533 parser->non_integral_constant_expression_p);
534 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
535 "current context",
536 parser->local_variables_forbidden_p);
537 cp_debug_print_flag (file, "In unbraced linkage specification",
538 parser->in_unbraced_linkage_specification_p);
539 cp_debug_print_flag (file, "Parsing a declarator",
540 parser->in_declarator_p);
541 cp_debug_print_flag (file, "In template argument list",
542 parser->in_template_argument_list_p);
543 cp_debug_print_flag (file, "Parsing an iteration statement",
544 parser->in_statement & IN_ITERATION_STMT);
545 cp_debug_print_flag (file, "Parsing a switch statement",
546 parser->in_statement & IN_SWITCH_STMT);
547 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
548 parser->in_statement & IN_OMP_BLOCK);
549 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
550 parser->in_statement & IN_CILK_SIMD_FOR);
551 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
552 parser->in_statement & IN_OMP_FOR);
553 cp_debug_print_flag (file, "Parsing an if statement",
554 parser->in_statement & IN_IF_STMT);
555 cp_debug_print_flag (file, "Parsing a type-id in an expression "
556 "context", parser->in_type_id_in_expr_p);
557 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
558 parser->implicit_extern_c);
559 cp_debug_print_flag (file, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
590 cp_debug_parser (stderr, &ref);
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
604 static cp_lexer *
605 cp_lexer_alloc (void)
607 cp_lexer *lexer;
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622 return lexer;
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
629 static cp_lexer *
630 cp_lexer_new_main (void)
632 cp_lexer *lexer;
633 cp_token token;
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
640 lexer = cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
691 /* Frees all resources associated with LEXER. */
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
701 /* Returns nonzero if debugging information should be output. */
703 static inline bool
704 cp_lexer_debugging_p (cp_lexer *lexer)
706 return lexer->debugging_p;
710 static inline cp_token_position
711 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
713 gcc_assert (!previous_p || lexer->next_token != &eof_token);
715 return lexer->next_token - previous_p;
718 static inline cp_token *
719 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
721 return pos;
724 static inline void
725 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
727 lexer->next_token = cp_lexer_token_at (lexer, pos);
730 static inline cp_token_position
731 cp_lexer_previous_token_position (cp_lexer *lexer)
733 if (lexer->next_token == &eof_token)
734 return lexer->last_token - 1;
735 else
736 return cp_lexer_token_position (lexer, true);
739 static inline cp_token *
740 cp_lexer_previous_token (cp_lexer *lexer)
742 cp_token_position tp = cp_lexer_previous_token_position (lexer);
744 return cp_lexer_token_at (lexer, tp);
747 /* nonzero if we are presently saving tokens. */
749 static inline int
750 cp_lexer_saving_tokens (const cp_lexer* lexer)
752 return lexer->saved_tokens.length () != 0;
755 /* Store the next token from the preprocessor in *TOKEN. Return true
756 if we reach EOF. If LEXER is NULL, assume we are handling an
757 initial #pragma pch_preprocess, and thus want the lexer to return
758 processed strings. */
760 static void
761 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
763 static int is_extern_c = 0;
765 /* Get a new token from the preprocessor. */
766 token->type
767 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
768 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
769 token->keyword = RID_MAX;
770 token->pragma_kind = PRAGMA_NONE;
771 token->purged_p = false;
772 token->error_reported = false;
774 /* On some systems, some header files are surrounded by an
775 implicit extern "C" block. Set a flag in the token if it
776 comes from such a header. */
777 is_extern_c += pending_lang_change;
778 pending_lang_change = 0;
779 token->implicit_extern_c = is_extern_c > 0;
781 /* Check to see if this token is a keyword. */
782 if (token->type == CPP_NAME)
784 if (C_IS_RESERVED_WORD (token->u.value))
786 /* Mark this token as a keyword. */
787 token->type = CPP_KEYWORD;
788 /* Record which keyword. */
789 token->keyword = C_RID_CODE (token->u.value);
791 else
793 if (warn_cxx11_compat
794 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
797 /* Warn about the C++0x keyword (but still treat it as
798 an identifier). */
799 warning (OPT_Wc__11_compat,
800 "identifier %qE is a keyword in C++11",
801 token->u.value);
803 /* Clear out the C_RID_CODE so we don't warn about this
804 particular identifier-turned-keyword again. */
805 C_SET_RID_CODE (token->u.value, RID_MAX);
808 token->keyword = RID_MAX;
811 else if (token->type == CPP_AT_NAME)
813 /* This only happens in Objective-C++; it must be a keyword. */
814 token->type = CPP_KEYWORD;
815 switch (C_RID_CODE (token->u.value))
817 /* Replace 'class' with '@class', 'private' with '@private',
818 etc. This prevents confusion with the C++ keyword
819 'class', and makes the tokens consistent with other
820 Objective-C 'AT' keywords. For example '@class' is
821 reported as RID_AT_CLASS which is consistent with
822 '@synchronized', which is reported as
823 RID_AT_SYNCHRONIZED.
825 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
826 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
827 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
828 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
829 case RID_THROW: token->keyword = RID_AT_THROW; break;
830 case RID_TRY: token->keyword = RID_AT_TRY; break;
831 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
832 default: token->keyword = C_RID_CODE (token->u.value);
835 else if (token->type == CPP_PRAGMA)
837 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
838 token->pragma_kind = ((enum pragma_kind)
839 TREE_INT_CST_LOW (token->u.value));
840 token->u.value = NULL_TREE;
844 /* Update the globals input_location and the input file stack from TOKEN. */
845 static inline void
846 cp_lexer_set_source_position_from_token (cp_token *token)
848 if (token->type != CPP_EOF)
850 input_location = token->location;
854 /* Update the globals input_location and the input file stack from LEXER. */
855 static inline void
856 cp_lexer_set_source_position (cp_lexer *lexer)
858 cp_token *token = cp_lexer_peek_token (lexer);
859 cp_lexer_set_source_position_from_token (token);
862 /* Return a pointer to the next token in the token stream, but do not
863 consume it. */
865 static inline cp_token *
866 cp_lexer_peek_token (cp_lexer *lexer)
868 if (cp_lexer_debugging_p (lexer))
870 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
871 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
872 putc ('\n', cp_lexer_debug_stream);
874 return lexer->next_token;
877 /* Return true if the next token has the indicated TYPE. */
879 static inline bool
880 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
882 return cp_lexer_peek_token (lexer)->type == type;
885 /* Return true if the next token does not have the indicated TYPE. */
887 static inline bool
888 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
890 return !cp_lexer_next_token_is (lexer, type);
893 /* Return true if the next token is the indicated KEYWORD. */
895 static inline bool
896 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
898 return cp_lexer_peek_token (lexer)->keyword == keyword;
901 static inline bool
902 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
904 return cp_lexer_peek_nth_token (lexer, n)->type == type;
907 static inline bool
908 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
910 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
913 /* Return true if the next token is not the indicated KEYWORD. */
915 static inline bool
916 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
918 return cp_lexer_peek_token (lexer)->keyword != keyword;
921 /* Return true if the next token is a keyword for a decl-specifier. */
923 static bool
924 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
926 cp_token *token;
928 token = cp_lexer_peek_token (lexer);
929 switch (token->keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 return true;
968 default:
969 if (token->keyword >= RID_FIRST_INT_N
970 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
977 /* Returns TRUE iff the token T begins a decltype type. */
979 static bool
980 token_is_decltype (cp_token *t)
982 return (t->keyword == RID_DECLTYPE
983 || t->type == CPP_DECLTYPE);
986 /* Returns TRUE iff the next token begins a decltype type. */
988 static bool
989 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
991 cp_token *t = cp_lexer_peek_token (lexer);
992 return token_is_decltype (t);
995 /* Return a pointer to the Nth token in the token stream. If N is 1,
996 then this is precisely equivalent to cp_lexer_peek_token (except
997 that it is not inline). One would like to disallow that case, but
998 there is one case (cp_parser_nth_token_starts_template_id) where
999 the caller passes a variable for N and it might be 1. */
1001 static cp_token *
1002 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1004 cp_token *token;
1006 /* N is 1-based, not zero-based. */
1007 gcc_assert (n > 0);
1009 if (cp_lexer_debugging_p (lexer))
1010 fprintf (cp_lexer_debug_stream,
1011 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1013 --n;
1014 token = lexer->next_token;
1015 gcc_assert (!n || token != &eof_token);
1016 while (n != 0)
1018 ++token;
1019 if (token == lexer->last_token)
1021 token = &eof_token;
1022 break;
1025 if (!token->purged_p)
1026 --n;
1029 if (cp_lexer_debugging_p (lexer))
1031 cp_lexer_print_token (cp_lexer_debug_stream, token);
1032 putc ('\n', cp_lexer_debug_stream);
1035 return token;
1038 /* Return the next token, and advance the lexer's next_token pointer
1039 to point to the next non-purged token. */
1041 static cp_token *
1042 cp_lexer_consume_token (cp_lexer* lexer)
1044 cp_token *token = lexer->next_token;
1046 gcc_assert (token != &eof_token);
1047 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1051 lexer->next_token++;
1052 if (lexer->next_token == lexer->last_token)
1054 lexer->next_token = &eof_token;
1055 break;
1059 while (lexer->next_token->purged_p);
1061 cp_lexer_set_source_position_from_token (token);
1063 /* Provide debugging output. */
1064 if (cp_lexer_debugging_p (lexer))
1066 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1067 cp_lexer_print_token (cp_lexer_debug_stream, token);
1068 putc ('\n', cp_lexer_debug_stream);
1071 return token;
1074 /* Permanently remove the next token from the token stream, and
1075 advance the next_token pointer to refer to the next non-purged
1076 token. */
1078 static void
1079 cp_lexer_purge_token (cp_lexer *lexer)
1081 cp_token *tok = lexer->next_token;
1083 gcc_assert (tok != &eof_token);
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1091 tok++;
1092 if (tok == lexer->last_token)
1094 tok = &eof_token;
1095 break;
1098 while (tok->purged_p);
1099 lexer->next_token = tok;
1102 /* Permanently remove all tokens after TOK, up to, but not
1103 including, the token that will be returned next by
1104 cp_lexer_peek_token. */
1106 static void
1107 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1109 cp_token *peek = lexer->next_token;
1111 if (peek == &eof_token)
1112 peek = lexer->last_token;
1114 gcc_assert (tok < peek);
1116 for ( tok += 1; tok != peek; tok += 1)
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 /* Begin saving tokens. All tokens consumed after this point will be
1126 preserved. */
1128 static void
1129 cp_lexer_save_tokens (cp_lexer* lexer)
1131 /* Provide debugging output. */
1132 if (cp_lexer_debugging_p (lexer))
1133 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1135 lexer->saved_tokens.safe_push (lexer->next_token);
1138 /* Commit to the portion of the token stream most recently saved. */
1140 static void
1141 cp_lexer_commit_tokens (cp_lexer* lexer)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1147 lexer->saved_tokens.pop ();
1150 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1151 to the token stream. Stop saving tokens. */
1153 static void
1154 cp_lexer_rollback_tokens (cp_lexer* lexer)
1156 /* Provide debugging output. */
1157 if (cp_lexer_debugging_p (lexer))
1158 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1160 lexer->next_token = lexer->saved_tokens.pop ();
1163 /* RAII wrapper around the above functions, with sanity checking. Creating
1164 a variable saves tokens, which are committed when the variable is
1165 destroyed unless they are explicitly rolled back by calling the rollback
1166 member function. */
1168 struct saved_token_sentinel
1170 cp_lexer *lexer;
1171 unsigned len;
1172 bool commit;
1173 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1175 len = lexer->saved_tokens.length ();
1176 cp_lexer_save_tokens (lexer);
1178 void rollback ()
1180 cp_lexer_rollback_tokens (lexer);
1181 commit = false;
1183 ~saved_token_sentinel()
1185 if (commit)
1186 cp_lexer_commit_tokens (lexer);
1187 gcc_assert (lexer->saved_tokens.length () == len);
1191 /* Print a representation of the TOKEN on the STREAM. */
1193 static void
1194 cp_lexer_print_token (FILE * stream, cp_token *token)
1196 /* We don't use cpp_type2name here because the parser defines
1197 a few tokens of its own. */
1198 static const char *const token_names[] = {
1199 /* cpplib-defined token types */
1200 #define OP(e, s) #e,
1201 #define TK(e, s) #e,
1202 TTYPE_TABLE
1203 #undef OP
1204 #undef TK
1205 /* C++ parser token types - see "Manifest constants", above. */
1206 "KEYWORD",
1207 "TEMPLATE_ID",
1208 "NESTED_NAME_SPECIFIER",
1211 /* For some tokens, print the associated data. */
1212 switch (token->type)
1214 case CPP_KEYWORD:
1215 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1216 For example, `struct' is mapped to an INTEGER_CST. */
1217 if (!identifier_p (token->u.value))
1218 break;
1219 /* else fall through */
1220 case CPP_NAME:
1221 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1222 break;
1224 case CPP_STRING:
1225 case CPP_STRING16:
1226 case CPP_STRING32:
1227 case CPP_WSTRING:
1228 case CPP_UTF8STRING:
1229 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1230 break;
1232 case CPP_NUMBER:
1233 print_generic_expr (stream, token->u.value, 0);
1234 break;
1236 default:
1237 /* If we have a name for the token, print it out. Otherwise, we
1238 simply give the numeric code. */
1239 if (token->type < ARRAY_SIZE(token_names))
1240 fputs (token_names[token->type], stream);
1241 else
1242 fprintf (stream, "[%d]", token->type);
1243 break;
1247 DEBUG_FUNCTION void
1248 debug (cp_token &ref)
1250 cp_lexer_print_token (stderr, &ref);
1251 fprintf (stderr, "\n");
1254 DEBUG_FUNCTION void
1255 debug (cp_token *ptr)
1257 if (ptr)
1258 debug (*ptr);
1259 else
1260 fprintf (stderr, "<nil>\n");
1264 /* Start emitting debugging information. */
1266 static void
1267 cp_lexer_start_debugging (cp_lexer* lexer)
1269 lexer->debugging_p = true;
1270 cp_lexer_debug_stream = stderr;
1273 /* Stop emitting debugging information. */
1275 static void
1276 cp_lexer_stop_debugging (cp_lexer* lexer)
1278 lexer->debugging_p = false;
1279 cp_lexer_debug_stream = NULL;
1282 /* Create a new cp_token_cache, representing a range of tokens. */
1284 static cp_token_cache *
1285 cp_token_cache_new (cp_token *first, cp_token *last)
1287 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1288 cache->first = first;
1289 cache->last = last;
1290 return cache;
1293 /* Diagnose if #pragma omp declare simd isn't followed immediately
1294 by function declaration or definition. */
1296 static inline void
1297 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1299 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1301 error ("%<#pragma omp declare simd%> not immediately followed by "
1302 "function declaration or definition");
1303 parser->omp_declare_simd = NULL;
1307 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1308 and put that into "omp declare simd" attribute. */
1310 static inline void
1311 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1313 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1315 if (fndecl == error_mark_node)
1317 parser->omp_declare_simd = NULL;
1318 return;
1320 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1322 cp_ensure_no_omp_declare_simd (parser);
1323 return;
1328 /* Decl-specifiers. */
1330 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1332 static void
1333 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1335 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1338 /* Declarators. */
1340 /* Nothing other than the parser should be creating declarators;
1341 declarators are a semi-syntactic representation of C++ entities.
1342 Other parts of the front end that need to create entities (like
1343 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1345 static cp_declarator *make_call_declarator
1346 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1347 static cp_declarator *make_array_declarator
1348 (cp_declarator *, tree);
1349 static cp_declarator *make_pointer_declarator
1350 (cp_cv_quals, cp_declarator *, tree);
1351 static cp_declarator *make_reference_declarator
1352 (cp_cv_quals, cp_declarator *, bool, tree);
1353 static cp_parameter_declarator *make_parameter_declarator
1354 (cp_decl_specifier_seq *, cp_declarator *, tree);
1355 static cp_declarator *make_ptrmem_declarator
1356 (cp_cv_quals, tree, cp_declarator *, tree);
1358 /* An erroneous declarator. */
1359 static cp_declarator *cp_error_declarator;
1361 /* The obstack on which declarators and related data structures are
1362 allocated. */
1363 static struct obstack declarator_obstack;
1365 /* Alloc BYTES from the declarator memory pool. */
1367 static inline void *
1368 alloc_declarator (size_t bytes)
1370 return obstack_alloc (&declarator_obstack, bytes);
1373 /* Allocate a declarator of the indicated KIND. Clear fields that are
1374 common to all declarators. */
1376 static cp_declarator *
1377 make_declarator (cp_declarator_kind kind)
1379 cp_declarator *declarator;
1381 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1382 declarator->kind = kind;
1383 declarator->attributes = NULL_TREE;
1384 declarator->std_attributes = NULL_TREE;
1385 declarator->declarator = NULL;
1386 declarator->parameter_pack_p = false;
1387 declarator->id_loc = UNKNOWN_LOCATION;
1389 return declarator;
1392 /* Make a declarator for a generalized identifier. If
1393 QUALIFYING_SCOPE is non-NULL, the identifier is
1394 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1395 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1396 is, if any. */
1398 static cp_declarator *
1399 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1400 special_function_kind sfk)
1402 cp_declarator *declarator;
1404 /* It is valid to write:
1406 class C { void f(); };
1407 typedef C D;
1408 void D::f();
1410 The standard is not clear about whether `typedef const C D' is
1411 legal; as of 2002-09-15 the committee is considering that
1412 question. EDG 3.0 allows that syntax. Therefore, we do as
1413 well. */
1414 if (qualifying_scope && TYPE_P (qualifying_scope))
1415 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1417 gcc_assert (identifier_p (unqualified_name)
1418 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1419 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1421 declarator = make_declarator (cdk_id);
1422 declarator->u.id.qualifying_scope = qualifying_scope;
1423 declarator->u.id.unqualified_name = unqualified_name;
1424 declarator->u.id.sfk = sfk;
1426 return declarator;
1429 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1430 of modifiers such as const or volatile to apply to the pointer
1431 type, represented as identifiers. ATTRIBUTES represent the attributes that
1432 appertain to the pointer or reference. */
1434 cp_declarator *
1435 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1436 tree attributes)
1438 cp_declarator *declarator;
1440 declarator = make_declarator (cdk_pointer);
1441 declarator->declarator = target;
1442 declarator->u.pointer.qualifiers = cv_qualifiers;
1443 declarator->u.pointer.class_type = NULL_TREE;
1444 if (target)
1446 declarator->id_loc = target->id_loc;
1447 declarator->parameter_pack_p = target->parameter_pack_p;
1448 target->parameter_pack_p = false;
1450 else
1451 declarator->parameter_pack_p = false;
1453 declarator->std_attributes = attributes;
1455 return declarator;
1458 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1459 represent the attributes that appertain to the pointer or
1460 reference. */
1462 cp_declarator *
1463 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1464 bool rvalue_ref, tree attributes)
1466 cp_declarator *declarator;
1468 declarator = make_declarator (cdk_reference);
1469 declarator->declarator = target;
1470 declarator->u.reference.qualifiers = cv_qualifiers;
1471 declarator->u.reference.rvalue_ref = rvalue_ref;
1472 if (target)
1474 declarator->id_loc = target->id_loc;
1475 declarator->parameter_pack_p = target->parameter_pack_p;
1476 target->parameter_pack_p = false;
1478 else
1479 declarator->parameter_pack_p = false;
1481 declarator->std_attributes = attributes;
1483 return declarator;
1486 /* Like make_pointer_declarator -- but for a pointer to a non-static
1487 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1490 cp_declarator *
1491 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1492 cp_declarator *pointee,
1493 tree attributes)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_ptrmem);
1498 declarator->declarator = pointee;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = class_type;
1502 if (pointee)
1504 declarator->parameter_pack_p = pointee->parameter_pack_p;
1505 pointee->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 declarator->std_attributes = attributes;
1512 return declarator;
1515 /* Make a declarator for the function given by TARGET, with the
1516 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1517 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1518 indicates what exceptions can be thrown. */
1520 cp_declarator *
1521 make_call_declarator (cp_declarator *target,
1522 tree parms,
1523 cp_cv_quals cv_qualifiers,
1524 cp_virt_specifiers virt_specifiers,
1525 cp_ref_qualifier ref_qualifier,
1526 tree exception_specification,
1527 tree late_return_type)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_function);
1532 declarator->declarator = target;
1533 declarator->u.function.parameters = parms;
1534 declarator->u.function.qualifiers = cv_qualifiers;
1535 declarator->u.function.virt_specifiers = virt_specifiers;
1536 declarator->u.function.ref_qualifier = ref_qualifier;
1537 declarator->u.function.exception_specification = exception_specification;
1538 declarator->u.function.late_return_type = late_return_type;
1539 if (target)
1541 declarator->id_loc = target->id_loc;
1542 declarator->parameter_pack_p = target->parameter_pack_p;
1543 target->parameter_pack_p = false;
1545 else
1546 declarator->parameter_pack_p = false;
1548 return declarator;
1551 /* Make a declarator for an array of BOUNDS elements, each of which is
1552 defined by ELEMENT. */
1554 cp_declarator *
1555 make_array_declarator (cp_declarator *element, tree bounds)
1557 cp_declarator *declarator;
1559 declarator = make_declarator (cdk_array);
1560 declarator->declarator = element;
1561 declarator->u.array.bounds = bounds;
1562 if (element)
1564 declarator->id_loc = element->id_loc;
1565 declarator->parameter_pack_p = element->parameter_pack_p;
1566 element->parameter_pack_p = false;
1568 else
1569 declarator->parameter_pack_p = false;
1571 return declarator;
1574 /* Determine whether the declarator we've seen so far can be a
1575 parameter pack, when followed by an ellipsis. */
1576 static bool
1577 declarator_can_be_parameter_pack (cp_declarator *declarator)
1579 /* Search for a declarator name, or any other declarator that goes
1580 after the point where the ellipsis could appear in a parameter
1581 pack. If we find any of these, then this declarator can not be
1582 made into a parameter pack. */
1583 bool found = false;
1584 while (declarator && !found)
1586 switch ((int)declarator->kind)
1588 case cdk_id:
1589 case cdk_array:
1590 found = true;
1591 break;
1593 case cdk_error:
1594 return true;
1596 default:
1597 declarator = declarator->declarator;
1598 break;
1602 return !found;
1605 cp_parameter_declarator *no_parameters;
1607 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1608 DECLARATOR and DEFAULT_ARGUMENT. */
1610 cp_parameter_declarator *
1611 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1612 cp_declarator *declarator,
1613 tree default_argument)
1615 cp_parameter_declarator *parameter;
1617 parameter = ((cp_parameter_declarator *)
1618 alloc_declarator (sizeof (cp_parameter_declarator)));
1619 parameter->next = NULL;
1620 if (decl_specifiers)
1621 parameter->decl_specifiers = *decl_specifiers;
1622 else
1623 clear_decl_specs (&parameter->decl_specifiers);
1624 parameter->declarator = declarator;
1625 parameter->default_argument = default_argument;
1626 parameter->ellipsis_p = false;
1628 return parameter;
1631 /* Returns true iff DECLARATOR is a declaration for a function. */
1633 static bool
1634 function_declarator_p (const cp_declarator *declarator)
1636 while (declarator)
1638 if (declarator->kind == cdk_function
1639 && declarator->declarator->kind == cdk_id)
1640 return true;
1641 if (declarator->kind == cdk_id
1642 || declarator->kind == cdk_error)
1643 return false;
1644 declarator = declarator->declarator;
1646 return false;
1649 /* The parser. */
1651 /* Overview
1652 --------
1654 A cp_parser parses the token stream as specified by the C++
1655 grammar. Its job is purely parsing, not semantic analysis. For
1656 example, the parser breaks the token stream into declarators,
1657 expressions, statements, and other similar syntactic constructs.
1658 It does not check that the types of the expressions on either side
1659 of an assignment-statement are compatible, or that a function is
1660 not declared with a parameter of type `void'.
1662 The parser invokes routines elsewhere in the compiler to perform
1663 semantic analysis and to build up the abstract syntax tree for the
1664 code processed.
1666 The parser (and the template instantiation code, which is, in a
1667 way, a close relative of parsing) are the only parts of the
1668 compiler that should be calling push_scope and pop_scope, or
1669 related functions. The parser (and template instantiation code)
1670 keeps track of what scope is presently active; everything else
1671 should simply honor that. (The code that generates static
1672 initializers may also need to set the scope, in order to check
1673 access control correctly when emitting the initializers.)
1675 Methodology
1676 -----------
1678 The parser is of the standard recursive-descent variety. Upcoming
1679 tokens in the token stream are examined in order to determine which
1680 production to use when parsing a non-terminal. Some C++ constructs
1681 require arbitrary look ahead to disambiguate. For example, it is
1682 impossible, in the general case, to tell whether a statement is an
1683 expression or declaration without scanning the entire statement.
1684 Therefore, the parser is capable of "parsing tentatively." When the
1685 parser is not sure what construct comes next, it enters this mode.
1686 Then, while we attempt to parse the construct, the parser queues up
1687 error messages, rather than issuing them immediately, and saves the
1688 tokens it consumes. If the construct is parsed successfully, the
1689 parser "commits", i.e., it issues any queued error messages and
1690 the tokens that were being preserved are permanently discarded.
1691 If, however, the construct is not parsed successfully, the parser
1692 rolls back its state completely so that it can resume parsing using
1693 a different alternative.
1695 Future Improvements
1696 -------------------
1698 The performance of the parser could probably be improved substantially.
1699 We could often eliminate the need to parse tentatively by looking ahead
1700 a little bit. In some places, this approach might not entirely eliminate
1701 the need to parse tentatively, but it might still speed up the average
1702 case. */
1704 /* Flags that are passed to some parsing functions. These values can
1705 be bitwise-ored together. */
1707 enum
1709 /* No flags. */
1710 CP_PARSER_FLAGS_NONE = 0x0,
1711 /* The construct is optional. If it is not present, then no error
1712 should be issued. */
1713 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1714 /* When parsing a type-specifier, treat user-defined type-names
1715 as non-type identifiers. */
1716 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1717 /* When parsing a type-specifier, do not try to parse a class-specifier
1718 or enum-specifier. */
1719 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1720 /* When parsing a decl-specifier-seq, only allow type-specifier or
1721 constexpr. */
1722 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1725 /* This type is used for parameters and variables which hold
1726 combinations of the above flags. */
1727 typedef int cp_parser_flags;
1729 /* The different kinds of declarators we want to parse. */
1731 typedef enum cp_parser_declarator_kind
1733 /* We want an abstract declarator. */
1734 CP_PARSER_DECLARATOR_ABSTRACT,
1735 /* We want a named declarator. */
1736 CP_PARSER_DECLARATOR_NAMED,
1737 /* We don't mind, but the name must be an unqualified-id. */
1738 CP_PARSER_DECLARATOR_EITHER
1739 } cp_parser_declarator_kind;
1741 /* The precedence values used to parse binary expressions. The minimum value
1742 of PREC must be 1, because zero is reserved to quickly discriminate
1743 binary operators from other tokens. */
1745 enum cp_parser_prec
1747 PREC_NOT_OPERATOR,
1748 PREC_LOGICAL_OR_EXPRESSION,
1749 PREC_LOGICAL_AND_EXPRESSION,
1750 PREC_INCLUSIVE_OR_EXPRESSION,
1751 PREC_EXCLUSIVE_OR_EXPRESSION,
1752 PREC_AND_EXPRESSION,
1753 PREC_EQUALITY_EXPRESSION,
1754 PREC_RELATIONAL_EXPRESSION,
1755 PREC_SHIFT_EXPRESSION,
1756 PREC_ADDITIVE_EXPRESSION,
1757 PREC_MULTIPLICATIVE_EXPRESSION,
1758 PREC_PM_EXPRESSION,
1759 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1762 /* A mapping from a token type to a corresponding tree node type, with a
1763 precedence value. */
1765 typedef struct cp_parser_binary_operations_map_node
1767 /* The token type. */
1768 enum cpp_ttype token_type;
1769 /* The corresponding tree code. */
1770 enum tree_code tree_type;
1771 /* The precedence of this operator. */
1772 enum cp_parser_prec prec;
1773 } cp_parser_binary_operations_map_node;
1775 typedef struct cp_parser_expression_stack_entry
1777 /* Left hand side of the binary operation we are currently
1778 parsing. */
1779 tree lhs;
1780 /* Original tree code for left hand side, if it was a binary
1781 expression itself (used for -Wparentheses). */
1782 enum tree_code lhs_type;
1783 /* Tree code for the binary operation we are parsing. */
1784 enum tree_code tree_type;
1785 /* Precedence of the binary operation we are parsing. */
1786 enum cp_parser_prec prec;
1787 /* Location of the binary operation we are parsing. */
1788 location_t loc;
1789 } cp_parser_expression_stack_entry;
1791 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1792 entries because precedence levels on the stack are monotonically
1793 increasing. */
1794 typedef struct cp_parser_expression_stack_entry
1795 cp_parser_expression_stack[NUM_PREC_VALUES];
1797 /* Prototypes. */
1799 /* Constructors and destructors. */
1801 static cp_parser_context *cp_parser_context_new
1802 (cp_parser_context *);
1804 /* Class variables. */
1806 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1808 /* The operator-precedence table used by cp_parser_binary_expression.
1809 Transformed into an associative array (binops_by_token) by
1810 cp_parser_new. */
1812 static const cp_parser_binary_operations_map_node binops[] = {
1813 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1814 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1816 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1817 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1818 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1820 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1821 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1823 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1824 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1826 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1827 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1828 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1829 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1831 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1832 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1834 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1836 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1838 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1840 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1842 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1845 /* The same as binops, but initialized by cp_parser_new so that
1846 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1847 for speed. */
1848 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1850 /* Constructors and destructors. */
1852 /* Construct a new context. The context below this one on the stack
1853 is given by NEXT. */
1855 static cp_parser_context *
1856 cp_parser_context_new (cp_parser_context* next)
1858 cp_parser_context *context;
1860 /* Allocate the storage. */
1861 if (cp_parser_context_free_list != NULL)
1863 /* Pull the first entry from the free list. */
1864 context = cp_parser_context_free_list;
1865 cp_parser_context_free_list = context->next;
1866 memset (context, 0, sizeof (*context));
1868 else
1869 context = ggc_cleared_alloc<cp_parser_context> ();
1871 /* No errors have occurred yet in this context. */
1872 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1873 /* If this is not the bottommost context, copy information that we
1874 need from the previous context. */
1875 if (next)
1877 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1878 expression, then we are parsing one in this context, too. */
1879 context->object_type = next->object_type;
1880 /* Thread the stack. */
1881 context->next = next;
1884 return context;
1887 /* Managing the unparsed function queues. */
1889 #define unparsed_funs_with_default_args \
1890 parser->unparsed_queues->last ().funs_with_default_args
1891 #define unparsed_funs_with_definitions \
1892 parser->unparsed_queues->last ().funs_with_definitions
1893 #define unparsed_nsdmis \
1894 parser->unparsed_queues->last ().nsdmis
1895 #define unparsed_classes \
1896 parser->unparsed_queues->last ().classes
1898 static void
1899 push_unparsed_function_queues (cp_parser *parser)
1901 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1902 vec_safe_push (parser->unparsed_queues, e);
1905 static void
1906 pop_unparsed_function_queues (cp_parser *parser)
1908 release_tree_vector (unparsed_funs_with_definitions);
1909 parser->unparsed_queues->pop ();
1912 /* Prototypes. */
1914 /* Constructors and destructors. */
1916 static cp_parser *cp_parser_new
1917 (void);
1919 /* Routines to parse various constructs.
1921 Those that return `tree' will return the error_mark_node (rather
1922 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1923 Sometimes, they will return an ordinary node if error-recovery was
1924 attempted, even though a parse error occurred. So, to check
1925 whether or not a parse error occurred, you should always use
1926 cp_parser_error_occurred. If the construct is optional (indicated
1927 either by an `_opt' in the name of the function that does the
1928 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1929 the construct is not present. */
1931 /* Lexical conventions [gram.lex] */
1933 static tree cp_parser_identifier
1934 (cp_parser *);
1935 static tree cp_parser_string_literal
1936 (cp_parser *, bool, bool, bool);
1937 static tree cp_parser_userdef_char_literal
1938 (cp_parser *);
1939 static tree cp_parser_userdef_string_literal
1940 (tree);
1941 static tree cp_parser_userdef_numeric_literal
1942 (cp_parser *);
1944 /* Basic concepts [gram.basic] */
1946 static bool cp_parser_translation_unit
1947 (cp_parser *);
1949 /* Expressions [gram.expr] */
1951 static tree cp_parser_primary_expression
1952 (cp_parser *, bool, bool, bool, cp_id_kind *);
1953 static tree cp_parser_id_expression
1954 (cp_parser *, bool, bool, bool *, bool, bool);
1955 static tree cp_parser_unqualified_id
1956 (cp_parser *, bool, bool, bool, bool);
1957 static tree cp_parser_nested_name_specifier_opt
1958 (cp_parser *, bool, bool, bool, bool);
1959 static tree cp_parser_nested_name_specifier
1960 (cp_parser *, bool, bool, bool, bool);
1961 static tree cp_parser_qualifying_entity
1962 (cp_parser *, bool, bool, bool, bool, bool);
1963 static tree cp_parser_postfix_expression
1964 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_postfix_open_square_expression
1966 (cp_parser *, tree, bool, bool);
1967 static tree cp_parser_postfix_dot_deref_expression
1968 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1969 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1970 (cp_parser *, int, bool, bool, bool *, bool = false);
1971 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1972 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1973 static void cp_parser_pseudo_destructor_name
1974 (cp_parser *, tree, tree *, tree *);
1975 static tree cp_parser_unary_expression
1976 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1977 static enum tree_code cp_parser_unary_operator
1978 (cp_token *);
1979 static tree cp_parser_new_expression
1980 (cp_parser *);
1981 static vec<tree, va_gc> *cp_parser_new_placement
1982 (cp_parser *);
1983 static tree cp_parser_new_type_id
1984 (cp_parser *, tree *);
1985 static cp_declarator *cp_parser_new_declarator_opt
1986 (cp_parser *);
1987 static cp_declarator *cp_parser_direct_new_declarator
1988 (cp_parser *);
1989 static vec<tree, va_gc> *cp_parser_new_initializer
1990 (cp_parser *);
1991 static tree cp_parser_delete_expression
1992 (cp_parser *);
1993 static tree cp_parser_cast_expression
1994 (cp_parser *, bool, bool, bool, cp_id_kind *);
1995 static tree cp_parser_binary_expression
1996 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1997 static tree cp_parser_question_colon_clause
1998 (cp_parser *, tree);
1999 static tree cp_parser_assignment_expression
2000 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2001 static enum tree_code cp_parser_assignment_operator_opt
2002 (cp_parser *);
2003 static tree cp_parser_expression
2004 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2005 static tree cp_parser_constant_expression
2006 (cp_parser *, bool = false, bool * = NULL);
2007 static tree cp_parser_builtin_offsetof
2008 (cp_parser *);
2009 static tree cp_parser_lambda_expression
2010 (cp_parser *);
2011 static void cp_parser_lambda_introducer
2012 (cp_parser *, tree);
2013 static bool cp_parser_lambda_declarator_opt
2014 (cp_parser *, tree);
2015 static void cp_parser_lambda_body
2016 (cp_parser *, tree);
2018 /* Statements [gram.stmt.stmt] */
2020 static void cp_parser_statement
2021 (cp_parser *, tree, bool, bool *);
2022 static void cp_parser_label_for_labeled_statement
2023 (cp_parser *, tree);
2024 static tree cp_parser_expression_statement
2025 (cp_parser *, tree);
2026 static tree cp_parser_compound_statement
2027 (cp_parser *, tree, bool, bool);
2028 static void cp_parser_statement_seq_opt
2029 (cp_parser *, tree);
2030 static tree cp_parser_selection_statement
2031 (cp_parser *, bool *);
2032 static tree cp_parser_condition
2033 (cp_parser *);
2034 static tree cp_parser_iteration_statement
2035 (cp_parser *, bool);
2036 static bool cp_parser_for_init_statement
2037 (cp_parser *, tree *decl);
2038 static tree cp_parser_for
2039 (cp_parser *, bool);
2040 static tree cp_parser_c_for
2041 (cp_parser *, tree, tree, bool);
2042 static tree cp_parser_range_for
2043 (cp_parser *, tree, tree, tree, bool);
2044 static void do_range_for_auto_deduction
2045 (tree, tree);
2046 static tree cp_parser_perform_range_for_lookup
2047 (tree, tree *, tree *);
2048 static tree cp_parser_range_for_member_function
2049 (tree, tree);
2050 static tree cp_parser_jump_statement
2051 (cp_parser *);
2052 static void cp_parser_declaration_statement
2053 (cp_parser *);
2055 static tree cp_parser_implicitly_scoped_statement
2056 (cp_parser *, bool *, location_t, const char *);
2057 static void cp_parser_already_scoped_statement
2058 (cp_parser *, location_t, const char *);
2060 /* Declarations [gram.dcl.dcl] */
2062 static void cp_parser_declaration_seq_opt
2063 (cp_parser *);
2064 static void cp_parser_declaration
2065 (cp_parser *);
2066 static void cp_parser_block_declaration
2067 (cp_parser *, bool);
2068 static void cp_parser_simple_declaration
2069 (cp_parser *, bool, tree *);
2070 static void cp_parser_decl_specifier_seq
2071 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2072 static tree cp_parser_storage_class_specifier_opt
2073 (cp_parser *);
2074 static tree cp_parser_function_specifier_opt
2075 (cp_parser *, cp_decl_specifier_seq *);
2076 static tree cp_parser_type_specifier
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2078 int *, bool *);
2079 static tree cp_parser_simple_type_specifier
2080 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2081 static tree cp_parser_type_name
2082 (cp_parser *);
2083 static tree cp_parser_nonclass_name
2084 (cp_parser* parser);
2085 static tree cp_parser_elaborated_type_specifier
2086 (cp_parser *, bool, bool);
2087 static tree cp_parser_enum_specifier
2088 (cp_parser *);
2089 static void cp_parser_enumerator_list
2090 (cp_parser *, tree);
2091 static void cp_parser_enumerator_definition
2092 (cp_parser *, tree);
2093 static tree cp_parser_namespace_name
2094 (cp_parser *);
2095 static void cp_parser_namespace_definition
2096 (cp_parser *);
2097 static void cp_parser_namespace_body
2098 (cp_parser *);
2099 static tree cp_parser_qualified_namespace_specifier
2100 (cp_parser *);
2101 static void cp_parser_namespace_alias_definition
2102 (cp_parser *);
2103 static bool cp_parser_using_declaration
2104 (cp_parser *, bool);
2105 static void cp_parser_using_directive
2106 (cp_parser *);
2107 static tree cp_parser_alias_declaration
2108 (cp_parser *);
2109 static void cp_parser_asm_definition
2110 (cp_parser *);
2111 static void cp_parser_linkage_specification
2112 (cp_parser *);
2113 static void cp_parser_static_assert
2114 (cp_parser *, bool);
2115 static tree cp_parser_decltype
2116 (cp_parser *);
2118 /* Declarators [gram.dcl.decl] */
2120 static tree cp_parser_init_declarator
2121 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2122 bool, bool, int, bool *, tree *, location_t *);
2123 static cp_declarator *cp_parser_declarator
2124 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2125 static cp_declarator *cp_parser_direct_declarator
2126 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2127 static enum tree_code cp_parser_ptr_operator
2128 (cp_parser *, tree *, cp_cv_quals *, tree *);
2129 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2130 (cp_parser *);
2131 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2132 (cp_parser *);
2133 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2134 (cp_parser *);
2135 static tree cp_parser_late_return_type_opt
2136 (cp_parser *, cp_declarator *, cp_cv_quals);
2137 static tree cp_parser_declarator_id
2138 (cp_parser *, bool);
2139 static tree cp_parser_type_id
2140 (cp_parser *);
2141 static tree cp_parser_template_type_arg
2142 (cp_parser *);
2143 static tree cp_parser_trailing_type_id (cp_parser *);
2144 static tree cp_parser_type_id_1
2145 (cp_parser *, bool, bool);
2146 static void cp_parser_type_specifier_seq
2147 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2148 static tree cp_parser_parameter_declaration_clause
2149 (cp_parser *);
2150 static tree cp_parser_parameter_declaration_list
2151 (cp_parser *, bool *);
2152 static cp_parameter_declarator *cp_parser_parameter_declaration
2153 (cp_parser *, bool, bool *);
2154 static tree cp_parser_default_argument
2155 (cp_parser *, bool);
2156 static void cp_parser_function_body
2157 (cp_parser *, bool);
2158 static tree cp_parser_initializer
2159 (cp_parser *, bool *, bool *);
2160 static tree cp_parser_initializer_clause
2161 (cp_parser *, bool *);
2162 static tree cp_parser_braced_list
2163 (cp_parser*, bool*);
2164 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2165 (cp_parser *, bool *);
2167 static bool cp_parser_ctor_initializer_opt_and_function_body
2168 (cp_parser *, bool);
2170 static tree cp_parser_late_parsing_omp_declare_simd
2171 (cp_parser *, tree);
2173 static tree cp_parser_late_parsing_cilk_simd_fn_info
2174 (cp_parser *, tree);
2176 static tree synthesize_implicit_template_parm
2177 (cp_parser *);
2178 static tree finish_fully_implicit_template
2179 (cp_parser *, tree);
2181 /* Classes [gram.class] */
2183 static tree cp_parser_class_name
2184 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2185 static tree cp_parser_class_specifier
2186 (cp_parser *);
2187 static tree cp_parser_class_head
2188 (cp_parser *, bool *);
2189 static enum tag_types cp_parser_class_key
2190 (cp_parser *);
2191 static void cp_parser_type_parameter_key
2192 (cp_parser* parser);
2193 static void cp_parser_member_specification_opt
2194 (cp_parser *);
2195 static void cp_parser_member_declaration
2196 (cp_parser *);
2197 static tree cp_parser_pure_specifier
2198 (cp_parser *);
2199 static tree cp_parser_constant_initializer
2200 (cp_parser *);
2202 /* Derived classes [gram.class.derived] */
2204 static tree cp_parser_base_clause
2205 (cp_parser *);
2206 static tree cp_parser_base_specifier
2207 (cp_parser *);
2209 /* Special member functions [gram.special] */
2211 static tree cp_parser_conversion_function_id
2212 (cp_parser *);
2213 static tree cp_parser_conversion_type_id
2214 (cp_parser *);
2215 static cp_declarator *cp_parser_conversion_declarator_opt
2216 (cp_parser *);
2217 static bool cp_parser_ctor_initializer_opt
2218 (cp_parser *);
2219 static void cp_parser_mem_initializer_list
2220 (cp_parser *);
2221 static tree cp_parser_mem_initializer
2222 (cp_parser *);
2223 static tree cp_parser_mem_initializer_id
2224 (cp_parser *);
2226 /* Overloading [gram.over] */
2228 static tree cp_parser_operator_function_id
2229 (cp_parser *);
2230 static tree cp_parser_operator
2231 (cp_parser *);
2233 /* Templates [gram.temp] */
2235 static void cp_parser_template_declaration
2236 (cp_parser *, bool);
2237 static tree cp_parser_template_parameter_list
2238 (cp_parser *);
2239 static tree cp_parser_template_parameter
2240 (cp_parser *, bool *, bool *);
2241 static tree cp_parser_type_parameter
2242 (cp_parser *, bool *);
2243 static tree cp_parser_template_id
2244 (cp_parser *, bool, bool, enum tag_types, bool);
2245 static tree cp_parser_template_name
2246 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2247 static tree cp_parser_template_argument_list
2248 (cp_parser *);
2249 static tree cp_parser_template_argument
2250 (cp_parser *);
2251 static void cp_parser_explicit_instantiation
2252 (cp_parser *);
2253 static void cp_parser_explicit_specialization
2254 (cp_parser *);
2256 /* Exception handling [gram.exception] */
2258 static tree cp_parser_try_block
2259 (cp_parser *);
2260 static bool cp_parser_function_try_block
2261 (cp_parser *);
2262 static void cp_parser_handler_seq
2263 (cp_parser *);
2264 static void cp_parser_handler
2265 (cp_parser *);
2266 static tree cp_parser_exception_declaration
2267 (cp_parser *);
2268 static tree cp_parser_throw_expression
2269 (cp_parser *);
2270 static tree cp_parser_exception_specification_opt
2271 (cp_parser *);
2272 static tree cp_parser_type_id_list
2273 (cp_parser *);
2275 /* GNU Extensions */
2277 static tree cp_parser_asm_specification_opt
2278 (cp_parser *);
2279 static tree cp_parser_asm_operand_list
2280 (cp_parser *);
2281 static tree cp_parser_asm_clobber_list
2282 (cp_parser *);
2283 static tree cp_parser_asm_label_list
2284 (cp_parser *);
2285 static bool cp_next_tokens_can_be_attribute_p
2286 (cp_parser *);
2287 static bool cp_next_tokens_can_be_gnu_attribute_p
2288 (cp_parser *);
2289 static bool cp_next_tokens_can_be_std_attribute_p
2290 (cp_parser *);
2291 static bool cp_nth_tokens_can_be_std_attribute_p
2292 (cp_parser *, size_t);
2293 static bool cp_nth_tokens_can_be_gnu_attribute_p
2294 (cp_parser *, size_t);
2295 static bool cp_nth_tokens_can_be_attribute_p
2296 (cp_parser *, size_t);
2297 static tree cp_parser_attributes_opt
2298 (cp_parser *);
2299 static tree cp_parser_gnu_attributes_opt
2300 (cp_parser *);
2301 static tree cp_parser_gnu_attribute_list
2302 (cp_parser *);
2303 static tree cp_parser_std_attribute
2304 (cp_parser *);
2305 static tree cp_parser_std_attribute_spec
2306 (cp_parser *);
2307 static tree cp_parser_std_attribute_spec_seq
2308 (cp_parser *);
2309 static bool cp_parser_extension_opt
2310 (cp_parser *, int *);
2311 static void cp_parser_label_declaration
2312 (cp_parser *);
2314 /* Transactional Memory Extensions */
2316 static tree cp_parser_transaction
2317 (cp_parser *, enum rid);
2318 static tree cp_parser_transaction_expression
2319 (cp_parser *, enum rid);
2320 static bool cp_parser_function_transaction
2321 (cp_parser *, enum rid);
2322 static tree cp_parser_transaction_cancel
2323 (cp_parser *);
2325 enum pragma_context {
2326 pragma_external,
2327 pragma_member,
2328 pragma_objc_icode,
2329 pragma_stmt,
2330 pragma_compound
2332 static bool cp_parser_pragma
2333 (cp_parser *, enum pragma_context);
2335 /* Objective-C++ Productions */
2337 static tree cp_parser_objc_message_receiver
2338 (cp_parser *);
2339 static tree cp_parser_objc_message_args
2340 (cp_parser *);
2341 static tree cp_parser_objc_message_expression
2342 (cp_parser *);
2343 static tree cp_parser_objc_encode_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_defs_expression
2346 (cp_parser *);
2347 static tree cp_parser_objc_protocol_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_selector_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_expression
2352 (cp_parser *);
2353 static bool cp_parser_objc_selector_p
2354 (enum cpp_ttype);
2355 static tree cp_parser_objc_selector
2356 (cp_parser *);
2357 static tree cp_parser_objc_protocol_refs_opt
2358 (cp_parser *);
2359 static void cp_parser_objc_declaration
2360 (cp_parser *, tree);
2361 static tree cp_parser_objc_statement
2362 (cp_parser *);
2363 static bool cp_parser_objc_valid_prefix_attributes
2364 (cp_parser *, tree *);
2365 static void cp_parser_objc_at_property_declaration
2366 (cp_parser *) ;
2367 static void cp_parser_objc_at_synthesize_declaration
2368 (cp_parser *) ;
2369 static void cp_parser_objc_at_dynamic_declaration
2370 (cp_parser *) ;
2371 static tree cp_parser_objc_struct_declaration
2372 (cp_parser *) ;
2374 /* Utility Routines */
2376 static tree cp_parser_lookup_name
2377 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2378 static tree cp_parser_lookup_name_simple
2379 (cp_parser *, tree, location_t);
2380 static tree cp_parser_maybe_treat_template_as_class
2381 (tree, bool);
2382 static bool cp_parser_check_declarator_template_parameters
2383 (cp_parser *, cp_declarator *, location_t);
2384 static bool cp_parser_check_template_parameters
2385 (cp_parser *, unsigned, location_t, cp_declarator *);
2386 static tree cp_parser_simple_cast_expression
2387 (cp_parser *);
2388 static tree cp_parser_global_scope_opt
2389 (cp_parser *, bool);
2390 static bool cp_parser_constructor_declarator_p
2391 (cp_parser *, bool);
2392 static tree cp_parser_function_definition_from_specifiers_and_declarator
2393 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2394 static tree cp_parser_function_definition_after_declarator
2395 (cp_parser *, bool);
2396 static void cp_parser_template_declaration_after_export
2397 (cp_parser *, bool);
2398 static void cp_parser_perform_template_parameter_access_checks
2399 (vec<deferred_access_check, va_gc> *);
2400 static tree cp_parser_single_declaration
2401 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2402 static tree cp_parser_functional_cast
2403 (cp_parser *, tree);
2404 static tree cp_parser_save_member_function_body
2405 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2406 static tree cp_parser_save_nsdmi
2407 (cp_parser *);
2408 static tree cp_parser_enclosed_template_argument_list
2409 (cp_parser *);
2410 static void cp_parser_save_default_args
2411 (cp_parser *, tree);
2412 static void cp_parser_late_parsing_for_member
2413 (cp_parser *, tree);
2414 static tree cp_parser_late_parse_one_default_arg
2415 (cp_parser *, tree, tree, tree);
2416 static void cp_parser_late_parsing_nsdmi
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_default_args
2419 (cp_parser *, tree);
2420 static tree cp_parser_sizeof_operand
2421 (cp_parser *, enum rid);
2422 static tree cp_parser_trait_expr
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_declares_only_class_p
2425 (cp_parser *);
2426 static void cp_parser_set_storage_class
2427 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2428 static void cp_parser_set_decl_spec_type
2429 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2430 static void set_and_check_decl_spec_loc
2431 (cp_decl_specifier_seq *decl_specs,
2432 cp_decl_spec ds, cp_token *);
2433 static bool cp_parser_friend_p
2434 (const cp_decl_specifier_seq *);
2435 static void cp_parser_required_error
2436 (cp_parser *, required_token, bool);
2437 static cp_token *cp_parser_require
2438 (cp_parser *, enum cpp_ttype, required_token);
2439 static cp_token *cp_parser_require_keyword
2440 (cp_parser *, enum rid, required_token);
2441 static bool cp_parser_token_starts_function_definition_p
2442 (cp_token *);
2443 static bool cp_parser_next_token_starts_class_definition_p
2444 (cp_parser *);
2445 static bool cp_parser_next_token_ends_template_argument_p
2446 (cp_parser *);
2447 static bool cp_parser_nth_token_starts_template_argument_list_p
2448 (cp_parser *, size_t);
2449 static enum tag_types cp_parser_token_is_class_key
2450 (cp_token *);
2451 static enum tag_types cp_parser_token_is_type_parameter_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, 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, location_t location);
2523 static cp_declarator * cp_parser_make_indirect_declarator
2524 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2525 static bool cp_parser_compound_literal_p
2526 (cp_parser *);
2527 static bool cp_parser_array_designator_p
2528 (cp_parser *);
2529 static bool cp_parser_skip_to_closing_square_bracket
2530 (cp_parser *);
2532 /* Returns nonzero if we are parsing tentatively. */
2534 static inline bool
2535 cp_parser_parsing_tentatively (cp_parser* parser)
2537 return parser->context->next != NULL;
2540 /* Returns nonzero if TOKEN is a string literal. */
2542 static bool
2543 cp_parser_is_pure_string_literal (cp_token* token)
2545 return (token->type == CPP_STRING ||
2546 token->type == CPP_STRING16 ||
2547 token->type == CPP_STRING32 ||
2548 token->type == CPP_WSTRING ||
2549 token->type == CPP_UTF8STRING);
2552 /* Returns nonzero if TOKEN is a string literal
2553 of a user-defined string literal. */
2555 static bool
2556 cp_parser_is_string_literal (cp_token* token)
2558 return (cp_parser_is_pure_string_literal (token) ||
2559 token->type == CPP_STRING_USERDEF ||
2560 token->type == CPP_STRING16_USERDEF ||
2561 token->type == CPP_STRING32_USERDEF ||
2562 token->type == CPP_WSTRING_USERDEF ||
2563 token->type == CPP_UTF8STRING_USERDEF);
2566 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2568 static bool
2569 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2571 return token->keyword == keyword;
2574 /* If not parsing tentatively, issue a diagnostic of the form
2575 FILE:LINE: MESSAGE before TOKEN
2576 where TOKEN is the next token in the input stream. MESSAGE
2577 (specified by the caller) is usually of the form "expected
2578 OTHER-TOKEN". */
2580 static void
2581 cp_parser_error (cp_parser* parser, const char* gmsgid)
2583 if (!cp_parser_simulate_error (parser))
2585 cp_token *token = cp_lexer_peek_token (parser->lexer);
2586 /* This diagnostic makes more sense if it is tagged to the line
2587 of the token we just peeked at. */
2588 cp_lexer_set_source_position_from_token (token);
2590 if (token->type == CPP_PRAGMA)
2592 error_at (token->location,
2593 "%<#pragma%> is not allowed here");
2594 cp_parser_skip_to_pragma_eol (parser, token);
2595 return;
2598 c_parse_error (gmsgid,
2599 /* Because c_parser_error does not understand
2600 CPP_KEYWORD, keywords are treated like
2601 identifiers. */
2602 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2603 token->u.value, token->flags);
2607 /* Issue an error about name-lookup failing. NAME is the
2608 IDENTIFIER_NODE DECL is the result of
2609 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2610 the thing that we hoped to find. */
2612 static void
2613 cp_parser_name_lookup_error (cp_parser* parser,
2614 tree name,
2615 tree decl,
2616 name_lookup_error desired,
2617 location_t location)
2619 /* If name lookup completely failed, tell the user that NAME was not
2620 declared. */
2621 if (decl == error_mark_node)
2623 if (parser->scope && parser->scope != global_namespace)
2624 error_at (location, "%<%E::%E%> has not been declared",
2625 parser->scope, name);
2626 else if (parser->scope == global_namespace)
2627 error_at (location, "%<::%E%> has not been declared", name);
2628 else if (parser->object_scope
2629 && !CLASS_TYPE_P (parser->object_scope))
2630 error_at (location, "request for member %qE in non-class type %qT",
2631 name, parser->object_scope);
2632 else if (parser->object_scope)
2633 error_at (location, "%<%T::%E%> has not been declared",
2634 parser->object_scope, name);
2635 else
2636 error_at (location, "%qE has not been declared", name);
2638 else if (parser->scope && parser->scope != global_namespace)
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%<%E::%E%> is not a type",
2644 parser->scope, name);
2645 break;
2646 case NLE_CXX98:
2647 error_at (location, "%<%E::%E%> is not a class or namespace",
2648 parser->scope, name);
2649 break;
2650 case NLE_NOT_CXX98:
2651 error_at (location,
2652 "%<%E::%E%> is not a class, namespace, or enumeration",
2653 parser->scope, name);
2654 break;
2655 default:
2656 gcc_unreachable ();
2660 else if (parser->scope == global_namespace)
2662 switch (desired)
2664 case NLE_TYPE:
2665 error_at (location, "%<::%E%> is not a type", name);
2666 break;
2667 case NLE_CXX98:
2668 error_at (location, "%<::%E%> is not a class or namespace", name);
2669 break;
2670 case NLE_NOT_CXX98:
2671 error_at (location,
2672 "%<::%E%> is not a class, namespace, or enumeration",
2673 name);
2674 break;
2675 default:
2676 gcc_unreachable ();
2679 else
2681 switch (desired)
2683 case NLE_TYPE:
2684 error_at (location, "%qE is not a type", name);
2685 break;
2686 case NLE_CXX98:
2687 error_at (location, "%qE is not a class or namespace", name);
2688 break;
2689 case NLE_NOT_CXX98:
2690 error_at (location,
2691 "%qE is not a class, namespace, or enumeration", name);
2692 break;
2693 default:
2694 gcc_unreachable ();
2699 /* If we are parsing tentatively, remember that an error has occurred
2700 during this tentative parse. Returns true if the error was
2701 simulated; false if a message should be issued by the caller. */
2703 static bool
2704 cp_parser_simulate_error (cp_parser* parser)
2706 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2708 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2709 return true;
2711 return false;
2714 /* This function is called when a type is defined. If type
2715 definitions are forbidden at this point, an error message is
2716 issued. */
2718 static bool
2719 cp_parser_check_type_definition (cp_parser* parser)
2721 /* If types are forbidden here, issue a message. */
2722 if (parser->type_definition_forbidden_message)
2724 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2725 in the message need to be interpreted. */
2726 error (parser->type_definition_forbidden_message);
2727 return false;
2729 return true;
2732 /* This function is called when the DECLARATOR is processed. The TYPE
2733 was a type defined in the decl-specifiers. If it is invalid to
2734 define a type in the decl-specifiers for DECLARATOR, an error is
2735 issued. TYPE_LOCATION is the location of TYPE and is used
2736 for error reporting. */
2738 static void
2739 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2740 tree type, location_t type_location)
2742 /* [dcl.fct] forbids type definitions in return types.
2743 Unfortunately, it's not easy to know whether or not we are
2744 processing a return type until after the fact. */
2745 while (declarator
2746 && (declarator->kind == cdk_pointer
2747 || declarator->kind == cdk_reference
2748 || declarator->kind == cdk_ptrmem))
2749 declarator = declarator->declarator;
2750 if (declarator
2751 && declarator->kind == cdk_function)
2753 error_at (type_location,
2754 "new types may not be defined in a return type");
2755 inform (type_location,
2756 "(perhaps a semicolon is missing after the definition of %qT)",
2757 type);
2761 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2762 "<" in any valid C++ program. If the next token is indeed "<",
2763 issue a message warning the user about what appears to be an
2764 invalid attempt to form a template-id. LOCATION is the location
2765 of the type-specifier (TYPE) */
2767 static void
2768 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2769 tree type,
2770 enum tag_types tag_type,
2771 location_t location)
2773 cp_token_position start = 0;
2775 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2777 if (TYPE_P (type))
2778 error_at (location, "%qT is not a template", type);
2779 else if (identifier_p (type))
2781 if (tag_type != none_type)
2782 error_at (location, "%qE is not a class template", type);
2783 else
2784 error_at (location, "%qE is not a template", type);
2786 else
2787 error_at (location, "invalid template-id");
2788 /* Remember the location of the invalid "<". */
2789 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2790 start = cp_lexer_token_position (parser->lexer, true);
2791 /* Consume the "<". */
2792 cp_lexer_consume_token (parser->lexer);
2793 /* Parse the template arguments. */
2794 cp_parser_enclosed_template_argument_list (parser);
2795 /* Permanently remove the invalid template arguments so that
2796 this error message is not issued again. */
2797 if (start)
2798 cp_lexer_purge_tokens_after (parser->lexer, start);
2802 /* If parsing an integral constant-expression, issue an error message
2803 about the fact that THING appeared and return true. Otherwise,
2804 return false. In either case, set
2805 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2807 static bool
2808 cp_parser_non_integral_constant_expression (cp_parser *parser,
2809 non_integral_constant thing)
2811 parser->non_integral_constant_expression_p = true;
2812 if (parser->integral_constant_expression_p)
2814 if (!parser->allow_non_integral_constant_expression_p)
2816 const char *msg = NULL;
2817 switch (thing)
2819 case NIC_FLOAT:
2820 error ("floating-point literal "
2821 "cannot appear in a constant-expression");
2822 return true;
2823 case NIC_CAST:
2824 error ("a cast to a type other than an integral or "
2825 "enumeration type cannot appear in a "
2826 "constant-expression");
2827 return true;
2828 case NIC_TYPEID:
2829 error ("%<typeid%> operator "
2830 "cannot appear in a constant-expression");
2831 return true;
2832 case NIC_NCC:
2833 error ("non-constant compound literals "
2834 "cannot appear in a constant-expression");
2835 return true;
2836 case NIC_FUNC_CALL:
2837 error ("a function call "
2838 "cannot appear in a constant-expression");
2839 return true;
2840 case NIC_INC:
2841 error ("an increment "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_DEC:
2845 error ("an decrement "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_ARRAY_REF:
2849 error ("an array reference "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_ADDR_LABEL:
2853 error ("the address of a label "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_OVERLOADED:
2857 error ("calls to overloaded operators "
2858 "cannot appear in a constant-expression");
2859 return true;
2860 case NIC_ASSIGNMENT:
2861 error ("an assignment cannot appear in a constant-expression");
2862 return true;
2863 case NIC_COMMA:
2864 error ("a comma operator "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_CONSTRUCTOR:
2868 error ("a call to a constructor "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_TRANSACTION:
2872 error ("a transaction expression "
2873 "cannot appear in a constant-expression");
2874 return true;
2875 case NIC_THIS:
2876 msg = "this";
2877 break;
2878 case NIC_FUNC_NAME:
2879 msg = "__FUNCTION__";
2880 break;
2881 case NIC_PRETTY_FUNC:
2882 msg = "__PRETTY_FUNCTION__";
2883 break;
2884 case NIC_C99_FUNC:
2885 msg = "__func__";
2886 break;
2887 case NIC_VA_ARG:
2888 msg = "va_arg";
2889 break;
2890 case NIC_ARROW:
2891 msg = "->";
2892 break;
2893 case NIC_POINT:
2894 msg = ".";
2895 break;
2896 case NIC_STAR:
2897 msg = "*";
2898 break;
2899 case NIC_ADDR:
2900 msg = "&";
2901 break;
2902 case NIC_PREINCREMENT:
2903 msg = "++";
2904 break;
2905 case NIC_PREDECREMENT:
2906 msg = "--";
2907 break;
2908 case NIC_NEW:
2909 msg = "new";
2910 break;
2911 case NIC_DEL:
2912 msg = "delete";
2913 break;
2914 default:
2915 gcc_unreachable ();
2917 if (msg)
2918 error ("%qs cannot appear in a constant-expression", msg);
2919 return true;
2922 return false;
2925 /* Emit a diagnostic for an invalid type name. This function commits
2926 to the current active tentative parse, if any. (Otherwise, the
2927 problematic construct might be encountered again later, resulting
2928 in duplicate error messages.) LOCATION is the location of ID. */
2930 static void
2931 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2932 location_t location)
2934 tree decl, ambiguous_decls;
2935 cp_parser_commit_to_tentative_parse (parser);
2936 /* Try to lookup the identifier. */
2937 decl = cp_parser_lookup_name (parser, id, none_type,
2938 /*is_template=*/false,
2939 /*is_namespace=*/false,
2940 /*check_dependency=*/true,
2941 &ambiguous_decls, location);
2942 if (ambiguous_decls)
2943 /* If the lookup was ambiguous, an error will already have
2944 been issued. */
2945 return;
2946 /* If the lookup found a template-name, it means that the user forgot
2947 to specify an argument list. Emit a useful error message. */
2948 if (DECL_TYPE_TEMPLATE_P (decl))
2950 error_at (location,
2951 "invalid use of template-name %qE without an argument list",
2952 decl);
2953 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2955 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2956 error_at (location, "invalid use of destructor %qD as a type", id);
2957 else if (TREE_CODE (decl) == TYPE_DECL)
2958 /* Something like 'unsigned A a;' */
2959 error_at (location, "invalid combination of multiple type-specifiers");
2960 else if (!parser->scope)
2962 /* Issue an error message. */
2963 error_at (location, "%qE does not name a type", id);
2964 /* If we're in a template class, it's possible that the user was
2965 referring to a type from a base class. For example:
2967 template <typename T> struct A { typedef T X; };
2968 template <typename T> struct B : public A<T> { X x; };
2970 The user should have said "typename A<T>::X". */
2971 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2972 inform (location, "C++11 %<constexpr%> only available with "
2973 "-std=c++11 or -std=gnu++11");
2974 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2975 inform (location, "C++11 %<noexcept%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11
2978 && TREE_CODE (id) == IDENTIFIER_NODE
2979 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2980 inform (location, "C++11 %<thread_local%> only available with "
2981 "-std=c++11 or -std=gnu++11");
2982 else if (processing_template_decl && current_class_type
2983 && TYPE_BINFO (current_class_type))
2985 tree b;
2987 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2989 b = TREE_CHAIN (b))
2991 tree base_type = BINFO_TYPE (b);
2992 if (CLASS_TYPE_P (base_type)
2993 && dependent_type_p (base_type))
2995 tree field;
2996 /* Go from a particular instantiation of the
2997 template (which will have an empty TYPE_FIELDs),
2998 to the main version. */
2999 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3000 for (field = TYPE_FIELDS (base_type);
3001 field;
3002 field = DECL_CHAIN (field))
3003 if (TREE_CODE (field) == TYPE_DECL
3004 && DECL_NAME (field) == id)
3006 inform (location,
3007 "(perhaps %<typename %T::%E%> was intended)",
3008 BINFO_TYPE (b), id);
3009 break;
3011 if (field)
3012 break;
3017 /* Here we diagnose qualified-ids where the scope is actually correct,
3018 but the identifier does not resolve to a valid type name. */
3019 else if (parser->scope != error_mark_node)
3021 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3023 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3024 error_at (location_of (id),
3025 "%qE in namespace %qE does not name a template type",
3026 id, parser->scope);
3027 else
3028 error_at (location_of (id),
3029 "%qE in namespace %qE does not name a type",
3030 id, parser->scope);
3031 if (DECL_P (decl))
3032 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3034 else if (CLASS_TYPE_P (parser->scope)
3035 && constructor_name_p (id, parser->scope))
3037 /* A<T>::A<T>() */
3038 error_at (location, "%<%T::%E%> names the constructor, not"
3039 " the type", parser->scope, id);
3040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3041 error_at (location, "and %qT has no template constructors",
3042 parser->scope);
3044 else if (TYPE_P (parser->scope)
3045 && dependent_scope_p (parser->scope))
3046 error_at (location, "need %<typename%> before %<%T::%E%> because "
3047 "%qT is a dependent scope",
3048 parser->scope, id, parser->scope);
3049 else if (TYPE_P (parser->scope))
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3052 error_at (location_of (id),
3053 "%qE in %q#T does not name a template type",
3054 id, parser->scope);
3055 else
3056 error_at (location_of (id),
3057 "%qE in %q#T does not name a type",
3058 id, parser->scope);
3059 if (DECL_P (decl))
3060 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3062 else
3063 gcc_unreachable ();
3067 /* Check for a common situation where a type-name should be present,
3068 but is not, and issue a sensible error message. Returns true if an
3069 invalid type-name was detected.
3071 The situation handled by this function are variable declarations of the
3072 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3073 Usually, `ID' should name a type, but if we got here it means that it
3074 does not. We try to emit the best possible error message depending on
3075 how exactly the id-expression looks like. */
3077 static bool
3078 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3080 tree id;
3081 cp_token *token = cp_lexer_peek_token (parser->lexer);
3083 /* Avoid duplicate error about ambiguous lookup. */
3084 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3086 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3087 if (next->type == CPP_NAME && next->error_reported)
3088 goto out;
3091 cp_parser_parse_tentatively (parser);
3092 id = cp_parser_id_expression (parser,
3093 /*template_keyword_p=*/false,
3094 /*check_dependency_p=*/true,
3095 /*template_p=*/NULL,
3096 /*declarator_p=*/true,
3097 /*optional_p=*/false);
3098 /* If the next token is a (, this is a function with no explicit return
3099 type, i.e. constructor, destructor or conversion op. */
3100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3101 || TREE_CODE (id) == TYPE_DECL)
3103 cp_parser_abort_tentative_parse (parser);
3104 return false;
3106 if (!cp_parser_parse_definitely (parser))
3107 return false;
3109 /* Emit a diagnostic for the invalid type. */
3110 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3111 out:
3112 /* If we aren't in the middle of a declarator (i.e. in a
3113 parameter-declaration-clause), skip to the end of the declaration;
3114 there's no point in trying to process it. */
3115 if (!parser->in_declarator_p)
3116 cp_parser_skip_to_end_of_block_or_statement (parser);
3117 return true;
3120 /* Consume tokens up to, and including, the next non-nested closing `)'.
3121 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3122 are doing error recovery. Returns -1 if OR_COMMA is true and we
3123 found an unnested comma. */
3125 static int
3126 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3127 bool recovering,
3128 bool or_comma,
3129 bool consume_paren)
3131 unsigned paren_depth = 0;
3132 unsigned brace_depth = 0;
3133 unsigned square_depth = 0;
3135 if (recovering && !or_comma
3136 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3137 return 0;
3139 while (true)
3141 cp_token * token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3145 case CPP_EOF:
3146 case CPP_PRAGMA_EOL:
3147 /* If we've run out of tokens, then there is no closing `)'. */
3148 return 0;
3150 /* This is good for lambda expression capture-lists. */
3151 case CPP_OPEN_SQUARE:
3152 ++square_depth;
3153 break;
3154 case CPP_CLOSE_SQUARE:
3155 if (!square_depth--)
3156 return 0;
3157 break;
3159 case CPP_SEMICOLON:
3160 /* This matches the processing in skip_to_end_of_statement. */
3161 if (!brace_depth)
3162 return 0;
3163 break;
3165 case CPP_OPEN_BRACE:
3166 ++brace_depth;
3167 break;
3168 case CPP_CLOSE_BRACE:
3169 if (!brace_depth--)
3170 return 0;
3171 break;
3173 case CPP_COMMA:
3174 if (recovering && or_comma && !brace_depth && !paren_depth
3175 && !square_depth)
3176 return -1;
3177 break;
3179 case CPP_OPEN_PAREN:
3180 if (!brace_depth)
3181 ++paren_depth;
3182 break;
3184 case CPP_CLOSE_PAREN:
3185 if (!brace_depth && !paren_depth--)
3187 if (consume_paren)
3188 cp_lexer_consume_token (parser->lexer);
3189 return 1;
3191 break;
3193 default:
3194 break;
3197 /* Consume the token. */
3198 cp_lexer_consume_token (parser->lexer);
3202 /* Consume tokens until we reach the end of the current statement.
3203 Normally, that will be just before consuming a `;'. However, if a
3204 non-nested `}' comes first, then we stop before consuming that. */
3206 static void
3207 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3209 unsigned nesting_depth = 0;
3211 /* Unwind generic function template scope if necessary. */
3212 if (parser->fully_implicit_function_template_p)
3213 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3215 while (true)
3217 cp_token *token = cp_lexer_peek_token (parser->lexer);
3219 switch (token->type)
3221 case CPP_EOF:
3222 case CPP_PRAGMA_EOL:
3223 /* If we've run out of tokens, stop. */
3224 return;
3226 case CPP_SEMICOLON:
3227 /* If the next token is a `;', we have reached the end of the
3228 statement. */
3229 if (!nesting_depth)
3230 return;
3231 break;
3233 case CPP_CLOSE_BRACE:
3234 /* If this is a non-nested '}', stop before consuming it.
3235 That way, when confronted with something like:
3237 { 3 + }
3239 we stop before consuming the closing '}', even though we
3240 have not yet reached a `;'. */
3241 if (nesting_depth == 0)
3242 return;
3244 /* If it is the closing '}' for a block that we have
3245 scanned, stop -- but only after consuming the token.
3246 That way given:
3248 void f g () { ... }
3249 typedef int I;
3251 we will stop after the body of the erroneously declared
3252 function, but before consuming the following `typedef'
3253 declaration. */
3254 if (--nesting_depth == 0)
3256 cp_lexer_consume_token (parser->lexer);
3257 return;
3260 case CPP_OPEN_BRACE:
3261 ++nesting_depth;
3262 break;
3264 default:
3265 break;
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser->lexer);
3273 /* This function is called at the end of a statement or declaration.
3274 If the next token is a semicolon, it is consumed; otherwise, error
3275 recovery is attempted. */
3277 static void
3278 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3280 /* Look for the trailing `;'. */
3281 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3283 /* If there is additional (erroneous) input, skip to the end of
3284 the statement. */
3285 cp_parser_skip_to_end_of_statement (parser);
3286 /* If the next token is now a `;', consume it. */
3287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3288 cp_lexer_consume_token (parser->lexer);
3292 /* Skip tokens until we have consumed an entire block, or until we
3293 have consumed a non-nested `;'. */
3295 static void
3296 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3298 int nesting_depth = 0;
3300 /* Unwind generic function template scope if necessary. */
3301 if (parser->fully_implicit_function_template_p)
3302 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3304 while (nesting_depth >= 0)
3306 cp_token *token = cp_lexer_peek_token (parser->lexer);
3308 switch (token->type)
3310 case CPP_EOF:
3311 case CPP_PRAGMA_EOL:
3312 /* If we've run out of tokens, stop. */
3313 return;
3315 case CPP_SEMICOLON:
3316 /* Stop if this is an unnested ';'. */
3317 if (!nesting_depth)
3318 nesting_depth = -1;
3319 break;
3321 case CPP_CLOSE_BRACE:
3322 /* Stop if this is an unnested '}', or closes the outermost
3323 nesting level. */
3324 nesting_depth--;
3325 if (nesting_depth < 0)
3326 return;
3327 if (!nesting_depth)
3328 nesting_depth = -1;
3329 break;
3331 case CPP_OPEN_BRACE:
3332 /* Nest. */
3333 nesting_depth++;
3334 break;
3336 default:
3337 break;
3340 /* Consume the token. */
3341 cp_lexer_consume_token (parser->lexer);
3345 /* Skip tokens until a non-nested closing curly brace is the next
3346 token, or there are no more tokens. Return true in the first case,
3347 false otherwise. */
3349 static bool
3350 cp_parser_skip_to_closing_brace (cp_parser *parser)
3352 unsigned nesting_depth = 0;
3354 while (true)
3356 cp_token *token = cp_lexer_peek_token (parser->lexer);
3358 switch (token->type)
3360 case CPP_EOF:
3361 case CPP_PRAGMA_EOL:
3362 /* If we've run out of tokens, stop. */
3363 return false;
3365 case CPP_CLOSE_BRACE:
3366 /* If the next token is a non-nested `}', then we have reached
3367 the end of the current block. */
3368 if (nesting_depth-- == 0)
3369 return true;
3370 break;
3372 case CPP_OPEN_BRACE:
3373 /* If it the next token is a `{', then we are entering a new
3374 block. Consume the entire block. */
3375 ++nesting_depth;
3376 break;
3378 default:
3379 break;
3382 /* Consume the token. */
3383 cp_lexer_consume_token (parser->lexer);
3387 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3388 parameter is the PRAGMA token, allowing us to purge the entire pragma
3389 sequence. */
3391 static void
3392 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3394 cp_token *token;
3396 parser->lexer->in_pragma = false;
3399 token = cp_lexer_consume_token (parser->lexer);
3400 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3402 /* Ensure that the pragma is not parsed again. */
3403 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3406 /* Require pragma end of line, resyncing with it as necessary. The
3407 arguments are as for cp_parser_skip_to_pragma_eol. */
3409 static void
3410 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3412 parser->lexer->in_pragma = false;
3413 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3417 /* This is a simple wrapper around make_typename_type. When the id is
3418 an unresolved identifier node, we can provide a superior diagnostic
3419 using cp_parser_diagnose_invalid_type_name. */
3421 static tree
3422 cp_parser_make_typename_type (cp_parser *parser, tree id,
3423 location_t id_location)
3425 tree result;
3426 if (identifier_p (id))
3428 result = make_typename_type (parser->scope, id, typename_type,
3429 /*complain=*/tf_none);
3430 if (result == error_mark_node)
3431 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3432 return result;
3434 return make_typename_type (parser->scope, id, typename_type, tf_error);
3437 /* This is a wrapper around the
3438 make_{pointer,ptrmem,reference}_declarator functions that decides
3439 which one to call based on the CODE and CLASS_TYPE arguments. The
3440 CODE argument should be one of the values returned by
3441 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3442 appertain to the pointer or reference. */
3444 static cp_declarator *
3445 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3446 cp_cv_quals cv_qualifiers,
3447 cp_declarator *target,
3448 tree attributes)
3450 if (code == ERROR_MARK)
3451 return cp_error_declarator;
3453 if (code == INDIRECT_REF)
3454 if (class_type == NULL_TREE)
3455 return make_pointer_declarator (cv_qualifiers, target, attributes);
3456 else
3457 return make_ptrmem_declarator (cv_qualifiers, class_type,
3458 target, attributes);
3459 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3460 return make_reference_declarator (cv_qualifiers, target,
3461 false, attributes);
3462 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3463 return make_reference_declarator (cv_qualifiers, target,
3464 true, attributes);
3465 gcc_unreachable ();
3468 /* Create a new C++ parser. */
3470 static cp_parser *
3471 cp_parser_new (void)
3473 cp_parser *parser;
3474 cp_lexer *lexer;
3475 unsigned i;
3477 /* cp_lexer_new_main is called before doing GC allocation because
3478 cp_lexer_new_main might load a PCH file. */
3479 lexer = cp_lexer_new_main ();
3481 /* Initialize the binops_by_token so that we can get the tree
3482 directly from the token. */
3483 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3484 binops_by_token[binops[i].token_type] = binops[i];
3486 parser = ggc_cleared_alloc<cp_parser> ();
3487 parser->lexer = lexer;
3488 parser->context = cp_parser_context_new (NULL);
3490 /* For now, we always accept GNU extensions. */
3491 parser->allow_gnu_extensions_p = 1;
3493 /* The `>' token is a greater-than operator, not the end of a
3494 template-id. */
3495 parser->greater_than_is_operator_p = true;
3497 parser->default_arg_ok_p = true;
3499 /* We are not parsing a constant-expression. */
3500 parser->integral_constant_expression_p = false;
3501 parser->allow_non_integral_constant_expression_p = false;
3502 parser->non_integral_constant_expression_p = false;
3504 /* Local variable names are not forbidden. */
3505 parser->local_variables_forbidden_p = false;
3507 /* We are not processing an `extern "C"' declaration. */
3508 parser->in_unbraced_linkage_specification_p = false;
3510 /* We are not processing a declarator. */
3511 parser->in_declarator_p = false;
3513 /* We are not processing a template-argument-list. */
3514 parser->in_template_argument_list_p = false;
3516 /* We are not in an iteration statement. */
3517 parser->in_statement = 0;
3519 /* We are not in a switch statement. */
3520 parser->in_switch_statement_p = false;
3522 /* We are not parsing a type-id inside an expression. */
3523 parser->in_type_id_in_expr_p = false;
3525 /* Declarations aren't implicitly extern "C". */
3526 parser->implicit_extern_c = false;
3528 /* String literals should be translated to the execution character set. */
3529 parser->translate_strings_p = true;
3531 /* We are not parsing a function body. */
3532 parser->in_function_body = false;
3534 /* We can correct until told otherwise. */
3535 parser->colon_corrects_to_scope_p = true;
3537 /* The unparsed function queue is empty. */
3538 push_unparsed_function_queues (parser);
3540 /* There are no classes being defined. */
3541 parser->num_classes_being_defined = 0;
3543 /* No template parameters apply. */
3544 parser->num_template_parameter_lists = 0;
3546 /* Not declaring an implicit function template. */
3547 parser->auto_is_implicit_function_template_parm_p = false;
3548 parser->fully_implicit_function_template_p = false;
3549 parser->implicit_template_parms = 0;
3550 parser->implicit_template_scope = 0;
3552 return parser;
3555 /* Create a cp_lexer structure which will emit the tokens in CACHE
3556 and push it onto the parser's lexer stack. This is used for delayed
3557 parsing of in-class method bodies and default arguments, and should
3558 not be confused with tentative parsing. */
3559 static void
3560 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3562 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3563 lexer->next = parser->lexer;
3564 parser->lexer = lexer;
3566 /* Move the current source position to that of the first token in the
3567 new lexer. */
3568 cp_lexer_set_source_position_from_token (lexer->next_token);
3571 /* Pop the top lexer off the parser stack. This is never used for the
3572 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3573 static void
3574 cp_parser_pop_lexer (cp_parser *parser)
3576 cp_lexer *lexer = parser->lexer;
3577 parser->lexer = lexer->next;
3578 cp_lexer_destroy (lexer);
3580 /* Put the current source position back where it was before this
3581 lexer was pushed. */
3582 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3585 /* Lexical conventions [gram.lex] */
3587 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3588 identifier. */
3590 static tree
3591 cp_parser_identifier (cp_parser* parser)
3593 cp_token *token;
3595 /* Look for the identifier. */
3596 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3597 /* Return the value. */
3598 return token ? token->u.value : error_mark_node;
3601 /* Parse a sequence of adjacent string constants. Returns a
3602 TREE_STRING representing the combined, nul-terminated string
3603 constant. If TRANSLATE is true, translate the string to the
3604 execution character set. If WIDE_OK is true, a wide string is
3605 invalid here.
3607 C++98 [lex.string] says that if a narrow string literal token is
3608 adjacent to a wide string literal token, the behavior is undefined.
3609 However, C99 6.4.5p4 says that this results in a wide string literal.
3610 We follow C99 here, for consistency with the C front end.
3612 This code is largely lifted from lex_string() in c-lex.c.
3614 FUTURE: ObjC++ will need to handle @-strings here. */
3615 static tree
3616 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3617 bool lookup_udlit = true)
3619 tree value;
3620 size_t count;
3621 struct obstack str_ob;
3622 cpp_string str, istr, *strs;
3623 cp_token *tok;
3624 enum cpp_ttype type, curr_type;
3625 int have_suffix_p = 0;
3626 tree string_tree;
3627 tree suffix_id = NULL_TREE;
3628 bool curr_tok_is_userdef_p = false;
3630 tok = cp_lexer_peek_token (parser->lexer);
3631 if (!cp_parser_is_string_literal (tok))
3633 cp_parser_error (parser, "expected string-literal");
3634 return error_mark_node;
3637 if (cpp_userdef_string_p (tok->type))
3639 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3640 curr_type = cpp_userdef_string_remove_type (tok->type);
3641 curr_tok_is_userdef_p = true;
3643 else
3645 string_tree = tok->u.value;
3646 curr_type = tok->type;
3648 type = curr_type;
3650 /* Try to avoid the overhead of creating and destroying an obstack
3651 for the common case of just one string. */
3652 if (!cp_parser_is_string_literal
3653 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3655 cp_lexer_consume_token (parser->lexer);
3657 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3658 str.len = TREE_STRING_LENGTH (string_tree);
3659 count = 1;
3661 if (curr_tok_is_userdef_p)
3663 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3664 have_suffix_p = 1;
3665 curr_type = cpp_userdef_string_remove_type (tok->type);
3667 else
3668 curr_type = tok->type;
3670 strs = &str;
3672 else
3674 gcc_obstack_init (&str_ob);
3675 count = 0;
3679 cp_lexer_consume_token (parser->lexer);
3680 count++;
3681 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3682 str.len = TREE_STRING_LENGTH (string_tree);
3684 if (curr_tok_is_userdef_p)
3686 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3687 if (have_suffix_p == 0)
3689 suffix_id = curr_suffix_id;
3690 have_suffix_p = 1;
3692 else if (have_suffix_p == 1
3693 && curr_suffix_id != suffix_id)
3695 error ("inconsistent user-defined literal suffixes"
3696 " %qD and %qD in string literal",
3697 suffix_id, curr_suffix_id);
3698 have_suffix_p = -1;
3700 curr_type = cpp_userdef_string_remove_type (tok->type);
3702 else
3703 curr_type = tok->type;
3705 if (type != curr_type)
3707 if (type == CPP_STRING)
3708 type = curr_type;
3709 else if (curr_type != CPP_STRING)
3710 error_at (tok->location,
3711 "unsupported non-standard concatenation "
3712 "of string literals");
3715 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3717 tok = cp_lexer_peek_token (parser->lexer);
3718 if (cpp_userdef_string_p (tok->type))
3720 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3721 curr_type = cpp_userdef_string_remove_type (tok->type);
3722 curr_tok_is_userdef_p = true;
3724 else
3726 string_tree = tok->u.value;
3727 curr_type = tok->type;
3728 curr_tok_is_userdef_p = false;
3731 while (cp_parser_is_string_literal (tok));
3733 strs = (cpp_string *) obstack_finish (&str_ob);
3736 if (type != CPP_STRING && !wide_ok)
3738 cp_parser_error (parser, "a wide string is invalid in this context");
3739 type = CPP_STRING;
3742 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3743 (parse_in, strs, count, &istr, type))
3745 value = build_string (istr.len, (const char *)istr.text);
3746 free (CONST_CAST (unsigned char *, istr.text));
3748 switch (type)
3750 default:
3751 case CPP_STRING:
3752 case CPP_UTF8STRING:
3753 TREE_TYPE (value) = char_array_type_node;
3754 break;
3755 case CPP_STRING16:
3756 TREE_TYPE (value) = char16_array_type_node;
3757 break;
3758 case CPP_STRING32:
3759 TREE_TYPE (value) = char32_array_type_node;
3760 break;
3761 case CPP_WSTRING:
3762 TREE_TYPE (value) = wchar_array_type_node;
3763 break;
3766 value = fix_string_type (value);
3768 if (have_suffix_p)
3770 tree literal = build_userdef_literal (suffix_id, value,
3771 OT_NONE, NULL_TREE);
3772 if (lookup_udlit)
3773 value = cp_parser_userdef_string_literal (literal);
3774 else
3775 value = literal;
3778 else
3779 /* cpp_interpret_string has issued an error. */
3780 value = error_mark_node;
3782 if (count > 1)
3783 obstack_free (&str_ob, 0);
3785 return value;
3788 /* Look up a literal operator with the name and the exact arguments. */
3790 static tree
3791 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3793 tree decl, fns;
3794 decl = lookup_name (name);
3795 if (!decl || !is_overloaded_fn (decl))
3796 return error_mark_node;
3798 for (fns = decl; fns; fns = OVL_NEXT (fns))
3800 unsigned int ix;
3801 bool found = true;
3802 tree fn = OVL_CURRENT (fns);
3803 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3804 if (parmtypes != NULL_TREE)
3806 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3807 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3809 tree tparm = TREE_VALUE (parmtypes);
3810 tree targ = TREE_TYPE ((*args)[ix]);
3811 bool ptr = TYPE_PTR_P (tparm);
3812 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3813 if ((ptr || arr || !same_type_p (tparm, targ))
3814 && (!ptr || !arr
3815 || !same_type_p (TREE_TYPE (tparm),
3816 TREE_TYPE (targ))))
3817 found = false;
3819 if (found
3820 && ix == vec_safe_length (args)
3821 /* May be this should be sufficient_parms_p instead,
3822 depending on how exactly should user-defined literals
3823 work in presence of default arguments on the literal
3824 operator parameters. */
3825 && parmtypes == void_list_node)
3826 return decl;
3830 return error_mark_node;
3833 /* Parse a user-defined char constant. Returns a call to a user-defined
3834 literal operator taking the character as an argument. */
3836 static tree
3837 cp_parser_userdef_char_literal (cp_parser *parser)
3839 cp_token *token = cp_lexer_consume_token (parser->lexer);
3840 tree literal = token->u.value;
3841 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3842 tree value = USERDEF_LITERAL_VALUE (literal);
3843 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3844 tree decl, result;
3846 /* Build up a call to the user-defined operator */
3847 /* Lookup the name we got back from the id-expression. */
3848 vec<tree, va_gc> *args = make_tree_vector ();
3849 vec_safe_push (args, value);
3850 decl = lookup_literal_operator (name, args);
3851 if (!decl || decl == error_mark_node)
3853 error ("unable to find character literal operator %qD with %qT argument",
3854 name, TREE_TYPE (value));
3855 release_tree_vector (args);
3856 return error_mark_node;
3858 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3859 release_tree_vector (args);
3860 return result;
3863 /* A subroutine of cp_parser_userdef_numeric_literal to
3864 create a char... template parameter pack from a string node. */
3866 static tree
3867 make_char_string_pack (tree value)
3869 tree charvec;
3870 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3871 const char *str = TREE_STRING_POINTER (value);
3872 int i, len = TREE_STRING_LENGTH (value) - 1;
3873 tree argvec = make_tree_vec (1);
3875 /* Fill in CHARVEC with all of the parameters. */
3876 charvec = make_tree_vec (len);
3877 for (i = 0; i < len; ++i)
3878 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3880 /* Build the argument packs. */
3881 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3882 TREE_TYPE (argpack) = char_type_node;
3884 TREE_VEC_ELT (argvec, 0) = argpack;
3886 return argvec;
3889 /* A subroutine of cp_parser_userdef_numeric_literal to
3890 create a char... template parameter pack from a string node. */
3892 static tree
3893 make_string_pack (tree value)
3895 tree charvec;
3896 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3897 const unsigned char *str
3898 = (const unsigned char *) TREE_STRING_POINTER (value);
3899 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3900 int len = TREE_STRING_LENGTH (value) / sz - 1;
3901 tree argvec = make_tree_vec (2);
3903 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3904 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3906 /* First template parm is character type. */
3907 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3909 /* Fill in CHARVEC with all of the parameters. */
3910 charvec = make_tree_vec (len);
3911 for (int i = 0; i < len; ++i)
3912 TREE_VEC_ELT (charvec, i)
3913 = double_int_to_tree (str_char_type_node,
3914 double_int::from_buffer (str + i * sz, sz));
3916 /* Build the argument packs. */
3917 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3918 TREE_TYPE (argpack) = str_char_type_node;
3920 TREE_VEC_ELT (argvec, 1) = argpack;
3922 return argvec;
3925 /* Parse a user-defined numeric constant. returns a call to a user-defined
3926 literal operator. */
3928 static tree
3929 cp_parser_userdef_numeric_literal (cp_parser *parser)
3931 cp_token *token = cp_lexer_consume_token (parser->lexer);
3932 tree literal = token->u.value;
3933 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3934 tree value = USERDEF_LITERAL_VALUE (literal);
3935 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3936 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3937 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3938 tree decl, result;
3939 vec<tree, va_gc> *args;
3941 /* Look for a literal operator taking the exact type of numeric argument
3942 as the literal value. */
3943 args = make_tree_vector ();
3944 vec_safe_push (args, value);
3945 decl = lookup_literal_operator (name, args);
3946 if (decl && decl != error_mark_node)
3948 result = finish_call_expr (decl, &args, false, true,
3949 tf_warning_or_error);
3951 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3953 warning_at (token->location, OPT_Woverflow,
3954 "integer literal exceeds range of %qT type",
3955 long_long_unsigned_type_node);
3957 else
3959 if (overflow > 0)
3960 warning_at (token->location, OPT_Woverflow,
3961 "floating literal exceeds range of %qT type",
3962 long_double_type_node);
3963 else if (overflow < 0)
3964 warning_at (token->location, OPT_Woverflow,
3965 "floating literal truncated to zero");
3968 release_tree_vector (args);
3969 return result;
3971 release_tree_vector (args);
3973 /* If the numeric argument didn't work, look for a raw literal
3974 operator taking a const char* argument consisting of the number
3975 in string format. */
3976 args = make_tree_vector ();
3977 vec_safe_push (args, num_string);
3978 decl = lookup_literal_operator (name, args);
3979 if (decl && decl != error_mark_node)
3981 result = finish_call_expr (decl, &args, false, true,
3982 tf_warning_or_error);
3983 release_tree_vector (args);
3984 return result;
3986 release_tree_vector (args);
3988 /* If the raw literal didn't work, look for a non-type template
3989 function with parameter pack char.... Call the function with
3990 template parameter characters representing the number. */
3991 args = make_tree_vector ();
3992 decl = lookup_literal_operator (name, args);
3993 if (decl && decl != error_mark_node)
3995 tree tmpl_args = make_char_string_pack (num_string);
3996 decl = lookup_template_function (decl, tmpl_args);
3997 result = finish_call_expr (decl, &args, false, true,
3998 tf_warning_or_error);
3999 release_tree_vector (args);
4000 return result;
4003 release_tree_vector (args);
4005 error ("unable to find numeric literal operator %qD", name);
4006 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4007 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4008 "to enable more built-in suffixes");
4009 return error_mark_node;
4012 /* Parse a user-defined string constant. Returns a call to a user-defined
4013 literal operator taking a character pointer and the length of the string
4014 as arguments. */
4016 static tree
4017 cp_parser_userdef_string_literal (tree literal)
4019 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4020 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4021 tree value = USERDEF_LITERAL_VALUE (literal);
4022 int len = TREE_STRING_LENGTH (value)
4023 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4024 tree decl, result;
4025 vec<tree, va_gc> *args;
4027 /* Build up a call to the user-defined operator. */
4028 /* Lookup the name we got back from the id-expression. */
4029 args = make_tree_vector ();
4030 vec_safe_push (args, value);
4031 vec_safe_push (args, build_int_cst (size_type_node, len));
4032 decl = lookup_literal_operator (name, args);
4034 if (decl && decl != error_mark_node)
4036 result = finish_call_expr (decl, &args, false, true,
4037 tf_warning_or_error);
4038 release_tree_vector (args);
4039 return result;
4041 release_tree_vector (args);
4043 /* Look for a template function with typename parameter CharT
4044 and parameter pack CharT... Call the function with
4045 template parameter characters representing the string. */
4046 args = make_tree_vector ();
4047 decl = lookup_literal_operator (name, args);
4048 if (decl && decl != error_mark_node)
4050 tree tmpl_args = make_string_pack (value);
4051 decl = lookup_template_function (decl, tmpl_args);
4052 result = finish_call_expr (decl, &args, false, true,
4053 tf_warning_or_error);
4054 release_tree_vector (args);
4055 return result;
4057 release_tree_vector (args);
4059 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4060 name, TREE_TYPE (value), size_type_node);
4061 return error_mark_node;
4065 /* Basic concepts [gram.basic] */
4067 /* Parse a translation-unit.
4069 translation-unit:
4070 declaration-seq [opt]
4072 Returns TRUE if all went well. */
4074 static bool
4075 cp_parser_translation_unit (cp_parser* parser)
4077 /* The address of the first non-permanent object on the declarator
4078 obstack. */
4079 static void *declarator_obstack_base;
4081 bool success;
4083 /* Create the declarator obstack, if necessary. */
4084 if (!cp_error_declarator)
4086 gcc_obstack_init (&declarator_obstack);
4087 /* Create the error declarator. */
4088 cp_error_declarator = make_declarator (cdk_error);
4089 /* Create the empty parameter list. */
4090 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4091 /* Remember where the base of the declarator obstack lies. */
4092 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4095 cp_parser_declaration_seq_opt (parser);
4097 /* If there are no tokens left then all went well. */
4098 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4100 /* Get rid of the token array; we don't need it any more. */
4101 cp_lexer_destroy (parser->lexer);
4102 parser->lexer = NULL;
4104 /* This file might have been a context that's implicitly extern
4105 "C". If so, pop the lang context. (Only relevant for PCH.) */
4106 if (parser->implicit_extern_c)
4108 pop_lang_context ();
4109 parser->implicit_extern_c = false;
4112 /* Finish up. */
4113 finish_translation_unit ();
4115 success = true;
4117 else
4119 cp_parser_error (parser, "expected declaration");
4120 success = false;
4123 /* Make sure the declarator obstack was fully cleaned up. */
4124 gcc_assert (obstack_next_free (&declarator_obstack)
4125 == declarator_obstack_base);
4127 /* All went well. */
4128 return success;
4131 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4132 decltype context. */
4134 static inline tsubst_flags_t
4135 complain_flags (bool decltype_p)
4137 tsubst_flags_t complain = tf_warning_or_error;
4138 if (decltype_p)
4139 complain |= tf_decltype;
4140 return complain;
4143 /* We're about to parse a collection of statements. If we're currently
4144 parsing tentatively, set up a firewall so that any nested
4145 cp_parser_commit_to_tentative_parse won't affect the current context. */
4147 static cp_token_position
4148 cp_parser_start_tentative_firewall (cp_parser *parser)
4150 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4151 return 0;
4153 cp_parser_parse_tentatively (parser);
4154 cp_parser_commit_to_topmost_tentative_parse (parser);
4155 return cp_lexer_token_position (parser->lexer, false);
4158 /* We've finished parsing the collection of statements. Wrap up the
4159 firewall and replace the relevant tokens with the parsed form. */
4161 static void
4162 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4163 tree expr)
4165 if (!start)
4166 return;
4168 /* Finish the firewall level. */
4169 cp_parser_parse_definitely (parser);
4170 /* And remember the result of the parse for when we try again. */
4171 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4172 token->type = CPP_PREPARSED_EXPR;
4173 token->u.value = expr;
4174 token->keyword = RID_MAX;
4175 cp_lexer_purge_tokens_after (parser->lexer, start);
4178 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4179 enclosing parentheses. */
4181 static tree
4182 cp_parser_statement_expr (cp_parser *parser)
4184 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4186 /* Consume the '('. */
4187 cp_lexer_consume_token (parser->lexer);
4188 /* Start the statement-expression. */
4189 tree expr = begin_stmt_expr ();
4190 /* Parse the compound-statement. */
4191 cp_parser_compound_statement (parser, expr, false, false);
4192 /* Finish up. */
4193 expr = finish_stmt_expr (expr, false);
4194 /* Consume the ')'. */
4195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4196 cp_parser_skip_to_end_of_statement (parser);
4198 cp_parser_end_tentative_firewall (parser, start, expr);
4199 return expr;
4202 /* Expressions [gram.expr] */
4204 /* Parse a primary-expression.
4206 primary-expression:
4207 literal
4208 this
4209 ( expression )
4210 id-expression
4211 lambda-expression (C++11)
4213 GNU Extensions:
4215 primary-expression:
4216 ( compound-statement )
4217 __builtin_va_arg ( assignment-expression , type-id )
4218 __builtin_offsetof ( type-id , offsetof-expression )
4220 C++ Extensions:
4221 __has_nothrow_assign ( type-id )
4222 __has_nothrow_constructor ( type-id )
4223 __has_nothrow_copy ( type-id )
4224 __has_trivial_assign ( type-id )
4225 __has_trivial_constructor ( type-id )
4226 __has_trivial_copy ( type-id )
4227 __has_trivial_destructor ( type-id )
4228 __has_virtual_destructor ( type-id )
4229 __is_abstract ( type-id )
4230 __is_base_of ( type-id , type-id )
4231 __is_class ( type-id )
4232 __is_empty ( type-id )
4233 __is_enum ( type-id )
4234 __is_final ( type-id )
4235 __is_literal_type ( type-id )
4236 __is_pod ( type-id )
4237 __is_polymorphic ( type-id )
4238 __is_std_layout ( type-id )
4239 __is_trivial ( type-id )
4240 __is_union ( type-id )
4242 Objective-C++ Extension:
4244 primary-expression:
4245 objc-expression
4247 literal:
4248 __null
4250 ADDRESS_P is true iff this expression was immediately preceded by
4251 "&" and therefore might denote a pointer-to-member. CAST_P is true
4252 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4253 true iff this expression is a template argument.
4255 Returns a representation of the expression. Upon return, *IDK
4256 indicates what kind of id-expression (if any) was present. */
4258 static tree
4259 cp_parser_primary_expression (cp_parser *parser,
4260 bool address_p,
4261 bool cast_p,
4262 bool template_arg_p,
4263 bool decltype_p,
4264 cp_id_kind *idk)
4266 cp_token *token = NULL;
4268 /* Assume the primary expression is not an id-expression. */
4269 *idk = CP_ID_KIND_NONE;
4271 /* Peek at the next token. */
4272 token = cp_lexer_peek_token (parser->lexer);
4273 switch ((int) token->type)
4275 /* literal:
4276 integer-literal
4277 character-literal
4278 floating-literal
4279 string-literal
4280 boolean-literal
4281 pointer-literal
4282 user-defined-literal */
4283 case CPP_CHAR:
4284 case CPP_CHAR16:
4285 case CPP_CHAR32:
4286 case CPP_WCHAR:
4287 case CPP_UTF8CHAR:
4288 case CPP_NUMBER:
4289 case CPP_PREPARSED_EXPR:
4290 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4291 return cp_parser_userdef_numeric_literal (parser);
4292 token = cp_lexer_consume_token (parser->lexer);
4293 if (TREE_CODE (token->u.value) == FIXED_CST)
4295 error_at (token->location,
4296 "fixed-point types not supported in C++");
4297 return error_mark_node;
4299 /* Floating-point literals are only allowed in an integral
4300 constant expression if they are cast to an integral or
4301 enumeration type. */
4302 if (TREE_CODE (token->u.value) == REAL_CST
4303 && parser->integral_constant_expression_p
4304 && pedantic)
4306 /* CAST_P will be set even in invalid code like "int(2.7 +
4307 ...)". Therefore, we have to check that the next token
4308 is sure to end the cast. */
4309 if (cast_p)
4311 cp_token *next_token;
4313 next_token = cp_lexer_peek_token (parser->lexer);
4314 if (/* The comma at the end of an
4315 enumerator-definition. */
4316 next_token->type != CPP_COMMA
4317 /* The curly brace at the end of an enum-specifier. */
4318 && next_token->type != CPP_CLOSE_BRACE
4319 /* The end of a statement. */
4320 && next_token->type != CPP_SEMICOLON
4321 /* The end of the cast-expression. */
4322 && next_token->type != CPP_CLOSE_PAREN
4323 /* The end of an array bound. */
4324 && next_token->type != CPP_CLOSE_SQUARE
4325 /* The closing ">" in a template-argument-list. */
4326 && (next_token->type != CPP_GREATER
4327 || parser->greater_than_is_operator_p)
4328 /* C++0x only: A ">>" treated like two ">" tokens,
4329 in a template-argument-list. */
4330 && (next_token->type != CPP_RSHIFT
4331 || (cxx_dialect == cxx98)
4332 || parser->greater_than_is_operator_p))
4333 cast_p = false;
4336 /* If we are within a cast, then the constraint that the
4337 cast is to an integral or enumeration type will be
4338 checked at that point. If we are not within a cast, then
4339 this code is invalid. */
4340 if (!cast_p)
4341 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4343 return token->u.value;
4345 case CPP_CHAR_USERDEF:
4346 case CPP_CHAR16_USERDEF:
4347 case CPP_CHAR32_USERDEF:
4348 case CPP_WCHAR_USERDEF:
4349 case CPP_UTF8CHAR_USERDEF:
4350 return cp_parser_userdef_char_literal (parser);
4352 case CPP_STRING:
4353 case CPP_STRING16:
4354 case CPP_STRING32:
4355 case CPP_WSTRING:
4356 case CPP_UTF8STRING:
4357 case CPP_STRING_USERDEF:
4358 case CPP_STRING16_USERDEF:
4359 case CPP_STRING32_USERDEF:
4360 case CPP_WSTRING_USERDEF:
4361 case CPP_UTF8STRING_USERDEF:
4362 /* ??? Should wide strings be allowed when parser->translate_strings_p
4363 is false (i.e. in attributes)? If not, we can kill the third
4364 argument to cp_parser_string_literal. */
4365 return cp_parser_string_literal (parser,
4366 parser->translate_strings_p,
4367 true);
4369 case CPP_OPEN_PAREN:
4370 /* If we see `( { ' then we are looking at the beginning of
4371 a GNU statement-expression. */
4372 if (cp_parser_allow_gnu_extensions_p (parser)
4373 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4375 /* Statement-expressions are not allowed by the standard. */
4376 pedwarn (token->location, OPT_Wpedantic,
4377 "ISO C++ forbids braced-groups within expressions");
4379 /* And they're not allowed outside of a function-body; you
4380 cannot, for example, write:
4382 int i = ({ int j = 3; j + 1; });
4384 at class or namespace scope. */
4385 if (!parser->in_function_body
4386 || parser->in_template_argument_list_p)
4388 error_at (token->location,
4389 "statement-expressions are not allowed outside "
4390 "functions nor in template-argument lists");
4391 cp_parser_skip_to_end_of_block_or_statement (parser);
4392 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4393 cp_lexer_consume_token (parser->lexer);
4394 return error_mark_node;
4396 else
4397 return cp_parser_statement_expr (parser);
4399 /* Otherwise it's a normal parenthesized expression. */
4401 tree expr;
4402 bool saved_greater_than_is_operator_p;
4404 /* Consume the `('. */
4405 cp_lexer_consume_token (parser->lexer);
4406 /* Within a parenthesized expression, a `>' token is always
4407 the greater-than operator. */
4408 saved_greater_than_is_operator_p
4409 = parser->greater_than_is_operator_p;
4410 parser->greater_than_is_operator_p = true;
4412 /* Parse the parenthesized expression. */
4413 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4414 /* Let the front end know that this expression was
4415 enclosed in parentheses. This matters in case, for
4416 example, the expression is of the form `A::B', since
4417 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4418 not. */
4419 expr = finish_parenthesized_expr (expr);
4420 /* DR 705: Wrapping an unqualified name in parentheses
4421 suppresses arg-dependent lookup. We want to pass back
4422 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4423 (c++/37862), but none of the others. */
4424 if (*idk != CP_ID_KIND_QUALIFIED)
4425 *idk = CP_ID_KIND_NONE;
4427 /* The `>' token might be the end of a template-id or
4428 template-parameter-list now. */
4429 parser->greater_than_is_operator_p
4430 = saved_greater_than_is_operator_p;
4431 /* Consume the `)'. */
4432 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4433 cp_parser_skip_to_end_of_statement (parser);
4435 return expr;
4438 case CPP_OPEN_SQUARE:
4440 if (c_dialect_objc ())
4442 /* We might have an Objective-C++ message. */
4443 cp_parser_parse_tentatively (parser);
4444 tree msg = cp_parser_objc_message_expression (parser);
4445 /* If that works out, we're done ... */
4446 if (cp_parser_parse_definitely (parser))
4447 return msg;
4448 /* ... else, fall though to see if it's a lambda. */
4450 tree lam = cp_parser_lambda_expression (parser);
4451 /* Don't warn about a failed tentative parse. */
4452 if (cp_parser_error_occurred (parser))
4453 return error_mark_node;
4454 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4455 return lam;
4458 case CPP_OBJC_STRING:
4459 if (c_dialect_objc ())
4460 /* We have an Objective-C++ string literal. */
4461 return cp_parser_objc_expression (parser);
4462 cp_parser_error (parser, "expected primary-expression");
4463 return error_mark_node;
4465 case CPP_KEYWORD:
4466 switch (token->keyword)
4468 /* These two are the boolean literals. */
4469 case RID_TRUE:
4470 cp_lexer_consume_token (parser->lexer);
4471 return boolean_true_node;
4472 case RID_FALSE:
4473 cp_lexer_consume_token (parser->lexer);
4474 return boolean_false_node;
4476 /* The `__null' literal. */
4477 case RID_NULL:
4478 cp_lexer_consume_token (parser->lexer);
4479 return null_node;
4481 /* The `nullptr' literal. */
4482 case RID_NULLPTR:
4483 cp_lexer_consume_token (parser->lexer);
4484 return nullptr_node;
4486 /* Recognize the `this' keyword. */
4487 case RID_THIS:
4488 cp_lexer_consume_token (parser->lexer);
4489 if (parser->local_variables_forbidden_p)
4491 error_at (token->location,
4492 "%<this%> may not be used in this context");
4493 return error_mark_node;
4495 /* Pointers cannot appear in constant-expressions. */
4496 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4497 return error_mark_node;
4498 return finish_this_expr ();
4500 /* The `operator' keyword can be the beginning of an
4501 id-expression. */
4502 case RID_OPERATOR:
4503 goto id_expression;
4505 case RID_FUNCTION_NAME:
4506 case RID_PRETTY_FUNCTION_NAME:
4507 case RID_C99_FUNCTION_NAME:
4509 non_integral_constant name;
4511 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4512 __func__ are the names of variables -- but they are
4513 treated specially. Therefore, they are handled here,
4514 rather than relying on the generic id-expression logic
4515 below. Grammatically, these names are id-expressions.
4517 Consume the token. */
4518 token = cp_lexer_consume_token (parser->lexer);
4520 switch (token->keyword)
4522 case RID_FUNCTION_NAME:
4523 name = NIC_FUNC_NAME;
4524 break;
4525 case RID_PRETTY_FUNCTION_NAME:
4526 name = NIC_PRETTY_FUNC;
4527 break;
4528 case RID_C99_FUNCTION_NAME:
4529 name = NIC_C99_FUNC;
4530 break;
4531 default:
4532 gcc_unreachable ();
4535 if (cp_parser_non_integral_constant_expression (parser, name))
4536 return error_mark_node;
4538 /* Look up the name. */
4539 return finish_fname (token->u.value);
4542 case RID_VA_ARG:
4544 tree expression;
4545 tree type;
4546 source_location type_location;
4548 /* The `__builtin_va_arg' construct is used to handle
4549 `va_arg'. Consume the `__builtin_va_arg' token. */
4550 cp_lexer_consume_token (parser->lexer);
4551 /* Look for the opening `('. */
4552 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4553 /* Now, parse the assignment-expression. */
4554 expression = cp_parser_assignment_expression (parser);
4555 /* Look for the `,'. */
4556 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4557 type_location = cp_lexer_peek_token (parser->lexer)->location;
4558 /* Parse the type-id. */
4559 type = cp_parser_type_id (parser);
4560 /* Look for the closing `)'. */
4561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4562 /* Using `va_arg' in a constant-expression is not
4563 allowed. */
4564 if (cp_parser_non_integral_constant_expression (parser,
4565 NIC_VA_ARG))
4566 return error_mark_node;
4567 return build_x_va_arg (type_location, expression, type);
4570 case RID_OFFSETOF:
4571 return cp_parser_builtin_offsetof (parser);
4573 case RID_HAS_NOTHROW_ASSIGN:
4574 case RID_HAS_NOTHROW_CONSTRUCTOR:
4575 case RID_HAS_NOTHROW_COPY:
4576 case RID_HAS_TRIVIAL_ASSIGN:
4577 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4578 case RID_HAS_TRIVIAL_COPY:
4579 case RID_HAS_TRIVIAL_DESTRUCTOR:
4580 case RID_HAS_VIRTUAL_DESTRUCTOR:
4581 case RID_IS_ABSTRACT:
4582 case RID_IS_BASE_OF:
4583 case RID_IS_CLASS:
4584 case RID_IS_EMPTY:
4585 case RID_IS_ENUM:
4586 case RID_IS_FINAL:
4587 case RID_IS_LITERAL_TYPE:
4588 case RID_IS_POD:
4589 case RID_IS_POLYMORPHIC:
4590 case RID_IS_STD_LAYOUT:
4591 case RID_IS_TRIVIAL:
4592 case RID_IS_TRIVIALLY_ASSIGNABLE:
4593 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4594 case RID_IS_TRIVIALLY_COPYABLE:
4595 case RID_IS_UNION:
4596 return cp_parser_trait_expr (parser, token->keyword);
4598 /* Objective-C++ expressions. */
4599 case RID_AT_ENCODE:
4600 case RID_AT_PROTOCOL:
4601 case RID_AT_SELECTOR:
4602 return cp_parser_objc_expression (parser);
4604 case RID_TEMPLATE:
4605 if (parser->in_function_body
4606 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4607 == CPP_LESS))
4609 error_at (token->location,
4610 "a template declaration cannot appear at block scope");
4611 cp_parser_skip_to_end_of_block_or_statement (parser);
4612 return error_mark_node;
4614 default:
4615 cp_parser_error (parser, "expected primary-expression");
4616 return error_mark_node;
4619 /* An id-expression can start with either an identifier, a
4620 `::' as the beginning of a qualified-id, or the "operator"
4621 keyword. */
4622 case CPP_NAME:
4623 case CPP_SCOPE:
4624 case CPP_TEMPLATE_ID:
4625 case CPP_NESTED_NAME_SPECIFIER:
4627 tree id_expression;
4628 tree decl;
4629 const char *error_msg;
4630 bool template_p;
4631 bool done;
4632 cp_token *id_expr_token;
4634 id_expression:
4635 /* Parse the id-expression. */
4636 id_expression
4637 = cp_parser_id_expression (parser,
4638 /*template_keyword_p=*/false,
4639 /*check_dependency_p=*/true,
4640 &template_p,
4641 /*declarator_p=*/false,
4642 /*optional_p=*/false);
4643 if (id_expression == error_mark_node)
4644 return error_mark_node;
4645 id_expr_token = token;
4646 token = cp_lexer_peek_token (parser->lexer);
4647 done = (token->type != CPP_OPEN_SQUARE
4648 && token->type != CPP_OPEN_PAREN
4649 && token->type != CPP_DOT
4650 && token->type != CPP_DEREF
4651 && token->type != CPP_PLUS_PLUS
4652 && token->type != CPP_MINUS_MINUS);
4653 /* If we have a template-id, then no further lookup is
4654 required. If the template-id was for a template-class, we
4655 will sometimes have a TYPE_DECL at this point. */
4656 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4657 || TREE_CODE (id_expression) == TYPE_DECL)
4658 decl = id_expression;
4659 /* Look up the name. */
4660 else
4662 tree ambiguous_decls;
4664 /* If we already know that this lookup is ambiguous, then
4665 we've already issued an error message; there's no reason
4666 to check again. */
4667 if (id_expr_token->type == CPP_NAME
4668 && id_expr_token->error_reported)
4670 cp_parser_simulate_error (parser);
4671 return error_mark_node;
4674 decl = cp_parser_lookup_name (parser, id_expression,
4675 none_type,
4676 template_p,
4677 /*is_namespace=*/false,
4678 /*check_dependency=*/true,
4679 &ambiguous_decls,
4680 id_expr_token->location);
4681 /* If the lookup was ambiguous, an error will already have
4682 been issued. */
4683 if (ambiguous_decls)
4684 return error_mark_node;
4686 /* In Objective-C++, we may have an Objective-C 2.0
4687 dot-syntax for classes here. */
4688 if (c_dialect_objc ()
4689 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4690 && TREE_CODE (decl) == TYPE_DECL
4691 && objc_is_class_name (decl))
4693 tree component;
4694 cp_lexer_consume_token (parser->lexer);
4695 component = cp_parser_identifier (parser);
4696 if (component == error_mark_node)
4697 return error_mark_node;
4699 return objc_build_class_component_ref (id_expression, component);
4702 /* In Objective-C++, an instance variable (ivar) may be preferred
4703 to whatever cp_parser_lookup_name() found. */
4704 decl = objc_lookup_ivar (decl, id_expression);
4706 /* If name lookup gives us a SCOPE_REF, then the
4707 qualifying scope was dependent. */
4708 if (TREE_CODE (decl) == SCOPE_REF)
4710 /* At this point, we do not know if DECL is a valid
4711 integral constant expression. We assume that it is
4712 in fact such an expression, so that code like:
4714 template <int N> struct A {
4715 int a[B<N>::i];
4718 is accepted. At template-instantiation time, we
4719 will check that B<N>::i is actually a constant. */
4720 return decl;
4722 /* Check to see if DECL is a local variable in a context
4723 where that is forbidden. */
4724 if (parser->local_variables_forbidden_p
4725 && local_variable_p (decl))
4727 /* It might be that we only found DECL because we are
4728 trying to be generous with pre-ISO scoping rules.
4729 For example, consider:
4731 int i;
4732 void g() {
4733 for (int i = 0; i < 10; ++i) {}
4734 extern void f(int j = i);
4737 Here, name look up will originally find the out
4738 of scope `i'. We need to issue a warning message,
4739 but then use the global `i'. */
4740 decl = check_for_out_of_scope_variable (decl);
4741 if (local_variable_p (decl))
4743 error_at (id_expr_token->location,
4744 "local variable %qD may not appear in this context",
4745 decl);
4746 return error_mark_node;
4751 decl = (finish_id_expression
4752 (id_expression, decl, parser->scope,
4753 idk,
4754 parser->integral_constant_expression_p,
4755 parser->allow_non_integral_constant_expression_p,
4756 &parser->non_integral_constant_expression_p,
4757 template_p, done, address_p,
4758 template_arg_p,
4759 &error_msg,
4760 id_expr_token->location));
4761 if (error_msg)
4762 cp_parser_error (parser, error_msg);
4763 return decl;
4766 /* Anything else is an error. */
4767 default:
4768 cp_parser_error (parser, "expected primary-expression");
4769 return error_mark_node;
4773 static inline tree
4774 cp_parser_primary_expression (cp_parser *parser,
4775 bool address_p,
4776 bool cast_p,
4777 bool template_arg_p,
4778 cp_id_kind *idk)
4780 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4781 /*decltype*/false, idk);
4784 /* Parse an id-expression.
4786 id-expression:
4787 unqualified-id
4788 qualified-id
4790 qualified-id:
4791 :: [opt] nested-name-specifier template [opt] unqualified-id
4792 :: identifier
4793 :: operator-function-id
4794 :: template-id
4796 Return a representation of the unqualified portion of the
4797 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4798 a `::' or nested-name-specifier.
4800 Often, if the id-expression was a qualified-id, the caller will
4801 want to make a SCOPE_REF to represent the qualified-id. This
4802 function does not do this in order to avoid wastefully creating
4803 SCOPE_REFs when they are not required.
4805 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4806 `template' keyword.
4808 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4809 uninstantiated templates.
4811 If *TEMPLATE_P is non-NULL, it is set to true iff the
4812 `template' keyword is used to explicitly indicate that the entity
4813 named is a template.
4815 If DECLARATOR_P is true, the id-expression is appearing as part of
4816 a declarator, rather than as part of an expression. */
4818 static tree
4819 cp_parser_id_expression (cp_parser *parser,
4820 bool template_keyword_p,
4821 bool check_dependency_p,
4822 bool *template_p,
4823 bool declarator_p,
4824 bool optional_p)
4826 bool global_scope_p;
4827 bool nested_name_specifier_p;
4829 /* Assume the `template' keyword was not used. */
4830 if (template_p)
4831 *template_p = template_keyword_p;
4833 /* Look for the optional `::' operator. */
4834 global_scope_p
4835 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4836 != NULL_TREE);
4837 /* Look for the optional nested-name-specifier. */
4838 nested_name_specifier_p
4839 = (cp_parser_nested_name_specifier_opt (parser,
4840 /*typename_keyword_p=*/false,
4841 check_dependency_p,
4842 /*type_p=*/false,
4843 declarator_p)
4844 != NULL_TREE);
4845 /* If there is a nested-name-specifier, then we are looking at
4846 the first qualified-id production. */
4847 if (nested_name_specifier_p)
4849 tree saved_scope;
4850 tree saved_object_scope;
4851 tree saved_qualifying_scope;
4852 tree unqualified_id;
4853 bool is_template;
4855 /* See if the next token is the `template' keyword. */
4856 if (!template_p)
4857 template_p = &is_template;
4858 *template_p = cp_parser_optional_template_keyword (parser);
4859 /* Name lookup we do during the processing of the
4860 unqualified-id might obliterate SCOPE. */
4861 saved_scope = parser->scope;
4862 saved_object_scope = parser->object_scope;
4863 saved_qualifying_scope = parser->qualifying_scope;
4864 /* Process the final unqualified-id. */
4865 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4866 check_dependency_p,
4867 declarator_p,
4868 /*optional_p=*/false);
4869 /* Restore the SAVED_SCOPE for our caller. */
4870 parser->scope = saved_scope;
4871 parser->object_scope = saved_object_scope;
4872 parser->qualifying_scope = saved_qualifying_scope;
4874 return unqualified_id;
4876 /* Otherwise, if we are in global scope, then we are looking at one
4877 of the other qualified-id productions. */
4878 else if (global_scope_p)
4880 cp_token *token;
4881 tree id;
4883 /* Peek at the next token. */
4884 token = cp_lexer_peek_token (parser->lexer);
4886 /* If it's an identifier, and the next token is not a "<", then
4887 we can avoid the template-id case. This is an optimization
4888 for this common case. */
4889 if (token->type == CPP_NAME
4890 && !cp_parser_nth_token_starts_template_argument_list_p
4891 (parser, 2))
4892 return cp_parser_identifier (parser);
4894 cp_parser_parse_tentatively (parser);
4895 /* Try a template-id. */
4896 id = cp_parser_template_id (parser,
4897 /*template_keyword_p=*/false,
4898 /*check_dependency_p=*/true,
4899 none_type,
4900 declarator_p);
4901 /* If that worked, we're done. */
4902 if (cp_parser_parse_definitely (parser))
4903 return id;
4905 /* Peek at the next token. (Changes in the token buffer may
4906 have invalidated the pointer obtained above.) */
4907 token = cp_lexer_peek_token (parser->lexer);
4909 switch (token->type)
4911 case CPP_NAME:
4912 return cp_parser_identifier (parser);
4914 case CPP_KEYWORD:
4915 if (token->keyword == RID_OPERATOR)
4916 return cp_parser_operator_function_id (parser);
4917 /* Fall through. */
4919 default:
4920 cp_parser_error (parser, "expected id-expression");
4921 return error_mark_node;
4924 else
4925 return cp_parser_unqualified_id (parser, template_keyword_p,
4926 /*check_dependency_p=*/true,
4927 declarator_p,
4928 optional_p);
4931 /* Parse an unqualified-id.
4933 unqualified-id:
4934 identifier
4935 operator-function-id
4936 conversion-function-id
4937 ~ class-name
4938 template-id
4940 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4941 keyword, in a construct like `A::template ...'.
4943 Returns a representation of unqualified-id. For the `identifier'
4944 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4945 production a BIT_NOT_EXPR is returned; the operand of the
4946 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4947 other productions, see the documentation accompanying the
4948 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4949 names are looked up in uninstantiated templates. If DECLARATOR_P
4950 is true, the unqualified-id is appearing as part of a declarator,
4951 rather than as part of an expression. */
4953 static tree
4954 cp_parser_unqualified_id (cp_parser* parser,
4955 bool template_keyword_p,
4956 bool check_dependency_p,
4957 bool declarator_p,
4958 bool optional_p)
4960 cp_token *token;
4962 /* Peek at the next token. */
4963 token = cp_lexer_peek_token (parser->lexer);
4965 switch ((int) token->type)
4967 case CPP_NAME:
4969 tree id;
4971 /* We don't know yet whether or not this will be a
4972 template-id. */
4973 cp_parser_parse_tentatively (parser);
4974 /* Try a template-id. */
4975 id = cp_parser_template_id (parser, template_keyword_p,
4976 check_dependency_p,
4977 none_type,
4978 declarator_p);
4979 /* If it worked, we're done. */
4980 if (cp_parser_parse_definitely (parser))
4981 return id;
4982 /* Otherwise, it's an ordinary identifier. */
4983 return cp_parser_identifier (parser);
4986 case CPP_TEMPLATE_ID:
4987 return cp_parser_template_id (parser, template_keyword_p,
4988 check_dependency_p,
4989 none_type,
4990 declarator_p);
4992 case CPP_COMPL:
4994 tree type_decl;
4995 tree qualifying_scope;
4996 tree object_scope;
4997 tree scope;
4998 bool done;
5000 /* Consume the `~' token. */
5001 cp_lexer_consume_token (parser->lexer);
5002 /* Parse the class-name. The standard, as written, seems to
5003 say that:
5005 template <typename T> struct S { ~S (); };
5006 template <typename T> S<T>::~S() {}
5008 is invalid, since `~' must be followed by a class-name, but
5009 `S<T>' is dependent, and so not known to be a class.
5010 That's not right; we need to look in uninstantiated
5011 templates. A further complication arises from:
5013 template <typename T> void f(T t) {
5014 t.T::~T();
5017 Here, it is not possible to look up `T' in the scope of `T'
5018 itself. We must look in both the current scope, and the
5019 scope of the containing complete expression.
5021 Yet another issue is:
5023 struct S {
5024 int S;
5025 ~S();
5028 S::~S() {}
5030 The standard does not seem to say that the `S' in `~S'
5031 should refer to the type `S' and not the data member
5032 `S::S'. */
5034 /* DR 244 says that we look up the name after the "~" in the
5035 same scope as we looked up the qualifying name. That idea
5036 isn't fully worked out; it's more complicated than that. */
5037 scope = parser->scope;
5038 object_scope = parser->object_scope;
5039 qualifying_scope = parser->qualifying_scope;
5041 /* Check for invalid scopes. */
5042 if (scope == error_mark_node)
5044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5045 cp_lexer_consume_token (parser->lexer);
5046 return error_mark_node;
5048 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5051 error_at (token->location,
5052 "scope %qT before %<~%> is not a class-name",
5053 scope);
5054 cp_parser_simulate_error (parser);
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5056 cp_lexer_consume_token (parser->lexer);
5057 return error_mark_node;
5059 gcc_assert (!scope || TYPE_P (scope));
5061 /* If the name is of the form "X::~X" it's OK even if X is a
5062 typedef. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 if (scope
5065 && token->type == CPP_NAME
5066 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5067 != CPP_LESS)
5068 && (token->u.value == TYPE_IDENTIFIER (scope)
5069 || (CLASS_TYPE_P (scope)
5070 && constructor_name_p (token->u.value, scope))))
5072 cp_lexer_consume_token (parser->lexer);
5073 return build_nt (BIT_NOT_EXPR, scope);
5076 /* ~auto means the destructor of whatever the object is. */
5077 if (cp_parser_is_keyword (token, RID_AUTO))
5079 if (cxx_dialect < cxx14)
5080 pedwarn (input_location, 0,
5081 "%<~auto%> only available with "
5082 "-std=c++14 or -std=gnu++14");
5083 cp_lexer_consume_token (parser->lexer);
5084 return build_nt (BIT_NOT_EXPR, make_auto ());
5087 /* If there was an explicit qualification (S::~T), first look
5088 in the scope given by the qualification (i.e., S).
5090 Note: in the calls to cp_parser_class_name below we pass
5091 typename_type so that lookup finds the injected-class-name
5092 rather than the constructor. */
5093 done = false;
5094 type_decl = NULL_TREE;
5095 if (scope)
5097 cp_parser_parse_tentatively (parser);
5098 type_decl = cp_parser_class_name (parser,
5099 /*typename_keyword_p=*/false,
5100 /*template_keyword_p=*/false,
5101 typename_type,
5102 /*check_dependency=*/false,
5103 /*class_head_p=*/false,
5104 declarator_p);
5105 if (cp_parser_parse_definitely (parser))
5106 done = true;
5108 /* In "N::S::~S", look in "N" as well. */
5109 if (!done && scope && qualifying_scope)
5111 cp_parser_parse_tentatively (parser);
5112 parser->scope = qualifying_scope;
5113 parser->object_scope = NULL_TREE;
5114 parser->qualifying_scope = NULL_TREE;
5115 type_decl
5116 = cp_parser_class_name (parser,
5117 /*typename_keyword_p=*/false,
5118 /*template_keyword_p=*/false,
5119 typename_type,
5120 /*check_dependency=*/false,
5121 /*class_head_p=*/false,
5122 declarator_p);
5123 if (cp_parser_parse_definitely (parser))
5124 done = true;
5126 /* In "p->S::~T", look in the scope given by "*p" as well. */
5127 else if (!done && object_scope)
5129 cp_parser_parse_tentatively (parser);
5130 parser->scope = object_scope;
5131 parser->object_scope = NULL_TREE;
5132 parser->qualifying_scope = NULL_TREE;
5133 type_decl
5134 = cp_parser_class_name (parser,
5135 /*typename_keyword_p=*/false,
5136 /*template_keyword_p=*/false,
5137 typename_type,
5138 /*check_dependency=*/false,
5139 /*class_head_p=*/false,
5140 declarator_p);
5141 if (cp_parser_parse_definitely (parser))
5142 done = true;
5144 /* Look in the surrounding context. */
5145 if (!done)
5147 parser->scope = NULL_TREE;
5148 parser->object_scope = NULL_TREE;
5149 parser->qualifying_scope = NULL_TREE;
5150 if (processing_template_decl)
5151 cp_parser_parse_tentatively (parser);
5152 type_decl
5153 = cp_parser_class_name (parser,
5154 /*typename_keyword_p=*/false,
5155 /*template_keyword_p=*/false,
5156 typename_type,
5157 /*check_dependency=*/false,
5158 /*class_head_p=*/false,
5159 declarator_p);
5160 if (processing_template_decl
5161 && ! cp_parser_parse_definitely (parser))
5163 /* We couldn't find a type with this name, so just accept
5164 it and check for a match at instantiation time. */
5165 type_decl = cp_parser_identifier (parser);
5166 if (type_decl != error_mark_node)
5167 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5168 return type_decl;
5171 /* If an error occurred, assume that the name of the
5172 destructor is the same as the name of the qualifying
5173 class. That allows us to keep parsing after running
5174 into ill-formed destructor names. */
5175 if (type_decl == error_mark_node && scope)
5176 return build_nt (BIT_NOT_EXPR, scope);
5177 else if (type_decl == error_mark_node)
5178 return error_mark_node;
5180 /* Check that destructor name and scope match. */
5181 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5184 error_at (token->location,
5185 "declaration of %<~%T%> as member of %qT",
5186 type_decl, scope);
5187 cp_parser_simulate_error (parser);
5188 return error_mark_node;
5191 /* [class.dtor]
5193 A typedef-name that names a class shall not be used as the
5194 identifier in the declarator for a destructor declaration. */
5195 if (declarator_p
5196 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5197 && !DECL_SELF_REFERENCE_P (type_decl)
5198 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5199 error_at (token->location,
5200 "typedef-name %qD used as destructor declarator",
5201 type_decl);
5203 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5206 case CPP_KEYWORD:
5207 if (token->keyword == RID_OPERATOR)
5209 tree id;
5211 /* This could be a template-id, so we try that first. */
5212 cp_parser_parse_tentatively (parser);
5213 /* Try a template-id. */
5214 id = cp_parser_template_id (parser, template_keyword_p,
5215 /*check_dependency_p=*/true,
5216 none_type,
5217 declarator_p);
5218 /* If that worked, we're done. */
5219 if (cp_parser_parse_definitely (parser))
5220 return id;
5221 /* We still don't know whether we're looking at an
5222 operator-function-id or a conversion-function-id. */
5223 cp_parser_parse_tentatively (parser);
5224 /* Try an operator-function-id. */
5225 id = cp_parser_operator_function_id (parser);
5226 /* If that didn't work, try a conversion-function-id. */
5227 if (!cp_parser_parse_definitely (parser))
5228 id = cp_parser_conversion_function_id (parser);
5229 else if (UDLIT_OPER_P (id))
5231 /* 17.6.3.3.5 */
5232 const char *name = UDLIT_OP_SUFFIX (id);
5233 if (name[0] != '_' && !in_system_header_at (input_location)
5234 && declarator_p)
5235 warning (0, "literal operator suffixes not preceded by %<_%>"
5236 " are reserved for future standardization");
5239 return id;
5241 /* Fall through. */
5243 default:
5244 if (optional_p)
5245 return NULL_TREE;
5246 cp_parser_error (parser, "expected unqualified-id");
5247 return error_mark_node;
5251 /* Parse an (optional) nested-name-specifier.
5253 nested-name-specifier: [C++98]
5254 class-or-namespace-name :: nested-name-specifier [opt]
5255 class-or-namespace-name :: template nested-name-specifier [opt]
5257 nested-name-specifier: [C++0x]
5258 type-name ::
5259 namespace-name ::
5260 nested-name-specifier identifier ::
5261 nested-name-specifier template [opt] simple-template-id ::
5263 PARSER->SCOPE should be set appropriately before this function is
5264 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5265 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5266 in name lookups.
5268 Sets PARSER->SCOPE to the class (TYPE) or namespace
5269 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5270 it unchanged if there is no nested-name-specifier. Returns the new
5271 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5273 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5274 part of a declaration and/or decl-specifier. */
5276 static tree
5277 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5278 bool typename_keyword_p,
5279 bool check_dependency_p,
5280 bool type_p,
5281 bool is_declaration)
5283 bool success = false;
5284 cp_token_position start = 0;
5285 cp_token *token;
5287 /* Remember where the nested-name-specifier starts. */
5288 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5290 start = cp_lexer_token_position (parser->lexer, false);
5291 push_deferring_access_checks (dk_deferred);
5294 while (true)
5296 tree new_scope;
5297 tree old_scope;
5298 tree saved_qualifying_scope;
5299 bool template_keyword_p;
5301 /* Spot cases that cannot be the beginning of a
5302 nested-name-specifier. */
5303 token = cp_lexer_peek_token (parser->lexer);
5305 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5306 the already parsed nested-name-specifier. */
5307 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5309 /* Grab the nested-name-specifier and continue the loop. */
5310 cp_parser_pre_parsed_nested_name_specifier (parser);
5311 /* If we originally encountered this nested-name-specifier
5312 with IS_DECLARATION set to false, we will not have
5313 resolved TYPENAME_TYPEs, so we must do so here. */
5314 if (is_declaration
5315 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5317 new_scope = resolve_typename_type (parser->scope,
5318 /*only_current_p=*/false);
5319 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5320 parser->scope = new_scope;
5322 success = true;
5323 continue;
5326 /* Spot cases that cannot be the beginning of a
5327 nested-name-specifier. On the second and subsequent times
5328 through the loop, we look for the `template' keyword. */
5329 if (success && token->keyword == RID_TEMPLATE)
5331 /* A template-id can start a nested-name-specifier. */
5332 else if (token->type == CPP_TEMPLATE_ID)
5334 /* DR 743: decltype can be used in a nested-name-specifier. */
5335 else if (token_is_decltype (token))
5337 else
5339 /* If the next token is not an identifier, then it is
5340 definitely not a type-name or namespace-name. */
5341 if (token->type != CPP_NAME)
5342 break;
5343 /* If the following token is neither a `<' (to begin a
5344 template-id), nor a `::', then we are not looking at a
5345 nested-name-specifier. */
5346 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5348 if (token->type == CPP_COLON
5349 && parser->colon_corrects_to_scope_p
5350 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5352 error_at (token->location,
5353 "found %<:%> in nested-name-specifier, expected %<::%>");
5354 token->type = CPP_SCOPE;
5357 if (token->type != CPP_SCOPE
5358 && !cp_parser_nth_token_starts_template_argument_list_p
5359 (parser, 2))
5360 break;
5363 /* The nested-name-specifier is optional, so we parse
5364 tentatively. */
5365 cp_parser_parse_tentatively (parser);
5367 /* Look for the optional `template' keyword, if this isn't the
5368 first time through the loop. */
5369 if (success)
5370 template_keyword_p = cp_parser_optional_template_keyword (parser);
5371 else
5372 template_keyword_p = false;
5374 /* Save the old scope since the name lookup we are about to do
5375 might destroy it. */
5376 old_scope = parser->scope;
5377 saved_qualifying_scope = parser->qualifying_scope;
5378 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5379 look up names in "X<T>::I" in order to determine that "Y" is
5380 a template. So, if we have a typename at this point, we make
5381 an effort to look through it. */
5382 if (is_declaration
5383 && !typename_keyword_p
5384 && parser->scope
5385 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5386 parser->scope = resolve_typename_type (parser->scope,
5387 /*only_current_p=*/false);
5388 /* Parse the qualifying entity. */
5389 new_scope
5390 = cp_parser_qualifying_entity (parser,
5391 typename_keyword_p,
5392 template_keyword_p,
5393 check_dependency_p,
5394 type_p,
5395 is_declaration);
5396 /* Look for the `::' token. */
5397 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5399 /* If we found what we wanted, we keep going; otherwise, we're
5400 done. */
5401 if (!cp_parser_parse_definitely (parser))
5403 bool error_p = false;
5405 /* Restore the OLD_SCOPE since it was valid before the
5406 failed attempt at finding the last
5407 class-or-namespace-name. */
5408 parser->scope = old_scope;
5409 parser->qualifying_scope = saved_qualifying_scope;
5411 /* If the next token is a decltype, and the one after that is a
5412 `::', then the decltype has failed to resolve to a class or
5413 enumeration type. Give this error even when parsing
5414 tentatively since it can't possibly be valid--and we're going
5415 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5416 won't get another chance.*/
5417 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5418 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5419 == CPP_SCOPE))
5421 token = cp_lexer_consume_token (parser->lexer);
5422 error_at (token->location, "decltype evaluates to %qT, "
5423 "which is not a class or enumeration type",
5424 token->u.value);
5425 parser->scope = error_mark_node;
5426 error_p = true;
5427 /* As below. */
5428 success = true;
5429 cp_lexer_consume_token (parser->lexer);
5432 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5433 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5435 /* If we have a non-type template-id followed by ::, it can't
5436 possibly be valid. */
5437 token = cp_lexer_peek_token (parser->lexer);
5438 tree tid = token->u.tree_check_value->value;
5439 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5440 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5442 tree tmpl = NULL_TREE;
5443 if (is_overloaded_fn (tid))
5445 tree fns = get_fns (tid);
5446 if (!OVL_CHAIN (fns))
5447 tmpl = OVL_CURRENT (fns);
5448 error_at (token->location, "function template-id %qD "
5449 "in nested-name-specifier", tid);
5451 else
5453 /* Variable template. */
5454 tmpl = TREE_OPERAND (tid, 0);
5455 gcc_assert (variable_template_p (tmpl));
5456 error_at (token->location, "variable template-id %qD "
5457 "in nested-name-specifier", tid);
5459 if (tmpl)
5460 inform (DECL_SOURCE_LOCATION (tmpl),
5461 "%qD declared here", tmpl);
5463 parser->scope = error_mark_node;
5464 error_p = true;
5465 /* As below. */
5466 success = true;
5467 cp_lexer_consume_token (parser->lexer);
5468 cp_lexer_consume_token (parser->lexer);
5472 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5473 break;
5474 /* If the next token is an identifier, and the one after
5475 that is a `::', then any valid interpretation would have
5476 found a class-or-namespace-name. */
5477 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5478 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5479 == CPP_SCOPE)
5480 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5481 != CPP_COMPL))
5483 token = cp_lexer_consume_token (parser->lexer);
5484 if (!error_p)
5486 if (!token->error_reported)
5488 tree decl;
5489 tree ambiguous_decls;
5491 decl = cp_parser_lookup_name (parser, token->u.value,
5492 none_type,
5493 /*is_template=*/false,
5494 /*is_namespace=*/false,
5495 /*check_dependency=*/true,
5496 &ambiguous_decls,
5497 token->location);
5498 if (TREE_CODE (decl) == TEMPLATE_DECL)
5499 error_at (token->location,
5500 "%qD used without template parameters",
5501 decl);
5502 else if (ambiguous_decls)
5504 // cp_parser_lookup_name has the same diagnostic,
5505 // thus make sure to emit it at most once.
5506 if (cp_parser_uncommitted_to_tentative_parse_p
5507 (parser))
5509 error_at (token->location,
5510 "reference to %qD is ambiguous",
5511 token->u.value);
5512 print_candidates (ambiguous_decls);
5514 decl = error_mark_node;
5516 else
5518 if (cxx_dialect != cxx98)
5519 cp_parser_name_lookup_error
5520 (parser, token->u.value, decl, NLE_NOT_CXX98,
5521 token->location);
5522 else
5523 cp_parser_name_lookup_error
5524 (parser, token->u.value, decl, NLE_CXX98,
5525 token->location);
5528 parser->scope = error_mark_node;
5529 error_p = true;
5530 /* Treat this as a successful nested-name-specifier
5531 due to:
5533 [basic.lookup.qual]
5535 If the name found is not a class-name (clause
5536 _class_) or namespace-name (_namespace.def_), the
5537 program is ill-formed. */
5538 success = true;
5540 cp_lexer_consume_token (parser->lexer);
5542 break;
5544 /* We've found one valid nested-name-specifier. */
5545 success = true;
5546 /* Name lookup always gives us a DECL. */
5547 if (TREE_CODE (new_scope) == TYPE_DECL)
5548 new_scope = TREE_TYPE (new_scope);
5549 /* Uses of "template" must be followed by actual templates. */
5550 if (template_keyword_p
5551 && !(CLASS_TYPE_P (new_scope)
5552 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5553 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5554 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5555 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5556 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5557 == TEMPLATE_ID_EXPR)))
5558 permerror (input_location, TYPE_P (new_scope)
5559 ? G_("%qT is not a template")
5560 : G_("%qD is not a template"),
5561 new_scope);
5562 /* If it is a class scope, try to complete it; we are about to
5563 be looking up names inside the class. */
5564 if (TYPE_P (new_scope)
5565 /* Since checking types for dependency can be expensive,
5566 avoid doing it if the type is already complete. */
5567 && !COMPLETE_TYPE_P (new_scope)
5568 /* Do not try to complete dependent types. */
5569 && !dependent_type_p (new_scope))
5571 new_scope = complete_type (new_scope);
5572 /* If it is a typedef to current class, use the current
5573 class instead, as the typedef won't have any names inside
5574 it yet. */
5575 if (!COMPLETE_TYPE_P (new_scope)
5576 && currently_open_class (new_scope))
5577 new_scope = TYPE_MAIN_VARIANT (new_scope);
5579 /* Make sure we look in the right scope the next time through
5580 the loop. */
5581 parser->scope = new_scope;
5584 /* If parsing tentatively, replace the sequence of tokens that makes
5585 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5586 token. That way, should we re-parse the token stream, we will
5587 not have to repeat the effort required to do the parse, nor will
5588 we issue duplicate error messages. */
5589 if (success && start)
5591 cp_token *token;
5593 token = cp_lexer_token_at (parser->lexer, start);
5594 /* Reset the contents of the START token. */
5595 token->type = CPP_NESTED_NAME_SPECIFIER;
5596 /* Retrieve any deferred checks. Do not pop this access checks yet
5597 so the memory will not be reclaimed during token replacing below. */
5598 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5599 token->u.tree_check_value->value = parser->scope;
5600 token->u.tree_check_value->checks = get_deferred_access_checks ();
5601 token->u.tree_check_value->qualifying_scope =
5602 parser->qualifying_scope;
5603 token->keyword = RID_MAX;
5605 /* Purge all subsequent tokens. */
5606 cp_lexer_purge_tokens_after (parser->lexer, start);
5609 if (start)
5610 pop_to_parent_deferring_access_checks ();
5612 return success ? parser->scope : NULL_TREE;
5615 /* Parse a nested-name-specifier. See
5616 cp_parser_nested_name_specifier_opt for details. This function
5617 behaves identically, except that it will an issue an error if no
5618 nested-name-specifier is present. */
5620 static tree
5621 cp_parser_nested_name_specifier (cp_parser *parser,
5622 bool typename_keyword_p,
5623 bool check_dependency_p,
5624 bool type_p,
5625 bool is_declaration)
5627 tree scope;
5629 /* Look for the nested-name-specifier. */
5630 scope = cp_parser_nested_name_specifier_opt (parser,
5631 typename_keyword_p,
5632 check_dependency_p,
5633 type_p,
5634 is_declaration);
5635 /* If it was not present, issue an error message. */
5636 if (!scope)
5638 cp_parser_error (parser, "expected nested-name-specifier");
5639 parser->scope = NULL_TREE;
5642 return scope;
5645 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5646 this is either a class-name or a namespace-name (which corresponds
5647 to the class-or-namespace-name production in the grammar). For
5648 C++0x, it can also be a type-name that refers to an enumeration
5649 type or a simple-template-id.
5651 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5652 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5653 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5654 TYPE_P is TRUE iff the next name should be taken as a class-name,
5655 even the same name is declared to be another entity in the same
5656 scope.
5658 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5659 specified by the class-or-namespace-name. If neither is found the
5660 ERROR_MARK_NODE is returned. */
5662 static tree
5663 cp_parser_qualifying_entity (cp_parser *parser,
5664 bool typename_keyword_p,
5665 bool template_keyword_p,
5666 bool check_dependency_p,
5667 bool type_p,
5668 bool is_declaration)
5670 tree saved_scope;
5671 tree saved_qualifying_scope;
5672 tree saved_object_scope;
5673 tree scope;
5674 bool only_class_p;
5675 bool successful_parse_p;
5677 /* DR 743: decltype can appear in a nested-name-specifier. */
5678 if (cp_lexer_next_token_is_decltype (parser->lexer))
5680 scope = cp_parser_decltype (parser);
5681 if (TREE_CODE (scope) != ENUMERAL_TYPE
5682 && !MAYBE_CLASS_TYPE_P (scope))
5684 cp_parser_simulate_error (parser);
5685 return error_mark_node;
5687 if (TYPE_NAME (scope))
5688 scope = TYPE_NAME (scope);
5689 return scope;
5692 /* Before we try to parse the class-name, we must save away the
5693 current PARSER->SCOPE since cp_parser_class_name will destroy
5694 it. */
5695 saved_scope = parser->scope;
5696 saved_qualifying_scope = parser->qualifying_scope;
5697 saved_object_scope = parser->object_scope;
5698 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5699 there is no need to look for a namespace-name. */
5700 only_class_p = template_keyword_p
5701 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5702 if (!only_class_p)
5703 cp_parser_parse_tentatively (parser);
5704 scope = cp_parser_class_name (parser,
5705 typename_keyword_p,
5706 template_keyword_p,
5707 type_p ? class_type : none_type,
5708 check_dependency_p,
5709 /*class_head_p=*/false,
5710 is_declaration,
5711 /*enum_ok=*/cxx_dialect > cxx98);
5712 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5713 /* If that didn't work, try for a namespace-name. */
5714 if (!only_class_p && !successful_parse_p)
5716 /* Restore the saved scope. */
5717 parser->scope = saved_scope;
5718 parser->qualifying_scope = saved_qualifying_scope;
5719 parser->object_scope = saved_object_scope;
5720 /* If we are not looking at an identifier followed by the scope
5721 resolution operator, then this is not part of a
5722 nested-name-specifier. (Note that this function is only used
5723 to parse the components of a nested-name-specifier.) */
5724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5725 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5726 return error_mark_node;
5727 scope = cp_parser_namespace_name (parser);
5730 return scope;
5733 /* Return true if we are looking at a compound-literal, false otherwise. */
5735 static bool
5736 cp_parser_compound_literal_p (cp_parser *parser)
5738 /* Consume the `('. */
5739 cp_lexer_consume_token (parser->lexer);
5741 cp_lexer_save_tokens (parser->lexer);
5743 /* Skip tokens until the next token is a closing parenthesis.
5744 If we find the closing `)', and the next token is a `{', then
5745 we are looking at a compound-literal. */
5746 bool compound_literal_p
5747 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5748 /*consume_paren=*/true)
5749 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5751 /* Roll back the tokens we skipped. */
5752 cp_lexer_rollback_tokens (parser->lexer);
5754 return compound_literal_p;
5757 /* Parse a postfix-expression.
5759 postfix-expression:
5760 primary-expression
5761 postfix-expression [ expression ]
5762 postfix-expression ( expression-list [opt] )
5763 simple-type-specifier ( expression-list [opt] )
5764 typename :: [opt] nested-name-specifier identifier
5765 ( expression-list [opt] )
5766 typename :: [opt] nested-name-specifier template [opt] template-id
5767 ( expression-list [opt] )
5768 postfix-expression . template [opt] id-expression
5769 postfix-expression -> template [opt] id-expression
5770 postfix-expression . pseudo-destructor-name
5771 postfix-expression -> pseudo-destructor-name
5772 postfix-expression ++
5773 postfix-expression --
5774 dynamic_cast < type-id > ( expression )
5775 static_cast < type-id > ( expression )
5776 reinterpret_cast < type-id > ( expression )
5777 const_cast < type-id > ( expression )
5778 typeid ( expression )
5779 typeid ( type-id )
5781 GNU Extension:
5783 postfix-expression:
5784 ( type-id ) { initializer-list , [opt] }
5786 This extension is a GNU version of the C99 compound-literal
5787 construct. (The C99 grammar uses `type-name' instead of `type-id',
5788 but they are essentially the same concept.)
5790 If ADDRESS_P is true, the postfix expression is the operand of the
5791 `&' operator. CAST_P is true if this expression is the target of a
5792 cast.
5794 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5795 class member access expressions [expr.ref].
5797 Returns a representation of the expression. */
5799 static tree
5800 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5801 bool member_access_only_p, bool decltype_p,
5802 cp_id_kind * pidk_return)
5804 cp_token *token;
5805 location_t loc;
5806 enum rid keyword;
5807 cp_id_kind idk = CP_ID_KIND_NONE;
5808 tree postfix_expression = NULL_TREE;
5809 bool is_member_access = false;
5810 int saved_in_statement = -1;
5812 /* Peek at the next token. */
5813 token = cp_lexer_peek_token (parser->lexer);
5814 loc = token->location;
5815 /* Some of the productions are determined by keywords. */
5816 keyword = token->keyword;
5817 switch (keyword)
5819 case RID_DYNCAST:
5820 case RID_STATCAST:
5821 case RID_REINTCAST:
5822 case RID_CONSTCAST:
5824 tree type;
5825 tree expression;
5826 const char *saved_message;
5827 bool saved_in_type_id_in_expr_p;
5829 /* All of these can be handled in the same way from the point
5830 of view of parsing. Begin by consuming the token
5831 identifying the cast. */
5832 cp_lexer_consume_token (parser->lexer);
5834 /* New types cannot be defined in the cast. */
5835 saved_message = parser->type_definition_forbidden_message;
5836 parser->type_definition_forbidden_message
5837 = G_("types may not be defined in casts");
5839 /* Look for the opening `<'. */
5840 cp_parser_require (parser, CPP_LESS, RT_LESS);
5841 /* Parse the type to which we are casting. */
5842 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5843 parser->in_type_id_in_expr_p = true;
5844 type = cp_parser_type_id (parser);
5845 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5846 /* Look for the closing `>'. */
5847 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5848 /* Restore the old message. */
5849 parser->type_definition_forbidden_message = saved_message;
5851 bool saved_greater_than_is_operator_p
5852 = parser->greater_than_is_operator_p;
5853 parser->greater_than_is_operator_p = true;
5855 /* And the expression which is being cast. */
5856 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5857 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5860 parser->greater_than_is_operator_p
5861 = saved_greater_than_is_operator_p;
5863 /* Only type conversions to integral or enumeration types
5864 can be used in constant-expressions. */
5865 if (!cast_valid_in_integral_constant_expression_p (type)
5866 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5867 return error_mark_node;
5869 switch (keyword)
5871 case RID_DYNCAST:
5872 postfix_expression
5873 = build_dynamic_cast (type, expression, tf_warning_or_error);
5874 break;
5875 case RID_STATCAST:
5876 postfix_expression
5877 = build_static_cast (type, expression, tf_warning_or_error);
5878 break;
5879 case RID_REINTCAST:
5880 postfix_expression
5881 = build_reinterpret_cast (type, expression,
5882 tf_warning_or_error);
5883 break;
5884 case RID_CONSTCAST:
5885 postfix_expression
5886 = build_const_cast (type, expression, tf_warning_or_error);
5887 break;
5888 default:
5889 gcc_unreachable ();
5892 break;
5894 case RID_TYPEID:
5896 tree type;
5897 const char *saved_message;
5898 bool saved_in_type_id_in_expr_p;
5900 /* Consume the `typeid' token. */
5901 cp_lexer_consume_token (parser->lexer);
5902 /* Look for the `(' token. */
5903 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5904 /* Types cannot be defined in a `typeid' expression. */
5905 saved_message = parser->type_definition_forbidden_message;
5906 parser->type_definition_forbidden_message
5907 = G_("types may not be defined in a %<typeid%> expression");
5908 /* We can't be sure yet whether we're looking at a type-id or an
5909 expression. */
5910 cp_parser_parse_tentatively (parser);
5911 /* Try a type-id first. */
5912 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5913 parser->in_type_id_in_expr_p = true;
5914 type = cp_parser_type_id (parser);
5915 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5916 /* Look for the `)' token. Otherwise, we can't be sure that
5917 we're not looking at an expression: consider `typeid (int
5918 (3))', for example. */
5919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5920 /* If all went well, simply lookup the type-id. */
5921 if (cp_parser_parse_definitely (parser))
5922 postfix_expression = get_typeid (type, tf_warning_or_error);
5923 /* Otherwise, fall back to the expression variant. */
5924 else
5926 tree expression;
5928 /* Look for an expression. */
5929 expression = cp_parser_expression (parser, & idk);
5930 /* Compute its typeid. */
5931 postfix_expression = build_typeid (expression, tf_warning_or_error);
5932 /* Look for the `)' token. */
5933 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5935 /* Restore the saved message. */
5936 parser->type_definition_forbidden_message = saved_message;
5937 /* `typeid' may not appear in an integral constant expression. */
5938 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5939 return error_mark_node;
5941 break;
5943 case RID_TYPENAME:
5945 tree type;
5946 /* The syntax permitted here is the same permitted for an
5947 elaborated-type-specifier. */
5948 type = cp_parser_elaborated_type_specifier (parser,
5949 /*is_friend=*/false,
5950 /*is_declaration=*/false);
5951 postfix_expression = cp_parser_functional_cast (parser, type);
5953 break;
5955 case RID_CILK_SPAWN:
5957 cp_lexer_consume_token (parser->lexer);
5958 token = cp_lexer_peek_token (parser->lexer);
5959 if (token->type == CPP_SEMICOLON)
5961 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5962 "an expression");
5963 postfix_expression = error_mark_node;
5964 break;
5966 else if (!current_function_decl)
5968 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5969 "inside a function");
5970 postfix_expression = error_mark_node;
5971 break;
5973 else
5975 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5976 saved_in_statement = parser->in_statement;
5977 parser->in_statement |= IN_CILK_SPAWN;
5979 cfun->calls_cilk_spawn = 1;
5980 postfix_expression =
5981 cp_parser_postfix_expression (parser, false, false,
5982 false, false, &idk);
5983 if (!flag_cilkplus)
5985 error_at (token->location, "-fcilkplus must be enabled to use"
5986 " %<_Cilk_spawn%>");
5987 cfun->calls_cilk_spawn = 0;
5989 else if (saved_in_statement & IN_CILK_SPAWN)
5991 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5992 "are not permitted");
5993 postfix_expression = error_mark_node;
5994 cfun->calls_cilk_spawn = 0;
5996 else
5998 postfix_expression = build_cilk_spawn (token->location,
5999 postfix_expression);
6000 if (postfix_expression != error_mark_node)
6001 SET_EXPR_LOCATION (postfix_expression, input_location);
6002 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6004 break;
6007 case RID_BUILTIN_SHUFFLE:
6009 vec<tree, va_gc> *vec;
6010 unsigned int i;
6011 tree p;
6013 cp_lexer_consume_token (parser->lexer);
6014 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6015 /*cast_p=*/false, /*allow_expansion_p=*/true,
6016 /*non_constant_p=*/NULL);
6017 if (vec == NULL)
6018 return error_mark_node;
6020 FOR_EACH_VEC_ELT (*vec, i, p)
6021 mark_exp_read (p);
6023 if (vec->length () == 2)
6024 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6025 tf_warning_or_error);
6026 else if (vec->length () == 3)
6027 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6028 tf_warning_or_error);
6029 else
6031 error_at (loc, "wrong number of arguments to "
6032 "%<__builtin_shuffle%>");
6033 return error_mark_node;
6035 break;
6038 default:
6040 tree type;
6042 /* If the next thing is a simple-type-specifier, we may be
6043 looking at a functional cast. We could also be looking at
6044 an id-expression. So, we try the functional cast, and if
6045 that doesn't work we fall back to the primary-expression. */
6046 cp_parser_parse_tentatively (parser);
6047 /* Look for the simple-type-specifier. */
6048 type = cp_parser_simple_type_specifier (parser,
6049 /*decl_specs=*/NULL,
6050 CP_PARSER_FLAGS_NONE);
6051 /* Parse the cast itself. */
6052 if (!cp_parser_error_occurred (parser))
6053 postfix_expression
6054 = cp_parser_functional_cast (parser, type);
6055 /* If that worked, we're done. */
6056 if (cp_parser_parse_definitely (parser))
6057 break;
6059 /* If the functional-cast didn't work out, try a
6060 compound-literal. */
6061 if (cp_parser_allow_gnu_extensions_p (parser)
6062 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6064 tree initializer = NULL_TREE;
6066 cp_parser_parse_tentatively (parser);
6068 /* Avoid calling cp_parser_type_id pointlessly, see comment
6069 in cp_parser_cast_expression about c++/29234. */
6070 if (!cp_parser_compound_literal_p (parser))
6071 cp_parser_simulate_error (parser);
6072 else
6074 /* Parse the type. */
6075 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6076 parser->in_type_id_in_expr_p = true;
6077 type = cp_parser_type_id (parser);
6078 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6079 /* Look for the `)'. */
6080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6083 /* If things aren't going well, there's no need to
6084 keep going. */
6085 if (!cp_parser_error_occurred (parser))
6087 bool non_constant_p;
6088 /* Parse the brace-enclosed initializer list. */
6089 initializer = cp_parser_braced_list (parser,
6090 &non_constant_p);
6092 /* If that worked, we're definitely looking at a
6093 compound-literal expression. */
6094 if (cp_parser_parse_definitely (parser))
6096 /* Warn the user that a compound literal is not
6097 allowed in standard C++. */
6098 pedwarn (input_location, OPT_Wpedantic,
6099 "ISO C++ forbids compound-literals");
6100 /* For simplicity, we disallow compound literals in
6101 constant-expressions. We could
6102 allow compound literals of integer type, whose
6103 initializer was a constant, in constant
6104 expressions. Permitting that usage, as a further
6105 extension, would not change the meaning of any
6106 currently accepted programs. (Of course, as
6107 compound literals are not part of ISO C++, the
6108 standard has nothing to say.) */
6109 if (cp_parser_non_integral_constant_expression (parser,
6110 NIC_NCC))
6112 postfix_expression = error_mark_node;
6113 break;
6115 /* Form the representation of the compound-literal. */
6116 postfix_expression
6117 = finish_compound_literal (type, initializer,
6118 tf_warning_or_error);
6119 break;
6123 /* It must be a primary-expression. */
6124 postfix_expression
6125 = cp_parser_primary_expression (parser, address_p, cast_p,
6126 /*template_arg_p=*/false,
6127 decltype_p,
6128 &idk);
6130 break;
6133 /* Note that we don't need to worry about calling build_cplus_new on a
6134 class-valued CALL_EXPR in decltype when it isn't the end of the
6135 postfix-expression; unary_complex_lvalue will take care of that for
6136 all these cases. */
6138 /* Keep looping until the postfix-expression is complete. */
6139 while (true)
6141 if (idk == CP_ID_KIND_UNQUALIFIED
6142 && identifier_p (postfix_expression)
6143 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6144 /* It is not a Koenig lookup function call. */
6145 postfix_expression
6146 = unqualified_name_lookup_error (postfix_expression);
6148 /* Peek at the next token. */
6149 token = cp_lexer_peek_token (parser->lexer);
6151 switch (token->type)
6153 case CPP_OPEN_SQUARE:
6154 if (cp_next_tokens_can_be_std_attribute_p (parser))
6156 cp_parser_error (parser,
6157 "two consecutive %<[%> shall "
6158 "only introduce an attribute");
6159 return error_mark_node;
6161 postfix_expression
6162 = cp_parser_postfix_open_square_expression (parser,
6163 postfix_expression,
6164 false,
6165 decltype_p);
6166 idk = CP_ID_KIND_NONE;
6167 is_member_access = false;
6168 break;
6170 case CPP_OPEN_PAREN:
6171 /* postfix-expression ( expression-list [opt] ) */
6173 bool koenig_p;
6174 bool is_builtin_constant_p;
6175 bool saved_integral_constant_expression_p = false;
6176 bool saved_non_integral_constant_expression_p = false;
6177 tsubst_flags_t complain = complain_flags (decltype_p);
6178 vec<tree, va_gc> *args;
6180 is_member_access = false;
6182 is_builtin_constant_p
6183 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6184 if (is_builtin_constant_p)
6186 /* The whole point of __builtin_constant_p is to allow
6187 non-constant expressions to appear as arguments. */
6188 saved_integral_constant_expression_p
6189 = parser->integral_constant_expression_p;
6190 saved_non_integral_constant_expression_p
6191 = parser->non_integral_constant_expression_p;
6192 parser->integral_constant_expression_p = false;
6194 args = (cp_parser_parenthesized_expression_list
6195 (parser, non_attr,
6196 /*cast_p=*/false, /*allow_expansion_p=*/true,
6197 /*non_constant_p=*/NULL,
6198 /*want_literal_zero_p=*/warn_memset_transposed_args));
6199 if (is_builtin_constant_p)
6201 parser->integral_constant_expression_p
6202 = saved_integral_constant_expression_p;
6203 parser->non_integral_constant_expression_p
6204 = saved_non_integral_constant_expression_p;
6207 if (args == NULL)
6209 postfix_expression = error_mark_node;
6210 break;
6213 /* Function calls are not permitted in
6214 constant-expressions. */
6215 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6216 && cp_parser_non_integral_constant_expression (parser,
6217 NIC_FUNC_CALL))
6219 postfix_expression = error_mark_node;
6220 release_tree_vector (args);
6221 break;
6224 koenig_p = false;
6225 if (idk == CP_ID_KIND_UNQUALIFIED
6226 || idk == CP_ID_KIND_TEMPLATE_ID)
6228 if (identifier_p (postfix_expression))
6230 if (!args->is_empty ())
6232 koenig_p = true;
6233 if (!any_type_dependent_arguments_p (args))
6234 postfix_expression
6235 = perform_koenig_lookup (postfix_expression, args,
6236 complain);
6238 else
6239 postfix_expression
6240 = unqualified_fn_lookup_error (postfix_expression);
6242 /* We do not perform argument-dependent lookup if
6243 normal lookup finds a non-function, in accordance
6244 with the expected resolution of DR 218. */
6245 else if (!args->is_empty ()
6246 && is_overloaded_fn (postfix_expression))
6248 tree fn = get_first_fn (postfix_expression);
6249 fn = STRIP_TEMPLATE (fn);
6251 /* Do not do argument dependent lookup if regular
6252 lookup finds a member function or a block-scope
6253 function declaration. [basic.lookup.argdep]/3 */
6254 if (!DECL_FUNCTION_MEMBER_P (fn)
6255 && !DECL_LOCAL_FUNCTION_P (fn))
6257 koenig_p = true;
6258 if (!any_type_dependent_arguments_p (args))
6259 postfix_expression
6260 = perform_koenig_lookup (postfix_expression, args,
6261 complain);
6266 if (warn_memset_transposed_args)
6268 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6269 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6270 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6271 && vec_safe_length (args) == 3
6272 && integer_zerop ((*args)[2])
6273 && LITERAL_ZERO_P ((*args)[2])
6274 && !(integer_zerop ((*args)[1])
6275 && LITERAL_ZERO_P ((*args)[1])))
6276 warning (OPT_Wmemset_transposed_args,
6277 "%<memset%> used with constant zero length "
6278 "parameter; this could be due to transposed "
6279 "parameters");
6281 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6282 to avoid leaking those into folder and middle-end. */
6283 unsigned int i;
6284 tree arg;
6285 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6286 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6287 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6290 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6292 tree instance = TREE_OPERAND (postfix_expression, 0);
6293 tree fn = TREE_OPERAND (postfix_expression, 1);
6295 if (processing_template_decl
6296 && (type_dependent_expression_p (instance)
6297 || (!BASELINK_P (fn)
6298 && TREE_CODE (fn) != FIELD_DECL)
6299 || type_dependent_expression_p (fn)
6300 || any_type_dependent_arguments_p (args)))
6302 postfix_expression
6303 = build_nt_call_vec (postfix_expression, args);
6304 release_tree_vector (args);
6305 break;
6308 if (BASELINK_P (fn))
6310 postfix_expression
6311 = (build_new_method_call
6312 (instance, fn, &args, NULL_TREE,
6313 (idk == CP_ID_KIND_QUALIFIED
6314 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6315 : LOOKUP_NORMAL),
6316 /*fn_p=*/NULL,
6317 complain));
6319 else
6320 postfix_expression
6321 = finish_call_expr (postfix_expression, &args,
6322 /*disallow_virtual=*/false,
6323 /*koenig_p=*/false,
6324 complain);
6326 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6327 || TREE_CODE (postfix_expression) == MEMBER_REF
6328 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6329 postfix_expression = (build_offset_ref_call_from_tree
6330 (postfix_expression, &args,
6331 complain));
6332 else if (idk == CP_ID_KIND_QUALIFIED)
6333 /* A call to a static class member, or a namespace-scope
6334 function. */
6335 postfix_expression
6336 = finish_call_expr (postfix_expression, &args,
6337 /*disallow_virtual=*/true,
6338 koenig_p,
6339 complain);
6340 else
6341 /* All other function calls. */
6342 postfix_expression
6343 = finish_call_expr (postfix_expression, &args,
6344 /*disallow_virtual=*/false,
6345 koenig_p,
6346 complain);
6348 protected_set_expr_location (postfix_expression, token->location);
6350 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6351 idk = CP_ID_KIND_NONE;
6353 release_tree_vector (args);
6355 break;
6357 case CPP_DOT:
6358 case CPP_DEREF:
6359 /* postfix-expression . template [opt] id-expression
6360 postfix-expression . pseudo-destructor-name
6361 postfix-expression -> template [opt] id-expression
6362 postfix-expression -> pseudo-destructor-name */
6364 /* Consume the `.' or `->' operator. */
6365 cp_lexer_consume_token (parser->lexer);
6367 postfix_expression
6368 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6369 postfix_expression,
6370 false, &idk, loc);
6372 is_member_access = true;
6373 break;
6375 case CPP_PLUS_PLUS:
6376 /* postfix-expression ++ */
6377 /* Consume the `++' token. */
6378 cp_lexer_consume_token (parser->lexer);
6379 /* Generate a representation for the complete expression. */
6380 postfix_expression
6381 = finish_increment_expr (postfix_expression,
6382 POSTINCREMENT_EXPR);
6383 /* Increments may not appear in constant-expressions. */
6384 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6385 postfix_expression = error_mark_node;
6386 idk = CP_ID_KIND_NONE;
6387 is_member_access = false;
6388 break;
6390 case CPP_MINUS_MINUS:
6391 /* postfix-expression -- */
6392 /* Consume the `--' token. */
6393 cp_lexer_consume_token (parser->lexer);
6394 /* Generate a representation for the complete expression. */
6395 postfix_expression
6396 = finish_increment_expr (postfix_expression,
6397 POSTDECREMENT_EXPR);
6398 /* Decrements may not appear in constant-expressions. */
6399 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6400 postfix_expression = error_mark_node;
6401 idk = CP_ID_KIND_NONE;
6402 is_member_access = false;
6403 break;
6405 default:
6406 if (pidk_return != NULL)
6407 * pidk_return = idk;
6408 if (member_access_only_p)
6409 return is_member_access? postfix_expression : error_mark_node;
6410 else
6411 return postfix_expression;
6415 /* We should never get here. */
6416 gcc_unreachable ();
6417 return error_mark_node;
6420 /* This function parses Cilk Plus array notations. If a normal array expr. is
6421 parsed then the array index is passed back to the caller through *INIT_INDEX
6422 and the function returns a NULL_TREE. If array notation expr. is parsed,
6423 then *INIT_INDEX is ignored by the caller and the function returns
6424 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6425 error_mark_node. */
6427 static tree
6428 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6429 tree array_value)
6431 cp_token *token = NULL;
6432 tree length_index, stride = NULL_TREE, value_tree, array_type;
6433 if (!array_value || array_value == error_mark_node)
6435 cp_parser_skip_to_end_of_statement (parser);
6436 return error_mark_node;
6439 array_type = TREE_TYPE (array_value);
6441 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6442 parser->colon_corrects_to_scope_p = false;
6443 token = cp_lexer_peek_token (parser->lexer);
6445 if (!token)
6447 cp_parser_error (parser, "expected %<:%> or numeral");
6448 return error_mark_node;
6450 else if (token->type == CPP_COLON)
6452 /* Consume the ':'. */
6453 cp_lexer_consume_token (parser->lexer);
6455 /* If we are here, then we have a case like this A[:]. */
6456 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6458 cp_parser_error (parser, "expected %<]%>");
6459 cp_parser_skip_to_end_of_statement (parser);
6460 return error_mark_node;
6462 *init_index = NULL_TREE;
6463 stride = NULL_TREE;
6464 length_index = NULL_TREE;
6466 else
6468 /* If we are here, then there are three valid possibilities:
6469 1. ARRAY [ EXP ]
6470 2. ARRAY [ EXP : EXP ]
6471 3. ARRAY [ EXP : EXP : EXP ] */
6473 *init_index = cp_parser_expression (parser);
6474 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6476 /* This indicates that we have a normal array expression. */
6477 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6478 return NULL_TREE;
6481 /* Consume the ':'. */
6482 cp_lexer_consume_token (parser->lexer);
6483 length_index = cp_parser_expression (parser);
6484 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6486 cp_lexer_consume_token (parser->lexer);
6487 stride = cp_parser_expression (parser);
6490 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6492 if (*init_index == error_mark_node || length_index == error_mark_node
6493 || stride == error_mark_node || array_type == error_mark_node)
6495 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6496 cp_lexer_consume_token (parser->lexer);
6497 return error_mark_node;
6499 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6501 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6502 length_index, stride, array_type);
6503 return value_tree;
6506 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6507 by cp_parser_builtin_offsetof. We're looking for
6509 postfix-expression [ expression ]
6510 postfix-expression [ braced-init-list ] (C++11)
6512 FOR_OFFSETOF is set if we're being called in that context, which
6513 changes how we deal with integer constant expressions. */
6515 static tree
6516 cp_parser_postfix_open_square_expression (cp_parser *parser,
6517 tree postfix_expression,
6518 bool for_offsetof,
6519 bool decltype_p)
6521 tree index = NULL_TREE;
6522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6523 bool saved_greater_than_is_operator_p;
6525 /* Consume the `[' token. */
6526 cp_lexer_consume_token (parser->lexer);
6528 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6529 parser->greater_than_is_operator_p = true;
6531 /* Parse the index expression. */
6532 /* ??? For offsetof, there is a question of what to allow here. If
6533 offsetof is not being used in an integral constant expression context,
6534 then we *could* get the right answer by computing the value at runtime.
6535 If we are in an integral constant expression context, then we might
6536 could accept any constant expression; hard to say without analysis.
6537 Rather than open the barn door too wide right away, allow only integer
6538 constant expressions here. */
6539 if (for_offsetof)
6540 index = cp_parser_constant_expression (parser);
6541 else
6543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6545 bool expr_nonconst_p;
6546 cp_lexer_set_source_position (parser->lexer);
6547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6548 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6549 if (flag_cilkplus
6550 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6552 error_at (cp_lexer_peek_token (parser->lexer)->location,
6553 "braced list index is not allowed with array "
6554 "notation");
6555 cp_parser_skip_to_end_of_statement (parser);
6556 return error_mark_node;
6559 else if (flag_cilkplus)
6561 /* Here are have these two options:
6562 ARRAY[EXP : EXP] - Array notation expr with default
6563 stride of 1.
6564 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6565 stride. */
6566 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6567 postfix_expression);
6568 if (an_exp)
6569 return an_exp;
6571 else
6572 index = cp_parser_expression (parser);
6575 /* For offsetof and declaration of types we need
6576 constant integeral values.
6577 Also we meed to fold for negative constants so that diagnostic in
6578 c-family/c-common.c doesn't fail for array-bounds. */
6579 index = fold_simple_on_cst (index);
6580 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6582 /* Look for the closing `]'. */
6583 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6585 /* Build the ARRAY_REF. */
6586 postfix_expression = grok_array_decl (loc, postfix_expression,
6587 index, decltype_p);
6589 /* When not doing offsetof, array references are not permitted in
6590 constant-expressions. */
6591 if (!for_offsetof
6592 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6593 postfix_expression = error_mark_node;
6595 return postfix_expression;
6598 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6599 by cp_parser_builtin_offsetof. We're looking for
6601 postfix-expression . template [opt] id-expression
6602 postfix-expression . pseudo-destructor-name
6603 postfix-expression -> template [opt] id-expression
6604 postfix-expression -> pseudo-destructor-name
6606 FOR_OFFSETOF is set if we're being called in that context. That sorta
6607 limits what of the above we'll actually accept, but nevermind.
6608 TOKEN_TYPE is the "." or "->" token, which will already have been
6609 removed from the stream. */
6611 static tree
6612 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6613 enum cpp_ttype token_type,
6614 tree postfix_expression,
6615 bool for_offsetof, cp_id_kind *idk,
6616 location_t location)
6618 tree name;
6619 bool dependent_p;
6620 bool pseudo_destructor_p;
6621 tree scope = NULL_TREE;
6623 /* If this is a `->' operator, dereference the pointer. */
6624 if (token_type == CPP_DEREF)
6625 postfix_expression = build_x_arrow (location, postfix_expression,
6626 tf_warning_or_error);
6627 /* Check to see whether or not the expression is type-dependent. */
6628 dependent_p = type_dependent_expression_p (postfix_expression);
6629 /* The identifier following the `->' or `.' is not qualified. */
6630 parser->scope = NULL_TREE;
6631 parser->qualifying_scope = NULL_TREE;
6632 parser->object_scope = NULL_TREE;
6633 *idk = CP_ID_KIND_NONE;
6635 /* Enter the scope corresponding to the type of the object
6636 given by the POSTFIX_EXPRESSION. */
6637 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6639 scope = TREE_TYPE (postfix_expression);
6640 /* According to the standard, no expression should ever have
6641 reference type. Unfortunately, we do not currently match
6642 the standard in this respect in that our internal representation
6643 of an expression may have reference type even when the standard
6644 says it does not. Therefore, we have to manually obtain the
6645 underlying type here. */
6646 scope = non_reference (scope);
6647 /* The type of the POSTFIX_EXPRESSION must be complete. */
6648 if (scope == unknown_type_node)
6650 error_at (location, "%qE does not have class type",
6651 postfix_expression);
6652 scope = NULL_TREE;
6654 /* Unlike the object expression in other contexts, *this is not
6655 required to be of complete type for purposes of class member
6656 access (5.2.5) outside the member function body. */
6657 else if (postfix_expression != current_class_ref
6658 && !(processing_template_decl && scope == current_class_type))
6659 scope = complete_type_or_else (scope, NULL_TREE);
6660 /* Let the name lookup machinery know that we are processing a
6661 class member access expression. */
6662 parser->context->object_type = scope;
6663 /* If something went wrong, we want to be able to discern that case,
6664 as opposed to the case where there was no SCOPE due to the type
6665 of expression being dependent. */
6666 if (!scope)
6667 scope = error_mark_node;
6668 /* If the SCOPE was erroneous, make the various semantic analysis
6669 functions exit quickly -- and without issuing additional error
6670 messages. */
6671 if (scope == error_mark_node)
6672 postfix_expression = error_mark_node;
6675 /* Assume this expression is not a pseudo-destructor access. */
6676 pseudo_destructor_p = false;
6678 /* If the SCOPE is a scalar type, then, if this is a valid program,
6679 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6680 is type dependent, it can be pseudo-destructor-name or something else.
6681 Try to parse it as pseudo-destructor-name first. */
6682 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6684 tree s;
6685 tree type;
6687 cp_parser_parse_tentatively (parser);
6688 /* Parse the pseudo-destructor-name. */
6689 s = NULL_TREE;
6690 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6691 &s, &type);
6692 if (dependent_p
6693 && (cp_parser_error_occurred (parser)
6694 || !SCALAR_TYPE_P (type)))
6695 cp_parser_abort_tentative_parse (parser);
6696 else if (cp_parser_parse_definitely (parser))
6698 pseudo_destructor_p = true;
6699 postfix_expression
6700 = finish_pseudo_destructor_expr (postfix_expression,
6701 s, type, location);
6705 if (!pseudo_destructor_p)
6707 /* If the SCOPE is not a scalar type, we are looking at an
6708 ordinary class member access expression, rather than a
6709 pseudo-destructor-name. */
6710 bool template_p;
6711 cp_token *token = cp_lexer_peek_token (parser->lexer);
6712 /* Parse the id-expression. */
6713 name = (cp_parser_id_expression
6714 (parser,
6715 cp_parser_optional_template_keyword (parser),
6716 /*check_dependency_p=*/true,
6717 &template_p,
6718 /*declarator_p=*/false,
6719 /*optional_p=*/false));
6720 /* In general, build a SCOPE_REF if the member name is qualified.
6721 However, if the name was not dependent and has already been
6722 resolved; there is no need to build the SCOPE_REF. For example;
6724 struct X { void f(); };
6725 template <typename T> void f(T* t) { t->X::f(); }
6727 Even though "t" is dependent, "X::f" is not and has been resolved
6728 to a BASELINK; there is no need to include scope information. */
6730 /* But we do need to remember that there was an explicit scope for
6731 virtual function calls. */
6732 if (parser->scope)
6733 *idk = CP_ID_KIND_QUALIFIED;
6735 /* If the name is a template-id that names a type, we will get a
6736 TYPE_DECL here. That is invalid code. */
6737 if (TREE_CODE (name) == TYPE_DECL)
6739 error_at (token->location, "invalid use of %qD", name);
6740 postfix_expression = error_mark_node;
6742 else
6744 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6746 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6748 error_at (token->location, "%<%D::%D%> is not a class member",
6749 parser->scope, name);
6750 postfix_expression = error_mark_node;
6752 else
6753 name = build_qualified_name (/*type=*/NULL_TREE,
6754 parser->scope,
6755 name,
6756 template_p);
6757 parser->scope = NULL_TREE;
6758 parser->qualifying_scope = NULL_TREE;
6759 parser->object_scope = NULL_TREE;
6761 if (parser->scope && name && BASELINK_P (name))
6762 adjust_result_of_qualified_name_lookup
6763 (name, parser->scope, scope);
6764 postfix_expression
6765 = finish_class_member_access_expr (postfix_expression, name,
6766 template_p,
6767 tf_warning_or_error);
6771 /* We no longer need to look up names in the scope of the object on
6772 the left-hand side of the `.' or `->' operator. */
6773 parser->context->object_type = NULL_TREE;
6775 /* Outside of offsetof, these operators may not appear in
6776 constant-expressions. */
6777 if (!for_offsetof
6778 && (cp_parser_non_integral_constant_expression
6779 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6780 postfix_expression = error_mark_node;
6782 return postfix_expression;
6785 /* Cache of LITERAL_ZERO_P constants. */
6787 static GTY(()) tree literal_zeros[itk_none];
6789 /* Parse a parenthesized expression-list.
6791 expression-list:
6792 assignment-expression
6793 expression-list, assignment-expression
6795 attribute-list:
6796 expression-list
6797 identifier
6798 identifier, expression-list
6800 CAST_P is true if this expression is the target of a cast.
6802 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6803 argument pack.
6805 Returns a vector of trees. Each element is a representation of an
6806 assignment-expression. NULL is returned if the ( and or ) are
6807 missing. An empty, but allocated, vector is returned on no
6808 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6809 if we are parsing an attribute list for an attribute that wants a
6810 plain identifier argument, normal_attr for an attribute that wants
6811 an expression, or non_attr if we aren't parsing an attribute list. If
6812 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6813 not all of the expressions in the list were constant.
6814 WANT_LITERAL_ZERO_P is true if the caller is interested in
6815 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6816 immediately, this can be removed. */
6818 static vec<tree, va_gc> *
6819 cp_parser_parenthesized_expression_list (cp_parser* parser,
6820 int is_attribute_list,
6821 bool cast_p,
6822 bool allow_expansion_p,
6823 bool *non_constant_p,
6824 bool want_literal_zero_p)
6826 vec<tree, va_gc> *expression_list;
6827 bool fold_expr_p = is_attribute_list != non_attr;
6828 tree identifier = NULL_TREE;
6829 bool saved_greater_than_is_operator_p;
6831 /* Assume all the expressions will be constant. */
6832 if (non_constant_p)
6833 *non_constant_p = false;
6835 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6836 return NULL;
6838 expression_list = make_tree_vector ();
6840 /* Within a parenthesized expression, a `>' token is always
6841 the greater-than operator. */
6842 saved_greater_than_is_operator_p
6843 = parser->greater_than_is_operator_p;
6844 parser->greater_than_is_operator_p = true;
6846 /* Consume expressions until there are no more. */
6847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6848 while (true)
6850 tree expr;
6852 /* At the beginning of attribute lists, check to see if the
6853 next token is an identifier. */
6854 if (is_attribute_list == id_attr
6855 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6857 cp_token *token;
6859 /* Consume the identifier. */
6860 token = cp_lexer_consume_token (parser->lexer);
6861 /* Save the identifier. */
6862 identifier = token->u.value;
6864 else
6866 bool expr_non_constant_p;
6868 /* Parse the next assignment-expression. */
6869 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6871 /* A braced-init-list. */
6872 cp_lexer_set_source_position (parser->lexer);
6873 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6874 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6875 if (non_constant_p && expr_non_constant_p)
6876 *non_constant_p = true;
6878 else if (non_constant_p)
6880 expr = (cp_parser_constant_expression
6881 (parser, /*allow_non_constant_p=*/true,
6882 &expr_non_constant_p));
6883 if (expr_non_constant_p)
6884 *non_constant_p = true;
6886 else
6888 expr = NULL_TREE;
6889 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6890 switch (tok->type)
6892 case CPP_NUMBER:
6893 case CPP_CHAR:
6894 case CPP_WCHAR:
6895 case CPP_CHAR16:
6896 case CPP_CHAR32:
6897 case CPP_UTF8CHAR:
6898 /* If a parameter is literal zero alone, remember it
6899 for -Wmemset-transposed-args warning. */
6900 if (integer_zerop (tok->u.value)
6901 && !TREE_OVERFLOW (tok->u.value)
6902 && want_literal_zero_p
6903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6904 == CPP_COMMA
6905 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6906 == CPP_CLOSE_PAREN))
6908 unsigned int i;
6909 for (i = 0; i < itk_none; ++i)
6910 if (TREE_TYPE (tok->u.value) == integer_types[i])
6911 break;
6912 if (i < itk_none && literal_zeros[i])
6913 expr = literal_zeros[i];
6914 else
6916 expr = copy_node (tok->u.value);
6917 LITERAL_ZERO_P (expr) = 1;
6918 if (i < itk_none)
6919 literal_zeros[i] = expr;
6921 /* Consume the 0 token (or '\0', 0LL etc.). */
6922 cp_lexer_consume_token (parser->lexer);
6924 break;
6925 default:
6926 break;
6928 if (expr == NULL_TREE)
6929 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6930 cast_p);
6933 if (fold_expr_p)
6934 expr = instantiate_non_dependent_expr (expr);
6936 /* If we have an ellipsis, then this is an expression
6937 expansion. */
6938 if (allow_expansion_p
6939 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6941 /* Consume the `...'. */
6942 cp_lexer_consume_token (parser->lexer);
6944 /* Build the argument pack. */
6945 expr = make_pack_expansion (expr);
6948 /* Add it to the list. We add error_mark_node
6949 expressions to the list, so that we can still tell if
6950 the correct form for a parenthesized expression-list
6951 is found. That gives better errors. */
6952 vec_safe_push (expression_list, expr);
6954 if (expr == error_mark_node)
6955 goto skip_comma;
6958 /* After the first item, attribute lists look the same as
6959 expression lists. */
6960 is_attribute_list = non_attr;
6962 get_comma:;
6963 /* If the next token isn't a `,', then we are done. */
6964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6965 break;
6967 /* Otherwise, consume the `,' and keep going. */
6968 cp_lexer_consume_token (parser->lexer);
6971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6973 int ending;
6975 skip_comma:;
6976 /* We try and resync to an unnested comma, as that will give the
6977 user better diagnostics. */
6978 ending = cp_parser_skip_to_closing_parenthesis (parser,
6979 /*recovering=*/true,
6980 /*or_comma=*/true,
6981 /*consume_paren=*/true);
6982 if (ending < 0)
6983 goto get_comma;
6984 if (!ending)
6986 parser->greater_than_is_operator_p
6987 = saved_greater_than_is_operator_p;
6988 return NULL;
6992 parser->greater_than_is_operator_p
6993 = saved_greater_than_is_operator_p;
6995 if (identifier)
6996 vec_safe_insert (expression_list, 0, identifier);
6998 return expression_list;
7001 /* Parse a pseudo-destructor-name.
7003 pseudo-destructor-name:
7004 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7005 :: [opt] nested-name-specifier template template-id :: ~ type-name
7006 :: [opt] nested-name-specifier [opt] ~ type-name
7008 If either of the first two productions is used, sets *SCOPE to the
7009 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7010 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7011 or ERROR_MARK_NODE if the parse fails. */
7013 static void
7014 cp_parser_pseudo_destructor_name (cp_parser* parser,
7015 tree object,
7016 tree* scope,
7017 tree* type)
7019 bool nested_name_specifier_p;
7021 /* Handle ~auto. */
7022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7023 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7024 && !type_dependent_expression_p (object))
7026 if (cxx_dialect < cxx14)
7027 pedwarn (input_location, 0,
7028 "%<~auto%> only available with "
7029 "-std=c++14 or -std=gnu++14");
7030 cp_lexer_consume_token (parser->lexer);
7031 cp_lexer_consume_token (parser->lexer);
7032 *scope = NULL_TREE;
7033 *type = TREE_TYPE (object);
7034 return;
7037 /* Assume that things will not work out. */
7038 *type = error_mark_node;
7040 /* Look for the optional `::' operator. */
7041 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7042 /* Look for the optional nested-name-specifier. */
7043 nested_name_specifier_p
7044 = (cp_parser_nested_name_specifier_opt (parser,
7045 /*typename_keyword_p=*/false,
7046 /*check_dependency_p=*/true,
7047 /*type_p=*/false,
7048 /*is_declaration=*/false)
7049 != NULL_TREE);
7050 /* Now, if we saw a nested-name-specifier, we might be doing the
7051 second production. */
7052 if (nested_name_specifier_p
7053 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7055 /* Consume the `template' keyword. */
7056 cp_lexer_consume_token (parser->lexer);
7057 /* Parse the template-id. */
7058 cp_parser_template_id (parser,
7059 /*template_keyword_p=*/true,
7060 /*check_dependency_p=*/false,
7061 class_type,
7062 /*is_declaration=*/true);
7063 /* Look for the `::' token. */
7064 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7066 /* If the next token is not a `~', then there might be some
7067 additional qualification. */
7068 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7070 /* At this point, we're looking for "type-name :: ~". The type-name
7071 must not be a class-name, since this is a pseudo-destructor. So,
7072 it must be either an enum-name, or a typedef-name -- both of which
7073 are just identifiers. So, we peek ahead to check that the "::"
7074 and "~" tokens are present; if they are not, then we can avoid
7075 calling type_name. */
7076 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7077 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7078 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7080 cp_parser_error (parser, "non-scalar type");
7081 return;
7084 /* Look for the type-name. */
7085 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7086 if (*scope == error_mark_node)
7087 return;
7089 /* Look for the `::' token. */
7090 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7092 else
7093 *scope = NULL_TREE;
7095 /* Look for the `~'. */
7096 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7098 /* Once we see the ~, this has to be a pseudo-destructor. */
7099 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7100 cp_parser_commit_to_topmost_tentative_parse (parser);
7102 /* Look for the type-name again. We are not responsible for
7103 checking that it matches the first type-name. */
7104 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7107 /* Parse a unary-expression.
7109 unary-expression:
7110 postfix-expression
7111 ++ cast-expression
7112 -- cast-expression
7113 unary-operator cast-expression
7114 sizeof unary-expression
7115 sizeof ( type-id )
7116 alignof ( type-id ) [C++0x]
7117 new-expression
7118 delete-expression
7120 GNU Extensions:
7122 unary-expression:
7123 __extension__ cast-expression
7124 __alignof__ unary-expression
7125 __alignof__ ( type-id )
7126 alignof unary-expression [C++0x]
7127 __real__ cast-expression
7128 __imag__ cast-expression
7129 && identifier
7130 sizeof ( type-id ) { initializer-list , [opt] }
7131 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7132 __alignof__ ( type-id ) { initializer-list , [opt] }
7134 ADDRESS_P is true iff the unary-expression is appearing as the
7135 operand of the `&' operator. CAST_P is true if this expression is
7136 the target of a cast.
7138 Returns a representation of the expression. */
7140 static tree
7141 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7142 bool address_p, bool cast_p, bool decltype_p)
7144 cp_token *token;
7145 enum tree_code unary_operator;
7147 /* Peek at the next token. */
7148 token = cp_lexer_peek_token (parser->lexer);
7149 /* Some keywords give away the kind of expression. */
7150 if (token->type == CPP_KEYWORD)
7152 enum rid keyword = token->keyword;
7154 switch (keyword)
7156 case RID_ALIGNOF:
7157 case RID_SIZEOF:
7159 tree operand, ret;
7160 enum tree_code op;
7161 location_t first_loc;
7163 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7164 /* Consume the token. */
7165 cp_lexer_consume_token (parser->lexer);
7166 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7167 /* Parse the operand. */
7168 operand = cp_parser_sizeof_operand (parser, keyword);
7170 if (TYPE_P (operand))
7171 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7172 else
7174 /* ISO C++ defines alignof only with types, not with
7175 expressions. So pedwarn if alignof is used with a non-
7176 type expression. However, __alignof__ is ok. */
7177 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7178 pedwarn (token->location, OPT_Wpedantic,
7179 "ISO C++ does not allow %<alignof%> "
7180 "with a non-type");
7182 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7184 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7185 SIZEOF_EXPR with the original operand. */
7186 if (op == SIZEOF_EXPR && ret != error_mark_node)
7188 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7190 if (!processing_template_decl && TYPE_P (operand))
7192 ret = build_min (SIZEOF_EXPR, size_type_node,
7193 build1 (NOP_EXPR, operand,
7194 error_mark_node));
7195 SIZEOF_EXPR_TYPE_P (ret) = 1;
7197 else
7198 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7199 TREE_SIDE_EFFECTS (ret) = 0;
7200 TREE_READONLY (ret) = 1;
7202 SET_EXPR_LOCATION (ret, first_loc);
7204 return ret;
7207 case RID_NEW:
7208 return cp_parser_new_expression (parser);
7210 case RID_DELETE:
7211 return cp_parser_delete_expression (parser);
7213 case RID_EXTENSION:
7215 /* The saved value of the PEDANTIC flag. */
7216 int saved_pedantic;
7217 tree expr;
7219 /* Save away the PEDANTIC flag. */
7220 cp_parser_extension_opt (parser, &saved_pedantic);
7221 /* Parse the cast-expression. */
7222 expr = cp_parser_simple_cast_expression (parser);
7223 /* Restore the PEDANTIC flag. */
7224 pedantic = saved_pedantic;
7226 return expr;
7229 case RID_REALPART:
7230 case RID_IMAGPART:
7232 tree expression;
7234 /* Consume the `__real__' or `__imag__' token. */
7235 cp_lexer_consume_token (parser->lexer);
7236 /* Parse the cast-expression. */
7237 expression = cp_parser_simple_cast_expression (parser);
7238 /* Create the complete representation. */
7239 return build_x_unary_op (token->location,
7240 (keyword == RID_REALPART
7241 ? REALPART_EXPR : IMAGPART_EXPR),
7242 expression,
7243 tf_warning_or_error);
7245 break;
7247 case RID_TRANSACTION_ATOMIC:
7248 case RID_TRANSACTION_RELAXED:
7249 return cp_parser_transaction_expression (parser, keyword);
7251 case RID_NOEXCEPT:
7253 tree expr;
7254 const char *saved_message;
7255 bool saved_integral_constant_expression_p;
7256 bool saved_non_integral_constant_expression_p;
7257 bool saved_greater_than_is_operator_p;
7259 cp_lexer_consume_token (parser->lexer);
7260 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7262 saved_message = parser->type_definition_forbidden_message;
7263 parser->type_definition_forbidden_message
7264 = G_("types may not be defined in %<noexcept%> expressions");
7266 saved_integral_constant_expression_p
7267 = parser->integral_constant_expression_p;
7268 saved_non_integral_constant_expression_p
7269 = parser->non_integral_constant_expression_p;
7270 parser->integral_constant_expression_p = false;
7272 saved_greater_than_is_operator_p
7273 = parser->greater_than_is_operator_p;
7274 parser->greater_than_is_operator_p = true;
7276 ++cp_unevaluated_operand;
7277 ++c_inhibit_evaluation_warnings;
7278 ++cp_noexcept_operand;
7279 expr = cp_parser_expression (parser);
7280 --cp_noexcept_operand;
7281 --c_inhibit_evaluation_warnings;
7282 --cp_unevaluated_operand;
7284 parser->greater_than_is_operator_p
7285 = saved_greater_than_is_operator_p;
7287 parser->integral_constant_expression_p
7288 = saved_integral_constant_expression_p;
7289 parser->non_integral_constant_expression_p
7290 = saved_non_integral_constant_expression_p;
7292 parser->type_definition_forbidden_message = saved_message;
7294 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7295 return finish_noexcept_expr (expr, tf_warning_or_error);
7298 default:
7299 break;
7303 /* Look for the `:: new' and `:: delete', which also signal the
7304 beginning of a new-expression, or delete-expression,
7305 respectively. If the next token is `::', then it might be one of
7306 these. */
7307 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7309 enum rid keyword;
7311 /* See if the token after the `::' is one of the keywords in
7312 which we're interested. */
7313 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7314 /* If it's `new', we have a new-expression. */
7315 if (keyword == RID_NEW)
7316 return cp_parser_new_expression (parser);
7317 /* Similarly, for `delete'. */
7318 else if (keyword == RID_DELETE)
7319 return cp_parser_delete_expression (parser);
7322 /* Look for a unary operator. */
7323 unary_operator = cp_parser_unary_operator (token);
7324 /* The `++' and `--' operators can be handled similarly, even though
7325 they are not technically unary-operators in the grammar. */
7326 if (unary_operator == ERROR_MARK)
7328 if (token->type == CPP_PLUS_PLUS)
7329 unary_operator = PREINCREMENT_EXPR;
7330 else if (token->type == CPP_MINUS_MINUS)
7331 unary_operator = PREDECREMENT_EXPR;
7332 /* Handle the GNU address-of-label extension. */
7333 else if (cp_parser_allow_gnu_extensions_p (parser)
7334 && token->type == CPP_AND_AND)
7336 tree identifier;
7337 tree expression;
7338 location_t loc = token->location;
7340 /* Consume the '&&' token. */
7341 cp_lexer_consume_token (parser->lexer);
7342 /* Look for the identifier. */
7343 identifier = cp_parser_identifier (parser);
7344 /* Create an expression representing the address. */
7345 expression = finish_label_address_expr (identifier, loc);
7346 if (cp_parser_non_integral_constant_expression (parser,
7347 NIC_ADDR_LABEL))
7348 expression = error_mark_node;
7349 return expression;
7352 if (unary_operator != ERROR_MARK)
7354 tree cast_expression;
7355 tree expression = error_mark_node;
7356 non_integral_constant non_constant_p = NIC_NONE;
7357 location_t loc = token->location;
7358 tsubst_flags_t complain = complain_flags (decltype_p);
7360 /* Consume the operator token. */
7361 token = cp_lexer_consume_token (parser->lexer);
7362 /* Parse the cast-expression. */
7363 cast_expression
7364 = cp_parser_cast_expression (parser,
7365 unary_operator == ADDR_EXPR,
7366 /*cast_p=*/false,
7367 /*decltype*/false,
7368 pidk);
7369 /* Now, build an appropriate representation. */
7370 switch (unary_operator)
7372 case INDIRECT_REF:
7373 non_constant_p = NIC_STAR;
7374 expression = build_x_indirect_ref (loc, cast_expression,
7375 RO_UNARY_STAR,
7376 complain);
7377 break;
7379 case ADDR_EXPR:
7380 non_constant_p = NIC_ADDR;
7381 /* Fall through. */
7382 case BIT_NOT_EXPR:
7383 expression = build_x_unary_op (loc, unary_operator,
7384 cast_expression,
7385 complain);
7386 break;
7388 case PREINCREMENT_EXPR:
7389 case PREDECREMENT_EXPR:
7390 non_constant_p = unary_operator == PREINCREMENT_EXPR
7391 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7392 /* Fall through. */
7393 case UNARY_PLUS_EXPR:
7394 case NEGATE_EXPR:
7395 case TRUTH_NOT_EXPR:
7396 expression = finish_unary_op_expr (loc, unary_operator,
7397 cast_expression, complain);
7398 break;
7400 default:
7401 gcc_unreachable ();
7404 if (non_constant_p != NIC_NONE
7405 && cp_parser_non_integral_constant_expression (parser,
7406 non_constant_p))
7407 expression = error_mark_node;
7409 return expression;
7412 return cp_parser_postfix_expression (parser, address_p, cast_p,
7413 /*member_access_only_p=*/false,
7414 decltype_p,
7415 pidk);
7418 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7419 unary-operator, the corresponding tree code is returned. */
7421 static enum tree_code
7422 cp_parser_unary_operator (cp_token* token)
7424 switch (token->type)
7426 case CPP_MULT:
7427 return INDIRECT_REF;
7429 case CPP_AND:
7430 return ADDR_EXPR;
7432 case CPP_PLUS:
7433 return UNARY_PLUS_EXPR;
7435 case CPP_MINUS:
7436 return NEGATE_EXPR;
7438 case CPP_NOT:
7439 return TRUTH_NOT_EXPR;
7441 case CPP_COMPL:
7442 return BIT_NOT_EXPR;
7444 default:
7445 return ERROR_MARK;
7449 /* Parse a new-expression.
7451 new-expression:
7452 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7453 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7455 Returns a representation of the expression. */
7457 static tree
7458 cp_parser_new_expression (cp_parser* parser)
7460 bool global_scope_p;
7461 vec<tree, va_gc> *placement;
7462 tree type;
7463 vec<tree, va_gc> *initializer;
7464 tree nelts = NULL_TREE;
7465 tree ret;
7467 /* Look for the optional `::' operator. */
7468 global_scope_p
7469 = (cp_parser_global_scope_opt (parser,
7470 /*current_scope_valid_p=*/false)
7471 != NULL_TREE);
7472 /* Look for the `new' operator. */
7473 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7474 /* There's no easy way to tell a new-placement from the
7475 `( type-id )' construct. */
7476 cp_parser_parse_tentatively (parser);
7477 /* Look for a new-placement. */
7478 placement = cp_parser_new_placement (parser);
7479 /* If that didn't work out, there's no new-placement. */
7480 if (!cp_parser_parse_definitely (parser))
7482 if (placement != NULL)
7483 release_tree_vector (placement);
7484 placement = NULL;
7487 /* If the next token is a `(', then we have a parenthesized
7488 type-id. */
7489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7491 cp_token *token;
7492 const char *saved_message = parser->type_definition_forbidden_message;
7494 /* Consume the `('. */
7495 cp_lexer_consume_token (parser->lexer);
7497 /* Parse the type-id. */
7498 parser->type_definition_forbidden_message
7499 = G_("types may not be defined in a new-expression");
7500 type = cp_parser_type_id (parser);
7501 parser->type_definition_forbidden_message = saved_message;
7503 /* Look for the closing `)'. */
7504 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7505 token = cp_lexer_peek_token (parser->lexer);
7506 /* There should not be a direct-new-declarator in this production,
7507 but GCC used to allowed this, so we check and emit a sensible error
7508 message for this case. */
7509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7511 error_at (token->location,
7512 "array bound forbidden after parenthesized type-id");
7513 inform (token->location,
7514 "try removing the parentheses around the type-id");
7515 cp_parser_direct_new_declarator (parser);
7518 /* Otherwise, there must be a new-type-id. */
7519 else
7520 type = cp_parser_new_type_id (parser, &nelts);
7522 /* If the next token is a `(' or '{', then we have a new-initializer. */
7523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7524 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7525 initializer = cp_parser_new_initializer (parser);
7526 else
7527 initializer = NULL;
7529 /* A new-expression may not appear in an integral constant
7530 expression. */
7531 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7532 ret = error_mark_node;
7533 else
7535 /* Create a representation of the new-expression. */
7536 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7537 tf_warning_or_error);
7540 if (placement != NULL)
7541 release_tree_vector (placement);
7542 if (initializer != NULL)
7543 release_tree_vector (initializer);
7545 return ret;
7548 /* Parse a new-placement.
7550 new-placement:
7551 ( expression-list )
7553 Returns the same representation as for an expression-list. */
7555 static vec<tree, va_gc> *
7556 cp_parser_new_placement (cp_parser* parser)
7558 vec<tree, va_gc> *expression_list;
7560 /* Parse the expression-list. */
7561 expression_list = (cp_parser_parenthesized_expression_list
7562 (parser, non_attr, /*cast_p=*/false,
7563 /*allow_expansion_p=*/true,
7564 /*non_constant_p=*/NULL));
7566 if (expression_list && expression_list->is_empty ())
7567 error ("expected expression-list or type-id");
7569 return expression_list;
7572 /* Parse a new-type-id.
7574 new-type-id:
7575 type-specifier-seq new-declarator [opt]
7577 Returns the TYPE allocated. If the new-type-id indicates an array
7578 type, *NELTS is set to the number of elements in the last array
7579 bound; the TYPE will not include the last array bound. */
7581 static tree
7582 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7584 cp_decl_specifier_seq type_specifier_seq;
7585 cp_declarator *new_declarator;
7586 cp_declarator *declarator;
7587 cp_declarator *outer_declarator;
7588 const char *saved_message;
7590 /* The type-specifier sequence must not contain type definitions.
7591 (It cannot contain declarations of new types either, but if they
7592 are not definitions we will catch that because they are not
7593 complete.) */
7594 saved_message = parser->type_definition_forbidden_message;
7595 parser->type_definition_forbidden_message
7596 = G_("types may not be defined in a new-type-id");
7597 /* Parse the type-specifier-seq. */
7598 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7599 /*is_trailing_return=*/false,
7600 &type_specifier_seq);
7601 /* Restore the old message. */
7602 parser->type_definition_forbidden_message = saved_message;
7604 if (type_specifier_seq.type == error_mark_node)
7605 return error_mark_node;
7607 /* Parse the new-declarator. */
7608 new_declarator = cp_parser_new_declarator_opt (parser);
7610 /* Determine the number of elements in the last array dimension, if
7611 any. */
7612 *nelts = NULL_TREE;
7613 /* Skip down to the last array dimension. */
7614 declarator = new_declarator;
7615 outer_declarator = NULL;
7616 while (declarator && (declarator->kind == cdk_pointer
7617 || declarator->kind == cdk_ptrmem))
7619 outer_declarator = declarator;
7620 declarator = declarator->declarator;
7622 while (declarator
7623 && declarator->kind == cdk_array
7624 && declarator->declarator
7625 && declarator->declarator->kind == cdk_array)
7627 outer_declarator = declarator;
7628 declarator = declarator->declarator;
7631 if (declarator && declarator->kind == cdk_array)
7633 *nelts = declarator->u.array.bounds;
7634 if (*nelts == error_mark_node)
7635 *nelts = integer_one_node;
7637 if (outer_declarator)
7638 outer_declarator->declarator = declarator->declarator;
7639 else
7640 new_declarator = NULL;
7643 return groktypename (&type_specifier_seq, new_declarator, false);
7646 /* Parse an (optional) new-declarator.
7648 new-declarator:
7649 ptr-operator new-declarator [opt]
7650 direct-new-declarator
7652 Returns the declarator. */
7654 static cp_declarator *
7655 cp_parser_new_declarator_opt (cp_parser* parser)
7657 enum tree_code code;
7658 tree type, std_attributes = NULL_TREE;
7659 cp_cv_quals cv_quals;
7661 /* We don't know if there's a ptr-operator next, or not. */
7662 cp_parser_parse_tentatively (parser);
7663 /* Look for a ptr-operator. */
7664 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7665 /* If that worked, look for more new-declarators. */
7666 if (cp_parser_parse_definitely (parser))
7668 cp_declarator *declarator;
7670 /* Parse another optional declarator. */
7671 declarator = cp_parser_new_declarator_opt (parser);
7673 declarator = cp_parser_make_indirect_declarator
7674 (code, type, cv_quals, declarator, std_attributes);
7676 return declarator;
7679 /* If the next token is a `[', there is a direct-new-declarator. */
7680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7681 return cp_parser_direct_new_declarator (parser);
7683 return NULL;
7686 /* Parse a direct-new-declarator.
7688 direct-new-declarator:
7689 [ expression ]
7690 direct-new-declarator [constant-expression]
7694 static cp_declarator *
7695 cp_parser_direct_new_declarator (cp_parser* parser)
7697 cp_declarator *declarator = NULL;
7699 while (true)
7701 tree expression;
7702 cp_token *token;
7704 /* Look for the opening `['. */
7705 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7707 token = cp_lexer_peek_token (parser->lexer);
7708 expression = cp_parser_expression (parser);
7709 /* The standard requires that the expression have integral
7710 type. DR 74 adds enumeration types. We believe that the
7711 real intent is that these expressions be handled like the
7712 expression in a `switch' condition, which also allows
7713 classes with a single conversion to integral or
7714 enumeration type. */
7715 if (!processing_template_decl)
7717 expression
7718 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7719 expression,
7720 /*complain=*/true);
7721 if (!expression)
7723 error_at (token->location,
7724 "expression in new-declarator must have integral "
7725 "or enumeration type");
7726 expression = error_mark_node;
7730 /* Look for the closing `]'. */
7731 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7733 /* Add this bound to the declarator. */
7734 declarator = make_array_declarator (declarator, expression);
7736 /* If the next token is not a `[', then there are no more
7737 bounds. */
7738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7739 break;
7742 return declarator;
7745 /* Parse a new-initializer.
7747 new-initializer:
7748 ( expression-list [opt] )
7749 braced-init-list
7751 Returns a representation of the expression-list. */
7753 static vec<tree, va_gc> *
7754 cp_parser_new_initializer (cp_parser* parser)
7756 vec<tree, va_gc> *expression_list;
7758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7760 tree t;
7761 bool expr_non_constant_p;
7762 cp_lexer_set_source_position (parser->lexer);
7763 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7764 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7765 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7766 expression_list = make_tree_vector_single (t);
7768 else
7769 expression_list = (cp_parser_parenthesized_expression_list
7770 (parser, non_attr, /*cast_p=*/false,
7771 /*allow_expansion_p=*/true,
7772 /*non_constant_p=*/NULL));
7774 return expression_list;
7777 /* Parse a delete-expression.
7779 delete-expression:
7780 :: [opt] delete cast-expression
7781 :: [opt] delete [ ] cast-expression
7783 Returns a representation of the expression. */
7785 static tree
7786 cp_parser_delete_expression (cp_parser* parser)
7788 bool global_scope_p;
7789 bool array_p;
7790 tree expression;
7792 /* Look for the optional `::' operator. */
7793 global_scope_p
7794 = (cp_parser_global_scope_opt (parser,
7795 /*current_scope_valid_p=*/false)
7796 != NULL_TREE);
7797 /* Look for the `delete' keyword. */
7798 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7799 /* See if the array syntax is in use. */
7800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7802 /* Consume the `[' token. */
7803 cp_lexer_consume_token (parser->lexer);
7804 /* Look for the `]' token. */
7805 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7806 /* Remember that this is the `[]' construct. */
7807 array_p = true;
7809 else
7810 array_p = false;
7812 /* Parse the cast-expression. */
7813 expression = cp_parser_simple_cast_expression (parser);
7815 /* A delete-expression may not appear in an integral constant
7816 expression. */
7817 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7818 return error_mark_node;
7820 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7821 tf_warning_or_error);
7824 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7825 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7826 0 otherwise. */
7828 static int
7829 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7831 cp_token *token = cp_lexer_peek_token (parser->lexer);
7832 switch (token->type)
7834 case CPP_COMMA:
7835 case CPP_SEMICOLON:
7836 case CPP_QUERY:
7837 case CPP_COLON:
7838 case CPP_CLOSE_SQUARE:
7839 case CPP_CLOSE_PAREN:
7840 case CPP_CLOSE_BRACE:
7841 case CPP_OPEN_BRACE:
7842 case CPP_DOT:
7843 case CPP_DOT_STAR:
7844 case CPP_DEREF:
7845 case CPP_DEREF_STAR:
7846 case CPP_DIV:
7847 case CPP_MOD:
7848 case CPP_LSHIFT:
7849 case CPP_RSHIFT:
7850 case CPP_LESS:
7851 case CPP_GREATER:
7852 case CPP_LESS_EQ:
7853 case CPP_GREATER_EQ:
7854 case CPP_EQ_EQ:
7855 case CPP_NOT_EQ:
7856 case CPP_EQ:
7857 case CPP_MULT_EQ:
7858 case CPP_DIV_EQ:
7859 case CPP_MOD_EQ:
7860 case CPP_PLUS_EQ:
7861 case CPP_MINUS_EQ:
7862 case CPP_RSHIFT_EQ:
7863 case CPP_LSHIFT_EQ:
7864 case CPP_AND_EQ:
7865 case CPP_XOR_EQ:
7866 case CPP_OR_EQ:
7867 case CPP_XOR:
7868 case CPP_OR:
7869 case CPP_OR_OR:
7870 case CPP_EOF:
7871 case CPP_ELLIPSIS:
7872 return 0;
7874 case CPP_OPEN_PAREN:
7875 /* In ((type ()) () the last () isn't a valid cast-expression,
7876 so the whole must be parsed as postfix-expression. */
7877 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7878 != CPP_CLOSE_PAREN;
7880 case CPP_OPEN_SQUARE:
7881 /* '[' may start a primary-expression in obj-c++ and in C++11,
7882 as a lambda-expression, eg, '(void)[]{}'. */
7883 if (cxx_dialect >= cxx11)
7884 return -1;
7885 return c_dialect_objc ();
7887 case CPP_PLUS_PLUS:
7888 case CPP_MINUS_MINUS:
7889 /* '++' and '--' may or may not start a cast-expression:
7891 struct T { void operator++(int); };
7892 void f() { (T())++; }
7896 int a;
7897 (int)++a; */
7898 return -1;
7900 default:
7901 return 1;
7905 /* Parse a cast-expression.
7907 cast-expression:
7908 unary-expression
7909 ( type-id ) cast-expression
7911 ADDRESS_P is true iff the unary-expression is appearing as the
7912 operand of the `&' operator. CAST_P is true if this expression is
7913 the target of a cast.
7915 Returns a representation of the expression. */
7917 static tree
7918 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7919 bool decltype_p, cp_id_kind * pidk)
7921 /* If it's a `(', then we might be looking at a cast. */
7922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7924 tree type = NULL_TREE;
7925 tree expr = NULL_TREE;
7926 int cast_expression = 0;
7927 const char *saved_message;
7929 /* There's no way to know yet whether or not this is a cast.
7930 For example, `(int (3))' is a unary-expression, while `(int)
7931 3' is a cast. So, we resort to parsing tentatively. */
7932 cp_parser_parse_tentatively (parser);
7933 /* Types may not be defined in a cast. */
7934 saved_message = parser->type_definition_forbidden_message;
7935 parser->type_definition_forbidden_message
7936 = G_("types may not be defined in casts");
7937 /* Consume the `('. */
7938 cp_lexer_consume_token (parser->lexer);
7939 /* A very tricky bit is that `(struct S) { 3 }' is a
7940 compound-literal (which we permit in C++ as an extension).
7941 But, that construct is not a cast-expression -- it is a
7942 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7943 is legal; if the compound-literal were a cast-expression,
7944 you'd need an extra set of parentheses.) But, if we parse
7945 the type-id, and it happens to be a class-specifier, then we
7946 will commit to the parse at that point, because we cannot
7947 undo the action that is done when creating a new class. So,
7948 then we cannot back up and do a postfix-expression.
7950 Another tricky case is the following (c++/29234):
7952 struct S { void operator () (); };
7954 void foo ()
7956 ( S()() );
7959 As a type-id we parse the parenthesized S()() as a function
7960 returning a function, groktypename complains and we cannot
7961 back up in this case either.
7963 Therefore, we scan ahead to the closing `)', and check to see
7964 if the tokens after the `)' can start a cast-expression. Otherwise
7965 we are dealing with an unary-expression, a postfix-expression
7966 or something else.
7968 Yet another tricky case, in C++11, is the following (c++/54891):
7970 (void)[]{};
7972 The issue is that usually, besides the case of lambda-expressions,
7973 the parenthesized type-id cannot be followed by '[', and, eg, we
7974 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7975 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7976 we don't commit, we try a cast-expression, then an unary-expression.
7978 Save tokens so that we can put them back. */
7979 cp_lexer_save_tokens (parser->lexer);
7981 /* We may be looking at a cast-expression. */
7982 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7983 /*consume_paren=*/true))
7984 cast_expression
7985 = cp_parser_tokens_start_cast_expression (parser);
7987 /* Roll back the tokens we skipped. */
7988 cp_lexer_rollback_tokens (parser->lexer);
7989 /* If we aren't looking at a cast-expression, simulate an error so
7990 that the call to cp_parser_error_occurred below returns true. */
7991 if (!cast_expression)
7992 cp_parser_simulate_error (parser);
7993 else
7995 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7996 parser->in_type_id_in_expr_p = true;
7997 /* Look for the type-id. */
7998 type = cp_parser_type_id (parser);
7999 /* Look for the closing `)'. */
8000 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8001 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8004 /* Restore the saved message. */
8005 parser->type_definition_forbidden_message = saved_message;
8007 /* At this point this can only be either a cast or a
8008 parenthesized ctor such as `(T ())' that looks like a cast to
8009 function returning T. */
8010 if (!cp_parser_error_occurred (parser))
8012 /* Only commit if the cast-expression doesn't start with
8013 '++', '--', or '[' in C++11. */
8014 if (cast_expression > 0)
8015 cp_parser_commit_to_topmost_tentative_parse (parser);
8017 expr = cp_parser_cast_expression (parser,
8018 /*address_p=*/false,
8019 /*cast_p=*/true,
8020 /*decltype_p=*/false,
8021 pidk);
8023 if (cp_parser_parse_definitely (parser))
8025 /* Warn about old-style casts, if so requested. */
8026 if (warn_old_style_cast
8027 && !in_system_header_at (input_location)
8028 && !VOID_TYPE_P (type)
8029 && current_lang_name != lang_name_c)
8030 warning (OPT_Wold_style_cast, "use of old-style cast");
8032 /* Only type conversions to integral or enumeration types
8033 can be used in constant-expressions. */
8034 if (!cast_valid_in_integral_constant_expression_p (type)
8035 && cp_parser_non_integral_constant_expression (parser,
8036 NIC_CAST))
8037 return error_mark_node;
8039 /* Perform the cast. */
8040 /* We don't want to resolve cast too early. Therefore we don't
8041 be able to use build_c_cast. */
8042 expr = cp_build_c_cast (type, expr, tf_warning_or_error);
8043 return expr;
8046 else
8047 cp_parser_abort_tentative_parse (parser);
8050 /* If we get here, then it's not a cast, so it must be a
8051 unary-expression. */
8052 return cp_parser_unary_expression (parser, pidk, address_p,
8053 cast_p, decltype_p);
8056 /* Parse a binary expression of the general form:
8058 pm-expression:
8059 cast-expression
8060 pm-expression .* cast-expression
8061 pm-expression ->* cast-expression
8063 multiplicative-expression:
8064 pm-expression
8065 multiplicative-expression * pm-expression
8066 multiplicative-expression / pm-expression
8067 multiplicative-expression % pm-expression
8069 additive-expression:
8070 multiplicative-expression
8071 additive-expression + multiplicative-expression
8072 additive-expression - multiplicative-expression
8074 shift-expression:
8075 additive-expression
8076 shift-expression << additive-expression
8077 shift-expression >> additive-expression
8079 relational-expression:
8080 shift-expression
8081 relational-expression < shift-expression
8082 relational-expression > shift-expression
8083 relational-expression <= shift-expression
8084 relational-expression >= shift-expression
8086 GNU Extension:
8088 relational-expression:
8089 relational-expression <? shift-expression
8090 relational-expression >? shift-expression
8092 equality-expression:
8093 relational-expression
8094 equality-expression == relational-expression
8095 equality-expression != relational-expression
8097 and-expression:
8098 equality-expression
8099 and-expression & equality-expression
8101 exclusive-or-expression:
8102 and-expression
8103 exclusive-or-expression ^ and-expression
8105 inclusive-or-expression:
8106 exclusive-or-expression
8107 inclusive-or-expression | exclusive-or-expression
8109 logical-and-expression:
8110 inclusive-or-expression
8111 logical-and-expression && inclusive-or-expression
8113 logical-or-expression:
8114 logical-and-expression
8115 logical-or-expression || logical-and-expression
8117 All these are implemented with a single function like:
8119 binary-expression:
8120 simple-cast-expression
8121 binary-expression <token> binary-expression
8123 CAST_P is true if this expression is the target of a cast.
8125 The binops_by_token map is used to get the tree codes for each <token> type.
8126 binary-expressions are associated according to a precedence table. */
8128 #define TOKEN_PRECEDENCE(token) \
8129 (((token->type == CPP_GREATER \
8130 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8131 && !parser->greater_than_is_operator_p) \
8132 ? PREC_NOT_OPERATOR \
8133 : binops_by_token[token->type].prec)
8135 static tree
8136 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8137 bool no_toplevel_fold_p,
8138 bool decltype_p,
8139 enum cp_parser_prec prec,
8140 cp_id_kind * pidk)
8142 cp_parser_expression_stack stack;
8143 cp_parser_expression_stack_entry *sp = &stack[0];
8144 cp_parser_expression_stack_entry current;
8145 tree rhs;
8146 cp_token *token;
8147 enum tree_code rhs_type;
8148 enum cp_parser_prec new_prec, lookahead_prec;
8149 tree overload, folded;
8151 /* Parse the first expression. */
8152 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8153 ? TRUTH_NOT_EXPR : ERROR_MARK);
8154 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8155 cast_p, decltype_p, pidk);
8156 current.prec = prec;
8158 if (cp_parser_error_occurred (parser))
8159 return error_mark_node;
8161 for (;;)
8163 /* Get an operator token. */
8164 token = cp_lexer_peek_token (parser->lexer);
8166 if (warn_cxx11_compat
8167 && token->type == CPP_RSHIFT
8168 && !parser->greater_than_is_operator_p)
8170 if (warning_at (token->location, OPT_Wc__11_compat,
8171 "%<>>%> operator is treated"
8172 " as two right angle brackets in C++11"))
8173 inform (token->location,
8174 "suggest parentheses around %<>>%> expression");
8177 new_prec = TOKEN_PRECEDENCE (token);
8179 /* Popping an entry off the stack means we completed a subexpression:
8180 - either we found a token which is not an operator (`>' where it is not
8181 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8182 will happen repeatedly;
8183 - or, we found an operator which has lower priority. This is the case
8184 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8185 parsing `3 * 4'. */
8186 if (new_prec <= current.prec)
8188 if (sp == stack)
8189 break;
8190 else
8191 goto pop;
8194 get_rhs:
8195 current.tree_type = binops_by_token[token->type].tree_type;
8196 current.loc = token->location;
8198 /* We used the operator token. */
8199 cp_lexer_consume_token (parser->lexer);
8201 folded = cp_fully_fold (current.lhs);
8202 /* For "false && x" or "true || x", x will never be executed;
8203 disable warnings while evaluating it. */
8204 if (current.tree_type == TRUTH_ANDIF_EXPR)
8205 c_inhibit_evaluation_warnings += folded == truthvalue_false_node;
8206 else if (current.tree_type == TRUTH_ORIF_EXPR)
8207 c_inhibit_evaluation_warnings += folded == truthvalue_true_node;
8209 /* Extract another operand. It may be the RHS of this expression
8210 or the LHS of a new, higher priority expression. */
8211 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8212 ? TRUTH_NOT_EXPR : ERROR_MARK);
8213 rhs = cp_parser_simple_cast_expression (parser);
8215 /* Get another operator token. Look up its precedence to avoid
8216 building a useless (immediately popped) stack entry for common
8217 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8218 token = cp_lexer_peek_token (parser->lexer);
8219 lookahead_prec = TOKEN_PRECEDENCE (token);
8220 if (lookahead_prec > new_prec)
8222 /* ... and prepare to parse the RHS of the new, higher priority
8223 expression. Since precedence levels on the stack are
8224 monotonically increasing, we do not have to care about
8225 stack overflows. */
8226 *sp = current;
8227 ++sp;
8228 current.lhs = rhs;
8229 current.lhs_type = rhs_type;
8230 current.prec = new_prec;
8231 new_prec = lookahead_prec;
8232 goto get_rhs;
8234 pop:
8235 lookahead_prec = new_prec;
8236 /* If the stack is not empty, we have parsed into LHS the right side
8237 (`4' in the example above) of an expression we had suspended.
8238 We can use the information on the stack to recover the LHS (`3')
8239 from the stack together with the tree code (`MULT_EXPR'), and
8240 the precedence of the higher level subexpression
8241 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8242 which will be used to actually build the additive expression. */
8243 rhs = current.lhs;
8244 rhs_type = current.lhs_type;
8245 --sp;
8246 current = *sp;
8249 folded = cp_fully_fold (current.lhs);
8250 /* Undo the disabling of warnings done above. */
8251 if (current.tree_type == TRUTH_ANDIF_EXPR)
8252 c_inhibit_evaluation_warnings -= folded == truthvalue_false_node;
8253 else if (current.tree_type == TRUTH_ORIF_EXPR)
8254 c_inhibit_evaluation_warnings -= folded == truthvalue_true_node;
8256 if (warn_logical_not_paren
8257 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8258 && current.lhs_type == TRUTH_NOT_EXPR
8259 /* Avoid warning for !!x == y. */
8260 && (TREE_CODE (current.lhs) != NE_EXPR
8261 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8262 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8263 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8264 /* Avoid warning for !b == y where b is boolean. */
8265 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8266 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8267 != BOOLEAN_TYPE))))
8268 /* Avoid warning for !!b == y where b is boolean. */
8269 && (!DECL_P (current.lhs)
8270 || TREE_TYPE (current.lhs) == NULL_TREE
8271 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8272 warn_logical_not_parentheses (current.loc, current.tree_type,
8273 maybe_constant_value (rhs));
8275 overload = NULL;
8276 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8277 ERROR_MARK for everything that is not a binary expression.
8278 This makes warn_about_parentheses miss some warnings that
8279 involve unary operators. For unary expressions we should
8280 pass the correct tree_code unless the unary expression was
8281 surrounded by parentheses.
8283 if (no_toplevel_fold_p
8284 && lookahead_prec <= current.prec
8285 && sp == stack)
8286 current.lhs = build2 (current.tree_type,
8287 TREE_CODE_CLASS (current.tree_type)
8288 == tcc_comparison
8289 ? boolean_type_node : TREE_TYPE (current.lhs),
8290 current.lhs, rhs);
8291 else
8292 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8293 current.lhs, current.lhs_type,
8294 rhs, rhs_type, &overload,
8295 complain_flags (decltype_p));
8296 current.lhs_type = current.tree_type;
8297 if (EXPR_P (current.lhs))
8298 SET_EXPR_LOCATION (current.lhs, current.loc);
8300 /* If the binary operator required the use of an overloaded operator,
8301 then this expression cannot be an integral constant-expression.
8302 An overloaded operator can be used even if both operands are
8303 otherwise permissible in an integral constant-expression if at
8304 least one of the operands is of enumeration type. */
8306 if (overload
8307 && cp_parser_non_integral_constant_expression (parser,
8308 NIC_OVERLOADED))
8309 return error_mark_node;
8312 return current.lhs;
8315 static tree
8316 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8317 bool no_toplevel_fold_p,
8318 enum cp_parser_prec prec,
8319 cp_id_kind * pidk)
8321 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8322 /*decltype*/false, prec, pidk);
8325 /* Parse the `? expression : assignment-expression' part of a
8326 conditional-expression. The LOGICAL_OR_EXPR is the
8327 logical-or-expression that started the conditional-expression.
8328 Returns a representation of the entire conditional-expression.
8330 This routine is used by cp_parser_assignment_expression.
8332 ? expression : assignment-expression
8334 GNU Extensions:
8336 ? : assignment-expression */
8338 static tree
8339 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8341 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8342 tree assignment_expr;
8343 struct cp_token *token;
8344 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8346 /* Consume the `?' token. */
8347 cp_lexer_consume_token (parser->lexer);
8348 token = cp_lexer_peek_token (parser->lexer);
8349 if (cp_parser_allow_gnu_extensions_p (parser)
8350 && token->type == CPP_COLON)
8352 pedwarn (token->location, OPT_Wpedantic,
8353 "ISO C++ does not allow ?: with omitted middle operand");
8354 /* Implicit true clause. */
8355 expr = NULL_TREE;
8356 c_inhibit_evaluation_warnings +=
8357 folded_logical_or_expr == truthvalue_true_node;
8358 warn_for_omitted_condop (token->location, logical_or_expr);
8360 else
8362 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8363 parser->colon_corrects_to_scope_p = false;
8364 /* Parse the expression. */
8365 c_inhibit_evaluation_warnings +=
8366 folded_logical_or_expr == truthvalue_false_node;
8367 expr = cp_parser_expression (parser);
8368 c_inhibit_evaluation_warnings +=
8369 ((folded_logical_or_expr == truthvalue_true_node)
8370 - (folded_logical_or_expr == truthvalue_false_node));
8371 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8374 /* The next token should be a `:'. */
8375 cp_parser_require (parser, CPP_COLON, RT_COLON);
8376 /* Parse the assignment-expression. */
8377 assignment_expr = cp_parser_assignment_expression (parser);
8378 c_inhibit_evaluation_warnings -=
8379 folded_logical_or_expr == truthvalue_true_node;
8381 /* Build the conditional-expression. */
8382 return build_x_conditional_expr (loc, logical_or_expr,
8383 expr,
8384 assignment_expr,
8385 tf_warning_or_error);
8388 /* Parse an assignment-expression.
8390 assignment-expression:
8391 conditional-expression
8392 logical-or-expression assignment-operator assignment_expression
8393 throw-expression
8395 CAST_P is true if this expression is the target of a cast.
8396 DECLTYPE_P is true if this expression is the operand of decltype.
8398 Returns a representation for the expression. */
8400 static tree
8401 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8402 bool cast_p, bool decltype_p)
8404 tree expr;
8406 /* If the next token is the `throw' keyword, then we're looking at
8407 a throw-expression. */
8408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8409 expr = cp_parser_throw_expression (parser);
8410 /* Otherwise, it must be that we are looking at a
8411 logical-or-expression. */
8412 else
8414 /* Parse the binary expressions (logical-or-expression). */
8415 expr = cp_parser_binary_expression (parser, cast_p, false,
8416 decltype_p,
8417 PREC_NOT_OPERATOR, pidk);
8418 /* If the next token is a `?' then we're actually looking at a
8419 conditional-expression. */
8420 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8421 return cp_parser_question_colon_clause (parser, expr);
8422 else
8424 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8426 /* If it's an assignment-operator, we're using the second
8427 production. */
8428 enum tree_code assignment_operator
8429 = cp_parser_assignment_operator_opt (parser);
8430 if (assignment_operator != ERROR_MARK)
8432 bool non_constant_p;
8433 location_t saved_input_location;
8435 /* Parse the right-hand side of the assignment. */
8436 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8438 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8441 /* An assignment may not appear in a
8442 constant-expression. */
8443 if (cp_parser_non_integral_constant_expression (parser,
8444 NIC_ASSIGNMENT))
8445 return error_mark_node;
8446 /* Build the assignment expression. Its default
8447 location is the location of the '=' token. */
8448 saved_input_location = input_location;
8449 input_location = loc;
8450 expr = build_x_modify_expr (loc, expr,
8451 assignment_operator,
8452 rhs,
8453 complain_flags (decltype_p));
8454 input_location = saved_input_location;
8459 return expr;
8462 /* Parse an (optional) assignment-operator.
8464 assignment-operator: one of
8465 = *= /= %= += -= >>= <<= &= ^= |=
8467 GNU Extension:
8469 assignment-operator: one of
8470 <?= >?=
8472 If the next token is an assignment operator, the corresponding tree
8473 code is returned, and the token is consumed. For example, for
8474 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8475 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8476 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8477 operator, ERROR_MARK is returned. */
8479 static enum tree_code
8480 cp_parser_assignment_operator_opt (cp_parser* parser)
8482 enum tree_code op;
8483 cp_token *token;
8485 /* Peek at the next token. */
8486 token = cp_lexer_peek_token (parser->lexer);
8488 switch (token->type)
8490 case CPP_EQ:
8491 op = NOP_EXPR;
8492 break;
8494 case CPP_MULT_EQ:
8495 op = MULT_EXPR;
8496 break;
8498 case CPP_DIV_EQ:
8499 op = TRUNC_DIV_EXPR;
8500 break;
8502 case CPP_MOD_EQ:
8503 op = TRUNC_MOD_EXPR;
8504 break;
8506 case CPP_PLUS_EQ:
8507 op = PLUS_EXPR;
8508 break;
8510 case CPP_MINUS_EQ:
8511 op = MINUS_EXPR;
8512 break;
8514 case CPP_RSHIFT_EQ:
8515 op = RSHIFT_EXPR;
8516 break;
8518 case CPP_LSHIFT_EQ:
8519 op = LSHIFT_EXPR;
8520 break;
8522 case CPP_AND_EQ:
8523 op = BIT_AND_EXPR;
8524 break;
8526 case CPP_XOR_EQ:
8527 op = BIT_XOR_EXPR;
8528 break;
8530 case CPP_OR_EQ:
8531 op = BIT_IOR_EXPR;
8532 break;
8534 default:
8535 /* Nothing else is an assignment operator. */
8536 op = ERROR_MARK;
8539 /* If it was an assignment operator, consume it. */
8540 if (op != ERROR_MARK)
8541 cp_lexer_consume_token (parser->lexer);
8543 return op;
8546 /* Parse an expression.
8548 expression:
8549 assignment-expression
8550 expression , assignment-expression
8552 CAST_P is true if this expression is the target of a cast.
8553 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8554 except possibly parenthesized or on the RHS of a comma (N3276).
8556 Returns a representation of the expression. */
8558 static tree
8559 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8560 bool cast_p, bool decltype_p)
8562 tree expression = NULL_TREE;
8563 location_t loc = UNKNOWN_LOCATION;
8565 while (true)
8567 tree assignment_expression;
8569 /* Parse the next assignment-expression. */
8570 assignment_expression
8571 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8573 /* We don't create a temporary for a call that is the immediate operand
8574 of decltype or on the RHS of a comma. But when we see a comma, we
8575 need to create a temporary for a call on the LHS. */
8576 if (decltype_p && !processing_template_decl
8577 && TREE_CODE (assignment_expression) == CALL_EXPR
8578 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8579 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8580 assignment_expression
8581 = build_cplus_new (TREE_TYPE (assignment_expression),
8582 assignment_expression, tf_warning_or_error);
8584 /* If this is the first assignment-expression, we can just
8585 save it away. */
8586 if (!expression)
8587 expression = assignment_expression;
8588 else
8589 expression = build_x_compound_expr (loc, expression,
8590 assignment_expression,
8591 complain_flags (decltype_p));
8592 /* If the next token is not a comma, then we are done with the
8593 expression. */
8594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8595 break;
8596 /* Consume the `,'. */
8597 loc = cp_lexer_peek_token (parser->lexer)->location;
8598 cp_lexer_consume_token (parser->lexer);
8599 /* A comma operator cannot appear in a constant-expression. */
8600 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8601 expression = error_mark_node;
8604 return expression;
8607 /* Parse a constant-expression.
8609 constant-expression:
8610 conditional-expression
8612 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8613 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8614 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8615 is false, NON_CONSTANT_P should be NULL. */
8617 static tree
8618 cp_parser_constant_expression (cp_parser* parser,
8619 bool allow_non_constant_p,
8620 bool *non_constant_p)
8622 bool saved_integral_constant_expression_p;
8623 bool saved_allow_non_integral_constant_expression_p;
8624 bool saved_non_integral_constant_expression_p;
8625 tree expression;
8627 /* It might seem that we could simply parse the
8628 conditional-expression, and then check to see if it were
8629 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8630 one that the compiler can figure out is constant, possibly after
8631 doing some simplifications or optimizations. The standard has a
8632 precise definition of constant-expression, and we must honor
8633 that, even though it is somewhat more restrictive.
8635 For example:
8637 int i[(2, 3)];
8639 is not a legal declaration, because `(2, 3)' is not a
8640 constant-expression. The `,' operator is forbidden in a
8641 constant-expression. However, GCC's constant-folding machinery
8642 will fold this operation to an INTEGER_CST for `3'. */
8644 /* Save the old settings. */
8645 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8646 saved_allow_non_integral_constant_expression_p
8647 = parser->allow_non_integral_constant_expression_p;
8648 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8649 /* We are now parsing a constant-expression. */
8650 parser->integral_constant_expression_p = true;
8651 parser->allow_non_integral_constant_expression_p
8652 = (allow_non_constant_p || cxx_dialect >= cxx11);
8653 parser->non_integral_constant_expression_p = false;
8654 /* Although the grammar says "conditional-expression", we parse an
8655 "assignment-expression", which also permits "throw-expression"
8656 and the use of assignment operators. In the case that
8657 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8658 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8659 actually essential that we look for an assignment-expression.
8660 For example, cp_parser_initializer_clauses uses this function to
8661 determine whether a particular assignment-expression is in fact
8662 constant. */
8663 expression = cp_parser_assignment_expression (parser);
8664 /* Restore the old settings. */
8665 parser->integral_constant_expression_p
8666 = saved_integral_constant_expression_p;
8667 parser->allow_non_integral_constant_expression_p
8668 = saved_allow_non_integral_constant_expression_p;
8669 if (cxx_dialect >= cxx11)
8671 /* Require an rvalue constant expression here; that's what our
8672 callers expect. Reference constant expressions are handled
8673 separately in e.g. cp_parser_template_argument. */
8674 bool is_const = potential_rvalue_constant_expression (expression);
8675 parser->non_integral_constant_expression_p = !is_const;
8676 if (!is_const && !allow_non_constant_p)
8677 require_potential_rvalue_constant_expression (expression);
8679 if (allow_non_constant_p)
8680 *non_constant_p = parser->non_integral_constant_expression_p;
8681 parser->non_integral_constant_expression_p
8682 = saved_non_integral_constant_expression_p;
8684 return expression;
8687 /* Parse __builtin_offsetof.
8689 offsetof-expression:
8690 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8692 offsetof-member-designator:
8693 id-expression
8694 | offsetof-member-designator "." id-expression
8695 | offsetof-member-designator "[" expression "]"
8696 | offsetof-member-designator "->" id-expression */
8698 static tree
8699 cp_parser_builtin_offsetof (cp_parser *parser)
8701 int save_ice_p, save_non_ice_p;
8702 tree type, expr;
8703 cp_id_kind dummy;
8704 cp_token *token;
8706 /* We're about to accept non-integral-constant things, but will
8707 definitely yield an integral constant expression. Save and
8708 restore these values around our local parsing. */
8709 save_ice_p = parser->integral_constant_expression_p;
8710 save_non_ice_p = parser->non_integral_constant_expression_p;
8712 /* Consume the "__builtin_offsetof" token. */
8713 cp_lexer_consume_token (parser->lexer);
8714 /* Consume the opening `('. */
8715 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8716 /* Parse the type-id. */
8717 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8718 type = cp_parser_type_id (parser);
8719 /* Look for the `,'. */
8720 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8721 token = cp_lexer_peek_token (parser->lexer);
8723 /* Build the (type *)null that begins the traditional offsetof macro. */
8724 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8725 tf_warning_or_error);
8727 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8728 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8729 true, &dummy, token->location);
8730 while (true)
8732 token = cp_lexer_peek_token (parser->lexer);
8733 switch (token->type)
8735 case CPP_OPEN_SQUARE:
8736 /* offsetof-member-designator "[" expression "]" */
8737 expr = cp_parser_postfix_open_square_expression (parser, expr,
8738 true, false);
8739 break;
8741 case CPP_DEREF:
8742 /* offsetof-member-designator "->" identifier */
8743 expr = grok_array_decl (token->location, expr,
8744 integer_zero_node, false);
8745 /* FALLTHRU */
8747 case CPP_DOT:
8748 /* offsetof-member-designator "." identifier */
8749 cp_lexer_consume_token (parser->lexer);
8750 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8751 expr, true, &dummy,
8752 token->location);
8753 break;
8755 case CPP_CLOSE_PAREN:
8756 /* Consume the ")" token. */
8757 cp_lexer_consume_token (parser->lexer);
8758 goto success;
8760 default:
8761 /* Error. We know the following require will fail, but
8762 that gives the proper error message. */
8763 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8764 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8765 expr = error_mark_node;
8766 goto failure;
8770 success:
8771 expr = finish_offsetof (expr, loc);
8773 failure:
8774 parser->integral_constant_expression_p = save_ice_p;
8775 parser->non_integral_constant_expression_p = save_non_ice_p;
8777 return expr;
8780 /* Parse a trait expression.
8782 Returns a representation of the expression, the underlying type
8783 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8785 static tree
8786 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8788 cp_trait_kind kind;
8789 tree type1, type2 = NULL_TREE;
8790 bool binary = false;
8791 bool variadic = false;
8793 switch (keyword)
8795 case RID_HAS_NOTHROW_ASSIGN:
8796 kind = CPTK_HAS_NOTHROW_ASSIGN;
8797 break;
8798 case RID_HAS_NOTHROW_CONSTRUCTOR:
8799 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8800 break;
8801 case RID_HAS_NOTHROW_COPY:
8802 kind = CPTK_HAS_NOTHROW_COPY;
8803 break;
8804 case RID_HAS_TRIVIAL_ASSIGN:
8805 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8806 break;
8807 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8808 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8809 break;
8810 case RID_HAS_TRIVIAL_COPY:
8811 kind = CPTK_HAS_TRIVIAL_COPY;
8812 break;
8813 case RID_HAS_TRIVIAL_DESTRUCTOR:
8814 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8815 break;
8816 case RID_HAS_VIRTUAL_DESTRUCTOR:
8817 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8818 break;
8819 case RID_IS_ABSTRACT:
8820 kind = CPTK_IS_ABSTRACT;
8821 break;
8822 case RID_IS_BASE_OF:
8823 kind = CPTK_IS_BASE_OF;
8824 binary = true;
8825 break;
8826 case RID_IS_CLASS:
8827 kind = CPTK_IS_CLASS;
8828 break;
8829 case RID_IS_EMPTY:
8830 kind = CPTK_IS_EMPTY;
8831 break;
8832 case RID_IS_ENUM:
8833 kind = CPTK_IS_ENUM;
8834 break;
8835 case RID_IS_FINAL:
8836 kind = CPTK_IS_FINAL;
8837 break;
8838 case RID_IS_LITERAL_TYPE:
8839 kind = CPTK_IS_LITERAL_TYPE;
8840 break;
8841 case RID_IS_POD:
8842 kind = CPTK_IS_POD;
8843 break;
8844 case RID_IS_POLYMORPHIC:
8845 kind = CPTK_IS_POLYMORPHIC;
8846 break;
8847 case RID_IS_STD_LAYOUT:
8848 kind = CPTK_IS_STD_LAYOUT;
8849 break;
8850 case RID_IS_TRIVIAL:
8851 kind = CPTK_IS_TRIVIAL;
8852 break;
8853 case RID_IS_TRIVIALLY_ASSIGNABLE:
8854 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8855 binary = true;
8856 break;
8857 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8858 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8859 variadic = true;
8860 break;
8861 case RID_IS_TRIVIALLY_COPYABLE:
8862 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8863 break;
8864 case RID_IS_UNION:
8865 kind = CPTK_IS_UNION;
8866 break;
8867 case RID_UNDERLYING_TYPE:
8868 kind = CPTK_UNDERLYING_TYPE;
8869 break;
8870 case RID_BASES:
8871 kind = CPTK_BASES;
8872 break;
8873 case RID_DIRECT_BASES:
8874 kind = CPTK_DIRECT_BASES;
8875 break;
8876 default:
8877 gcc_unreachable ();
8880 /* Consume the token. */
8881 cp_lexer_consume_token (parser->lexer);
8883 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8885 type1 = cp_parser_type_id (parser);
8887 if (type1 == error_mark_node)
8888 return error_mark_node;
8890 if (binary)
8892 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8894 type2 = cp_parser_type_id (parser);
8896 if (type2 == error_mark_node)
8897 return error_mark_node;
8899 else if (variadic)
8901 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8903 cp_lexer_consume_token (parser->lexer);
8904 tree elt = cp_parser_type_id (parser);
8905 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8907 cp_lexer_consume_token (parser->lexer);
8908 elt = make_pack_expansion (elt);
8910 if (elt == error_mark_node)
8911 return error_mark_node;
8912 type2 = tree_cons (NULL_TREE, elt, type2);
8916 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8918 /* Complete the trait expression, which may mean either processing
8919 the trait expr now or saving it for template instantiation. */
8920 switch(kind)
8922 case CPTK_UNDERLYING_TYPE:
8923 return finish_underlying_type (type1);
8924 case CPTK_BASES:
8925 return finish_bases (type1, false);
8926 case CPTK_DIRECT_BASES:
8927 return finish_bases (type1, true);
8928 default:
8929 return finish_trait_expr (kind, type1, type2);
8933 /* Lambdas that appear in variable initializer or default argument scope
8934 get that in their mangling, so we need to record it. We might as well
8935 use the count for function and namespace scopes as well. */
8936 static GTY(()) tree lambda_scope;
8937 static GTY(()) int lambda_count;
8938 typedef struct GTY(()) tree_int
8940 tree t;
8941 int i;
8942 } tree_int;
8943 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8945 static void
8946 start_lambda_scope (tree decl)
8948 tree_int ti;
8949 gcc_assert (decl);
8950 /* Once we're inside a function, we ignore other scopes and just push
8951 the function again so that popping works properly. */
8952 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8953 decl = current_function_decl;
8954 ti.t = lambda_scope;
8955 ti.i = lambda_count;
8956 vec_safe_push (lambda_scope_stack, ti);
8957 if (lambda_scope != decl)
8959 /* Don't reset the count if we're still in the same function. */
8960 lambda_scope = decl;
8961 lambda_count = 0;
8965 static void
8966 record_lambda_scope (tree lambda)
8968 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8969 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8972 static void
8973 finish_lambda_scope (void)
8975 tree_int *p = &lambda_scope_stack->last ();
8976 if (lambda_scope != p->t)
8978 lambda_scope = p->t;
8979 lambda_count = p->i;
8981 lambda_scope_stack->pop ();
8984 /* Parse a lambda expression.
8986 lambda-expression:
8987 lambda-introducer lambda-declarator [opt] compound-statement
8989 Returns a representation of the expression. */
8991 static tree
8992 cp_parser_lambda_expression (cp_parser* parser)
8994 tree lambda_expr = build_lambda_expr ();
8995 tree type;
8996 bool ok = true;
8997 cp_token *token = cp_lexer_peek_token (parser->lexer);
8998 cp_token_position start = 0;
9000 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9002 if (cp_unevaluated_operand)
9004 if (!token->error_reported)
9006 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9007 "lambda-expression in unevaluated context");
9008 token->error_reported = true;
9010 ok = false;
9012 else if (parser->in_template_argument_list_p)
9014 if (!token->error_reported)
9016 error_at (token->location, "lambda-expression in template-argument");
9017 token->error_reported = true;
9019 ok = false;
9022 /* We may be in the middle of deferred access check. Disable
9023 it now. */
9024 push_deferring_access_checks (dk_no_deferred);
9026 cp_parser_lambda_introducer (parser, lambda_expr);
9028 type = begin_lambda_type (lambda_expr);
9029 if (type == error_mark_node)
9030 return error_mark_node;
9032 record_lambda_scope (lambda_expr);
9034 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9035 determine_visibility (TYPE_NAME (type));
9037 /* Now that we've started the type, add the capture fields for any
9038 explicit captures. */
9039 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9042 /* Inside the class, surrounding template-parameter-lists do not apply. */
9043 unsigned int saved_num_template_parameter_lists
9044 = parser->num_template_parameter_lists;
9045 unsigned char in_statement = parser->in_statement;
9046 bool in_switch_statement_p = parser->in_switch_statement_p;
9047 bool fully_implicit_function_template_p
9048 = parser->fully_implicit_function_template_p;
9049 tree implicit_template_parms = parser->implicit_template_parms;
9050 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9051 bool auto_is_implicit_function_template_parm_p
9052 = parser->auto_is_implicit_function_template_parm_p;
9054 parser->num_template_parameter_lists = 0;
9055 parser->in_statement = 0;
9056 parser->in_switch_statement_p = false;
9057 parser->fully_implicit_function_template_p = false;
9058 parser->implicit_template_parms = 0;
9059 parser->implicit_template_scope = 0;
9060 parser->auto_is_implicit_function_template_parm_p = false;
9062 /* By virtue of defining a local class, a lambda expression has access to
9063 the private variables of enclosing classes. */
9065 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9067 if (ok)
9069 if (!cp_parser_error_occurred (parser)
9070 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9071 && cp_parser_start_tentative_firewall (parser))
9072 start = token;
9073 cp_parser_lambda_body (parser, lambda_expr);
9075 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9077 if (cp_parser_skip_to_closing_brace (parser))
9078 cp_lexer_consume_token (parser->lexer);
9081 /* The capture list was built up in reverse order; fix that now. */
9082 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9083 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9085 if (ok)
9086 maybe_add_lambda_conv_op (type);
9088 type = finish_struct (type, /*attributes=*/NULL_TREE);
9090 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9091 parser->in_statement = in_statement;
9092 parser->in_switch_statement_p = in_switch_statement_p;
9093 parser->fully_implicit_function_template_p
9094 = fully_implicit_function_template_p;
9095 parser->implicit_template_parms = implicit_template_parms;
9096 parser->implicit_template_scope = implicit_template_scope;
9097 parser->auto_is_implicit_function_template_parm_p
9098 = auto_is_implicit_function_template_parm_p;
9101 pop_deferring_access_checks ();
9103 /* This field is only used during parsing of the lambda. */
9104 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9106 /* This lambda shouldn't have any proxies left at this point. */
9107 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9108 /* And now that we're done, push proxies for an enclosing lambda. */
9109 insert_pending_capture_proxies ();
9111 if (ok)
9112 lambda_expr = build_lambda_object (lambda_expr);
9113 else
9114 lambda_expr = error_mark_node;
9116 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9118 return lambda_expr;
9121 /* Parse the beginning of a lambda expression.
9123 lambda-introducer:
9124 [ lambda-capture [opt] ]
9126 LAMBDA_EXPR is the current representation of the lambda expression. */
9128 static void
9129 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9131 /* Need commas after the first capture. */
9132 bool first = true;
9134 /* Eat the leading `['. */
9135 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9137 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9138 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9139 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9140 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9141 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9142 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9144 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9146 cp_lexer_consume_token (parser->lexer);
9147 first = false;
9150 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9152 cp_token* capture_token;
9153 tree capture_id;
9154 tree capture_init_expr;
9155 cp_id_kind idk = CP_ID_KIND_NONE;
9156 bool explicit_init_p = false;
9158 enum capture_kind_type
9160 BY_COPY,
9161 BY_REFERENCE
9163 enum capture_kind_type capture_kind = BY_COPY;
9165 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9167 error ("expected end of capture-list");
9168 return;
9171 if (first)
9172 first = false;
9173 else
9174 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9176 /* Possibly capture `this'. */
9177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9180 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9181 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9182 "with by-copy capture default");
9183 cp_lexer_consume_token (parser->lexer);
9184 add_capture (lambda_expr,
9185 /*id=*/this_identifier,
9186 /*initializer=*/finish_this_expr(),
9187 /*by_reference_p=*/false,
9188 explicit_init_p);
9189 continue;
9192 /* Remember whether we want to capture as a reference or not. */
9193 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9195 capture_kind = BY_REFERENCE;
9196 cp_lexer_consume_token (parser->lexer);
9199 /* Get the identifier. */
9200 capture_token = cp_lexer_peek_token (parser->lexer);
9201 capture_id = cp_parser_identifier (parser);
9203 if (capture_id == error_mark_node)
9204 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9205 delimiters, but I modified this to stop on unnested ']' as well. It
9206 was already changed to stop on unnested '}', so the
9207 "closing_parenthesis" name is no more misleading with my change. */
9209 cp_parser_skip_to_closing_parenthesis (parser,
9210 /*recovering=*/true,
9211 /*or_comma=*/true,
9212 /*consume_paren=*/true);
9213 break;
9216 /* Find the initializer for this capture. */
9217 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9218 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9219 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9221 bool direct, non_constant;
9222 /* An explicit initializer exists. */
9223 if (cxx_dialect < cxx14)
9224 pedwarn (input_location, 0,
9225 "lambda capture initializers "
9226 "only available with -std=c++14 or -std=gnu++14");
9227 capture_init_expr = cp_parser_initializer (parser, &direct,
9228 &non_constant);
9229 explicit_init_p = true;
9230 if (capture_init_expr == NULL_TREE)
9232 error ("empty initializer for lambda init-capture");
9233 capture_init_expr = error_mark_node;
9236 else
9238 const char* error_msg;
9240 /* Turn the identifier into an id-expression. */
9241 capture_init_expr
9242 = cp_parser_lookup_name_simple (parser, capture_id,
9243 capture_token->location);
9245 if (capture_init_expr == error_mark_node)
9247 unqualified_name_lookup_error (capture_id);
9248 continue;
9250 else if (DECL_P (capture_init_expr)
9251 && (!VAR_P (capture_init_expr)
9252 && TREE_CODE (capture_init_expr) != PARM_DECL))
9254 error_at (capture_token->location,
9255 "capture of non-variable %qD ",
9256 capture_init_expr);
9257 inform (0, "%q+#D declared here", capture_init_expr);
9258 continue;
9260 if (VAR_P (capture_init_expr)
9261 && decl_storage_duration (capture_init_expr) != dk_auto)
9263 if (pedwarn (capture_token->location, 0, "capture of variable "
9264 "%qD with non-automatic storage duration",
9265 capture_init_expr))
9266 inform (0, "%q+#D declared here", capture_init_expr);
9267 continue;
9270 capture_init_expr
9271 = finish_id_expression
9272 (capture_id,
9273 capture_init_expr,
9274 parser->scope,
9275 &idk,
9276 /*integral_constant_expression_p=*/false,
9277 /*allow_non_integral_constant_expression_p=*/false,
9278 /*non_integral_constant_expression_p=*/NULL,
9279 /*template_p=*/false,
9280 /*done=*/true,
9281 /*address_p=*/false,
9282 /*template_arg_p=*/false,
9283 &error_msg,
9284 capture_token->location);
9286 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9288 cp_lexer_consume_token (parser->lexer);
9289 capture_init_expr = make_pack_expansion (capture_init_expr);
9291 else
9292 check_for_bare_parameter_packs (capture_init_expr);
9295 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9296 && !explicit_init_p)
9298 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9299 && capture_kind == BY_COPY)
9300 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9301 "of %qD redundant with by-copy capture default",
9302 capture_id);
9303 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9304 && capture_kind == BY_REFERENCE)
9305 pedwarn (capture_token->location, 0, "explicit by-reference "
9306 "capture of %qD redundant with by-reference capture "
9307 "default", capture_id);
9310 add_capture (lambda_expr,
9311 capture_id,
9312 capture_init_expr,
9313 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9314 explicit_init_p);
9317 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9320 /* Parse the (optional) middle of a lambda expression.
9322 lambda-declarator:
9323 < template-parameter-list [opt] >
9324 ( parameter-declaration-clause [opt] )
9325 attribute-specifier [opt]
9326 mutable [opt]
9327 exception-specification [opt]
9328 lambda-return-type-clause [opt]
9330 LAMBDA_EXPR is the current representation of the lambda expression. */
9332 static bool
9333 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9335 /* 5.1.1.4 of the standard says:
9336 If a lambda-expression does not include a lambda-declarator, it is as if
9337 the lambda-declarator were ().
9338 This means an empty parameter list, no attributes, and no exception
9339 specification. */
9340 tree param_list = void_list_node;
9341 tree attributes = NULL_TREE;
9342 tree exception_spec = NULL_TREE;
9343 tree template_param_list = NULL_TREE;
9345 /* The template-parameter-list is optional, but must begin with
9346 an opening angle if present. */
9347 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9349 if (cxx_dialect < cxx14)
9350 pedwarn (parser->lexer->next_token->location, 0,
9351 "lambda templates are only available with "
9352 "-std=c++14 or -std=gnu++14");
9354 cp_lexer_consume_token (parser->lexer);
9356 template_param_list = cp_parser_template_parameter_list (parser);
9358 cp_parser_skip_to_end_of_template_parameter_list (parser);
9360 /* We just processed one more parameter list. */
9361 ++parser->num_template_parameter_lists;
9364 /* The parameter-declaration-clause is optional (unless
9365 template-parameter-list was given), but must begin with an
9366 opening parenthesis if present. */
9367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9369 cp_lexer_consume_token (parser->lexer);
9371 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9373 /* Parse parameters. */
9374 param_list = cp_parser_parameter_declaration_clause (parser);
9376 /* Default arguments shall not be specified in the
9377 parameter-declaration-clause of a lambda-declarator. */
9378 for (tree t = param_list; t; t = TREE_CHAIN (t))
9379 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9380 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9381 "default argument specified for lambda parameter");
9383 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9385 attributes = cp_parser_attributes_opt (parser);
9387 /* Parse optional `mutable' keyword. */
9388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9390 cp_lexer_consume_token (parser->lexer);
9391 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9394 /* Parse optional exception specification. */
9395 exception_spec = cp_parser_exception_specification_opt (parser);
9397 /* Parse optional trailing return type. */
9398 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9400 cp_lexer_consume_token (parser->lexer);
9401 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9402 = cp_parser_trailing_type_id (parser);
9405 /* The function parameters must be in scope all the way until after the
9406 trailing-return-type in case of decltype. */
9407 pop_bindings_and_leave_scope ();
9409 else if (template_param_list != NULL_TREE) // generate diagnostic
9410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9412 /* Create the function call operator.
9414 Messing with declarators like this is no uglier than building up the
9415 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9416 other code. */
9418 cp_decl_specifier_seq return_type_specs;
9419 cp_declarator* declarator;
9420 tree fco;
9421 int quals;
9422 void *p;
9424 clear_decl_specs (&return_type_specs);
9425 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9426 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9427 else
9428 /* Maybe we will deduce the return type later. */
9429 return_type_specs.type = make_auto ();
9431 p = obstack_alloc (&declarator_obstack, 0);
9433 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9434 sfk_none);
9436 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9437 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9438 declarator = make_call_declarator (declarator, param_list, quals,
9439 VIRT_SPEC_UNSPECIFIED,
9440 REF_QUAL_NONE,
9441 exception_spec,
9442 /*late_return_type=*/NULL_TREE);
9443 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9445 fco = grokmethod (&return_type_specs,
9446 declarator,
9447 attributes);
9448 if (fco != error_mark_node)
9450 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9451 DECL_ARTIFICIAL (fco) = 1;
9452 /* Give the object parameter a different name. */
9453 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9454 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9455 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9457 if (template_param_list)
9459 fco = finish_member_template_decl (fco);
9460 finish_template_decl (template_param_list);
9461 --parser->num_template_parameter_lists;
9463 else if (parser->fully_implicit_function_template_p)
9464 fco = finish_fully_implicit_template (parser, fco);
9466 finish_member_declaration (fco);
9468 obstack_free (&declarator_obstack, p);
9470 return (fco != error_mark_node);
9474 /* Parse the body of a lambda expression, which is simply
9476 compound-statement
9478 but which requires special handling.
9479 LAMBDA_EXPR is the current representation of the lambda expression. */
9481 static void
9482 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9484 bool nested = (current_function_decl != NULL_TREE);
9485 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9486 if (nested)
9487 push_function_context ();
9488 else
9489 /* Still increment function_depth so that we don't GC in the
9490 middle of an expression. */
9491 ++function_depth;
9492 /* Clear this in case we're in the middle of a default argument. */
9493 parser->local_variables_forbidden_p = false;
9495 /* Finish the function call operator
9496 - class_specifier
9497 + late_parsing_for_member
9498 + function_definition_after_declarator
9499 + ctor_initializer_opt_and_function_body */
9501 tree fco = lambda_function (lambda_expr);
9502 tree body;
9503 bool done = false;
9504 tree compound_stmt;
9505 tree cap;
9507 /* Let the front end know that we are going to be defining this
9508 function. */
9509 start_preparsed_function (fco,
9510 NULL_TREE,
9511 SF_PRE_PARSED | SF_INCLASS_INLINE);
9513 start_lambda_scope (fco);
9514 body = begin_function_body ();
9516 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9517 goto out;
9519 /* Push the proxies for any explicit captures. */
9520 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9521 cap = TREE_CHAIN (cap))
9522 build_capture_proxy (TREE_PURPOSE (cap));
9524 compound_stmt = begin_compound_stmt (0);
9526 /* 5.1.1.4 of the standard says:
9527 If a lambda-expression does not include a trailing-return-type, it
9528 is as if the trailing-return-type denotes the following type:
9529 * if the compound-statement is of the form
9530 { return attribute-specifier [opt] expression ; }
9531 the type of the returned expression after lvalue-to-rvalue
9532 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9533 (_conv.array_ 4.2), and function-to-pointer conversion
9534 (_conv.func_ 4.3);
9535 * otherwise, void. */
9537 /* In a lambda that has neither a lambda-return-type-clause
9538 nor a deducible form, errors should be reported for return statements
9539 in the body. Since we used void as the placeholder return type, parsing
9540 the body as usual will give such desired behavior. */
9541 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9542 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9543 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9545 tree expr = NULL_TREE;
9546 cp_id_kind idk = CP_ID_KIND_NONE;
9548 /* Parse tentatively in case there's more after the initial return
9549 statement. */
9550 cp_parser_parse_tentatively (parser);
9552 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9554 expr = cp_parser_expression (parser, &idk);
9556 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9559 if (cp_parser_parse_definitely (parser))
9561 if (!processing_template_decl)
9562 apply_deduced_return_type (fco, lambda_return_type (expr));
9564 /* Will get error here if type not deduced yet. */
9565 finish_return_stmt (expr);
9567 done = true;
9571 if (!done)
9573 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9574 cp_parser_label_declaration (parser);
9575 cp_parser_statement_seq_opt (parser, NULL_TREE);
9576 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9579 finish_compound_stmt (compound_stmt);
9581 out:
9582 finish_function_body (body);
9583 finish_lambda_scope ();
9585 /* Finish the function and generate code for it if necessary. */
9586 tree fn = finish_function (/*inline*/2);
9588 /* Only expand if the call op is not a template. */
9589 if (!DECL_TEMPLATE_INFO (fco))
9590 expand_or_defer_fn (fn);
9593 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9594 if (nested)
9595 pop_function_context();
9596 else
9597 --function_depth;
9600 /* Statements [gram.stmt.stmt] */
9602 /* Parse a statement.
9604 statement:
9605 labeled-statement
9606 expression-statement
9607 compound-statement
9608 selection-statement
9609 iteration-statement
9610 jump-statement
9611 declaration-statement
9612 try-block
9614 C++11:
9616 statement:
9617 labeled-statement
9618 attribute-specifier-seq (opt) expression-statement
9619 attribute-specifier-seq (opt) compound-statement
9620 attribute-specifier-seq (opt) selection-statement
9621 attribute-specifier-seq (opt) iteration-statement
9622 attribute-specifier-seq (opt) jump-statement
9623 declaration-statement
9624 attribute-specifier-seq (opt) try-block
9626 TM Extension:
9628 statement:
9629 atomic-statement
9631 IN_COMPOUND is true when the statement is nested inside a
9632 cp_parser_compound_statement; this matters for certain pragmas.
9634 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9635 is a (possibly labeled) if statement which is not enclosed in braces
9636 and has an else clause. This is used to implement -Wparentheses. */
9638 static void
9639 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9640 bool in_compound, bool *if_p)
9642 tree statement, std_attrs = NULL_TREE;
9643 cp_token *token;
9644 location_t statement_location, attrs_location;
9646 restart:
9647 if (if_p != NULL)
9648 *if_p = false;
9649 /* There is no statement yet. */
9650 statement = NULL_TREE;
9652 saved_token_sentinel saved_tokens (parser->lexer);
9653 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9654 if (c_dialect_objc ())
9655 /* In obj-c++, seeing '[[' might be the either the beginning of
9656 c++11 attributes, or a nested objc-message-expression. So
9657 let's parse the c++11 attributes tentatively. */
9658 cp_parser_parse_tentatively (parser);
9659 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9660 if (c_dialect_objc ())
9662 if (!cp_parser_parse_definitely (parser))
9663 std_attrs = NULL_TREE;
9666 /* Peek at the next token. */
9667 token = cp_lexer_peek_token (parser->lexer);
9668 /* Remember the location of the first token in the statement. */
9669 statement_location = token->location;
9670 /* If this is a keyword, then that will often determine what kind of
9671 statement we have. */
9672 if (token->type == CPP_KEYWORD)
9674 enum rid keyword = token->keyword;
9676 switch (keyword)
9678 case RID_CASE:
9679 case RID_DEFAULT:
9680 /* Looks like a labeled-statement with a case label.
9681 Parse the label, and then use tail recursion to parse
9682 the statement. */
9683 cp_parser_label_for_labeled_statement (parser, std_attrs);
9684 goto restart;
9686 case RID_IF:
9687 case RID_SWITCH:
9688 statement = cp_parser_selection_statement (parser, if_p);
9689 break;
9691 case RID_WHILE:
9692 case RID_DO:
9693 case RID_FOR:
9694 statement = cp_parser_iteration_statement (parser, false);
9695 break;
9697 case RID_CILK_FOR:
9698 if (!flag_cilkplus)
9700 error_at (cp_lexer_peek_token (parser->lexer)->location,
9701 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9702 cp_lexer_consume_token (parser->lexer);
9703 statement = error_mark_node;
9705 else
9706 statement = cp_parser_cilk_for (parser, integer_zero_node);
9707 break;
9709 case RID_BREAK:
9710 case RID_CONTINUE:
9711 case RID_RETURN:
9712 case RID_GOTO:
9713 statement = cp_parser_jump_statement (parser);
9714 break;
9716 case RID_CILK_SYNC:
9717 cp_lexer_consume_token (parser->lexer);
9718 if (flag_cilkplus)
9720 tree sync_expr = build_cilk_sync ();
9721 SET_EXPR_LOCATION (sync_expr,
9722 token->location);
9723 statement = finish_expr_stmt (sync_expr);
9725 else
9727 error_at (token->location, "-fcilkplus must be enabled to use"
9728 " %<_Cilk_sync%>");
9729 statement = error_mark_node;
9731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9732 break;
9734 /* Objective-C++ exception-handling constructs. */
9735 case RID_AT_TRY:
9736 case RID_AT_CATCH:
9737 case RID_AT_FINALLY:
9738 case RID_AT_SYNCHRONIZED:
9739 case RID_AT_THROW:
9740 statement = cp_parser_objc_statement (parser);
9741 break;
9743 case RID_TRY:
9744 statement = cp_parser_try_block (parser);
9745 break;
9747 case RID_NAMESPACE:
9748 /* This must be a namespace alias definition. */
9749 cp_parser_declaration_statement (parser);
9750 return;
9752 case RID_TRANSACTION_ATOMIC:
9753 case RID_TRANSACTION_RELAXED:
9754 statement = cp_parser_transaction (parser, keyword);
9755 break;
9756 case RID_TRANSACTION_CANCEL:
9757 statement = cp_parser_transaction_cancel (parser);
9758 break;
9760 default:
9761 /* It might be a keyword like `int' that can start a
9762 declaration-statement. */
9763 break;
9766 else if (token->type == CPP_NAME)
9768 /* If the next token is a `:', then we are looking at a
9769 labeled-statement. */
9770 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9771 if (token->type == CPP_COLON)
9773 /* Looks like a labeled-statement with an ordinary label.
9774 Parse the label, and then use tail recursion to parse
9775 the statement. */
9777 cp_parser_label_for_labeled_statement (parser, std_attrs);
9778 goto restart;
9781 /* Anything that starts with a `{' must be a compound-statement. */
9782 else if (token->type == CPP_OPEN_BRACE)
9783 statement = cp_parser_compound_statement (parser, NULL, false, false);
9784 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9785 a statement all its own. */
9786 else if (token->type == CPP_PRAGMA)
9788 /* Only certain OpenMP pragmas are attached to statements, and thus
9789 are considered statements themselves. All others are not. In
9790 the context of a compound, accept the pragma as a "statement" and
9791 return so that we can check for a close brace. Otherwise we
9792 require a real statement and must go back and read one. */
9793 if (in_compound)
9794 cp_parser_pragma (parser, pragma_compound);
9795 else if (!cp_parser_pragma (parser, pragma_stmt))
9796 goto restart;
9797 return;
9799 else if (token->type == CPP_EOF)
9801 cp_parser_error (parser, "expected statement");
9802 return;
9805 /* Everything else must be a declaration-statement or an
9806 expression-statement. Try for the declaration-statement
9807 first, unless we are looking at a `;', in which case we know that
9808 we have an expression-statement. */
9809 if (!statement)
9811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9813 if (std_attrs != NULL_TREE)
9815 /* Attributes should be parsed as part of the the
9816 declaration, so let's un-parse them. */
9817 saved_tokens.rollback();
9818 std_attrs = NULL_TREE;
9821 cp_parser_parse_tentatively (parser);
9822 /* Try to parse the declaration-statement. */
9823 cp_parser_declaration_statement (parser);
9824 /* If that worked, we're done. */
9825 if (cp_parser_parse_definitely (parser))
9826 return;
9828 /* Look for an expression-statement instead. */
9829 statement = cp_parser_expression_statement (parser, in_statement_expr);
9832 /* Set the line number for the statement. */
9833 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9834 SET_EXPR_LOCATION (statement, statement_location);
9836 /* Note that for now, we don't do anything with c++11 statements
9837 parsed at this level. */
9838 if (std_attrs != NULL_TREE)
9839 warning_at (attrs_location,
9840 OPT_Wattributes,
9841 "attributes at the beginning of statement are ignored");
9844 /* Parse the label for a labeled-statement, i.e.
9846 identifier :
9847 case constant-expression :
9848 default :
9850 GNU Extension:
9851 case constant-expression ... constant-expression : statement
9853 When a label is parsed without errors, the label is added to the
9854 parse tree by the finish_* functions, so this function doesn't
9855 have to return the label. */
9857 static void
9858 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9860 cp_token *token;
9861 tree label = NULL_TREE;
9862 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9864 /* The next token should be an identifier. */
9865 token = cp_lexer_peek_token (parser->lexer);
9866 if (token->type != CPP_NAME
9867 && token->type != CPP_KEYWORD)
9869 cp_parser_error (parser, "expected labeled-statement");
9870 return;
9873 parser->colon_corrects_to_scope_p = false;
9874 switch (token->keyword)
9876 case RID_CASE:
9878 tree expr, expr_hi;
9879 cp_token *ellipsis;
9881 /* Consume the `case' token. */
9882 cp_lexer_consume_token (parser->lexer);
9883 /* Parse the constant-expression. */
9884 expr = cp_parser_constant_expression (parser);
9885 if (check_for_bare_parameter_packs (expr))
9886 expr = error_mark_node;
9888 ellipsis = cp_lexer_peek_token (parser->lexer);
9889 if (ellipsis->type == CPP_ELLIPSIS)
9891 /* Consume the `...' token. */
9892 cp_lexer_consume_token (parser->lexer);
9893 expr_hi = cp_parser_constant_expression (parser);
9894 if (check_for_bare_parameter_packs (expr_hi))
9895 expr_hi = error_mark_node;
9897 /* We don't need to emit warnings here, as the common code
9898 will do this for us. */
9900 else
9901 expr_hi = NULL_TREE;
9903 if (parser->in_switch_statement_p)
9904 finish_case_label (token->location, expr, expr_hi);
9905 else
9906 error_at (token->location,
9907 "case label %qE not within a switch statement",
9908 expr);
9910 break;
9912 case RID_DEFAULT:
9913 /* Consume the `default' token. */
9914 cp_lexer_consume_token (parser->lexer);
9916 if (parser->in_switch_statement_p)
9917 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9918 else
9919 error_at (token->location, "case label not within a switch statement");
9920 break;
9922 default:
9923 /* Anything else must be an ordinary label. */
9924 label = finish_label_stmt (cp_parser_identifier (parser));
9925 break;
9928 /* Require the `:' token. */
9929 cp_parser_require (parser, CPP_COLON, RT_COLON);
9931 /* An ordinary label may optionally be followed by attributes.
9932 However, this is only permitted if the attributes are then
9933 followed by a semicolon. This is because, for backward
9934 compatibility, when parsing
9935 lab: __attribute__ ((unused)) int i;
9936 we want the attribute to attach to "i", not "lab". */
9937 if (label != NULL_TREE
9938 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9940 tree attrs;
9941 cp_parser_parse_tentatively (parser);
9942 attrs = cp_parser_gnu_attributes_opt (parser);
9943 if (attrs == NULL_TREE
9944 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9945 cp_parser_abort_tentative_parse (parser);
9946 else if (!cp_parser_parse_definitely (parser))
9948 else
9949 attributes = chainon (attributes, attrs);
9952 if (attributes != NULL_TREE)
9953 cplus_decl_attributes (&label, attributes, 0);
9955 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9958 /* Parse an expression-statement.
9960 expression-statement:
9961 expression [opt] ;
9963 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9964 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9965 indicates whether this expression-statement is part of an
9966 expression statement. */
9968 static tree
9969 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9971 tree statement = NULL_TREE;
9972 cp_token *token = cp_lexer_peek_token (parser->lexer);
9974 /* If the next token is a ';', then there is no expression
9975 statement. */
9976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9978 statement = cp_parser_expression (parser);
9979 if (statement == error_mark_node
9980 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9982 cp_parser_skip_to_end_of_block_or_statement (parser);
9983 return error_mark_node;
9987 /* Give a helpful message for "A<T>::type t;" and the like. */
9988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9989 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9991 if (TREE_CODE (statement) == SCOPE_REF)
9992 error_at (token->location, "need %<typename%> before %qE because "
9993 "%qT is a dependent scope",
9994 statement, TREE_OPERAND (statement, 0));
9995 else if (is_overloaded_fn (statement)
9996 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9998 /* A::A a; */
9999 tree fn = get_first_fn (statement);
10000 error_at (token->location,
10001 "%<%T::%D%> names the constructor, not the type",
10002 DECL_CONTEXT (fn), DECL_NAME (fn));
10006 /* Consume the final `;'. */
10007 cp_parser_consume_semicolon_at_end_of_statement (parser);
10009 if (in_statement_expr
10010 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10011 /* This is the final expression statement of a statement
10012 expression. */
10013 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10014 else if (statement)
10015 statement = finish_expr_stmt (statement);
10017 return statement;
10020 /* Parse a compound-statement.
10022 compound-statement:
10023 { statement-seq [opt] }
10025 GNU extension:
10027 compound-statement:
10028 { label-declaration-seq [opt] statement-seq [opt] }
10030 label-declaration-seq:
10031 label-declaration
10032 label-declaration-seq label-declaration
10034 Returns a tree representing the statement. */
10036 static tree
10037 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10038 bool in_try, bool function_body)
10040 tree compound_stmt;
10042 /* Consume the `{'. */
10043 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10044 return error_mark_node;
10045 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10046 && !function_body && cxx_dialect < cxx14)
10047 pedwarn (input_location, OPT_Wpedantic,
10048 "compound-statement in constexpr function");
10049 /* Begin the compound-statement. */
10050 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10051 /* If the next keyword is `__label__' we have a label declaration. */
10052 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10053 cp_parser_label_declaration (parser);
10054 /* Parse an (optional) statement-seq. */
10055 cp_parser_statement_seq_opt (parser, in_statement_expr);
10056 /* Finish the compound-statement. */
10057 finish_compound_stmt (compound_stmt);
10058 /* Consume the `}'. */
10059 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10061 return compound_stmt;
10064 /* Parse an (optional) statement-seq.
10066 statement-seq:
10067 statement
10068 statement-seq [opt] statement */
10070 static void
10071 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10073 /* Scan statements until there aren't any more. */
10074 while (true)
10076 cp_token *token = cp_lexer_peek_token (parser->lexer);
10078 /* If we are looking at a `}', then we have run out of
10079 statements; the same is true if we have reached the end
10080 of file, or have stumbled upon a stray '@end'. */
10081 if (token->type == CPP_CLOSE_BRACE
10082 || token->type == CPP_EOF
10083 || token->type == CPP_PRAGMA_EOL
10084 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10085 break;
10087 /* If we are in a compound statement and find 'else' then
10088 something went wrong. */
10089 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10091 if (parser->in_statement & IN_IF_STMT)
10092 break;
10093 else
10095 token = cp_lexer_consume_token (parser->lexer);
10096 error_at (token->location, "%<else%> without a previous %<if%>");
10100 /* Parse the statement. */
10101 cp_parser_statement (parser, in_statement_expr, true, NULL);
10105 /* Parse a selection-statement.
10107 selection-statement:
10108 if ( condition ) statement
10109 if ( condition ) statement else statement
10110 switch ( condition ) statement
10112 Returns the new IF_STMT or SWITCH_STMT.
10114 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10115 is a (possibly labeled) if statement which is not enclosed in
10116 braces and has an else clause. This is used to implement
10117 -Wparentheses. */
10119 static tree
10120 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10122 cp_token *token;
10123 enum rid keyword;
10125 if (if_p != NULL)
10126 *if_p = false;
10128 /* Peek at the next token. */
10129 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10131 /* See what kind of keyword it is. */
10132 keyword = token->keyword;
10133 switch (keyword)
10135 case RID_IF:
10136 case RID_SWITCH:
10138 tree statement;
10139 tree condition;
10141 /* Look for the `('. */
10142 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10144 cp_parser_skip_to_end_of_statement (parser);
10145 return error_mark_node;
10148 /* Begin the selection-statement. */
10149 if (keyword == RID_IF)
10150 statement = begin_if_stmt ();
10151 else
10152 statement = begin_switch_stmt ();
10154 /* Parse the condition. */
10155 condition = cp_parser_condition (parser);
10156 /* Look for the `)'. */
10157 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10159 /*consume_paren=*/true);
10161 if (keyword == RID_IF)
10163 bool nested_if;
10164 unsigned char in_statement;
10166 /* Add the condition. */
10167 finish_if_stmt_cond (condition, statement);
10169 /* Parse the then-clause. */
10170 in_statement = parser->in_statement;
10171 parser->in_statement |= IN_IF_STMT;
10172 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10174 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10175 add_stmt (build_empty_stmt (loc));
10176 cp_lexer_consume_token (parser->lexer);
10177 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10178 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10179 "empty body in an %<if%> statement");
10180 nested_if = false;
10182 else
10183 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10184 token->location, "if");
10185 parser->in_statement = in_statement;
10187 finish_then_clause (statement);
10189 /* If the next token is `else', parse the else-clause. */
10190 if (cp_lexer_next_token_is_keyword (parser->lexer,
10191 RID_ELSE))
10193 /* Consume the `else' keyword. */
10194 location_t else_tok_loc
10195 = cp_lexer_consume_token (parser->lexer)->location;
10196 begin_else_clause (statement);
10197 /* Parse the else-clause. */
10198 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10200 location_t loc;
10201 loc = cp_lexer_peek_token (parser->lexer)->location;
10202 warning_at (loc,
10203 OPT_Wempty_body, "suggest braces around "
10204 "empty body in an %<else%> statement");
10205 add_stmt (build_empty_stmt (loc));
10206 cp_lexer_consume_token (parser->lexer);
10208 else
10209 cp_parser_implicitly_scoped_statement (parser, NULL,
10210 else_tok_loc, "else");
10212 finish_else_clause (statement);
10214 /* If we are currently parsing a then-clause, then
10215 IF_P will not be NULL. We set it to true to
10216 indicate that this if statement has an else clause.
10217 This may trigger the Wparentheses warning below
10218 when we get back up to the parent if statement. */
10219 if (if_p != NULL)
10220 *if_p = true;
10222 else
10224 /* This if statement does not have an else clause. If
10225 NESTED_IF is true, then the then-clause is an if
10226 statement which does have an else clause. We warn
10227 about the potential ambiguity. */
10228 if (nested_if)
10229 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10230 "suggest explicit braces to avoid ambiguous"
10231 " %<else%>");
10234 /* Now we're all done with the if-statement. */
10235 finish_if_stmt (statement);
10237 else
10239 bool in_switch_statement_p;
10240 unsigned char in_statement;
10242 /* Add the condition. */
10243 finish_switch_cond (condition, statement);
10245 /* Parse the body of the switch-statement. */
10246 in_switch_statement_p = parser->in_switch_statement_p;
10247 in_statement = parser->in_statement;
10248 parser->in_switch_statement_p = true;
10249 parser->in_statement |= IN_SWITCH_STMT;
10250 cp_parser_implicitly_scoped_statement (parser, NULL,
10251 0, "switch");
10252 parser->in_switch_statement_p = in_switch_statement_p;
10253 parser->in_statement = in_statement;
10255 /* Now we're all done with the switch-statement. */
10256 finish_switch_stmt (statement);
10259 return statement;
10261 break;
10263 default:
10264 cp_parser_error (parser, "expected selection-statement");
10265 return error_mark_node;
10269 /* Parse a condition.
10271 condition:
10272 expression
10273 type-specifier-seq declarator = initializer-clause
10274 type-specifier-seq declarator braced-init-list
10276 GNU Extension:
10278 condition:
10279 type-specifier-seq declarator asm-specification [opt]
10280 attributes [opt] = assignment-expression
10282 Returns the expression that should be tested. */
10284 static tree
10285 cp_parser_condition (cp_parser* parser)
10287 cp_decl_specifier_seq type_specifiers;
10288 const char *saved_message;
10289 int declares_class_or_enum;
10291 /* Try the declaration first. */
10292 cp_parser_parse_tentatively (parser);
10293 /* New types are not allowed in the type-specifier-seq for a
10294 condition. */
10295 saved_message = parser->type_definition_forbidden_message;
10296 parser->type_definition_forbidden_message
10297 = G_("types may not be defined in conditions");
10298 /* Parse the type-specifier-seq. */
10299 cp_parser_decl_specifier_seq (parser,
10300 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10301 &type_specifiers,
10302 &declares_class_or_enum);
10303 /* Restore the saved message. */
10304 parser->type_definition_forbidden_message = saved_message;
10305 /* If all is well, we might be looking at a declaration. */
10306 if (!cp_parser_error_occurred (parser))
10308 tree decl;
10309 tree asm_specification;
10310 tree attributes;
10311 cp_declarator *declarator;
10312 tree initializer = NULL_TREE;
10314 /* Parse the declarator. */
10315 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10316 /*ctor_dtor_or_conv_p=*/NULL,
10317 /*parenthesized_p=*/NULL,
10318 /*member_p=*/false,
10319 /*friend_p=*/false);
10320 /* Parse the attributes. */
10321 attributes = cp_parser_attributes_opt (parser);
10322 /* Parse the asm-specification. */
10323 asm_specification = cp_parser_asm_specification_opt (parser);
10324 /* If the next token is not an `=' or '{', then we might still be
10325 looking at an expression. For example:
10327 if (A(a).x)
10329 looks like a decl-specifier-seq and a declarator -- but then
10330 there is no `=', so this is an expression. */
10331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10332 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10333 cp_parser_simulate_error (parser);
10335 /* If we did see an `=' or '{', then we are looking at a declaration
10336 for sure. */
10337 if (cp_parser_parse_definitely (parser))
10339 tree pushed_scope;
10340 bool non_constant_p;
10341 bool flags = LOOKUP_ONLYCONVERTING;
10343 /* Create the declaration. */
10344 decl = start_decl (declarator, &type_specifiers,
10345 /*initialized_p=*/true,
10346 attributes, /*prefix_attributes=*/NULL_TREE,
10347 &pushed_scope);
10349 /* Parse the initializer. */
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10352 initializer = cp_parser_braced_list (parser, &non_constant_p);
10353 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10354 flags = 0;
10356 else
10358 /* Consume the `='. */
10359 cp_parser_require (parser, CPP_EQ, RT_EQ);
10360 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10362 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10363 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10365 /* Process the initializer. */
10366 cp_finish_decl (decl,
10367 initializer, !non_constant_p,
10368 asm_specification,
10369 flags);
10371 if (pushed_scope)
10372 pop_scope (pushed_scope);
10374 return convert_from_reference (decl);
10377 /* If we didn't even get past the declarator successfully, we are
10378 definitely not looking at a declaration. */
10379 else
10380 cp_parser_abort_tentative_parse (parser);
10382 /* Otherwise, we are looking at an expression. */
10383 return cp_parser_expression (parser);
10386 /* Parses a for-statement or range-for-statement until the closing ')',
10387 not included. */
10389 static tree
10390 cp_parser_for (cp_parser *parser, bool ivdep)
10392 tree init, scope, decl;
10393 bool is_range_for;
10395 /* Begin the for-statement. */
10396 scope = begin_for_scope (&init);
10398 /* Parse the initialization. */
10399 is_range_for = cp_parser_for_init_statement (parser, &decl);
10401 if (is_range_for)
10402 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10403 else
10404 return cp_parser_c_for (parser, scope, init, ivdep);
10407 static tree
10408 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10410 /* Normal for loop */
10411 tree condition = NULL_TREE;
10412 tree expression = NULL_TREE;
10413 tree stmt;
10415 stmt = begin_for_stmt (scope, init);
10416 /* The for-init-statement has already been parsed in
10417 cp_parser_for_init_statement, so no work is needed here. */
10418 finish_for_init_stmt (stmt);
10420 /* If there's a condition, process it. */
10421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10422 condition = cp_parser_condition (parser);
10423 else if (ivdep)
10425 cp_parser_error (parser, "missing loop condition in loop with "
10426 "%<GCC ivdep%> pragma");
10427 condition = error_mark_node;
10429 finish_for_cond (condition, stmt, ivdep);
10430 /* Look for the `;'. */
10431 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10433 /* If there's an expression, process it. */
10434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10435 expression = cp_parser_expression (parser);
10436 finish_for_expr (expression, stmt);
10438 return stmt;
10441 /* Tries to parse a range-based for-statement:
10443 range-based-for:
10444 decl-specifier-seq declarator : expression
10446 The decl-specifier-seq declarator and the `:' are already parsed by
10447 cp_parser_for_init_statement. If processing_template_decl it returns a
10448 newly created RANGE_FOR_STMT; if not, it is converted to a
10449 regular FOR_STMT. */
10451 static tree
10452 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10453 bool ivdep)
10455 tree stmt, range_expr;
10457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10459 bool expr_non_constant_p;
10460 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10462 else
10463 range_expr = cp_parser_expression (parser);
10465 /* If in template, STMT is converted to a normal for-statement
10466 at instantiation. If not, it is done just ahead. */
10467 if (processing_template_decl)
10469 if (check_for_bare_parameter_packs (range_expr))
10470 range_expr = error_mark_node;
10471 stmt = begin_range_for_stmt (scope, init);
10472 if (ivdep)
10473 RANGE_FOR_IVDEP (stmt) = 1;
10474 finish_range_for_decl (stmt, range_decl, range_expr);
10475 if (!type_dependent_expression_p (range_expr)
10476 /* do_auto_deduction doesn't mess with template init-lists. */
10477 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10478 do_range_for_auto_deduction (range_decl, range_expr);
10480 else
10482 stmt = begin_for_stmt (scope, init);
10483 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10485 return stmt;
10488 /* Subroutine of cp_convert_range_for: given the initializer expression,
10489 builds up the range temporary. */
10491 static tree
10492 build_range_temp (tree range_expr)
10494 tree range_type, range_temp;
10496 /* Find out the type deduced by the declaration
10497 `auto &&__range = range_expr'. */
10498 range_type = cp_build_reference_type (make_auto (), true);
10499 range_type = do_auto_deduction (range_type, range_expr,
10500 type_uses_auto (range_type));
10502 /* Create the __range variable. */
10503 range_temp = build_decl (input_location, VAR_DECL,
10504 get_identifier ("__for_range"), range_type);
10505 TREE_USED (range_temp) = 1;
10506 DECL_ARTIFICIAL (range_temp) = 1;
10508 return range_temp;
10511 /* Used by cp_parser_range_for in template context: we aren't going to
10512 do a full conversion yet, but we still need to resolve auto in the
10513 type of the for-range-declaration if present. This is basically
10514 a shortcut version of cp_convert_range_for. */
10516 static void
10517 do_range_for_auto_deduction (tree decl, tree range_expr)
10519 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10520 if (auto_node)
10522 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10523 range_temp = convert_from_reference (build_range_temp (range_expr));
10524 iter_type = (cp_parser_perform_range_for_lookup
10525 (range_temp, &begin_dummy, &end_dummy));
10526 if (iter_type)
10528 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10529 iter_type);
10530 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10531 tf_warning_or_error);
10532 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10533 iter_decl, auto_node);
10538 /* Converts a range-based for-statement into a normal
10539 for-statement, as per the definition.
10541 for (RANGE_DECL : RANGE_EXPR)
10542 BLOCK
10544 should be equivalent to:
10547 auto &&__range = RANGE_EXPR;
10548 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10549 __begin != __end;
10550 ++__begin)
10552 RANGE_DECL = *__begin;
10553 BLOCK
10557 If RANGE_EXPR is an array:
10558 BEGIN_EXPR = __range
10559 END_EXPR = __range + ARRAY_SIZE(__range)
10560 Else if RANGE_EXPR has a member 'begin' or 'end':
10561 BEGIN_EXPR = __range.begin()
10562 END_EXPR = __range.end()
10563 Else:
10564 BEGIN_EXPR = begin(__range)
10565 END_EXPR = end(__range);
10567 If __range has a member 'begin' but not 'end', or vice versa, we must
10568 still use the second alternative (it will surely fail, however).
10569 When calling begin()/end() in the third alternative we must use
10570 argument dependent lookup, but always considering 'std' as an associated
10571 namespace. */
10573 tree
10574 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10575 bool ivdep)
10577 tree begin, end;
10578 tree iter_type, begin_expr, end_expr;
10579 tree condition, expression;
10581 if (range_decl == error_mark_node || range_expr == error_mark_node)
10582 /* If an error happened previously do nothing or else a lot of
10583 unhelpful errors would be issued. */
10584 begin_expr = end_expr = iter_type = error_mark_node;
10585 else
10587 tree range_temp;
10589 if (VAR_P (range_expr)
10590 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10591 /* Can't bind a reference to an array of runtime bound. */
10592 range_temp = range_expr;
10593 else
10595 range_temp = build_range_temp (range_expr);
10596 pushdecl (range_temp);
10597 cp_finish_decl (range_temp, range_expr,
10598 /*is_constant_init*/false, NULL_TREE,
10599 LOOKUP_ONLYCONVERTING);
10600 range_temp = convert_from_reference (range_temp);
10602 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10603 &begin_expr, &end_expr);
10606 /* The new for initialization statement. */
10607 begin = build_decl (input_location, VAR_DECL,
10608 get_identifier ("__for_begin"), iter_type);
10609 TREE_USED (begin) = 1;
10610 DECL_ARTIFICIAL (begin) = 1;
10611 pushdecl (begin);
10612 cp_finish_decl (begin, begin_expr,
10613 /*is_constant_init*/false, NULL_TREE,
10614 LOOKUP_ONLYCONVERTING);
10616 end = build_decl (input_location, VAR_DECL,
10617 get_identifier ("__for_end"), iter_type);
10618 TREE_USED (end) = 1;
10619 DECL_ARTIFICIAL (end) = 1;
10620 pushdecl (end);
10621 cp_finish_decl (end, end_expr,
10622 /*is_constant_init*/false, NULL_TREE,
10623 LOOKUP_ONLYCONVERTING);
10625 finish_for_init_stmt (statement);
10627 /* The new for condition. */
10628 condition = build_x_binary_op (input_location, NE_EXPR,
10629 begin, ERROR_MARK,
10630 end, ERROR_MARK,
10631 NULL, tf_warning_or_error);
10632 finish_for_cond (condition, statement, ivdep);
10634 /* The new increment expression. */
10635 expression = finish_unary_op_expr (input_location,
10636 PREINCREMENT_EXPR, begin,
10637 tf_warning_or_error);
10638 finish_for_expr (expression, statement);
10640 /* The declaration is initialized with *__begin inside the loop body. */
10641 cp_finish_decl (range_decl,
10642 build_x_indirect_ref (input_location, begin, RO_NULL,
10643 tf_warning_or_error),
10644 /*is_constant_init*/false, NULL_TREE,
10645 LOOKUP_ONLYCONVERTING);
10647 return statement;
10650 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10651 We need to solve both at the same time because the method used
10652 depends on the existence of members begin or end.
10653 Returns the type deduced for the iterator expression. */
10655 static tree
10656 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10658 if (error_operand_p (range))
10660 *begin = *end = error_mark_node;
10661 return error_mark_node;
10664 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10666 error ("range-based %<for%> expression of type %qT "
10667 "has incomplete type", TREE_TYPE (range));
10668 *begin = *end = error_mark_node;
10669 return error_mark_node;
10671 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10673 /* If RANGE is an array, we will use pointer arithmetic. */
10674 *begin = range;
10675 *end = build_binary_op (input_location, PLUS_EXPR,
10676 range,
10677 array_type_nelts_top (TREE_TYPE (range)),
10679 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10681 else
10683 /* If it is not an array, we must do a bit of magic. */
10684 tree id_begin, id_end;
10685 tree member_begin, member_end;
10687 *begin = *end = error_mark_node;
10689 id_begin = get_identifier ("begin");
10690 id_end = get_identifier ("end");
10691 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10692 /*protect=*/2, /*want_type=*/false,
10693 tf_warning_or_error);
10694 member_end = lookup_member (TREE_TYPE (range), id_end,
10695 /*protect=*/2, /*want_type=*/false,
10696 tf_warning_or_error);
10698 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10700 /* Use the member functions. */
10701 if (member_begin != NULL_TREE)
10702 *begin = cp_parser_range_for_member_function (range, id_begin);
10703 else
10704 error ("range-based %<for%> expression of type %qT has an "
10705 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10707 if (member_end != NULL_TREE)
10708 *end = cp_parser_range_for_member_function (range, id_end);
10709 else
10710 error ("range-based %<for%> expression of type %qT has a "
10711 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10713 else
10715 /* Use global functions with ADL. */
10716 vec<tree, va_gc> *vec;
10717 vec = make_tree_vector ();
10719 vec_safe_push (vec, range);
10721 member_begin = perform_koenig_lookup (id_begin, vec,
10722 tf_warning_or_error);
10723 *begin = finish_call_expr (member_begin, &vec, false, true,
10724 tf_warning_or_error);
10725 member_end = perform_koenig_lookup (id_end, vec,
10726 tf_warning_or_error);
10727 *end = finish_call_expr (member_end, &vec, false, true,
10728 tf_warning_or_error);
10730 release_tree_vector (vec);
10733 /* Last common checks. */
10734 if (*begin == error_mark_node || *end == error_mark_node)
10736 /* If one of the expressions is an error do no more checks. */
10737 *begin = *end = error_mark_node;
10738 return error_mark_node;
10740 else if (type_dependent_expression_p (*begin)
10741 || type_dependent_expression_p (*end))
10742 /* Can happen, when, eg, in a template context, Koenig lookup
10743 can't resolve begin/end (c++/58503). */
10744 return NULL_TREE;
10745 else
10747 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10748 /* The unqualified type of the __begin and __end temporaries should
10749 be the same, as required by the multiple auto declaration. */
10750 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10751 error ("inconsistent begin/end types in range-based %<for%> "
10752 "statement: %qT and %qT",
10753 TREE_TYPE (*begin), TREE_TYPE (*end));
10754 return iter_type;
10759 /* Helper function for cp_parser_perform_range_for_lookup.
10760 Builds a tree for RANGE.IDENTIFIER(). */
10762 static tree
10763 cp_parser_range_for_member_function (tree range, tree identifier)
10765 tree member, res;
10766 vec<tree, va_gc> *vec;
10768 member = finish_class_member_access_expr (range, identifier,
10769 false, tf_warning_or_error);
10770 if (member == error_mark_node)
10771 return error_mark_node;
10773 vec = make_tree_vector ();
10774 res = finish_call_expr (member, &vec,
10775 /*disallow_virtual=*/false,
10776 /*koenig_p=*/false,
10777 tf_warning_or_error);
10778 release_tree_vector (vec);
10779 return res;
10782 /* Parse an iteration-statement.
10784 iteration-statement:
10785 while ( condition ) statement
10786 do statement while ( expression ) ;
10787 for ( for-init-statement condition [opt] ; expression [opt] )
10788 statement
10790 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10792 static tree
10793 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10795 cp_token *token;
10796 location_t tok_loc;
10797 enum rid keyword;
10798 tree statement;
10799 unsigned char in_statement;
10801 /* Peek at the next token. */
10802 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10803 if (!token)
10804 return error_mark_node;
10806 tok_loc = token->location;
10808 /* Remember whether or not we are already within an iteration
10809 statement. */
10810 in_statement = parser->in_statement;
10812 /* See what kind of keyword it is. */
10813 keyword = token->keyword;
10814 switch (keyword)
10816 case RID_WHILE:
10818 tree condition;
10820 /* Begin the while-statement. */
10821 statement = begin_while_stmt ();
10822 /* Look for the `('. */
10823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10824 /* Parse the condition. */
10825 condition = cp_parser_condition (parser);
10826 finish_while_stmt_cond (condition, statement, ivdep);
10827 /* Look for the `)'. */
10828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10829 /* Parse the dependent statement. */
10830 parser->in_statement = IN_ITERATION_STMT;
10831 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10832 parser->in_statement = in_statement;
10833 /* We're done with the while-statement. */
10834 finish_while_stmt (statement);
10836 break;
10838 case RID_DO:
10840 tree expression;
10842 /* Begin the do-statement. */
10843 statement = begin_do_stmt ();
10844 /* Parse the body of the do-statement. */
10845 parser->in_statement = IN_ITERATION_STMT;
10846 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10847 parser->in_statement = in_statement;
10848 finish_do_body (statement);
10849 /* Look for the `while' keyword. */
10850 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10851 /* Look for the `('. */
10852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10853 /* Parse the expression. */
10854 expression = cp_parser_expression (parser);
10855 /* We're done with the do-statement. */
10856 finish_do_stmt (expression, statement, ivdep);
10857 /* Look for the `)'. */
10858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10859 /* Look for the `;'. */
10860 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10862 break;
10864 case RID_FOR:
10866 /* Look for the `('. */
10867 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10869 statement = cp_parser_for (parser, ivdep);
10871 /* Look for the `)'. */
10872 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10874 /* Parse the body of the for-statement. */
10875 parser->in_statement = IN_ITERATION_STMT;
10876 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10877 parser->in_statement = in_statement;
10879 /* We're done with the for-statement. */
10880 finish_for_stmt (statement);
10882 break;
10884 default:
10885 cp_parser_error (parser, "expected iteration-statement");
10886 statement = error_mark_node;
10887 break;
10890 return statement;
10893 /* Parse a for-init-statement or the declarator of a range-based-for.
10894 Returns true if a range-based-for declaration is seen.
10896 for-init-statement:
10897 expression-statement
10898 simple-declaration */
10900 static bool
10901 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10903 /* If the next token is a `;', then we have an empty
10904 expression-statement. Grammatically, this is also a
10905 simple-declaration, but an invalid one, because it does not
10906 declare anything. Therefore, if we did not handle this case
10907 specially, we would issue an error message about an invalid
10908 declaration. */
10909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10911 bool is_range_for = false;
10912 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10915 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10917 /* N3994 -- for (id : init) ... */
10918 if (cxx_dialect < cxx1z)
10919 pedwarn (input_location, 0, "range-based for loop without a "
10920 "type-specifier only available with "
10921 "-std=c++1z or -std=gnu++1z");
10922 tree name = cp_parser_identifier (parser);
10923 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10924 *decl = build_decl (input_location, VAR_DECL, name, type);
10925 pushdecl (*decl);
10926 cp_lexer_consume_token (parser->lexer);
10927 return true;
10930 /* A colon is used in range-based for. */
10931 parser->colon_corrects_to_scope_p = false;
10933 /* We're going to speculatively look for a declaration, falling back
10934 to an expression, if necessary. */
10935 cp_parser_parse_tentatively (parser);
10936 /* Parse the declaration. */
10937 cp_parser_simple_declaration (parser,
10938 /*function_definition_allowed_p=*/false,
10939 decl);
10940 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10941 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10943 /* It is a range-for, consume the ':' */
10944 cp_lexer_consume_token (parser->lexer);
10945 is_range_for = true;
10946 if (cxx_dialect < cxx11)
10948 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10949 "range-based %<for%> loops only available with "
10950 "-std=c++11 or -std=gnu++11");
10951 *decl = error_mark_node;
10954 else
10955 /* The ';' is not consumed yet because we told
10956 cp_parser_simple_declaration not to. */
10957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10959 if (cp_parser_parse_definitely (parser))
10960 return is_range_for;
10961 /* If the tentative parse failed, then we shall need to look for an
10962 expression-statement. */
10964 /* If we are here, it is an expression-statement. */
10965 cp_parser_expression_statement (parser, NULL_TREE);
10966 return false;
10969 /* Parse a jump-statement.
10971 jump-statement:
10972 break ;
10973 continue ;
10974 return expression [opt] ;
10975 return braced-init-list ;
10976 goto identifier ;
10978 GNU extension:
10980 jump-statement:
10981 goto * expression ;
10983 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10985 static tree
10986 cp_parser_jump_statement (cp_parser* parser)
10988 tree statement = error_mark_node;
10989 cp_token *token;
10990 enum rid keyword;
10991 unsigned char in_statement;
10993 /* Peek at the next token. */
10994 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10995 if (!token)
10996 return error_mark_node;
10998 /* See what kind of keyword it is. */
10999 keyword = token->keyword;
11000 switch (keyword)
11002 case RID_BREAK:
11003 in_statement = parser->in_statement & ~IN_IF_STMT;
11004 switch (in_statement)
11006 case 0:
11007 error_at (token->location, "break statement not within loop or switch");
11008 break;
11009 default:
11010 gcc_assert ((in_statement & IN_SWITCH_STMT)
11011 || in_statement == IN_ITERATION_STMT);
11012 statement = finish_break_stmt ();
11013 if (in_statement == IN_ITERATION_STMT)
11014 break_maybe_infinite_loop ();
11015 break;
11016 case IN_OMP_BLOCK:
11017 error_at (token->location, "invalid exit from OpenMP structured block");
11018 break;
11019 case IN_OMP_FOR:
11020 error_at (token->location, "break statement used with OpenMP for loop");
11021 break;
11022 case IN_CILK_SIMD_FOR:
11023 error_at (token->location, "break statement used with Cilk Plus for loop");
11024 break;
11026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11027 break;
11029 case RID_CONTINUE:
11030 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11032 case 0:
11033 error_at (token->location, "continue statement not within a loop");
11034 break;
11035 case IN_CILK_SIMD_FOR:
11036 error_at (token->location,
11037 "continue statement within %<#pragma simd%> loop body");
11038 /* Fall through. */
11039 case IN_ITERATION_STMT:
11040 case IN_OMP_FOR:
11041 statement = finish_continue_stmt ();
11042 break;
11043 case IN_OMP_BLOCK:
11044 error_at (token->location, "invalid exit from OpenMP structured block");
11045 break;
11046 default:
11047 gcc_unreachable ();
11049 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11050 break;
11052 case RID_RETURN:
11054 tree expr;
11055 bool expr_non_constant_p;
11057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11059 cp_lexer_set_source_position (parser->lexer);
11060 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11061 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11063 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11064 expr = cp_parser_expression (parser);
11065 else
11066 /* If the next token is a `;', then there is no
11067 expression. */
11068 expr = NULL_TREE;
11069 /* Build the return-statement. */
11070 statement = finish_return_stmt (expr);
11071 /* Look for the final `;'. */
11072 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11074 break;
11076 case RID_GOTO:
11077 if (parser->in_function_body
11078 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11080 error ("%<goto%> in %<constexpr%> function");
11081 cp_function_chain->invalid_constexpr = true;
11084 /* Create the goto-statement. */
11085 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11087 /* Issue a warning about this use of a GNU extension. */
11088 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11089 /* Consume the '*' token. */
11090 cp_lexer_consume_token (parser->lexer);
11091 /* Parse the dependent expression. */
11092 finish_goto_stmt (cp_parser_expression (parser));
11094 else
11095 finish_goto_stmt (cp_parser_identifier (parser));
11096 /* Look for the final `;'. */
11097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11098 break;
11100 default:
11101 cp_parser_error (parser, "expected jump-statement");
11102 break;
11105 return statement;
11108 /* Parse a declaration-statement.
11110 declaration-statement:
11111 block-declaration */
11113 static void
11114 cp_parser_declaration_statement (cp_parser* parser)
11116 void *p;
11118 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11119 p = obstack_alloc (&declarator_obstack, 0);
11121 /* Parse the block-declaration. */
11122 cp_parser_block_declaration (parser, /*statement_p=*/true);
11124 /* Free any declarators allocated. */
11125 obstack_free (&declarator_obstack, p);
11128 /* Some dependent statements (like `if (cond) statement'), are
11129 implicitly in their own scope. In other words, if the statement is
11130 a single statement (as opposed to a compound-statement), it is
11131 none-the-less treated as if it were enclosed in braces. Any
11132 declarations appearing in the dependent statement are out of scope
11133 after control passes that point. This function parses a statement,
11134 but ensures that is in its own scope, even if it is not a
11135 compound-statement.
11137 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11138 is a (possibly labeled) if statement which is not enclosed in
11139 braces and has an else clause. This is used to implement
11140 -Wparentheses.
11142 Returns the new statement. */
11144 static tree
11145 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11146 location_t guard_loc,
11147 const char *guard_kind)
11149 tree statement;
11151 if (if_p != NULL)
11152 *if_p = false;
11154 /* Mark if () ; with a special NOP_EXPR. */
11155 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11158 cp_lexer_consume_token (parser->lexer);
11159 statement = add_stmt (build_empty_stmt (loc));
11161 /* if a compound is opened, we simply parse the statement directly. */
11162 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11163 statement = cp_parser_compound_statement (parser, NULL, false, false);
11164 /* If the token is not a `{', then we must take special action. */
11165 else
11167 /* Create a compound-statement. */
11168 statement = begin_compound_stmt (0);
11169 /* Parse the dependent-statement. */
11170 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11171 cp_parser_statement (parser, NULL_TREE, false, if_p);
11172 /* Finish the dummy compound-statement. */
11173 finish_compound_stmt (statement);
11174 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11175 if (next_tok->keyword != RID_ELSE)
11177 location_t next_stmt_loc = next_tok->location;
11178 warn_for_misleading_indentation (guard_loc, body_loc,
11179 next_stmt_loc, next_tok->type,
11180 guard_kind);
11184 /* Return the statement. */
11185 return statement;
11188 /* For some dependent statements (like `while (cond) statement'), we
11189 have already created a scope. Therefore, even if the dependent
11190 statement is a compound-statement, we do not want to create another
11191 scope. */
11193 static void
11194 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11195 const char *guard_kind)
11197 /* If the token is a `{', then we must take special action. */
11198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11200 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11201 cp_parser_statement (parser, NULL_TREE, false, NULL);
11202 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11203 location_t next_stmt_loc = next_tok->location;
11204 warn_for_misleading_indentation (guard_loc, body_loc,
11205 next_stmt_loc, next_tok->type,
11206 guard_kind);
11208 else
11210 /* Avoid calling cp_parser_compound_statement, so that we
11211 don't create a new scope. Do everything else by hand. */
11212 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11213 /* If the next keyword is `__label__' we have a label declaration. */
11214 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11215 cp_parser_label_declaration (parser);
11216 /* Parse an (optional) statement-seq. */
11217 cp_parser_statement_seq_opt (parser, NULL_TREE);
11218 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11222 /* Declarations [gram.dcl.dcl] */
11224 /* Parse an optional declaration-sequence.
11226 declaration-seq:
11227 declaration
11228 declaration-seq declaration */
11230 static void
11231 cp_parser_declaration_seq_opt (cp_parser* parser)
11233 while (true)
11235 cp_token *token;
11237 token = cp_lexer_peek_token (parser->lexer);
11239 if (token->type == CPP_CLOSE_BRACE
11240 || token->type == CPP_EOF
11241 || token->type == CPP_PRAGMA_EOL)
11242 break;
11244 if (token->type == CPP_SEMICOLON)
11246 /* A declaration consisting of a single semicolon is
11247 invalid. Allow it unless we're being pedantic. */
11248 cp_lexer_consume_token (parser->lexer);
11249 if (!in_system_header_at (input_location))
11250 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11251 continue;
11254 /* If we're entering or exiting a region that's implicitly
11255 extern "C", modify the lang context appropriately. */
11256 if (!parser->implicit_extern_c && token->implicit_extern_c)
11258 push_lang_context (lang_name_c);
11259 parser->implicit_extern_c = true;
11261 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11263 pop_lang_context ();
11264 parser->implicit_extern_c = false;
11267 if (token->type == CPP_PRAGMA)
11269 /* A top-level declaration can consist solely of a #pragma.
11270 A nested declaration cannot, so this is done here and not
11271 in cp_parser_declaration. (A #pragma at block scope is
11272 handled in cp_parser_statement.) */
11273 cp_parser_pragma (parser, pragma_external);
11274 continue;
11277 /* Parse the declaration itself. */
11278 cp_parser_declaration (parser);
11282 /* Parse a declaration.
11284 declaration:
11285 block-declaration
11286 function-definition
11287 template-declaration
11288 explicit-instantiation
11289 explicit-specialization
11290 linkage-specification
11291 namespace-definition
11293 GNU extension:
11295 declaration:
11296 __extension__ declaration */
11298 static void
11299 cp_parser_declaration (cp_parser* parser)
11301 cp_token token1;
11302 cp_token token2;
11303 int saved_pedantic;
11304 void *p;
11305 tree attributes = NULL_TREE;
11307 /* Check for the `__extension__' keyword. */
11308 if (cp_parser_extension_opt (parser, &saved_pedantic))
11310 /* Parse the qualified declaration. */
11311 cp_parser_declaration (parser);
11312 /* Restore the PEDANTIC flag. */
11313 pedantic = saved_pedantic;
11315 return;
11318 /* Try to figure out what kind of declaration is present. */
11319 token1 = *cp_lexer_peek_token (parser->lexer);
11321 if (token1.type != CPP_EOF)
11322 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11323 else
11325 token2.type = CPP_EOF;
11326 token2.keyword = RID_MAX;
11329 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11330 p = obstack_alloc (&declarator_obstack, 0);
11332 /* If the next token is `extern' and the following token is a string
11333 literal, then we have a linkage specification. */
11334 if (token1.keyword == RID_EXTERN
11335 && cp_parser_is_pure_string_literal (&token2))
11336 cp_parser_linkage_specification (parser);
11337 /* If the next token is `template', then we have either a template
11338 declaration, an explicit instantiation, or an explicit
11339 specialization. */
11340 else if (token1.keyword == RID_TEMPLATE)
11342 /* `template <>' indicates a template specialization. */
11343 if (token2.type == CPP_LESS
11344 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11345 cp_parser_explicit_specialization (parser);
11346 /* `template <' indicates a template declaration. */
11347 else if (token2.type == CPP_LESS)
11348 cp_parser_template_declaration (parser, /*member_p=*/false);
11349 /* Anything else must be an explicit instantiation. */
11350 else
11351 cp_parser_explicit_instantiation (parser);
11353 /* If the next token is `export', then we have a template
11354 declaration. */
11355 else if (token1.keyword == RID_EXPORT)
11356 cp_parser_template_declaration (parser, /*member_p=*/false);
11357 /* If the next token is `extern', 'static' or 'inline' and the one
11358 after that is `template', we have a GNU extended explicit
11359 instantiation directive. */
11360 else if (cp_parser_allow_gnu_extensions_p (parser)
11361 && (token1.keyword == RID_EXTERN
11362 || token1.keyword == RID_STATIC
11363 || token1.keyword == RID_INLINE)
11364 && token2.keyword == RID_TEMPLATE)
11365 cp_parser_explicit_instantiation (parser);
11366 /* If the next token is `namespace', check for a named or unnamed
11367 namespace definition. */
11368 else if (token1.keyword == RID_NAMESPACE
11369 && (/* A named namespace definition. */
11370 (token2.type == CPP_NAME
11371 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11372 != CPP_EQ))
11373 /* An unnamed namespace definition. */
11374 || token2.type == CPP_OPEN_BRACE
11375 || token2.keyword == RID_ATTRIBUTE))
11376 cp_parser_namespace_definition (parser);
11377 /* An inline (associated) namespace definition. */
11378 else if (token1.keyword == RID_INLINE
11379 && token2.keyword == RID_NAMESPACE)
11380 cp_parser_namespace_definition (parser);
11381 /* Objective-C++ declaration/definition. */
11382 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11383 cp_parser_objc_declaration (parser, NULL_TREE);
11384 else if (c_dialect_objc ()
11385 && token1.keyword == RID_ATTRIBUTE
11386 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11387 cp_parser_objc_declaration (parser, attributes);
11388 /* We must have either a block declaration or a function
11389 definition. */
11390 else
11391 /* Try to parse a block-declaration, or a function-definition. */
11392 cp_parser_block_declaration (parser, /*statement_p=*/false);
11394 /* Free any declarators allocated. */
11395 obstack_free (&declarator_obstack, p);
11398 /* Parse a block-declaration.
11400 block-declaration:
11401 simple-declaration
11402 asm-definition
11403 namespace-alias-definition
11404 using-declaration
11405 using-directive
11407 GNU Extension:
11409 block-declaration:
11410 __extension__ block-declaration
11412 C++0x Extension:
11414 block-declaration:
11415 static_assert-declaration
11417 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11418 part of a declaration-statement. */
11420 static void
11421 cp_parser_block_declaration (cp_parser *parser,
11422 bool statement_p)
11424 cp_token *token1;
11425 int saved_pedantic;
11427 /* Check for the `__extension__' keyword. */
11428 if (cp_parser_extension_opt (parser, &saved_pedantic))
11430 /* Parse the qualified declaration. */
11431 cp_parser_block_declaration (parser, statement_p);
11432 /* Restore the PEDANTIC flag. */
11433 pedantic = saved_pedantic;
11435 return;
11438 /* Peek at the next token to figure out which kind of declaration is
11439 present. */
11440 token1 = cp_lexer_peek_token (parser->lexer);
11442 /* If the next keyword is `asm', we have an asm-definition. */
11443 if (token1->keyword == RID_ASM)
11445 if (statement_p)
11446 cp_parser_commit_to_tentative_parse (parser);
11447 cp_parser_asm_definition (parser);
11449 /* If the next keyword is `namespace', we have a
11450 namespace-alias-definition. */
11451 else if (token1->keyword == RID_NAMESPACE)
11452 cp_parser_namespace_alias_definition (parser);
11453 /* If the next keyword is `using', we have a
11454 using-declaration, a using-directive, or an alias-declaration. */
11455 else if (token1->keyword == RID_USING)
11457 cp_token *token2;
11459 if (statement_p)
11460 cp_parser_commit_to_tentative_parse (parser);
11461 /* If the token after `using' is `namespace', then we have a
11462 using-directive. */
11463 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11464 if (token2->keyword == RID_NAMESPACE)
11465 cp_parser_using_directive (parser);
11466 /* If the second token after 'using' is '=', then we have an
11467 alias-declaration. */
11468 else if (cxx_dialect >= cxx11
11469 && token2->type == CPP_NAME
11470 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11471 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11472 cp_parser_alias_declaration (parser);
11473 /* Otherwise, it's a using-declaration. */
11474 else
11475 cp_parser_using_declaration (parser,
11476 /*access_declaration_p=*/false);
11478 /* If the next keyword is `__label__' we have a misplaced label
11479 declaration. */
11480 else if (token1->keyword == RID_LABEL)
11482 cp_lexer_consume_token (parser->lexer);
11483 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11484 cp_parser_skip_to_end_of_statement (parser);
11485 /* If the next token is now a `;', consume it. */
11486 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11487 cp_lexer_consume_token (parser->lexer);
11489 /* If the next token is `static_assert' we have a static assertion. */
11490 else if (token1->keyword == RID_STATIC_ASSERT)
11491 cp_parser_static_assert (parser, /*member_p=*/false);
11492 /* Anything else must be a simple-declaration. */
11493 else
11494 cp_parser_simple_declaration (parser, !statement_p,
11495 /*maybe_range_for_decl*/NULL);
11498 /* Parse a simple-declaration.
11500 simple-declaration:
11501 decl-specifier-seq [opt] init-declarator-list [opt] ;
11503 init-declarator-list:
11504 init-declarator
11505 init-declarator-list , init-declarator
11507 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11508 function-definition as a simple-declaration.
11510 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11511 parsed declaration if it is an uninitialized single declarator not followed
11512 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11513 if present, will not be consumed. */
11515 static void
11516 cp_parser_simple_declaration (cp_parser* parser,
11517 bool function_definition_allowed_p,
11518 tree *maybe_range_for_decl)
11520 cp_decl_specifier_seq decl_specifiers;
11521 int declares_class_or_enum;
11522 bool saw_declarator;
11523 location_t comma_loc = UNKNOWN_LOCATION;
11524 location_t init_loc = UNKNOWN_LOCATION;
11526 if (maybe_range_for_decl)
11527 *maybe_range_for_decl = NULL_TREE;
11529 /* Defer access checks until we know what is being declared; the
11530 checks for names appearing in the decl-specifier-seq should be
11531 done as if we were in the scope of the thing being declared. */
11532 push_deferring_access_checks (dk_deferred);
11534 /* Parse the decl-specifier-seq. We have to keep track of whether
11535 or not the decl-specifier-seq declares a named class or
11536 enumeration type, since that is the only case in which the
11537 init-declarator-list is allowed to be empty.
11539 [dcl.dcl]
11541 In a simple-declaration, the optional init-declarator-list can be
11542 omitted only when declaring a class or enumeration, that is when
11543 the decl-specifier-seq contains either a class-specifier, an
11544 elaborated-type-specifier, or an enum-specifier. */
11545 cp_parser_decl_specifier_seq (parser,
11546 CP_PARSER_FLAGS_OPTIONAL,
11547 &decl_specifiers,
11548 &declares_class_or_enum);
11549 /* We no longer need to defer access checks. */
11550 stop_deferring_access_checks ();
11552 /* In a block scope, a valid declaration must always have a
11553 decl-specifier-seq. By not trying to parse declarators, we can
11554 resolve the declaration/expression ambiguity more quickly. */
11555 if (!function_definition_allowed_p
11556 && !decl_specifiers.any_specifiers_p)
11558 cp_parser_error (parser, "expected declaration");
11559 goto done;
11562 /* If the next two tokens are both identifiers, the code is
11563 erroneous. The usual cause of this situation is code like:
11565 T t;
11567 where "T" should name a type -- but does not. */
11568 if (!decl_specifiers.any_type_specifiers_p
11569 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11571 /* If parsing tentatively, we should commit; we really are
11572 looking at a declaration. */
11573 cp_parser_commit_to_tentative_parse (parser);
11574 /* Give up. */
11575 goto done;
11578 /* If we have seen at least one decl-specifier, and the next token
11579 is not a parenthesis, then we must be looking at a declaration.
11580 (After "int (" we might be looking at a functional cast.) */
11581 if (decl_specifiers.any_specifiers_p
11582 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11583 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11584 && !cp_parser_error_occurred (parser))
11585 cp_parser_commit_to_tentative_parse (parser);
11587 /* Keep going until we hit the `;' at the end of the simple
11588 declaration. */
11589 saw_declarator = false;
11590 while (cp_lexer_next_token_is_not (parser->lexer,
11591 CPP_SEMICOLON))
11593 cp_token *token;
11594 bool function_definition_p;
11595 tree decl;
11597 if (saw_declarator)
11599 /* If we are processing next declarator, comma is expected */
11600 token = cp_lexer_peek_token (parser->lexer);
11601 gcc_assert (token->type == CPP_COMMA);
11602 cp_lexer_consume_token (parser->lexer);
11603 if (maybe_range_for_decl)
11605 *maybe_range_for_decl = error_mark_node;
11606 if (comma_loc == UNKNOWN_LOCATION)
11607 comma_loc = token->location;
11610 else
11611 saw_declarator = true;
11613 /* Parse the init-declarator. */
11614 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11615 /*checks=*/NULL,
11616 function_definition_allowed_p,
11617 /*member_p=*/false,
11618 declares_class_or_enum,
11619 &function_definition_p,
11620 maybe_range_for_decl,
11621 &init_loc);
11622 /* If an error occurred while parsing tentatively, exit quickly.
11623 (That usually happens when in the body of a function; each
11624 statement is treated as a declaration-statement until proven
11625 otherwise.) */
11626 if (cp_parser_error_occurred (parser))
11627 goto done;
11628 /* Handle function definitions specially. */
11629 if (function_definition_p)
11631 /* If the next token is a `,', then we are probably
11632 processing something like:
11634 void f() {}, *p;
11636 which is erroneous. */
11637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11639 cp_token *token = cp_lexer_peek_token (parser->lexer);
11640 error_at (token->location,
11641 "mixing"
11642 " declarations and function-definitions is forbidden");
11644 /* Otherwise, we're done with the list of declarators. */
11645 else
11647 pop_deferring_access_checks ();
11648 return;
11651 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11652 *maybe_range_for_decl = decl;
11653 /* The next token should be either a `,' or a `;'. */
11654 token = cp_lexer_peek_token (parser->lexer);
11655 /* If it's a `,', there are more declarators to come. */
11656 if (token->type == CPP_COMMA)
11657 /* will be consumed next time around */;
11658 /* If it's a `;', we are done. */
11659 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11660 break;
11661 /* Anything else is an error. */
11662 else
11664 /* If we have already issued an error message we don't need
11665 to issue another one. */
11666 if (decl != error_mark_node
11667 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11668 cp_parser_error (parser, "expected %<,%> or %<;%>");
11669 /* Skip tokens until we reach the end of the statement. */
11670 cp_parser_skip_to_end_of_statement (parser);
11671 /* If the next token is now a `;', consume it. */
11672 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11673 cp_lexer_consume_token (parser->lexer);
11674 goto done;
11676 /* After the first time around, a function-definition is not
11677 allowed -- even if it was OK at first. For example:
11679 int i, f() {}
11681 is not valid. */
11682 function_definition_allowed_p = false;
11685 /* Issue an error message if no declarators are present, and the
11686 decl-specifier-seq does not itself declare a class or
11687 enumeration: [dcl.dcl]/3. */
11688 if (!saw_declarator)
11690 if (cp_parser_declares_only_class_p (parser))
11692 if (!declares_class_or_enum
11693 && decl_specifiers.type
11694 && OVERLOAD_TYPE_P (decl_specifiers.type))
11695 /* Ensure an error is issued anyway when finish_decltype_type,
11696 called via cp_parser_decl_specifier_seq, returns a class or
11697 an enumeration (c++/51786). */
11698 decl_specifiers.type = NULL_TREE;
11699 shadow_tag (&decl_specifiers);
11701 /* Perform any deferred access checks. */
11702 perform_deferred_access_checks (tf_warning_or_error);
11705 /* Consume the `;'. */
11706 if (!maybe_range_for_decl)
11707 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11708 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11710 if (init_loc != UNKNOWN_LOCATION)
11711 error_at (init_loc, "initializer in range-based %<for%> loop");
11712 if (comma_loc != UNKNOWN_LOCATION)
11713 error_at (comma_loc,
11714 "multiple declarations in range-based %<for%> loop");
11717 done:
11718 pop_deferring_access_checks ();
11721 /* Parse a decl-specifier-seq.
11723 decl-specifier-seq:
11724 decl-specifier-seq [opt] decl-specifier
11725 decl-specifier attribute-specifier-seq [opt] (C++11)
11727 decl-specifier:
11728 storage-class-specifier
11729 type-specifier
11730 function-specifier
11731 friend
11732 typedef
11734 GNU Extension:
11736 decl-specifier:
11737 attributes
11739 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11741 The parser flags FLAGS is used to control type-specifier parsing.
11743 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11744 flags:
11746 1: one of the decl-specifiers is an elaborated-type-specifier
11747 (i.e., a type declaration)
11748 2: one of the decl-specifiers is an enum-specifier or a
11749 class-specifier (i.e., a type definition)
11753 static void
11754 cp_parser_decl_specifier_seq (cp_parser* parser,
11755 cp_parser_flags flags,
11756 cp_decl_specifier_seq *decl_specs,
11757 int* declares_class_or_enum)
11759 bool constructor_possible_p = !parser->in_declarator_p;
11760 bool found_decl_spec = false;
11761 cp_token *start_token = NULL;
11762 cp_decl_spec ds;
11764 /* Clear DECL_SPECS. */
11765 clear_decl_specs (decl_specs);
11767 /* Assume no class or enumeration type is declared. */
11768 *declares_class_or_enum = 0;
11770 /* Keep reading specifiers until there are no more to read. */
11771 while (true)
11773 bool constructor_p;
11774 cp_token *token;
11775 ds = ds_last;
11777 /* Peek at the next token. */
11778 token = cp_lexer_peek_token (parser->lexer);
11780 /* Save the first token of the decl spec list for error
11781 reporting. */
11782 if (!start_token)
11783 start_token = token;
11784 /* Handle attributes. */
11785 if (cp_next_tokens_can_be_attribute_p (parser))
11787 /* Parse the attributes. */
11788 tree attrs = cp_parser_attributes_opt (parser);
11790 /* In a sequence of declaration specifiers, c++11 attributes
11791 appertain to the type that precede them. In that case
11792 [dcl.spec]/1 says:
11794 The attribute-specifier-seq affects the type only for
11795 the declaration it appears in, not other declarations
11796 involving the same type.
11798 But for now let's force the user to position the
11799 attribute either at the beginning of the declaration or
11800 after the declarator-id, which would clearly mean that it
11801 applies to the declarator. */
11802 if (cxx11_attribute_p (attrs))
11804 if (!found_decl_spec)
11805 /* The c++11 attribute is at the beginning of the
11806 declaration. It appertains to the entity being
11807 declared. */;
11808 else
11810 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11812 /* This is an attribute following a
11813 class-specifier. */
11814 if (decl_specs->type_definition_p)
11815 warn_misplaced_attr_for_class_type (token->location,
11816 decl_specs->type);
11817 attrs = NULL_TREE;
11819 else
11821 decl_specs->std_attributes
11822 = chainon (decl_specs->std_attributes,
11823 attrs);
11824 if (decl_specs->locations[ds_std_attribute] == 0)
11825 decl_specs->locations[ds_std_attribute] = token->location;
11827 continue;
11831 decl_specs->attributes
11832 = chainon (decl_specs->attributes,
11833 attrs);
11834 if (decl_specs->locations[ds_attribute] == 0)
11835 decl_specs->locations[ds_attribute] = token->location;
11836 continue;
11838 /* Assume we will find a decl-specifier keyword. */
11839 found_decl_spec = true;
11840 /* If the next token is an appropriate keyword, we can simply
11841 add it to the list. */
11842 switch (token->keyword)
11844 /* decl-specifier:
11845 friend
11846 constexpr */
11847 case RID_FRIEND:
11848 if (!at_class_scope_p ())
11850 error_at (token->location, "%<friend%> used outside of class");
11851 cp_lexer_purge_token (parser->lexer);
11853 else
11855 ds = ds_friend;
11856 /* Consume the token. */
11857 cp_lexer_consume_token (parser->lexer);
11859 break;
11861 case RID_CONSTEXPR:
11862 ds = ds_constexpr;
11863 cp_lexer_consume_token (parser->lexer);
11864 break;
11866 /* function-specifier:
11867 inline
11868 virtual
11869 explicit */
11870 case RID_INLINE:
11871 case RID_VIRTUAL:
11872 case RID_EXPLICIT:
11873 cp_parser_function_specifier_opt (parser, decl_specs);
11874 break;
11876 /* decl-specifier:
11877 typedef */
11878 case RID_TYPEDEF:
11879 ds = ds_typedef;
11880 /* Consume the token. */
11881 cp_lexer_consume_token (parser->lexer);
11882 /* A constructor declarator cannot appear in a typedef. */
11883 constructor_possible_p = false;
11884 /* The "typedef" keyword can only occur in a declaration; we
11885 may as well commit at this point. */
11886 cp_parser_commit_to_tentative_parse (parser);
11888 if (decl_specs->storage_class != sc_none)
11889 decl_specs->conflicting_specifiers_p = true;
11890 break;
11892 /* storage-class-specifier:
11893 auto
11894 register
11895 static
11896 extern
11897 mutable
11899 GNU Extension:
11900 thread */
11901 case RID_AUTO:
11902 if (cxx_dialect == cxx98)
11904 /* Consume the token. */
11905 cp_lexer_consume_token (parser->lexer);
11907 /* Complain about `auto' as a storage specifier, if
11908 we're complaining about C++0x compatibility. */
11909 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11910 " changes meaning in C++11; please remove it");
11912 /* Set the storage class anyway. */
11913 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11914 token);
11916 else
11917 /* C++0x auto type-specifier. */
11918 found_decl_spec = false;
11919 break;
11921 case RID_REGISTER:
11922 case RID_STATIC:
11923 case RID_EXTERN:
11924 case RID_MUTABLE:
11925 /* Consume the token. */
11926 cp_lexer_consume_token (parser->lexer);
11927 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11928 token);
11929 break;
11930 case RID_THREAD:
11931 /* Consume the token. */
11932 ds = ds_thread;
11933 cp_lexer_consume_token (parser->lexer);
11934 break;
11936 default:
11937 /* We did not yet find a decl-specifier yet. */
11938 found_decl_spec = false;
11939 break;
11942 if (found_decl_spec
11943 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11944 && token->keyword != RID_CONSTEXPR)
11945 error ("decl-specifier invalid in condition");
11947 if (ds != ds_last)
11948 set_and_check_decl_spec_loc (decl_specs, ds, token);
11950 /* Constructors are a special case. The `S' in `S()' is not a
11951 decl-specifier; it is the beginning of the declarator. */
11952 constructor_p
11953 = (!found_decl_spec
11954 && constructor_possible_p
11955 && (cp_parser_constructor_declarator_p
11956 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11958 /* If we don't have a DECL_SPEC yet, then we must be looking at
11959 a type-specifier. */
11960 if (!found_decl_spec && !constructor_p)
11962 int decl_spec_declares_class_or_enum;
11963 bool is_cv_qualifier;
11964 tree type_spec;
11966 type_spec
11967 = cp_parser_type_specifier (parser, flags,
11968 decl_specs,
11969 /*is_declaration=*/true,
11970 &decl_spec_declares_class_or_enum,
11971 &is_cv_qualifier);
11972 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11974 /* If this type-specifier referenced a user-defined type
11975 (a typedef, class-name, etc.), then we can't allow any
11976 more such type-specifiers henceforth.
11978 [dcl.spec]
11980 The longest sequence of decl-specifiers that could
11981 possibly be a type name is taken as the
11982 decl-specifier-seq of a declaration. The sequence shall
11983 be self-consistent as described below.
11985 [dcl.type]
11987 As a general rule, at most one type-specifier is allowed
11988 in the complete decl-specifier-seq of a declaration. The
11989 only exceptions are the following:
11991 -- const or volatile can be combined with any other
11992 type-specifier.
11994 -- signed or unsigned can be combined with char, long,
11995 short, or int.
11997 -- ..
11999 Example:
12001 typedef char* Pc;
12002 void g (const int Pc);
12004 Here, Pc is *not* part of the decl-specifier seq; it's
12005 the declarator. Therefore, once we see a type-specifier
12006 (other than a cv-qualifier), we forbid any additional
12007 user-defined types. We *do* still allow things like `int
12008 int' to be considered a decl-specifier-seq, and issue the
12009 error message later. */
12010 if (type_spec && !is_cv_qualifier)
12011 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12012 /* A constructor declarator cannot follow a type-specifier. */
12013 if (type_spec)
12015 constructor_possible_p = false;
12016 found_decl_spec = true;
12017 if (!is_cv_qualifier)
12018 decl_specs->any_type_specifiers_p = true;
12022 /* If we still do not have a DECL_SPEC, then there are no more
12023 decl-specifiers. */
12024 if (!found_decl_spec)
12025 break;
12027 decl_specs->any_specifiers_p = true;
12028 /* After we see one decl-specifier, further decl-specifiers are
12029 always optional. */
12030 flags |= CP_PARSER_FLAGS_OPTIONAL;
12033 /* Don't allow a friend specifier with a class definition. */
12034 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12035 && (*declares_class_or_enum & 2))
12036 error_at (decl_specs->locations[ds_friend],
12037 "class definition may not be declared a friend");
12040 /* Parse an (optional) storage-class-specifier.
12042 storage-class-specifier:
12043 auto
12044 register
12045 static
12046 extern
12047 mutable
12049 GNU Extension:
12051 storage-class-specifier:
12052 thread
12054 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12056 static tree
12057 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12059 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12061 case RID_AUTO:
12062 if (cxx_dialect != cxx98)
12063 return NULL_TREE;
12064 /* Fall through for C++98. */
12066 case RID_REGISTER:
12067 case RID_STATIC:
12068 case RID_EXTERN:
12069 case RID_MUTABLE:
12070 case RID_THREAD:
12071 /* Consume the token. */
12072 return cp_lexer_consume_token (parser->lexer)->u.value;
12074 default:
12075 return NULL_TREE;
12079 /* Parse an (optional) function-specifier.
12081 function-specifier:
12082 inline
12083 virtual
12084 explicit
12086 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12087 Updates DECL_SPECS, if it is non-NULL. */
12089 static tree
12090 cp_parser_function_specifier_opt (cp_parser* parser,
12091 cp_decl_specifier_seq *decl_specs)
12093 cp_token *token = cp_lexer_peek_token (parser->lexer);
12094 switch (token->keyword)
12096 case RID_INLINE:
12097 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12098 break;
12100 case RID_VIRTUAL:
12101 /* 14.5.2.3 [temp.mem]
12103 A member function template shall not be virtual. */
12104 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12105 error_at (token->location, "templates may not be %<virtual%>");
12106 else
12107 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12108 break;
12110 case RID_EXPLICIT:
12111 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12112 break;
12114 default:
12115 return NULL_TREE;
12118 /* Consume the token. */
12119 return cp_lexer_consume_token (parser->lexer)->u.value;
12122 /* Parse a linkage-specification.
12124 linkage-specification:
12125 extern string-literal { declaration-seq [opt] }
12126 extern string-literal declaration */
12128 static void
12129 cp_parser_linkage_specification (cp_parser* parser)
12131 tree linkage;
12133 /* Look for the `extern' keyword. */
12134 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12136 /* Look for the string-literal. */
12137 linkage = cp_parser_string_literal (parser, false, false);
12139 /* Transform the literal into an identifier. If the literal is a
12140 wide-character string, or contains embedded NULs, then we can't
12141 handle it as the user wants. */
12142 if (strlen (TREE_STRING_POINTER (linkage))
12143 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12145 cp_parser_error (parser, "invalid linkage-specification");
12146 /* Assume C++ linkage. */
12147 linkage = lang_name_cplusplus;
12149 else
12150 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12152 /* We're now using the new linkage. */
12153 push_lang_context (linkage);
12155 /* If the next token is a `{', then we're using the first
12156 production. */
12157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12159 cp_ensure_no_omp_declare_simd (parser);
12161 /* Consume the `{' token. */
12162 cp_lexer_consume_token (parser->lexer);
12163 /* Parse the declarations. */
12164 cp_parser_declaration_seq_opt (parser);
12165 /* Look for the closing `}'. */
12166 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12168 /* Otherwise, there's just one declaration. */
12169 else
12171 bool saved_in_unbraced_linkage_specification_p;
12173 saved_in_unbraced_linkage_specification_p
12174 = parser->in_unbraced_linkage_specification_p;
12175 parser->in_unbraced_linkage_specification_p = true;
12176 cp_parser_declaration (parser);
12177 parser->in_unbraced_linkage_specification_p
12178 = saved_in_unbraced_linkage_specification_p;
12181 /* We're done with the linkage-specification. */
12182 pop_lang_context ();
12185 /* Parse a static_assert-declaration.
12187 static_assert-declaration:
12188 static_assert ( constant-expression , string-literal ) ;
12189 static_assert ( constant-expression ) ; (C++1Z)
12191 If MEMBER_P, this static_assert is a class member. */
12193 static void
12194 cp_parser_static_assert(cp_parser *parser, bool member_p)
12196 tree condition;
12197 tree message;
12198 cp_token *token;
12199 location_t saved_loc;
12200 bool dummy;
12202 /* Peek at the `static_assert' token so we can keep track of exactly
12203 where the static assertion started. */
12204 token = cp_lexer_peek_token (parser->lexer);
12205 saved_loc = token->location;
12207 /* Look for the `static_assert' keyword. */
12208 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12209 RT_STATIC_ASSERT))
12210 return;
12212 /* We know we are in a static assertion; commit to any tentative
12213 parse. */
12214 if (cp_parser_parsing_tentatively (parser))
12215 cp_parser_commit_to_tentative_parse (parser);
12217 /* Parse the `(' starting the static assertion condition. */
12218 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12220 /* Parse the constant-expression. Allow a non-constant expression
12221 here in order to give better diagnostics in finish_static_assert. */
12222 condition =
12223 cp_parser_constant_expression (parser,
12224 /*allow_non_constant_p=*/true,
12225 /*non_constant_p=*/&dummy);
12227 /* Make sure we folded it completely before doing trying to get
12228 constant value. */
12229 condition = fold_non_dependent_expr (condition);
12231 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12233 if (cxx_dialect < cxx1z)
12234 pedwarn (input_location, OPT_Wpedantic,
12235 "static_assert without a message "
12236 "only available with -std=c++1z or -std=gnu++1z");
12237 /* Eat the ')' */
12238 cp_lexer_consume_token (parser->lexer);
12239 message = build_string (1, "");
12240 TREE_TYPE (message) = char_array_type_node;
12241 fix_string_type (message);
12243 else
12245 /* Parse the separating `,'. */
12246 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12248 /* Parse the string-literal message. */
12249 message = cp_parser_string_literal (parser,
12250 /*translate=*/false,
12251 /*wide_ok=*/true);
12253 /* A `)' completes the static assertion. */
12254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12255 cp_parser_skip_to_closing_parenthesis (parser,
12256 /*recovering=*/true,
12257 /*or_comma=*/false,
12258 /*consume_paren=*/true);
12261 /* A semicolon terminates the declaration. */
12262 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12264 /* Complete the static assertion, which may mean either processing
12265 the static assert now or saving it for template instantiation. */
12266 finish_static_assert (condition, message, saved_loc, member_p);
12269 /* Parse the expression in decltype ( expression ). */
12271 static tree
12272 cp_parser_decltype_expr (cp_parser *parser,
12273 bool &id_expression_or_member_access_p)
12275 cp_token *id_expr_start_token;
12276 tree expr;
12278 /* First, try parsing an id-expression. */
12279 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12280 cp_parser_parse_tentatively (parser);
12281 expr = cp_parser_id_expression (parser,
12282 /*template_keyword_p=*/false,
12283 /*check_dependency_p=*/true,
12284 /*template_p=*/NULL,
12285 /*declarator_p=*/false,
12286 /*optional_p=*/false);
12288 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12290 bool non_integral_constant_expression_p = false;
12291 tree id_expression = expr;
12292 cp_id_kind idk;
12293 const char *error_msg;
12295 if (identifier_p (expr))
12296 /* Lookup the name we got back from the id-expression. */
12297 expr = cp_parser_lookup_name_simple (parser, expr,
12298 id_expr_start_token->location);
12300 if (expr
12301 && expr != error_mark_node
12302 && TREE_CODE (expr) != TYPE_DECL
12303 && (TREE_CODE (expr) != BIT_NOT_EXPR
12304 || !TYPE_P (TREE_OPERAND (expr, 0)))
12305 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12307 /* Complete lookup of the id-expression. */
12308 expr = (finish_id_expression
12309 (id_expression, expr, parser->scope, &idk,
12310 /*integral_constant_expression_p=*/false,
12311 /*allow_non_integral_constant_expression_p=*/true,
12312 &non_integral_constant_expression_p,
12313 /*template_p=*/false,
12314 /*done=*/true,
12315 /*address_p=*/false,
12316 /*template_arg_p=*/false,
12317 &error_msg,
12318 id_expr_start_token->location));
12320 if (expr == error_mark_node)
12321 /* We found an id-expression, but it was something that we
12322 should not have found. This is an error, not something
12323 we can recover from, so note that we found an
12324 id-expression and we'll recover as gracefully as
12325 possible. */
12326 id_expression_or_member_access_p = true;
12329 if (expr
12330 && expr != error_mark_node
12331 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12332 /* We have an id-expression. */
12333 id_expression_or_member_access_p = true;
12336 if (!id_expression_or_member_access_p)
12338 /* Abort the id-expression parse. */
12339 cp_parser_abort_tentative_parse (parser);
12341 /* Parsing tentatively, again. */
12342 cp_parser_parse_tentatively (parser);
12344 /* Parse a class member access. */
12345 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12346 /*cast_p=*/false, /*decltype*/true,
12347 /*member_access_only_p=*/true, NULL);
12349 if (expr
12350 && expr != error_mark_node
12351 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12352 /* We have an id-expression. */
12353 id_expression_or_member_access_p = true;
12356 if (id_expression_or_member_access_p)
12357 /* We have parsed the complete id-expression or member access. */
12358 cp_parser_parse_definitely (parser);
12359 else
12361 /* Abort our attempt to parse an id-expression or member access
12362 expression. */
12363 cp_parser_abort_tentative_parse (parser);
12365 /* Parse a full expression. */
12366 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12367 /*decltype_p=*/true);
12370 return expr;
12373 /* Parse a `decltype' type. Returns the type.
12375 simple-type-specifier:
12376 decltype ( expression )
12377 C++14 proposal:
12378 decltype ( auto ) */
12380 static tree
12381 cp_parser_decltype (cp_parser *parser)
12383 tree expr;
12384 bool id_expression_or_member_access_p = false;
12385 const char *saved_message;
12386 bool saved_integral_constant_expression_p;
12387 bool saved_non_integral_constant_expression_p;
12388 bool saved_greater_than_is_operator_p;
12389 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12391 if (start_token->type == CPP_DECLTYPE)
12393 /* Already parsed. */
12394 cp_lexer_consume_token (parser->lexer);
12395 return start_token->u.value;
12398 /* Look for the `decltype' token. */
12399 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12400 return error_mark_node;
12402 /* Parse the opening `('. */
12403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12404 return error_mark_node;
12406 /* decltype (auto) */
12407 if (cxx_dialect >= cxx14
12408 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12410 cp_lexer_consume_token (parser->lexer);
12411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12412 return error_mark_node;
12413 expr = make_decltype_auto ();
12414 AUTO_IS_DECLTYPE (expr) = true;
12415 goto rewrite;
12418 /* Types cannot be defined in a `decltype' expression. Save away the
12419 old message. */
12420 saved_message = parser->type_definition_forbidden_message;
12422 /* And create the new one. */
12423 parser->type_definition_forbidden_message
12424 = G_("types may not be defined in %<decltype%> expressions");
12426 /* The restrictions on constant-expressions do not apply inside
12427 decltype expressions. */
12428 saved_integral_constant_expression_p
12429 = parser->integral_constant_expression_p;
12430 saved_non_integral_constant_expression_p
12431 = parser->non_integral_constant_expression_p;
12432 parser->integral_constant_expression_p = false;
12434 /* Within a parenthesized expression, a `>' token is always
12435 the greater-than operator. */
12436 saved_greater_than_is_operator_p
12437 = parser->greater_than_is_operator_p;
12438 parser->greater_than_is_operator_p = true;
12440 /* Do not actually evaluate the expression. */
12441 ++cp_unevaluated_operand;
12443 /* Do not warn about problems with the expression. */
12444 ++c_inhibit_evaluation_warnings;
12446 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12448 /* Go back to evaluating expressions. */
12449 --cp_unevaluated_operand;
12450 --c_inhibit_evaluation_warnings;
12452 /* The `>' token might be the end of a template-id or
12453 template-parameter-list now. */
12454 parser->greater_than_is_operator_p
12455 = saved_greater_than_is_operator_p;
12457 /* Restore the old message and the integral constant expression
12458 flags. */
12459 parser->type_definition_forbidden_message = saved_message;
12460 parser->integral_constant_expression_p
12461 = saved_integral_constant_expression_p;
12462 parser->non_integral_constant_expression_p
12463 = saved_non_integral_constant_expression_p;
12465 /* Parse to the closing `)'. */
12466 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12468 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12469 /*consume_paren=*/true);
12470 return error_mark_node;
12473 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12474 tf_warning_or_error);
12476 rewrite:
12477 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12478 it again. */
12479 start_token->type = CPP_DECLTYPE;
12480 start_token->u.value = expr;
12481 start_token->keyword = RID_MAX;
12482 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12484 return expr;
12487 /* Special member functions [gram.special] */
12489 /* Parse a conversion-function-id.
12491 conversion-function-id:
12492 operator conversion-type-id
12494 Returns an IDENTIFIER_NODE representing the operator. */
12496 static tree
12497 cp_parser_conversion_function_id (cp_parser* parser)
12499 tree type;
12500 tree saved_scope;
12501 tree saved_qualifying_scope;
12502 tree saved_object_scope;
12503 tree pushed_scope = NULL_TREE;
12505 /* Look for the `operator' token. */
12506 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12507 return error_mark_node;
12508 /* When we parse the conversion-type-id, the current scope will be
12509 reset. However, we need that information in able to look up the
12510 conversion function later, so we save it here. */
12511 saved_scope = parser->scope;
12512 saved_qualifying_scope = parser->qualifying_scope;
12513 saved_object_scope = parser->object_scope;
12514 /* We must enter the scope of the class so that the names of
12515 entities declared within the class are available in the
12516 conversion-type-id. For example, consider:
12518 struct S {
12519 typedef int I;
12520 operator I();
12523 S::operator I() { ... }
12525 In order to see that `I' is a type-name in the definition, we
12526 must be in the scope of `S'. */
12527 if (saved_scope)
12528 pushed_scope = push_scope (saved_scope);
12529 /* Parse the conversion-type-id. */
12530 type = cp_parser_conversion_type_id (parser);
12531 /* Leave the scope of the class, if any. */
12532 if (pushed_scope)
12533 pop_scope (pushed_scope);
12534 /* Restore the saved scope. */
12535 parser->scope = saved_scope;
12536 parser->qualifying_scope = saved_qualifying_scope;
12537 parser->object_scope = saved_object_scope;
12538 /* If the TYPE is invalid, indicate failure. */
12539 if (type == error_mark_node)
12540 return error_mark_node;
12541 return mangle_conv_op_name_for_type (type);
12544 /* Parse a conversion-type-id:
12546 conversion-type-id:
12547 type-specifier-seq conversion-declarator [opt]
12549 Returns the TYPE specified. */
12551 static tree
12552 cp_parser_conversion_type_id (cp_parser* parser)
12554 tree attributes;
12555 cp_decl_specifier_seq type_specifiers;
12556 cp_declarator *declarator;
12557 tree type_specified;
12558 const char *saved_message;
12560 /* Parse the attributes. */
12561 attributes = cp_parser_attributes_opt (parser);
12563 saved_message = parser->type_definition_forbidden_message;
12564 parser->type_definition_forbidden_message
12565 = G_("types may not be defined in a conversion-type-id");
12567 /* Parse the type-specifiers. */
12568 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12569 /*is_trailing_return=*/false,
12570 &type_specifiers);
12572 parser->type_definition_forbidden_message = saved_message;
12574 /* If that didn't work, stop. */
12575 if (type_specifiers.type == error_mark_node)
12576 return error_mark_node;
12577 /* Parse the conversion-declarator. */
12578 declarator = cp_parser_conversion_declarator_opt (parser);
12580 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12581 /*initialized=*/0, &attributes);
12582 if (attributes)
12583 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12585 /* Don't give this error when parsing tentatively. This happens to
12586 work because we always parse this definitively once. */
12587 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12588 && type_uses_auto (type_specified))
12590 if (cxx_dialect < cxx14)
12592 error ("invalid use of %<auto%> in conversion operator");
12593 return error_mark_node;
12595 else if (template_parm_scope_p ())
12596 warning (0, "use of %<auto%> in member template "
12597 "conversion operator can never be deduced");
12600 return type_specified;
12603 /* Parse an (optional) conversion-declarator.
12605 conversion-declarator:
12606 ptr-operator conversion-declarator [opt]
12610 static cp_declarator *
12611 cp_parser_conversion_declarator_opt (cp_parser* parser)
12613 enum tree_code code;
12614 tree class_type, std_attributes = NULL_TREE;
12615 cp_cv_quals cv_quals;
12617 /* We don't know if there's a ptr-operator next, or not. */
12618 cp_parser_parse_tentatively (parser);
12619 /* Try the ptr-operator. */
12620 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12621 &std_attributes);
12622 /* If it worked, look for more conversion-declarators. */
12623 if (cp_parser_parse_definitely (parser))
12625 cp_declarator *declarator;
12627 /* Parse another optional declarator. */
12628 declarator = cp_parser_conversion_declarator_opt (parser);
12630 declarator = cp_parser_make_indirect_declarator
12631 (code, class_type, cv_quals, declarator, std_attributes);
12633 return declarator;
12636 return NULL;
12639 /* Parse an (optional) ctor-initializer.
12641 ctor-initializer:
12642 : mem-initializer-list
12644 Returns TRUE iff the ctor-initializer was actually present. */
12646 static bool
12647 cp_parser_ctor_initializer_opt (cp_parser* parser)
12649 /* If the next token is not a `:', then there is no
12650 ctor-initializer. */
12651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12653 /* Do default initialization of any bases and members. */
12654 if (DECL_CONSTRUCTOR_P (current_function_decl))
12655 finish_mem_initializers (NULL_TREE);
12657 return false;
12660 /* Consume the `:' token. */
12661 cp_lexer_consume_token (parser->lexer);
12662 /* And the mem-initializer-list. */
12663 cp_parser_mem_initializer_list (parser);
12665 return true;
12668 /* Parse a mem-initializer-list.
12670 mem-initializer-list:
12671 mem-initializer ... [opt]
12672 mem-initializer ... [opt] , mem-initializer-list */
12674 static void
12675 cp_parser_mem_initializer_list (cp_parser* parser)
12677 tree mem_initializer_list = NULL_TREE;
12678 tree target_ctor = error_mark_node;
12679 cp_token *token = cp_lexer_peek_token (parser->lexer);
12681 /* Let the semantic analysis code know that we are starting the
12682 mem-initializer-list. */
12683 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12684 error_at (token->location,
12685 "only constructors take member initializers");
12687 /* Loop through the list. */
12688 while (true)
12690 tree mem_initializer;
12692 token = cp_lexer_peek_token (parser->lexer);
12693 /* Parse the mem-initializer. */
12694 mem_initializer = cp_parser_mem_initializer (parser);
12695 /* If the next token is a `...', we're expanding member initializers. */
12696 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12698 /* Consume the `...'. */
12699 cp_lexer_consume_token (parser->lexer);
12701 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12702 can be expanded but members cannot. */
12703 if (mem_initializer != error_mark_node
12704 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12706 error_at (token->location,
12707 "cannot expand initializer for member %<%D%>",
12708 TREE_PURPOSE (mem_initializer));
12709 mem_initializer = error_mark_node;
12712 /* Construct the pack expansion type. */
12713 if (mem_initializer != error_mark_node)
12714 mem_initializer = make_pack_expansion (mem_initializer);
12716 if (target_ctor != error_mark_node
12717 && mem_initializer != error_mark_node)
12719 error ("mem-initializer for %qD follows constructor delegation",
12720 TREE_PURPOSE (mem_initializer));
12721 mem_initializer = error_mark_node;
12723 /* Look for a target constructor. */
12724 if (mem_initializer != error_mark_node
12725 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12726 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12728 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12729 if (mem_initializer_list)
12731 error ("constructor delegation follows mem-initializer for %qD",
12732 TREE_PURPOSE (mem_initializer_list));
12733 mem_initializer = error_mark_node;
12735 target_ctor = mem_initializer;
12737 /* Add it to the list, unless it was erroneous. */
12738 if (mem_initializer != error_mark_node)
12740 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12741 mem_initializer_list = mem_initializer;
12743 /* If the next token is not a `,', we're done. */
12744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12745 break;
12746 /* Consume the `,' token. */
12747 cp_lexer_consume_token (parser->lexer);
12750 /* Perform semantic analysis. */
12751 if (DECL_CONSTRUCTOR_P (current_function_decl))
12752 finish_mem_initializers (mem_initializer_list);
12755 /* Parse a mem-initializer.
12757 mem-initializer:
12758 mem-initializer-id ( expression-list [opt] )
12759 mem-initializer-id braced-init-list
12761 GNU extension:
12763 mem-initializer:
12764 ( expression-list [opt] )
12766 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12767 class) or FIELD_DECL (for a non-static data member) to initialize;
12768 the TREE_VALUE is the expression-list. An empty initialization
12769 list is represented by void_list_node. */
12771 static tree
12772 cp_parser_mem_initializer (cp_parser* parser)
12774 tree mem_initializer_id;
12775 tree expression_list;
12776 tree member;
12777 cp_token *token = cp_lexer_peek_token (parser->lexer);
12779 /* Find out what is being initialized. */
12780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12782 permerror (token->location,
12783 "anachronistic old-style base class initializer");
12784 mem_initializer_id = NULL_TREE;
12786 else
12788 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12789 if (mem_initializer_id == error_mark_node)
12790 return mem_initializer_id;
12792 member = expand_member_init (mem_initializer_id);
12793 if (member && !DECL_P (member))
12794 in_base_initializer = 1;
12796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12798 bool expr_non_constant_p;
12799 cp_lexer_set_source_position (parser->lexer);
12800 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12801 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12802 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12803 expression_list = build_tree_list (NULL_TREE, expression_list);
12805 else
12807 vec<tree, va_gc> *vec;
12808 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12809 /*cast_p=*/false,
12810 /*allow_expansion_p=*/true,
12811 /*non_constant_p=*/NULL);
12812 if (vec == NULL)
12813 return error_mark_node;
12814 expression_list = build_tree_list_vec (vec);
12815 release_tree_vector (vec);
12818 if (expression_list == error_mark_node)
12819 return error_mark_node;
12820 if (!expression_list)
12821 expression_list = void_type_node;
12823 in_base_initializer = 0;
12825 return member ? build_tree_list (member, expression_list) : error_mark_node;
12828 /* Parse a mem-initializer-id.
12830 mem-initializer-id:
12831 :: [opt] nested-name-specifier [opt] class-name
12832 decltype-specifier (C++11)
12833 identifier
12835 Returns a TYPE indicating the class to be initialized for the first
12836 production (and the second in C++11). Returns an IDENTIFIER_NODE
12837 indicating the data member to be initialized for the last production. */
12839 static tree
12840 cp_parser_mem_initializer_id (cp_parser* parser)
12842 bool global_scope_p;
12843 bool nested_name_specifier_p;
12844 bool template_p = false;
12845 tree id;
12847 cp_token *token = cp_lexer_peek_token (parser->lexer);
12849 /* `typename' is not allowed in this context ([temp.res]). */
12850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12852 error_at (token->location,
12853 "keyword %<typename%> not allowed in this context (a qualified "
12854 "member initializer is implicitly a type)");
12855 cp_lexer_consume_token (parser->lexer);
12857 /* Look for the optional `::' operator. */
12858 global_scope_p
12859 = (cp_parser_global_scope_opt (parser,
12860 /*current_scope_valid_p=*/false)
12861 != NULL_TREE);
12862 /* Look for the optional nested-name-specifier. The simplest way to
12863 implement:
12865 [temp.res]
12867 The keyword `typename' is not permitted in a base-specifier or
12868 mem-initializer; in these contexts a qualified name that
12869 depends on a template-parameter is implicitly assumed to be a
12870 type name.
12872 is to assume that we have seen the `typename' keyword at this
12873 point. */
12874 nested_name_specifier_p
12875 = (cp_parser_nested_name_specifier_opt (parser,
12876 /*typename_keyword_p=*/true,
12877 /*check_dependency_p=*/true,
12878 /*type_p=*/true,
12879 /*is_declaration=*/true)
12880 != NULL_TREE);
12881 if (nested_name_specifier_p)
12882 template_p = cp_parser_optional_template_keyword (parser);
12883 /* If there is a `::' operator or a nested-name-specifier, then we
12884 are definitely looking for a class-name. */
12885 if (global_scope_p || nested_name_specifier_p)
12886 return cp_parser_class_name (parser,
12887 /*typename_keyword_p=*/true,
12888 /*template_keyword_p=*/template_p,
12889 typename_type,
12890 /*check_dependency_p=*/true,
12891 /*class_head_p=*/false,
12892 /*is_declaration=*/true);
12893 /* Otherwise, we could also be looking for an ordinary identifier. */
12894 cp_parser_parse_tentatively (parser);
12895 if (cp_lexer_next_token_is_decltype (parser->lexer))
12896 /* Try a decltype-specifier. */
12897 id = cp_parser_decltype (parser);
12898 else
12899 /* Otherwise, try a class-name. */
12900 id = cp_parser_class_name (parser,
12901 /*typename_keyword_p=*/true,
12902 /*template_keyword_p=*/false,
12903 none_type,
12904 /*check_dependency_p=*/true,
12905 /*class_head_p=*/false,
12906 /*is_declaration=*/true);
12907 /* If we found one, we're done. */
12908 if (cp_parser_parse_definitely (parser))
12909 return id;
12910 /* Otherwise, look for an ordinary identifier. */
12911 return cp_parser_identifier (parser);
12914 /* Overloading [gram.over] */
12916 /* Parse an operator-function-id.
12918 operator-function-id:
12919 operator operator
12921 Returns an IDENTIFIER_NODE for the operator which is a
12922 human-readable spelling of the identifier, e.g., `operator +'. */
12924 static tree
12925 cp_parser_operator_function_id (cp_parser* parser)
12927 /* Look for the `operator' keyword. */
12928 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12929 return error_mark_node;
12930 /* And then the name of the operator itself. */
12931 return cp_parser_operator (parser);
12934 /* Return an identifier node for a user-defined literal operator.
12935 The suffix identifier is chained to the operator name identifier. */
12937 static tree
12938 cp_literal_operator_id (const char* name)
12940 tree identifier;
12941 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12942 + strlen (name) + 10);
12943 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12944 identifier = get_identifier (buffer);
12946 return identifier;
12949 /* Parse an operator.
12951 operator:
12952 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12953 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12954 || ++ -- , ->* -> () []
12956 GNU Extensions:
12958 operator:
12959 <? >? <?= >?=
12961 Returns an IDENTIFIER_NODE for the operator which is a
12962 human-readable spelling of the identifier, e.g., `operator +'. */
12964 static tree
12965 cp_parser_operator (cp_parser* parser)
12967 tree id = NULL_TREE;
12968 cp_token *token;
12969 bool utf8 = false;
12971 /* Peek at the next token. */
12972 token = cp_lexer_peek_token (parser->lexer);
12973 /* Figure out which operator we have. */
12974 switch (token->type)
12976 case CPP_KEYWORD:
12978 enum tree_code op;
12980 /* The keyword should be either `new' or `delete'. */
12981 if (token->keyword == RID_NEW)
12982 op = NEW_EXPR;
12983 else if (token->keyword == RID_DELETE)
12984 op = DELETE_EXPR;
12985 else
12986 break;
12988 /* Consume the `new' or `delete' token. */
12989 cp_lexer_consume_token (parser->lexer);
12991 /* Peek at the next token. */
12992 token = cp_lexer_peek_token (parser->lexer);
12993 /* If it's a `[' token then this is the array variant of the
12994 operator. */
12995 if (token->type == CPP_OPEN_SQUARE)
12997 /* Consume the `[' token. */
12998 cp_lexer_consume_token (parser->lexer);
12999 /* Look for the `]' token. */
13000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13001 id = ansi_opname (op == NEW_EXPR
13002 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13004 /* Otherwise, we have the non-array variant. */
13005 else
13006 id = ansi_opname (op);
13008 return id;
13011 case CPP_PLUS:
13012 id = ansi_opname (PLUS_EXPR);
13013 break;
13015 case CPP_MINUS:
13016 id = ansi_opname (MINUS_EXPR);
13017 break;
13019 case CPP_MULT:
13020 id = ansi_opname (MULT_EXPR);
13021 break;
13023 case CPP_DIV:
13024 id = ansi_opname (TRUNC_DIV_EXPR);
13025 break;
13027 case CPP_MOD:
13028 id = ansi_opname (TRUNC_MOD_EXPR);
13029 break;
13031 case CPP_XOR:
13032 id = ansi_opname (BIT_XOR_EXPR);
13033 break;
13035 case CPP_AND:
13036 id = ansi_opname (BIT_AND_EXPR);
13037 break;
13039 case CPP_OR:
13040 id = ansi_opname (BIT_IOR_EXPR);
13041 break;
13043 case CPP_COMPL:
13044 id = ansi_opname (BIT_NOT_EXPR);
13045 break;
13047 case CPP_NOT:
13048 id = ansi_opname (TRUTH_NOT_EXPR);
13049 break;
13051 case CPP_EQ:
13052 id = ansi_assopname (NOP_EXPR);
13053 break;
13055 case CPP_LESS:
13056 id = ansi_opname (LT_EXPR);
13057 break;
13059 case CPP_GREATER:
13060 id = ansi_opname (GT_EXPR);
13061 break;
13063 case CPP_PLUS_EQ:
13064 id = ansi_assopname (PLUS_EXPR);
13065 break;
13067 case CPP_MINUS_EQ:
13068 id = ansi_assopname (MINUS_EXPR);
13069 break;
13071 case CPP_MULT_EQ:
13072 id = ansi_assopname (MULT_EXPR);
13073 break;
13075 case CPP_DIV_EQ:
13076 id = ansi_assopname (TRUNC_DIV_EXPR);
13077 break;
13079 case CPP_MOD_EQ:
13080 id = ansi_assopname (TRUNC_MOD_EXPR);
13081 break;
13083 case CPP_XOR_EQ:
13084 id = ansi_assopname (BIT_XOR_EXPR);
13085 break;
13087 case CPP_AND_EQ:
13088 id = ansi_assopname (BIT_AND_EXPR);
13089 break;
13091 case CPP_OR_EQ:
13092 id = ansi_assopname (BIT_IOR_EXPR);
13093 break;
13095 case CPP_LSHIFT:
13096 id = ansi_opname (LSHIFT_EXPR);
13097 break;
13099 case CPP_RSHIFT:
13100 id = ansi_opname (RSHIFT_EXPR);
13101 break;
13103 case CPP_LSHIFT_EQ:
13104 id = ansi_assopname (LSHIFT_EXPR);
13105 break;
13107 case CPP_RSHIFT_EQ:
13108 id = ansi_assopname (RSHIFT_EXPR);
13109 break;
13111 case CPP_EQ_EQ:
13112 id = ansi_opname (EQ_EXPR);
13113 break;
13115 case CPP_NOT_EQ:
13116 id = ansi_opname (NE_EXPR);
13117 break;
13119 case CPP_LESS_EQ:
13120 id = ansi_opname (LE_EXPR);
13121 break;
13123 case CPP_GREATER_EQ:
13124 id = ansi_opname (GE_EXPR);
13125 break;
13127 case CPP_AND_AND:
13128 id = ansi_opname (TRUTH_ANDIF_EXPR);
13129 break;
13131 case CPP_OR_OR:
13132 id = ansi_opname (TRUTH_ORIF_EXPR);
13133 break;
13135 case CPP_PLUS_PLUS:
13136 id = ansi_opname (POSTINCREMENT_EXPR);
13137 break;
13139 case CPP_MINUS_MINUS:
13140 id = ansi_opname (PREDECREMENT_EXPR);
13141 break;
13143 case CPP_COMMA:
13144 id = ansi_opname (COMPOUND_EXPR);
13145 break;
13147 case CPP_DEREF_STAR:
13148 id = ansi_opname (MEMBER_REF);
13149 break;
13151 case CPP_DEREF:
13152 id = ansi_opname (COMPONENT_REF);
13153 break;
13155 case CPP_OPEN_PAREN:
13156 /* Consume the `('. */
13157 cp_lexer_consume_token (parser->lexer);
13158 /* Look for the matching `)'. */
13159 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13160 return ansi_opname (CALL_EXPR);
13162 case CPP_OPEN_SQUARE:
13163 /* Consume the `['. */
13164 cp_lexer_consume_token (parser->lexer);
13165 /* Look for the matching `]'. */
13166 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13167 return ansi_opname (ARRAY_REF);
13169 case CPP_UTF8STRING:
13170 case CPP_UTF8STRING_USERDEF:
13171 utf8 = true;
13172 case CPP_STRING:
13173 case CPP_WSTRING:
13174 case CPP_STRING16:
13175 case CPP_STRING32:
13176 case CPP_STRING_USERDEF:
13177 case CPP_WSTRING_USERDEF:
13178 case CPP_STRING16_USERDEF:
13179 case CPP_STRING32_USERDEF:
13181 tree str, string_tree;
13182 int sz, len;
13184 if (cxx_dialect == cxx98)
13185 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13187 /* Consume the string. */
13188 str = cp_parser_string_literal (parser, /*translate=*/true,
13189 /*wide_ok=*/true, /*lookup_udlit=*/false);
13190 if (str == error_mark_node)
13191 return error_mark_node;
13192 else if (TREE_CODE (str) == USERDEF_LITERAL)
13194 string_tree = USERDEF_LITERAL_VALUE (str);
13195 id = USERDEF_LITERAL_SUFFIX_ID (str);
13197 else
13199 string_tree = str;
13200 /* Look for the suffix identifier. */
13201 token = cp_lexer_peek_token (parser->lexer);
13202 if (token->type == CPP_NAME)
13203 id = cp_parser_identifier (parser);
13204 else if (token->type == CPP_KEYWORD)
13206 error ("unexpected keyword;"
13207 " remove space between quotes and suffix identifier");
13208 return error_mark_node;
13210 else
13212 error ("expected suffix identifier");
13213 return error_mark_node;
13216 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13217 (TREE_TYPE (TREE_TYPE (string_tree))));
13218 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13219 if (len != 0)
13221 error ("expected empty string after %<operator%> keyword");
13222 return error_mark_node;
13224 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13225 != char_type_node)
13227 error ("invalid encoding prefix in literal operator");
13228 return error_mark_node;
13230 if (id != error_mark_node)
13232 const char *name = IDENTIFIER_POINTER (id);
13233 id = cp_literal_operator_id (name);
13235 return id;
13238 default:
13239 /* Anything else is an error. */
13240 break;
13243 /* If we have selected an identifier, we need to consume the
13244 operator token. */
13245 if (id)
13246 cp_lexer_consume_token (parser->lexer);
13247 /* Otherwise, no valid operator name was present. */
13248 else
13250 cp_parser_error (parser, "expected operator");
13251 id = error_mark_node;
13254 return id;
13257 /* Parse a template-declaration.
13259 template-declaration:
13260 export [opt] template < template-parameter-list > declaration
13262 If MEMBER_P is TRUE, this template-declaration occurs within a
13263 class-specifier.
13265 The grammar rule given by the standard isn't correct. What
13266 is really meant is:
13268 template-declaration:
13269 export [opt] template-parameter-list-seq
13270 decl-specifier-seq [opt] init-declarator [opt] ;
13271 export [opt] template-parameter-list-seq
13272 function-definition
13274 template-parameter-list-seq:
13275 template-parameter-list-seq [opt]
13276 template < template-parameter-list > */
13278 static void
13279 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13281 /* Check for `export'. */
13282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13284 /* Consume the `export' token. */
13285 cp_lexer_consume_token (parser->lexer);
13286 /* Warn that we do not support `export'. */
13287 warning (0, "keyword %<export%> not implemented, and will be ignored");
13290 cp_parser_template_declaration_after_export (parser, member_p);
13293 /* Parse a template-parameter-list.
13295 template-parameter-list:
13296 template-parameter
13297 template-parameter-list , template-parameter
13299 Returns a TREE_LIST. Each node represents a template parameter.
13300 The nodes are connected via their TREE_CHAINs. */
13302 static tree
13303 cp_parser_template_parameter_list (cp_parser* parser)
13305 tree parameter_list = NULL_TREE;
13307 begin_template_parm_list ();
13309 /* The loop below parses the template parms. We first need to know
13310 the total number of template parms to be able to compute proper
13311 canonical types of each dependent type. So after the loop, when
13312 we know the total number of template parms,
13313 end_template_parm_list computes the proper canonical types and
13314 fixes up the dependent types accordingly. */
13315 while (true)
13317 tree parameter;
13318 bool is_non_type;
13319 bool is_parameter_pack;
13320 location_t parm_loc;
13322 /* Parse the template-parameter. */
13323 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13324 parameter = cp_parser_template_parameter (parser,
13325 &is_non_type,
13326 &is_parameter_pack);
13327 /* Add it to the list. */
13328 if (parameter != error_mark_node)
13329 parameter_list = process_template_parm (parameter_list,
13330 parm_loc,
13331 parameter,
13332 is_non_type,
13333 is_parameter_pack);
13334 else
13336 tree err_parm = build_tree_list (parameter, parameter);
13337 parameter_list = chainon (parameter_list, err_parm);
13340 /* If the next token is not a `,', we're done. */
13341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13342 break;
13343 /* Otherwise, consume the `,' token. */
13344 cp_lexer_consume_token (parser->lexer);
13347 return end_template_parm_list (parameter_list);
13350 /* Parse a template-parameter.
13352 template-parameter:
13353 type-parameter
13354 parameter-declaration
13356 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13357 the parameter. The TREE_PURPOSE is the default value, if any.
13358 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13359 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13360 set to true iff this parameter is a parameter pack. */
13362 static tree
13363 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13364 bool *is_parameter_pack)
13366 cp_token *token;
13367 cp_parameter_declarator *parameter_declarator;
13368 cp_declarator *id_declarator;
13369 tree parm;
13371 /* Assume it is a type parameter or a template parameter. */
13372 *is_non_type = false;
13373 /* Assume it not a parameter pack. */
13374 *is_parameter_pack = false;
13375 /* Peek at the next token. */
13376 token = cp_lexer_peek_token (parser->lexer);
13377 /* If it is `class' or `template', we have a type-parameter. */
13378 if (token->keyword == RID_TEMPLATE)
13379 return cp_parser_type_parameter (parser, is_parameter_pack);
13380 /* If it is `class' or `typename' we do not know yet whether it is a
13381 type parameter or a non-type parameter. Consider:
13383 template <typename T, typename T::X X> ...
13387 template <class C, class D*> ...
13389 Here, the first parameter is a type parameter, and the second is
13390 a non-type parameter. We can tell by looking at the token after
13391 the identifier -- if it is a `,', `=', or `>' then we have a type
13392 parameter. */
13393 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13395 /* Peek at the token after `class' or `typename'. */
13396 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13397 /* If it's an ellipsis, we have a template type parameter
13398 pack. */
13399 if (token->type == CPP_ELLIPSIS)
13400 return cp_parser_type_parameter (parser, is_parameter_pack);
13401 /* If it's an identifier, skip it. */
13402 if (token->type == CPP_NAME)
13403 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13404 /* Now, see if the token looks like the end of a template
13405 parameter. */
13406 if (token->type == CPP_COMMA
13407 || token->type == CPP_EQ
13408 || token->type == CPP_GREATER)
13409 return cp_parser_type_parameter (parser, is_parameter_pack);
13412 /* Otherwise, it is a non-type parameter.
13414 [temp.param]
13416 When parsing a default template-argument for a non-type
13417 template-parameter, the first non-nested `>' is taken as the end
13418 of the template parameter-list rather than a greater-than
13419 operator. */
13420 *is_non_type = true;
13421 parameter_declarator
13422 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13423 /*parenthesized_p=*/NULL);
13425 if (!parameter_declarator)
13426 return error_mark_node;
13428 /* If the parameter declaration is marked as a parameter pack, set
13429 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13430 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13431 grokdeclarator. */
13432 if (parameter_declarator->declarator
13433 && parameter_declarator->declarator->parameter_pack_p)
13435 *is_parameter_pack = true;
13436 parameter_declarator->declarator->parameter_pack_p = false;
13439 if (parameter_declarator->default_argument)
13441 /* Can happen in some cases of erroneous input (c++/34892). */
13442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13443 /* Consume the `...' for better error recovery. */
13444 cp_lexer_consume_token (parser->lexer);
13446 /* If the next token is an ellipsis, and we don't already have it
13447 marked as a parameter pack, then we have a parameter pack (that
13448 has no declarator). */
13449 else if (!*is_parameter_pack
13450 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13451 && (declarator_can_be_parameter_pack
13452 (parameter_declarator->declarator)))
13454 /* Consume the `...'. */
13455 cp_lexer_consume_token (parser->lexer);
13456 maybe_warn_variadic_templates ();
13458 *is_parameter_pack = true;
13460 /* We might end up with a pack expansion as the type of the non-type
13461 template parameter, in which case this is a non-type template
13462 parameter pack. */
13463 else if (parameter_declarator->decl_specifiers.type
13464 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13466 *is_parameter_pack = true;
13467 parameter_declarator->decl_specifiers.type =
13468 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13471 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13473 /* Parameter packs cannot have default arguments. However, a
13474 user may try to do so, so we'll parse them and give an
13475 appropriate diagnostic here. */
13477 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13479 /* Find the name of the parameter pack. */
13480 id_declarator = parameter_declarator->declarator;
13481 while (id_declarator && id_declarator->kind != cdk_id)
13482 id_declarator = id_declarator->declarator;
13484 if (id_declarator && id_declarator->kind == cdk_id)
13485 error_at (start_token->location,
13486 "template parameter pack %qD cannot have a default argument",
13487 id_declarator->u.id.unqualified_name);
13488 else
13489 error_at (start_token->location,
13490 "template parameter pack cannot have a default argument");
13492 /* Parse the default argument, but throw away the result. */
13493 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13496 parm = grokdeclarator (parameter_declarator->declarator,
13497 &parameter_declarator->decl_specifiers,
13498 TPARM, /*initialized=*/0,
13499 /*attrlist=*/NULL);
13500 if (parm == error_mark_node)
13501 return error_mark_node;
13503 return build_tree_list (parameter_declarator->default_argument, parm);
13506 /* Parse a type-parameter.
13508 type-parameter:
13509 class identifier [opt]
13510 class identifier [opt] = type-id
13511 typename identifier [opt]
13512 typename identifier [opt] = type-id
13513 template < template-parameter-list > class identifier [opt]
13514 template < template-parameter-list > class identifier [opt]
13515 = id-expression
13517 GNU Extension (variadic templates):
13519 type-parameter:
13520 class ... identifier [opt]
13521 typename ... identifier [opt]
13523 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13524 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13525 the declaration of the parameter.
13527 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13529 static tree
13530 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13532 cp_token *token;
13533 tree parameter;
13535 /* Look for a keyword to tell us what kind of parameter this is. */
13536 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13537 if (!token)
13538 return error_mark_node;
13540 switch (token->keyword)
13542 case RID_CLASS:
13543 case RID_TYPENAME:
13545 tree identifier;
13546 tree default_argument;
13548 /* If the next token is an ellipsis, we have a template
13549 argument pack. */
13550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13552 /* Consume the `...' token. */
13553 cp_lexer_consume_token (parser->lexer);
13554 maybe_warn_variadic_templates ();
13556 *is_parameter_pack = true;
13559 /* If the next token is an identifier, then it names the
13560 parameter. */
13561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13562 identifier = cp_parser_identifier (parser);
13563 else
13564 identifier = NULL_TREE;
13566 /* Create the parameter. */
13567 parameter = finish_template_type_parm (class_type_node, identifier);
13569 /* If the next token is an `=', we have a default argument. */
13570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13572 /* Consume the `=' token. */
13573 cp_lexer_consume_token (parser->lexer);
13574 /* Parse the default-argument. */
13575 push_deferring_access_checks (dk_no_deferred);
13576 default_argument = cp_parser_type_id (parser);
13578 /* Template parameter packs cannot have default
13579 arguments. */
13580 if (*is_parameter_pack)
13582 if (identifier)
13583 error_at (token->location,
13584 "template parameter pack %qD cannot have a "
13585 "default argument", identifier);
13586 else
13587 error_at (token->location,
13588 "template parameter packs cannot have "
13589 "default arguments");
13590 default_argument = NULL_TREE;
13592 else if (check_for_bare_parameter_packs (default_argument))
13593 default_argument = error_mark_node;
13594 pop_deferring_access_checks ();
13596 else
13597 default_argument = NULL_TREE;
13599 /* Create the combined representation of the parameter and the
13600 default argument. */
13601 parameter = build_tree_list (default_argument, parameter);
13603 break;
13605 case RID_TEMPLATE:
13607 tree identifier;
13608 tree default_argument;
13610 /* Look for the `<'. */
13611 cp_parser_require (parser, CPP_LESS, RT_LESS);
13612 /* Parse the template-parameter-list. */
13613 cp_parser_template_parameter_list (parser);
13614 /* Look for the `>'. */
13615 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13616 /* Look for the `class' or 'typename' keywords. */
13617 cp_parser_type_parameter_key (parser);
13618 /* If the next token is an ellipsis, we have a template
13619 argument pack. */
13620 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13622 /* Consume the `...' token. */
13623 cp_lexer_consume_token (parser->lexer);
13624 maybe_warn_variadic_templates ();
13626 *is_parameter_pack = true;
13628 /* If the next token is an `=', then there is a
13629 default-argument. If the next token is a `>', we are at
13630 the end of the parameter-list. If the next token is a `,',
13631 then we are at the end of this parameter. */
13632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13633 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13634 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13636 identifier = cp_parser_identifier (parser);
13637 /* Treat invalid names as if the parameter were nameless. */
13638 if (identifier == error_mark_node)
13639 identifier = NULL_TREE;
13641 else
13642 identifier = NULL_TREE;
13644 /* Create the template parameter. */
13645 parameter = finish_template_template_parm (class_type_node,
13646 identifier);
13648 /* If the next token is an `=', then there is a
13649 default-argument. */
13650 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13652 bool is_template;
13654 /* Consume the `='. */
13655 cp_lexer_consume_token (parser->lexer);
13656 /* Parse the id-expression. */
13657 push_deferring_access_checks (dk_no_deferred);
13658 /* save token before parsing the id-expression, for error
13659 reporting */
13660 token = cp_lexer_peek_token (parser->lexer);
13661 default_argument
13662 = cp_parser_id_expression (parser,
13663 /*template_keyword_p=*/false,
13664 /*check_dependency_p=*/true,
13665 /*template_p=*/&is_template,
13666 /*declarator_p=*/false,
13667 /*optional_p=*/false);
13668 if (TREE_CODE (default_argument) == TYPE_DECL)
13669 /* If the id-expression was a template-id that refers to
13670 a template-class, we already have the declaration here,
13671 so no further lookup is needed. */
13673 else
13674 /* Look up the name. */
13675 default_argument
13676 = cp_parser_lookup_name (parser, default_argument,
13677 none_type,
13678 /*is_template=*/is_template,
13679 /*is_namespace=*/false,
13680 /*check_dependency=*/true,
13681 /*ambiguous_decls=*/NULL,
13682 token->location);
13683 /* See if the default argument is valid. */
13684 default_argument
13685 = check_template_template_default_arg (default_argument);
13687 /* Template parameter packs cannot have default
13688 arguments. */
13689 if (*is_parameter_pack)
13691 if (identifier)
13692 error_at (token->location,
13693 "template parameter pack %qD cannot "
13694 "have a default argument",
13695 identifier);
13696 else
13697 error_at (token->location, "template parameter packs cannot "
13698 "have default arguments");
13699 default_argument = NULL_TREE;
13701 pop_deferring_access_checks ();
13703 else
13704 default_argument = NULL_TREE;
13706 /* Create the combined representation of the parameter and the
13707 default argument. */
13708 parameter = build_tree_list (default_argument, parameter);
13710 break;
13712 default:
13713 gcc_unreachable ();
13714 break;
13717 return parameter;
13720 /* Parse a template-id.
13722 template-id:
13723 template-name < template-argument-list [opt] >
13725 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13726 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13727 returned. Otherwise, if the template-name names a function, or set
13728 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13729 names a class, returns a TYPE_DECL for the specialization.
13731 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13732 uninstantiated templates. */
13734 static tree
13735 cp_parser_template_id (cp_parser *parser,
13736 bool template_keyword_p,
13737 bool check_dependency_p,
13738 enum tag_types tag_type,
13739 bool is_declaration)
13741 int i;
13742 tree templ;
13743 tree arguments;
13744 tree template_id;
13745 cp_token_position start_of_id = 0;
13746 deferred_access_check *chk;
13747 vec<deferred_access_check, va_gc> *access_check;
13748 cp_token *next_token = NULL, *next_token_2 = NULL;
13749 bool is_identifier;
13751 /* If the next token corresponds to a template-id, there is no need
13752 to reparse it. */
13753 next_token = cp_lexer_peek_token (parser->lexer);
13754 if (next_token->type == CPP_TEMPLATE_ID)
13756 struct tree_check *check_value;
13758 /* Get the stored value. */
13759 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13760 /* Perform any access checks that were deferred. */
13761 access_check = check_value->checks;
13762 if (access_check)
13764 FOR_EACH_VEC_ELT (*access_check, i, chk)
13765 perform_or_defer_access_check (chk->binfo,
13766 chk->decl,
13767 chk->diag_decl,
13768 tf_warning_or_error);
13770 /* Return the stored value. */
13771 return check_value->value;
13774 /* Avoid performing name lookup if there is no possibility of
13775 finding a template-id. */
13776 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13777 || (next_token->type == CPP_NAME
13778 && !cp_parser_nth_token_starts_template_argument_list_p
13779 (parser, 2)))
13781 cp_parser_error (parser, "expected template-id");
13782 return error_mark_node;
13785 /* Remember where the template-id starts. */
13786 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13787 start_of_id = cp_lexer_token_position (parser->lexer, false);
13789 push_deferring_access_checks (dk_deferred);
13791 /* Parse the template-name. */
13792 is_identifier = false;
13793 templ = cp_parser_template_name (parser, template_keyword_p,
13794 check_dependency_p,
13795 is_declaration,
13796 tag_type,
13797 &is_identifier);
13798 if (templ == error_mark_node || is_identifier)
13800 pop_deferring_access_checks ();
13801 return templ;
13804 /* If we find the sequence `[:' after a template-name, it's probably
13805 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13806 parse correctly the argument list. */
13807 next_token = cp_lexer_peek_token (parser->lexer);
13808 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13809 if (next_token->type == CPP_OPEN_SQUARE
13810 && next_token->flags & DIGRAPH
13811 && next_token_2->type == CPP_COLON
13812 && !(next_token_2->flags & PREV_WHITE))
13814 cp_parser_parse_tentatively (parser);
13815 /* Change `:' into `::'. */
13816 next_token_2->type = CPP_SCOPE;
13817 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13818 CPP_LESS. */
13819 cp_lexer_consume_token (parser->lexer);
13821 /* Parse the arguments. */
13822 arguments = cp_parser_enclosed_template_argument_list (parser);
13823 if (!cp_parser_parse_definitely (parser))
13825 /* If we couldn't parse an argument list, then we revert our changes
13826 and return simply an error. Maybe this is not a template-id
13827 after all. */
13828 next_token_2->type = CPP_COLON;
13829 cp_parser_error (parser, "expected %<<%>");
13830 pop_deferring_access_checks ();
13831 return error_mark_node;
13833 /* Otherwise, emit an error about the invalid digraph, but continue
13834 parsing because we got our argument list. */
13835 if (permerror (next_token->location,
13836 "%<<::%> cannot begin a template-argument list"))
13838 static bool hint = false;
13839 inform (next_token->location,
13840 "%<<:%> is an alternate spelling for %<[%>."
13841 " Insert whitespace between %<<%> and %<::%>");
13842 if (!hint && !flag_permissive)
13844 inform (next_token->location, "(if you use %<-fpermissive%> "
13845 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13846 "accept your code)");
13847 hint = true;
13851 else
13853 /* Look for the `<' that starts the template-argument-list. */
13854 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13856 pop_deferring_access_checks ();
13857 return error_mark_node;
13859 /* Parse the arguments. */
13860 arguments = cp_parser_enclosed_template_argument_list (parser);
13863 /* Build a representation of the specialization. */
13864 if (identifier_p (templ))
13865 template_id = build_min_nt_loc (next_token->location,
13866 TEMPLATE_ID_EXPR,
13867 templ, arguments);
13868 else if (DECL_TYPE_TEMPLATE_P (templ)
13869 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13871 bool entering_scope;
13872 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13873 template (rather than some instantiation thereof) only if
13874 is not nested within some other construct. For example, in
13875 "template <typename T> void f(T) { A<T>::", A<T> is just an
13876 instantiation of A. */
13877 entering_scope = (template_parm_scope_p ()
13878 && cp_lexer_next_token_is (parser->lexer,
13879 CPP_SCOPE));
13880 template_id
13881 = finish_template_type (templ, arguments, entering_scope);
13883 else if (variable_template_p (templ))
13885 template_id = lookup_template_variable (templ, arguments);
13887 else
13889 /* If it's not a class-template or a template-template, it should be
13890 a function-template. */
13891 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13892 || TREE_CODE (templ) == OVERLOAD
13893 || BASELINK_P (templ)));
13895 template_id = lookup_template_function (templ, arguments);
13898 /* If parsing tentatively, replace the sequence of tokens that makes
13899 up the template-id with a CPP_TEMPLATE_ID token. That way,
13900 should we re-parse the token stream, we will not have to repeat
13901 the effort required to do the parse, nor will we issue duplicate
13902 error messages about problems during instantiation of the
13903 template. */
13904 if (start_of_id
13905 /* Don't do this if we had a parse error in a declarator; re-parsing
13906 might succeed if a name changes meaning (60361). */
13907 && !(cp_parser_error_occurred (parser)
13908 && cp_parser_parsing_tentatively (parser)
13909 && parser->in_declarator_p))
13911 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13913 /* Reset the contents of the START_OF_ID token. */
13914 token->type = CPP_TEMPLATE_ID;
13915 /* Retrieve any deferred checks. Do not pop this access checks yet
13916 so the memory will not be reclaimed during token replacing below. */
13917 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13918 token->u.tree_check_value->value = template_id;
13919 token->u.tree_check_value->checks = get_deferred_access_checks ();
13920 token->keyword = RID_MAX;
13922 /* Purge all subsequent tokens. */
13923 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13925 /* ??? Can we actually assume that, if template_id ==
13926 error_mark_node, we will have issued a diagnostic to the
13927 user, as opposed to simply marking the tentative parse as
13928 failed? */
13929 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13930 error_at (token->location, "parse error in template argument list");
13933 pop_to_parent_deferring_access_checks ();
13934 return template_id;
13937 /* Parse a template-name.
13939 template-name:
13940 identifier
13942 The standard should actually say:
13944 template-name:
13945 identifier
13946 operator-function-id
13948 A defect report has been filed about this issue.
13950 A conversion-function-id cannot be a template name because they cannot
13951 be part of a template-id. In fact, looking at this code:
13953 a.operator K<int>()
13955 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13956 It is impossible to call a templated conversion-function-id with an
13957 explicit argument list, since the only allowed template parameter is
13958 the type to which it is converting.
13960 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13961 `template' keyword, in a construction like:
13963 T::template f<3>()
13965 In that case `f' is taken to be a template-name, even though there
13966 is no way of knowing for sure.
13968 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13969 name refers to a set of overloaded functions, at least one of which
13970 is a template, or an IDENTIFIER_NODE with the name of the template,
13971 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13972 names are looked up inside uninstantiated templates. */
13974 static tree
13975 cp_parser_template_name (cp_parser* parser,
13976 bool template_keyword_p,
13977 bool check_dependency_p,
13978 bool is_declaration,
13979 enum tag_types tag_type,
13980 bool *is_identifier)
13982 tree identifier;
13983 tree decl;
13984 tree fns;
13985 cp_token *token = cp_lexer_peek_token (parser->lexer);
13987 /* If the next token is `operator', then we have either an
13988 operator-function-id or a conversion-function-id. */
13989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13991 /* We don't know whether we're looking at an
13992 operator-function-id or a conversion-function-id. */
13993 cp_parser_parse_tentatively (parser);
13994 /* Try an operator-function-id. */
13995 identifier = cp_parser_operator_function_id (parser);
13996 /* If that didn't work, try a conversion-function-id. */
13997 if (!cp_parser_parse_definitely (parser))
13999 cp_parser_error (parser, "expected template-name");
14000 return error_mark_node;
14003 /* Look for the identifier. */
14004 else
14005 identifier = cp_parser_identifier (parser);
14007 /* If we didn't find an identifier, we don't have a template-id. */
14008 if (identifier == error_mark_node)
14009 return error_mark_node;
14011 /* If the name immediately followed the `template' keyword, then it
14012 is a template-name. However, if the next token is not `<', then
14013 we do not treat it as a template-name, since it is not being used
14014 as part of a template-id. This enables us to handle constructs
14015 like:
14017 template <typename T> struct S { S(); };
14018 template <typename T> S<T>::S();
14020 correctly. We would treat `S' as a template -- if it were `S<T>'
14021 -- but we do not if there is no `<'. */
14023 if (processing_template_decl
14024 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14026 /* In a declaration, in a dependent context, we pretend that the
14027 "template" keyword was present in order to improve error
14028 recovery. For example, given:
14030 template <typename T> void f(T::X<int>);
14032 we want to treat "X<int>" as a template-id. */
14033 if (is_declaration
14034 && !template_keyword_p
14035 && parser->scope && TYPE_P (parser->scope)
14036 && check_dependency_p
14037 && dependent_scope_p (parser->scope)
14038 /* Do not do this for dtors (or ctors), since they never
14039 need the template keyword before their name. */
14040 && !constructor_name_p (identifier, parser->scope))
14042 cp_token_position start = 0;
14044 /* Explain what went wrong. */
14045 error_at (token->location, "non-template %qD used as template",
14046 identifier);
14047 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14048 parser->scope, identifier);
14049 /* If parsing tentatively, find the location of the "<" token. */
14050 if (cp_parser_simulate_error (parser))
14051 start = cp_lexer_token_position (parser->lexer, true);
14052 /* Parse the template arguments so that we can issue error
14053 messages about them. */
14054 cp_lexer_consume_token (parser->lexer);
14055 cp_parser_enclosed_template_argument_list (parser);
14056 /* Skip tokens until we find a good place from which to
14057 continue parsing. */
14058 cp_parser_skip_to_closing_parenthesis (parser,
14059 /*recovering=*/true,
14060 /*or_comma=*/true,
14061 /*consume_paren=*/false);
14062 /* If parsing tentatively, permanently remove the
14063 template argument list. That will prevent duplicate
14064 error messages from being issued about the missing
14065 "template" keyword. */
14066 if (start)
14067 cp_lexer_purge_tokens_after (parser->lexer, start);
14068 if (is_identifier)
14069 *is_identifier = true;
14070 return identifier;
14073 /* If the "template" keyword is present, then there is generally
14074 no point in doing name-lookup, so we just return IDENTIFIER.
14075 But, if the qualifying scope is non-dependent then we can
14076 (and must) do name-lookup normally. */
14077 if (template_keyword_p
14078 && (!parser->scope
14079 || (TYPE_P (parser->scope)
14080 && dependent_type_p (parser->scope))))
14081 return identifier;
14084 /* Look up the name. */
14085 decl = cp_parser_lookup_name (parser, identifier,
14086 tag_type,
14087 /*is_template=*/true,
14088 /*is_namespace=*/false,
14089 check_dependency_p,
14090 /*ambiguous_decls=*/NULL,
14091 token->location);
14093 decl = strip_using_decl (decl);
14095 /* If DECL is a template, then the name was a template-name. */
14096 if (TREE_CODE (decl) == TEMPLATE_DECL)
14098 if (TREE_DEPRECATED (decl)
14099 && deprecated_state != DEPRECATED_SUPPRESS)
14100 warn_deprecated_use (decl, NULL_TREE);
14102 else
14104 tree fn = NULL_TREE;
14106 /* The standard does not explicitly indicate whether a name that
14107 names a set of overloaded declarations, some of which are
14108 templates, is a template-name. However, such a name should
14109 be a template-name; otherwise, there is no way to form a
14110 template-id for the overloaded templates. */
14111 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14112 if (TREE_CODE (fns) == OVERLOAD)
14113 for (fn = fns; fn; fn = OVL_NEXT (fn))
14114 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14115 break;
14117 if (!fn)
14119 /* The name does not name a template. */
14120 cp_parser_error (parser, "expected template-name");
14121 return error_mark_node;
14125 /* If DECL is dependent, and refers to a function, then just return
14126 its name; we will look it up again during template instantiation. */
14127 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14129 tree scope = ovl_scope (decl);
14130 if (TYPE_P (scope) && dependent_type_p (scope))
14131 return identifier;
14134 return decl;
14137 /* Parse a template-argument-list.
14139 template-argument-list:
14140 template-argument ... [opt]
14141 template-argument-list , template-argument ... [opt]
14143 Returns a TREE_VEC containing the arguments. */
14145 static tree
14146 cp_parser_template_argument_list (cp_parser* parser)
14148 tree fixed_args[10];
14149 unsigned n_args = 0;
14150 unsigned alloced = 10;
14151 tree *arg_ary = fixed_args;
14152 tree vec;
14153 bool saved_in_template_argument_list_p;
14154 bool saved_ice_p;
14155 bool saved_non_ice_p;
14157 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14158 parser->in_template_argument_list_p = true;
14159 /* Even if the template-id appears in an integral
14160 constant-expression, the contents of the argument list do
14161 not. */
14162 saved_ice_p = parser->integral_constant_expression_p;
14163 parser->integral_constant_expression_p = false;
14164 saved_non_ice_p = parser->non_integral_constant_expression_p;
14165 parser->non_integral_constant_expression_p = false;
14167 /* Parse the arguments. */
14170 tree argument;
14172 if (n_args)
14173 /* Consume the comma. */
14174 cp_lexer_consume_token (parser->lexer);
14176 /* Parse the template-argument. */
14177 argument = cp_parser_template_argument (parser);
14179 /* If the next token is an ellipsis, we're expanding a template
14180 argument pack. */
14181 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14183 if (argument == error_mark_node)
14185 cp_token *token = cp_lexer_peek_token (parser->lexer);
14186 error_at (token->location,
14187 "expected parameter pack before %<...%>");
14189 /* Consume the `...' token. */
14190 cp_lexer_consume_token (parser->lexer);
14192 /* Make the argument into a TYPE_PACK_EXPANSION or
14193 EXPR_PACK_EXPANSION. */
14194 argument = make_pack_expansion (argument);
14197 if (n_args == alloced)
14199 alloced *= 2;
14201 if (arg_ary == fixed_args)
14203 arg_ary = XNEWVEC (tree, alloced);
14204 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14206 else
14207 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14209 arg_ary[n_args++] = argument;
14211 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14213 vec = make_tree_vec (n_args);
14215 while (n_args--)
14216 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14218 if (arg_ary != fixed_args)
14219 free (arg_ary);
14220 parser->non_integral_constant_expression_p = saved_non_ice_p;
14221 parser->integral_constant_expression_p = saved_ice_p;
14222 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14223 #ifdef ENABLE_CHECKING
14224 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14225 #endif
14226 return vec;
14229 /* Parse a template-argument.
14231 template-argument:
14232 assignment-expression
14233 type-id
14234 id-expression
14236 The representation is that of an assignment-expression, type-id, or
14237 id-expression -- except that the qualified id-expression is
14238 evaluated, so that the value returned is either a DECL or an
14239 OVERLOAD.
14241 Although the standard says "assignment-expression", it forbids
14242 throw-expressions or assignments in the template argument.
14243 Therefore, we use "conditional-expression" instead. */
14245 static tree
14246 cp_parser_template_argument (cp_parser* parser)
14248 tree argument;
14249 bool template_p;
14250 bool address_p;
14251 bool maybe_type_id = false;
14252 cp_token *token = NULL, *argument_start_token = NULL;
14253 location_t loc = 0;
14254 cp_id_kind idk;
14256 /* There's really no way to know what we're looking at, so we just
14257 try each alternative in order.
14259 [temp.arg]
14261 In a template-argument, an ambiguity between a type-id and an
14262 expression is resolved to a type-id, regardless of the form of
14263 the corresponding template-parameter.
14265 Therefore, we try a type-id first. */
14266 cp_parser_parse_tentatively (parser);
14267 argument = cp_parser_template_type_arg (parser);
14268 /* If there was no error parsing the type-id but the next token is a
14269 '>>', our behavior depends on which dialect of C++ we're
14270 parsing. In C++98, we probably found a typo for '> >'. But there
14271 are type-id which are also valid expressions. For instance:
14273 struct X { int operator >> (int); };
14274 template <int V> struct Foo {};
14275 Foo<X () >> 5> r;
14277 Here 'X()' is a valid type-id of a function type, but the user just
14278 wanted to write the expression "X() >> 5". Thus, we remember that we
14279 found a valid type-id, but we still try to parse the argument as an
14280 expression to see what happens.
14282 In C++0x, the '>>' will be considered two separate '>'
14283 tokens. */
14284 if (!cp_parser_error_occurred (parser)
14285 && cxx_dialect == cxx98
14286 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14288 maybe_type_id = true;
14289 cp_parser_abort_tentative_parse (parser);
14291 else
14293 /* If the next token isn't a `,' or a `>', then this argument wasn't
14294 really finished. This means that the argument is not a valid
14295 type-id. */
14296 if (!cp_parser_next_token_ends_template_argument_p (parser))
14297 cp_parser_error (parser, "expected template-argument");
14298 /* If that worked, we're done. */
14299 if (cp_parser_parse_definitely (parser))
14300 return argument;
14302 /* We're still not sure what the argument will be. */
14303 cp_parser_parse_tentatively (parser);
14304 /* Try a template. */
14305 argument_start_token = cp_lexer_peek_token (parser->lexer);
14306 argument = cp_parser_id_expression (parser,
14307 /*template_keyword_p=*/false,
14308 /*check_dependency_p=*/true,
14309 &template_p,
14310 /*declarator_p=*/false,
14311 /*optional_p=*/false);
14312 /* If the next token isn't a `,' or a `>', then this argument wasn't
14313 really finished. */
14314 if (!cp_parser_next_token_ends_template_argument_p (parser))
14315 cp_parser_error (parser, "expected template-argument");
14316 if (!cp_parser_error_occurred (parser))
14318 /* Figure out what is being referred to. If the id-expression
14319 was for a class template specialization, then we will have a
14320 TYPE_DECL at this point. There is no need to do name lookup
14321 at this point in that case. */
14322 if (TREE_CODE (argument) != TYPE_DECL)
14323 argument = cp_parser_lookup_name (parser, argument,
14324 none_type,
14325 /*is_template=*/template_p,
14326 /*is_namespace=*/false,
14327 /*check_dependency=*/true,
14328 /*ambiguous_decls=*/NULL,
14329 argument_start_token->location);
14330 if (TREE_CODE (argument) != TEMPLATE_DECL
14331 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14332 cp_parser_error (parser, "expected template-name");
14334 if (cp_parser_parse_definitely (parser))
14336 if (TREE_DEPRECATED (argument))
14337 warn_deprecated_use (argument, NULL_TREE);
14338 return argument;
14340 /* It must be a non-type argument. There permitted cases are given
14341 in [temp.arg.nontype]:
14343 -- an integral constant-expression of integral or enumeration
14344 type; or
14346 -- the name of a non-type template-parameter; or
14348 -- the name of an object or function with external linkage...
14350 -- the address of an object or function with external linkage...
14352 -- a pointer to member... */
14353 /* Look for a non-type template parameter. */
14354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14356 cp_parser_parse_tentatively (parser);
14357 argument = cp_parser_primary_expression (parser,
14358 /*address_p=*/false,
14359 /*cast_p=*/false,
14360 /*template_arg_p=*/true,
14361 &idk);
14362 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14363 || !cp_parser_next_token_ends_template_argument_p (parser))
14364 cp_parser_simulate_error (parser);
14365 if (cp_parser_parse_definitely (parser))
14366 return argument;
14369 /* If the next token is "&", the argument must be the address of an
14370 object or function with external linkage. */
14371 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14372 if (address_p)
14374 loc = cp_lexer_peek_token (parser->lexer)->location;
14375 cp_lexer_consume_token (parser->lexer);
14377 /* See if we might have an id-expression. */
14378 token = cp_lexer_peek_token (parser->lexer);
14379 if (token->type == CPP_NAME
14380 || token->keyword == RID_OPERATOR
14381 || token->type == CPP_SCOPE
14382 || token->type == CPP_TEMPLATE_ID
14383 || token->type == CPP_NESTED_NAME_SPECIFIER)
14385 cp_parser_parse_tentatively (parser);
14386 argument = cp_parser_primary_expression (parser,
14387 address_p,
14388 /*cast_p=*/false,
14389 /*template_arg_p=*/true,
14390 &idk);
14391 if (cp_parser_error_occurred (parser)
14392 || !cp_parser_next_token_ends_template_argument_p (parser))
14393 cp_parser_abort_tentative_parse (parser);
14394 else
14396 tree probe;
14398 if (INDIRECT_REF_P (argument))
14400 /* Strip the dereference temporarily. */
14401 gcc_assert (REFERENCE_REF_P (argument));
14402 argument = TREE_OPERAND (argument, 0);
14405 /* If we're in a template, we represent a qualified-id referring
14406 to a static data member as a SCOPE_REF even if the scope isn't
14407 dependent so that we can check access control later. */
14408 probe = argument;
14409 if (TREE_CODE (probe) == SCOPE_REF)
14410 probe = TREE_OPERAND (probe, 1);
14411 if (VAR_P (probe))
14413 /* A variable without external linkage might still be a
14414 valid constant-expression, so no error is issued here
14415 if the external-linkage check fails. */
14416 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14417 cp_parser_simulate_error (parser);
14419 else if (is_overloaded_fn (argument))
14420 /* All overloaded functions are allowed; if the external
14421 linkage test does not pass, an error will be issued
14422 later. */
14424 else if (address_p
14425 && (TREE_CODE (argument) == OFFSET_REF
14426 || TREE_CODE (argument) == SCOPE_REF))
14427 /* A pointer-to-member. */
14429 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14431 else
14432 cp_parser_simulate_error (parser);
14434 if (cp_parser_parse_definitely (parser))
14436 if (address_p)
14437 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14438 tf_warning_or_error);
14439 else
14440 argument = convert_from_reference (argument);
14441 return argument;
14445 /* If the argument started with "&", there are no other valid
14446 alternatives at this point. */
14447 if (address_p)
14449 cp_parser_error (parser, "invalid non-type template argument");
14450 return error_mark_node;
14453 /* If the argument wasn't successfully parsed as a type-id followed
14454 by '>>', the argument can only be a constant expression now.
14455 Otherwise, we try parsing the constant-expression tentatively,
14456 because the argument could really be a type-id. */
14457 if (maybe_type_id)
14458 cp_parser_parse_tentatively (parser);
14459 argument = cp_parser_constant_expression (parser);
14461 if (!maybe_type_id)
14462 return argument;
14463 if (!cp_parser_next_token_ends_template_argument_p (parser))
14464 cp_parser_error (parser, "expected template-argument");
14465 if (cp_parser_parse_definitely (parser))
14466 return argument;
14467 /* We did our best to parse the argument as a non type-id, but that
14468 was the only alternative that matched (albeit with a '>' after
14469 it). We can assume it's just a typo from the user, and a
14470 diagnostic will then be issued. */
14471 return cp_parser_template_type_arg (parser);
14474 /* Parse an explicit-instantiation.
14476 explicit-instantiation:
14477 template declaration
14479 Although the standard says `declaration', what it really means is:
14481 explicit-instantiation:
14482 template decl-specifier-seq [opt] declarator [opt] ;
14484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14485 supposed to be allowed. A defect report has been filed about this
14486 issue.
14488 GNU Extension:
14490 explicit-instantiation:
14491 storage-class-specifier template
14492 decl-specifier-seq [opt] declarator [opt] ;
14493 function-specifier template
14494 decl-specifier-seq [opt] declarator [opt] ; */
14496 static void
14497 cp_parser_explicit_instantiation (cp_parser* parser)
14499 int declares_class_or_enum;
14500 cp_decl_specifier_seq decl_specifiers;
14501 tree extension_specifier = NULL_TREE;
14503 timevar_push (TV_TEMPLATE_INST);
14505 /* Look for an (optional) storage-class-specifier or
14506 function-specifier. */
14507 if (cp_parser_allow_gnu_extensions_p (parser))
14509 extension_specifier
14510 = cp_parser_storage_class_specifier_opt (parser);
14511 if (!extension_specifier)
14512 extension_specifier
14513 = cp_parser_function_specifier_opt (parser,
14514 /*decl_specs=*/NULL);
14517 /* Look for the `template' keyword. */
14518 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14519 /* Let the front end know that we are processing an explicit
14520 instantiation. */
14521 begin_explicit_instantiation ();
14522 /* [temp.explicit] says that we are supposed to ignore access
14523 control while processing explicit instantiation directives. */
14524 push_deferring_access_checks (dk_no_check);
14525 /* Parse a decl-specifier-seq. */
14526 cp_parser_decl_specifier_seq (parser,
14527 CP_PARSER_FLAGS_OPTIONAL,
14528 &decl_specifiers,
14529 &declares_class_or_enum);
14530 /* If there was exactly one decl-specifier, and it declared a class,
14531 and there's no declarator, then we have an explicit type
14532 instantiation. */
14533 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14535 tree type;
14537 type = check_tag_decl (&decl_specifiers,
14538 /*explicit_type_instantiation_p=*/true);
14539 /* Turn access control back on for names used during
14540 template instantiation. */
14541 pop_deferring_access_checks ();
14542 if (type)
14543 do_type_instantiation (type, extension_specifier,
14544 /*complain=*/tf_error);
14546 else
14548 cp_declarator *declarator;
14549 tree decl;
14551 /* Parse the declarator. */
14552 declarator
14553 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14554 /*ctor_dtor_or_conv_p=*/NULL,
14555 /*parenthesized_p=*/NULL,
14556 /*member_p=*/false,
14557 /*friend_p=*/false);
14558 if (declares_class_or_enum & 2)
14559 cp_parser_check_for_definition_in_return_type (declarator,
14560 decl_specifiers.type,
14561 decl_specifiers.locations[ds_type_spec]);
14562 if (declarator != cp_error_declarator)
14564 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14565 permerror (decl_specifiers.locations[ds_inline],
14566 "explicit instantiation shall not use"
14567 " %<inline%> specifier");
14568 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14569 permerror (decl_specifiers.locations[ds_constexpr],
14570 "explicit instantiation shall not use"
14571 " %<constexpr%> specifier");
14573 decl = grokdeclarator (declarator, &decl_specifiers,
14574 NORMAL, 0, &decl_specifiers.attributes);
14575 /* Turn access control back on for names used during
14576 template instantiation. */
14577 pop_deferring_access_checks ();
14578 /* Do the explicit instantiation. */
14579 do_decl_instantiation (decl, extension_specifier);
14581 else
14583 pop_deferring_access_checks ();
14584 /* Skip the body of the explicit instantiation. */
14585 cp_parser_skip_to_end_of_statement (parser);
14588 /* We're done with the instantiation. */
14589 end_explicit_instantiation ();
14591 cp_parser_consume_semicolon_at_end_of_statement (parser);
14593 timevar_pop (TV_TEMPLATE_INST);
14596 /* Parse an explicit-specialization.
14598 explicit-specialization:
14599 template < > declaration
14601 Although the standard says `declaration', what it really means is:
14603 explicit-specialization:
14604 template <> decl-specifier [opt] init-declarator [opt] ;
14605 template <> function-definition
14606 template <> explicit-specialization
14607 template <> template-declaration */
14609 static void
14610 cp_parser_explicit_specialization (cp_parser* parser)
14612 bool need_lang_pop;
14613 cp_token *token = cp_lexer_peek_token (parser->lexer);
14615 /* Look for the `template' keyword. */
14616 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14617 /* Look for the `<'. */
14618 cp_parser_require (parser, CPP_LESS, RT_LESS);
14619 /* Look for the `>'. */
14620 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14621 /* We have processed another parameter list. */
14622 ++parser->num_template_parameter_lists;
14623 /* [temp]
14625 A template ... explicit specialization ... shall not have C
14626 linkage. */
14627 if (current_lang_name == lang_name_c)
14629 error_at (token->location, "template specialization with C linkage");
14630 /* Give it C++ linkage to avoid confusing other parts of the
14631 front end. */
14632 push_lang_context (lang_name_cplusplus);
14633 need_lang_pop = true;
14635 else
14636 need_lang_pop = false;
14637 /* Let the front end know that we are beginning a specialization. */
14638 if (!begin_specialization ())
14640 end_specialization ();
14641 return;
14644 /* If the next keyword is `template', we need to figure out whether
14645 or not we're looking a template-declaration. */
14646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14648 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14650 cp_parser_template_declaration_after_export (parser,
14651 /*member_p=*/false);
14652 else
14653 cp_parser_explicit_specialization (parser);
14655 else
14656 /* Parse the dependent declaration. */
14657 cp_parser_single_declaration (parser,
14658 /*checks=*/NULL,
14659 /*member_p=*/false,
14660 /*explicit_specialization_p=*/true,
14661 /*friend_p=*/NULL);
14662 /* We're done with the specialization. */
14663 end_specialization ();
14664 /* For the erroneous case of a template with C linkage, we pushed an
14665 implicit C++ linkage scope; exit that scope now. */
14666 if (need_lang_pop)
14667 pop_lang_context ();
14668 /* We're done with this parameter list. */
14669 --parser->num_template_parameter_lists;
14672 /* Parse a type-specifier.
14674 type-specifier:
14675 simple-type-specifier
14676 class-specifier
14677 enum-specifier
14678 elaborated-type-specifier
14679 cv-qualifier
14681 GNU Extension:
14683 type-specifier:
14684 __complex__
14686 Returns a representation of the type-specifier. For a
14687 class-specifier, enum-specifier, or elaborated-type-specifier, a
14688 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14690 The parser flags FLAGS is used to control type-specifier parsing.
14692 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14693 in a decl-specifier-seq.
14695 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14696 class-specifier, enum-specifier, or elaborated-type-specifier, then
14697 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14698 if a type is declared; 2 if it is defined. Otherwise, it is set to
14699 zero.
14701 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14702 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14703 is set to FALSE. */
14705 static tree
14706 cp_parser_type_specifier (cp_parser* parser,
14707 cp_parser_flags flags,
14708 cp_decl_specifier_seq *decl_specs,
14709 bool is_declaration,
14710 int* declares_class_or_enum,
14711 bool* is_cv_qualifier)
14713 tree type_spec = NULL_TREE;
14714 cp_token *token;
14715 enum rid keyword;
14716 cp_decl_spec ds = ds_last;
14718 /* Assume this type-specifier does not declare a new type. */
14719 if (declares_class_or_enum)
14720 *declares_class_or_enum = 0;
14721 /* And that it does not specify a cv-qualifier. */
14722 if (is_cv_qualifier)
14723 *is_cv_qualifier = false;
14724 /* Peek at the next token. */
14725 token = cp_lexer_peek_token (parser->lexer);
14727 /* If we're looking at a keyword, we can use that to guide the
14728 production we choose. */
14729 keyword = token->keyword;
14730 switch (keyword)
14732 case RID_ENUM:
14733 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14734 goto elaborated_type_specifier;
14736 /* Look for the enum-specifier. */
14737 type_spec = cp_parser_enum_specifier (parser);
14738 /* If that worked, we're done. */
14739 if (type_spec)
14741 if (declares_class_or_enum)
14742 *declares_class_or_enum = 2;
14743 if (decl_specs)
14744 cp_parser_set_decl_spec_type (decl_specs,
14745 type_spec,
14746 token,
14747 /*type_definition_p=*/true);
14748 return type_spec;
14750 else
14751 goto elaborated_type_specifier;
14753 /* Any of these indicate either a class-specifier, or an
14754 elaborated-type-specifier. */
14755 case RID_CLASS:
14756 case RID_STRUCT:
14757 case RID_UNION:
14758 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14759 goto elaborated_type_specifier;
14761 /* Parse tentatively so that we can back up if we don't find a
14762 class-specifier. */
14763 cp_parser_parse_tentatively (parser);
14764 /* Look for the class-specifier. */
14765 type_spec = cp_parser_class_specifier (parser);
14766 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14767 /* If that worked, we're done. */
14768 if (cp_parser_parse_definitely (parser))
14770 if (declares_class_or_enum)
14771 *declares_class_or_enum = 2;
14772 if (decl_specs)
14773 cp_parser_set_decl_spec_type (decl_specs,
14774 type_spec,
14775 token,
14776 /*type_definition_p=*/true);
14777 return type_spec;
14780 /* Fall through. */
14781 elaborated_type_specifier:
14782 /* We're declaring (not defining) a class or enum. */
14783 if (declares_class_or_enum)
14784 *declares_class_or_enum = 1;
14786 /* Fall through. */
14787 case RID_TYPENAME:
14788 /* Look for an elaborated-type-specifier. */
14789 type_spec
14790 = (cp_parser_elaborated_type_specifier
14791 (parser,
14792 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14793 is_declaration));
14794 if (decl_specs)
14795 cp_parser_set_decl_spec_type (decl_specs,
14796 type_spec,
14797 token,
14798 /*type_definition_p=*/false);
14799 return type_spec;
14801 case RID_CONST:
14802 ds = ds_const;
14803 if (is_cv_qualifier)
14804 *is_cv_qualifier = true;
14805 break;
14807 case RID_VOLATILE:
14808 ds = ds_volatile;
14809 if (is_cv_qualifier)
14810 *is_cv_qualifier = true;
14811 break;
14813 case RID_RESTRICT:
14814 ds = ds_restrict;
14815 if (is_cv_qualifier)
14816 *is_cv_qualifier = true;
14817 break;
14819 case RID_COMPLEX:
14820 /* The `__complex__' keyword is a GNU extension. */
14821 ds = ds_complex;
14822 break;
14824 default:
14825 break;
14828 /* Handle simple keywords. */
14829 if (ds != ds_last)
14831 if (decl_specs)
14833 set_and_check_decl_spec_loc (decl_specs, ds, token);
14834 decl_specs->any_specifiers_p = true;
14836 return cp_lexer_consume_token (parser->lexer)->u.value;
14839 /* If we do not already have a type-specifier, assume we are looking
14840 at a simple-type-specifier. */
14841 type_spec = cp_parser_simple_type_specifier (parser,
14842 decl_specs,
14843 flags);
14845 /* If we didn't find a type-specifier, and a type-specifier was not
14846 optional in this context, issue an error message. */
14847 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14849 cp_parser_error (parser, "expected type specifier");
14850 return error_mark_node;
14853 return type_spec;
14856 /* Parse a simple-type-specifier.
14858 simple-type-specifier:
14859 :: [opt] nested-name-specifier [opt] type-name
14860 :: [opt] nested-name-specifier template template-id
14861 char
14862 wchar_t
14863 bool
14864 short
14866 long
14867 signed
14868 unsigned
14869 float
14870 double
14871 void
14873 C++0x Extension:
14875 simple-type-specifier:
14876 auto
14877 decltype ( expression )
14878 char16_t
14879 char32_t
14880 __underlying_type ( type-id )
14882 GNU Extension:
14884 simple-type-specifier:
14885 __int128
14886 __typeof__ unary-expression
14887 __typeof__ ( type-id )
14888 __typeof__ ( type-id ) { initializer-list , [opt] }
14890 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14891 appropriately updated. */
14893 static tree
14894 cp_parser_simple_type_specifier (cp_parser* parser,
14895 cp_decl_specifier_seq *decl_specs,
14896 cp_parser_flags flags)
14898 tree type = NULL_TREE;
14899 cp_token *token;
14900 int idx;
14902 /* Peek at the next token. */
14903 token = cp_lexer_peek_token (parser->lexer);
14905 /* If we're looking at a keyword, things are easy. */
14906 switch (token->keyword)
14908 case RID_CHAR:
14909 if (decl_specs)
14910 decl_specs->explicit_char_p = true;
14911 type = char_type_node;
14912 break;
14913 case RID_CHAR16:
14914 type = char16_type_node;
14915 break;
14916 case RID_CHAR32:
14917 type = char32_type_node;
14918 break;
14919 case RID_WCHAR:
14920 type = wchar_type_node;
14921 break;
14922 case RID_BOOL:
14923 type = boolean_type_node;
14924 break;
14925 case RID_SHORT:
14926 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14927 type = short_integer_type_node;
14928 break;
14929 case RID_INT:
14930 if (decl_specs)
14931 decl_specs->explicit_int_p = true;
14932 type = integer_type_node;
14933 break;
14934 case RID_INT_N_0:
14935 case RID_INT_N_1:
14936 case RID_INT_N_2:
14937 case RID_INT_N_3:
14938 idx = token->keyword - RID_INT_N_0;
14939 if (! int_n_enabled_p [idx])
14940 break;
14941 if (decl_specs)
14943 decl_specs->explicit_intN_p = true;
14944 decl_specs->int_n_idx = idx;
14946 type = int_n_trees [idx].signed_type;
14947 break;
14948 case RID_LONG:
14949 if (decl_specs)
14950 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14951 type = long_integer_type_node;
14952 break;
14953 case RID_SIGNED:
14954 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14955 type = integer_type_node;
14956 break;
14957 case RID_UNSIGNED:
14958 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14959 type = unsigned_type_node;
14960 break;
14961 case RID_FLOAT:
14962 type = float_type_node;
14963 break;
14964 case RID_DOUBLE:
14965 type = double_type_node;
14966 break;
14967 case RID_VOID:
14968 type = void_type_node;
14969 break;
14971 case RID_AUTO:
14972 maybe_warn_cpp0x (CPP0X_AUTO);
14973 if (parser->auto_is_implicit_function_template_parm_p)
14975 /* The 'auto' might be the placeholder return type for a function decl
14976 with trailing return type. */
14977 bool have_trailing_return_fn_decl = false;
14978 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14979 == CPP_OPEN_PAREN)
14981 cp_parser_parse_tentatively (parser);
14982 cp_lexer_consume_token (parser->lexer);
14983 cp_lexer_consume_token (parser->lexer);
14984 if (cp_parser_skip_to_closing_parenthesis (parser,
14985 /*recovering*/false,
14986 /*or_comma*/false,
14987 /*consume_paren*/true))
14988 have_trailing_return_fn_decl
14989 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14990 cp_parser_abort_tentative_parse (parser);
14993 if (have_trailing_return_fn_decl)
14995 type = make_auto ();
14996 break;
14999 if (cxx_dialect >= cxx14)
15000 type = synthesize_implicit_template_parm (parser);
15001 else
15002 type = error_mark_node;
15004 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15006 if (cxx_dialect < cxx14)
15007 error_at (token->location,
15008 "use of %<auto%> in lambda parameter declaration "
15009 "only available with "
15010 "-std=c++14 or -std=gnu++14");
15012 else if (cxx_dialect < cxx14)
15013 error_at (token->location,
15014 "use of %<auto%> in parameter declaration "
15015 "only available with "
15016 "-std=c++14 or -std=gnu++14");
15017 else
15018 pedwarn (token->location, OPT_Wpedantic,
15019 "ISO C++ forbids use of %<auto%> in parameter "
15020 "declaration");
15022 else
15023 type = make_auto ();
15024 break;
15026 case RID_DECLTYPE:
15027 /* Since DR 743, decltype can either be a simple-type-specifier by
15028 itself or begin a nested-name-specifier. Parsing it will replace
15029 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15030 handling below decide what to do. */
15031 cp_parser_decltype (parser);
15032 cp_lexer_set_token_position (parser->lexer, token);
15033 break;
15035 case RID_TYPEOF:
15036 /* Consume the `typeof' token. */
15037 cp_lexer_consume_token (parser->lexer);
15038 /* Parse the operand to `typeof'. */
15039 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15040 /* If it is not already a TYPE, take its type. */
15041 if (!TYPE_P (type))
15042 type = finish_typeof (type);
15044 if (decl_specs)
15045 cp_parser_set_decl_spec_type (decl_specs, type,
15046 token,
15047 /*type_definition_p=*/false);
15049 return type;
15051 case RID_UNDERLYING_TYPE:
15052 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15053 if (decl_specs)
15054 cp_parser_set_decl_spec_type (decl_specs, type,
15055 token,
15056 /*type_definition_p=*/false);
15058 return type;
15060 case RID_BASES:
15061 case RID_DIRECT_BASES:
15062 type = cp_parser_trait_expr (parser, token->keyword);
15063 if (decl_specs)
15064 cp_parser_set_decl_spec_type (decl_specs, type,
15065 token,
15066 /*type_definition_p=*/false);
15067 return type;
15068 default:
15069 break;
15072 /* If token is an already-parsed decltype not followed by ::,
15073 it's a simple-type-specifier. */
15074 if (token->type == CPP_DECLTYPE
15075 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15077 type = token->u.value;
15078 if (decl_specs)
15080 cp_parser_set_decl_spec_type (decl_specs, type,
15081 token,
15082 /*type_definition_p=*/false);
15083 /* Remember that we are handling a decltype in order to
15084 implement the resolution of DR 1510 when the argument
15085 isn't instantiation dependent. */
15086 decl_specs->decltype_p = true;
15088 cp_lexer_consume_token (parser->lexer);
15089 return type;
15092 /* If the type-specifier was for a built-in type, we're done. */
15093 if (type)
15095 /* Record the type. */
15096 if (decl_specs
15097 && (token->keyword != RID_SIGNED
15098 && token->keyword != RID_UNSIGNED
15099 && token->keyword != RID_SHORT
15100 && token->keyword != RID_LONG))
15101 cp_parser_set_decl_spec_type (decl_specs,
15102 type,
15103 token,
15104 /*type_definition_p=*/false);
15105 if (decl_specs)
15106 decl_specs->any_specifiers_p = true;
15108 /* Consume the token. */
15109 cp_lexer_consume_token (parser->lexer);
15111 if (type == error_mark_node)
15112 return error_mark_node;
15114 /* There is no valid C++ program where a non-template type is
15115 followed by a "<". That usually indicates that the user thought
15116 that the type was a template. */
15117 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15118 token->location);
15120 return TYPE_NAME (type);
15123 /* The type-specifier must be a user-defined type. */
15124 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15126 bool qualified_p;
15127 bool global_p;
15129 /* Don't gobble tokens or issue error messages if this is an
15130 optional type-specifier. */
15131 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15132 cp_parser_parse_tentatively (parser);
15134 /* Look for the optional `::' operator. */
15135 global_p
15136 = (cp_parser_global_scope_opt (parser,
15137 /*current_scope_valid_p=*/false)
15138 != NULL_TREE);
15139 /* Look for the nested-name specifier. */
15140 qualified_p
15141 = (cp_parser_nested_name_specifier_opt (parser,
15142 /*typename_keyword_p=*/false,
15143 /*check_dependency_p=*/true,
15144 /*type_p=*/false,
15145 /*is_declaration=*/false)
15146 != NULL_TREE);
15147 token = cp_lexer_peek_token (parser->lexer);
15148 /* If we have seen a nested-name-specifier, and the next token
15149 is `template', then we are using the template-id production. */
15150 if (parser->scope
15151 && cp_parser_optional_template_keyword (parser))
15153 /* Look for the template-id. */
15154 type = cp_parser_template_id (parser,
15155 /*template_keyword_p=*/true,
15156 /*check_dependency_p=*/true,
15157 none_type,
15158 /*is_declaration=*/false);
15159 /* If the template-id did not name a type, we are out of
15160 luck. */
15161 if (TREE_CODE (type) != TYPE_DECL)
15163 cp_parser_error (parser, "expected template-id for type");
15164 type = NULL_TREE;
15167 /* Otherwise, look for a type-name. */
15168 else
15169 type = cp_parser_type_name (parser);
15170 /* Keep track of all name-lookups performed in class scopes. */
15171 if (type
15172 && !global_p
15173 && !qualified_p
15174 && TREE_CODE (type) == TYPE_DECL
15175 && identifier_p (DECL_NAME (type)))
15176 maybe_note_name_used_in_class (DECL_NAME (type), type);
15177 /* If it didn't work out, we don't have a TYPE. */
15178 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15179 && !cp_parser_parse_definitely (parser))
15180 type = NULL_TREE;
15181 if (type && decl_specs)
15182 cp_parser_set_decl_spec_type (decl_specs, type,
15183 token,
15184 /*type_definition_p=*/false);
15187 /* If we didn't get a type-name, issue an error message. */
15188 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15190 cp_parser_error (parser, "expected type-name");
15191 return error_mark_node;
15194 if (type && type != error_mark_node)
15196 /* See if TYPE is an Objective-C type, and if so, parse and
15197 accept any protocol references following it. Do this before
15198 the cp_parser_check_for_invalid_template_id() call, because
15199 Objective-C types can be followed by '<...>' which would
15200 enclose protocol names rather than template arguments, and so
15201 everything is fine. */
15202 if (c_dialect_objc () && !parser->scope
15203 && (objc_is_id (type) || objc_is_class_name (type)))
15205 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15206 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15208 /* Clobber the "unqualified" type previously entered into
15209 DECL_SPECS with the new, improved protocol-qualified version. */
15210 if (decl_specs)
15211 decl_specs->type = qual_type;
15213 return qual_type;
15216 /* There is no valid C++ program where a non-template type is
15217 followed by a "<". That usually indicates that the user
15218 thought that the type was a template. */
15219 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15220 none_type,
15221 token->location);
15224 return type;
15227 /* Parse a type-name.
15229 type-name:
15230 class-name
15231 enum-name
15232 typedef-name
15233 simple-template-id [in c++0x]
15235 enum-name:
15236 identifier
15238 typedef-name:
15239 identifier
15241 Returns a TYPE_DECL for the type. */
15243 static tree
15244 cp_parser_type_name (cp_parser* parser)
15246 tree type_decl;
15248 /* We can't know yet whether it is a class-name or not. */
15249 cp_parser_parse_tentatively (parser);
15250 /* Try a class-name. */
15251 type_decl = cp_parser_class_name (parser,
15252 /*typename_keyword_p=*/false,
15253 /*template_keyword_p=*/false,
15254 none_type,
15255 /*check_dependency_p=*/true,
15256 /*class_head_p=*/false,
15257 /*is_declaration=*/false);
15258 /* If it's not a class-name, keep looking. */
15259 if (!cp_parser_parse_definitely (parser))
15261 if (cxx_dialect < cxx11)
15262 /* It must be a typedef-name or an enum-name. */
15263 return cp_parser_nonclass_name (parser);
15265 cp_parser_parse_tentatively (parser);
15266 /* It is either a simple-template-id representing an
15267 instantiation of an alias template... */
15268 type_decl = cp_parser_template_id (parser,
15269 /*template_keyword_p=*/false,
15270 /*check_dependency_p=*/true,
15271 none_type,
15272 /*is_declaration=*/false);
15273 /* Note that this must be an instantiation of an alias template
15274 because [temp.names]/6 says:
15276 A template-id that names an alias template specialization
15277 is a type-name.
15279 Whereas [temp.names]/7 says:
15281 A simple-template-id that names a class template
15282 specialization is a class-name. */
15283 if (type_decl != NULL_TREE
15284 && TREE_CODE (type_decl) == TYPE_DECL
15285 && TYPE_DECL_ALIAS_P (type_decl))
15286 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15287 else
15288 cp_parser_simulate_error (parser);
15290 if (!cp_parser_parse_definitely (parser))
15291 /* ... Or a typedef-name or an enum-name. */
15292 return cp_parser_nonclass_name (parser);
15295 return type_decl;
15298 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15300 enum-name:
15301 identifier
15303 typedef-name:
15304 identifier
15306 Returns a TYPE_DECL for the type. */
15308 static tree
15309 cp_parser_nonclass_name (cp_parser* parser)
15311 tree type_decl;
15312 tree identifier;
15314 cp_token *token = cp_lexer_peek_token (parser->lexer);
15315 identifier = cp_parser_identifier (parser);
15316 if (identifier == error_mark_node)
15317 return error_mark_node;
15319 /* Look up the type-name. */
15320 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15322 type_decl = strip_using_decl (type_decl);
15324 if (TREE_CODE (type_decl) != TYPE_DECL
15325 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15327 /* See if this is an Objective-C type. */
15328 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15329 tree type = objc_get_protocol_qualified_type (identifier, protos);
15330 if (type)
15331 type_decl = TYPE_NAME (type);
15334 /* Issue an error if we did not find a type-name. */
15335 if (TREE_CODE (type_decl) != TYPE_DECL
15336 /* In Objective-C, we have the complication that class names are
15337 normally type names and start declarations (eg, the
15338 "NSObject" in "NSObject *object;"), but can be used in an
15339 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15340 is an expression. So, a classname followed by a dot is not a
15341 valid type-name. */
15342 || (objc_is_class_name (TREE_TYPE (type_decl))
15343 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15345 if (!cp_parser_simulate_error (parser))
15346 cp_parser_name_lookup_error (parser, identifier, type_decl,
15347 NLE_TYPE, token->location);
15348 return error_mark_node;
15350 /* Remember that the name was used in the definition of the
15351 current class so that we can check later to see if the
15352 meaning would have been different after the class was
15353 entirely defined. */
15354 else if (type_decl != error_mark_node
15355 && !parser->scope)
15356 maybe_note_name_used_in_class (identifier, type_decl);
15358 return type_decl;
15361 /* Parse an elaborated-type-specifier. Note that the grammar given
15362 here incorporates the resolution to DR68.
15364 elaborated-type-specifier:
15365 class-key :: [opt] nested-name-specifier [opt] identifier
15366 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15367 enum-key :: [opt] nested-name-specifier [opt] identifier
15368 typename :: [opt] nested-name-specifier identifier
15369 typename :: [opt] nested-name-specifier template [opt]
15370 template-id
15372 GNU extension:
15374 elaborated-type-specifier:
15375 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15376 class-key attributes :: [opt] nested-name-specifier [opt]
15377 template [opt] template-id
15378 enum attributes :: [opt] nested-name-specifier [opt] identifier
15380 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15381 declared `friend'. If IS_DECLARATION is TRUE, then this
15382 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15383 something is being declared.
15385 Returns the TYPE specified. */
15387 static tree
15388 cp_parser_elaborated_type_specifier (cp_parser* parser,
15389 bool is_friend,
15390 bool is_declaration)
15392 enum tag_types tag_type;
15393 tree identifier;
15394 tree type = NULL_TREE;
15395 tree attributes = NULL_TREE;
15396 tree globalscope;
15397 cp_token *token = NULL;
15399 /* See if we're looking at the `enum' keyword. */
15400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15402 /* Consume the `enum' token. */
15403 cp_lexer_consume_token (parser->lexer);
15404 /* Remember that it's an enumeration type. */
15405 tag_type = enum_type;
15406 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15407 enums) is used here. */
15408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15409 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15411 pedwarn (input_location, 0, "elaborated-type-specifier "
15412 "for a scoped enum must not use the %<%D%> keyword",
15413 cp_lexer_peek_token (parser->lexer)->u.value);
15414 /* Consume the `struct' or `class' and parse it anyway. */
15415 cp_lexer_consume_token (parser->lexer);
15417 /* Parse the attributes. */
15418 attributes = cp_parser_attributes_opt (parser);
15420 /* Or, it might be `typename'. */
15421 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15422 RID_TYPENAME))
15424 /* Consume the `typename' token. */
15425 cp_lexer_consume_token (parser->lexer);
15426 /* Remember that it's a `typename' type. */
15427 tag_type = typename_type;
15429 /* Otherwise it must be a class-key. */
15430 else
15432 tag_type = cp_parser_class_key (parser);
15433 if (tag_type == none_type)
15434 return error_mark_node;
15435 /* Parse the attributes. */
15436 attributes = cp_parser_attributes_opt (parser);
15439 /* Look for the `::' operator. */
15440 globalscope = cp_parser_global_scope_opt (parser,
15441 /*current_scope_valid_p=*/false);
15442 /* Look for the nested-name-specifier. */
15443 if (tag_type == typename_type && !globalscope)
15445 if (!cp_parser_nested_name_specifier (parser,
15446 /*typename_keyword_p=*/true,
15447 /*check_dependency_p=*/true,
15448 /*type_p=*/true,
15449 is_declaration))
15450 return error_mark_node;
15452 else
15453 /* Even though `typename' is not present, the proposed resolution
15454 to Core Issue 180 says that in `class A<T>::B', `B' should be
15455 considered a type-name, even if `A<T>' is dependent. */
15456 cp_parser_nested_name_specifier_opt (parser,
15457 /*typename_keyword_p=*/true,
15458 /*check_dependency_p=*/true,
15459 /*type_p=*/true,
15460 is_declaration);
15461 /* For everything but enumeration types, consider a template-id.
15462 For an enumeration type, consider only a plain identifier. */
15463 if (tag_type != enum_type)
15465 bool template_p = false;
15466 tree decl;
15468 /* Allow the `template' keyword. */
15469 template_p = cp_parser_optional_template_keyword (parser);
15470 /* If we didn't see `template', we don't know if there's a
15471 template-id or not. */
15472 if (!template_p)
15473 cp_parser_parse_tentatively (parser);
15474 /* Parse the template-id. */
15475 token = cp_lexer_peek_token (parser->lexer);
15476 decl = cp_parser_template_id (parser, template_p,
15477 /*check_dependency_p=*/true,
15478 tag_type,
15479 is_declaration);
15480 /* If we didn't find a template-id, look for an ordinary
15481 identifier. */
15482 if (!template_p && !cp_parser_parse_definitely (parser))
15484 /* We can get here when cp_parser_template_id, called by
15485 cp_parser_class_name with tag_type == none_type, succeeds
15486 and caches a BASELINK. Then, when called again here,
15487 instead of failing and returning an error_mark_node
15488 returns it (see template/typename17.C in C++11).
15489 ??? Could we diagnose this earlier? */
15490 else if (tag_type == typename_type && BASELINK_P (decl))
15492 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15493 type = error_mark_node;
15495 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15496 in effect, then we must assume that, upon instantiation, the
15497 template will correspond to a class. */
15498 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15499 && tag_type == typename_type)
15500 type = make_typename_type (parser->scope, decl,
15501 typename_type,
15502 /*complain=*/tf_error);
15503 /* If the `typename' keyword is in effect and DECL is not a type
15504 decl, then type is non existent. */
15505 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15507 else if (TREE_CODE (decl) == TYPE_DECL)
15508 type = check_elaborated_type_specifier (tag_type, decl,
15509 /*allow_template_p=*/true);
15510 else if (decl == error_mark_node)
15511 type = error_mark_node;
15514 if (!type)
15516 token = cp_lexer_peek_token (parser->lexer);
15517 identifier = cp_parser_identifier (parser);
15519 if (identifier == error_mark_node)
15521 parser->scope = NULL_TREE;
15522 return error_mark_node;
15525 /* For a `typename', we needn't call xref_tag. */
15526 if (tag_type == typename_type
15527 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15528 return cp_parser_make_typename_type (parser, identifier,
15529 token->location);
15531 /* Template parameter lists apply only if we are not within a
15532 function parameter list. */
15533 bool template_parm_lists_apply
15534 = parser->num_template_parameter_lists;
15535 if (template_parm_lists_apply)
15536 for (cp_binding_level *s = current_binding_level;
15537 s && s->kind != sk_template_parms;
15538 s = s->level_chain)
15539 if (s->kind == sk_function_parms)
15540 template_parm_lists_apply = false;
15542 /* Look up a qualified name in the usual way. */
15543 if (parser->scope)
15545 tree decl;
15546 tree ambiguous_decls;
15548 decl = cp_parser_lookup_name (parser, identifier,
15549 tag_type,
15550 /*is_template=*/false,
15551 /*is_namespace=*/false,
15552 /*check_dependency=*/true,
15553 &ambiguous_decls,
15554 token->location);
15556 /* If the lookup was ambiguous, an error will already have been
15557 issued. */
15558 if (ambiguous_decls)
15559 return error_mark_node;
15561 /* If we are parsing friend declaration, DECL may be a
15562 TEMPLATE_DECL tree node here. However, we need to check
15563 whether this TEMPLATE_DECL results in valid code. Consider
15564 the following example:
15566 namespace N {
15567 template <class T> class C {};
15569 class X {
15570 template <class T> friend class N::C; // #1, valid code
15572 template <class T> class Y {
15573 friend class N::C; // #2, invalid code
15576 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15577 name lookup of `N::C'. We see that friend declaration must
15578 be template for the code to be valid. Note that
15579 processing_template_decl does not work here since it is
15580 always 1 for the above two cases. */
15582 decl = (cp_parser_maybe_treat_template_as_class
15583 (decl, /*tag_name_p=*/is_friend
15584 && template_parm_lists_apply));
15586 if (TREE_CODE (decl) != TYPE_DECL)
15588 cp_parser_diagnose_invalid_type_name (parser,
15589 identifier,
15590 token->location);
15591 return error_mark_node;
15594 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15596 bool allow_template = (template_parm_lists_apply
15597 || DECL_SELF_REFERENCE_P (decl));
15598 type = check_elaborated_type_specifier (tag_type, decl,
15599 allow_template);
15601 if (type == error_mark_node)
15602 return error_mark_node;
15605 /* Forward declarations of nested types, such as
15607 class C1::C2;
15608 class C1::C2::C3;
15610 are invalid unless all components preceding the final '::'
15611 are complete. If all enclosing types are complete, these
15612 declarations become merely pointless.
15614 Invalid forward declarations of nested types are errors
15615 caught elsewhere in parsing. Those that are pointless arrive
15616 here. */
15618 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15619 && !is_friend && !processing_explicit_instantiation)
15620 warning (0, "declaration %qD does not declare anything", decl);
15622 type = TREE_TYPE (decl);
15624 else
15626 /* An elaborated-type-specifier sometimes introduces a new type and
15627 sometimes names an existing type. Normally, the rule is that it
15628 introduces a new type only if there is not an existing type of
15629 the same name already in scope. For example, given:
15631 struct S {};
15632 void f() { struct S s; }
15634 the `struct S' in the body of `f' is the same `struct S' as in
15635 the global scope; the existing definition is used. However, if
15636 there were no global declaration, this would introduce a new
15637 local class named `S'.
15639 An exception to this rule applies to the following code:
15641 namespace N { struct S; }
15643 Here, the elaborated-type-specifier names a new type
15644 unconditionally; even if there is already an `S' in the
15645 containing scope this declaration names a new type.
15646 This exception only applies if the elaborated-type-specifier
15647 forms the complete declaration:
15649 [class.name]
15651 A declaration consisting solely of `class-key identifier ;' is
15652 either a redeclaration of the name in the current scope or a
15653 forward declaration of the identifier as a class name. It
15654 introduces the name into the current scope.
15656 We are in this situation precisely when the next token is a `;'.
15658 An exception to the exception is that a `friend' declaration does
15659 *not* name a new type; i.e., given:
15661 struct S { friend struct T; };
15663 `T' is not a new type in the scope of `S'.
15665 Also, `new struct S' or `sizeof (struct S)' never results in the
15666 definition of a new type; a new type can only be declared in a
15667 declaration context. */
15669 tag_scope ts;
15670 bool template_p;
15672 if (is_friend)
15673 /* Friends have special name lookup rules. */
15674 ts = ts_within_enclosing_non_class;
15675 else if (is_declaration
15676 && cp_lexer_next_token_is (parser->lexer,
15677 CPP_SEMICOLON))
15678 /* This is a `class-key identifier ;' */
15679 ts = ts_current;
15680 else
15681 ts = ts_global;
15683 template_p =
15684 (template_parm_lists_apply
15685 && (cp_parser_next_token_starts_class_definition_p (parser)
15686 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15687 /* An unqualified name was used to reference this type, so
15688 there were no qualifying templates. */
15689 if (template_parm_lists_apply
15690 && !cp_parser_check_template_parameters (parser,
15691 /*num_templates=*/0,
15692 token->location,
15693 /*declarator=*/NULL))
15694 return error_mark_node;
15695 type = xref_tag (tag_type, identifier, ts, template_p);
15699 if (type == error_mark_node)
15700 return error_mark_node;
15702 /* Allow attributes on forward declarations of classes. */
15703 if (attributes)
15705 if (TREE_CODE (type) == TYPENAME_TYPE)
15706 warning (OPT_Wattributes,
15707 "attributes ignored on uninstantiated type");
15708 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15709 && ! processing_explicit_instantiation)
15710 warning (OPT_Wattributes,
15711 "attributes ignored on template instantiation");
15712 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15713 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15714 else
15715 warning (OPT_Wattributes,
15716 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15719 if (tag_type != enum_type)
15721 /* Indicate whether this class was declared as a `class' or as a
15722 `struct'. */
15723 if (TREE_CODE (type) == RECORD_TYPE)
15724 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15725 cp_parser_check_class_key (tag_type, type);
15728 /* A "<" cannot follow an elaborated type specifier. If that
15729 happens, the user was probably trying to form a template-id. */
15730 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15731 token->location);
15733 return type;
15736 /* Parse an enum-specifier.
15738 enum-specifier:
15739 enum-head { enumerator-list [opt] }
15740 enum-head { enumerator-list , } [C++0x]
15742 enum-head:
15743 enum-key identifier [opt] enum-base [opt]
15744 enum-key nested-name-specifier identifier enum-base [opt]
15746 enum-key:
15747 enum
15748 enum class [C++0x]
15749 enum struct [C++0x]
15751 enum-base: [C++0x]
15752 : type-specifier-seq
15754 opaque-enum-specifier:
15755 enum-key identifier enum-base [opt] ;
15757 GNU Extensions:
15758 enum-key attributes[opt] identifier [opt] enum-base [opt]
15759 { enumerator-list [opt] }attributes[opt]
15760 enum-key attributes[opt] identifier [opt] enum-base [opt]
15761 { enumerator-list, }attributes[opt] [C++0x]
15763 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15764 if the token stream isn't an enum-specifier after all. */
15766 static tree
15767 cp_parser_enum_specifier (cp_parser* parser)
15769 tree identifier;
15770 tree type = NULL_TREE;
15771 tree prev_scope;
15772 tree nested_name_specifier = NULL_TREE;
15773 tree attributes;
15774 bool scoped_enum_p = false;
15775 bool has_underlying_type = false;
15776 bool nested_being_defined = false;
15777 bool new_value_list = false;
15778 bool is_new_type = false;
15779 bool is_anonymous = false;
15780 tree underlying_type = NULL_TREE;
15781 cp_token *type_start_token = NULL;
15782 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15784 parser->colon_corrects_to_scope_p = false;
15786 /* Parse tentatively so that we can back up if we don't find a
15787 enum-specifier. */
15788 cp_parser_parse_tentatively (parser);
15790 /* Caller guarantees that the current token is 'enum', an identifier
15791 possibly follows, and the token after that is an opening brace.
15792 If we don't have an identifier, fabricate an anonymous name for
15793 the enumeration being defined. */
15794 cp_lexer_consume_token (parser->lexer);
15796 /* Parse the "class" or "struct", which indicates a scoped
15797 enumeration type in C++0x. */
15798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15799 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15801 if (cxx_dialect < cxx11)
15802 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15804 /* Consume the `struct' or `class' token. */
15805 cp_lexer_consume_token (parser->lexer);
15807 scoped_enum_p = true;
15810 attributes = cp_parser_attributes_opt (parser);
15812 /* Clear the qualification. */
15813 parser->scope = NULL_TREE;
15814 parser->qualifying_scope = NULL_TREE;
15815 parser->object_scope = NULL_TREE;
15817 /* Figure out in what scope the declaration is being placed. */
15818 prev_scope = current_scope ();
15820 type_start_token = cp_lexer_peek_token (parser->lexer);
15822 push_deferring_access_checks (dk_no_check);
15823 nested_name_specifier
15824 = cp_parser_nested_name_specifier_opt (parser,
15825 /*typename_keyword_p=*/true,
15826 /*check_dependency_p=*/false,
15827 /*type_p=*/false,
15828 /*is_declaration=*/false);
15830 if (nested_name_specifier)
15832 tree name;
15834 identifier = cp_parser_identifier (parser);
15835 name = cp_parser_lookup_name (parser, identifier,
15836 enum_type,
15837 /*is_template=*/false,
15838 /*is_namespace=*/false,
15839 /*check_dependency=*/true,
15840 /*ambiguous_decls=*/NULL,
15841 input_location);
15842 if (name && name != error_mark_node)
15844 type = TREE_TYPE (name);
15845 if (TREE_CODE (type) == TYPENAME_TYPE)
15847 /* Are template enums allowed in ISO? */
15848 if (template_parm_scope_p ())
15849 pedwarn (type_start_token->location, OPT_Wpedantic,
15850 "%qD is an enumeration template", name);
15851 /* ignore a typename reference, for it will be solved by name
15852 in start_enum. */
15853 type = NULL_TREE;
15856 else if (nested_name_specifier == error_mark_node)
15857 /* We already issued an error. */;
15858 else
15859 error_at (type_start_token->location,
15860 "%qD is not an enumerator-name", identifier);
15862 else
15864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15865 identifier = cp_parser_identifier (parser);
15866 else
15868 identifier = make_anon_name ();
15869 is_anonymous = true;
15870 if (scoped_enum_p)
15871 error_at (type_start_token->location,
15872 "anonymous scoped enum is not allowed");
15875 pop_deferring_access_checks ();
15877 /* Check for the `:' that denotes a specified underlying type in C++0x.
15878 Note that a ':' could also indicate a bitfield width, however. */
15879 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15881 cp_decl_specifier_seq type_specifiers;
15883 /* Consume the `:'. */
15884 cp_lexer_consume_token (parser->lexer);
15886 /* Parse the type-specifier-seq. */
15887 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15888 /*is_trailing_return=*/false,
15889 &type_specifiers);
15891 /* At this point this is surely not elaborated type specifier. */
15892 if (!cp_parser_parse_definitely (parser))
15893 return NULL_TREE;
15895 if (cxx_dialect < cxx11)
15896 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15898 has_underlying_type = true;
15900 /* If that didn't work, stop. */
15901 if (type_specifiers.type != error_mark_node)
15903 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15904 /*initialized=*/0, NULL);
15905 if (underlying_type == error_mark_node
15906 || check_for_bare_parameter_packs (underlying_type))
15907 underlying_type = NULL_TREE;
15911 /* Look for the `{' but don't consume it yet. */
15912 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15914 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15916 cp_parser_error (parser, "expected %<{%>");
15917 if (has_underlying_type)
15919 type = NULL_TREE;
15920 goto out;
15923 /* An opaque-enum-specifier must have a ';' here. */
15924 if ((scoped_enum_p || underlying_type)
15925 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15927 cp_parser_error (parser, "expected %<;%> or %<{%>");
15928 if (has_underlying_type)
15930 type = NULL_TREE;
15931 goto out;
15936 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15937 return NULL_TREE;
15939 if (nested_name_specifier)
15941 if (CLASS_TYPE_P (nested_name_specifier))
15943 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15944 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15945 push_scope (nested_name_specifier);
15947 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15949 push_nested_namespace (nested_name_specifier);
15953 /* Issue an error message if type-definitions are forbidden here. */
15954 if (!cp_parser_check_type_definition (parser))
15955 type = error_mark_node;
15956 else
15957 /* Create the new type. We do this before consuming the opening
15958 brace so the enum will be recorded as being on the line of its
15959 tag (or the 'enum' keyword, if there is no tag). */
15960 type = start_enum (identifier, type, underlying_type,
15961 scoped_enum_p, &is_new_type);
15963 /* If the next token is not '{' it is an opaque-enum-specifier or an
15964 elaborated-type-specifier. */
15965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15967 timevar_push (TV_PARSE_ENUM);
15968 if (nested_name_specifier
15969 && nested_name_specifier != error_mark_node)
15971 /* The following catches invalid code such as:
15972 enum class S<int>::E { A, B, C }; */
15973 if (!processing_specialization
15974 && CLASS_TYPE_P (nested_name_specifier)
15975 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15976 error_at (type_start_token->location, "cannot add an enumerator "
15977 "list to a template instantiation");
15979 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15981 error_at (type_start_token->location,
15982 "%<%T::%E%> has not been declared",
15983 TYPE_CONTEXT (nested_name_specifier),
15984 nested_name_specifier);
15985 type = error_mark_node;
15987 /* If that scope does not contain the scope in which the
15988 class was originally declared, the program is invalid. */
15989 else if (prev_scope && !is_ancestor (prev_scope,
15990 nested_name_specifier))
15992 if (at_namespace_scope_p ())
15993 error_at (type_start_token->location,
15994 "declaration of %qD in namespace %qD which does not "
15995 "enclose %qD",
15996 type, prev_scope, nested_name_specifier);
15997 else
15998 error_at (type_start_token->location,
15999 "declaration of %qD in %qD which does not "
16000 "enclose %qD",
16001 type, prev_scope, nested_name_specifier);
16002 type = error_mark_node;
16006 if (scoped_enum_p)
16007 begin_scope (sk_scoped_enum, type);
16009 /* Consume the opening brace. */
16010 cp_lexer_consume_token (parser->lexer);
16012 if (type == error_mark_node)
16013 ; /* Nothing to add */
16014 else if (OPAQUE_ENUM_P (type)
16015 || (cxx_dialect > cxx98 && processing_specialization))
16017 new_value_list = true;
16018 SET_OPAQUE_ENUM_P (type, false);
16019 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16021 else
16023 error_at (type_start_token->location,
16024 "multiple definition of %q#T", type);
16025 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16026 "previous definition here");
16027 type = error_mark_node;
16030 if (type == error_mark_node)
16031 cp_parser_skip_to_end_of_block_or_statement (parser);
16032 /* If the next token is not '}', then there are some enumerators. */
16033 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16035 if (is_anonymous && !scoped_enum_p)
16036 pedwarn (type_start_token->location, OPT_Wpedantic,
16037 "ISO C++ forbids empty anonymous enum");
16039 else
16040 cp_parser_enumerator_list (parser, type);
16042 /* Consume the final '}'. */
16043 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16045 if (scoped_enum_p)
16046 finish_scope ();
16047 timevar_pop (TV_PARSE_ENUM);
16049 else
16051 /* If a ';' follows, then it is an opaque-enum-specifier
16052 and additional restrictions apply. */
16053 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16055 if (is_anonymous)
16056 error_at (type_start_token->location,
16057 "opaque-enum-specifier without name");
16058 else if (nested_name_specifier)
16059 error_at (type_start_token->location,
16060 "opaque-enum-specifier must use a simple identifier");
16064 /* Look for trailing attributes to apply to this enumeration, and
16065 apply them if appropriate. */
16066 if (cp_parser_allow_gnu_extensions_p (parser))
16068 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16069 trailing_attr = chainon (trailing_attr, attributes);
16070 cplus_decl_attributes (&type,
16071 trailing_attr,
16072 (int) ATTR_FLAG_TYPE_IN_PLACE);
16075 /* Finish up the enumeration. */
16076 if (type != error_mark_node)
16078 if (new_value_list)
16079 finish_enum_value_list (type);
16080 if (is_new_type)
16081 finish_enum (type);
16084 if (nested_name_specifier)
16086 if (CLASS_TYPE_P (nested_name_specifier))
16088 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16089 pop_scope (nested_name_specifier);
16091 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16093 pop_nested_namespace (nested_name_specifier);
16096 out:
16097 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16098 return type;
16101 /* Parse an enumerator-list. The enumerators all have the indicated
16102 TYPE.
16104 enumerator-list:
16105 enumerator-definition
16106 enumerator-list , enumerator-definition */
16108 static void
16109 cp_parser_enumerator_list (cp_parser* parser, tree type)
16111 while (true)
16113 /* Parse an enumerator-definition. */
16114 cp_parser_enumerator_definition (parser, type);
16116 /* If the next token is not a ',', we've reached the end of
16117 the list. */
16118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16119 break;
16120 /* Otherwise, consume the `,' and keep going. */
16121 cp_lexer_consume_token (parser->lexer);
16122 /* If the next token is a `}', there is a trailing comma. */
16123 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16125 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16126 pedwarn (input_location, OPT_Wpedantic,
16127 "comma at end of enumerator list");
16128 break;
16133 /* Parse an enumerator-definition. The enumerator has the indicated
16134 TYPE.
16136 enumerator-definition:
16137 enumerator
16138 enumerator = constant-expression
16140 enumerator:
16141 identifier
16143 GNU Extensions:
16145 enumerator-definition:
16146 enumerator attributes [opt]
16147 enumerator attributes [opt] = constant-expression */
16149 static void
16150 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16152 tree identifier;
16153 tree value;
16154 location_t loc;
16156 /* Save the input location because we are interested in the location
16157 of the identifier and not the location of the explicit value. */
16158 loc = cp_lexer_peek_token (parser->lexer)->location;
16160 /* Look for the identifier. */
16161 identifier = cp_parser_identifier (parser);
16162 if (identifier == error_mark_node)
16163 return;
16165 /* Parse any specified attributes. */
16166 tree attrs = cp_parser_attributes_opt (parser);
16168 /* If the next token is an '=', then there is an explicit value. */
16169 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16171 /* Consume the `=' token. */
16172 cp_lexer_consume_token (parser->lexer);
16173 /* Parse the value. */
16174 value = cp_parser_constant_expression (parser);
16176 else
16177 value = NULL_TREE;
16179 /* If we are processing a template, make sure the initializer of the
16180 enumerator doesn't contain any bare template parameter pack. */
16181 if (check_for_bare_parameter_packs (value))
16182 value = error_mark_node;
16184 /* Create the enumerator. */
16185 build_enumerator (identifier, value, type, attrs, loc);
16188 /* Parse a namespace-name.
16190 namespace-name:
16191 original-namespace-name
16192 namespace-alias
16194 Returns the NAMESPACE_DECL for the namespace. */
16196 static tree
16197 cp_parser_namespace_name (cp_parser* parser)
16199 tree identifier;
16200 tree namespace_decl;
16202 cp_token *token = cp_lexer_peek_token (parser->lexer);
16204 /* Get the name of the namespace. */
16205 identifier = cp_parser_identifier (parser);
16206 if (identifier == error_mark_node)
16207 return error_mark_node;
16209 /* Look up the identifier in the currently active scope. Look only
16210 for namespaces, due to:
16212 [basic.lookup.udir]
16214 When looking up a namespace-name in a using-directive or alias
16215 definition, only namespace names are considered.
16217 And:
16219 [basic.lookup.qual]
16221 During the lookup of a name preceding the :: scope resolution
16222 operator, object, function, and enumerator names are ignored.
16224 (Note that cp_parser_qualifying_entity only calls this
16225 function if the token after the name is the scope resolution
16226 operator.) */
16227 namespace_decl = cp_parser_lookup_name (parser, identifier,
16228 none_type,
16229 /*is_template=*/false,
16230 /*is_namespace=*/true,
16231 /*check_dependency=*/true,
16232 /*ambiguous_decls=*/NULL,
16233 token->location);
16234 /* If it's not a namespace, issue an error. */
16235 if (namespace_decl == error_mark_node
16236 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16238 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16239 error_at (token->location, "%qD is not a namespace-name", identifier);
16240 cp_parser_error (parser, "expected namespace-name");
16241 namespace_decl = error_mark_node;
16244 return namespace_decl;
16247 /* Parse a namespace-definition.
16249 namespace-definition:
16250 named-namespace-definition
16251 unnamed-namespace-definition
16253 named-namespace-definition:
16254 original-namespace-definition
16255 extension-namespace-definition
16257 original-namespace-definition:
16258 namespace identifier { namespace-body }
16260 extension-namespace-definition:
16261 namespace original-namespace-name { namespace-body }
16263 unnamed-namespace-definition:
16264 namespace { namespace-body } */
16266 static void
16267 cp_parser_namespace_definition (cp_parser* parser)
16269 tree identifier, attribs;
16270 bool has_visibility;
16271 bool is_inline;
16273 cp_ensure_no_omp_declare_simd (parser);
16274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16276 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16277 is_inline = true;
16278 cp_lexer_consume_token (parser->lexer);
16280 else
16281 is_inline = false;
16283 /* Look for the `namespace' keyword. */
16284 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16286 /* Get the name of the namespace. We do not attempt to distinguish
16287 between an original-namespace-definition and an
16288 extension-namespace-definition at this point. The semantic
16289 analysis routines are responsible for that. */
16290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16291 identifier = cp_parser_identifier (parser);
16292 else
16293 identifier = NULL_TREE;
16295 /* Parse any specified attributes. */
16296 attribs = cp_parser_attributes_opt (parser);
16298 /* Look for the `{' to start the namespace. */
16299 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16300 /* Start the namespace. */
16301 push_namespace (identifier);
16303 /* "inline namespace" is equivalent to a stub namespace definition
16304 followed by a strong using directive. */
16305 if (is_inline)
16307 tree name_space = current_namespace;
16308 /* Set up namespace association. */
16309 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16310 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16311 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16312 /* Import the contents of the inline namespace. */
16313 pop_namespace ();
16314 do_using_directive (name_space);
16315 push_namespace (identifier);
16318 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16320 /* Parse the body of the namespace. */
16321 cp_parser_namespace_body (parser);
16323 if (has_visibility)
16324 pop_visibility (1);
16326 /* Finish the namespace. */
16327 pop_namespace ();
16328 /* Look for the final `}'. */
16329 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16332 /* Parse a namespace-body.
16334 namespace-body:
16335 declaration-seq [opt] */
16337 static void
16338 cp_parser_namespace_body (cp_parser* parser)
16340 cp_parser_declaration_seq_opt (parser);
16343 /* Parse a namespace-alias-definition.
16345 namespace-alias-definition:
16346 namespace identifier = qualified-namespace-specifier ; */
16348 static void
16349 cp_parser_namespace_alias_definition (cp_parser* parser)
16351 tree identifier;
16352 tree namespace_specifier;
16354 cp_token *token = cp_lexer_peek_token (parser->lexer);
16356 /* Look for the `namespace' keyword. */
16357 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16358 /* Look for the identifier. */
16359 identifier = cp_parser_identifier (parser);
16360 if (identifier == error_mark_node)
16361 return;
16362 /* Look for the `=' token. */
16363 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16364 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16366 error_at (token->location, "%<namespace%> definition is not allowed here");
16367 /* Skip the definition. */
16368 cp_lexer_consume_token (parser->lexer);
16369 if (cp_parser_skip_to_closing_brace (parser))
16370 cp_lexer_consume_token (parser->lexer);
16371 return;
16373 cp_parser_require (parser, CPP_EQ, RT_EQ);
16374 /* Look for the qualified-namespace-specifier. */
16375 namespace_specifier
16376 = cp_parser_qualified_namespace_specifier (parser);
16377 /* Look for the `;' token. */
16378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16380 /* Register the alias in the symbol table. */
16381 do_namespace_alias (identifier, namespace_specifier);
16384 /* Parse a qualified-namespace-specifier.
16386 qualified-namespace-specifier:
16387 :: [opt] nested-name-specifier [opt] namespace-name
16389 Returns a NAMESPACE_DECL corresponding to the specified
16390 namespace. */
16392 static tree
16393 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16395 /* Look for the optional `::'. */
16396 cp_parser_global_scope_opt (parser,
16397 /*current_scope_valid_p=*/false);
16399 /* Look for the optional nested-name-specifier. */
16400 cp_parser_nested_name_specifier_opt (parser,
16401 /*typename_keyword_p=*/false,
16402 /*check_dependency_p=*/true,
16403 /*type_p=*/false,
16404 /*is_declaration=*/true);
16406 return cp_parser_namespace_name (parser);
16409 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16410 access declaration.
16412 using-declaration:
16413 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16414 using :: unqualified-id ;
16416 access-declaration:
16417 qualified-id ;
16421 static bool
16422 cp_parser_using_declaration (cp_parser* parser,
16423 bool access_declaration_p)
16425 cp_token *token;
16426 bool typename_p = false;
16427 bool global_scope_p;
16428 tree decl;
16429 tree identifier;
16430 tree qscope;
16431 int oldcount = errorcount;
16432 cp_token *diag_token = NULL;
16434 if (access_declaration_p)
16436 diag_token = cp_lexer_peek_token (parser->lexer);
16437 cp_parser_parse_tentatively (parser);
16439 else
16441 /* Look for the `using' keyword. */
16442 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16444 /* Peek at the next token. */
16445 token = cp_lexer_peek_token (parser->lexer);
16446 /* See if it's `typename'. */
16447 if (token->keyword == RID_TYPENAME)
16449 /* Remember that we've seen it. */
16450 typename_p = true;
16451 /* Consume the `typename' token. */
16452 cp_lexer_consume_token (parser->lexer);
16456 /* Look for the optional global scope qualification. */
16457 global_scope_p
16458 = (cp_parser_global_scope_opt (parser,
16459 /*current_scope_valid_p=*/false)
16460 != NULL_TREE);
16462 /* If we saw `typename', or didn't see `::', then there must be a
16463 nested-name-specifier present. */
16464 if (typename_p || !global_scope_p)
16466 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16467 /*check_dependency_p=*/true,
16468 /*type_p=*/false,
16469 /*is_declaration=*/true);
16470 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16472 cp_parser_skip_to_end_of_block_or_statement (parser);
16473 return false;
16476 /* Otherwise, we could be in either of the two productions. In that
16477 case, treat the nested-name-specifier as optional. */
16478 else
16479 qscope = cp_parser_nested_name_specifier_opt (parser,
16480 /*typename_keyword_p=*/false,
16481 /*check_dependency_p=*/true,
16482 /*type_p=*/false,
16483 /*is_declaration=*/true);
16484 if (!qscope)
16485 qscope = global_namespace;
16486 else if (UNSCOPED_ENUM_P (qscope))
16487 qscope = CP_TYPE_CONTEXT (qscope);
16489 if (access_declaration_p && cp_parser_error_occurred (parser))
16490 /* Something has already gone wrong; there's no need to parse
16491 further. Since an error has occurred, the return value of
16492 cp_parser_parse_definitely will be false, as required. */
16493 return cp_parser_parse_definitely (parser);
16495 token = cp_lexer_peek_token (parser->lexer);
16496 /* Parse the unqualified-id. */
16497 identifier = cp_parser_unqualified_id (parser,
16498 /*template_keyword_p=*/false,
16499 /*check_dependency_p=*/true,
16500 /*declarator_p=*/true,
16501 /*optional_p=*/false);
16503 if (access_declaration_p)
16505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16506 cp_parser_simulate_error (parser);
16507 if (!cp_parser_parse_definitely (parser))
16508 return false;
16511 /* The function we call to handle a using-declaration is different
16512 depending on what scope we are in. */
16513 if (qscope == error_mark_node || identifier == error_mark_node)
16515 else if (!identifier_p (identifier)
16516 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16517 /* [namespace.udecl]
16519 A using declaration shall not name a template-id. */
16520 error_at (token->location,
16521 "a template-id may not appear in a using-declaration");
16522 else
16524 if (at_class_scope_p ())
16526 /* Create the USING_DECL. */
16527 decl = do_class_using_decl (parser->scope, identifier);
16529 if (decl && typename_p)
16530 USING_DECL_TYPENAME_P (decl) = 1;
16532 if (check_for_bare_parameter_packs (decl))
16534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16535 return false;
16537 else
16538 /* Add it to the list of members in this class. */
16539 finish_member_declaration (decl);
16541 else
16543 decl = cp_parser_lookup_name_simple (parser,
16544 identifier,
16545 token->location);
16546 if (decl == error_mark_node)
16547 cp_parser_name_lookup_error (parser, identifier,
16548 decl, NLE_NULL,
16549 token->location);
16550 else if (check_for_bare_parameter_packs (decl))
16552 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16553 return false;
16555 else if (!at_namespace_scope_p ())
16556 do_local_using_decl (decl, qscope, identifier);
16557 else
16558 do_toplevel_using_decl (decl, qscope, identifier);
16562 /* Look for the final `;'. */
16563 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16565 if (access_declaration_p && errorcount == oldcount)
16566 warning_at (diag_token->location, OPT_Wdeprecated,
16567 "access declarations are deprecated "
16568 "in favour of using-declarations; "
16569 "suggestion: add the %<using%> keyword");
16571 return true;
16574 /* Parse an alias-declaration.
16576 alias-declaration:
16577 using identifier attribute-specifier-seq [opt] = type-id */
16579 static tree
16580 cp_parser_alias_declaration (cp_parser* parser)
16582 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16583 location_t id_location;
16584 cp_declarator *declarator;
16585 cp_decl_specifier_seq decl_specs;
16586 bool member_p;
16587 const char *saved_message = NULL;
16589 /* Look for the `using' keyword. */
16590 cp_token *using_token
16591 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16592 if (using_token == NULL)
16593 return error_mark_node;
16595 id_location = cp_lexer_peek_token (parser->lexer)->location;
16596 id = cp_parser_identifier (parser);
16597 if (id == error_mark_node)
16598 return error_mark_node;
16600 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16601 attributes = cp_parser_attributes_opt (parser);
16602 if (attributes == error_mark_node)
16603 return error_mark_node;
16605 cp_parser_require (parser, CPP_EQ, RT_EQ);
16607 if (cp_parser_error_occurred (parser))
16608 return error_mark_node;
16610 cp_parser_commit_to_tentative_parse (parser);
16612 /* Now we are going to parse the type-id of the declaration. */
16615 [dcl.type]/3 says:
16617 "A type-specifier-seq shall not define a class or enumeration
16618 unless it appears in the type-id of an alias-declaration (7.1.3) that
16619 is not the declaration of a template-declaration."
16621 In other words, if we currently are in an alias template, the
16622 type-id should not define a type.
16624 So let's set parser->type_definition_forbidden_message in that
16625 case; cp_parser_check_type_definition (called by
16626 cp_parser_class_specifier) will then emit an error if a type is
16627 defined in the type-id. */
16628 if (parser->num_template_parameter_lists)
16630 saved_message = parser->type_definition_forbidden_message;
16631 parser->type_definition_forbidden_message =
16632 G_("types may not be defined in alias template declarations");
16635 type = cp_parser_type_id (parser);
16637 /* Restore the error message if need be. */
16638 if (parser->num_template_parameter_lists)
16639 parser->type_definition_forbidden_message = saved_message;
16641 if (type == error_mark_node
16642 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16644 cp_parser_skip_to_end_of_block_or_statement (parser);
16645 return error_mark_node;
16648 /* A typedef-name can also be introduced by an alias-declaration. The
16649 identifier following the using keyword becomes a typedef-name. It has
16650 the same semantics as if it were introduced by the typedef
16651 specifier. In particular, it does not define a new type and it shall
16652 not appear in the type-id. */
16654 clear_decl_specs (&decl_specs);
16655 decl_specs.type = type;
16656 if (attributes != NULL_TREE)
16658 decl_specs.attributes = attributes;
16659 set_and_check_decl_spec_loc (&decl_specs,
16660 ds_attribute,
16661 attrs_token);
16663 set_and_check_decl_spec_loc (&decl_specs,
16664 ds_typedef,
16665 using_token);
16666 set_and_check_decl_spec_loc (&decl_specs,
16667 ds_alias,
16668 using_token);
16670 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16671 declarator->id_loc = id_location;
16673 member_p = at_class_scope_p ();
16674 if (member_p)
16675 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16676 NULL_TREE, attributes);
16677 else
16678 decl = start_decl (declarator, &decl_specs, 0,
16679 attributes, NULL_TREE, &pushed_scope);
16680 if (decl == error_mark_node)
16681 return decl;
16683 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16685 if (pushed_scope)
16686 pop_scope (pushed_scope);
16688 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16689 added into the symbol table; otherwise, return the TYPE_DECL. */
16690 if (DECL_LANG_SPECIFIC (decl)
16691 && DECL_TEMPLATE_INFO (decl)
16692 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16694 decl = DECL_TI_TEMPLATE (decl);
16695 if (member_p)
16696 check_member_template (decl);
16699 return decl;
16702 /* Parse a using-directive.
16704 using-directive:
16705 using namespace :: [opt] nested-name-specifier [opt]
16706 namespace-name ; */
16708 static void
16709 cp_parser_using_directive (cp_parser* parser)
16711 tree namespace_decl;
16712 tree attribs;
16714 /* Look for the `using' keyword. */
16715 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16716 /* And the `namespace' keyword. */
16717 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16718 /* Look for the optional `::' operator. */
16719 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16720 /* And the optional nested-name-specifier. */
16721 cp_parser_nested_name_specifier_opt (parser,
16722 /*typename_keyword_p=*/false,
16723 /*check_dependency_p=*/true,
16724 /*type_p=*/false,
16725 /*is_declaration=*/true);
16726 /* Get the namespace being used. */
16727 namespace_decl = cp_parser_namespace_name (parser);
16728 /* And any specified attributes. */
16729 attribs = cp_parser_attributes_opt (parser);
16730 /* Update the symbol table. */
16731 parse_using_directive (namespace_decl, attribs);
16732 /* Look for the final `;'. */
16733 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16736 /* Parse an asm-definition.
16738 asm-definition:
16739 asm ( string-literal ) ;
16741 GNU Extension:
16743 asm-definition:
16744 asm volatile [opt] ( string-literal ) ;
16745 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16746 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16747 : asm-operand-list [opt] ) ;
16748 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16749 : asm-operand-list [opt]
16750 : asm-clobber-list [opt] ) ;
16751 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16752 : asm-clobber-list [opt]
16753 : asm-goto-list ) ; */
16755 static void
16756 cp_parser_asm_definition (cp_parser* parser)
16758 tree string;
16759 tree outputs = NULL_TREE;
16760 tree inputs = NULL_TREE;
16761 tree clobbers = NULL_TREE;
16762 tree labels = NULL_TREE;
16763 tree asm_stmt;
16764 bool volatile_p = false;
16765 bool extended_p = false;
16766 bool invalid_inputs_p = false;
16767 bool invalid_outputs_p = false;
16768 bool goto_p = false;
16769 required_token missing = RT_NONE;
16771 /* Look for the `asm' keyword. */
16772 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16774 if (parser->in_function_body
16775 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16777 error ("%<asm%> in %<constexpr%> function");
16778 cp_function_chain->invalid_constexpr = true;
16781 /* See if the next token is `volatile'. */
16782 if (cp_parser_allow_gnu_extensions_p (parser)
16783 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16785 /* Remember that we saw the `volatile' keyword. */
16786 volatile_p = true;
16787 /* Consume the token. */
16788 cp_lexer_consume_token (parser->lexer);
16790 if (cp_parser_allow_gnu_extensions_p (parser)
16791 && parser->in_function_body
16792 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16794 /* Remember that we saw the `goto' keyword. */
16795 goto_p = true;
16796 /* Consume the token. */
16797 cp_lexer_consume_token (parser->lexer);
16799 /* Look for the opening `('. */
16800 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16801 return;
16802 /* Look for the string. */
16803 string = cp_parser_string_literal (parser, false, false);
16804 if (string == error_mark_node)
16806 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16807 /*consume_paren=*/true);
16808 return;
16811 /* If we're allowing GNU extensions, check for the extended assembly
16812 syntax. Unfortunately, the `:' tokens need not be separated by
16813 a space in C, and so, for compatibility, we tolerate that here
16814 too. Doing that means that we have to treat the `::' operator as
16815 two `:' tokens. */
16816 if (cp_parser_allow_gnu_extensions_p (parser)
16817 && parser->in_function_body
16818 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16819 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16821 bool inputs_p = false;
16822 bool clobbers_p = false;
16823 bool labels_p = false;
16825 /* The extended syntax was used. */
16826 extended_p = true;
16828 /* Look for outputs. */
16829 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16831 /* Consume the `:'. */
16832 cp_lexer_consume_token (parser->lexer);
16833 /* Parse the output-operands. */
16834 if (cp_lexer_next_token_is_not (parser->lexer,
16835 CPP_COLON)
16836 && cp_lexer_next_token_is_not (parser->lexer,
16837 CPP_SCOPE)
16838 && cp_lexer_next_token_is_not (parser->lexer,
16839 CPP_CLOSE_PAREN)
16840 && !goto_p)
16842 outputs = cp_parser_asm_operand_list (parser);
16843 if (outputs == error_mark_node)
16844 invalid_outputs_p = true;
16847 /* If the next token is `::', there are no outputs, and the
16848 next token is the beginning of the inputs. */
16849 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16850 /* The inputs are coming next. */
16851 inputs_p = true;
16853 /* Look for inputs. */
16854 if (inputs_p
16855 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16857 /* Consume the `:' or `::'. */
16858 cp_lexer_consume_token (parser->lexer);
16859 /* Parse the output-operands. */
16860 if (cp_lexer_next_token_is_not (parser->lexer,
16861 CPP_COLON)
16862 && cp_lexer_next_token_is_not (parser->lexer,
16863 CPP_SCOPE)
16864 && cp_lexer_next_token_is_not (parser->lexer,
16865 CPP_CLOSE_PAREN))
16867 inputs = cp_parser_asm_operand_list (parser);
16868 if (inputs == error_mark_node)
16869 invalid_inputs_p = true;
16872 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16873 /* The clobbers are coming next. */
16874 clobbers_p = true;
16876 /* Look for clobbers. */
16877 if (clobbers_p
16878 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16880 clobbers_p = true;
16881 /* Consume the `:' or `::'. */
16882 cp_lexer_consume_token (parser->lexer);
16883 /* Parse the clobbers. */
16884 if (cp_lexer_next_token_is_not (parser->lexer,
16885 CPP_COLON)
16886 && cp_lexer_next_token_is_not (parser->lexer,
16887 CPP_CLOSE_PAREN))
16888 clobbers = cp_parser_asm_clobber_list (parser);
16890 else if (goto_p
16891 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16892 /* The labels are coming next. */
16893 labels_p = true;
16895 /* Look for labels. */
16896 if (labels_p
16897 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16899 labels_p = true;
16900 /* Consume the `:' or `::'. */
16901 cp_lexer_consume_token (parser->lexer);
16902 /* Parse the labels. */
16903 labels = cp_parser_asm_label_list (parser);
16906 if (goto_p && !labels_p)
16907 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16909 else if (goto_p)
16910 missing = RT_COLON_SCOPE;
16912 /* Look for the closing `)'. */
16913 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16914 missing ? missing : RT_CLOSE_PAREN))
16915 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16916 /*consume_paren=*/true);
16917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16919 if (!invalid_inputs_p && !invalid_outputs_p)
16921 /* Create the ASM_EXPR. */
16922 if (parser->in_function_body)
16924 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16925 inputs, clobbers, labels);
16926 /* If the extended syntax was not used, mark the ASM_EXPR. */
16927 if (!extended_p)
16929 tree temp = asm_stmt;
16930 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16931 temp = TREE_OPERAND (temp, 0);
16933 ASM_INPUT_P (temp) = 1;
16936 else
16937 symtab->finalize_toplevel_asm (string);
16941 /* Declarators [gram.dcl.decl] */
16943 /* Parse an init-declarator.
16945 init-declarator:
16946 declarator initializer [opt]
16948 GNU Extension:
16950 init-declarator:
16951 declarator asm-specification [opt] attributes [opt] initializer [opt]
16953 function-definition:
16954 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16955 function-body
16956 decl-specifier-seq [opt] declarator function-try-block
16958 GNU Extension:
16960 function-definition:
16961 __extension__ function-definition
16963 TM Extension:
16965 function-definition:
16966 decl-specifier-seq [opt] declarator function-transaction-block
16968 The DECL_SPECIFIERS apply to this declarator. Returns a
16969 representation of the entity declared. If MEMBER_P is TRUE, then
16970 this declarator appears in a class scope. The new DECL created by
16971 this declarator is returned.
16973 The CHECKS are access checks that should be performed once we know
16974 what entity is being declared (and, therefore, what classes have
16975 befriended it).
16977 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16978 for a function-definition here as well. If the declarator is a
16979 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16980 be TRUE upon return. By that point, the function-definition will
16981 have been completely parsed.
16983 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16984 is FALSE.
16986 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16987 parsed declaration if it is an uninitialized single declarator not followed
16988 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16989 if present, will not be consumed. If returned, this declarator will be
16990 created with SD_INITIALIZED but will not call cp_finish_decl.
16992 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16993 and there is an initializer, the pointed location_t is set to the
16994 location of the '=' or `(', or '{' in C++11 token introducing the
16995 initializer. */
16997 static tree
16998 cp_parser_init_declarator (cp_parser* parser,
16999 cp_decl_specifier_seq *decl_specifiers,
17000 vec<deferred_access_check, va_gc> *checks,
17001 bool function_definition_allowed_p,
17002 bool member_p,
17003 int declares_class_or_enum,
17004 bool* function_definition_p,
17005 tree* maybe_range_for_decl,
17006 location_t* init_loc)
17008 cp_token *token = NULL, *asm_spec_start_token = NULL,
17009 *attributes_start_token = NULL;
17010 cp_declarator *declarator;
17011 tree prefix_attributes;
17012 tree attributes = NULL;
17013 tree asm_specification;
17014 tree initializer;
17015 tree decl = NULL_TREE;
17016 tree scope;
17017 int is_initialized;
17018 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17019 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17020 "(...)". */
17021 enum cpp_ttype initialization_kind;
17022 bool is_direct_init = false;
17023 bool is_non_constant_init;
17024 int ctor_dtor_or_conv_p;
17025 bool friend_p = cp_parser_friend_p (decl_specifiers);
17026 tree pushed_scope = NULL_TREE;
17027 bool range_for_decl_p = false;
17028 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17029 location_t tmp_init_loc = UNKNOWN_LOCATION;
17031 /* Gather the attributes that were provided with the
17032 decl-specifiers. */
17033 prefix_attributes = decl_specifiers->attributes;
17035 /* Assume that this is not the declarator for a function
17036 definition. */
17037 if (function_definition_p)
17038 *function_definition_p = false;
17040 /* Default arguments are only permitted for function parameters. */
17041 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17042 parser->default_arg_ok_p = false;
17044 /* Defer access checks while parsing the declarator; we cannot know
17045 what names are accessible until we know what is being
17046 declared. */
17047 resume_deferring_access_checks ();
17049 /* Parse the declarator. */
17050 token = cp_lexer_peek_token (parser->lexer);
17051 declarator
17052 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17053 &ctor_dtor_or_conv_p,
17054 /*parenthesized_p=*/NULL,
17055 member_p, friend_p);
17056 /* Gather up the deferred checks. */
17057 stop_deferring_access_checks ();
17059 parser->default_arg_ok_p = saved_default_arg_ok_p;
17061 /* If the DECLARATOR was erroneous, there's no need to go
17062 further. */
17063 if (declarator == cp_error_declarator)
17064 return error_mark_node;
17066 /* Check that the number of template-parameter-lists is OK. */
17067 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17068 token->location))
17069 return error_mark_node;
17071 if (declares_class_or_enum & 2)
17072 cp_parser_check_for_definition_in_return_type (declarator,
17073 decl_specifiers->type,
17074 decl_specifiers->locations[ds_type_spec]);
17076 /* Figure out what scope the entity declared by the DECLARATOR is
17077 located in. `grokdeclarator' sometimes changes the scope, so
17078 we compute it now. */
17079 scope = get_scope_of_declarator (declarator);
17081 /* Perform any lookups in the declared type which were thought to be
17082 dependent, but are not in the scope of the declarator. */
17083 decl_specifiers->type
17084 = maybe_update_decl_type (decl_specifiers->type, scope);
17086 /* If we're allowing GNU extensions, look for an
17087 asm-specification. */
17088 if (cp_parser_allow_gnu_extensions_p (parser))
17090 /* Look for an asm-specification. */
17091 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17092 asm_specification = cp_parser_asm_specification_opt (parser);
17094 else
17095 asm_specification = NULL_TREE;
17097 /* Look for attributes. */
17098 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17099 attributes = cp_parser_attributes_opt (parser);
17101 /* Peek at the next token. */
17102 token = cp_lexer_peek_token (parser->lexer);
17104 bool bogus_implicit_tmpl = false;
17106 if (function_declarator_p (declarator))
17108 /* Check to see if the token indicates the start of a
17109 function-definition. */
17110 if (cp_parser_token_starts_function_definition_p (token))
17112 if (!function_definition_allowed_p)
17114 /* If a function-definition should not appear here, issue an
17115 error message. */
17116 cp_parser_error (parser,
17117 "a function-definition is not allowed here");
17118 return error_mark_node;
17121 location_t func_brace_location
17122 = cp_lexer_peek_token (parser->lexer)->location;
17124 /* Neither attributes nor an asm-specification are allowed
17125 on a function-definition. */
17126 if (asm_specification)
17127 error_at (asm_spec_start_token->location,
17128 "an asm-specification is not allowed "
17129 "on a function-definition");
17130 if (attributes)
17131 error_at (attributes_start_token->location,
17132 "attributes are not allowed "
17133 "on a function-definition");
17134 /* This is a function-definition. */
17135 *function_definition_p = true;
17137 /* Parse the function definition. */
17138 if (member_p)
17139 decl = cp_parser_save_member_function_body (parser,
17140 decl_specifiers,
17141 declarator,
17142 prefix_attributes);
17143 else
17144 decl =
17145 (cp_parser_function_definition_from_specifiers_and_declarator
17146 (parser, decl_specifiers, prefix_attributes, declarator));
17148 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17150 /* This is where the prologue starts... */
17151 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17152 = func_brace_location;
17155 return decl;
17158 else if (parser->fully_implicit_function_template_p)
17160 /* A non-template declaration involving a function parameter list
17161 containing an implicit template parameter will be made into a
17162 template. If the resulting declaration is not going to be an
17163 actual function then finish the template scope here to prevent it.
17164 An error message will be issued once we have a decl to talk about.
17166 FIXME probably we should do type deduction rather than create an
17167 implicit template, but the standard currently doesn't allow it. */
17168 bogus_implicit_tmpl = true;
17169 finish_fully_implicit_template (parser, NULL_TREE);
17172 /* [dcl.dcl]
17174 Only in function declarations for constructors, destructors, and
17175 type conversions can the decl-specifier-seq be omitted.
17177 We explicitly postpone this check past the point where we handle
17178 function-definitions because we tolerate function-definitions
17179 that are missing their return types in some modes. */
17180 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17182 cp_parser_error (parser,
17183 "expected constructor, destructor, or type conversion");
17184 return error_mark_node;
17187 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17188 if (token->type == CPP_EQ
17189 || token->type == CPP_OPEN_PAREN
17190 || token->type == CPP_OPEN_BRACE)
17192 is_initialized = SD_INITIALIZED;
17193 initialization_kind = token->type;
17194 if (maybe_range_for_decl)
17195 *maybe_range_for_decl = error_mark_node;
17196 tmp_init_loc = token->location;
17197 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17198 *init_loc = tmp_init_loc;
17200 if (token->type == CPP_EQ
17201 && function_declarator_p (declarator))
17203 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17204 if (t2->keyword == RID_DEFAULT)
17205 is_initialized = SD_DEFAULTED;
17206 else if (t2->keyword == RID_DELETE)
17207 is_initialized = SD_DELETED;
17210 else
17212 /* If the init-declarator isn't initialized and isn't followed by a
17213 `,' or `;', it's not a valid init-declarator. */
17214 if (token->type != CPP_COMMA
17215 && token->type != CPP_SEMICOLON)
17217 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17218 range_for_decl_p = true;
17219 else
17221 if (!maybe_range_for_decl)
17222 cp_parser_error (parser, "expected initializer");
17223 return error_mark_node;
17226 is_initialized = SD_UNINITIALIZED;
17227 initialization_kind = CPP_EOF;
17230 /* Because start_decl has side-effects, we should only call it if we
17231 know we're going ahead. By this point, we know that we cannot
17232 possibly be looking at any other construct. */
17233 cp_parser_commit_to_tentative_parse (parser);
17235 /* Enter the newly declared entry in the symbol table. If we're
17236 processing a declaration in a class-specifier, we wait until
17237 after processing the initializer. */
17238 if (!member_p)
17240 if (parser->in_unbraced_linkage_specification_p)
17241 decl_specifiers->storage_class = sc_extern;
17242 decl = start_decl (declarator, decl_specifiers,
17243 range_for_decl_p? SD_INITIALIZED : is_initialized,
17244 attributes, prefix_attributes, &pushed_scope);
17245 cp_finalize_omp_declare_simd (parser, decl);
17246 /* Adjust location of decl if declarator->id_loc is more appropriate:
17247 set, and decl wasn't merged with another decl, in which case its
17248 location would be different from input_location, and more accurate. */
17249 if (DECL_P (decl)
17250 && declarator->id_loc != UNKNOWN_LOCATION
17251 && DECL_SOURCE_LOCATION (decl) == input_location)
17252 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17254 else if (scope)
17255 /* Enter the SCOPE. That way unqualified names appearing in the
17256 initializer will be looked up in SCOPE. */
17257 pushed_scope = push_scope (scope);
17259 /* Perform deferred access control checks, now that we know in which
17260 SCOPE the declared entity resides. */
17261 if (!member_p && decl)
17263 tree saved_current_function_decl = NULL_TREE;
17265 /* If the entity being declared is a function, pretend that we
17266 are in its scope. If it is a `friend', it may have access to
17267 things that would not otherwise be accessible. */
17268 if (TREE_CODE (decl) == FUNCTION_DECL)
17270 saved_current_function_decl = current_function_decl;
17271 current_function_decl = decl;
17274 /* Perform access checks for template parameters. */
17275 cp_parser_perform_template_parameter_access_checks (checks);
17277 /* Perform the access control checks for the declarator and the
17278 decl-specifiers. */
17279 perform_deferred_access_checks (tf_warning_or_error);
17281 /* Restore the saved value. */
17282 if (TREE_CODE (decl) == FUNCTION_DECL)
17283 current_function_decl = saved_current_function_decl;
17286 /* Parse the initializer. */
17287 initializer = NULL_TREE;
17288 is_direct_init = false;
17289 is_non_constant_init = true;
17290 if (is_initialized)
17292 if (function_declarator_p (declarator))
17294 if (initialization_kind == CPP_EQ)
17295 initializer = cp_parser_pure_specifier (parser);
17296 else
17298 /* If the declaration was erroneous, we don't really
17299 know what the user intended, so just silently
17300 consume the initializer. */
17301 if (decl != error_mark_node)
17302 error_at (tmp_init_loc, "initializer provided for function");
17303 cp_parser_skip_to_closing_parenthesis (parser,
17304 /*recovering=*/true,
17305 /*or_comma=*/false,
17306 /*consume_paren=*/true);
17309 else
17311 /* We want to record the extra mangling scope for in-class
17312 initializers of class members and initializers of static data
17313 member templates. The former involves deferring
17314 parsing of the initializer until end of class as with default
17315 arguments. So right here we only handle the latter. */
17316 if (!member_p && processing_template_decl)
17317 start_lambda_scope (decl);
17318 initializer = cp_parser_initializer (parser,
17319 &is_direct_init,
17320 &is_non_constant_init);
17321 if (!member_p && processing_template_decl)
17322 finish_lambda_scope ();
17323 if (initializer == error_mark_node)
17324 cp_parser_skip_to_end_of_statement (parser);
17328 /* The old parser allows attributes to appear after a parenthesized
17329 initializer. Mark Mitchell proposed removing this functionality
17330 on the GCC mailing lists on 2002-08-13. This parser accepts the
17331 attributes -- but ignores them. */
17332 if (cp_parser_allow_gnu_extensions_p (parser)
17333 && initialization_kind == CPP_OPEN_PAREN)
17334 if (cp_parser_attributes_opt (parser))
17335 warning (OPT_Wattributes,
17336 "attributes after parenthesized initializer ignored");
17338 /* And now complain about a non-function implicit template. */
17339 if (bogus_implicit_tmpl)
17340 error_at (DECL_SOURCE_LOCATION (decl),
17341 "non-function %qD declared as implicit template", decl);
17343 /* For an in-class declaration, use `grokfield' to create the
17344 declaration. */
17345 if (member_p)
17347 if (pushed_scope)
17349 pop_scope (pushed_scope);
17350 pushed_scope = NULL_TREE;
17352 decl = grokfield (declarator, decl_specifiers,
17353 initializer, !is_non_constant_init,
17354 /*asmspec=*/NULL_TREE,
17355 chainon (attributes, prefix_attributes));
17356 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17357 cp_parser_save_default_args (parser, decl);
17358 cp_finalize_omp_declare_simd (parser, decl);
17361 /* Finish processing the declaration. But, skip member
17362 declarations. */
17363 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17365 cp_finish_decl (decl,
17366 initializer, !is_non_constant_init,
17367 asm_specification,
17368 /* If the initializer is in parentheses, then this is
17369 a direct-initialization, which means that an
17370 `explicit' constructor is OK. Otherwise, an
17371 `explicit' constructor cannot be used. */
17372 ((is_direct_init || !is_initialized)
17373 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17375 else if ((cxx_dialect != cxx98) && friend_p
17376 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17377 /* Core issue #226 (C++0x only): A default template-argument
17378 shall not be specified in a friend class template
17379 declaration. */
17380 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17381 /*is_partial=*/false, /*is_friend_decl=*/1);
17383 if (!friend_p && pushed_scope)
17384 pop_scope (pushed_scope);
17386 if (function_declarator_p (declarator)
17387 && parser->fully_implicit_function_template_p)
17389 if (member_p)
17390 decl = finish_fully_implicit_template (parser, decl);
17391 else
17392 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17395 return decl;
17398 /* Parse a declarator.
17400 declarator:
17401 direct-declarator
17402 ptr-operator declarator
17404 abstract-declarator:
17405 ptr-operator abstract-declarator [opt]
17406 direct-abstract-declarator
17408 GNU Extensions:
17410 declarator:
17411 attributes [opt] direct-declarator
17412 attributes [opt] ptr-operator declarator
17414 abstract-declarator:
17415 attributes [opt] ptr-operator abstract-declarator [opt]
17416 attributes [opt] direct-abstract-declarator
17418 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17419 detect constructor, destructor or conversion operators. It is set
17420 to -1 if the declarator is a name, and +1 if it is a
17421 function. Otherwise it is set to zero. Usually you just want to
17422 test for >0, but internally the negative value is used.
17424 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17425 a decl-specifier-seq unless it declares a constructor, destructor,
17426 or conversion. It might seem that we could check this condition in
17427 semantic analysis, rather than parsing, but that makes it difficult
17428 to handle something like `f()'. We want to notice that there are
17429 no decl-specifiers, and therefore realize that this is an
17430 expression, not a declaration.)
17432 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17433 the declarator is a direct-declarator of the form "(...)".
17435 MEMBER_P is true iff this declarator is a member-declarator.
17437 FRIEND_P is true iff this declarator is a friend. */
17439 static cp_declarator *
17440 cp_parser_declarator (cp_parser* parser,
17441 cp_parser_declarator_kind dcl_kind,
17442 int* ctor_dtor_or_conv_p,
17443 bool* parenthesized_p,
17444 bool member_p, bool friend_p)
17446 cp_declarator *declarator;
17447 enum tree_code code;
17448 cp_cv_quals cv_quals;
17449 tree class_type;
17450 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17452 /* Assume this is not a constructor, destructor, or type-conversion
17453 operator. */
17454 if (ctor_dtor_or_conv_p)
17455 *ctor_dtor_or_conv_p = 0;
17457 if (cp_parser_allow_gnu_extensions_p (parser))
17458 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17460 /* Check for the ptr-operator production. */
17461 cp_parser_parse_tentatively (parser);
17462 /* Parse the ptr-operator. */
17463 code = cp_parser_ptr_operator (parser,
17464 &class_type,
17465 &cv_quals,
17466 &std_attributes);
17468 /* If that worked, then we have a ptr-operator. */
17469 if (cp_parser_parse_definitely (parser))
17471 /* If a ptr-operator was found, then this declarator was not
17472 parenthesized. */
17473 if (parenthesized_p)
17474 *parenthesized_p = true;
17475 /* The dependent declarator is optional if we are parsing an
17476 abstract-declarator. */
17477 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17478 cp_parser_parse_tentatively (parser);
17480 /* Parse the dependent declarator. */
17481 declarator = cp_parser_declarator (parser, dcl_kind,
17482 /*ctor_dtor_or_conv_p=*/NULL,
17483 /*parenthesized_p=*/NULL,
17484 /*member_p=*/false,
17485 friend_p);
17487 /* If we are parsing an abstract-declarator, we must handle the
17488 case where the dependent declarator is absent. */
17489 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17490 && !cp_parser_parse_definitely (parser))
17491 declarator = NULL;
17493 declarator = cp_parser_make_indirect_declarator
17494 (code, class_type, cv_quals, declarator, std_attributes);
17496 /* Everything else is a direct-declarator. */
17497 else
17499 if (parenthesized_p)
17500 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17501 CPP_OPEN_PAREN);
17502 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17503 ctor_dtor_or_conv_p,
17504 member_p, friend_p);
17507 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17508 declarator->attributes = gnu_attributes;
17509 return declarator;
17512 /* Parse a direct-declarator or direct-abstract-declarator.
17514 direct-declarator:
17515 declarator-id
17516 direct-declarator ( parameter-declaration-clause )
17517 cv-qualifier-seq [opt]
17518 ref-qualifier [opt]
17519 exception-specification [opt]
17520 direct-declarator [ constant-expression [opt] ]
17521 ( declarator )
17523 direct-abstract-declarator:
17524 direct-abstract-declarator [opt]
17525 ( parameter-declaration-clause )
17526 cv-qualifier-seq [opt]
17527 ref-qualifier [opt]
17528 exception-specification [opt]
17529 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17530 ( abstract-declarator )
17532 Returns a representation of the declarator. DCL_KIND is
17533 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17534 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17535 we are parsing a direct-declarator. It is
17536 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17537 of ambiguity we prefer an abstract declarator, as per
17538 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17539 as for cp_parser_declarator. */
17541 static cp_declarator *
17542 cp_parser_direct_declarator (cp_parser* parser,
17543 cp_parser_declarator_kind dcl_kind,
17544 int* ctor_dtor_or_conv_p,
17545 bool member_p, bool friend_p)
17547 cp_token *token;
17548 cp_declarator *declarator = NULL;
17549 tree scope = NULL_TREE;
17550 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17551 bool saved_in_declarator_p = parser->in_declarator_p;
17552 bool first = true;
17553 tree pushed_scope = NULL_TREE;
17555 while (true)
17557 /* Peek at the next token. */
17558 token = cp_lexer_peek_token (parser->lexer);
17559 if (token->type == CPP_OPEN_PAREN)
17561 /* This is either a parameter-declaration-clause, or a
17562 parenthesized declarator. When we know we are parsing a
17563 named declarator, it must be a parenthesized declarator
17564 if FIRST is true. For instance, `(int)' is a
17565 parameter-declaration-clause, with an omitted
17566 direct-abstract-declarator. But `((*))', is a
17567 parenthesized abstract declarator. Finally, when T is a
17568 template parameter `(T)' is a
17569 parameter-declaration-clause, and not a parenthesized
17570 named declarator.
17572 We first try and parse a parameter-declaration-clause,
17573 and then try a nested declarator (if FIRST is true).
17575 It is not an error for it not to be a
17576 parameter-declaration-clause, even when FIRST is
17577 false. Consider,
17579 int i (int);
17580 int i (3);
17582 The first is the declaration of a function while the
17583 second is the definition of a variable, including its
17584 initializer.
17586 Having seen only the parenthesis, we cannot know which of
17587 these two alternatives should be selected. Even more
17588 complex are examples like:
17590 int i (int (a));
17591 int i (int (3));
17593 The former is a function-declaration; the latter is a
17594 variable initialization.
17596 Thus again, we try a parameter-declaration-clause, and if
17597 that fails, we back out and return. */
17599 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17601 tree params;
17602 bool is_declarator = false;
17604 /* In a member-declarator, the only valid interpretation
17605 of a parenthesis is the start of a
17606 parameter-declaration-clause. (It is invalid to
17607 initialize a static data member with a parenthesized
17608 initializer; only the "=" form of initialization is
17609 permitted.) */
17610 if (!member_p)
17611 cp_parser_parse_tentatively (parser);
17613 /* Consume the `('. */
17614 cp_lexer_consume_token (parser->lexer);
17615 if (first)
17617 /* If this is going to be an abstract declarator, we're
17618 in a declarator and we can't have default args. */
17619 parser->default_arg_ok_p = false;
17620 parser->in_declarator_p = true;
17623 begin_scope (sk_function_parms, NULL_TREE);
17625 /* Parse the parameter-declaration-clause. */
17626 params = cp_parser_parameter_declaration_clause (parser);
17628 /* Consume the `)'. */
17629 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17631 /* If all went well, parse the cv-qualifier-seq,
17632 ref-qualifier and the exception-specification. */
17633 if (member_p || cp_parser_parse_definitely (parser))
17635 cp_cv_quals cv_quals;
17636 cp_virt_specifiers virt_specifiers;
17637 cp_ref_qualifier ref_qual;
17638 tree exception_specification;
17639 tree late_return;
17640 tree attrs;
17641 bool memfn = (member_p || (pushed_scope
17642 && CLASS_TYPE_P (pushed_scope)));
17644 is_declarator = true;
17646 if (ctor_dtor_or_conv_p)
17647 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17648 first = false;
17650 /* Parse the cv-qualifier-seq. */
17651 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17652 /* Parse the ref-qualifier. */
17653 ref_qual = cp_parser_ref_qualifier_opt (parser);
17654 /* And the exception-specification. */
17655 exception_specification
17656 = cp_parser_exception_specification_opt (parser);
17658 attrs = cp_parser_std_attribute_spec_seq (parser);
17660 /* In here, we handle cases where attribute is used after
17661 the function declaration. For example:
17662 void func (int x) __attribute__((vector(..))); */
17663 if (flag_cilkplus
17664 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17666 cp_parser_parse_tentatively (parser);
17667 tree attr = cp_parser_gnu_attributes_opt (parser);
17668 if (cp_lexer_next_token_is_not (parser->lexer,
17669 CPP_SEMICOLON)
17670 && cp_lexer_next_token_is_not (parser->lexer,
17671 CPP_OPEN_BRACE))
17672 cp_parser_abort_tentative_parse (parser);
17673 else if (!cp_parser_parse_definitely (parser))
17675 else
17676 attrs = chainon (attr, attrs);
17678 late_return = (cp_parser_late_return_type_opt
17679 (parser, declarator,
17680 memfn ? cv_quals : -1));
17683 /* Parse the virt-specifier-seq. */
17684 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17686 /* Create the function-declarator. */
17687 declarator = make_call_declarator (declarator,
17688 params,
17689 cv_quals,
17690 virt_specifiers,
17691 ref_qual,
17692 exception_specification,
17693 late_return);
17694 declarator->std_attributes = attrs;
17695 /* Any subsequent parameter lists are to do with
17696 return type, so are not those of the declared
17697 function. */
17698 parser->default_arg_ok_p = false;
17701 /* Remove the function parms from scope. */
17702 pop_bindings_and_leave_scope ();
17704 if (is_declarator)
17705 /* Repeat the main loop. */
17706 continue;
17709 /* If this is the first, we can try a parenthesized
17710 declarator. */
17711 if (first)
17713 bool saved_in_type_id_in_expr_p;
17715 parser->default_arg_ok_p = saved_default_arg_ok_p;
17716 parser->in_declarator_p = saved_in_declarator_p;
17718 /* Consume the `('. */
17719 cp_lexer_consume_token (parser->lexer);
17720 /* Parse the nested declarator. */
17721 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17722 parser->in_type_id_in_expr_p = true;
17723 declarator
17724 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17725 /*parenthesized_p=*/NULL,
17726 member_p, friend_p);
17727 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17728 first = false;
17729 /* Expect a `)'. */
17730 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17731 declarator = cp_error_declarator;
17732 if (declarator == cp_error_declarator)
17733 break;
17735 goto handle_declarator;
17737 /* Otherwise, we must be done. */
17738 else
17739 break;
17741 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17742 && token->type == CPP_OPEN_SQUARE
17743 && !cp_next_tokens_can_be_attribute_p (parser))
17745 /* Parse an array-declarator. */
17746 tree bounds, attrs;
17748 if (ctor_dtor_or_conv_p)
17749 *ctor_dtor_or_conv_p = 0;
17751 first = false;
17752 parser->default_arg_ok_p = false;
17753 parser->in_declarator_p = true;
17754 /* Consume the `['. */
17755 cp_lexer_consume_token (parser->lexer);
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 /* If the next token is `]', then there is no
17759 constant-expression. */
17760 if (token->type != CPP_CLOSE_SQUARE)
17762 bool non_constant_p;
17764 bounds
17765 = cp_parser_constant_expression (parser,
17766 /*allow_non_constant=*/true,
17767 &non_constant_p);
17768 if (!non_constant_p)
17769 /* OK */;
17770 else if (error_operand_p (bounds))
17771 /* Already gave an error. */;
17772 else if (!parser->in_function_body
17773 || current_binding_level->kind == sk_function_parms)
17775 /* Normally, the array bound must be an integral constant
17776 expression. However, as an extension, we allow VLAs
17777 in function scopes as long as they aren't part of a
17778 parameter declaration. */
17779 cp_parser_error (parser,
17780 "array bound is not an integer constant");
17781 bounds = error_mark_node;
17783 else if (processing_template_decl
17784 && !type_dependent_expression_p (bounds))
17786 /* Remember this wasn't a constant-expression. */
17787 bounds = build_nop (TREE_TYPE (bounds), bounds);
17788 TREE_SIDE_EFFECTS (bounds) = 1;
17791 else
17792 bounds = NULL_TREE;
17793 /* Look for the closing `]'. */
17794 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17796 declarator = cp_error_declarator;
17797 break;
17800 attrs = cp_parser_std_attribute_spec_seq (parser);
17801 declarator = make_array_declarator (declarator, bounds);
17802 declarator->std_attributes = attrs;
17804 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17807 tree qualifying_scope;
17808 tree unqualified_name;
17809 tree attrs;
17810 special_function_kind sfk;
17811 bool abstract_ok;
17812 bool pack_expansion_p = false;
17813 cp_token *declarator_id_start_token;
17815 /* Parse a declarator-id */
17816 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17817 if (abstract_ok)
17819 cp_parser_parse_tentatively (parser);
17821 /* If we see an ellipsis, we should be looking at a
17822 parameter pack. */
17823 if (token->type == CPP_ELLIPSIS)
17825 /* Consume the `...' */
17826 cp_lexer_consume_token (parser->lexer);
17828 pack_expansion_p = true;
17832 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17833 unqualified_name
17834 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17835 qualifying_scope = parser->scope;
17836 if (abstract_ok)
17838 bool okay = false;
17840 if (!unqualified_name && pack_expansion_p)
17842 /* Check whether an error occurred. */
17843 okay = !cp_parser_error_occurred (parser);
17845 /* We already consumed the ellipsis to mark a
17846 parameter pack, but we have no way to report it,
17847 so abort the tentative parse. We will be exiting
17848 immediately anyway. */
17849 cp_parser_abort_tentative_parse (parser);
17851 else
17852 okay = cp_parser_parse_definitely (parser);
17854 if (!okay)
17855 unqualified_name = error_mark_node;
17856 else if (unqualified_name
17857 && (qualifying_scope
17858 || (!identifier_p (unqualified_name))))
17860 cp_parser_error (parser, "expected unqualified-id");
17861 unqualified_name = error_mark_node;
17865 if (!unqualified_name)
17866 return NULL;
17867 if (unqualified_name == error_mark_node)
17869 declarator = cp_error_declarator;
17870 pack_expansion_p = false;
17871 declarator->parameter_pack_p = false;
17872 break;
17875 attrs = cp_parser_std_attribute_spec_seq (parser);
17877 if (qualifying_scope && at_namespace_scope_p ()
17878 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17880 /* In the declaration of a member of a template class
17881 outside of the class itself, the SCOPE will sometimes
17882 be a TYPENAME_TYPE. For example, given:
17884 template <typename T>
17885 int S<T>::R::i = 3;
17887 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17888 this context, we must resolve S<T>::R to an ordinary
17889 type, rather than a typename type.
17891 The reason we normally avoid resolving TYPENAME_TYPEs
17892 is that a specialization of `S' might render
17893 `S<T>::R' not a type. However, if `S' is
17894 specialized, then this `i' will not be used, so there
17895 is no harm in resolving the types here. */
17896 tree type;
17898 /* Resolve the TYPENAME_TYPE. */
17899 type = resolve_typename_type (qualifying_scope,
17900 /*only_current_p=*/false);
17901 /* If that failed, the declarator is invalid. */
17902 if (TREE_CODE (type) == TYPENAME_TYPE)
17904 if (typedef_variant_p (type))
17905 error_at (declarator_id_start_token->location,
17906 "cannot define member of dependent typedef "
17907 "%qT", type);
17908 else
17909 error_at (declarator_id_start_token->location,
17910 "%<%T::%E%> is not a type",
17911 TYPE_CONTEXT (qualifying_scope),
17912 TYPE_IDENTIFIER (qualifying_scope));
17914 qualifying_scope = type;
17917 sfk = sfk_none;
17919 if (unqualified_name)
17921 tree class_type;
17923 if (qualifying_scope
17924 && CLASS_TYPE_P (qualifying_scope))
17925 class_type = qualifying_scope;
17926 else
17927 class_type = current_class_type;
17929 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17931 tree name_type = TREE_TYPE (unqualified_name);
17932 if (class_type && same_type_p (name_type, class_type))
17934 if (qualifying_scope
17935 && CLASSTYPE_USE_TEMPLATE (name_type))
17937 error_at (declarator_id_start_token->location,
17938 "invalid use of constructor as a template");
17939 inform (declarator_id_start_token->location,
17940 "use %<%T::%D%> instead of %<%T::%D%> to "
17941 "name the constructor in a qualified name",
17942 class_type,
17943 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17944 class_type, name_type);
17945 declarator = cp_error_declarator;
17946 break;
17948 else
17949 unqualified_name = constructor_name (class_type);
17951 else
17953 /* We do not attempt to print the declarator
17954 here because we do not have enough
17955 information about its original syntactic
17956 form. */
17957 cp_parser_error (parser, "invalid declarator");
17958 declarator = cp_error_declarator;
17959 break;
17963 if (class_type)
17965 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17966 sfk = sfk_destructor;
17967 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17968 sfk = sfk_conversion;
17969 else if (/* There's no way to declare a constructor
17970 for an anonymous type, even if the type
17971 got a name for linkage purposes. */
17972 !TYPE_WAS_ANONYMOUS (class_type)
17973 /* Handle correctly (c++/19200):
17975 struct S {
17976 struct T{};
17977 friend void S(T);
17980 and also:
17982 namespace N {
17983 void S();
17986 struct S {
17987 friend void N::S();
17988 }; */
17989 && !(friend_p
17990 && class_type != qualifying_scope)
17991 && constructor_name_p (unqualified_name,
17992 class_type))
17994 unqualified_name = constructor_name (class_type);
17995 sfk = sfk_constructor;
17997 else if (is_overloaded_fn (unqualified_name)
17998 && DECL_CONSTRUCTOR_P (get_first_fn
17999 (unqualified_name)))
18000 sfk = sfk_constructor;
18002 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18003 *ctor_dtor_or_conv_p = -1;
18006 declarator = make_id_declarator (qualifying_scope,
18007 unqualified_name,
18008 sfk);
18009 declarator->std_attributes = attrs;
18010 declarator->id_loc = token->location;
18011 declarator->parameter_pack_p = pack_expansion_p;
18013 if (pack_expansion_p)
18014 maybe_warn_variadic_templates ();
18017 handle_declarator:;
18018 scope = get_scope_of_declarator (declarator);
18019 if (scope)
18021 /* Any names that appear after the declarator-id for a
18022 member are looked up in the containing scope. */
18023 if (at_function_scope_p ())
18025 /* But declarations with qualified-ids can't appear in a
18026 function. */
18027 cp_parser_error (parser, "qualified-id in declaration");
18028 declarator = cp_error_declarator;
18029 break;
18031 pushed_scope = push_scope (scope);
18033 parser->in_declarator_p = true;
18034 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18035 || (declarator && declarator->kind == cdk_id))
18036 /* Default args are only allowed on function
18037 declarations. */
18038 parser->default_arg_ok_p = saved_default_arg_ok_p;
18039 else
18040 parser->default_arg_ok_p = false;
18042 first = false;
18044 /* We're done. */
18045 else
18046 break;
18049 /* For an abstract declarator, we might wind up with nothing at this
18050 point. That's an error; the declarator is not optional. */
18051 if (!declarator)
18052 cp_parser_error (parser, "expected declarator");
18054 /* If we entered a scope, we must exit it now. */
18055 if (pushed_scope)
18056 pop_scope (pushed_scope);
18058 parser->default_arg_ok_p = saved_default_arg_ok_p;
18059 parser->in_declarator_p = saved_in_declarator_p;
18061 return declarator;
18064 /* Parse a ptr-operator.
18066 ptr-operator:
18067 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18068 * cv-qualifier-seq [opt]
18070 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18071 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18073 GNU Extension:
18075 ptr-operator:
18076 & cv-qualifier-seq [opt]
18078 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18079 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18080 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18081 filled in with the TYPE containing the member. *CV_QUALS is
18082 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18083 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18084 Note that the tree codes returned by this function have nothing
18085 to do with the types of trees that will be eventually be created
18086 to represent the pointer or reference type being parsed. They are
18087 just constants with suggestive names. */
18088 static enum tree_code
18089 cp_parser_ptr_operator (cp_parser* parser,
18090 tree* type,
18091 cp_cv_quals *cv_quals,
18092 tree *attributes)
18094 enum tree_code code = ERROR_MARK;
18095 cp_token *token;
18096 tree attrs = NULL_TREE;
18098 /* Assume that it's not a pointer-to-member. */
18099 *type = NULL_TREE;
18100 /* And that there are no cv-qualifiers. */
18101 *cv_quals = TYPE_UNQUALIFIED;
18103 /* Peek at the next token. */
18104 token = cp_lexer_peek_token (parser->lexer);
18106 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18107 if (token->type == CPP_MULT)
18108 code = INDIRECT_REF;
18109 else if (token->type == CPP_AND)
18110 code = ADDR_EXPR;
18111 else if ((cxx_dialect != cxx98) &&
18112 token->type == CPP_AND_AND) /* C++0x only */
18113 code = NON_LVALUE_EXPR;
18115 if (code != ERROR_MARK)
18117 /* Consume the `*', `&' or `&&'. */
18118 cp_lexer_consume_token (parser->lexer);
18120 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18121 `&', if we are allowing GNU extensions. (The only qualifier
18122 that can legally appear after `&' is `restrict', but that is
18123 enforced during semantic analysis. */
18124 if (code == INDIRECT_REF
18125 || cp_parser_allow_gnu_extensions_p (parser))
18126 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18128 attrs = cp_parser_std_attribute_spec_seq (parser);
18129 if (attributes != NULL)
18130 *attributes = attrs;
18132 else
18134 /* Try the pointer-to-member case. */
18135 cp_parser_parse_tentatively (parser);
18136 /* Look for the optional `::' operator. */
18137 cp_parser_global_scope_opt (parser,
18138 /*current_scope_valid_p=*/false);
18139 /* Look for the nested-name specifier. */
18140 token = cp_lexer_peek_token (parser->lexer);
18141 cp_parser_nested_name_specifier (parser,
18142 /*typename_keyword_p=*/false,
18143 /*check_dependency_p=*/true,
18144 /*type_p=*/false,
18145 /*is_declaration=*/false);
18146 /* If we found it, and the next token is a `*', then we are
18147 indeed looking at a pointer-to-member operator. */
18148 if (!cp_parser_error_occurred (parser)
18149 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18151 /* Indicate that the `*' operator was used. */
18152 code = INDIRECT_REF;
18154 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18155 error_at (token->location, "%qD is a namespace", parser->scope);
18156 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18157 error_at (token->location, "cannot form pointer to member of "
18158 "non-class %q#T", parser->scope);
18159 else
18161 /* The type of which the member is a member is given by the
18162 current SCOPE. */
18163 *type = parser->scope;
18164 /* The next name will not be qualified. */
18165 parser->scope = NULL_TREE;
18166 parser->qualifying_scope = NULL_TREE;
18167 parser->object_scope = NULL_TREE;
18168 /* Look for optional c++11 attributes. */
18169 attrs = cp_parser_std_attribute_spec_seq (parser);
18170 if (attributes != NULL)
18171 *attributes = attrs;
18172 /* Look for the optional cv-qualifier-seq. */
18173 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18176 /* If that didn't work we don't have a ptr-operator. */
18177 if (!cp_parser_parse_definitely (parser))
18178 cp_parser_error (parser, "expected ptr-operator");
18181 return code;
18184 /* Parse an (optional) cv-qualifier-seq.
18186 cv-qualifier-seq:
18187 cv-qualifier cv-qualifier-seq [opt]
18189 cv-qualifier:
18190 const
18191 volatile
18193 GNU Extension:
18195 cv-qualifier:
18196 __restrict__
18198 Returns a bitmask representing the cv-qualifiers. */
18200 static cp_cv_quals
18201 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18203 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18205 while (true)
18207 cp_token *token;
18208 cp_cv_quals cv_qualifier;
18210 /* Peek at the next token. */
18211 token = cp_lexer_peek_token (parser->lexer);
18212 /* See if it's a cv-qualifier. */
18213 switch (token->keyword)
18215 case RID_CONST:
18216 cv_qualifier = TYPE_QUAL_CONST;
18217 break;
18219 case RID_VOLATILE:
18220 cv_qualifier = TYPE_QUAL_VOLATILE;
18221 break;
18223 case RID_RESTRICT:
18224 cv_qualifier = TYPE_QUAL_RESTRICT;
18225 break;
18227 default:
18228 cv_qualifier = TYPE_UNQUALIFIED;
18229 break;
18232 if (!cv_qualifier)
18233 break;
18235 if (cv_quals & cv_qualifier)
18237 error_at (token->location, "duplicate cv-qualifier");
18238 cp_lexer_purge_token (parser->lexer);
18240 else
18242 cp_lexer_consume_token (parser->lexer);
18243 cv_quals |= cv_qualifier;
18247 return cv_quals;
18250 /* Parse an (optional) ref-qualifier
18252 ref-qualifier:
18256 Returns cp_ref_qualifier representing ref-qualifier. */
18258 static cp_ref_qualifier
18259 cp_parser_ref_qualifier_opt (cp_parser* parser)
18261 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18263 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18264 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18265 return ref_qual;
18267 while (true)
18269 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18270 cp_token *token = cp_lexer_peek_token (parser->lexer);
18272 switch (token->type)
18274 case CPP_AND:
18275 curr_ref_qual = REF_QUAL_LVALUE;
18276 break;
18278 case CPP_AND_AND:
18279 curr_ref_qual = REF_QUAL_RVALUE;
18280 break;
18282 default:
18283 curr_ref_qual = REF_QUAL_NONE;
18284 break;
18287 if (!curr_ref_qual)
18288 break;
18289 else if (ref_qual)
18291 error_at (token->location, "multiple ref-qualifiers");
18292 cp_lexer_purge_token (parser->lexer);
18294 else
18296 ref_qual = curr_ref_qual;
18297 cp_lexer_consume_token (parser->lexer);
18301 return ref_qual;
18304 /* Parse an (optional) virt-specifier-seq.
18306 virt-specifier-seq:
18307 virt-specifier virt-specifier-seq [opt]
18309 virt-specifier:
18310 override
18311 final
18313 Returns a bitmask representing the virt-specifiers. */
18315 static cp_virt_specifiers
18316 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18318 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18320 while (true)
18322 cp_token *token;
18323 cp_virt_specifiers virt_specifier;
18325 /* Peek at the next token. */
18326 token = cp_lexer_peek_token (parser->lexer);
18327 /* See if it's a virt-specifier-qualifier. */
18328 if (token->type != CPP_NAME)
18329 break;
18330 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18332 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18333 virt_specifier = VIRT_SPEC_OVERRIDE;
18335 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18337 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18338 virt_specifier = VIRT_SPEC_FINAL;
18340 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18342 virt_specifier = VIRT_SPEC_FINAL;
18344 else
18345 break;
18347 if (virt_specifiers & virt_specifier)
18349 error_at (token->location, "duplicate virt-specifier");
18350 cp_lexer_purge_token (parser->lexer);
18352 else
18354 cp_lexer_consume_token (parser->lexer);
18355 virt_specifiers |= virt_specifier;
18358 return virt_specifiers;
18361 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18362 is in scope even though it isn't real. */
18364 void
18365 inject_this_parameter (tree ctype, cp_cv_quals quals)
18367 tree this_parm;
18369 if (current_class_ptr)
18371 /* We don't clear this between NSDMIs. Is it already what we want? */
18372 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18373 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18374 && cp_type_quals (type) == quals)
18375 return;
18378 this_parm = build_this_parm (ctype, quals);
18379 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18380 current_class_ptr = NULL_TREE;
18381 current_class_ref
18382 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18383 current_class_ptr = this_parm;
18386 /* Return true iff our current scope is a non-static data member
18387 initializer. */
18389 bool
18390 parsing_nsdmi (void)
18392 /* We recognize NSDMI context by the context-less 'this' pointer set up
18393 by the function above. */
18394 if (current_class_ptr
18395 && TREE_CODE (current_class_ptr) == PARM_DECL
18396 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18397 return true;
18398 return false;
18401 /* Parse a late-specified return type, if any. This is not a separate
18402 non-terminal, but part of a function declarator, which looks like
18404 -> trailing-type-specifier-seq abstract-declarator(opt)
18406 Returns the type indicated by the type-id.
18408 In addition to this this parses any queued up omp declare simd
18409 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18411 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18412 function. */
18414 static tree
18415 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18416 cp_cv_quals quals)
18418 cp_token *token;
18419 tree type = NULL_TREE;
18420 bool declare_simd_p = (parser->omp_declare_simd
18421 && declarator
18422 && declarator->kind == cdk_id);
18424 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18425 && declarator && declarator->kind == cdk_id);
18427 /* Peek at the next token. */
18428 token = cp_lexer_peek_token (parser->lexer);
18429 /* A late-specified return type is indicated by an initial '->'. */
18430 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18431 return NULL_TREE;
18433 tree save_ccp = current_class_ptr;
18434 tree save_ccr = current_class_ref;
18435 if (quals >= 0)
18437 /* DR 1207: 'this' is in scope in the trailing return type. */
18438 inject_this_parameter (current_class_type, quals);
18441 if (token->type == CPP_DEREF)
18443 /* Consume the ->. */
18444 cp_lexer_consume_token (parser->lexer);
18446 type = cp_parser_trailing_type_id (parser);
18449 if (cilk_simd_fn_vector_p)
18450 declarator->std_attributes
18451 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18452 declarator->std_attributes);
18453 if (declare_simd_p)
18454 declarator->std_attributes
18455 = cp_parser_late_parsing_omp_declare_simd (parser,
18456 declarator->std_attributes);
18458 if (quals >= 0)
18460 current_class_ptr = save_ccp;
18461 current_class_ref = save_ccr;
18464 return type;
18467 /* Parse a declarator-id.
18469 declarator-id:
18470 id-expression
18471 :: [opt] nested-name-specifier [opt] type-name
18473 In the `id-expression' case, the value returned is as for
18474 cp_parser_id_expression if the id-expression was an unqualified-id.
18475 If the id-expression was a qualified-id, then a SCOPE_REF is
18476 returned. The first operand is the scope (either a NAMESPACE_DECL
18477 or TREE_TYPE), but the second is still just a representation of an
18478 unqualified-id. */
18480 static tree
18481 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18483 tree id;
18484 /* The expression must be an id-expression. Assume that qualified
18485 names are the names of types so that:
18487 template <class T>
18488 int S<T>::R::i = 3;
18490 will work; we must treat `S<T>::R' as the name of a type.
18491 Similarly, assume that qualified names are templates, where
18492 required, so that:
18494 template <class T>
18495 int S<T>::R<T>::i = 3;
18497 will work, too. */
18498 id = cp_parser_id_expression (parser,
18499 /*template_keyword_p=*/false,
18500 /*check_dependency_p=*/false,
18501 /*template_p=*/NULL,
18502 /*declarator_p=*/true,
18503 optional_p);
18504 if (id && BASELINK_P (id))
18505 id = BASELINK_FUNCTIONS (id);
18506 return id;
18509 /* Parse a type-id.
18511 type-id:
18512 type-specifier-seq abstract-declarator [opt]
18514 Returns the TYPE specified. */
18516 static tree
18517 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18518 bool is_trailing_return)
18520 cp_decl_specifier_seq type_specifier_seq;
18521 cp_declarator *abstract_declarator;
18523 /* Parse the type-specifier-seq. */
18524 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18525 is_trailing_return,
18526 &type_specifier_seq);
18527 if (type_specifier_seq.type == error_mark_node)
18528 return error_mark_node;
18530 /* There might or might not be an abstract declarator. */
18531 cp_parser_parse_tentatively (parser);
18532 /* Look for the declarator. */
18533 abstract_declarator
18534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18535 /*parenthesized_p=*/NULL,
18536 /*member_p=*/false,
18537 /*friend_p=*/false);
18538 /* Check to see if there really was a declarator. */
18539 if (!cp_parser_parse_definitely (parser))
18540 abstract_declarator = NULL;
18542 if (type_specifier_seq.type
18543 /* None of the valid uses of 'auto' in C++14 involve the type-id
18544 nonterminal, but it is valid in a trailing-return-type. */
18545 && !(cxx_dialect >= cxx14 && is_trailing_return)
18546 && type_uses_auto (type_specifier_seq.type))
18548 /* A type-id with type 'auto' is only ok if the abstract declarator
18549 is a function declarator with a late-specified return type. */
18550 if (abstract_declarator
18551 && abstract_declarator->kind == cdk_function
18552 && abstract_declarator->u.function.late_return_type)
18553 /* OK */;
18554 else
18556 error ("invalid use of %<auto%>");
18557 return error_mark_node;
18561 return groktypename (&type_specifier_seq, abstract_declarator,
18562 is_template_arg);
18565 static tree cp_parser_type_id (cp_parser *parser)
18567 return cp_parser_type_id_1 (parser, false, false);
18570 static tree cp_parser_template_type_arg (cp_parser *parser)
18572 tree r;
18573 const char *saved_message = parser->type_definition_forbidden_message;
18574 parser->type_definition_forbidden_message
18575 = G_("types may not be defined in template arguments");
18576 r = cp_parser_type_id_1 (parser, true, false);
18577 parser->type_definition_forbidden_message = saved_message;
18578 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18580 error ("invalid use of %<auto%> in template argument");
18581 r = error_mark_node;
18583 return r;
18586 static tree cp_parser_trailing_type_id (cp_parser *parser)
18588 return cp_parser_type_id_1 (parser, false, true);
18591 /* Parse a type-specifier-seq.
18593 type-specifier-seq:
18594 type-specifier type-specifier-seq [opt]
18596 GNU extension:
18598 type-specifier-seq:
18599 attributes type-specifier-seq [opt]
18601 If IS_DECLARATION is true, we are at the start of a "condition" or
18602 exception-declaration, so we might be followed by a declarator-id.
18604 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18605 i.e. we've just seen "->".
18607 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18609 static void
18610 cp_parser_type_specifier_seq (cp_parser* parser,
18611 bool is_declaration,
18612 bool is_trailing_return,
18613 cp_decl_specifier_seq *type_specifier_seq)
18615 bool seen_type_specifier = false;
18616 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18617 cp_token *start_token = NULL;
18619 /* Clear the TYPE_SPECIFIER_SEQ. */
18620 clear_decl_specs (type_specifier_seq);
18622 /* In the context of a trailing return type, enum E { } is an
18623 elaborated-type-specifier followed by a function-body, not an
18624 enum-specifier. */
18625 if (is_trailing_return)
18626 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18628 /* Parse the type-specifiers and attributes. */
18629 while (true)
18631 tree type_specifier;
18632 bool is_cv_qualifier;
18634 /* Check for attributes first. */
18635 if (cp_next_tokens_can_be_attribute_p (parser))
18637 type_specifier_seq->attributes =
18638 chainon (type_specifier_seq->attributes,
18639 cp_parser_attributes_opt (parser));
18640 continue;
18643 /* record the token of the beginning of the type specifier seq,
18644 for error reporting purposes*/
18645 if (!start_token)
18646 start_token = cp_lexer_peek_token (parser->lexer);
18648 /* Look for the type-specifier. */
18649 type_specifier = cp_parser_type_specifier (parser,
18650 flags,
18651 type_specifier_seq,
18652 /*is_declaration=*/false,
18653 NULL,
18654 &is_cv_qualifier);
18655 if (!type_specifier)
18657 /* If the first type-specifier could not be found, this is not a
18658 type-specifier-seq at all. */
18659 if (!seen_type_specifier)
18661 /* Set in_declarator_p to avoid skipping to the semicolon. */
18662 int in_decl = parser->in_declarator_p;
18663 parser->in_declarator_p = true;
18665 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18666 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18667 cp_parser_error (parser, "expected type-specifier");
18669 parser->in_declarator_p = in_decl;
18671 type_specifier_seq->type = error_mark_node;
18672 return;
18674 /* If subsequent type-specifiers could not be found, the
18675 type-specifier-seq is complete. */
18676 break;
18679 seen_type_specifier = true;
18680 /* The standard says that a condition can be:
18682 type-specifier-seq declarator = assignment-expression
18684 However, given:
18686 struct S {};
18687 if (int S = ...)
18689 we should treat the "S" as a declarator, not as a
18690 type-specifier. The standard doesn't say that explicitly for
18691 type-specifier-seq, but it does say that for
18692 decl-specifier-seq in an ordinary declaration. Perhaps it
18693 would be clearer just to allow a decl-specifier-seq here, and
18694 then add a semantic restriction that if any decl-specifiers
18695 that are not type-specifiers appear, the program is invalid. */
18696 if (is_declaration && !is_cv_qualifier)
18697 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18701 /* Return whether the function currently being declared has an associated
18702 template parameter list. */
18704 static bool
18705 function_being_declared_is_template_p (cp_parser* parser)
18707 if (!current_template_parms || processing_template_parmlist)
18708 return false;
18710 if (parser->implicit_template_scope)
18711 return true;
18713 if (at_class_scope_p ()
18714 && TYPE_BEING_DEFINED (current_class_type))
18715 return parser->num_template_parameter_lists != 0;
18717 return ((int) parser->num_template_parameter_lists > template_class_depth
18718 (current_class_type));
18721 /* Parse a parameter-declaration-clause.
18723 parameter-declaration-clause:
18724 parameter-declaration-list [opt] ... [opt]
18725 parameter-declaration-list , ...
18727 Returns a representation for the parameter declarations. A return
18728 value of NULL indicates a parameter-declaration-clause consisting
18729 only of an ellipsis. */
18731 static tree
18732 cp_parser_parameter_declaration_clause (cp_parser* parser)
18734 tree parameters;
18735 cp_token *token;
18736 bool ellipsis_p;
18737 bool is_error;
18739 struct cleanup {
18740 cp_parser* parser;
18741 int auto_is_implicit_function_template_parm_p;
18742 ~cleanup() {
18743 parser->auto_is_implicit_function_template_parm_p
18744 = auto_is_implicit_function_template_parm_p;
18746 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18748 (void) cleanup;
18750 if (!processing_specialization
18751 && !processing_template_parmlist
18752 && !processing_explicit_instantiation)
18753 if (!current_function_decl
18754 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18755 parser->auto_is_implicit_function_template_parm_p = true;
18757 /* Peek at the next token. */
18758 token = cp_lexer_peek_token (parser->lexer);
18759 /* Check for trivial parameter-declaration-clauses. */
18760 if (token->type == CPP_ELLIPSIS)
18762 /* Consume the `...' token. */
18763 cp_lexer_consume_token (parser->lexer);
18764 return NULL_TREE;
18766 else if (token->type == CPP_CLOSE_PAREN)
18767 /* There are no parameters. */
18769 #ifndef NO_IMPLICIT_EXTERN_C
18770 if (in_system_header_at (input_location)
18771 && current_class_type == NULL
18772 && current_lang_name == lang_name_c)
18773 return NULL_TREE;
18774 else
18775 #endif
18776 return void_list_node;
18778 /* Check for `(void)', too, which is a special case. */
18779 else if (token->keyword == RID_VOID
18780 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18781 == CPP_CLOSE_PAREN))
18783 /* Consume the `void' token. */
18784 cp_lexer_consume_token (parser->lexer);
18785 /* There are no parameters. */
18786 return void_list_node;
18789 /* Parse the parameter-declaration-list. */
18790 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18791 /* If a parse error occurred while parsing the
18792 parameter-declaration-list, then the entire
18793 parameter-declaration-clause is erroneous. */
18794 if (is_error)
18795 return NULL;
18797 /* Peek at the next token. */
18798 token = cp_lexer_peek_token (parser->lexer);
18799 /* If it's a `,', the clause should terminate with an ellipsis. */
18800 if (token->type == CPP_COMMA)
18802 /* Consume the `,'. */
18803 cp_lexer_consume_token (parser->lexer);
18804 /* Expect an ellipsis. */
18805 ellipsis_p
18806 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18808 /* It might also be `...' if the optional trailing `,' was
18809 omitted. */
18810 else if (token->type == CPP_ELLIPSIS)
18812 /* Consume the `...' token. */
18813 cp_lexer_consume_token (parser->lexer);
18814 /* And remember that we saw it. */
18815 ellipsis_p = true;
18817 else
18818 ellipsis_p = false;
18820 /* Finish the parameter list. */
18821 if (!ellipsis_p)
18822 parameters = chainon (parameters, void_list_node);
18824 return parameters;
18827 /* Parse a parameter-declaration-list.
18829 parameter-declaration-list:
18830 parameter-declaration
18831 parameter-declaration-list , parameter-declaration
18833 Returns a representation of the parameter-declaration-list, as for
18834 cp_parser_parameter_declaration_clause. However, the
18835 `void_list_node' is never appended to the list. Upon return,
18836 *IS_ERROR will be true iff an error occurred. */
18838 static tree
18839 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18841 tree parameters = NULL_TREE;
18842 tree *tail = &parameters;
18843 bool saved_in_unbraced_linkage_specification_p;
18844 int index = 0;
18846 /* Assume all will go well. */
18847 *is_error = false;
18848 /* The special considerations that apply to a function within an
18849 unbraced linkage specifications do not apply to the parameters
18850 to the function. */
18851 saved_in_unbraced_linkage_specification_p
18852 = parser->in_unbraced_linkage_specification_p;
18853 parser->in_unbraced_linkage_specification_p = false;
18855 /* Look for more parameters. */
18856 while (true)
18858 cp_parameter_declarator *parameter;
18859 tree decl = error_mark_node;
18860 bool parenthesized_p = false;
18861 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18862 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18863 (current_template_parms)) : 0);
18865 /* Parse the parameter. */
18866 parameter
18867 = cp_parser_parameter_declaration (parser,
18868 /*template_parm_p=*/false,
18869 &parenthesized_p);
18871 /* We don't know yet if the enclosing context is deprecated, so wait
18872 and warn in grokparms if appropriate. */
18873 deprecated_state = DEPRECATED_SUPPRESS;
18875 if (parameter)
18877 /* If a function parameter pack was specified and an implicit template
18878 parameter was introduced during cp_parser_parameter_declaration,
18879 change any implicit parameters introduced into packs. */
18880 if (parser->implicit_template_parms
18881 && parameter->declarator
18882 && parameter->declarator->parameter_pack_p)
18884 int latest_template_parm_idx = TREE_VEC_LENGTH
18885 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18887 if (latest_template_parm_idx != template_parm_idx)
18888 parameter->decl_specifiers.type = convert_generic_types_to_packs
18889 (parameter->decl_specifiers.type,
18890 template_parm_idx, latest_template_parm_idx);
18893 decl = grokdeclarator (parameter->declarator,
18894 &parameter->decl_specifiers,
18895 PARM,
18896 parameter->default_argument != NULL_TREE,
18897 &parameter->decl_specifiers.attributes);
18900 deprecated_state = DEPRECATED_NORMAL;
18902 /* If a parse error occurred parsing the parameter declaration,
18903 then the entire parameter-declaration-list is erroneous. */
18904 if (decl == error_mark_node)
18906 *is_error = true;
18907 parameters = error_mark_node;
18908 break;
18911 if (parameter->decl_specifiers.attributes)
18912 cplus_decl_attributes (&decl,
18913 parameter->decl_specifiers.attributes,
18915 if (DECL_NAME (decl))
18916 decl = pushdecl (decl);
18918 if (decl != error_mark_node)
18920 retrofit_lang_decl (decl);
18921 DECL_PARM_INDEX (decl) = ++index;
18922 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18925 /* Add the new parameter to the list. */
18926 *tail = build_tree_list (parameter->default_argument, decl);
18927 tail = &TREE_CHAIN (*tail);
18929 /* Peek at the next token. */
18930 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18931 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18932 /* These are for Objective-C++ */
18933 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18934 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18935 /* The parameter-declaration-list is complete. */
18936 break;
18937 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18939 cp_token *token;
18941 /* Peek at the next token. */
18942 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18943 /* If it's an ellipsis, then the list is complete. */
18944 if (token->type == CPP_ELLIPSIS)
18945 break;
18946 /* Otherwise, there must be more parameters. Consume the
18947 `,'. */
18948 cp_lexer_consume_token (parser->lexer);
18949 /* When parsing something like:
18951 int i(float f, double d)
18953 we can tell after seeing the declaration for "f" that we
18954 are not looking at an initialization of a variable "i",
18955 but rather at the declaration of a function "i".
18957 Due to the fact that the parsing of template arguments
18958 (as specified to a template-id) requires backtracking we
18959 cannot use this technique when inside a template argument
18960 list. */
18961 if (!parser->in_template_argument_list_p
18962 && !parser->in_type_id_in_expr_p
18963 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18964 /* However, a parameter-declaration of the form
18965 "float(f)" (which is a valid declaration of a
18966 parameter "f") can also be interpreted as an
18967 expression (the conversion of "f" to "float"). */
18968 && !parenthesized_p)
18969 cp_parser_commit_to_tentative_parse (parser);
18971 else
18973 cp_parser_error (parser, "expected %<,%> or %<...%>");
18974 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18975 cp_parser_skip_to_closing_parenthesis (parser,
18976 /*recovering=*/true,
18977 /*or_comma=*/false,
18978 /*consume_paren=*/false);
18979 break;
18983 parser->in_unbraced_linkage_specification_p
18984 = saved_in_unbraced_linkage_specification_p;
18986 /* Reset implicit_template_scope if we are about to leave the function
18987 parameter list that introduced it. Note that for out-of-line member
18988 definitions, there will be one or more class scopes before we get to
18989 the template parameter scope. */
18991 if (cp_binding_level *its = parser->implicit_template_scope)
18992 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18994 while (maybe_its->kind == sk_class)
18995 maybe_its = maybe_its->level_chain;
18996 if (maybe_its == its)
18998 parser->implicit_template_parms = 0;
18999 parser->implicit_template_scope = 0;
19003 return parameters;
19006 /* Parse a parameter declaration.
19008 parameter-declaration:
19009 decl-specifier-seq ... [opt] declarator
19010 decl-specifier-seq declarator = assignment-expression
19011 decl-specifier-seq ... [opt] abstract-declarator [opt]
19012 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19014 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19015 declares a template parameter. (In that case, a non-nested `>'
19016 token encountered during the parsing of the assignment-expression
19017 is not interpreted as a greater-than operator.)
19019 Returns a representation of the parameter, or NULL if an error
19020 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19021 true iff the declarator is of the form "(p)". */
19023 static cp_parameter_declarator *
19024 cp_parser_parameter_declaration (cp_parser *parser,
19025 bool template_parm_p,
19026 bool *parenthesized_p)
19028 int declares_class_or_enum;
19029 cp_decl_specifier_seq decl_specifiers;
19030 cp_declarator *declarator;
19031 tree default_argument;
19032 cp_token *token = NULL, *declarator_token_start = NULL;
19033 const char *saved_message;
19035 /* In a template parameter, `>' is not an operator.
19037 [temp.param]
19039 When parsing a default template-argument for a non-type
19040 template-parameter, the first non-nested `>' is taken as the end
19041 of the template parameter-list rather than a greater-than
19042 operator. */
19044 /* Type definitions may not appear in parameter types. */
19045 saved_message = parser->type_definition_forbidden_message;
19046 parser->type_definition_forbidden_message
19047 = G_("types may not be defined in parameter types");
19049 /* Parse the declaration-specifiers. */
19050 cp_parser_decl_specifier_seq (parser,
19051 CP_PARSER_FLAGS_NONE,
19052 &decl_specifiers,
19053 &declares_class_or_enum);
19055 /* Complain about missing 'typename' or other invalid type names. */
19056 if (!decl_specifiers.any_type_specifiers_p
19057 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19058 decl_specifiers.type = error_mark_node;
19060 /* If an error occurred, there's no reason to attempt to parse the
19061 rest of the declaration. */
19062 if (cp_parser_error_occurred (parser))
19064 parser->type_definition_forbidden_message = saved_message;
19065 return NULL;
19068 /* Peek at the next token. */
19069 token = cp_lexer_peek_token (parser->lexer);
19071 /* If the next token is a `)', `,', `=', `>', or `...', then there
19072 is no declarator. However, when variadic templates are enabled,
19073 there may be a declarator following `...'. */
19074 if (token->type == CPP_CLOSE_PAREN
19075 || token->type == CPP_COMMA
19076 || token->type == CPP_EQ
19077 || token->type == CPP_GREATER)
19079 declarator = NULL;
19080 if (parenthesized_p)
19081 *parenthesized_p = false;
19083 /* Otherwise, there should be a declarator. */
19084 else
19086 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19087 parser->default_arg_ok_p = false;
19089 /* After seeing a decl-specifier-seq, if the next token is not a
19090 "(", there is no possibility that the code is a valid
19091 expression. Therefore, if parsing tentatively, we commit at
19092 this point. */
19093 if (!parser->in_template_argument_list_p
19094 /* In an expression context, having seen:
19096 (int((char ...
19098 we cannot be sure whether we are looking at a
19099 function-type (taking a "char" as a parameter) or a cast
19100 of some object of type "char" to "int". */
19101 && !parser->in_type_id_in_expr_p
19102 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19103 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19104 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19105 cp_parser_commit_to_tentative_parse (parser);
19106 /* Parse the declarator. */
19107 declarator_token_start = token;
19108 declarator = cp_parser_declarator (parser,
19109 CP_PARSER_DECLARATOR_EITHER,
19110 /*ctor_dtor_or_conv_p=*/NULL,
19111 parenthesized_p,
19112 /*member_p=*/false,
19113 /*friend_p=*/false);
19114 parser->default_arg_ok_p = saved_default_arg_ok_p;
19115 /* After the declarator, allow more attributes. */
19116 decl_specifiers.attributes
19117 = chainon (decl_specifiers.attributes,
19118 cp_parser_attributes_opt (parser));
19121 /* If the next token is an ellipsis, and we have not seen a
19122 declarator name, and the type of the declarator contains parameter
19123 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19124 a parameter pack expansion expression. Otherwise, leave the
19125 ellipsis for a C-style variadic function. */
19126 token = cp_lexer_peek_token (parser->lexer);
19127 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19129 tree type = decl_specifiers.type;
19131 if (type && DECL_P (type))
19132 type = TREE_TYPE (type);
19134 if (type
19135 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19136 && declarator_can_be_parameter_pack (declarator)
19137 && (!declarator || !declarator->parameter_pack_p)
19138 && uses_parameter_packs (type))
19140 /* Consume the `...'. */
19141 cp_lexer_consume_token (parser->lexer);
19142 maybe_warn_variadic_templates ();
19144 /* Build a pack expansion type */
19145 if (declarator)
19146 declarator->parameter_pack_p = true;
19147 else
19148 decl_specifiers.type = make_pack_expansion (type);
19152 /* The restriction on defining new types applies only to the type
19153 of the parameter, not to the default argument. */
19154 parser->type_definition_forbidden_message = saved_message;
19156 /* If the next token is `=', then process a default argument. */
19157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19159 token = cp_lexer_peek_token (parser->lexer);
19160 /* If we are defining a class, then the tokens that make up the
19161 default argument must be saved and processed later. */
19162 if (!template_parm_p && at_class_scope_p ()
19163 && TYPE_BEING_DEFINED (current_class_type)
19164 && !LAMBDA_TYPE_P (current_class_type))
19165 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19166 /* Outside of a class definition, we can just parse the
19167 assignment-expression. */
19168 else
19169 default_argument
19170 = cp_parser_default_argument (parser, template_parm_p);
19172 if (!parser->default_arg_ok_p)
19174 if (flag_permissive)
19175 warning (0, "deprecated use of default argument for parameter of non-function");
19176 else
19178 error_at (token->location,
19179 "default arguments are only "
19180 "permitted for function parameters");
19181 default_argument = NULL_TREE;
19184 else if ((declarator && declarator->parameter_pack_p)
19185 || (decl_specifiers.type
19186 && PACK_EXPANSION_P (decl_specifiers.type)))
19188 /* Find the name of the parameter pack. */
19189 cp_declarator *id_declarator = declarator;
19190 while (id_declarator && id_declarator->kind != cdk_id)
19191 id_declarator = id_declarator->declarator;
19193 if (id_declarator && id_declarator->kind == cdk_id)
19194 error_at (declarator_token_start->location,
19195 template_parm_p
19196 ? G_("template parameter pack %qD "
19197 "cannot have a default argument")
19198 : G_("parameter pack %qD cannot have "
19199 "a default argument"),
19200 id_declarator->u.id.unqualified_name);
19201 else
19202 error_at (declarator_token_start->location,
19203 template_parm_p
19204 ? G_("template parameter pack cannot have "
19205 "a default argument")
19206 : G_("parameter pack cannot have a "
19207 "default argument"));
19209 default_argument = NULL_TREE;
19212 else
19213 default_argument = NULL_TREE;
19215 return make_parameter_declarator (&decl_specifiers,
19216 declarator,
19217 default_argument);
19220 /* Parse a default argument and return it.
19222 TEMPLATE_PARM_P is true if this is a default argument for a
19223 non-type template parameter. */
19224 static tree
19225 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19227 tree default_argument = NULL_TREE;
19228 bool saved_greater_than_is_operator_p;
19229 bool saved_local_variables_forbidden_p;
19230 bool non_constant_p, is_direct_init;
19232 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19233 set correctly. */
19234 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19235 parser->greater_than_is_operator_p = !template_parm_p;
19236 /* Local variable names (and the `this' keyword) may not
19237 appear in a default argument. */
19238 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19239 parser->local_variables_forbidden_p = true;
19240 /* Parse the assignment-expression. */
19241 if (template_parm_p)
19242 push_deferring_access_checks (dk_no_deferred);
19243 tree saved_class_ptr = NULL_TREE;
19244 tree saved_class_ref = NULL_TREE;
19245 /* The "this" pointer is not valid in a default argument. */
19246 if (cfun)
19248 saved_class_ptr = current_class_ptr;
19249 cp_function_chain->x_current_class_ptr = NULL_TREE;
19250 saved_class_ref = current_class_ref;
19251 cp_function_chain->x_current_class_ref = NULL_TREE;
19253 default_argument
19254 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19255 /* Restore the "this" pointer. */
19256 if (cfun)
19258 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19259 cp_function_chain->x_current_class_ref = saved_class_ref;
19261 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19262 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19263 if (template_parm_p)
19264 pop_deferring_access_checks ();
19265 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19266 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19268 return default_argument;
19271 /* Parse a function-body.
19273 function-body:
19274 compound_statement */
19276 static void
19277 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19279 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19282 /* Parse a ctor-initializer-opt followed by a function-body. Return
19283 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19284 is true we are parsing a function-try-block. */
19286 static bool
19287 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19288 bool in_function_try_block)
19290 tree body, list;
19291 bool ctor_initializer_p;
19292 const bool check_body_p =
19293 DECL_CONSTRUCTOR_P (current_function_decl)
19294 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19295 tree last = NULL;
19297 /* Begin the function body. */
19298 body = begin_function_body ();
19299 /* Parse the optional ctor-initializer. */
19300 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19302 /* If we're parsing a constexpr constructor definition, we need
19303 to check that the constructor body is indeed empty. However,
19304 before we get to cp_parser_function_body lot of junk has been
19305 generated, so we can't just check that we have an empty block.
19306 Rather we take a snapshot of the outermost block, and check whether
19307 cp_parser_function_body changed its state. */
19308 if (check_body_p)
19310 list = cur_stmt_list;
19311 if (STATEMENT_LIST_TAIL (list))
19312 last = STATEMENT_LIST_TAIL (list)->stmt;
19314 /* Parse the function-body. */
19315 cp_parser_function_body (parser, in_function_try_block);
19316 if (check_body_p)
19317 check_constexpr_ctor_body (last, list, /*complain=*/true);
19318 /* Finish the function body. */
19319 finish_function_body (body);
19321 return ctor_initializer_p;
19324 /* Parse an initializer.
19326 initializer:
19327 = initializer-clause
19328 ( expression-list )
19330 Returns an expression representing the initializer. If no
19331 initializer is present, NULL_TREE is returned.
19333 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19334 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19335 set to TRUE if there is no initializer present. If there is an
19336 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19337 is set to true; otherwise it is set to false. */
19339 static tree
19340 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19341 bool* non_constant_p)
19343 cp_token *token;
19344 tree init;
19346 /* Peek at the next token. */
19347 token = cp_lexer_peek_token (parser->lexer);
19349 /* Let our caller know whether or not this initializer was
19350 parenthesized. */
19351 *is_direct_init = (token->type != CPP_EQ);
19352 /* Assume that the initializer is constant. */
19353 *non_constant_p = false;
19355 if (token->type == CPP_EQ)
19357 /* Consume the `='. */
19358 cp_lexer_consume_token (parser->lexer);
19359 /* Parse the initializer-clause. */
19360 init = cp_parser_initializer_clause (parser, non_constant_p);
19362 else if (token->type == CPP_OPEN_PAREN)
19364 vec<tree, va_gc> *vec;
19365 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19366 /*cast_p=*/false,
19367 /*allow_expansion_p=*/true,
19368 non_constant_p);
19369 if (vec == NULL)
19370 return error_mark_node;
19371 init = build_tree_list_vec (vec);
19372 release_tree_vector (vec);
19374 else if (token->type == CPP_OPEN_BRACE)
19376 cp_lexer_set_source_position (parser->lexer);
19377 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19378 init = cp_parser_braced_list (parser, non_constant_p);
19379 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19381 else
19383 /* Anything else is an error. */
19384 cp_parser_error (parser, "expected initializer");
19385 init = error_mark_node;
19388 return init;
19391 /* Parse an initializer-clause.
19393 initializer-clause:
19394 assignment-expression
19395 braced-init-list
19397 Returns an expression representing the initializer.
19399 If the `assignment-expression' production is used the value
19400 returned is simply a representation for the expression.
19402 Otherwise, calls cp_parser_braced_list. */
19404 static tree
19405 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19407 tree initializer;
19409 /* Assume the expression is constant. */
19410 *non_constant_p = false;
19412 /* If it is not a `{', then we are looking at an
19413 assignment-expression. */
19414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19416 initializer
19417 = cp_parser_constant_expression (parser,
19418 /*allow_non_constant_p=*/true,
19419 non_constant_p);
19421 else
19422 initializer = cp_parser_braced_list (parser, non_constant_p);
19424 return initializer;
19427 /* Parse a brace-enclosed initializer list.
19429 braced-init-list:
19430 { initializer-list , [opt] }
19433 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19434 the elements of the initializer-list (or NULL, if the last
19435 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19436 NULL_TREE. There is no way to detect whether or not the optional
19437 trailing `,' was provided. NON_CONSTANT_P is as for
19438 cp_parser_initializer. */
19440 static tree
19441 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19443 tree initializer;
19445 /* Consume the `{' token. */
19446 cp_lexer_consume_token (parser->lexer);
19447 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19448 initializer = make_node (CONSTRUCTOR);
19449 /* If it's not a `}', then there is a non-trivial initializer. */
19450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19452 /* Parse the initializer list. */
19453 CONSTRUCTOR_ELTS (initializer)
19454 = cp_parser_initializer_list (parser, non_constant_p);
19455 /* A trailing `,' token is allowed. */
19456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19457 cp_lexer_consume_token (parser->lexer);
19459 else
19460 *non_constant_p = false;
19461 /* Now, there should be a trailing `}'. */
19462 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19463 TREE_TYPE (initializer) = init_list_type_node;
19464 return initializer;
19467 /* Consume tokens up to, and including, the next non-nested closing `]'.
19468 Returns true iff we found a closing `]'. */
19470 static bool
19471 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19473 unsigned square_depth = 0;
19475 while (true)
19477 cp_token * token = cp_lexer_peek_token (parser->lexer);
19479 switch (token->type)
19481 case CPP_EOF:
19482 case CPP_PRAGMA_EOL:
19483 /* If we've run out of tokens, then there is no closing `]'. */
19484 return false;
19486 case CPP_OPEN_SQUARE:
19487 ++square_depth;
19488 break;
19490 case CPP_CLOSE_SQUARE:
19491 if (!square_depth--)
19493 cp_lexer_consume_token (parser->lexer);
19494 return true;
19496 break;
19498 default:
19499 break;
19502 /* Consume the token. */
19503 cp_lexer_consume_token (parser->lexer);
19507 /* Return true if we are looking at an array-designator, false otherwise. */
19509 static bool
19510 cp_parser_array_designator_p (cp_parser *parser)
19512 /* Consume the `['. */
19513 cp_lexer_consume_token (parser->lexer);
19515 cp_lexer_save_tokens (parser->lexer);
19517 /* Skip tokens until the next token is a closing square bracket.
19518 If we find the closing `]', and the next token is a `=', then
19519 we are looking at an array designator. */
19520 bool array_designator_p
19521 = (cp_parser_skip_to_closing_square_bracket (parser)
19522 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19524 /* Roll back the tokens we skipped. */
19525 cp_lexer_rollback_tokens (parser->lexer);
19527 return array_designator_p;
19530 /* Parse an initializer-list.
19532 initializer-list:
19533 initializer-clause ... [opt]
19534 initializer-list , initializer-clause ... [opt]
19536 GNU Extension:
19538 initializer-list:
19539 designation initializer-clause ...[opt]
19540 initializer-list , designation initializer-clause ...[opt]
19542 designation:
19543 . identifier =
19544 identifier :
19545 [ constant-expression ] =
19547 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19548 for the initializer. If the INDEX of the elt is non-NULL, it is the
19549 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19550 as for cp_parser_initializer. */
19552 static vec<constructor_elt, va_gc> *
19553 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19555 vec<constructor_elt, va_gc> *v = NULL;
19557 /* Assume all of the expressions are constant. */
19558 *non_constant_p = false;
19560 /* Parse the rest of the list. */
19561 while (true)
19563 cp_token *token;
19564 tree designator;
19565 tree initializer;
19566 bool clause_non_constant_p;
19568 /* If the next token is an identifier and the following one is a
19569 colon, we are looking at the GNU designated-initializer
19570 syntax. */
19571 if (cp_parser_allow_gnu_extensions_p (parser)
19572 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19573 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19575 /* Warn the user that they are using an extension. */
19576 pedwarn (input_location, OPT_Wpedantic,
19577 "ISO C++ does not allow designated initializers");
19578 /* Consume the identifier. */
19579 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19580 /* Consume the `:'. */
19581 cp_lexer_consume_token (parser->lexer);
19583 /* Also handle the C99 syntax, '. id ='. */
19584 else if (cp_parser_allow_gnu_extensions_p (parser)
19585 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19587 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19589 /* Warn the user that they are using an extension. */
19590 pedwarn (input_location, OPT_Wpedantic,
19591 "ISO C++ does not allow C99 designated initializers");
19592 /* Consume the `.'. */
19593 cp_lexer_consume_token (parser->lexer);
19594 /* Consume the identifier. */
19595 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19596 /* Consume the `='. */
19597 cp_lexer_consume_token (parser->lexer);
19599 /* Also handle C99 array designators, '[ const ] ='. */
19600 else if (cp_parser_allow_gnu_extensions_p (parser)
19601 && !c_dialect_objc ()
19602 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19604 /* In C++11, [ could start a lambda-introducer. */
19605 bool non_const = false;
19607 cp_parser_parse_tentatively (parser);
19609 if (!cp_parser_array_designator_p (parser))
19611 cp_parser_simulate_error (parser);
19612 designator = NULL_TREE;
19614 else
19616 designator = cp_parser_constant_expression (parser, true,
19617 &non_const);
19618 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19619 cp_parser_require (parser, CPP_EQ, RT_EQ);
19622 if (!cp_parser_parse_definitely (parser))
19623 designator = NULL_TREE;
19624 else if (non_const)
19625 require_potential_rvalue_constant_expression (designator);
19627 else
19628 designator = NULL_TREE;
19630 /* Parse the initializer. */
19631 initializer = cp_parser_initializer_clause (parser,
19632 &clause_non_constant_p);
19633 /* If any clause is non-constant, so is the entire initializer. */
19634 if (clause_non_constant_p)
19635 *non_constant_p = true;
19637 /* If we have an ellipsis, this is an initializer pack
19638 expansion. */
19639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19641 /* Consume the `...'. */
19642 cp_lexer_consume_token (parser->lexer);
19644 /* Turn the initializer into an initializer expansion. */
19645 initializer = make_pack_expansion (initializer);
19648 /* Add it to the vector. */
19649 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19651 /* If the next token is not a comma, we have reached the end of
19652 the list. */
19653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19654 break;
19656 /* Peek at the next token. */
19657 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19658 /* If the next token is a `}', then we're still done. An
19659 initializer-clause can have a trailing `,' after the
19660 initializer-list and before the closing `}'. */
19661 if (token->type == CPP_CLOSE_BRACE)
19662 break;
19664 /* Consume the `,' token. */
19665 cp_lexer_consume_token (parser->lexer);
19668 return v;
19671 /* Classes [gram.class] */
19673 /* Parse a class-name.
19675 class-name:
19676 identifier
19677 template-id
19679 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19680 to indicate that names looked up in dependent types should be
19681 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19682 keyword has been used to indicate that the name that appears next
19683 is a template. TAG_TYPE indicates the explicit tag given before
19684 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19685 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19686 is the class being defined in a class-head. If ENUM_OK is TRUE,
19687 enum-names are also accepted.
19689 Returns the TYPE_DECL representing the class. */
19691 static tree
19692 cp_parser_class_name (cp_parser *parser,
19693 bool typename_keyword_p,
19694 bool template_keyword_p,
19695 enum tag_types tag_type,
19696 bool check_dependency_p,
19697 bool class_head_p,
19698 bool is_declaration,
19699 bool enum_ok)
19701 tree decl;
19702 tree scope;
19703 bool typename_p;
19704 cp_token *token;
19705 tree identifier = NULL_TREE;
19707 /* All class-names start with an identifier. */
19708 token = cp_lexer_peek_token (parser->lexer);
19709 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19711 cp_parser_error (parser, "expected class-name");
19712 return error_mark_node;
19715 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19716 to a template-id, so we save it here. */
19717 scope = parser->scope;
19718 if (scope == error_mark_node)
19719 return error_mark_node;
19721 /* Any name names a type if we're following the `typename' keyword
19722 in a qualified name where the enclosing scope is type-dependent. */
19723 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19724 && dependent_type_p (scope));
19725 /* Handle the common case (an identifier, but not a template-id)
19726 efficiently. */
19727 if (token->type == CPP_NAME
19728 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19730 cp_token *identifier_token;
19731 bool ambiguous_p;
19733 /* Look for the identifier. */
19734 identifier_token = cp_lexer_peek_token (parser->lexer);
19735 ambiguous_p = identifier_token->error_reported;
19736 identifier = cp_parser_identifier (parser);
19737 /* If the next token isn't an identifier, we are certainly not
19738 looking at a class-name. */
19739 if (identifier == error_mark_node)
19740 decl = error_mark_node;
19741 /* If we know this is a type-name, there's no need to look it
19742 up. */
19743 else if (typename_p)
19744 decl = identifier;
19745 else
19747 tree ambiguous_decls;
19748 /* If we already know that this lookup is ambiguous, then
19749 we've already issued an error message; there's no reason
19750 to check again. */
19751 if (ambiguous_p)
19753 cp_parser_simulate_error (parser);
19754 return error_mark_node;
19756 /* If the next token is a `::', then the name must be a type
19757 name.
19759 [basic.lookup.qual]
19761 During the lookup for a name preceding the :: scope
19762 resolution operator, object, function, and enumerator
19763 names are ignored. */
19764 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19765 tag_type = typename_type;
19766 /* Look up the name. */
19767 decl = cp_parser_lookup_name (parser, identifier,
19768 tag_type,
19769 /*is_template=*/false,
19770 /*is_namespace=*/false,
19771 check_dependency_p,
19772 &ambiguous_decls,
19773 identifier_token->location);
19774 if (ambiguous_decls)
19776 if (cp_parser_parsing_tentatively (parser))
19777 cp_parser_simulate_error (parser);
19778 return error_mark_node;
19782 else
19784 /* Try a template-id. */
19785 decl = cp_parser_template_id (parser, template_keyword_p,
19786 check_dependency_p,
19787 tag_type,
19788 is_declaration);
19789 if (decl == error_mark_node)
19790 return error_mark_node;
19793 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19795 /* If this is a typename, create a TYPENAME_TYPE. */
19796 if (typename_p && decl != error_mark_node)
19798 decl = make_typename_type (scope, decl, typename_type,
19799 /*complain=*/tf_error);
19800 if (decl != error_mark_node)
19801 decl = TYPE_NAME (decl);
19804 decl = strip_using_decl (decl);
19806 /* Check to see that it is really the name of a class. */
19807 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19808 && identifier_p (TREE_OPERAND (decl, 0))
19809 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19810 /* Situations like this:
19812 template <typename T> struct A {
19813 typename T::template X<int>::I i;
19816 are problematic. Is `T::template X<int>' a class-name? The
19817 standard does not seem to be definitive, but there is no other
19818 valid interpretation of the following `::'. Therefore, those
19819 names are considered class-names. */
19821 decl = make_typename_type (scope, decl, tag_type, tf_error);
19822 if (decl != error_mark_node)
19823 decl = TYPE_NAME (decl);
19825 else if (TREE_CODE (decl) != TYPE_DECL
19826 || TREE_TYPE (decl) == error_mark_node
19827 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19828 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19829 /* In Objective-C 2.0, a classname followed by '.' starts a
19830 dot-syntax expression, and it's not a type-name. */
19831 || (c_dialect_objc ()
19832 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19833 && objc_is_class_name (decl)))
19834 decl = error_mark_node;
19836 if (decl == error_mark_node)
19837 cp_parser_error (parser, "expected class-name");
19838 else if (identifier && !parser->scope)
19839 maybe_note_name_used_in_class (identifier, decl);
19841 return decl;
19844 /* Parse a class-specifier.
19846 class-specifier:
19847 class-head { member-specification [opt] }
19849 Returns the TREE_TYPE representing the class. */
19851 static tree
19852 cp_parser_class_specifier_1 (cp_parser* parser)
19854 tree type;
19855 tree attributes = NULL_TREE;
19856 bool nested_name_specifier_p;
19857 unsigned saved_num_template_parameter_lists;
19858 bool saved_in_function_body;
19859 unsigned char in_statement;
19860 bool in_switch_statement_p;
19861 bool saved_in_unbraced_linkage_specification_p;
19862 tree old_scope = NULL_TREE;
19863 tree scope = NULL_TREE;
19864 cp_token *closing_brace;
19866 push_deferring_access_checks (dk_no_deferred);
19868 /* Parse the class-head. */
19869 type = cp_parser_class_head (parser,
19870 &nested_name_specifier_p);
19871 /* If the class-head was a semantic disaster, skip the entire body
19872 of the class. */
19873 if (!type)
19875 cp_parser_skip_to_end_of_block_or_statement (parser);
19876 pop_deferring_access_checks ();
19877 return error_mark_node;
19880 /* Look for the `{'. */
19881 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19883 pop_deferring_access_checks ();
19884 return error_mark_node;
19887 cp_ensure_no_omp_declare_simd (parser);
19889 /* Issue an error message if type-definitions are forbidden here. */
19890 cp_parser_check_type_definition (parser);
19891 /* Remember that we are defining one more class. */
19892 ++parser->num_classes_being_defined;
19893 /* Inside the class, surrounding template-parameter-lists do not
19894 apply. */
19895 saved_num_template_parameter_lists
19896 = parser->num_template_parameter_lists;
19897 parser->num_template_parameter_lists = 0;
19898 /* We are not in a function body. */
19899 saved_in_function_body = parser->in_function_body;
19900 parser->in_function_body = false;
19901 /* Or in a loop. */
19902 in_statement = parser->in_statement;
19903 parser->in_statement = 0;
19904 /* Or in a switch. */
19905 in_switch_statement_p = parser->in_switch_statement_p;
19906 parser->in_switch_statement_p = false;
19907 /* We are not immediately inside an extern "lang" block. */
19908 saved_in_unbraced_linkage_specification_p
19909 = parser->in_unbraced_linkage_specification_p;
19910 parser->in_unbraced_linkage_specification_p = false;
19912 /* Start the class. */
19913 if (nested_name_specifier_p)
19915 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19916 old_scope = push_inner_scope (scope);
19918 type = begin_class_definition (type);
19920 if (type == error_mark_node)
19921 /* If the type is erroneous, skip the entire body of the class. */
19922 cp_parser_skip_to_closing_brace (parser);
19923 else
19924 /* Parse the member-specification. */
19925 cp_parser_member_specification_opt (parser);
19927 /* Look for the trailing `}'. */
19928 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19929 /* Look for trailing attributes to apply to this class. */
19930 if (cp_parser_allow_gnu_extensions_p (parser))
19931 attributes = cp_parser_gnu_attributes_opt (parser);
19932 if (type != error_mark_node)
19933 type = finish_struct (type, attributes);
19934 if (nested_name_specifier_p)
19935 pop_inner_scope (old_scope, scope);
19937 /* We've finished a type definition. Check for the common syntax
19938 error of forgetting a semicolon after the definition. We need to
19939 be careful, as we can't just check for not-a-semicolon and be done
19940 with it; the user might have typed:
19942 class X { } c = ...;
19943 class X { } *p = ...;
19945 and so forth. Instead, enumerate all the possible tokens that
19946 might follow this production; if we don't see one of them, then
19947 complain and silently insert the semicolon. */
19949 cp_token *token = cp_lexer_peek_token (parser->lexer);
19950 bool want_semicolon = true;
19952 if (cp_next_tokens_can_be_std_attribute_p (parser))
19953 /* Don't try to parse c++11 attributes here. As per the
19954 grammar, that should be a task for
19955 cp_parser_decl_specifier_seq. */
19956 want_semicolon = false;
19958 switch (token->type)
19960 case CPP_NAME:
19961 case CPP_SEMICOLON:
19962 case CPP_MULT:
19963 case CPP_AND:
19964 case CPP_OPEN_PAREN:
19965 case CPP_CLOSE_PAREN:
19966 case CPP_COMMA:
19967 want_semicolon = false;
19968 break;
19970 /* While it's legal for type qualifiers and storage class
19971 specifiers to follow type definitions in the grammar, only
19972 compiler testsuites contain code like that. Assume that if
19973 we see such code, then what we're really seeing is a case
19974 like:
19976 class X { }
19977 const <type> var = ...;
19981 class Y { }
19982 static <type> func (...) ...
19984 i.e. the qualifier or specifier applies to the next
19985 declaration. To do so, however, we need to look ahead one
19986 more token to see if *that* token is a type specifier.
19988 This code could be improved to handle:
19990 class Z { }
19991 static const <type> var = ...; */
19992 case CPP_KEYWORD:
19993 if (keyword_is_decl_specifier (token->keyword))
19995 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19997 /* Handling user-defined types here would be nice, but very
19998 tricky. */
19999 want_semicolon
20000 = (lookahead->type == CPP_KEYWORD
20001 && keyword_begins_type_specifier (lookahead->keyword));
20003 break;
20004 default:
20005 break;
20008 /* If we don't have a type, then something is very wrong and we
20009 shouldn't try to do anything clever. Likewise for not seeing the
20010 closing brace. */
20011 if (closing_brace && TYPE_P (type) && want_semicolon)
20013 cp_token_position prev
20014 = cp_lexer_previous_token_position (parser->lexer);
20015 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20016 location_t loc = prev_token->location;
20018 if (CLASSTYPE_DECLARED_CLASS (type))
20019 error_at (loc, "expected %<;%> after class definition");
20020 else if (TREE_CODE (type) == RECORD_TYPE)
20021 error_at (loc, "expected %<;%> after struct definition");
20022 else if (TREE_CODE (type) == UNION_TYPE)
20023 error_at (loc, "expected %<;%> after union definition");
20024 else
20025 gcc_unreachable ();
20027 /* Unget one token and smash it to look as though we encountered
20028 a semicolon in the input stream. */
20029 cp_lexer_set_token_position (parser->lexer, prev);
20030 token = cp_lexer_peek_token (parser->lexer);
20031 token->type = CPP_SEMICOLON;
20032 token->keyword = RID_MAX;
20036 /* If this class is not itself within the scope of another class,
20037 then we need to parse the bodies of all of the queued function
20038 definitions. Note that the queued functions defined in a class
20039 are not always processed immediately following the
20040 class-specifier for that class. Consider:
20042 struct A {
20043 struct B { void f() { sizeof (A); } };
20046 If `f' were processed before the processing of `A' were
20047 completed, there would be no way to compute the size of `A'.
20048 Note that the nesting we are interested in here is lexical --
20049 not the semantic nesting given by TYPE_CONTEXT. In particular,
20050 for:
20052 struct A { struct B; };
20053 struct A::B { void f() { } };
20055 there is no need to delay the parsing of `A::B::f'. */
20056 if (--parser->num_classes_being_defined == 0)
20058 tree decl;
20059 tree class_type = NULL_TREE;
20060 tree pushed_scope = NULL_TREE;
20061 unsigned ix;
20062 cp_default_arg_entry *e;
20063 tree save_ccp, save_ccr;
20065 /* In a first pass, parse default arguments to the functions.
20066 Then, in a second pass, parse the bodies of the functions.
20067 This two-phased approach handles cases like:
20069 struct S {
20070 void f() { g(); }
20071 void g(int i = 3);
20075 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20077 decl = e->decl;
20078 /* If there are default arguments that have not yet been processed,
20079 take care of them now. */
20080 if (class_type != e->class_type)
20082 if (pushed_scope)
20083 pop_scope (pushed_scope);
20084 class_type = e->class_type;
20085 pushed_scope = push_scope (class_type);
20087 /* Make sure that any template parameters are in scope. */
20088 maybe_begin_member_template_processing (decl);
20089 /* Parse the default argument expressions. */
20090 cp_parser_late_parsing_default_args (parser, decl);
20091 /* Remove any template parameters from the symbol table. */
20092 maybe_end_member_template_processing ();
20094 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20095 /* Now parse any NSDMIs. */
20096 save_ccp = current_class_ptr;
20097 save_ccr = current_class_ref;
20098 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20100 if (class_type != DECL_CONTEXT (decl))
20102 if (pushed_scope)
20103 pop_scope (pushed_scope);
20104 class_type = DECL_CONTEXT (decl);
20105 pushed_scope = push_scope (class_type);
20107 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20108 cp_parser_late_parsing_nsdmi (parser, decl);
20110 vec_safe_truncate (unparsed_nsdmis, 0);
20111 current_class_ptr = save_ccp;
20112 current_class_ref = save_ccr;
20113 if (pushed_scope)
20114 pop_scope (pushed_scope);
20116 /* Now do some post-NSDMI bookkeeping. */
20117 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20118 after_nsdmi_defaulted_late_checks (class_type);
20119 vec_safe_truncate (unparsed_classes, 0);
20120 after_nsdmi_defaulted_late_checks (type);
20122 /* Now parse the body of the functions. */
20123 if (flag_openmp)
20125 /* OpenMP UDRs need to be parsed before all other functions. */
20126 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20127 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20128 cp_parser_late_parsing_for_member (parser, decl);
20129 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20130 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20131 cp_parser_late_parsing_for_member (parser, decl);
20133 else
20134 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20135 cp_parser_late_parsing_for_member (parser, decl);
20136 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20138 else
20139 vec_safe_push (unparsed_classes, type);
20141 /* Put back any saved access checks. */
20142 pop_deferring_access_checks ();
20144 /* Restore saved state. */
20145 parser->in_switch_statement_p = in_switch_statement_p;
20146 parser->in_statement = in_statement;
20147 parser->in_function_body = saved_in_function_body;
20148 parser->num_template_parameter_lists
20149 = saved_num_template_parameter_lists;
20150 parser->in_unbraced_linkage_specification_p
20151 = saved_in_unbraced_linkage_specification_p;
20153 return type;
20156 static tree
20157 cp_parser_class_specifier (cp_parser* parser)
20159 tree ret;
20160 timevar_push (TV_PARSE_STRUCT);
20161 ret = cp_parser_class_specifier_1 (parser);
20162 timevar_pop (TV_PARSE_STRUCT);
20163 return ret;
20166 /* Parse a class-head.
20168 class-head:
20169 class-key identifier [opt] base-clause [opt]
20170 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20171 class-key nested-name-specifier [opt] template-id
20172 base-clause [opt]
20174 class-virt-specifier:
20175 final
20177 GNU Extensions:
20178 class-key attributes identifier [opt] base-clause [opt]
20179 class-key attributes nested-name-specifier identifier base-clause [opt]
20180 class-key attributes nested-name-specifier [opt] template-id
20181 base-clause [opt]
20183 Upon return BASES is initialized to the list of base classes (or
20184 NULL, if there are none) in the same form returned by
20185 cp_parser_base_clause.
20187 Returns the TYPE of the indicated class. Sets
20188 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20189 involving a nested-name-specifier was used, and FALSE otherwise.
20191 Returns error_mark_node if this is not a class-head.
20193 Returns NULL_TREE if the class-head is syntactically valid, but
20194 semantically invalid in a way that means we should skip the entire
20195 body of the class. */
20197 static tree
20198 cp_parser_class_head (cp_parser* parser,
20199 bool* nested_name_specifier_p)
20201 tree nested_name_specifier;
20202 enum tag_types class_key;
20203 tree id = NULL_TREE;
20204 tree type = NULL_TREE;
20205 tree attributes;
20206 tree bases;
20207 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20208 bool template_id_p = false;
20209 bool qualified_p = false;
20210 bool invalid_nested_name_p = false;
20211 bool invalid_explicit_specialization_p = false;
20212 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20213 tree pushed_scope = NULL_TREE;
20214 unsigned num_templates;
20215 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20216 /* Assume no nested-name-specifier will be present. */
20217 *nested_name_specifier_p = false;
20218 /* Assume no template parameter lists will be used in defining the
20219 type. */
20220 num_templates = 0;
20221 parser->colon_corrects_to_scope_p = false;
20223 /* Look for the class-key. */
20224 class_key = cp_parser_class_key (parser);
20225 if (class_key == none_type)
20226 return error_mark_node;
20228 /* Parse the attributes. */
20229 attributes = cp_parser_attributes_opt (parser);
20231 /* If the next token is `::', that is invalid -- but sometimes
20232 people do try to write:
20234 struct ::S {};
20236 Handle this gracefully by accepting the extra qualifier, and then
20237 issuing an error about it later if this really is a
20238 class-head. If it turns out just to be an elaborated type
20239 specifier, remain silent. */
20240 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20241 qualified_p = true;
20243 push_deferring_access_checks (dk_no_check);
20245 /* Determine the name of the class. Begin by looking for an
20246 optional nested-name-specifier. */
20247 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20248 nested_name_specifier
20249 = cp_parser_nested_name_specifier_opt (parser,
20250 /*typename_keyword_p=*/false,
20251 /*check_dependency_p=*/false,
20252 /*type_p=*/true,
20253 /*is_declaration=*/false);
20254 /* If there was a nested-name-specifier, then there *must* be an
20255 identifier. */
20256 if (nested_name_specifier)
20258 type_start_token = cp_lexer_peek_token (parser->lexer);
20259 /* Although the grammar says `identifier', it really means
20260 `class-name' or `template-name'. You are only allowed to
20261 define a class that has already been declared with this
20262 syntax.
20264 The proposed resolution for Core Issue 180 says that wherever
20265 you see `class T::X' you should treat `X' as a type-name.
20267 It is OK to define an inaccessible class; for example:
20269 class A { class B; };
20270 class A::B {};
20272 We do not know if we will see a class-name, or a
20273 template-name. We look for a class-name first, in case the
20274 class-name is a template-id; if we looked for the
20275 template-name first we would stop after the template-name. */
20276 cp_parser_parse_tentatively (parser);
20277 type = cp_parser_class_name (parser,
20278 /*typename_keyword_p=*/false,
20279 /*template_keyword_p=*/false,
20280 class_type,
20281 /*check_dependency_p=*/false,
20282 /*class_head_p=*/true,
20283 /*is_declaration=*/false);
20284 /* If that didn't work, ignore the nested-name-specifier. */
20285 if (!cp_parser_parse_definitely (parser))
20287 invalid_nested_name_p = true;
20288 type_start_token = cp_lexer_peek_token (parser->lexer);
20289 id = cp_parser_identifier (parser);
20290 if (id == error_mark_node)
20291 id = NULL_TREE;
20293 /* If we could not find a corresponding TYPE, treat this
20294 declaration like an unqualified declaration. */
20295 if (type == error_mark_node)
20296 nested_name_specifier = NULL_TREE;
20297 /* Otherwise, count the number of templates used in TYPE and its
20298 containing scopes. */
20299 else
20301 tree scope;
20303 for (scope = TREE_TYPE (type);
20304 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20305 scope = get_containing_scope (scope))
20306 if (TYPE_P (scope)
20307 && CLASS_TYPE_P (scope)
20308 && CLASSTYPE_TEMPLATE_INFO (scope)
20309 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20310 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20311 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20312 ++num_templates;
20315 /* Otherwise, the identifier is optional. */
20316 else
20318 /* We don't know whether what comes next is a template-id,
20319 an identifier, or nothing at all. */
20320 cp_parser_parse_tentatively (parser);
20321 /* Check for a template-id. */
20322 type_start_token = cp_lexer_peek_token (parser->lexer);
20323 id = cp_parser_template_id (parser,
20324 /*template_keyword_p=*/false,
20325 /*check_dependency_p=*/true,
20326 class_key,
20327 /*is_declaration=*/true);
20328 /* If that didn't work, it could still be an identifier. */
20329 if (!cp_parser_parse_definitely (parser))
20331 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20333 type_start_token = cp_lexer_peek_token (parser->lexer);
20334 id = cp_parser_identifier (parser);
20336 else
20337 id = NULL_TREE;
20339 else
20341 template_id_p = true;
20342 ++num_templates;
20346 pop_deferring_access_checks ();
20348 if (id)
20350 cp_parser_check_for_invalid_template_id (parser, id,
20351 class_key,
20352 type_start_token->location);
20354 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20356 /* If it's not a `:' or a `{' then we can't really be looking at a
20357 class-head, since a class-head only appears as part of a
20358 class-specifier. We have to detect this situation before calling
20359 xref_tag, since that has irreversible side-effects. */
20360 if (!cp_parser_next_token_starts_class_definition_p (parser))
20362 cp_parser_error (parser, "expected %<{%> or %<:%>");
20363 type = error_mark_node;
20364 goto out;
20367 /* At this point, we're going ahead with the class-specifier, even
20368 if some other problem occurs. */
20369 cp_parser_commit_to_tentative_parse (parser);
20370 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20372 cp_parser_error (parser,
20373 "cannot specify %<override%> for a class");
20374 type = error_mark_node;
20375 goto out;
20377 /* Issue the error about the overly-qualified name now. */
20378 if (qualified_p)
20380 cp_parser_error (parser,
20381 "global qualification of class name is invalid");
20382 type = error_mark_node;
20383 goto out;
20385 else if (invalid_nested_name_p)
20387 cp_parser_error (parser,
20388 "qualified name does not name a class");
20389 type = error_mark_node;
20390 goto out;
20392 else if (nested_name_specifier)
20394 tree scope;
20396 /* Reject typedef-names in class heads. */
20397 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20399 error_at (type_start_token->location,
20400 "invalid class name in declaration of %qD",
20401 type);
20402 type = NULL_TREE;
20403 goto done;
20406 /* Figure out in what scope the declaration is being placed. */
20407 scope = current_scope ();
20408 /* If that scope does not contain the scope in which the
20409 class was originally declared, the program is invalid. */
20410 if (scope && !is_ancestor (scope, nested_name_specifier))
20412 if (at_namespace_scope_p ())
20413 error_at (type_start_token->location,
20414 "declaration of %qD in namespace %qD which does not "
20415 "enclose %qD",
20416 type, scope, nested_name_specifier);
20417 else
20418 error_at (type_start_token->location,
20419 "declaration of %qD in %qD which does not enclose %qD",
20420 type, scope, nested_name_specifier);
20421 type = NULL_TREE;
20422 goto done;
20424 /* [dcl.meaning]
20426 A declarator-id shall not be qualified except for the
20427 definition of a ... nested class outside of its class
20428 ... [or] the definition or explicit instantiation of a
20429 class member of a namespace outside of its namespace. */
20430 if (scope == nested_name_specifier)
20432 permerror (nested_name_specifier_token_start->location,
20433 "extra qualification not allowed");
20434 nested_name_specifier = NULL_TREE;
20435 num_templates = 0;
20438 /* An explicit-specialization must be preceded by "template <>". If
20439 it is not, try to recover gracefully. */
20440 if (at_namespace_scope_p ()
20441 && parser->num_template_parameter_lists == 0
20442 && template_id_p)
20444 error_at (type_start_token->location,
20445 "an explicit specialization must be preceded by %<template <>%>");
20446 invalid_explicit_specialization_p = true;
20447 /* Take the same action that would have been taken by
20448 cp_parser_explicit_specialization. */
20449 ++parser->num_template_parameter_lists;
20450 begin_specialization ();
20452 /* There must be no "return" statements between this point and the
20453 end of this function; set "type "to the correct return value and
20454 use "goto done;" to return. */
20455 /* Make sure that the right number of template parameters were
20456 present. */
20457 if (!cp_parser_check_template_parameters (parser, num_templates,
20458 type_start_token->location,
20459 /*declarator=*/NULL))
20461 /* If something went wrong, there is no point in even trying to
20462 process the class-definition. */
20463 type = NULL_TREE;
20464 goto done;
20467 /* Look up the type. */
20468 if (template_id_p)
20470 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20471 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20472 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20474 error_at (type_start_token->location,
20475 "function template %qD redeclared as a class template", id);
20476 type = error_mark_node;
20478 else
20480 type = TREE_TYPE (id);
20481 type = maybe_process_partial_specialization (type);
20483 if (nested_name_specifier)
20484 pushed_scope = push_scope (nested_name_specifier);
20486 else if (nested_name_specifier)
20488 tree class_type;
20490 /* Given:
20492 template <typename T> struct S { struct T };
20493 template <typename T> struct S<T>::T { };
20495 we will get a TYPENAME_TYPE when processing the definition of
20496 `S::T'. We need to resolve it to the actual type before we
20497 try to define it. */
20498 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20500 class_type = resolve_typename_type (TREE_TYPE (type),
20501 /*only_current_p=*/false);
20502 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20503 type = TYPE_NAME (class_type);
20504 else
20506 cp_parser_error (parser, "could not resolve typename type");
20507 type = error_mark_node;
20511 if (maybe_process_partial_specialization (TREE_TYPE (type))
20512 == error_mark_node)
20514 type = NULL_TREE;
20515 goto done;
20518 class_type = current_class_type;
20519 /* Enter the scope indicated by the nested-name-specifier. */
20520 pushed_scope = push_scope (nested_name_specifier);
20521 /* Get the canonical version of this type. */
20522 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20523 /* Call push_template_decl if it seems like we should be defining a
20524 template either from the template headers or the type we're
20525 defining, so that we diagnose both extra and missing headers. */
20526 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20527 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20528 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20530 type = push_template_decl (type);
20531 if (type == error_mark_node)
20533 type = NULL_TREE;
20534 goto done;
20538 type = TREE_TYPE (type);
20539 *nested_name_specifier_p = true;
20541 else /* The name is not a nested name. */
20543 /* If the class was unnamed, create a dummy name. */
20544 if (!id)
20545 id = make_anon_name ();
20546 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20547 parser->num_template_parameter_lists);
20550 /* Indicate whether this class was declared as a `class' or as a
20551 `struct'. */
20552 if (TREE_CODE (type) == RECORD_TYPE)
20553 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20554 cp_parser_check_class_key (class_key, type);
20556 /* If this type was already complete, and we see another definition,
20557 that's an error. */
20558 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20560 error_at (type_start_token->location, "redefinition of %q#T",
20561 type);
20562 error_at (type_start_token->location, "previous definition of %q+#T",
20563 type);
20564 type = NULL_TREE;
20565 goto done;
20567 else if (type == error_mark_node)
20568 type = NULL_TREE;
20570 if (type)
20572 /* Apply attributes now, before any use of the class as a template
20573 argument in its base list. */
20574 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20575 fixup_attribute_variants (type);
20578 /* We will have entered the scope containing the class; the names of
20579 base classes should be looked up in that context. For example:
20581 struct A { struct B {}; struct C; };
20582 struct A::C : B {};
20584 is valid. */
20586 /* Get the list of base-classes, if there is one. */
20587 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20589 /* PR59482: enter the class scope so that base-specifiers are looked
20590 up correctly. */
20591 if (type)
20592 pushclass (type);
20593 bases = cp_parser_base_clause (parser);
20594 /* PR59482: get out of the previously pushed class scope so that the
20595 subsequent pops pop the right thing. */
20596 if (type)
20597 popclass ();
20599 else
20600 bases = NULL_TREE;
20602 /* If we're really defining a class, process the base classes.
20603 If they're invalid, fail. */
20604 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20605 && !xref_basetypes (type, bases))
20606 type = NULL_TREE;
20608 done:
20609 /* Leave the scope given by the nested-name-specifier. We will
20610 enter the class scope itself while processing the members. */
20611 if (pushed_scope)
20612 pop_scope (pushed_scope);
20614 if (invalid_explicit_specialization_p)
20616 end_specialization ();
20617 --parser->num_template_parameter_lists;
20620 if (type)
20621 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20622 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20623 CLASSTYPE_FINAL (type) = 1;
20624 out:
20625 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20626 return type;
20629 /* Parse a class-key.
20631 class-key:
20632 class
20633 struct
20634 union
20636 Returns the kind of class-key specified, or none_type to indicate
20637 error. */
20639 static enum tag_types
20640 cp_parser_class_key (cp_parser* parser)
20642 cp_token *token;
20643 enum tag_types tag_type;
20645 /* Look for the class-key. */
20646 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20647 if (!token)
20648 return none_type;
20650 /* Check to see if the TOKEN is a class-key. */
20651 tag_type = cp_parser_token_is_class_key (token);
20652 if (!tag_type)
20653 cp_parser_error (parser, "expected class-key");
20654 return tag_type;
20657 /* Parse a type-parameter-key.
20659 type-parameter-key:
20660 class
20661 typename
20664 static void
20665 cp_parser_type_parameter_key (cp_parser* parser)
20667 /* Look for the type-parameter-key. */
20668 enum tag_types tag_type = none_type;
20669 cp_token *token = cp_lexer_peek_token (parser->lexer);
20670 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20672 cp_lexer_consume_token (parser->lexer);
20673 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20674 /* typename is not allowed in a template template parameter
20675 by the standard until C++1Z. */
20676 pedwarn (token->location, OPT_Wpedantic,
20677 "ISO C++ forbids typename key in template template parameter;"
20678 " use -std=c++1z or -std=gnu++1z");
20680 else
20681 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20683 return;
20686 /* Parse an (optional) member-specification.
20688 member-specification:
20689 member-declaration member-specification [opt]
20690 access-specifier : member-specification [opt] */
20692 static void
20693 cp_parser_member_specification_opt (cp_parser* parser)
20695 while (true)
20697 cp_token *token;
20698 enum rid keyword;
20700 /* Peek at the next token. */
20701 token = cp_lexer_peek_token (parser->lexer);
20702 /* If it's a `}', or EOF then we've seen all the members. */
20703 if (token->type == CPP_CLOSE_BRACE
20704 || token->type == CPP_EOF
20705 || token->type == CPP_PRAGMA_EOL)
20706 break;
20708 /* See if this token is a keyword. */
20709 keyword = token->keyword;
20710 switch (keyword)
20712 case RID_PUBLIC:
20713 case RID_PROTECTED:
20714 case RID_PRIVATE:
20715 /* Consume the access-specifier. */
20716 cp_lexer_consume_token (parser->lexer);
20717 /* Remember which access-specifier is active. */
20718 current_access_specifier = token->u.value;
20719 /* Look for the `:'. */
20720 cp_parser_require (parser, CPP_COLON, RT_COLON);
20721 break;
20723 default:
20724 /* Accept #pragmas at class scope. */
20725 if (token->type == CPP_PRAGMA)
20727 cp_parser_pragma (parser, pragma_member);
20728 break;
20731 /* Otherwise, the next construction must be a
20732 member-declaration. */
20733 cp_parser_member_declaration (parser);
20738 /* Parse a member-declaration.
20740 member-declaration:
20741 decl-specifier-seq [opt] member-declarator-list [opt] ;
20742 function-definition ; [opt]
20743 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20744 using-declaration
20745 template-declaration
20746 alias-declaration
20748 member-declarator-list:
20749 member-declarator
20750 member-declarator-list , member-declarator
20752 member-declarator:
20753 declarator pure-specifier [opt]
20754 declarator constant-initializer [opt]
20755 identifier [opt] : constant-expression
20757 GNU Extensions:
20759 member-declaration:
20760 __extension__ member-declaration
20762 member-declarator:
20763 declarator attributes [opt] pure-specifier [opt]
20764 declarator attributes [opt] constant-initializer [opt]
20765 identifier [opt] attributes [opt] : constant-expression
20767 C++0x Extensions:
20769 member-declaration:
20770 static_assert-declaration */
20772 static void
20773 cp_parser_member_declaration (cp_parser* parser)
20775 cp_decl_specifier_seq decl_specifiers;
20776 tree prefix_attributes;
20777 tree decl;
20778 int declares_class_or_enum;
20779 bool friend_p;
20780 cp_token *token = NULL;
20781 cp_token *decl_spec_token_start = NULL;
20782 cp_token *initializer_token_start = NULL;
20783 int saved_pedantic;
20784 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20786 /* Check for the `__extension__' keyword. */
20787 if (cp_parser_extension_opt (parser, &saved_pedantic))
20789 /* Recurse. */
20790 cp_parser_member_declaration (parser);
20791 /* Restore the old value of the PEDANTIC flag. */
20792 pedantic = saved_pedantic;
20794 return;
20797 /* Check for a template-declaration. */
20798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20800 /* An explicit specialization here is an error condition, and we
20801 expect the specialization handler to detect and report this. */
20802 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20803 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20804 cp_parser_explicit_specialization (parser);
20805 else
20806 cp_parser_template_declaration (parser, /*member_p=*/true);
20808 return;
20811 /* Check for a using-declaration. */
20812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20814 if (cxx_dialect < cxx11)
20816 /* Parse the using-declaration. */
20817 cp_parser_using_declaration (parser,
20818 /*access_declaration_p=*/false);
20819 return;
20821 else
20823 tree decl;
20824 bool alias_decl_expected;
20825 cp_parser_parse_tentatively (parser);
20826 decl = cp_parser_alias_declaration (parser);
20827 /* Note that if we actually see the '=' token after the
20828 identifier, cp_parser_alias_declaration commits the
20829 tentative parse. In that case, we really expects an
20830 alias-declaration. Otherwise, we expect a using
20831 declaration. */
20832 alias_decl_expected =
20833 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20834 cp_parser_parse_definitely (parser);
20836 if (alias_decl_expected)
20837 finish_member_declaration (decl);
20838 else
20839 cp_parser_using_declaration (parser,
20840 /*access_declaration_p=*/false);
20841 return;
20845 /* Check for @defs. */
20846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20848 tree ivar, member;
20849 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20850 ivar = ivar_chains;
20851 while (ivar)
20853 member = ivar;
20854 ivar = TREE_CHAIN (member);
20855 TREE_CHAIN (member) = NULL_TREE;
20856 finish_member_declaration (member);
20858 return;
20861 /* If the next token is `static_assert' we have a static assertion. */
20862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20864 cp_parser_static_assert (parser, /*member_p=*/true);
20865 return;
20868 parser->colon_corrects_to_scope_p = false;
20870 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20871 goto out;
20873 /* Parse the decl-specifier-seq. */
20874 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20875 cp_parser_decl_specifier_seq (parser,
20876 CP_PARSER_FLAGS_OPTIONAL,
20877 &decl_specifiers,
20878 &declares_class_or_enum);
20879 /* Check for an invalid type-name. */
20880 if (!decl_specifiers.any_type_specifiers_p
20881 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20882 goto out;
20883 /* If there is no declarator, then the decl-specifier-seq should
20884 specify a type. */
20885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20887 /* If there was no decl-specifier-seq, and the next token is a
20888 `;', then we have something like:
20890 struct S { ; };
20892 [class.mem]
20894 Each member-declaration shall declare at least one member
20895 name of the class. */
20896 if (!decl_specifiers.any_specifiers_p)
20898 cp_token *token = cp_lexer_peek_token (parser->lexer);
20899 if (!in_system_header_at (token->location))
20900 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20902 else
20904 tree type;
20906 /* See if this declaration is a friend. */
20907 friend_p = cp_parser_friend_p (&decl_specifiers);
20908 /* If there were decl-specifiers, check to see if there was
20909 a class-declaration. */
20910 type = check_tag_decl (&decl_specifiers,
20911 /*explicit_type_instantiation_p=*/false);
20912 /* Nested classes have already been added to the class, but
20913 a `friend' needs to be explicitly registered. */
20914 if (friend_p)
20916 /* If the `friend' keyword was present, the friend must
20917 be introduced with a class-key. */
20918 if (!declares_class_or_enum && cxx_dialect < cxx11)
20919 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20920 "in C++03 a class-key must be used "
20921 "when declaring a friend");
20922 /* In this case:
20924 template <typename T> struct A {
20925 friend struct A<T>::B;
20928 A<T>::B will be represented by a TYPENAME_TYPE, and
20929 therefore not recognized by check_tag_decl. */
20930 if (!type)
20932 type = decl_specifiers.type;
20933 if (type && TREE_CODE (type) == TYPE_DECL)
20934 type = TREE_TYPE (type);
20936 if (!type || !TYPE_P (type))
20937 error_at (decl_spec_token_start->location,
20938 "friend declaration does not name a class or "
20939 "function");
20940 else
20941 make_friend_class (current_class_type, type,
20942 /*complain=*/true);
20944 /* If there is no TYPE, an error message will already have
20945 been issued. */
20946 else if (!type || type == error_mark_node)
20948 /* An anonymous aggregate has to be handled specially; such
20949 a declaration really declares a data member (with a
20950 particular type), as opposed to a nested class. */
20951 else if (ANON_AGGR_TYPE_P (type))
20953 /* C++11 9.5/6. */
20954 if (decl_specifiers.storage_class != sc_none)
20955 error_at (decl_spec_token_start->location,
20956 "a storage class on an anonymous aggregate "
20957 "in class scope is not allowed");
20959 /* Remove constructors and such from TYPE, now that we
20960 know it is an anonymous aggregate. */
20961 fixup_anonymous_aggr (type);
20962 /* And make the corresponding data member. */
20963 decl = build_decl (decl_spec_token_start->location,
20964 FIELD_DECL, NULL_TREE, type);
20965 /* Add it to the class. */
20966 finish_member_declaration (decl);
20968 else
20969 cp_parser_check_access_in_redeclaration
20970 (TYPE_NAME (type),
20971 decl_spec_token_start->location);
20974 else
20976 bool assume_semicolon = false;
20978 /* Clear attributes from the decl_specifiers but keep them
20979 around as prefix attributes that apply them to the entity
20980 being declared. */
20981 prefix_attributes = decl_specifiers.attributes;
20982 decl_specifiers.attributes = NULL_TREE;
20984 /* See if these declarations will be friends. */
20985 friend_p = cp_parser_friend_p (&decl_specifiers);
20987 /* Keep going until we hit the `;' at the end of the
20988 declaration. */
20989 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20991 tree attributes = NULL_TREE;
20992 tree first_attribute;
20994 /* Peek at the next token. */
20995 token = cp_lexer_peek_token (parser->lexer);
20997 /* Check for a bitfield declaration. */
20998 if (token->type == CPP_COLON
20999 || (token->type == CPP_NAME
21000 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21001 == CPP_COLON))
21003 tree identifier;
21004 tree width;
21006 /* Get the name of the bitfield. Note that we cannot just
21007 check TOKEN here because it may have been invalidated by
21008 the call to cp_lexer_peek_nth_token above. */
21009 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21010 identifier = cp_parser_identifier (parser);
21011 else
21012 identifier = NULL_TREE;
21014 /* Consume the `:' token. */
21015 cp_lexer_consume_token (parser->lexer);
21017 /* Get the width of the bitfield. */
21018 width
21019 = cp_parser_constant_expression (parser);
21021 /* Look for attributes that apply to the bitfield. */
21022 attributes = cp_parser_attributes_opt (parser);
21023 /* Remember which attributes are prefix attributes and
21024 which are not. */
21025 first_attribute = attributes;
21026 /* Combine the attributes. */
21027 attributes = chainon (prefix_attributes, attributes);
21029 /* Create the bitfield declaration. */
21030 decl = grokbitfield (identifier
21031 ? make_id_declarator (NULL_TREE,
21032 identifier,
21033 sfk_none)
21034 : NULL,
21035 &decl_specifiers,
21036 width,
21037 attributes);
21039 else
21041 cp_declarator *declarator;
21042 tree initializer;
21043 tree asm_specification;
21044 int ctor_dtor_or_conv_p;
21046 /* Parse the declarator. */
21047 declarator
21048 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21049 &ctor_dtor_or_conv_p,
21050 /*parenthesized_p=*/NULL,
21051 /*member_p=*/true,
21052 friend_p);
21054 /* If something went wrong parsing the declarator, make sure
21055 that we at least consume some tokens. */
21056 if (declarator == cp_error_declarator)
21058 /* Skip to the end of the statement. */
21059 cp_parser_skip_to_end_of_statement (parser);
21060 /* If the next token is not a semicolon, that is
21061 probably because we just skipped over the body of
21062 a function. So, we consume a semicolon if
21063 present, but do not issue an error message if it
21064 is not present. */
21065 if (cp_lexer_next_token_is (parser->lexer,
21066 CPP_SEMICOLON))
21067 cp_lexer_consume_token (parser->lexer);
21068 goto out;
21071 if (declares_class_or_enum & 2)
21072 cp_parser_check_for_definition_in_return_type
21073 (declarator, decl_specifiers.type,
21074 decl_specifiers.locations[ds_type_spec]);
21076 /* Look for an asm-specification. */
21077 asm_specification = cp_parser_asm_specification_opt (parser);
21078 /* Look for attributes that apply to the declaration. */
21079 attributes = cp_parser_attributes_opt (parser);
21080 /* Remember which attributes are prefix attributes and
21081 which are not. */
21082 first_attribute = attributes;
21083 /* Combine the attributes. */
21084 attributes = chainon (prefix_attributes, attributes);
21086 /* If it's an `=', then we have a constant-initializer or a
21087 pure-specifier. It is not correct to parse the
21088 initializer before registering the member declaration
21089 since the member declaration should be in scope while
21090 its initializer is processed. However, the rest of the
21091 front end does not yet provide an interface that allows
21092 us to handle this correctly. */
21093 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21095 /* In [class.mem]:
21097 A pure-specifier shall be used only in the declaration of
21098 a virtual function.
21100 A member-declarator can contain a constant-initializer
21101 only if it declares a static member of integral or
21102 enumeration type.
21104 Therefore, if the DECLARATOR is for a function, we look
21105 for a pure-specifier; otherwise, we look for a
21106 constant-initializer. When we call `grokfield', it will
21107 perform more stringent semantics checks. */
21108 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21109 if (function_declarator_p (declarator)
21110 || (decl_specifiers.type
21111 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21112 && declarator->kind == cdk_id
21113 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21114 == FUNCTION_TYPE)))
21115 initializer = cp_parser_pure_specifier (parser);
21116 else if (decl_specifiers.storage_class != sc_static)
21117 initializer = cp_parser_save_nsdmi (parser);
21118 else if (cxx_dialect >= cxx11)
21120 bool nonconst;
21121 /* Don't require a constant rvalue in C++11, since we
21122 might want a reference constant. We'll enforce
21123 constancy later. */
21124 cp_lexer_consume_token (parser->lexer);
21125 /* Parse the initializer. */
21126 initializer = cp_parser_initializer_clause (parser,
21127 &nonconst);
21129 else
21130 /* Parse the initializer. */
21131 initializer = cp_parser_constant_initializer (parser);
21133 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21134 && !function_declarator_p (declarator))
21136 bool x;
21137 if (decl_specifiers.storage_class != sc_static)
21138 initializer = cp_parser_save_nsdmi (parser);
21139 else
21140 initializer = cp_parser_initializer (parser, &x, &x);
21142 /* Otherwise, there is no initializer. */
21143 else
21144 initializer = NULL_TREE;
21146 /* See if we are probably looking at a function
21147 definition. We are certainly not looking at a
21148 member-declarator. Calling `grokfield' has
21149 side-effects, so we must not do it unless we are sure
21150 that we are looking at a member-declarator. */
21151 if (cp_parser_token_starts_function_definition_p
21152 (cp_lexer_peek_token (parser->lexer)))
21154 /* The grammar does not allow a pure-specifier to be
21155 used when a member function is defined. (It is
21156 possible that this fact is an oversight in the
21157 standard, since a pure function may be defined
21158 outside of the class-specifier. */
21159 if (initializer && initializer_token_start)
21160 error_at (initializer_token_start->location,
21161 "pure-specifier on function-definition");
21162 decl = cp_parser_save_member_function_body (parser,
21163 &decl_specifiers,
21164 declarator,
21165 attributes);
21166 if (parser->fully_implicit_function_template_p)
21167 decl = finish_fully_implicit_template (parser, decl);
21168 /* If the member was not a friend, declare it here. */
21169 if (!friend_p)
21170 finish_member_declaration (decl);
21171 /* Peek at the next token. */
21172 token = cp_lexer_peek_token (parser->lexer);
21173 /* If the next token is a semicolon, consume it. */
21174 if (token->type == CPP_SEMICOLON)
21175 cp_lexer_consume_token (parser->lexer);
21176 goto out;
21178 else
21179 if (declarator->kind == cdk_function)
21180 declarator->id_loc = token->location;
21181 /* Create the declaration. */
21182 decl = grokfield (declarator, &decl_specifiers,
21183 initializer, /*init_const_expr_p=*/true,
21184 asm_specification, attributes);
21185 if (parser->fully_implicit_function_template_p)
21187 if (friend_p)
21188 finish_fully_implicit_template (parser, 0);
21189 else
21190 decl = finish_fully_implicit_template (parser, decl);
21194 cp_finalize_omp_declare_simd (parser, decl);
21196 /* Reset PREFIX_ATTRIBUTES. */
21197 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21198 attributes = TREE_CHAIN (attributes);
21199 if (attributes)
21200 TREE_CHAIN (attributes) = NULL_TREE;
21202 /* If there is any qualification still in effect, clear it
21203 now; we will be starting fresh with the next declarator. */
21204 parser->scope = NULL_TREE;
21205 parser->qualifying_scope = NULL_TREE;
21206 parser->object_scope = NULL_TREE;
21207 /* If it's a `,', then there are more declarators. */
21208 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21210 cp_lexer_consume_token (parser->lexer);
21211 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21213 cp_token *token = cp_lexer_previous_token (parser->lexer);
21214 error_at (token->location,
21215 "stray %<,%> at end of member declaration");
21218 /* If the next token isn't a `;', then we have a parse error. */
21219 else if (cp_lexer_next_token_is_not (parser->lexer,
21220 CPP_SEMICOLON))
21222 /* The next token might be a ways away from where the
21223 actual semicolon is missing. Find the previous token
21224 and use that for our error position. */
21225 cp_token *token = cp_lexer_previous_token (parser->lexer);
21226 error_at (token->location,
21227 "expected %<;%> at end of member declaration");
21229 /* Assume that the user meant to provide a semicolon. If
21230 we were to cp_parser_skip_to_end_of_statement, we might
21231 skip to a semicolon inside a member function definition
21232 and issue nonsensical error messages. */
21233 assume_semicolon = true;
21236 if (decl)
21238 /* Add DECL to the list of members. */
21239 if (!friend_p
21240 /* Explicitly include, eg, NSDMIs, for better error
21241 recovery (c++/58650). */
21242 || !DECL_DECLARES_FUNCTION_P (decl))
21243 finish_member_declaration (decl);
21245 if (TREE_CODE (decl) == FUNCTION_DECL)
21246 cp_parser_save_default_args (parser, decl);
21247 else if (TREE_CODE (decl) == FIELD_DECL
21248 && !DECL_C_BIT_FIELD (decl)
21249 && DECL_INITIAL (decl))
21250 /* Add DECL to the queue of NSDMI to be parsed later. */
21251 vec_safe_push (unparsed_nsdmis, decl);
21254 if (assume_semicolon)
21255 goto out;
21259 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21260 out:
21261 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21264 /* Parse a pure-specifier.
21266 pure-specifier:
21269 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21270 Otherwise, ERROR_MARK_NODE is returned. */
21272 static tree
21273 cp_parser_pure_specifier (cp_parser* parser)
21275 cp_token *token;
21277 /* Look for the `=' token. */
21278 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21279 return error_mark_node;
21280 /* Look for the `0' token. */
21281 token = cp_lexer_peek_token (parser->lexer);
21283 if (token->type == CPP_EOF
21284 || token->type == CPP_PRAGMA_EOL)
21285 return error_mark_node;
21287 cp_lexer_consume_token (parser->lexer);
21289 /* Accept = default or = delete in c++0x mode. */
21290 if (token->keyword == RID_DEFAULT
21291 || token->keyword == RID_DELETE)
21293 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21294 return token->u.value;
21297 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21298 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21300 cp_parser_error (parser,
21301 "invalid pure specifier (only %<= 0%> is allowed)");
21302 cp_parser_skip_to_end_of_statement (parser);
21303 return error_mark_node;
21305 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21307 error_at (token->location, "templates may not be %<virtual%>");
21308 return error_mark_node;
21311 return integer_zero_node;
21314 /* Parse a constant-initializer.
21316 constant-initializer:
21317 = constant-expression
21319 Returns a representation of the constant-expression. */
21321 static tree
21322 cp_parser_constant_initializer (cp_parser* parser)
21324 /* Look for the `=' token. */
21325 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21326 return error_mark_node;
21328 /* It is invalid to write:
21330 struct S { static const int i = { 7 }; };
21333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21335 cp_parser_error (parser,
21336 "a brace-enclosed initializer is not allowed here");
21337 /* Consume the opening brace. */
21338 cp_lexer_consume_token (parser->lexer);
21339 /* Skip the initializer. */
21340 cp_parser_skip_to_closing_brace (parser);
21341 /* Look for the trailing `}'. */
21342 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21344 return error_mark_node;
21347 return cp_parser_constant_expression (parser);
21350 /* Derived classes [gram.class.derived] */
21352 /* Parse a base-clause.
21354 base-clause:
21355 : base-specifier-list
21357 base-specifier-list:
21358 base-specifier ... [opt]
21359 base-specifier-list , base-specifier ... [opt]
21361 Returns a TREE_LIST representing the base-classes, in the order in
21362 which they were declared. The representation of each node is as
21363 described by cp_parser_base_specifier.
21365 In the case that no bases are specified, this function will return
21366 NULL_TREE, not ERROR_MARK_NODE. */
21368 static tree
21369 cp_parser_base_clause (cp_parser* parser)
21371 tree bases = NULL_TREE;
21373 /* Look for the `:' that begins the list. */
21374 cp_parser_require (parser, CPP_COLON, RT_COLON);
21376 /* Scan the base-specifier-list. */
21377 while (true)
21379 cp_token *token;
21380 tree base;
21381 bool pack_expansion_p = false;
21383 /* Look for the base-specifier. */
21384 base = cp_parser_base_specifier (parser);
21385 /* Look for the (optional) ellipsis. */
21386 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21388 /* Consume the `...'. */
21389 cp_lexer_consume_token (parser->lexer);
21391 pack_expansion_p = true;
21394 /* Add BASE to the front of the list. */
21395 if (base && base != error_mark_node)
21397 if (pack_expansion_p)
21398 /* Make this a pack expansion type. */
21399 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21401 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21403 TREE_CHAIN (base) = bases;
21404 bases = base;
21407 /* Peek at the next token. */
21408 token = cp_lexer_peek_token (parser->lexer);
21409 /* If it's not a comma, then the list is complete. */
21410 if (token->type != CPP_COMMA)
21411 break;
21412 /* Consume the `,'. */
21413 cp_lexer_consume_token (parser->lexer);
21416 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21417 base class had a qualified name. However, the next name that
21418 appears is certainly not qualified. */
21419 parser->scope = NULL_TREE;
21420 parser->qualifying_scope = NULL_TREE;
21421 parser->object_scope = NULL_TREE;
21423 return nreverse (bases);
21426 /* Parse a base-specifier.
21428 base-specifier:
21429 :: [opt] nested-name-specifier [opt] class-name
21430 virtual access-specifier [opt] :: [opt] nested-name-specifier
21431 [opt] class-name
21432 access-specifier virtual [opt] :: [opt] nested-name-specifier
21433 [opt] class-name
21435 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21436 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21437 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21438 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21440 static tree
21441 cp_parser_base_specifier (cp_parser* parser)
21443 cp_token *token;
21444 bool done = false;
21445 bool virtual_p = false;
21446 bool duplicate_virtual_error_issued_p = false;
21447 bool duplicate_access_error_issued_p = false;
21448 bool class_scope_p, template_p;
21449 tree access = access_default_node;
21450 tree type;
21452 /* Process the optional `virtual' and `access-specifier'. */
21453 while (!done)
21455 /* Peek at the next token. */
21456 token = cp_lexer_peek_token (parser->lexer);
21457 /* Process `virtual'. */
21458 switch (token->keyword)
21460 case RID_VIRTUAL:
21461 /* If `virtual' appears more than once, issue an error. */
21462 if (virtual_p && !duplicate_virtual_error_issued_p)
21464 cp_parser_error (parser,
21465 "%<virtual%> specified more than once in base-specified");
21466 duplicate_virtual_error_issued_p = true;
21469 virtual_p = true;
21471 /* Consume the `virtual' token. */
21472 cp_lexer_consume_token (parser->lexer);
21474 break;
21476 case RID_PUBLIC:
21477 case RID_PROTECTED:
21478 case RID_PRIVATE:
21479 /* If more than one access specifier appears, issue an
21480 error. */
21481 if (access != access_default_node
21482 && !duplicate_access_error_issued_p)
21484 cp_parser_error (parser,
21485 "more than one access specifier in base-specified");
21486 duplicate_access_error_issued_p = true;
21489 access = ridpointers[(int) token->keyword];
21491 /* Consume the access-specifier. */
21492 cp_lexer_consume_token (parser->lexer);
21494 break;
21496 default:
21497 done = true;
21498 break;
21501 /* It is not uncommon to see programs mechanically, erroneously, use
21502 the 'typename' keyword to denote (dependent) qualified types
21503 as base classes. */
21504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21506 token = cp_lexer_peek_token (parser->lexer);
21507 if (!processing_template_decl)
21508 error_at (token->location,
21509 "keyword %<typename%> not allowed outside of templates");
21510 else
21511 error_at (token->location,
21512 "keyword %<typename%> not allowed in this context "
21513 "(the base class is implicitly a type)");
21514 cp_lexer_consume_token (parser->lexer);
21517 /* Look for the optional `::' operator. */
21518 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21519 /* Look for the nested-name-specifier. The simplest way to
21520 implement:
21522 [temp.res]
21524 The keyword `typename' is not permitted in a base-specifier or
21525 mem-initializer; in these contexts a qualified name that
21526 depends on a template-parameter is implicitly assumed to be a
21527 type name.
21529 is to pretend that we have seen the `typename' keyword at this
21530 point. */
21531 cp_parser_nested_name_specifier_opt (parser,
21532 /*typename_keyword_p=*/true,
21533 /*check_dependency_p=*/true,
21534 typename_type,
21535 /*is_declaration=*/true);
21536 /* If the base class is given by a qualified name, assume that names
21537 we see are type names or templates, as appropriate. */
21538 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21539 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21541 if (!parser->scope
21542 && cp_lexer_next_token_is_decltype (parser->lexer))
21543 /* DR 950 allows decltype as a base-specifier. */
21544 type = cp_parser_decltype (parser);
21545 else
21547 /* Otherwise, look for the class-name. */
21548 type = cp_parser_class_name (parser,
21549 class_scope_p,
21550 template_p,
21551 typename_type,
21552 /*check_dependency_p=*/true,
21553 /*class_head_p=*/false,
21554 /*is_declaration=*/true);
21555 type = TREE_TYPE (type);
21558 if (type == error_mark_node)
21559 return error_mark_node;
21561 return finish_base_specifier (type, access, virtual_p);
21564 /* Exception handling [gram.exception] */
21566 /* Parse an (optional) noexcept-specification.
21568 noexcept-specification:
21569 noexcept ( constant-expression ) [opt]
21571 If no noexcept-specification is present, returns NULL_TREE.
21572 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21573 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21574 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21575 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21576 in which case a boolean condition is returned instead. */
21578 static tree
21579 cp_parser_noexcept_specification_opt (cp_parser* parser,
21580 bool require_constexpr,
21581 bool* consumed_expr,
21582 bool return_cond)
21584 cp_token *token;
21585 const char *saved_message;
21587 /* Peek at the next token. */
21588 token = cp_lexer_peek_token (parser->lexer);
21590 /* Is it a noexcept-specification? */
21591 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21593 tree expr;
21594 cp_lexer_consume_token (parser->lexer);
21596 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21598 cp_lexer_consume_token (parser->lexer);
21600 if (require_constexpr)
21602 /* Types may not be defined in an exception-specification. */
21603 saved_message = parser->type_definition_forbidden_message;
21604 parser->type_definition_forbidden_message
21605 = G_("types may not be defined in an exception-specification");
21607 expr = cp_parser_constant_expression (parser);
21608 /* Restore the saved message. */
21609 parser->type_definition_forbidden_message = saved_message;
21611 else
21613 expr = cp_parser_expression (parser);
21614 *consumed_expr = true;
21617 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21619 else
21621 expr = boolean_true_node;
21622 if (!require_constexpr)
21623 *consumed_expr = false;
21626 /* We cannot build a noexcept-spec right away because this will check
21627 that expr is a constexpr. */
21628 if (!return_cond)
21629 return build_noexcept_spec (expr, tf_warning_or_error);
21630 else
21631 return expr;
21633 else
21634 return NULL_TREE;
21637 /* Parse an (optional) exception-specification.
21639 exception-specification:
21640 throw ( type-id-list [opt] )
21642 Returns a TREE_LIST representing the exception-specification. The
21643 TREE_VALUE of each node is a type. */
21645 static tree
21646 cp_parser_exception_specification_opt (cp_parser* parser)
21648 cp_token *token;
21649 tree type_id_list;
21650 const char *saved_message;
21652 /* Peek at the next token. */
21653 token = cp_lexer_peek_token (parser->lexer);
21655 /* Is it a noexcept-specification? */
21656 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21657 false);
21658 if (type_id_list != NULL_TREE)
21659 return type_id_list;
21661 /* If it's not `throw', then there's no exception-specification. */
21662 if (!cp_parser_is_keyword (token, RID_THROW))
21663 return NULL_TREE;
21665 #if 0
21666 /* Enable this once a lot of code has transitioned to noexcept? */
21667 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21668 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21669 "deprecated in C++0x; use %<noexcept%> instead");
21670 #endif
21672 /* Consume the `throw'. */
21673 cp_lexer_consume_token (parser->lexer);
21675 /* Look for the `('. */
21676 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21678 /* Peek at the next token. */
21679 token = cp_lexer_peek_token (parser->lexer);
21680 /* If it's not a `)', then there is a type-id-list. */
21681 if (token->type != CPP_CLOSE_PAREN)
21683 /* Types may not be defined in an exception-specification. */
21684 saved_message = parser->type_definition_forbidden_message;
21685 parser->type_definition_forbidden_message
21686 = G_("types may not be defined in an exception-specification");
21687 /* Parse the type-id-list. */
21688 type_id_list = cp_parser_type_id_list (parser);
21689 /* Restore the saved message. */
21690 parser->type_definition_forbidden_message = saved_message;
21692 else
21693 type_id_list = empty_except_spec;
21695 /* Look for the `)'. */
21696 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21698 return type_id_list;
21701 /* Parse an (optional) type-id-list.
21703 type-id-list:
21704 type-id ... [opt]
21705 type-id-list , type-id ... [opt]
21707 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21708 in the order that the types were presented. */
21710 static tree
21711 cp_parser_type_id_list (cp_parser* parser)
21713 tree types = NULL_TREE;
21715 while (true)
21717 cp_token *token;
21718 tree type;
21720 /* Get the next type-id. */
21721 type = cp_parser_type_id (parser);
21722 /* Parse the optional ellipsis. */
21723 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21725 /* Consume the `...'. */
21726 cp_lexer_consume_token (parser->lexer);
21728 /* Turn the type into a pack expansion expression. */
21729 type = make_pack_expansion (type);
21731 /* Add it to the list. */
21732 types = add_exception_specifier (types, type, /*complain=*/1);
21733 /* Peek at the next token. */
21734 token = cp_lexer_peek_token (parser->lexer);
21735 /* If it is not a `,', we are done. */
21736 if (token->type != CPP_COMMA)
21737 break;
21738 /* Consume the `,'. */
21739 cp_lexer_consume_token (parser->lexer);
21742 return nreverse (types);
21745 /* Parse a try-block.
21747 try-block:
21748 try compound-statement handler-seq */
21750 static tree
21751 cp_parser_try_block (cp_parser* parser)
21753 tree try_block;
21755 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21756 if (parser->in_function_body
21757 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21758 error ("%<try%> in %<constexpr%> function");
21760 try_block = begin_try_block ();
21761 cp_parser_compound_statement (parser, NULL, true, false);
21762 finish_try_block (try_block);
21763 cp_parser_handler_seq (parser);
21764 finish_handler_sequence (try_block);
21766 return try_block;
21769 /* Parse a function-try-block.
21771 function-try-block:
21772 try ctor-initializer [opt] function-body handler-seq */
21774 static bool
21775 cp_parser_function_try_block (cp_parser* parser)
21777 tree compound_stmt;
21778 tree try_block;
21779 bool ctor_initializer_p;
21781 /* Look for the `try' keyword. */
21782 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21783 return false;
21784 /* Let the rest of the front end know where we are. */
21785 try_block = begin_function_try_block (&compound_stmt);
21786 /* Parse the function-body. */
21787 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21788 (parser, /*in_function_try_block=*/true);
21789 /* We're done with the `try' part. */
21790 finish_function_try_block (try_block);
21791 /* Parse the handlers. */
21792 cp_parser_handler_seq (parser);
21793 /* We're done with the handlers. */
21794 finish_function_handler_sequence (try_block, compound_stmt);
21796 return ctor_initializer_p;
21799 /* Parse a handler-seq.
21801 handler-seq:
21802 handler handler-seq [opt] */
21804 static void
21805 cp_parser_handler_seq (cp_parser* parser)
21807 while (true)
21809 cp_token *token;
21811 /* Parse the handler. */
21812 cp_parser_handler (parser);
21813 /* Peek at the next token. */
21814 token = cp_lexer_peek_token (parser->lexer);
21815 /* If it's not `catch' then there are no more handlers. */
21816 if (!cp_parser_is_keyword (token, RID_CATCH))
21817 break;
21821 /* Parse a handler.
21823 handler:
21824 catch ( exception-declaration ) compound-statement */
21826 static void
21827 cp_parser_handler (cp_parser* parser)
21829 tree handler;
21830 tree declaration;
21832 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21833 handler = begin_handler ();
21834 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21835 declaration = cp_parser_exception_declaration (parser);
21836 finish_handler_parms (declaration, handler);
21837 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21838 cp_parser_compound_statement (parser, NULL, false, false);
21839 finish_handler (handler);
21842 /* Parse an exception-declaration.
21844 exception-declaration:
21845 type-specifier-seq declarator
21846 type-specifier-seq abstract-declarator
21847 type-specifier-seq
21850 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21851 ellipsis variant is used. */
21853 static tree
21854 cp_parser_exception_declaration (cp_parser* parser)
21856 cp_decl_specifier_seq type_specifiers;
21857 cp_declarator *declarator;
21858 const char *saved_message;
21860 /* If it's an ellipsis, it's easy to handle. */
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21863 /* Consume the `...' token. */
21864 cp_lexer_consume_token (parser->lexer);
21865 return NULL_TREE;
21868 /* Types may not be defined in exception-declarations. */
21869 saved_message = parser->type_definition_forbidden_message;
21870 parser->type_definition_forbidden_message
21871 = G_("types may not be defined in exception-declarations");
21873 /* Parse the type-specifier-seq. */
21874 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21875 /*is_trailing_return=*/false,
21876 &type_specifiers);
21877 /* If it's a `)', then there is no declarator. */
21878 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21879 declarator = NULL;
21880 else
21881 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21882 /*ctor_dtor_or_conv_p=*/NULL,
21883 /*parenthesized_p=*/NULL,
21884 /*member_p=*/false,
21885 /*friend_p=*/false);
21887 /* Restore the saved message. */
21888 parser->type_definition_forbidden_message = saved_message;
21890 if (!type_specifiers.any_specifiers_p)
21891 return error_mark_node;
21893 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21896 /* Parse a throw-expression.
21898 throw-expression:
21899 throw assignment-expression [opt]
21901 Returns a THROW_EXPR representing the throw-expression. */
21903 static tree
21904 cp_parser_throw_expression (cp_parser* parser)
21906 tree expression;
21907 cp_token* token;
21909 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21910 token = cp_lexer_peek_token (parser->lexer);
21911 /* Figure out whether or not there is an assignment-expression
21912 following the "throw" keyword. */
21913 if (token->type == CPP_COMMA
21914 || token->type == CPP_SEMICOLON
21915 || token->type == CPP_CLOSE_PAREN
21916 || token->type == CPP_CLOSE_SQUARE
21917 || token->type == CPP_CLOSE_BRACE
21918 || token->type == CPP_COLON)
21919 expression = NULL_TREE;
21920 else
21921 expression = cp_parser_assignment_expression (parser);
21923 return build_throw (expression);
21926 /* GNU Extensions */
21928 /* Parse an (optional) asm-specification.
21930 asm-specification:
21931 asm ( string-literal )
21933 If the asm-specification is present, returns a STRING_CST
21934 corresponding to the string-literal. Otherwise, returns
21935 NULL_TREE. */
21937 static tree
21938 cp_parser_asm_specification_opt (cp_parser* parser)
21940 cp_token *token;
21941 tree asm_specification;
21943 /* Peek at the next token. */
21944 token = cp_lexer_peek_token (parser->lexer);
21945 /* If the next token isn't the `asm' keyword, then there's no
21946 asm-specification. */
21947 if (!cp_parser_is_keyword (token, RID_ASM))
21948 return NULL_TREE;
21950 /* Consume the `asm' token. */
21951 cp_lexer_consume_token (parser->lexer);
21952 /* Look for the `('. */
21953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21955 /* Look for the string-literal. */
21956 asm_specification = cp_parser_string_literal (parser, false, false);
21958 /* Look for the `)'. */
21959 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21961 return asm_specification;
21964 /* Parse an asm-operand-list.
21966 asm-operand-list:
21967 asm-operand
21968 asm-operand-list , asm-operand
21970 asm-operand:
21971 string-literal ( expression )
21972 [ string-literal ] string-literal ( expression )
21974 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21975 each node is the expression. The TREE_PURPOSE is itself a
21976 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21977 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21978 is a STRING_CST for the string literal before the parenthesis. Returns
21979 ERROR_MARK_NODE if any of the operands are invalid. */
21981 static tree
21982 cp_parser_asm_operand_list (cp_parser* parser)
21984 tree asm_operands = NULL_TREE;
21985 bool invalid_operands = false;
21987 while (true)
21989 tree string_literal;
21990 tree expression;
21991 tree name;
21993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21995 /* Consume the `[' token. */
21996 cp_lexer_consume_token (parser->lexer);
21997 /* Read the operand name. */
21998 name = cp_parser_identifier (parser);
21999 if (name != error_mark_node)
22000 name = build_string (IDENTIFIER_LENGTH (name),
22001 IDENTIFIER_POINTER (name));
22002 /* Look for the closing `]'. */
22003 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22005 else
22006 name = NULL_TREE;
22007 /* Look for the string-literal. */
22008 string_literal = cp_parser_string_literal (parser, false, false);
22010 /* Look for the `('. */
22011 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22012 /* Parse the expression. */
22013 expression = cp_parser_expression (parser);
22014 /* Look for the `)'. */
22015 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22017 if (name == error_mark_node
22018 || string_literal == error_mark_node
22019 || expression == error_mark_node)
22020 invalid_operands = true;
22022 /* Add this operand to the list. */
22023 asm_operands = tree_cons (build_tree_list (name, string_literal),
22024 expression,
22025 asm_operands);
22026 /* If the next token is not a `,', there are no more
22027 operands. */
22028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22029 break;
22030 /* Consume the `,'. */
22031 cp_lexer_consume_token (parser->lexer);
22034 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22037 /* Parse an asm-clobber-list.
22039 asm-clobber-list:
22040 string-literal
22041 asm-clobber-list , string-literal
22043 Returns a TREE_LIST, indicating the clobbers in the order that they
22044 appeared. The TREE_VALUE of each node is a STRING_CST. */
22046 static tree
22047 cp_parser_asm_clobber_list (cp_parser* parser)
22049 tree clobbers = NULL_TREE;
22051 while (true)
22053 tree string_literal;
22055 /* Look for the string literal. */
22056 string_literal = cp_parser_string_literal (parser, false, false);
22057 /* Add it to the list. */
22058 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22059 /* If the next token is not a `,', then the list is
22060 complete. */
22061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22062 break;
22063 /* Consume the `,' token. */
22064 cp_lexer_consume_token (parser->lexer);
22067 return clobbers;
22070 /* Parse an asm-label-list.
22072 asm-label-list:
22073 identifier
22074 asm-label-list , identifier
22076 Returns a TREE_LIST, indicating the labels in the order that they
22077 appeared. The TREE_VALUE of each node is a label. */
22079 static tree
22080 cp_parser_asm_label_list (cp_parser* parser)
22082 tree labels = NULL_TREE;
22084 while (true)
22086 tree identifier, label, name;
22088 /* Look for the identifier. */
22089 identifier = cp_parser_identifier (parser);
22090 if (!error_operand_p (identifier))
22092 label = lookup_label (identifier);
22093 if (TREE_CODE (label) == LABEL_DECL)
22095 TREE_USED (label) = 1;
22096 check_goto (label);
22097 name = build_string (IDENTIFIER_LENGTH (identifier),
22098 IDENTIFIER_POINTER (identifier));
22099 labels = tree_cons (name, label, labels);
22102 /* If the next token is not a `,', then the list is
22103 complete. */
22104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22105 break;
22106 /* Consume the `,' token. */
22107 cp_lexer_consume_token (parser->lexer);
22110 return nreverse (labels);
22113 /* Return TRUE iff the next tokens in the stream are possibly the
22114 beginning of a GNU extension attribute. */
22116 static bool
22117 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22119 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22122 /* Return TRUE iff the next tokens in the stream are possibly the
22123 beginning of a standard C++-11 attribute specifier. */
22125 static bool
22126 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22128 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22131 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22132 beginning of a standard C++-11 attribute specifier. */
22134 static bool
22135 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22137 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22139 return (cxx_dialect >= cxx11
22140 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22141 || (token->type == CPP_OPEN_SQUARE
22142 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22143 && token->type == CPP_OPEN_SQUARE)));
22146 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22147 beginning of a GNU extension attribute. */
22149 static bool
22150 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22152 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22154 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22157 /* Return true iff the next tokens can be the beginning of either a
22158 GNU attribute list, or a standard C++11 attribute sequence. */
22160 static bool
22161 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22163 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22164 || cp_next_tokens_can_be_std_attribute_p (parser));
22167 /* Return true iff the next Nth tokens can be the beginning of either
22168 a GNU attribute list, or a standard C++11 attribute sequence. */
22170 static bool
22171 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22173 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22174 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22177 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22178 of GNU attributes, or return NULL. */
22180 static tree
22181 cp_parser_attributes_opt (cp_parser *parser)
22183 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22184 return cp_parser_gnu_attributes_opt (parser);
22185 return cp_parser_std_attribute_spec_seq (parser);
22188 #define CILK_SIMD_FN_CLAUSE_MASK \
22189 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22190 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22191 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22192 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22193 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22195 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22196 vector [(<clauses>)] */
22198 static void
22199 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22201 bool first_p = parser->cilk_simd_fn_info == NULL;
22202 cp_token *token = v_token;
22203 if (first_p)
22205 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22206 parser->cilk_simd_fn_info->error_seen = false;
22207 parser->cilk_simd_fn_info->fndecl_seen = false;
22208 parser->cilk_simd_fn_info->tokens = vNULL;
22210 int paren_scope = 0;
22211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22213 cp_lexer_consume_token (parser->lexer);
22214 v_token = cp_lexer_peek_token (parser->lexer);
22215 paren_scope++;
22217 while (paren_scope > 0)
22219 token = cp_lexer_peek_token (parser->lexer);
22220 if (token->type == CPP_OPEN_PAREN)
22221 paren_scope++;
22222 else if (token->type == CPP_CLOSE_PAREN)
22223 paren_scope--;
22224 /* Do not push the last ')' */
22225 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22226 cp_lexer_consume_token (parser->lexer);
22229 token->type = CPP_PRAGMA_EOL;
22230 parser->lexer->next_token = token;
22231 cp_lexer_consume_token (parser->lexer);
22233 struct cp_token_cache *cp
22234 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22235 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22238 /* Parse an (optional) series of attributes.
22240 attributes:
22241 attributes attribute
22243 attribute:
22244 __attribute__ (( attribute-list [opt] ))
22246 The return value is as for cp_parser_gnu_attribute_list. */
22248 static tree
22249 cp_parser_gnu_attributes_opt (cp_parser* parser)
22251 tree attributes = NULL_TREE;
22253 while (true)
22255 cp_token *token;
22256 tree attribute_list;
22257 bool ok = true;
22259 /* Peek at the next token. */
22260 token = cp_lexer_peek_token (parser->lexer);
22261 /* If it's not `__attribute__', then we're done. */
22262 if (token->keyword != RID_ATTRIBUTE)
22263 break;
22265 /* Consume the `__attribute__' keyword. */
22266 cp_lexer_consume_token (parser->lexer);
22267 /* Look for the two `(' tokens. */
22268 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22269 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22271 /* Peek at the next token. */
22272 token = cp_lexer_peek_token (parser->lexer);
22273 if (token->type != CPP_CLOSE_PAREN)
22274 /* Parse the attribute-list. */
22275 attribute_list = cp_parser_gnu_attribute_list (parser);
22276 else
22277 /* If the next token is a `)', then there is no attribute
22278 list. */
22279 attribute_list = NULL;
22281 /* Look for the two `)' tokens. */
22282 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22283 ok = false;
22284 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22285 ok = false;
22286 if (!ok)
22287 cp_parser_skip_to_end_of_statement (parser);
22289 /* Add these new attributes to the list. */
22290 attributes = chainon (attributes, attribute_list);
22293 return attributes;
22296 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22297 "__vector" or "__vector__." */
22299 static inline bool
22300 is_cilkplus_vector_p (tree name)
22302 if (flag_cilkplus && is_attribute_p ("vector", name))
22303 return true;
22304 return false;
22307 /* Parse a GNU attribute-list.
22309 attribute-list:
22310 attribute
22311 attribute-list , attribute
22313 attribute:
22314 identifier
22315 identifier ( identifier )
22316 identifier ( identifier , expression-list )
22317 identifier ( expression-list )
22319 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22320 to an attribute. The TREE_PURPOSE of each node is the identifier
22321 indicating which attribute is in use. The TREE_VALUE represents
22322 the arguments, if any. */
22324 static tree
22325 cp_parser_gnu_attribute_list (cp_parser* parser)
22327 tree attribute_list = NULL_TREE;
22328 bool save_translate_strings_p = parser->translate_strings_p;
22330 parser->translate_strings_p = false;
22331 while (true)
22333 cp_token *token;
22334 tree identifier;
22335 tree attribute;
22337 /* Look for the identifier. We also allow keywords here; for
22338 example `__attribute__ ((const))' is legal. */
22339 token = cp_lexer_peek_token (parser->lexer);
22340 if (token->type == CPP_NAME
22341 || token->type == CPP_KEYWORD)
22343 tree arguments = NULL_TREE;
22345 /* Consume the token, but save it since we need it for the
22346 SIMD enabled function parsing. */
22347 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22349 /* Save away the identifier that indicates which attribute
22350 this is. */
22351 identifier = (token->type == CPP_KEYWORD)
22352 /* For keywords, use the canonical spelling, not the
22353 parsed identifier. */
22354 ? ridpointers[(int) token->keyword]
22355 : id_token->u.value;
22357 attribute = build_tree_list (identifier, NULL_TREE);
22359 /* Peek at the next token. */
22360 token = cp_lexer_peek_token (parser->lexer);
22361 /* If it's an `(', then parse the attribute arguments. */
22362 if (token->type == CPP_OPEN_PAREN)
22364 vec<tree, va_gc> *vec;
22365 int attr_flag = (attribute_takes_identifier_p (identifier)
22366 ? id_attr : normal_attr);
22367 if (is_cilkplus_vector_p (identifier))
22369 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22370 continue;
22372 else
22373 vec = cp_parser_parenthesized_expression_list
22374 (parser, attr_flag, /*cast_p=*/false,
22375 /*allow_expansion_p=*/false,
22376 /*non_constant_p=*/NULL);
22377 if (vec == NULL)
22378 arguments = error_mark_node;
22379 else
22381 arguments = build_tree_list_vec (vec);
22382 release_tree_vector (vec);
22384 /* Save the arguments away. */
22385 TREE_VALUE (attribute) = arguments;
22387 else if (is_cilkplus_vector_p (identifier))
22389 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22390 continue;
22393 if (arguments != error_mark_node)
22395 /* Add this attribute to the list. */
22396 TREE_CHAIN (attribute) = attribute_list;
22397 attribute_list = attribute;
22400 token = cp_lexer_peek_token (parser->lexer);
22402 /* Now, look for more attributes. If the next token isn't a
22403 `,', we're done. */
22404 if (token->type != CPP_COMMA)
22405 break;
22407 /* Consume the comma and keep going. */
22408 cp_lexer_consume_token (parser->lexer);
22410 parser->translate_strings_p = save_translate_strings_p;
22412 /* We built up the list in reverse order. */
22413 return nreverse (attribute_list);
22416 /* Parse a standard C++11 attribute.
22418 The returned representation is a TREE_LIST which TREE_PURPOSE is
22419 the scoped name of the attribute, and the TREE_VALUE is its
22420 arguments list.
22422 Note that the scoped name of the attribute is itself a TREE_LIST
22423 which TREE_PURPOSE is the namespace of the attribute, and
22424 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22425 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22426 and which TREE_PURPOSE is directly the attribute name.
22428 Clients of the attribute code should use get_attribute_namespace
22429 and get_attribute_name to get the actual namespace and name of
22430 attributes, regardless of their being GNU or C++11 attributes.
22432 attribute:
22433 attribute-token attribute-argument-clause [opt]
22435 attribute-token:
22436 identifier
22437 attribute-scoped-token
22439 attribute-scoped-token:
22440 attribute-namespace :: identifier
22442 attribute-namespace:
22443 identifier
22445 attribute-argument-clause:
22446 ( balanced-token-seq )
22448 balanced-token-seq:
22449 balanced-token [opt]
22450 balanced-token-seq balanced-token
22452 balanced-token:
22453 ( balanced-token-seq )
22454 [ balanced-token-seq ]
22455 { balanced-token-seq }. */
22457 static tree
22458 cp_parser_std_attribute (cp_parser *parser)
22460 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22461 cp_token *token;
22463 /* First, parse name of the the attribute, a.k.a
22464 attribute-token. */
22466 token = cp_lexer_peek_token (parser->lexer);
22467 if (token->type == CPP_NAME)
22468 attr_id = token->u.value;
22469 else if (token->type == CPP_KEYWORD)
22470 attr_id = ridpointers[(int) token->keyword];
22471 else if (token->flags & NAMED_OP)
22472 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22474 if (attr_id == NULL_TREE)
22475 return NULL_TREE;
22477 cp_lexer_consume_token (parser->lexer);
22479 token = cp_lexer_peek_token (parser->lexer);
22480 if (token->type == CPP_SCOPE)
22482 /* We are seeing a scoped attribute token. */
22484 cp_lexer_consume_token (parser->lexer);
22485 attr_ns = attr_id;
22487 token = cp_lexer_consume_token (parser->lexer);
22488 if (token->type == CPP_NAME)
22489 attr_id = token->u.value;
22490 else if (token->type == CPP_KEYWORD)
22491 attr_id = ridpointers[(int) token->keyword];
22492 else
22494 error_at (token->location,
22495 "expected an identifier for the attribute name");
22496 return error_mark_node;
22498 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22499 NULL_TREE);
22500 token = cp_lexer_peek_token (parser->lexer);
22502 else
22504 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22505 NULL_TREE);
22506 /* C++11 noreturn attribute is equivalent to GNU's. */
22507 if (is_attribute_p ("noreturn", attr_id))
22508 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22509 /* C++14 deprecated attribute is equivalent to GNU's. */
22510 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22512 if (cxx_dialect == cxx11)
22513 pedwarn (token->location, OPT_Wpedantic,
22514 "%<deprecated%> is a C++14 feature;"
22515 " use %<gnu::deprecated%>");
22516 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22520 /* Now parse the optional argument clause of the attribute. */
22522 if (token->type != CPP_OPEN_PAREN)
22523 return attribute;
22526 vec<tree, va_gc> *vec;
22527 int attr_flag = normal_attr;
22529 if (attr_ns == get_identifier ("gnu")
22530 && attribute_takes_identifier_p (attr_id))
22531 /* A GNU attribute that takes an identifier in parameter. */
22532 attr_flag = id_attr;
22534 vec = cp_parser_parenthesized_expression_list
22535 (parser, attr_flag, /*cast_p=*/false,
22536 /*allow_expansion_p=*/true,
22537 /*non_constant_p=*/NULL);
22538 if (vec == NULL)
22539 arguments = error_mark_node;
22540 else
22542 arguments = build_tree_list_vec (vec);
22543 release_tree_vector (vec);
22546 if (arguments == error_mark_node)
22547 attribute = error_mark_node;
22548 else
22549 TREE_VALUE (attribute) = arguments;
22552 return attribute;
22555 /* Check that the attribute ATTRIBUTE appears at most once in the
22556 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
22557 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
22558 isn't implemented yet in GCC. */
22560 static void
22561 cp_parser_check_std_attribute (tree attributes, tree attribute)
22563 if (attributes)
22565 tree name = get_attribute_name (attribute);
22566 if (is_attribute_p ("noreturn", name)
22567 && lookup_attribute ("noreturn", attributes))
22568 error ("attribute noreturn can appear at most once "
22569 "in an attribute-list");
22570 else if (is_attribute_p ("deprecated", name)
22571 && lookup_attribute ("deprecated", attributes))
22572 error ("attribute deprecated can appear at most once "
22573 "in an attribute-list");
22577 /* Parse a list of standard C++-11 attributes.
22579 attribute-list:
22580 attribute [opt]
22581 attribute-list , attribute[opt]
22582 attribute ...
22583 attribute-list , attribute ...
22586 static tree
22587 cp_parser_std_attribute_list (cp_parser *parser)
22589 tree attributes = NULL_TREE, attribute = NULL_TREE;
22590 cp_token *token = NULL;
22592 while (true)
22594 attribute = cp_parser_std_attribute (parser);
22595 if (attribute == error_mark_node)
22596 break;
22597 if (attribute != NULL_TREE)
22599 cp_parser_check_std_attribute (attributes, attribute);
22600 TREE_CHAIN (attribute) = attributes;
22601 attributes = attribute;
22603 token = cp_lexer_peek_token (parser->lexer);
22604 if (token->type == CPP_ELLIPSIS)
22606 cp_lexer_consume_token (parser->lexer);
22607 TREE_VALUE (attribute)
22608 = make_pack_expansion (TREE_VALUE (attribute));
22609 token = cp_lexer_peek_token (parser->lexer);
22611 if (token->type != CPP_COMMA)
22612 break;
22613 cp_lexer_consume_token (parser->lexer);
22615 attributes = nreverse (attributes);
22616 return attributes;
22619 /* Parse a standard C++-11 attribute specifier.
22621 attribute-specifier:
22622 [ [ attribute-list ] ]
22623 alignment-specifier
22625 alignment-specifier:
22626 alignas ( type-id ... [opt] )
22627 alignas ( alignment-expression ... [opt] ). */
22629 static tree
22630 cp_parser_std_attribute_spec (cp_parser *parser)
22632 tree attributes = NULL_TREE;
22633 cp_token *token = cp_lexer_peek_token (parser->lexer);
22635 if (token->type == CPP_OPEN_SQUARE
22636 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22638 cp_lexer_consume_token (parser->lexer);
22639 cp_lexer_consume_token (parser->lexer);
22641 attributes = cp_parser_std_attribute_list (parser);
22643 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22644 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22645 cp_parser_skip_to_end_of_statement (parser);
22646 else
22647 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22648 when we are sure that we have actually parsed them. */
22649 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22651 else
22653 tree alignas_expr;
22655 /* Look for an alignment-specifier. */
22657 token = cp_lexer_peek_token (parser->lexer);
22659 if (token->type != CPP_KEYWORD
22660 || token->keyword != RID_ALIGNAS)
22661 return NULL_TREE;
22663 cp_lexer_consume_token (parser->lexer);
22664 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22666 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22668 cp_parser_error (parser, "expected %<(%>");
22669 return error_mark_node;
22672 cp_parser_parse_tentatively (parser);
22673 alignas_expr = cp_parser_type_id (parser);
22675 if (!cp_parser_parse_definitely (parser))
22677 gcc_assert (alignas_expr == error_mark_node
22678 || alignas_expr == NULL_TREE);
22680 alignas_expr =
22681 cp_parser_assignment_expression (parser);
22682 if (alignas_expr == error_mark_node)
22683 cp_parser_skip_to_end_of_statement (parser);
22684 if (alignas_expr == NULL_TREE
22685 || alignas_expr == error_mark_node)
22686 return alignas_expr;
22689 alignas_expr = cxx_alignas_expr (alignas_expr);
22690 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22692 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22694 cp_lexer_consume_token (parser->lexer);
22695 alignas_expr = make_pack_expansion (alignas_expr);
22698 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22700 cp_parser_error (parser, "expected %<)%>");
22701 return error_mark_node;
22704 /* Build the C++-11 representation of an 'aligned'
22705 attribute. */
22706 attributes =
22707 build_tree_list (build_tree_list (get_identifier ("gnu"),
22708 get_identifier ("aligned")),
22709 alignas_expr);
22712 return attributes;
22715 /* Parse a standard C++-11 attribute-specifier-seq.
22717 attribute-specifier-seq:
22718 attribute-specifier-seq [opt] attribute-specifier
22721 static tree
22722 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22724 tree attr_specs = NULL;
22726 while (true)
22728 tree attr_spec = cp_parser_std_attribute_spec (parser);
22729 if (attr_spec == NULL_TREE)
22730 break;
22731 if (attr_spec == error_mark_node)
22732 return error_mark_node;
22734 TREE_CHAIN (attr_spec) = attr_specs;
22735 attr_specs = attr_spec;
22738 attr_specs = nreverse (attr_specs);
22739 return attr_specs;
22742 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22743 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22744 current value of the PEDANTIC flag, regardless of whether or not
22745 the `__extension__' keyword is present. The caller is responsible
22746 for restoring the value of the PEDANTIC flag. */
22748 static bool
22749 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22751 /* Save the old value of the PEDANTIC flag. */
22752 *saved_pedantic = pedantic;
22754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22756 /* Consume the `__extension__' token. */
22757 cp_lexer_consume_token (parser->lexer);
22758 /* We're not being pedantic while the `__extension__' keyword is
22759 in effect. */
22760 pedantic = 0;
22762 return true;
22765 return false;
22768 /* Parse a label declaration.
22770 label-declaration:
22771 __label__ label-declarator-seq ;
22773 label-declarator-seq:
22774 identifier , label-declarator-seq
22775 identifier */
22777 static void
22778 cp_parser_label_declaration (cp_parser* parser)
22780 /* Look for the `__label__' keyword. */
22781 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22783 while (true)
22785 tree identifier;
22787 /* Look for an identifier. */
22788 identifier = cp_parser_identifier (parser);
22789 /* If we failed, stop. */
22790 if (identifier == error_mark_node)
22791 break;
22792 /* Declare it as a label. */
22793 finish_label_decl (identifier);
22794 /* If the next token is a `;', stop. */
22795 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22796 break;
22797 /* Look for the `,' separating the label declarations. */
22798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22801 /* Look for the final `;'. */
22802 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22805 /* Support Functions */
22807 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22808 NAME should have one of the representations used for an
22809 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22810 is returned. If PARSER->SCOPE is a dependent type, then a
22811 SCOPE_REF is returned.
22813 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22814 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22815 was formed. Abstractly, such entities should not be passed to this
22816 function, because they do not need to be looked up, but it is
22817 simpler to check for this special case here, rather than at the
22818 call-sites.
22820 In cases not explicitly covered above, this function returns a
22821 DECL, OVERLOAD, or baselink representing the result of the lookup.
22822 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22823 is returned.
22825 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22826 (e.g., "struct") that was used. In that case bindings that do not
22827 refer to types are ignored.
22829 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22830 ignored.
22832 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22833 are ignored.
22835 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22836 types.
22838 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22839 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22840 NULL_TREE otherwise. */
22842 static tree
22843 cp_parser_lookup_name (cp_parser *parser, tree name,
22844 enum tag_types tag_type,
22845 bool is_template,
22846 bool is_namespace,
22847 bool check_dependency,
22848 tree *ambiguous_decls,
22849 location_t name_location)
22851 tree decl;
22852 tree object_type = parser->context->object_type;
22854 /* Assume that the lookup will be unambiguous. */
22855 if (ambiguous_decls)
22856 *ambiguous_decls = NULL_TREE;
22858 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22859 no longer valid. Note that if we are parsing tentatively, and
22860 the parse fails, OBJECT_TYPE will be automatically restored. */
22861 parser->context->object_type = NULL_TREE;
22863 if (name == error_mark_node)
22864 return error_mark_node;
22866 /* A template-id has already been resolved; there is no lookup to
22867 do. */
22868 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22869 return name;
22870 if (BASELINK_P (name))
22872 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22873 == TEMPLATE_ID_EXPR);
22874 return name;
22877 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22878 it should already have been checked to make sure that the name
22879 used matches the type being destroyed. */
22880 if (TREE_CODE (name) == BIT_NOT_EXPR)
22882 tree type;
22884 /* Figure out to which type this destructor applies. */
22885 if (parser->scope)
22886 type = parser->scope;
22887 else if (object_type)
22888 type = object_type;
22889 else
22890 type = current_class_type;
22891 /* If that's not a class type, there is no destructor. */
22892 if (!type || !CLASS_TYPE_P (type))
22893 return error_mark_node;
22894 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22895 lazily_declare_fn (sfk_destructor, type);
22896 if (!CLASSTYPE_DESTRUCTORS (type))
22897 return error_mark_node;
22898 /* If it was a class type, return the destructor. */
22899 return CLASSTYPE_DESTRUCTORS (type);
22902 /* By this point, the NAME should be an ordinary identifier. If
22903 the id-expression was a qualified name, the qualifying scope is
22904 stored in PARSER->SCOPE at this point. */
22905 gcc_assert (identifier_p (name));
22907 /* Perform the lookup. */
22908 if (parser->scope)
22910 bool dependent_p;
22912 if (parser->scope == error_mark_node)
22913 return error_mark_node;
22915 /* If the SCOPE is dependent, the lookup must be deferred until
22916 the template is instantiated -- unless we are explicitly
22917 looking up names in uninstantiated templates. Even then, we
22918 cannot look up the name if the scope is not a class type; it
22919 might, for example, be a template type parameter. */
22920 dependent_p = (TYPE_P (parser->scope)
22921 && dependent_scope_p (parser->scope));
22922 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22923 && dependent_p)
22924 /* Defer lookup. */
22925 decl = error_mark_node;
22926 else
22928 tree pushed_scope = NULL_TREE;
22930 /* If PARSER->SCOPE is a dependent type, then it must be a
22931 class type, and we must not be checking dependencies;
22932 otherwise, we would have processed this lookup above. So
22933 that PARSER->SCOPE is not considered a dependent base by
22934 lookup_member, we must enter the scope here. */
22935 if (dependent_p)
22936 pushed_scope = push_scope (parser->scope);
22938 /* If the PARSER->SCOPE is a template specialization, it
22939 may be instantiated during name lookup. In that case,
22940 errors may be issued. Even if we rollback the current
22941 tentative parse, those errors are valid. */
22942 decl = lookup_qualified_name (parser->scope, name,
22943 tag_type != none_type,
22944 /*complain=*/true);
22946 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22947 lookup result and the nested-name-specifier nominates a class C:
22948 * if the name specified after the nested-name-specifier, when
22949 looked up in C, is the injected-class-name of C (Clause 9), or
22950 * if the name specified after the nested-name-specifier is the
22951 same as the identifier or the simple-template-id's template-
22952 name in the last component of the nested-name-specifier,
22953 the name is instead considered to name the constructor of
22954 class C. [ Note: for example, the constructor is not an
22955 acceptable lookup result in an elaborated-type-specifier so
22956 the constructor would not be used in place of the
22957 injected-class-name. --end note ] Such a constructor name
22958 shall be used only in the declarator-id of a declaration that
22959 names a constructor or in a using-declaration. */
22960 if (tag_type == none_type
22961 && DECL_SELF_REFERENCE_P (decl)
22962 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22963 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22964 tag_type != none_type,
22965 /*complain=*/true);
22967 /* If we have a single function from a using decl, pull it out. */
22968 if (TREE_CODE (decl) == OVERLOAD
22969 && !really_overloaded_fn (decl))
22970 decl = OVL_FUNCTION (decl);
22972 if (pushed_scope)
22973 pop_scope (pushed_scope);
22976 /* If the scope is a dependent type and either we deferred lookup or
22977 we did lookup but didn't find the name, rememeber the name. */
22978 if (decl == error_mark_node && TYPE_P (parser->scope)
22979 && dependent_type_p (parser->scope))
22981 if (tag_type)
22983 tree type;
22985 /* The resolution to Core Issue 180 says that `struct
22986 A::B' should be considered a type-name, even if `A'
22987 is dependent. */
22988 type = make_typename_type (parser->scope, name, tag_type,
22989 /*complain=*/tf_error);
22990 if (type != error_mark_node)
22991 decl = TYPE_NAME (type);
22993 else if (is_template
22994 && (cp_parser_next_token_ends_template_argument_p (parser)
22995 || cp_lexer_next_token_is (parser->lexer,
22996 CPP_CLOSE_PAREN)))
22997 decl = make_unbound_class_template (parser->scope,
22998 name, NULL_TREE,
22999 /*complain=*/tf_error);
23000 else
23001 decl = build_qualified_name (/*type=*/NULL_TREE,
23002 parser->scope, name,
23003 is_template);
23005 parser->qualifying_scope = parser->scope;
23006 parser->object_scope = NULL_TREE;
23008 else if (object_type)
23010 /* Look up the name in the scope of the OBJECT_TYPE, unless the
23011 OBJECT_TYPE is not a class. */
23012 if (CLASS_TYPE_P (object_type))
23013 /* If the OBJECT_TYPE is a template specialization, it may
23014 be instantiated during name lookup. In that case, errors
23015 may be issued. Even if we rollback the current tentative
23016 parse, those errors are valid. */
23017 decl = lookup_member (object_type,
23018 name,
23019 /*protect=*/0,
23020 tag_type != none_type,
23021 tf_warning_or_error);
23022 else
23023 decl = NULL_TREE;
23025 if (!decl)
23026 /* Look it up in the enclosing context. */
23027 decl = lookup_name_real (name, tag_type != none_type,
23028 /*nonclass=*/0,
23029 /*block_p=*/true, is_namespace, 0);
23030 parser->object_scope = object_type;
23031 parser->qualifying_scope = NULL_TREE;
23033 else
23035 decl = lookup_name_real (name, tag_type != none_type,
23036 /*nonclass=*/0,
23037 /*block_p=*/true, is_namespace, 0);
23038 parser->qualifying_scope = NULL_TREE;
23039 parser->object_scope = NULL_TREE;
23042 /* If the lookup failed, let our caller know. */
23043 if (!decl || decl == error_mark_node)
23044 return error_mark_node;
23046 /* Pull out the template from an injected-class-name (or multiple). */
23047 if (is_template)
23048 decl = maybe_get_template_decl_from_type_decl (decl);
23050 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23051 if (TREE_CODE (decl) == TREE_LIST)
23053 if (ambiguous_decls)
23054 *ambiguous_decls = decl;
23055 /* The error message we have to print is too complicated for
23056 cp_parser_error, so we incorporate its actions directly. */
23057 if (!cp_parser_simulate_error (parser))
23059 error_at (name_location, "reference to %qD is ambiguous",
23060 name);
23061 print_candidates (decl);
23063 return error_mark_node;
23066 gcc_assert (DECL_P (decl)
23067 || TREE_CODE (decl) == OVERLOAD
23068 || TREE_CODE (decl) == SCOPE_REF
23069 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23070 || BASELINK_P (decl));
23072 /* If we have resolved the name of a member declaration, check to
23073 see if the declaration is accessible. When the name resolves to
23074 set of overloaded functions, accessibility is checked when
23075 overload resolution is done.
23077 During an explicit instantiation, access is not checked at all,
23078 as per [temp.explicit]. */
23079 if (DECL_P (decl))
23080 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23082 maybe_record_typedef_use (decl);
23084 return decl;
23087 /* Like cp_parser_lookup_name, but for use in the typical case where
23088 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23089 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23091 static tree
23092 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23094 return cp_parser_lookup_name (parser, name,
23095 none_type,
23096 /*is_template=*/false,
23097 /*is_namespace=*/false,
23098 /*check_dependency=*/true,
23099 /*ambiguous_decls=*/NULL,
23100 location);
23103 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23104 the current context, return the TYPE_DECL. If TAG_NAME_P is
23105 true, the DECL indicates the class being defined in a class-head,
23106 or declared in an elaborated-type-specifier.
23108 Otherwise, return DECL. */
23110 static tree
23111 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23113 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23114 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23116 struct A {
23117 template <typename T> struct B;
23120 template <typename T> struct A::B {};
23122 Similarly, in an elaborated-type-specifier:
23124 namespace N { struct X{}; }
23126 struct A {
23127 template <typename T> friend struct N::X;
23130 However, if the DECL refers to a class type, and we are in
23131 the scope of the class, then the name lookup automatically
23132 finds the TYPE_DECL created by build_self_reference rather
23133 than a TEMPLATE_DECL. For example, in:
23135 template <class T> struct S {
23136 S s;
23139 there is no need to handle such case. */
23141 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23142 return DECL_TEMPLATE_RESULT (decl);
23144 return decl;
23147 /* If too many, or too few, template-parameter lists apply to the
23148 declarator, issue an error message. Returns TRUE if all went well,
23149 and FALSE otherwise. */
23151 static bool
23152 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23153 cp_declarator *declarator,
23154 location_t declarator_location)
23156 switch (declarator->kind)
23158 case cdk_id:
23160 unsigned num_templates = 0;
23161 tree scope = declarator->u.id.qualifying_scope;
23163 if (scope)
23164 num_templates = num_template_headers_for_class (scope);
23165 else if (TREE_CODE (declarator->u.id.unqualified_name)
23166 == TEMPLATE_ID_EXPR)
23167 /* If the DECLARATOR has the form `X<y>' then it uses one
23168 additional level of template parameters. */
23169 ++num_templates;
23171 return cp_parser_check_template_parameters
23172 (parser, num_templates, declarator_location, declarator);
23175 case cdk_function:
23176 case cdk_array:
23177 case cdk_pointer:
23178 case cdk_reference:
23179 case cdk_ptrmem:
23180 return (cp_parser_check_declarator_template_parameters
23181 (parser, declarator->declarator, declarator_location));
23183 case cdk_error:
23184 return true;
23186 default:
23187 gcc_unreachable ();
23189 return false;
23192 /* NUM_TEMPLATES were used in the current declaration. If that is
23193 invalid, return FALSE and issue an error messages. Otherwise,
23194 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23195 declarator and we can print more accurate diagnostics. */
23197 static bool
23198 cp_parser_check_template_parameters (cp_parser* parser,
23199 unsigned num_templates,
23200 location_t location,
23201 cp_declarator *declarator)
23203 /* If there are the same number of template classes and parameter
23204 lists, that's OK. */
23205 if (parser->num_template_parameter_lists == num_templates)
23206 return true;
23207 /* If there are more, but only one more, then we are referring to a
23208 member template. That's OK too. */
23209 if (parser->num_template_parameter_lists == num_templates + 1)
23210 return true;
23211 /* If there are more template classes than parameter lists, we have
23212 something like:
23214 template <class T> void S<T>::R<T>::f (); */
23215 if (parser->num_template_parameter_lists < num_templates)
23217 if (declarator && !current_function_decl)
23218 error_at (location, "specializing member %<%T::%E%> "
23219 "requires %<template<>%> syntax",
23220 declarator->u.id.qualifying_scope,
23221 declarator->u.id.unqualified_name);
23222 else if (declarator)
23223 error_at (location, "invalid declaration of %<%T::%E%>",
23224 declarator->u.id.qualifying_scope,
23225 declarator->u.id.unqualified_name);
23226 else
23227 error_at (location, "too few template-parameter-lists");
23228 return false;
23230 /* Otherwise, there are too many template parameter lists. We have
23231 something like:
23233 template <class T> template <class U> void S::f(); */
23234 error_at (location, "too many template-parameter-lists");
23235 return false;
23238 /* Parse an optional `::' token indicating that the following name is
23239 from the global namespace. If so, PARSER->SCOPE is set to the
23240 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23241 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23242 Returns the new value of PARSER->SCOPE, if the `::' token is
23243 present, and NULL_TREE otherwise. */
23245 static tree
23246 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23248 cp_token *token;
23250 /* Peek at the next token. */
23251 token = cp_lexer_peek_token (parser->lexer);
23252 /* If we're looking at a `::' token then we're starting from the
23253 global namespace, not our current location. */
23254 if (token->type == CPP_SCOPE)
23256 /* Consume the `::' token. */
23257 cp_lexer_consume_token (parser->lexer);
23258 /* Set the SCOPE so that we know where to start the lookup. */
23259 parser->scope = global_namespace;
23260 parser->qualifying_scope = global_namespace;
23261 parser->object_scope = NULL_TREE;
23263 return parser->scope;
23265 else if (!current_scope_valid_p)
23267 parser->scope = NULL_TREE;
23268 parser->qualifying_scope = NULL_TREE;
23269 parser->object_scope = NULL_TREE;
23272 return NULL_TREE;
23275 /* Returns TRUE if the upcoming token sequence is the start of a
23276 constructor declarator. If FRIEND_P is true, the declarator is
23277 preceded by the `friend' specifier. */
23279 static bool
23280 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23282 bool constructor_p;
23283 bool outside_class_specifier_p;
23284 tree nested_name_specifier;
23285 cp_token *next_token;
23287 /* The common case is that this is not a constructor declarator, so
23288 try to avoid doing lots of work if at all possible. It's not
23289 valid declare a constructor at function scope. */
23290 if (parser->in_function_body)
23291 return false;
23292 /* And only certain tokens can begin a constructor declarator. */
23293 next_token = cp_lexer_peek_token (parser->lexer);
23294 if (next_token->type != CPP_NAME
23295 && next_token->type != CPP_SCOPE
23296 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23297 && next_token->type != CPP_TEMPLATE_ID)
23298 return false;
23300 /* Parse tentatively; we are going to roll back all of the tokens
23301 consumed here. */
23302 cp_parser_parse_tentatively (parser);
23303 /* Assume that we are looking at a constructor declarator. */
23304 constructor_p = true;
23306 /* Look for the optional `::' operator. */
23307 cp_parser_global_scope_opt (parser,
23308 /*current_scope_valid_p=*/false);
23309 /* Look for the nested-name-specifier. */
23310 nested_name_specifier
23311 = (cp_parser_nested_name_specifier_opt (parser,
23312 /*typename_keyword_p=*/false,
23313 /*check_dependency_p=*/false,
23314 /*type_p=*/false,
23315 /*is_declaration=*/false));
23317 outside_class_specifier_p = (!at_class_scope_p ()
23318 || !TYPE_BEING_DEFINED (current_class_type)
23319 || friend_p);
23321 /* Outside of a class-specifier, there must be a
23322 nested-name-specifier. */
23323 if (!nested_name_specifier && outside_class_specifier_p)
23324 constructor_p = false;
23325 else if (nested_name_specifier == error_mark_node)
23326 constructor_p = false;
23328 /* If we have a class scope, this is easy; DR 147 says that S::S always
23329 names the constructor, and no other qualified name could. */
23330 if (constructor_p && nested_name_specifier
23331 && CLASS_TYPE_P (nested_name_specifier))
23333 tree id = cp_parser_unqualified_id (parser,
23334 /*template_keyword_p=*/false,
23335 /*check_dependency_p=*/false,
23336 /*declarator_p=*/true,
23337 /*optional_p=*/false);
23338 if (is_overloaded_fn (id))
23339 id = DECL_NAME (get_first_fn (id));
23340 if (!constructor_name_p (id, nested_name_specifier))
23341 constructor_p = false;
23343 /* If we still think that this might be a constructor-declarator,
23344 look for a class-name. */
23345 else if (constructor_p)
23347 /* If we have:
23349 template <typename T> struct S {
23350 S();
23353 we must recognize that the nested `S' names a class. */
23354 tree type_decl;
23355 type_decl = cp_parser_class_name (parser,
23356 /*typename_keyword_p=*/false,
23357 /*template_keyword_p=*/false,
23358 none_type,
23359 /*check_dependency_p=*/false,
23360 /*class_head_p=*/false,
23361 /*is_declaration=*/false);
23362 /* If there was no class-name, then this is not a constructor.
23363 Otherwise, if we are in a class-specifier and we aren't
23364 handling a friend declaration, check that its type matches
23365 current_class_type (c++/38313). Note: error_mark_node
23366 is left alone for error recovery purposes. */
23367 constructor_p = (!cp_parser_error_occurred (parser)
23368 && (outside_class_specifier_p
23369 || type_decl == error_mark_node
23370 || same_type_p (current_class_type,
23371 TREE_TYPE (type_decl))));
23373 /* If we're still considering a constructor, we have to see a `(',
23374 to begin the parameter-declaration-clause, followed by either a
23375 `)', an `...', or a decl-specifier. We need to check for a
23376 type-specifier to avoid being fooled into thinking that:
23378 S (f) (int);
23380 is a constructor. (It is actually a function named `f' that
23381 takes one parameter (of type `int') and returns a value of type
23382 `S'. */
23383 if (constructor_p
23384 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23385 constructor_p = false;
23387 if (constructor_p
23388 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23389 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23390 /* A parameter declaration begins with a decl-specifier,
23391 which is either the "attribute" keyword, a storage class
23392 specifier, or (usually) a type-specifier. */
23393 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23395 tree type;
23396 tree pushed_scope = NULL_TREE;
23397 unsigned saved_num_template_parameter_lists;
23399 /* Names appearing in the type-specifier should be looked up
23400 in the scope of the class. */
23401 if (current_class_type)
23402 type = NULL_TREE;
23403 else
23405 type = TREE_TYPE (type_decl);
23406 if (TREE_CODE (type) == TYPENAME_TYPE)
23408 type = resolve_typename_type (type,
23409 /*only_current_p=*/false);
23410 if (TREE_CODE (type) == TYPENAME_TYPE)
23412 cp_parser_abort_tentative_parse (parser);
23413 return false;
23416 pushed_scope = push_scope (type);
23419 /* Inside the constructor parameter list, surrounding
23420 template-parameter-lists do not apply. */
23421 saved_num_template_parameter_lists
23422 = parser->num_template_parameter_lists;
23423 parser->num_template_parameter_lists = 0;
23425 /* Look for the type-specifier. */
23426 cp_parser_type_specifier (parser,
23427 CP_PARSER_FLAGS_NONE,
23428 /*decl_specs=*/NULL,
23429 /*is_declarator=*/true,
23430 /*declares_class_or_enum=*/NULL,
23431 /*is_cv_qualifier=*/NULL);
23433 parser->num_template_parameter_lists
23434 = saved_num_template_parameter_lists;
23436 /* Leave the scope of the class. */
23437 if (pushed_scope)
23438 pop_scope (pushed_scope);
23440 constructor_p = !cp_parser_error_occurred (parser);
23444 /* We did not really want to consume any tokens. */
23445 cp_parser_abort_tentative_parse (parser);
23447 return constructor_p;
23450 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23451 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23452 they must be performed once we are in the scope of the function.
23454 Returns the function defined. */
23456 static tree
23457 cp_parser_function_definition_from_specifiers_and_declarator
23458 (cp_parser* parser,
23459 cp_decl_specifier_seq *decl_specifiers,
23460 tree attributes,
23461 const cp_declarator *declarator)
23463 tree fn;
23464 bool success_p;
23466 /* Begin the function-definition. */
23467 success_p = start_function (decl_specifiers, declarator, attributes);
23469 /* The things we're about to see are not directly qualified by any
23470 template headers we've seen thus far. */
23471 reset_specialization ();
23473 /* If there were names looked up in the decl-specifier-seq that we
23474 did not check, check them now. We must wait until we are in the
23475 scope of the function to perform the checks, since the function
23476 might be a friend. */
23477 perform_deferred_access_checks (tf_warning_or_error);
23479 if (success_p)
23481 cp_finalize_omp_declare_simd (parser, current_function_decl);
23482 parser->omp_declare_simd = NULL;
23485 if (!success_p)
23487 /* Skip the entire function. */
23488 cp_parser_skip_to_end_of_block_or_statement (parser);
23489 fn = error_mark_node;
23491 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23493 /* Seen already, skip it. An error message has already been output. */
23494 cp_parser_skip_to_end_of_block_or_statement (parser);
23495 fn = current_function_decl;
23496 current_function_decl = NULL_TREE;
23497 /* If this is a function from a class, pop the nested class. */
23498 if (current_class_name)
23499 pop_nested_class ();
23501 else
23503 timevar_id_t tv;
23504 if (DECL_DECLARED_INLINE_P (current_function_decl))
23505 tv = TV_PARSE_INLINE;
23506 else
23507 tv = TV_PARSE_FUNC;
23508 timevar_push (tv);
23509 fn = cp_parser_function_definition_after_declarator (parser,
23510 /*inline_p=*/false);
23511 timevar_pop (tv);
23514 return fn;
23517 /* Parse the part of a function-definition that follows the
23518 declarator. INLINE_P is TRUE iff this function is an inline
23519 function defined within a class-specifier.
23521 Returns the function defined. */
23523 static tree
23524 cp_parser_function_definition_after_declarator (cp_parser* parser,
23525 bool inline_p)
23527 tree fn;
23528 bool ctor_initializer_p = false;
23529 bool saved_in_unbraced_linkage_specification_p;
23530 bool saved_in_function_body;
23531 unsigned saved_num_template_parameter_lists;
23532 cp_token *token;
23533 bool fully_implicit_function_template_p
23534 = parser->fully_implicit_function_template_p;
23535 parser->fully_implicit_function_template_p = false;
23536 tree implicit_template_parms
23537 = parser->implicit_template_parms;
23538 parser->implicit_template_parms = 0;
23539 cp_binding_level* implicit_template_scope
23540 = parser->implicit_template_scope;
23541 parser->implicit_template_scope = 0;
23543 saved_in_function_body = parser->in_function_body;
23544 parser->in_function_body = true;
23545 /* If the next token is `return', then the code may be trying to
23546 make use of the "named return value" extension that G++ used to
23547 support. */
23548 token = cp_lexer_peek_token (parser->lexer);
23549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23551 /* Consume the `return' keyword. */
23552 cp_lexer_consume_token (parser->lexer);
23553 /* Look for the identifier that indicates what value is to be
23554 returned. */
23555 cp_parser_identifier (parser);
23556 /* Issue an error message. */
23557 error_at (token->location,
23558 "named return values are no longer supported");
23559 /* Skip tokens until we reach the start of the function body. */
23560 while (true)
23562 cp_token *token = cp_lexer_peek_token (parser->lexer);
23563 if (token->type == CPP_OPEN_BRACE
23564 || token->type == CPP_EOF
23565 || token->type == CPP_PRAGMA_EOL)
23566 break;
23567 cp_lexer_consume_token (parser->lexer);
23570 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23571 anything declared inside `f'. */
23572 saved_in_unbraced_linkage_specification_p
23573 = parser->in_unbraced_linkage_specification_p;
23574 parser->in_unbraced_linkage_specification_p = false;
23575 /* Inside the function, surrounding template-parameter-lists do not
23576 apply. */
23577 saved_num_template_parameter_lists
23578 = parser->num_template_parameter_lists;
23579 parser->num_template_parameter_lists = 0;
23581 start_lambda_scope (current_function_decl);
23583 /* If the next token is `try', `__transaction_atomic', or
23584 `__transaction_relaxed`, then we are looking at either function-try-block
23585 or function-transaction-block. Note that all of these include the
23586 function-body. */
23587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23588 ctor_initializer_p = cp_parser_function_transaction (parser,
23589 RID_TRANSACTION_ATOMIC);
23590 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23591 RID_TRANSACTION_RELAXED))
23592 ctor_initializer_p = cp_parser_function_transaction (parser,
23593 RID_TRANSACTION_RELAXED);
23594 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23595 ctor_initializer_p = cp_parser_function_try_block (parser);
23596 else
23597 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23598 (parser, /*in_function_try_block=*/false);
23600 finish_lambda_scope ();
23602 /* Finish the function. */
23603 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23604 (inline_p ? 2 : 0));
23605 /* Generate code for it, if necessary. */
23606 expand_or_defer_fn (fn);
23607 /* Restore the saved values. */
23608 parser->in_unbraced_linkage_specification_p
23609 = saved_in_unbraced_linkage_specification_p;
23610 parser->num_template_parameter_lists
23611 = saved_num_template_parameter_lists;
23612 parser->in_function_body = saved_in_function_body;
23614 parser->fully_implicit_function_template_p
23615 = fully_implicit_function_template_p;
23616 parser->implicit_template_parms
23617 = implicit_template_parms;
23618 parser->implicit_template_scope
23619 = implicit_template_scope;
23621 if (parser->fully_implicit_function_template_p)
23622 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23624 return fn;
23627 /* Parse a template-declaration, assuming that the `export' (and
23628 `extern') keywords, if present, has already been scanned. MEMBER_P
23629 is as for cp_parser_template_declaration. */
23631 static void
23632 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23634 tree decl = NULL_TREE;
23635 vec<deferred_access_check, va_gc> *checks;
23636 tree parameter_list;
23637 bool friend_p = false;
23638 bool need_lang_pop;
23639 cp_token *token;
23641 /* Look for the `template' keyword. */
23642 token = cp_lexer_peek_token (parser->lexer);
23643 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23644 return;
23646 /* And the `<'. */
23647 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23648 return;
23649 if (at_class_scope_p () && current_function_decl)
23651 /* 14.5.2.2 [temp.mem]
23653 A local class shall not have member templates. */
23654 error_at (token->location,
23655 "invalid declaration of member template in local class");
23656 cp_parser_skip_to_end_of_block_or_statement (parser);
23657 return;
23659 /* [temp]
23661 A template ... shall not have C linkage. */
23662 if (current_lang_name == lang_name_c)
23664 error_at (token->location, "template with C linkage");
23665 /* Give it C++ linkage to avoid confusing other parts of the
23666 front end. */
23667 push_lang_context (lang_name_cplusplus);
23668 need_lang_pop = true;
23670 else
23671 need_lang_pop = false;
23673 /* We cannot perform access checks on the template parameter
23674 declarations until we know what is being declared, just as we
23675 cannot check the decl-specifier list. */
23676 push_deferring_access_checks (dk_deferred);
23678 /* If the next token is `>', then we have an invalid
23679 specialization. Rather than complain about an invalid template
23680 parameter, issue an error message here. */
23681 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23683 cp_parser_error (parser, "invalid explicit specialization");
23684 begin_specialization ();
23685 parameter_list = NULL_TREE;
23687 else
23689 /* Parse the template parameters. */
23690 parameter_list = cp_parser_template_parameter_list (parser);
23693 /* Get the deferred access checks from the parameter list. These
23694 will be checked once we know what is being declared, as for a
23695 member template the checks must be performed in the scope of the
23696 class containing the member. */
23697 checks = get_deferred_access_checks ();
23699 /* Look for the `>'. */
23700 cp_parser_skip_to_end_of_template_parameter_list (parser);
23701 /* We just processed one more parameter list. */
23702 ++parser->num_template_parameter_lists;
23703 /* If the next token is `template', there are more template
23704 parameters. */
23705 if (cp_lexer_next_token_is_keyword (parser->lexer,
23706 RID_TEMPLATE))
23707 cp_parser_template_declaration_after_export (parser, member_p);
23708 else if (cxx_dialect >= cxx11
23709 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23710 decl = cp_parser_alias_declaration (parser);
23711 else
23713 /* There are no access checks when parsing a template, as we do not
23714 know if a specialization will be a friend. */
23715 push_deferring_access_checks (dk_no_check);
23716 token = cp_lexer_peek_token (parser->lexer);
23717 decl = cp_parser_single_declaration (parser,
23718 checks,
23719 member_p,
23720 /*explicit_specialization_p=*/false,
23721 &friend_p);
23722 pop_deferring_access_checks ();
23724 /* If this is a member template declaration, let the front
23725 end know. */
23726 if (member_p && !friend_p && decl)
23728 if (TREE_CODE (decl) == TYPE_DECL)
23729 cp_parser_check_access_in_redeclaration (decl, token->location);
23731 decl = finish_member_template_decl (decl);
23733 else if (friend_p && decl
23734 && DECL_DECLARES_TYPE_P (decl))
23735 make_friend_class (current_class_type, TREE_TYPE (decl),
23736 /*complain=*/true);
23738 /* We are done with the current parameter list. */
23739 --parser->num_template_parameter_lists;
23741 pop_deferring_access_checks ();
23743 /* Finish up. */
23744 finish_template_decl (parameter_list);
23746 /* Check the template arguments for a literal operator template. */
23747 if (decl
23748 && DECL_DECLARES_FUNCTION_P (decl)
23749 && UDLIT_OPER_P (DECL_NAME (decl)))
23751 bool ok = true;
23752 if (parameter_list == NULL_TREE)
23753 ok = false;
23754 else
23756 int num_parms = TREE_VEC_LENGTH (parameter_list);
23757 if (num_parms == 1)
23759 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23760 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23761 if (TREE_TYPE (parm) != char_type_node
23762 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23763 ok = false;
23765 else if (num_parms == 2 && cxx_dialect >= cxx14)
23767 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23768 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23769 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23770 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23771 if (TREE_TYPE (parm) != TREE_TYPE (type)
23772 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23773 ok = false;
23775 else
23776 ok = false;
23778 if (!ok)
23780 if (cxx_dialect >= cxx14)
23781 error ("literal operator template %qD has invalid parameter list."
23782 " Expected non-type template argument pack <char...>"
23783 " or <typename CharT, CharT...>",
23784 decl);
23785 else
23786 error ("literal operator template %qD has invalid parameter list."
23787 " Expected non-type template argument pack <char...>",
23788 decl);
23791 /* Register member declarations. */
23792 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23793 finish_member_declaration (decl);
23794 /* For the erroneous case of a template with C linkage, we pushed an
23795 implicit C++ linkage scope; exit that scope now. */
23796 if (need_lang_pop)
23797 pop_lang_context ();
23798 /* If DECL is a function template, we must return to parse it later.
23799 (Even though there is no definition, there might be default
23800 arguments that need handling.) */
23801 if (member_p && decl
23802 && DECL_DECLARES_FUNCTION_P (decl))
23803 vec_safe_push (unparsed_funs_with_definitions, decl);
23806 /* Perform the deferred access checks from a template-parameter-list.
23807 CHECKS is a TREE_LIST of access checks, as returned by
23808 get_deferred_access_checks. */
23810 static void
23811 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23813 ++processing_template_parmlist;
23814 perform_access_checks (checks, tf_warning_or_error);
23815 --processing_template_parmlist;
23818 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23819 `function-definition' sequence that follows a template header.
23820 If MEMBER_P is true, this declaration appears in a class scope.
23822 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23823 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23825 static tree
23826 cp_parser_single_declaration (cp_parser* parser,
23827 vec<deferred_access_check, va_gc> *checks,
23828 bool member_p,
23829 bool explicit_specialization_p,
23830 bool* friend_p)
23832 int declares_class_or_enum;
23833 tree decl = NULL_TREE;
23834 cp_decl_specifier_seq decl_specifiers;
23835 bool function_definition_p = false;
23836 cp_token *decl_spec_token_start;
23838 /* This function is only used when processing a template
23839 declaration. */
23840 gcc_assert (innermost_scope_kind () == sk_template_parms
23841 || innermost_scope_kind () == sk_template_spec);
23843 /* Defer access checks until we know what is being declared. */
23844 push_deferring_access_checks (dk_deferred);
23846 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23847 alternative. */
23848 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23849 cp_parser_decl_specifier_seq (parser,
23850 CP_PARSER_FLAGS_OPTIONAL,
23851 &decl_specifiers,
23852 &declares_class_or_enum);
23853 if (friend_p)
23854 *friend_p = cp_parser_friend_p (&decl_specifiers);
23856 /* There are no template typedefs. */
23857 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23859 error_at (decl_spec_token_start->location,
23860 "template declaration of %<typedef%>");
23861 decl = error_mark_node;
23864 /* Gather up the access checks that occurred the
23865 decl-specifier-seq. */
23866 stop_deferring_access_checks ();
23868 /* Check for the declaration of a template class. */
23869 if (declares_class_or_enum)
23871 if (cp_parser_declares_only_class_p (parser))
23873 decl = shadow_tag (&decl_specifiers);
23875 /* In this case:
23877 struct C {
23878 friend template <typename T> struct A<T>::B;
23881 A<T>::B will be represented by a TYPENAME_TYPE, and
23882 therefore not recognized by shadow_tag. */
23883 if (friend_p && *friend_p
23884 && !decl
23885 && decl_specifiers.type
23886 && TYPE_P (decl_specifiers.type))
23887 decl = decl_specifiers.type;
23889 if (decl && decl != error_mark_node)
23890 decl = TYPE_NAME (decl);
23891 else
23892 decl = error_mark_node;
23894 /* Perform access checks for template parameters. */
23895 cp_parser_perform_template_parameter_access_checks (checks);
23899 /* Complain about missing 'typename' or other invalid type names. */
23900 if (!decl_specifiers.any_type_specifiers_p
23901 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23903 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23904 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23905 the rest of this declaration. */
23906 decl = error_mark_node;
23907 goto out;
23910 /* If it's not a template class, try for a template function. If
23911 the next token is a `;', then this declaration does not declare
23912 anything. But, if there were errors in the decl-specifiers, then
23913 the error might well have come from an attempted class-specifier.
23914 In that case, there's no need to warn about a missing declarator. */
23915 if (!decl
23916 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23917 || decl_specifiers.type != error_mark_node))
23919 decl = cp_parser_init_declarator (parser,
23920 &decl_specifiers,
23921 checks,
23922 /*function_definition_allowed_p=*/true,
23923 member_p,
23924 declares_class_or_enum,
23925 &function_definition_p,
23926 NULL, NULL);
23928 /* 7.1.1-1 [dcl.stc]
23930 A storage-class-specifier shall not be specified in an explicit
23931 specialization... */
23932 if (decl
23933 && explicit_specialization_p
23934 && decl_specifiers.storage_class != sc_none)
23936 error_at (decl_spec_token_start->location,
23937 "explicit template specialization cannot have a storage class");
23938 decl = error_mark_node;
23941 if (decl && VAR_P (decl))
23942 check_template_variable (decl);
23945 /* Look for a trailing `;' after the declaration. */
23946 if (!function_definition_p
23947 && (decl == error_mark_node
23948 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23949 cp_parser_skip_to_end_of_block_or_statement (parser);
23951 out:
23952 pop_deferring_access_checks ();
23954 /* Clear any current qualification; whatever comes next is the start
23955 of something new. */
23956 parser->scope = NULL_TREE;
23957 parser->qualifying_scope = NULL_TREE;
23958 parser->object_scope = NULL_TREE;
23960 return decl;
23963 /* Parse a cast-expression that is not the operand of a unary "&". */
23965 static tree
23966 cp_parser_simple_cast_expression (cp_parser *parser)
23968 return cp_parser_cast_expression (parser, /*address_p=*/false,
23969 /*cast_p=*/false, /*decltype*/false, NULL);
23972 /* Parse a functional cast to TYPE. Returns an expression
23973 representing the cast. */
23975 static tree
23976 cp_parser_functional_cast (cp_parser* parser, tree type)
23978 vec<tree, va_gc> *vec;
23979 tree expression_list;
23980 tree cast;
23981 bool nonconst_p;
23983 if (!type)
23984 type = error_mark_node;
23986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23988 cp_lexer_set_source_position (parser->lexer);
23989 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23990 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23991 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23992 if (TREE_CODE (type) == TYPE_DECL)
23993 type = TREE_TYPE (type);
23994 return finish_compound_literal (type, expression_list,
23995 tf_warning_or_error);
23999 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24000 /*cast_p=*/true,
24001 /*allow_expansion_p=*/true,
24002 /*non_constant_p=*/NULL);
24003 if (vec == NULL)
24004 expression_list = error_mark_node;
24005 else
24007 expression_list = build_tree_list_vec (vec);
24008 release_tree_vector (vec);
24011 cast = build_functional_cast (type, expression_list,
24012 tf_warning_or_error);
24013 /* [expr.const]/1: In an integral constant expression "only type
24014 conversions to integral or enumeration type can be used". */
24015 if (TREE_CODE (type) == TYPE_DECL)
24016 type = TREE_TYPE (type);
24017 if (cast != error_mark_node
24018 && !cast_valid_in_integral_constant_expression_p (type)
24019 && cp_parser_non_integral_constant_expression (parser,
24020 NIC_CONSTRUCTOR))
24021 return error_mark_node;
24022 return cast;
24025 /* Save the tokens that make up the body of a member function defined
24026 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24027 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24028 specifiers applied to the declaration. Returns the FUNCTION_DECL
24029 for the member function. */
24031 static tree
24032 cp_parser_save_member_function_body (cp_parser* parser,
24033 cp_decl_specifier_seq *decl_specifiers,
24034 cp_declarator *declarator,
24035 tree attributes)
24037 cp_token *first;
24038 cp_token *last;
24039 tree fn;
24041 /* Create the FUNCTION_DECL. */
24042 fn = grokmethod (decl_specifiers, declarator, attributes);
24043 cp_finalize_omp_declare_simd (parser, fn);
24044 /* If something went badly wrong, bail out now. */
24045 if (fn == error_mark_node)
24047 /* If there's a function-body, skip it. */
24048 if (cp_parser_token_starts_function_definition_p
24049 (cp_lexer_peek_token (parser->lexer)))
24050 cp_parser_skip_to_end_of_block_or_statement (parser);
24051 return error_mark_node;
24054 /* Remember it, if there default args to post process. */
24055 cp_parser_save_default_args (parser, fn);
24057 /* Save away the tokens that make up the body of the
24058 function. */
24059 first = parser->lexer->next_token;
24060 /* Handle function try blocks. */
24061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24062 cp_lexer_consume_token (parser->lexer);
24063 /* We can have braced-init-list mem-initializers before the fn body. */
24064 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24066 cp_lexer_consume_token (parser->lexer);
24067 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24069 /* cache_group will stop after an un-nested { } pair, too. */
24070 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24071 break;
24073 /* variadic mem-inits have ... after the ')'. */
24074 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24075 cp_lexer_consume_token (parser->lexer);
24078 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24079 /* Handle function try blocks. */
24080 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24081 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24082 last = parser->lexer->next_token;
24084 /* Save away the inline definition; we will process it when the
24085 class is complete. */
24086 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24087 DECL_PENDING_INLINE_P (fn) = 1;
24089 /* We need to know that this was defined in the class, so that
24090 friend templates are handled correctly. */
24091 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24093 /* Add FN to the queue of functions to be parsed later. */
24094 vec_safe_push (unparsed_funs_with_definitions, fn);
24096 return fn;
24099 /* Save the tokens that make up the in-class initializer for a non-static
24100 data member. Returns a DEFAULT_ARG. */
24102 static tree
24103 cp_parser_save_nsdmi (cp_parser* parser)
24105 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24108 /* Parse a template-argument-list, as well as the trailing ">" (but
24109 not the opening "<"). See cp_parser_template_argument_list for the
24110 return value. */
24112 static tree
24113 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24115 tree arguments;
24116 tree saved_scope;
24117 tree saved_qualifying_scope;
24118 tree saved_object_scope;
24119 bool saved_greater_than_is_operator_p;
24120 int saved_unevaluated_operand;
24121 int saved_inhibit_evaluation_warnings;
24123 /* [temp.names]
24125 When parsing a template-id, the first non-nested `>' is taken as
24126 the end of the template-argument-list rather than a greater-than
24127 operator. */
24128 saved_greater_than_is_operator_p
24129 = parser->greater_than_is_operator_p;
24130 parser->greater_than_is_operator_p = false;
24131 /* Parsing the argument list may modify SCOPE, so we save it
24132 here. */
24133 saved_scope = parser->scope;
24134 saved_qualifying_scope = parser->qualifying_scope;
24135 saved_object_scope = parser->object_scope;
24136 /* We need to evaluate the template arguments, even though this
24137 template-id may be nested within a "sizeof". */
24138 saved_unevaluated_operand = cp_unevaluated_operand;
24139 cp_unevaluated_operand = 0;
24140 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24141 c_inhibit_evaluation_warnings = 0;
24142 /* Parse the template-argument-list itself. */
24143 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24144 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24145 arguments = NULL_TREE;
24146 else
24147 arguments = cp_parser_template_argument_list (parser);
24148 /* Look for the `>' that ends the template-argument-list. If we find
24149 a '>>' instead, it's probably just a typo. */
24150 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24152 if (cxx_dialect != cxx98)
24154 /* In C++0x, a `>>' in a template argument list or cast
24155 expression is considered to be two separate `>'
24156 tokens. So, change the current token to a `>', but don't
24157 consume it: it will be consumed later when the outer
24158 template argument list (or cast expression) is parsed.
24159 Note that this replacement of `>' for `>>' is necessary
24160 even if we are parsing tentatively: in the tentative
24161 case, after calling
24162 cp_parser_enclosed_template_argument_list we will always
24163 throw away all of the template arguments and the first
24164 closing `>', either because the template argument list
24165 was erroneous or because we are replacing those tokens
24166 with a CPP_TEMPLATE_ID token. The second `>' (which will
24167 not have been thrown away) is needed either to close an
24168 outer template argument list or to complete a new-style
24169 cast. */
24170 cp_token *token = cp_lexer_peek_token (parser->lexer);
24171 token->type = CPP_GREATER;
24173 else if (!saved_greater_than_is_operator_p)
24175 /* If we're in a nested template argument list, the '>>' has
24176 to be a typo for '> >'. We emit the error message, but we
24177 continue parsing and we push a '>' as next token, so that
24178 the argument list will be parsed correctly. Note that the
24179 global source location is still on the token before the
24180 '>>', so we need to say explicitly where we want it. */
24181 cp_token *token = cp_lexer_peek_token (parser->lexer);
24182 error_at (token->location, "%<>>%> should be %<> >%> "
24183 "within a nested template argument list");
24185 token->type = CPP_GREATER;
24187 else
24189 /* If this is not a nested template argument list, the '>>'
24190 is a typo for '>'. Emit an error message and continue.
24191 Same deal about the token location, but here we can get it
24192 right by consuming the '>>' before issuing the diagnostic. */
24193 cp_token *token = cp_lexer_consume_token (parser->lexer);
24194 error_at (token->location,
24195 "spurious %<>>%>, use %<>%> to terminate "
24196 "a template argument list");
24199 else
24200 cp_parser_skip_to_end_of_template_parameter_list (parser);
24201 /* The `>' token might be a greater-than operator again now. */
24202 parser->greater_than_is_operator_p
24203 = saved_greater_than_is_operator_p;
24204 /* Restore the SAVED_SCOPE. */
24205 parser->scope = saved_scope;
24206 parser->qualifying_scope = saved_qualifying_scope;
24207 parser->object_scope = saved_object_scope;
24208 cp_unevaluated_operand = saved_unevaluated_operand;
24209 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24211 return arguments;
24214 /* MEMBER_FUNCTION is a member function, or a friend. If default
24215 arguments, or the body of the function have not yet been parsed,
24216 parse them now. */
24218 static void
24219 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24221 timevar_push (TV_PARSE_INMETH);
24222 /* If this member is a template, get the underlying
24223 FUNCTION_DECL. */
24224 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24225 member_function = DECL_TEMPLATE_RESULT (member_function);
24227 /* There should not be any class definitions in progress at this
24228 point; the bodies of members are only parsed outside of all class
24229 definitions. */
24230 gcc_assert (parser->num_classes_being_defined == 0);
24231 /* While we're parsing the member functions we might encounter more
24232 classes. We want to handle them right away, but we don't want
24233 them getting mixed up with functions that are currently in the
24234 queue. */
24235 push_unparsed_function_queues (parser);
24237 /* Make sure that any template parameters are in scope. */
24238 maybe_begin_member_template_processing (member_function);
24240 /* If the body of the function has not yet been parsed, parse it
24241 now. */
24242 if (DECL_PENDING_INLINE_P (member_function))
24244 tree function_scope;
24245 cp_token_cache *tokens;
24247 /* The function is no longer pending; we are processing it. */
24248 tokens = DECL_PENDING_INLINE_INFO (member_function);
24249 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24250 DECL_PENDING_INLINE_P (member_function) = 0;
24252 /* If this is a local class, enter the scope of the containing
24253 function. */
24254 function_scope = current_function_decl;
24255 if (function_scope)
24256 push_function_context ();
24258 /* Push the body of the function onto the lexer stack. */
24259 cp_parser_push_lexer_for_tokens (parser, tokens);
24261 /* Let the front end know that we going to be defining this
24262 function. */
24263 start_preparsed_function (member_function, NULL_TREE,
24264 SF_PRE_PARSED | SF_INCLASS_INLINE);
24266 /* Don't do access checking if it is a templated function. */
24267 if (processing_template_decl)
24268 push_deferring_access_checks (dk_no_check);
24270 /* #pragma omp declare reduction needs special parsing. */
24271 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24273 parser->lexer->in_pragma = true;
24274 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24275 finish_function (/*inline*/2);
24276 cp_check_omp_declare_reduction (member_function);
24278 else
24279 /* Now, parse the body of the function. */
24280 cp_parser_function_definition_after_declarator (parser,
24281 /*inline_p=*/true);
24283 if (processing_template_decl)
24284 pop_deferring_access_checks ();
24286 /* Leave the scope of the containing function. */
24287 if (function_scope)
24288 pop_function_context ();
24289 cp_parser_pop_lexer (parser);
24292 /* Remove any template parameters from the symbol table. */
24293 maybe_end_member_template_processing ();
24295 /* Restore the queue. */
24296 pop_unparsed_function_queues (parser);
24297 timevar_pop (TV_PARSE_INMETH);
24300 /* If DECL contains any default args, remember it on the unparsed
24301 functions queue. */
24303 static void
24304 cp_parser_save_default_args (cp_parser* parser, tree decl)
24306 tree probe;
24308 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24309 probe;
24310 probe = TREE_CHAIN (probe))
24311 if (TREE_PURPOSE (probe))
24313 cp_default_arg_entry entry = {current_class_type, decl};
24314 vec_safe_push (unparsed_funs_with_default_args, entry);
24315 break;
24319 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24320 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24321 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24322 from the parameter-type-list. */
24324 static tree
24325 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24326 tree default_arg, tree parmtype)
24328 cp_token_cache *tokens;
24329 tree parsed_arg;
24330 bool dummy;
24332 if (default_arg == error_mark_node)
24333 return error_mark_node;
24335 /* Push the saved tokens for the default argument onto the parser's
24336 lexer stack. */
24337 tokens = DEFARG_TOKENS (default_arg);
24338 cp_parser_push_lexer_for_tokens (parser, tokens);
24340 start_lambda_scope (decl);
24342 /* Parse the default argument. */
24343 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24344 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24345 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24347 finish_lambda_scope ();
24349 if (parsed_arg == error_mark_node)
24350 cp_parser_skip_to_end_of_statement (parser);
24352 if (!processing_template_decl)
24354 /* In a non-template class, check conversions now. In a template,
24355 we'll wait and instantiate these as needed. */
24356 if (TREE_CODE (decl) == PARM_DECL)
24357 parsed_arg = check_default_argument (parmtype, parsed_arg,
24358 tf_warning_or_error);
24359 else
24360 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24363 /* If the token stream has not been completely used up, then
24364 there was extra junk after the end of the default
24365 argument. */
24366 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24368 if (TREE_CODE (decl) == PARM_DECL)
24369 cp_parser_error (parser, "expected %<,%>");
24370 else
24371 cp_parser_error (parser, "expected %<;%>");
24374 /* Revert to the main lexer. */
24375 cp_parser_pop_lexer (parser);
24377 return parsed_arg;
24380 /* FIELD is a non-static data member with an initializer which we saved for
24381 later; parse it now. */
24383 static void
24384 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24386 tree def;
24388 maybe_begin_member_template_processing (field);
24390 push_unparsed_function_queues (parser);
24391 def = cp_parser_late_parse_one_default_arg (parser, field,
24392 DECL_INITIAL (field),
24393 NULL_TREE);
24394 pop_unparsed_function_queues (parser);
24396 maybe_end_member_template_processing ();
24398 DECL_INITIAL (field) = def;
24401 /* FN is a FUNCTION_DECL which may contains a parameter with an
24402 unparsed DEFAULT_ARG. Parse the default args now. This function
24403 assumes that the current scope is the scope in which the default
24404 argument should be processed. */
24406 static void
24407 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24409 bool saved_local_variables_forbidden_p;
24410 tree parm, parmdecl;
24412 /* While we're parsing the default args, we might (due to the
24413 statement expression extension) encounter more classes. We want
24414 to handle them right away, but we don't want them getting mixed
24415 up with default args that are currently in the queue. */
24416 push_unparsed_function_queues (parser);
24418 /* Local variable names (and the `this' keyword) may not appear
24419 in a default argument. */
24420 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24421 parser->local_variables_forbidden_p = true;
24423 push_defarg_context (fn);
24425 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24426 parmdecl = DECL_ARGUMENTS (fn);
24427 parm && parm != void_list_node;
24428 parm = TREE_CHAIN (parm),
24429 parmdecl = DECL_CHAIN (parmdecl))
24431 tree default_arg = TREE_PURPOSE (parm);
24432 tree parsed_arg;
24433 vec<tree, va_gc> *insts;
24434 tree copy;
24435 unsigned ix;
24437 if (!default_arg)
24438 continue;
24440 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24441 /* This can happen for a friend declaration for a function
24442 already declared with default arguments. */
24443 continue;
24445 parsed_arg
24446 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24447 default_arg,
24448 TREE_VALUE (parm));
24449 if (parsed_arg == error_mark_node)
24451 continue;
24454 TREE_PURPOSE (parm) = parsed_arg;
24456 /* Update any instantiations we've already created. */
24457 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24458 vec_safe_iterate (insts, ix, &copy); ix++)
24459 TREE_PURPOSE (copy) = parsed_arg;
24462 pop_defarg_context ();
24464 /* Make sure no default arg is missing. */
24465 check_default_args (fn);
24467 /* Restore the state of local_variables_forbidden_p. */
24468 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24470 /* Restore the queue. */
24471 pop_unparsed_function_queues (parser);
24474 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24476 sizeof ... ( identifier )
24478 where the 'sizeof' token has already been consumed. */
24480 static tree
24481 cp_parser_sizeof_pack (cp_parser *parser)
24483 /* Consume the `...'. */
24484 cp_lexer_consume_token (parser->lexer);
24485 maybe_warn_variadic_templates ();
24487 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24488 if (paren)
24489 cp_lexer_consume_token (parser->lexer);
24490 else
24491 permerror (cp_lexer_peek_token (parser->lexer)->location,
24492 "%<sizeof...%> argument must be surrounded by parentheses");
24494 cp_token *token = cp_lexer_peek_token (parser->lexer);
24495 tree name = cp_parser_identifier (parser);
24496 if (name == error_mark_node)
24497 return error_mark_node;
24498 /* The name is not qualified. */
24499 parser->scope = NULL_TREE;
24500 parser->qualifying_scope = NULL_TREE;
24501 parser->object_scope = NULL_TREE;
24502 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24503 if (expr == error_mark_node)
24504 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24505 token->location);
24506 if (TREE_CODE (expr) == TYPE_DECL)
24507 expr = TREE_TYPE (expr);
24508 else if (TREE_CODE (expr) == CONST_DECL)
24509 expr = DECL_INITIAL (expr);
24510 expr = make_pack_expansion (expr);
24512 if (paren)
24513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24515 return expr;
24518 /* Parse the operand of `sizeof' (or a similar operator). Returns
24519 either a TYPE or an expression, depending on the form of the
24520 input. The KEYWORD indicates which kind of expression we have
24521 encountered. */
24523 static tree
24524 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24526 tree expr = NULL_TREE;
24527 const char *saved_message;
24528 char *tmp;
24529 bool saved_integral_constant_expression_p;
24530 bool saved_non_integral_constant_expression_p;
24532 /* If it's a `...', then we are computing the length of a parameter
24533 pack. */
24534 if (keyword == RID_SIZEOF
24535 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24536 return cp_parser_sizeof_pack (parser);
24538 /* Types cannot be defined in a `sizeof' expression. Save away the
24539 old message. */
24540 saved_message = parser->type_definition_forbidden_message;
24541 /* And create the new one. */
24542 tmp = concat ("types may not be defined in %<",
24543 IDENTIFIER_POINTER (ridpointers[keyword]),
24544 "%> expressions", NULL);
24545 parser->type_definition_forbidden_message = tmp;
24547 /* The restrictions on constant-expressions do not apply inside
24548 sizeof expressions. */
24549 saved_integral_constant_expression_p
24550 = parser->integral_constant_expression_p;
24551 saved_non_integral_constant_expression_p
24552 = parser->non_integral_constant_expression_p;
24553 parser->integral_constant_expression_p = false;
24555 /* Do not actually evaluate the expression. */
24556 ++cp_unevaluated_operand;
24557 ++c_inhibit_evaluation_warnings;
24558 /* If it's a `(', then we might be looking at the type-id
24559 construction. */
24560 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24562 tree type = NULL_TREE;
24564 /* We can't be sure yet whether we're looking at a type-id or an
24565 expression. */
24566 cp_parser_parse_tentatively (parser);
24567 /* Note: as a GNU Extension, compound literals are considered
24568 postfix-expressions as they are in C99, so they are valid
24569 arguments to sizeof. See comment in cp_parser_cast_expression
24570 for details. */
24571 if (cp_parser_compound_literal_p (parser))
24572 cp_parser_simulate_error (parser);
24573 else
24575 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24576 parser->in_type_id_in_expr_p = true;
24577 /* Look for the type-id. */
24578 type = cp_parser_type_id (parser);
24579 /* Look for the closing `)'. */
24580 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24581 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24584 /* If all went well, then we're done. */
24585 if (cp_parser_parse_definitely (parser))
24587 cp_decl_specifier_seq decl_specs;
24589 /* Build a trivial decl-specifier-seq. */
24590 clear_decl_specs (&decl_specs);
24591 decl_specs.type = type;
24593 /* Call grokdeclarator to figure out what type this is. */
24594 expr = grokdeclarator (NULL,
24595 &decl_specs,
24596 TYPENAME,
24597 /*initialized=*/0,
24598 /*attrlist=*/NULL);
24602 /* If the type-id production did not work out, then we must be
24603 looking at the unary-expression production. */
24604 if (!expr)
24605 expr = cp_parser_unary_expression (parser);
24607 /* Go back to evaluating expressions. */
24608 --cp_unevaluated_operand;
24609 --c_inhibit_evaluation_warnings;
24611 /* Free the message we created. */
24612 free (tmp);
24613 /* And restore the old one. */
24614 parser->type_definition_forbidden_message = saved_message;
24615 parser->integral_constant_expression_p
24616 = saved_integral_constant_expression_p;
24617 parser->non_integral_constant_expression_p
24618 = saved_non_integral_constant_expression_p;
24620 return expr;
24623 /* If the current declaration has no declarator, return true. */
24625 static bool
24626 cp_parser_declares_only_class_p (cp_parser *parser)
24628 /* If the next token is a `;' or a `,' then there is no
24629 declarator. */
24630 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24631 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24634 /* Update the DECL_SPECS to reflect the storage class indicated by
24635 KEYWORD. */
24637 static void
24638 cp_parser_set_storage_class (cp_parser *parser,
24639 cp_decl_specifier_seq *decl_specs,
24640 enum rid keyword,
24641 cp_token *token)
24643 cp_storage_class storage_class;
24645 if (parser->in_unbraced_linkage_specification_p)
24647 error_at (token->location, "invalid use of %qD in linkage specification",
24648 ridpointers[keyword]);
24649 return;
24651 else if (decl_specs->storage_class != sc_none)
24653 decl_specs->conflicting_specifiers_p = true;
24654 return;
24657 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24658 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24659 && decl_specs->gnu_thread_keyword_p)
24661 pedwarn (decl_specs->locations[ds_thread], 0,
24662 "%<__thread%> before %qD", ridpointers[keyword]);
24665 switch (keyword)
24667 case RID_AUTO:
24668 storage_class = sc_auto;
24669 break;
24670 case RID_REGISTER:
24671 storage_class = sc_register;
24672 break;
24673 case RID_STATIC:
24674 storage_class = sc_static;
24675 break;
24676 case RID_EXTERN:
24677 storage_class = sc_extern;
24678 break;
24679 case RID_MUTABLE:
24680 storage_class = sc_mutable;
24681 break;
24682 default:
24683 gcc_unreachable ();
24685 decl_specs->storage_class = storage_class;
24686 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24688 /* A storage class specifier cannot be applied alongside a typedef
24689 specifier. If there is a typedef specifier present then set
24690 conflicting_specifiers_p which will trigger an error later
24691 on in grokdeclarator. */
24692 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24693 decl_specs->conflicting_specifiers_p = true;
24696 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24697 is true, the type is a class or enum definition. */
24699 static void
24700 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24701 tree type_spec,
24702 cp_token *token,
24703 bool type_definition_p)
24705 decl_specs->any_specifiers_p = true;
24707 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24708 (with, for example, in "typedef int wchar_t;") we remember that
24709 this is what happened. In system headers, we ignore these
24710 declarations so that G++ can work with system headers that are not
24711 C++-safe. */
24712 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24713 && !type_definition_p
24714 && (type_spec == boolean_type_node
24715 || type_spec == char16_type_node
24716 || type_spec == char32_type_node
24717 || type_spec == wchar_type_node)
24718 && (decl_specs->type
24719 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24720 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24721 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24722 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24724 decl_specs->redefined_builtin_type = type_spec;
24725 set_and_check_decl_spec_loc (decl_specs,
24726 ds_redefined_builtin_type_spec,
24727 token);
24728 if (!decl_specs->type)
24730 decl_specs->type = type_spec;
24731 decl_specs->type_definition_p = false;
24732 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24735 else if (decl_specs->type)
24736 decl_specs->multiple_types_p = true;
24737 else
24739 decl_specs->type = type_spec;
24740 decl_specs->type_definition_p = type_definition_p;
24741 decl_specs->redefined_builtin_type = NULL_TREE;
24742 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24746 /* True iff TOKEN is the GNU keyword __thread. */
24748 static bool
24749 token_is__thread (cp_token *token)
24751 gcc_assert (token->keyword == RID_THREAD);
24752 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24755 /* Set the location for a declarator specifier and check if it is
24756 duplicated.
24758 DECL_SPECS is the sequence of declarator specifiers onto which to
24759 set the location.
24761 DS is the single declarator specifier to set which location is to
24762 be set onto the existing sequence of declarators.
24764 LOCATION is the location for the declarator specifier to
24765 consider. */
24767 static void
24768 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24769 cp_decl_spec ds, cp_token *token)
24771 gcc_assert (ds < ds_last);
24773 if (decl_specs == NULL)
24774 return;
24776 source_location location = token->location;
24778 if (decl_specs->locations[ds] == 0)
24780 decl_specs->locations[ds] = location;
24781 if (ds == ds_thread)
24782 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24784 else
24786 if (ds == ds_long)
24788 if (decl_specs->locations[ds_long_long] != 0)
24789 error_at (location,
24790 "%<long long long%> is too long for GCC");
24791 else
24793 decl_specs->locations[ds_long_long] = location;
24794 pedwarn_cxx98 (location,
24795 OPT_Wlong_long,
24796 "ISO C++ 1998 does not support %<long long%>");
24799 else if (ds == ds_thread)
24801 bool gnu = token_is__thread (token);
24802 if (gnu != decl_specs->gnu_thread_keyword_p)
24803 error_at (location,
24804 "both %<__thread%> and %<thread_local%> specified");
24805 else
24806 error_at (location, "duplicate %qD", token->u.value);
24808 else
24810 static const char *const decl_spec_names[] = {
24811 "signed",
24812 "unsigned",
24813 "short",
24814 "long",
24815 "const",
24816 "volatile",
24817 "restrict",
24818 "inline",
24819 "virtual",
24820 "explicit",
24821 "friend",
24822 "typedef",
24823 "using",
24824 "constexpr",
24825 "__complex"
24827 error_at (location,
24828 "duplicate %qs", decl_spec_names[ds]);
24833 /* Return true iff the declarator specifier DS is present in the
24834 sequence of declarator specifiers DECL_SPECS. */
24836 bool
24837 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24838 cp_decl_spec ds)
24840 gcc_assert (ds < ds_last);
24842 if (decl_specs == NULL)
24843 return false;
24845 return decl_specs->locations[ds] != 0;
24848 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24849 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24851 static bool
24852 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24854 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24857 /* Issue an error message indicating that TOKEN_DESC was expected.
24858 If KEYWORD is true, it indicated this function is called by
24859 cp_parser_require_keword and the required token can only be
24860 a indicated keyword. */
24862 static void
24863 cp_parser_required_error (cp_parser *parser,
24864 required_token token_desc,
24865 bool keyword)
24867 switch (token_desc)
24869 case RT_NEW:
24870 cp_parser_error (parser, "expected %<new%>");
24871 return;
24872 case RT_DELETE:
24873 cp_parser_error (parser, "expected %<delete%>");
24874 return;
24875 case RT_RETURN:
24876 cp_parser_error (parser, "expected %<return%>");
24877 return;
24878 case RT_WHILE:
24879 cp_parser_error (parser, "expected %<while%>");
24880 return;
24881 case RT_EXTERN:
24882 cp_parser_error (parser, "expected %<extern%>");
24883 return;
24884 case RT_STATIC_ASSERT:
24885 cp_parser_error (parser, "expected %<static_assert%>");
24886 return;
24887 case RT_DECLTYPE:
24888 cp_parser_error (parser, "expected %<decltype%>");
24889 return;
24890 case RT_OPERATOR:
24891 cp_parser_error (parser, "expected %<operator%>");
24892 return;
24893 case RT_CLASS:
24894 cp_parser_error (parser, "expected %<class%>");
24895 return;
24896 case RT_TEMPLATE:
24897 cp_parser_error (parser, "expected %<template%>");
24898 return;
24899 case RT_NAMESPACE:
24900 cp_parser_error (parser, "expected %<namespace%>");
24901 return;
24902 case RT_USING:
24903 cp_parser_error (parser, "expected %<using%>");
24904 return;
24905 case RT_ASM:
24906 cp_parser_error (parser, "expected %<asm%>");
24907 return;
24908 case RT_TRY:
24909 cp_parser_error (parser, "expected %<try%>");
24910 return;
24911 case RT_CATCH:
24912 cp_parser_error (parser, "expected %<catch%>");
24913 return;
24914 case RT_THROW:
24915 cp_parser_error (parser, "expected %<throw%>");
24916 return;
24917 case RT_LABEL:
24918 cp_parser_error (parser, "expected %<__label__%>");
24919 return;
24920 case RT_AT_TRY:
24921 cp_parser_error (parser, "expected %<@try%>");
24922 return;
24923 case RT_AT_SYNCHRONIZED:
24924 cp_parser_error (parser, "expected %<@synchronized%>");
24925 return;
24926 case RT_AT_THROW:
24927 cp_parser_error (parser, "expected %<@throw%>");
24928 return;
24929 case RT_TRANSACTION_ATOMIC:
24930 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24931 return;
24932 case RT_TRANSACTION_RELAXED:
24933 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24934 return;
24935 default:
24936 break;
24938 if (!keyword)
24940 switch (token_desc)
24942 case RT_SEMICOLON:
24943 cp_parser_error (parser, "expected %<;%>");
24944 return;
24945 case RT_OPEN_PAREN:
24946 cp_parser_error (parser, "expected %<(%>");
24947 return;
24948 case RT_CLOSE_BRACE:
24949 cp_parser_error (parser, "expected %<}%>");
24950 return;
24951 case RT_OPEN_BRACE:
24952 cp_parser_error (parser, "expected %<{%>");
24953 return;
24954 case RT_CLOSE_SQUARE:
24955 cp_parser_error (parser, "expected %<]%>");
24956 return;
24957 case RT_OPEN_SQUARE:
24958 cp_parser_error (parser, "expected %<[%>");
24959 return;
24960 case RT_COMMA:
24961 cp_parser_error (parser, "expected %<,%>");
24962 return;
24963 case RT_SCOPE:
24964 cp_parser_error (parser, "expected %<::%>");
24965 return;
24966 case RT_LESS:
24967 cp_parser_error (parser, "expected %<<%>");
24968 return;
24969 case RT_GREATER:
24970 cp_parser_error (parser, "expected %<>%>");
24971 return;
24972 case RT_EQ:
24973 cp_parser_error (parser, "expected %<=%>");
24974 return;
24975 case RT_ELLIPSIS:
24976 cp_parser_error (parser, "expected %<...%>");
24977 return;
24978 case RT_MULT:
24979 cp_parser_error (parser, "expected %<*%>");
24980 return;
24981 case RT_COMPL:
24982 cp_parser_error (parser, "expected %<~%>");
24983 return;
24984 case RT_COLON:
24985 cp_parser_error (parser, "expected %<:%>");
24986 return;
24987 case RT_COLON_SCOPE:
24988 cp_parser_error (parser, "expected %<:%> or %<::%>");
24989 return;
24990 case RT_CLOSE_PAREN:
24991 cp_parser_error (parser, "expected %<)%>");
24992 return;
24993 case RT_COMMA_CLOSE_PAREN:
24994 cp_parser_error (parser, "expected %<,%> or %<)%>");
24995 return;
24996 case RT_PRAGMA_EOL:
24997 cp_parser_error (parser, "expected end of line");
24998 return;
24999 case RT_NAME:
25000 cp_parser_error (parser, "expected identifier");
25001 return;
25002 case RT_SELECT:
25003 cp_parser_error (parser, "expected selection-statement");
25004 return;
25005 case RT_INTERATION:
25006 cp_parser_error (parser, "expected iteration-statement");
25007 return;
25008 case RT_JUMP:
25009 cp_parser_error (parser, "expected jump-statement");
25010 return;
25011 case RT_CLASS_KEY:
25012 cp_parser_error (parser, "expected class-key");
25013 return;
25014 case RT_CLASS_TYPENAME_TEMPLATE:
25015 cp_parser_error (parser,
25016 "expected %<class%>, %<typename%>, or %<template%>");
25017 return;
25018 default:
25019 gcc_unreachable ();
25022 else
25023 gcc_unreachable ();
25028 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25029 issue an error message indicating that TOKEN_DESC was expected.
25031 Returns the token consumed, if the token had the appropriate type.
25032 Otherwise, returns NULL. */
25034 static cp_token *
25035 cp_parser_require (cp_parser* parser,
25036 enum cpp_ttype type,
25037 required_token token_desc)
25039 if (cp_lexer_next_token_is (parser->lexer, type))
25040 return cp_lexer_consume_token (parser->lexer);
25041 else
25043 /* Output the MESSAGE -- unless we're parsing tentatively. */
25044 if (!cp_parser_simulate_error (parser))
25045 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25046 return NULL;
25050 /* An error message is produced if the next token is not '>'.
25051 All further tokens are skipped until the desired token is
25052 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25054 static void
25055 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25057 /* Current level of '< ... >'. */
25058 unsigned level = 0;
25059 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25060 unsigned nesting_depth = 0;
25062 /* Are we ready, yet? If not, issue error message. */
25063 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25064 return;
25066 /* Skip tokens until the desired token is found. */
25067 while (true)
25069 /* Peek at the next token. */
25070 switch (cp_lexer_peek_token (parser->lexer)->type)
25072 case CPP_LESS:
25073 if (!nesting_depth)
25074 ++level;
25075 break;
25077 case CPP_RSHIFT:
25078 if (cxx_dialect == cxx98)
25079 /* C++0x views the `>>' operator as two `>' tokens, but
25080 C++98 does not. */
25081 break;
25082 else if (!nesting_depth && level-- == 0)
25084 /* We've hit a `>>' where the first `>' closes the
25085 template argument list, and the second `>' is
25086 spurious. Just consume the `>>' and stop; we've
25087 already produced at least one error. */
25088 cp_lexer_consume_token (parser->lexer);
25089 return;
25091 /* Fall through for C++0x, so we handle the second `>' in
25092 the `>>'. */
25094 case CPP_GREATER:
25095 if (!nesting_depth && level-- == 0)
25097 /* We've reached the token we want, consume it and stop. */
25098 cp_lexer_consume_token (parser->lexer);
25099 return;
25101 break;
25103 case CPP_OPEN_PAREN:
25104 case CPP_OPEN_SQUARE:
25105 ++nesting_depth;
25106 break;
25108 case CPP_CLOSE_PAREN:
25109 case CPP_CLOSE_SQUARE:
25110 if (nesting_depth-- == 0)
25111 return;
25112 break;
25114 case CPP_EOF:
25115 case CPP_PRAGMA_EOL:
25116 case CPP_SEMICOLON:
25117 case CPP_OPEN_BRACE:
25118 case CPP_CLOSE_BRACE:
25119 /* The '>' was probably forgotten, don't look further. */
25120 return;
25122 default:
25123 break;
25126 /* Consume this token. */
25127 cp_lexer_consume_token (parser->lexer);
25131 /* If the next token is the indicated keyword, consume it. Otherwise,
25132 issue an error message indicating that TOKEN_DESC was expected.
25134 Returns the token consumed, if the token had the appropriate type.
25135 Otherwise, returns NULL. */
25137 static cp_token *
25138 cp_parser_require_keyword (cp_parser* parser,
25139 enum rid keyword,
25140 required_token token_desc)
25142 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25144 if (token && token->keyword != keyword)
25146 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25147 return NULL;
25150 return token;
25153 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25154 function-definition. */
25156 static bool
25157 cp_parser_token_starts_function_definition_p (cp_token* token)
25159 return (/* An ordinary function-body begins with an `{'. */
25160 token->type == CPP_OPEN_BRACE
25161 /* A ctor-initializer begins with a `:'. */
25162 || token->type == CPP_COLON
25163 /* A function-try-block begins with `try'. */
25164 || token->keyword == RID_TRY
25165 /* A function-transaction-block begins with `__transaction_atomic'
25166 or `__transaction_relaxed'. */
25167 || token->keyword == RID_TRANSACTION_ATOMIC
25168 || token->keyword == RID_TRANSACTION_RELAXED
25169 /* The named return value extension begins with `return'. */
25170 || token->keyword == RID_RETURN);
25173 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25174 definition. */
25176 static bool
25177 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25179 cp_token *token;
25181 token = cp_lexer_peek_token (parser->lexer);
25182 return (token->type == CPP_OPEN_BRACE
25183 || (token->type == CPP_COLON
25184 && !parser->colon_doesnt_start_class_def_p));
25187 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25188 C++0x) ending a template-argument. */
25190 static bool
25191 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25193 cp_token *token;
25195 token = cp_lexer_peek_token (parser->lexer);
25196 return (token->type == CPP_COMMA
25197 || token->type == CPP_GREATER
25198 || token->type == CPP_ELLIPSIS
25199 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25202 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25203 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25205 static bool
25206 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25207 size_t n)
25209 cp_token *token;
25211 token = cp_lexer_peek_nth_token (parser->lexer, n);
25212 if (token->type == CPP_LESS)
25213 return true;
25214 /* Check for the sequence `<::' in the original code. It would be lexed as
25215 `[:', where `[' is a digraph, and there is no whitespace before
25216 `:'. */
25217 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25219 cp_token *token2;
25220 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25221 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25222 return true;
25224 return false;
25227 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25228 or none_type otherwise. */
25230 static enum tag_types
25231 cp_parser_token_is_class_key (cp_token* token)
25233 switch (token->keyword)
25235 case RID_CLASS:
25236 return class_type;
25237 case RID_STRUCT:
25238 return record_type;
25239 case RID_UNION:
25240 return union_type;
25242 default:
25243 return none_type;
25247 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25248 or none_type otherwise or if the token is null. */
25250 static enum tag_types
25251 cp_parser_token_is_type_parameter_key (cp_token* token)
25253 if (!token)
25254 return none_type;
25256 switch (token->keyword)
25258 case RID_CLASS:
25259 return class_type;
25260 case RID_TYPENAME:
25261 return typename_type;
25263 default:
25264 return none_type;
25268 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25270 static void
25271 cp_parser_check_class_key (enum tag_types class_key, tree type)
25273 if (type == error_mark_node)
25274 return;
25275 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25277 if (permerror (input_location, "%qs tag used in naming %q#T",
25278 class_key == union_type ? "union"
25279 : class_key == record_type ? "struct" : "class",
25280 type))
25281 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25282 "%q#T was previously declared here", type);
25286 /* Issue an error message if DECL is redeclared with different
25287 access than its original declaration [class.access.spec/3].
25288 This applies to nested classes and nested class templates.
25289 [class.mem/1]. */
25291 static void
25292 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25294 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25295 return;
25297 if ((TREE_PRIVATE (decl)
25298 != (current_access_specifier == access_private_node))
25299 || (TREE_PROTECTED (decl)
25300 != (current_access_specifier == access_protected_node)))
25301 error_at (location, "%qD redeclared with different access", decl);
25304 /* Look for the `template' keyword, as a syntactic disambiguator.
25305 Return TRUE iff it is present, in which case it will be
25306 consumed. */
25308 static bool
25309 cp_parser_optional_template_keyword (cp_parser *parser)
25311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25313 /* In C++98 the `template' keyword can only be used within templates;
25314 outside templates the parser can always figure out what is a
25315 template and what is not. In C++11, per the resolution of DR 468,
25316 `template' is allowed in cases where it is not strictly necessary. */
25317 if (!processing_template_decl
25318 && pedantic && cxx_dialect == cxx98)
25320 cp_token *token = cp_lexer_peek_token (parser->lexer);
25321 pedwarn (token->location, OPT_Wpedantic,
25322 "in C++98 %<template%> (as a disambiguator) is only "
25323 "allowed within templates");
25324 /* If this part of the token stream is rescanned, the same
25325 error message would be generated. So, we purge the token
25326 from the stream. */
25327 cp_lexer_purge_token (parser->lexer);
25328 return false;
25330 else
25332 /* Consume the `template' keyword. */
25333 cp_lexer_consume_token (parser->lexer);
25334 return true;
25337 return false;
25340 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25341 set PARSER->SCOPE, and perform other related actions. */
25343 static void
25344 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25346 int i;
25347 struct tree_check *check_value;
25348 deferred_access_check *chk;
25349 vec<deferred_access_check, va_gc> *checks;
25351 /* Get the stored value. */
25352 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25353 /* Perform any access checks that were deferred. */
25354 checks = check_value->checks;
25355 if (checks)
25357 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25358 perform_or_defer_access_check (chk->binfo,
25359 chk->decl,
25360 chk->diag_decl, tf_warning_or_error);
25362 /* Set the scope from the stored value. */
25363 parser->scope = check_value->value;
25364 parser->qualifying_scope = check_value->qualifying_scope;
25365 parser->object_scope = NULL_TREE;
25368 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25369 encounter the end of a block before what we were looking for. */
25371 static bool
25372 cp_parser_cache_group (cp_parser *parser,
25373 enum cpp_ttype end,
25374 unsigned depth)
25376 while (true)
25378 cp_token *token = cp_lexer_peek_token (parser->lexer);
25380 /* Abort a parenthesized expression if we encounter a semicolon. */
25381 if ((end == CPP_CLOSE_PAREN || depth == 0)
25382 && token->type == CPP_SEMICOLON)
25383 return true;
25384 /* If we've reached the end of the file, stop. */
25385 if (token->type == CPP_EOF
25386 || (end != CPP_PRAGMA_EOL
25387 && token->type == CPP_PRAGMA_EOL))
25388 return true;
25389 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25390 /* We've hit the end of an enclosing block, so there's been some
25391 kind of syntax error. */
25392 return true;
25394 /* Consume the token. */
25395 cp_lexer_consume_token (parser->lexer);
25396 /* See if it starts a new group. */
25397 if (token->type == CPP_OPEN_BRACE)
25399 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25400 /* In theory this should probably check end == '}', but
25401 cp_parser_save_member_function_body needs it to exit
25402 after either '}' or ')' when called with ')'. */
25403 if (depth == 0)
25404 return false;
25406 else if (token->type == CPP_OPEN_PAREN)
25408 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25409 if (depth == 0 && end == CPP_CLOSE_PAREN)
25410 return false;
25412 else if (token->type == CPP_PRAGMA)
25413 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25414 else if (token->type == end)
25415 return false;
25419 /* Like above, for caching a default argument or NSDMI. Both of these are
25420 terminated by a non-nested comma, but it can be unclear whether or not a
25421 comma is nested in a template argument list unless we do more parsing.
25422 In order to handle this ambiguity, when we encounter a ',' after a '<'
25423 we try to parse what follows as a parameter-declaration-list (in the
25424 case of a default argument) or a member-declarator (in the case of an
25425 NSDMI). If that succeeds, then we stop caching. */
25427 static tree
25428 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25430 unsigned depth = 0;
25431 int maybe_template_id = 0;
25432 cp_token *first_token;
25433 cp_token *token;
25434 tree default_argument;
25436 /* Add tokens until we have processed the entire default
25437 argument. We add the range [first_token, token). */
25438 first_token = cp_lexer_peek_token (parser->lexer);
25439 if (first_token->type == CPP_OPEN_BRACE)
25441 /* For list-initialization, this is straightforward. */
25442 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25443 token = cp_lexer_peek_token (parser->lexer);
25445 else while (true)
25447 bool done = false;
25449 /* Peek at the next token. */
25450 token = cp_lexer_peek_token (parser->lexer);
25451 /* What we do depends on what token we have. */
25452 switch (token->type)
25454 /* In valid code, a default argument must be
25455 immediately followed by a `,' `)', or `...'. */
25456 case CPP_COMMA:
25457 if (depth == 0 && maybe_template_id)
25459 /* If we've seen a '<', we might be in a
25460 template-argument-list. Until Core issue 325 is
25461 resolved, we don't know how this situation ought
25462 to be handled, so try to DTRT. We check whether
25463 what comes after the comma is a valid parameter
25464 declaration list. If it is, then the comma ends
25465 the default argument; otherwise the default
25466 argument continues. */
25467 bool error = false;
25468 cp_token *peek;
25470 /* Set ITALP so cp_parser_parameter_declaration_list
25471 doesn't decide to commit to this parse. */
25472 bool saved_italp = parser->in_template_argument_list_p;
25473 parser->in_template_argument_list_p = true;
25475 cp_parser_parse_tentatively (parser);
25477 if (nsdmi)
25479 /* Parse declarators until we reach a non-comma or
25480 somthing that cannot be an initializer.
25481 Just checking whether we're looking at a single
25482 declarator is insufficient. Consider:
25483 int var = tuple<T,U>::x;
25484 The template parameter 'U' looks exactly like a
25485 declarator. */
25488 int ctor_dtor_or_conv_p;
25489 cp_lexer_consume_token (parser->lexer);
25490 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25491 &ctor_dtor_or_conv_p,
25492 /*parenthesized_p=*/NULL,
25493 /*member_p=*/true,
25494 /*friend_p=*/false);
25495 peek = cp_lexer_peek_token (parser->lexer);
25496 if (cp_parser_error_occurred (parser))
25497 break;
25499 while (peek->type == CPP_COMMA);
25500 /* If we met an '=' or ';' then the original comma
25501 was the end of the NSDMI. Otherwise assume
25502 we're still in the NSDMI. */
25503 error = (peek->type != CPP_EQ
25504 && peek->type != CPP_SEMICOLON);
25506 else
25508 cp_lexer_consume_token (parser->lexer);
25509 begin_scope (sk_function_parms, NULL_TREE);
25510 cp_parser_parameter_declaration_list (parser, &error);
25511 pop_bindings_and_leave_scope ();
25513 if (!cp_parser_error_occurred (parser) && !error)
25514 done = true;
25515 cp_parser_abort_tentative_parse (parser);
25517 parser->in_template_argument_list_p = saved_italp;
25518 break;
25520 case CPP_CLOSE_PAREN:
25521 case CPP_ELLIPSIS:
25522 /* If we run into a non-nested `;', `}', or `]',
25523 then the code is invalid -- but the default
25524 argument is certainly over. */
25525 case CPP_SEMICOLON:
25526 case CPP_CLOSE_BRACE:
25527 case CPP_CLOSE_SQUARE:
25528 if (depth == 0
25529 /* Handle correctly int n = sizeof ... ( p ); */
25530 && token->type != CPP_ELLIPSIS)
25531 done = true;
25532 /* Update DEPTH, if necessary. */
25533 else if (token->type == CPP_CLOSE_PAREN
25534 || token->type == CPP_CLOSE_BRACE
25535 || token->type == CPP_CLOSE_SQUARE)
25536 --depth;
25537 break;
25539 case CPP_OPEN_PAREN:
25540 case CPP_OPEN_SQUARE:
25541 case CPP_OPEN_BRACE:
25542 ++depth;
25543 break;
25545 case CPP_LESS:
25546 if (depth == 0)
25547 /* This might be the comparison operator, or it might
25548 start a template argument list. */
25549 ++maybe_template_id;
25550 break;
25552 case CPP_RSHIFT:
25553 if (cxx_dialect == cxx98)
25554 break;
25555 /* Fall through for C++0x, which treats the `>>'
25556 operator like two `>' tokens in certain
25557 cases. */
25559 case CPP_GREATER:
25560 if (depth == 0)
25562 /* This might be an operator, or it might close a
25563 template argument list. But if a previous '<'
25564 started a template argument list, this will have
25565 closed it, so we can't be in one anymore. */
25566 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25567 if (maybe_template_id < 0)
25568 maybe_template_id = 0;
25570 break;
25572 /* If we run out of tokens, issue an error message. */
25573 case CPP_EOF:
25574 case CPP_PRAGMA_EOL:
25575 error_at (token->location, "file ends in default argument");
25576 done = true;
25577 break;
25579 case CPP_NAME:
25580 case CPP_SCOPE:
25581 /* In these cases, we should look for template-ids.
25582 For example, if the default argument is
25583 `X<int, double>()', we need to do name lookup to
25584 figure out whether or not `X' is a template; if
25585 so, the `,' does not end the default argument.
25587 That is not yet done. */
25588 break;
25590 default:
25591 break;
25594 /* If we've reached the end, stop. */
25595 if (done)
25596 break;
25598 /* Add the token to the token block. */
25599 token = cp_lexer_consume_token (parser->lexer);
25602 /* Create a DEFAULT_ARG to represent the unparsed default
25603 argument. */
25604 default_argument = make_node (DEFAULT_ARG);
25605 DEFARG_TOKENS (default_argument)
25606 = cp_token_cache_new (first_token, token);
25607 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25609 return default_argument;
25612 /* Begin parsing tentatively. We always save tokens while parsing
25613 tentatively so that if the tentative parsing fails we can restore the
25614 tokens. */
25616 static void
25617 cp_parser_parse_tentatively (cp_parser* parser)
25619 /* Enter a new parsing context. */
25620 parser->context = cp_parser_context_new (parser->context);
25621 /* Begin saving tokens. */
25622 cp_lexer_save_tokens (parser->lexer);
25623 /* In order to avoid repetitive access control error messages,
25624 access checks are queued up until we are no longer parsing
25625 tentatively. */
25626 push_deferring_access_checks (dk_deferred);
25629 /* Commit to the currently active tentative parse. */
25631 static void
25632 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25634 cp_parser_context *context;
25635 cp_lexer *lexer;
25637 /* Mark all of the levels as committed. */
25638 lexer = parser->lexer;
25639 for (context = parser->context; context->next; context = context->next)
25641 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25642 break;
25643 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25644 while (!cp_lexer_saving_tokens (lexer))
25645 lexer = lexer->next;
25646 cp_lexer_commit_tokens (lexer);
25650 /* Commit to the topmost currently active tentative parse.
25652 Note that this function shouldn't be called when there are
25653 irreversible side-effects while in a tentative state. For
25654 example, we shouldn't create a permanent entry in the symbol
25655 table, or issue an error message that might not apply if the
25656 tentative parse is aborted. */
25658 static void
25659 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25661 cp_parser_context *context = parser->context;
25662 cp_lexer *lexer = parser->lexer;
25664 if (context)
25666 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25667 return;
25668 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25670 while (!cp_lexer_saving_tokens (lexer))
25671 lexer = lexer->next;
25672 cp_lexer_commit_tokens (lexer);
25676 /* Abort the currently active tentative parse. All consumed tokens
25677 will be rolled back, and no diagnostics will be issued. */
25679 static void
25680 cp_parser_abort_tentative_parse (cp_parser* parser)
25682 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25683 || errorcount > 0);
25684 cp_parser_simulate_error (parser);
25685 /* Now, pretend that we want to see if the construct was
25686 successfully parsed. */
25687 cp_parser_parse_definitely (parser);
25690 /* Stop parsing tentatively. If a parse error has occurred, restore the
25691 token stream. Otherwise, commit to the tokens we have consumed.
25692 Returns true if no error occurred; false otherwise. */
25694 static bool
25695 cp_parser_parse_definitely (cp_parser* parser)
25697 bool error_occurred;
25698 cp_parser_context *context;
25700 /* Remember whether or not an error occurred, since we are about to
25701 destroy that information. */
25702 error_occurred = cp_parser_error_occurred (parser);
25703 /* Remove the topmost context from the stack. */
25704 context = parser->context;
25705 parser->context = context->next;
25706 /* If no parse errors occurred, commit to the tentative parse. */
25707 if (!error_occurred)
25709 /* Commit to the tokens read tentatively, unless that was
25710 already done. */
25711 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25712 cp_lexer_commit_tokens (parser->lexer);
25714 pop_to_parent_deferring_access_checks ();
25716 /* Otherwise, if errors occurred, roll back our state so that things
25717 are just as they were before we began the tentative parse. */
25718 else
25720 cp_lexer_rollback_tokens (parser->lexer);
25721 pop_deferring_access_checks ();
25723 /* Add the context to the front of the free list. */
25724 context->next = cp_parser_context_free_list;
25725 cp_parser_context_free_list = context;
25727 return !error_occurred;
25730 /* Returns true if we are parsing tentatively and are not committed to
25731 this tentative parse. */
25733 static bool
25734 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25736 return (cp_parser_parsing_tentatively (parser)
25737 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25740 /* Returns nonzero iff an error has occurred during the most recent
25741 tentative parse. */
25743 static bool
25744 cp_parser_error_occurred (cp_parser* parser)
25746 return (cp_parser_parsing_tentatively (parser)
25747 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25750 /* Returns nonzero if GNU extensions are allowed. */
25752 static bool
25753 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25755 return parser->allow_gnu_extensions_p;
25758 /* Objective-C++ Productions */
25761 /* Parse an Objective-C expression, which feeds into a primary-expression
25762 above.
25764 objc-expression:
25765 objc-message-expression
25766 objc-string-literal
25767 objc-encode-expression
25768 objc-protocol-expression
25769 objc-selector-expression
25771 Returns a tree representation of the expression. */
25773 static tree
25774 cp_parser_objc_expression (cp_parser* parser)
25776 /* Try to figure out what kind of declaration is present. */
25777 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25779 switch (kwd->type)
25781 case CPP_OPEN_SQUARE:
25782 return cp_parser_objc_message_expression (parser);
25784 case CPP_OBJC_STRING:
25785 kwd = cp_lexer_consume_token (parser->lexer);
25786 return objc_build_string_object (kwd->u.value);
25788 case CPP_KEYWORD:
25789 switch (kwd->keyword)
25791 case RID_AT_ENCODE:
25792 return cp_parser_objc_encode_expression (parser);
25794 case RID_AT_PROTOCOL:
25795 return cp_parser_objc_protocol_expression (parser);
25797 case RID_AT_SELECTOR:
25798 return cp_parser_objc_selector_expression (parser);
25800 default:
25801 break;
25803 default:
25804 error_at (kwd->location,
25805 "misplaced %<@%D%> Objective-C++ construct",
25806 kwd->u.value);
25807 cp_parser_skip_to_end_of_block_or_statement (parser);
25810 return error_mark_node;
25813 /* Parse an Objective-C message expression.
25815 objc-message-expression:
25816 [ objc-message-receiver objc-message-args ]
25818 Returns a representation of an Objective-C message. */
25820 static tree
25821 cp_parser_objc_message_expression (cp_parser* parser)
25823 tree receiver, messageargs;
25825 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25826 receiver = cp_parser_objc_message_receiver (parser);
25827 messageargs = cp_parser_objc_message_args (parser);
25828 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25830 return objc_build_message_expr (receiver, messageargs);
25833 /* Parse an objc-message-receiver.
25835 objc-message-receiver:
25836 expression
25837 simple-type-specifier
25839 Returns a representation of the type or expression. */
25841 static tree
25842 cp_parser_objc_message_receiver (cp_parser* parser)
25844 tree rcv;
25846 /* An Objective-C message receiver may be either (1) a type
25847 or (2) an expression. */
25848 cp_parser_parse_tentatively (parser);
25849 rcv = cp_parser_expression (parser);
25851 /* If that worked out, fine. */
25852 if (cp_parser_parse_definitely (parser))
25853 return rcv;
25855 cp_parser_parse_tentatively (parser);
25856 rcv = cp_parser_simple_type_specifier (parser,
25857 /*decl_specs=*/NULL,
25858 CP_PARSER_FLAGS_NONE);
25860 if (cp_parser_parse_definitely (parser))
25861 return objc_get_class_reference (rcv);
25863 cp_parser_error (parser, "objective-c++ message receiver expected");
25864 return error_mark_node;
25867 /* Parse the arguments and selectors comprising an Objective-C message.
25869 objc-message-args:
25870 objc-selector
25871 objc-selector-args
25872 objc-selector-args , objc-comma-args
25874 objc-selector-args:
25875 objc-selector [opt] : assignment-expression
25876 objc-selector-args objc-selector [opt] : assignment-expression
25878 objc-comma-args:
25879 assignment-expression
25880 objc-comma-args , assignment-expression
25882 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25883 selector arguments and TREE_VALUE containing a list of comma
25884 arguments. */
25886 static tree
25887 cp_parser_objc_message_args (cp_parser* parser)
25889 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25890 bool maybe_unary_selector_p = true;
25891 cp_token *token = cp_lexer_peek_token (parser->lexer);
25893 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25895 tree selector = NULL_TREE, arg;
25897 if (token->type != CPP_COLON)
25898 selector = cp_parser_objc_selector (parser);
25900 /* Detect if we have a unary selector. */
25901 if (maybe_unary_selector_p
25902 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25903 return build_tree_list (selector, NULL_TREE);
25905 maybe_unary_selector_p = false;
25906 cp_parser_require (parser, CPP_COLON, RT_COLON);
25907 arg = cp_parser_assignment_expression (parser);
25909 sel_args
25910 = chainon (sel_args,
25911 build_tree_list (selector, arg));
25913 token = cp_lexer_peek_token (parser->lexer);
25916 /* Handle non-selector arguments, if any. */
25917 while (token->type == CPP_COMMA)
25919 tree arg;
25921 cp_lexer_consume_token (parser->lexer);
25922 arg = cp_parser_assignment_expression (parser);
25924 addl_args
25925 = chainon (addl_args,
25926 build_tree_list (NULL_TREE, arg));
25928 token = cp_lexer_peek_token (parser->lexer);
25931 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25933 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25934 return build_tree_list (error_mark_node, error_mark_node);
25937 return build_tree_list (sel_args, addl_args);
25940 /* Parse an Objective-C encode expression.
25942 objc-encode-expression:
25943 @encode objc-typename
25945 Returns an encoded representation of the type argument. */
25947 static tree
25948 cp_parser_objc_encode_expression (cp_parser* parser)
25950 tree type;
25951 cp_token *token;
25953 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25954 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25955 token = cp_lexer_peek_token (parser->lexer);
25956 type = complete_type (cp_parser_type_id (parser));
25957 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25959 if (!type)
25961 error_at (token->location,
25962 "%<@encode%> must specify a type as an argument");
25963 return error_mark_node;
25966 /* This happens if we find @encode(T) (where T is a template
25967 typename or something dependent on a template typename) when
25968 parsing a template. In that case, we can't compile it
25969 immediately, but we rather create an AT_ENCODE_EXPR which will
25970 need to be instantiated when the template is used.
25972 if (dependent_type_p (type))
25974 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25975 TREE_READONLY (value) = 1;
25976 return value;
25979 return objc_build_encode_expr (type);
25982 /* Parse an Objective-C @defs expression. */
25984 static tree
25985 cp_parser_objc_defs_expression (cp_parser *parser)
25987 tree name;
25989 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25990 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25991 name = cp_parser_identifier (parser);
25992 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25994 return objc_get_class_ivars (name);
25997 /* Parse an Objective-C protocol expression.
25999 objc-protocol-expression:
26000 @protocol ( identifier )
26002 Returns a representation of the protocol expression. */
26004 static tree
26005 cp_parser_objc_protocol_expression (cp_parser* parser)
26007 tree proto;
26009 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26010 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26011 proto = cp_parser_identifier (parser);
26012 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26014 return objc_build_protocol_expr (proto);
26017 /* Parse an Objective-C selector expression.
26019 objc-selector-expression:
26020 @selector ( objc-method-signature )
26022 objc-method-signature:
26023 objc-selector
26024 objc-selector-seq
26026 objc-selector-seq:
26027 objc-selector :
26028 objc-selector-seq objc-selector :
26030 Returns a representation of the method selector. */
26032 static tree
26033 cp_parser_objc_selector_expression (cp_parser* parser)
26035 tree sel_seq = NULL_TREE;
26036 bool maybe_unary_selector_p = true;
26037 cp_token *token;
26038 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26040 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26041 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26042 token = cp_lexer_peek_token (parser->lexer);
26044 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26045 || token->type == CPP_SCOPE)
26047 tree selector = NULL_TREE;
26049 if (token->type != CPP_COLON
26050 || token->type == CPP_SCOPE)
26051 selector = cp_parser_objc_selector (parser);
26053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26054 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26056 /* Detect if we have a unary selector. */
26057 if (maybe_unary_selector_p)
26059 sel_seq = selector;
26060 goto finish_selector;
26062 else
26064 cp_parser_error (parser, "expected %<:%>");
26067 maybe_unary_selector_p = false;
26068 token = cp_lexer_consume_token (parser->lexer);
26070 if (token->type == CPP_SCOPE)
26072 sel_seq
26073 = chainon (sel_seq,
26074 build_tree_list (selector, NULL_TREE));
26075 sel_seq
26076 = chainon (sel_seq,
26077 build_tree_list (NULL_TREE, NULL_TREE));
26079 else
26080 sel_seq
26081 = chainon (sel_seq,
26082 build_tree_list (selector, NULL_TREE));
26084 token = cp_lexer_peek_token (parser->lexer);
26087 finish_selector:
26088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26090 return objc_build_selector_expr (loc, sel_seq);
26093 /* Parse a list of identifiers.
26095 objc-identifier-list:
26096 identifier
26097 objc-identifier-list , identifier
26099 Returns a TREE_LIST of identifier nodes. */
26101 static tree
26102 cp_parser_objc_identifier_list (cp_parser* parser)
26104 tree identifier;
26105 tree list;
26106 cp_token *sep;
26108 identifier = cp_parser_identifier (parser);
26109 if (identifier == error_mark_node)
26110 return error_mark_node;
26112 list = build_tree_list (NULL_TREE, identifier);
26113 sep = cp_lexer_peek_token (parser->lexer);
26115 while (sep->type == CPP_COMMA)
26117 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26118 identifier = cp_parser_identifier (parser);
26119 if (identifier == error_mark_node)
26120 return list;
26122 list = chainon (list, build_tree_list (NULL_TREE,
26123 identifier));
26124 sep = cp_lexer_peek_token (parser->lexer);
26127 return list;
26130 /* Parse an Objective-C alias declaration.
26132 objc-alias-declaration:
26133 @compatibility_alias identifier identifier ;
26135 This function registers the alias mapping with the Objective-C front end.
26136 It returns nothing. */
26138 static void
26139 cp_parser_objc_alias_declaration (cp_parser* parser)
26141 tree alias, orig;
26143 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26144 alias = cp_parser_identifier (parser);
26145 orig = cp_parser_identifier (parser);
26146 objc_declare_alias (alias, orig);
26147 cp_parser_consume_semicolon_at_end_of_statement (parser);
26150 /* Parse an Objective-C class forward-declaration.
26152 objc-class-declaration:
26153 @class objc-identifier-list ;
26155 The function registers the forward declarations with the Objective-C
26156 front end. It returns nothing. */
26158 static void
26159 cp_parser_objc_class_declaration (cp_parser* parser)
26161 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26162 while (true)
26164 tree id;
26166 id = cp_parser_identifier (parser);
26167 if (id == error_mark_node)
26168 break;
26170 objc_declare_class (id);
26172 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26173 cp_lexer_consume_token (parser->lexer);
26174 else
26175 break;
26177 cp_parser_consume_semicolon_at_end_of_statement (parser);
26180 /* Parse a list of Objective-C protocol references.
26182 objc-protocol-refs-opt:
26183 objc-protocol-refs [opt]
26185 objc-protocol-refs:
26186 < objc-identifier-list >
26188 Returns a TREE_LIST of identifiers, if any. */
26190 static tree
26191 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26193 tree protorefs = NULL_TREE;
26195 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26197 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26198 protorefs = cp_parser_objc_identifier_list (parser);
26199 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26202 return protorefs;
26205 /* Parse a Objective-C visibility specification. */
26207 static void
26208 cp_parser_objc_visibility_spec (cp_parser* parser)
26210 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26212 switch (vis->keyword)
26214 case RID_AT_PRIVATE:
26215 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26216 break;
26217 case RID_AT_PROTECTED:
26218 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26219 break;
26220 case RID_AT_PUBLIC:
26221 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26222 break;
26223 case RID_AT_PACKAGE:
26224 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26225 break;
26226 default:
26227 return;
26230 /* Eat '@private'/'@protected'/'@public'. */
26231 cp_lexer_consume_token (parser->lexer);
26234 /* Parse an Objective-C method type. Return 'true' if it is a class
26235 (+) method, and 'false' if it is an instance (-) method. */
26237 static inline bool
26238 cp_parser_objc_method_type (cp_parser* parser)
26240 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26241 return true;
26242 else
26243 return false;
26246 /* Parse an Objective-C protocol qualifier. */
26248 static tree
26249 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26251 tree quals = NULL_TREE, node;
26252 cp_token *token = cp_lexer_peek_token (parser->lexer);
26254 node = token->u.value;
26256 while (node && identifier_p (node)
26257 && (node == ridpointers [(int) RID_IN]
26258 || node == ridpointers [(int) RID_OUT]
26259 || node == ridpointers [(int) RID_INOUT]
26260 || node == ridpointers [(int) RID_BYCOPY]
26261 || node == ridpointers [(int) RID_BYREF]
26262 || node == ridpointers [(int) RID_ONEWAY]))
26264 quals = tree_cons (NULL_TREE, node, quals);
26265 cp_lexer_consume_token (parser->lexer);
26266 token = cp_lexer_peek_token (parser->lexer);
26267 node = token->u.value;
26270 return quals;
26273 /* Parse an Objective-C typename. */
26275 static tree
26276 cp_parser_objc_typename (cp_parser* parser)
26278 tree type_name = NULL_TREE;
26280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26282 tree proto_quals, cp_type = NULL_TREE;
26284 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26285 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26287 /* An ObjC type name may consist of just protocol qualifiers, in which
26288 case the type shall default to 'id'. */
26289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26291 cp_type = cp_parser_type_id (parser);
26293 /* If the type could not be parsed, an error has already
26294 been produced. For error recovery, behave as if it had
26295 not been specified, which will use the default type
26296 'id'. */
26297 if (cp_type == error_mark_node)
26299 cp_type = NULL_TREE;
26300 /* We need to skip to the closing parenthesis as
26301 cp_parser_type_id() does not seem to do it for
26302 us. */
26303 cp_parser_skip_to_closing_parenthesis (parser,
26304 /*recovering=*/true,
26305 /*or_comma=*/false,
26306 /*consume_paren=*/false);
26310 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26311 type_name = build_tree_list (proto_quals, cp_type);
26314 return type_name;
26317 /* Check to see if TYPE refers to an Objective-C selector name. */
26319 static bool
26320 cp_parser_objc_selector_p (enum cpp_ttype type)
26322 return (type == CPP_NAME || type == CPP_KEYWORD
26323 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26324 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26325 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26326 || type == CPP_XOR || type == CPP_XOR_EQ);
26329 /* Parse an Objective-C selector. */
26331 static tree
26332 cp_parser_objc_selector (cp_parser* parser)
26334 cp_token *token = cp_lexer_consume_token (parser->lexer);
26336 if (!cp_parser_objc_selector_p (token->type))
26338 error_at (token->location, "invalid Objective-C++ selector name");
26339 return error_mark_node;
26342 /* C++ operator names are allowed to appear in ObjC selectors. */
26343 switch (token->type)
26345 case CPP_AND_AND: return get_identifier ("and");
26346 case CPP_AND_EQ: return get_identifier ("and_eq");
26347 case CPP_AND: return get_identifier ("bitand");
26348 case CPP_OR: return get_identifier ("bitor");
26349 case CPP_COMPL: return get_identifier ("compl");
26350 case CPP_NOT: return get_identifier ("not");
26351 case CPP_NOT_EQ: return get_identifier ("not_eq");
26352 case CPP_OR_OR: return get_identifier ("or");
26353 case CPP_OR_EQ: return get_identifier ("or_eq");
26354 case CPP_XOR: return get_identifier ("xor");
26355 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26356 default: return token->u.value;
26360 /* Parse an Objective-C params list. */
26362 static tree
26363 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26365 tree params = NULL_TREE;
26366 bool maybe_unary_selector_p = true;
26367 cp_token *token = cp_lexer_peek_token (parser->lexer);
26369 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26371 tree selector = NULL_TREE, type_name, identifier;
26372 tree parm_attr = NULL_TREE;
26374 if (token->keyword == RID_ATTRIBUTE)
26375 break;
26377 if (token->type != CPP_COLON)
26378 selector = cp_parser_objc_selector (parser);
26380 /* Detect if we have a unary selector. */
26381 if (maybe_unary_selector_p
26382 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26384 params = selector; /* Might be followed by attributes. */
26385 break;
26388 maybe_unary_selector_p = false;
26389 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26391 /* Something went quite wrong. There should be a colon
26392 here, but there is not. Stop parsing parameters. */
26393 break;
26395 type_name = cp_parser_objc_typename (parser);
26396 /* New ObjC allows attributes on parameters too. */
26397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26398 parm_attr = cp_parser_attributes_opt (parser);
26399 identifier = cp_parser_identifier (parser);
26401 params
26402 = chainon (params,
26403 objc_build_keyword_decl (selector,
26404 type_name,
26405 identifier,
26406 parm_attr));
26408 token = cp_lexer_peek_token (parser->lexer);
26411 if (params == NULL_TREE)
26413 cp_parser_error (parser, "objective-c++ method declaration is expected");
26414 return error_mark_node;
26417 /* We allow tail attributes for the method. */
26418 if (token->keyword == RID_ATTRIBUTE)
26420 *attributes = cp_parser_attributes_opt (parser);
26421 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26422 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26423 return params;
26424 cp_parser_error (parser,
26425 "method attributes must be specified at the end");
26426 return error_mark_node;
26429 if (params == NULL_TREE)
26431 cp_parser_error (parser, "objective-c++ method declaration is expected");
26432 return error_mark_node;
26434 return params;
26437 /* Parse the non-keyword Objective-C params. */
26439 static tree
26440 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26441 tree* attributes)
26443 tree params = make_node (TREE_LIST);
26444 cp_token *token = cp_lexer_peek_token (parser->lexer);
26445 *ellipsisp = false; /* Initially, assume no ellipsis. */
26447 while (token->type == CPP_COMMA)
26449 cp_parameter_declarator *parmdecl;
26450 tree parm;
26452 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26453 token = cp_lexer_peek_token (parser->lexer);
26455 if (token->type == CPP_ELLIPSIS)
26457 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26458 *ellipsisp = true;
26459 token = cp_lexer_peek_token (parser->lexer);
26460 break;
26463 /* TODO: parse attributes for tail parameters. */
26464 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26465 parm = grokdeclarator (parmdecl->declarator,
26466 &parmdecl->decl_specifiers,
26467 PARM, /*initialized=*/0,
26468 /*attrlist=*/NULL);
26470 chainon (params, build_tree_list (NULL_TREE, parm));
26471 token = cp_lexer_peek_token (parser->lexer);
26474 /* We allow tail attributes for the method. */
26475 if (token->keyword == RID_ATTRIBUTE)
26477 if (*attributes == NULL_TREE)
26479 *attributes = cp_parser_attributes_opt (parser);
26480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26481 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26482 return params;
26484 else
26485 /* We have an error, but parse the attributes, so that we can
26486 carry on. */
26487 *attributes = cp_parser_attributes_opt (parser);
26489 cp_parser_error (parser,
26490 "method attributes must be specified at the end");
26491 return error_mark_node;
26494 return params;
26497 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26499 static void
26500 cp_parser_objc_interstitial_code (cp_parser* parser)
26502 cp_token *token = cp_lexer_peek_token (parser->lexer);
26504 /* If the next token is `extern' and the following token is a string
26505 literal, then we have a linkage specification. */
26506 if (token->keyword == RID_EXTERN
26507 && cp_parser_is_pure_string_literal
26508 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26509 cp_parser_linkage_specification (parser);
26510 /* Handle #pragma, if any. */
26511 else if (token->type == CPP_PRAGMA)
26512 cp_parser_pragma (parser, pragma_objc_icode);
26513 /* Allow stray semicolons. */
26514 else if (token->type == CPP_SEMICOLON)
26515 cp_lexer_consume_token (parser->lexer);
26516 /* Mark methods as optional or required, when building protocols. */
26517 else if (token->keyword == RID_AT_OPTIONAL)
26519 cp_lexer_consume_token (parser->lexer);
26520 objc_set_method_opt (true);
26522 else if (token->keyword == RID_AT_REQUIRED)
26524 cp_lexer_consume_token (parser->lexer);
26525 objc_set_method_opt (false);
26527 else if (token->keyword == RID_NAMESPACE)
26528 cp_parser_namespace_definition (parser);
26529 /* Other stray characters must generate errors. */
26530 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26532 cp_lexer_consume_token (parser->lexer);
26533 error ("stray %qs between Objective-C++ methods",
26534 token->type == CPP_OPEN_BRACE ? "{" : "}");
26536 /* Finally, try to parse a block-declaration, or a function-definition. */
26537 else
26538 cp_parser_block_declaration (parser, /*statement_p=*/false);
26541 /* Parse a method signature. */
26543 static tree
26544 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26546 tree rettype, kwdparms, optparms;
26547 bool ellipsis = false;
26548 bool is_class_method;
26550 is_class_method = cp_parser_objc_method_type (parser);
26551 rettype = cp_parser_objc_typename (parser);
26552 *attributes = NULL_TREE;
26553 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26554 if (kwdparms == error_mark_node)
26555 return error_mark_node;
26556 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26557 if (optparms == error_mark_node)
26558 return error_mark_node;
26560 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26563 static bool
26564 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26566 tree tattr;
26567 cp_lexer_save_tokens (parser->lexer);
26568 tattr = cp_parser_attributes_opt (parser);
26569 gcc_assert (tattr) ;
26571 /* If the attributes are followed by a method introducer, this is not allowed.
26572 Dump the attributes and flag the situation. */
26573 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26574 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26575 return true;
26577 /* Otherwise, the attributes introduce some interstitial code, possibly so
26578 rewind to allow that check. */
26579 cp_lexer_rollback_tokens (parser->lexer);
26580 return false;
26583 /* Parse an Objective-C method prototype list. */
26585 static void
26586 cp_parser_objc_method_prototype_list (cp_parser* parser)
26588 cp_token *token = cp_lexer_peek_token (parser->lexer);
26590 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26592 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26594 tree attributes, sig;
26595 bool is_class_method;
26596 if (token->type == CPP_PLUS)
26597 is_class_method = true;
26598 else
26599 is_class_method = false;
26600 sig = cp_parser_objc_method_signature (parser, &attributes);
26601 if (sig == error_mark_node)
26603 cp_parser_skip_to_end_of_block_or_statement (parser);
26604 token = cp_lexer_peek_token (parser->lexer);
26605 continue;
26607 objc_add_method_declaration (is_class_method, sig, attributes);
26608 cp_parser_consume_semicolon_at_end_of_statement (parser);
26610 else if (token->keyword == RID_AT_PROPERTY)
26611 cp_parser_objc_at_property_declaration (parser);
26612 else if (token->keyword == RID_ATTRIBUTE
26613 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26614 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26615 OPT_Wattributes,
26616 "prefix attributes are ignored for methods");
26617 else
26618 /* Allow for interspersed non-ObjC++ code. */
26619 cp_parser_objc_interstitial_code (parser);
26621 token = cp_lexer_peek_token (parser->lexer);
26624 if (token->type != CPP_EOF)
26625 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26626 else
26627 cp_parser_error (parser, "expected %<@end%>");
26629 objc_finish_interface ();
26632 /* Parse an Objective-C method definition list. */
26634 static void
26635 cp_parser_objc_method_definition_list (cp_parser* parser)
26637 cp_token *token = cp_lexer_peek_token (parser->lexer);
26639 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26641 tree meth;
26643 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26645 cp_token *ptk;
26646 tree sig, attribute;
26647 bool is_class_method;
26648 if (token->type == CPP_PLUS)
26649 is_class_method = true;
26650 else
26651 is_class_method = false;
26652 push_deferring_access_checks (dk_deferred);
26653 sig = cp_parser_objc_method_signature (parser, &attribute);
26654 if (sig == error_mark_node)
26656 cp_parser_skip_to_end_of_block_or_statement (parser);
26657 token = cp_lexer_peek_token (parser->lexer);
26658 continue;
26660 objc_start_method_definition (is_class_method, sig, attribute,
26661 NULL_TREE);
26663 /* For historical reasons, we accept an optional semicolon. */
26664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26665 cp_lexer_consume_token (parser->lexer);
26667 ptk = cp_lexer_peek_token (parser->lexer);
26668 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26669 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26671 perform_deferred_access_checks (tf_warning_or_error);
26672 stop_deferring_access_checks ();
26673 meth = cp_parser_function_definition_after_declarator (parser,
26674 false);
26675 pop_deferring_access_checks ();
26676 objc_finish_method_definition (meth);
26679 /* The following case will be removed once @synthesize is
26680 completely implemented. */
26681 else if (token->keyword == RID_AT_PROPERTY)
26682 cp_parser_objc_at_property_declaration (parser);
26683 else if (token->keyword == RID_AT_SYNTHESIZE)
26684 cp_parser_objc_at_synthesize_declaration (parser);
26685 else if (token->keyword == RID_AT_DYNAMIC)
26686 cp_parser_objc_at_dynamic_declaration (parser);
26687 else if (token->keyword == RID_ATTRIBUTE
26688 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26689 warning_at (token->location, OPT_Wattributes,
26690 "prefix attributes are ignored for methods");
26691 else
26692 /* Allow for interspersed non-ObjC++ code. */
26693 cp_parser_objc_interstitial_code (parser);
26695 token = cp_lexer_peek_token (parser->lexer);
26698 if (token->type != CPP_EOF)
26699 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26700 else
26701 cp_parser_error (parser, "expected %<@end%>");
26703 objc_finish_implementation ();
26706 /* Parse Objective-C ivars. */
26708 static void
26709 cp_parser_objc_class_ivars (cp_parser* parser)
26711 cp_token *token = cp_lexer_peek_token (parser->lexer);
26713 if (token->type != CPP_OPEN_BRACE)
26714 return; /* No ivars specified. */
26716 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26717 token = cp_lexer_peek_token (parser->lexer);
26719 while (token->type != CPP_CLOSE_BRACE
26720 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26722 cp_decl_specifier_seq declspecs;
26723 int decl_class_or_enum_p;
26724 tree prefix_attributes;
26726 cp_parser_objc_visibility_spec (parser);
26728 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26729 break;
26731 cp_parser_decl_specifier_seq (parser,
26732 CP_PARSER_FLAGS_OPTIONAL,
26733 &declspecs,
26734 &decl_class_or_enum_p);
26736 /* auto, register, static, extern, mutable. */
26737 if (declspecs.storage_class != sc_none)
26739 cp_parser_error (parser, "invalid type for instance variable");
26740 declspecs.storage_class = sc_none;
26743 /* thread_local. */
26744 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26746 cp_parser_error (parser, "invalid type for instance variable");
26747 declspecs.locations[ds_thread] = 0;
26750 /* typedef. */
26751 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26753 cp_parser_error (parser, "invalid type for instance variable");
26754 declspecs.locations[ds_typedef] = 0;
26757 prefix_attributes = declspecs.attributes;
26758 declspecs.attributes = NULL_TREE;
26760 /* Keep going until we hit the `;' at the end of the
26761 declaration. */
26762 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26764 tree width = NULL_TREE, attributes, first_attribute, decl;
26765 cp_declarator *declarator = NULL;
26766 int ctor_dtor_or_conv_p;
26768 /* Check for a (possibly unnamed) bitfield declaration. */
26769 token = cp_lexer_peek_token (parser->lexer);
26770 if (token->type == CPP_COLON)
26771 goto eat_colon;
26773 if (token->type == CPP_NAME
26774 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26775 == CPP_COLON))
26777 /* Get the name of the bitfield. */
26778 declarator = make_id_declarator (NULL_TREE,
26779 cp_parser_identifier (parser),
26780 sfk_none);
26782 eat_colon:
26783 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26784 /* Get the width of the bitfield. */
26785 width
26786 = cp_parser_constant_expression (parser);
26788 else
26790 /* Parse the declarator. */
26791 declarator
26792 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26793 &ctor_dtor_or_conv_p,
26794 /*parenthesized_p=*/NULL,
26795 /*member_p=*/false,
26796 /*friend_p=*/false);
26799 /* Look for attributes that apply to the ivar. */
26800 attributes = cp_parser_attributes_opt (parser);
26801 /* Remember which attributes are prefix attributes and
26802 which are not. */
26803 first_attribute = attributes;
26804 /* Combine the attributes. */
26805 attributes = chainon (prefix_attributes, attributes);
26807 if (width)
26808 /* Create the bitfield declaration. */
26809 decl = grokbitfield (declarator, &declspecs,
26810 width,
26811 attributes);
26812 else
26813 decl = grokfield (declarator, &declspecs,
26814 NULL_TREE, /*init_const_expr_p=*/false,
26815 NULL_TREE, attributes);
26817 /* Add the instance variable. */
26818 if (decl != error_mark_node && decl != NULL_TREE)
26819 objc_add_instance_variable (decl);
26821 /* Reset PREFIX_ATTRIBUTES. */
26822 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26823 attributes = TREE_CHAIN (attributes);
26824 if (attributes)
26825 TREE_CHAIN (attributes) = NULL_TREE;
26827 token = cp_lexer_peek_token (parser->lexer);
26829 if (token->type == CPP_COMMA)
26831 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26832 continue;
26834 break;
26837 cp_parser_consume_semicolon_at_end_of_statement (parser);
26838 token = cp_lexer_peek_token (parser->lexer);
26841 if (token->keyword == RID_AT_END)
26842 cp_parser_error (parser, "expected %<}%>");
26844 /* Do not consume the RID_AT_END, so it will be read again as terminating
26845 the @interface of @implementation. */
26846 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26847 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26849 /* For historical reasons, we accept an optional semicolon. */
26850 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26851 cp_lexer_consume_token (parser->lexer);
26854 /* Parse an Objective-C protocol declaration. */
26856 static void
26857 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26859 tree proto, protorefs;
26860 cp_token *tok;
26862 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26865 tok = cp_lexer_peek_token (parser->lexer);
26866 error_at (tok->location, "identifier expected after %<@protocol%>");
26867 cp_parser_consume_semicolon_at_end_of_statement (parser);
26868 return;
26871 /* See if we have a forward declaration or a definition. */
26872 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26874 /* Try a forward declaration first. */
26875 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26877 while (true)
26879 tree id;
26881 id = cp_parser_identifier (parser);
26882 if (id == error_mark_node)
26883 break;
26885 objc_declare_protocol (id, attributes);
26887 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26888 cp_lexer_consume_token (parser->lexer);
26889 else
26890 break;
26892 cp_parser_consume_semicolon_at_end_of_statement (parser);
26895 /* Ok, we got a full-fledged definition (or at least should). */
26896 else
26898 proto = cp_parser_identifier (parser);
26899 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26900 objc_start_protocol (proto, protorefs, attributes);
26901 cp_parser_objc_method_prototype_list (parser);
26905 /* Parse an Objective-C superclass or category. */
26907 static void
26908 cp_parser_objc_superclass_or_category (cp_parser *parser,
26909 bool iface_p,
26910 tree *super,
26911 tree *categ, bool *is_class_extension)
26913 cp_token *next = cp_lexer_peek_token (parser->lexer);
26915 *super = *categ = NULL_TREE;
26916 *is_class_extension = false;
26917 if (next->type == CPP_COLON)
26919 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26920 *super = cp_parser_identifier (parser);
26922 else if (next->type == CPP_OPEN_PAREN)
26924 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26926 /* If there is no category name, and this is an @interface, we
26927 have a class extension. */
26928 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26930 *categ = NULL_TREE;
26931 *is_class_extension = true;
26933 else
26934 *categ = cp_parser_identifier (parser);
26936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26940 /* Parse an Objective-C class interface. */
26942 static void
26943 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26945 tree name, super, categ, protos;
26946 bool is_class_extension;
26948 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26949 name = cp_parser_identifier (parser);
26950 if (name == error_mark_node)
26952 /* It's hard to recover because even if valid @interface stuff
26953 is to follow, we can't compile it (or validate it) if we
26954 don't even know which class it refers to. Let's assume this
26955 was a stray '@interface' token in the stream and skip it.
26957 return;
26959 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26960 &is_class_extension);
26961 protos = cp_parser_objc_protocol_refs_opt (parser);
26963 /* We have either a class or a category on our hands. */
26964 if (categ || is_class_extension)
26965 objc_start_category_interface (name, categ, protos, attributes);
26966 else
26968 objc_start_class_interface (name, super, protos, attributes);
26969 /* Handle instance variable declarations, if any. */
26970 cp_parser_objc_class_ivars (parser);
26971 objc_continue_interface ();
26974 cp_parser_objc_method_prototype_list (parser);
26977 /* Parse an Objective-C class implementation. */
26979 static void
26980 cp_parser_objc_class_implementation (cp_parser* parser)
26982 tree name, super, categ;
26983 bool is_class_extension;
26985 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26986 name = cp_parser_identifier (parser);
26987 if (name == error_mark_node)
26989 /* It's hard to recover because even if valid @implementation
26990 stuff is to follow, we can't compile it (or validate it) if
26991 we don't even know which class it refers to. Let's assume
26992 this was a stray '@implementation' token in the stream and
26993 skip it.
26995 return;
26997 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26998 &is_class_extension);
27000 /* We have either a class or a category on our hands. */
27001 if (categ)
27002 objc_start_category_implementation (name, categ);
27003 else
27005 objc_start_class_implementation (name, super);
27006 /* Handle instance variable declarations, if any. */
27007 cp_parser_objc_class_ivars (parser);
27008 objc_continue_implementation ();
27011 cp_parser_objc_method_definition_list (parser);
27014 /* Consume the @end token and finish off the implementation. */
27016 static void
27017 cp_parser_objc_end_implementation (cp_parser* parser)
27019 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27020 objc_finish_implementation ();
27023 /* Parse an Objective-C declaration. */
27025 static void
27026 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27028 /* Try to figure out what kind of declaration is present. */
27029 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27031 if (attributes)
27032 switch (kwd->keyword)
27034 case RID_AT_ALIAS:
27035 case RID_AT_CLASS:
27036 case RID_AT_END:
27037 error_at (kwd->location, "attributes may not be specified before"
27038 " the %<@%D%> Objective-C++ keyword",
27039 kwd->u.value);
27040 attributes = NULL;
27041 break;
27042 case RID_AT_IMPLEMENTATION:
27043 warning_at (kwd->location, OPT_Wattributes,
27044 "prefix attributes are ignored before %<@%D%>",
27045 kwd->u.value);
27046 attributes = NULL;
27047 default:
27048 break;
27051 switch (kwd->keyword)
27053 case RID_AT_ALIAS:
27054 cp_parser_objc_alias_declaration (parser);
27055 break;
27056 case RID_AT_CLASS:
27057 cp_parser_objc_class_declaration (parser);
27058 break;
27059 case RID_AT_PROTOCOL:
27060 cp_parser_objc_protocol_declaration (parser, attributes);
27061 break;
27062 case RID_AT_INTERFACE:
27063 cp_parser_objc_class_interface (parser, attributes);
27064 break;
27065 case RID_AT_IMPLEMENTATION:
27066 cp_parser_objc_class_implementation (parser);
27067 break;
27068 case RID_AT_END:
27069 cp_parser_objc_end_implementation (parser);
27070 break;
27071 default:
27072 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27073 kwd->u.value);
27074 cp_parser_skip_to_end_of_block_or_statement (parser);
27078 /* Parse an Objective-C try-catch-finally statement.
27080 objc-try-catch-finally-stmt:
27081 @try compound-statement objc-catch-clause-seq [opt]
27082 objc-finally-clause [opt]
27084 objc-catch-clause-seq:
27085 objc-catch-clause objc-catch-clause-seq [opt]
27087 objc-catch-clause:
27088 @catch ( objc-exception-declaration ) compound-statement
27090 objc-finally-clause:
27091 @finally compound-statement
27093 objc-exception-declaration:
27094 parameter-declaration
27095 '...'
27097 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27099 Returns NULL_TREE.
27101 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27102 for C. Keep them in sync. */
27104 static tree
27105 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27107 location_t location;
27108 tree stmt;
27110 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27111 location = cp_lexer_peek_token (parser->lexer)->location;
27112 objc_maybe_warn_exceptions (location);
27113 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27114 node, lest it get absorbed into the surrounding block. */
27115 stmt = push_stmt_list ();
27116 cp_parser_compound_statement (parser, NULL, false, false);
27117 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27119 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27121 cp_parameter_declarator *parm;
27122 tree parameter_declaration = error_mark_node;
27123 bool seen_open_paren = false;
27125 cp_lexer_consume_token (parser->lexer);
27126 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27127 seen_open_paren = true;
27128 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27130 /* We have "@catch (...)" (where the '...' are literally
27131 what is in the code). Skip the '...'.
27132 parameter_declaration is set to NULL_TREE, and
27133 objc_being_catch_clauses() knows that that means
27134 '...'. */
27135 cp_lexer_consume_token (parser->lexer);
27136 parameter_declaration = NULL_TREE;
27138 else
27140 /* We have "@catch (NSException *exception)" or something
27141 like that. Parse the parameter declaration. */
27142 parm = cp_parser_parameter_declaration (parser, false, NULL);
27143 if (parm == NULL)
27144 parameter_declaration = error_mark_node;
27145 else
27146 parameter_declaration = grokdeclarator (parm->declarator,
27147 &parm->decl_specifiers,
27148 PARM, /*initialized=*/0,
27149 /*attrlist=*/NULL);
27151 if (seen_open_paren)
27152 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27153 else
27155 /* If there was no open parenthesis, we are recovering from
27156 an error, and we are trying to figure out what mistake
27157 the user has made. */
27159 /* If there is an immediate closing parenthesis, the user
27160 probably forgot the opening one (ie, they typed "@catch
27161 NSException *e)". Parse the closing parenthesis and keep
27162 going. */
27163 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27164 cp_lexer_consume_token (parser->lexer);
27166 /* If these is no immediate closing parenthesis, the user
27167 probably doesn't know that parenthesis are required at
27168 all (ie, they typed "@catch NSException *e"). So, just
27169 forget about the closing parenthesis and keep going. */
27171 objc_begin_catch_clause (parameter_declaration);
27172 cp_parser_compound_statement (parser, NULL, false, false);
27173 objc_finish_catch_clause ();
27175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27177 cp_lexer_consume_token (parser->lexer);
27178 location = cp_lexer_peek_token (parser->lexer)->location;
27179 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27180 node, lest it get absorbed into the surrounding block. */
27181 stmt = push_stmt_list ();
27182 cp_parser_compound_statement (parser, NULL, false, false);
27183 objc_build_finally_clause (location, pop_stmt_list (stmt));
27186 return objc_finish_try_stmt ();
27189 /* Parse an Objective-C synchronized statement.
27191 objc-synchronized-stmt:
27192 @synchronized ( expression ) compound-statement
27194 Returns NULL_TREE. */
27196 static tree
27197 cp_parser_objc_synchronized_statement (cp_parser *parser)
27199 location_t location;
27200 tree lock, stmt;
27202 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27204 location = cp_lexer_peek_token (parser->lexer)->location;
27205 objc_maybe_warn_exceptions (location);
27206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27207 lock = cp_parser_expression (parser);
27208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27210 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27211 node, lest it get absorbed into the surrounding block. */
27212 stmt = push_stmt_list ();
27213 cp_parser_compound_statement (parser, NULL, false, false);
27215 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27218 /* Parse an Objective-C throw statement.
27220 objc-throw-stmt:
27221 @throw assignment-expression [opt] ;
27223 Returns a constructed '@throw' statement. */
27225 static tree
27226 cp_parser_objc_throw_statement (cp_parser *parser)
27228 tree expr = NULL_TREE;
27229 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27231 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27234 expr = cp_parser_expression (parser);
27236 cp_parser_consume_semicolon_at_end_of_statement (parser);
27238 return objc_build_throw_stmt (loc, expr);
27241 /* Parse an Objective-C statement. */
27243 static tree
27244 cp_parser_objc_statement (cp_parser * parser)
27246 /* Try to figure out what kind of declaration is present. */
27247 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27249 switch (kwd->keyword)
27251 case RID_AT_TRY:
27252 return cp_parser_objc_try_catch_finally_statement (parser);
27253 case RID_AT_SYNCHRONIZED:
27254 return cp_parser_objc_synchronized_statement (parser);
27255 case RID_AT_THROW:
27256 return cp_parser_objc_throw_statement (parser);
27257 default:
27258 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27259 kwd->u.value);
27260 cp_parser_skip_to_end_of_block_or_statement (parser);
27263 return error_mark_node;
27266 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27267 look ahead to see if an objc keyword follows the attributes. This
27268 is to detect the use of prefix attributes on ObjC @interface and
27269 @protocol. */
27271 static bool
27272 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27274 cp_lexer_save_tokens (parser->lexer);
27275 *attrib = cp_parser_attributes_opt (parser);
27276 gcc_assert (*attrib);
27277 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27279 cp_lexer_commit_tokens (parser->lexer);
27280 return true;
27282 cp_lexer_rollback_tokens (parser->lexer);
27283 return false;
27286 /* This routine is a minimal replacement for
27287 c_parser_struct_declaration () used when parsing the list of
27288 types/names or ObjC++ properties. For example, when parsing the
27289 code
27291 @property (readonly) int a, b, c;
27293 this function is responsible for parsing "int a, int b, int c" and
27294 returning the declarations as CHAIN of DECLs.
27296 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27297 similar parsing. */
27298 static tree
27299 cp_parser_objc_struct_declaration (cp_parser *parser)
27301 tree decls = NULL_TREE;
27302 cp_decl_specifier_seq declspecs;
27303 int decl_class_or_enum_p;
27304 tree prefix_attributes;
27306 cp_parser_decl_specifier_seq (parser,
27307 CP_PARSER_FLAGS_NONE,
27308 &declspecs,
27309 &decl_class_or_enum_p);
27311 if (declspecs.type == error_mark_node)
27312 return error_mark_node;
27314 /* auto, register, static, extern, mutable. */
27315 if (declspecs.storage_class != sc_none)
27317 cp_parser_error (parser, "invalid type for property");
27318 declspecs.storage_class = sc_none;
27321 /* thread_local. */
27322 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27324 cp_parser_error (parser, "invalid type for property");
27325 declspecs.locations[ds_thread] = 0;
27328 /* typedef. */
27329 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27331 cp_parser_error (parser, "invalid type for property");
27332 declspecs.locations[ds_typedef] = 0;
27335 prefix_attributes = declspecs.attributes;
27336 declspecs.attributes = NULL_TREE;
27338 /* Keep going until we hit the `;' at the end of the declaration. */
27339 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27341 tree attributes, first_attribute, decl;
27342 cp_declarator *declarator;
27343 cp_token *token;
27345 /* Parse the declarator. */
27346 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27347 NULL, NULL, false, false);
27349 /* Look for attributes that apply to the ivar. */
27350 attributes = cp_parser_attributes_opt (parser);
27351 /* Remember which attributes are prefix attributes and
27352 which are not. */
27353 first_attribute = attributes;
27354 /* Combine the attributes. */
27355 attributes = chainon (prefix_attributes, attributes);
27357 decl = grokfield (declarator, &declspecs,
27358 NULL_TREE, /*init_const_expr_p=*/false,
27359 NULL_TREE, attributes);
27361 if (decl == error_mark_node || decl == NULL_TREE)
27362 return error_mark_node;
27364 /* Reset PREFIX_ATTRIBUTES. */
27365 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27366 attributes = TREE_CHAIN (attributes);
27367 if (attributes)
27368 TREE_CHAIN (attributes) = NULL_TREE;
27370 DECL_CHAIN (decl) = decls;
27371 decls = decl;
27373 token = cp_lexer_peek_token (parser->lexer);
27374 if (token->type == CPP_COMMA)
27376 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27377 continue;
27379 else
27380 break;
27382 return decls;
27385 /* Parse an Objective-C @property declaration. The syntax is:
27387 objc-property-declaration:
27388 '@property' objc-property-attributes[opt] struct-declaration ;
27390 objc-property-attributes:
27391 '(' objc-property-attribute-list ')'
27393 objc-property-attribute-list:
27394 objc-property-attribute
27395 objc-property-attribute-list, objc-property-attribute
27397 objc-property-attribute
27398 'getter' = identifier
27399 'setter' = identifier
27400 'readonly'
27401 'readwrite'
27402 'assign'
27403 'retain'
27404 'copy'
27405 'nonatomic'
27407 For example:
27408 @property NSString *name;
27409 @property (readonly) id object;
27410 @property (retain, nonatomic, getter=getTheName) id name;
27411 @property int a, b, c;
27413 PS: This function is identical to
27414 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27415 static void
27416 cp_parser_objc_at_property_declaration (cp_parser *parser)
27418 /* The following variables hold the attributes of the properties as
27419 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27420 seen. When we see an attribute, we set them to 'true' (if they
27421 are boolean properties) or to the identifier (if they have an
27422 argument, ie, for getter and setter). Note that here we only
27423 parse the list of attributes, check the syntax and accumulate the
27424 attributes that we find. objc_add_property_declaration() will
27425 then process the information. */
27426 bool property_assign = false;
27427 bool property_copy = false;
27428 tree property_getter_ident = NULL_TREE;
27429 bool property_nonatomic = false;
27430 bool property_readonly = false;
27431 bool property_readwrite = false;
27432 bool property_retain = false;
27433 tree property_setter_ident = NULL_TREE;
27435 /* 'properties' is the list of properties that we read. Usually a
27436 single one, but maybe more (eg, in "@property int a, b, c;" there
27437 are three). */
27438 tree properties;
27439 location_t loc;
27441 loc = cp_lexer_peek_token (parser->lexer)->location;
27443 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27445 /* Parse the optional attribute list... */
27446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27448 /* Eat the '('. */
27449 cp_lexer_consume_token (parser->lexer);
27451 while (true)
27453 bool syntax_error = false;
27454 cp_token *token = cp_lexer_peek_token (parser->lexer);
27455 enum rid keyword;
27457 if (token->type != CPP_NAME)
27459 cp_parser_error (parser, "expected identifier");
27460 break;
27462 keyword = C_RID_CODE (token->u.value);
27463 cp_lexer_consume_token (parser->lexer);
27464 switch (keyword)
27466 case RID_ASSIGN: property_assign = true; break;
27467 case RID_COPY: property_copy = true; break;
27468 case RID_NONATOMIC: property_nonatomic = true; break;
27469 case RID_READONLY: property_readonly = true; break;
27470 case RID_READWRITE: property_readwrite = true; break;
27471 case RID_RETAIN: property_retain = true; break;
27473 case RID_GETTER:
27474 case RID_SETTER:
27475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27477 if (keyword == RID_GETTER)
27478 cp_parser_error (parser,
27479 "missing %<=%> (after %<getter%> attribute)");
27480 else
27481 cp_parser_error (parser,
27482 "missing %<=%> (after %<setter%> attribute)");
27483 syntax_error = true;
27484 break;
27486 cp_lexer_consume_token (parser->lexer); /* eat the = */
27487 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27489 cp_parser_error (parser, "expected identifier");
27490 syntax_error = true;
27491 break;
27493 if (keyword == RID_SETTER)
27495 if (property_setter_ident != NULL_TREE)
27497 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27498 cp_lexer_consume_token (parser->lexer);
27500 else
27501 property_setter_ident = cp_parser_objc_selector (parser);
27502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27503 cp_parser_error (parser, "setter name must terminate with %<:%>");
27504 else
27505 cp_lexer_consume_token (parser->lexer);
27507 else
27509 if (property_getter_ident != NULL_TREE)
27511 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27512 cp_lexer_consume_token (parser->lexer);
27514 else
27515 property_getter_ident = cp_parser_objc_selector (parser);
27517 break;
27518 default:
27519 cp_parser_error (parser, "unknown property attribute");
27520 syntax_error = true;
27521 break;
27524 if (syntax_error)
27525 break;
27527 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27528 cp_lexer_consume_token (parser->lexer);
27529 else
27530 break;
27533 /* FIXME: "@property (setter, assign);" will generate a spurious
27534 "error: expected ‘)’ before ‘,’ token". This is because
27535 cp_parser_require, unlike the C counterpart, will produce an
27536 error even if we are in error recovery. */
27537 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27539 cp_parser_skip_to_closing_parenthesis (parser,
27540 /*recovering=*/true,
27541 /*or_comma=*/false,
27542 /*consume_paren=*/true);
27546 /* ... and the property declaration(s). */
27547 properties = cp_parser_objc_struct_declaration (parser);
27549 if (properties == error_mark_node)
27551 cp_parser_skip_to_end_of_statement (parser);
27552 /* If the next token is now a `;', consume it. */
27553 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27554 cp_lexer_consume_token (parser->lexer);
27555 return;
27558 if (properties == NULL_TREE)
27559 cp_parser_error (parser, "expected identifier");
27560 else
27562 /* Comma-separated properties are chained together in
27563 reverse order; add them one by one. */
27564 properties = nreverse (properties);
27566 for (; properties; properties = TREE_CHAIN (properties))
27567 objc_add_property_declaration (loc, copy_node (properties),
27568 property_readonly, property_readwrite,
27569 property_assign, property_retain,
27570 property_copy, property_nonatomic,
27571 property_getter_ident, property_setter_ident);
27574 cp_parser_consume_semicolon_at_end_of_statement (parser);
27577 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27579 objc-synthesize-declaration:
27580 @synthesize objc-synthesize-identifier-list ;
27582 objc-synthesize-identifier-list:
27583 objc-synthesize-identifier
27584 objc-synthesize-identifier-list, objc-synthesize-identifier
27586 objc-synthesize-identifier
27587 identifier
27588 identifier = identifier
27590 For example:
27591 @synthesize MyProperty;
27592 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27594 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27595 for C. Keep them in sync.
27597 static void
27598 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27600 tree list = NULL_TREE;
27601 location_t loc;
27602 loc = cp_lexer_peek_token (parser->lexer)->location;
27604 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27605 while (true)
27607 tree property, ivar;
27608 property = cp_parser_identifier (parser);
27609 if (property == error_mark_node)
27611 cp_parser_consume_semicolon_at_end_of_statement (parser);
27612 return;
27614 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27616 cp_lexer_consume_token (parser->lexer);
27617 ivar = cp_parser_identifier (parser);
27618 if (ivar == error_mark_node)
27620 cp_parser_consume_semicolon_at_end_of_statement (parser);
27621 return;
27624 else
27625 ivar = NULL_TREE;
27626 list = chainon (list, build_tree_list (ivar, property));
27627 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27628 cp_lexer_consume_token (parser->lexer);
27629 else
27630 break;
27632 cp_parser_consume_semicolon_at_end_of_statement (parser);
27633 objc_add_synthesize_declaration (loc, list);
27636 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27638 objc-dynamic-declaration:
27639 @dynamic identifier-list ;
27641 For example:
27642 @dynamic MyProperty;
27643 @dynamic MyProperty, AnotherProperty;
27645 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27646 for C. Keep them in sync.
27648 static void
27649 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27651 tree list = NULL_TREE;
27652 location_t loc;
27653 loc = cp_lexer_peek_token (parser->lexer)->location;
27655 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27656 while (true)
27658 tree property;
27659 property = cp_parser_identifier (parser);
27660 if (property == error_mark_node)
27662 cp_parser_consume_semicolon_at_end_of_statement (parser);
27663 return;
27665 list = chainon (list, build_tree_list (NULL, property));
27666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27667 cp_lexer_consume_token (parser->lexer);
27668 else
27669 break;
27671 cp_parser_consume_semicolon_at_end_of_statement (parser);
27672 objc_add_dynamic_declaration (loc, list);
27676 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27678 /* Returns name of the next clause.
27679 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27680 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27681 returned and the token is consumed. */
27683 static pragma_omp_clause
27684 cp_parser_omp_clause_name (cp_parser *parser)
27686 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27689 result = PRAGMA_OMP_CLAUSE_IF;
27690 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27691 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27692 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27693 result = PRAGMA_OACC_CLAUSE_DELETE;
27694 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27695 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27696 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27697 result = PRAGMA_OMP_CLAUSE_FOR;
27698 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27701 const char *p = IDENTIFIER_POINTER (id);
27703 switch (p[0])
27705 case 'a':
27706 if (!strcmp ("aligned", p))
27707 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27708 else if (!strcmp ("async", p))
27709 result = PRAGMA_OACC_CLAUSE_ASYNC;
27710 break;
27711 case 'c':
27712 if (!strcmp ("collapse", p))
27713 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27714 else if (!strcmp ("copy", p))
27715 result = PRAGMA_OACC_CLAUSE_COPY;
27716 else if (!strcmp ("copyin", p))
27717 result = PRAGMA_OMP_CLAUSE_COPYIN;
27718 else if (!strcmp ("copyout", p))
27719 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27720 else if (!strcmp ("copyprivate", p))
27721 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27722 else if (!strcmp ("create", p))
27723 result = PRAGMA_OACC_CLAUSE_CREATE;
27724 break;
27725 case 'd':
27726 if (!strcmp ("depend", p))
27727 result = PRAGMA_OMP_CLAUSE_DEPEND;
27728 else if (!strcmp ("device", p))
27729 result = PRAGMA_OMP_CLAUSE_DEVICE;
27730 else if (!strcmp ("deviceptr", p))
27731 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27732 else if (!strcmp ("dist_schedule", p))
27733 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27734 break;
27735 case 'f':
27736 if (!strcmp ("final", p))
27737 result = PRAGMA_OMP_CLAUSE_FINAL;
27738 else if (!strcmp ("firstprivate", p))
27739 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27740 else if (!strcmp ("from", p))
27741 result = PRAGMA_OMP_CLAUSE_FROM;
27742 break;
27743 case 'h':
27744 if (!strcmp ("host", p))
27745 result = PRAGMA_OACC_CLAUSE_HOST;
27746 break;
27747 case 'i':
27748 if (!strcmp ("inbranch", p))
27749 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27750 break;
27751 case 'l':
27752 if (!strcmp ("lastprivate", p))
27753 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27754 else if (!strcmp ("linear", p))
27755 result = PRAGMA_OMP_CLAUSE_LINEAR;
27756 break;
27757 case 'm':
27758 if (!strcmp ("map", p))
27759 result = PRAGMA_OMP_CLAUSE_MAP;
27760 else if (!strcmp ("mergeable", p))
27761 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27762 else if (flag_cilkplus && !strcmp ("mask", p))
27763 result = PRAGMA_CILK_CLAUSE_MASK;
27764 break;
27765 case 'n':
27766 if (!strcmp ("notinbranch", p))
27767 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27768 else if (!strcmp ("nowait", p))
27769 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27770 else if (flag_cilkplus && !strcmp ("nomask", p))
27771 result = PRAGMA_CILK_CLAUSE_NOMASK;
27772 else if (!strcmp ("num_gangs", p))
27773 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27774 else if (!strcmp ("num_teams", p))
27775 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27776 else if (!strcmp ("num_threads", p))
27777 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27778 else if (!strcmp ("num_workers", p))
27779 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27780 break;
27781 case 'o':
27782 if (!strcmp ("ordered", p))
27783 result = PRAGMA_OMP_CLAUSE_ORDERED;
27784 break;
27785 case 'p':
27786 if (!strcmp ("parallel", p))
27787 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27788 else if (!strcmp ("present", p))
27789 result = PRAGMA_OACC_CLAUSE_PRESENT;
27790 else if (!strcmp ("present_or_copy", p)
27791 || !strcmp ("pcopy", p))
27792 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27793 else if (!strcmp ("present_or_copyin", p)
27794 || !strcmp ("pcopyin", p))
27795 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27796 else if (!strcmp ("present_or_copyout", p)
27797 || !strcmp ("pcopyout", p))
27798 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27799 else if (!strcmp ("present_or_create", p)
27800 || !strcmp ("pcreate", p))
27801 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27802 else if (!strcmp ("proc_bind", p))
27803 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27804 break;
27805 case 'r':
27806 if (!strcmp ("reduction", p))
27807 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27808 break;
27809 case 's':
27810 if (!strcmp ("safelen", p))
27811 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27812 else if (!strcmp ("schedule", p))
27813 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27814 else if (!strcmp ("sections", p))
27815 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27816 else if (!strcmp ("self", p))
27817 result = PRAGMA_OACC_CLAUSE_SELF;
27818 else if (!strcmp ("shared", p))
27819 result = PRAGMA_OMP_CLAUSE_SHARED;
27820 else if (!strcmp ("simdlen", p))
27821 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27822 break;
27823 case 't':
27824 if (!strcmp ("taskgroup", p))
27825 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27826 else if (!strcmp ("thread_limit", p))
27827 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27828 else if (!strcmp ("to", p))
27829 result = PRAGMA_OMP_CLAUSE_TO;
27830 break;
27831 case 'u':
27832 if (!strcmp ("uniform", p))
27833 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27834 else if (!strcmp ("untied", p))
27835 result = PRAGMA_OMP_CLAUSE_UNTIED;
27836 break;
27837 case 'v':
27838 if (!strcmp ("vector_length", p))
27839 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27840 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27841 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27842 break;
27843 case 'w':
27844 if (!strcmp ("wait", p))
27845 result = PRAGMA_OACC_CLAUSE_WAIT;
27846 break;
27850 if (result != PRAGMA_OMP_CLAUSE_NONE)
27851 cp_lexer_consume_token (parser->lexer);
27853 return result;
27856 /* Validate that a clause of the given type does not already exist. */
27858 static void
27859 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27860 const char *name, location_t location)
27862 tree c;
27864 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27865 if (OMP_CLAUSE_CODE (c) == code)
27867 error_at (location, "too many %qs clauses", name);
27868 break;
27872 /* OpenMP 2.5:
27873 variable-list:
27874 identifier
27875 variable-list , identifier
27877 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27878 colon). An opening parenthesis will have been consumed by the caller.
27880 If KIND is nonzero, create the appropriate node and install the decl
27881 in OMP_CLAUSE_DECL and add the node to the head of the list.
27883 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27884 return the list created.
27886 COLON can be NULL if only closing parenthesis should end the list,
27887 or pointer to bool which will receive false if the list is terminated
27888 by closing parenthesis or true if the list is terminated by colon. */
27890 static tree
27891 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27892 tree list, bool *colon)
27894 cp_token *token;
27895 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27896 if (colon)
27898 parser->colon_corrects_to_scope_p = false;
27899 *colon = false;
27901 while (1)
27903 tree name, decl;
27905 token = cp_lexer_peek_token (parser->lexer);
27906 name = cp_parser_id_expression (parser, /*template_p=*/false,
27907 /*check_dependency_p=*/true,
27908 /*template_p=*/NULL,
27909 /*declarator_p=*/false,
27910 /*optional_p=*/false);
27911 if (name == error_mark_node)
27912 goto skip_comma;
27914 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27915 if (decl == error_mark_node)
27916 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27917 token->location);
27918 else if (kind != 0)
27920 switch (kind)
27922 case OMP_CLAUSE__CACHE_:
27923 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27925 error_at (token->location, "expected %<[%>");
27926 decl = error_mark_node;
27927 break;
27929 /* FALL THROUGH. */
27930 case OMP_CLAUSE_MAP:
27931 case OMP_CLAUSE_FROM:
27932 case OMP_CLAUSE_TO:
27933 case OMP_CLAUSE_DEPEND:
27934 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27936 tree low_bound = NULL_TREE, length = NULL_TREE;
27938 parser->colon_corrects_to_scope_p = false;
27939 cp_lexer_consume_token (parser->lexer);
27940 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27941 low_bound = cp_parser_expression (parser);
27942 if (!colon)
27943 parser->colon_corrects_to_scope_p
27944 = saved_colon_corrects_to_scope_p;
27945 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27946 length = integer_one_node;
27947 else
27949 /* Look for `:'. */
27950 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27951 goto skip_comma;
27952 if (!cp_lexer_next_token_is (parser->lexer,
27953 CPP_CLOSE_SQUARE))
27954 length = cp_parser_expression (parser);
27956 /* So we need here fully folded values. */
27957 if (length)
27958 length = cp_fully_fold (length);
27959 if (low_bound)
27960 low_bound = cp_fully_fold (low_bound);
27961 /* Look for the closing `]'. */
27962 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27963 RT_CLOSE_SQUARE))
27964 goto skip_comma;
27966 if (kind == OMP_CLAUSE__CACHE_)
27968 if (TREE_CODE (low_bound) != INTEGER_CST
27969 && !TREE_READONLY (low_bound))
27971 error_at (token->location,
27972 "%qD is not a constant", low_bound);
27973 decl = error_mark_node;
27976 if (TREE_CODE (length) != INTEGER_CST
27977 && !TREE_READONLY (length))
27979 error_at (token->location,
27980 "%qD is not a constant", length);
27981 decl = error_mark_node;
27985 decl = tree_cons (low_bound, length, decl);
27987 break;
27988 default:
27989 break;
27992 tree u = build_omp_clause (token->location, kind);
27993 OMP_CLAUSE_DECL (u) = decl;
27994 OMP_CLAUSE_CHAIN (u) = list;
27995 list = u;
27997 else
27998 list = tree_cons (decl, NULL_TREE, list);
28000 get_comma:
28001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28002 break;
28003 cp_lexer_consume_token (parser->lexer);
28006 if (colon)
28007 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28009 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28011 *colon = true;
28012 cp_parser_require (parser, CPP_COLON, RT_COLON);
28013 return list;
28016 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28018 int ending;
28020 /* Try to resync to an unnested comma. Copied from
28021 cp_parser_parenthesized_expression_list. */
28022 skip_comma:
28023 if (colon)
28024 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28025 ending = cp_parser_skip_to_closing_parenthesis (parser,
28026 /*recovering=*/true,
28027 /*or_comma=*/true,
28028 /*consume_paren=*/true);
28029 if (ending < 0)
28030 goto get_comma;
28033 return list;
28036 /* Similarly, but expect leading and trailing parenthesis. This is a very
28037 common case for omp clauses. */
28039 static tree
28040 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28042 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28043 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28044 return list;
28047 /* OpenACC 2.0:
28048 copy ( variable-list )
28049 copyin ( variable-list )
28050 copyout ( variable-list )
28051 create ( variable-list )
28052 delete ( variable-list )
28053 present ( variable-list )
28054 present_or_copy ( variable-list )
28055 pcopy ( variable-list )
28056 present_or_copyin ( variable-list )
28057 pcopyin ( variable-list )
28058 present_or_copyout ( variable-list )
28059 pcopyout ( variable-list )
28060 present_or_create ( variable-list )
28061 pcreate ( variable-list ) */
28063 static tree
28064 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28065 tree list)
28067 enum gomp_map_kind kind;
28068 switch (c_kind)
28070 case PRAGMA_OACC_CLAUSE_COPY:
28071 kind = GOMP_MAP_FORCE_TOFROM;
28072 break;
28073 case PRAGMA_OACC_CLAUSE_COPYIN:
28074 kind = GOMP_MAP_FORCE_TO;
28075 break;
28076 case PRAGMA_OACC_CLAUSE_COPYOUT:
28077 kind = GOMP_MAP_FORCE_FROM;
28078 break;
28079 case PRAGMA_OACC_CLAUSE_CREATE:
28080 kind = GOMP_MAP_FORCE_ALLOC;
28081 break;
28082 case PRAGMA_OACC_CLAUSE_DELETE:
28083 kind = GOMP_MAP_FORCE_DEALLOC;
28084 break;
28085 case PRAGMA_OACC_CLAUSE_DEVICE:
28086 kind = GOMP_MAP_FORCE_TO;
28087 break;
28088 case PRAGMA_OACC_CLAUSE_HOST:
28089 case PRAGMA_OACC_CLAUSE_SELF:
28090 kind = GOMP_MAP_FORCE_FROM;
28091 break;
28092 case PRAGMA_OACC_CLAUSE_PRESENT:
28093 kind = GOMP_MAP_FORCE_PRESENT;
28094 break;
28095 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28096 kind = GOMP_MAP_TOFROM;
28097 break;
28098 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28099 kind = GOMP_MAP_TO;
28100 break;
28101 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28102 kind = GOMP_MAP_FROM;
28103 break;
28104 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28105 kind = GOMP_MAP_ALLOC;
28106 break;
28107 default:
28108 gcc_unreachable ();
28110 tree nl, c;
28111 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28113 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28114 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28116 return nl;
28119 /* OpenACC 2.0:
28120 deviceptr ( variable-list ) */
28122 static tree
28123 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28126 tree vars, t;
28128 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28129 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28130 variable-list must only allow for pointer variables. */
28131 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28132 for (t = vars; t; t = TREE_CHAIN (t))
28134 tree v = TREE_PURPOSE (t);
28136 /* FIXME diagnostics: Ideally we should keep individual
28137 locations for all the variables in the var list to make the
28138 following errors more precise. Perhaps
28139 c_parser_omp_var_list_parens should construct a list of
28140 locations to go along with the var list. */
28142 if (!VAR_P (v))
28143 error_at (loc, "%qD is not a variable", v);
28144 else if (TREE_TYPE (v) == error_mark_node)
28146 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28147 error_at (loc, "%qD is not a pointer variable", v);
28149 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28150 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28151 OMP_CLAUSE_DECL (u) = v;
28152 OMP_CLAUSE_CHAIN (u) = list;
28153 list = u;
28156 return list;
28159 /* OpenACC:
28160 vector_length ( expression ) */
28162 static tree
28163 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28165 tree t, c;
28166 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28167 bool error = false;
28169 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28170 return list;
28172 t = cp_parser_condition (parser);
28173 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28175 error_at (location, "expected positive integer expression");
28176 error = true;
28179 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28182 /*or_comma=*/false,
28183 /*consume_paren=*/true);
28184 return list;
28187 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28188 location);
28190 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28191 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28192 OMP_CLAUSE_CHAIN (c) = list;
28193 list = c;
28195 return list;
28198 /* OpenACC 2.0
28199 Parse wait clause or directive parameters. */
28201 static tree
28202 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28204 vec<tree, va_gc> *args;
28205 tree t, args_tree;
28207 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28208 /*cast_p=*/false,
28209 /*allow_expansion_p=*/true,
28210 /*non_constant_p=*/NULL);
28212 if (args == NULL || args->length () == 0)
28214 cp_parser_error (parser, "expected integer expression before ')'");
28215 if (args != NULL)
28216 release_tree_vector (args);
28217 return list;
28220 args_tree = build_tree_list_vec (args);
28222 release_tree_vector (args);
28224 for (t = args_tree; t; t = TREE_CHAIN (t))
28226 tree targ = TREE_VALUE (t);
28228 if (targ != error_mark_node)
28230 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28231 error ("%<wait%> expression must be integral");
28232 else
28234 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28236 mark_rvalue_use (targ);
28237 OMP_CLAUSE_DECL (c) = targ;
28238 OMP_CLAUSE_CHAIN (c) = list;
28239 list = c;
28244 return list;
28247 /* OpenACC:
28248 wait ( int-expr-list ) */
28250 static tree
28251 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28253 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28255 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28256 return list;
28258 list = cp_parser_oacc_wait_list (parser, location, list);
28260 return list;
28263 /* OpenMP 3.0:
28264 collapse ( constant-expression ) */
28266 static tree
28267 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28269 tree c, num;
28270 location_t loc;
28271 HOST_WIDE_INT n;
28273 loc = cp_lexer_peek_token (parser->lexer)->location;
28274 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28275 return list;
28277 num = cp_parser_constant_expression (parser);
28279 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28280 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28281 /*or_comma=*/false,
28282 /*consume_paren=*/true);
28284 if (num == error_mark_node)
28285 return list;
28286 num = fold_non_dependent_expr (num);
28287 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28288 || !tree_fits_shwi_p (num)
28289 || (n = tree_to_shwi (num)) <= 0
28290 || (int) n != n)
28292 error_at (loc, "collapse argument needs positive constant integer expression");
28293 return list;
28296 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28297 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28298 OMP_CLAUSE_CHAIN (c) = list;
28299 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28301 return c;
28304 /* OpenMP 2.5:
28305 default ( shared | none ) */
28307 static tree
28308 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28310 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28311 tree c;
28313 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28314 return list;
28315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28318 const char *p = IDENTIFIER_POINTER (id);
28320 switch (p[0])
28322 case 'n':
28323 if (strcmp ("none", p) != 0)
28324 goto invalid_kind;
28325 kind = OMP_CLAUSE_DEFAULT_NONE;
28326 break;
28328 case 's':
28329 if (strcmp ("shared", p) != 0)
28330 goto invalid_kind;
28331 kind = OMP_CLAUSE_DEFAULT_SHARED;
28332 break;
28334 default:
28335 goto invalid_kind;
28338 cp_lexer_consume_token (parser->lexer);
28340 else
28342 invalid_kind:
28343 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28347 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28348 /*or_comma=*/false,
28349 /*consume_paren=*/true);
28351 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28352 return list;
28354 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28355 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28356 OMP_CLAUSE_CHAIN (c) = list;
28357 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28359 return c;
28362 /* OpenMP 3.1:
28363 final ( expression ) */
28365 static tree
28366 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28368 tree t, c;
28370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28371 return list;
28373 t = cp_parser_condition (parser);
28375 if (t == error_mark_node
28376 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28378 /*or_comma=*/false,
28379 /*consume_paren=*/true);
28381 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28383 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28384 OMP_CLAUSE_FINAL_EXPR (c) = t;
28385 OMP_CLAUSE_CHAIN (c) = list;
28387 return c;
28390 /* OpenMP 2.5:
28391 if ( expression ) */
28393 static tree
28394 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28396 tree t, c;
28398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28399 return list;
28401 t = cp_parser_condition (parser);
28403 if (t == error_mark_node
28404 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28406 /*or_comma=*/false,
28407 /*consume_paren=*/true);
28409 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28411 c = build_omp_clause (location, OMP_CLAUSE_IF);
28412 OMP_CLAUSE_IF_EXPR (c) = t;
28413 OMP_CLAUSE_CHAIN (c) = list;
28415 return c;
28418 /* OpenMP 3.1:
28419 mergeable */
28421 static tree
28422 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28423 tree list, location_t location)
28425 tree c;
28427 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28428 location);
28430 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28431 OMP_CLAUSE_CHAIN (c) = list;
28432 return c;
28435 /* OpenMP 2.5:
28436 nowait */
28438 static tree
28439 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28440 tree list, location_t location)
28442 tree c;
28444 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28446 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28447 OMP_CLAUSE_CHAIN (c) = list;
28448 return c;
28451 /* OpenACC:
28452 num_gangs ( expression ) */
28454 static tree
28455 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28457 tree t, c;
28458 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28461 return list;
28463 t = cp_parser_condition (parser);
28465 if (t == error_mark_node
28466 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28468 /*or_comma=*/false,
28469 /*consume_paren=*/true);
28471 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28473 error_at (location, "expected positive integer expression");
28474 return list;
28477 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28479 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28480 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28481 OMP_CLAUSE_CHAIN (c) = list;
28482 list = c;
28484 return list;
28487 /* OpenMP 2.5:
28488 num_threads ( expression ) */
28490 static tree
28491 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28492 location_t location)
28494 tree t, c;
28496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28497 return list;
28499 t = cp_parser_expression (parser);
28501 if (t == error_mark_node
28502 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28504 /*or_comma=*/false,
28505 /*consume_paren=*/true);
28507 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28508 "num_threads", location);
28510 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28511 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28512 OMP_CLAUSE_CHAIN (c) = list;
28514 return c;
28517 /* OpenACC:
28518 num_workers ( expression ) */
28520 static tree
28521 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28523 tree t, c;
28524 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28527 return list;
28529 t = cp_parser_condition (parser);
28531 if (t == error_mark_node
28532 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28533 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28534 /*or_comma=*/false,
28535 /*consume_paren=*/true);
28537 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28539 error_at (location, "expected positive integer expression");
28540 return list;
28543 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28544 location);
28546 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28547 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28548 OMP_CLAUSE_CHAIN (c) = list;
28549 list = c;
28551 return list;
28554 /* OpenMP 2.5:
28555 ordered */
28557 static tree
28558 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28559 tree list, location_t location)
28561 tree c;
28563 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28564 "ordered", location);
28566 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28567 OMP_CLAUSE_CHAIN (c) = list;
28568 return c;
28571 /* OpenMP 2.5:
28572 reduction ( reduction-operator : variable-list )
28574 reduction-operator:
28575 One of: + * - & ^ | && ||
28577 OpenMP 3.1:
28579 reduction-operator:
28580 One of: + * - & ^ | && || min max
28582 OpenMP 4.0:
28584 reduction-operator:
28585 One of: + * - & ^ | && ||
28586 id-expression */
28588 static tree
28589 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28591 enum tree_code code = ERROR_MARK;
28592 tree nlist, c, id = NULL_TREE;
28594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28595 return list;
28597 switch (cp_lexer_peek_token (parser->lexer)->type)
28599 case CPP_PLUS: code = PLUS_EXPR; break;
28600 case CPP_MULT: code = MULT_EXPR; break;
28601 case CPP_MINUS: code = MINUS_EXPR; break;
28602 case CPP_AND: code = BIT_AND_EXPR; break;
28603 case CPP_XOR: code = BIT_XOR_EXPR; break;
28604 case CPP_OR: code = BIT_IOR_EXPR; break;
28605 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28606 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28607 default: break;
28610 if (code != ERROR_MARK)
28611 cp_lexer_consume_token (parser->lexer);
28612 else
28614 bool saved_colon_corrects_to_scope_p;
28615 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28616 parser->colon_corrects_to_scope_p = false;
28617 id = cp_parser_id_expression (parser, /*template_p=*/false,
28618 /*check_dependency_p=*/true,
28619 /*template_p=*/NULL,
28620 /*declarator_p=*/false,
28621 /*optional_p=*/false);
28622 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28623 if (identifier_p (id))
28625 const char *p = IDENTIFIER_POINTER (id);
28627 if (strcmp (p, "min") == 0)
28628 code = MIN_EXPR;
28629 else if (strcmp (p, "max") == 0)
28630 code = MAX_EXPR;
28631 else if (id == ansi_opname (PLUS_EXPR))
28632 code = PLUS_EXPR;
28633 else if (id == ansi_opname (MULT_EXPR))
28634 code = MULT_EXPR;
28635 else if (id == ansi_opname (MINUS_EXPR))
28636 code = MINUS_EXPR;
28637 else if (id == ansi_opname (BIT_AND_EXPR))
28638 code = BIT_AND_EXPR;
28639 else if (id == ansi_opname (BIT_IOR_EXPR))
28640 code = BIT_IOR_EXPR;
28641 else if (id == ansi_opname (BIT_XOR_EXPR))
28642 code = BIT_XOR_EXPR;
28643 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28644 code = TRUTH_ANDIF_EXPR;
28645 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28646 code = TRUTH_ORIF_EXPR;
28647 id = omp_reduction_id (code, id, NULL_TREE);
28648 tree scope = parser->scope;
28649 if (scope)
28650 id = build_qualified_name (NULL_TREE, scope, id, false);
28651 parser->scope = NULL_TREE;
28652 parser->qualifying_scope = NULL_TREE;
28653 parser->object_scope = NULL_TREE;
28655 else
28657 error ("invalid reduction-identifier");
28658 resync_fail:
28659 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28660 /*or_comma=*/false,
28661 /*consume_paren=*/true);
28662 return list;
28666 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28667 goto resync_fail;
28669 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28670 NULL);
28671 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28673 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28674 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28677 return nlist;
28680 /* OpenMP 2.5:
28681 schedule ( schedule-kind )
28682 schedule ( schedule-kind , expression )
28684 schedule-kind:
28685 static | dynamic | guided | runtime | auto */
28687 static tree
28688 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28690 tree c, t;
28692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28693 return list;
28695 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28699 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28700 const char *p = IDENTIFIER_POINTER (id);
28702 switch (p[0])
28704 case 'd':
28705 if (strcmp ("dynamic", p) != 0)
28706 goto invalid_kind;
28707 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28708 break;
28710 case 'g':
28711 if (strcmp ("guided", p) != 0)
28712 goto invalid_kind;
28713 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28714 break;
28716 case 'r':
28717 if (strcmp ("runtime", p) != 0)
28718 goto invalid_kind;
28719 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28720 break;
28722 default:
28723 goto invalid_kind;
28726 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28727 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28728 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28729 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28730 else
28731 goto invalid_kind;
28732 cp_lexer_consume_token (parser->lexer);
28734 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28736 cp_token *token;
28737 cp_lexer_consume_token (parser->lexer);
28739 token = cp_lexer_peek_token (parser->lexer);
28740 t = cp_parser_assignment_expression (parser);
28742 if (t == error_mark_node)
28743 goto resync_fail;
28744 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28745 error_at (token->location, "schedule %<runtime%> does not take "
28746 "a %<chunk_size%> parameter");
28747 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28748 error_at (token->location, "schedule %<auto%> does not take "
28749 "a %<chunk_size%> parameter");
28750 else
28751 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28753 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28754 goto resync_fail;
28756 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28757 goto resync_fail;
28759 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28760 OMP_CLAUSE_CHAIN (c) = list;
28761 return c;
28763 invalid_kind:
28764 cp_parser_error (parser, "invalid schedule kind");
28765 resync_fail:
28766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28767 /*or_comma=*/false,
28768 /*consume_paren=*/true);
28769 return list;
28772 /* OpenMP 3.0:
28773 untied */
28775 static tree
28776 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28777 tree list, location_t location)
28779 tree c;
28781 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28783 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28784 OMP_CLAUSE_CHAIN (c) = list;
28785 return c;
28788 /* OpenMP 4.0:
28789 inbranch
28790 notinbranch */
28792 static tree
28793 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28794 tree list, location_t location)
28796 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28797 tree c = build_omp_clause (location, code);
28798 OMP_CLAUSE_CHAIN (c) = list;
28799 return c;
28802 /* OpenMP 4.0:
28803 parallel
28805 sections
28806 taskgroup */
28808 static tree
28809 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28810 enum omp_clause_code code,
28811 tree list, location_t location)
28813 tree c = build_omp_clause (location, code);
28814 OMP_CLAUSE_CHAIN (c) = list;
28815 return c;
28818 /* OpenMP 4.0:
28819 num_teams ( expression ) */
28821 static tree
28822 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28823 location_t location)
28825 tree t, c;
28827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28828 return list;
28830 t = cp_parser_expression (parser);
28832 if (t == error_mark_node
28833 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28835 /*or_comma=*/false,
28836 /*consume_paren=*/true);
28838 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28839 "num_teams", location);
28841 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28842 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28843 OMP_CLAUSE_CHAIN (c) = list;
28845 return c;
28848 /* OpenMP 4.0:
28849 thread_limit ( expression ) */
28851 static tree
28852 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28853 location_t location)
28855 tree t, c;
28857 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28858 return list;
28860 t = cp_parser_expression (parser);
28862 if (t == error_mark_node
28863 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28865 /*or_comma=*/false,
28866 /*consume_paren=*/true);
28868 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28869 "thread_limit", location);
28871 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28872 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28873 OMP_CLAUSE_CHAIN (c) = list;
28875 return c;
28878 /* OpenMP 4.0:
28879 aligned ( variable-list )
28880 aligned ( variable-list : constant-expression ) */
28882 static tree
28883 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28885 tree nlist, c, alignment = NULL_TREE;
28886 bool colon;
28888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28889 return list;
28891 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28892 &colon);
28894 if (colon)
28896 alignment = cp_parser_constant_expression (parser);
28897 alignment = fold_simple_on_cst (alignment);
28899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28900 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28901 /*or_comma=*/false,
28902 /*consume_paren=*/true);
28904 if (alignment == error_mark_node)
28905 alignment = NULL_TREE;
28908 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28909 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28911 return nlist;
28914 /* OpenMP 4.0:
28915 linear ( variable-list )
28916 linear ( variable-list : expression ) */
28918 static tree
28919 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28920 bool is_cilk_simd_fn)
28922 tree nlist, c, step = integer_one_node;
28923 bool colon;
28925 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28926 return list;
28928 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28929 &colon);
28931 if (colon)
28933 step = cp_parser_expression (parser);
28935 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28937 sorry ("using parameters for %<linear%> step is not supported yet");
28938 step = integer_one_node;
28940 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28942 /*or_comma=*/false,
28943 /*consume_paren=*/true);
28945 if (step == error_mark_node)
28946 return list;
28949 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28950 OMP_CLAUSE_LINEAR_STEP (c) = step;
28952 return nlist;
28955 /* OpenMP 4.0:
28956 safelen ( constant-expression ) */
28958 static tree
28959 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28960 location_t location)
28962 tree t, c;
28964 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28965 return list;
28967 t = cp_parser_constant_expression (parser);
28969 if (t == error_mark_node
28970 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28972 /*or_comma=*/false,
28973 /*consume_paren=*/true);
28975 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28977 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28978 OMP_CLAUSE_SAFELEN_EXPR (c) = cp_fully_fold (t);
28979 OMP_CLAUSE_CHAIN (c) = list;
28981 return c;
28984 /* OpenMP 4.0:
28985 simdlen ( constant-expression ) */
28987 static tree
28988 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28989 location_t location)
28991 tree t, c;
28993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28994 return list;
28996 t = cp_parser_constant_expression (parser);
28998 if (t == error_mark_node
28999 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29001 /*or_comma=*/false,
29002 /*consume_paren=*/true);
29004 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
29006 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
29007 OMP_CLAUSE_SIMDLEN_EXPR (c) = cp_fully_fold (t);
29008 OMP_CLAUSE_CHAIN (c) = list;
29010 return c;
29013 /* OpenMP 4.0:
29014 depend ( depend-kind : variable-list )
29016 depend-kind:
29017 in | out | inout */
29019 static tree
29020 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
29022 tree nlist, c;
29023 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29025 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29026 return list;
29028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29030 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29031 const char *p = IDENTIFIER_POINTER (id);
29033 if (strcmp ("in", p) == 0)
29034 kind = OMP_CLAUSE_DEPEND_IN;
29035 else if (strcmp ("inout", p) == 0)
29036 kind = OMP_CLAUSE_DEPEND_INOUT;
29037 else if (strcmp ("out", p) == 0)
29038 kind = OMP_CLAUSE_DEPEND_OUT;
29039 else
29040 goto invalid_kind;
29042 else
29043 goto invalid_kind;
29045 cp_lexer_consume_token (parser->lexer);
29046 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29047 goto resync_fail;
29049 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29050 NULL);
29052 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29053 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29055 return nlist;
29057 invalid_kind:
29058 cp_parser_error (parser, "invalid depend kind");
29059 resync_fail:
29060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29061 /*or_comma=*/false,
29062 /*consume_paren=*/true);
29063 return list;
29066 /* OpenMP 4.0:
29067 map ( map-kind : variable-list )
29068 map ( variable-list )
29070 map-kind:
29071 alloc | to | from | tofrom */
29073 static tree
29074 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29076 tree nlist, c;
29077 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29079 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29080 return list;
29082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29083 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29086 const char *p = IDENTIFIER_POINTER (id);
29088 if (strcmp ("alloc", p) == 0)
29089 kind = GOMP_MAP_ALLOC;
29090 else if (strcmp ("to", p) == 0)
29091 kind = GOMP_MAP_TO;
29092 else if (strcmp ("from", p) == 0)
29093 kind = GOMP_MAP_FROM;
29094 else if (strcmp ("tofrom", p) == 0)
29095 kind = GOMP_MAP_TOFROM;
29096 else
29098 cp_parser_error (parser, "invalid map kind");
29099 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29100 /*or_comma=*/false,
29101 /*consume_paren=*/true);
29102 return list;
29104 cp_lexer_consume_token (parser->lexer);
29105 cp_lexer_consume_token (parser->lexer);
29108 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29109 NULL);
29111 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29112 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29114 return nlist;
29117 /* OpenMP 4.0:
29118 device ( expression ) */
29120 static tree
29121 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29122 location_t location)
29124 tree t, c;
29126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29127 return list;
29129 t = cp_parser_expression (parser);
29131 if (t == error_mark_node
29132 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29133 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29134 /*or_comma=*/false,
29135 /*consume_paren=*/true);
29137 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29138 "device", location);
29140 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29141 OMP_CLAUSE_DEVICE_ID (c) = t;
29142 OMP_CLAUSE_CHAIN (c) = list;
29144 return c;
29147 /* OpenMP 4.0:
29148 dist_schedule ( static )
29149 dist_schedule ( static , expression ) */
29151 static tree
29152 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29153 location_t location)
29155 tree c, t;
29157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29158 return list;
29160 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29162 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29163 goto invalid_kind;
29164 cp_lexer_consume_token (parser->lexer);
29166 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29168 cp_lexer_consume_token (parser->lexer);
29170 t = cp_parser_assignment_expression (parser);
29172 if (t == error_mark_node)
29173 goto resync_fail;
29174 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29177 goto resync_fail;
29179 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29180 goto resync_fail;
29182 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29183 location);
29184 OMP_CLAUSE_CHAIN (c) = list;
29185 return c;
29187 invalid_kind:
29188 cp_parser_error (parser, "invalid dist_schedule kind");
29189 resync_fail:
29190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29191 /*or_comma=*/false,
29192 /*consume_paren=*/true);
29193 return list;
29196 /* OpenMP 4.0:
29197 proc_bind ( proc-bind-kind )
29199 proc-bind-kind:
29200 master | close | spread */
29202 static tree
29203 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29204 location_t location)
29206 tree c;
29207 enum omp_clause_proc_bind_kind kind;
29209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29210 return list;
29212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29214 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29215 const char *p = IDENTIFIER_POINTER (id);
29217 if (strcmp ("master", p) == 0)
29218 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29219 else if (strcmp ("close", p) == 0)
29220 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29221 else if (strcmp ("spread", p) == 0)
29222 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29223 else
29224 goto invalid_kind;
29226 else
29227 goto invalid_kind;
29229 cp_lexer_consume_token (parser->lexer);
29230 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29231 goto resync_fail;
29233 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29234 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29235 location);
29236 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29237 OMP_CLAUSE_CHAIN (c) = list;
29238 return c;
29240 invalid_kind:
29241 cp_parser_error (parser, "invalid depend kind");
29242 resync_fail:
29243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29244 /*or_comma=*/false,
29245 /*consume_paren=*/true);
29246 return list;
29249 /* OpenACC:
29250 async [( int-expr )] */
29252 static tree
29253 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29255 tree c, t;
29256 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29258 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29260 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29262 cp_lexer_consume_token (parser->lexer);
29264 t = cp_parser_expression (parser);
29265 if (t == error_mark_node
29266 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29268 /*or_comma=*/false,
29269 /*consume_paren=*/true);
29272 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29274 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29275 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29276 OMP_CLAUSE_CHAIN (c) = list;
29277 list = c;
29279 return list;
29282 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29283 is a bitmask in MASK. Return the list of clauses found. */
29285 static tree
29286 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29287 const char *where, cp_token *pragma_tok,
29288 bool finish_p = true)
29290 tree clauses = NULL;
29291 bool first = true;
29293 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29295 location_t here;
29296 pragma_omp_clause c_kind;
29297 const char *c_name;
29298 tree prev = clauses;
29300 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29301 cp_lexer_consume_token (parser->lexer);
29303 here = cp_lexer_peek_token (parser->lexer)->location;
29304 c_kind = cp_parser_omp_clause_name (parser);
29306 switch (c_kind)
29308 case PRAGMA_OACC_CLAUSE_ASYNC:
29309 clauses = cp_parser_oacc_clause_async (parser, clauses);
29310 c_name = "async";
29311 break;
29312 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29313 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29314 c_name = "collapse";
29315 break;
29316 case PRAGMA_OACC_CLAUSE_COPY:
29317 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29318 c_name = "copy";
29319 break;
29320 case PRAGMA_OACC_CLAUSE_COPYIN:
29321 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29322 c_name = "copyin";
29323 break;
29324 case PRAGMA_OACC_CLAUSE_COPYOUT:
29325 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29326 c_name = "copyout";
29327 break;
29328 case PRAGMA_OACC_CLAUSE_CREATE:
29329 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29330 c_name = "create";
29331 break;
29332 case PRAGMA_OACC_CLAUSE_DELETE:
29333 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29334 c_name = "delete";
29335 break;
29336 case PRAGMA_OACC_CLAUSE_DEVICE:
29337 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29338 c_name = "device";
29339 break;
29340 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29341 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29342 c_name = "deviceptr";
29343 break;
29344 case PRAGMA_OACC_CLAUSE_HOST:
29345 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29346 c_name = "host";
29347 break;
29348 case PRAGMA_OACC_CLAUSE_IF:
29349 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29350 c_name = "if";
29351 break;
29352 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29353 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29354 c_name = "num_gangs";
29355 break;
29356 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29357 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29358 c_name = "num_workers";
29359 break;
29360 case PRAGMA_OACC_CLAUSE_PRESENT:
29361 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29362 c_name = "present";
29363 break;
29364 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29365 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29366 c_name = "present_or_copy";
29367 break;
29368 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29369 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29370 c_name = "present_or_copyin";
29371 break;
29372 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29373 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29374 c_name = "present_or_copyout";
29375 break;
29376 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29377 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29378 c_name = "present_or_create";
29379 break;
29380 case PRAGMA_OACC_CLAUSE_REDUCTION:
29381 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29382 c_name = "reduction";
29383 break;
29384 case PRAGMA_OACC_CLAUSE_SELF:
29385 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29386 c_name = "self";
29387 break;
29388 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29389 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29390 c_name = "vector_length";
29391 break;
29392 case PRAGMA_OACC_CLAUSE_WAIT:
29393 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29394 c_name = "wait";
29395 break;
29396 default:
29397 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29398 goto saw_error;
29401 first = false;
29403 if (((mask >> c_kind) & 1) == 0)
29405 /* Remove the invalid clause(s) from the list to avoid
29406 confusing the rest of the compiler. */
29407 clauses = prev;
29408 error_at (here, "%qs is not valid for %qs", c_name, where);
29412 saw_error:
29413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29415 if (finish_p)
29416 return finish_omp_clauses (clauses);
29418 return clauses;
29421 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29422 is a bitmask in MASK. Return the list of clauses found; the result
29423 of clause default goes in *pdefault. */
29425 static tree
29426 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29427 const char *where, cp_token *pragma_tok,
29428 bool finish_p = true)
29430 tree clauses = NULL;
29431 bool first = true;
29432 cp_token *token = NULL;
29433 bool cilk_simd_fn = false;
29435 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29437 pragma_omp_clause c_kind;
29438 const char *c_name;
29439 tree prev = clauses;
29441 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29442 cp_lexer_consume_token (parser->lexer);
29444 token = cp_lexer_peek_token (parser->lexer);
29445 c_kind = cp_parser_omp_clause_name (parser);
29447 switch (c_kind)
29449 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29450 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29451 token->location);
29452 c_name = "collapse";
29453 break;
29454 case PRAGMA_OMP_CLAUSE_COPYIN:
29455 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29456 c_name = "copyin";
29457 break;
29458 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29459 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29460 clauses);
29461 c_name = "copyprivate";
29462 break;
29463 case PRAGMA_OMP_CLAUSE_DEFAULT:
29464 clauses = cp_parser_omp_clause_default (parser, clauses,
29465 token->location);
29466 c_name = "default";
29467 break;
29468 case PRAGMA_OMP_CLAUSE_FINAL:
29469 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29470 c_name = "final";
29471 break;
29472 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29474 clauses);
29475 c_name = "firstprivate";
29476 break;
29477 case PRAGMA_OMP_CLAUSE_IF:
29478 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29479 c_name = "if";
29480 break;
29481 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29482 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29483 clauses);
29484 c_name = "lastprivate";
29485 break;
29486 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29487 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29488 token->location);
29489 c_name = "mergeable";
29490 break;
29491 case PRAGMA_OMP_CLAUSE_NOWAIT:
29492 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29493 c_name = "nowait";
29494 break;
29495 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29496 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29497 token->location);
29498 c_name = "num_threads";
29499 break;
29500 case PRAGMA_OMP_CLAUSE_ORDERED:
29501 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29502 token->location);
29503 c_name = "ordered";
29504 break;
29505 case PRAGMA_OMP_CLAUSE_PRIVATE:
29506 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29507 clauses);
29508 c_name = "private";
29509 break;
29510 case PRAGMA_OMP_CLAUSE_REDUCTION:
29511 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29512 c_name = "reduction";
29513 break;
29514 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29515 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29516 token->location);
29517 c_name = "schedule";
29518 break;
29519 case PRAGMA_OMP_CLAUSE_SHARED:
29520 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29521 clauses);
29522 c_name = "shared";
29523 break;
29524 case PRAGMA_OMP_CLAUSE_UNTIED:
29525 clauses = cp_parser_omp_clause_untied (parser, clauses,
29526 token->location);
29527 c_name = "untied";
29528 break;
29529 case PRAGMA_OMP_CLAUSE_INBRANCH:
29530 case PRAGMA_CILK_CLAUSE_MASK:
29531 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29532 clauses, token->location);
29533 c_name = "inbranch";
29534 break;
29535 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29536 case PRAGMA_CILK_CLAUSE_NOMASK:
29537 clauses = cp_parser_omp_clause_branch (parser,
29538 OMP_CLAUSE_NOTINBRANCH,
29539 clauses, token->location);
29540 c_name = "notinbranch";
29541 break;
29542 case PRAGMA_OMP_CLAUSE_PARALLEL:
29543 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29544 clauses, token->location);
29545 c_name = "parallel";
29546 if (!first)
29548 clause_not_first:
29549 error_at (token->location, "%qs must be the first clause of %qs",
29550 c_name, where);
29551 clauses = prev;
29553 break;
29554 case PRAGMA_OMP_CLAUSE_FOR:
29555 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29556 clauses, token->location);
29557 c_name = "for";
29558 if (!first)
29559 goto clause_not_first;
29560 break;
29561 case PRAGMA_OMP_CLAUSE_SECTIONS:
29562 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29563 clauses, token->location);
29564 c_name = "sections";
29565 if (!first)
29566 goto clause_not_first;
29567 break;
29568 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29569 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29570 clauses, token->location);
29571 c_name = "taskgroup";
29572 if (!first)
29573 goto clause_not_first;
29574 break;
29575 case PRAGMA_OMP_CLAUSE_TO:
29576 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29577 clauses);
29578 c_name = "to";
29579 break;
29580 case PRAGMA_OMP_CLAUSE_FROM:
29581 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29582 clauses);
29583 c_name = "from";
29584 break;
29585 case PRAGMA_OMP_CLAUSE_UNIFORM:
29586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29587 clauses);
29588 c_name = "uniform";
29589 break;
29590 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29591 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29592 token->location);
29593 c_name = "num_teams";
29594 break;
29595 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29596 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29597 token->location);
29598 c_name = "thread_limit";
29599 break;
29600 case PRAGMA_OMP_CLAUSE_ALIGNED:
29601 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29602 c_name = "aligned";
29603 break;
29604 case PRAGMA_OMP_CLAUSE_LINEAR:
29605 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29606 cilk_simd_fn = true;
29607 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29608 c_name = "linear";
29609 break;
29610 case PRAGMA_OMP_CLAUSE_DEPEND:
29611 clauses = cp_parser_omp_clause_depend (parser, clauses);
29612 c_name = "depend";
29613 break;
29614 case PRAGMA_OMP_CLAUSE_MAP:
29615 clauses = cp_parser_omp_clause_map (parser, clauses);
29616 c_name = "map";
29617 break;
29618 case PRAGMA_OMP_CLAUSE_DEVICE:
29619 clauses = cp_parser_omp_clause_device (parser, clauses,
29620 token->location);
29621 c_name = "device";
29622 break;
29623 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29624 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29625 token->location);
29626 c_name = "dist_schedule";
29627 break;
29628 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29629 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29630 token->location);
29631 c_name = "proc_bind";
29632 break;
29633 case PRAGMA_OMP_CLAUSE_SAFELEN:
29634 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29635 token->location);
29636 c_name = "safelen";
29637 break;
29638 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29639 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29640 token->location);
29641 c_name = "simdlen";
29642 break;
29643 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29644 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29645 c_name = "simdlen";
29646 break;
29647 default:
29648 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29649 goto saw_error;
29652 first = false;
29654 if (((mask >> c_kind) & 1) == 0)
29656 /* Remove the invalid clause(s) from the list to avoid
29657 confusing the rest of the compiler. */
29658 clauses = prev;
29659 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29662 saw_error:
29663 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29664 no reason to skip to the end. */
29665 if (!(flag_cilkplus && pragma_tok == NULL))
29666 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29667 if (finish_p)
29668 return finish_omp_clauses (clauses);
29669 return clauses;
29672 /* OpenMP 2.5:
29673 structured-block:
29674 statement
29676 In practice, we're also interested in adding the statement to an
29677 outer node. So it is convenient if we work around the fact that
29678 cp_parser_statement calls add_stmt. */
29680 static unsigned
29681 cp_parser_begin_omp_structured_block (cp_parser *parser)
29683 unsigned save = parser->in_statement;
29685 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29686 This preserves the "not within loop or switch" style error messages
29687 for nonsense cases like
29688 void foo() {
29689 #pragma omp single
29690 break;
29693 if (parser->in_statement)
29694 parser->in_statement = IN_OMP_BLOCK;
29696 return save;
29699 static void
29700 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29702 parser->in_statement = save;
29705 static tree
29706 cp_parser_omp_structured_block (cp_parser *parser)
29708 tree stmt = begin_omp_structured_block ();
29709 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29711 cp_parser_statement (parser, NULL_TREE, false, NULL);
29713 cp_parser_end_omp_structured_block (parser, save);
29714 return finish_omp_structured_block (stmt);
29717 /* OpenMP 2.5:
29718 # pragma omp atomic new-line
29719 expression-stmt
29721 expression-stmt:
29722 x binop= expr | x++ | ++x | x-- | --x
29723 binop:
29724 +, *, -, /, &, ^, |, <<, >>
29726 where x is an lvalue expression with scalar type.
29728 OpenMP 3.1:
29729 # pragma omp atomic new-line
29730 update-stmt
29732 # pragma omp atomic read new-line
29733 read-stmt
29735 # pragma omp atomic write new-line
29736 write-stmt
29738 # pragma omp atomic update new-line
29739 update-stmt
29741 # pragma omp atomic capture new-line
29742 capture-stmt
29744 # pragma omp atomic capture new-line
29745 capture-block
29747 read-stmt:
29748 v = x
29749 write-stmt:
29750 x = expr
29751 update-stmt:
29752 expression-stmt | x = x binop expr
29753 capture-stmt:
29754 v = expression-stmt
29755 capture-block:
29756 { v = x; update-stmt; } | { update-stmt; v = x; }
29758 OpenMP 4.0:
29759 update-stmt:
29760 expression-stmt | x = x binop expr | x = expr binop x
29761 capture-stmt:
29762 v = update-stmt
29763 capture-block:
29764 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29766 where x and v are lvalue expressions with scalar type. */
29768 static void
29769 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29771 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29772 tree rhs1 = NULL_TREE, orig_lhs;
29773 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29774 bool structured_block = false;
29775 bool seq_cst = false;
29777 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29779 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29780 const char *p = IDENTIFIER_POINTER (id);
29782 if (!strcmp (p, "seq_cst"))
29784 seq_cst = true;
29785 cp_lexer_consume_token (parser->lexer);
29786 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29787 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29788 cp_lexer_consume_token (parser->lexer);
29791 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29793 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29794 const char *p = IDENTIFIER_POINTER (id);
29796 if (!strcmp (p, "read"))
29797 code = OMP_ATOMIC_READ;
29798 else if (!strcmp (p, "write"))
29799 code = NOP_EXPR;
29800 else if (!strcmp (p, "update"))
29801 code = OMP_ATOMIC;
29802 else if (!strcmp (p, "capture"))
29803 code = OMP_ATOMIC_CAPTURE_NEW;
29804 else
29805 p = NULL;
29806 if (p)
29807 cp_lexer_consume_token (parser->lexer);
29809 if (!seq_cst)
29811 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29812 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29813 cp_lexer_consume_token (parser->lexer);
29815 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29817 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29818 const char *p = IDENTIFIER_POINTER (id);
29820 if (!strcmp (p, "seq_cst"))
29822 seq_cst = true;
29823 cp_lexer_consume_token (parser->lexer);
29827 cp_parser_require_pragma_eol (parser, pragma_tok);
29829 switch (code)
29831 case OMP_ATOMIC_READ:
29832 case NOP_EXPR: /* atomic write */
29833 v = cp_parser_unary_expression (parser);
29834 if (v == error_mark_node)
29835 goto saw_error;
29836 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29837 goto saw_error;
29838 if (code == NOP_EXPR)
29839 lhs = cp_parser_expression (parser);
29840 else
29841 lhs = cp_parser_unary_expression (parser);
29842 if (lhs == error_mark_node)
29843 goto saw_error;
29844 if (code == NOP_EXPR)
29846 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29847 opcode. */
29848 code = OMP_ATOMIC;
29849 rhs = lhs;
29850 lhs = v;
29851 v = NULL_TREE;
29853 goto done;
29854 case OMP_ATOMIC_CAPTURE_NEW:
29855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29857 cp_lexer_consume_token (parser->lexer);
29858 structured_block = true;
29860 else
29862 v = cp_parser_unary_expression (parser);
29863 if (v == error_mark_node)
29864 goto saw_error;
29865 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29866 goto saw_error;
29868 default:
29869 break;
29872 restart:
29873 lhs = cp_parser_unary_expression (parser);
29874 orig_lhs = lhs;
29875 switch (TREE_CODE (lhs))
29877 case ERROR_MARK:
29878 goto saw_error;
29880 case POSTINCREMENT_EXPR:
29881 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29882 code = OMP_ATOMIC_CAPTURE_OLD;
29883 /* FALLTHROUGH */
29884 case PREINCREMENT_EXPR:
29885 lhs = TREE_OPERAND (lhs, 0);
29886 opcode = PLUS_EXPR;
29887 rhs = integer_one_node;
29888 break;
29890 case POSTDECREMENT_EXPR:
29891 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29892 code = OMP_ATOMIC_CAPTURE_OLD;
29893 /* FALLTHROUGH */
29894 case PREDECREMENT_EXPR:
29895 lhs = TREE_OPERAND (lhs, 0);
29896 opcode = MINUS_EXPR;
29897 rhs = integer_one_node;
29898 break;
29900 case COMPOUND_EXPR:
29901 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29902 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29903 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29904 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29905 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29906 (TREE_OPERAND (lhs, 1), 0), 0)))
29907 == BOOLEAN_TYPE)
29908 /* Undo effects of boolean_increment for post {in,de}crement. */
29909 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29910 /* FALLTHRU */
29911 case MODIFY_EXPR:
29912 if (TREE_CODE (lhs) == MODIFY_EXPR
29913 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29915 /* Undo effects of boolean_increment. */
29916 if (integer_onep (TREE_OPERAND (lhs, 1)))
29918 /* This is pre or post increment. */
29919 rhs = TREE_OPERAND (lhs, 1);
29920 lhs = TREE_OPERAND (lhs, 0);
29921 opcode = NOP_EXPR;
29922 if (code == OMP_ATOMIC_CAPTURE_NEW
29923 && !structured_block
29924 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29925 code = OMP_ATOMIC_CAPTURE_OLD;
29926 break;
29929 /* FALLTHRU */
29930 default:
29931 switch (cp_lexer_peek_token (parser->lexer)->type)
29933 case CPP_MULT_EQ:
29934 opcode = MULT_EXPR;
29935 break;
29936 case CPP_DIV_EQ:
29937 opcode = TRUNC_DIV_EXPR;
29938 break;
29939 case CPP_PLUS_EQ:
29940 opcode = PLUS_EXPR;
29941 break;
29942 case CPP_MINUS_EQ:
29943 opcode = MINUS_EXPR;
29944 break;
29945 case CPP_LSHIFT_EQ:
29946 opcode = LSHIFT_EXPR;
29947 break;
29948 case CPP_RSHIFT_EQ:
29949 opcode = RSHIFT_EXPR;
29950 break;
29951 case CPP_AND_EQ:
29952 opcode = BIT_AND_EXPR;
29953 break;
29954 case CPP_OR_EQ:
29955 opcode = BIT_IOR_EXPR;
29956 break;
29957 case CPP_XOR_EQ:
29958 opcode = BIT_XOR_EXPR;
29959 break;
29960 case CPP_EQ:
29961 enum cp_parser_prec oprec;
29962 cp_token *token;
29963 cp_lexer_consume_token (parser->lexer);
29964 cp_parser_parse_tentatively (parser);
29965 rhs1 = cp_parser_simple_cast_expression (parser);
29966 if (rhs1 == error_mark_node)
29968 cp_parser_abort_tentative_parse (parser);
29969 cp_parser_simple_cast_expression (parser);
29970 goto saw_error;
29972 token = cp_lexer_peek_token (parser->lexer);
29973 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29975 cp_parser_abort_tentative_parse (parser);
29976 cp_parser_parse_tentatively (parser);
29977 rhs = cp_parser_binary_expression (parser, false, true,
29978 PREC_NOT_OPERATOR, NULL);
29979 if (rhs == error_mark_node)
29981 cp_parser_abort_tentative_parse (parser);
29982 cp_parser_binary_expression (parser, false, true,
29983 PREC_NOT_OPERATOR, NULL);
29984 goto saw_error;
29986 switch (TREE_CODE (rhs))
29988 case MULT_EXPR:
29989 case TRUNC_DIV_EXPR:
29990 case RDIV_EXPR:
29991 case PLUS_EXPR:
29992 case MINUS_EXPR:
29993 case LSHIFT_EXPR:
29994 case RSHIFT_EXPR:
29995 case BIT_AND_EXPR:
29996 case BIT_IOR_EXPR:
29997 case BIT_XOR_EXPR:
29998 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
30000 if (cp_parser_parse_definitely (parser))
30002 opcode = TREE_CODE (rhs);
30003 rhs1 = TREE_OPERAND (rhs, 0);
30004 rhs = TREE_OPERAND (rhs, 1);
30005 goto stmt_done;
30007 else
30008 goto saw_error;
30010 break;
30011 default:
30012 break;
30014 cp_parser_abort_tentative_parse (parser);
30015 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
30017 rhs = cp_parser_expression (parser);
30018 if (rhs == error_mark_node)
30019 goto saw_error;
30020 opcode = NOP_EXPR;
30021 rhs1 = NULL_TREE;
30022 goto stmt_done;
30024 cp_parser_error (parser,
30025 "invalid form of %<#pragma omp atomic%>");
30026 goto saw_error;
30028 if (!cp_parser_parse_definitely (parser))
30029 goto saw_error;
30030 switch (token->type)
30032 case CPP_SEMICOLON:
30033 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30035 code = OMP_ATOMIC_CAPTURE_OLD;
30036 v = lhs;
30037 lhs = NULL_TREE;
30038 lhs1 = rhs1;
30039 rhs1 = NULL_TREE;
30040 cp_lexer_consume_token (parser->lexer);
30041 goto restart;
30043 else if (structured_block)
30045 opcode = NOP_EXPR;
30046 rhs = rhs1;
30047 rhs1 = NULL_TREE;
30048 goto stmt_done;
30050 cp_parser_error (parser,
30051 "invalid form of %<#pragma omp atomic%>");
30052 goto saw_error;
30053 case CPP_MULT:
30054 opcode = MULT_EXPR;
30055 break;
30056 case CPP_DIV:
30057 opcode = TRUNC_DIV_EXPR;
30058 break;
30059 case CPP_PLUS:
30060 opcode = PLUS_EXPR;
30061 break;
30062 case CPP_MINUS:
30063 opcode = MINUS_EXPR;
30064 break;
30065 case CPP_LSHIFT:
30066 opcode = LSHIFT_EXPR;
30067 break;
30068 case CPP_RSHIFT:
30069 opcode = RSHIFT_EXPR;
30070 break;
30071 case CPP_AND:
30072 opcode = BIT_AND_EXPR;
30073 break;
30074 case CPP_OR:
30075 opcode = BIT_IOR_EXPR;
30076 break;
30077 case CPP_XOR:
30078 opcode = BIT_XOR_EXPR;
30079 break;
30080 default:
30081 cp_parser_error (parser,
30082 "invalid operator for %<#pragma omp atomic%>");
30083 goto saw_error;
30085 oprec = TOKEN_PRECEDENCE (token);
30086 gcc_assert (oprec != PREC_NOT_OPERATOR);
30087 if (commutative_tree_code (opcode))
30088 oprec = (enum cp_parser_prec) (oprec - 1);
30089 cp_lexer_consume_token (parser->lexer);
30090 rhs = cp_parser_binary_expression (parser, false, false,
30091 oprec, NULL);
30092 if (rhs == error_mark_node)
30093 goto saw_error;
30094 goto stmt_done;
30095 /* FALLTHROUGH */
30096 default:
30097 cp_parser_error (parser,
30098 "invalid operator for %<#pragma omp atomic%>");
30099 goto saw_error;
30101 cp_lexer_consume_token (parser->lexer);
30103 rhs = cp_parser_expression (parser);
30104 if (rhs == error_mark_node)
30105 goto saw_error;
30106 break;
30108 stmt_done:
30109 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30111 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30112 goto saw_error;
30113 v = cp_parser_unary_expression (parser);
30114 if (v == error_mark_node)
30115 goto saw_error;
30116 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30117 goto saw_error;
30118 lhs1 = cp_parser_unary_expression (parser);
30119 if (lhs1 == error_mark_node)
30120 goto saw_error;
30122 if (structured_block)
30124 cp_parser_consume_semicolon_at_end_of_statement (parser);
30125 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30127 done:
30128 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30129 if (!structured_block)
30130 cp_parser_consume_semicolon_at_end_of_statement (parser);
30131 return;
30133 saw_error:
30134 cp_parser_skip_to_end_of_block_or_statement (parser);
30135 if (structured_block)
30137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30138 cp_lexer_consume_token (parser->lexer);
30139 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30141 cp_parser_skip_to_end_of_block_or_statement (parser);
30142 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30143 cp_lexer_consume_token (parser->lexer);
30149 /* OpenMP 2.5:
30150 # pragma omp barrier new-line */
30152 static void
30153 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30155 cp_parser_require_pragma_eol (parser, pragma_tok);
30156 finish_omp_barrier ();
30159 /* OpenMP 2.5:
30160 # pragma omp critical [(name)] new-line
30161 structured-block */
30163 static tree
30164 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30166 tree stmt, name = NULL;
30168 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30170 cp_lexer_consume_token (parser->lexer);
30172 name = cp_parser_identifier (parser);
30174 if (name == error_mark_node
30175 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30176 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30177 /*or_comma=*/false,
30178 /*consume_paren=*/true);
30179 if (name == error_mark_node)
30180 name = NULL;
30182 cp_parser_require_pragma_eol (parser, pragma_tok);
30184 stmt = cp_parser_omp_structured_block (parser);
30185 return c_finish_omp_critical (input_location, stmt, name);
30188 /* OpenMP 2.5:
30189 # pragma omp flush flush-vars[opt] new-line
30191 flush-vars:
30192 ( variable-list ) */
30194 static void
30195 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30198 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30199 cp_parser_require_pragma_eol (parser, pragma_tok);
30201 finish_omp_flush ();
30204 /* Helper function, to parse omp for increment expression. */
30206 static tree
30207 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30209 tree cond = cp_parser_binary_expression (parser, false, true,
30210 PREC_NOT_OPERATOR, NULL);
30211 if (cond == error_mark_node
30212 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30214 cp_parser_skip_to_end_of_statement (parser);
30215 return error_mark_node;
30218 switch (TREE_CODE (cond))
30220 case GT_EXPR:
30221 case GE_EXPR:
30222 case LT_EXPR:
30223 case LE_EXPR:
30224 break;
30225 case NE_EXPR:
30226 if (code == CILK_SIMD || code == CILK_FOR)
30227 break;
30228 /* Fall through: OpenMP disallows NE_EXPR. */
30229 default:
30230 return error_mark_node;
30233 /* If decl is an iterator, preserve LHS and RHS of the relational
30234 expr until finish_omp_for. */
30235 if (decl
30236 && (type_dependent_expression_p (decl)
30237 || CLASS_TYPE_P (TREE_TYPE (decl))))
30238 return cond;
30240 return build_x_binary_op (input_location, TREE_CODE (cond),
30241 TREE_OPERAND (cond, 0), ERROR_MARK,
30242 TREE_OPERAND (cond, 1), ERROR_MARK,
30243 /*overload=*/NULL, tf_warning_or_error);
30246 /* Helper function, to parse omp for increment expression. */
30248 static tree
30249 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30251 cp_token *token = cp_lexer_peek_token (parser->lexer);
30252 enum tree_code op;
30253 tree lhs, rhs;
30254 cp_id_kind idk;
30255 bool decl_first;
30257 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30259 op = (token->type == CPP_PLUS_PLUS
30260 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30261 cp_lexer_consume_token (parser->lexer);
30262 lhs = cp_parser_simple_cast_expression (parser);
30263 if (lhs != decl)
30264 return error_mark_node;
30265 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30268 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30269 if (lhs != decl)
30270 return error_mark_node;
30272 token = cp_lexer_peek_token (parser->lexer);
30273 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30275 op = (token->type == CPP_PLUS_PLUS
30276 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30277 cp_lexer_consume_token (parser->lexer);
30278 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30281 op = cp_parser_assignment_operator_opt (parser);
30282 if (op == ERROR_MARK)
30283 return error_mark_node;
30285 if (op != NOP_EXPR)
30287 rhs = cp_parser_assignment_expression (parser);
30288 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30289 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30292 lhs = cp_parser_binary_expression (parser, false, false,
30293 PREC_ADDITIVE_EXPRESSION, NULL);
30294 token = cp_lexer_peek_token (parser->lexer);
30295 decl_first = lhs == decl;
30296 if (decl_first)
30297 lhs = NULL_TREE;
30298 if (token->type != CPP_PLUS
30299 && token->type != CPP_MINUS)
30300 return error_mark_node;
30304 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30305 cp_lexer_consume_token (parser->lexer);
30306 rhs = cp_parser_binary_expression (parser, false, false,
30307 PREC_ADDITIVE_EXPRESSION, NULL);
30308 token = cp_lexer_peek_token (parser->lexer);
30309 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30311 if (lhs == NULL_TREE)
30313 if (op == PLUS_EXPR)
30314 lhs = rhs;
30315 else
30316 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30317 tf_warning_or_error);
30318 if (op != PLUS_EXPR && CONSTANT_CLASS_P (rhs))
30319 lhs = fold (lhs);
30321 else
30322 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30323 ERROR_MARK, NULL, tf_warning_or_error);
30326 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30328 if (!decl_first)
30330 if (rhs != decl || op == MINUS_EXPR)
30331 return error_mark_node;
30332 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30334 else
30335 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30337 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30340 /* Parse the initialization statement of either an OpenMP for loop or
30341 a Cilk Plus for loop.
30343 Return true if the resulting construct should have an
30344 OMP_CLAUSE_PRIVATE added to it. */
30346 static bool
30347 cp_parser_omp_for_loop_init (cp_parser *parser,
30348 enum tree_code code,
30349 tree &this_pre_body,
30350 vec<tree, va_gc> *for_block,
30351 tree &init,
30352 tree &decl,
30353 tree &real_decl)
30355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30356 return false;
30358 bool add_private_clause = false;
30360 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30362 init-expr:
30363 var = lb
30364 integer-type var = lb
30365 random-access-iterator-type var = lb
30366 pointer-type var = lb
30368 cp_decl_specifier_seq type_specifiers;
30370 /* First, try to parse as an initialized declaration. See
30371 cp_parser_condition, from whence the bulk of this is copied. */
30373 cp_parser_parse_tentatively (parser);
30374 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30375 /*is_trailing_return=*/false,
30376 &type_specifiers);
30377 if (cp_parser_parse_definitely (parser))
30379 /* If parsing a type specifier seq succeeded, then this
30380 MUST be a initialized declaration. */
30381 tree asm_specification, attributes;
30382 cp_declarator *declarator;
30384 declarator = cp_parser_declarator (parser,
30385 CP_PARSER_DECLARATOR_NAMED,
30386 /*ctor_dtor_or_conv_p=*/NULL,
30387 /*parenthesized_p=*/NULL,
30388 /*member_p=*/false,
30389 /*friend_p=*/false);
30390 attributes = cp_parser_attributes_opt (parser);
30391 asm_specification = cp_parser_asm_specification_opt (parser);
30393 if (declarator == cp_error_declarator)
30394 cp_parser_skip_to_end_of_statement (parser);
30396 else
30398 tree pushed_scope, auto_node;
30400 decl = start_decl (declarator, &type_specifiers,
30401 SD_INITIALIZED, attributes,
30402 /*prefix_attributes=*/NULL_TREE,
30403 &pushed_scope);
30405 auto_node = type_uses_auto (TREE_TYPE (decl));
30406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30408 if (cp_lexer_next_token_is (parser->lexer,
30409 CPP_OPEN_PAREN))
30411 if (code != CILK_SIMD && code != CILK_FOR)
30412 error ("parenthesized initialization is not allowed in "
30413 "OpenMP %<for%> loop");
30414 else
30415 error ("parenthesized initialization is "
30416 "not allowed in for-loop");
30418 else
30419 /* Trigger an error. */
30420 cp_parser_require (parser, CPP_EQ, RT_EQ);
30422 init = error_mark_node;
30423 cp_parser_skip_to_end_of_statement (parser);
30425 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30426 || type_dependent_expression_p (decl)
30427 || auto_node)
30429 bool is_direct_init, is_non_constant_init;
30431 init = cp_parser_initializer (parser,
30432 &is_direct_init,
30433 &is_non_constant_init);
30435 if (auto_node)
30437 TREE_TYPE (decl)
30438 = do_auto_deduction (TREE_TYPE (decl), init,
30439 auto_node);
30441 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30442 && !type_dependent_expression_p (decl))
30443 goto non_class;
30446 cp_finish_decl (decl, init, !is_non_constant_init,
30447 asm_specification,
30448 LOOKUP_ONLYCONVERTING);
30449 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30451 vec_safe_push (for_block, this_pre_body);
30452 init = NULL_TREE;
30454 else
30455 init = pop_stmt_list (this_pre_body);
30456 this_pre_body = NULL_TREE;
30458 else
30460 /* Consume '='. */
30461 cp_lexer_consume_token (parser->lexer);
30462 init = cp_parser_assignment_expression (parser);
30464 non_class:
30465 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30466 init = error_mark_node;
30467 else
30468 cp_finish_decl (decl, NULL_TREE,
30469 /*init_const_expr_p=*/false,
30470 asm_specification,
30471 LOOKUP_ONLYCONVERTING);
30474 if (pushed_scope)
30475 pop_scope (pushed_scope);
30478 else
30480 cp_id_kind idk;
30481 /* If parsing a type specifier sequence failed, then
30482 this MUST be a simple expression. */
30483 if (code == CILK_FOR)
30484 error ("%<_Cilk_for%> allows expression instead of declaration only "
30485 "in C, not in C++");
30486 cp_parser_parse_tentatively (parser);
30487 decl = cp_parser_primary_expression (parser, false, false,
30488 false, &idk);
30489 if (!cp_parser_error_occurred (parser)
30490 && decl
30491 && DECL_P (decl)
30492 && CLASS_TYPE_P (TREE_TYPE (decl)))
30494 tree rhs;
30496 cp_parser_parse_definitely (parser);
30497 cp_parser_require (parser, CPP_EQ, RT_EQ);
30498 rhs = cp_parser_assignment_expression (parser);
30499 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30500 decl, NOP_EXPR,
30501 rhs,
30502 tf_warning_or_error));
30503 add_private_clause = true;
30505 else
30507 decl = NULL;
30508 cp_parser_abort_tentative_parse (parser);
30509 init = cp_parser_expression (parser);
30510 if (init)
30512 if (TREE_CODE (init) == MODIFY_EXPR
30513 || TREE_CODE (init) == MODOP_EXPR)
30514 real_decl = TREE_OPERAND (init, 0);
30518 return add_private_clause;
30521 /* Parse the restricted form of the for statement allowed by OpenMP. */
30523 static tree
30524 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30525 tree *cclauses)
30527 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30528 tree real_decl, initv, condv, incrv, declv;
30529 tree this_pre_body, cl;
30530 location_t loc_first;
30531 bool collapse_err = false;
30532 int i, collapse = 1, nbraces = 0;
30533 vec<tree, va_gc> *for_block = make_tree_vector ();
30535 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30536 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30537 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30539 gcc_assert (collapse >= 1);
30541 declv = make_tree_vec (collapse);
30542 initv = make_tree_vec (collapse);
30543 condv = make_tree_vec (collapse);
30544 incrv = make_tree_vec (collapse);
30546 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30548 for (i = 0; i < collapse; i++)
30550 int bracecount = 0;
30551 bool add_private_clause = false;
30552 location_t loc;
30554 if (code != CILK_FOR
30555 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30557 cp_parser_error (parser, "for statement expected");
30558 return NULL;
30560 if (code == CILK_FOR
30561 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30563 cp_parser_error (parser, "_Cilk_for statement expected");
30564 return NULL;
30566 loc = cp_lexer_consume_token (parser->lexer)->location;
30568 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30569 return NULL;
30571 init = decl = real_decl = NULL;
30572 this_pre_body = push_stmt_list ();
30574 add_private_clause
30575 |= cp_parser_omp_for_loop_init (parser, code,
30576 this_pre_body, for_block,
30577 init, decl, real_decl);
30579 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30580 if (this_pre_body)
30582 this_pre_body = pop_stmt_list (this_pre_body);
30583 if (pre_body)
30585 tree t = pre_body;
30586 pre_body = push_stmt_list ();
30587 add_stmt (t);
30588 add_stmt (this_pre_body);
30589 pre_body = pop_stmt_list (pre_body);
30591 else
30592 pre_body = this_pre_body;
30595 if (decl)
30596 real_decl = decl;
30597 if (cclauses != NULL
30598 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30599 && real_decl != NULL_TREE)
30601 tree *c;
30602 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30603 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30604 && OMP_CLAUSE_DECL (*c) == real_decl)
30606 error_at (loc, "iteration variable %qD"
30607 " should not be firstprivate", real_decl);
30608 *c = OMP_CLAUSE_CHAIN (*c);
30610 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30611 && OMP_CLAUSE_DECL (*c) == real_decl)
30613 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
30614 tree l = *c;
30615 *c = OMP_CLAUSE_CHAIN (*c);
30616 if (code == OMP_SIMD)
30618 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30619 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30621 else
30623 OMP_CLAUSE_CHAIN (l) = clauses;
30624 clauses = l;
30626 add_private_clause = false;
30628 else
30630 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30631 && OMP_CLAUSE_DECL (*c) == real_decl)
30632 add_private_clause = false;
30633 c = &OMP_CLAUSE_CHAIN (*c);
30637 if (add_private_clause)
30639 tree c;
30640 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30642 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30643 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30644 && OMP_CLAUSE_DECL (c) == decl)
30645 break;
30646 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30647 && OMP_CLAUSE_DECL (c) == decl)
30648 error_at (loc, "iteration variable %qD "
30649 "should not be firstprivate",
30650 decl);
30651 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30652 && OMP_CLAUSE_DECL (c) == decl)
30653 error_at (loc, "iteration variable %qD should not be reduction",
30654 decl);
30656 if (c == NULL)
30658 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30659 OMP_CLAUSE_DECL (c) = decl;
30660 c = finish_omp_clauses (c);
30661 if (c)
30663 OMP_CLAUSE_CHAIN (c) = clauses;
30664 clauses = c;
30669 cond = NULL;
30670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30671 cond = cp_parser_omp_for_cond (parser, decl, code);
30672 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30674 incr = NULL;
30675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30677 /* If decl is an iterator, preserve the operator on decl
30678 until finish_omp_for. */
30679 if (real_decl
30680 && ((processing_template_decl
30681 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30682 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30683 incr = cp_parser_omp_for_incr (parser, real_decl);
30684 else
30685 incr = cp_parser_expression (parser);
30686 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30687 SET_EXPR_LOCATION (incr, input_location);
30690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30692 /*or_comma=*/false,
30693 /*consume_paren=*/true);
30695 TREE_VEC_ELT (declv, i) = decl;
30696 TREE_VEC_ELT (initv, i) = init;
30697 TREE_VEC_ELT (condv, i) = cond;
30698 TREE_VEC_ELT (incrv, i) = incr;
30700 if (i == collapse - 1)
30701 break;
30703 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30704 in between the collapsed for loops to be still considered perfectly
30705 nested. Hopefully the final version clarifies this.
30706 For now handle (multiple) {'s and empty statements. */
30707 cp_parser_parse_tentatively (parser);
30710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30711 break;
30712 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30714 cp_lexer_consume_token (parser->lexer);
30715 bracecount++;
30717 else if (bracecount
30718 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30719 cp_lexer_consume_token (parser->lexer);
30720 else
30722 loc = cp_lexer_peek_token (parser->lexer)->location;
30723 error_at (loc, "not enough collapsed for loops");
30724 collapse_err = true;
30725 cp_parser_abort_tentative_parse (parser);
30726 declv = NULL_TREE;
30727 break;
30730 while (1);
30732 if (declv)
30734 cp_parser_parse_definitely (parser);
30735 nbraces += bracecount;
30739 /* Note that we saved the original contents of this flag when we entered
30740 the structured block, and so we don't need to re-save it here. */
30741 if (code == CILK_SIMD || code == CILK_FOR)
30742 parser->in_statement = IN_CILK_SIMD_FOR;
30743 else
30744 parser->in_statement = IN_OMP_FOR;
30746 /* Note that the grammar doesn't call for a structured block here,
30747 though the loop as a whole is a structured block. */
30748 body = push_stmt_list ();
30749 cp_parser_statement (parser, NULL_TREE, false, NULL);
30750 body = pop_stmt_list (body);
30752 if (declv == NULL_TREE)
30753 ret = NULL_TREE;
30754 else
30755 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30756 pre_body, clauses);
30758 while (nbraces)
30760 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30762 cp_lexer_consume_token (parser->lexer);
30763 nbraces--;
30765 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30766 cp_lexer_consume_token (parser->lexer);
30767 else
30769 if (!collapse_err)
30771 error_at (cp_lexer_peek_token (parser->lexer)->location,
30772 "collapsed loops not perfectly nested");
30774 collapse_err = true;
30775 cp_parser_statement_seq_opt (parser, NULL);
30776 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30777 break;
30781 while (!for_block->is_empty ())
30782 add_stmt (pop_stmt_list (for_block->pop ()));
30783 release_tree_vector (for_block);
30785 return ret;
30788 /* Helper function for OpenMP parsing, split clauses and call
30789 finish_omp_clauses on each of the set of clauses afterwards. */
30791 static void
30792 cp_omp_split_clauses (location_t loc, enum tree_code code,
30793 omp_clause_mask mask, tree clauses, tree *cclauses)
30795 int i;
30796 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30797 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30798 if (cclauses[i])
30799 cclauses[i] = finish_omp_clauses (cclauses[i]);
30802 /* OpenMP 4.0:
30803 #pragma omp simd simd-clause[optseq] new-line
30804 for-loop */
30806 #define OMP_SIMD_CLAUSE_MASK \
30807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30815 static tree
30816 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30817 char *p_name, omp_clause_mask mask, tree *cclauses)
30819 tree clauses, sb, ret;
30820 unsigned int save;
30821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30823 strcat (p_name, " simd");
30824 mask |= OMP_SIMD_CLAUSE_MASK;
30825 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30827 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30828 cclauses == NULL);
30829 if (cclauses)
30831 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30832 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30835 sb = begin_omp_structured_block ();
30836 save = cp_parser_begin_omp_structured_block (parser);
30838 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30840 cp_parser_end_omp_structured_block (parser, save);
30841 add_stmt (finish_omp_structured_block (sb));
30843 return ret;
30846 /* OpenMP 2.5:
30847 #pragma omp for for-clause[optseq] new-line
30848 for-loop
30850 OpenMP 4.0:
30851 #pragma omp for simd for-simd-clause[optseq] new-line
30852 for-loop */
30854 #define OMP_FOR_CLAUSE_MASK \
30855 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30864 static tree
30865 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30866 char *p_name, omp_clause_mask mask, tree *cclauses)
30868 tree clauses, sb, ret;
30869 unsigned int save;
30870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30872 strcat (p_name, " for");
30873 mask |= OMP_FOR_CLAUSE_MASK;
30874 if (cclauses)
30875 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30877 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30879 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30880 const char *p = IDENTIFIER_POINTER (id);
30882 if (strcmp (p, "simd") == 0)
30884 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30885 if (cclauses == NULL)
30886 cclauses = cclauses_buf;
30888 cp_lexer_consume_token (parser->lexer);
30889 if (!flag_openmp) /* flag_openmp_simd */
30890 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30891 cclauses);
30892 sb = begin_omp_structured_block ();
30893 save = cp_parser_begin_omp_structured_block (parser);
30894 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30895 cclauses);
30896 cp_parser_end_omp_structured_block (parser, save);
30897 tree body = finish_omp_structured_block (sb);
30898 if (ret == NULL)
30899 return ret;
30900 ret = make_node (OMP_FOR);
30901 TREE_TYPE (ret) = void_type_node;
30902 OMP_FOR_BODY (ret) = body;
30903 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30904 SET_EXPR_LOCATION (ret, loc);
30905 add_stmt (ret);
30906 return ret;
30909 if (!flag_openmp) /* flag_openmp_simd */
30911 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30912 return NULL_TREE;
30915 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30916 cclauses == NULL);
30917 if (cclauses)
30919 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30920 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30923 sb = begin_omp_structured_block ();
30924 save = cp_parser_begin_omp_structured_block (parser);
30926 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30928 cp_parser_end_omp_structured_block (parser, save);
30929 add_stmt (finish_omp_structured_block (sb));
30931 return ret;
30934 /* OpenMP 2.5:
30935 # pragma omp master new-line
30936 structured-block */
30938 static tree
30939 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30941 cp_parser_require_pragma_eol (parser, pragma_tok);
30942 return c_finish_omp_master (input_location,
30943 cp_parser_omp_structured_block (parser));
30946 /* OpenMP 2.5:
30947 # pragma omp ordered new-line
30948 structured-block */
30950 static tree
30951 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30954 cp_parser_require_pragma_eol (parser, pragma_tok);
30955 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30958 /* OpenMP 2.5:
30960 section-scope:
30961 { section-sequence }
30963 section-sequence:
30964 section-directive[opt] structured-block
30965 section-sequence section-directive structured-block */
30967 static tree
30968 cp_parser_omp_sections_scope (cp_parser *parser)
30970 tree stmt, substmt;
30971 bool error_suppress = false;
30972 cp_token *tok;
30974 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30975 return NULL_TREE;
30977 stmt = push_stmt_list ();
30979 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30981 substmt = cp_parser_omp_structured_block (parser);
30982 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30983 add_stmt (substmt);
30986 while (1)
30988 tok = cp_lexer_peek_token (parser->lexer);
30989 if (tok->type == CPP_CLOSE_BRACE)
30990 break;
30991 if (tok->type == CPP_EOF)
30992 break;
30994 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30996 cp_lexer_consume_token (parser->lexer);
30997 cp_parser_require_pragma_eol (parser, tok);
30998 error_suppress = false;
31000 else if (!error_suppress)
31002 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
31003 error_suppress = true;
31006 substmt = cp_parser_omp_structured_block (parser);
31007 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31008 add_stmt (substmt);
31010 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31012 substmt = pop_stmt_list (stmt);
31014 stmt = make_node (OMP_SECTIONS);
31015 TREE_TYPE (stmt) = void_type_node;
31016 OMP_SECTIONS_BODY (stmt) = substmt;
31018 add_stmt (stmt);
31019 return stmt;
31022 /* OpenMP 2.5:
31023 # pragma omp sections sections-clause[optseq] newline
31024 sections-scope */
31026 #define OMP_SECTIONS_CLAUSE_MASK \
31027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31033 static tree
31034 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
31035 char *p_name, omp_clause_mask mask, tree *cclauses)
31037 tree clauses, ret;
31038 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31040 strcat (p_name, " sections");
31041 mask |= OMP_SECTIONS_CLAUSE_MASK;
31042 if (cclauses)
31043 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31045 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31046 cclauses == NULL);
31047 if (cclauses)
31049 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31050 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31053 ret = cp_parser_omp_sections_scope (parser);
31054 if (ret)
31055 OMP_SECTIONS_CLAUSES (ret) = clauses;
31057 return ret;
31060 /* OpenMP 2.5:
31061 # pragma omp parallel parallel-clause[optseq] new-line
31062 structured-block
31063 # pragma omp parallel for parallel-for-clause[optseq] new-line
31064 structured-block
31065 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31066 structured-block
31068 OpenMP 4.0:
31069 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31070 structured-block */
31072 #define OMP_PARALLEL_CLAUSE_MASK \
31073 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31083 static tree
31084 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31085 char *p_name, omp_clause_mask mask, tree *cclauses)
31087 tree stmt, clauses, block;
31088 unsigned int save;
31089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31091 strcat (p_name, " parallel");
31092 mask |= OMP_PARALLEL_CLAUSE_MASK;
31094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31096 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31097 if (cclauses == NULL)
31098 cclauses = cclauses_buf;
31100 cp_lexer_consume_token (parser->lexer);
31101 if (!flag_openmp) /* flag_openmp_simd */
31102 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31103 block = begin_omp_parallel ();
31104 save = cp_parser_begin_omp_structured_block (parser);
31105 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31106 cp_parser_end_omp_structured_block (parser, save);
31107 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31108 block);
31109 if (ret == NULL_TREE)
31110 return ret;
31111 OMP_PARALLEL_COMBINED (stmt) = 1;
31112 return stmt;
31114 else if (cclauses)
31116 error_at (loc, "expected %<for%> after %qs", p_name);
31117 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31118 return NULL_TREE;
31120 else if (!flag_openmp) /* flag_openmp_simd */
31122 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31123 return NULL_TREE;
31125 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31127 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31128 const char *p = IDENTIFIER_POINTER (id);
31129 if (strcmp (p, "sections") == 0)
31131 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31132 cclauses = cclauses_buf;
31134 cp_lexer_consume_token (parser->lexer);
31135 block = begin_omp_parallel ();
31136 save = cp_parser_begin_omp_structured_block (parser);
31137 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31138 cp_parser_end_omp_structured_block (parser, save);
31139 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31140 block);
31141 OMP_PARALLEL_COMBINED (stmt) = 1;
31142 return stmt;
31146 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31148 block = begin_omp_parallel ();
31149 save = cp_parser_begin_omp_structured_block (parser);
31150 cp_parser_statement (parser, NULL_TREE, false, NULL);
31151 cp_parser_end_omp_structured_block (parser, save);
31152 stmt = finish_omp_parallel (clauses, block);
31153 return stmt;
31156 /* OpenMP 2.5:
31157 # pragma omp single single-clause[optseq] new-line
31158 structured-block */
31160 #define OMP_SINGLE_CLAUSE_MASK \
31161 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31166 static tree
31167 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31169 tree stmt = make_node (OMP_SINGLE);
31170 TREE_TYPE (stmt) = void_type_node;
31172 OMP_SINGLE_CLAUSES (stmt)
31173 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31174 "#pragma omp single", pragma_tok);
31175 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31177 return add_stmt (stmt);
31180 /* OpenMP 3.0:
31181 # pragma omp task task-clause[optseq] new-line
31182 structured-block */
31184 #define OMP_TASK_CLAUSE_MASK \
31185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31195 static tree
31196 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31198 tree clauses, block;
31199 unsigned int save;
31201 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31202 "#pragma omp task", pragma_tok);
31203 block = begin_omp_task ();
31204 save = cp_parser_begin_omp_structured_block (parser);
31205 cp_parser_statement (parser, NULL_TREE, false, NULL);
31206 cp_parser_end_omp_structured_block (parser, save);
31207 return finish_omp_task (clauses, block);
31210 /* OpenMP 3.0:
31211 # pragma omp taskwait new-line */
31213 static void
31214 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31216 cp_parser_require_pragma_eol (parser, pragma_tok);
31217 finish_omp_taskwait ();
31220 /* OpenMP 3.1:
31221 # pragma omp taskyield new-line */
31223 static void
31224 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31226 cp_parser_require_pragma_eol (parser, pragma_tok);
31227 finish_omp_taskyield ();
31230 /* OpenMP 4.0:
31231 # pragma omp taskgroup new-line
31232 structured-block */
31234 static tree
31235 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31237 cp_parser_require_pragma_eol (parser, pragma_tok);
31238 return c_finish_omp_taskgroup (input_location,
31239 cp_parser_omp_structured_block (parser));
31243 /* OpenMP 2.5:
31244 # pragma omp threadprivate (variable-list) */
31246 static void
31247 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31249 tree vars;
31251 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31252 cp_parser_require_pragma_eol (parser, pragma_tok);
31254 finish_omp_threadprivate (vars);
31257 /* OpenMP 4.0:
31258 # pragma omp cancel cancel-clause[optseq] new-line */
31260 #define OMP_CANCEL_CLAUSE_MASK \
31261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31267 static void
31268 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31270 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31271 "#pragma omp cancel", pragma_tok);
31272 finish_omp_cancel (clauses);
31275 /* OpenMP 4.0:
31276 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31278 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31284 static void
31285 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31287 tree clauses;
31288 bool point_seen = false;
31290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31293 const char *p = IDENTIFIER_POINTER (id);
31295 if (strcmp (p, "point") == 0)
31297 cp_lexer_consume_token (parser->lexer);
31298 point_seen = true;
31301 if (!point_seen)
31303 cp_parser_error (parser, "expected %<point%>");
31304 cp_parser_require_pragma_eol (parser, pragma_tok);
31305 return;
31308 clauses = cp_parser_omp_all_clauses (parser,
31309 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31310 "#pragma omp cancellation point",
31311 pragma_tok);
31312 finish_omp_cancellation_point (clauses);
31315 /* OpenMP 4.0:
31316 #pragma omp distribute distribute-clause[optseq] new-line
31317 for-loop */
31319 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31325 static tree
31326 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31327 char *p_name, omp_clause_mask mask, tree *cclauses)
31329 tree clauses, sb, ret;
31330 unsigned int save;
31331 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31333 strcat (p_name, " distribute");
31334 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31339 const char *p = IDENTIFIER_POINTER (id);
31340 bool simd = false;
31341 bool parallel = false;
31343 if (strcmp (p, "simd") == 0)
31344 simd = true;
31345 else
31346 parallel = strcmp (p, "parallel") == 0;
31347 if (parallel || simd)
31349 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31350 if (cclauses == NULL)
31351 cclauses = cclauses_buf;
31352 cp_lexer_consume_token (parser->lexer);
31353 if (!flag_openmp) /* flag_openmp_simd */
31355 if (simd)
31356 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31357 cclauses);
31358 else
31359 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31360 cclauses);
31362 sb = begin_omp_structured_block ();
31363 save = cp_parser_begin_omp_structured_block (parser);
31364 if (simd)
31365 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31366 cclauses);
31367 else
31368 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31369 cclauses);
31370 cp_parser_end_omp_structured_block (parser, save);
31371 tree body = finish_omp_structured_block (sb);
31372 if (ret == NULL)
31373 return ret;
31374 ret = make_node (OMP_DISTRIBUTE);
31375 TREE_TYPE (ret) = void_type_node;
31376 OMP_FOR_BODY (ret) = body;
31377 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31378 SET_EXPR_LOCATION (ret, loc);
31379 add_stmt (ret);
31380 return ret;
31383 if (!flag_openmp) /* flag_openmp_simd */
31385 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31386 return NULL_TREE;
31389 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31390 cclauses == NULL);
31391 if (cclauses)
31393 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31394 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31397 sb = begin_omp_structured_block ();
31398 save = cp_parser_begin_omp_structured_block (parser);
31400 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31402 cp_parser_end_omp_structured_block (parser, save);
31403 add_stmt (finish_omp_structured_block (sb));
31405 return ret;
31408 /* OpenMP 4.0:
31409 # pragma omp teams teams-clause[optseq] new-line
31410 structured-block */
31412 #define OMP_TEAMS_CLAUSE_MASK \
31413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31421 static tree
31422 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31423 char *p_name, omp_clause_mask mask, tree *cclauses)
31425 tree clauses, sb, ret;
31426 unsigned int save;
31427 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31429 strcat (p_name, " teams");
31430 mask |= OMP_TEAMS_CLAUSE_MASK;
31432 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31434 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31435 const char *p = IDENTIFIER_POINTER (id);
31436 if (strcmp (p, "distribute") == 0)
31438 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31439 if (cclauses == NULL)
31440 cclauses = cclauses_buf;
31442 cp_lexer_consume_token (parser->lexer);
31443 if (!flag_openmp) /* flag_openmp_simd */
31444 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31445 cclauses);
31446 sb = begin_omp_structured_block ();
31447 save = cp_parser_begin_omp_structured_block (parser);
31448 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31449 cclauses);
31450 cp_parser_end_omp_structured_block (parser, save);
31451 tree body = finish_omp_structured_block (sb);
31452 if (ret == NULL)
31453 return ret;
31454 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31455 ret = make_node (OMP_TEAMS);
31456 TREE_TYPE (ret) = void_type_node;
31457 OMP_TEAMS_CLAUSES (ret) = clauses;
31458 OMP_TEAMS_BODY (ret) = body;
31459 OMP_TEAMS_COMBINED (ret) = 1;
31460 return add_stmt (ret);
31463 if (!flag_openmp) /* flag_openmp_simd */
31465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31466 return NULL_TREE;
31469 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31470 cclauses == NULL);
31471 if (cclauses)
31473 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31474 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31477 tree stmt = make_node (OMP_TEAMS);
31478 TREE_TYPE (stmt) = void_type_node;
31479 OMP_TEAMS_CLAUSES (stmt) = clauses;
31480 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31482 return add_stmt (stmt);
31485 /* OpenMP 4.0:
31486 # pragma omp target data target-data-clause[optseq] new-line
31487 structured-block */
31489 #define OMP_TARGET_DATA_CLAUSE_MASK \
31490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31494 static tree
31495 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31497 tree stmt = make_node (OMP_TARGET_DATA);
31498 TREE_TYPE (stmt) = void_type_node;
31500 OMP_TARGET_DATA_CLAUSES (stmt)
31501 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31502 "#pragma omp target data", pragma_tok);
31503 keep_next_level (true);
31504 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31506 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31507 return add_stmt (stmt);
31510 /* OpenMP 4.0:
31511 # pragma omp target update target-update-clause[optseq] new-line */
31513 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31519 static bool
31520 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31521 enum pragma_context context)
31523 if (context == pragma_stmt)
31525 error_at (pragma_tok->location,
31526 "%<#pragma omp target update%> may only be "
31527 "used in compound statements");
31528 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31529 return false;
31532 tree clauses
31533 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31534 "#pragma omp target update", pragma_tok);
31535 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31536 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31538 error_at (pragma_tok->location,
31539 "%<#pragma omp target update%> must contain at least one "
31540 "%<from%> or %<to%> clauses");
31541 return false;
31544 tree stmt = make_node (OMP_TARGET_UPDATE);
31545 TREE_TYPE (stmt) = void_type_node;
31546 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31547 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31548 add_stmt (stmt);
31549 return false;
31552 /* OpenMP 4.0:
31553 # pragma omp target target-clause[optseq] new-line
31554 structured-block */
31556 #define OMP_TARGET_CLAUSE_MASK \
31557 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31561 static bool
31562 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31563 enum pragma_context context)
31565 if (context != pragma_stmt && context != pragma_compound)
31567 cp_parser_error (parser, "expected declaration specifiers");
31568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31569 return false;
31572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31575 const char *p = IDENTIFIER_POINTER (id);
31577 if (strcmp (p, "teams") == 0)
31579 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31580 char p_name[sizeof ("#pragma omp target teams distribute "
31581 "parallel for simd")];
31583 cp_lexer_consume_token (parser->lexer);
31584 strcpy (p_name, "#pragma omp target");
31585 if (!flag_openmp) /* flag_openmp_simd */
31587 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31588 OMP_TARGET_CLAUSE_MASK,
31589 cclauses);
31590 return stmt != NULL_TREE;
31592 keep_next_level (true);
31593 tree sb = begin_omp_structured_block ();
31594 unsigned save = cp_parser_begin_omp_structured_block (parser);
31595 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31596 OMP_TARGET_CLAUSE_MASK, cclauses);
31597 cp_parser_end_omp_structured_block (parser, save);
31598 tree body = finish_omp_structured_block (sb);
31599 if (ret == NULL_TREE)
31600 return false;
31601 tree stmt = make_node (OMP_TARGET);
31602 TREE_TYPE (stmt) = void_type_node;
31603 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31604 OMP_TARGET_BODY (stmt) = body;
31605 add_stmt (stmt);
31606 return true;
31608 else if (!flag_openmp) /* flag_openmp_simd */
31610 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31611 return false;
31613 else if (strcmp (p, "data") == 0)
31615 cp_lexer_consume_token (parser->lexer);
31616 cp_parser_omp_target_data (parser, pragma_tok);
31617 return true;
31619 else if (strcmp (p, "update") == 0)
31621 cp_lexer_consume_token (parser->lexer);
31622 return cp_parser_omp_target_update (parser, pragma_tok, context);
31626 tree stmt = make_node (OMP_TARGET);
31627 TREE_TYPE (stmt) = void_type_node;
31629 OMP_TARGET_CLAUSES (stmt)
31630 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31631 "#pragma omp target", pragma_tok);
31632 keep_next_level (true);
31633 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31635 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31636 add_stmt (stmt);
31637 return true;
31640 /* OpenACC 2.0:
31641 # pragma acc cache (variable-list) new-line
31644 static tree
31645 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31647 tree stmt, clauses;
31649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31650 clauses = finish_omp_clauses (clauses);
31652 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31654 stmt = make_node (OACC_CACHE);
31655 TREE_TYPE (stmt) = void_type_node;
31656 OACC_CACHE_CLAUSES (stmt) = clauses;
31657 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31658 add_stmt (stmt);
31660 return stmt;
31663 /* OpenACC 2.0:
31664 # pragma acc data oacc-data-clause[optseq] new-line
31665 structured-block */
31667 #define OACC_DATA_CLAUSE_MASK \
31668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31680 static tree
31681 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31683 tree stmt, clauses, block;
31684 unsigned int save;
31686 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31687 "#pragma acc data", pragma_tok);
31689 block = begin_omp_parallel ();
31690 save = cp_parser_begin_omp_structured_block (parser);
31691 cp_parser_statement (parser, NULL_TREE, false, NULL);
31692 cp_parser_end_omp_structured_block (parser, save);
31693 stmt = finish_oacc_data (clauses, block);
31694 return stmt;
31697 /* OpenACC 2.0:
31698 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31702 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31704 LOC is the location of the #pragma token.
31707 #define OACC_ENTER_DATA_CLAUSE_MASK \
31708 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31716 #define OACC_EXIT_DATA_CLAUSE_MASK \
31717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31723 static tree
31724 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31725 bool enter)
31727 tree stmt, clauses;
31729 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31730 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31732 cp_parser_error (parser, enter
31733 ? "expected %<data%> in %<#pragma acc enter data%>"
31734 : "expected %<data%> in %<#pragma acc exit data%>");
31735 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31736 return NULL_TREE;
31739 const char *p =
31740 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31741 if (strcmp (p, "data") != 0)
31743 cp_parser_error (parser, "invalid pragma");
31744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31745 return NULL_TREE;
31748 cp_lexer_consume_token (parser->lexer);
31750 if (enter)
31751 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31752 "#pragma acc enter data", pragma_tok);
31753 else
31754 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31755 "#pragma acc exit data", pragma_tok);
31757 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31759 error_at (pragma_tok->location,
31760 "%<#pragma acc enter data%> has no data movement clause");
31761 return NULL_TREE;
31764 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31765 TREE_TYPE (stmt) = void_type_node;
31766 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31767 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31768 add_stmt (stmt);
31769 return stmt;
31772 /* OpenACC 2.0:
31773 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31774 structured-block */
31776 #define OACC_KERNELS_CLAUSE_MASK \
31777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31791 static tree
31792 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31794 tree stmt, clauses, block;
31795 unsigned int save;
31797 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31798 "#pragma acc kernels", pragma_tok);
31800 block = begin_omp_parallel ();
31801 save = cp_parser_begin_omp_structured_block (parser);
31802 cp_parser_statement (parser, NULL_TREE, false, NULL);
31803 cp_parser_end_omp_structured_block (parser, save);
31804 stmt = finish_oacc_kernels (clauses, block);
31805 return stmt;
31808 /* OpenACC 2.0:
31809 # pragma acc loop oacc-loop-clause[optseq] new-line
31810 structured-block */
31812 #define OACC_LOOP_CLAUSE_MASK \
31813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31816 static tree
31817 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31819 tree stmt, clauses, block;
31820 int save;
31822 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31823 "#pragma acc loop", pragma_tok);
31825 block = begin_omp_structured_block ();
31826 save = cp_parser_begin_omp_structured_block (parser);
31827 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31828 cp_parser_end_omp_structured_block (parser, save);
31829 add_stmt (finish_omp_structured_block (block));
31830 return stmt;
31833 /* OpenACC 2.0:
31834 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31835 structured-block */
31837 #define OACC_PARALLEL_CLAUSE_MASK \
31838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31856 static tree
31857 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31859 tree stmt, clauses, block;
31860 unsigned int save;
31862 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31863 "#pragma acc parallel", pragma_tok);
31865 block = begin_omp_parallel ();
31866 save = cp_parser_begin_omp_structured_block (parser);
31867 cp_parser_statement (parser, NULL_TREE, false, NULL);
31868 cp_parser_end_omp_structured_block (parser, save);
31869 stmt = finish_oacc_parallel (clauses, block);
31870 return stmt;
31873 /* OpenACC 2.0:
31874 # pragma acc update oacc-update-clause[optseq] new-line
31877 #define OACC_UPDATE_CLAUSE_MASK \
31878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31885 static tree
31886 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31888 tree stmt, clauses;
31890 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31891 "#pragma acc update", pragma_tok);
31893 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31895 error_at (pragma_tok->location,
31896 "%<#pragma acc update%> must contain at least one "
31897 "%<device%> or %<host/self%> clause");
31898 return NULL_TREE;
31901 stmt = make_node (OACC_UPDATE);
31902 TREE_TYPE (stmt) = void_type_node;
31903 OACC_UPDATE_CLAUSES (stmt) = clauses;
31904 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31905 add_stmt (stmt);
31906 return stmt;
31909 /* OpenACC 2.0:
31910 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31912 LOC is the location of the #pragma token.
31915 #define OACC_WAIT_CLAUSE_MASK \
31916 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31918 static tree
31919 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31921 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31922 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31924 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31925 list = cp_parser_oacc_wait_list (parser, loc, list);
31927 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31928 "#pragma acc wait", pragma_tok);
31930 stmt = c_finish_oacc_wait (loc, list, clauses);
31932 return stmt;
31935 /* OpenMP 4.0:
31936 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31938 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31939 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31946 static void
31947 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31948 enum pragma_context context)
31950 bool first_p = parser->omp_declare_simd == NULL;
31951 cp_omp_declare_simd_data data;
31952 if (first_p)
31954 data.error_seen = false;
31955 data.fndecl_seen = false;
31956 data.tokens = vNULL;
31957 parser->omp_declare_simd = &data;
31959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31960 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31961 cp_lexer_consume_token (parser->lexer);
31962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31963 parser->omp_declare_simd->error_seen = true;
31964 cp_parser_require_pragma_eol (parser, pragma_tok);
31965 struct cp_token_cache *cp
31966 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31967 parser->omp_declare_simd->tokens.safe_push (cp);
31968 if (first_p)
31970 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31971 cp_parser_pragma (parser, context);
31972 switch (context)
31974 case pragma_external:
31975 cp_parser_declaration (parser);
31976 break;
31977 case pragma_member:
31978 cp_parser_member_declaration (parser);
31979 break;
31980 case pragma_objc_icode:
31981 cp_parser_block_declaration (parser, /*statement_p=*/false);
31982 break;
31983 default:
31984 cp_parser_declaration_statement (parser);
31985 break;
31987 if (parser->omp_declare_simd
31988 && !parser->omp_declare_simd->error_seen
31989 && !parser->omp_declare_simd->fndecl_seen)
31990 error_at (pragma_tok->location,
31991 "%<#pragma omp declare simd%> not immediately followed by "
31992 "function declaration or definition");
31993 data.tokens.release ();
31994 parser->omp_declare_simd = NULL;
31998 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31999 This function is modelled similar to the late parsing of omp declare
32000 simd. */
32002 static tree
32003 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
32005 struct cp_token_cache *ce;
32006 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
32007 int ii = 0;
32009 if (parser->omp_declare_simd != NULL)
32011 error ("%<#pragma omp declare simd%> cannot be used in the same function"
32012 " marked as a Cilk Plus SIMD-enabled function");
32013 XDELETE (parser->cilk_simd_fn_info);
32014 parser->cilk_simd_fn_info = NULL;
32015 return attrs;
32017 if (!info->error_seen && info->fndecl_seen)
32019 error ("vector attribute not immediately followed by a single function"
32020 " declaration or definition");
32021 info->error_seen = true;
32023 if (info->error_seen)
32024 return attrs;
32026 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
32028 tree c, cl;
32030 cp_parser_push_lexer_for_tokens (parser, ce);
32031 parser->lexer->in_pragma = true;
32032 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
32033 "SIMD-enabled functions attribute",
32034 NULL);
32035 cp_parser_pop_lexer (parser);
32036 if (cl)
32037 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32039 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
32040 TREE_CHAIN (c) = attrs;
32041 attrs = c;
32043 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32044 TREE_CHAIN (c) = attrs;
32045 if (processing_template_decl)
32046 ATTR_IS_DEPENDENT (c) = 1;
32047 attrs = c;
32049 info->fndecl_seen = true;
32050 XDELETE (parser->cilk_simd_fn_info);
32051 parser->cilk_simd_fn_info = NULL;
32052 return attrs;
32055 /* Finalize #pragma omp declare simd clauses after direct declarator has
32056 been parsed, and put that into "omp declare simd" attribute. */
32058 static tree
32059 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
32061 struct cp_token_cache *ce;
32062 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32063 int i;
32065 if (!data->error_seen && data->fndecl_seen)
32067 error ("%<#pragma omp declare simd%> not immediately followed by "
32068 "a single function declaration or definition");
32069 data->error_seen = true;
32070 return attrs;
32072 if (data->error_seen)
32073 return attrs;
32075 FOR_EACH_VEC_ELT (data->tokens, i, ce)
32077 tree c, cl;
32079 cp_parser_push_lexer_for_tokens (parser, ce);
32080 parser->lexer->in_pragma = true;
32081 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32082 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32083 cp_lexer_consume_token (parser->lexer);
32084 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32085 "#pragma omp declare simd", pragma_tok);
32086 cp_parser_pop_lexer (parser);
32087 if (cl)
32088 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32089 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32090 TREE_CHAIN (c) = attrs;
32091 if (processing_template_decl)
32092 ATTR_IS_DEPENDENT (c) = 1;
32093 attrs = c;
32096 data->fndecl_seen = true;
32097 return attrs;
32101 /* OpenMP 4.0:
32102 # pragma omp declare target new-line
32103 declarations and definitions
32104 # pragma omp end declare target new-line */
32106 static void
32107 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32110 scope_chain->omp_declare_target_attribute++;
32113 static void
32114 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32116 const char *p = "";
32117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32120 p = IDENTIFIER_POINTER (id);
32122 if (strcmp (p, "declare") == 0)
32124 cp_lexer_consume_token (parser->lexer);
32125 p = "";
32126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32129 p = IDENTIFIER_POINTER (id);
32131 if (strcmp (p, "target") == 0)
32132 cp_lexer_consume_token (parser->lexer);
32133 else
32135 cp_parser_error (parser, "expected %<target%>");
32136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32137 return;
32140 else
32142 cp_parser_error (parser, "expected %<declare%>");
32143 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32144 return;
32146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32147 if (!scope_chain->omp_declare_target_attribute)
32148 error_at (pragma_tok->location,
32149 "%<#pragma omp end declare target%> without corresponding "
32150 "%<#pragma omp declare target%>");
32151 else
32152 scope_chain->omp_declare_target_attribute--;
32155 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32156 expression and optional initializer clause of
32157 #pragma omp declare reduction. We store the expression(s) as
32158 either 3, 6 or 7 special statements inside of the artificial function's
32159 body. The first two statements are DECL_EXPRs for the artificial
32160 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32161 expression that uses those variables.
32162 If there was any INITIALIZER clause, this is followed by further statements,
32163 the fourth and fifth statements are DECL_EXPRs for the artificial
32164 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32165 constructor variant (first token after open paren is not omp_priv),
32166 then the sixth statement is a statement with the function call expression
32167 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32168 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32169 to initialize the OMP_PRIV artificial variable and there is seventh
32170 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32172 static bool
32173 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32175 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32176 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32177 type = TREE_TYPE (type);
32178 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32179 DECL_ARTIFICIAL (omp_out) = 1;
32180 pushdecl (omp_out);
32181 add_decl_expr (omp_out);
32182 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32183 DECL_ARTIFICIAL (omp_in) = 1;
32184 pushdecl (omp_in);
32185 add_decl_expr (omp_in);
32186 tree combiner;
32187 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32189 keep_next_level (true);
32190 tree block = begin_omp_structured_block ();
32191 combiner = cp_parser_expression (parser);
32192 finish_expr_stmt (combiner);
32193 block = finish_omp_structured_block (block);
32194 add_stmt (block);
32196 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32197 return false;
32199 const char *p = "";
32200 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32202 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32203 p = IDENTIFIER_POINTER (id);
32206 if (strcmp (p, "initializer") == 0)
32208 cp_lexer_consume_token (parser->lexer);
32209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32210 return false;
32212 p = "";
32213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32215 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32216 p = IDENTIFIER_POINTER (id);
32219 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32220 DECL_ARTIFICIAL (omp_priv) = 1;
32221 pushdecl (omp_priv);
32222 add_decl_expr (omp_priv);
32223 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32224 DECL_ARTIFICIAL (omp_orig) = 1;
32225 pushdecl (omp_orig);
32226 add_decl_expr (omp_orig);
32228 keep_next_level (true);
32229 block = begin_omp_structured_block ();
32231 bool ctor = false;
32232 if (strcmp (p, "omp_priv") == 0)
32234 bool is_direct_init, is_non_constant_init;
32235 ctor = true;
32236 cp_lexer_consume_token (parser->lexer);
32237 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32239 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32240 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32241 == CPP_CLOSE_PAREN
32242 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32243 == CPP_CLOSE_PAREN))
32245 finish_omp_structured_block (block);
32246 error ("invalid initializer clause");
32247 return false;
32249 initializer = cp_parser_initializer (parser, &is_direct_init,
32250 &is_non_constant_init);
32251 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32252 NULL_TREE, LOOKUP_ONLYCONVERTING);
32254 else
32256 cp_parser_parse_tentatively (parser);
32257 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32258 /*check_dependency_p=*/true,
32259 /*template_p=*/NULL,
32260 /*declarator_p=*/false,
32261 /*optional_p=*/false);
32262 vec<tree, va_gc> *args;
32263 if (fn_name == error_mark_node
32264 || cp_parser_error_occurred (parser)
32265 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32266 || ((args = cp_parser_parenthesized_expression_list
32267 (parser, non_attr, /*cast_p=*/false,
32268 /*allow_expansion_p=*/true,
32269 /*non_constant_p=*/NULL)),
32270 cp_parser_error_occurred (parser)))
32272 finish_omp_structured_block (block);
32273 cp_parser_abort_tentative_parse (parser);
32274 cp_parser_error (parser, "expected id-expression (arguments)");
32275 return false;
32277 unsigned int i;
32278 tree arg;
32279 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32280 if (arg == omp_priv
32281 || (TREE_CODE (arg) == ADDR_EXPR
32282 && TREE_OPERAND (arg, 0) == omp_priv))
32283 break;
32284 cp_parser_abort_tentative_parse (parser);
32285 if (arg == NULL_TREE)
32286 error ("one of the initializer call arguments should be %<omp_priv%>"
32287 " or %<&omp_priv%>");
32288 initializer = cp_parser_postfix_expression (parser, false, false, false,
32289 false, NULL);
32290 finish_expr_stmt (initializer);
32293 block = finish_omp_structured_block (block);
32294 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32295 add_stmt (block);
32297 if (ctor)
32298 add_decl_expr (omp_orig);
32300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32301 return false;
32304 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32305 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32307 return true;
32310 /* OpenMP 4.0
32311 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32312 initializer-clause[opt] new-line
32314 initializer-clause:
32315 initializer (omp_priv initializer)
32316 initializer (function-name (argument-list)) */
32318 static void
32319 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32320 enum pragma_context)
32322 auto_vec<tree> types;
32323 enum tree_code reduc_code = ERROR_MARK;
32324 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32325 unsigned int i;
32326 cp_token *first_token;
32327 cp_token_cache *cp;
32328 int errs;
32329 void *p;
32331 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32332 p = obstack_alloc (&declarator_obstack, 0);
32334 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32335 goto fail;
32337 switch (cp_lexer_peek_token (parser->lexer)->type)
32339 case CPP_PLUS:
32340 reduc_code = PLUS_EXPR;
32341 break;
32342 case CPP_MULT:
32343 reduc_code = MULT_EXPR;
32344 break;
32345 case CPP_MINUS:
32346 reduc_code = MINUS_EXPR;
32347 break;
32348 case CPP_AND:
32349 reduc_code = BIT_AND_EXPR;
32350 break;
32351 case CPP_XOR:
32352 reduc_code = BIT_XOR_EXPR;
32353 break;
32354 case CPP_OR:
32355 reduc_code = BIT_IOR_EXPR;
32356 break;
32357 case CPP_AND_AND:
32358 reduc_code = TRUTH_ANDIF_EXPR;
32359 break;
32360 case CPP_OR_OR:
32361 reduc_code = TRUTH_ORIF_EXPR;
32362 break;
32363 case CPP_NAME:
32364 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32365 break;
32366 default:
32367 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32368 "%<|%>, %<&&%>, %<||%> or identifier");
32369 goto fail;
32372 if (reduc_code != ERROR_MARK)
32373 cp_lexer_consume_token (parser->lexer);
32375 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32376 if (reduc_id == error_mark_node)
32377 goto fail;
32379 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32380 goto fail;
32382 /* Types may not be defined in declare reduction type list. */
32383 const char *saved_message;
32384 saved_message = parser->type_definition_forbidden_message;
32385 parser->type_definition_forbidden_message
32386 = G_("types may not be defined in declare reduction type list");
32387 bool saved_colon_corrects_to_scope_p;
32388 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32389 parser->colon_corrects_to_scope_p = false;
32390 bool saved_colon_doesnt_start_class_def_p;
32391 saved_colon_doesnt_start_class_def_p
32392 = parser->colon_doesnt_start_class_def_p;
32393 parser->colon_doesnt_start_class_def_p = true;
32395 while (true)
32397 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32398 type = cp_parser_type_id (parser);
32399 if (type == error_mark_node)
32401 else if (ARITHMETIC_TYPE_P (type)
32402 && (orig_reduc_id == NULL_TREE
32403 || (TREE_CODE (type) != COMPLEX_TYPE
32404 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32405 "min") == 0
32406 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32407 "max") == 0))))
32408 error_at (loc, "predeclared arithmetic type %qT in "
32409 "%<#pragma omp declare reduction%>", type);
32410 else if (TREE_CODE (type) == FUNCTION_TYPE
32411 || TREE_CODE (type) == METHOD_TYPE
32412 || TREE_CODE (type) == ARRAY_TYPE)
32413 error_at (loc, "function or array type %qT in "
32414 "%<#pragma omp declare reduction%>", type);
32415 else if (TREE_CODE (type) == REFERENCE_TYPE)
32416 error_at (loc, "reference type %qT in "
32417 "%<#pragma omp declare reduction%>", type);
32418 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32419 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32420 "%<#pragma omp declare reduction%>", type);
32421 else
32422 types.safe_push (type);
32424 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32425 cp_lexer_consume_token (parser->lexer);
32426 else
32427 break;
32430 /* Restore the saved message. */
32431 parser->type_definition_forbidden_message = saved_message;
32432 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32433 parser->colon_doesnt_start_class_def_p
32434 = saved_colon_doesnt_start_class_def_p;
32436 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32437 || types.is_empty ())
32439 fail:
32440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32441 goto done;
32444 first_token = cp_lexer_peek_token (parser->lexer);
32445 cp = NULL;
32446 errs = errorcount;
32447 FOR_EACH_VEC_ELT (types, i, type)
32449 tree fntype
32450 = build_function_type_list (void_type_node,
32451 cp_build_reference_type (type, false),
32452 NULL_TREE);
32453 tree this_reduc_id = reduc_id;
32454 if (!dependent_type_p (type))
32455 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32456 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32457 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32458 DECL_ARTIFICIAL (fndecl) = 1;
32459 DECL_EXTERNAL (fndecl) = 1;
32460 DECL_DECLARED_INLINE_P (fndecl) = 1;
32461 DECL_IGNORED_P (fndecl) = 1;
32462 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32463 DECL_ATTRIBUTES (fndecl)
32464 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32465 DECL_ATTRIBUTES (fndecl));
32466 if (processing_template_decl)
32467 fndecl = push_template_decl (fndecl);
32468 bool block_scope = false;
32469 tree block = NULL_TREE;
32470 if (current_function_decl)
32472 block_scope = true;
32473 DECL_CONTEXT (fndecl) = global_namespace;
32474 if (!processing_template_decl)
32475 pushdecl (fndecl);
32477 else if (current_class_type)
32479 if (cp == NULL)
32481 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32482 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32483 cp_lexer_consume_token (parser->lexer);
32484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32485 goto fail;
32486 cp = cp_token_cache_new (first_token,
32487 cp_lexer_peek_nth_token (parser->lexer,
32488 2));
32490 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32491 finish_member_declaration (fndecl);
32492 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32493 DECL_PENDING_INLINE_P (fndecl) = 1;
32494 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32495 continue;
32497 else
32499 DECL_CONTEXT (fndecl) = current_namespace;
32500 pushdecl (fndecl);
32502 if (!block_scope)
32503 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32504 else
32505 block = begin_omp_structured_block ();
32506 if (cp)
32508 cp_parser_push_lexer_for_tokens (parser, cp);
32509 parser->lexer->in_pragma = true;
32511 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32513 if (!block_scope)
32514 finish_function (0);
32515 else
32516 DECL_CONTEXT (fndecl) = current_function_decl;
32517 if (cp)
32518 cp_parser_pop_lexer (parser);
32519 goto fail;
32521 if (cp)
32522 cp_parser_pop_lexer (parser);
32523 if (!block_scope)
32524 finish_function (0);
32525 else
32527 DECL_CONTEXT (fndecl) = current_function_decl;
32528 block = finish_omp_structured_block (block);
32529 if (TREE_CODE (block) == BIND_EXPR)
32530 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32531 else if (TREE_CODE (block) == STATEMENT_LIST)
32532 DECL_SAVED_TREE (fndecl) = block;
32533 if (processing_template_decl)
32534 add_decl_expr (fndecl);
32536 cp_check_omp_declare_reduction (fndecl);
32537 if (cp == NULL && types.length () > 1)
32538 cp = cp_token_cache_new (first_token,
32539 cp_lexer_peek_nth_token (parser->lexer, 2));
32540 if (errs != errorcount)
32541 break;
32544 cp_parser_require_pragma_eol (parser, pragma_tok);
32546 done:
32547 /* Free any declarators allocated. */
32548 obstack_free (&declarator_obstack, p);
32551 /* OpenMP 4.0
32552 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32553 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32554 initializer-clause[opt] new-line
32555 #pragma omp declare target new-line */
32557 static void
32558 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32559 enum pragma_context context)
32561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32564 const char *p = IDENTIFIER_POINTER (id);
32566 if (strcmp (p, "simd") == 0)
32568 cp_lexer_consume_token (parser->lexer);
32569 cp_parser_omp_declare_simd (parser, pragma_tok,
32570 context);
32571 return;
32573 cp_ensure_no_omp_declare_simd (parser);
32574 if (strcmp (p, "reduction") == 0)
32576 cp_lexer_consume_token (parser->lexer);
32577 cp_parser_omp_declare_reduction (parser, pragma_tok,
32578 context);
32579 return;
32581 if (!flag_openmp) /* flag_openmp_simd */
32583 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32584 return;
32586 if (strcmp (p, "target") == 0)
32588 cp_lexer_consume_token (parser->lexer);
32589 cp_parser_omp_declare_target (parser, pragma_tok);
32590 return;
32593 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32594 "or %<target%>");
32595 cp_parser_require_pragma_eol (parser, pragma_tok);
32598 /* Main entry point to OpenMP statement pragmas. */
32600 static void
32601 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32603 tree stmt;
32604 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32605 omp_clause_mask mask (0);
32607 switch (pragma_tok->pragma_kind)
32609 case PRAGMA_OACC_CACHE:
32610 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32611 break;
32612 case PRAGMA_OACC_DATA:
32613 stmt = cp_parser_oacc_data (parser, pragma_tok);
32614 break;
32615 case PRAGMA_OACC_ENTER_DATA:
32616 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32617 break;
32618 case PRAGMA_OACC_EXIT_DATA:
32619 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32620 break;
32621 case PRAGMA_OACC_KERNELS:
32622 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32623 break;
32624 case PRAGMA_OACC_LOOP:
32625 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32626 break;
32627 case PRAGMA_OACC_PARALLEL:
32628 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32629 break;
32630 case PRAGMA_OACC_UPDATE:
32631 stmt = cp_parser_oacc_update (parser, pragma_tok);
32632 break;
32633 case PRAGMA_OACC_WAIT:
32634 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32635 break;
32636 case PRAGMA_OMP_ATOMIC:
32637 cp_parser_omp_atomic (parser, pragma_tok);
32638 return;
32639 case PRAGMA_OMP_CRITICAL:
32640 stmt = cp_parser_omp_critical (parser, pragma_tok);
32641 break;
32642 case PRAGMA_OMP_DISTRIBUTE:
32643 strcpy (p_name, "#pragma omp");
32644 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32645 break;
32646 case PRAGMA_OMP_FOR:
32647 strcpy (p_name, "#pragma omp");
32648 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32649 break;
32650 case PRAGMA_OMP_MASTER:
32651 stmt = cp_parser_omp_master (parser, pragma_tok);
32652 break;
32653 case PRAGMA_OMP_ORDERED:
32654 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32655 break;
32656 case PRAGMA_OMP_PARALLEL:
32657 strcpy (p_name, "#pragma omp");
32658 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32659 break;
32660 case PRAGMA_OMP_SECTIONS:
32661 strcpy (p_name, "#pragma omp");
32662 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32663 break;
32664 case PRAGMA_OMP_SIMD:
32665 strcpy (p_name, "#pragma omp");
32666 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32667 break;
32668 case PRAGMA_OMP_SINGLE:
32669 stmt = cp_parser_omp_single (parser, pragma_tok);
32670 break;
32671 case PRAGMA_OMP_TASK:
32672 stmt = cp_parser_omp_task (parser, pragma_tok);
32673 break;
32674 case PRAGMA_OMP_TASKGROUP:
32675 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32676 break;
32677 case PRAGMA_OMP_TEAMS:
32678 strcpy (p_name, "#pragma omp");
32679 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32680 break;
32681 default:
32682 gcc_unreachable ();
32685 if (stmt)
32686 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32689 /* Transactional Memory parsing routines. */
32691 /* Parse a transaction attribute.
32693 txn-attribute:
32694 attribute
32695 [ [ identifier ] ]
32697 ??? Simplify this when C++0x bracket attributes are
32698 implemented properly. */
32700 static tree
32701 cp_parser_txn_attribute_opt (cp_parser *parser)
32703 cp_token *token;
32704 tree attr_name, attr = NULL;
32706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32707 return cp_parser_attributes_opt (parser);
32709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32710 return NULL_TREE;
32711 cp_lexer_consume_token (parser->lexer);
32712 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32713 goto error1;
32715 token = cp_lexer_peek_token (parser->lexer);
32716 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32718 token = cp_lexer_consume_token (parser->lexer);
32720 attr_name = (token->type == CPP_KEYWORD
32721 /* For keywords, use the canonical spelling,
32722 not the parsed identifier. */
32723 ? ridpointers[(int) token->keyword]
32724 : token->u.value);
32725 attr = build_tree_list (attr_name, NULL_TREE);
32727 else
32728 cp_parser_error (parser, "expected identifier");
32730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32731 error1:
32732 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32733 return attr;
32736 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32738 transaction-statement:
32739 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32740 compound-statement
32741 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32744 static tree
32745 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32747 unsigned char old_in = parser->in_transaction;
32748 unsigned char this_in = 1, new_in;
32749 cp_token *token;
32750 tree stmt, attrs, noex;
32752 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32753 || keyword == RID_TRANSACTION_RELAXED);
32754 token = cp_parser_require_keyword (parser, keyword,
32755 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32756 : RT_TRANSACTION_RELAXED));
32757 gcc_assert (token != NULL);
32759 if (keyword == RID_TRANSACTION_RELAXED)
32760 this_in |= TM_STMT_ATTR_RELAXED;
32761 else
32763 attrs = cp_parser_txn_attribute_opt (parser);
32764 if (attrs)
32765 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32768 /* Parse a noexcept specification. */
32769 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32771 /* Keep track if we're in the lexical scope of an outer transaction. */
32772 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32774 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32776 parser->in_transaction = new_in;
32777 cp_parser_compound_statement (parser, NULL, false, false);
32778 parser->in_transaction = old_in;
32780 finish_transaction_stmt (stmt, NULL, this_in, noex);
32782 return stmt;
32785 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32787 transaction-expression:
32788 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32789 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32792 static tree
32793 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32795 unsigned char old_in = parser->in_transaction;
32796 unsigned char this_in = 1;
32797 cp_token *token;
32798 tree expr, noex;
32799 bool noex_expr;
32801 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32802 || keyword == RID_TRANSACTION_RELAXED);
32804 if (!flag_tm)
32805 error (keyword == RID_TRANSACTION_RELAXED
32806 ? G_("%<__transaction_relaxed%> without transactional memory "
32807 "support enabled")
32808 : G_("%<__transaction_atomic%> without transactional memory "
32809 "support enabled"));
32811 token = cp_parser_require_keyword (parser, keyword,
32812 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32813 : RT_TRANSACTION_RELAXED));
32814 gcc_assert (token != NULL);
32816 if (keyword == RID_TRANSACTION_RELAXED)
32817 this_in |= TM_STMT_ATTR_RELAXED;
32819 /* Set this early. This might mean that we allow transaction_cancel in
32820 an expression that we find out later actually has to be a constexpr.
32821 However, we expect that cxx_constant_value will be able to deal with
32822 this; also, if the noexcept has no constexpr, then what we parse next
32823 really is a transaction's body. */
32824 parser->in_transaction = this_in;
32826 /* Parse a noexcept specification. */
32827 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32828 true);
32830 if (!noex || !noex_expr
32831 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32833 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32835 expr = cp_parser_expression (parser);
32836 expr = finish_parenthesized_expr (expr);
32838 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32840 else
32842 /* The only expression that is available got parsed for the noexcept
32843 already. noexcept is true then. */
32844 expr = noex;
32845 noex = boolean_true_node;
32848 expr = build_transaction_expr (token->location, expr, this_in, noex);
32849 parser->in_transaction = old_in;
32851 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32852 return error_mark_node;
32854 return (flag_tm ? expr : error_mark_node);
32857 /* Parse a function-transaction-block.
32859 function-transaction-block:
32860 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32861 function-body
32862 __transaction_atomic txn-attribute[opt] function-try-block
32863 __transaction_relaxed ctor-initializer[opt] function-body
32864 __transaction_relaxed function-try-block
32867 static bool
32868 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32870 unsigned char old_in = parser->in_transaction;
32871 unsigned char new_in = 1;
32872 tree compound_stmt, stmt, attrs;
32873 bool ctor_initializer_p;
32874 cp_token *token;
32876 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32877 || keyword == RID_TRANSACTION_RELAXED);
32878 token = cp_parser_require_keyword (parser, keyword,
32879 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32880 : RT_TRANSACTION_RELAXED));
32881 gcc_assert (token != NULL);
32883 if (keyword == RID_TRANSACTION_RELAXED)
32884 new_in |= TM_STMT_ATTR_RELAXED;
32885 else
32887 attrs = cp_parser_txn_attribute_opt (parser);
32888 if (attrs)
32889 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32892 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32894 parser->in_transaction = new_in;
32896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32897 ctor_initializer_p = cp_parser_function_try_block (parser);
32898 else
32899 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32900 (parser, /*in_function_try_block=*/false);
32902 parser->in_transaction = old_in;
32904 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32906 return ctor_initializer_p;
32909 /* Parse a __transaction_cancel statement.
32911 cancel-statement:
32912 __transaction_cancel txn-attribute[opt] ;
32913 __transaction_cancel txn-attribute[opt] throw-expression ;
32915 ??? Cancel and throw is not yet implemented. */
32917 static tree
32918 cp_parser_transaction_cancel (cp_parser *parser)
32920 cp_token *token;
32921 bool is_outer = false;
32922 tree stmt, attrs;
32924 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32925 RT_TRANSACTION_CANCEL);
32926 gcc_assert (token != NULL);
32928 attrs = cp_parser_txn_attribute_opt (parser);
32929 if (attrs)
32930 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32932 /* ??? Parse cancel-and-throw here. */
32934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32936 if (!flag_tm)
32938 error_at (token->location, "%<__transaction_cancel%> without "
32939 "transactional memory support enabled");
32940 return error_mark_node;
32942 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32944 error_at (token->location, "%<__transaction_cancel%> within a "
32945 "%<__transaction_relaxed%>");
32946 return error_mark_node;
32948 else if (is_outer)
32950 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32951 && !is_tm_may_cancel_outer (current_function_decl))
32953 error_at (token->location, "outer %<__transaction_cancel%> not "
32954 "within outer %<__transaction_atomic%>");
32955 error_at (token->location,
32956 " or a %<transaction_may_cancel_outer%> function");
32957 return error_mark_node;
32960 else if (parser->in_transaction == 0)
32962 error_at (token->location, "%<__transaction_cancel%> not within "
32963 "%<__transaction_atomic%>");
32964 return error_mark_node;
32967 stmt = build_tm_abort_call (token->location, is_outer);
32968 add_stmt (stmt);
32970 return stmt;
32973 /* The parser. */
32975 static GTY (()) cp_parser *the_parser;
32978 /* Special handling for the first token or line in the file. The first
32979 thing in the file might be #pragma GCC pch_preprocess, which loads a
32980 PCH file, which is a GC collection point. So we need to handle this
32981 first pragma without benefit of an existing lexer structure.
32983 Always returns one token to the caller in *FIRST_TOKEN. This is
32984 either the true first token of the file, or the first token after
32985 the initial pragma. */
32987 static void
32988 cp_parser_initial_pragma (cp_token *first_token)
32990 tree name = NULL;
32992 cp_lexer_get_preprocessor_token (NULL, first_token);
32993 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32994 return;
32996 cp_lexer_get_preprocessor_token (NULL, first_token);
32997 if (first_token->type == CPP_STRING)
32999 name = first_token->u.value;
33001 cp_lexer_get_preprocessor_token (NULL, first_token);
33002 if (first_token->type != CPP_PRAGMA_EOL)
33003 error_at (first_token->location,
33004 "junk at end of %<#pragma GCC pch_preprocess%>");
33006 else
33007 error_at (first_token->location, "expected string literal");
33009 /* Skip to the end of the pragma. */
33010 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
33011 cp_lexer_get_preprocessor_token (NULL, first_token);
33013 /* Now actually load the PCH file. */
33014 if (name)
33015 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
33017 /* Read one more token to return to our caller. We have to do this
33018 after reading the PCH file in, since its pointers have to be
33019 live. */
33020 cp_lexer_get_preprocessor_token (NULL, first_token);
33023 /* Parses the grainsize pragma for the _Cilk_for statement.
33024 Syntax:
33025 #pragma cilk grainsize = <VALUE>. */
33027 static void
33028 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
33030 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
33032 tree exp = cp_parser_binary_expression (parser, false, false,
33033 PREC_NOT_OPERATOR, NULL);
33034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33035 if (!exp || exp == error_mark_node)
33037 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
33038 return;
33041 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
33042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33043 cp_parser_cilk_for (parser, fold_simple_on_cst (exp));
33044 else
33045 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
33046 "%<#pragma cilk grainsize%> is not followed by "
33047 "%<_Cilk_for%>");
33048 return;
33050 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33053 /* Normal parsing of a pragma token. Here we can (and must) use the
33054 regular lexer. */
33056 static bool
33057 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33059 cp_token *pragma_tok;
33060 unsigned int id;
33062 pragma_tok = cp_lexer_consume_token (parser->lexer);
33063 gcc_assert (pragma_tok->type == CPP_PRAGMA);
33064 parser->lexer->in_pragma = true;
33066 id = pragma_tok->pragma_kind;
33067 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33068 cp_ensure_no_omp_declare_simd (parser);
33069 switch (id)
33071 case PRAGMA_GCC_PCH_PREPROCESS:
33072 error_at (pragma_tok->location,
33073 "%<#pragma GCC pch_preprocess%> must be first");
33074 break;
33076 case PRAGMA_OMP_BARRIER:
33077 switch (context)
33079 case pragma_compound:
33080 cp_parser_omp_barrier (parser, pragma_tok);
33081 return false;
33082 case pragma_stmt:
33083 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33084 "used in compound statements");
33085 break;
33086 default:
33087 goto bad_stmt;
33089 break;
33091 case PRAGMA_OMP_FLUSH:
33092 switch (context)
33094 case pragma_compound:
33095 cp_parser_omp_flush (parser, pragma_tok);
33096 return false;
33097 case pragma_stmt:
33098 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33099 "used in compound statements");
33100 break;
33101 default:
33102 goto bad_stmt;
33104 break;
33106 case PRAGMA_OMP_TASKWAIT:
33107 switch (context)
33109 case pragma_compound:
33110 cp_parser_omp_taskwait (parser, pragma_tok);
33111 return false;
33112 case pragma_stmt:
33113 error_at (pragma_tok->location,
33114 "%<#pragma omp taskwait%> may only be "
33115 "used in compound statements");
33116 break;
33117 default:
33118 goto bad_stmt;
33120 break;
33122 case PRAGMA_OMP_TASKYIELD:
33123 switch (context)
33125 case pragma_compound:
33126 cp_parser_omp_taskyield (parser, pragma_tok);
33127 return false;
33128 case pragma_stmt:
33129 error_at (pragma_tok->location,
33130 "%<#pragma omp taskyield%> may only be "
33131 "used in compound statements");
33132 break;
33133 default:
33134 goto bad_stmt;
33136 break;
33138 case PRAGMA_OMP_CANCEL:
33139 switch (context)
33141 case pragma_compound:
33142 cp_parser_omp_cancel (parser, pragma_tok);
33143 return false;
33144 case pragma_stmt:
33145 error_at (pragma_tok->location,
33146 "%<#pragma omp cancel%> may only be "
33147 "used in compound statements");
33148 break;
33149 default:
33150 goto bad_stmt;
33152 break;
33154 case PRAGMA_OMP_CANCELLATION_POINT:
33155 switch (context)
33157 case pragma_compound:
33158 cp_parser_omp_cancellation_point (parser, pragma_tok);
33159 return false;
33160 case pragma_stmt:
33161 error_at (pragma_tok->location,
33162 "%<#pragma omp cancellation point%> may only be "
33163 "used in compound statements");
33164 break;
33165 default:
33166 goto bad_stmt;
33168 break;
33170 case PRAGMA_OMP_THREADPRIVATE:
33171 cp_parser_omp_threadprivate (parser, pragma_tok);
33172 return false;
33174 case PRAGMA_OMP_DECLARE_REDUCTION:
33175 cp_parser_omp_declare (parser, pragma_tok, context);
33176 return false;
33178 case PRAGMA_OACC_CACHE:
33179 case PRAGMA_OACC_DATA:
33180 case PRAGMA_OACC_ENTER_DATA:
33181 case PRAGMA_OACC_EXIT_DATA:
33182 case PRAGMA_OACC_KERNELS:
33183 case PRAGMA_OACC_PARALLEL:
33184 case PRAGMA_OACC_LOOP:
33185 case PRAGMA_OACC_UPDATE:
33186 case PRAGMA_OACC_WAIT:
33187 case PRAGMA_OMP_ATOMIC:
33188 case PRAGMA_OMP_CRITICAL:
33189 case PRAGMA_OMP_DISTRIBUTE:
33190 case PRAGMA_OMP_FOR:
33191 case PRAGMA_OMP_MASTER:
33192 case PRAGMA_OMP_ORDERED:
33193 case PRAGMA_OMP_PARALLEL:
33194 case PRAGMA_OMP_SECTIONS:
33195 case PRAGMA_OMP_SIMD:
33196 case PRAGMA_OMP_SINGLE:
33197 case PRAGMA_OMP_TASK:
33198 case PRAGMA_OMP_TASKGROUP:
33199 case PRAGMA_OMP_TEAMS:
33200 if (context != pragma_stmt && context != pragma_compound)
33201 goto bad_stmt;
33202 cp_parser_omp_construct (parser, pragma_tok);
33203 return true;
33205 case PRAGMA_OMP_TARGET:
33206 return cp_parser_omp_target (parser, pragma_tok, context);
33208 case PRAGMA_OMP_END_DECLARE_TARGET:
33209 cp_parser_omp_end_declare_target (parser, pragma_tok);
33210 return false;
33212 case PRAGMA_OMP_SECTION:
33213 error_at (pragma_tok->location,
33214 "%<#pragma omp section%> may only be used in "
33215 "%<#pragma omp sections%> construct");
33216 break;
33218 case PRAGMA_IVDEP:
33220 if (context == pragma_external)
33222 error_at (pragma_tok->location,
33223 "%<#pragma GCC ivdep%> must be inside a function");
33224 break;
33226 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33227 cp_token *tok;
33228 tok = cp_lexer_peek_token (the_parser->lexer);
33229 if (tok->type != CPP_KEYWORD
33230 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33231 && tok->keyword != RID_DO))
33233 cp_parser_error (parser, "for, while or do statement expected");
33234 return false;
33236 cp_parser_iteration_statement (parser, true);
33237 return true;
33240 case PRAGMA_CILK_SIMD:
33241 if (context == pragma_external)
33243 error_at (pragma_tok->location,
33244 "%<#pragma simd%> must be inside a function");
33245 break;
33247 cp_parser_cilk_simd (parser, pragma_tok);
33248 return true;
33250 case PRAGMA_CILK_GRAINSIZE:
33251 if (context == pragma_external)
33253 error_at (pragma_tok->location,
33254 "%<#pragma cilk grainsize%> must be inside a function");
33255 break;
33258 /* Ignore the pragma if Cilk Plus is not enabled. */
33259 if (flag_cilkplus)
33261 cp_parser_cilk_grainsize (parser, pragma_tok);
33262 return true;
33264 else
33266 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33267 "%<#pragma cilk grainsize%>");
33268 break;
33271 default:
33272 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33273 c_invoke_pragma_handler (id);
33274 break;
33276 bad_stmt:
33277 cp_parser_error (parser, "expected declaration specifiers");
33278 break;
33281 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33282 return false;
33285 /* The interface the pragma parsers have to the lexer. */
33287 enum cpp_ttype
33288 pragma_lex (tree *value)
33290 cp_token *tok;
33291 enum cpp_ttype ret;
33293 tok = cp_lexer_peek_token (the_parser->lexer);
33295 ret = tok->type;
33296 *value = tok->u.value;
33298 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33299 ret = CPP_EOF;
33300 else if (ret == CPP_STRING)
33301 *value = cp_parser_string_literal (the_parser, false, false);
33302 else
33304 cp_lexer_consume_token (the_parser->lexer);
33305 if (ret == CPP_KEYWORD)
33306 ret = CPP_NAME;
33309 return ret;
33313 /* External interface. */
33315 /* Parse one entire translation unit. */
33317 void
33318 c_parse_file (void)
33320 static bool already_called = false;
33322 if (already_called)
33323 fatal_error (input_location,
33324 "inter-module optimizations not implemented for C++");
33325 already_called = true;
33327 the_parser = cp_parser_new ();
33328 push_deferring_access_checks (flag_access_control
33329 ? dk_no_deferred : dk_no_check);
33330 cp_parser_translation_unit (the_parser);
33331 the_parser = NULL;
33334 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33335 vectorlength clause:
33336 Syntax:
33337 vectorlength ( constant-expression ) */
33339 static tree
33340 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33341 bool is_simd_fn)
33343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33344 tree expr;
33345 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33346 safelen clause. Thus, vectorlength is represented as OMP 4.0
33347 safelen. For SIMD-enabled function it is represented by OMP 4.0
33348 simdlen. */
33349 if (!is_simd_fn)
33350 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33351 loc);
33352 else
33353 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33354 loc);
33356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33357 return error_mark_node;
33359 expr = cp_parser_constant_expression (parser);
33360 expr = maybe_constant_value (expr);
33362 /* If expr == error_mark_node, then don't emit any errors nor
33363 create a clause. if any of the above functions returns
33364 error mark node then they would have emitted an error message. */
33365 if (expr == error_mark_node)
33367 else if (!TREE_TYPE (expr)
33368 || !TREE_CONSTANT (expr)
33369 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33370 error_at (loc, "vectorlength must be an integer constant");
33371 else if (TREE_CONSTANT (expr)
33372 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33373 error_at (loc, "vectorlength must be a power of 2");
33374 else
33376 tree c;
33377 if (!is_simd_fn)
33379 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33380 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33381 OMP_CLAUSE_CHAIN (c) = clauses;
33382 clauses = c;
33384 else
33386 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33387 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33388 OMP_CLAUSE_CHAIN (c) = clauses;
33389 clauses = c;
33393 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33394 return error_mark_node;
33395 return clauses;
33398 /* Handles the Cilk Plus #pragma simd linear clause.
33399 Syntax:
33400 linear ( simd-linear-variable-list )
33402 simd-linear-variable-list:
33403 simd-linear-variable
33404 simd-linear-variable-list , simd-linear-variable
33406 simd-linear-variable:
33407 id-expression
33408 id-expression : simd-linear-step
33410 simd-linear-step:
33411 conditional-expression */
33413 static tree
33414 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33418 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33419 return clauses;
33420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33422 cp_parser_error (parser, "expected identifier");
33423 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33424 return error_mark_node;
33427 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33428 parser->colon_corrects_to_scope_p = false;
33429 while (1)
33431 cp_token *token = cp_lexer_peek_token (parser->lexer);
33432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33434 cp_parser_error (parser, "expected variable-name");
33435 clauses = error_mark_node;
33436 break;
33439 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33440 false, false);
33441 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33442 token->location);
33443 if (decl == error_mark_node)
33445 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33446 token->location);
33447 clauses = error_mark_node;
33449 else
33451 tree e = NULL_TREE;
33452 tree step_size = integer_one_node;
33454 /* If present, parse the linear step. Otherwise, assume the default
33455 value of 1. */
33456 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33458 cp_lexer_consume_token (parser->lexer);
33460 e = cp_parser_assignment_expression (parser);
33461 e = maybe_constant_value (e);
33463 if (e == error_mark_node)
33465 /* If an error has occurred, then the whole pragma is
33466 considered ill-formed. Thus, no reason to keep
33467 parsing. */
33468 clauses = error_mark_node;
33469 break;
33471 else if (type_dependent_expression_p (e)
33472 || value_dependent_expression_p (e)
33473 || (TREE_TYPE (e)
33474 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33475 && (TREE_CONSTANT (e)
33476 || DECL_P (e))))
33477 step_size = e;
33478 else
33479 cp_parser_error (parser,
33480 "step size must be an integer constant "
33481 "expression or an integer variable");
33484 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33485 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33486 OMP_CLAUSE_DECL (l) = decl;
33487 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33488 OMP_CLAUSE_CHAIN (l) = clauses;
33489 clauses = l;
33491 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33492 cp_lexer_consume_token (parser->lexer);
33493 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33494 break;
33495 else
33497 error_at (cp_lexer_peek_token (parser->lexer)->location,
33498 "expected %<,%> or %<)%> after %qE", decl);
33499 clauses = error_mark_node;
33500 break;
33503 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33504 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33505 return clauses;
33508 /* Returns the name of the next clause. If the clause is not
33509 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33510 token is not consumed. Otherwise, the appropriate enum from the
33511 pragma_simd_clause is returned and the token is consumed. */
33513 static pragma_omp_clause
33514 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33516 pragma_omp_clause clause_type;
33517 cp_token *token = cp_lexer_peek_token (parser->lexer);
33519 if (token->keyword == RID_PRIVATE)
33520 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33521 else if (!token->u.value || token->type != CPP_NAME)
33522 return PRAGMA_CILK_CLAUSE_NONE;
33523 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33524 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33525 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33526 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33527 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33528 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33529 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33530 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33531 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33532 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33533 else
33534 return PRAGMA_CILK_CLAUSE_NONE;
33536 cp_lexer_consume_token (parser->lexer);
33537 return clause_type;
33540 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33542 static tree
33543 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33545 tree clauses = NULL_TREE;
33547 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33548 && clauses != error_mark_node)
33550 pragma_omp_clause c_kind;
33551 c_kind = cp_parser_cilk_simd_clause_name (parser);
33552 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33553 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33554 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33555 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33556 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33557 /* Use the OpenMP 4.0 equivalent function. */
33558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33559 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33560 /* Use the OpenMP 4.0 equivalent function. */
33561 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33562 clauses);
33563 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33564 /* Use the OMP 4.0 equivalent function. */
33565 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33566 clauses);
33567 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33568 /* Use the OMP 4.0 equivalent function. */
33569 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33570 else
33572 clauses = error_mark_node;
33573 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33574 break;
33578 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33580 if (clauses == error_mark_node)
33581 return error_mark_node;
33582 else
33583 return c_finish_cilk_clauses (clauses);
33586 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33588 static void
33589 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33591 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33593 if (clauses == error_mark_node)
33594 return;
33596 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33598 error_at (cp_lexer_peek_token (parser->lexer)->location,
33599 "for statement expected");
33600 return;
33603 tree sb = begin_omp_structured_block ();
33604 int save = cp_parser_begin_omp_structured_block (parser);
33605 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33606 if (ret)
33607 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33608 cp_parser_end_omp_structured_block (parser, save);
33609 add_stmt (finish_omp_structured_block (sb));
33612 /* Main entry-point for parsing Cilk Plus _Cilk_for
33613 loops. The return value is error_mark_node
33614 when errors happen and CILK_FOR tree on success. */
33616 static tree
33617 cp_parser_cilk_for (cp_parser *parser, tree grain)
33619 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33620 gcc_unreachable ();
33622 tree sb = begin_omp_structured_block ();
33623 int save = cp_parser_begin_omp_structured_block (parser);
33625 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33626 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33627 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33628 clauses = finish_omp_clauses (clauses);
33630 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33631 if (ret)
33632 cpp_validate_cilk_plus_loop (ret);
33633 else
33634 ret = error_mark_node;
33636 cp_parser_end_omp_structured_block (parser, save);
33637 add_stmt (finish_omp_structured_block (sb));
33638 return ret;
33641 /* Create an identifier for a generic parameter type (a synthesized
33642 template parameter implied by `auto' or a concept identifier). */
33644 static GTY(()) int generic_parm_count;
33645 static tree
33646 make_generic_type_name ()
33648 char buf[32];
33649 sprintf (buf, "auto:%d", ++generic_parm_count);
33650 return get_identifier (buf);
33653 /* Predicate that behaves as is_auto_or_concept but matches the parent
33654 node of the generic type rather than the generic type itself. This
33655 allows for type transformation in add_implicit_template_parms. */
33657 static inline bool
33658 tree_type_is_auto_or_concept (const_tree t)
33660 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33663 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33664 (creating a new template parameter list if necessary). Returns the newly
33665 created template type parm. */
33667 tree
33668 synthesize_implicit_template_parm (cp_parser *parser)
33670 gcc_assert (current_binding_level->kind == sk_function_parms);
33672 /* We are either continuing a function template that already contains implicit
33673 template parameters, creating a new fully-implicit function template, or
33674 extending an existing explicit function template with implicit template
33675 parameters. */
33677 cp_binding_level *const entry_scope = current_binding_level;
33679 bool become_template = false;
33680 cp_binding_level *parent_scope = 0;
33682 if (parser->implicit_template_scope)
33684 gcc_assert (parser->implicit_template_parms);
33686 current_binding_level = parser->implicit_template_scope;
33688 else
33690 /* Roll back to the existing template parameter scope (in the case of
33691 extending an explicit function template) or introduce a new template
33692 parameter scope ahead of the function parameter scope (or class scope
33693 in the case of out-of-line member definitions). The function scope is
33694 added back after template parameter synthesis below. */
33696 cp_binding_level *scope = entry_scope;
33698 while (scope->kind == sk_function_parms)
33700 parent_scope = scope;
33701 scope = scope->level_chain;
33703 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33705 /* If not defining a class, then any class scope is a scope level in
33706 an out-of-line member definition. In this case simply wind back
33707 beyond the first such scope to inject the template parameter list.
33708 Otherwise wind back to the class being defined. The latter can
33709 occur in class member friend declarations such as:
33711 class A {
33712 void foo (auto);
33714 class B {
33715 friend void A::foo (auto);
33718 The template parameter list synthesized for the friend declaration
33719 must be injected in the scope of 'B'. This can also occur in
33720 erroneous cases such as:
33722 struct A {
33723 struct B {
33724 void foo (auto);
33726 void B::foo (auto) {}
33729 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33730 but, nevertheless, the template parameter list synthesized for the
33731 declarator should be injected into the scope of 'A' as if the
33732 ill-formed template was specified explicitly. */
33734 while (scope->kind == sk_class && !scope->defining_class_p)
33736 parent_scope = scope;
33737 scope = scope->level_chain;
33741 current_binding_level = scope;
33743 if (scope->kind != sk_template_parms
33744 || !function_being_declared_is_template_p (parser))
33746 /* Introduce a new template parameter list for implicit template
33747 parameters. */
33749 become_template = true;
33751 parser->implicit_template_scope
33752 = begin_scope (sk_template_parms, NULL);
33754 ++processing_template_decl;
33756 parser->fully_implicit_function_template_p = true;
33757 ++parser->num_template_parameter_lists;
33759 else
33761 /* Synthesize implicit template parameters at the end of the explicit
33762 template parameter list. */
33764 gcc_assert (current_template_parms);
33766 parser->implicit_template_scope = scope;
33768 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33769 parser->implicit_template_parms
33770 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33774 /* Synthesize a new template parameter and track the current template
33775 parameter chain with implicit_template_parms. */
33777 tree synth_id = make_generic_type_name ();
33778 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33779 synth_id);
33780 tree new_parm
33781 = process_template_parm (parser->implicit_template_parms,
33782 input_location,
33783 build_tree_list (NULL_TREE, synth_tmpl_parm),
33784 /*non_type=*/false,
33785 /*param_pack=*/false);
33788 if (parser->implicit_template_parms)
33789 parser->implicit_template_parms
33790 = TREE_CHAIN (parser->implicit_template_parms);
33791 else
33792 parser->implicit_template_parms = new_parm;
33794 tree new_type = TREE_TYPE (getdecls ());
33796 /* If creating a fully implicit function template, start the new implicit
33797 template parameter list with this synthesized type, otherwise grow the
33798 current template parameter list. */
33800 if (become_template)
33802 parent_scope->level_chain = current_binding_level;
33804 tree new_parms = make_tree_vec (1);
33805 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33806 current_template_parms = tree_cons (size_int (processing_template_decl),
33807 new_parms, current_template_parms);
33809 else
33811 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33812 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33813 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33814 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33817 current_binding_level = entry_scope;
33819 return new_type;
33822 /* Finish the declaration of a fully implicit function template. Such a
33823 template has no explicit template parameter list so has not been through the
33824 normal template head and tail processing. synthesize_implicit_template_parm
33825 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33826 provided if the declaration is a class member such that its template
33827 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33828 form is returned. Otherwise NULL_TREE is returned. */
33830 tree
33831 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33833 gcc_assert (parser->fully_implicit_function_template_p);
33835 if (member_decl_opt && member_decl_opt != error_mark_node
33836 && DECL_VIRTUAL_P (member_decl_opt))
33838 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33839 "implicit templates may not be %<virtual%>");
33840 DECL_VIRTUAL_P (member_decl_opt) = false;
33843 if (member_decl_opt)
33844 member_decl_opt = finish_member_template_decl (member_decl_opt);
33845 end_template_decl ();
33847 parser->fully_implicit_function_template_p = false;
33848 --parser->num_template_parameter_lists;
33850 return member_decl_opt;
33853 #include "gt-cp-parser.h"