/cp
[official-gcc.git] / gcc / cp / parser.c
blob35191a1cabc5889f31173fea832128ea2fba58ab
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_CXX0X
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
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 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6577 /* Look for the closing `]'. */
6578 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6580 /* Build the ARRAY_REF. */
6581 postfix_expression = grok_array_decl (loc, postfix_expression,
6582 index, decltype_p);
6584 /* When not doing offsetof, array references are not permitted in
6585 constant-expressions. */
6586 if (!for_offsetof
6587 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6588 postfix_expression = error_mark_node;
6590 return postfix_expression;
6593 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6594 by cp_parser_builtin_offsetof. We're looking for
6596 postfix-expression . template [opt] id-expression
6597 postfix-expression . pseudo-destructor-name
6598 postfix-expression -> template [opt] id-expression
6599 postfix-expression -> pseudo-destructor-name
6601 FOR_OFFSETOF is set if we're being called in that context. That sorta
6602 limits what of the above we'll actually accept, but nevermind.
6603 TOKEN_TYPE is the "." or "->" token, which will already have been
6604 removed from the stream. */
6606 static tree
6607 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6608 enum cpp_ttype token_type,
6609 tree postfix_expression,
6610 bool for_offsetof, cp_id_kind *idk,
6611 location_t location)
6613 tree name;
6614 bool dependent_p;
6615 bool pseudo_destructor_p;
6616 tree scope = NULL_TREE;
6618 /* If this is a `->' operator, dereference the pointer. */
6619 if (token_type == CPP_DEREF)
6620 postfix_expression = build_x_arrow (location, postfix_expression,
6621 tf_warning_or_error);
6622 /* Check to see whether or not the expression is type-dependent. */
6623 dependent_p = type_dependent_expression_p (postfix_expression);
6624 /* The identifier following the `->' or `.' is not qualified. */
6625 parser->scope = NULL_TREE;
6626 parser->qualifying_scope = NULL_TREE;
6627 parser->object_scope = NULL_TREE;
6628 *idk = CP_ID_KIND_NONE;
6630 /* Enter the scope corresponding to the type of the object
6631 given by the POSTFIX_EXPRESSION. */
6632 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6634 scope = TREE_TYPE (postfix_expression);
6635 /* According to the standard, no expression should ever have
6636 reference type. Unfortunately, we do not currently match
6637 the standard in this respect in that our internal representation
6638 of an expression may have reference type even when the standard
6639 says it does not. Therefore, we have to manually obtain the
6640 underlying type here. */
6641 scope = non_reference (scope);
6642 /* The type of the POSTFIX_EXPRESSION must be complete. */
6643 if (scope == unknown_type_node)
6645 error_at (location, "%qE does not have class type",
6646 postfix_expression);
6647 scope = NULL_TREE;
6649 /* Unlike the object expression in other contexts, *this is not
6650 required to be of complete type for purposes of class member
6651 access (5.2.5) outside the member function body. */
6652 else if (postfix_expression != current_class_ref
6653 && !(processing_template_decl && scope == current_class_type))
6654 scope = complete_type_or_else (scope, NULL_TREE);
6655 /* Let the name lookup machinery know that we are processing a
6656 class member access expression. */
6657 parser->context->object_type = scope;
6658 /* If something went wrong, we want to be able to discern that case,
6659 as opposed to the case where there was no SCOPE due to the type
6660 of expression being dependent. */
6661 if (!scope)
6662 scope = error_mark_node;
6663 /* If the SCOPE was erroneous, make the various semantic analysis
6664 functions exit quickly -- and without issuing additional error
6665 messages. */
6666 if (scope == error_mark_node)
6667 postfix_expression = error_mark_node;
6670 /* Assume this expression is not a pseudo-destructor access. */
6671 pseudo_destructor_p = false;
6673 /* If the SCOPE is a scalar type, then, if this is a valid program,
6674 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6675 is type dependent, it can be pseudo-destructor-name or something else.
6676 Try to parse it as pseudo-destructor-name first. */
6677 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6679 tree s;
6680 tree type;
6682 cp_parser_parse_tentatively (parser);
6683 /* Parse the pseudo-destructor-name. */
6684 s = NULL_TREE;
6685 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6686 &s, &type);
6687 if (dependent_p
6688 && (cp_parser_error_occurred (parser)
6689 || !SCALAR_TYPE_P (type)))
6690 cp_parser_abort_tentative_parse (parser);
6691 else if (cp_parser_parse_definitely (parser))
6693 pseudo_destructor_p = true;
6694 postfix_expression
6695 = finish_pseudo_destructor_expr (postfix_expression,
6696 s, type, location);
6700 if (!pseudo_destructor_p)
6702 /* If the SCOPE is not a scalar type, we are looking at an
6703 ordinary class member access expression, rather than a
6704 pseudo-destructor-name. */
6705 bool template_p;
6706 cp_token *token = cp_lexer_peek_token (parser->lexer);
6707 /* Parse the id-expression. */
6708 name = (cp_parser_id_expression
6709 (parser,
6710 cp_parser_optional_template_keyword (parser),
6711 /*check_dependency_p=*/true,
6712 &template_p,
6713 /*declarator_p=*/false,
6714 /*optional_p=*/false));
6715 /* In general, build a SCOPE_REF if the member name is qualified.
6716 However, if the name was not dependent and has already been
6717 resolved; there is no need to build the SCOPE_REF. For example;
6719 struct X { void f(); };
6720 template <typename T> void f(T* t) { t->X::f(); }
6722 Even though "t" is dependent, "X::f" is not and has been resolved
6723 to a BASELINK; there is no need to include scope information. */
6725 /* But we do need to remember that there was an explicit scope for
6726 virtual function calls. */
6727 if (parser->scope)
6728 *idk = CP_ID_KIND_QUALIFIED;
6730 /* If the name is a template-id that names a type, we will get a
6731 TYPE_DECL here. That is invalid code. */
6732 if (TREE_CODE (name) == TYPE_DECL)
6734 error_at (token->location, "invalid use of %qD", name);
6735 postfix_expression = error_mark_node;
6737 else
6739 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6741 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6743 error_at (token->location, "%<%D::%D%> is not a class member",
6744 parser->scope, name);
6745 postfix_expression = error_mark_node;
6747 else
6748 name = build_qualified_name (/*type=*/NULL_TREE,
6749 parser->scope,
6750 name,
6751 template_p);
6752 parser->scope = NULL_TREE;
6753 parser->qualifying_scope = NULL_TREE;
6754 parser->object_scope = NULL_TREE;
6756 if (parser->scope && name && BASELINK_P (name))
6757 adjust_result_of_qualified_name_lookup
6758 (name, parser->scope, scope);
6759 postfix_expression
6760 = finish_class_member_access_expr (postfix_expression, name,
6761 template_p,
6762 tf_warning_or_error);
6766 /* We no longer need to look up names in the scope of the object on
6767 the left-hand side of the `.' or `->' operator. */
6768 parser->context->object_type = NULL_TREE;
6770 /* Outside of offsetof, these operators may not appear in
6771 constant-expressions. */
6772 if (!for_offsetof
6773 && (cp_parser_non_integral_constant_expression
6774 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6775 postfix_expression = error_mark_node;
6777 return postfix_expression;
6780 /* Cache of LITERAL_ZERO_P constants. */
6782 static GTY(()) tree literal_zeros[itk_none];
6784 /* Parse a parenthesized expression-list.
6786 expression-list:
6787 assignment-expression
6788 expression-list, assignment-expression
6790 attribute-list:
6791 expression-list
6792 identifier
6793 identifier, expression-list
6795 CAST_P is true if this expression is the target of a cast.
6797 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6798 argument pack.
6800 Returns a vector of trees. Each element is a representation of an
6801 assignment-expression. NULL is returned if the ( and or ) are
6802 missing. An empty, but allocated, vector is returned on no
6803 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6804 if we are parsing an attribute list for an attribute that wants a
6805 plain identifier argument, normal_attr for an attribute that wants
6806 an expression, or non_attr if we aren't parsing an attribute list. If
6807 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6808 not all of the expressions in the list were constant.
6809 WANT_LITERAL_ZERO_P is true if the caller is interested in
6810 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6811 immediately, this can be removed. */
6813 static vec<tree, va_gc> *
6814 cp_parser_parenthesized_expression_list (cp_parser* parser,
6815 int is_attribute_list,
6816 bool cast_p,
6817 bool allow_expansion_p,
6818 bool *non_constant_p,
6819 bool want_literal_zero_p)
6821 vec<tree, va_gc> *expression_list;
6822 bool fold_expr_p = is_attribute_list != non_attr;
6823 tree identifier = NULL_TREE;
6824 bool saved_greater_than_is_operator_p;
6826 /* Assume all the expressions will be constant. */
6827 if (non_constant_p)
6828 *non_constant_p = false;
6830 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6831 return NULL;
6833 expression_list = make_tree_vector ();
6835 /* Within a parenthesized expression, a `>' token is always
6836 the greater-than operator. */
6837 saved_greater_than_is_operator_p
6838 = parser->greater_than_is_operator_p;
6839 parser->greater_than_is_operator_p = true;
6841 /* Consume expressions until there are no more. */
6842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6843 while (true)
6845 tree expr;
6847 /* At the beginning of attribute lists, check to see if the
6848 next token is an identifier. */
6849 if (is_attribute_list == id_attr
6850 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6852 cp_token *token;
6854 /* Consume the identifier. */
6855 token = cp_lexer_consume_token (parser->lexer);
6856 /* Save the identifier. */
6857 identifier = token->u.value;
6859 else
6861 bool expr_non_constant_p;
6863 /* Parse the next assignment-expression. */
6864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6866 /* A braced-init-list. */
6867 cp_lexer_set_source_position (parser->lexer);
6868 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6869 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6870 if (non_constant_p && expr_non_constant_p)
6871 *non_constant_p = true;
6873 else if (non_constant_p)
6875 expr = (cp_parser_constant_expression
6876 (parser, /*allow_non_constant_p=*/true,
6877 &expr_non_constant_p));
6878 if (expr_non_constant_p)
6879 *non_constant_p = true;
6881 else
6883 expr = NULL_TREE;
6884 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6885 switch (tok->type)
6887 case CPP_NUMBER:
6888 case CPP_CHAR:
6889 case CPP_WCHAR:
6890 case CPP_CHAR16:
6891 case CPP_CHAR32:
6892 case CPP_UTF8CHAR:
6893 /* If a parameter is literal zero alone, remember it
6894 for -Wmemset-transposed-args warning. */
6895 if (integer_zerop (tok->u.value)
6896 && !TREE_OVERFLOW (tok->u.value)
6897 && want_literal_zero_p
6898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6899 == CPP_COMMA
6900 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6901 == CPP_CLOSE_PAREN))
6903 unsigned int i;
6904 for (i = 0; i < itk_none; ++i)
6905 if (TREE_TYPE (tok->u.value) == integer_types[i])
6906 break;
6907 if (i < itk_none && literal_zeros[i])
6908 expr = literal_zeros[i];
6909 else
6911 expr = copy_node (tok->u.value);
6912 LITERAL_ZERO_P (expr) = 1;
6913 if (i < itk_none)
6914 literal_zeros[i] = expr;
6916 /* Consume the 0 token (or '\0', 0LL etc.). */
6917 cp_lexer_consume_token (parser->lexer);
6919 break;
6920 default:
6921 break;
6923 if (expr == NULL_TREE)
6924 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6925 cast_p);
6928 if (fold_expr_p)
6929 expr = instantiate_non_dependent_expr (expr);
6931 /* If we have an ellipsis, then this is an expression
6932 expansion. */
6933 if (allow_expansion_p
6934 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6936 /* Consume the `...'. */
6937 cp_lexer_consume_token (parser->lexer);
6939 /* Build the argument pack. */
6940 expr = make_pack_expansion (expr);
6943 /* Add it to the list. We add error_mark_node
6944 expressions to the list, so that we can still tell if
6945 the correct form for a parenthesized expression-list
6946 is found. That gives better errors. */
6947 vec_safe_push (expression_list, expr);
6949 if (expr == error_mark_node)
6950 goto skip_comma;
6953 /* After the first item, attribute lists look the same as
6954 expression lists. */
6955 is_attribute_list = non_attr;
6957 get_comma:;
6958 /* If the next token isn't a `,', then we are done. */
6959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6960 break;
6962 /* Otherwise, consume the `,' and keep going. */
6963 cp_lexer_consume_token (parser->lexer);
6966 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6968 int ending;
6970 skip_comma:;
6971 /* We try and resync to an unnested comma, as that will give the
6972 user better diagnostics. */
6973 ending = cp_parser_skip_to_closing_parenthesis (parser,
6974 /*recovering=*/true,
6975 /*or_comma=*/true,
6976 /*consume_paren=*/true);
6977 if (ending < 0)
6978 goto get_comma;
6979 if (!ending)
6981 parser->greater_than_is_operator_p
6982 = saved_greater_than_is_operator_p;
6983 return NULL;
6987 parser->greater_than_is_operator_p
6988 = saved_greater_than_is_operator_p;
6990 if (identifier)
6991 vec_safe_insert (expression_list, 0, identifier);
6993 return expression_list;
6996 /* Parse a pseudo-destructor-name.
6998 pseudo-destructor-name:
6999 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7000 :: [opt] nested-name-specifier template template-id :: ~ type-name
7001 :: [opt] nested-name-specifier [opt] ~ type-name
7003 If either of the first two productions is used, sets *SCOPE to the
7004 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7005 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7006 or ERROR_MARK_NODE if the parse fails. */
7008 static void
7009 cp_parser_pseudo_destructor_name (cp_parser* parser,
7010 tree object,
7011 tree* scope,
7012 tree* type)
7014 bool nested_name_specifier_p;
7016 /* Handle ~auto. */
7017 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7018 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7019 && !type_dependent_expression_p (object))
7021 if (cxx_dialect < cxx14)
7022 pedwarn (input_location, 0,
7023 "%<~auto%> only available with "
7024 "-std=c++14 or -std=gnu++14");
7025 cp_lexer_consume_token (parser->lexer);
7026 cp_lexer_consume_token (parser->lexer);
7027 *scope = NULL_TREE;
7028 *type = TREE_TYPE (object);
7029 return;
7032 /* Assume that things will not work out. */
7033 *type = error_mark_node;
7035 /* Look for the optional `::' operator. */
7036 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7037 /* Look for the optional nested-name-specifier. */
7038 nested_name_specifier_p
7039 = (cp_parser_nested_name_specifier_opt (parser,
7040 /*typename_keyword_p=*/false,
7041 /*check_dependency_p=*/true,
7042 /*type_p=*/false,
7043 /*is_declaration=*/false)
7044 != NULL_TREE);
7045 /* Now, if we saw a nested-name-specifier, we might be doing the
7046 second production. */
7047 if (nested_name_specifier_p
7048 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7050 /* Consume the `template' keyword. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Parse the template-id. */
7053 cp_parser_template_id (parser,
7054 /*template_keyword_p=*/true,
7055 /*check_dependency_p=*/false,
7056 class_type,
7057 /*is_declaration=*/true);
7058 /* Look for the `::' token. */
7059 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7061 /* If the next token is not a `~', then there might be some
7062 additional qualification. */
7063 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7065 /* At this point, we're looking for "type-name :: ~". The type-name
7066 must not be a class-name, since this is a pseudo-destructor. So,
7067 it must be either an enum-name, or a typedef-name -- both of which
7068 are just identifiers. So, we peek ahead to check that the "::"
7069 and "~" tokens are present; if they are not, then we can avoid
7070 calling type_name. */
7071 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7072 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7073 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7075 cp_parser_error (parser, "non-scalar type");
7076 return;
7079 /* Look for the type-name. */
7080 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7081 if (*scope == error_mark_node)
7082 return;
7084 /* Look for the `::' token. */
7085 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7087 else
7088 *scope = NULL_TREE;
7090 /* Look for the `~'. */
7091 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7093 /* Once we see the ~, this has to be a pseudo-destructor. */
7094 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7095 cp_parser_commit_to_topmost_tentative_parse (parser);
7097 /* Look for the type-name again. We are not responsible for
7098 checking that it matches the first type-name. */
7099 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7102 /* Parse a unary-expression.
7104 unary-expression:
7105 postfix-expression
7106 ++ cast-expression
7107 -- cast-expression
7108 unary-operator cast-expression
7109 sizeof unary-expression
7110 sizeof ( type-id )
7111 alignof ( type-id ) [C++0x]
7112 new-expression
7113 delete-expression
7115 GNU Extensions:
7117 unary-expression:
7118 __extension__ cast-expression
7119 __alignof__ unary-expression
7120 __alignof__ ( type-id )
7121 alignof unary-expression [C++0x]
7122 __real__ cast-expression
7123 __imag__ cast-expression
7124 && identifier
7125 sizeof ( type-id ) { initializer-list , [opt] }
7126 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7127 __alignof__ ( type-id ) { initializer-list , [opt] }
7129 ADDRESS_P is true iff the unary-expression is appearing as the
7130 operand of the `&' operator. CAST_P is true if this expression is
7131 the target of a cast.
7133 Returns a representation of the expression. */
7135 static tree
7136 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7137 bool address_p, bool cast_p, bool decltype_p)
7139 cp_token *token;
7140 enum tree_code unary_operator;
7142 /* Peek at the next token. */
7143 token = cp_lexer_peek_token (parser->lexer);
7144 /* Some keywords give away the kind of expression. */
7145 if (token->type == CPP_KEYWORD)
7147 enum rid keyword = token->keyword;
7149 switch (keyword)
7151 case RID_ALIGNOF:
7152 case RID_SIZEOF:
7154 tree operand, ret;
7155 enum tree_code op;
7156 location_t first_loc;
7158 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7159 /* Consume the token. */
7160 cp_lexer_consume_token (parser->lexer);
7161 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7162 /* Parse the operand. */
7163 operand = cp_parser_sizeof_operand (parser, keyword);
7165 if (TYPE_P (operand))
7166 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7167 else
7169 /* ISO C++ defines alignof only with types, not with
7170 expressions. So pedwarn if alignof is used with a non-
7171 type expression. However, __alignof__ is ok. */
7172 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7173 pedwarn (token->location, OPT_Wpedantic,
7174 "ISO C++ does not allow %<alignof%> "
7175 "with a non-type");
7177 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7179 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7180 SIZEOF_EXPR with the original operand. */
7181 if (op == SIZEOF_EXPR && ret != error_mark_node)
7183 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7185 if (!processing_template_decl && TYPE_P (operand))
7187 ret = build_min (SIZEOF_EXPR, size_type_node,
7188 build1 (NOP_EXPR, operand,
7189 error_mark_node));
7190 SIZEOF_EXPR_TYPE_P (ret) = 1;
7192 else
7193 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7194 TREE_SIDE_EFFECTS (ret) = 0;
7195 TREE_READONLY (ret) = 1;
7197 SET_EXPR_LOCATION (ret, first_loc);
7199 return ret;
7202 case RID_NEW:
7203 return cp_parser_new_expression (parser);
7205 case RID_DELETE:
7206 return cp_parser_delete_expression (parser);
7208 case RID_EXTENSION:
7210 /* The saved value of the PEDANTIC flag. */
7211 int saved_pedantic;
7212 tree expr;
7214 /* Save away the PEDANTIC flag. */
7215 cp_parser_extension_opt (parser, &saved_pedantic);
7216 /* Parse the cast-expression. */
7217 expr = cp_parser_simple_cast_expression (parser);
7218 /* Restore the PEDANTIC flag. */
7219 pedantic = saved_pedantic;
7221 return expr;
7224 case RID_REALPART:
7225 case RID_IMAGPART:
7227 tree expression;
7229 /* Consume the `__real__' or `__imag__' token. */
7230 cp_lexer_consume_token (parser->lexer);
7231 /* Parse the cast-expression. */
7232 expression = cp_parser_simple_cast_expression (parser);
7233 /* Create the complete representation. */
7234 return build_x_unary_op (token->location,
7235 (keyword == RID_REALPART
7236 ? REALPART_EXPR : IMAGPART_EXPR),
7237 expression,
7238 tf_warning_or_error);
7240 break;
7242 case RID_TRANSACTION_ATOMIC:
7243 case RID_TRANSACTION_RELAXED:
7244 return cp_parser_transaction_expression (parser, keyword);
7246 case RID_NOEXCEPT:
7248 tree expr;
7249 const char *saved_message;
7250 bool saved_integral_constant_expression_p;
7251 bool saved_non_integral_constant_expression_p;
7252 bool saved_greater_than_is_operator_p;
7254 cp_lexer_consume_token (parser->lexer);
7255 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7257 saved_message = parser->type_definition_forbidden_message;
7258 parser->type_definition_forbidden_message
7259 = G_("types may not be defined in %<noexcept%> expressions");
7261 saved_integral_constant_expression_p
7262 = parser->integral_constant_expression_p;
7263 saved_non_integral_constant_expression_p
7264 = parser->non_integral_constant_expression_p;
7265 parser->integral_constant_expression_p = false;
7267 saved_greater_than_is_operator_p
7268 = parser->greater_than_is_operator_p;
7269 parser->greater_than_is_operator_p = true;
7271 ++cp_unevaluated_operand;
7272 ++c_inhibit_evaluation_warnings;
7273 ++cp_noexcept_operand;
7274 expr = cp_parser_expression (parser);
7275 --cp_noexcept_operand;
7276 --c_inhibit_evaluation_warnings;
7277 --cp_unevaluated_operand;
7279 parser->greater_than_is_operator_p
7280 = saved_greater_than_is_operator_p;
7282 parser->integral_constant_expression_p
7283 = saved_integral_constant_expression_p;
7284 parser->non_integral_constant_expression_p
7285 = saved_non_integral_constant_expression_p;
7287 parser->type_definition_forbidden_message = saved_message;
7289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7290 return finish_noexcept_expr (expr, tf_warning_or_error);
7293 default:
7294 break;
7298 /* Look for the `:: new' and `:: delete', which also signal the
7299 beginning of a new-expression, or delete-expression,
7300 respectively. If the next token is `::', then it might be one of
7301 these. */
7302 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7304 enum rid keyword;
7306 /* See if the token after the `::' is one of the keywords in
7307 which we're interested. */
7308 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7309 /* If it's `new', we have a new-expression. */
7310 if (keyword == RID_NEW)
7311 return cp_parser_new_expression (parser);
7312 /* Similarly, for `delete'. */
7313 else if (keyword == RID_DELETE)
7314 return cp_parser_delete_expression (parser);
7317 /* Look for a unary operator. */
7318 unary_operator = cp_parser_unary_operator (token);
7319 /* The `++' and `--' operators can be handled similarly, even though
7320 they are not technically unary-operators in the grammar. */
7321 if (unary_operator == ERROR_MARK)
7323 if (token->type == CPP_PLUS_PLUS)
7324 unary_operator = PREINCREMENT_EXPR;
7325 else if (token->type == CPP_MINUS_MINUS)
7326 unary_operator = PREDECREMENT_EXPR;
7327 /* Handle the GNU address-of-label extension. */
7328 else if (cp_parser_allow_gnu_extensions_p (parser)
7329 && token->type == CPP_AND_AND)
7331 tree identifier;
7332 tree expression;
7333 location_t loc = token->location;
7335 /* Consume the '&&' token. */
7336 cp_lexer_consume_token (parser->lexer);
7337 /* Look for the identifier. */
7338 identifier = cp_parser_identifier (parser);
7339 /* Create an expression representing the address. */
7340 expression = finish_label_address_expr (identifier, loc);
7341 if (cp_parser_non_integral_constant_expression (parser,
7342 NIC_ADDR_LABEL))
7343 expression = error_mark_node;
7344 return expression;
7347 if (unary_operator != ERROR_MARK)
7349 tree cast_expression;
7350 tree expression = error_mark_node;
7351 non_integral_constant non_constant_p = NIC_NONE;
7352 location_t loc = token->location;
7353 tsubst_flags_t complain = complain_flags (decltype_p);
7355 /* Consume the operator token. */
7356 token = cp_lexer_consume_token (parser->lexer);
7357 /* Parse the cast-expression. */
7358 cast_expression
7359 = cp_parser_cast_expression (parser,
7360 unary_operator == ADDR_EXPR,
7361 /*cast_p=*/false,
7362 /*decltype*/false,
7363 pidk);
7364 /* Now, build an appropriate representation. */
7365 switch (unary_operator)
7367 case INDIRECT_REF:
7368 non_constant_p = NIC_STAR;
7369 expression = build_x_indirect_ref (loc, cast_expression,
7370 RO_UNARY_STAR,
7371 complain);
7372 break;
7374 case ADDR_EXPR:
7375 non_constant_p = NIC_ADDR;
7376 /* Fall through. */
7377 case BIT_NOT_EXPR:
7378 expression = build_x_unary_op (loc, unary_operator,
7379 cast_expression,
7380 complain);
7381 break;
7383 case PREINCREMENT_EXPR:
7384 case PREDECREMENT_EXPR:
7385 non_constant_p = unary_operator == PREINCREMENT_EXPR
7386 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7387 /* Fall through. */
7388 case UNARY_PLUS_EXPR:
7389 case NEGATE_EXPR:
7390 case TRUTH_NOT_EXPR:
7391 expression = finish_unary_op_expr (loc, unary_operator,
7392 cast_expression, complain);
7393 break;
7395 default:
7396 gcc_unreachable ();
7399 if (non_constant_p != NIC_NONE
7400 && cp_parser_non_integral_constant_expression (parser,
7401 non_constant_p))
7402 expression = error_mark_node;
7404 return expression;
7407 return cp_parser_postfix_expression (parser, address_p, cast_p,
7408 /*member_access_only_p=*/false,
7409 decltype_p,
7410 pidk);
7413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7414 unary-operator, the corresponding tree code is returned. */
7416 static enum tree_code
7417 cp_parser_unary_operator (cp_token* token)
7419 switch (token->type)
7421 case CPP_MULT:
7422 return INDIRECT_REF;
7424 case CPP_AND:
7425 return ADDR_EXPR;
7427 case CPP_PLUS:
7428 return UNARY_PLUS_EXPR;
7430 case CPP_MINUS:
7431 return NEGATE_EXPR;
7433 case CPP_NOT:
7434 return TRUTH_NOT_EXPR;
7436 case CPP_COMPL:
7437 return BIT_NOT_EXPR;
7439 default:
7440 return ERROR_MARK;
7444 /* Parse a new-expression.
7446 new-expression:
7447 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7448 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7450 Returns a representation of the expression. */
7452 static tree
7453 cp_parser_new_expression (cp_parser* parser)
7455 bool global_scope_p;
7456 vec<tree, va_gc> *placement;
7457 tree type;
7458 vec<tree, va_gc> *initializer;
7459 tree nelts = NULL_TREE;
7460 tree ret;
7462 /* Look for the optional `::' operator. */
7463 global_scope_p
7464 = (cp_parser_global_scope_opt (parser,
7465 /*current_scope_valid_p=*/false)
7466 != NULL_TREE);
7467 /* Look for the `new' operator. */
7468 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7469 /* There's no easy way to tell a new-placement from the
7470 `( type-id )' construct. */
7471 cp_parser_parse_tentatively (parser);
7472 /* Look for a new-placement. */
7473 placement = cp_parser_new_placement (parser);
7474 /* If that didn't work out, there's no new-placement. */
7475 if (!cp_parser_parse_definitely (parser))
7477 if (placement != NULL)
7478 release_tree_vector (placement);
7479 placement = NULL;
7482 /* If the next token is a `(', then we have a parenthesized
7483 type-id. */
7484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7486 cp_token *token;
7487 const char *saved_message = parser->type_definition_forbidden_message;
7489 /* Consume the `('. */
7490 cp_lexer_consume_token (parser->lexer);
7492 /* Parse the type-id. */
7493 parser->type_definition_forbidden_message
7494 = G_("types may not be defined in a new-expression");
7495 type = cp_parser_type_id (parser);
7496 parser->type_definition_forbidden_message = saved_message;
7498 /* Look for the closing `)'. */
7499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7500 token = cp_lexer_peek_token (parser->lexer);
7501 /* There should not be a direct-new-declarator in this production,
7502 but GCC used to allowed this, so we check and emit a sensible error
7503 message for this case. */
7504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7506 error_at (token->location,
7507 "array bound forbidden after parenthesized type-id");
7508 inform (token->location,
7509 "try removing the parentheses around the type-id");
7510 cp_parser_direct_new_declarator (parser);
7513 /* Otherwise, there must be a new-type-id. */
7514 else
7515 type = cp_parser_new_type_id (parser, &nelts);
7517 /* If the next token is a `(' or '{', then we have a new-initializer. */
7518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7519 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7520 initializer = cp_parser_new_initializer (parser);
7521 else
7522 initializer = NULL;
7524 /* A new-expression may not appear in an integral constant
7525 expression. */
7526 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7527 ret = error_mark_node;
7528 else
7530 /* Create a representation of the new-expression. */
7531 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7532 tf_warning_or_error);
7535 if (placement != NULL)
7536 release_tree_vector (placement);
7537 if (initializer != NULL)
7538 release_tree_vector (initializer);
7540 return ret;
7543 /* Parse a new-placement.
7545 new-placement:
7546 ( expression-list )
7548 Returns the same representation as for an expression-list. */
7550 static vec<tree, va_gc> *
7551 cp_parser_new_placement (cp_parser* parser)
7553 vec<tree, va_gc> *expression_list;
7555 /* Parse the expression-list. */
7556 expression_list = (cp_parser_parenthesized_expression_list
7557 (parser, non_attr, /*cast_p=*/false,
7558 /*allow_expansion_p=*/true,
7559 /*non_constant_p=*/NULL));
7561 if (expression_list && expression_list->is_empty ())
7562 error ("expected expression-list or type-id");
7564 return expression_list;
7567 /* Parse a new-type-id.
7569 new-type-id:
7570 type-specifier-seq new-declarator [opt]
7572 Returns the TYPE allocated. If the new-type-id indicates an array
7573 type, *NELTS is set to the number of elements in the last array
7574 bound; the TYPE will not include the last array bound. */
7576 static tree
7577 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7579 cp_decl_specifier_seq type_specifier_seq;
7580 cp_declarator *new_declarator;
7581 cp_declarator *declarator;
7582 cp_declarator *outer_declarator;
7583 const char *saved_message;
7585 /* The type-specifier sequence must not contain type definitions.
7586 (It cannot contain declarations of new types either, but if they
7587 are not definitions we will catch that because they are not
7588 complete.) */
7589 saved_message = parser->type_definition_forbidden_message;
7590 parser->type_definition_forbidden_message
7591 = G_("types may not be defined in a new-type-id");
7592 /* Parse the type-specifier-seq. */
7593 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7594 /*is_trailing_return=*/false,
7595 &type_specifier_seq);
7596 /* Restore the old message. */
7597 parser->type_definition_forbidden_message = saved_message;
7599 if (type_specifier_seq.type == error_mark_node)
7600 return error_mark_node;
7602 /* Parse the new-declarator. */
7603 new_declarator = cp_parser_new_declarator_opt (parser);
7605 /* Determine the number of elements in the last array dimension, if
7606 any. */
7607 *nelts = NULL_TREE;
7608 /* Skip down to the last array dimension. */
7609 declarator = new_declarator;
7610 outer_declarator = NULL;
7611 while (declarator && (declarator->kind == cdk_pointer
7612 || declarator->kind == cdk_ptrmem))
7614 outer_declarator = declarator;
7615 declarator = declarator->declarator;
7617 while (declarator
7618 && declarator->kind == cdk_array
7619 && declarator->declarator
7620 && declarator->declarator->kind == cdk_array)
7622 outer_declarator = declarator;
7623 declarator = declarator->declarator;
7626 if (declarator && declarator->kind == cdk_array)
7628 *nelts = declarator->u.array.bounds;
7629 if (*nelts == error_mark_node)
7630 *nelts = integer_one_node;
7632 if (outer_declarator)
7633 outer_declarator->declarator = declarator->declarator;
7634 else
7635 new_declarator = NULL;
7638 return groktypename (&type_specifier_seq, new_declarator, false);
7641 /* Parse an (optional) new-declarator.
7643 new-declarator:
7644 ptr-operator new-declarator [opt]
7645 direct-new-declarator
7647 Returns the declarator. */
7649 static cp_declarator *
7650 cp_parser_new_declarator_opt (cp_parser* parser)
7652 enum tree_code code;
7653 tree type, std_attributes = NULL_TREE;
7654 cp_cv_quals cv_quals;
7656 /* We don't know if there's a ptr-operator next, or not. */
7657 cp_parser_parse_tentatively (parser);
7658 /* Look for a ptr-operator. */
7659 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7660 /* If that worked, look for more new-declarators. */
7661 if (cp_parser_parse_definitely (parser))
7663 cp_declarator *declarator;
7665 /* Parse another optional declarator. */
7666 declarator = cp_parser_new_declarator_opt (parser);
7668 declarator = cp_parser_make_indirect_declarator
7669 (code, type, cv_quals, declarator, std_attributes);
7671 return declarator;
7674 /* If the next token is a `[', there is a direct-new-declarator. */
7675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7676 return cp_parser_direct_new_declarator (parser);
7678 return NULL;
7681 /* Parse a direct-new-declarator.
7683 direct-new-declarator:
7684 [ expression ]
7685 direct-new-declarator [constant-expression]
7689 static cp_declarator *
7690 cp_parser_direct_new_declarator (cp_parser* parser)
7692 cp_declarator *declarator = NULL;
7694 while (true)
7696 tree expression;
7697 cp_token *token;
7699 /* Look for the opening `['. */
7700 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7702 token = cp_lexer_peek_token (parser->lexer);
7703 expression = cp_parser_expression (parser);
7704 /* The standard requires that the expression have integral
7705 type. DR 74 adds enumeration types. We believe that the
7706 real intent is that these expressions be handled like the
7707 expression in a `switch' condition, which also allows
7708 classes with a single conversion to integral or
7709 enumeration type. */
7710 if (!processing_template_decl)
7712 expression
7713 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7714 expression,
7715 /*complain=*/true);
7716 if (!expression)
7718 error_at (token->location,
7719 "expression in new-declarator must have integral "
7720 "or enumeration type");
7721 expression = error_mark_node;
7725 /* Look for the closing `]'. */
7726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7728 /* Add this bound to the declarator. */
7729 declarator = make_array_declarator (declarator, expression);
7731 /* If the next token is not a `[', then there are no more
7732 bounds. */
7733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7734 break;
7737 return declarator;
7740 /* Parse a new-initializer.
7742 new-initializer:
7743 ( expression-list [opt] )
7744 braced-init-list
7746 Returns a representation of the expression-list. */
7748 static vec<tree, va_gc> *
7749 cp_parser_new_initializer (cp_parser* parser)
7751 vec<tree, va_gc> *expression_list;
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7755 tree t;
7756 bool expr_non_constant_p;
7757 cp_lexer_set_source_position (parser->lexer);
7758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7759 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7760 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7761 expression_list = make_tree_vector_single (t);
7763 else
7764 expression_list = (cp_parser_parenthesized_expression_list
7765 (parser, non_attr, /*cast_p=*/false,
7766 /*allow_expansion_p=*/true,
7767 /*non_constant_p=*/NULL));
7769 return expression_list;
7772 /* Parse a delete-expression.
7774 delete-expression:
7775 :: [opt] delete cast-expression
7776 :: [opt] delete [ ] cast-expression
7778 Returns a representation of the expression. */
7780 static tree
7781 cp_parser_delete_expression (cp_parser* parser)
7783 bool global_scope_p;
7784 bool array_p;
7785 tree expression;
7787 /* Look for the optional `::' operator. */
7788 global_scope_p
7789 = (cp_parser_global_scope_opt (parser,
7790 /*current_scope_valid_p=*/false)
7791 != NULL_TREE);
7792 /* Look for the `delete' keyword. */
7793 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7794 /* See if the array syntax is in use. */
7795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7797 /* Consume the `[' token. */
7798 cp_lexer_consume_token (parser->lexer);
7799 /* Look for the `]' token. */
7800 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7801 /* Remember that this is the `[]' construct. */
7802 array_p = true;
7804 else
7805 array_p = false;
7807 /* Parse the cast-expression. */
7808 expression = cp_parser_simple_cast_expression (parser);
7810 /* A delete-expression may not appear in an integral constant
7811 expression. */
7812 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7813 return error_mark_node;
7815 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7816 tf_warning_or_error);
7819 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7820 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7821 0 otherwise. */
7823 static int
7824 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7826 cp_token *token = cp_lexer_peek_token (parser->lexer);
7827 switch (token->type)
7829 case CPP_COMMA:
7830 case CPP_SEMICOLON:
7831 case CPP_QUERY:
7832 case CPP_COLON:
7833 case CPP_CLOSE_SQUARE:
7834 case CPP_CLOSE_PAREN:
7835 case CPP_CLOSE_BRACE:
7836 case CPP_OPEN_BRACE:
7837 case CPP_DOT:
7838 case CPP_DOT_STAR:
7839 case CPP_DEREF:
7840 case CPP_DEREF_STAR:
7841 case CPP_DIV:
7842 case CPP_MOD:
7843 case CPP_LSHIFT:
7844 case CPP_RSHIFT:
7845 case CPP_LESS:
7846 case CPP_GREATER:
7847 case CPP_LESS_EQ:
7848 case CPP_GREATER_EQ:
7849 case CPP_EQ_EQ:
7850 case CPP_NOT_EQ:
7851 case CPP_EQ:
7852 case CPP_MULT_EQ:
7853 case CPP_DIV_EQ:
7854 case CPP_MOD_EQ:
7855 case CPP_PLUS_EQ:
7856 case CPP_MINUS_EQ:
7857 case CPP_RSHIFT_EQ:
7858 case CPP_LSHIFT_EQ:
7859 case CPP_AND_EQ:
7860 case CPP_XOR_EQ:
7861 case CPP_OR_EQ:
7862 case CPP_XOR:
7863 case CPP_OR:
7864 case CPP_OR_OR:
7865 case CPP_EOF:
7866 case CPP_ELLIPSIS:
7867 return 0;
7869 case CPP_OPEN_PAREN:
7870 /* In ((type ()) () the last () isn't a valid cast-expression,
7871 so the whole must be parsed as postfix-expression. */
7872 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7873 != CPP_CLOSE_PAREN;
7875 case CPP_OPEN_SQUARE:
7876 /* '[' may start a primary-expression in obj-c++ and in C++11,
7877 as a lambda-expression, eg, '(void)[]{}'. */
7878 if (cxx_dialect >= cxx11)
7879 return -1;
7880 return c_dialect_objc ();
7882 case CPP_PLUS_PLUS:
7883 case CPP_MINUS_MINUS:
7884 /* '++' and '--' may or may not start a cast-expression:
7886 struct T { void operator++(int); };
7887 void f() { (T())++; }
7891 int a;
7892 (int)++a; */
7893 return -1;
7895 default:
7896 return 1;
7900 /* Parse a cast-expression.
7902 cast-expression:
7903 unary-expression
7904 ( type-id ) cast-expression
7906 ADDRESS_P is true iff the unary-expression is appearing as the
7907 operand of the `&' operator. CAST_P is true if this expression is
7908 the target of a cast.
7910 Returns a representation of the expression. */
7912 static tree
7913 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7914 bool decltype_p, cp_id_kind * pidk)
7916 /* If it's a `(', then we might be looking at a cast. */
7917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7919 tree type = NULL_TREE;
7920 tree expr = NULL_TREE;
7921 int cast_expression = 0;
7922 const char *saved_message;
7924 /* There's no way to know yet whether or not this is a cast.
7925 For example, `(int (3))' is a unary-expression, while `(int)
7926 3' is a cast. So, we resort to parsing tentatively. */
7927 cp_parser_parse_tentatively (parser);
7928 /* Types may not be defined in a cast. */
7929 saved_message = parser->type_definition_forbidden_message;
7930 parser->type_definition_forbidden_message
7931 = G_("types may not be defined in casts");
7932 /* Consume the `('. */
7933 cp_lexer_consume_token (parser->lexer);
7934 /* A very tricky bit is that `(struct S) { 3 }' is a
7935 compound-literal (which we permit in C++ as an extension).
7936 But, that construct is not a cast-expression -- it is a
7937 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7938 is legal; if the compound-literal were a cast-expression,
7939 you'd need an extra set of parentheses.) But, if we parse
7940 the type-id, and it happens to be a class-specifier, then we
7941 will commit to the parse at that point, because we cannot
7942 undo the action that is done when creating a new class. So,
7943 then we cannot back up and do a postfix-expression.
7945 Another tricky case is the following (c++/29234):
7947 struct S { void operator () (); };
7949 void foo ()
7951 ( S()() );
7954 As a type-id we parse the parenthesized S()() as a function
7955 returning a function, groktypename complains and we cannot
7956 back up in this case either.
7958 Therefore, we scan ahead to the closing `)', and check to see
7959 if the tokens after the `)' can start a cast-expression. Otherwise
7960 we are dealing with an unary-expression, a postfix-expression
7961 or something else.
7963 Yet another tricky case, in C++11, is the following (c++/54891):
7965 (void)[]{};
7967 The issue is that usually, besides the case of lambda-expressions,
7968 the parenthesized type-id cannot be followed by '[', and, eg, we
7969 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7970 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7971 we don't commit, we try a cast-expression, then an unary-expression.
7973 Save tokens so that we can put them back. */
7974 cp_lexer_save_tokens (parser->lexer);
7976 /* We may be looking at a cast-expression. */
7977 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7978 /*consume_paren=*/true))
7979 cast_expression
7980 = cp_parser_tokens_start_cast_expression (parser);
7982 /* Roll back the tokens we skipped. */
7983 cp_lexer_rollback_tokens (parser->lexer);
7984 /* If we aren't looking at a cast-expression, simulate an error so
7985 that the call to cp_parser_error_occurred below returns true. */
7986 if (!cast_expression)
7987 cp_parser_simulate_error (parser);
7988 else
7990 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7991 parser->in_type_id_in_expr_p = true;
7992 /* Look for the type-id. */
7993 type = cp_parser_type_id (parser);
7994 /* Look for the closing `)'. */
7995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7996 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7999 /* Restore the saved message. */
8000 parser->type_definition_forbidden_message = saved_message;
8002 /* At this point this can only be either a cast or a
8003 parenthesized ctor such as `(T ())' that looks like a cast to
8004 function returning T. */
8005 if (!cp_parser_error_occurred (parser))
8007 /* Only commit if the cast-expression doesn't start with
8008 '++', '--', or '[' in C++11. */
8009 if (cast_expression > 0)
8010 cp_parser_commit_to_topmost_tentative_parse (parser);
8012 expr = cp_parser_cast_expression (parser,
8013 /*address_p=*/false,
8014 /*cast_p=*/true,
8015 /*decltype_p=*/false,
8016 pidk);
8018 if (cp_parser_parse_definitely (parser))
8020 /* Warn about old-style casts, if so requested. */
8021 if (warn_old_style_cast
8022 && !in_system_header_at (input_location)
8023 && !VOID_TYPE_P (type)
8024 && current_lang_name != lang_name_c)
8025 warning (OPT_Wold_style_cast, "use of old-style cast");
8027 /* Only type conversions to integral or enumeration types
8028 can be used in constant-expressions. */
8029 if (!cast_valid_in_integral_constant_expression_p (type)
8030 && cp_parser_non_integral_constant_expression (parser,
8031 NIC_CAST))
8032 return error_mark_node;
8034 /* Perform the cast. */
8035 expr = build_c_cast (input_location, type, expr);
8036 return expr;
8039 else
8040 cp_parser_abort_tentative_parse (parser);
8043 /* If we get here, then it's not a cast, so it must be a
8044 unary-expression. */
8045 return cp_parser_unary_expression (parser, pidk, address_p,
8046 cast_p, decltype_p);
8049 /* Parse a binary expression of the general form:
8051 pm-expression:
8052 cast-expression
8053 pm-expression .* cast-expression
8054 pm-expression ->* cast-expression
8056 multiplicative-expression:
8057 pm-expression
8058 multiplicative-expression * pm-expression
8059 multiplicative-expression / pm-expression
8060 multiplicative-expression % pm-expression
8062 additive-expression:
8063 multiplicative-expression
8064 additive-expression + multiplicative-expression
8065 additive-expression - multiplicative-expression
8067 shift-expression:
8068 additive-expression
8069 shift-expression << additive-expression
8070 shift-expression >> additive-expression
8072 relational-expression:
8073 shift-expression
8074 relational-expression < shift-expression
8075 relational-expression > shift-expression
8076 relational-expression <= shift-expression
8077 relational-expression >= shift-expression
8079 GNU Extension:
8081 relational-expression:
8082 relational-expression <? shift-expression
8083 relational-expression >? shift-expression
8085 equality-expression:
8086 relational-expression
8087 equality-expression == relational-expression
8088 equality-expression != relational-expression
8090 and-expression:
8091 equality-expression
8092 and-expression & equality-expression
8094 exclusive-or-expression:
8095 and-expression
8096 exclusive-or-expression ^ and-expression
8098 inclusive-or-expression:
8099 exclusive-or-expression
8100 inclusive-or-expression | exclusive-or-expression
8102 logical-and-expression:
8103 inclusive-or-expression
8104 logical-and-expression && inclusive-or-expression
8106 logical-or-expression:
8107 logical-and-expression
8108 logical-or-expression || logical-and-expression
8110 All these are implemented with a single function like:
8112 binary-expression:
8113 simple-cast-expression
8114 binary-expression <token> binary-expression
8116 CAST_P is true if this expression is the target of a cast.
8118 The binops_by_token map is used to get the tree codes for each <token> type.
8119 binary-expressions are associated according to a precedence table. */
8121 #define TOKEN_PRECEDENCE(token) \
8122 (((token->type == CPP_GREATER \
8123 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8124 && !parser->greater_than_is_operator_p) \
8125 ? PREC_NOT_OPERATOR \
8126 : binops_by_token[token->type].prec)
8128 static tree
8129 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8130 bool no_toplevel_fold_p,
8131 bool decltype_p,
8132 enum cp_parser_prec prec,
8133 cp_id_kind * pidk)
8135 cp_parser_expression_stack stack;
8136 cp_parser_expression_stack_entry *sp = &stack[0];
8137 cp_parser_expression_stack_entry current;
8138 tree rhs;
8139 cp_token *token;
8140 enum tree_code rhs_type;
8141 enum cp_parser_prec new_prec, lookahead_prec;
8142 tree overload;
8144 /* Parse the first expression. */
8145 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8146 ? TRUTH_NOT_EXPR : ERROR_MARK);
8147 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8148 cast_p, decltype_p, pidk);
8149 current.prec = prec;
8151 if (cp_parser_error_occurred (parser))
8152 return error_mark_node;
8154 for (;;)
8156 /* Get an operator token. */
8157 token = cp_lexer_peek_token (parser->lexer);
8159 if (warn_cxx11_compat
8160 && token->type == CPP_RSHIFT
8161 && !parser->greater_than_is_operator_p)
8163 if (warning_at (token->location, OPT_Wc__11_compat,
8164 "%<>>%> operator is treated"
8165 " as two right angle brackets in C++11"))
8166 inform (token->location,
8167 "suggest parentheses around %<>>%> expression");
8170 new_prec = TOKEN_PRECEDENCE (token);
8172 /* Popping an entry off the stack means we completed a subexpression:
8173 - either we found a token which is not an operator (`>' where it is not
8174 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8175 will happen repeatedly;
8176 - or, we found an operator which has lower priority. This is the case
8177 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8178 parsing `3 * 4'. */
8179 if (new_prec <= current.prec)
8181 if (sp == stack)
8182 break;
8183 else
8184 goto pop;
8187 get_rhs:
8188 current.tree_type = binops_by_token[token->type].tree_type;
8189 current.loc = token->location;
8191 /* We used the operator token. */
8192 cp_lexer_consume_token (parser->lexer);
8194 /* For "false && x" or "true || x", x will never be executed;
8195 disable warnings while evaluating it. */
8196 if (current.tree_type == TRUTH_ANDIF_EXPR)
8197 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8198 else if (current.tree_type == TRUTH_ORIF_EXPR)
8199 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8201 /* Extract another operand. It may be the RHS of this expression
8202 or the LHS of a new, higher priority expression. */
8203 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8204 ? TRUTH_NOT_EXPR : ERROR_MARK);
8205 rhs = cp_parser_simple_cast_expression (parser);
8207 /* Get another operator token. Look up its precedence to avoid
8208 building a useless (immediately popped) stack entry for common
8209 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8210 token = cp_lexer_peek_token (parser->lexer);
8211 lookahead_prec = TOKEN_PRECEDENCE (token);
8212 if (lookahead_prec > new_prec)
8214 /* ... and prepare to parse the RHS of the new, higher priority
8215 expression. Since precedence levels on the stack are
8216 monotonically increasing, we do not have to care about
8217 stack overflows. */
8218 *sp = current;
8219 ++sp;
8220 current.lhs = rhs;
8221 current.lhs_type = rhs_type;
8222 current.prec = new_prec;
8223 new_prec = lookahead_prec;
8224 goto get_rhs;
8226 pop:
8227 lookahead_prec = new_prec;
8228 /* If the stack is not empty, we have parsed into LHS the right side
8229 (`4' in the example above) of an expression we had suspended.
8230 We can use the information on the stack to recover the LHS (`3')
8231 from the stack together with the tree code (`MULT_EXPR'), and
8232 the precedence of the higher level subexpression
8233 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8234 which will be used to actually build the additive expression. */
8235 rhs = current.lhs;
8236 rhs_type = current.lhs_type;
8237 --sp;
8238 current = *sp;
8241 /* Undo the disabling of warnings done above. */
8242 if (current.tree_type == TRUTH_ANDIF_EXPR)
8243 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8244 else if (current.tree_type == TRUTH_ORIF_EXPR)
8245 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8247 if (warn_logical_not_paren
8248 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8249 && current.lhs_type == TRUTH_NOT_EXPR
8250 /* Avoid warning for !!x == y. */
8251 && (TREE_CODE (current.lhs) != NE_EXPR
8252 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8253 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8254 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8255 /* Avoid warning for !b == y where b is boolean. */
8256 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8257 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8258 != BOOLEAN_TYPE))))
8259 /* Avoid warning for !!b == y where b is boolean. */
8260 && (!DECL_P (current.lhs)
8261 || TREE_TYPE (current.lhs) == NULL_TREE
8262 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8263 warn_logical_not_parentheses (current.loc, current.tree_type,
8264 maybe_constant_value (rhs));
8266 overload = NULL;
8267 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8268 ERROR_MARK for everything that is not a binary expression.
8269 This makes warn_about_parentheses miss some warnings that
8270 involve unary operators. For unary expressions we should
8271 pass the correct tree_code unless the unary expression was
8272 surrounded by parentheses.
8274 if (no_toplevel_fold_p
8275 && lookahead_prec <= current.prec
8276 && sp == stack)
8277 current.lhs = build2 (current.tree_type,
8278 TREE_CODE_CLASS (current.tree_type)
8279 == tcc_comparison
8280 ? boolean_type_node : TREE_TYPE (current.lhs),
8281 current.lhs, rhs);
8282 else
8283 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8284 current.lhs, current.lhs_type,
8285 rhs, rhs_type, &overload,
8286 complain_flags (decltype_p));
8287 current.lhs_type = current.tree_type;
8288 if (EXPR_P (current.lhs))
8289 SET_EXPR_LOCATION (current.lhs, current.loc);
8291 /* If the binary operator required the use of an overloaded operator,
8292 then this expression cannot be an integral constant-expression.
8293 An overloaded operator can be used even if both operands are
8294 otherwise permissible in an integral constant-expression if at
8295 least one of the operands is of enumeration type. */
8297 if (overload
8298 && cp_parser_non_integral_constant_expression (parser,
8299 NIC_OVERLOADED))
8300 return error_mark_node;
8303 return current.lhs;
8306 static tree
8307 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8308 bool no_toplevel_fold_p,
8309 enum cp_parser_prec prec,
8310 cp_id_kind * pidk)
8312 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8313 /*decltype*/false, prec, pidk);
8316 /* Parse the `? expression : assignment-expression' part of a
8317 conditional-expression. The LOGICAL_OR_EXPR is the
8318 logical-or-expression that started the conditional-expression.
8319 Returns a representation of the entire conditional-expression.
8321 This routine is used by cp_parser_assignment_expression.
8323 ? expression : assignment-expression
8325 GNU Extensions:
8327 ? : assignment-expression */
8329 static tree
8330 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8332 tree expr;
8333 tree assignment_expr;
8334 struct cp_token *token;
8335 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8337 /* Consume the `?' token. */
8338 cp_lexer_consume_token (parser->lexer);
8339 token = cp_lexer_peek_token (parser->lexer);
8340 if (cp_parser_allow_gnu_extensions_p (parser)
8341 && token->type == CPP_COLON)
8343 pedwarn (token->location, OPT_Wpedantic,
8344 "ISO C++ does not allow ?: with omitted middle operand");
8345 /* Implicit true clause. */
8346 expr = NULL_TREE;
8347 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8348 warn_for_omitted_condop (token->location, logical_or_expr);
8350 else
8352 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8353 parser->colon_corrects_to_scope_p = false;
8354 /* Parse the expression. */
8355 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8356 expr = cp_parser_expression (parser);
8357 c_inhibit_evaluation_warnings +=
8358 ((logical_or_expr == truthvalue_true_node)
8359 - (logical_or_expr == truthvalue_false_node));
8360 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8363 /* The next token should be a `:'. */
8364 cp_parser_require (parser, CPP_COLON, RT_COLON);
8365 /* Parse the assignment-expression. */
8366 assignment_expr = cp_parser_assignment_expression (parser);
8367 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8369 /* Build the conditional-expression. */
8370 return build_x_conditional_expr (loc, logical_or_expr,
8371 expr,
8372 assignment_expr,
8373 tf_warning_or_error);
8376 /* Parse an assignment-expression.
8378 assignment-expression:
8379 conditional-expression
8380 logical-or-expression assignment-operator assignment_expression
8381 throw-expression
8383 CAST_P is true if this expression is the target of a cast.
8384 DECLTYPE_P is true if this expression is the operand of decltype.
8386 Returns a representation for the expression. */
8388 static tree
8389 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8390 bool cast_p, bool decltype_p)
8392 tree expr;
8394 /* If the next token is the `throw' keyword, then we're looking at
8395 a throw-expression. */
8396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8397 expr = cp_parser_throw_expression (parser);
8398 /* Otherwise, it must be that we are looking at a
8399 logical-or-expression. */
8400 else
8402 /* Parse the binary expressions (logical-or-expression). */
8403 expr = cp_parser_binary_expression (parser, cast_p, false,
8404 decltype_p,
8405 PREC_NOT_OPERATOR, pidk);
8406 /* If the next token is a `?' then we're actually looking at a
8407 conditional-expression. */
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8409 return cp_parser_question_colon_clause (parser, expr);
8410 else
8412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8414 /* If it's an assignment-operator, we're using the second
8415 production. */
8416 enum tree_code assignment_operator
8417 = cp_parser_assignment_operator_opt (parser);
8418 if (assignment_operator != ERROR_MARK)
8420 bool non_constant_p;
8421 location_t saved_input_location;
8423 /* Parse the right-hand side of the assignment. */
8424 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8426 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8427 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8429 /* An assignment may not appear in a
8430 constant-expression. */
8431 if (cp_parser_non_integral_constant_expression (parser,
8432 NIC_ASSIGNMENT))
8433 return error_mark_node;
8434 /* Build the assignment expression. Its default
8435 location is the location of the '=' token. */
8436 saved_input_location = input_location;
8437 input_location = loc;
8438 expr = build_x_modify_expr (loc, expr,
8439 assignment_operator,
8440 rhs,
8441 complain_flags (decltype_p));
8442 input_location = saved_input_location;
8447 return expr;
8450 /* Parse an (optional) assignment-operator.
8452 assignment-operator: one of
8453 = *= /= %= += -= >>= <<= &= ^= |=
8455 GNU Extension:
8457 assignment-operator: one of
8458 <?= >?=
8460 If the next token is an assignment operator, the corresponding tree
8461 code is returned, and the token is consumed. For example, for
8462 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8463 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8464 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8465 operator, ERROR_MARK is returned. */
8467 static enum tree_code
8468 cp_parser_assignment_operator_opt (cp_parser* parser)
8470 enum tree_code op;
8471 cp_token *token;
8473 /* Peek at the next token. */
8474 token = cp_lexer_peek_token (parser->lexer);
8476 switch (token->type)
8478 case CPP_EQ:
8479 op = NOP_EXPR;
8480 break;
8482 case CPP_MULT_EQ:
8483 op = MULT_EXPR;
8484 break;
8486 case CPP_DIV_EQ:
8487 op = TRUNC_DIV_EXPR;
8488 break;
8490 case CPP_MOD_EQ:
8491 op = TRUNC_MOD_EXPR;
8492 break;
8494 case CPP_PLUS_EQ:
8495 op = PLUS_EXPR;
8496 break;
8498 case CPP_MINUS_EQ:
8499 op = MINUS_EXPR;
8500 break;
8502 case CPP_RSHIFT_EQ:
8503 op = RSHIFT_EXPR;
8504 break;
8506 case CPP_LSHIFT_EQ:
8507 op = LSHIFT_EXPR;
8508 break;
8510 case CPP_AND_EQ:
8511 op = BIT_AND_EXPR;
8512 break;
8514 case CPP_XOR_EQ:
8515 op = BIT_XOR_EXPR;
8516 break;
8518 case CPP_OR_EQ:
8519 op = BIT_IOR_EXPR;
8520 break;
8522 default:
8523 /* Nothing else is an assignment operator. */
8524 op = ERROR_MARK;
8527 /* If it was an assignment operator, consume it. */
8528 if (op != ERROR_MARK)
8529 cp_lexer_consume_token (parser->lexer);
8531 return op;
8534 /* Parse an expression.
8536 expression:
8537 assignment-expression
8538 expression , assignment-expression
8540 CAST_P is true if this expression is the target of a cast.
8541 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8542 except possibly parenthesized or on the RHS of a comma (N3276).
8544 Returns a representation of the expression. */
8546 static tree
8547 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8548 bool cast_p, bool decltype_p)
8550 tree expression = NULL_TREE;
8551 location_t loc = UNKNOWN_LOCATION;
8553 while (true)
8555 tree assignment_expression;
8557 /* Parse the next assignment-expression. */
8558 assignment_expression
8559 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8561 /* We don't create a temporary for a call that is the immediate operand
8562 of decltype or on the RHS of a comma. But when we see a comma, we
8563 need to create a temporary for a call on the LHS. */
8564 if (decltype_p && !processing_template_decl
8565 && TREE_CODE (assignment_expression) == CALL_EXPR
8566 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8567 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8568 assignment_expression
8569 = build_cplus_new (TREE_TYPE (assignment_expression),
8570 assignment_expression, tf_warning_or_error);
8572 /* If this is the first assignment-expression, we can just
8573 save it away. */
8574 if (!expression)
8575 expression = assignment_expression;
8576 else
8577 expression = build_x_compound_expr (loc, expression,
8578 assignment_expression,
8579 complain_flags (decltype_p));
8580 /* If the next token is not a comma, then we are done with the
8581 expression. */
8582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8583 break;
8584 /* Consume the `,'. */
8585 loc = cp_lexer_peek_token (parser->lexer)->location;
8586 cp_lexer_consume_token (parser->lexer);
8587 /* A comma operator cannot appear in a constant-expression. */
8588 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8589 expression = error_mark_node;
8592 return expression;
8595 /* Parse a constant-expression.
8597 constant-expression:
8598 conditional-expression
8600 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8601 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8602 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8603 is false, NON_CONSTANT_P should be NULL. */
8605 static tree
8606 cp_parser_constant_expression (cp_parser* parser,
8607 bool allow_non_constant_p,
8608 bool *non_constant_p)
8610 bool saved_integral_constant_expression_p;
8611 bool saved_allow_non_integral_constant_expression_p;
8612 bool saved_non_integral_constant_expression_p;
8613 tree expression;
8615 /* It might seem that we could simply parse the
8616 conditional-expression, and then check to see if it were
8617 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8618 one that the compiler can figure out is constant, possibly after
8619 doing some simplifications or optimizations. The standard has a
8620 precise definition of constant-expression, and we must honor
8621 that, even though it is somewhat more restrictive.
8623 For example:
8625 int i[(2, 3)];
8627 is not a legal declaration, because `(2, 3)' is not a
8628 constant-expression. The `,' operator is forbidden in a
8629 constant-expression. However, GCC's constant-folding machinery
8630 will fold this operation to an INTEGER_CST for `3'. */
8632 /* Save the old settings. */
8633 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8634 saved_allow_non_integral_constant_expression_p
8635 = parser->allow_non_integral_constant_expression_p;
8636 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8637 /* We are now parsing a constant-expression. */
8638 parser->integral_constant_expression_p = true;
8639 parser->allow_non_integral_constant_expression_p
8640 = (allow_non_constant_p || cxx_dialect >= cxx11);
8641 parser->non_integral_constant_expression_p = false;
8642 /* Although the grammar says "conditional-expression", we parse an
8643 "assignment-expression", which also permits "throw-expression"
8644 and the use of assignment operators. In the case that
8645 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8646 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8647 actually essential that we look for an assignment-expression.
8648 For example, cp_parser_initializer_clauses uses this function to
8649 determine whether a particular assignment-expression is in fact
8650 constant. */
8651 expression = cp_parser_assignment_expression (parser);
8652 /* Restore the old settings. */
8653 parser->integral_constant_expression_p
8654 = saved_integral_constant_expression_p;
8655 parser->allow_non_integral_constant_expression_p
8656 = saved_allow_non_integral_constant_expression_p;
8657 if (cxx_dialect >= cxx11)
8659 /* Require an rvalue constant expression here; that's what our
8660 callers expect. Reference constant expressions are handled
8661 separately in e.g. cp_parser_template_argument. */
8662 bool is_const = potential_rvalue_constant_expression (expression);
8663 parser->non_integral_constant_expression_p = !is_const;
8664 if (!is_const && !allow_non_constant_p)
8665 require_potential_rvalue_constant_expression (expression);
8667 if (allow_non_constant_p)
8668 *non_constant_p = parser->non_integral_constant_expression_p;
8669 parser->non_integral_constant_expression_p
8670 = saved_non_integral_constant_expression_p;
8672 return expression;
8675 /* Parse __builtin_offsetof.
8677 offsetof-expression:
8678 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8680 offsetof-member-designator:
8681 id-expression
8682 | offsetof-member-designator "." id-expression
8683 | offsetof-member-designator "[" expression "]"
8684 | offsetof-member-designator "->" id-expression */
8686 static tree
8687 cp_parser_builtin_offsetof (cp_parser *parser)
8689 int save_ice_p, save_non_ice_p;
8690 tree type, expr;
8691 cp_id_kind dummy;
8692 cp_token *token;
8694 /* We're about to accept non-integral-constant things, but will
8695 definitely yield an integral constant expression. Save and
8696 restore these values around our local parsing. */
8697 save_ice_p = parser->integral_constant_expression_p;
8698 save_non_ice_p = parser->non_integral_constant_expression_p;
8700 /* Consume the "__builtin_offsetof" token. */
8701 cp_lexer_consume_token (parser->lexer);
8702 /* Consume the opening `('. */
8703 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8704 /* Parse the type-id. */
8705 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8706 type = cp_parser_type_id (parser);
8707 /* Look for the `,'. */
8708 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8709 token = cp_lexer_peek_token (parser->lexer);
8711 /* Build the (type *)null that begins the traditional offsetof macro. */
8712 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8713 tf_warning_or_error);
8715 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8716 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8717 true, &dummy, token->location);
8718 while (true)
8720 token = cp_lexer_peek_token (parser->lexer);
8721 switch (token->type)
8723 case CPP_OPEN_SQUARE:
8724 /* offsetof-member-designator "[" expression "]" */
8725 expr = cp_parser_postfix_open_square_expression (parser, expr,
8726 true, false);
8727 break;
8729 case CPP_DEREF:
8730 /* offsetof-member-designator "->" identifier */
8731 expr = grok_array_decl (token->location, expr,
8732 integer_zero_node, false);
8733 /* FALLTHRU */
8735 case CPP_DOT:
8736 /* offsetof-member-designator "." identifier */
8737 cp_lexer_consume_token (parser->lexer);
8738 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8739 expr, true, &dummy,
8740 token->location);
8741 break;
8743 case CPP_CLOSE_PAREN:
8744 /* Consume the ")" token. */
8745 cp_lexer_consume_token (parser->lexer);
8746 goto success;
8748 default:
8749 /* Error. We know the following require will fail, but
8750 that gives the proper error message. */
8751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8752 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8753 expr = error_mark_node;
8754 goto failure;
8758 success:
8759 expr = finish_offsetof (expr, loc);
8761 failure:
8762 parser->integral_constant_expression_p = save_ice_p;
8763 parser->non_integral_constant_expression_p = save_non_ice_p;
8765 return expr;
8768 /* Parse a trait expression.
8770 Returns a representation of the expression, the underlying type
8771 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8773 static tree
8774 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8776 cp_trait_kind kind;
8777 tree type1, type2 = NULL_TREE;
8778 bool binary = false;
8779 bool variadic = false;
8781 switch (keyword)
8783 case RID_HAS_NOTHROW_ASSIGN:
8784 kind = CPTK_HAS_NOTHROW_ASSIGN;
8785 break;
8786 case RID_HAS_NOTHROW_CONSTRUCTOR:
8787 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8788 break;
8789 case RID_HAS_NOTHROW_COPY:
8790 kind = CPTK_HAS_NOTHROW_COPY;
8791 break;
8792 case RID_HAS_TRIVIAL_ASSIGN:
8793 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8794 break;
8795 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8796 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8797 break;
8798 case RID_HAS_TRIVIAL_COPY:
8799 kind = CPTK_HAS_TRIVIAL_COPY;
8800 break;
8801 case RID_HAS_TRIVIAL_DESTRUCTOR:
8802 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8803 break;
8804 case RID_HAS_VIRTUAL_DESTRUCTOR:
8805 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8806 break;
8807 case RID_IS_ABSTRACT:
8808 kind = CPTK_IS_ABSTRACT;
8809 break;
8810 case RID_IS_BASE_OF:
8811 kind = CPTK_IS_BASE_OF;
8812 binary = true;
8813 break;
8814 case RID_IS_CLASS:
8815 kind = CPTK_IS_CLASS;
8816 break;
8817 case RID_IS_EMPTY:
8818 kind = CPTK_IS_EMPTY;
8819 break;
8820 case RID_IS_ENUM:
8821 kind = CPTK_IS_ENUM;
8822 break;
8823 case RID_IS_FINAL:
8824 kind = CPTK_IS_FINAL;
8825 break;
8826 case RID_IS_LITERAL_TYPE:
8827 kind = CPTK_IS_LITERAL_TYPE;
8828 break;
8829 case RID_IS_POD:
8830 kind = CPTK_IS_POD;
8831 break;
8832 case RID_IS_POLYMORPHIC:
8833 kind = CPTK_IS_POLYMORPHIC;
8834 break;
8835 case RID_IS_STD_LAYOUT:
8836 kind = CPTK_IS_STD_LAYOUT;
8837 break;
8838 case RID_IS_TRIVIAL:
8839 kind = CPTK_IS_TRIVIAL;
8840 break;
8841 case RID_IS_TRIVIALLY_ASSIGNABLE:
8842 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8843 binary = true;
8844 break;
8845 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8846 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8847 variadic = true;
8848 break;
8849 case RID_IS_TRIVIALLY_COPYABLE:
8850 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8851 break;
8852 case RID_IS_UNION:
8853 kind = CPTK_IS_UNION;
8854 break;
8855 case RID_UNDERLYING_TYPE:
8856 kind = CPTK_UNDERLYING_TYPE;
8857 break;
8858 case RID_BASES:
8859 kind = CPTK_BASES;
8860 break;
8861 case RID_DIRECT_BASES:
8862 kind = CPTK_DIRECT_BASES;
8863 break;
8864 default:
8865 gcc_unreachable ();
8868 /* Consume the token. */
8869 cp_lexer_consume_token (parser->lexer);
8871 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8873 type1 = cp_parser_type_id (parser);
8875 if (type1 == error_mark_node)
8876 return error_mark_node;
8878 if (binary)
8880 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8882 type2 = cp_parser_type_id (parser);
8884 if (type2 == error_mark_node)
8885 return error_mark_node;
8887 else if (variadic)
8889 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8891 cp_lexer_consume_token (parser->lexer);
8892 tree elt = cp_parser_type_id (parser);
8893 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8895 cp_lexer_consume_token (parser->lexer);
8896 elt = make_pack_expansion (elt);
8898 if (elt == error_mark_node)
8899 return error_mark_node;
8900 type2 = tree_cons (NULL_TREE, elt, type2);
8904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8906 /* Complete the trait expression, which may mean either processing
8907 the trait expr now or saving it for template instantiation. */
8908 switch(kind)
8910 case CPTK_UNDERLYING_TYPE:
8911 return finish_underlying_type (type1);
8912 case CPTK_BASES:
8913 return finish_bases (type1, false);
8914 case CPTK_DIRECT_BASES:
8915 return finish_bases (type1, true);
8916 default:
8917 return finish_trait_expr (kind, type1, type2);
8921 /* Lambdas that appear in variable initializer or default argument scope
8922 get that in their mangling, so we need to record it. We might as well
8923 use the count for function and namespace scopes as well. */
8924 static GTY(()) tree lambda_scope;
8925 static GTY(()) int lambda_count;
8926 typedef struct GTY(()) tree_int
8928 tree t;
8929 int i;
8930 } tree_int;
8931 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8933 static void
8934 start_lambda_scope (tree decl)
8936 tree_int ti;
8937 gcc_assert (decl);
8938 /* Once we're inside a function, we ignore other scopes and just push
8939 the function again so that popping works properly. */
8940 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8941 decl = current_function_decl;
8942 ti.t = lambda_scope;
8943 ti.i = lambda_count;
8944 vec_safe_push (lambda_scope_stack, ti);
8945 if (lambda_scope != decl)
8947 /* Don't reset the count if we're still in the same function. */
8948 lambda_scope = decl;
8949 lambda_count = 0;
8953 static void
8954 record_lambda_scope (tree lambda)
8956 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8957 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8960 static void
8961 finish_lambda_scope (void)
8963 tree_int *p = &lambda_scope_stack->last ();
8964 if (lambda_scope != p->t)
8966 lambda_scope = p->t;
8967 lambda_count = p->i;
8969 lambda_scope_stack->pop ();
8972 /* Parse a lambda expression.
8974 lambda-expression:
8975 lambda-introducer lambda-declarator [opt] compound-statement
8977 Returns a representation of the expression. */
8979 static tree
8980 cp_parser_lambda_expression (cp_parser* parser)
8982 tree lambda_expr = build_lambda_expr ();
8983 tree type;
8984 bool ok = true;
8985 cp_token *token = cp_lexer_peek_token (parser->lexer);
8986 cp_token_position start = 0;
8988 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8990 if (cp_unevaluated_operand)
8992 if (!token->error_reported)
8994 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8995 "lambda-expression in unevaluated context");
8996 token->error_reported = true;
8998 ok = false;
9000 else if (parser->in_template_argument_list_p)
9002 if (!token->error_reported)
9004 error_at (token->location, "lambda-expression in template-argument");
9005 token->error_reported = true;
9007 ok = false;
9010 /* We may be in the middle of deferred access check. Disable
9011 it now. */
9012 push_deferring_access_checks (dk_no_deferred);
9014 cp_parser_lambda_introducer (parser, lambda_expr);
9016 type = begin_lambda_type (lambda_expr);
9017 if (type == error_mark_node)
9018 return error_mark_node;
9020 record_lambda_scope (lambda_expr);
9022 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9023 determine_visibility (TYPE_NAME (type));
9025 /* Now that we've started the type, add the capture fields for any
9026 explicit captures. */
9027 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9030 /* Inside the class, surrounding template-parameter-lists do not apply. */
9031 unsigned int saved_num_template_parameter_lists
9032 = parser->num_template_parameter_lists;
9033 unsigned char in_statement = parser->in_statement;
9034 bool in_switch_statement_p = parser->in_switch_statement_p;
9035 bool fully_implicit_function_template_p
9036 = parser->fully_implicit_function_template_p;
9037 tree implicit_template_parms = parser->implicit_template_parms;
9038 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9039 bool auto_is_implicit_function_template_parm_p
9040 = parser->auto_is_implicit_function_template_parm_p;
9042 parser->num_template_parameter_lists = 0;
9043 parser->in_statement = 0;
9044 parser->in_switch_statement_p = false;
9045 parser->fully_implicit_function_template_p = false;
9046 parser->implicit_template_parms = 0;
9047 parser->implicit_template_scope = 0;
9048 parser->auto_is_implicit_function_template_parm_p = false;
9050 /* By virtue of defining a local class, a lambda expression has access to
9051 the private variables of enclosing classes. */
9053 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9055 if (ok)
9057 if (!cp_parser_error_occurred (parser)
9058 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9059 && cp_parser_start_tentative_firewall (parser))
9060 start = token;
9061 cp_parser_lambda_body (parser, lambda_expr);
9063 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9065 if (cp_parser_skip_to_closing_brace (parser))
9066 cp_lexer_consume_token (parser->lexer);
9069 /* The capture list was built up in reverse order; fix that now. */
9070 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9071 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9073 if (ok)
9074 maybe_add_lambda_conv_op (type);
9076 type = finish_struct (type, /*attributes=*/NULL_TREE);
9078 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9079 parser->in_statement = in_statement;
9080 parser->in_switch_statement_p = in_switch_statement_p;
9081 parser->fully_implicit_function_template_p
9082 = fully_implicit_function_template_p;
9083 parser->implicit_template_parms = implicit_template_parms;
9084 parser->implicit_template_scope = implicit_template_scope;
9085 parser->auto_is_implicit_function_template_parm_p
9086 = auto_is_implicit_function_template_parm_p;
9089 pop_deferring_access_checks ();
9091 /* This field is only used during parsing of the lambda. */
9092 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9094 /* This lambda shouldn't have any proxies left at this point. */
9095 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9096 /* And now that we're done, push proxies for an enclosing lambda. */
9097 insert_pending_capture_proxies ();
9099 if (ok)
9100 lambda_expr = build_lambda_object (lambda_expr);
9101 else
9102 lambda_expr = error_mark_node;
9104 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9106 return lambda_expr;
9109 /* Parse the beginning of a lambda expression.
9111 lambda-introducer:
9112 [ lambda-capture [opt] ]
9114 LAMBDA_EXPR is the current representation of the lambda expression. */
9116 static void
9117 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9119 /* Need commas after the first capture. */
9120 bool first = true;
9122 /* Eat the leading `['. */
9123 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9125 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9127 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9128 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9129 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9130 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9132 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9134 cp_lexer_consume_token (parser->lexer);
9135 first = false;
9138 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9140 cp_token* capture_token;
9141 tree capture_id;
9142 tree capture_init_expr;
9143 cp_id_kind idk = CP_ID_KIND_NONE;
9144 bool explicit_init_p = false;
9146 enum capture_kind_type
9148 BY_COPY,
9149 BY_REFERENCE
9151 enum capture_kind_type capture_kind = BY_COPY;
9153 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9155 error ("expected end of capture-list");
9156 return;
9159 if (first)
9160 first = false;
9161 else
9162 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9164 /* Possibly capture `this'. */
9165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9167 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9168 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9169 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9170 "with by-copy capture default");
9171 cp_lexer_consume_token (parser->lexer);
9172 add_capture (lambda_expr,
9173 /*id=*/this_identifier,
9174 /*initializer=*/finish_this_expr(),
9175 /*by_reference_p=*/false,
9176 explicit_init_p);
9177 continue;
9180 /* Remember whether we want to capture as a reference or not. */
9181 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9183 capture_kind = BY_REFERENCE;
9184 cp_lexer_consume_token (parser->lexer);
9187 /* Get the identifier. */
9188 capture_token = cp_lexer_peek_token (parser->lexer);
9189 capture_id = cp_parser_identifier (parser);
9191 if (capture_id == error_mark_node)
9192 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9193 delimiters, but I modified this to stop on unnested ']' as well. It
9194 was already changed to stop on unnested '}', so the
9195 "closing_parenthesis" name is no more misleading with my change. */
9197 cp_parser_skip_to_closing_parenthesis (parser,
9198 /*recovering=*/true,
9199 /*or_comma=*/true,
9200 /*consume_paren=*/true);
9201 break;
9204 /* Find the initializer for this capture. */
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9206 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9207 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9209 bool direct, non_constant;
9210 /* An explicit initializer exists. */
9211 if (cxx_dialect < cxx14)
9212 pedwarn (input_location, 0,
9213 "lambda capture initializers "
9214 "only available with -std=c++14 or -std=gnu++14");
9215 capture_init_expr = cp_parser_initializer (parser, &direct,
9216 &non_constant);
9217 explicit_init_p = true;
9218 if (capture_init_expr == NULL_TREE)
9220 error ("empty initializer for lambda init-capture");
9221 capture_init_expr = error_mark_node;
9224 else
9226 const char* error_msg;
9228 /* Turn the identifier into an id-expression. */
9229 capture_init_expr
9230 = cp_parser_lookup_name_simple (parser, capture_id,
9231 capture_token->location);
9233 if (capture_init_expr == error_mark_node)
9235 unqualified_name_lookup_error (capture_id);
9236 continue;
9238 else if (DECL_P (capture_init_expr)
9239 && (!VAR_P (capture_init_expr)
9240 && TREE_CODE (capture_init_expr) != PARM_DECL))
9242 error_at (capture_token->location,
9243 "capture of non-variable %qD ",
9244 capture_init_expr);
9245 inform (0, "%q+#D declared here", capture_init_expr);
9246 continue;
9248 if (VAR_P (capture_init_expr)
9249 && decl_storage_duration (capture_init_expr) != dk_auto)
9251 if (pedwarn (capture_token->location, 0, "capture of variable "
9252 "%qD with non-automatic storage duration",
9253 capture_init_expr))
9254 inform (0, "%q+#D declared here", capture_init_expr);
9255 continue;
9258 capture_init_expr
9259 = finish_id_expression
9260 (capture_id,
9261 capture_init_expr,
9262 parser->scope,
9263 &idk,
9264 /*integral_constant_expression_p=*/false,
9265 /*allow_non_integral_constant_expression_p=*/false,
9266 /*non_integral_constant_expression_p=*/NULL,
9267 /*template_p=*/false,
9268 /*done=*/true,
9269 /*address_p=*/false,
9270 /*template_arg_p=*/false,
9271 &error_msg,
9272 capture_token->location);
9274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9276 cp_lexer_consume_token (parser->lexer);
9277 capture_init_expr = make_pack_expansion (capture_init_expr);
9279 else
9280 check_for_bare_parameter_packs (capture_init_expr);
9283 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9284 && !explicit_init_p)
9286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9287 && capture_kind == BY_COPY)
9288 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9289 "of %qD redundant with by-copy capture default",
9290 capture_id);
9291 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9292 && capture_kind == BY_REFERENCE)
9293 pedwarn (capture_token->location, 0, "explicit by-reference "
9294 "capture of %qD redundant with by-reference capture "
9295 "default", capture_id);
9298 add_capture (lambda_expr,
9299 capture_id,
9300 capture_init_expr,
9301 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9302 explicit_init_p);
9305 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9308 /* Parse the (optional) middle of a lambda expression.
9310 lambda-declarator:
9311 < template-parameter-list [opt] >
9312 ( parameter-declaration-clause [opt] )
9313 attribute-specifier [opt]
9314 mutable [opt]
9315 exception-specification [opt]
9316 lambda-return-type-clause [opt]
9318 LAMBDA_EXPR is the current representation of the lambda expression. */
9320 static bool
9321 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9323 /* 5.1.1.4 of the standard says:
9324 If a lambda-expression does not include a lambda-declarator, it is as if
9325 the lambda-declarator were ().
9326 This means an empty parameter list, no attributes, and no exception
9327 specification. */
9328 tree param_list = void_list_node;
9329 tree attributes = NULL_TREE;
9330 tree exception_spec = NULL_TREE;
9331 tree template_param_list = NULL_TREE;
9333 /* The template-parameter-list is optional, but must begin with
9334 an opening angle if present. */
9335 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9337 if (cxx_dialect < cxx14)
9338 pedwarn (parser->lexer->next_token->location, 0,
9339 "lambda templates are only available with "
9340 "-std=c++14 or -std=gnu++14");
9342 cp_lexer_consume_token (parser->lexer);
9344 template_param_list = cp_parser_template_parameter_list (parser);
9346 cp_parser_skip_to_end_of_template_parameter_list (parser);
9348 /* We just processed one more parameter list. */
9349 ++parser->num_template_parameter_lists;
9352 /* The parameter-declaration-clause is optional (unless
9353 template-parameter-list was given), but must begin with an
9354 opening parenthesis if present. */
9355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9357 cp_lexer_consume_token (parser->lexer);
9359 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9361 /* Parse parameters. */
9362 param_list = cp_parser_parameter_declaration_clause (parser);
9364 /* Default arguments shall not be specified in the
9365 parameter-declaration-clause of a lambda-declarator. */
9366 for (tree t = param_list; t; t = TREE_CHAIN (t))
9367 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9368 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9369 "default argument specified for lambda parameter");
9371 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9373 attributes = cp_parser_attributes_opt (parser);
9375 /* Parse optional `mutable' keyword. */
9376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9378 cp_lexer_consume_token (parser->lexer);
9379 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9382 /* Parse optional exception specification. */
9383 exception_spec = cp_parser_exception_specification_opt (parser);
9385 /* Parse optional trailing return type. */
9386 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9388 cp_lexer_consume_token (parser->lexer);
9389 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9390 = cp_parser_trailing_type_id (parser);
9393 /* The function parameters must be in scope all the way until after the
9394 trailing-return-type in case of decltype. */
9395 pop_bindings_and_leave_scope ();
9397 else if (template_param_list != NULL_TREE) // generate diagnostic
9398 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9400 /* Create the function call operator.
9402 Messing with declarators like this is no uglier than building up the
9403 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9404 other code. */
9406 cp_decl_specifier_seq return_type_specs;
9407 cp_declarator* declarator;
9408 tree fco;
9409 int quals;
9410 void *p;
9412 clear_decl_specs (&return_type_specs);
9413 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9414 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9415 else
9416 /* Maybe we will deduce the return type later. */
9417 return_type_specs.type = make_auto ();
9419 p = obstack_alloc (&declarator_obstack, 0);
9421 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9422 sfk_none);
9424 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9425 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9426 declarator = make_call_declarator (declarator, param_list, quals,
9427 VIRT_SPEC_UNSPECIFIED,
9428 REF_QUAL_NONE,
9429 exception_spec,
9430 /*late_return_type=*/NULL_TREE);
9431 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9433 fco = grokmethod (&return_type_specs,
9434 declarator,
9435 attributes);
9436 if (fco != error_mark_node)
9438 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9439 DECL_ARTIFICIAL (fco) = 1;
9440 /* Give the object parameter a different name. */
9441 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9442 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9443 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9445 if (template_param_list)
9447 fco = finish_member_template_decl (fco);
9448 finish_template_decl (template_param_list);
9449 --parser->num_template_parameter_lists;
9451 else if (parser->fully_implicit_function_template_p)
9452 fco = finish_fully_implicit_template (parser, fco);
9454 finish_member_declaration (fco);
9456 obstack_free (&declarator_obstack, p);
9458 return (fco != error_mark_node);
9462 /* Parse the body of a lambda expression, which is simply
9464 compound-statement
9466 but which requires special handling.
9467 LAMBDA_EXPR is the current representation of the lambda expression. */
9469 static void
9470 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9472 bool nested = (current_function_decl != NULL_TREE);
9473 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9474 if (nested)
9475 push_function_context ();
9476 else
9477 /* Still increment function_depth so that we don't GC in the
9478 middle of an expression. */
9479 ++function_depth;
9480 /* Clear this in case we're in the middle of a default argument. */
9481 parser->local_variables_forbidden_p = false;
9483 /* Finish the function call operator
9484 - class_specifier
9485 + late_parsing_for_member
9486 + function_definition_after_declarator
9487 + ctor_initializer_opt_and_function_body */
9489 tree fco = lambda_function (lambda_expr);
9490 tree body;
9491 bool done = false;
9492 tree compound_stmt;
9493 tree cap;
9495 /* Let the front end know that we are going to be defining this
9496 function. */
9497 start_preparsed_function (fco,
9498 NULL_TREE,
9499 SF_PRE_PARSED | SF_INCLASS_INLINE);
9501 start_lambda_scope (fco);
9502 body = begin_function_body ();
9504 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9505 goto out;
9507 /* Push the proxies for any explicit captures. */
9508 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9509 cap = TREE_CHAIN (cap))
9510 build_capture_proxy (TREE_PURPOSE (cap));
9512 compound_stmt = begin_compound_stmt (0);
9514 /* 5.1.1.4 of the standard says:
9515 If a lambda-expression does not include a trailing-return-type, it
9516 is as if the trailing-return-type denotes the following type:
9517 * if the compound-statement is of the form
9518 { return attribute-specifier [opt] expression ; }
9519 the type of the returned expression after lvalue-to-rvalue
9520 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9521 (_conv.array_ 4.2), and function-to-pointer conversion
9522 (_conv.func_ 4.3);
9523 * otherwise, void. */
9525 /* In a lambda that has neither a lambda-return-type-clause
9526 nor a deducible form, errors should be reported for return statements
9527 in the body. Since we used void as the placeholder return type, parsing
9528 the body as usual will give such desired behavior. */
9529 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9530 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9533 tree expr = NULL_TREE;
9534 cp_id_kind idk = CP_ID_KIND_NONE;
9536 /* Parse tentatively in case there's more after the initial return
9537 statement. */
9538 cp_parser_parse_tentatively (parser);
9540 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9542 expr = cp_parser_expression (parser, &idk);
9544 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9547 if (cp_parser_parse_definitely (parser))
9549 if (!processing_template_decl)
9550 apply_deduced_return_type (fco, lambda_return_type (expr));
9552 /* Will get error here if type not deduced yet. */
9553 finish_return_stmt (expr);
9555 done = true;
9559 if (!done)
9561 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9562 cp_parser_label_declaration (parser);
9563 cp_parser_statement_seq_opt (parser, NULL_TREE);
9564 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9567 finish_compound_stmt (compound_stmt);
9569 out:
9570 finish_function_body (body);
9571 finish_lambda_scope ();
9573 /* Finish the function and generate code for it if necessary. */
9574 tree fn = finish_function (/*inline*/2);
9576 /* Only expand if the call op is not a template. */
9577 if (!DECL_TEMPLATE_INFO (fco))
9578 expand_or_defer_fn (fn);
9581 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9582 if (nested)
9583 pop_function_context();
9584 else
9585 --function_depth;
9588 /* Statements [gram.stmt.stmt] */
9590 /* Parse a statement.
9592 statement:
9593 labeled-statement
9594 expression-statement
9595 compound-statement
9596 selection-statement
9597 iteration-statement
9598 jump-statement
9599 declaration-statement
9600 try-block
9602 C++11:
9604 statement:
9605 labeled-statement
9606 attribute-specifier-seq (opt) expression-statement
9607 attribute-specifier-seq (opt) compound-statement
9608 attribute-specifier-seq (opt) selection-statement
9609 attribute-specifier-seq (opt) iteration-statement
9610 attribute-specifier-seq (opt) jump-statement
9611 declaration-statement
9612 attribute-specifier-seq (opt) try-block
9614 TM Extension:
9616 statement:
9617 atomic-statement
9619 IN_COMPOUND is true when the statement is nested inside a
9620 cp_parser_compound_statement; this matters for certain pragmas.
9622 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9623 is a (possibly labeled) if statement which is not enclosed in braces
9624 and has an else clause. This is used to implement -Wparentheses. */
9626 static void
9627 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9628 bool in_compound, bool *if_p)
9630 tree statement, std_attrs = NULL_TREE;
9631 cp_token *token;
9632 location_t statement_location, attrs_location;
9634 restart:
9635 if (if_p != NULL)
9636 *if_p = false;
9637 /* There is no statement yet. */
9638 statement = NULL_TREE;
9640 saved_token_sentinel saved_tokens (parser->lexer);
9641 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9642 if (c_dialect_objc ())
9643 /* In obj-c++, seeing '[[' might be the either the beginning of
9644 c++11 attributes, or a nested objc-message-expression. So
9645 let's parse the c++11 attributes tentatively. */
9646 cp_parser_parse_tentatively (parser);
9647 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9648 if (c_dialect_objc ())
9650 if (!cp_parser_parse_definitely (parser))
9651 std_attrs = NULL_TREE;
9654 /* Peek at the next token. */
9655 token = cp_lexer_peek_token (parser->lexer);
9656 /* Remember the location of the first token in the statement. */
9657 statement_location = token->location;
9658 /* If this is a keyword, then that will often determine what kind of
9659 statement we have. */
9660 if (token->type == CPP_KEYWORD)
9662 enum rid keyword = token->keyword;
9664 switch (keyword)
9666 case RID_CASE:
9667 case RID_DEFAULT:
9668 /* Looks like a labeled-statement with a case label.
9669 Parse the label, and then use tail recursion to parse
9670 the statement. */
9671 cp_parser_label_for_labeled_statement (parser, std_attrs);
9672 goto restart;
9674 case RID_IF:
9675 case RID_SWITCH:
9676 statement = cp_parser_selection_statement (parser, if_p);
9677 break;
9679 case RID_WHILE:
9680 case RID_DO:
9681 case RID_FOR:
9682 statement = cp_parser_iteration_statement (parser, false);
9683 break;
9685 case RID_CILK_FOR:
9686 if (!flag_cilkplus)
9688 error_at (cp_lexer_peek_token (parser->lexer)->location,
9689 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9690 cp_lexer_consume_token (parser->lexer);
9691 statement = error_mark_node;
9693 else
9694 statement = cp_parser_cilk_for (parser, integer_zero_node);
9695 break;
9697 case RID_BREAK:
9698 case RID_CONTINUE:
9699 case RID_RETURN:
9700 case RID_GOTO:
9701 statement = cp_parser_jump_statement (parser);
9702 break;
9704 case RID_CILK_SYNC:
9705 cp_lexer_consume_token (parser->lexer);
9706 if (flag_cilkplus)
9708 tree sync_expr = build_cilk_sync ();
9709 SET_EXPR_LOCATION (sync_expr,
9710 token->location);
9711 statement = finish_expr_stmt (sync_expr);
9713 else
9715 error_at (token->location, "-fcilkplus must be enabled to use"
9716 " %<_Cilk_sync%>");
9717 statement = error_mark_node;
9719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9720 break;
9722 /* Objective-C++ exception-handling constructs. */
9723 case RID_AT_TRY:
9724 case RID_AT_CATCH:
9725 case RID_AT_FINALLY:
9726 case RID_AT_SYNCHRONIZED:
9727 case RID_AT_THROW:
9728 statement = cp_parser_objc_statement (parser);
9729 break;
9731 case RID_TRY:
9732 statement = cp_parser_try_block (parser);
9733 break;
9735 case RID_NAMESPACE:
9736 /* This must be a namespace alias definition. */
9737 cp_parser_declaration_statement (parser);
9738 return;
9740 case RID_TRANSACTION_ATOMIC:
9741 case RID_TRANSACTION_RELAXED:
9742 statement = cp_parser_transaction (parser, keyword);
9743 break;
9744 case RID_TRANSACTION_CANCEL:
9745 statement = cp_parser_transaction_cancel (parser);
9746 break;
9748 default:
9749 /* It might be a keyword like `int' that can start a
9750 declaration-statement. */
9751 break;
9754 else if (token->type == CPP_NAME)
9756 /* If the next token is a `:', then we are looking at a
9757 labeled-statement. */
9758 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9759 if (token->type == CPP_COLON)
9761 /* Looks like a labeled-statement with an ordinary label.
9762 Parse the label, and then use tail recursion to parse
9763 the statement. */
9765 cp_parser_label_for_labeled_statement (parser, std_attrs);
9766 goto restart;
9769 /* Anything that starts with a `{' must be a compound-statement. */
9770 else if (token->type == CPP_OPEN_BRACE)
9771 statement = cp_parser_compound_statement (parser, NULL, false, false);
9772 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9773 a statement all its own. */
9774 else if (token->type == CPP_PRAGMA)
9776 /* Only certain OpenMP pragmas are attached to statements, and thus
9777 are considered statements themselves. All others are not. In
9778 the context of a compound, accept the pragma as a "statement" and
9779 return so that we can check for a close brace. Otherwise we
9780 require a real statement and must go back and read one. */
9781 if (in_compound)
9782 cp_parser_pragma (parser, pragma_compound);
9783 else if (!cp_parser_pragma (parser, pragma_stmt))
9784 goto restart;
9785 return;
9787 else if (token->type == CPP_EOF)
9789 cp_parser_error (parser, "expected statement");
9790 return;
9793 /* Everything else must be a declaration-statement or an
9794 expression-statement. Try for the declaration-statement
9795 first, unless we are looking at a `;', in which case we know that
9796 we have an expression-statement. */
9797 if (!statement)
9799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9801 if (std_attrs != NULL_TREE)
9803 /* Attributes should be parsed as part of the the
9804 declaration, so let's un-parse them. */
9805 saved_tokens.rollback();
9806 std_attrs = NULL_TREE;
9809 cp_parser_parse_tentatively (parser);
9810 /* Try to parse the declaration-statement. */
9811 cp_parser_declaration_statement (parser);
9812 /* If that worked, we're done. */
9813 if (cp_parser_parse_definitely (parser))
9814 return;
9816 /* Look for an expression-statement instead. */
9817 statement = cp_parser_expression_statement (parser, in_statement_expr);
9820 /* Set the line number for the statement. */
9821 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9822 SET_EXPR_LOCATION (statement, statement_location);
9824 /* Note that for now, we don't do anything with c++11 statements
9825 parsed at this level. */
9826 if (std_attrs != NULL_TREE)
9827 warning_at (attrs_location,
9828 OPT_Wattributes,
9829 "attributes at the beginning of statement are ignored");
9832 /* Parse the label for a labeled-statement, i.e.
9834 identifier :
9835 case constant-expression :
9836 default :
9838 GNU Extension:
9839 case constant-expression ... constant-expression : statement
9841 When a label is parsed without errors, the label is added to the
9842 parse tree by the finish_* functions, so this function doesn't
9843 have to return the label. */
9845 static void
9846 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9848 cp_token *token;
9849 tree label = NULL_TREE;
9850 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9852 /* The next token should be an identifier. */
9853 token = cp_lexer_peek_token (parser->lexer);
9854 if (token->type != CPP_NAME
9855 && token->type != CPP_KEYWORD)
9857 cp_parser_error (parser, "expected labeled-statement");
9858 return;
9861 parser->colon_corrects_to_scope_p = false;
9862 switch (token->keyword)
9864 case RID_CASE:
9866 tree expr, expr_hi;
9867 cp_token *ellipsis;
9869 /* Consume the `case' token. */
9870 cp_lexer_consume_token (parser->lexer);
9871 /* Parse the constant-expression. */
9872 expr = cp_parser_constant_expression (parser);
9873 if (check_for_bare_parameter_packs (expr))
9874 expr = error_mark_node;
9876 ellipsis = cp_lexer_peek_token (parser->lexer);
9877 if (ellipsis->type == CPP_ELLIPSIS)
9879 /* Consume the `...' token. */
9880 cp_lexer_consume_token (parser->lexer);
9881 expr_hi = cp_parser_constant_expression (parser);
9882 if (check_for_bare_parameter_packs (expr_hi))
9883 expr_hi = error_mark_node;
9885 /* We don't need to emit warnings here, as the common code
9886 will do this for us. */
9888 else
9889 expr_hi = NULL_TREE;
9891 if (parser->in_switch_statement_p)
9892 finish_case_label (token->location, expr, expr_hi);
9893 else
9894 error_at (token->location,
9895 "case label %qE not within a switch statement",
9896 expr);
9898 break;
9900 case RID_DEFAULT:
9901 /* Consume the `default' token. */
9902 cp_lexer_consume_token (parser->lexer);
9904 if (parser->in_switch_statement_p)
9905 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9906 else
9907 error_at (token->location, "case label not within a switch statement");
9908 break;
9910 default:
9911 /* Anything else must be an ordinary label. */
9912 label = finish_label_stmt (cp_parser_identifier (parser));
9913 break;
9916 /* Require the `:' token. */
9917 cp_parser_require (parser, CPP_COLON, RT_COLON);
9919 /* An ordinary label may optionally be followed by attributes.
9920 However, this is only permitted if the attributes are then
9921 followed by a semicolon. This is because, for backward
9922 compatibility, when parsing
9923 lab: __attribute__ ((unused)) int i;
9924 we want the attribute to attach to "i", not "lab". */
9925 if (label != NULL_TREE
9926 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9928 tree attrs;
9929 cp_parser_parse_tentatively (parser);
9930 attrs = cp_parser_gnu_attributes_opt (parser);
9931 if (attrs == NULL_TREE
9932 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9933 cp_parser_abort_tentative_parse (parser);
9934 else if (!cp_parser_parse_definitely (parser))
9936 else
9937 attributes = chainon (attributes, attrs);
9940 if (attributes != NULL_TREE)
9941 cplus_decl_attributes (&label, attributes, 0);
9943 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9946 /* Parse an expression-statement.
9948 expression-statement:
9949 expression [opt] ;
9951 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9952 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9953 indicates whether this expression-statement is part of an
9954 expression statement. */
9956 static tree
9957 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9959 tree statement = NULL_TREE;
9960 cp_token *token = cp_lexer_peek_token (parser->lexer);
9962 /* If the next token is a ';', then there is no expression
9963 statement. */
9964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9966 statement = cp_parser_expression (parser);
9967 if (statement == error_mark_node
9968 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9970 cp_parser_skip_to_end_of_block_or_statement (parser);
9971 return error_mark_node;
9975 /* Give a helpful message for "A<T>::type t;" and the like. */
9976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9977 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9979 if (TREE_CODE (statement) == SCOPE_REF)
9980 error_at (token->location, "need %<typename%> before %qE because "
9981 "%qT is a dependent scope",
9982 statement, TREE_OPERAND (statement, 0));
9983 else if (is_overloaded_fn (statement)
9984 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9986 /* A::A a; */
9987 tree fn = get_first_fn (statement);
9988 error_at (token->location,
9989 "%<%T::%D%> names the constructor, not the type",
9990 DECL_CONTEXT (fn), DECL_NAME (fn));
9994 /* Consume the final `;'. */
9995 cp_parser_consume_semicolon_at_end_of_statement (parser);
9997 if (in_statement_expr
9998 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9999 /* This is the final expression statement of a statement
10000 expression. */
10001 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10002 else if (statement)
10003 statement = finish_expr_stmt (statement);
10005 return statement;
10008 /* Parse a compound-statement.
10010 compound-statement:
10011 { statement-seq [opt] }
10013 GNU extension:
10015 compound-statement:
10016 { label-declaration-seq [opt] statement-seq [opt] }
10018 label-declaration-seq:
10019 label-declaration
10020 label-declaration-seq label-declaration
10022 Returns a tree representing the statement. */
10024 static tree
10025 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10026 bool in_try, bool function_body)
10028 tree compound_stmt;
10030 /* Consume the `{'. */
10031 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10032 return error_mark_node;
10033 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10034 && !function_body && cxx_dialect < cxx14)
10035 pedwarn (input_location, OPT_Wpedantic,
10036 "compound-statement in constexpr function");
10037 /* Begin the compound-statement. */
10038 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10039 /* If the next keyword is `__label__' we have a label declaration. */
10040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10041 cp_parser_label_declaration (parser);
10042 /* Parse an (optional) statement-seq. */
10043 cp_parser_statement_seq_opt (parser, in_statement_expr);
10044 /* Finish the compound-statement. */
10045 finish_compound_stmt (compound_stmt);
10046 /* Consume the `}'. */
10047 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10049 return compound_stmt;
10052 /* Parse an (optional) statement-seq.
10054 statement-seq:
10055 statement
10056 statement-seq [opt] statement */
10058 static void
10059 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10061 /* Scan statements until there aren't any more. */
10062 while (true)
10064 cp_token *token = cp_lexer_peek_token (parser->lexer);
10066 /* If we are looking at a `}', then we have run out of
10067 statements; the same is true if we have reached the end
10068 of file, or have stumbled upon a stray '@end'. */
10069 if (token->type == CPP_CLOSE_BRACE
10070 || token->type == CPP_EOF
10071 || token->type == CPP_PRAGMA_EOL
10072 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10073 break;
10075 /* If we are in a compound statement and find 'else' then
10076 something went wrong. */
10077 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10079 if (parser->in_statement & IN_IF_STMT)
10080 break;
10081 else
10083 token = cp_lexer_consume_token (parser->lexer);
10084 error_at (token->location, "%<else%> without a previous %<if%>");
10088 /* Parse the statement. */
10089 cp_parser_statement (parser, in_statement_expr, true, NULL);
10093 /* Parse a selection-statement.
10095 selection-statement:
10096 if ( condition ) statement
10097 if ( condition ) statement else statement
10098 switch ( condition ) statement
10100 Returns the new IF_STMT or SWITCH_STMT.
10102 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10103 is a (possibly labeled) if statement which is not enclosed in
10104 braces and has an else clause. This is used to implement
10105 -Wparentheses. */
10107 static tree
10108 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10110 cp_token *token;
10111 enum rid keyword;
10113 if (if_p != NULL)
10114 *if_p = false;
10116 /* Peek at the next token. */
10117 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10119 /* See what kind of keyword it is. */
10120 keyword = token->keyword;
10121 switch (keyword)
10123 case RID_IF:
10124 case RID_SWITCH:
10126 tree statement;
10127 tree condition;
10129 /* Look for the `('. */
10130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10132 cp_parser_skip_to_end_of_statement (parser);
10133 return error_mark_node;
10136 /* Begin the selection-statement. */
10137 if (keyword == RID_IF)
10138 statement = begin_if_stmt ();
10139 else
10140 statement = begin_switch_stmt ();
10142 /* Parse the condition. */
10143 condition = cp_parser_condition (parser);
10144 /* Look for the `)'. */
10145 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10146 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10147 /*consume_paren=*/true);
10149 if (keyword == RID_IF)
10151 bool nested_if;
10152 unsigned char in_statement;
10154 /* Add the condition. */
10155 finish_if_stmt_cond (condition, statement);
10157 /* Parse the then-clause. */
10158 in_statement = parser->in_statement;
10159 parser->in_statement |= IN_IF_STMT;
10160 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10162 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10163 add_stmt (build_empty_stmt (loc));
10164 cp_lexer_consume_token (parser->lexer);
10165 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10166 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10167 "empty body in an %<if%> statement");
10168 nested_if = false;
10170 else
10171 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10172 token->location, "if");
10173 parser->in_statement = in_statement;
10175 finish_then_clause (statement);
10177 /* If the next token is `else', parse the else-clause. */
10178 if (cp_lexer_next_token_is_keyword (parser->lexer,
10179 RID_ELSE))
10181 /* Consume the `else' keyword. */
10182 location_t else_tok_loc
10183 = cp_lexer_consume_token (parser->lexer)->location;
10184 begin_else_clause (statement);
10185 /* Parse the else-clause. */
10186 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10188 location_t loc;
10189 loc = cp_lexer_peek_token (parser->lexer)->location;
10190 warning_at (loc,
10191 OPT_Wempty_body, "suggest braces around "
10192 "empty body in an %<else%> statement");
10193 add_stmt (build_empty_stmt (loc));
10194 cp_lexer_consume_token (parser->lexer);
10196 else
10197 cp_parser_implicitly_scoped_statement (parser, NULL,
10198 else_tok_loc, "else");
10200 finish_else_clause (statement);
10202 /* If we are currently parsing a then-clause, then
10203 IF_P will not be NULL. We set it to true to
10204 indicate that this if statement has an else clause.
10205 This may trigger the Wparentheses warning below
10206 when we get back up to the parent if statement. */
10207 if (if_p != NULL)
10208 *if_p = true;
10210 else
10212 /* This if statement does not have an else clause. If
10213 NESTED_IF is true, then the then-clause is an if
10214 statement which does have an else clause. We warn
10215 about the potential ambiguity. */
10216 if (nested_if)
10217 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10218 "suggest explicit braces to avoid ambiguous"
10219 " %<else%>");
10222 /* Now we're all done with the if-statement. */
10223 finish_if_stmt (statement);
10225 else
10227 bool in_switch_statement_p;
10228 unsigned char in_statement;
10230 /* Add the condition. */
10231 finish_switch_cond (condition, statement);
10233 /* Parse the body of the switch-statement. */
10234 in_switch_statement_p = parser->in_switch_statement_p;
10235 in_statement = parser->in_statement;
10236 parser->in_switch_statement_p = true;
10237 parser->in_statement |= IN_SWITCH_STMT;
10238 cp_parser_implicitly_scoped_statement (parser, NULL,
10239 0, "switch");
10240 parser->in_switch_statement_p = in_switch_statement_p;
10241 parser->in_statement = in_statement;
10243 /* Now we're all done with the switch-statement. */
10244 finish_switch_stmt (statement);
10247 return statement;
10249 break;
10251 default:
10252 cp_parser_error (parser, "expected selection-statement");
10253 return error_mark_node;
10257 /* Parse a condition.
10259 condition:
10260 expression
10261 type-specifier-seq declarator = initializer-clause
10262 type-specifier-seq declarator braced-init-list
10264 GNU Extension:
10266 condition:
10267 type-specifier-seq declarator asm-specification [opt]
10268 attributes [opt] = assignment-expression
10270 Returns the expression that should be tested. */
10272 static tree
10273 cp_parser_condition (cp_parser* parser)
10275 cp_decl_specifier_seq type_specifiers;
10276 const char *saved_message;
10277 int declares_class_or_enum;
10279 /* Try the declaration first. */
10280 cp_parser_parse_tentatively (parser);
10281 /* New types are not allowed in the type-specifier-seq for a
10282 condition. */
10283 saved_message = parser->type_definition_forbidden_message;
10284 parser->type_definition_forbidden_message
10285 = G_("types may not be defined in conditions");
10286 /* Parse the type-specifier-seq. */
10287 cp_parser_decl_specifier_seq (parser,
10288 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10289 &type_specifiers,
10290 &declares_class_or_enum);
10291 /* Restore the saved message. */
10292 parser->type_definition_forbidden_message = saved_message;
10293 /* If all is well, we might be looking at a declaration. */
10294 if (!cp_parser_error_occurred (parser))
10296 tree decl;
10297 tree asm_specification;
10298 tree attributes;
10299 cp_declarator *declarator;
10300 tree initializer = NULL_TREE;
10302 /* Parse the declarator. */
10303 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10304 /*ctor_dtor_or_conv_p=*/NULL,
10305 /*parenthesized_p=*/NULL,
10306 /*member_p=*/false,
10307 /*friend_p=*/false);
10308 /* Parse the attributes. */
10309 attributes = cp_parser_attributes_opt (parser);
10310 /* Parse the asm-specification. */
10311 asm_specification = cp_parser_asm_specification_opt (parser);
10312 /* If the next token is not an `=' or '{', then we might still be
10313 looking at an expression. For example:
10315 if (A(a).x)
10317 looks like a decl-specifier-seq and a declarator -- but then
10318 there is no `=', so this is an expression. */
10319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10320 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10321 cp_parser_simulate_error (parser);
10323 /* If we did see an `=' or '{', then we are looking at a declaration
10324 for sure. */
10325 if (cp_parser_parse_definitely (parser))
10327 tree pushed_scope;
10328 bool non_constant_p;
10329 bool flags = LOOKUP_ONLYCONVERTING;
10331 /* Create the declaration. */
10332 decl = start_decl (declarator, &type_specifiers,
10333 /*initialized_p=*/true,
10334 attributes, /*prefix_attributes=*/NULL_TREE,
10335 &pushed_scope);
10337 /* Parse the initializer. */
10338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10340 initializer = cp_parser_braced_list (parser, &non_constant_p);
10341 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10342 flags = 0;
10344 else
10346 /* Consume the `='. */
10347 cp_parser_require (parser, CPP_EQ, RT_EQ);
10348 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10350 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10351 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10353 /* Process the initializer. */
10354 cp_finish_decl (decl,
10355 initializer, !non_constant_p,
10356 asm_specification,
10357 flags);
10359 if (pushed_scope)
10360 pop_scope (pushed_scope);
10362 return convert_from_reference (decl);
10365 /* If we didn't even get past the declarator successfully, we are
10366 definitely not looking at a declaration. */
10367 else
10368 cp_parser_abort_tentative_parse (parser);
10370 /* Otherwise, we are looking at an expression. */
10371 return cp_parser_expression (parser);
10374 /* Parses a for-statement or range-for-statement until the closing ')',
10375 not included. */
10377 static tree
10378 cp_parser_for (cp_parser *parser, bool ivdep)
10380 tree init, scope, decl;
10381 bool is_range_for;
10383 /* Begin the for-statement. */
10384 scope = begin_for_scope (&init);
10386 /* Parse the initialization. */
10387 is_range_for = cp_parser_for_init_statement (parser, &decl);
10389 if (is_range_for)
10390 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10391 else
10392 return cp_parser_c_for (parser, scope, init, ivdep);
10395 static tree
10396 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10398 /* Normal for loop */
10399 tree condition = NULL_TREE;
10400 tree expression = NULL_TREE;
10401 tree stmt;
10403 stmt = begin_for_stmt (scope, init);
10404 /* The for-init-statement has already been parsed in
10405 cp_parser_for_init_statement, so no work is needed here. */
10406 finish_for_init_stmt (stmt);
10408 /* If there's a condition, process it. */
10409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10410 condition = cp_parser_condition (parser);
10411 else if (ivdep)
10413 cp_parser_error (parser, "missing loop condition in loop with "
10414 "%<GCC ivdep%> pragma");
10415 condition = error_mark_node;
10417 finish_for_cond (condition, stmt, ivdep);
10418 /* Look for the `;'. */
10419 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10421 /* If there's an expression, process it. */
10422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10423 expression = cp_parser_expression (parser);
10424 finish_for_expr (expression, stmt);
10426 return stmt;
10429 /* Tries to parse a range-based for-statement:
10431 range-based-for:
10432 decl-specifier-seq declarator : expression
10434 The decl-specifier-seq declarator and the `:' are already parsed by
10435 cp_parser_for_init_statement. If processing_template_decl it returns a
10436 newly created RANGE_FOR_STMT; if not, it is converted to a
10437 regular FOR_STMT. */
10439 static tree
10440 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10441 bool ivdep)
10443 tree stmt, range_expr;
10445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10447 bool expr_non_constant_p;
10448 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10450 else
10451 range_expr = cp_parser_expression (parser);
10453 /* If in template, STMT is converted to a normal for-statement
10454 at instantiation. If not, it is done just ahead. */
10455 if (processing_template_decl)
10457 if (check_for_bare_parameter_packs (range_expr))
10458 range_expr = error_mark_node;
10459 stmt = begin_range_for_stmt (scope, init);
10460 if (ivdep)
10461 RANGE_FOR_IVDEP (stmt) = 1;
10462 finish_range_for_decl (stmt, range_decl, range_expr);
10463 if (!type_dependent_expression_p (range_expr)
10464 /* do_auto_deduction doesn't mess with template init-lists. */
10465 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10466 do_range_for_auto_deduction (range_decl, range_expr);
10468 else
10470 stmt = begin_for_stmt (scope, init);
10471 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10473 return stmt;
10476 /* Subroutine of cp_convert_range_for: given the initializer expression,
10477 builds up the range temporary. */
10479 static tree
10480 build_range_temp (tree range_expr)
10482 tree range_type, range_temp;
10484 /* Find out the type deduced by the declaration
10485 `auto &&__range = range_expr'. */
10486 range_type = cp_build_reference_type (make_auto (), true);
10487 range_type = do_auto_deduction (range_type, range_expr,
10488 type_uses_auto (range_type));
10490 /* Create the __range variable. */
10491 range_temp = build_decl (input_location, VAR_DECL,
10492 get_identifier ("__for_range"), range_type);
10493 TREE_USED (range_temp) = 1;
10494 DECL_ARTIFICIAL (range_temp) = 1;
10496 return range_temp;
10499 /* Used by cp_parser_range_for in template context: we aren't going to
10500 do a full conversion yet, but we still need to resolve auto in the
10501 type of the for-range-declaration if present. This is basically
10502 a shortcut version of cp_convert_range_for. */
10504 static void
10505 do_range_for_auto_deduction (tree decl, tree range_expr)
10507 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10508 if (auto_node)
10510 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10511 range_temp = convert_from_reference (build_range_temp (range_expr));
10512 iter_type = (cp_parser_perform_range_for_lookup
10513 (range_temp, &begin_dummy, &end_dummy));
10514 if (iter_type)
10516 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10517 iter_type);
10518 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10519 tf_warning_or_error);
10520 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10521 iter_decl, auto_node);
10526 /* Converts a range-based for-statement into a normal
10527 for-statement, as per the definition.
10529 for (RANGE_DECL : RANGE_EXPR)
10530 BLOCK
10532 should be equivalent to:
10535 auto &&__range = RANGE_EXPR;
10536 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10537 __begin != __end;
10538 ++__begin)
10540 RANGE_DECL = *__begin;
10541 BLOCK
10545 If RANGE_EXPR is an array:
10546 BEGIN_EXPR = __range
10547 END_EXPR = __range + ARRAY_SIZE(__range)
10548 Else if RANGE_EXPR has a member 'begin' or 'end':
10549 BEGIN_EXPR = __range.begin()
10550 END_EXPR = __range.end()
10551 Else:
10552 BEGIN_EXPR = begin(__range)
10553 END_EXPR = end(__range);
10555 If __range has a member 'begin' but not 'end', or vice versa, we must
10556 still use the second alternative (it will surely fail, however).
10557 When calling begin()/end() in the third alternative we must use
10558 argument dependent lookup, but always considering 'std' as an associated
10559 namespace. */
10561 tree
10562 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10563 bool ivdep)
10565 tree begin, end;
10566 tree iter_type, begin_expr, end_expr;
10567 tree condition, expression;
10569 if (range_decl == error_mark_node || range_expr == error_mark_node)
10570 /* If an error happened previously do nothing or else a lot of
10571 unhelpful errors would be issued. */
10572 begin_expr = end_expr = iter_type = error_mark_node;
10573 else
10575 tree range_temp;
10577 if (VAR_P (range_expr)
10578 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10579 /* Can't bind a reference to an array of runtime bound. */
10580 range_temp = range_expr;
10581 else
10583 range_temp = build_range_temp (range_expr);
10584 pushdecl (range_temp);
10585 cp_finish_decl (range_temp, range_expr,
10586 /*is_constant_init*/false, NULL_TREE,
10587 LOOKUP_ONLYCONVERTING);
10588 range_temp = convert_from_reference (range_temp);
10590 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10591 &begin_expr, &end_expr);
10594 /* The new for initialization statement. */
10595 begin = build_decl (input_location, VAR_DECL,
10596 get_identifier ("__for_begin"), iter_type);
10597 TREE_USED (begin) = 1;
10598 DECL_ARTIFICIAL (begin) = 1;
10599 pushdecl (begin);
10600 cp_finish_decl (begin, begin_expr,
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10604 end = build_decl (input_location, VAR_DECL,
10605 get_identifier ("__for_end"), iter_type);
10606 TREE_USED (end) = 1;
10607 DECL_ARTIFICIAL (end) = 1;
10608 pushdecl (end);
10609 cp_finish_decl (end, end_expr,
10610 /*is_constant_init*/false, NULL_TREE,
10611 LOOKUP_ONLYCONVERTING);
10613 finish_for_init_stmt (statement);
10615 /* The new for condition. */
10616 condition = build_x_binary_op (input_location, NE_EXPR,
10617 begin, ERROR_MARK,
10618 end, ERROR_MARK,
10619 NULL, tf_warning_or_error);
10620 finish_for_cond (condition, statement, ivdep);
10622 /* The new increment expression. */
10623 expression = finish_unary_op_expr (input_location,
10624 PREINCREMENT_EXPR, begin,
10625 tf_warning_or_error);
10626 finish_for_expr (expression, statement);
10628 /* The declaration is initialized with *__begin inside the loop body. */
10629 cp_finish_decl (range_decl,
10630 build_x_indirect_ref (input_location, begin, RO_NULL,
10631 tf_warning_or_error),
10632 /*is_constant_init*/false, NULL_TREE,
10633 LOOKUP_ONLYCONVERTING);
10635 return statement;
10638 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10639 We need to solve both at the same time because the method used
10640 depends on the existence of members begin or end.
10641 Returns the type deduced for the iterator expression. */
10643 static tree
10644 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10646 if (error_operand_p (range))
10648 *begin = *end = error_mark_node;
10649 return error_mark_node;
10652 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10654 error ("range-based %<for%> expression of type %qT "
10655 "has incomplete type", TREE_TYPE (range));
10656 *begin = *end = error_mark_node;
10657 return error_mark_node;
10659 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10661 /* If RANGE is an array, we will use pointer arithmetic. */
10662 *begin = range;
10663 *end = build_binary_op (input_location, PLUS_EXPR,
10664 range,
10665 array_type_nelts_top (TREE_TYPE (range)),
10667 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10669 else
10671 /* If it is not an array, we must do a bit of magic. */
10672 tree id_begin, id_end;
10673 tree member_begin, member_end;
10675 *begin = *end = error_mark_node;
10677 id_begin = get_identifier ("begin");
10678 id_end = get_identifier ("end");
10679 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10680 /*protect=*/2, /*want_type=*/false,
10681 tf_warning_or_error);
10682 member_end = lookup_member (TREE_TYPE (range), id_end,
10683 /*protect=*/2, /*want_type=*/false,
10684 tf_warning_or_error);
10686 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10688 /* Use the member functions. */
10689 if (member_begin != NULL_TREE)
10690 *begin = cp_parser_range_for_member_function (range, id_begin);
10691 else
10692 error ("range-based %<for%> expression of type %qT has an "
10693 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10695 if (member_end != NULL_TREE)
10696 *end = cp_parser_range_for_member_function (range, id_end);
10697 else
10698 error ("range-based %<for%> expression of type %qT has a "
10699 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10701 else
10703 /* Use global functions with ADL. */
10704 vec<tree, va_gc> *vec;
10705 vec = make_tree_vector ();
10707 vec_safe_push (vec, range);
10709 member_begin = perform_koenig_lookup (id_begin, vec,
10710 tf_warning_or_error);
10711 *begin = finish_call_expr (member_begin, &vec, false, true,
10712 tf_warning_or_error);
10713 member_end = perform_koenig_lookup (id_end, vec,
10714 tf_warning_or_error);
10715 *end = finish_call_expr (member_end, &vec, false, true,
10716 tf_warning_or_error);
10718 release_tree_vector (vec);
10721 /* Last common checks. */
10722 if (*begin == error_mark_node || *end == error_mark_node)
10724 /* If one of the expressions is an error do no more checks. */
10725 *begin = *end = error_mark_node;
10726 return error_mark_node;
10728 else if (type_dependent_expression_p (*begin)
10729 || type_dependent_expression_p (*end))
10730 /* Can happen, when, eg, in a template context, Koenig lookup
10731 can't resolve begin/end (c++/58503). */
10732 return NULL_TREE;
10733 else
10735 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10736 /* The unqualified type of the __begin and __end temporaries should
10737 be the same, as required by the multiple auto declaration. */
10738 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10739 error ("inconsistent begin/end types in range-based %<for%> "
10740 "statement: %qT and %qT",
10741 TREE_TYPE (*begin), TREE_TYPE (*end));
10742 return iter_type;
10747 /* Helper function for cp_parser_perform_range_for_lookup.
10748 Builds a tree for RANGE.IDENTIFIER(). */
10750 static tree
10751 cp_parser_range_for_member_function (tree range, tree identifier)
10753 tree member, res;
10754 vec<tree, va_gc> *vec;
10756 member = finish_class_member_access_expr (range, identifier,
10757 false, tf_warning_or_error);
10758 if (member == error_mark_node)
10759 return error_mark_node;
10761 vec = make_tree_vector ();
10762 res = finish_call_expr (member, &vec,
10763 /*disallow_virtual=*/false,
10764 /*koenig_p=*/false,
10765 tf_warning_or_error);
10766 release_tree_vector (vec);
10767 return res;
10770 /* Parse an iteration-statement.
10772 iteration-statement:
10773 while ( condition ) statement
10774 do statement while ( expression ) ;
10775 for ( for-init-statement condition [opt] ; expression [opt] )
10776 statement
10778 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10780 static tree
10781 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10783 cp_token *token;
10784 location_t tok_loc;
10785 enum rid keyword;
10786 tree statement;
10787 unsigned char in_statement;
10789 /* Peek at the next token. */
10790 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10791 if (!token)
10792 return error_mark_node;
10794 tok_loc = token->location;
10796 /* Remember whether or not we are already within an iteration
10797 statement. */
10798 in_statement = parser->in_statement;
10800 /* See what kind of keyword it is. */
10801 keyword = token->keyword;
10802 switch (keyword)
10804 case RID_WHILE:
10806 tree condition;
10808 /* Begin the while-statement. */
10809 statement = begin_while_stmt ();
10810 /* Look for the `('. */
10811 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10812 /* Parse the condition. */
10813 condition = cp_parser_condition (parser);
10814 finish_while_stmt_cond (condition, statement, ivdep);
10815 /* Look for the `)'. */
10816 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10817 /* Parse the dependent statement. */
10818 parser->in_statement = IN_ITERATION_STMT;
10819 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10820 parser->in_statement = in_statement;
10821 /* We're done with the while-statement. */
10822 finish_while_stmt (statement);
10824 break;
10826 case RID_DO:
10828 tree expression;
10830 /* Begin the do-statement. */
10831 statement = begin_do_stmt ();
10832 /* Parse the body of the do-statement. */
10833 parser->in_statement = IN_ITERATION_STMT;
10834 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10835 parser->in_statement = in_statement;
10836 finish_do_body (statement);
10837 /* Look for the `while' keyword. */
10838 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10839 /* Look for the `('. */
10840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10841 /* Parse the expression. */
10842 expression = cp_parser_expression (parser);
10843 /* We're done with the do-statement. */
10844 finish_do_stmt (expression, statement, ivdep);
10845 /* Look for the `)'. */
10846 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10847 /* Look for the `;'. */
10848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10850 break;
10852 case RID_FOR:
10854 /* Look for the `('. */
10855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10857 statement = cp_parser_for (parser, ivdep);
10859 /* Look for the `)'. */
10860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10862 /* Parse the body of the for-statement. */
10863 parser->in_statement = IN_ITERATION_STMT;
10864 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10865 parser->in_statement = in_statement;
10867 /* We're done with the for-statement. */
10868 finish_for_stmt (statement);
10870 break;
10872 default:
10873 cp_parser_error (parser, "expected iteration-statement");
10874 statement = error_mark_node;
10875 break;
10878 return statement;
10881 /* Parse a for-init-statement or the declarator of a range-based-for.
10882 Returns true if a range-based-for declaration is seen.
10884 for-init-statement:
10885 expression-statement
10886 simple-declaration */
10888 static bool
10889 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10891 /* If the next token is a `;', then we have an empty
10892 expression-statement. Grammatically, this is also a
10893 simple-declaration, but an invalid one, because it does not
10894 declare anything. Therefore, if we did not handle this case
10895 specially, we would issue an error message about an invalid
10896 declaration. */
10897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10899 bool is_range_for = false;
10900 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10902 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10903 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10905 /* N3994 -- for (id : init) ... */
10906 if (cxx_dialect < cxx1z)
10907 pedwarn (input_location, 0, "range-based for loop without a "
10908 "type-specifier only available with "
10909 "-std=c++1z or -std=gnu++1z");
10910 tree name = cp_parser_identifier (parser);
10911 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10912 *decl = build_decl (input_location, VAR_DECL, name, type);
10913 pushdecl (*decl);
10914 cp_lexer_consume_token (parser->lexer);
10915 return true;
10918 /* A colon is used in range-based for. */
10919 parser->colon_corrects_to_scope_p = false;
10921 /* We're going to speculatively look for a declaration, falling back
10922 to an expression, if necessary. */
10923 cp_parser_parse_tentatively (parser);
10924 /* Parse the declaration. */
10925 cp_parser_simple_declaration (parser,
10926 /*function_definition_allowed_p=*/false,
10927 decl);
10928 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10931 /* It is a range-for, consume the ':' */
10932 cp_lexer_consume_token (parser->lexer);
10933 is_range_for = true;
10934 if (cxx_dialect < cxx11)
10936 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10937 "range-based %<for%> loops only available with "
10938 "-std=c++11 or -std=gnu++11");
10939 *decl = error_mark_node;
10942 else
10943 /* The ';' is not consumed yet because we told
10944 cp_parser_simple_declaration not to. */
10945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10947 if (cp_parser_parse_definitely (parser))
10948 return is_range_for;
10949 /* If the tentative parse failed, then we shall need to look for an
10950 expression-statement. */
10952 /* If we are here, it is an expression-statement. */
10953 cp_parser_expression_statement (parser, NULL_TREE);
10954 return false;
10957 /* Parse a jump-statement.
10959 jump-statement:
10960 break ;
10961 continue ;
10962 return expression [opt] ;
10963 return braced-init-list ;
10964 goto identifier ;
10966 GNU extension:
10968 jump-statement:
10969 goto * expression ;
10971 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10973 static tree
10974 cp_parser_jump_statement (cp_parser* parser)
10976 tree statement = error_mark_node;
10977 cp_token *token;
10978 enum rid keyword;
10979 unsigned char in_statement;
10981 /* Peek at the next token. */
10982 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10983 if (!token)
10984 return error_mark_node;
10986 /* See what kind of keyword it is. */
10987 keyword = token->keyword;
10988 switch (keyword)
10990 case RID_BREAK:
10991 in_statement = parser->in_statement & ~IN_IF_STMT;
10992 switch (in_statement)
10994 case 0:
10995 error_at (token->location, "break statement not within loop or switch");
10996 break;
10997 default:
10998 gcc_assert ((in_statement & IN_SWITCH_STMT)
10999 || in_statement == IN_ITERATION_STMT);
11000 statement = finish_break_stmt ();
11001 if (in_statement == IN_ITERATION_STMT)
11002 break_maybe_infinite_loop ();
11003 break;
11004 case IN_OMP_BLOCK:
11005 error_at (token->location, "invalid exit from OpenMP structured block");
11006 break;
11007 case IN_OMP_FOR:
11008 error_at (token->location, "break statement used with OpenMP for loop");
11009 break;
11010 case IN_CILK_SIMD_FOR:
11011 error_at (token->location, "break statement used with Cilk Plus for loop");
11012 break;
11014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11015 break;
11017 case RID_CONTINUE:
11018 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11020 case 0:
11021 error_at (token->location, "continue statement not within a loop");
11022 break;
11023 case IN_CILK_SIMD_FOR:
11024 error_at (token->location,
11025 "continue statement within %<#pragma simd%> loop body");
11026 /* Fall through. */
11027 case IN_ITERATION_STMT:
11028 case IN_OMP_FOR:
11029 statement = finish_continue_stmt ();
11030 break;
11031 case IN_OMP_BLOCK:
11032 error_at (token->location, "invalid exit from OpenMP structured block");
11033 break;
11034 default:
11035 gcc_unreachable ();
11037 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11038 break;
11040 case RID_RETURN:
11042 tree expr;
11043 bool expr_non_constant_p;
11045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11047 cp_lexer_set_source_position (parser->lexer);
11048 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11049 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11051 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11052 expr = cp_parser_expression (parser);
11053 else
11054 /* If the next token is a `;', then there is no
11055 expression. */
11056 expr = NULL_TREE;
11057 /* Build the return-statement. */
11058 statement = finish_return_stmt (expr);
11059 /* Look for the final `;'. */
11060 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11062 break;
11064 case RID_GOTO:
11065 if (parser->in_function_body
11066 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11068 error ("%<goto%> in %<constexpr%> function");
11069 cp_function_chain->invalid_constexpr = true;
11072 /* Create the goto-statement. */
11073 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11075 /* Issue a warning about this use of a GNU extension. */
11076 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11077 /* Consume the '*' token. */
11078 cp_lexer_consume_token (parser->lexer);
11079 /* Parse the dependent expression. */
11080 finish_goto_stmt (cp_parser_expression (parser));
11082 else
11083 finish_goto_stmt (cp_parser_identifier (parser));
11084 /* Look for the final `;'. */
11085 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11086 break;
11088 default:
11089 cp_parser_error (parser, "expected jump-statement");
11090 break;
11093 return statement;
11096 /* Parse a declaration-statement.
11098 declaration-statement:
11099 block-declaration */
11101 static void
11102 cp_parser_declaration_statement (cp_parser* parser)
11104 void *p;
11106 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11107 p = obstack_alloc (&declarator_obstack, 0);
11109 /* Parse the block-declaration. */
11110 cp_parser_block_declaration (parser, /*statement_p=*/true);
11112 /* Free any declarators allocated. */
11113 obstack_free (&declarator_obstack, p);
11116 /* Some dependent statements (like `if (cond) statement'), are
11117 implicitly in their own scope. In other words, if the statement is
11118 a single statement (as opposed to a compound-statement), it is
11119 none-the-less treated as if it were enclosed in braces. Any
11120 declarations appearing in the dependent statement are out of scope
11121 after control passes that point. This function parses a statement,
11122 but ensures that is in its own scope, even if it is not a
11123 compound-statement.
11125 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11126 is a (possibly labeled) if statement which is not enclosed in
11127 braces and has an else clause. This is used to implement
11128 -Wparentheses.
11130 Returns the new statement. */
11132 static tree
11133 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11134 location_t guard_loc,
11135 const char *guard_kind)
11137 tree statement;
11139 if (if_p != NULL)
11140 *if_p = false;
11142 /* Mark if () ; with a special NOP_EXPR. */
11143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11145 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11146 cp_lexer_consume_token (parser->lexer);
11147 statement = add_stmt (build_empty_stmt (loc));
11149 /* if a compound is opened, we simply parse the statement directly. */
11150 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11151 statement = cp_parser_compound_statement (parser, NULL, false, false);
11152 /* If the token is not a `{', then we must take special action. */
11153 else
11155 /* Create a compound-statement. */
11156 statement = begin_compound_stmt (0);
11157 /* Parse the dependent-statement. */
11158 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11159 cp_parser_statement (parser, NULL_TREE, false, if_p);
11160 /* Finish the dummy compound-statement. */
11161 finish_compound_stmt (statement);
11162 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11163 if (next_tok->keyword != RID_ELSE)
11165 location_t next_stmt_loc = next_tok->location;
11166 warn_for_misleading_indentation (guard_loc, body_loc,
11167 next_stmt_loc, next_tok->type,
11168 guard_kind);
11172 /* Return the statement. */
11173 return statement;
11176 /* For some dependent statements (like `while (cond) statement'), we
11177 have already created a scope. Therefore, even if the dependent
11178 statement is a compound-statement, we do not want to create another
11179 scope. */
11181 static void
11182 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11183 const char *guard_kind)
11185 /* If the token is a `{', then we must take special action. */
11186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11188 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11189 cp_parser_statement (parser, NULL_TREE, false, NULL);
11190 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11191 location_t next_stmt_loc = next_tok->location;
11192 warn_for_misleading_indentation (guard_loc, body_loc,
11193 next_stmt_loc, next_tok->type,
11194 guard_kind);
11196 else
11198 /* Avoid calling cp_parser_compound_statement, so that we
11199 don't create a new scope. Do everything else by hand. */
11200 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11201 /* If the next keyword is `__label__' we have a label declaration. */
11202 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11203 cp_parser_label_declaration (parser);
11204 /* Parse an (optional) statement-seq. */
11205 cp_parser_statement_seq_opt (parser, NULL_TREE);
11206 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11210 /* Declarations [gram.dcl.dcl] */
11212 /* Parse an optional declaration-sequence.
11214 declaration-seq:
11215 declaration
11216 declaration-seq declaration */
11218 static void
11219 cp_parser_declaration_seq_opt (cp_parser* parser)
11221 while (true)
11223 cp_token *token;
11225 token = cp_lexer_peek_token (parser->lexer);
11227 if (token->type == CPP_CLOSE_BRACE
11228 || token->type == CPP_EOF
11229 || token->type == CPP_PRAGMA_EOL)
11230 break;
11232 if (token->type == CPP_SEMICOLON)
11234 /* A declaration consisting of a single semicolon is
11235 invalid. Allow it unless we're being pedantic. */
11236 cp_lexer_consume_token (parser->lexer);
11237 if (!in_system_header_at (input_location))
11238 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11239 continue;
11242 /* If we're entering or exiting a region that's implicitly
11243 extern "C", modify the lang context appropriately. */
11244 if (!parser->implicit_extern_c && token->implicit_extern_c)
11246 push_lang_context (lang_name_c);
11247 parser->implicit_extern_c = true;
11249 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11251 pop_lang_context ();
11252 parser->implicit_extern_c = false;
11255 if (token->type == CPP_PRAGMA)
11257 /* A top-level declaration can consist solely of a #pragma.
11258 A nested declaration cannot, so this is done here and not
11259 in cp_parser_declaration. (A #pragma at block scope is
11260 handled in cp_parser_statement.) */
11261 cp_parser_pragma (parser, pragma_external);
11262 continue;
11265 /* Parse the declaration itself. */
11266 cp_parser_declaration (parser);
11270 /* Parse a declaration.
11272 declaration:
11273 block-declaration
11274 function-definition
11275 template-declaration
11276 explicit-instantiation
11277 explicit-specialization
11278 linkage-specification
11279 namespace-definition
11281 GNU extension:
11283 declaration:
11284 __extension__ declaration */
11286 static void
11287 cp_parser_declaration (cp_parser* parser)
11289 cp_token token1;
11290 cp_token token2;
11291 int saved_pedantic;
11292 void *p;
11293 tree attributes = NULL_TREE;
11295 /* Check for the `__extension__' keyword. */
11296 if (cp_parser_extension_opt (parser, &saved_pedantic))
11298 /* Parse the qualified declaration. */
11299 cp_parser_declaration (parser);
11300 /* Restore the PEDANTIC flag. */
11301 pedantic = saved_pedantic;
11303 return;
11306 /* Try to figure out what kind of declaration is present. */
11307 token1 = *cp_lexer_peek_token (parser->lexer);
11309 if (token1.type != CPP_EOF)
11310 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11311 else
11313 token2.type = CPP_EOF;
11314 token2.keyword = RID_MAX;
11317 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11318 p = obstack_alloc (&declarator_obstack, 0);
11320 /* If the next token is `extern' and the following token is a string
11321 literal, then we have a linkage specification. */
11322 if (token1.keyword == RID_EXTERN
11323 && cp_parser_is_pure_string_literal (&token2))
11324 cp_parser_linkage_specification (parser);
11325 /* If the next token is `template', then we have either a template
11326 declaration, an explicit instantiation, or an explicit
11327 specialization. */
11328 else if (token1.keyword == RID_TEMPLATE)
11330 /* `template <>' indicates a template specialization. */
11331 if (token2.type == CPP_LESS
11332 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11333 cp_parser_explicit_specialization (parser);
11334 /* `template <' indicates a template declaration. */
11335 else if (token2.type == CPP_LESS)
11336 cp_parser_template_declaration (parser, /*member_p=*/false);
11337 /* Anything else must be an explicit instantiation. */
11338 else
11339 cp_parser_explicit_instantiation (parser);
11341 /* If the next token is `export', then we have a template
11342 declaration. */
11343 else if (token1.keyword == RID_EXPORT)
11344 cp_parser_template_declaration (parser, /*member_p=*/false);
11345 /* If the next token is `extern', 'static' or 'inline' and the one
11346 after that is `template', we have a GNU extended explicit
11347 instantiation directive. */
11348 else if (cp_parser_allow_gnu_extensions_p (parser)
11349 && (token1.keyword == RID_EXTERN
11350 || token1.keyword == RID_STATIC
11351 || token1.keyword == RID_INLINE)
11352 && token2.keyword == RID_TEMPLATE)
11353 cp_parser_explicit_instantiation (parser);
11354 /* If the next token is `namespace', check for a named or unnamed
11355 namespace definition. */
11356 else if (token1.keyword == RID_NAMESPACE
11357 && (/* A named namespace definition. */
11358 (token2.type == CPP_NAME
11359 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11360 != CPP_EQ))
11361 /* An unnamed namespace definition. */
11362 || token2.type == CPP_OPEN_BRACE
11363 || token2.keyword == RID_ATTRIBUTE))
11364 cp_parser_namespace_definition (parser);
11365 /* An inline (associated) namespace definition. */
11366 else if (token1.keyword == RID_INLINE
11367 && token2.keyword == RID_NAMESPACE)
11368 cp_parser_namespace_definition (parser);
11369 /* Objective-C++ declaration/definition. */
11370 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11371 cp_parser_objc_declaration (parser, NULL_TREE);
11372 else if (c_dialect_objc ()
11373 && token1.keyword == RID_ATTRIBUTE
11374 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11375 cp_parser_objc_declaration (parser, attributes);
11376 /* We must have either a block declaration or a function
11377 definition. */
11378 else
11379 /* Try to parse a block-declaration, or a function-definition. */
11380 cp_parser_block_declaration (parser, /*statement_p=*/false);
11382 /* Free any declarators allocated. */
11383 obstack_free (&declarator_obstack, p);
11386 /* Parse a block-declaration.
11388 block-declaration:
11389 simple-declaration
11390 asm-definition
11391 namespace-alias-definition
11392 using-declaration
11393 using-directive
11395 GNU Extension:
11397 block-declaration:
11398 __extension__ block-declaration
11400 C++0x Extension:
11402 block-declaration:
11403 static_assert-declaration
11405 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11406 part of a declaration-statement. */
11408 static void
11409 cp_parser_block_declaration (cp_parser *parser,
11410 bool statement_p)
11412 cp_token *token1;
11413 int saved_pedantic;
11415 /* Check for the `__extension__' keyword. */
11416 if (cp_parser_extension_opt (parser, &saved_pedantic))
11418 /* Parse the qualified declaration. */
11419 cp_parser_block_declaration (parser, statement_p);
11420 /* Restore the PEDANTIC flag. */
11421 pedantic = saved_pedantic;
11423 return;
11426 /* Peek at the next token to figure out which kind of declaration is
11427 present. */
11428 token1 = cp_lexer_peek_token (parser->lexer);
11430 /* If the next keyword is `asm', we have an asm-definition. */
11431 if (token1->keyword == RID_ASM)
11433 if (statement_p)
11434 cp_parser_commit_to_tentative_parse (parser);
11435 cp_parser_asm_definition (parser);
11437 /* If the next keyword is `namespace', we have a
11438 namespace-alias-definition. */
11439 else if (token1->keyword == RID_NAMESPACE)
11440 cp_parser_namespace_alias_definition (parser);
11441 /* If the next keyword is `using', we have a
11442 using-declaration, a using-directive, or an alias-declaration. */
11443 else if (token1->keyword == RID_USING)
11445 cp_token *token2;
11447 if (statement_p)
11448 cp_parser_commit_to_tentative_parse (parser);
11449 /* If the token after `using' is `namespace', then we have a
11450 using-directive. */
11451 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11452 if (token2->keyword == RID_NAMESPACE)
11453 cp_parser_using_directive (parser);
11454 /* If the second token after 'using' is '=', then we have an
11455 alias-declaration. */
11456 else if (cxx_dialect >= cxx11
11457 && token2->type == CPP_NAME
11458 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11459 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11460 cp_parser_alias_declaration (parser);
11461 /* Otherwise, it's a using-declaration. */
11462 else
11463 cp_parser_using_declaration (parser,
11464 /*access_declaration_p=*/false);
11466 /* If the next keyword is `__label__' we have a misplaced label
11467 declaration. */
11468 else if (token1->keyword == RID_LABEL)
11470 cp_lexer_consume_token (parser->lexer);
11471 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11472 cp_parser_skip_to_end_of_statement (parser);
11473 /* If the next token is now a `;', consume it. */
11474 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11475 cp_lexer_consume_token (parser->lexer);
11477 /* If the next token is `static_assert' we have a static assertion. */
11478 else if (token1->keyword == RID_STATIC_ASSERT)
11479 cp_parser_static_assert (parser, /*member_p=*/false);
11480 /* Anything else must be a simple-declaration. */
11481 else
11482 cp_parser_simple_declaration (parser, !statement_p,
11483 /*maybe_range_for_decl*/NULL);
11486 /* Parse a simple-declaration.
11488 simple-declaration:
11489 decl-specifier-seq [opt] init-declarator-list [opt] ;
11491 init-declarator-list:
11492 init-declarator
11493 init-declarator-list , init-declarator
11495 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11496 function-definition as a simple-declaration.
11498 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11499 parsed declaration if it is an uninitialized single declarator not followed
11500 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11501 if present, will not be consumed. */
11503 static void
11504 cp_parser_simple_declaration (cp_parser* parser,
11505 bool function_definition_allowed_p,
11506 tree *maybe_range_for_decl)
11508 cp_decl_specifier_seq decl_specifiers;
11509 int declares_class_or_enum;
11510 bool saw_declarator;
11511 location_t comma_loc = UNKNOWN_LOCATION;
11512 location_t init_loc = UNKNOWN_LOCATION;
11514 if (maybe_range_for_decl)
11515 *maybe_range_for_decl = NULL_TREE;
11517 /* Defer access checks until we know what is being declared; the
11518 checks for names appearing in the decl-specifier-seq should be
11519 done as if we were in the scope of the thing being declared. */
11520 push_deferring_access_checks (dk_deferred);
11522 /* Parse the decl-specifier-seq. We have to keep track of whether
11523 or not the decl-specifier-seq declares a named class or
11524 enumeration type, since that is the only case in which the
11525 init-declarator-list is allowed to be empty.
11527 [dcl.dcl]
11529 In a simple-declaration, the optional init-declarator-list can be
11530 omitted only when declaring a class or enumeration, that is when
11531 the decl-specifier-seq contains either a class-specifier, an
11532 elaborated-type-specifier, or an enum-specifier. */
11533 cp_parser_decl_specifier_seq (parser,
11534 CP_PARSER_FLAGS_OPTIONAL,
11535 &decl_specifiers,
11536 &declares_class_or_enum);
11537 /* We no longer need to defer access checks. */
11538 stop_deferring_access_checks ();
11540 /* In a block scope, a valid declaration must always have a
11541 decl-specifier-seq. By not trying to parse declarators, we can
11542 resolve the declaration/expression ambiguity more quickly. */
11543 if (!function_definition_allowed_p
11544 && !decl_specifiers.any_specifiers_p)
11546 cp_parser_error (parser, "expected declaration");
11547 goto done;
11550 /* If the next two tokens are both identifiers, the code is
11551 erroneous. The usual cause of this situation is code like:
11553 T t;
11555 where "T" should name a type -- but does not. */
11556 if (!decl_specifiers.any_type_specifiers_p
11557 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11559 /* If parsing tentatively, we should commit; we really are
11560 looking at a declaration. */
11561 cp_parser_commit_to_tentative_parse (parser);
11562 /* Give up. */
11563 goto done;
11566 /* If we have seen at least one decl-specifier, and the next token
11567 is not a parenthesis, then we must be looking at a declaration.
11568 (After "int (" we might be looking at a functional cast.) */
11569 if (decl_specifiers.any_specifiers_p
11570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11571 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11572 && !cp_parser_error_occurred (parser))
11573 cp_parser_commit_to_tentative_parse (parser);
11575 /* Keep going until we hit the `;' at the end of the simple
11576 declaration. */
11577 saw_declarator = false;
11578 while (cp_lexer_next_token_is_not (parser->lexer,
11579 CPP_SEMICOLON))
11581 cp_token *token;
11582 bool function_definition_p;
11583 tree decl;
11585 if (saw_declarator)
11587 /* If we are processing next declarator, comma is expected */
11588 token = cp_lexer_peek_token (parser->lexer);
11589 gcc_assert (token->type == CPP_COMMA);
11590 cp_lexer_consume_token (parser->lexer);
11591 if (maybe_range_for_decl)
11593 *maybe_range_for_decl = error_mark_node;
11594 if (comma_loc == UNKNOWN_LOCATION)
11595 comma_loc = token->location;
11598 else
11599 saw_declarator = true;
11601 /* Parse the init-declarator. */
11602 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11603 /*checks=*/NULL,
11604 function_definition_allowed_p,
11605 /*member_p=*/false,
11606 declares_class_or_enum,
11607 &function_definition_p,
11608 maybe_range_for_decl,
11609 &init_loc);
11610 /* If an error occurred while parsing tentatively, exit quickly.
11611 (That usually happens when in the body of a function; each
11612 statement is treated as a declaration-statement until proven
11613 otherwise.) */
11614 if (cp_parser_error_occurred (parser))
11615 goto done;
11616 /* Handle function definitions specially. */
11617 if (function_definition_p)
11619 /* If the next token is a `,', then we are probably
11620 processing something like:
11622 void f() {}, *p;
11624 which is erroneous. */
11625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11627 cp_token *token = cp_lexer_peek_token (parser->lexer);
11628 error_at (token->location,
11629 "mixing"
11630 " declarations and function-definitions is forbidden");
11632 /* Otherwise, we're done with the list of declarators. */
11633 else
11635 pop_deferring_access_checks ();
11636 return;
11639 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11640 *maybe_range_for_decl = decl;
11641 /* The next token should be either a `,' or a `;'. */
11642 token = cp_lexer_peek_token (parser->lexer);
11643 /* If it's a `,', there are more declarators to come. */
11644 if (token->type == CPP_COMMA)
11645 /* will be consumed next time around */;
11646 /* If it's a `;', we are done. */
11647 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11648 break;
11649 /* Anything else is an error. */
11650 else
11652 /* If we have already issued an error message we don't need
11653 to issue another one. */
11654 if (decl != error_mark_node
11655 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11656 cp_parser_error (parser, "expected %<,%> or %<;%>");
11657 /* Skip tokens until we reach the end of the statement. */
11658 cp_parser_skip_to_end_of_statement (parser);
11659 /* If the next token is now a `;', consume it. */
11660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11661 cp_lexer_consume_token (parser->lexer);
11662 goto done;
11664 /* After the first time around, a function-definition is not
11665 allowed -- even if it was OK at first. For example:
11667 int i, f() {}
11669 is not valid. */
11670 function_definition_allowed_p = false;
11673 /* Issue an error message if no declarators are present, and the
11674 decl-specifier-seq does not itself declare a class or
11675 enumeration: [dcl.dcl]/3. */
11676 if (!saw_declarator)
11678 if (cp_parser_declares_only_class_p (parser))
11680 if (!declares_class_or_enum
11681 && decl_specifiers.type
11682 && OVERLOAD_TYPE_P (decl_specifiers.type))
11683 /* Ensure an error is issued anyway when finish_decltype_type,
11684 called via cp_parser_decl_specifier_seq, returns a class or
11685 an enumeration (c++/51786). */
11686 decl_specifiers.type = NULL_TREE;
11687 shadow_tag (&decl_specifiers);
11689 /* Perform any deferred access checks. */
11690 perform_deferred_access_checks (tf_warning_or_error);
11693 /* Consume the `;'. */
11694 if (!maybe_range_for_decl)
11695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11696 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11698 if (init_loc != UNKNOWN_LOCATION)
11699 error_at (init_loc, "initializer in range-based %<for%> loop");
11700 if (comma_loc != UNKNOWN_LOCATION)
11701 error_at (comma_loc,
11702 "multiple declarations in range-based %<for%> loop");
11705 done:
11706 pop_deferring_access_checks ();
11709 /* Parse a decl-specifier-seq.
11711 decl-specifier-seq:
11712 decl-specifier-seq [opt] decl-specifier
11713 decl-specifier attribute-specifier-seq [opt] (C++11)
11715 decl-specifier:
11716 storage-class-specifier
11717 type-specifier
11718 function-specifier
11719 friend
11720 typedef
11722 GNU Extension:
11724 decl-specifier:
11725 attributes
11727 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11729 The parser flags FLAGS is used to control type-specifier parsing.
11731 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11732 flags:
11734 1: one of the decl-specifiers is an elaborated-type-specifier
11735 (i.e., a type declaration)
11736 2: one of the decl-specifiers is an enum-specifier or a
11737 class-specifier (i.e., a type definition)
11741 static void
11742 cp_parser_decl_specifier_seq (cp_parser* parser,
11743 cp_parser_flags flags,
11744 cp_decl_specifier_seq *decl_specs,
11745 int* declares_class_or_enum)
11747 bool constructor_possible_p = !parser->in_declarator_p;
11748 bool found_decl_spec = false;
11749 cp_token *start_token = NULL;
11750 cp_decl_spec ds;
11752 /* Clear DECL_SPECS. */
11753 clear_decl_specs (decl_specs);
11755 /* Assume no class or enumeration type is declared. */
11756 *declares_class_or_enum = 0;
11758 /* Keep reading specifiers until there are no more to read. */
11759 while (true)
11761 bool constructor_p;
11762 cp_token *token;
11763 ds = ds_last;
11765 /* Peek at the next token. */
11766 token = cp_lexer_peek_token (parser->lexer);
11768 /* Save the first token of the decl spec list for error
11769 reporting. */
11770 if (!start_token)
11771 start_token = token;
11772 /* Handle attributes. */
11773 if (cp_next_tokens_can_be_attribute_p (parser))
11775 /* Parse the attributes. */
11776 tree attrs = cp_parser_attributes_opt (parser);
11778 /* In a sequence of declaration specifiers, c++11 attributes
11779 appertain to the type that precede them. In that case
11780 [dcl.spec]/1 says:
11782 The attribute-specifier-seq affects the type only for
11783 the declaration it appears in, not other declarations
11784 involving the same type.
11786 But for now let's force the user to position the
11787 attribute either at the beginning of the declaration or
11788 after the declarator-id, which would clearly mean that it
11789 applies to the declarator. */
11790 if (cxx11_attribute_p (attrs))
11792 if (!found_decl_spec)
11793 /* The c++11 attribute is at the beginning of the
11794 declaration. It appertains to the entity being
11795 declared. */;
11796 else
11798 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11800 /* This is an attribute following a
11801 class-specifier. */
11802 if (decl_specs->type_definition_p)
11803 warn_misplaced_attr_for_class_type (token->location,
11804 decl_specs->type);
11805 attrs = NULL_TREE;
11807 else
11809 decl_specs->std_attributes
11810 = chainon (decl_specs->std_attributes,
11811 attrs);
11812 if (decl_specs->locations[ds_std_attribute] == 0)
11813 decl_specs->locations[ds_std_attribute] = token->location;
11815 continue;
11819 decl_specs->attributes
11820 = chainon (decl_specs->attributes,
11821 attrs);
11822 if (decl_specs->locations[ds_attribute] == 0)
11823 decl_specs->locations[ds_attribute] = token->location;
11824 continue;
11826 /* Assume we will find a decl-specifier keyword. */
11827 found_decl_spec = true;
11828 /* If the next token is an appropriate keyword, we can simply
11829 add it to the list. */
11830 switch (token->keyword)
11832 /* decl-specifier:
11833 friend
11834 constexpr */
11835 case RID_FRIEND:
11836 if (!at_class_scope_p ())
11838 error_at (token->location, "%<friend%> used outside of class");
11839 cp_lexer_purge_token (parser->lexer);
11841 else
11843 ds = ds_friend;
11844 /* Consume the token. */
11845 cp_lexer_consume_token (parser->lexer);
11847 break;
11849 case RID_CONSTEXPR:
11850 ds = ds_constexpr;
11851 cp_lexer_consume_token (parser->lexer);
11852 break;
11854 /* function-specifier:
11855 inline
11856 virtual
11857 explicit */
11858 case RID_INLINE:
11859 case RID_VIRTUAL:
11860 case RID_EXPLICIT:
11861 cp_parser_function_specifier_opt (parser, decl_specs);
11862 break;
11864 /* decl-specifier:
11865 typedef */
11866 case RID_TYPEDEF:
11867 ds = ds_typedef;
11868 /* Consume the token. */
11869 cp_lexer_consume_token (parser->lexer);
11870 /* A constructor declarator cannot appear in a typedef. */
11871 constructor_possible_p = false;
11872 /* The "typedef" keyword can only occur in a declaration; we
11873 may as well commit at this point. */
11874 cp_parser_commit_to_tentative_parse (parser);
11876 if (decl_specs->storage_class != sc_none)
11877 decl_specs->conflicting_specifiers_p = true;
11878 break;
11880 /* storage-class-specifier:
11881 auto
11882 register
11883 static
11884 extern
11885 mutable
11887 GNU Extension:
11888 thread */
11889 case RID_AUTO:
11890 if (cxx_dialect == cxx98)
11892 /* Consume the token. */
11893 cp_lexer_consume_token (parser->lexer);
11895 /* Complain about `auto' as a storage specifier, if
11896 we're complaining about C++0x compatibility. */
11897 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11898 " changes meaning in C++11; please remove it");
11900 /* Set the storage class anyway. */
11901 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11902 token);
11904 else
11905 /* C++0x auto type-specifier. */
11906 found_decl_spec = false;
11907 break;
11909 case RID_REGISTER:
11910 case RID_STATIC:
11911 case RID_EXTERN:
11912 case RID_MUTABLE:
11913 /* Consume the token. */
11914 cp_lexer_consume_token (parser->lexer);
11915 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11916 token);
11917 break;
11918 case RID_THREAD:
11919 /* Consume the token. */
11920 ds = ds_thread;
11921 cp_lexer_consume_token (parser->lexer);
11922 break;
11924 default:
11925 /* We did not yet find a decl-specifier yet. */
11926 found_decl_spec = false;
11927 break;
11930 if (found_decl_spec
11931 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11932 && token->keyword != RID_CONSTEXPR)
11933 error ("decl-specifier invalid in condition");
11935 if (ds != ds_last)
11936 set_and_check_decl_spec_loc (decl_specs, ds, token);
11938 /* Constructors are a special case. The `S' in `S()' is not a
11939 decl-specifier; it is the beginning of the declarator. */
11940 constructor_p
11941 = (!found_decl_spec
11942 && constructor_possible_p
11943 && (cp_parser_constructor_declarator_p
11944 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11946 /* If we don't have a DECL_SPEC yet, then we must be looking at
11947 a type-specifier. */
11948 if (!found_decl_spec && !constructor_p)
11950 int decl_spec_declares_class_or_enum;
11951 bool is_cv_qualifier;
11952 tree type_spec;
11954 type_spec
11955 = cp_parser_type_specifier (parser, flags,
11956 decl_specs,
11957 /*is_declaration=*/true,
11958 &decl_spec_declares_class_or_enum,
11959 &is_cv_qualifier);
11960 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11962 /* If this type-specifier referenced a user-defined type
11963 (a typedef, class-name, etc.), then we can't allow any
11964 more such type-specifiers henceforth.
11966 [dcl.spec]
11968 The longest sequence of decl-specifiers that could
11969 possibly be a type name is taken as the
11970 decl-specifier-seq of a declaration. The sequence shall
11971 be self-consistent as described below.
11973 [dcl.type]
11975 As a general rule, at most one type-specifier is allowed
11976 in the complete decl-specifier-seq of a declaration. The
11977 only exceptions are the following:
11979 -- const or volatile can be combined with any other
11980 type-specifier.
11982 -- signed or unsigned can be combined with char, long,
11983 short, or int.
11985 -- ..
11987 Example:
11989 typedef char* Pc;
11990 void g (const int Pc);
11992 Here, Pc is *not* part of the decl-specifier seq; it's
11993 the declarator. Therefore, once we see a type-specifier
11994 (other than a cv-qualifier), we forbid any additional
11995 user-defined types. We *do* still allow things like `int
11996 int' to be considered a decl-specifier-seq, and issue the
11997 error message later. */
11998 if (type_spec && !is_cv_qualifier)
11999 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12000 /* A constructor declarator cannot follow a type-specifier. */
12001 if (type_spec)
12003 constructor_possible_p = false;
12004 found_decl_spec = true;
12005 if (!is_cv_qualifier)
12006 decl_specs->any_type_specifiers_p = true;
12010 /* If we still do not have a DECL_SPEC, then there are no more
12011 decl-specifiers. */
12012 if (!found_decl_spec)
12013 break;
12015 decl_specs->any_specifiers_p = true;
12016 /* After we see one decl-specifier, further decl-specifiers are
12017 always optional. */
12018 flags |= CP_PARSER_FLAGS_OPTIONAL;
12021 /* Don't allow a friend specifier with a class definition. */
12022 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12023 && (*declares_class_or_enum & 2))
12024 error_at (decl_specs->locations[ds_friend],
12025 "class definition may not be declared a friend");
12028 /* Parse an (optional) storage-class-specifier.
12030 storage-class-specifier:
12031 auto
12032 register
12033 static
12034 extern
12035 mutable
12037 GNU Extension:
12039 storage-class-specifier:
12040 thread
12042 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12044 static tree
12045 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12047 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12049 case RID_AUTO:
12050 if (cxx_dialect != cxx98)
12051 return NULL_TREE;
12052 /* Fall through for C++98. */
12054 case RID_REGISTER:
12055 case RID_STATIC:
12056 case RID_EXTERN:
12057 case RID_MUTABLE:
12058 case RID_THREAD:
12059 /* Consume the token. */
12060 return cp_lexer_consume_token (parser->lexer)->u.value;
12062 default:
12063 return NULL_TREE;
12067 /* Parse an (optional) function-specifier.
12069 function-specifier:
12070 inline
12071 virtual
12072 explicit
12074 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12075 Updates DECL_SPECS, if it is non-NULL. */
12077 static tree
12078 cp_parser_function_specifier_opt (cp_parser* parser,
12079 cp_decl_specifier_seq *decl_specs)
12081 cp_token *token = cp_lexer_peek_token (parser->lexer);
12082 switch (token->keyword)
12084 case RID_INLINE:
12085 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12086 break;
12088 case RID_VIRTUAL:
12089 /* 14.5.2.3 [temp.mem]
12091 A member function template shall not be virtual. */
12092 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12093 error_at (token->location, "templates may not be %<virtual%>");
12094 else
12095 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12096 break;
12098 case RID_EXPLICIT:
12099 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12100 break;
12102 default:
12103 return NULL_TREE;
12106 /* Consume the token. */
12107 return cp_lexer_consume_token (parser->lexer)->u.value;
12110 /* Parse a linkage-specification.
12112 linkage-specification:
12113 extern string-literal { declaration-seq [opt] }
12114 extern string-literal declaration */
12116 static void
12117 cp_parser_linkage_specification (cp_parser* parser)
12119 tree linkage;
12121 /* Look for the `extern' keyword. */
12122 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12124 /* Look for the string-literal. */
12125 linkage = cp_parser_string_literal (parser, false, false);
12127 /* Transform the literal into an identifier. If the literal is a
12128 wide-character string, or contains embedded NULs, then we can't
12129 handle it as the user wants. */
12130 if (strlen (TREE_STRING_POINTER (linkage))
12131 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12133 cp_parser_error (parser, "invalid linkage-specification");
12134 /* Assume C++ linkage. */
12135 linkage = lang_name_cplusplus;
12137 else
12138 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12140 /* We're now using the new linkage. */
12141 push_lang_context (linkage);
12143 /* If the next token is a `{', then we're using the first
12144 production. */
12145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12147 cp_ensure_no_omp_declare_simd (parser);
12149 /* Consume the `{' token. */
12150 cp_lexer_consume_token (parser->lexer);
12151 /* Parse the declarations. */
12152 cp_parser_declaration_seq_opt (parser);
12153 /* Look for the closing `}'. */
12154 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12156 /* Otherwise, there's just one declaration. */
12157 else
12159 bool saved_in_unbraced_linkage_specification_p;
12161 saved_in_unbraced_linkage_specification_p
12162 = parser->in_unbraced_linkage_specification_p;
12163 parser->in_unbraced_linkage_specification_p = true;
12164 cp_parser_declaration (parser);
12165 parser->in_unbraced_linkage_specification_p
12166 = saved_in_unbraced_linkage_specification_p;
12169 /* We're done with the linkage-specification. */
12170 pop_lang_context ();
12173 /* Parse a static_assert-declaration.
12175 static_assert-declaration:
12176 static_assert ( constant-expression , string-literal ) ;
12177 static_assert ( constant-expression ) ; (C++1Z)
12179 If MEMBER_P, this static_assert is a class member. */
12181 static void
12182 cp_parser_static_assert(cp_parser *parser, bool member_p)
12184 tree condition;
12185 tree message;
12186 cp_token *token;
12187 location_t saved_loc;
12188 bool dummy;
12190 /* Peek at the `static_assert' token so we can keep track of exactly
12191 where the static assertion started. */
12192 token = cp_lexer_peek_token (parser->lexer);
12193 saved_loc = token->location;
12195 /* Look for the `static_assert' keyword. */
12196 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12197 RT_STATIC_ASSERT))
12198 return;
12200 /* We know we are in a static assertion; commit to any tentative
12201 parse. */
12202 if (cp_parser_parsing_tentatively (parser))
12203 cp_parser_commit_to_tentative_parse (parser);
12205 /* Parse the `(' starting the static assertion condition. */
12206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12208 /* Parse the constant-expression. Allow a non-constant expression
12209 here in order to give better diagnostics in finish_static_assert. */
12210 condition =
12211 cp_parser_constant_expression (parser,
12212 /*allow_non_constant_p=*/true,
12213 /*non_constant_p=*/&dummy);
12215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12217 if (cxx_dialect < cxx1z)
12218 pedwarn (input_location, OPT_Wpedantic,
12219 "static_assert without a message "
12220 "only available with -std=c++1z or -std=gnu++1z");
12221 /* Eat the ')' */
12222 cp_lexer_consume_token (parser->lexer);
12223 message = build_string (1, "");
12224 TREE_TYPE (message) = char_array_type_node;
12225 fix_string_type (message);
12227 else
12229 /* Parse the separating `,'. */
12230 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12232 /* Parse the string-literal message. */
12233 message = cp_parser_string_literal (parser,
12234 /*translate=*/false,
12235 /*wide_ok=*/true);
12237 /* A `)' completes the static assertion. */
12238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12239 cp_parser_skip_to_closing_parenthesis (parser,
12240 /*recovering=*/true,
12241 /*or_comma=*/false,
12242 /*consume_paren=*/true);
12245 /* A semicolon terminates the declaration. */
12246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12248 /* Complete the static assertion, which may mean either processing
12249 the static assert now or saving it for template instantiation. */
12250 finish_static_assert (condition, message, saved_loc, member_p);
12253 /* Parse the expression in decltype ( expression ). */
12255 static tree
12256 cp_parser_decltype_expr (cp_parser *parser,
12257 bool &id_expression_or_member_access_p)
12259 cp_token *id_expr_start_token;
12260 tree expr;
12262 /* First, try parsing an id-expression. */
12263 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12264 cp_parser_parse_tentatively (parser);
12265 expr = cp_parser_id_expression (parser,
12266 /*template_keyword_p=*/false,
12267 /*check_dependency_p=*/true,
12268 /*template_p=*/NULL,
12269 /*declarator_p=*/false,
12270 /*optional_p=*/false);
12272 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12274 bool non_integral_constant_expression_p = false;
12275 tree id_expression = expr;
12276 cp_id_kind idk;
12277 const char *error_msg;
12279 if (identifier_p (expr))
12280 /* Lookup the name we got back from the id-expression. */
12281 expr = cp_parser_lookup_name_simple (parser, expr,
12282 id_expr_start_token->location);
12284 if (expr
12285 && expr != error_mark_node
12286 && TREE_CODE (expr) != TYPE_DECL
12287 && (TREE_CODE (expr) != BIT_NOT_EXPR
12288 || !TYPE_P (TREE_OPERAND (expr, 0)))
12289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12291 /* Complete lookup of the id-expression. */
12292 expr = (finish_id_expression
12293 (id_expression, expr, parser->scope, &idk,
12294 /*integral_constant_expression_p=*/false,
12295 /*allow_non_integral_constant_expression_p=*/true,
12296 &non_integral_constant_expression_p,
12297 /*template_p=*/false,
12298 /*done=*/true,
12299 /*address_p=*/false,
12300 /*template_arg_p=*/false,
12301 &error_msg,
12302 id_expr_start_token->location));
12304 if (expr == error_mark_node)
12305 /* We found an id-expression, but it was something that we
12306 should not have found. This is an error, not something
12307 we can recover from, so note that we found an
12308 id-expression and we'll recover as gracefully as
12309 possible. */
12310 id_expression_or_member_access_p = true;
12313 if (expr
12314 && expr != error_mark_node
12315 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12316 /* We have an id-expression. */
12317 id_expression_or_member_access_p = true;
12320 if (!id_expression_or_member_access_p)
12322 /* Abort the id-expression parse. */
12323 cp_parser_abort_tentative_parse (parser);
12325 /* Parsing tentatively, again. */
12326 cp_parser_parse_tentatively (parser);
12328 /* Parse a class member access. */
12329 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12330 /*cast_p=*/false, /*decltype*/true,
12331 /*member_access_only_p=*/true, NULL);
12333 if (expr
12334 && expr != error_mark_node
12335 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12336 /* We have an id-expression. */
12337 id_expression_or_member_access_p = true;
12340 if (id_expression_or_member_access_p)
12341 /* We have parsed the complete id-expression or member access. */
12342 cp_parser_parse_definitely (parser);
12343 else
12345 /* Abort our attempt to parse an id-expression or member access
12346 expression. */
12347 cp_parser_abort_tentative_parse (parser);
12349 /* Parse a full expression. */
12350 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12351 /*decltype_p=*/true);
12354 return expr;
12357 /* Parse a `decltype' type. Returns the type.
12359 simple-type-specifier:
12360 decltype ( expression )
12361 C++14 proposal:
12362 decltype ( auto ) */
12364 static tree
12365 cp_parser_decltype (cp_parser *parser)
12367 tree expr;
12368 bool id_expression_or_member_access_p = false;
12369 const char *saved_message;
12370 bool saved_integral_constant_expression_p;
12371 bool saved_non_integral_constant_expression_p;
12372 bool saved_greater_than_is_operator_p;
12373 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12375 if (start_token->type == CPP_DECLTYPE)
12377 /* Already parsed. */
12378 cp_lexer_consume_token (parser->lexer);
12379 return start_token->u.value;
12382 /* Look for the `decltype' token. */
12383 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12384 return error_mark_node;
12386 /* Parse the opening `('. */
12387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12388 return error_mark_node;
12390 /* decltype (auto) */
12391 if (cxx_dialect >= cxx14
12392 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12394 cp_lexer_consume_token (parser->lexer);
12395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12396 return error_mark_node;
12397 expr = make_decltype_auto ();
12398 AUTO_IS_DECLTYPE (expr) = true;
12399 goto rewrite;
12402 /* Types cannot be defined in a `decltype' expression. Save away the
12403 old message. */
12404 saved_message = parser->type_definition_forbidden_message;
12406 /* And create the new one. */
12407 parser->type_definition_forbidden_message
12408 = G_("types may not be defined in %<decltype%> expressions");
12410 /* The restrictions on constant-expressions do not apply inside
12411 decltype expressions. */
12412 saved_integral_constant_expression_p
12413 = parser->integral_constant_expression_p;
12414 saved_non_integral_constant_expression_p
12415 = parser->non_integral_constant_expression_p;
12416 parser->integral_constant_expression_p = false;
12418 /* Within a parenthesized expression, a `>' token is always
12419 the greater-than operator. */
12420 saved_greater_than_is_operator_p
12421 = parser->greater_than_is_operator_p;
12422 parser->greater_than_is_operator_p = true;
12424 /* Do not actually evaluate the expression. */
12425 ++cp_unevaluated_operand;
12427 /* Do not warn about problems with the expression. */
12428 ++c_inhibit_evaluation_warnings;
12430 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12432 /* Go back to evaluating expressions. */
12433 --cp_unevaluated_operand;
12434 --c_inhibit_evaluation_warnings;
12436 /* The `>' token might be the end of a template-id or
12437 template-parameter-list now. */
12438 parser->greater_than_is_operator_p
12439 = saved_greater_than_is_operator_p;
12441 /* Restore the old message and the integral constant expression
12442 flags. */
12443 parser->type_definition_forbidden_message = saved_message;
12444 parser->integral_constant_expression_p
12445 = saved_integral_constant_expression_p;
12446 parser->non_integral_constant_expression_p
12447 = saved_non_integral_constant_expression_p;
12449 /* Parse to the closing `)'. */
12450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12452 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12453 /*consume_paren=*/true);
12454 return error_mark_node;
12457 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12458 tf_warning_or_error);
12460 rewrite:
12461 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12462 it again. */
12463 start_token->type = CPP_DECLTYPE;
12464 start_token->u.value = expr;
12465 start_token->keyword = RID_MAX;
12466 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12468 return expr;
12471 /* Special member functions [gram.special] */
12473 /* Parse a conversion-function-id.
12475 conversion-function-id:
12476 operator conversion-type-id
12478 Returns an IDENTIFIER_NODE representing the operator. */
12480 static tree
12481 cp_parser_conversion_function_id (cp_parser* parser)
12483 tree type;
12484 tree saved_scope;
12485 tree saved_qualifying_scope;
12486 tree saved_object_scope;
12487 tree pushed_scope = NULL_TREE;
12489 /* Look for the `operator' token. */
12490 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12491 return error_mark_node;
12492 /* When we parse the conversion-type-id, the current scope will be
12493 reset. However, we need that information in able to look up the
12494 conversion function later, so we save it here. */
12495 saved_scope = parser->scope;
12496 saved_qualifying_scope = parser->qualifying_scope;
12497 saved_object_scope = parser->object_scope;
12498 /* We must enter the scope of the class so that the names of
12499 entities declared within the class are available in the
12500 conversion-type-id. For example, consider:
12502 struct S {
12503 typedef int I;
12504 operator I();
12507 S::operator I() { ... }
12509 In order to see that `I' is a type-name in the definition, we
12510 must be in the scope of `S'. */
12511 if (saved_scope)
12512 pushed_scope = push_scope (saved_scope);
12513 /* Parse the conversion-type-id. */
12514 type = cp_parser_conversion_type_id (parser);
12515 /* Leave the scope of the class, if any. */
12516 if (pushed_scope)
12517 pop_scope (pushed_scope);
12518 /* Restore the saved scope. */
12519 parser->scope = saved_scope;
12520 parser->qualifying_scope = saved_qualifying_scope;
12521 parser->object_scope = saved_object_scope;
12522 /* If the TYPE is invalid, indicate failure. */
12523 if (type == error_mark_node)
12524 return error_mark_node;
12525 return mangle_conv_op_name_for_type (type);
12528 /* Parse a conversion-type-id:
12530 conversion-type-id:
12531 type-specifier-seq conversion-declarator [opt]
12533 Returns the TYPE specified. */
12535 static tree
12536 cp_parser_conversion_type_id (cp_parser* parser)
12538 tree attributes;
12539 cp_decl_specifier_seq type_specifiers;
12540 cp_declarator *declarator;
12541 tree type_specified;
12542 const char *saved_message;
12544 /* Parse the attributes. */
12545 attributes = cp_parser_attributes_opt (parser);
12547 saved_message = parser->type_definition_forbidden_message;
12548 parser->type_definition_forbidden_message
12549 = G_("types may not be defined in a conversion-type-id");
12551 /* Parse the type-specifiers. */
12552 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12553 /*is_trailing_return=*/false,
12554 &type_specifiers);
12556 parser->type_definition_forbidden_message = saved_message;
12558 /* If that didn't work, stop. */
12559 if (type_specifiers.type == error_mark_node)
12560 return error_mark_node;
12561 /* Parse the conversion-declarator. */
12562 declarator = cp_parser_conversion_declarator_opt (parser);
12564 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12565 /*initialized=*/0, &attributes);
12566 if (attributes)
12567 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12569 /* Don't give this error when parsing tentatively. This happens to
12570 work because we always parse this definitively once. */
12571 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12572 && type_uses_auto (type_specified))
12574 if (cxx_dialect < cxx14)
12576 error ("invalid use of %<auto%> in conversion operator");
12577 return error_mark_node;
12579 else if (template_parm_scope_p ())
12580 warning (0, "use of %<auto%> in member template "
12581 "conversion operator can never be deduced");
12584 return type_specified;
12587 /* Parse an (optional) conversion-declarator.
12589 conversion-declarator:
12590 ptr-operator conversion-declarator [opt]
12594 static cp_declarator *
12595 cp_parser_conversion_declarator_opt (cp_parser* parser)
12597 enum tree_code code;
12598 tree class_type, std_attributes = NULL_TREE;
12599 cp_cv_quals cv_quals;
12601 /* We don't know if there's a ptr-operator next, or not. */
12602 cp_parser_parse_tentatively (parser);
12603 /* Try the ptr-operator. */
12604 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12605 &std_attributes);
12606 /* If it worked, look for more conversion-declarators. */
12607 if (cp_parser_parse_definitely (parser))
12609 cp_declarator *declarator;
12611 /* Parse another optional declarator. */
12612 declarator = cp_parser_conversion_declarator_opt (parser);
12614 declarator = cp_parser_make_indirect_declarator
12615 (code, class_type, cv_quals, declarator, std_attributes);
12617 return declarator;
12620 return NULL;
12623 /* Parse an (optional) ctor-initializer.
12625 ctor-initializer:
12626 : mem-initializer-list
12628 Returns TRUE iff the ctor-initializer was actually present. */
12630 static bool
12631 cp_parser_ctor_initializer_opt (cp_parser* parser)
12633 /* If the next token is not a `:', then there is no
12634 ctor-initializer. */
12635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12637 /* Do default initialization of any bases and members. */
12638 if (DECL_CONSTRUCTOR_P (current_function_decl))
12639 finish_mem_initializers (NULL_TREE);
12641 return false;
12644 /* Consume the `:' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* And the mem-initializer-list. */
12647 cp_parser_mem_initializer_list (parser);
12649 return true;
12652 /* Parse a mem-initializer-list.
12654 mem-initializer-list:
12655 mem-initializer ... [opt]
12656 mem-initializer ... [opt] , mem-initializer-list */
12658 static void
12659 cp_parser_mem_initializer_list (cp_parser* parser)
12661 tree mem_initializer_list = NULL_TREE;
12662 tree target_ctor = error_mark_node;
12663 cp_token *token = cp_lexer_peek_token (parser->lexer);
12665 /* Let the semantic analysis code know that we are starting the
12666 mem-initializer-list. */
12667 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12668 error_at (token->location,
12669 "only constructors take member initializers");
12671 /* Loop through the list. */
12672 while (true)
12674 tree mem_initializer;
12676 token = cp_lexer_peek_token (parser->lexer);
12677 /* Parse the mem-initializer. */
12678 mem_initializer = cp_parser_mem_initializer (parser);
12679 /* If the next token is a `...', we're expanding member initializers. */
12680 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12682 /* Consume the `...'. */
12683 cp_lexer_consume_token (parser->lexer);
12685 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12686 can be expanded but members cannot. */
12687 if (mem_initializer != error_mark_node
12688 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12690 error_at (token->location,
12691 "cannot expand initializer for member %<%D%>",
12692 TREE_PURPOSE (mem_initializer));
12693 mem_initializer = error_mark_node;
12696 /* Construct the pack expansion type. */
12697 if (mem_initializer != error_mark_node)
12698 mem_initializer = make_pack_expansion (mem_initializer);
12700 if (target_ctor != error_mark_node
12701 && mem_initializer != error_mark_node)
12703 error ("mem-initializer for %qD follows constructor delegation",
12704 TREE_PURPOSE (mem_initializer));
12705 mem_initializer = error_mark_node;
12707 /* Look for a target constructor. */
12708 if (mem_initializer != error_mark_node
12709 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12710 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12712 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12713 if (mem_initializer_list)
12715 error ("constructor delegation follows mem-initializer for %qD",
12716 TREE_PURPOSE (mem_initializer_list));
12717 mem_initializer = error_mark_node;
12719 target_ctor = mem_initializer;
12721 /* Add it to the list, unless it was erroneous. */
12722 if (mem_initializer != error_mark_node)
12724 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12725 mem_initializer_list = mem_initializer;
12727 /* If the next token is not a `,', we're done. */
12728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12729 break;
12730 /* Consume the `,' token. */
12731 cp_lexer_consume_token (parser->lexer);
12734 /* Perform semantic analysis. */
12735 if (DECL_CONSTRUCTOR_P (current_function_decl))
12736 finish_mem_initializers (mem_initializer_list);
12739 /* Parse a mem-initializer.
12741 mem-initializer:
12742 mem-initializer-id ( expression-list [opt] )
12743 mem-initializer-id braced-init-list
12745 GNU extension:
12747 mem-initializer:
12748 ( expression-list [opt] )
12750 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12751 class) or FIELD_DECL (for a non-static data member) to initialize;
12752 the TREE_VALUE is the expression-list. An empty initialization
12753 list is represented by void_list_node. */
12755 static tree
12756 cp_parser_mem_initializer (cp_parser* parser)
12758 tree mem_initializer_id;
12759 tree expression_list;
12760 tree member;
12761 cp_token *token = cp_lexer_peek_token (parser->lexer);
12763 /* Find out what is being initialized. */
12764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12766 permerror (token->location,
12767 "anachronistic old-style base class initializer");
12768 mem_initializer_id = NULL_TREE;
12770 else
12772 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12773 if (mem_initializer_id == error_mark_node)
12774 return mem_initializer_id;
12776 member = expand_member_init (mem_initializer_id);
12777 if (member && !DECL_P (member))
12778 in_base_initializer = 1;
12780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12782 bool expr_non_constant_p;
12783 cp_lexer_set_source_position (parser->lexer);
12784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12785 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12786 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12787 expression_list = build_tree_list (NULL_TREE, expression_list);
12789 else
12791 vec<tree, va_gc> *vec;
12792 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12793 /*cast_p=*/false,
12794 /*allow_expansion_p=*/true,
12795 /*non_constant_p=*/NULL);
12796 if (vec == NULL)
12797 return error_mark_node;
12798 expression_list = build_tree_list_vec (vec);
12799 release_tree_vector (vec);
12802 if (expression_list == error_mark_node)
12803 return error_mark_node;
12804 if (!expression_list)
12805 expression_list = void_type_node;
12807 in_base_initializer = 0;
12809 return member ? build_tree_list (member, expression_list) : error_mark_node;
12812 /* Parse a mem-initializer-id.
12814 mem-initializer-id:
12815 :: [opt] nested-name-specifier [opt] class-name
12816 decltype-specifier (C++11)
12817 identifier
12819 Returns a TYPE indicating the class to be initialized for the first
12820 production (and the second in C++11). Returns an IDENTIFIER_NODE
12821 indicating the data member to be initialized for the last production. */
12823 static tree
12824 cp_parser_mem_initializer_id (cp_parser* parser)
12826 bool global_scope_p;
12827 bool nested_name_specifier_p;
12828 bool template_p = false;
12829 tree id;
12831 cp_token *token = cp_lexer_peek_token (parser->lexer);
12833 /* `typename' is not allowed in this context ([temp.res]). */
12834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12836 error_at (token->location,
12837 "keyword %<typename%> not allowed in this context (a qualified "
12838 "member initializer is implicitly a type)");
12839 cp_lexer_consume_token (parser->lexer);
12841 /* Look for the optional `::' operator. */
12842 global_scope_p
12843 = (cp_parser_global_scope_opt (parser,
12844 /*current_scope_valid_p=*/false)
12845 != NULL_TREE);
12846 /* Look for the optional nested-name-specifier. The simplest way to
12847 implement:
12849 [temp.res]
12851 The keyword `typename' is not permitted in a base-specifier or
12852 mem-initializer; in these contexts a qualified name that
12853 depends on a template-parameter is implicitly assumed to be a
12854 type name.
12856 is to assume that we have seen the `typename' keyword at this
12857 point. */
12858 nested_name_specifier_p
12859 = (cp_parser_nested_name_specifier_opt (parser,
12860 /*typename_keyword_p=*/true,
12861 /*check_dependency_p=*/true,
12862 /*type_p=*/true,
12863 /*is_declaration=*/true)
12864 != NULL_TREE);
12865 if (nested_name_specifier_p)
12866 template_p = cp_parser_optional_template_keyword (parser);
12867 /* If there is a `::' operator or a nested-name-specifier, then we
12868 are definitely looking for a class-name. */
12869 if (global_scope_p || nested_name_specifier_p)
12870 return cp_parser_class_name (parser,
12871 /*typename_keyword_p=*/true,
12872 /*template_keyword_p=*/template_p,
12873 typename_type,
12874 /*check_dependency_p=*/true,
12875 /*class_head_p=*/false,
12876 /*is_declaration=*/true);
12877 /* Otherwise, we could also be looking for an ordinary identifier. */
12878 cp_parser_parse_tentatively (parser);
12879 if (cp_lexer_next_token_is_decltype (parser->lexer))
12880 /* Try a decltype-specifier. */
12881 id = cp_parser_decltype (parser);
12882 else
12883 /* Otherwise, try a class-name. */
12884 id = cp_parser_class_name (parser,
12885 /*typename_keyword_p=*/true,
12886 /*template_keyword_p=*/false,
12887 none_type,
12888 /*check_dependency_p=*/true,
12889 /*class_head_p=*/false,
12890 /*is_declaration=*/true);
12891 /* If we found one, we're done. */
12892 if (cp_parser_parse_definitely (parser))
12893 return id;
12894 /* Otherwise, look for an ordinary identifier. */
12895 return cp_parser_identifier (parser);
12898 /* Overloading [gram.over] */
12900 /* Parse an operator-function-id.
12902 operator-function-id:
12903 operator operator
12905 Returns an IDENTIFIER_NODE for the operator which is a
12906 human-readable spelling of the identifier, e.g., `operator +'. */
12908 static tree
12909 cp_parser_operator_function_id (cp_parser* parser)
12911 /* Look for the `operator' keyword. */
12912 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12913 return error_mark_node;
12914 /* And then the name of the operator itself. */
12915 return cp_parser_operator (parser);
12918 /* Return an identifier node for a user-defined literal operator.
12919 The suffix identifier is chained to the operator name identifier. */
12921 static tree
12922 cp_literal_operator_id (const char* name)
12924 tree identifier;
12925 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12926 + strlen (name) + 10);
12927 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12928 identifier = get_identifier (buffer);
12930 return identifier;
12933 /* Parse an operator.
12935 operator:
12936 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12937 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12938 || ++ -- , ->* -> () []
12940 GNU Extensions:
12942 operator:
12943 <? >? <?= >?=
12945 Returns an IDENTIFIER_NODE for the operator which is a
12946 human-readable spelling of the identifier, e.g., `operator +'. */
12948 static tree
12949 cp_parser_operator (cp_parser* parser)
12951 tree id = NULL_TREE;
12952 cp_token *token;
12953 bool utf8 = false;
12955 /* Peek at the next token. */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 /* Figure out which operator we have. */
12958 switch (token->type)
12960 case CPP_KEYWORD:
12962 enum tree_code op;
12964 /* The keyword should be either `new' or `delete'. */
12965 if (token->keyword == RID_NEW)
12966 op = NEW_EXPR;
12967 else if (token->keyword == RID_DELETE)
12968 op = DELETE_EXPR;
12969 else
12970 break;
12972 /* Consume the `new' or `delete' token. */
12973 cp_lexer_consume_token (parser->lexer);
12975 /* Peek at the next token. */
12976 token = cp_lexer_peek_token (parser->lexer);
12977 /* If it's a `[' token then this is the array variant of the
12978 operator. */
12979 if (token->type == CPP_OPEN_SQUARE)
12981 /* Consume the `[' token. */
12982 cp_lexer_consume_token (parser->lexer);
12983 /* Look for the `]' token. */
12984 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12985 id = ansi_opname (op == NEW_EXPR
12986 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12988 /* Otherwise, we have the non-array variant. */
12989 else
12990 id = ansi_opname (op);
12992 return id;
12995 case CPP_PLUS:
12996 id = ansi_opname (PLUS_EXPR);
12997 break;
12999 case CPP_MINUS:
13000 id = ansi_opname (MINUS_EXPR);
13001 break;
13003 case CPP_MULT:
13004 id = ansi_opname (MULT_EXPR);
13005 break;
13007 case CPP_DIV:
13008 id = ansi_opname (TRUNC_DIV_EXPR);
13009 break;
13011 case CPP_MOD:
13012 id = ansi_opname (TRUNC_MOD_EXPR);
13013 break;
13015 case CPP_XOR:
13016 id = ansi_opname (BIT_XOR_EXPR);
13017 break;
13019 case CPP_AND:
13020 id = ansi_opname (BIT_AND_EXPR);
13021 break;
13023 case CPP_OR:
13024 id = ansi_opname (BIT_IOR_EXPR);
13025 break;
13027 case CPP_COMPL:
13028 id = ansi_opname (BIT_NOT_EXPR);
13029 break;
13031 case CPP_NOT:
13032 id = ansi_opname (TRUTH_NOT_EXPR);
13033 break;
13035 case CPP_EQ:
13036 id = ansi_assopname (NOP_EXPR);
13037 break;
13039 case CPP_LESS:
13040 id = ansi_opname (LT_EXPR);
13041 break;
13043 case CPP_GREATER:
13044 id = ansi_opname (GT_EXPR);
13045 break;
13047 case CPP_PLUS_EQ:
13048 id = ansi_assopname (PLUS_EXPR);
13049 break;
13051 case CPP_MINUS_EQ:
13052 id = ansi_assopname (MINUS_EXPR);
13053 break;
13055 case CPP_MULT_EQ:
13056 id = ansi_assopname (MULT_EXPR);
13057 break;
13059 case CPP_DIV_EQ:
13060 id = ansi_assopname (TRUNC_DIV_EXPR);
13061 break;
13063 case CPP_MOD_EQ:
13064 id = ansi_assopname (TRUNC_MOD_EXPR);
13065 break;
13067 case CPP_XOR_EQ:
13068 id = ansi_assopname (BIT_XOR_EXPR);
13069 break;
13071 case CPP_AND_EQ:
13072 id = ansi_assopname (BIT_AND_EXPR);
13073 break;
13075 case CPP_OR_EQ:
13076 id = ansi_assopname (BIT_IOR_EXPR);
13077 break;
13079 case CPP_LSHIFT:
13080 id = ansi_opname (LSHIFT_EXPR);
13081 break;
13083 case CPP_RSHIFT:
13084 id = ansi_opname (RSHIFT_EXPR);
13085 break;
13087 case CPP_LSHIFT_EQ:
13088 id = ansi_assopname (LSHIFT_EXPR);
13089 break;
13091 case CPP_RSHIFT_EQ:
13092 id = ansi_assopname (RSHIFT_EXPR);
13093 break;
13095 case CPP_EQ_EQ:
13096 id = ansi_opname (EQ_EXPR);
13097 break;
13099 case CPP_NOT_EQ:
13100 id = ansi_opname (NE_EXPR);
13101 break;
13103 case CPP_LESS_EQ:
13104 id = ansi_opname (LE_EXPR);
13105 break;
13107 case CPP_GREATER_EQ:
13108 id = ansi_opname (GE_EXPR);
13109 break;
13111 case CPP_AND_AND:
13112 id = ansi_opname (TRUTH_ANDIF_EXPR);
13113 break;
13115 case CPP_OR_OR:
13116 id = ansi_opname (TRUTH_ORIF_EXPR);
13117 break;
13119 case CPP_PLUS_PLUS:
13120 id = ansi_opname (POSTINCREMENT_EXPR);
13121 break;
13123 case CPP_MINUS_MINUS:
13124 id = ansi_opname (PREDECREMENT_EXPR);
13125 break;
13127 case CPP_COMMA:
13128 id = ansi_opname (COMPOUND_EXPR);
13129 break;
13131 case CPP_DEREF_STAR:
13132 id = ansi_opname (MEMBER_REF);
13133 break;
13135 case CPP_DEREF:
13136 id = ansi_opname (COMPONENT_REF);
13137 break;
13139 case CPP_OPEN_PAREN:
13140 /* Consume the `('. */
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Look for the matching `)'. */
13143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13144 return ansi_opname (CALL_EXPR);
13146 case CPP_OPEN_SQUARE:
13147 /* Consume the `['. */
13148 cp_lexer_consume_token (parser->lexer);
13149 /* Look for the matching `]'. */
13150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13151 return ansi_opname (ARRAY_REF);
13153 case CPP_UTF8STRING:
13154 case CPP_UTF8STRING_USERDEF:
13155 utf8 = true;
13156 case CPP_STRING:
13157 case CPP_WSTRING:
13158 case CPP_STRING16:
13159 case CPP_STRING32:
13160 case CPP_STRING_USERDEF:
13161 case CPP_WSTRING_USERDEF:
13162 case CPP_STRING16_USERDEF:
13163 case CPP_STRING32_USERDEF:
13165 tree str, string_tree;
13166 int sz, len;
13168 if (cxx_dialect == cxx98)
13169 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13171 /* Consume the string. */
13172 str = cp_parser_string_literal (parser, /*translate=*/true,
13173 /*wide_ok=*/true, /*lookup_udlit=*/false);
13174 if (str == error_mark_node)
13175 return error_mark_node;
13176 else if (TREE_CODE (str) == USERDEF_LITERAL)
13178 string_tree = USERDEF_LITERAL_VALUE (str);
13179 id = USERDEF_LITERAL_SUFFIX_ID (str);
13181 else
13183 string_tree = str;
13184 /* Look for the suffix identifier. */
13185 token = cp_lexer_peek_token (parser->lexer);
13186 if (token->type == CPP_NAME)
13187 id = cp_parser_identifier (parser);
13188 else if (token->type == CPP_KEYWORD)
13190 error ("unexpected keyword;"
13191 " remove space between quotes and suffix identifier");
13192 return error_mark_node;
13194 else
13196 error ("expected suffix identifier");
13197 return error_mark_node;
13200 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13201 (TREE_TYPE (TREE_TYPE (string_tree))));
13202 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13203 if (len != 0)
13205 error ("expected empty string after %<operator%> keyword");
13206 return error_mark_node;
13208 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13209 != char_type_node)
13211 error ("invalid encoding prefix in literal operator");
13212 return error_mark_node;
13214 if (id != error_mark_node)
13216 const char *name = IDENTIFIER_POINTER (id);
13217 id = cp_literal_operator_id (name);
13219 return id;
13222 default:
13223 /* Anything else is an error. */
13224 break;
13227 /* If we have selected an identifier, we need to consume the
13228 operator token. */
13229 if (id)
13230 cp_lexer_consume_token (parser->lexer);
13231 /* Otherwise, no valid operator name was present. */
13232 else
13234 cp_parser_error (parser, "expected operator");
13235 id = error_mark_node;
13238 return id;
13241 /* Parse a template-declaration.
13243 template-declaration:
13244 export [opt] template < template-parameter-list > declaration
13246 If MEMBER_P is TRUE, this template-declaration occurs within a
13247 class-specifier.
13249 The grammar rule given by the standard isn't correct. What
13250 is really meant is:
13252 template-declaration:
13253 export [opt] template-parameter-list-seq
13254 decl-specifier-seq [opt] init-declarator [opt] ;
13255 export [opt] template-parameter-list-seq
13256 function-definition
13258 template-parameter-list-seq:
13259 template-parameter-list-seq [opt]
13260 template < template-parameter-list > */
13262 static void
13263 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13265 /* Check for `export'. */
13266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13268 /* Consume the `export' token. */
13269 cp_lexer_consume_token (parser->lexer);
13270 /* Warn that we do not support `export'. */
13271 warning (0, "keyword %<export%> not implemented, and will be ignored");
13274 cp_parser_template_declaration_after_export (parser, member_p);
13277 /* Parse a template-parameter-list.
13279 template-parameter-list:
13280 template-parameter
13281 template-parameter-list , template-parameter
13283 Returns a TREE_LIST. Each node represents a template parameter.
13284 The nodes are connected via their TREE_CHAINs. */
13286 static tree
13287 cp_parser_template_parameter_list (cp_parser* parser)
13289 tree parameter_list = NULL_TREE;
13291 begin_template_parm_list ();
13293 /* The loop below parses the template parms. We first need to know
13294 the total number of template parms to be able to compute proper
13295 canonical types of each dependent type. So after the loop, when
13296 we know the total number of template parms,
13297 end_template_parm_list computes the proper canonical types and
13298 fixes up the dependent types accordingly. */
13299 while (true)
13301 tree parameter;
13302 bool is_non_type;
13303 bool is_parameter_pack;
13304 location_t parm_loc;
13306 /* Parse the template-parameter. */
13307 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13308 parameter = cp_parser_template_parameter (parser,
13309 &is_non_type,
13310 &is_parameter_pack);
13311 /* Add it to the list. */
13312 if (parameter != error_mark_node)
13313 parameter_list = process_template_parm (parameter_list,
13314 parm_loc,
13315 parameter,
13316 is_non_type,
13317 is_parameter_pack);
13318 else
13320 tree err_parm = build_tree_list (parameter, parameter);
13321 parameter_list = chainon (parameter_list, err_parm);
13324 /* If the next token is not a `,', we're done. */
13325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13326 break;
13327 /* Otherwise, consume the `,' token. */
13328 cp_lexer_consume_token (parser->lexer);
13331 return end_template_parm_list (parameter_list);
13334 /* Parse a template-parameter.
13336 template-parameter:
13337 type-parameter
13338 parameter-declaration
13340 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13341 the parameter. The TREE_PURPOSE is the default value, if any.
13342 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13343 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13344 set to true iff this parameter is a parameter pack. */
13346 static tree
13347 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13348 bool *is_parameter_pack)
13350 cp_token *token;
13351 cp_parameter_declarator *parameter_declarator;
13352 cp_declarator *id_declarator;
13353 tree parm;
13355 /* Assume it is a type parameter or a template parameter. */
13356 *is_non_type = false;
13357 /* Assume it not a parameter pack. */
13358 *is_parameter_pack = false;
13359 /* Peek at the next token. */
13360 token = cp_lexer_peek_token (parser->lexer);
13361 /* If it is `class' or `template', we have a type-parameter. */
13362 if (token->keyword == RID_TEMPLATE)
13363 return cp_parser_type_parameter (parser, is_parameter_pack);
13364 /* If it is `class' or `typename' we do not know yet whether it is a
13365 type parameter or a non-type parameter. Consider:
13367 template <typename T, typename T::X X> ...
13371 template <class C, class D*> ...
13373 Here, the first parameter is a type parameter, and the second is
13374 a non-type parameter. We can tell by looking at the token after
13375 the identifier -- if it is a `,', `=', or `>' then we have a type
13376 parameter. */
13377 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13379 /* Peek at the token after `class' or `typename'. */
13380 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13381 /* If it's an ellipsis, we have a template type parameter
13382 pack. */
13383 if (token->type == CPP_ELLIPSIS)
13384 return cp_parser_type_parameter (parser, is_parameter_pack);
13385 /* If it's an identifier, skip it. */
13386 if (token->type == CPP_NAME)
13387 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13388 /* Now, see if the token looks like the end of a template
13389 parameter. */
13390 if (token->type == CPP_COMMA
13391 || token->type == CPP_EQ
13392 || token->type == CPP_GREATER)
13393 return cp_parser_type_parameter (parser, is_parameter_pack);
13396 /* Otherwise, it is a non-type parameter.
13398 [temp.param]
13400 When parsing a default template-argument for a non-type
13401 template-parameter, the first non-nested `>' is taken as the end
13402 of the template parameter-list rather than a greater-than
13403 operator. */
13404 *is_non_type = true;
13405 parameter_declarator
13406 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13407 /*parenthesized_p=*/NULL);
13409 if (!parameter_declarator)
13410 return error_mark_node;
13412 /* If the parameter declaration is marked as a parameter pack, set
13413 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13414 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13415 grokdeclarator. */
13416 if (parameter_declarator->declarator
13417 && parameter_declarator->declarator->parameter_pack_p)
13419 *is_parameter_pack = true;
13420 parameter_declarator->declarator->parameter_pack_p = false;
13423 if (parameter_declarator->default_argument)
13425 /* Can happen in some cases of erroneous input (c++/34892). */
13426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13427 /* Consume the `...' for better error recovery. */
13428 cp_lexer_consume_token (parser->lexer);
13430 /* If the next token is an ellipsis, and we don't already have it
13431 marked as a parameter pack, then we have a parameter pack (that
13432 has no declarator). */
13433 else if (!*is_parameter_pack
13434 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13435 && (declarator_can_be_parameter_pack
13436 (parameter_declarator->declarator)))
13438 /* Consume the `...'. */
13439 cp_lexer_consume_token (parser->lexer);
13440 maybe_warn_variadic_templates ();
13442 *is_parameter_pack = true;
13444 /* We might end up with a pack expansion as the type of the non-type
13445 template parameter, in which case this is a non-type template
13446 parameter pack. */
13447 else if (parameter_declarator->decl_specifiers.type
13448 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13450 *is_parameter_pack = true;
13451 parameter_declarator->decl_specifiers.type =
13452 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13455 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13457 /* Parameter packs cannot have default arguments. However, a
13458 user may try to do so, so we'll parse them and give an
13459 appropriate diagnostic here. */
13461 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13463 /* Find the name of the parameter pack. */
13464 id_declarator = parameter_declarator->declarator;
13465 while (id_declarator && id_declarator->kind != cdk_id)
13466 id_declarator = id_declarator->declarator;
13468 if (id_declarator && id_declarator->kind == cdk_id)
13469 error_at (start_token->location,
13470 "template parameter pack %qD cannot have a default argument",
13471 id_declarator->u.id.unqualified_name);
13472 else
13473 error_at (start_token->location,
13474 "template parameter pack cannot have a default argument");
13476 /* Parse the default argument, but throw away the result. */
13477 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13480 parm = grokdeclarator (parameter_declarator->declarator,
13481 &parameter_declarator->decl_specifiers,
13482 TPARM, /*initialized=*/0,
13483 /*attrlist=*/NULL);
13484 if (parm == error_mark_node)
13485 return error_mark_node;
13487 return build_tree_list (parameter_declarator->default_argument, parm);
13490 /* Parse a type-parameter.
13492 type-parameter:
13493 class identifier [opt]
13494 class identifier [opt] = type-id
13495 typename identifier [opt]
13496 typename identifier [opt] = type-id
13497 template < template-parameter-list > class identifier [opt]
13498 template < template-parameter-list > class identifier [opt]
13499 = id-expression
13501 GNU Extension (variadic templates):
13503 type-parameter:
13504 class ... identifier [opt]
13505 typename ... identifier [opt]
13507 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13508 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13509 the declaration of the parameter.
13511 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13513 static tree
13514 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13516 cp_token *token;
13517 tree parameter;
13519 /* Look for a keyword to tell us what kind of parameter this is. */
13520 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13521 if (!token)
13522 return error_mark_node;
13524 switch (token->keyword)
13526 case RID_CLASS:
13527 case RID_TYPENAME:
13529 tree identifier;
13530 tree default_argument;
13532 /* If the next token is an ellipsis, we have a template
13533 argument pack. */
13534 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13536 /* Consume the `...' token. */
13537 cp_lexer_consume_token (parser->lexer);
13538 maybe_warn_variadic_templates ();
13540 *is_parameter_pack = true;
13543 /* If the next token is an identifier, then it names the
13544 parameter. */
13545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13546 identifier = cp_parser_identifier (parser);
13547 else
13548 identifier = NULL_TREE;
13550 /* Create the parameter. */
13551 parameter = finish_template_type_parm (class_type_node, identifier);
13553 /* If the next token is an `=', we have a default argument. */
13554 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13556 /* Consume the `=' token. */
13557 cp_lexer_consume_token (parser->lexer);
13558 /* Parse the default-argument. */
13559 push_deferring_access_checks (dk_no_deferred);
13560 default_argument = cp_parser_type_id (parser);
13562 /* Template parameter packs cannot have default
13563 arguments. */
13564 if (*is_parameter_pack)
13566 if (identifier)
13567 error_at (token->location,
13568 "template parameter pack %qD cannot have a "
13569 "default argument", identifier);
13570 else
13571 error_at (token->location,
13572 "template parameter packs cannot have "
13573 "default arguments");
13574 default_argument = NULL_TREE;
13576 else if (check_for_bare_parameter_packs (default_argument))
13577 default_argument = error_mark_node;
13578 pop_deferring_access_checks ();
13580 else
13581 default_argument = NULL_TREE;
13583 /* Create the combined representation of the parameter and the
13584 default argument. */
13585 parameter = build_tree_list (default_argument, parameter);
13587 break;
13589 case RID_TEMPLATE:
13591 tree identifier;
13592 tree default_argument;
13594 /* Look for the `<'. */
13595 cp_parser_require (parser, CPP_LESS, RT_LESS);
13596 /* Parse the template-parameter-list. */
13597 cp_parser_template_parameter_list (parser);
13598 /* Look for the `>'. */
13599 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13600 /* Look for the `class' or 'typename' keywords. */
13601 cp_parser_type_parameter_key (parser);
13602 /* If the next token is an ellipsis, we have a template
13603 argument pack. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13606 /* Consume the `...' token. */
13607 cp_lexer_consume_token (parser->lexer);
13608 maybe_warn_variadic_templates ();
13610 *is_parameter_pack = true;
13612 /* If the next token is an `=', then there is a
13613 default-argument. If the next token is a `>', we are at
13614 the end of the parameter-list. If the next token is a `,',
13615 then we are at the end of this parameter. */
13616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13617 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13618 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13620 identifier = cp_parser_identifier (parser);
13621 /* Treat invalid names as if the parameter were nameless. */
13622 if (identifier == error_mark_node)
13623 identifier = NULL_TREE;
13625 else
13626 identifier = NULL_TREE;
13628 /* Create the template parameter. */
13629 parameter = finish_template_template_parm (class_type_node,
13630 identifier);
13632 /* If the next token is an `=', then there is a
13633 default-argument. */
13634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13636 bool is_template;
13638 /* Consume the `='. */
13639 cp_lexer_consume_token (parser->lexer);
13640 /* Parse the id-expression. */
13641 push_deferring_access_checks (dk_no_deferred);
13642 /* save token before parsing the id-expression, for error
13643 reporting */
13644 token = cp_lexer_peek_token (parser->lexer);
13645 default_argument
13646 = cp_parser_id_expression (parser,
13647 /*template_keyword_p=*/false,
13648 /*check_dependency_p=*/true,
13649 /*template_p=*/&is_template,
13650 /*declarator_p=*/false,
13651 /*optional_p=*/false);
13652 if (TREE_CODE (default_argument) == TYPE_DECL)
13653 /* If the id-expression was a template-id that refers to
13654 a template-class, we already have the declaration here,
13655 so no further lookup is needed. */
13657 else
13658 /* Look up the name. */
13659 default_argument
13660 = cp_parser_lookup_name (parser, default_argument,
13661 none_type,
13662 /*is_template=*/is_template,
13663 /*is_namespace=*/false,
13664 /*check_dependency=*/true,
13665 /*ambiguous_decls=*/NULL,
13666 token->location);
13667 /* See if the default argument is valid. */
13668 default_argument
13669 = check_template_template_default_arg (default_argument);
13671 /* Template parameter packs cannot have default
13672 arguments. */
13673 if (*is_parameter_pack)
13675 if (identifier)
13676 error_at (token->location,
13677 "template parameter pack %qD cannot "
13678 "have a default argument",
13679 identifier);
13680 else
13681 error_at (token->location, "template parameter packs cannot "
13682 "have default arguments");
13683 default_argument = NULL_TREE;
13685 pop_deferring_access_checks ();
13687 else
13688 default_argument = NULL_TREE;
13690 /* Create the combined representation of the parameter and the
13691 default argument. */
13692 parameter = build_tree_list (default_argument, parameter);
13694 break;
13696 default:
13697 gcc_unreachable ();
13698 break;
13701 return parameter;
13704 /* Parse a template-id.
13706 template-id:
13707 template-name < template-argument-list [opt] >
13709 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13710 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13711 returned. Otherwise, if the template-name names a function, or set
13712 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13713 names a class, returns a TYPE_DECL for the specialization.
13715 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13716 uninstantiated templates. */
13718 static tree
13719 cp_parser_template_id (cp_parser *parser,
13720 bool template_keyword_p,
13721 bool check_dependency_p,
13722 enum tag_types tag_type,
13723 bool is_declaration)
13725 int i;
13726 tree templ;
13727 tree arguments;
13728 tree template_id;
13729 cp_token_position start_of_id = 0;
13730 deferred_access_check *chk;
13731 vec<deferred_access_check, va_gc> *access_check;
13732 cp_token *next_token = NULL, *next_token_2 = NULL;
13733 bool is_identifier;
13735 /* If the next token corresponds to a template-id, there is no need
13736 to reparse it. */
13737 next_token = cp_lexer_peek_token (parser->lexer);
13738 if (next_token->type == CPP_TEMPLATE_ID)
13740 struct tree_check *check_value;
13742 /* Get the stored value. */
13743 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13744 /* Perform any access checks that were deferred. */
13745 access_check = check_value->checks;
13746 if (access_check)
13748 FOR_EACH_VEC_ELT (*access_check, i, chk)
13749 perform_or_defer_access_check (chk->binfo,
13750 chk->decl,
13751 chk->diag_decl,
13752 tf_warning_or_error);
13754 /* Return the stored value. */
13755 return check_value->value;
13758 /* Avoid performing name lookup if there is no possibility of
13759 finding a template-id. */
13760 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13761 || (next_token->type == CPP_NAME
13762 && !cp_parser_nth_token_starts_template_argument_list_p
13763 (parser, 2)))
13765 cp_parser_error (parser, "expected template-id");
13766 return error_mark_node;
13769 /* Remember where the template-id starts. */
13770 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13771 start_of_id = cp_lexer_token_position (parser->lexer, false);
13773 push_deferring_access_checks (dk_deferred);
13775 /* Parse the template-name. */
13776 is_identifier = false;
13777 templ = cp_parser_template_name (parser, template_keyword_p,
13778 check_dependency_p,
13779 is_declaration,
13780 tag_type,
13781 &is_identifier);
13782 if (templ == error_mark_node || is_identifier)
13784 pop_deferring_access_checks ();
13785 return templ;
13788 /* If we find the sequence `[:' after a template-name, it's probably
13789 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13790 parse correctly the argument list. */
13791 next_token = cp_lexer_peek_token (parser->lexer);
13792 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13793 if (next_token->type == CPP_OPEN_SQUARE
13794 && next_token->flags & DIGRAPH
13795 && next_token_2->type == CPP_COLON
13796 && !(next_token_2->flags & PREV_WHITE))
13798 cp_parser_parse_tentatively (parser);
13799 /* Change `:' into `::'. */
13800 next_token_2->type = CPP_SCOPE;
13801 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13802 CPP_LESS. */
13803 cp_lexer_consume_token (parser->lexer);
13805 /* Parse the arguments. */
13806 arguments = cp_parser_enclosed_template_argument_list (parser);
13807 if (!cp_parser_parse_definitely (parser))
13809 /* If we couldn't parse an argument list, then we revert our changes
13810 and return simply an error. Maybe this is not a template-id
13811 after all. */
13812 next_token_2->type = CPP_COLON;
13813 cp_parser_error (parser, "expected %<<%>");
13814 pop_deferring_access_checks ();
13815 return error_mark_node;
13817 /* Otherwise, emit an error about the invalid digraph, but continue
13818 parsing because we got our argument list. */
13819 if (permerror (next_token->location,
13820 "%<<::%> cannot begin a template-argument list"))
13822 static bool hint = false;
13823 inform (next_token->location,
13824 "%<<:%> is an alternate spelling for %<[%>."
13825 " Insert whitespace between %<<%> and %<::%>");
13826 if (!hint && !flag_permissive)
13828 inform (next_token->location, "(if you use %<-fpermissive%> "
13829 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13830 "accept your code)");
13831 hint = true;
13835 else
13837 /* Look for the `<' that starts the template-argument-list. */
13838 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13840 pop_deferring_access_checks ();
13841 return error_mark_node;
13843 /* Parse the arguments. */
13844 arguments = cp_parser_enclosed_template_argument_list (parser);
13847 /* Build a representation of the specialization. */
13848 if (identifier_p (templ))
13849 template_id = build_min_nt_loc (next_token->location,
13850 TEMPLATE_ID_EXPR,
13851 templ, arguments);
13852 else if (DECL_TYPE_TEMPLATE_P (templ)
13853 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13855 bool entering_scope;
13856 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13857 template (rather than some instantiation thereof) only if
13858 is not nested within some other construct. For example, in
13859 "template <typename T> void f(T) { A<T>::", A<T> is just an
13860 instantiation of A. */
13861 entering_scope = (template_parm_scope_p ()
13862 && cp_lexer_next_token_is (parser->lexer,
13863 CPP_SCOPE));
13864 template_id
13865 = finish_template_type (templ, arguments, entering_scope);
13867 else if (variable_template_p (templ))
13869 template_id = lookup_template_variable (templ, arguments);
13871 else
13873 /* If it's not a class-template or a template-template, it should be
13874 a function-template. */
13875 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13876 || TREE_CODE (templ) == OVERLOAD
13877 || BASELINK_P (templ)));
13879 template_id = lookup_template_function (templ, arguments);
13882 /* If parsing tentatively, replace the sequence of tokens that makes
13883 up the template-id with a CPP_TEMPLATE_ID token. That way,
13884 should we re-parse the token stream, we will not have to repeat
13885 the effort required to do the parse, nor will we issue duplicate
13886 error messages about problems during instantiation of the
13887 template. */
13888 if (start_of_id
13889 /* Don't do this if we had a parse error in a declarator; re-parsing
13890 might succeed if a name changes meaning (60361). */
13891 && !(cp_parser_error_occurred (parser)
13892 && cp_parser_parsing_tentatively (parser)
13893 && parser->in_declarator_p))
13895 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13897 /* Reset the contents of the START_OF_ID token. */
13898 token->type = CPP_TEMPLATE_ID;
13899 /* Retrieve any deferred checks. Do not pop this access checks yet
13900 so the memory will not be reclaimed during token replacing below. */
13901 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13902 token->u.tree_check_value->value = template_id;
13903 token->u.tree_check_value->checks = get_deferred_access_checks ();
13904 token->keyword = RID_MAX;
13906 /* Purge all subsequent tokens. */
13907 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13909 /* ??? Can we actually assume that, if template_id ==
13910 error_mark_node, we will have issued a diagnostic to the
13911 user, as opposed to simply marking the tentative parse as
13912 failed? */
13913 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13914 error_at (token->location, "parse error in template argument list");
13917 pop_to_parent_deferring_access_checks ();
13918 return template_id;
13921 /* Parse a template-name.
13923 template-name:
13924 identifier
13926 The standard should actually say:
13928 template-name:
13929 identifier
13930 operator-function-id
13932 A defect report has been filed about this issue.
13934 A conversion-function-id cannot be a template name because they cannot
13935 be part of a template-id. In fact, looking at this code:
13937 a.operator K<int>()
13939 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13940 It is impossible to call a templated conversion-function-id with an
13941 explicit argument list, since the only allowed template parameter is
13942 the type to which it is converting.
13944 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13945 `template' keyword, in a construction like:
13947 T::template f<3>()
13949 In that case `f' is taken to be a template-name, even though there
13950 is no way of knowing for sure.
13952 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13953 name refers to a set of overloaded functions, at least one of which
13954 is a template, or an IDENTIFIER_NODE with the name of the template,
13955 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13956 names are looked up inside uninstantiated templates. */
13958 static tree
13959 cp_parser_template_name (cp_parser* parser,
13960 bool template_keyword_p,
13961 bool check_dependency_p,
13962 bool is_declaration,
13963 enum tag_types tag_type,
13964 bool *is_identifier)
13966 tree identifier;
13967 tree decl;
13968 tree fns;
13969 cp_token *token = cp_lexer_peek_token (parser->lexer);
13971 /* If the next token is `operator', then we have either an
13972 operator-function-id or a conversion-function-id. */
13973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13975 /* We don't know whether we're looking at an
13976 operator-function-id or a conversion-function-id. */
13977 cp_parser_parse_tentatively (parser);
13978 /* Try an operator-function-id. */
13979 identifier = cp_parser_operator_function_id (parser);
13980 /* If that didn't work, try a conversion-function-id. */
13981 if (!cp_parser_parse_definitely (parser))
13983 cp_parser_error (parser, "expected template-name");
13984 return error_mark_node;
13987 /* Look for the identifier. */
13988 else
13989 identifier = cp_parser_identifier (parser);
13991 /* If we didn't find an identifier, we don't have a template-id. */
13992 if (identifier == error_mark_node)
13993 return error_mark_node;
13995 /* If the name immediately followed the `template' keyword, then it
13996 is a template-name. However, if the next token is not `<', then
13997 we do not treat it as a template-name, since it is not being used
13998 as part of a template-id. This enables us to handle constructs
13999 like:
14001 template <typename T> struct S { S(); };
14002 template <typename T> S<T>::S();
14004 correctly. We would treat `S' as a template -- if it were `S<T>'
14005 -- but we do not if there is no `<'. */
14007 if (processing_template_decl
14008 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14010 /* In a declaration, in a dependent context, we pretend that the
14011 "template" keyword was present in order to improve error
14012 recovery. For example, given:
14014 template <typename T> void f(T::X<int>);
14016 we want to treat "X<int>" as a template-id. */
14017 if (is_declaration
14018 && !template_keyword_p
14019 && parser->scope && TYPE_P (parser->scope)
14020 && check_dependency_p
14021 && dependent_scope_p (parser->scope)
14022 /* Do not do this for dtors (or ctors), since they never
14023 need the template keyword before their name. */
14024 && !constructor_name_p (identifier, parser->scope))
14026 cp_token_position start = 0;
14028 /* Explain what went wrong. */
14029 error_at (token->location, "non-template %qD used as template",
14030 identifier);
14031 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14032 parser->scope, identifier);
14033 /* If parsing tentatively, find the location of the "<" token. */
14034 if (cp_parser_simulate_error (parser))
14035 start = cp_lexer_token_position (parser->lexer, true);
14036 /* Parse the template arguments so that we can issue error
14037 messages about them. */
14038 cp_lexer_consume_token (parser->lexer);
14039 cp_parser_enclosed_template_argument_list (parser);
14040 /* Skip tokens until we find a good place from which to
14041 continue parsing. */
14042 cp_parser_skip_to_closing_parenthesis (parser,
14043 /*recovering=*/true,
14044 /*or_comma=*/true,
14045 /*consume_paren=*/false);
14046 /* If parsing tentatively, permanently remove the
14047 template argument list. That will prevent duplicate
14048 error messages from being issued about the missing
14049 "template" keyword. */
14050 if (start)
14051 cp_lexer_purge_tokens_after (parser->lexer, start);
14052 if (is_identifier)
14053 *is_identifier = true;
14054 return identifier;
14057 /* If the "template" keyword is present, then there is generally
14058 no point in doing name-lookup, so we just return IDENTIFIER.
14059 But, if the qualifying scope is non-dependent then we can
14060 (and must) do name-lookup normally. */
14061 if (template_keyword_p
14062 && (!parser->scope
14063 || (TYPE_P (parser->scope)
14064 && dependent_type_p (parser->scope))))
14065 return identifier;
14068 /* Look up the name. */
14069 decl = cp_parser_lookup_name (parser, identifier,
14070 tag_type,
14071 /*is_template=*/true,
14072 /*is_namespace=*/false,
14073 check_dependency_p,
14074 /*ambiguous_decls=*/NULL,
14075 token->location);
14077 decl = strip_using_decl (decl);
14079 /* If DECL is a template, then the name was a template-name. */
14080 if (TREE_CODE (decl) == TEMPLATE_DECL)
14082 if (TREE_DEPRECATED (decl)
14083 && deprecated_state != DEPRECATED_SUPPRESS)
14084 warn_deprecated_use (decl, NULL_TREE);
14086 else
14088 tree fn = NULL_TREE;
14090 /* The standard does not explicitly indicate whether a name that
14091 names a set of overloaded declarations, some of which are
14092 templates, is a template-name. However, such a name should
14093 be a template-name; otherwise, there is no way to form a
14094 template-id for the overloaded templates. */
14095 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14096 if (TREE_CODE (fns) == OVERLOAD)
14097 for (fn = fns; fn; fn = OVL_NEXT (fn))
14098 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14099 break;
14101 if (!fn)
14103 /* The name does not name a template. */
14104 cp_parser_error (parser, "expected template-name");
14105 return error_mark_node;
14109 /* If DECL is dependent, and refers to a function, then just return
14110 its name; we will look it up again during template instantiation. */
14111 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14113 tree scope = ovl_scope (decl);
14114 if (TYPE_P (scope) && dependent_type_p (scope))
14115 return identifier;
14118 return decl;
14121 /* Parse a template-argument-list.
14123 template-argument-list:
14124 template-argument ... [opt]
14125 template-argument-list , template-argument ... [opt]
14127 Returns a TREE_VEC containing the arguments. */
14129 static tree
14130 cp_parser_template_argument_list (cp_parser* parser)
14132 tree fixed_args[10];
14133 unsigned n_args = 0;
14134 unsigned alloced = 10;
14135 tree *arg_ary = fixed_args;
14136 tree vec;
14137 bool saved_in_template_argument_list_p;
14138 bool saved_ice_p;
14139 bool saved_non_ice_p;
14141 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14142 parser->in_template_argument_list_p = true;
14143 /* Even if the template-id appears in an integral
14144 constant-expression, the contents of the argument list do
14145 not. */
14146 saved_ice_p = parser->integral_constant_expression_p;
14147 parser->integral_constant_expression_p = false;
14148 saved_non_ice_p = parser->non_integral_constant_expression_p;
14149 parser->non_integral_constant_expression_p = false;
14151 /* Parse the arguments. */
14154 tree argument;
14156 if (n_args)
14157 /* Consume the comma. */
14158 cp_lexer_consume_token (parser->lexer);
14160 /* Parse the template-argument. */
14161 argument = cp_parser_template_argument (parser);
14163 /* If the next token is an ellipsis, we're expanding a template
14164 argument pack. */
14165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14167 if (argument == error_mark_node)
14169 cp_token *token = cp_lexer_peek_token (parser->lexer);
14170 error_at (token->location,
14171 "expected parameter pack before %<...%>");
14173 /* Consume the `...' token. */
14174 cp_lexer_consume_token (parser->lexer);
14176 /* Make the argument into a TYPE_PACK_EXPANSION or
14177 EXPR_PACK_EXPANSION. */
14178 argument = make_pack_expansion (argument);
14181 if (n_args == alloced)
14183 alloced *= 2;
14185 if (arg_ary == fixed_args)
14187 arg_ary = XNEWVEC (tree, alloced);
14188 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14190 else
14191 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14193 arg_ary[n_args++] = argument;
14195 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14197 vec = make_tree_vec (n_args);
14199 while (n_args--)
14200 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14202 if (arg_ary != fixed_args)
14203 free (arg_ary);
14204 parser->non_integral_constant_expression_p = saved_non_ice_p;
14205 parser->integral_constant_expression_p = saved_ice_p;
14206 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14207 #ifdef ENABLE_CHECKING
14208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14209 #endif
14210 return vec;
14213 /* Parse a template-argument.
14215 template-argument:
14216 assignment-expression
14217 type-id
14218 id-expression
14220 The representation is that of an assignment-expression, type-id, or
14221 id-expression -- except that the qualified id-expression is
14222 evaluated, so that the value returned is either a DECL or an
14223 OVERLOAD.
14225 Although the standard says "assignment-expression", it forbids
14226 throw-expressions or assignments in the template argument.
14227 Therefore, we use "conditional-expression" instead. */
14229 static tree
14230 cp_parser_template_argument (cp_parser* parser)
14232 tree argument;
14233 bool template_p;
14234 bool address_p;
14235 bool maybe_type_id = false;
14236 cp_token *token = NULL, *argument_start_token = NULL;
14237 location_t loc = 0;
14238 cp_id_kind idk;
14240 /* There's really no way to know what we're looking at, so we just
14241 try each alternative in order.
14243 [temp.arg]
14245 In a template-argument, an ambiguity between a type-id and an
14246 expression is resolved to a type-id, regardless of the form of
14247 the corresponding template-parameter.
14249 Therefore, we try a type-id first. */
14250 cp_parser_parse_tentatively (parser);
14251 argument = cp_parser_template_type_arg (parser);
14252 /* If there was no error parsing the type-id but the next token is a
14253 '>>', our behavior depends on which dialect of C++ we're
14254 parsing. In C++98, we probably found a typo for '> >'. But there
14255 are type-id which are also valid expressions. For instance:
14257 struct X { int operator >> (int); };
14258 template <int V> struct Foo {};
14259 Foo<X () >> 5> r;
14261 Here 'X()' is a valid type-id of a function type, but the user just
14262 wanted to write the expression "X() >> 5". Thus, we remember that we
14263 found a valid type-id, but we still try to parse the argument as an
14264 expression to see what happens.
14266 In C++0x, the '>>' will be considered two separate '>'
14267 tokens. */
14268 if (!cp_parser_error_occurred (parser)
14269 && cxx_dialect == cxx98
14270 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14272 maybe_type_id = true;
14273 cp_parser_abort_tentative_parse (parser);
14275 else
14277 /* If the next token isn't a `,' or a `>', then this argument wasn't
14278 really finished. This means that the argument is not a valid
14279 type-id. */
14280 if (!cp_parser_next_token_ends_template_argument_p (parser))
14281 cp_parser_error (parser, "expected template-argument");
14282 /* If that worked, we're done. */
14283 if (cp_parser_parse_definitely (parser))
14284 return argument;
14286 /* We're still not sure what the argument will be. */
14287 cp_parser_parse_tentatively (parser);
14288 /* Try a template. */
14289 argument_start_token = cp_lexer_peek_token (parser->lexer);
14290 argument = cp_parser_id_expression (parser,
14291 /*template_keyword_p=*/false,
14292 /*check_dependency_p=*/true,
14293 &template_p,
14294 /*declarator_p=*/false,
14295 /*optional_p=*/false);
14296 /* If the next token isn't a `,' or a `>', then this argument wasn't
14297 really finished. */
14298 if (!cp_parser_next_token_ends_template_argument_p (parser))
14299 cp_parser_error (parser, "expected template-argument");
14300 if (!cp_parser_error_occurred (parser))
14302 /* Figure out what is being referred to. If the id-expression
14303 was for a class template specialization, then we will have a
14304 TYPE_DECL at this point. There is no need to do name lookup
14305 at this point in that case. */
14306 if (TREE_CODE (argument) != TYPE_DECL)
14307 argument = cp_parser_lookup_name (parser, argument,
14308 none_type,
14309 /*is_template=*/template_p,
14310 /*is_namespace=*/false,
14311 /*check_dependency=*/true,
14312 /*ambiguous_decls=*/NULL,
14313 argument_start_token->location);
14314 if (TREE_CODE (argument) != TEMPLATE_DECL
14315 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14316 cp_parser_error (parser, "expected template-name");
14318 if (cp_parser_parse_definitely (parser))
14320 if (TREE_DEPRECATED (argument))
14321 warn_deprecated_use (argument, NULL_TREE);
14322 return argument;
14324 /* It must be a non-type argument. There permitted cases are given
14325 in [temp.arg.nontype]:
14327 -- an integral constant-expression of integral or enumeration
14328 type; or
14330 -- the name of a non-type template-parameter; or
14332 -- the name of an object or function with external linkage...
14334 -- the address of an object or function with external linkage...
14336 -- a pointer to member... */
14337 /* Look for a non-type template parameter. */
14338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14340 cp_parser_parse_tentatively (parser);
14341 argument = cp_parser_primary_expression (parser,
14342 /*address_p=*/false,
14343 /*cast_p=*/false,
14344 /*template_arg_p=*/true,
14345 &idk);
14346 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14347 || !cp_parser_next_token_ends_template_argument_p (parser))
14348 cp_parser_simulate_error (parser);
14349 if (cp_parser_parse_definitely (parser))
14350 return argument;
14353 /* If the next token is "&", the argument must be the address of an
14354 object or function with external linkage. */
14355 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14356 if (address_p)
14358 loc = cp_lexer_peek_token (parser->lexer)->location;
14359 cp_lexer_consume_token (parser->lexer);
14361 /* See if we might have an id-expression. */
14362 token = cp_lexer_peek_token (parser->lexer);
14363 if (token->type == CPP_NAME
14364 || token->keyword == RID_OPERATOR
14365 || token->type == CPP_SCOPE
14366 || token->type == CPP_TEMPLATE_ID
14367 || token->type == CPP_NESTED_NAME_SPECIFIER)
14369 cp_parser_parse_tentatively (parser);
14370 argument = cp_parser_primary_expression (parser,
14371 address_p,
14372 /*cast_p=*/false,
14373 /*template_arg_p=*/true,
14374 &idk);
14375 if (cp_parser_error_occurred (parser)
14376 || !cp_parser_next_token_ends_template_argument_p (parser))
14377 cp_parser_abort_tentative_parse (parser);
14378 else
14380 tree probe;
14382 if (INDIRECT_REF_P (argument))
14384 /* Strip the dereference temporarily. */
14385 gcc_assert (REFERENCE_REF_P (argument));
14386 argument = TREE_OPERAND (argument, 0);
14389 /* If we're in a template, we represent a qualified-id referring
14390 to a static data member as a SCOPE_REF even if the scope isn't
14391 dependent so that we can check access control later. */
14392 probe = argument;
14393 if (TREE_CODE (probe) == SCOPE_REF)
14394 probe = TREE_OPERAND (probe, 1);
14395 if (VAR_P (probe))
14397 /* A variable without external linkage might still be a
14398 valid constant-expression, so no error is issued here
14399 if the external-linkage check fails. */
14400 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14401 cp_parser_simulate_error (parser);
14403 else if (is_overloaded_fn (argument))
14404 /* All overloaded functions are allowed; if the external
14405 linkage test does not pass, an error will be issued
14406 later. */
14408 else if (address_p
14409 && (TREE_CODE (argument) == OFFSET_REF
14410 || TREE_CODE (argument) == SCOPE_REF))
14411 /* A pointer-to-member. */
14413 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14415 else
14416 cp_parser_simulate_error (parser);
14418 if (cp_parser_parse_definitely (parser))
14420 if (address_p)
14421 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14422 tf_warning_or_error);
14423 else
14424 argument = convert_from_reference (argument);
14425 return argument;
14429 /* If the argument started with "&", there are no other valid
14430 alternatives at this point. */
14431 if (address_p)
14433 cp_parser_error (parser, "invalid non-type template argument");
14434 return error_mark_node;
14437 /* If the argument wasn't successfully parsed as a type-id followed
14438 by '>>', the argument can only be a constant expression now.
14439 Otherwise, we try parsing the constant-expression tentatively,
14440 because the argument could really be a type-id. */
14441 if (maybe_type_id)
14442 cp_parser_parse_tentatively (parser);
14443 argument = cp_parser_constant_expression (parser);
14445 if (!maybe_type_id)
14446 return argument;
14447 if (!cp_parser_next_token_ends_template_argument_p (parser))
14448 cp_parser_error (parser, "expected template-argument");
14449 if (cp_parser_parse_definitely (parser))
14450 return argument;
14451 /* We did our best to parse the argument as a non type-id, but that
14452 was the only alternative that matched (albeit with a '>' after
14453 it). We can assume it's just a typo from the user, and a
14454 diagnostic will then be issued. */
14455 return cp_parser_template_type_arg (parser);
14458 /* Parse an explicit-instantiation.
14460 explicit-instantiation:
14461 template declaration
14463 Although the standard says `declaration', what it really means is:
14465 explicit-instantiation:
14466 template decl-specifier-seq [opt] declarator [opt] ;
14468 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14469 supposed to be allowed. A defect report has been filed about this
14470 issue.
14472 GNU Extension:
14474 explicit-instantiation:
14475 storage-class-specifier template
14476 decl-specifier-seq [opt] declarator [opt] ;
14477 function-specifier template
14478 decl-specifier-seq [opt] declarator [opt] ; */
14480 static void
14481 cp_parser_explicit_instantiation (cp_parser* parser)
14483 int declares_class_or_enum;
14484 cp_decl_specifier_seq decl_specifiers;
14485 tree extension_specifier = NULL_TREE;
14487 timevar_push (TV_TEMPLATE_INST);
14489 /* Look for an (optional) storage-class-specifier or
14490 function-specifier. */
14491 if (cp_parser_allow_gnu_extensions_p (parser))
14493 extension_specifier
14494 = cp_parser_storage_class_specifier_opt (parser);
14495 if (!extension_specifier)
14496 extension_specifier
14497 = cp_parser_function_specifier_opt (parser,
14498 /*decl_specs=*/NULL);
14501 /* Look for the `template' keyword. */
14502 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14503 /* Let the front end know that we are processing an explicit
14504 instantiation. */
14505 begin_explicit_instantiation ();
14506 /* [temp.explicit] says that we are supposed to ignore access
14507 control while processing explicit instantiation directives. */
14508 push_deferring_access_checks (dk_no_check);
14509 /* Parse a decl-specifier-seq. */
14510 cp_parser_decl_specifier_seq (parser,
14511 CP_PARSER_FLAGS_OPTIONAL,
14512 &decl_specifiers,
14513 &declares_class_or_enum);
14514 /* If there was exactly one decl-specifier, and it declared a class,
14515 and there's no declarator, then we have an explicit type
14516 instantiation. */
14517 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14519 tree type;
14521 type = check_tag_decl (&decl_specifiers,
14522 /*explicit_type_instantiation_p=*/true);
14523 /* Turn access control back on for names used during
14524 template instantiation. */
14525 pop_deferring_access_checks ();
14526 if (type)
14527 do_type_instantiation (type, extension_specifier,
14528 /*complain=*/tf_error);
14530 else
14532 cp_declarator *declarator;
14533 tree decl;
14535 /* Parse the declarator. */
14536 declarator
14537 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14538 /*ctor_dtor_or_conv_p=*/NULL,
14539 /*parenthesized_p=*/NULL,
14540 /*member_p=*/false,
14541 /*friend_p=*/false);
14542 if (declares_class_or_enum & 2)
14543 cp_parser_check_for_definition_in_return_type (declarator,
14544 decl_specifiers.type,
14545 decl_specifiers.locations[ds_type_spec]);
14546 if (declarator != cp_error_declarator)
14548 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14549 permerror (decl_specifiers.locations[ds_inline],
14550 "explicit instantiation shall not use"
14551 " %<inline%> specifier");
14552 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14553 permerror (decl_specifiers.locations[ds_constexpr],
14554 "explicit instantiation shall not use"
14555 " %<constexpr%> specifier");
14557 decl = grokdeclarator (declarator, &decl_specifiers,
14558 NORMAL, 0, &decl_specifiers.attributes);
14559 /* Turn access control back on for names used during
14560 template instantiation. */
14561 pop_deferring_access_checks ();
14562 /* Do the explicit instantiation. */
14563 do_decl_instantiation (decl, extension_specifier);
14565 else
14567 pop_deferring_access_checks ();
14568 /* Skip the body of the explicit instantiation. */
14569 cp_parser_skip_to_end_of_statement (parser);
14572 /* We're done with the instantiation. */
14573 end_explicit_instantiation ();
14575 cp_parser_consume_semicolon_at_end_of_statement (parser);
14577 timevar_pop (TV_TEMPLATE_INST);
14580 /* Parse an explicit-specialization.
14582 explicit-specialization:
14583 template < > declaration
14585 Although the standard says `declaration', what it really means is:
14587 explicit-specialization:
14588 template <> decl-specifier [opt] init-declarator [opt] ;
14589 template <> function-definition
14590 template <> explicit-specialization
14591 template <> template-declaration */
14593 static void
14594 cp_parser_explicit_specialization (cp_parser* parser)
14596 bool need_lang_pop;
14597 cp_token *token = cp_lexer_peek_token (parser->lexer);
14599 /* Look for the `template' keyword. */
14600 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14601 /* Look for the `<'. */
14602 cp_parser_require (parser, CPP_LESS, RT_LESS);
14603 /* Look for the `>'. */
14604 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14605 /* We have processed another parameter list. */
14606 ++parser->num_template_parameter_lists;
14607 /* [temp]
14609 A template ... explicit specialization ... shall not have C
14610 linkage. */
14611 if (current_lang_name == lang_name_c)
14613 error_at (token->location, "template specialization with C linkage");
14614 /* Give it C++ linkage to avoid confusing other parts of the
14615 front end. */
14616 push_lang_context (lang_name_cplusplus);
14617 need_lang_pop = true;
14619 else
14620 need_lang_pop = false;
14621 /* Let the front end know that we are beginning a specialization. */
14622 if (!begin_specialization ())
14624 end_specialization ();
14625 return;
14628 /* If the next keyword is `template', we need to figure out whether
14629 or not we're looking a template-declaration. */
14630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14632 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14633 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14634 cp_parser_template_declaration_after_export (parser,
14635 /*member_p=*/false);
14636 else
14637 cp_parser_explicit_specialization (parser);
14639 else
14640 /* Parse the dependent declaration. */
14641 cp_parser_single_declaration (parser,
14642 /*checks=*/NULL,
14643 /*member_p=*/false,
14644 /*explicit_specialization_p=*/true,
14645 /*friend_p=*/NULL);
14646 /* We're done with the specialization. */
14647 end_specialization ();
14648 /* For the erroneous case of a template with C linkage, we pushed an
14649 implicit C++ linkage scope; exit that scope now. */
14650 if (need_lang_pop)
14651 pop_lang_context ();
14652 /* We're done with this parameter list. */
14653 --parser->num_template_parameter_lists;
14656 /* Parse a type-specifier.
14658 type-specifier:
14659 simple-type-specifier
14660 class-specifier
14661 enum-specifier
14662 elaborated-type-specifier
14663 cv-qualifier
14665 GNU Extension:
14667 type-specifier:
14668 __complex__
14670 Returns a representation of the type-specifier. For a
14671 class-specifier, enum-specifier, or elaborated-type-specifier, a
14672 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14674 The parser flags FLAGS is used to control type-specifier parsing.
14676 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14677 in a decl-specifier-seq.
14679 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14680 class-specifier, enum-specifier, or elaborated-type-specifier, then
14681 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14682 if a type is declared; 2 if it is defined. Otherwise, it is set to
14683 zero.
14685 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14686 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14687 is set to FALSE. */
14689 static tree
14690 cp_parser_type_specifier (cp_parser* parser,
14691 cp_parser_flags flags,
14692 cp_decl_specifier_seq *decl_specs,
14693 bool is_declaration,
14694 int* declares_class_or_enum,
14695 bool* is_cv_qualifier)
14697 tree type_spec = NULL_TREE;
14698 cp_token *token;
14699 enum rid keyword;
14700 cp_decl_spec ds = ds_last;
14702 /* Assume this type-specifier does not declare a new type. */
14703 if (declares_class_or_enum)
14704 *declares_class_or_enum = 0;
14705 /* And that it does not specify a cv-qualifier. */
14706 if (is_cv_qualifier)
14707 *is_cv_qualifier = false;
14708 /* Peek at the next token. */
14709 token = cp_lexer_peek_token (parser->lexer);
14711 /* If we're looking at a keyword, we can use that to guide the
14712 production we choose. */
14713 keyword = token->keyword;
14714 switch (keyword)
14716 case RID_ENUM:
14717 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14718 goto elaborated_type_specifier;
14720 /* Look for the enum-specifier. */
14721 type_spec = cp_parser_enum_specifier (parser);
14722 /* If that worked, we're done. */
14723 if (type_spec)
14725 if (declares_class_or_enum)
14726 *declares_class_or_enum = 2;
14727 if (decl_specs)
14728 cp_parser_set_decl_spec_type (decl_specs,
14729 type_spec,
14730 token,
14731 /*type_definition_p=*/true);
14732 return type_spec;
14734 else
14735 goto elaborated_type_specifier;
14737 /* Any of these indicate either a class-specifier, or an
14738 elaborated-type-specifier. */
14739 case RID_CLASS:
14740 case RID_STRUCT:
14741 case RID_UNION:
14742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14743 goto elaborated_type_specifier;
14745 /* Parse tentatively so that we can back up if we don't find a
14746 class-specifier. */
14747 cp_parser_parse_tentatively (parser);
14748 /* Look for the class-specifier. */
14749 type_spec = cp_parser_class_specifier (parser);
14750 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14751 /* If that worked, we're done. */
14752 if (cp_parser_parse_definitely (parser))
14754 if (declares_class_or_enum)
14755 *declares_class_or_enum = 2;
14756 if (decl_specs)
14757 cp_parser_set_decl_spec_type (decl_specs,
14758 type_spec,
14759 token,
14760 /*type_definition_p=*/true);
14761 return type_spec;
14764 /* Fall through. */
14765 elaborated_type_specifier:
14766 /* We're declaring (not defining) a class or enum. */
14767 if (declares_class_or_enum)
14768 *declares_class_or_enum = 1;
14770 /* Fall through. */
14771 case RID_TYPENAME:
14772 /* Look for an elaborated-type-specifier. */
14773 type_spec
14774 = (cp_parser_elaborated_type_specifier
14775 (parser,
14776 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14777 is_declaration));
14778 if (decl_specs)
14779 cp_parser_set_decl_spec_type (decl_specs,
14780 type_spec,
14781 token,
14782 /*type_definition_p=*/false);
14783 return type_spec;
14785 case RID_CONST:
14786 ds = ds_const;
14787 if (is_cv_qualifier)
14788 *is_cv_qualifier = true;
14789 break;
14791 case RID_VOLATILE:
14792 ds = ds_volatile;
14793 if (is_cv_qualifier)
14794 *is_cv_qualifier = true;
14795 break;
14797 case RID_RESTRICT:
14798 ds = ds_restrict;
14799 if (is_cv_qualifier)
14800 *is_cv_qualifier = true;
14801 break;
14803 case RID_COMPLEX:
14804 /* The `__complex__' keyword is a GNU extension. */
14805 ds = ds_complex;
14806 break;
14808 default:
14809 break;
14812 /* Handle simple keywords. */
14813 if (ds != ds_last)
14815 if (decl_specs)
14817 set_and_check_decl_spec_loc (decl_specs, ds, token);
14818 decl_specs->any_specifiers_p = true;
14820 return cp_lexer_consume_token (parser->lexer)->u.value;
14823 /* If we do not already have a type-specifier, assume we are looking
14824 at a simple-type-specifier. */
14825 type_spec = cp_parser_simple_type_specifier (parser,
14826 decl_specs,
14827 flags);
14829 /* If we didn't find a type-specifier, and a type-specifier was not
14830 optional in this context, issue an error message. */
14831 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14833 cp_parser_error (parser, "expected type specifier");
14834 return error_mark_node;
14837 return type_spec;
14840 /* Parse a simple-type-specifier.
14842 simple-type-specifier:
14843 :: [opt] nested-name-specifier [opt] type-name
14844 :: [opt] nested-name-specifier template template-id
14845 char
14846 wchar_t
14847 bool
14848 short
14850 long
14851 signed
14852 unsigned
14853 float
14854 double
14855 void
14857 C++0x Extension:
14859 simple-type-specifier:
14860 auto
14861 decltype ( expression )
14862 char16_t
14863 char32_t
14864 __underlying_type ( type-id )
14866 GNU Extension:
14868 simple-type-specifier:
14869 __int128
14870 __typeof__ unary-expression
14871 __typeof__ ( type-id )
14872 __typeof__ ( type-id ) { initializer-list , [opt] }
14874 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14875 appropriately updated. */
14877 static tree
14878 cp_parser_simple_type_specifier (cp_parser* parser,
14879 cp_decl_specifier_seq *decl_specs,
14880 cp_parser_flags flags)
14882 tree type = NULL_TREE;
14883 cp_token *token;
14884 int idx;
14886 /* Peek at the next token. */
14887 token = cp_lexer_peek_token (parser->lexer);
14889 /* If we're looking at a keyword, things are easy. */
14890 switch (token->keyword)
14892 case RID_CHAR:
14893 if (decl_specs)
14894 decl_specs->explicit_char_p = true;
14895 type = char_type_node;
14896 break;
14897 case RID_CHAR16:
14898 type = char16_type_node;
14899 break;
14900 case RID_CHAR32:
14901 type = char32_type_node;
14902 break;
14903 case RID_WCHAR:
14904 type = wchar_type_node;
14905 break;
14906 case RID_BOOL:
14907 type = boolean_type_node;
14908 break;
14909 case RID_SHORT:
14910 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14911 type = short_integer_type_node;
14912 break;
14913 case RID_INT:
14914 if (decl_specs)
14915 decl_specs->explicit_int_p = true;
14916 type = integer_type_node;
14917 break;
14918 case RID_INT_N_0:
14919 case RID_INT_N_1:
14920 case RID_INT_N_2:
14921 case RID_INT_N_3:
14922 idx = token->keyword - RID_INT_N_0;
14923 if (! int_n_enabled_p [idx])
14924 break;
14925 if (decl_specs)
14927 decl_specs->explicit_intN_p = true;
14928 decl_specs->int_n_idx = idx;
14930 type = int_n_trees [idx].signed_type;
14931 break;
14932 case RID_LONG:
14933 if (decl_specs)
14934 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14935 type = long_integer_type_node;
14936 break;
14937 case RID_SIGNED:
14938 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14939 type = integer_type_node;
14940 break;
14941 case RID_UNSIGNED:
14942 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14943 type = unsigned_type_node;
14944 break;
14945 case RID_FLOAT:
14946 type = float_type_node;
14947 break;
14948 case RID_DOUBLE:
14949 type = double_type_node;
14950 break;
14951 case RID_VOID:
14952 type = void_type_node;
14953 break;
14955 case RID_AUTO:
14956 maybe_warn_cpp0x (CPP0X_AUTO);
14957 if (parser->auto_is_implicit_function_template_parm_p)
14959 /* The 'auto' might be the placeholder return type for a function decl
14960 with trailing return type. */
14961 bool have_trailing_return_fn_decl = false;
14962 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14963 == CPP_OPEN_PAREN)
14965 cp_parser_parse_tentatively (parser);
14966 cp_lexer_consume_token (parser->lexer);
14967 cp_lexer_consume_token (parser->lexer);
14968 if (cp_parser_skip_to_closing_parenthesis (parser,
14969 /*recovering*/false,
14970 /*or_comma*/false,
14971 /*consume_paren*/true))
14972 have_trailing_return_fn_decl
14973 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14974 cp_parser_abort_tentative_parse (parser);
14977 if (have_trailing_return_fn_decl)
14979 type = make_auto ();
14980 break;
14983 if (cxx_dialect >= cxx14)
14984 type = synthesize_implicit_template_parm (parser);
14985 else
14986 type = error_mark_node;
14988 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14990 if (cxx_dialect < cxx14)
14991 error_at (token->location,
14992 "use of %<auto%> in lambda parameter declaration "
14993 "only available with "
14994 "-std=c++14 or -std=gnu++14");
14996 else if (cxx_dialect < cxx14)
14997 error_at (token->location,
14998 "use of %<auto%> in parameter declaration "
14999 "only available with "
15000 "-std=c++14 or -std=gnu++14");
15001 else
15002 pedwarn (token->location, OPT_Wpedantic,
15003 "ISO C++ forbids use of %<auto%> in parameter "
15004 "declaration");
15006 else
15007 type = make_auto ();
15008 break;
15010 case RID_DECLTYPE:
15011 /* Since DR 743, decltype can either be a simple-type-specifier by
15012 itself or begin a nested-name-specifier. Parsing it will replace
15013 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15014 handling below decide what to do. */
15015 cp_parser_decltype (parser);
15016 cp_lexer_set_token_position (parser->lexer, token);
15017 break;
15019 case RID_TYPEOF:
15020 /* Consume the `typeof' token. */
15021 cp_lexer_consume_token (parser->lexer);
15022 /* Parse the operand to `typeof'. */
15023 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15024 /* If it is not already a TYPE, take its type. */
15025 if (!TYPE_P (type))
15026 type = finish_typeof (type);
15028 if (decl_specs)
15029 cp_parser_set_decl_spec_type (decl_specs, type,
15030 token,
15031 /*type_definition_p=*/false);
15033 return type;
15035 case RID_UNDERLYING_TYPE:
15036 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15037 if (decl_specs)
15038 cp_parser_set_decl_spec_type (decl_specs, type,
15039 token,
15040 /*type_definition_p=*/false);
15042 return type;
15044 case RID_BASES:
15045 case RID_DIRECT_BASES:
15046 type = cp_parser_trait_expr (parser, token->keyword);
15047 if (decl_specs)
15048 cp_parser_set_decl_spec_type (decl_specs, type,
15049 token,
15050 /*type_definition_p=*/false);
15051 return type;
15052 default:
15053 break;
15056 /* If token is an already-parsed decltype not followed by ::,
15057 it's a simple-type-specifier. */
15058 if (token->type == CPP_DECLTYPE
15059 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15061 type = token->u.value;
15062 if (decl_specs)
15064 cp_parser_set_decl_spec_type (decl_specs, type,
15065 token,
15066 /*type_definition_p=*/false);
15067 /* Remember that we are handling a decltype in order to
15068 implement the resolution of DR 1510 when the argument
15069 isn't instantiation dependent. */
15070 decl_specs->decltype_p = true;
15072 cp_lexer_consume_token (parser->lexer);
15073 return type;
15076 /* If the type-specifier was for a built-in type, we're done. */
15077 if (type)
15079 /* Record the type. */
15080 if (decl_specs
15081 && (token->keyword != RID_SIGNED
15082 && token->keyword != RID_UNSIGNED
15083 && token->keyword != RID_SHORT
15084 && token->keyword != RID_LONG))
15085 cp_parser_set_decl_spec_type (decl_specs,
15086 type,
15087 token,
15088 /*type_definition_p=*/false);
15089 if (decl_specs)
15090 decl_specs->any_specifiers_p = true;
15092 /* Consume the token. */
15093 cp_lexer_consume_token (parser->lexer);
15095 if (type == error_mark_node)
15096 return error_mark_node;
15098 /* There is no valid C++ program where a non-template type is
15099 followed by a "<". That usually indicates that the user thought
15100 that the type was a template. */
15101 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15102 token->location);
15104 return TYPE_NAME (type);
15107 /* The type-specifier must be a user-defined type. */
15108 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15110 bool qualified_p;
15111 bool global_p;
15113 /* Don't gobble tokens or issue error messages if this is an
15114 optional type-specifier. */
15115 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15116 cp_parser_parse_tentatively (parser);
15118 /* Look for the optional `::' operator. */
15119 global_p
15120 = (cp_parser_global_scope_opt (parser,
15121 /*current_scope_valid_p=*/false)
15122 != NULL_TREE);
15123 /* Look for the nested-name specifier. */
15124 qualified_p
15125 = (cp_parser_nested_name_specifier_opt (parser,
15126 /*typename_keyword_p=*/false,
15127 /*check_dependency_p=*/true,
15128 /*type_p=*/false,
15129 /*is_declaration=*/false)
15130 != NULL_TREE);
15131 token = cp_lexer_peek_token (parser->lexer);
15132 /* If we have seen a nested-name-specifier, and the next token
15133 is `template', then we are using the template-id production. */
15134 if (parser->scope
15135 && cp_parser_optional_template_keyword (parser))
15137 /* Look for the template-id. */
15138 type = cp_parser_template_id (parser,
15139 /*template_keyword_p=*/true,
15140 /*check_dependency_p=*/true,
15141 none_type,
15142 /*is_declaration=*/false);
15143 /* If the template-id did not name a type, we are out of
15144 luck. */
15145 if (TREE_CODE (type) != TYPE_DECL)
15147 cp_parser_error (parser, "expected template-id for type");
15148 type = NULL_TREE;
15151 /* Otherwise, look for a type-name. */
15152 else
15153 type = cp_parser_type_name (parser);
15154 /* Keep track of all name-lookups performed in class scopes. */
15155 if (type
15156 && !global_p
15157 && !qualified_p
15158 && TREE_CODE (type) == TYPE_DECL
15159 && identifier_p (DECL_NAME (type)))
15160 maybe_note_name_used_in_class (DECL_NAME (type), type);
15161 /* If it didn't work out, we don't have a TYPE. */
15162 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15163 && !cp_parser_parse_definitely (parser))
15164 type = NULL_TREE;
15165 if (type && decl_specs)
15166 cp_parser_set_decl_spec_type (decl_specs, type,
15167 token,
15168 /*type_definition_p=*/false);
15171 /* If we didn't get a type-name, issue an error message. */
15172 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15174 cp_parser_error (parser, "expected type-name");
15175 return error_mark_node;
15178 if (type && type != error_mark_node)
15180 /* See if TYPE is an Objective-C type, and if so, parse and
15181 accept any protocol references following it. Do this before
15182 the cp_parser_check_for_invalid_template_id() call, because
15183 Objective-C types can be followed by '<...>' which would
15184 enclose protocol names rather than template arguments, and so
15185 everything is fine. */
15186 if (c_dialect_objc () && !parser->scope
15187 && (objc_is_id (type) || objc_is_class_name (type)))
15189 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15190 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15192 /* Clobber the "unqualified" type previously entered into
15193 DECL_SPECS with the new, improved protocol-qualified version. */
15194 if (decl_specs)
15195 decl_specs->type = qual_type;
15197 return qual_type;
15200 /* There is no valid C++ program where a non-template type is
15201 followed by a "<". That usually indicates that the user
15202 thought that the type was a template. */
15203 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15204 none_type,
15205 token->location);
15208 return type;
15211 /* Parse a type-name.
15213 type-name:
15214 class-name
15215 enum-name
15216 typedef-name
15217 simple-template-id [in c++0x]
15219 enum-name:
15220 identifier
15222 typedef-name:
15223 identifier
15225 Returns a TYPE_DECL for the type. */
15227 static tree
15228 cp_parser_type_name (cp_parser* parser)
15230 tree type_decl;
15232 /* We can't know yet whether it is a class-name or not. */
15233 cp_parser_parse_tentatively (parser);
15234 /* Try a class-name. */
15235 type_decl = cp_parser_class_name (parser,
15236 /*typename_keyword_p=*/false,
15237 /*template_keyword_p=*/false,
15238 none_type,
15239 /*check_dependency_p=*/true,
15240 /*class_head_p=*/false,
15241 /*is_declaration=*/false);
15242 /* If it's not a class-name, keep looking. */
15243 if (!cp_parser_parse_definitely (parser))
15245 if (cxx_dialect < cxx11)
15246 /* It must be a typedef-name or an enum-name. */
15247 return cp_parser_nonclass_name (parser);
15249 cp_parser_parse_tentatively (parser);
15250 /* It is either a simple-template-id representing an
15251 instantiation of an alias template... */
15252 type_decl = cp_parser_template_id (parser,
15253 /*template_keyword_p=*/false,
15254 /*check_dependency_p=*/true,
15255 none_type,
15256 /*is_declaration=*/false);
15257 /* Note that this must be an instantiation of an alias template
15258 because [temp.names]/6 says:
15260 A template-id that names an alias template specialization
15261 is a type-name.
15263 Whereas [temp.names]/7 says:
15265 A simple-template-id that names a class template
15266 specialization is a class-name. */
15267 if (type_decl != NULL_TREE
15268 && TREE_CODE (type_decl) == TYPE_DECL
15269 && TYPE_DECL_ALIAS_P (type_decl))
15270 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15271 else
15272 cp_parser_simulate_error (parser);
15274 if (!cp_parser_parse_definitely (parser))
15275 /* ... Or a typedef-name or an enum-name. */
15276 return cp_parser_nonclass_name (parser);
15279 return type_decl;
15282 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15284 enum-name:
15285 identifier
15287 typedef-name:
15288 identifier
15290 Returns a TYPE_DECL for the type. */
15292 static tree
15293 cp_parser_nonclass_name (cp_parser* parser)
15295 tree type_decl;
15296 tree identifier;
15298 cp_token *token = cp_lexer_peek_token (parser->lexer);
15299 identifier = cp_parser_identifier (parser);
15300 if (identifier == error_mark_node)
15301 return error_mark_node;
15303 /* Look up the type-name. */
15304 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15306 type_decl = strip_using_decl (type_decl);
15308 if (TREE_CODE (type_decl) != TYPE_DECL
15309 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15311 /* See if this is an Objective-C type. */
15312 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15313 tree type = objc_get_protocol_qualified_type (identifier, protos);
15314 if (type)
15315 type_decl = TYPE_NAME (type);
15318 /* Issue an error if we did not find a type-name. */
15319 if (TREE_CODE (type_decl) != TYPE_DECL
15320 /* In Objective-C, we have the complication that class names are
15321 normally type names and start declarations (eg, the
15322 "NSObject" in "NSObject *object;"), but can be used in an
15323 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15324 is an expression. So, a classname followed by a dot is not a
15325 valid type-name. */
15326 || (objc_is_class_name (TREE_TYPE (type_decl))
15327 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15329 if (!cp_parser_simulate_error (parser))
15330 cp_parser_name_lookup_error (parser, identifier, type_decl,
15331 NLE_TYPE, token->location);
15332 return error_mark_node;
15334 /* Remember that the name was used in the definition of the
15335 current class so that we can check later to see if the
15336 meaning would have been different after the class was
15337 entirely defined. */
15338 else if (type_decl != error_mark_node
15339 && !parser->scope)
15340 maybe_note_name_used_in_class (identifier, type_decl);
15342 return type_decl;
15345 /* Parse an elaborated-type-specifier. Note that the grammar given
15346 here incorporates the resolution to DR68.
15348 elaborated-type-specifier:
15349 class-key :: [opt] nested-name-specifier [opt] identifier
15350 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15351 enum-key :: [opt] nested-name-specifier [opt] identifier
15352 typename :: [opt] nested-name-specifier identifier
15353 typename :: [opt] nested-name-specifier template [opt]
15354 template-id
15356 GNU extension:
15358 elaborated-type-specifier:
15359 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15360 class-key attributes :: [opt] nested-name-specifier [opt]
15361 template [opt] template-id
15362 enum attributes :: [opt] nested-name-specifier [opt] identifier
15364 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15365 declared `friend'. If IS_DECLARATION is TRUE, then this
15366 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15367 something is being declared.
15369 Returns the TYPE specified. */
15371 static tree
15372 cp_parser_elaborated_type_specifier (cp_parser* parser,
15373 bool is_friend,
15374 bool is_declaration)
15376 enum tag_types tag_type;
15377 tree identifier;
15378 tree type = NULL_TREE;
15379 tree attributes = NULL_TREE;
15380 tree globalscope;
15381 cp_token *token = NULL;
15383 /* See if we're looking at the `enum' keyword. */
15384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15386 /* Consume the `enum' token. */
15387 cp_lexer_consume_token (parser->lexer);
15388 /* Remember that it's an enumeration type. */
15389 tag_type = enum_type;
15390 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15391 enums) is used here. */
15392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15393 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15395 pedwarn (input_location, 0, "elaborated-type-specifier "
15396 "for a scoped enum must not use the %<%D%> keyword",
15397 cp_lexer_peek_token (parser->lexer)->u.value);
15398 /* Consume the `struct' or `class' and parse it anyway. */
15399 cp_lexer_consume_token (parser->lexer);
15401 /* Parse the attributes. */
15402 attributes = cp_parser_attributes_opt (parser);
15404 /* Or, it might be `typename'. */
15405 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15406 RID_TYPENAME))
15408 /* Consume the `typename' token. */
15409 cp_lexer_consume_token (parser->lexer);
15410 /* Remember that it's a `typename' type. */
15411 tag_type = typename_type;
15413 /* Otherwise it must be a class-key. */
15414 else
15416 tag_type = cp_parser_class_key (parser);
15417 if (tag_type == none_type)
15418 return error_mark_node;
15419 /* Parse the attributes. */
15420 attributes = cp_parser_attributes_opt (parser);
15423 /* Look for the `::' operator. */
15424 globalscope = cp_parser_global_scope_opt (parser,
15425 /*current_scope_valid_p=*/false);
15426 /* Look for the nested-name-specifier. */
15427 if (tag_type == typename_type && !globalscope)
15429 if (!cp_parser_nested_name_specifier (parser,
15430 /*typename_keyword_p=*/true,
15431 /*check_dependency_p=*/true,
15432 /*type_p=*/true,
15433 is_declaration))
15434 return error_mark_node;
15436 else
15437 /* Even though `typename' is not present, the proposed resolution
15438 to Core Issue 180 says that in `class A<T>::B', `B' should be
15439 considered a type-name, even if `A<T>' is dependent. */
15440 cp_parser_nested_name_specifier_opt (parser,
15441 /*typename_keyword_p=*/true,
15442 /*check_dependency_p=*/true,
15443 /*type_p=*/true,
15444 is_declaration);
15445 /* For everything but enumeration types, consider a template-id.
15446 For an enumeration type, consider only a plain identifier. */
15447 if (tag_type != enum_type)
15449 bool template_p = false;
15450 tree decl;
15452 /* Allow the `template' keyword. */
15453 template_p = cp_parser_optional_template_keyword (parser);
15454 /* If we didn't see `template', we don't know if there's a
15455 template-id or not. */
15456 if (!template_p)
15457 cp_parser_parse_tentatively (parser);
15458 /* Parse the template-id. */
15459 token = cp_lexer_peek_token (parser->lexer);
15460 decl = cp_parser_template_id (parser, template_p,
15461 /*check_dependency_p=*/true,
15462 tag_type,
15463 is_declaration);
15464 /* If we didn't find a template-id, look for an ordinary
15465 identifier. */
15466 if (!template_p && !cp_parser_parse_definitely (parser))
15468 /* We can get here when cp_parser_template_id, called by
15469 cp_parser_class_name with tag_type == none_type, succeeds
15470 and caches a BASELINK. Then, when called again here,
15471 instead of failing and returning an error_mark_node
15472 returns it (see template/typename17.C in C++11).
15473 ??? Could we diagnose this earlier? */
15474 else if (tag_type == typename_type && BASELINK_P (decl))
15476 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15477 type = error_mark_node;
15479 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15480 in effect, then we must assume that, upon instantiation, the
15481 template will correspond to a class. */
15482 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15483 && tag_type == typename_type)
15484 type = make_typename_type (parser->scope, decl,
15485 typename_type,
15486 /*complain=*/tf_error);
15487 /* If the `typename' keyword is in effect and DECL is not a type
15488 decl, then type is non existent. */
15489 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15491 else if (TREE_CODE (decl) == TYPE_DECL)
15492 type = check_elaborated_type_specifier (tag_type, decl,
15493 /*allow_template_p=*/true);
15494 else if (decl == error_mark_node)
15495 type = error_mark_node;
15498 if (!type)
15500 token = cp_lexer_peek_token (parser->lexer);
15501 identifier = cp_parser_identifier (parser);
15503 if (identifier == error_mark_node)
15505 parser->scope = NULL_TREE;
15506 return error_mark_node;
15509 /* For a `typename', we needn't call xref_tag. */
15510 if (tag_type == typename_type
15511 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15512 return cp_parser_make_typename_type (parser, identifier,
15513 token->location);
15515 /* Template parameter lists apply only if we are not within a
15516 function parameter list. */
15517 bool template_parm_lists_apply
15518 = parser->num_template_parameter_lists;
15519 if (template_parm_lists_apply)
15520 for (cp_binding_level *s = current_binding_level;
15521 s && s->kind != sk_template_parms;
15522 s = s->level_chain)
15523 if (s->kind == sk_function_parms)
15524 template_parm_lists_apply = false;
15526 /* Look up a qualified name in the usual way. */
15527 if (parser->scope)
15529 tree decl;
15530 tree ambiguous_decls;
15532 decl = cp_parser_lookup_name (parser, identifier,
15533 tag_type,
15534 /*is_template=*/false,
15535 /*is_namespace=*/false,
15536 /*check_dependency=*/true,
15537 &ambiguous_decls,
15538 token->location);
15540 /* If the lookup was ambiguous, an error will already have been
15541 issued. */
15542 if (ambiguous_decls)
15543 return error_mark_node;
15545 /* If we are parsing friend declaration, DECL may be a
15546 TEMPLATE_DECL tree node here. However, we need to check
15547 whether this TEMPLATE_DECL results in valid code. Consider
15548 the following example:
15550 namespace N {
15551 template <class T> class C {};
15553 class X {
15554 template <class T> friend class N::C; // #1, valid code
15556 template <class T> class Y {
15557 friend class N::C; // #2, invalid code
15560 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15561 name lookup of `N::C'. We see that friend declaration must
15562 be template for the code to be valid. Note that
15563 processing_template_decl does not work here since it is
15564 always 1 for the above two cases. */
15566 decl = (cp_parser_maybe_treat_template_as_class
15567 (decl, /*tag_name_p=*/is_friend
15568 && template_parm_lists_apply));
15570 if (TREE_CODE (decl) != TYPE_DECL)
15572 cp_parser_diagnose_invalid_type_name (parser,
15573 identifier,
15574 token->location);
15575 return error_mark_node;
15578 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15580 bool allow_template = (template_parm_lists_apply
15581 || DECL_SELF_REFERENCE_P (decl));
15582 type = check_elaborated_type_specifier (tag_type, decl,
15583 allow_template);
15585 if (type == error_mark_node)
15586 return error_mark_node;
15589 /* Forward declarations of nested types, such as
15591 class C1::C2;
15592 class C1::C2::C3;
15594 are invalid unless all components preceding the final '::'
15595 are complete. If all enclosing types are complete, these
15596 declarations become merely pointless.
15598 Invalid forward declarations of nested types are errors
15599 caught elsewhere in parsing. Those that are pointless arrive
15600 here. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15603 && !is_friend && !processing_explicit_instantiation)
15604 warning (0, "declaration %qD does not declare anything", decl);
15606 type = TREE_TYPE (decl);
15608 else
15610 /* An elaborated-type-specifier sometimes introduces a new type and
15611 sometimes names an existing type. Normally, the rule is that it
15612 introduces a new type only if there is not an existing type of
15613 the same name already in scope. For example, given:
15615 struct S {};
15616 void f() { struct S s; }
15618 the `struct S' in the body of `f' is the same `struct S' as in
15619 the global scope; the existing definition is used. However, if
15620 there were no global declaration, this would introduce a new
15621 local class named `S'.
15623 An exception to this rule applies to the following code:
15625 namespace N { struct S; }
15627 Here, the elaborated-type-specifier names a new type
15628 unconditionally; even if there is already an `S' in the
15629 containing scope this declaration names a new type.
15630 This exception only applies if the elaborated-type-specifier
15631 forms the complete declaration:
15633 [class.name]
15635 A declaration consisting solely of `class-key identifier ;' is
15636 either a redeclaration of the name in the current scope or a
15637 forward declaration of the identifier as a class name. It
15638 introduces the name into the current scope.
15640 We are in this situation precisely when the next token is a `;'.
15642 An exception to the exception is that a `friend' declaration does
15643 *not* name a new type; i.e., given:
15645 struct S { friend struct T; };
15647 `T' is not a new type in the scope of `S'.
15649 Also, `new struct S' or `sizeof (struct S)' never results in the
15650 definition of a new type; a new type can only be declared in a
15651 declaration context. */
15653 tag_scope ts;
15654 bool template_p;
15656 if (is_friend)
15657 /* Friends have special name lookup rules. */
15658 ts = ts_within_enclosing_non_class;
15659 else if (is_declaration
15660 && cp_lexer_next_token_is (parser->lexer,
15661 CPP_SEMICOLON))
15662 /* This is a `class-key identifier ;' */
15663 ts = ts_current;
15664 else
15665 ts = ts_global;
15667 template_p =
15668 (template_parm_lists_apply
15669 && (cp_parser_next_token_starts_class_definition_p (parser)
15670 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15671 /* An unqualified name was used to reference this type, so
15672 there were no qualifying templates. */
15673 if (template_parm_lists_apply
15674 && !cp_parser_check_template_parameters (parser,
15675 /*num_templates=*/0,
15676 token->location,
15677 /*declarator=*/NULL))
15678 return error_mark_node;
15679 type = xref_tag (tag_type, identifier, ts, template_p);
15683 if (type == error_mark_node)
15684 return error_mark_node;
15686 /* Allow attributes on forward declarations of classes. */
15687 if (attributes)
15689 if (TREE_CODE (type) == TYPENAME_TYPE)
15690 warning (OPT_Wattributes,
15691 "attributes ignored on uninstantiated type");
15692 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15693 && ! processing_explicit_instantiation)
15694 warning (OPT_Wattributes,
15695 "attributes ignored on template instantiation");
15696 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15697 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15698 else
15699 warning (OPT_Wattributes,
15700 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15703 if (tag_type != enum_type)
15705 /* Indicate whether this class was declared as a `class' or as a
15706 `struct'. */
15707 if (TREE_CODE (type) == RECORD_TYPE)
15708 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15709 cp_parser_check_class_key (tag_type, type);
15712 /* A "<" cannot follow an elaborated type specifier. If that
15713 happens, the user was probably trying to form a template-id. */
15714 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15715 token->location);
15717 return type;
15720 /* Parse an enum-specifier.
15722 enum-specifier:
15723 enum-head { enumerator-list [opt] }
15724 enum-head { enumerator-list , } [C++0x]
15726 enum-head:
15727 enum-key identifier [opt] enum-base [opt]
15728 enum-key nested-name-specifier identifier enum-base [opt]
15730 enum-key:
15731 enum
15732 enum class [C++0x]
15733 enum struct [C++0x]
15735 enum-base: [C++0x]
15736 : type-specifier-seq
15738 opaque-enum-specifier:
15739 enum-key identifier enum-base [opt] ;
15741 GNU Extensions:
15742 enum-key attributes[opt] identifier [opt] enum-base [opt]
15743 { enumerator-list [opt] }attributes[opt]
15744 enum-key attributes[opt] identifier [opt] enum-base [opt]
15745 { enumerator-list, }attributes[opt] [C++0x]
15747 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15748 if the token stream isn't an enum-specifier after all. */
15750 static tree
15751 cp_parser_enum_specifier (cp_parser* parser)
15753 tree identifier;
15754 tree type = NULL_TREE;
15755 tree prev_scope;
15756 tree nested_name_specifier = NULL_TREE;
15757 tree attributes;
15758 bool scoped_enum_p = false;
15759 bool has_underlying_type = false;
15760 bool nested_being_defined = false;
15761 bool new_value_list = false;
15762 bool is_new_type = false;
15763 bool is_anonymous = false;
15764 tree underlying_type = NULL_TREE;
15765 cp_token *type_start_token = NULL;
15766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15768 parser->colon_corrects_to_scope_p = false;
15770 /* Parse tentatively so that we can back up if we don't find a
15771 enum-specifier. */
15772 cp_parser_parse_tentatively (parser);
15774 /* Caller guarantees that the current token is 'enum', an identifier
15775 possibly follows, and the token after that is an opening brace.
15776 If we don't have an identifier, fabricate an anonymous name for
15777 the enumeration being defined. */
15778 cp_lexer_consume_token (parser->lexer);
15780 /* Parse the "class" or "struct", which indicates a scoped
15781 enumeration type in C++0x. */
15782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15783 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15785 if (cxx_dialect < cxx11)
15786 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15788 /* Consume the `struct' or `class' token. */
15789 cp_lexer_consume_token (parser->lexer);
15791 scoped_enum_p = true;
15794 attributes = cp_parser_attributes_opt (parser);
15796 /* Clear the qualification. */
15797 parser->scope = NULL_TREE;
15798 parser->qualifying_scope = NULL_TREE;
15799 parser->object_scope = NULL_TREE;
15801 /* Figure out in what scope the declaration is being placed. */
15802 prev_scope = current_scope ();
15804 type_start_token = cp_lexer_peek_token (parser->lexer);
15806 push_deferring_access_checks (dk_no_check);
15807 nested_name_specifier
15808 = cp_parser_nested_name_specifier_opt (parser,
15809 /*typename_keyword_p=*/true,
15810 /*check_dependency_p=*/false,
15811 /*type_p=*/false,
15812 /*is_declaration=*/false);
15814 if (nested_name_specifier)
15816 tree name;
15818 identifier = cp_parser_identifier (parser);
15819 name = cp_parser_lookup_name (parser, identifier,
15820 enum_type,
15821 /*is_template=*/false,
15822 /*is_namespace=*/false,
15823 /*check_dependency=*/true,
15824 /*ambiguous_decls=*/NULL,
15825 input_location);
15826 if (name && name != error_mark_node)
15828 type = TREE_TYPE (name);
15829 if (TREE_CODE (type) == TYPENAME_TYPE)
15831 /* Are template enums allowed in ISO? */
15832 if (template_parm_scope_p ())
15833 pedwarn (type_start_token->location, OPT_Wpedantic,
15834 "%qD is an enumeration template", name);
15835 /* ignore a typename reference, for it will be solved by name
15836 in start_enum. */
15837 type = NULL_TREE;
15840 else if (nested_name_specifier == error_mark_node)
15841 /* We already issued an error. */;
15842 else
15843 error_at (type_start_token->location,
15844 "%qD is not an enumerator-name", identifier);
15846 else
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15849 identifier = cp_parser_identifier (parser);
15850 else
15852 identifier = make_anon_name ();
15853 is_anonymous = true;
15854 if (scoped_enum_p)
15855 error_at (type_start_token->location,
15856 "anonymous scoped enum is not allowed");
15859 pop_deferring_access_checks ();
15861 /* Check for the `:' that denotes a specified underlying type in C++0x.
15862 Note that a ':' could also indicate a bitfield width, however. */
15863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15865 cp_decl_specifier_seq type_specifiers;
15867 /* Consume the `:'. */
15868 cp_lexer_consume_token (parser->lexer);
15870 /* Parse the type-specifier-seq. */
15871 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15872 /*is_trailing_return=*/false,
15873 &type_specifiers);
15875 /* At this point this is surely not elaborated type specifier. */
15876 if (!cp_parser_parse_definitely (parser))
15877 return NULL_TREE;
15879 if (cxx_dialect < cxx11)
15880 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15882 has_underlying_type = true;
15884 /* If that didn't work, stop. */
15885 if (type_specifiers.type != error_mark_node)
15887 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15888 /*initialized=*/0, NULL);
15889 if (underlying_type == error_mark_node
15890 || check_for_bare_parameter_packs (underlying_type))
15891 underlying_type = NULL_TREE;
15895 /* Look for the `{' but don't consume it yet. */
15896 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15898 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15900 cp_parser_error (parser, "expected %<{%>");
15901 if (has_underlying_type)
15903 type = NULL_TREE;
15904 goto out;
15907 /* An opaque-enum-specifier must have a ';' here. */
15908 if ((scoped_enum_p || underlying_type)
15909 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15911 cp_parser_error (parser, "expected %<;%> or %<{%>");
15912 if (has_underlying_type)
15914 type = NULL_TREE;
15915 goto out;
15920 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15921 return NULL_TREE;
15923 if (nested_name_specifier)
15925 if (CLASS_TYPE_P (nested_name_specifier))
15927 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15928 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15929 push_scope (nested_name_specifier);
15931 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15933 push_nested_namespace (nested_name_specifier);
15937 /* Issue an error message if type-definitions are forbidden here. */
15938 if (!cp_parser_check_type_definition (parser))
15939 type = error_mark_node;
15940 else
15941 /* Create the new type. We do this before consuming the opening
15942 brace so the enum will be recorded as being on the line of its
15943 tag (or the 'enum' keyword, if there is no tag). */
15944 type = start_enum (identifier, type, underlying_type,
15945 scoped_enum_p, &is_new_type);
15947 /* If the next token is not '{' it is an opaque-enum-specifier or an
15948 elaborated-type-specifier. */
15949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15951 timevar_push (TV_PARSE_ENUM);
15952 if (nested_name_specifier
15953 && nested_name_specifier != error_mark_node)
15955 /* The following catches invalid code such as:
15956 enum class S<int>::E { A, B, C }; */
15957 if (!processing_specialization
15958 && CLASS_TYPE_P (nested_name_specifier)
15959 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15960 error_at (type_start_token->location, "cannot add an enumerator "
15961 "list to a template instantiation");
15963 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15965 error_at (type_start_token->location,
15966 "%<%T::%E%> has not been declared",
15967 TYPE_CONTEXT (nested_name_specifier),
15968 nested_name_specifier);
15969 type = error_mark_node;
15971 /* If that scope does not contain the scope in which the
15972 class was originally declared, the program is invalid. */
15973 else if (prev_scope && !is_ancestor (prev_scope,
15974 nested_name_specifier))
15976 if (at_namespace_scope_p ())
15977 error_at (type_start_token->location,
15978 "declaration of %qD in namespace %qD which does not "
15979 "enclose %qD",
15980 type, prev_scope, nested_name_specifier);
15981 else
15982 error_at (type_start_token->location,
15983 "declaration of %qD in %qD which does not "
15984 "enclose %qD",
15985 type, prev_scope, nested_name_specifier);
15986 type = error_mark_node;
15990 if (scoped_enum_p)
15991 begin_scope (sk_scoped_enum, type);
15993 /* Consume the opening brace. */
15994 cp_lexer_consume_token (parser->lexer);
15996 if (type == error_mark_node)
15997 ; /* Nothing to add */
15998 else if (OPAQUE_ENUM_P (type)
15999 || (cxx_dialect > cxx98 && processing_specialization))
16001 new_value_list = true;
16002 SET_OPAQUE_ENUM_P (type, false);
16003 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16005 else
16007 error_at (type_start_token->location,
16008 "multiple definition of %q#T", type);
16009 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16010 "previous definition here");
16011 type = error_mark_node;
16014 if (type == error_mark_node)
16015 cp_parser_skip_to_end_of_block_or_statement (parser);
16016 /* If the next token is not '}', then there are some enumerators. */
16017 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16019 if (is_anonymous && !scoped_enum_p)
16020 pedwarn (type_start_token->location, OPT_Wpedantic,
16021 "ISO C++ forbids empty anonymous enum");
16023 else
16024 cp_parser_enumerator_list (parser, type);
16026 /* Consume the final '}'. */
16027 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16029 if (scoped_enum_p)
16030 finish_scope ();
16031 timevar_pop (TV_PARSE_ENUM);
16033 else
16035 /* If a ';' follows, then it is an opaque-enum-specifier
16036 and additional restrictions apply. */
16037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16039 if (is_anonymous)
16040 error_at (type_start_token->location,
16041 "opaque-enum-specifier without name");
16042 else if (nested_name_specifier)
16043 error_at (type_start_token->location,
16044 "opaque-enum-specifier must use a simple identifier");
16048 /* Look for trailing attributes to apply to this enumeration, and
16049 apply them if appropriate. */
16050 if (cp_parser_allow_gnu_extensions_p (parser))
16052 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16053 trailing_attr = chainon (trailing_attr, attributes);
16054 cplus_decl_attributes (&type,
16055 trailing_attr,
16056 (int) ATTR_FLAG_TYPE_IN_PLACE);
16059 /* Finish up the enumeration. */
16060 if (type != error_mark_node)
16062 if (new_value_list)
16063 finish_enum_value_list (type);
16064 if (is_new_type)
16065 finish_enum (type);
16068 if (nested_name_specifier)
16070 if (CLASS_TYPE_P (nested_name_specifier))
16072 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16073 pop_scope (nested_name_specifier);
16075 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16077 pop_nested_namespace (nested_name_specifier);
16080 out:
16081 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16082 return type;
16085 /* Parse an enumerator-list. The enumerators all have the indicated
16086 TYPE.
16088 enumerator-list:
16089 enumerator-definition
16090 enumerator-list , enumerator-definition */
16092 static void
16093 cp_parser_enumerator_list (cp_parser* parser, tree type)
16095 while (true)
16097 /* Parse an enumerator-definition. */
16098 cp_parser_enumerator_definition (parser, type);
16100 /* If the next token is not a ',', we've reached the end of
16101 the list. */
16102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16103 break;
16104 /* Otherwise, consume the `,' and keep going. */
16105 cp_lexer_consume_token (parser->lexer);
16106 /* If the next token is a `}', there is a trailing comma. */
16107 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16109 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16110 pedwarn (input_location, OPT_Wpedantic,
16111 "comma at end of enumerator list");
16112 break;
16117 /* Parse an enumerator-definition. The enumerator has the indicated
16118 TYPE.
16120 enumerator-definition:
16121 enumerator
16122 enumerator = constant-expression
16124 enumerator:
16125 identifier
16127 GNU Extensions:
16129 enumerator-definition:
16130 enumerator attributes [opt]
16131 enumerator attributes [opt] = constant-expression */
16133 static void
16134 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16136 tree identifier;
16137 tree value;
16138 location_t loc;
16140 /* Save the input location because we are interested in the location
16141 of the identifier and not the location of the explicit value. */
16142 loc = cp_lexer_peek_token (parser->lexer)->location;
16144 /* Look for the identifier. */
16145 identifier = cp_parser_identifier (parser);
16146 if (identifier == error_mark_node)
16147 return;
16149 /* Parse any specified attributes. */
16150 tree attrs = cp_parser_attributes_opt (parser);
16152 /* If the next token is an '=', then there is an explicit value. */
16153 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16155 /* Consume the `=' token. */
16156 cp_lexer_consume_token (parser->lexer);
16157 /* Parse the value. */
16158 value = cp_parser_constant_expression (parser);
16160 else
16161 value = NULL_TREE;
16163 /* If we are processing a template, make sure the initializer of the
16164 enumerator doesn't contain any bare template parameter pack. */
16165 if (check_for_bare_parameter_packs (value))
16166 value = error_mark_node;
16168 /* Create the enumerator. */
16169 build_enumerator (identifier, value, type, attrs, loc);
16172 /* Parse a namespace-name.
16174 namespace-name:
16175 original-namespace-name
16176 namespace-alias
16178 Returns the NAMESPACE_DECL for the namespace. */
16180 static tree
16181 cp_parser_namespace_name (cp_parser* parser)
16183 tree identifier;
16184 tree namespace_decl;
16186 cp_token *token = cp_lexer_peek_token (parser->lexer);
16188 /* Get the name of the namespace. */
16189 identifier = cp_parser_identifier (parser);
16190 if (identifier == error_mark_node)
16191 return error_mark_node;
16193 /* Look up the identifier in the currently active scope. Look only
16194 for namespaces, due to:
16196 [basic.lookup.udir]
16198 When looking up a namespace-name in a using-directive or alias
16199 definition, only namespace names are considered.
16201 And:
16203 [basic.lookup.qual]
16205 During the lookup of a name preceding the :: scope resolution
16206 operator, object, function, and enumerator names are ignored.
16208 (Note that cp_parser_qualifying_entity only calls this
16209 function if the token after the name is the scope resolution
16210 operator.) */
16211 namespace_decl = cp_parser_lookup_name (parser, identifier,
16212 none_type,
16213 /*is_template=*/false,
16214 /*is_namespace=*/true,
16215 /*check_dependency=*/true,
16216 /*ambiguous_decls=*/NULL,
16217 token->location);
16218 /* If it's not a namespace, issue an error. */
16219 if (namespace_decl == error_mark_node
16220 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16222 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16223 error_at (token->location, "%qD is not a namespace-name", identifier);
16224 cp_parser_error (parser, "expected namespace-name");
16225 namespace_decl = error_mark_node;
16228 return namespace_decl;
16231 /* Parse a namespace-definition.
16233 namespace-definition:
16234 named-namespace-definition
16235 unnamed-namespace-definition
16237 named-namespace-definition:
16238 original-namespace-definition
16239 extension-namespace-definition
16241 original-namespace-definition:
16242 namespace identifier { namespace-body }
16244 extension-namespace-definition:
16245 namespace original-namespace-name { namespace-body }
16247 unnamed-namespace-definition:
16248 namespace { namespace-body } */
16250 static void
16251 cp_parser_namespace_definition (cp_parser* parser)
16253 tree identifier, attribs;
16254 bool has_visibility;
16255 bool is_inline;
16257 cp_ensure_no_omp_declare_simd (parser);
16258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16260 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16261 is_inline = true;
16262 cp_lexer_consume_token (parser->lexer);
16264 else
16265 is_inline = false;
16267 /* Look for the `namespace' keyword. */
16268 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16270 /* Get the name of the namespace. We do not attempt to distinguish
16271 between an original-namespace-definition and an
16272 extension-namespace-definition at this point. The semantic
16273 analysis routines are responsible for that. */
16274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16275 identifier = cp_parser_identifier (parser);
16276 else
16277 identifier = NULL_TREE;
16279 /* Parse any specified attributes. */
16280 attribs = cp_parser_attributes_opt (parser);
16282 /* Look for the `{' to start the namespace. */
16283 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16284 /* Start the namespace. */
16285 push_namespace (identifier);
16287 /* "inline namespace" is equivalent to a stub namespace definition
16288 followed by a strong using directive. */
16289 if (is_inline)
16291 tree name_space = current_namespace;
16292 /* Set up namespace association. */
16293 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16294 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16295 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16296 /* Import the contents of the inline namespace. */
16297 pop_namespace ();
16298 do_using_directive (name_space);
16299 push_namespace (identifier);
16302 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16304 /* Parse the body of the namespace. */
16305 cp_parser_namespace_body (parser);
16307 if (has_visibility)
16308 pop_visibility (1);
16310 /* Finish the namespace. */
16311 pop_namespace ();
16312 /* Look for the final `}'. */
16313 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16316 /* Parse a namespace-body.
16318 namespace-body:
16319 declaration-seq [opt] */
16321 static void
16322 cp_parser_namespace_body (cp_parser* parser)
16324 cp_parser_declaration_seq_opt (parser);
16327 /* Parse a namespace-alias-definition.
16329 namespace-alias-definition:
16330 namespace identifier = qualified-namespace-specifier ; */
16332 static void
16333 cp_parser_namespace_alias_definition (cp_parser* parser)
16335 tree identifier;
16336 tree namespace_specifier;
16338 cp_token *token = cp_lexer_peek_token (parser->lexer);
16340 /* Look for the `namespace' keyword. */
16341 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16342 /* Look for the identifier. */
16343 identifier = cp_parser_identifier (parser);
16344 if (identifier == error_mark_node)
16345 return;
16346 /* Look for the `=' token. */
16347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16348 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16350 error_at (token->location, "%<namespace%> definition is not allowed here");
16351 /* Skip the definition. */
16352 cp_lexer_consume_token (parser->lexer);
16353 if (cp_parser_skip_to_closing_brace (parser))
16354 cp_lexer_consume_token (parser->lexer);
16355 return;
16357 cp_parser_require (parser, CPP_EQ, RT_EQ);
16358 /* Look for the qualified-namespace-specifier. */
16359 namespace_specifier
16360 = cp_parser_qualified_namespace_specifier (parser);
16361 /* Look for the `;' token. */
16362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16364 /* Register the alias in the symbol table. */
16365 do_namespace_alias (identifier, namespace_specifier);
16368 /* Parse a qualified-namespace-specifier.
16370 qualified-namespace-specifier:
16371 :: [opt] nested-name-specifier [opt] namespace-name
16373 Returns a NAMESPACE_DECL corresponding to the specified
16374 namespace. */
16376 static tree
16377 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16379 /* Look for the optional `::'. */
16380 cp_parser_global_scope_opt (parser,
16381 /*current_scope_valid_p=*/false);
16383 /* Look for the optional nested-name-specifier. */
16384 cp_parser_nested_name_specifier_opt (parser,
16385 /*typename_keyword_p=*/false,
16386 /*check_dependency_p=*/true,
16387 /*type_p=*/false,
16388 /*is_declaration=*/true);
16390 return cp_parser_namespace_name (parser);
16393 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16394 access declaration.
16396 using-declaration:
16397 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16398 using :: unqualified-id ;
16400 access-declaration:
16401 qualified-id ;
16405 static bool
16406 cp_parser_using_declaration (cp_parser* parser,
16407 bool access_declaration_p)
16409 cp_token *token;
16410 bool typename_p = false;
16411 bool global_scope_p;
16412 tree decl;
16413 tree identifier;
16414 tree qscope;
16415 int oldcount = errorcount;
16416 cp_token *diag_token = NULL;
16418 if (access_declaration_p)
16420 diag_token = cp_lexer_peek_token (parser->lexer);
16421 cp_parser_parse_tentatively (parser);
16423 else
16425 /* Look for the `using' keyword. */
16426 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16428 /* Peek at the next token. */
16429 token = cp_lexer_peek_token (parser->lexer);
16430 /* See if it's `typename'. */
16431 if (token->keyword == RID_TYPENAME)
16433 /* Remember that we've seen it. */
16434 typename_p = true;
16435 /* Consume the `typename' token. */
16436 cp_lexer_consume_token (parser->lexer);
16440 /* Look for the optional global scope qualification. */
16441 global_scope_p
16442 = (cp_parser_global_scope_opt (parser,
16443 /*current_scope_valid_p=*/false)
16444 != NULL_TREE);
16446 /* If we saw `typename', or didn't see `::', then there must be a
16447 nested-name-specifier present. */
16448 if (typename_p || !global_scope_p)
16450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16451 /*check_dependency_p=*/true,
16452 /*type_p=*/false,
16453 /*is_declaration=*/true);
16454 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16456 cp_parser_skip_to_end_of_block_or_statement (parser);
16457 return false;
16460 /* Otherwise, we could be in either of the two productions. In that
16461 case, treat the nested-name-specifier as optional. */
16462 else
16463 qscope = cp_parser_nested_name_specifier_opt (parser,
16464 /*typename_keyword_p=*/false,
16465 /*check_dependency_p=*/true,
16466 /*type_p=*/false,
16467 /*is_declaration=*/true);
16468 if (!qscope)
16469 qscope = global_namespace;
16470 else if (UNSCOPED_ENUM_P (qscope))
16471 qscope = CP_TYPE_CONTEXT (qscope);
16473 if (access_declaration_p && cp_parser_error_occurred (parser))
16474 /* Something has already gone wrong; there's no need to parse
16475 further. Since an error has occurred, the return value of
16476 cp_parser_parse_definitely will be false, as required. */
16477 return cp_parser_parse_definitely (parser);
16479 token = cp_lexer_peek_token (parser->lexer);
16480 /* Parse the unqualified-id. */
16481 identifier = cp_parser_unqualified_id (parser,
16482 /*template_keyword_p=*/false,
16483 /*check_dependency_p=*/true,
16484 /*declarator_p=*/true,
16485 /*optional_p=*/false);
16487 if (access_declaration_p)
16489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16490 cp_parser_simulate_error (parser);
16491 if (!cp_parser_parse_definitely (parser))
16492 return false;
16495 /* The function we call to handle a using-declaration is different
16496 depending on what scope we are in. */
16497 if (qscope == error_mark_node || identifier == error_mark_node)
16499 else if (!identifier_p (identifier)
16500 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16501 /* [namespace.udecl]
16503 A using declaration shall not name a template-id. */
16504 error_at (token->location,
16505 "a template-id may not appear in a using-declaration");
16506 else
16508 if (at_class_scope_p ())
16510 /* Create the USING_DECL. */
16511 decl = do_class_using_decl (parser->scope, identifier);
16513 if (decl && typename_p)
16514 USING_DECL_TYPENAME_P (decl) = 1;
16516 if (check_for_bare_parameter_packs (decl))
16518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16519 return false;
16521 else
16522 /* Add it to the list of members in this class. */
16523 finish_member_declaration (decl);
16525 else
16527 decl = cp_parser_lookup_name_simple (parser,
16528 identifier,
16529 token->location);
16530 if (decl == error_mark_node)
16531 cp_parser_name_lookup_error (parser, identifier,
16532 decl, NLE_NULL,
16533 token->location);
16534 else if (check_for_bare_parameter_packs (decl))
16536 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16537 return false;
16539 else if (!at_namespace_scope_p ())
16540 do_local_using_decl (decl, qscope, identifier);
16541 else
16542 do_toplevel_using_decl (decl, qscope, identifier);
16546 /* Look for the final `;'. */
16547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16549 if (access_declaration_p && errorcount == oldcount)
16550 warning_at (diag_token->location, OPT_Wdeprecated,
16551 "access declarations are deprecated "
16552 "in favour of using-declarations; "
16553 "suggestion: add the %<using%> keyword");
16555 return true;
16558 /* Parse an alias-declaration.
16560 alias-declaration:
16561 using identifier attribute-specifier-seq [opt] = type-id */
16563 static tree
16564 cp_parser_alias_declaration (cp_parser* parser)
16566 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16567 location_t id_location;
16568 cp_declarator *declarator;
16569 cp_decl_specifier_seq decl_specs;
16570 bool member_p;
16571 const char *saved_message = NULL;
16573 /* Look for the `using' keyword. */
16574 cp_token *using_token
16575 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16576 if (using_token == NULL)
16577 return error_mark_node;
16579 id_location = cp_lexer_peek_token (parser->lexer)->location;
16580 id = cp_parser_identifier (parser);
16581 if (id == error_mark_node)
16582 return error_mark_node;
16584 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16585 attributes = cp_parser_attributes_opt (parser);
16586 if (attributes == error_mark_node)
16587 return error_mark_node;
16589 cp_parser_require (parser, CPP_EQ, RT_EQ);
16591 if (cp_parser_error_occurred (parser))
16592 return error_mark_node;
16594 cp_parser_commit_to_tentative_parse (parser);
16596 /* Now we are going to parse the type-id of the declaration. */
16599 [dcl.type]/3 says:
16601 "A type-specifier-seq shall not define a class or enumeration
16602 unless it appears in the type-id of an alias-declaration (7.1.3) that
16603 is not the declaration of a template-declaration."
16605 In other words, if we currently are in an alias template, the
16606 type-id should not define a type.
16608 So let's set parser->type_definition_forbidden_message in that
16609 case; cp_parser_check_type_definition (called by
16610 cp_parser_class_specifier) will then emit an error if a type is
16611 defined in the type-id. */
16612 if (parser->num_template_parameter_lists)
16614 saved_message = parser->type_definition_forbidden_message;
16615 parser->type_definition_forbidden_message =
16616 G_("types may not be defined in alias template declarations");
16619 type = cp_parser_type_id (parser);
16621 /* Restore the error message if need be. */
16622 if (parser->num_template_parameter_lists)
16623 parser->type_definition_forbidden_message = saved_message;
16625 if (type == error_mark_node
16626 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16628 cp_parser_skip_to_end_of_block_or_statement (parser);
16629 return error_mark_node;
16632 /* A typedef-name can also be introduced by an alias-declaration. The
16633 identifier following the using keyword becomes a typedef-name. It has
16634 the same semantics as if it were introduced by the typedef
16635 specifier. In particular, it does not define a new type and it shall
16636 not appear in the type-id. */
16638 clear_decl_specs (&decl_specs);
16639 decl_specs.type = type;
16640 if (attributes != NULL_TREE)
16642 decl_specs.attributes = attributes;
16643 set_and_check_decl_spec_loc (&decl_specs,
16644 ds_attribute,
16645 attrs_token);
16647 set_and_check_decl_spec_loc (&decl_specs,
16648 ds_typedef,
16649 using_token);
16650 set_and_check_decl_spec_loc (&decl_specs,
16651 ds_alias,
16652 using_token);
16654 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16655 declarator->id_loc = id_location;
16657 member_p = at_class_scope_p ();
16658 if (member_p)
16659 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16660 NULL_TREE, attributes);
16661 else
16662 decl = start_decl (declarator, &decl_specs, 0,
16663 attributes, NULL_TREE, &pushed_scope);
16664 if (decl == error_mark_node)
16665 return decl;
16667 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16669 if (pushed_scope)
16670 pop_scope (pushed_scope);
16672 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16673 added into the symbol table; otherwise, return the TYPE_DECL. */
16674 if (DECL_LANG_SPECIFIC (decl)
16675 && DECL_TEMPLATE_INFO (decl)
16676 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16678 decl = DECL_TI_TEMPLATE (decl);
16679 if (member_p)
16680 check_member_template (decl);
16683 return decl;
16686 /* Parse a using-directive.
16688 using-directive:
16689 using namespace :: [opt] nested-name-specifier [opt]
16690 namespace-name ; */
16692 static void
16693 cp_parser_using_directive (cp_parser* parser)
16695 tree namespace_decl;
16696 tree attribs;
16698 /* Look for the `using' keyword. */
16699 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16700 /* And the `namespace' keyword. */
16701 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16702 /* Look for the optional `::' operator. */
16703 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16704 /* And the optional nested-name-specifier. */
16705 cp_parser_nested_name_specifier_opt (parser,
16706 /*typename_keyword_p=*/false,
16707 /*check_dependency_p=*/true,
16708 /*type_p=*/false,
16709 /*is_declaration=*/true);
16710 /* Get the namespace being used. */
16711 namespace_decl = cp_parser_namespace_name (parser);
16712 /* And any specified attributes. */
16713 attribs = cp_parser_attributes_opt (parser);
16714 /* Update the symbol table. */
16715 parse_using_directive (namespace_decl, attribs);
16716 /* Look for the final `;'. */
16717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16720 /* Parse an asm-definition.
16722 asm-definition:
16723 asm ( string-literal ) ;
16725 GNU Extension:
16727 asm-definition:
16728 asm volatile [opt] ( string-literal ) ;
16729 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16730 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16731 : asm-operand-list [opt] ) ;
16732 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16733 : asm-operand-list [opt]
16734 : asm-clobber-list [opt] ) ;
16735 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16736 : asm-clobber-list [opt]
16737 : asm-goto-list ) ; */
16739 static void
16740 cp_parser_asm_definition (cp_parser* parser)
16742 tree string;
16743 tree outputs = NULL_TREE;
16744 tree inputs = NULL_TREE;
16745 tree clobbers = NULL_TREE;
16746 tree labels = NULL_TREE;
16747 tree asm_stmt;
16748 bool volatile_p = false;
16749 bool extended_p = false;
16750 bool invalid_inputs_p = false;
16751 bool invalid_outputs_p = false;
16752 bool goto_p = false;
16753 required_token missing = RT_NONE;
16755 /* Look for the `asm' keyword. */
16756 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16758 if (parser->in_function_body
16759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16761 error ("%<asm%> in %<constexpr%> function");
16762 cp_function_chain->invalid_constexpr = true;
16765 /* See if the next token is `volatile'. */
16766 if (cp_parser_allow_gnu_extensions_p (parser)
16767 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16769 /* Remember that we saw the `volatile' keyword. */
16770 volatile_p = true;
16771 /* Consume the token. */
16772 cp_lexer_consume_token (parser->lexer);
16774 if (cp_parser_allow_gnu_extensions_p (parser)
16775 && parser->in_function_body
16776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16778 /* Remember that we saw the `goto' keyword. */
16779 goto_p = true;
16780 /* Consume the token. */
16781 cp_lexer_consume_token (parser->lexer);
16783 /* Look for the opening `('. */
16784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16785 return;
16786 /* Look for the string. */
16787 string = cp_parser_string_literal (parser, false, false);
16788 if (string == error_mark_node)
16790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16791 /*consume_paren=*/true);
16792 return;
16795 /* If we're allowing GNU extensions, check for the extended assembly
16796 syntax. Unfortunately, the `:' tokens need not be separated by
16797 a space in C, and so, for compatibility, we tolerate that here
16798 too. Doing that means that we have to treat the `::' operator as
16799 two `:' tokens. */
16800 if (cp_parser_allow_gnu_extensions_p (parser)
16801 && parser->in_function_body
16802 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16803 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16805 bool inputs_p = false;
16806 bool clobbers_p = false;
16807 bool labels_p = false;
16809 /* The extended syntax was used. */
16810 extended_p = true;
16812 /* Look for outputs. */
16813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16815 /* Consume the `:'. */
16816 cp_lexer_consume_token (parser->lexer);
16817 /* Parse the output-operands. */
16818 if (cp_lexer_next_token_is_not (parser->lexer,
16819 CPP_COLON)
16820 && cp_lexer_next_token_is_not (parser->lexer,
16821 CPP_SCOPE)
16822 && cp_lexer_next_token_is_not (parser->lexer,
16823 CPP_CLOSE_PAREN)
16824 && !goto_p)
16826 outputs = cp_parser_asm_operand_list (parser);
16827 if (outputs == error_mark_node)
16828 invalid_outputs_p = true;
16831 /* If the next token is `::', there are no outputs, and the
16832 next token is the beginning of the inputs. */
16833 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16834 /* The inputs are coming next. */
16835 inputs_p = true;
16837 /* Look for inputs. */
16838 if (inputs_p
16839 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16841 /* Consume the `:' or `::'. */
16842 cp_lexer_consume_token (parser->lexer);
16843 /* Parse the output-operands. */
16844 if (cp_lexer_next_token_is_not (parser->lexer,
16845 CPP_COLON)
16846 && cp_lexer_next_token_is_not (parser->lexer,
16847 CPP_SCOPE)
16848 && cp_lexer_next_token_is_not (parser->lexer,
16849 CPP_CLOSE_PAREN))
16851 inputs = cp_parser_asm_operand_list (parser);
16852 if (inputs == error_mark_node)
16853 invalid_inputs_p = true;
16856 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16857 /* The clobbers are coming next. */
16858 clobbers_p = true;
16860 /* Look for clobbers. */
16861 if (clobbers_p
16862 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16864 clobbers_p = true;
16865 /* Consume the `:' or `::'. */
16866 cp_lexer_consume_token (parser->lexer);
16867 /* Parse the clobbers. */
16868 if (cp_lexer_next_token_is_not (parser->lexer,
16869 CPP_COLON)
16870 && cp_lexer_next_token_is_not (parser->lexer,
16871 CPP_CLOSE_PAREN))
16872 clobbers = cp_parser_asm_clobber_list (parser);
16874 else if (goto_p
16875 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16876 /* The labels are coming next. */
16877 labels_p = true;
16879 /* Look for labels. */
16880 if (labels_p
16881 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16883 labels_p = true;
16884 /* Consume the `:' or `::'. */
16885 cp_lexer_consume_token (parser->lexer);
16886 /* Parse the labels. */
16887 labels = cp_parser_asm_label_list (parser);
16890 if (goto_p && !labels_p)
16891 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16893 else if (goto_p)
16894 missing = RT_COLON_SCOPE;
16896 /* Look for the closing `)'. */
16897 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16898 missing ? missing : RT_CLOSE_PAREN))
16899 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16900 /*consume_paren=*/true);
16901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16903 if (!invalid_inputs_p && !invalid_outputs_p)
16905 /* Create the ASM_EXPR. */
16906 if (parser->in_function_body)
16908 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16909 inputs, clobbers, labels);
16910 /* If the extended syntax was not used, mark the ASM_EXPR. */
16911 if (!extended_p)
16913 tree temp = asm_stmt;
16914 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16915 temp = TREE_OPERAND (temp, 0);
16917 ASM_INPUT_P (temp) = 1;
16920 else
16921 symtab->finalize_toplevel_asm (string);
16925 /* Declarators [gram.dcl.decl] */
16927 /* Parse an init-declarator.
16929 init-declarator:
16930 declarator initializer [opt]
16932 GNU Extension:
16934 init-declarator:
16935 declarator asm-specification [opt] attributes [opt] initializer [opt]
16937 function-definition:
16938 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16939 function-body
16940 decl-specifier-seq [opt] declarator function-try-block
16942 GNU Extension:
16944 function-definition:
16945 __extension__ function-definition
16947 TM Extension:
16949 function-definition:
16950 decl-specifier-seq [opt] declarator function-transaction-block
16952 The DECL_SPECIFIERS apply to this declarator. Returns a
16953 representation of the entity declared. If MEMBER_P is TRUE, then
16954 this declarator appears in a class scope. The new DECL created by
16955 this declarator is returned.
16957 The CHECKS are access checks that should be performed once we know
16958 what entity is being declared (and, therefore, what classes have
16959 befriended it).
16961 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16962 for a function-definition here as well. If the declarator is a
16963 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16964 be TRUE upon return. By that point, the function-definition will
16965 have been completely parsed.
16967 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16968 is FALSE.
16970 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16971 parsed declaration if it is an uninitialized single declarator not followed
16972 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16973 if present, will not be consumed. If returned, this declarator will be
16974 created with SD_INITIALIZED but will not call cp_finish_decl.
16976 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16977 and there is an initializer, the pointed location_t is set to the
16978 location of the '=' or `(', or '{' in C++11 token introducing the
16979 initializer. */
16981 static tree
16982 cp_parser_init_declarator (cp_parser* parser,
16983 cp_decl_specifier_seq *decl_specifiers,
16984 vec<deferred_access_check, va_gc> *checks,
16985 bool function_definition_allowed_p,
16986 bool member_p,
16987 int declares_class_or_enum,
16988 bool* function_definition_p,
16989 tree* maybe_range_for_decl,
16990 location_t* init_loc)
16992 cp_token *token = NULL, *asm_spec_start_token = NULL,
16993 *attributes_start_token = NULL;
16994 cp_declarator *declarator;
16995 tree prefix_attributes;
16996 tree attributes = NULL;
16997 tree asm_specification;
16998 tree initializer;
16999 tree decl = NULL_TREE;
17000 tree scope;
17001 int is_initialized;
17002 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17003 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17004 "(...)". */
17005 enum cpp_ttype initialization_kind;
17006 bool is_direct_init = false;
17007 bool is_non_constant_init;
17008 int ctor_dtor_or_conv_p;
17009 bool friend_p = cp_parser_friend_p (decl_specifiers);
17010 tree pushed_scope = NULL_TREE;
17011 bool range_for_decl_p = false;
17012 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17013 location_t tmp_init_loc = UNKNOWN_LOCATION;
17015 /* Gather the attributes that were provided with the
17016 decl-specifiers. */
17017 prefix_attributes = decl_specifiers->attributes;
17019 /* Assume that this is not the declarator for a function
17020 definition. */
17021 if (function_definition_p)
17022 *function_definition_p = false;
17024 /* Default arguments are only permitted for function parameters. */
17025 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17026 parser->default_arg_ok_p = false;
17028 /* Defer access checks while parsing the declarator; we cannot know
17029 what names are accessible until we know what is being
17030 declared. */
17031 resume_deferring_access_checks ();
17033 /* Parse the declarator. */
17034 token = cp_lexer_peek_token (parser->lexer);
17035 declarator
17036 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17037 &ctor_dtor_or_conv_p,
17038 /*parenthesized_p=*/NULL,
17039 member_p, friend_p);
17040 /* Gather up the deferred checks. */
17041 stop_deferring_access_checks ();
17043 parser->default_arg_ok_p = saved_default_arg_ok_p;
17045 /* If the DECLARATOR was erroneous, there's no need to go
17046 further. */
17047 if (declarator == cp_error_declarator)
17048 return error_mark_node;
17050 /* Check that the number of template-parameter-lists is OK. */
17051 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17052 token->location))
17053 return error_mark_node;
17055 if (declares_class_or_enum & 2)
17056 cp_parser_check_for_definition_in_return_type (declarator,
17057 decl_specifiers->type,
17058 decl_specifiers->locations[ds_type_spec]);
17060 /* Figure out what scope the entity declared by the DECLARATOR is
17061 located in. `grokdeclarator' sometimes changes the scope, so
17062 we compute it now. */
17063 scope = get_scope_of_declarator (declarator);
17065 /* Perform any lookups in the declared type which were thought to be
17066 dependent, but are not in the scope of the declarator. */
17067 decl_specifiers->type
17068 = maybe_update_decl_type (decl_specifiers->type, scope);
17070 /* If we're allowing GNU extensions, look for an
17071 asm-specification. */
17072 if (cp_parser_allow_gnu_extensions_p (parser))
17074 /* Look for an asm-specification. */
17075 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17076 asm_specification = cp_parser_asm_specification_opt (parser);
17078 else
17079 asm_specification = NULL_TREE;
17081 /* Look for attributes. */
17082 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17083 attributes = cp_parser_attributes_opt (parser);
17085 /* Peek at the next token. */
17086 token = cp_lexer_peek_token (parser->lexer);
17088 bool bogus_implicit_tmpl = false;
17090 if (function_declarator_p (declarator))
17092 /* Check to see if the token indicates the start of a
17093 function-definition. */
17094 if (cp_parser_token_starts_function_definition_p (token))
17096 if (!function_definition_allowed_p)
17098 /* If a function-definition should not appear here, issue an
17099 error message. */
17100 cp_parser_error (parser,
17101 "a function-definition is not allowed here");
17102 return error_mark_node;
17105 location_t func_brace_location
17106 = cp_lexer_peek_token (parser->lexer)->location;
17108 /* Neither attributes nor an asm-specification are allowed
17109 on a function-definition. */
17110 if (asm_specification)
17111 error_at (asm_spec_start_token->location,
17112 "an asm-specification is not allowed "
17113 "on a function-definition");
17114 if (attributes)
17115 error_at (attributes_start_token->location,
17116 "attributes are not allowed "
17117 "on a function-definition");
17118 /* This is a function-definition. */
17119 *function_definition_p = true;
17121 /* Parse the function definition. */
17122 if (member_p)
17123 decl = cp_parser_save_member_function_body (parser,
17124 decl_specifiers,
17125 declarator,
17126 prefix_attributes);
17127 else
17128 decl =
17129 (cp_parser_function_definition_from_specifiers_and_declarator
17130 (parser, decl_specifiers, prefix_attributes, declarator));
17132 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17134 /* This is where the prologue starts... */
17135 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17136 = func_brace_location;
17139 return decl;
17142 else if (parser->fully_implicit_function_template_p)
17144 /* A non-template declaration involving a function parameter list
17145 containing an implicit template parameter will be made into a
17146 template. If the resulting declaration is not going to be an
17147 actual function then finish the template scope here to prevent it.
17148 An error message will be issued once we have a decl to talk about.
17150 FIXME probably we should do type deduction rather than create an
17151 implicit template, but the standard currently doesn't allow it. */
17152 bogus_implicit_tmpl = true;
17153 finish_fully_implicit_template (parser, NULL_TREE);
17156 /* [dcl.dcl]
17158 Only in function declarations for constructors, destructors, and
17159 type conversions can the decl-specifier-seq be omitted.
17161 We explicitly postpone this check past the point where we handle
17162 function-definitions because we tolerate function-definitions
17163 that are missing their return types in some modes. */
17164 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17166 cp_parser_error (parser,
17167 "expected constructor, destructor, or type conversion");
17168 return error_mark_node;
17171 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17172 if (token->type == CPP_EQ
17173 || token->type == CPP_OPEN_PAREN
17174 || token->type == CPP_OPEN_BRACE)
17176 is_initialized = SD_INITIALIZED;
17177 initialization_kind = token->type;
17178 if (maybe_range_for_decl)
17179 *maybe_range_for_decl = error_mark_node;
17180 tmp_init_loc = token->location;
17181 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17182 *init_loc = tmp_init_loc;
17184 if (token->type == CPP_EQ
17185 && function_declarator_p (declarator))
17187 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17188 if (t2->keyword == RID_DEFAULT)
17189 is_initialized = SD_DEFAULTED;
17190 else if (t2->keyword == RID_DELETE)
17191 is_initialized = SD_DELETED;
17194 else
17196 /* If the init-declarator isn't initialized and isn't followed by a
17197 `,' or `;', it's not a valid init-declarator. */
17198 if (token->type != CPP_COMMA
17199 && token->type != CPP_SEMICOLON)
17201 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17202 range_for_decl_p = true;
17203 else
17205 if (!maybe_range_for_decl)
17206 cp_parser_error (parser, "expected initializer");
17207 return error_mark_node;
17210 is_initialized = SD_UNINITIALIZED;
17211 initialization_kind = CPP_EOF;
17214 /* Because start_decl has side-effects, we should only call it if we
17215 know we're going ahead. By this point, we know that we cannot
17216 possibly be looking at any other construct. */
17217 cp_parser_commit_to_tentative_parse (parser);
17219 /* Enter the newly declared entry in the symbol table. If we're
17220 processing a declaration in a class-specifier, we wait until
17221 after processing the initializer. */
17222 if (!member_p)
17224 if (parser->in_unbraced_linkage_specification_p)
17225 decl_specifiers->storage_class = sc_extern;
17226 decl = start_decl (declarator, decl_specifiers,
17227 range_for_decl_p? SD_INITIALIZED : is_initialized,
17228 attributes, prefix_attributes, &pushed_scope);
17229 cp_finalize_omp_declare_simd (parser, decl);
17230 /* Adjust location of decl if declarator->id_loc is more appropriate:
17231 set, and decl wasn't merged with another decl, in which case its
17232 location would be different from input_location, and more accurate. */
17233 if (DECL_P (decl)
17234 && declarator->id_loc != UNKNOWN_LOCATION
17235 && DECL_SOURCE_LOCATION (decl) == input_location)
17236 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17238 else if (scope)
17239 /* Enter the SCOPE. That way unqualified names appearing in the
17240 initializer will be looked up in SCOPE. */
17241 pushed_scope = push_scope (scope);
17243 /* Perform deferred access control checks, now that we know in which
17244 SCOPE the declared entity resides. */
17245 if (!member_p && decl)
17247 tree saved_current_function_decl = NULL_TREE;
17249 /* If the entity being declared is a function, pretend that we
17250 are in its scope. If it is a `friend', it may have access to
17251 things that would not otherwise be accessible. */
17252 if (TREE_CODE (decl) == FUNCTION_DECL)
17254 saved_current_function_decl = current_function_decl;
17255 current_function_decl = decl;
17258 /* Perform access checks for template parameters. */
17259 cp_parser_perform_template_parameter_access_checks (checks);
17261 /* Perform the access control checks for the declarator and the
17262 decl-specifiers. */
17263 perform_deferred_access_checks (tf_warning_or_error);
17265 /* Restore the saved value. */
17266 if (TREE_CODE (decl) == FUNCTION_DECL)
17267 current_function_decl = saved_current_function_decl;
17270 /* Parse the initializer. */
17271 initializer = NULL_TREE;
17272 is_direct_init = false;
17273 is_non_constant_init = true;
17274 if (is_initialized)
17276 if (function_declarator_p (declarator))
17278 if (initialization_kind == CPP_EQ)
17279 initializer = cp_parser_pure_specifier (parser);
17280 else
17282 /* If the declaration was erroneous, we don't really
17283 know what the user intended, so just silently
17284 consume the initializer. */
17285 if (decl != error_mark_node)
17286 error_at (tmp_init_loc, "initializer provided for function");
17287 cp_parser_skip_to_closing_parenthesis (parser,
17288 /*recovering=*/true,
17289 /*or_comma=*/false,
17290 /*consume_paren=*/true);
17293 else
17295 /* We want to record the extra mangling scope for in-class
17296 initializers of class members and initializers of static data
17297 member templates. The former involves deferring
17298 parsing of the initializer until end of class as with default
17299 arguments. So right here we only handle the latter. */
17300 if (!member_p && processing_template_decl)
17301 start_lambda_scope (decl);
17302 initializer = cp_parser_initializer (parser,
17303 &is_direct_init,
17304 &is_non_constant_init);
17305 if (!member_p && processing_template_decl)
17306 finish_lambda_scope ();
17307 if (initializer == error_mark_node)
17308 cp_parser_skip_to_end_of_statement (parser);
17312 /* The old parser allows attributes to appear after a parenthesized
17313 initializer. Mark Mitchell proposed removing this functionality
17314 on the GCC mailing lists on 2002-08-13. This parser accepts the
17315 attributes -- but ignores them. */
17316 if (cp_parser_allow_gnu_extensions_p (parser)
17317 && initialization_kind == CPP_OPEN_PAREN)
17318 if (cp_parser_attributes_opt (parser))
17319 warning (OPT_Wattributes,
17320 "attributes after parenthesized initializer ignored");
17322 /* And now complain about a non-function implicit template. */
17323 if (bogus_implicit_tmpl)
17324 error_at (DECL_SOURCE_LOCATION (decl),
17325 "non-function %qD declared as implicit template", decl);
17327 /* For an in-class declaration, use `grokfield' to create the
17328 declaration. */
17329 if (member_p)
17331 if (pushed_scope)
17333 pop_scope (pushed_scope);
17334 pushed_scope = NULL_TREE;
17336 decl = grokfield (declarator, decl_specifiers,
17337 initializer, !is_non_constant_init,
17338 /*asmspec=*/NULL_TREE,
17339 chainon (attributes, prefix_attributes));
17340 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17341 cp_parser_save_default_args (parser, decl);
17342 cp_finalize_omp_declare_simd (parser, decl);
17345 /* Finish processing the declaration. But, skip member
17346 declarations. */
17347 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17349 cp_finish_decl (decl,
17350 initializer, !is_non_constant_init,
17351 asm_specification,
17352 /* If the initializer is in parentheses, then this is
17353 a direct-initialization, which means that an
17354 `explicit' constructor is OK. Otherwise, an
17355 `explicit' constructor cannot be used. */
17356 ((is_direct_init || !is_initialized)
17357 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17359 else if ((cxx_dialect != cxx98) && friend_p
17360 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17361 /* Core issue #226 (C++0x only): A default template-argument
17362 shall not be specified in a friend class template
17363 declaration. */
17364 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17365 /*is_partial=*/false, /*is_friend_decl=*/1);
17367 if (!friend_p && pushed_scope)
17368 pop_scope (pushed_scope);
17370 if (function_declarator_p (declarator)
17371 && parser->fully_implicit_function_template_p)
17373 if (member_p)
17374 decl = finish_fully_implicit_template (parser, decl);
17375 else
17376 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17379 return decl;
17382 /* Parse a declarator.
17384 declarator:
17385 direct-declarator
17386 ptr-operator declarator
17388 abstract-declarator:
17389 ptr-operator abstract-declarator [opt]
17390 direct-abstract-declarator
17392 GNU Extensions:
17394 declarator:
17395 attributes [opt] direct-declarator
17396 attributes [opt] ptr-operator declarator
17398 abstract-declarator:
17399 attributes [opt] ptr-operator abstract-declarator [opt]
17400 attributes [opt] direct-abstract-declarator
17402 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17403 detect constructor, destructor or conversion operators. It is set
17404 to -1 if the declarator is a name, and +1 if it is a
17405 function. Otherwise it is set to zero. Usually you just want to
17406 test for >0, but internally the negative value is used.
17408 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17409 a decl-specifier-seq unless it declares a constructor, destructor,
17410 or conversion. It might seem that we could check this condition in
17411 semantic analysis, rather than parsing, but that makes it difficult
17412 to handle something like `f()'. We want to notice that there are
17413 no decl-specifiers, and therefore realize that this is an
17414 expression, not a declaration.)
17416 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17417 the declarator is a direct-declarator of the form "(...)".
17419 MEMBER_P is true iff this declarator is a member-declarator.
17421 FRIEND_P is true iff this declarator is a friend. */
17423 static cp_declarator *
17424 cp_parser_declarator (cp_parser* parser,
17425 cp_parser_declarator_kind dcl_kind,
17426 int* ctor_dtor_or_conv_p,
17427 bool* parenthesized_p,
17428 bool member_p, bool friend_p)
17430 cp_declarator *declarator;
17431 enum tree_code code;
17432 cp_cv_quals cv_quals;
17433 tree class_type;
17434 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17436 /* Assume this is not a constructor, destructor, or type-conversion
17437 operator. */
17438 if (ctor_dtor_or_conv_p)
17439 *ctor_dtor_or_conv_p = 0;
17441 if (cp_parser_allow_gnu_extensions_p (parser))
17442 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17444 /* Check for the ptr-operator production. */
17445 cp_parser_parse_tentatively (parser);
17446 /* Parse the ptr-operator. */
17447 code = cp_parser_ptr_operator (parser,
17448 &class_type,
17449 &cv_quals,
17450 &std_attributes);
17452 /* If that worked, then we have a ptr-operator. */
17453 if (cp_parser_parse_definitely (parser))
17455 /* If a ptr-operator was found, then this declarator was not
17456 parenthesized. */
17457 if (parenthesized_p)
17458 *parenthesized_p = true;
17459 /* The dependent declarator is optional if we are parsing an
17460 abstract-declarator. */
17461 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17462 cp_parser_parse_tentatively (parser);
17464 /* Parse the dependent declarator. */
17465 declarator = cp_parser_declarator (parser, dcl_kind,
17466 /*ctor_dtor_or_conv_p=*/NULL,
17467 /*parenthesized_p=*/NULL,
17468 /*member_p=*/false,
17469 friend_p);
17471 /* If we are parsing an abstract-declarator, we must handle the
17472 case where the dependent declarator is absent. */
17473 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17474 && !cp_parser_parse_definitely (parser))
17475 declarator = NULL;
17477 declarator = cp_parser_make_indirect_declarator
17478 (code, class_type, cv_quals, declarator, std_attributes);
17480 /* Everything else is a direct-declarator. */
17481 else
17483 if (parenthesized_p)
17484 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17485 CPP_OPEN_PAREN);
17486 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17487 ctor_dtor_or_conv_p,
17488 member_p, friend_p);
17491 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17492 declarator->attributes = gnu_attributes;
17493 return declarator;
17496 /* Parse a direct-declarator or direct-abstract-declarator.
17498 direct-declarator:
17499 declarator-id
17500 direct-declarator ( parameter-declaration-clause )
17501 cv-qualifier-seq [opt]
17502 ref-qualifier [opt]
17503 exception-specification [opt]
17504 direct-declarator [ constant-expression [opt] ]
17505 ( declarator )
17507 direct-abstract-declarator:
17508 direct-abstract-declarator [opt]
17509 ( parameter-declaration-clause )
17510 cv-qualifier-seq [opt]
17511 ref-qualifier [opt]
17512 exception-specification [opt]
17513 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17514 ( abstract-declarator )
17516 Returns a representation of the declarator. DCL_KIND is
17517 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17518 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17519 we are parsing a direct-declarator. It is
17520 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17521 of ambiguity we prefer an abstract declarator, as per
17522 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17523 as for cp_parser_declarator. */
17525 static cp_declarator *
17526 cp_parser_direct_declarator (cp_parser* parser,
17527 cp_parser_declarator_kind dcl_kind,
17528 int* ctor_dtor_or_conv_p,
17529 bool member_p, bool friend_p)
17531 cp_token *token;
17532 cp_declarator *declarator = NULL;
17533 tree scope = NULL_TREE;
17534 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17535 bool saved_in_declarator_p = parser->in_declarator_p;
17536 bool first = true;
17537 tree pushed_scope = NULL_TREE;
17539 while (true)
17541 /* Peek at the next token. */
17542 token = cp_lexer_peek_token (parser->lexer);
17543 if (token->type == CPP_OPEN_PAREN)
17545 /* This is either a parameter-declaration-clause, or a
17546 parenthesized declarator. When we know we are parsing a
17547 named declarator, it must be a parenthesized declarator
17548 if FIRST is true. For instance, `(int)' is a
17549 parameter-declaration-clause, with an omitted
17550 direct-abstract-declarator. But `((*))', is a
17551 parenthesized abstract declarator. Finally, when T is a
17552 template parameter `(T)' is a
17553 parameter-declaration-clause, and not a parenthesized
17554 named declarator.
17556 We first try and parse a parameter-declaration-clause,
17557 and then try a nested declarator (if FIRST is true).
17559 It is not an error for it not to be a
17560 parameter-declaration-clause, even when FIRST is
17561 false. Consider,
17563 int i (int);
17564 int i (3);
17566 The first is the declaration of a function while the
17567 second is the definition of a variable, including its
17568 initializer.
17570 Having seen only the parenthesis, we cannot know which of
17571 these two alternatives should be selected. Even more
17572 complex are examples like:
17574 int i (int (a));
17575 int i (int (3));
17577 The former is a function-declaration; the latter is a
17578 variable initialization.
17580 Thus again, we try a parameter-declaration-clause, and if
17581 that fails, we back out and return. */
17583 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17585 tree params;
17586 bool is_declarator = false;
17588 /* In a member-declarator, the only valid interpretation
17589 of a parenthesis is the start of a
17590 parameter-declaration-clause. (It is invalid to
17591 initialize a static data member with a parenthesized
17592 initializer; only the "=" form of initialization is
17593 permitted.) */
17594 if (!member_p)
17595 cp_parser_parse_tentatively (parser);
17597 /* Consume the `('. */
17598 cp_lexer_consume_token (parser->lexer);
17599 if (first)
17601 /* If this is going to be an abstract declarator, we're
17602 in a declarator and we can't have default args. */
17603 parser->default_arg_ok_p = false;
17604 parser->in_declarator_p = true;
17607 begin_scope (sk_function_parms, NULL_TREE);
17609 /* Parse the parameter-declaration-clause. */
17610 params = cp_parser_parameter_declaration_clause (parser);
17612 /* Consume the `)'. */
17613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17615 /* If all went well, parse the cv-qualifier-seq,
17616 ref-qualifier and the exception-specification. */
17617 if (member_p || cp_parser_parse_definitely (parser))
17619 cp_cv_quals cv_quals;
17620 cp_virt_specifiers virt_specifiers;
17621 cp_ref_qualifier ref_qual;
17622 tree exception_specification;
17623 tree late_return;
17624 tree attrs;
17625 bool memfn = (member_p || (pushed_scope
17626 && CLASS_TYPE_P (pushed_scope)));
17628 is_declarator = true;
17630 if (ctor_dtor_or_conv_p)
17631 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17632 first = false;
17634 /* Parse the cv-qualifier-seq. */
17635 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17636 /* Parse the ref-qualifier. */
17637 ref_qual = cp_parser_ref_qualifier_opt (parser);
17638 /* And the exception-specification. */
17639 exception_specification
17640 = cp_parser_exception_specification_opt (parser);
17642 attrs = cp_parser_std_attribute_spec_seq (parser);
17644 /* In here, we handle cases where attribute is used after
17645 the function declaration. For example:
17646 void func (int x) __attribute__((vector(..))); */
17647 if (flag_cilkplus
17648 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17650 cp_parser_parse_tentatively (parser);
17651 tree attr = cp_parser_gnu_attributes_opt (parser);
17652 if (cp_lexer_next_token_is_not (parser->lexer,
17653 CPP_SEMICOLON)
17654 && cp_lexer_next_token_is_not (parser->lexer,
17655 CPP_OPEN_BRACE))
17656 cp_parser_abort_tentative_parse (parser);
17657 else if (!cp_parser_parse_definitely (parser))
17659 else
17660 attrs = chainon (attr, attrs);
17662 late_return = (cp_parser_late_return_type_opt
17663 (parser, declarator,
17664 memfn ? cv_quals : -1));
17667 /* Parse the virt-specifier-seq. */
17668 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17670 /* Create the function-declarator. */
17671 declarator = make_call_declarator (declarator,
17672 params,
17673 cv_quals,
17674 virt_specifiers,
17675 ref_qual,
17676 exception_specification,
17677 late_return);
17678 declarator->std_attributes = attrs;
17679 /* Any subsequent parameter lists are to do with
17680 return type, so are not those of the declared
17681 function. */
17682 parser->default_arg_ok_p = false;
17685 /* Remove the function parms from scope. */
17686 pop_bindings_and_leave_scope ();
17688 if (is_declarator)
17689 /* Repeat the main loop. */
17690 continue;
17693 /* If this is the first, we can try a parenthesized
17694 declarator. */
17695 if (first)
17697 bool saved_in_type_id_in_expr_p;
17699 parser->default_arg_ok_p = saved_default_arg_ok_p;
17700 parser->in_declarator_p = saved_in_declarator_p;
17702 /* Consume the `('. */
17703 cp_lexer_consume_token (parser->lexer);
17704 /* Parse the nested declarator. */
17705 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17706 parser->in_type_id_in_expr_p = true;
17707 declarator
17708 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17709 /*parenthesized_p=*/NULL,
17710 member_p, friend_p);
17711 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17712 first = false;
17713 /* Expect a `)'. */
17714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17715 declarator = cp_error_declarator;
17716 if (declarator == cp_error_declarator)
17717 break;
17719 goto handle_declarator;
17721 /* Otherwise, we must be done. */
17722 else
17723 break;
17725 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17726 && token->type == CPP_OPEN_SQUARE
17727 && !cp_next_tokens_can_be_attribute_p (parser))
17729 /* Parse an array-declarator. */
17730 tree bounds, attrs;
17732 if (ctor_dtor_or_conv_p)
17733 *ctor_dtor_or_conv_p = 0;
17735 first = false;
17736 parser->default_arg_ok_p = false;
17737 parser->in_declarator_p = true;
17738 /* Consume the `['. */
17739 cp_lexer_consume_token (parser->lexer);
17740 /* Peek at the next token. */
17741 token = cp_lexer_peek_token (parser->lexer);
17742 /* If the next token is `]', then there is no
17743 constant-expression. */
17744 if (token->type != CPP_CLOSE_SQUARE)
17746 bool non_constant_p;
17747 bounds
17748 = cp_parser_constant_expression (parser,
17749 /*allow_non_constant=*/true,
17750 &non_constant_p);
17751 if (!non_constant_p)
17752 /* OK */;
17753 else if (error_operand_p (bounds))
17754 /* Already gave an error. */;
17755 else if (!parser->in_function_body
17756 || current_binding_level->kind == sk_function_parms)
17758 /* Normally, the array bound must be an integral constant
17759 expression. However, as an extension, we allow VLAs
17760 in function scopes as long as they aren't part of a
17761 parameter declaration. */
17762 cp_parser_error (parser,
17763 "array bound is not an integer constant");
17764 bounds = error_mark_node;
17766 else if (processing_template_decl
17767 && !type_dependent_expression_p (bounds))
17769 /* Remember this wasn't a constant-expression. */
17770 bounds = build_nop (TREE_TYPE (bounds), bounds);
17771 TREE_SIDE_EFFECTS (bounds) = 1;
17774 else
17775 bounds = NULL_TREE;
17776 /* Look for the closing `]'. */
17777 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17779 declarator = cp_error_declarator;
17780 break;
17783 attrs = cp_parser_std_attribute_spec_seq (parser);
17784 declarator = make_array_declarator (declarator, bounds);
17785 declarator->std_attributes = attrs;
17787 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17790 tree qualifying_scope;
17791 tree unqualified_name;
17792 tree attrs;
17793 special_function_kind sfk;
17794 bool abstract_ok;
17795 bool pack_expansion_p = false;
17796 cp_token *declarator_id_start_token;
17798 /* Parse a declarator-id */
17799 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17800 if (abstract_ok)
17802 cp_parser_parse_tentatively (parser);
17804 /* If we see an ellipsis, we should be looking at a
17805 parameter pack. */
17806 if (token->type == CPP_ELLIPSIS)
17808 /* Consume the `...' */
17809 cp_lexer_consume_token (parser->lexer);
17811 pack_expansion_p = true;
17815 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17816 unqualified_name
17817 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17818 qualifying_scope = parser->scope;
17819 if (abstract_ok)
17821 bool okay = false;
17823 if (!unqualified_name && pack_expansion_p)
17825 /* Check whether an error occurred. */
17826 okay = !cp_parser_error_occurred (parser);
17828 /* We already consumed the ellipsis to mark a
17829 parameter pack, but we have no way to report it,
17830 so abort the tentative parse. We will be exiting
17831 immediately anyway. */
17832 cp_parser_abort_tentative_parse (parser);
17834 else
17835 okay = cp_parser_parse_definitely (parser);
17837 if (!okay)
17838 unqualified_name = error_mark_node;
17839 else if (unqualified_name
17840 && (qualifying_scope
17841 || (!identifier_p (unqualified_name))))
17843 cp_parser_error (parser, "expected unqualified-id");
17844 unqualified_name = error_mark_node;
17848 if (!unqualified_name)
17849 return NULL;
17850 if (unqualified_name == error_mark_node)
17852 declarator = cp_error_declarator;
17853 pack_expansion_p = false;
17854 declarator->parameter_pack_p = false;
17855 break;
17858 attrs = cp_parser_std_attribute_spec_seq (parser);
17860 if (qualifying_scope && at_namespace_scope_p ()
17861 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17863 /* In the declaration of a member of a template class
17864 outside of the class itself, the SCOPE will sometimes
17865 be a TYPENAME_TYPE. For example, given:
17867 template <typename T>
17868 int S<T>::R::i = 3;
17870 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17871 this context, we must resolve S<T>::R to an ordinary
17872 type, rather than a typename type.
17874 The reason we normally avoid resolving TYPENAME_TYPEs
17875 is that a specialization of `S' might render
17876 `S<T>::R' not a type. However, if `S' is
17877 specialized, then this `i' will not be used, so there
17878 is no harm in resolving the types here. */
17879 tree type;
17881 /* Resolve the TYPENAME_TYPE. */
17882 type = resolve_typename_type (qualifying_scope,
17883 /*only_current_p=*/false);
17884 /* If that failed, the declarator is invalid. */
17885 if (TREE_CODE (type) == TYPENAME_TYPE)
17887 if (typedef_variant_p (type))
17888 error_at (declarator_id_start_token->location,
17889 "cannot define member of dependent typedef "
17890 "%qT", type);
17891 else
17892 error_at (declarator_id_start_token->location,
17893 "%<%T::%E%> is not a type",
17894 TYPE_CONTEXT (qualifying_scope),
17895 TYPE_IDENTIFIER (qualifying_scope));
17897 qualifying_scope = type;
17900 sfk = sfk_none;
17902 if (unqualified_name)
17904 tree class_type;
17906 if (qualifying_scope
17907 && CLASS_TYPE_P (qualifying_scope))
17908 class_type = qualifying_scope;
17909 else
17910 class_type = current_class_type;
17912 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17914 tree name_type = TREE_TYPE (unqualified_name);
17915 if (class_type && same_type_p (name_type, class_type))
17917 if (qualifying_scope
17918 && CLASSTYPE_USE_TEMPLATE (name_type))
17920 error_at (declarator_id_start_token->location,
17921 "invalid use of constructor as a template");
17922 inform (declarator_id_start_token->location,
17923 "use %<%T::%D%> instead of %<%T::%D%> to "
17924 "name the constructor in a qualified name",
17925 class_type,
17926 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17927 class_type, name_type);
17928 declarator = cp_error_declarator;
17929 break;
17931 else
17932 unqualified_name = constructor_name (class_type);
17934 else
17936 /* We do not attempt to print the declarator
17937 here because we do not have enough
17938 information about its original syntactic
17939 form. */
17940 cp_parser_error (parser, "invalid declarator");
17941 declarator = cp_error_declarator;
17942 break;
17946 if (class_type)
17948 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17949 sfk = sfk_destructor;
17950 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17951 sfk = sfk_conversion;
17952 else if (/* There's no way to declare a constructor
17953 for an anonymous type, even if the type
17954 got a name for linkage purposes. */
17955 !TYPE_WAS_ANONYMOUS (class_type)
17956 /* Handle correctly (c++/19200):
17958 struct S {
17959 struct T{};
17960 friend void S(T);
17963 and also:
17965 namespace N {
17966 void S();
17969 struct S {
17970 friend void N::S();
17971 }; */
17972 && !(friend_p
17973 && class_type != qualifying_scope)
17974 && constructor_name_p (unqualified_name,
17975 class_type))
17977 unqualified_name = constructor_name (class_type);
17978 sfk = sfk_constructor;
17980 else if (is_overloaded_fn (unqualified_name)
17981 && DECL_CONSTRUCTOR_P (get_first_fn
17982 (unqualified_name)))
17983 sfk = sfk_constructor;
17985 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17986 *ctor_dtor_or_conv_p = -1;
17989 declarator = make_id_declarator (qualifying_scope,
17990 unqualified_name,
17991 sfk);
17992 declarator->std_attributes = attrs;
17993 declarator->id_loc = token->location;
17994 declarator->parameter_pack_p = pack_expansion_p;
17996 if (pack_expansion_p)
17997 maybe_warn_variadic_templates ();
18000 handle_declarator:;
18001 scope = get_scope_of_declarator (declarator);
18002 if (scope)
18004 /* Any names that appear after the declarator-id for a
18005 member are looked up in the containing scope. */
18006 if (at_function_scope_p ())
18008 /* But declarations with qualified-ids can't appear in a
18009 function. */
18010 cp_parser_error (parser, "qualified-id in declaration");
18011 declarator = cp_error_declarator;
18012 break;
18014 pushed_scope = push_scope (scope);
18016 parser->in_declarator_p = true;
18017 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18018 || (declarator && declarator->kind == cdk_id))
18019 /* Default args are only allowed on function
18020 declarations. */
18021 parser->default_arg_ok_p = saved_default_arg_ok_p;
18022 else
18023 parser->default_arg_ok_p = false;
18025 first = false;
18027 /* We're done. */
18028 else
18029 break;
18032 /* For an abstract declarator, we might wind up with nothing at this
18033 point. That's an error; the declarator is not optional. */
18034 if (!declarator)
18035 cp_parser_error (parser, "expected declarator");
18037 /* If we entered a scope, we must exit it now. */
18038 if (pushed_scope)
18039 pop_scope (pushed_scope);
18041 parser->default_arg_ok_p = saved_default_arg_ok_p;
18042 parser->in_declarator_p = saved_in_declarator_p;
18044 return declarator;
18047 /* Parse a ptr-operator.
18049 ptr-operator:
18050 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18051 * cv-qualifier-seq [opt]
18053 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18054 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18056 GNU Extension:
18058 ptr-operator:
18059 & cv-qualifier-seq [opt]
18061 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18062 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18063 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18064 filled in with the TYPE containing the member. *CV_QUALS is
18065 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18066 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18067 Note that the tree codes returned by this function have nothing
18068 to do with the types of trees that will be eventually be created
18069 to represent the pointer or reference type being parsed. They are
18070 just constants with suggestive names. */
18071 static enum tree_code
18072 cp_parser_ptr_operator (cp_parser* parser,
18073 tree* type,
18074 cp_cv_quals *cv_quals,
18075 tree *attributes)
18077 enum tree_code code = ERROR_MARK;
18078 cp_token *token;
18079 tree attrs = NULL_TREE;
18081 /* Assume that it's not a pointer-to-member. */
18082 *type = NULL_TREE;
18083 /* And that there are no cv-qualifiers. */
18084 *cv_quals = TYPE_UNQUALIFIED;
18086 /* Peek at the next token. */
18087 token = cp_lexer_peek_token (parser->lexer);
18089 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18090 if (token->type == CPP_MULT)
18091 code = INDIRECT_REF;
18092 else if (token->type == CPP_AND)
18093 code = ADDR_EXPR;
18094 else if ((cxx_dialect != cxx98) &&
18095 token->type == CPP_AND_AND) /* C++0x only */
18096 code = NON_LVALUE_EXPR;
18098 if (code != ERROR_MARK)
18100 /* Consume the `*', `&' or `&&'. */
18101 cp_lexer_consume_token (parser->lexer);
18103 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18104 `&', if we are allowing GNU extensions. (The only qualifier
18105 that can legally appear after `&' is `restrict', but that is
18106 enforced during semantic analysis. */
18107 if (code == INDIRECT_REF
18108 || cp_parser_allow_gnu_extensions_p (parser))
18109 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18111 attrs = cp_parser_std_attribute_spec_seq (parser);
18112 if (attributes != NULL)
18113 *attributes = attrs;
18115 else
18117 /* Try the pointer-to-member case. */
18118 cp_parser_parse_tentatively (parser);
18119 /* Look for the optional `::' operator. */
18120 cp_parser_global_scope_opt (parser,
18121 /*current_scope_valid_p=*/false);
18122 /* Look for the nested-name specifier. */
18123 token = cp_lexer_peek_token (parser->lexer);
18124 cp_parser_nested_name_specifier (parser,
18125 /*typename_keyword_p=*/false,
18126 /*check_dependency_p=*/true,
18127 /*type_p=*/false,
18128 /*is_declaration=*/false);
18129 /* If we found it, and the next token is a `*', then we are
18130 indeed looking at a pointer-to-member operator. */
18131 if (!cp_parser_error_occurred (parser)
18132 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18134 /* Indicate that the `*' operator was used. */
18135 code = INDIRECT_REF;
18137 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18138 error_at (token->location, "%qD is a namespace", parser->scope);
18139 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18140 error_at (token->location, "cannot form pointer to member of "
18141 "non-class %q#T", parser->scope);
18142 else
18144 /* The type of which the member is a member is given by the
18145 current SCOPE. */
18146 *type = parser->scope;
18147 /* The next name will not be qualified. */
18148 parser->scope = NULL_TREE;
18149 parser->qualifying_scope = NULL_TREE;
18150 parser->object_scope = NULL_TREE;
18151 /* Look for optional c++11 attributes. */
18152 attrs = cp_parser_std_attribute_spec_seq (parser);
18153 if (attributes != NULL)
18154 *attributes = attrs;
18155 /* Look for the optional cv-qualifier-seq. */
18156 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18159 /* If that didn't work we don't have a ptr-operator. */
18160 if (!cp_parser_parse_definitely (parser))
18161 cp_parser_error (parser, "expected ptr-operator");
18164 return code;
18167 /* Parse an (optional) cv-qualifier-seq.
18169 cv-qualifier-seq:
18170 cv-qualifier cv-qualifier-seq [opt]
18172 cv-qualifier:
18173 const
18174 volatile
18176 GNU Extension:
18178 cv-qualifier:
18179 __restrict__
18181 Returns a bitmask representing the cv-qualifiers. */
18183 static cp_cv_quals
18184 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18186 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18188 while (true)
18190 cp_token *token;
18191 cp_cv_quals cv_qualifier;
18193 /* Peek at the next token. */
18194 token = cp_lexer_peek_token (parser->lexer);
18195 /* See if it's a cv-qualifier. */
18196 switch (token->keyword)
18198 case RID_CONST:
18199 cv_qualifier = TYPE_QUAL_CONST;
18200 break;
18202 case RID_VOLATILE:
18203 cv_qualifier = TYPE_QUAL_VOLATILE;
18204 break;
18206 case RID_RESTRICT:
18207 cv_qualifier = TYPE_QUAL_RESTRICT;
18208 break;
18210 default:
18211 cv_qualifier = TYPE_UNQUALIFIED;
18212 break;
18215 if (!cv_qualifier)
18216 break;
18218 if (cv_quals & cv_qualifier)
18220 error_at (token->location, "duplicate cv-qualifier");
18221 cp_lexer_purge_token (parser->lexer);
18223 else
18225 cp_lexer_consume_token (parser->lexer);
18226 cv_quals |= cv_qualifier;
18230 return cv_quals;
18233 /* Parse an (optional) ref-qualifier
18235 ref-qualifier:
18239 Returns cp_ref_qualifier representing ref-qualifier. */
18241 static cp_ref_qualifier
18242 cp_parser_ref_qualifier_opt (cp_parser* parser)
18244 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18246 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18247 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18248 return ref_qual;
18250 while (true)
18252 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18253 cp_token *token = cp_lexer_peek_token (parser->lexer);
18255 switch (token->type)
18257 case CPP_AND:
18258 curr_ref_qual = REF_QUAL_LVALUE;
18259 break;
18261 case CPP_AND_AND:
18262 curr_ref_qual = REF_QUAL_RVALUE;
18263 break;
18265 default:
18266 curr_ref_qual = REF_QUAL_NONE;
18267 break;
18270 if (!curr_ref_qual)
18271 break;
18272 else if (ref_qual)
18274 error_at (token->location, "multiple ref-qualifiers");
18275 cp_lexer_purge_token (parser->lexer);
18277 else
18279 ref_qual = curr_ref_qual;
18280 cp_lexer_consume_token (parser->lexer);
18284 return ref_qual;
18287 /* Parse an (optional) virt-specifier-seq.
18289 virt-specifier-seq:
18290 virt-specifier virt-specifier-seq [opt]
18292 virt-specifier:
18293 override
18294 final
18296 Returns a bitmask representing the virt-specifiers. */
18298 static cp_virt_specifiers
18299 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18301 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18303 while (true)
18305 cp_token *token;
18306 cp_virt_specifiers virt_specifier;
18308 /* Peek at the next token. */
18309 token = cp_lexer_peek_token (parser->lexer);
18310 /* See if it's a virt-specifier-qualifier. */
18311 if (token->type != CPP_NAME)
18312 break;
18313 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18315 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18316 virt_specifier = VIRT_SPEC_OVERRIDE;
18318 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18320 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18321 virt_specifier = VIRT_SPEC_FINAL;
18323 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18325 virt_specifier = VIRT_SPEC_FINAL;
18327 else
18328 break;
18330 if (virt_specifiers & virt_specifier)
18332 error_at (token->location, "duplicate virt-specifier");
18333 cp_lexer_purge_token (parser->lexer);
18335 else
18337 cp_lexer_consume_token (parser->lexer);
18338 virt_specifiers |= virt_specifier;
18341 return virt_specifiers;
18344 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18345 is in scope even though it isn't real. */
18347 void
18348 inject_this_parameter (tree ctype, cp_cv_quals quals)
18350 tree this_parm;
18352 if (current_class_ptr)
18354 /* We don't clear this between NSDMIs. Is it already what we want? */
18355 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18356 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18357 && cp_type_quals (type) == quals)
18358 return;
18361 this_parm = build_this_parm (ctype, quals);
18362 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18363 current_class_ptr = NULL_TREE;
18364 current_class_ref
18365 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18366 current_class_ptr = this_parm;
18369 /* Return true iff our current scope is a non-static data member
18370 initializer. */
18372 bool
18373 parsing_nsdmi (void)
18375 /* We recognize NSDMI context by the context-less 'this' pointer set up
18376 by the function above. */
18377 if (current_class_ptr
18378 && TREE_CODE (current_class_ptr) == PARM_DECL
18379 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18380 return true;
18381 return false;
18384 /* Parse a late-specified return type, if any. This is not a separate
18385 non-terminal, but part of a function declarator, which looks like
18387 -> trailing-type-specifier-seq abstract-declarator(opt)
18389 Returns the type indicated by the type-id.
18391 In addition to this this parses any queued up omp declare simd
18392 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18394 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18395 function. */
18397 static tree
18398 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18399 cp_cv_quals quals)
18401 cp_token *token;
18402 tree type = NULL_TREE;
18403 bool declare_simd_p = (parser->omp_declare_simd
18404 && declarator
18405 && declarator->kind == cdk_id);
18407 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18408 && declarator && declarator->kind == cdk_id);
18410 /* Peek at the next token. */
18411 token = cp_lexer_peek_token (parser->lexer);
18412 /* A late-specified return type is indicated by an initial '->'. */
18413 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18414 return NULL_TREE;
18416 tree save_ccp = current_class_ptr;
18417 tree save_ccr = current_class_ref;
18418 if (quals >= 0)
18420 /* DR 1207: 'this' is in scope in the trailing return type. */
18421 inject_this_parameter (current_class_type, quals);
18424 if (token->type == CPP_DEREF)
18426 /* Consume the ->. */
18427 cp_lexer_consume_token (parser->lexer);
18429 type = cp_parser_trailing_type_id (parser);
18432 if (cilk_simd_fn_vector_p)
18433 declarator->std_attributes
18434 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18435 declarator->std_attributes);
18436 if (declare_simd_p)
18437 declarator->std_attributes
18438 = cp_parser_late_parsing_omp_declare_simd (parser,
18439 declarator->std_attributes);
18441 if (quals >= 0)
18443 current_class_ptr = save_ccp;
18444 current_class_ref = save_ccr;
18447 return type;
18450 /* Parse a declarator-id.
18452 declarator-id:
18453 id-expression
18454 :: [opt] nested-name-specifier [opt] type-name
18456 In the `id-expression' case, the value returned is as for
18457 cp_parser_id_expression if the id-expression was an unqualified-id.
18458 If the id-expression was a qualified-id, then a SCOPE_REF is
18459 returned. The first operand is the scope (either a NAMESPACE_DECL
18460 or TREE_TYPE), but the second is still just a representation of an
18461 unqualified-id. */
18463 static tree
18464 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18466 tree id;
18467 /* The expression must be an id-expression. Assume that qualified
18468 names are the names of types so that:
18470 template <class T>
18471 int S<T>::R::i = 3;
18473 will work; we must treat `S<T>::R' as the name of a type.
18474 Similarly, assume that qualified names are templates, where
18475 required, so that:
18477 template <class T>
18478 int S<T>::R<T>::i = 3;
18480 will work, too. */
18481 id = cp_parser_id_expression (parser,
18482 /*template_keyword_p=*/false,
18483 /*check_dependency_p=*/false,
18484 /*template_p=*/NULL,
18485 /*declarator_p=*/true,
18486 optional_p);
18487 if (id && BASELINK_P (id))
18488 id = BASELINK_FUNCTIONS (id);
18489 return id;
18492 /* Parse a type-id.
18494 type-id:
18495 type-specifier-seq abstract-declarator [opt]
18497 Returns the TYPE specified. */
18499 static tree
18500 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18501 bool is_trailing_return)
18503 cp_decl_specifier_seq type_specifier_seq;
18504 cp_declarator *abstract_declarator;
18506 /* Parse the type-specifier-seq. */
18507 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18508 is_trailing_return,
18509 &type_specifier_seq);
18510 if (type_specifier_seq.type == error_mark_node)
18511 return error_mark_node;
18513 /* There might or might not be an abstract declarator. */
18514 cp_parser_parse_tentatively (parser);
18515 /* Look for the declarator. */
18516 abstract_declarator
18517 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18518 /*parenthesized_p=*/NULL,
18519 /*member_p=*/false,
18520 /*friend_p=*/false);
18521 /* Check to see if there really was a declarator. */
18522 if (!cp_parser_parse_definitely (parser))
18523 abstract_declarator = NULL;
18525 if (type_specifier_seq.type
18526 /* None of the valid uses of 'auto' in C++14 involve the type-id
18527 nonterminal, but it is valid in a trailing-return-type. */
18528 && !(cxx_dialect >= cxx14 && is_trailing_return)
18529 && type_uses_auto (type_specifier_seq.type))
18531 /* A type-id with type 'auto' is only ok if the abstract declarator
18532 is a function declarator with a late-specified return type. */
18533 if (abstract_declarator
18534 && abstract_declarator->kind == cdk_function
18535 && abstract_declarator->u.function.late_return_type)
18536 /* OK */;
18537 else
18539 error ("invalid use of %<auto%>");
18540 return error_mark_node;
18544 return groktypename (&type_specifier_seq, abstract_declarator,
18545 is_template_arg);
18548 static tree cp_parser_type_id (cp_parser *parser)
18550 return cp_parser_type_id_1 (parser, false, false);
18553 static tree cp_parser_template_type_arg (cp_parser *parser)
18555 tree r;
18556 const char *saved_message = parser->type_definition_forbidden_message;
18557 parser->type_definition_forbidden_message
18558 = G_("types may not be defined in template arguments");
18559 r = cp_parser_type_id_1 (parser, true, false);
18560 parser->type_definition_forbidden_message = saved_message;
18561 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18563 error ("invalid use of %<auto%> in template argument");
18564 r = error_mark_node;
18566 return r;
18569 static tree cp_parser_trailing_type_id (cp_parser *parser)
18571 return cp_parser_type_id_1 (parser, false, true);
18574 /* Parse a type-specifier-seq.
18576 type-specifier-seq:
18577 type-specifier type-specifier-seq [opt]
18579 GNU extension:
18581 type-specifier-seq:
18582 attributes type-specifier-seq [opt]
18584 If IS_DECLARATION is true, we are at the start of a "condition" or
18585 exception-declaration, so we might be followed by a declarator-id.
18587 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18588 i.e. we've just seen "->".
18590 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18592 static void
18593 cp_parser_type_specifier_seq (cp_parser* parser,
18594 bool is_declaration,
18595 bool is_trailing_return,
18596 cp_decl_specifier_seq *type_specifier_seq)
18598 bool seen_type_specifier = false;
18599 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18600 cp_token *start_token = NULL;
18602 /* Clear the TYPE_SPECIFIER_SEQ. */
18603 clear_decl_specs (type_specifier_seq);
18605 /* In the context of a trailing return type, enum E { } is an
18606 elaborated-type-specifier followed by a function-body, not an
18607 enum-specifier. */
18608 if (is_trailing_return)
18609 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18611 /* Parse the type-specifiers and attributes. */
18612 while (true)
18614 tree type_specifier;
18615 bool is_cv_qualifier;
18617 /* Check for attributes first. */
18618 if (cp_next_tokens_can_be_attribute_p (parser))
18620 type_specifier_seq->attributes =
18621 chainon (type_specifier_seq->attributes,
18622 cp_parser_attributes_opt (parser));
18623 continue;
18626 /* record the token of the beginning of the type specifier seq,
18627 for error reporting purposes*/
18628 if (!start_token)
18629 start_token = cp_lexer_peek_token (parser->lexer);
18631 /* Look for the type-specifier. */
18632 type_specifier = cp_parser_type_specifier (parser,
18633 flags,
18634 type_specifier_seq,
18635 /*is_declaration=*/false,
18636 NULL,
18637 &is_cv_qualifier);
18638 if (!type_specifier)
18640 /* If the first type-specifier could not be found, this is not a
18641 type-specifier-seq at all. */
18642 if (!seen_type_specifier)
18644 /* Set in_declarator_p to avoid skipping to the semicolon. */
18645 int in_decl = parser->in_declarator_p;
18646 parser->in_declarator_p = true;
18648 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18649 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18650 cp_parser_error (parser, "expected type-specifier");
18652 parser->in_declarator_p = in_decl;
18654 type_specifier_seq->type = error_mark_node;
18655 return;
18657 /* If subsequent type-specifiers could not be found, the
18658 type-specifier-seq is complete. */
18659 break;
18662 seen_type_specifier = true;
18663 /* The standard says that a condition can be:
18665 type-specifier-seq declarator = assignment-expression
18667 However, given:
18669 struct S {};
18670 if (int S = ...)
18672 we should treat the "S" as a declarator, not as a
18673 type-specifier. The standard doesn't say that explicitly for
18674 type-specifier-seq, but it does say that for
18675 decl-specifier-seq in an ordinary declaration. Perhaps it
18676 would be clearer just to allow a decl-specifier-seq here, and
18677 then add a semantic restriction that if any decl-specifiers
18678 that are not type-specifiers appear, the program is invalid. */
18679 if (is_declaration && !is_cv_qualifier)
18680 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18684 /* Return whether the function currently being declared has an associated
18685 template parameter list. */
18687 static bool
18688 function_being_declared_is_template_p (cp_parser* parser)
18690 if (!current_template_parms || processing_template_parmlist)
18691 return false;
18693 if (parser->implicit_template_scope)
18694 return true;
18696 if (at_class_scope_p ()
18697 && TYPE_BEING_DEFINED (current_class_type))
18698 return parser->num_template_parameter_lists != 0;
18700 return ((int) parser->num_template_parameter_lists > template_class_depth
18701 (current_class_type));
18704 /* Parse a parameter-declaration-clause.
18706 parameter-declaration-clause:
18707 parameter-declaration-list [opt] ... [opt]
18708 parameter-declaration-list , ...
18710 Returns a representation for the parameter declarations. A return
18711 value of NULL indicates a parameter-declaration-clause consisting
18712 only of an ellipsis. */
18714 static tree
18715 cp_parser_parameter_declaration_clause (cp_parser* parser)
18717 tree parameters;
18718 cp_token *token;
18719 bool ellipsis_p;
18720 bool is_error;
18722 struct cleanup {
18723 cp_parser* parser;
18724 int auto_is_implicit_function_template_parm_p;
18725 ~cleanup() {
18726 parser->auto_is_implicit_function_template_parm_p
18727 = auto_is_implicit_function_template_parm_p;
18729 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18731 (void) cleanup;
18733 if (!processing_specialization
18734 && !processing_template_parmlist
18735 && !processing_explicit_instantiation)
18736 if (!current_function_decl
18737 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18738 parser->auto_is_implicit_function_template_parm_p = true;
18740 /* Peek at the next token. */
18741 token = cp_lexer_peek_token (parser->lexer);
18742 /* Check for trivial parameter-declaration-clauses. */
18743 if (token->type == CPP_ELLIPSIS)
18745 /* Consume the `...' token. */
18746 cp_lexer_consume_token (parser->lexer);
18747 return NULL_TREE;
18749 else if (token->type == CPP_CLOSE_PAREN)
18750 /* There are no parameters. */
18752 #ifndef NO_IMPLICIT_EXTERN_C
18753 if (in_system_header_at (input_location)
18754 && current_class_type == NULL
18755 && current_lang_name == lang_name_c)
18756 return NULL_TREE;
18757 else
18758 #endif
18759 return void_list_node;
18761 /* Check for `(void)', too, which is a special case. */
18762 else if (token->keyword == RID_VOID
18763 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18764 == CPP_CLOSE_PAREN))
18766 /* Consume the `void' token. */
18767 cp_lexer_consume_token (parser->lexer);
18768 /* There are no parameters. */
18769 return void_list_node;
18772 /* Parse the parameter-declaration-list. */
18773 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18774 /* If a parse error occurred while parsing the
18775 parameter-declaration-list, then the entire
18776 parameter-declaration-clause is erroneous. */
18777 if (is_error)
18778 return NULL;
18780 /* Peek at the next token. */
18781 token = cp_lexer_peek_token (parser->lexer);
18782 /* If it's a `,', the clause should terminate with an ellipsis. */
18783 if (token->type == CPP_COMMA)
18785 /* Consume the `,'. */
18786 cp_lexer_consume_token (parser->lexer);
18787 /* Expect an ellipsis. */
18788 ellipsis_p
18789 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18791 /* It might also be `...' if the optional trailing `,' was
18792 omitted. */
18793 else if (token->type == CPP_ELLIPSIS)
18795 /* Consume the `...' token. */
18796 cp_lexer_consume_token (parser->lexer);
18797 /* And remember that we saw it. */
18798 ellipsis_p = true;
18800 else
18801 ellipsis_p = false;
18803 /* Finish the parameter list. */
18804 if (!ellipsis_p)
18805 parameters = chainon (parameters, void_list_node);
18807 return parameters;
18810 /* Parse a parameter-declaration-list.
18812 parameter-declaration-list:
18813 parameter-declaration
18814 parameter-declaration-list , parameter-declaration
18816 Returns a representation of the parameter-declaration-list, as for
18817 cp_parser_parameter_declaration_clause. However, the
18818 `void_list_node' is never appended to the list. Upon return,
18819 *IS_ERROR will be true iff an error occurred. */
18821 static tree
18822 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18824 tree parameters = NULL_TREE;
18825 tree *tail = &parameters;
18826 bool saved_in_unbraced_linkage_specification_p;
18827 int index = 0;
18829 /* Assume all will go well. */
18830 *is_error = false;
18831 /* The special considerations that apply to a function within an
18832 unbraced linkage specifications do not apply to the parameters
18833 to the function. */
18834 saved_in_unbraced_linkage_specification_p
18835 = parser->in_unbraced_linkage_specification_p;
18836 parser->in_unbraced_linkage_specification_p = false;
18838 /* Look for more parameters. */
18839 while (true)
18841 cp_parameter_declarator *parameter;
18842 tree decl = error_mark_node;
18843 bool parenthesized_p = false;
18844 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18845 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18846 (current_template_parms)) : 0);
18848 /* Parse the parameter. */
18849 parameter
18850 = cp_parser_parameter_declaration (parser,
18851 /*template_parm_p=*/false,
18852 &parenthesized_p);
18854 /* We don't know yet if the enclosing context is deprecated, so wait
18855 and warn in grokparms if appropriate. */
18856 deprecated_state = DEPRECATED_SUPPRESS;
18858 if (parameter)
18860 /* If a function parameter pack was specified and an implicit template
18861 parameter was introduced during cp_parser_parameter_declaration,
18862 change any implicit parameters introduced into packs. */
18863 if (parser->implicit_template_parms
18864 && parameter->declarator
18865 && parameter->declarator->parameter_pack_p)
18867 int latest_template_parm_idx = TREE_VEC_LENGTH
18868 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18870 if (latest_template_parm_idx != template_parm_idx)
18871 parameter->decl_specifiers.type = convert_generic_types_to_packs
18872 (parameter->decl_specifiers.type,
18873 template_parm_idx, latest_template_parm_idx);
18876 decl = grokdeclarator (parameter->declarator,
18877 &parameter->decl_specifiers,
18878 PARM,
18879 parameter->default_argument != NULL_TREE,
18880 &parameter->decl_specifiers.attributes);
18883 deprecated_state = DEPRECATED_NORMAL;
18885 /* If a parse error occurred parsing the parameter declaration,
18886 then the entire parameter-declaration-list is erroneous. */
18887 if (decl == error_mark_node)
18889 *is_error = true;
18890 parameters = error_mark_node;
18891 break;
18894 if (parameter->decl_specifiers.attributes)
18895 cplus_decl_attributes (&decl,
18896 parameter->decl_specifiers.attributes,
18898 if (DECL_NAME (decl))
18899 decl = pushdecl (decl);
18901 if (decl != error_mark_node)
18903 retrofit_lang_decl (decl);
18904 DECL_PARM_INDEX (decl) = ++index;
18905 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18908 /* Add the new parameter to the list. */
18909 *tail = build_tree_list (parameter->default_argument, decl);
18910 tail = &TREE_CHAIN (*tail);
18912 /* Peek at the next token. */
18913 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18914 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18915 /* These are for Objective-C++ */
18916 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18917 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18918 /* The parameter-declaration-list is complete. */
18919 break;
18920 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18922 cp_token *token;
18924 /* Peek at the next token. */
18925 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18926 /* If it's an ellipsis, then the list is complete. */
18927 if (token->type == CPP_ELLIPSIS)
18928 break;
18929 /* Otherwise, there must be more parameters. Consume the
18930 `,'. */
18931 cp_lexer_consume_token (parser->lexer);
18932 /* When parsing something like:
18934 int i(float f, double d)
18936 we can tell after seeing the declaration for "f" that we
18937 are not looking at an initialization of a variable "i",
18938 but rather at the declaration of a function "i".
18940 Due to the fact that the parsing of template arguments
18941 (as specified to a template-id) requires backtracking we
18942 cannot use this technique when inside a template argument
18943 list. */
18944 if (!parser->in_template_argument_list_p
18945 && !parser->in_type_id_in_expr_p
18946 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18947 /* However, a parameter-declaration of the form
18948 "float(f)" (which is a valid declaration of a
18949 parameter "f") can also be interpreted as an
18950 expression (the conversion of "f" to "float"). */
18951 && !parenthesized_p)
18952 cp_parser_commit_to_tentative_parse (parser);
18954 else
18956 cp_parser_error (parser, "expected %<,%> or %<...%>");
18957 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18958 cp_parser_skip_to_closing_parenthesis (parser,
18959 /*recovering=*/true,
18960 /*or_comma=*/false,
18961 /*consume_paren=*/false);
18962 break;
18966 parser->in_unbraced_linkage_specification_p
18967 = saved_in_unbraced_linkage_specification_p;
18969 /* Reset implicit_template_scope if we are about to leave the function
18970 parameter list that introduced it. Note that for out-of-line member
18971 definitions, there will be one or more class scopes before we get to
18972 the template parameter scope. */
18974 if (cp_binding_level *its = parser->implicit_template_scope)
18975 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18977 while (maybe_its->kind == sk_class)
18978 maybe_its = maybe_its->level_chain;
18979 if (maybe_its == its)
18981 parser->implicit_template_parms = 0;
18982 parser->implicit_template_scope = 0;
18986 return parameters;
18989 /* Parse a parameter declaration.
18991 parameter-declaration:
18992 decl-specifier-seq ... [opt] declarator
18993 decl-specifier-seq declarator = assignment-expression
18994 decl-specifier-seq ... [opt] abstract-declarator [opt]
18995 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18997 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18998 declares a template parameter. (In that case, a non-nested `>'
18999 token encountered during the parsing of the assignment-expression
19000 is not interpreted as a greater-than operator.)
19002 Returns a representation of the parameter, or NULL if an error
19003 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19004 true iff the declarator is of the form "(p)". */
19006 static cp_parameter_declarator *
19007 cp_parser_parameter_declaration (cp_parser *parser,
19008 bool template_parm_p,
19009 bool *parenthesized_p)
19011 int declares_class_or_enum;
19012 cp_decl_specifier_seq decl_specifiers;
19013 cp_declarator *declarator;
19014 tree default_argument;
19015 cp_token *token = NULL, *declarator_token_start = NULL;
19016 const char *saved_message;
19018 /* In a template parameter, `>' is not an operator.
19020 [temp.param]
19022 When parsing a default template-argument for a non-type
19023 template-parameter, the first non-nested `>' is taken as the end
19024 of the template parameter-list rather than a greater-than
19025 operator. */
19027 /* Type definitions may not appear in parameter types. */
19028 saved_message = parser->type_definition_forbidden_message;
19029 parser->type_definition_forbidden_message
19030 = G_("types may not be defined in parameter types");
19032 /* Parse the declaration-specifiers. */
19033 cp_parser_decl_specifier_seq (parser,
19034 CP_PARSER_FLAGS_NONE,
19035 &decl_specifiers,
19036 &declares_class_or_enum);
19038 /* Complain about missing 'typename' or other invalid type names. */
19039 if (!decl_specifiers.any_type_specifiers_p
19040 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19041 decl_specifiers.type = error_mark_node;
19043 /* If an error occurred, there's no reason to attempt to parse the
19044 rest of the declaration. */
19045 if (cp_parser_error_occurred (parser))
19047 parser->type_definition_forbidden_message = saved_message;
19048 return NULL;
19051 /* Peek at the next token. */
19052 token = cp_lexer_peek_token (parser->lexer);
19054 /* If the next token is a `)', `,', `=', `>', or `...', then there
19055 is no declarator. However, when variadic templates are enabled,
19056 there may be a declarator following `...'. */
19057 if (token->type == CPP_CLOSE_PAREN
19058 || token->type == CPP_COMMA
19059 || token->type == CPP_EQ
19060 || token->type == CPP_GREATER)
19062 declarator = NULL;
19063 if (parenthesized_p)
19064 *parenthesized_p = false;
19066 /* Otherwise, there should be a declarator. */
19067 else
19069 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19070 parser->default_arg_ok_p = false;
19072 /* After seeing a decl-specifier-seq, if the next token is not a
19073 "(", there is no possibility that the code is a valid
19074 expression. Therefore, if parsing tentatively, we commit at
19075 this point. */
19076 if (!parser->in_template_argument_list_p
19077 /* In an expression context, having seen:
19079 (int((char ...
19081 we cannot be sure whether we are looking at a
19082 function-type (taking a "char" as a parameter) or a cast
19083 of some object of type "char" to "int". */
19084 && !parser->in_type_id_in_expr_p
19085 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19086 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19087 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19088 cp_parser_commit_to_tentative_parse (parser);
19089 /* Parse the declarator. */
19090 declarator_token_start = token;
19091 declarator = cp_parser_declarator (parser,
19092 CP_PARSER_DECLARATOR_EITHER,
19093 /*ctor_dtor_or_conv_p=*/NULL,
19094 parenthesized_p,
19095 /*member_p=*/false,
19096 /*friend_p=*/false);
19097 parser->default_arg_ok_p = saved_default_arg_ok_p;
19098 /* After the declarator, allow more attributes. */
19099 decl_specifiers.attributes
19100 = chainon (decl_specifiers.attributes,
19101 cp_parser_attributes_opt (parser));
19104 /* If the next token is an ellipsis, and we have not seen a
19105 declarator name, and the type of the declarator contains parameter
19106 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19107 a parameter pack expansion expression. Otherwise, leave the
19108 ellipsis for a C-style variadic function. */
19109 token = cp_lexer_peek_token (parser->lexer);
19110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19112 tree type = decl_specifiers.type;
19114 if (type && DECL_P (type))
19115 type = TREE_TYPE (type);
19117 if (type
19118 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19119 && declarator_can_be_parameter_pack (declarator)
19120 && (!declarator || !declarator->parameter_pack_p)
19121 && uses_parameter_packs (type))
19123 /* Consume the `...'. */
19124 cp_lexer_consume_token (parser->lexer);
19125 maybe_warn_variadic_templates ();
19127 /* Build a pack expansion type */
19128 if (declarator)
19129 declarator->parameter_pack_p = true;
19130 else
19131 decl_specifiers.type = make_pack_expansion (type);
19135 /* The restriction on defining new types applies only to the type
19136 of the parameter, not to the default argument. */
19137 parser->type_definition_forbidden_message = saved_message;
19139 /* If the next token is `=', then process a default argument. */
19140 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19142 token = cp_lexer_peek_token (parser->lexer);
19143 /* If we are defining a class, then the tokens that make up the
19144 default argument must be saved and processed later. */
19145 if (!template_parm_p && at_class_scope_p ()
19146 && TYPE_BEING_DEFINED (current_class_type)
19147 && !LAMBDA_TYPE_P (current_class_type))
19148 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19149 /* Outside of a class definition, we can just parse the
19150 assignment-expression. */
19151 else
19152 default_argument
19153 = cp_parser_default_argument (parser, template_parm_p);
19155 if (!parser->default_arg_ok_p)
19157 if (flag_permissive)
19158 warning (0, "deprecated use of default argument for parameter of non-function");
19159 else
19161 error_at (token->location,
19162 "default arguments are only "
19163 "permitted for function parameters");
19164 default_argument = NULL_TREE;
19167 else if ((declarator && declarator->parameter_pack_p)
19168 || (decl_specifiers.type
19169 && PACK_EXPANSION_P (decl_specifiers.type)))
19171 /* Find the name of the parameter pack. */
19172 cp_declarator *id_declarator = declarator;
19173 while (id_declarator && id_declarator->kind != cdk_id)
19174 id_declarator = id_declarator->declarator;
19176 if (id_declarator && id_declarator->kind == cdk_id)
19177 error_at (declarator_token_start->location,
19178 template_parm_p
19179 ? G_("template parameter pack %qD "
19180 "cannot have a default argument")
19181 : G_("parameter pack %qD cannot have "
19182 "a default argument"),
19183 id_declarator->u.id.unqualified_name);
19184 else
19185 error_at (declarator_token_start->location,
19186 template_parm_p
19187 ? G_("template parameter pack cannot have "
19188 "a default argument")
19189 : G_("parameter pack cannot have a "
19190 "default argument"));
19192 default_argument = NULL_TREE;
19195 else
19196 default_argument = NULL_TREE;
19198 return make_parameter_declarator (&decl_specifiers,
19199 declarator,
19200 default_argument);
19203 /* Parse a default argument and return it.
19205 TEMPLATE_PARM_P is true if this is a default argument for a
19206 non-type template parameter. */
19207 static tree
19208 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19210 tree default_argument = NULL_TREE;
19211 bool saved_greater_than_is_operator_p;
19212 bool saved_local_variables_forbidden_p;
19213 bool non_constant_p, is_direct_init;
19215 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19216 set correctly. */
19217 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19218 parser->greater_than_is_operator_p = !template_parm_p;
19219 /* Local variable names (and the `this' keyword) may not
19220 appear in a default argument. */
19221 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19222 parser->local_variables_forbidden_p = true;
19223 /* Parse the assignment-expression. */
19224 if (template_parm_p)
19225 push_deferring_access_checks (dk_no_deferred);
19226 tree saved_class_ptr = NULL_TREE;
19227 tree saved_class_ref = NULL_TREE;
19228 /* The "this" pointer is not valid in a default argument. */
19229 if (cfun)
19231 saved_class_ptr = current_class_ptr;
19232 cp_function_chain->x_current_class_ptr = NULL_TREE;
19233 saved_class_ref = current_class_ref;
19234 cp_function_chain->x_current_class_ref = NULL_TREE;
19236 default_argument
19237 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19238 /* Restore the "this" pointer. */
19239 if (cfun)
19241 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19242 cp_function_chain->x_current_class_ref = saved_class_ref;
19244 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19246 if (template_parm_p)
19247 pop_deferring_access_checks ();
19248 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19249 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19251 return default_argument;
19254 /* Parse a function-body.
19256 function-body:
19257 compound_statement */
19259 static void
19260 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19262 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19265 /* Parse a ctor-initializer-opt followed by a function-body. Return
19266 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19267 is true we are parsing a function-try-block. */
19269 static bool
19270 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19271 bool in_function_try_block)
19273 tree body, list;
19274 bool ctor_initializer_p;
19275 const bool check_body_p =
19276 DECL_CONSTRUCTOR_P (current_function_decl)
19277 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19278 tree last = NULL;
19280 /* Begin the function body. */
19281 body = begin_function_body ();
19282 /* Parse the optional ctor-initializer. */
19283 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19285 /* If we're parsing a constexpr constructor definition, we need
19286 to check that the constructor body is indeed empty. However,
19287 before we get to cp_parser_function_body lot of junk has been
19288 generated, so we can't just check that we have an empty block.
19289 Rather we take a snapshot of the outermost block, and check whether
19290 cp_parser_function_body changed its state. */
19291 if (check_body_p)
19293 list = cur_stmt_list;
19294 if (STATEMENT_LIST_TAIL (list))
19295 last = STATEMENT_LIST_TAIL (list)->stmt;
19297 /* Parse the function-body. */
19298 cp_parser_function_body (parser, in_function_try_block);
19299 if (check_body_p)
19300 check_constexpr_ctor_body (last, list, /*complain=*/true);
19301 /* Finish the function body. */
19302 finish_function_body (body);
19304 return ctor_initializer_p;
19307 /* Parse an initializer.
19309 initializer:
19310 = initializer-clause
19311 ( expression-list )
19313 Returns an expression representing the initializer. If no
19314 initializer is present, NULL_TREE is returned.
19316 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19317 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19318 set to TRUE if there is no initializer present. If there is an
19319 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19320 is set to true; otherwise it is set to false. */
19322 static tree
19323 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19324 bool* non_constant_p)
19326 cp_token *token;
19327 tree init;
19329 /* Peek at the next token. */
19330 token = cp_lexer_peek_token (parser->lexer);
19332 /* Let our caller know whether or not this initializer was
19333 parenthesized. */
19334 *is_direct_init = (token->type != CPP_EQ);
19335 /* Assume that the initializer is constant. */
19336 *non_constant_p = false;
19338 if (token->type == CPP_EQ)
19340 /* Consume the `='. */
19341 cp_lexer_consume_token (parser->lexer);
19342 /* Parse the initializer-clause. */
19343 init = cp_parser_initializer_clause (parser, non_constant_p);
19345 else if (token->type == CPP_OPEN_PAREN)
19347 vec<tree, va_gc> *vec;
19348 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19349 /*cast_p=*/false,
19350 /*allow_expansion_p=*/true,
19351 non_constant_p);
19352 if (vec == NULL)
19353 return error_mark_node;
19354 init = build_tree_list_vec (vec);
19355 release_tree_vector (vec);
19357 else if (token->type == CPP_OPEN_BRACE)
19359 cp_lexer_set_source_position (parser->lexer);
19360 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19361 init = cp_parser_braced_list (parser, non_constant_p);
19362 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19364 else
19366 /* Anything else is an error. */
19367 cp_parser_error (parser, "expected initializer");
19368 init = error_mark_node;
19371 return init;
19374 /* Parse an initializer-clause.
19376 initializer-clause:
19377 assignment-expression
19378 braced-init-list
19380 Returns an expression representing the initializer.
19382 If the `assignment-expression' production is used the value
19383 returned is simply a representation for the expression.
19385 Otherwise, calls cp_parser_braced_list. */
19387 static tree
19388 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19390 tree initializer;
19392 /* Assume the expression is constant. */
19393 *non_constant_p = false;
19395 /* If it is not a `{', then we are looking at an
19396 assignment-expression. */
19397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19399 initializer
19400 = cp_parser_constant_expression (parser,
19401 /*allow_non_constant_p=*/true,
19402 non_constant_p);
19404 else
19405 initializer = cp_parser_braced_list (parser, non_constant_p);
19407 return initializer;
19410 /* Parse a brace-enclosed initializer list.
19412 braced-init-list:
19413 { initializer-list , [opt] }
19416 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19417 the elements of the initializer-list (or NULL, if the last
19418 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19419 NULL_TREE. There is no way to detect whether or not the optional
19420 trailing `,' was provided. NON_CONSTANT_P is as for
19421 cp_parser_initializer. */
19423 static tree
19424 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19426 tree initializer;
19428 /* Consume the `{' token. */
19429 cp_lexer_consume_token (parser->lexer);
19430 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19431 initializer = make_node (CONSTRUCTOR);
19432 /* If it's not a `}', then there is a non-trivial initializer. */
19433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19435 /* Parse the initializer list. */
19436 CONSTRUCTOR_ELTS (initializer)
19437 = cp_parser_initializer_list (parser, non_constant_p);
19438 /* A trailing `,' token is allowed. */
19439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19440 cp_lexer_consume_token (parser->lexer);
19442 else
19443 *non_constant_p = false;
19444 /* Now, there should be a trailing `}'. */
19445 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19446 TREE_TYPE (initializer) = init_list_type_node;
19447 return initializer;
19450 /* Consume tokens up to, and including, the next non-nested closing `]'.
19451 Returns true iff we found a closing `]'. */
19453 static bool
19454 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19456 unsigned square_depth = 0;
19458 while (true)
19460 cp_token * token = cp_lexer_peek_token (parser->lexer);
19462 switch (token->type)
19464 case CPP_EOF:
19465 case CPP_PRAGMA_EOL:
19466 /* If we've run out of tokens, then there is no closing `]'. */
19467 return false;
19469 case CPP_OPEN_SQUARE:
19470 ++square_depth;
19471 break;
19473 case CPP_CLOSE_SQUARE:
19474 if (!square_depth--)
19476 cp_lexer_consume_token (parser->lexer);
19477 return true;
19479 break;
19481 default:
19482 break;
19485 /* Consume the token. */
19486 cp_lexer_consume_token (parser->lexer);
19490 /* Return true if we are looking at an array-designator, false otherwise. */
19492 static bool
19493 cp_parser_array_designator_p (cp_parser *parser)
19495 /* Consume the `['. */
19496 cp_lexer_consume_token (parser->lexer);
19498 cp_lexer_save_tokens (parser->lexer);
19500 /* Skip tokens until the next token is a closing square bracket.
19501 If we find the closing `]', and the next token is a `=', then
19502 we are looking at an array designator. */
19503 bool array_designator_p
19504 = (cp_parser_skip_to_closing_square_bracket (parser)
19505 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19507 /* Roll back the tokens we skipped. */
19508 cp_lexer_rollback_tokens (parser->lexer);
19510 return array_designator_p;
19513 /* Parse an initializer-list.
19515 initializer-list:
19516 initializer-clause ... [opt]
19517 initializer-list , initializer-clause ... [opt]
19519 GNU Extension:
19521 initializer-list:
19522 designation initializer-clause ...[opt]
19523 initializer-list , designation initializer-clause ...[opt]
19525 designation:
19526 . identifier =
19527 identifier :
19528 [ constant-expression ] =
19530 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19531 for the initializer. If the INDEX of the elt is non-NULL, it is the
19532 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19533 as for cp_parser_initializer. */
19535 static vec<constructor_elt, va_gc> *
19536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19538 vec<constructor_elt, va_gc> *v = NULL;
19540 /* Assume all of the expressions are constant. */
19541 *non_constant_p = false;
19543 /* Parse the rest of the list. */
19544 while (true)
19546 cp_token *token;
19547 tree designator;
19548 tree initializer;
19549 bool clause_non_constant_p;
19551 /* If the next token is an identifier and the following one is a
19552 colon, we are looking at the GNU designated-initializer
19553 syntax. */
19554 if (cp_parser_allow_gnu_extensions_p (parser)
19555 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19558 /* Warn the user that they are using an extension. */
19559 pedwarn (input_location, OPT_Wpedantic,
19560 "ISO C++ does not allow designated initializers");
19561 /* Consume the identifier. */
19562 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19563 /* Consume the `:'. */
19564 cp_lexer_consume_token (parser->lexer);
19566 /* Also handle the C99 syntax, '. id ='. */
19567 else if (cp_parser_allow_gnu_extensions_p (parser)
19568 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19569 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19570 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19572 /* Warn the user that they are using an extension. */
19573 pedwarn (input_location, OPT_Wpedantic,
19574 "ISO C++ does not allow C99 designated initializers");
19575 /* Consume the `.'. */
19576 cp_lexer_consume_token (parser->lexer);
19577 /* Consume the identifier. */
19578 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19579 /* Consume the `='. */
19580 cp_lexer_consume_token (parser->lexer);
19582 /* Also handle C99 array designators, '[ const ] ='. */
19583 else if (cp_parser_allow_gnu_extensions_p (parser)
19584 && !c_dialect_objc ()
19585 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19587 /* In C++11, [ could start a lambda-introducer. */
19588 bool non_const = false;
19590 cp_parser_parse_tentatively (parser);
19592 if (!cp_parser_array_designator_p (parser))
19594 cp_parser_simulate_error (parser);
19595 designator = NULL_TREE;
19597 else
19599 designator = cp_parser_constant_expression (parser, true,
19600 &non_const);
19601 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19602 cp_parser_require (parser, CPP_EQ, RT_EQ);
19605 if (!cp_parser_parse_definitely (parser))
19606 designator = NULL_TREE;
19607 else if (non_const)
19608 require_potential_rvalue_constant_expression (designator);
19610 else
19611 designator = NULL_TREE;
19613 /* Parse the initializer. */
19614 initializer = cp_parser_initializer_clause (parser,
19615 &clause_non_constant_p);
19616 /* If any clause is non-constant, so is the entire initializer. */
19617 if (clause_non_constant_p)
19618 *non_constant_p = true;
19620 /* If we have an ellipsis, this is an initializer pack
19621 expansion. */
19622 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19624 /* Consume the `...'. */
19625 cp_lexer_consume_token (parser->lexer);
19627 /* Turn the initializer into an initializer expansion. */
19628 initializer = make_pack_expansion (initializer);
19631 /* Add it to the vector. */
19632 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19634 /* If the next token is not a comma, we have reached the end of
19635 the list. */
19636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19637 break;
19639 /* Peek at the next token. */
19640 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19641 /* If the next token is a `}', then we're still done. An
19642 initializer-clause can have a trailing `,' after the
19643 initializer-list and before the closing `}'. */
19644 if (token->type == CPP_CLOSE_BRACE)
19645 break;
19647 /* Consume the `,' token. */
19648 cp_lexer_consume_token (parser->lexer);
19651 return v;
19654 /* Classes [gram.class] */
19656 /* Parse a class-name.
19658 class-name:
19659 identifier
19660 template-id
19662 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19663 to indicate that names looked up in dependent types should be
19664 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19665 keyword has been used to indicate that the name that appears next
19666 is a template. TAG_TYPE indicates the explicit tag given before
19667 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19668 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19669 is the class being defined in a class-head. If ENUM_OK is TRUE,
19670 enum-names are also accepted.
19672 Returns the TYPE_DECL representing the class. */
19674 static tree
19675 cp_parser_class_name (cp_parser *parser,
19676 bool typename_keyword_p,
19677 bool template_keyword_p,
19678 enum tag_types tag_type,
19679 bool check_dependency_p,
19680 bool class_head_p,
19681 bool is_declaration,
19682 bool enum_ok)
19684 tree decl;
19685 tree scope;
19686 bool typename_p;
19687 cp_token *token;
19688 tree identifier = NULL_TREE;
19690 /* All class-names start with an identifier. */
19691 token = cp_lexer_peek_token (parser->lexer);
19692 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19694 cp_parser_error (parser, "expected class-name");
19695 return error_mark_node;
19698 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19699 to a template-id, so we save it here. */
19700 scope = parser->scope;
19701 if (scope == error_mark_node)
19702 return error_mark_node;
19704 /* Any name names a type if we're following the `typename' keyword
19705 in a qualified name where the enclosing scope is type-dependent. */
19706 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19707 && dependent_type_p (scope));
19708 /* Handle the common case (an identifier, but not a template-id)
19709 efficiently. */
19710 if (token->type == CPP_NAME
19711 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19713 cp_token *identifier_token;
19714 bool ambiguous_p;
19716 /* Look for the identifier. */
19717 identifier_token = cp_lexer_peek_token (parser->lexer);
19718 ambiguous_p = identifier_token->error_reported;
19719 identifier = cp_parser_identifier (parser);
19720 /* If the next token isn't an identifier, we are certainly not
19721 looking at a class-name. */
19722 if (identifier == error_mark_node)
19723 decl = error_mark_node;
19724 /* If we know this is a type-name, there's no need to look it
19725 up. */
19726 else if (typename_p)
19727 decl = identifier;
19728 else
19730 tree ambiguous_decls;
19731 /* If we already know that this lookup is ambiguous, then
19732 we've already issued an error message; there's no reason
19733 to check again. */
19734 if (ambiguous_p)
19736 cp_parser_simulate_error (parser);
19737 return error_mark_node;
19739 /* If the next token is a `::', then the name must be a type
19740 name.
19742 [basic.lookup.qual]
19744 During the lookup for a name preceding the :: scope
19745 resolution operator, object, function, and enumerator
19746 names are ignored. */
19747 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19748 tag_type = typename_type;
19749 /* Look up the name. */
19750 decl = cp_parser_lookup_name (parser, identifier,
19751 tag_type,
19752 /*is_template=*/false,
19753 /*is_namespace=*/false,
19754 check_dependency_p,
19755 &ambiguous_decls,
19756 identifier_token->location);
19757 if (ambiguous_decls)
19759 if (cp_parser_parsing_tentatively (parser))
19760 cp_parser_simulate_error (parser);
19761 return error_mark_node;
19765 else
19767 /* Try a template-id. */
19768 decl = cp_parser_template_id (parser, template_keyword_p,
19769 check_dependency_p,
19770 tag_type,
19771 is_declaration);
19772 if (decl == error_mark_node)
19773 return error_mark_node;
19776 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19778 /* If this is a typename, create a TYPENAME_TYPE. */
19779 if (typename_p && decl != error_mark_node)
19781 decl = make_typename_type (scope, decl, typename_type,
19782 /*complain=*/tf_error);
19783 if (decl != error_mark_node)
19784 decl = TYPE_NAME (decl);
19787 decl = strip_using_decl (decl);
19789 /* Check to see that it is really the name of a class. */
19790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19791 && identifier_p (TREE_OPERAND (decl, 0))
19792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19793 /* Situations like this:
19795 template <typename T> struct A {
19796 typename T::template X<int>::I i;
19799 are problematic. Is `T::template X<int>' a class-name? The
19800 standard does not seem to be definitive, but there is no other
19801 valid interpretation of the following `::'. Therefore, those
19802 names are considered class-names. */
19804 decl = make_typename_type (scope, decl, tag_type, tf_error);
19805 if (decl != error_mark_node)
19806 decl = TYPE_NAME (decl);
19808 else if (TREE_CODE (decl) != TYPE_DECL
19809 || TREE_TYPE (decl) == error_mark_node
19810 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19811 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19812 /* In Objective-C 2.0, a classname followed by '.' starts a
19813 dot-syntax expression, and it's not a type-name. */
19814 || (c_dialect_objc ()
19815 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19816 && objc_is_class_name (decl)))
19817 decl = error_mark_node;
19819 if (decl == error_mark_node)
19820 cp_parser_error (parser, "expected class-name");
19821 else if (identifier && !parser->scope)
19822 maybe_note_name_used_in_class (identifier, decl);
19824 return decl;
19827 /* Parse a class-specifier.
19829 class-specifier:
19830 class-head { member-specification [opt] }
19832 Returns the TREE_TYPE representing the class. */
19834 static tree
19835 cp_parser_class_specifier_1 (cp_parser* parser)
19837 tree type;
19838 tree attributes = NULL_TREE;
19839 bool nested_name_specifier_p;
19840 unsigned saved_num_template_parameter_lists;
19841 bool saved_in_function_body;
19842 unsigned char in_statement;
19843 bool in_switch_statement_p;
19844 bool saved_in_unbraced_linkage_specification_p;
19845 tree old_scope = NULL_TREE;
19846 tree scope = NULL_TREE;
19847 cp_token *closing_brace;
19849 push_deferring_access_checks (dk_no_deferred);
19851 /* Parse the class-head. */
19852 type = cp_parser_class_head (parser,
19853 &nested_name_specifier_p);
19854 /* If the class-head was a semantic disaster, skip the entire body
19855 of the class. */
19856 if (!type)
19858 cp_parser_skip_to_end_of_block_or_statement (parser);
19859 pop_deferring_access_checks ();
19860 return error_mark_node;
19863 /* Look for the `{'. */
19864 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19866 pop_deferring_access_checks ();
19867 return error_mark_node;
19870 cp_ensure_no_omp_declare_simd (parser);
19872 /* Issue an error message if type-definitions are forbidden here. */
19873 cp_parser_check_type_definition (parser);
19874 /* Remember that we are defining one more class. */
19875 ++parser->num_classes_being_defined;
19876 /* Inside the class, surrounding template-parameter-lists do not
19877 apply. */
19878 saved_num_template_parameter_lists
19879 = parser->num_template_parameter_lists;
19880 parser->num_template_parameter_lists = 0;
19881 /* We are not in a function body. */
19882 saved_in_function_body = parser->in_function_body;
19883 parser->in_function_body = false;
19884 /* Or in a loop. */
19885 in_statement = parser->in_statement;
19886 parser->in_statement = 0;
19887 /* Or in a switch. */
19888 in_switch_statement_p = parser->in_switch_statement_p;
19889 parser->in_switch_statement_p = false;
19890 /* We are not immediately inside an extern "lang" block. */
19891 saved_in_unbraced_linkage_specification_p
19892 = parser->in_unbraced_linkage_specification_p;
19893 parser->in_unbraced_linkage_specification_p = false;
19895 /* Start the class. */
19896 if (nested_name_specifier_p)
19898 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19899 old_scope = push_inner_scope (scope);
19901 type = begin_class_definition (type);
19903 if (type == error_mark_node)
19904 /* If the type is erroneous, skip the entire body of the class. */
19905 cp_parser_skip_to_closing_brace (parser);
19906 else
19907 /* Parse the member-specification. */
19908 cp_parser_member_specification_opt (parser);
19910 /* Look for the trailing `}'. */
19911 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19912 /* Look for trailing attributes to apply to this class. */
19913 if (cp_parser_allow_gnu_extensions_p (parser))
19914 attributes = cp_parser_gnu_attributes_opt (parser);
19915 if (type != error_mark_node)
19916 type = finish_struct (type, attributes);
19917 if (nested_name_specifier_p)
19918 pop_inner_scope (old_scope, scope);
19920 /* We've finished a type definition. Check for the common syntax
19921 error of forgetting a semicolon after the definition. We need to
19922 be careful, as we can't just check for not-a-semicolon and be done
19923 with it; the user might have typed:
19925 class X { } c = ...;
19926 class X { } *p = ...;
19928 and so forth. Instead, enumerate all the possible tokens that
19929 might follow this production; if we don't see one of them, then
19930 complain and silently insert the semicolon. */
19932 cp_token *token = cp_lexer_peek_token (parser->lexer);
19933 bool want_semicolon = true;
19935 if (cp_next_tokens_can_be_std_attribute_p (parser))
19936 /* Don't try to parse c++11 attributes here. As per the
19937 grammar, that should be a task for
19938 cp_parser_decl_specifier_seq. */
19939 want_semicolon = false;
19941 switch (token->type)
19943 case CPP_NAME:
19944 case CPP_SEMICOLON:
19945 case CPP_MULT:
19946 case CPP_AND:
19947 case CPP_OPEN_PAREN:
19948 case CPP_CLOSE_PAREN:
19949 case CPP_COMMA:
19950 want_semicolon = false;
19951 break;
19953 /* While it's legal for type qualifiers and storage class
19954 specifiers to follow type definitions in the grammar, only
19955 compiler testsuites contain code like that. Assume that if
19956 we see such code, then what we're really seeing is a case
19957 like:
19959 class X { }
19960 const <type> var = ...;
19964 class Y { }
19965 static <type> func (...) ...
19967 i.e. the qualifier or specifier applies to the next
19968 declaration. To do so, however, we need to look ahead one
19969 more token to see if *that* token is a type specifier.
19971 This code could be improved to handle:
19973 class Z { }
19974 static const <type> var = ...; */
19975 case CPP_KEYWORD:
19976 if (keyword_is_decl_specifier (token->keyword))
19978 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19980 /* Handling user-defined types here would be nice, but very
19981 tricky. */
19982 want_semicolon
19983 = (lookahead->type == CPP_KEYWORD
19984 && keyword_begins_type_specifier (lookahead->keyword));
19986 break;
19987 default:
19988 break;
19991 /* If we don't have a type, then something is very wrong and we
19992 shouldn't try to do anything clever. Likewise for not seeing the
19993 closing brace. */
19994 if (closing_brace && TYPE_P (type) && want_semicolon)
19996 cp_token_position prev
19997 = cp_lexer_previous_token_position (parser->lexer);
19998 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19999 location_t loc = prev_token->location;
20001 if (CLASSTYPE_DECLARED_CLASS (type))
20002 error_at (loc, "expected %<;%> after class definition");
20003 else if (TREE_CODE (type) == RECORD_TYPE)
20004 error_at (loc, "expected %<;%> after struct definition");
20005 else if (TREE_CODE (type) == UNION_TYPE)
20006 error_at (loc, "expected %<;%> after union definition");
20007 else
20008 gcc_unreachable ();
20010 /* Unget one token and smash it to look as though we encountered
20011 a semicolon in the input stream. */
20012 cp_lexer_set_token_position (parser->lexer, prev);
20013 token = cp_lexer_peek_token (parser->lexer);
20014 token->type = CPP_SEMICOLON;
20015 token->keyword = RID_MAX;
20019 /* If this class is not itself within the scope of another class,
20020 then we need to parse the bodies of all of the queued function
20021 definitions. Note that the queued functions defined in a class
20022 are not always processed immediately following the
20023 class-specifier for that class. Consider:
20025 struct A {
20026 struct B { void f() { sizeof (A); } };
20029 If `f' were processed before the processing of `A' were
20030 completed, there would be no way to compute the size of `A'.
20031 Note that the nesting we are interested in here is lexical --
20032 not the semantic nesting given by TYPE_CONTEXT. In particular,
20033 for:
20035 struct A { struct B; };
20036 struct A::B { void f() { } };
20038 there is no need to delay the parsing of `A::B::f'. */
20039 if (--parser->num_classes_being_defined == 0)
20041 tree decl;
20042 tree class_type = NULL_TREE;
20043 tree pushed_scope = NULL_TREE;
20044 unsigned ix;
20045 cp_default_arg_entry *e;
20046 tree save_ccp, save_ccr;
20048 /* In a first pass, parse default arguments to the functions.
20049 Then, in a second pass, parse the bodies of the functions.
20050 This two-phased approach handles cases like:
20052 struct S {
20053 void f() { g(); }
20054 void g(int i = 3);
20058 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20060 decl = e->decl;
20061 /* If there are default arguments that have not yet been processed,
20062 take care of them now. */
20063 if (class_type != e->class_type)
20065 if (pushed_scope)
20066 pop_scope (pushed_scope);
20067 class_type = e->class_type;
20068 pushed_scope = push_scope (class_type);
20070 /* Make sure that any template parameters are in scope. */
20071 maybe_begin_member_template_processing (decl);
20072 /* Parse the default argument expressions. */
20073 cp_parser_late_parsing_default_args (parser, decl);
20074 /* Remove any template parameters from the symbol table. */
20075 maybe_end_member_template_processing ();
20077 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20078 /* Now parse any NSDMIs. */
20079 save_ccp = current_class_ptr;
20080 save_ccr = current_class_ref;
20081 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20083 if (class_type != DECL_CONTEXT (decl))
20085 if (pushed_scope)
20086 pop_scope (pushed_scope);
20087 class_type = DECL_CONTEXT (decl);
20088 pushed_scope = push_scope (class_type);
20090 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20091 cp_parser_late_parsing_nsdmi (parser, decl);
20093 vec_safe_truncate (unparsed_nsdmis, 0);
20094 current_class_ptr = save_ccp;
20095 current_class_ref = save_ccr;
20096 if (pushed_scope)
20097 pop_scope (pushed_scope);
20099 /* Now do some post-NSDMI bookkeeping. */
20100 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20101 after_nsdmi_defaulted_late_checks (class_type);
20102 vec_safe_truncate (unparsed_classes, 0);
20103 after_nsdmi_defaulted_late_checks (type);
20105 /* Now parse the body of the functions. */
20106 if (flag_openmp)
20108 /* OpenMP UDRs need to be parsed before all other functions. */
20109 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20110 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20111 cp_parser_late_parsing_for_member (parser, decl);
20112 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20113 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20114 cp_parser_late_parsing_for_member (parser, decl);
20116 else
20117 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20118 cp_parser_late_parsing_for_member (parser, decl);
20119 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20121 else
20122 vec_safe_push (unparsed_classes, type);
20124 /* Put back any saved access checks. */
20125 pop_deferring_access_checks ();
20127 /* Restore saved state. */
20128 parser->in_switch_statement_p = in_switch_statement_p;
20129 parser->in_statement = in_statement;
20130 parser->in_function_body = saved_in_function_body;
20131 parser->num_template_parameter_lists
20132 = saved_num_template_parameter_lists;
20133 parser->in_unbraced_linkage_specification_p
20134 = saved_in_unbraced_linkage_specification_p;
20136 return type;
20139 static tree
20140 cp_parser_class_specifier (cp_parser* parser)
20142 tree ret;
20143 timevar_push (TV_PARSE_STRUCT);
20144 ret = cp_parser_class_specifier_1 (parser);
20145 timevar_pop (TV_PARSE_STRUCT);
20146 return ret;
20149 /* Parse a class-head.
20151 class-head:
20152 class-key identifier [opt] base-clause [opt]
20153 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20154 class-key nested-name-specifier [opt] template-id
20155 base-clause [opt]
20157 class-virt-specifier:
20158 final
20160 GNU Extensions:
20161 class-key attributes identifier [opt] base-clause [opt]
20162 class-key attributes nested-name-specifier identifier base-clause [opt]
20163 class-key attributes nested-name-specifier [opt] template-id
20164 base-clause [opt]
20166 Upon return BASES is initialized to the list of base classes (or
20167 NULL, if there are none) in the same form returned by
20168 cp_parser_base_clause.
20170 Returns the TYPE of the indicated class. Sets
20171 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20172 involving a nested-name-specifier was used, and FALSE otherwise.
20174 Returns error_mark_node if this is not a class-head.
20176 Returns NULL_TREE if the class-head is syntactically valid, but
20177 semantically invalid in a way that means we should skip the entire
20178 body of the class. */
20180 static tree
20181 cp_parser_class_head (cp_parser* parser,
20182 bool* nested_name_specifier_p)
20184 tree nested_name_specifier;
20185 enum tag_types class_key;
20186 tree id = NULL_TREE;
20187 tree type = NULL_TREE;
20188 tree attributes;
20189 tree bases;
20190 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20191 bool template_id_p = false;
20192 bool qualified_p = false;
20193 bool invalid_nested_name_p = false;
20194 bool invalid_explicit_specialization_p = false;
20195 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20196 tree pushed_scope = NULL_TREE;
20197 unsigned num_templates;
20198 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20199 /* Assume no nested-name-specifier will be present. */
20200 *nested_name_specifier_p = false;
20201 /* Assume no template parameter lists will be used in defining the
20202 type. */
20203 num_templates = 0;
20204 parser->colon_corrects_to_scope_p = false;
20206 /* Look for the class-key. */
20207 class_key = cp_parser_class_key (parser);
20208 if (class_key == none_type)
20209 return error_mark_node;
20211 /* Parse the attributes. */
20212 attributes = cp_parser_attributes_opt (parser);
20214 /* If the next token is `::', that is invalid -- but sometimes
20215 people do try to write:
20217 struct ::S {};
20219 Handle this gracefully by accepting the extra qualifier, and then
20220 issuing an error about it later if this really is a
20221 class-head. If it turns out just to be an elaborated type
20222 specifier, remain silent. */
20223 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20224 qualified_p = true;
20226 push_deferring_access_checks (dk_no_check);
20228 /* Determine the name of the class. Begin by looking for an
20229 optional nested-name-specifier. */
20230 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20231 nested_name_specifier
20232 = cp_parser_nested_name_specifier_opt (parser,
20233 /*typename_keyword_p=*/false,
20234 /*check_dependency_p=*/false,
20235 /*type_p=*/true,
20236 /*is_declaration=*/false);
20237 /* If there was a nested-name-specifier, then there *must* be an
20238 identifier. */
20239 if (nested_name_specifier)
20241 type_start_token = cp_lexer_peek_token (parser->lexer);
20242 /* Although the grammar says `identifier', it really means
20243 `class-name' or `template-name'. You are only allowed to
20244 define a class that has already been declared with this
20245 syntax.
20247 The proposed resolution for Core Issue 180 says that wherever
20248 you see `class T::X' you should treat `X' as a type-name.
20250 It is OK to define an inaccessible class; for example:
20252 class A { class B; };
20253 class A::B {};
20255 We do not know if we will see a class-name, or a
20256 template-name. We look for a class-name first, in case the
20257 class-name is a template-id; if we looked for the
20258 template-name first we would stop after the template-name. */
20259 cp_parser_parse_tentatively (parser);
20260 type = cp_parser_class_name (parser,
20261 /*typename_keyword_p=*/false,
20262 /*template_keyword_p=*/false,
20263 class_type,
20264 /*check_dependency_p=*/false,
20265 /*class_head_p=*/true,
20266 /*is_declaration=*/false);
20267 /* If that didn't work, ignore the nested-name-specifier. */
20268 if (!cp_parser_parse_definitely (parser))
20270 invalid_nested_name_p = true;
20271 type_start_token = cp_lexer_peek_token (parser->lexer);
20272 id = cp_parser_identifier (parser);
20273 if (id == error_mark_node)
20274 id = NULL_TREE;
20276 /* If we could not find a corresponding TYPE, treat this
20277 declaration like an unqualified declaration. */
20278 if (type == error_mark_node)
20279 nested_name_specifier = NULL_TREE;
20280 /* Otherwise, count the number of templates used in TYPE and its
20281 containing scopes. */
20282 else
20284 tree scope;
20286 for (scope = TREE_TYPE (type);
20287 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20288 scope = get_containing_scope (scope))
20289 if (TYPE_P (scope)
20290 && CLASS_TYPE_P (scope)
20291 && CLASSTYPE_TEMPLATE_INFO (scope)
20292 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20293 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20294 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20295 ++num_templates;
20298 /* Otherwise, the identifier is optional. */
20299 else
20301 /* We don't know whether what comes next is a template-id,
20302 an identifier, or nothing at all. */
20303 cp_parser_parse_tentatively (parser);
20304 /* Check for a template-id. */
20305 type_start_token = cp_lexer_peek_token (parser->lexer);
20306 id = cp_parser_template_id (parser,
20307 /*template_keyword_p=*/false,
20308 /*check_dependency_p=*/true,
20309 class_key,
20310 /*is_declaration=*/true);
20311 /* If that didn't work, it could still be an identifier. */
20312 if (!cp_parser_parse_definitely (parser))
20314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20316 type_start_token = cp_lexer_peek_token (parser->lexer);
20317 id = cp_parser_identifier (parser);
20319 else
20320 id = NULL_TREE;
20322 else
20324 template_id_p = true;
20325 ++num_templates;
20329 pop_deferring_access_checks ();
20331 if (id)
20333 cp_parser_check_for_invalid_template_id (parser, id,
20334 class_key,
20335 type_start_token->location);
20337 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20339 /* If it's not a `:' or a `{' then we can't really be looking at a
20340 class-head, since a class-head only appears as part of a
20341 class-specifier. We have to detect this situation before calling
20342 xref_tag, since that has irreversible side-effects. */
20343 if (!cp_parser_next_token_starts_class_definition_p (parser))
20345 cp_parser_error (parser, "expected %<{%> or %<:%>");
20346 type = error_mark_node;
20347 goto out;
20350 /* At this point, we're going ahead with the class-specifier, even
20351 if some other problem occurs. */
20352 cp_parser_commit_to_tentative_parse (parser);
20353 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20355 cp_parser_error (parser,
20356 "cannot specify %<override%> for a class");
20357 type = error_mark_node;
20358 goto out;
20360 /* Issue the error about the overly-qualified name now. */
20361 if (qualified_p)
20363 cp_parser_error (parser,
20364 "global qualification of class name is invalid");
20365 type = error_mark_node;
20366 goto out;
20368 else if (invalid_nested_name_p)
20370 cp_parser_error (parser,
20371 "qualified name does not name a class");
20372 type = error_mark_node;
20373 goto out;
20375 else if (nested_name_specifier)
20377 tree scope;
20379 /* Reject typedef-names in class heads. */
20380 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20382 error_at (type_start_token->location,
20383 "invalid class name in declaration of %qD",
20384 type);
20385 type = NULL_TREE;
20386 goto done;
20389 /* Figure out in what scope the declaration is being placed. */
20390 scope = current_scope ();
20391 /* If that scope does not contain the scope in which the
20392 class was originally declared, the program is invalid. */
20393 if (scope && !is_ancestor (scope, nested_name_specifier))
20395 if (at_namespace_scope_p ())
20396 error_at (type_start_token->location,
20397 "declaration of %qD in namespace %qD which does not "
20398 "enclose %qD",
20399 type, scope, nested_name_specifier);
20400 else
20401 error_at (type_start_token->location,
20402 "declaration of %qD in %qD which does not enclose %qD",
20403 type, scope, nested_name_specifier);
20404 type = NULL_TREE;
20405 goto done;
20407 /* [dcl.meaning]
20409 A declarator-id shall not be qualified except for the
20410 definition of a ... nested class outside of its class
20411 ... [or] the definition or explicit instantiation of a
20412 class member of a namespace outside of its namespace. */
20413 if (scope == nested_name_specifier)
20415 permerror (nested_name_specifier_token_start->location,
20416 "extra qualification not allowed");
20417 nested_name_specifier = NULL_TREE;
20418 num_templates = 0;
20421 /* An explicit-specialization must be preceded by "template <>". If
20422 it is not, try to recover gracefully. */
20423 if (at_namespace_scope_p ()
20424 && parser->num_template_parameter_lists == 0
20425 && template_id_p)
20427 error_at (type_start_token->location,
20428 "an explicit specialization must be preceded by %<template <>%>");
20429 invalid_explicit_specialization_p = true;
20430 /* Take the same action that would have been taken by
20431 cp_parser_explicit_specialization. */
20432 ++parser->num_template_parameter_lists;
20433 begin_specialization ();
20435 /* There must be no "return" statements between this point and the
20436 end of this function; set "type "to the correct return value and
20437 use "goto done;" to return. */
20438 /* Make sure that the right number of template parameters were
20439 present. */
20440 if (!cp_parser_check_template_parameters (parser, num_templates,
20441 type_start_token->location,
20442 /*declarator=*/NULL))
20444 /* If something went wrong, there is no point in even trying to
20445 process the class-definition. */
20446 type = NULL_TREE;
20447 goto done;
20450 /* Look up the type. */
20451 if (template_id_p)
20453 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20454 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20455 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20457 error_at (type_start_token->location,
20458 "function template %qD redeclared as a class template", id);
20459 type = error_mark_node;
20461 else
20463 type = TREE_TYPE (id);
20464 type = maybe_process_partial_specialization (type);
20466 if (nested_name_specifier)
20467 pushed_scope = push_scope (nested_name_specifier);
20469 else if (nested_name_specifier)
20471 tree class_type;
20473 /* Given:
20475 template <typename T> struct S { struct T };
20476 template <typename T> struct S<T>::T { };
20478 we will get a TYPENAME_TYPE when processing the definition of
20479 `S::T'. We need to resolve it to the actual type before we
20480 try to define it. */
20481 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20483 class_type = resolve_typename_type (TREE_TYPE (type),
20484 /*only_current_p=*/false);
20485 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20486 type = TYPE_NAME (class_type);
20487 else
20489 cp_parser_error (parser, "could not resolve typename type");
20490 type = error_mark_node;
20494 if (maybe_process_partial_specialization (TREE_TYPE (type))
20495 == error_mark_node)
20497 type = NULL_TREE;
20498 goto done;
20501 class_type = current_class_type;
20502 /* Enter the scope indicated by the nested-name-specifier. */
20503 pushed_scope = push_scope (nested_name_specifier);
20504 /* Get the canonical version of this type. */
20505 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20506 /* Call push_template_decl if it seems like we should be defining a
20507 template either from the template headers or the type we're
20508 defining, so that we diagnose both extra and missing headers. */
20509 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20510 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20511 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20513 type = push_template_decl (type);
20514 if (type == error_mark_node)
20516 type = NULL_TREE;
20517 goto done;
20521 type = TREE_TYPE (type);
20522 *nested_name_specifier_p = true;
20524 else /* The name is not a nested name. */
20526 /* If the class was unnamed, create a dummy name. */
20527 if (!id)
20528 id = make_anon_name ();
20529 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20530 parser->num_template_parameter_lists);
20533 /* Indicate whether this class was declared as a `class' or as a
20534 `struct'. */
20535 if (TREE_CODE (type) == RECORD_TYPE)
20536 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20537 cp_parser_check_class_key (class_key, type);
20539 /* If this type was already complete, and we see another definition,
20540 that's an error. */
20541 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20543 error_at (type_start_token->location, "redefinition of %q#T",
20544 type);
20545 error_at (type_start_token->location, "previous definition of %q+#T",
20546 type);
20547 type = NULL_TREE;
20548 goto done;
20550 else if (type == error_mark_node)
20551 type = NULL_TREE;
20553 if (type)
20555 /* Apply attributes now, before any use of the class as a template
20556 argument in its base list. */
20557 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20558 fixup_attribute_variants (type);
20561 /* We will have entered the scope containing the class; the names of
20562 base classes should be looked up in that context. For example:
20564 struct A { struct B {}; struct C; };
20565 struct A::C : B {};
20567 is valid. */
20569 /* Get the list of base-classes, if there is one. */
20570 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20572 /* PR59482: enter the class scope so that base-specifiers are looked
20573 up correctly. */
20574 if (type)
20575 pushclass (type);
20576 bases = cp_parser_base_clause (parser);
20577 /* PR59482: get out of the previously pushed class scope so that the
20578 subsequent pops pop the right thing. */
20579 if (type)
20580 popclass ();
20582 else
20583 bases = NULL_TREE;
20585 /* If we're really defining a class, process the base classes.
20586 If they're invalid, fail. */
20587 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20588 && !xref_basetypes (type, bases))
20589 type = NULL_TREE;
20591 done:
20592 /* Leave the scope given by the nested-name-specifier. We will
20593 enter the class scope itself while processing the members. */
20594 if (pushed_scope)
20595 pop_scope (pushed_scope);
20597 if (invalid_explicit_specialization_p)
20599 end_specialization ();
20600 --parser->num_template_parameter_lists;
20603 if (type)
20604 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20605 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20606 CLASSTYPE_FINAL (type) = 1;
20607 out:
20608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20609 return type;
20612 /* Parse a class-key.
20614 class-key:
20615 class
20616 struct
20617 union
20619 Returns the kind of class-key specified, or none_type to indicate
20620 error. */
20622 static enum tag_types
20623 cp_parser_class_key (cp_parser* parser)
20625 cp_token *token;
20626 enum tag_types tag_type;
20628 /* Look for the class-key. */
20629 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20630 if (!token)
20631 return none_type;
20633 /* Check to see if the TOKEN is a class-key. */
20634 tag_type = cp_parser_token_is_class_key (token);
20635 if (!tag_type)
20636 cp_parser_error (parser, "expected class-key");
20637 return tag_type;
20640 /* Parse a type-parameter-key.
20642 type-parameter-key:
20643 class
20644 typename
20647 static void
20648 cp_parser_type_parameter_key (cp_parser* parser)
20650 /* Look for the type-parameter-key. */
20651 enum tag_types tag_type = none_type;
20652 cp_token *token = cp_lexer_peek_token (parser->lexer);
20653 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20655 cp_lexer_consume_token (parser->lexer);
20656 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20657 /* typename is not allowed in a template template parameter
20658 by the standard until C++1Z. */
20659 pedwarn (token->location, OPT_Wpedantic,
20660 "ISO C++ forbids typename key in template template parameter;"
20661 " use -std=c++1z or -std=gnu++1z");
20663 else
20664 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20666 return;
20669 /* Parse an (optional) member-specification.
20671 member-specification:
20672 member-declaration member-specification [opt]
20673 access-specifier : member-specification [opt] */
20675 static void
20676 cp_parser_member_specification_opt (cp_parser* parser)
20678 while (true)
20680 cp_token *token;
20681 enum rid keyword;
20683 /* Peek at the next token. */
20684 token = cp_lexer_peek_token (parser->lexer);
20685 /* If it's a `}', or EOF then we've seen all the members. */
20686 if (token->type == CPP_CLOSE_BRACE
20687 || token->type == CPP_EOF
20688 || token->type == CPP_PRAGMA_EOL)
20689 break;
20691 /* See if this token is a keyword. */
20692 keyword = token->keyword;
20693 switch (keyword)
20695 case RID_PUBLIC:
20696 case RID_PROTECTED:
20697 case RID_PRIVATE:
20698 /* Consume the access-specifier. */
20699 cp_lexer_consume_token (parser->lexer);
20700 /* Remember which access-specifier is active. */
20701 current_access_specifier = token->u.value;
20702 /* Look for the `:'. */
20703 cp_parser_require (parser, CPP_COLON, RT_COLON);
20704 break;
20706 default:
20707 /* Accept #pragmas at class scope. */
20708 if (token->type == CPP_PRAGMA)
20710 cp_parser_pragma (parser, pragma_member);
20711 break;
20714 /* Otherwise, the next construction must be a
20715 member-declaration. */
20716 cp_parser_member_declaration (parser);
20721 /* Parse a member-declaration.
20723 member-declaration:
20724 decl-specifier-seq [opt] member-declarator-list [opt] ;
20725 function-definition ; [opt]
20726 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20727 using-declaration
20728 template-declaration
20729 alias-declaration
20731 member-declarator-list:
20732 member-declarator
20733 member-declarator-list , member-declarator
20735 member-declarator:
20736 declarator pure-specifier [opt]
20737 declarator constant-initializer [opt]
20738 identifier [opt] : constant-expression
20740 GNU Extensions:
20742 member-declaration:
20743 __extension__ member-declaration
20745 member-declarator:
20746 declarator attributes [opt] pure-specifier [opt]
20747 declarator attributes [opt] constant-initializer [opt]
20748 identifier [opt] attributes [opt] : constant-expression
20750 C++0x Extensions:
20752 member-declaration:
20753 static_assert-declaration */
20755 static void
20756 cp_parser_member_declaration (cp_parser* parser)
20758 cp_decl_specifier_seq decl_specifiers;
20759 tree prefix_attributes;
20760 tree decl;
20761 int declares_class_or_enum;
20762 bool friend_p;
20763 cp_token *token = NULL;
20764 cp_token *decl_spec_token_start = NULL;
20765 cp_token *initializer_token_start = NULL;
20766 int saved_pedantic;
20767 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20769 /* Check for the `__extension__' keyword. */
20770 if (cp_parser_extension_opt (parser, &saved_pedantic))
20772 /* Recurse. */
20773 cp_parser_member_declaration (parser);
20774 /* Restore the old value of the PEDANTIC flag. */
20775 pedantic = saved_pedantic;
20777 return;
20780 /* Check for a template-declaration. */
20781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20783 /* An explicit specialization here is an error condition, and we
20784 expect the specialization handler to detect and report this. */
20785 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20786 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20787 cp_parser_explicit_specialization (parser);
20788 else
20789 cp_parser_template_declaration (parser, /*member_p=*/true);
20791 return;
20794 /* Check for a using-declaration. */
20795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20797 if (cxx_dialect < cxx11)
20799 /* Parse the using-declaration. */
20800 cp_parser_using_declaration (parser,
20801 /*access_declaration_p=*/false);
20802 return;
20804 else
20806 tree decl;
20807 bool alias_decl_expected;
20808 cp_parser_parse_tentatively (parser);
20809 decl = cp_parser_alias_declaration (parser);
20810 /* Note that if we actually see the '=' token after the
20811 identifier, cp_parser_alias_declaration commits the
20812 tentative parse. In that case, we really expects an
20813 alias-declaration. Otherwise, we expect a using
20814 declaration. */
20815 alias_decl_expected =
20816 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20817 cp_parser_parse_definitely (parser);
20819 if (alias_decl_expected)
20820 finish_member_declaration (decl);
20821 else
20822 cp_parser_using_declaration (parser,
20823 /*access_declaration_p=*/false);
20824 return;
20828 /* Check for @defs. */
20829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20831 tree ivar, member;
20832 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20833 ivar = ivar_chains;
20834 while (ivar)
20836 member = ivar;
20837 ivar = TREE_CHAIN (member);
20838 TREE_CHAIN (member) = NULL_TREE;
20839 finish_member_declaration (member);
20841 return;
20844 /* If the next token is `static_assert' we have a static assertion. */
20845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20847 cp_parser_static_assert (parser, /*member_p=*/true);
20848 return;
20851 parser->colon_corrects_to_scope_p = false;
20853 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20854 goto out;
20856 /* Parse the decl-specifier-seq. */
20857 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20858 cp_parser_decl_specifier_seq (parser,
20859 CP_PARSER_FLAGS_OPTIONAL,
20860 &decl_specifiers,
20861 &declares_class_or_enum);
20862 /* Check for an invalid type-name. */
20863 if (!decl_specifiers.any_type_specifiers_p
20864 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20865 goto out;
20866 /* If there is no declarator, then the decl-specifier-seq should
20867 specify a type. */
20868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20870 /* If there was no decl-specifier-seq, and the next token is a
20871 `;', then we have something like:
20873 struct S { ; };
20875 [class.mem]
20877 Each member-declaration shall declare at least one member
20878 name of the class. */
20879 if (!decl_specifiers.any_specifiers_p)
20881 cp_token *token = cp_lexer_peek_token (parser->lexer);
20882 if (!in_system_header_at (token->location))
20883 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20885 else
20887 tree type;
20889 /* See if this declaration is a friend. */
20890 friend_p = cp_parser_friend_p (&decl_specifiers);
20891 /* If there were decl-specifiers, check to see if there was
20892 a class-declaration. */
20893 type = check_tag_decl (&decl_specifiers,
20894 /*explicit_type_instantiation_p=*/false);
20895 /* Nested classes have already been added to the class, but
20896 a `friend' needs to be explicitly registered. */
20897 if (friend_p)
20899 /* If the `friend' keyword was present, the friend must
20900 be introduced with a class-key. */
20901 if (!declares_class_or_enum && cxx_dialect < cxx11)
20902 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20903 "in C++03 a class-key must be used "
20904 "when declaring a friend");
20905 /* In this case:
20907 template <typename T> struct A {
20908 friend struct A<T>::B;
20911 A<T>::B will be represented by a TYPENAME_TYPE, and
20912 therefore not recognized by check_tag_decl. */
20913 if (!type)
20915 type = decl_specifiers.type;
20916 if (type && TREE_CODE (type) == TYPE_DECL)
20917 type = TREE_TYPE (type);
20919 if (!type || !TYPE_P (type))
20920 error_at (decl_spec_token_start->location,
20921 "friend declaration does not name a class or "
20922 "function");
20923 else
20924 make_friend_class (current_class_type, type,
20925 /*complain=*/true);
20927 /* If there is no TYPE, an error message will already have
20928 been issued. */
20929 else if (!type || type == error_mark_node)
20931 /* An anonymous aggregate has to be handled specially; such
20932 a declaration really declares a data member (with a
20933 particular type), as opposed to a nested class. */
20934 else if (ANON_AGGR_TYPE_P (type))
20936 /* C++11 9.5/6. */
20937 if (decl_specifiers.storage_class != sc_none)
20938 error_at (decl_spec_token_start->location,
20939 "a storage class on an anonymous aggregate "
20940 "in class scope is not allowed");
20942 /* Remove constructors and such from TYPE, now that we
20943 know it is an anonymous aggregate. */
20944 fixup_anonymous_aggr (type);
20945 /* And make the corresponding data member. */
20946 decl = build_decl (decl_spec_token_start->location,
20947 FIELD_DECL, NULL_TREE, type);
20948 /* Add it to the class. */
20949 finish_member_declaration (decl);
20951 else
20952 cp_parser_check_access_in_redeclaration
20953 (TYPE_NAME (type),
20954 decl_spec_token_start->location);
20957 else
20959 bool assume_semicolon = false;
20961 /* Clear attributes from the decl_specifiers but keep them
20962 around as prefix attributes that apply them to the entity
20963 being declared. */
20964 prefix_attributes = decl_specifiers.attributes;
20965 decl_specifiers.attributes = NULL_TREE;
20967 /* See if these declarations will be friends. */
20968 friend_p = cp_parser_friend_p (&decl_specifiers);
20970 /* Keep going until we hit the `;' at the end of the
20971 declaration. */
20972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20974 tree attributes = NULL_TREE;
20975 tree first_attribute;
20977 /* Peek at the next token. */
20978 token = cp_lexer_peek_token (parser->lexer);
20980 /* Check for a bitfield declaration. */
20981 if (token->type == CPP_COLON
20982 || (token->type == CPP_NAME
20983 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20984 == CPP_COLON))
20986 tree identifier;
20987 tree width;
20989 /* Get the name of the bitfield. Note that we cannot just
20990 check TOKEN here because it may have been invalidated by
20991 the call to cp_lexer_peek_nth_token above. */
20992 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20993 identifier = cp_parser_identifier (parser);
20994 else
20995 identifier = NULL_TREE;
20997 /* Consume the `:' token. */
20998 cp_lexer_consume_token (parser->lexer);
20999 /* Get the width of the bitfield. */
21000 width
21001 = cp_parser_constant_expression (parser);
21003 /* Look for attributes that apply to the bitfield. */
21004 attributes = cp_parser_attributes_opt (parser);
21005 /* Remember which attributes are prefix attributes and
21006 which are not. */
21007 first_attribute = attributes;
21008 /* Combine the attributes. */
21009 attributes = chainon (prefix_attributes, attributes);
21011 /* Create the bitfield declaration. */
21012 decl = grokbitfield (identifier
21013 ? make_id_declarator (NULL_TREE,
21014 identifier,
21015 sfk_none)
21016 : NULL,
21017 &decl_specifiers,
21018 width,
21019 attributes);
21021 else
21023 cp_declarator *declarator;
21024 tree initializer;
21025 tree asm_specification;
21026 int ctor_dtor_or_conv_p;
21028 /* Parse the declarator. */
21029 declarator
21030 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21031 &ctor_dtor_or_conv_p,
21032 /*parenthesized_p=*/NULL,
21033 /*member_p=*/true,
21034 friend_p);
21036 /* If something went wrong parsing the declarator, make sure
21037 that we at least consume some tokens. */
21038 if (declarator == cp_error_declarator)
21040 /* Skip to the end of the statement. */
21041 cp_parser_skip_to_end_of_statement (parser);
21042 /* If the next token is not a semicolon, that is
21043 probably because we just skipped over the body of
21044 a function. So, we consume a semicolon if
21045 present, but do not issue an error message if it
21046 is not present. */
21047 if (cp_lexer_next_token_is (parser->lexer,
21048 CPP_SEMICOLON))
21049 cp_lexer_consume_token (parser->lexer);
21050 goto out;
21053 if (declares_class_or_enum & 2)
21054 cp_parser_check_for_definition_in_return_type
21055 (declarator, decl_specifiers.type,
21056 decl_specifiers.locations[ds_type_spec]);
21058 /* Look for an asm-specification. */
21059 asm_specification = cp_parser_asm_specification_opt (parser);
21060 /* Look for attributes that apply to the declaration. */
21061 attributes = cp_parser_attributes_opt (parser);
21062 /* Remember which attributes are prefix attributes and
21063 which are not. */
21064 first_attribute = attributes;
21065 /* Combine the attributes. */
21066 attributes = chainon (prefix_attributes, attributes);
21068 /* If it's an `=', then we have a constant-initializer or a
21069 pure-specifier. It is not correct to parse the
21070 initializer before registering the member declaration
21071 since the member declaration should be in scope while
21072 its initializer is processed. However, the rest of the
21073 front end does not yet provide an interface that allows
21074 us to handle this correctly. */
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21077 /* In [class.mem]:
21079 A pure-specifier shall be used only in the declaration of
21080 a virtual function.
21082 A member-declarator can contain a constant-initializer
21083 only if it declares a static member of integral or
21084 enumeration type.
21086 Therefore, if the DECLARATOR is for a function, we look
21087 for a pure-specifier; otherwise, we look for a
21088 constant-initializer. When we call `grokfield', it will
21089 perform more stringent semantics checks. */
21090 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21091 if (function_declarator_p (declarator)
21092 || (decl_specifiers.type
21093 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21094 && declarator->kind == cdk_id
21095 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21096 == FUNCTION_TYPE)))
21097 initializer = cp_parser_pure_specifier (parser);
21098 else if (decl_specifiers.storage_class != sc_static)
21099 initializer = cp_parser_save_nsdmi (parser);
21100 else if (cxx_dialect >= cxx11)
21102 bool nonconst;
21103 /* Don't require a constant rvalue in C++11, since we
21104 might want a reference constant. We'll enforce
21105 constancy later. */
21106 cp_lexer_consume_token (parser->lexer);
21107 /* Parse the initializer. */
21108 initializer = cp_parser_initializer_clause (parser,
21109 &nonconst);
21111 else
21112 /* Parse the initializer. */
21113 initializer = cp_parser_constant_initializer (parser);
21115 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21116 && !function_declarator_p (declarator))
21118 bool x;
21119 if (decl_specifiers.storage_class != sc_static)
21120 initializer = cp_parser_save_nsdmi (parser);
21121 else
21122 initializer = cp_parser_initializer (parser, &x, &x);
21124 /* Otherwise, there is no initializer. */
21125 else
21126 initializer = NULL_TREE;
21128 /* See if we are probably looking at a function
21129 definition. We are certainly not looking at a
21130 member-declarator. Calling `grokfield' has
21131 side-effects, so we must not do it unless we are sure
21132 that we are looking at a member-declarator. */
21133 if (cp_parser_token_starts_function_definition_p
21134 (cp_lexer_peek_token (parser->lexer)))
21136 /* The grammar does not allow a pure-specifier to be
21137 used when a member function is defined. (It is
21138 possible that this fact is an oversight in the
21139 standard, since a pure function may be defined
21140 outside of the class-specifier. */
21141 if (initializer && initializer_token_start)
21142 error_at (initializer_token_start->location,
21143 "pure-specifier on function-definition");
21144 decl = cp_parser_save_member_function_body (parser,
21145 &decl_specifiers,
21146 declarator,
21147 attributes);
21148 if (parser->fully_implicit_function_template_p)
21149 decl = finish_fully_implicit_template (parser, decl);
21150 /* If the member was not a friend, declare it here. */
21151 if (!friend_p)
21152 finish_member_declaration (decl);
21153 /* Peek at the next token. */
21154 token = cp_lexer_peek_token (parser->lexer);
21155 /* If the next token is a semicolon, consume it. */
21156 if (token->type == CPP_SEMICOLON)
21157 cp_lexer_consume_token (parser->lexer);
21158 goto out;
21160 else
21161 if (declarator->kind == cdk_function)
21162 declarator->id_loc = token->location;
21163 /* Create the declaration. */
21164 decl = grokfield (declarator, &decl_specifiers,
21165 initializer, /*init_const_expr_p=*/true,
21166 asm_specification, attributes);
21167 if (parser->fully_implicit_function_template_p)
21169 if (friend_p)
21170 finish_fully_implicit_template (parser, 0);
21171 else
21172 decl = finish_fully_implicit_template (parser, decl);
21176 cp_finalize_omp_declare_simd (parser, decl);
21178 /* Reset PREFIX_ATTRIBUTES. */
21179 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21180 attributes = TREE_CHAIN (attributes);
21181 if (attributes)
21182 TREE_CHAIN (attributes) = NULL_TREE;
21184 /* If there is any qualification still in effect, clear it
21185 now; we will be starting fresh with the next declarator. */
21186 parser->scope = NULL_TREE;
21187 parser->qualifying_scope = NULL_TREE;
21188 parser->object_scope = NULL_TREE;
21189 /* If it's a `,', then there are more declarators. */
21190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21192 cp_lexer_consume_token (parser->lexer);
21193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21195 cp_token *token = cp_lexer_previous_token (parser->lexer);
21196 error_at (token->location,
21197 "stray %<,%> at end of member declaration");
21200 /* If the next token isn't a `;', then we have a parse error. */
21201 else if (cp_lexer_next_token_is_not (parser->lexer,
21202 CPP_SEMICOLON))
21204 /* The next token might be a ways away from where the
21205 actual semicolon is missing. Find the previous token
21206 and use that for our error position. */
21207 cp_token *token = cp_lexer_previous_token (parser->lexer);
21208 error_at (token->location,
21209 "expected %<;%> at end of member declaration");
21211 /* Assume that the user meant to provide a semicolon. If
21212 we were to cp_parser_skip_to_end_of_statement, we might
21213 skip to a semicolon inside a member function definition
21214 and issue nonsensical error messages. */
21215 assume_semicolon = true;
21218 if (decl)
21220 /* Add DECL to the list of members. */
21221 if (!friend_p
21222 /* Explicitly include, eg, NSDMIs, for better error
21223 recovery (c++/58650). */
21224 || !DECL_DECLARES_FUNCTION_P (decl))
21225 finish_member_declaration (decl);
21227 if (TREE_CODE (decl) == FUNCTION_DECL)
21228 cp_parser_save_default_args (parser, decl);
21229 else if (TREE_CODE (decl) == FIELD_DECL
21230 && !DECL_C_BIT_FIELD (decl)
21231 && DECL_INITIAL (decl))
21232 /* Add DECL to the queue of NSDMI to be parsed later. */
21233 vec_safe_push (unparsed_nsdmis, decl);
21236 if (assume_semicolon)
21237 goto out;
21241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21242 out:
21243 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21246 /* Parse a pure-specifier.
21248 pure-specifier:
21251 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21252 Otherwise, ERROR_MARK_NODE is returned. */
21254 static tree
21255 cp_parser_pure_specifier (cp_parser* parser)
21257 cp_token *token;
21259 /* Look for the `=' token. */
21260 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21261 return error_mark_node;
21262 /* Look for the `0' token. */
21263 token = cp_lexer_peek_token (parser->lexer);
21265 if (token->type == CPP_EOF
21266 || token->type == CPP_PRAGMA_EOL)
21267 return error_mark_node;
21269 cp_lexer_consume_token (parser->lexer);
21271 /* Accept = default or = delete in c++0x mode. */
21272 if (token->keyword == RID_DEFAULT
21273 || token->keyword == RID_DELETE)
21275 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21276 return token->u.value;
21279 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21280 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21282 cp_parser_error (parser,
21283 "invalid pure specifier (only %<= 0%> is allowed)");
21284 cp_parser_skip_to_end_of_statement (parser);
21285 return error_mark_node;
21287 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21289 error_at (token->location, "templates may not be %<virtual%>");
21290 return error_mark_node;
21293 return integer_zero_node;
21296 /* Parse a constant-initializer.
21298 constant-initializer:
21299 = constant-expression
21301 Returns a representation of the constant-expression. */
21303 static tree
21304 cp_parser_constant_initializer (cp_parser* parser)
21306 /* Look for the `=' token. */
21307 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21308 return error_mark_node;
21310 /* It is invalid to write:
21312 struct S { static const int i = { 7 }; };
21315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21317 cp_parser_error (parser,
21318 "a brace-enclosed initializer is not allowed here");
21319 /* Consume the opening brace. */
21320 cp_lexer_consume_token (parser->lexer);
21321 /* Skip the initializer. */
21322 cp_parser_skip_to_closing_brace (parser);
21323 /* Look for the trailing `}'. */
21324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21326 return error_mark_node;
21329 return cp_parser_constant_expression (parser);
21332 /* Derived classes [gram.class.derived] */
21334 /* Parse a base-clause.
21336 base-clause:
21337 : base-specifier-list
21339 base-specifier-list:
21340 base-specifier ... [opt]
21341 base-specifier-list , base-specifier ... [opt]
21343 Returns a TREE_LIST representing the base-classes, in the order in
21344 which they were declared. The representation of each node is as
21345 described by cp_parser_base_specifier.
21347 In the case that no bases are specified, this function will return
21348 NULL_TREE, not ERROR_MARK_NODE. */
21350 static tree
21351 cp_parser_base_clause (cp_parser* parser)
21353 tree bases = NULL_TREE;
21355 /* Look for the `:' that begins the list. */
21356 cp_parser_require (parser, CPP_COLON, RT_COLON);
21358 /* Scan the base-specifier-list. */
21359 while (true)
21361 cp_token *token;
21362 tree base;
21363 bool pack_expansion_p = false;
21365 /* Look for the base-specifier. */
21366 base = cp_parser_base_specifier (parser);
21367 /* Look for the (optional) ellipsis. */
21368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21370 /* Consume the `...'. */
21371 cp_lexer_consume_token (parser->lexer);
21373 pack_expansion_p = true;
21376 /* Add BASE to the front of the list. */
21377 if (base && base != error_mark_node)
21379 if (pack_expansion_p)
21380 /* Make this a pack expansion type. */
21381 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21383 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21385 TREE_CHAIN (base) = bases;
21386 bases = base;
21389 /* Peek at the next token. */
21390 token = cp_lexer_peek_token (parser->lexer);
21391 /* If it's not a comma, then the list is complete. */
21392 if (token->type != CPP_COMMA)
21393 break;
21394 /* Consume the `,'. */
21395 cp_lexer_consume_token (parser->lexer);
21398 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21399 base class had a qualified name. However, the next name that
21400 appears is certainly not qualified. */
21401 parser->scope = NULL_TREE;
21402 parser->qualifying_scope = NULL_TREE;
21403 parser->object_scope = NULL_TREE;
21405 return nreverse (bases);
21408 /* Parse a base-specifier.
21410 base-specifier:
21411 :: [opt] nested-name-specifier [opt] class-name
21412 virtual access-specifier [opt] :: [opt] nested-name-specifier
21413 [opt] class-name
21414 access-specifier virtual [opt] :: [opt] nested-name-specifier
21415 [opt] class-name
21417 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21418 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21419 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21420 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21422 static tree
21423 cp_parser_base_specifier (cp_parser* parser)
21425 cp_token *token;
21426 bool done = false;
21427 bool virtual_p = false;
21428 bool duplicate_virtual_error_issued_p = false;
21429 bool duplicate_access_error_issued_p = false;
21430 bool class_scope_p, template_p;
21431 tree access = access_default_node;
21432 tree type;
21434 /* Process the optional `virtual' and `access-specifier'. */
21435 while (!done)
21437 /* Peek at the next token. */
21438 token = cp_lexer_peek_token (parser->lexer);
21439 /* Process `virtual'. */
21440 switch (token->keyword)
21442 case RID_VIRTUAL:
21443 /* If `virtual' appears more than once, issue an error. */
21444 if (virtual_p && !duplicate_virtual_error_issued_p)
21446 cp_parser_error (parser,
21447 "%<virtual%> specified more than once in base-specified");
21448 duplicate_virtual_error_issued_p = true;
21451 virtual_p = true;
21453 /* Consume the `virtual' token. */
21454 cp_lexer_consume_token (parser->lexer);
21456 break;
21458 case RID_PUBLIC:
21459 case RID_PROTECTED:
21460 case RID_PRIVATE:
21461 /* If more than one access specifier appears, issue an
21462 error. */
21463 if (access != access_default_node
21464 && !duplicate_access_error_issued_p)
21466 cp_parser_error (parser,
21467 "more than one access specifier in base-specified");
21468 duplicate_access_error_issued_p = true;
21471 access = ridpointers[(int) token->keyword];
21473 /* Consume the access-specifier. */
21474 cp_lexer_consume_token (parser->lexer);
21476 break;
21478 default:
21479 done = true;
21480 break;
21483 /* It is not uncommon to see programs mechanically, erroneously, use
21484 the 'typename' keyword to denote (dependent) qualified types
21485 as base classes. */
21486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21488 token = cp_lexer_peek_token (parser->lexer);
21489 if (!processing_template_decl)
21490 error_at (token->location,
21491 "keyword %<typename%> not allowed outside of templates");
21492 else
21493 error_at (token->location,
21494 "keyword %<typename%> not allowed in this context "
21495 "(the base class is implicitly a type)");
21496 cp_lexer_consume_token (parser->lexer);
21499 /* Look for the optional `::' operator. */
21500 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21501 /* Look for the nested-name-specifier. The simplest way to
21502 implement:
21504 [temp.res]
21506 The keyword `typename' is not permitted in a base-specifier or
21507 mem-initializer; in these contexts a qualified name that
21508 depends on a template-parameter is implicitly assumed to be a
21509 type name.
21511 is to pretend that we have seen the `typename' keyword at this
21512 point. */
21513 cp_parser_nested_name_specifier_opt (parser,
21514 /*typename_keyword_p=*/true,
21515 /*check_dependency_p=*/true,
21516 typename_type,
21517 /*is_declaration=*/true);
21518 /* If the base class is given by a qualified name, assume that names
21519 we see are type names or templates, as appropriate. */
21520 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21521 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21523 if (!parser->scope
21524 && cp_lexer_next_token_is_decltype (parser->lexer))
21525 /* DR 950 allows decltype as a base-specifier. */
21526 type = cp_parser_decltype (parser);
21527 else
21529 /* Otherwise, look for the class-name. */
21530 type = cp_parser_class_name (parser,
21531 class_scope_p,
21532 template_p,
21533 typename_type,
21534 /*check_dependency_p=*/true,
21535 /*class_head_p=*/false,
21536 /*is_declaration=*/true);
21537 type = TREE_TYPE (type);
21540 if (type == error_mark_node)
21541 return error_mark_node;
21543 return finish_base_specifier (type, access, virtual_p);
21546 /* Exception handling [gram.exception] */
21548 /* Parse an (optional) noexcept-specification.
21550 noexcept-specification:
21551 noexcept ( constant-expression ) [opt]
21553 If no noexcept-specification is present, returns NULL_TREE.
21554 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21555 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21556 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21557 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21558 in which case a boolean condition is returned instead. */
21560 static tree
21561 cp_parser_noexcept_specification_opt (cp_parser* parser,
21562 bool require_constexpr,
21563 bool* consumed_expr,
21564 bool return_cond)
21566 cp_token *token;
21567 const char *saved_message;
21569 /* Peek at the next token. */
21570 token = cp_lexer_peek_token (parser->lexer);
21572 /* Is it a noexcept-specification? */
21573 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21575 tree expr;
21576 cp_lexer_consume_token (parser->lexer);
21578 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21580 cp_lexer_consume_token (parser->lexer);
21582 if (require_constexpr)
21584 /* Types may not be defined in an exception-specification. */
21585 saved_message = parser->type_definition_forbidden_message;
21586 parser->type_definition_forbidden_message
21587 = G_("types may not be defined in an exception-specification");
21589 expr = cp_parser_constant_expression (parser);
21591 /* Restore the saved message. */
21592 parser->type_definition_forbidden_message = saved_message;
21594 else
21596 expr = cp_parser_expression (parser);
21597 *consumed_expr = true;
21600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21602 else
21604 expr = boolean_true_node;
21605 if (!require_constexpr)
21606 *consumed_expr = false;
21609 /* We cannot build a noexcept-spec right away because this will check
21610 that expr is a constexpr. */
21611 if (!return_cond)
21612 return build_noexcept_spec (expr, tf_warning_or_error);
21613 else
21614 return expr;
21616 else
21617 return NULL_TREE;
21620 /* Parse an (optional) exception-specification.
21622 exception-specification:
21623 throw ( type-id-list [opt] )
21625 Returns a TREE_LIST representing the exception-specification. The
21626 TREE_VALUE of each node is a type. */
21628 static tree
21629 cp_parser_exception_specification_opt (cp_parser* parser)
21631 cp_token *token;
21632 tree type_id_list;
21633 const char *saved_message;
21635 /* Peek at the next token. */
21636 token = cp_lexer_peek_token (parser->lexer);
21638 /* Is it a noexcept-specification? */
21639 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21640 false);
21641 if (type_id_list != NULL_TREE)
21642 return type_id_list;
21644 /* If it's not `throw', then there's no exception-specification. */
21645 if (!cp_parser_is_keyword (token, RID_THROW))
21646 return NULL_TREE;
21648 #if 0
21649 /* Enable this once a lot of code has transitioned to noexcept? */
21650 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21651 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21652 "deprecated in C++0x; use %<noexcept%> instead");
21653 #endif
21655 /* Consume the `throw'. */
21656 cp_lexer_consume_token (parser->lexer);
21658 /* Look for the `('. */
21659 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21661 /* Peek at the next token. */
21662 token = cp_lexer_peek_token (parser->lexer);
21663 /* If it's not a `)', then there is a type-id-list. */
21664 if (token->type != CPP_CLOSE_PAREN)
21666 /* Types may not be defined in an exception-specification. */
21667 saved_message = parser->type_definition_forbidden_message;
21668 parser->type_definition_forbidden_message
21669 = G_("types may not be defined in an exception-specification");
21670 /* Parse the type-id-list. */
21671 type_id_list = cp_parser_type_id_list (parser);
21672 /* Restore the saved message. */
21673 parser->type_definition_forbidden_message = saved_message;
21675 else
21676 type_id_list = empty_except_spec;
21678 /* Look for the `)'. */
21679 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21681 return type_id_list;
21684 /* Parse an (optional) type-id-list.
21686 type-id-list:
21687 type-id ... [opt]
21688 type-id-list , type-id ... [opt]
21690 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21691 in the order that the types were presented. */
21693 static tree
21694 cp_parser_type_id_list (cp_parser* parser)
21696 tree types = NULL_TREE;
21698 while (true)
21700 cp_token *token;
21701 tree type;
21703 /* Get the next type-id. */
21704 type = cp_parser_type_id (parser);
21705 /* Parse the optional ellipsis. */
21706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21708 /* Consume the `...'. */
21709 cp_lexer_consume_token (parser->lexer);
21711 /* Turn the type into a pack expansion expression. */
21712 type = make_pack_expansion (type);
21714 /* Add it to the list. */
21715 types = add_exception_specifier (types, type, /*complain=*/1);
21716 /* Peek at the next token. */
21717 token = cp_lexer_peek_token (parser->lexer);
21718 /* If it is not a `,', we are done. */
21719 if (token->type != CPP_COMMA)
21720 break;
21721 /* Consume the `,'. */
21722 cp_lexer_consume_token (parser->lexer);
21725 return nreverse (types);
21728 /* Parse a try-block.
21730 try-block:
21731 try compound-statement handler-seq */
21733 static tree
21734 cp_parser_try_block (cp_parser* parser)
21736 tree try_block;
21738 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21739 if (parser->in_function_body
21740 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21741 error ("%<try%> in %<constexpr%> function");
21743 try_block = begin_try_block ();
21744 cp_parser_compound_statement (parser, NULL, true, false);
21745 finish_try_block (try_block);
21746 cp_parser_handler_seq (parser);
21747 finish_handler_sequence (try_block);
21749 return try_block;
21752 /* Parse a function-try-block.
21754 function-try-block:
21755 try ctor-initializer [opt] function-body handler-seq */
21757 static bool
21758 cp_parser_function_try_block (cp_parser* parser)
21760 tree compound_stmt;
21761 tree try_block;
21762 bool ctor_initializer_p;
21764 /* Look for the `try' keyword. */
21765 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21766 return false;
21767 /* Let the rest of the front end know where we are. */
21768 try_block = begin_function_try_block (&compound_stmt);
21769 /* Parse the function-body. */
21770 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21771 (parser, /*in_function_try_block=*/true);
21772 /* We're done with the `try' part. */
21773 finish_function_try_block (try_block);
21774 /* Parse the handlers. */
21775 cp_parser_handler_seq (parser);
21776 /* We're done with the handlers. */
21777 finish_function_handler_sequence (try_block, compound_stmt);
21779 return ctor_initializer_p;
21782 /* Parse a handler-seq.
21784 handler-seq:
21785 handler handler-seq [opt] */
21787 static void
21788 cp_parser_handler_seq (cp_parser* parser)
21790 while (true)
21792 cp_token *token;
21794 /* Parse the handler. */
21795 cp_parser_handler (parser);
21796 /* Peek at the next token. */
21797 token = cp_lexer_peek_token (parser->lexer);
21798 /* If it's not `catch' then there are no more handlers. */
21799 if (!cp_parser_is_keyword (token, RID_CATCH))
21800 break;
21804 /* Parse a handler.
21806 handler:
21807 catch ( exception-declaration ) compound-statement */
21809 static void
21810 cp_parser_handler (cp_parser* parser)
21812 tree handler;
21813 tree declaration;
21815 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21816 handler = begin_handler ();
21817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21818 declaration = cp_parser_exception_declaration (parser);
21819 finish_handler_parms (declaration, handler);
21820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21821 cp_parser_compound_statement (parser, NULL, false, false);
21822 finish_handler (handler);
21825 /* Parse an exception-declaration.
21827 exception-declaration:
21828 type-specifier-seq declarator
21829 type-specifier-seq abstract-declarator
21830 type-specifier-seq
21833 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21834 ellipsis variant is used. */
21836 static tree
21837 cp_parser_exception_declaration (cp_parser* parser)
21839 cp_decl_specifier_seq type_specifiers;
21840 cp_declarator *declarator;
21841 const char *saved_message;
21843 /* If it's an ellipsis, it's easy to handle. */
21844 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21846 /* Consume the `...' token. */
21847 cp_lexer_consume_token (parser->lexer);
21848 return NULL_TREE;
21851 /* Types may not be defined in exception-declarations. */
21852 saved_message = parser->type_definition_forbidden_message;
21853 parser->type_definition_forbidden_message
21854 = G_("types may not be defined in exception-declarations");
21856 /* Parse the type-specifier-seq. */
21857 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21858 /*is_trailing_return=*/false,
21859 &type_specifiers);
21860 /* If it's a `)', then there is no declarator. */
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21862 declarator = NULL;
21863 else
21864 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21865 /*ctor_dtor_or_conv_p=*/NULL,
21866 /*parenthesized_p=*/NULL,
21867 /*member_p=*/false,
21868 /*friend_p=*/false);
21870 /* Restore the saved message. */
21871 parser->type_definition_forbidden_message = saved_message;
21873 if (!type_specifiers.any_specifiers_p)
21874 return error_mark_node;
21876 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21879 /* Parse a throw-expression.
21881 throw-expression:
21882 throw assignment-expression [opt]
21884 Returns a THROW_EXPR representing the throw-expression. */
21886 static tree
21887 cp_parser_throw_expression (cp_parser* parser)
21889 tree expression;
21890 cp_token* token;
21892 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21893 token = cp_lexer_peek_token (parser->lexer);
21894 /* Figure out whether or not there is an assignment-expression
21895 following the "throw" keyword. */
21896 if (token->type == CPP_COMMA
21897 || token->type == CPP_SEMICOLON
21898 || token->type == CPP_CLOSE_PAREN
21899 || token->type == CPP_CLOSE_SQUARE
21900 || token->type == CPP_CLOSE_BRACE
21901 || token->type == CPP_COLON)
21902 expression = NULL_TREE;
21903 else
21904 expression = cp_parser_assignment_expression (parser);
21906 return build_throw (expression);
21909 /* GNU Extensions */
21911 /* Parse an (optional) asm-specification.
21913 asm-specification:
21914 asm ( string-literal )
21916 If the asm-specification is present, returns a STRING_CST
21917 corresponding to the string-literal. Otherwise, returns
21918 NULL_TREE. */
21920 static tree
21921 cp_parser_asm_specification_opt (cp_parser* parser)
21923 cp_token *token;
21924 tree asm_specification;
21926 /* Peek at the next token. */
21927 token = cp_lexer_peek_token (parser->lexer);
21928 /* If the next token isn't the `asm' keyword, then there's no
21929 asm-specification. */
21930 if (!cp_parser_is_keyword (token, RID_ASM))
21931 return NULL_TREE;
21933 /* Consume the `asm' token. */
21934 cp_lexer_consume_token (parser->lexer);
21935 /* Look for the `('. */
21936 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21938 /* Look for the string-literal. */
21939 asm_specification = cp_parser_string_literal (parser, false, false);
21941 /* Look for the `)'. */
21942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21944 return asm_specification;
21947 /* Parse an asm-operand-list.
21949 asm-operand-list:
21950 asm-operand
21951 asm-operand-list , asm-operand
21953 asm-operand:
21954 string-literal ( expression )
21955 [ string-literal ] string-literal ( expression )
21957 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21958 each node is the expression. The TREE_PURPOSE is itself a
21959 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21960 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21961 is a STRING_CST for the string literal before the parenthesis. Returns
21962 ERROR_MARK_NODE if any of the operands are invalid. */
21964 static tree
21965 cp_parser_asm_operand_list (cp_parser* parser)
21967 tree asm_operands = NULL_TREE;
21968 bool invalid_operands = false;
21970 while (true)
21972 tree string_literal;
21973 tree expression;
21974 tree name;
21976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21978 /* Consume the `[' token. */
21979 cp_lexer_consume_token (parser->lexer);
21980 /* Read the operand name. */
21981 name = cp_parser_identifier (parser);
21982 if (name != error_mark_node)
21983 name = build_string (IDENTIFIER_LENGTH (name),
21984 IDENTIFIER_POINTER (name));
21985 /* Look for the closing `]'. */
21986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21988 else
21989 name = NULL_TREE;
21990 /* Look for the string-literal. */
21991 string_literal = cp_parser_string_literal (parser, false, false);
21993 /* Look for the `('. */
21994 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21995 /* Parse the expression. */
21996 expression = cp_parser_expression (parser);
21997 /* Look for the `)'. */
21998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22000 if (name == error_mark_node
22001 || string_literal == error_mark_node
22002 || expression == error_mark_node)
22003 invalid_operands = true;
22005 /* Add this operand to the list. */
22006 asm_operands = tree_cons (build_tree_list (name, string_literal),
22007 expression,
22008 asm_operands);
22009 /* If the next token is not a `,', there are no more
22010 operands. */
22011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22012 break;
22013 /* Consume the `,'. */
22014 cp_lexer_consume_token (parser->lexer);
22017 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22020 /* Parse an asm-clobber-list.
22022 asm-clobber-list:
22023 string-literal
22024 asm-clobber-list , string-literal
22026 Returns a TREE_LIST, indicating the clobbers in the order that they
22027 appeared. The TREE_VALUE of each node is a STRING_CST. */
22029 static tree
22030 cp_parser_asm_clobber_list (cp_parser* parser)
22032 tree clobbers = NULL_TREE;
22034 while (true)
22036 tree string_literal;
22038 /* Look for the string literal. */
22039 string_literal = cp_parser_string_literal (parser, false, false);
22040 /* Add it to the list. */
22041 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22042 /* If the next token is not a `,', then the list is
22043 complete. */
22044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22045 break;
22046 /* Consume the `,' token. */
22047 cp_lexer_consume_token (parser->lexer);
22050 return clobbers;
22053 /* Parse an asm-label-list.
22055 asm-label-list:
22056 identifier
22057 asm-label-list , identifier
22059 Returns a TREE_LIST, indicating the labels in the order that they
22060 appeared. The TREE_VALUE of each node is a label. */
22062 static tree
22063 cp_parser_asm_label_list (cp_parser* parser)
22065 tree labels = NULL_TREE;
22067 while (true)
22069 tree identifier, label, name;
22071 /* Look for the identifier. */
22072 identifier = cp_parser_identifier (parser);
22073 if (!error_operand_p (identifier))
22075 label = lookup_label (identifier);
22076 if (TREE_CODE (label) == LABEL_DECL)
22078 TREE_USED (label) = 1;
22079 check_goto (label);
22080 name = build_string (IDENTIFIER_LENGTH (identifier),
22081 IDENTIFIER_POINTER (identifier));
22082 labels = tree_cons (name, label, labels);
22085 /* If the next token is not a `,', then the list is
22086 complete. */
22087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22088 break;
22089 /* Consume the `,' token. */
22090 cp_lexer_consume_token (parser->lexer);
22093 return nreverse (labels);
22096 /* Return TRUE iff the next tokens in the stream are possibly the
22097 beginning of a GNU extension attribute. */
22099 static bool
22100 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22102 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22105 /* Return TRUE iff the next tokens in the stream are possibly the
22106 beginning of a standard C++-11 attribute specifier. */
22108 static bool
22109 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22111 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22114 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22115 beginning of a standard C++-11 attribute specifier. */
22117 static bool
22118 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22120 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22122 return (cxx_dialect >= cxx11
22123 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22124 || (token->type == CPP_OPEN_SQUARE
22125 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22126 && token->type == CPP_OPEN_SQUARE)));
22129 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22130 beginning of a GNU extension attribute. */
22132 static bool
22133 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22135 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22137 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22140 /* Return true iff the next tokens can be the beginning of either a
22141 GNU attribute list, or a standard C++11 attribute sequence. */
22143 static bool
22144 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22146 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22147 || cp_next_tokens_can_be_std_attribute_p (parser));
22150 /* Return true iff the next Nth tokens can be the beginning of either
22151 a GNU attribute list, or a standard C++11 attribute sequence. */
22153 static bool
22154 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22156 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22157 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22160 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22161 of GNU attributes, or return NULL. */
22163 static tree
22164 cp_parser_attributes_opt (cp_parser *parser)
22166 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22167 return cp_parser_gnu_attributes_opt (parser);
22168 return cp_parser_std_attribute_spec_seq (parser);
22171 #define CILK_SIMD_FN_CLAUSE_MASK \
22172 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22173 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22174 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22175 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22176 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22178 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22179 vector [(<clauses>)] */
22181 static void
22182 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22184 bool first_p = parser->cilk_simd_fn_info == NULL;
22185 cp_token *token = v_token;
22186 if (first_p)
22188 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22189 parser->cilk_simd_fn_info->error_seen = false;
22190 parser->cilk_simd_fn_info->fndecl_seen = false;
22191 parser->cilk_simd_fn_info->tokens = vNULL;
22193 int paren_scope = 0;
22194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22196 cp_lexer_consume_token (parser->lexer);
22197 v_token = cp_lexer_peek_token (parser->lexer);
22198 paren_scope++;
22200 while (paren_scope > 0)
22202 token = cp_lexer_peek_token (parser->lexer);
22203 if (token->type == CPP_OPEN_PAREN)
22204 paren_scope++;
22205 else if (token->type == CPP_CLOSE_PAREN)
22206 paren_scope--;
22207 /* Do not push the last ')' */
22208 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22209 cp_lexer_consume_token (parser->lexer);
22212 token->type = CPP_PRAGMA_EOL;
22213 parser->lexer->next_token = token;
22214 cp_lexer_consume_token (parser->lexer);
22216 struct cp_token_cache *cp
22217 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22218 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22221 /* Parse an (optional) series of attributes.
22223 attributes:
22224 attributes attribute
22226 attribute:
22227 __attribute__ (( attribute-list [opt] ))
22229 The return value is as for cp_parser_gnu_attribute_list. */
22231 static tree
22232 cp_parser_gnu_attributes_opt (cp_parser* parser)
22234 tree attributes = NULL_TREE;
22236 while (true)
22238 cp_token *token;
22239 tree attribute_list;
22240 bool ok = true;
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_token (parser->lexer);
22244 /* If it's not `__attribute__', then we're done. */
22245 if (token->keyword != RID_ATTRIBUTE)
22246 break;
22248 /* Consume the `__attribute__' keyword. */
22249 cp_lexer_consume_token (parser->lexer);
22250 /* Look for the two `(' tokens. */
22251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22252 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22254 /* Peek at the next token. */
22255 token = cp_lexer_peek_token (parser->lexer);
22256 if (token->type != CPP_CLOSE_PAREN)
22257 /* Parse the attribute-list. */
22258 attribute_list = cp_parser_gnu_attribute_list (parser);
22259 else
22260 /* If the next token is a `)', then there is no attribute
22261 list. */
22262 attribute_list = NULL;
22264 /* Look for the two `)' tokens. */
22265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22266 ok = false;
22267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22268 ok = false;
22269 if (!ok)
22270 cp_parser_skip_to_end_of_statement (parser);
22272 /* Add these new attributes to the list. */
22273 attributes = chainon (attributes, attribute_list);
22276 return attributes;
22279 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22280 "__vector" or "__vector__." */
22282 static inline bool
22283 is_cilkplus_vector_p (tree name)
22285 if (flag_cilkplus && is_attribute_p ("vector", name))
22286 return true;
22287 return false;
22290 /* Parse a GNU attribute-list.
22292 attribute-list:
22293 attribute
22294 attribute-list , attribute
22296 attribute:
22297 identifier
22298 identifier ( identifier )
22299 identifier ( identifier , expression-list )
22300 identifier ( expression-list )
22302 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22303 to an attribute. The TREE_PURPOSE of each node is the identifier
22304 indicating which attribute is in use. The TREE_VALUE represents
22305 the arguments, if any. */
22307 static tree
22308 cp_parser_gnu_attribute_list (cp_parser* parser)
22310 tree attribute_list = NULL_TREE;
22311 bool save_translate_strings_p = parser->translate_strings_p;
22313 parser->translate_strings_p = false;
22314 while (true)
22316 cp_token *token;
22317 tree identifier;
22318 tree attribute;
22320 /* Look for the identifier. We also allow keywords here; for
22321 example `__attribute__ ((const))' is legal. */
22322 token = cp_lexer_peek_token (parser->lexer);
22323 if (token->type == CPP_NAME
22324 || token->type == CPP_KEYWORD)
22326 tree arguments = NULL_TREE;
22328 /* Consume the token, but save it since we need it for the
22329 SIMD enabled function parsing. */
22330 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22332 /* Save away the identifier that indicates which attribute
22333 this is. */
22334 identifier = (token->type == CPP_KEYWORD)
22335 /* For keywords, use the canonical spelling, not the
22336 parsed identifier. */
22337 ? ridpointers[(int) token->keyword]
22338 : id_token->u.value;
22340 attribute = build_tree_list (identifier, NULL_TREE);
22342 /* Peek at the next token. */
22343 token = cp_lexer_peek_token (parser->lexer);
22344 /* If it's an `(', then parse the attribute arguments. */
22345 if (token->type == CPP_OPEN_PAREN)
22347 vec<tree, va_gc> *vec;
22348 int attr_flag = (attribute_takes_identifier_p (identifier)
22349 ? id_attr : normal_attr);
22350 if (is_cilkplus_vector_p (identifier))
22352 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22353 continue;
22355 else
22356 vec = cp_parser_parenthesized_expression_list
22357 (parser, attr_flag, /*cast_p=*/false,
22358 /*allow_expansion_p=*/false,
22359 /*non_constant_p=*/NULL);
22360 if (vec == NULL)
22361 arguments = error_mark_node;
22362 else
22364 arguments = build_tree_list_vec (vec);
22365 release_tree_vector (vec);
22367 /* Save the arguments away. */
22368 TREE_VALUE (attribute) = arguments;
22370 else if (is_cilkplus_vector_p (identifier))
22372 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22373 continue;
22376 if (arguments != error_mark_node)
22378 /* Add this attribute to the list. */
22379 TREE_CHAIN (attribute) = attribute_list;
22380 attribute_list = attribute;
22383 token = cp_lexer_peek_token (parser->lexer);
22385 /* Now, look for more attributes. If the next token isn't a
22386 `,', we're done. */
22387 if (token->type != CPP_COMMA)
22388 break;
22390 /* Consume the comma and keep going. */
22391 cp_lexer_consume_token (parser->lexer);
22393 parser->translate_strings_p = save_translate_strings_p;
22395 /* We built up the list in reverse order. */
22396 return nreverse (attribute_list);
22399 /* Parse a standard C++11 attribute.
22401 The returned representation is a TREE_LIST which TREE_PURPOSE is
22402 the scoped name of the attribute, and the TREE_VALUE is its
22403 arguments list.
22405 Note that the scoped name of the attribute is itself a TREE_LIST
22406 which TREE_PURPOSE is the namespace of the attribute, and
22407 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22408 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22409 and which TREE_PURPOSE is directly the attribute name.
22411 Clients of the attribute code should use get_attribute_namespace
22412 and get_attribute_name to get the actual namespace and name of
22413 attributes, regardless of their being GNU or C++11 attributes.
22415 attribute:
22416 attribute-token attribute-argument-clause [opt]
22418 attribute-token:
22419 identifier
22420 attribute-scoped-token
22422 attribute-scoped-token:
22423 attribute-namespace :: identifier
22425 attribute-namespace:
22426 identifier
22428 attribute-argument-clause:
22429 ( balanced-token-seq )
22431 balanced-token-seq:
22432 balanced-token [opt]
22433 balanced-token-seq balanced-token
22435 balanced-token:
22436 ( balanced-token-seq )
22437 [ balanced-token-seq ]
22438 { balanced-token-seq }. */
22440 static tree
22441 cp_parser_std_attribute (cp_parser *parser)
22443 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22444 cp_token *token;
22446 /* First, parse name of the the attribute, a.k.a
22447 attribute-token. */
22449 token = cp_lexer_peek_token (parser->lexer);
22450 if (token->type == CPP_NAME)
22451 attr_id = token->u.value;
22452 else if (token->type == CPP_KEYWORD)
22453 attr_id = ridpointers[(int) token->keyword];
22454 else if (token->flags & NAMED_OP)
22455 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22457 if (attr_id == NULL_TREE)
22458 return NULL_TREE;
22460 cp_lexer_consume_token (parser->lexer);
22462 token = cp_lexer_peek_token (parser->lexer);
22463 if (token->type == CPP_SCOPE)
22465 /* We are seeing a scoped attribute token. */
22467 cp_lexer_consume_token (parser->lexer);
22468 attr_ns = attr_id;
22470 token = cp_lexer_consume_token (parser->lexer);
22471 if (token->type == CPP_NAME)
22472 attr_id = token->u.value;
22473 else if (token->type == CPP_KEYWORD)
22474 attr_id = ridpointers[(int) token->keyword];
22475 else
22477 error_at (token->location,
22478 "expected an identifier for the attribute name");
22479 return error_mark_node;
22481 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22482 NULL_TREE);
22483 token = cp_lexer_peek_token (parser->lexer);
22485 else
22487 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22488 NULL_TREE);
22489 /* C++11 noreturn attribute is equivalent to GNU's. */
22490 if (is_attribute_p ("noreturn", attr_id))
22491 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22492 /* C++14 deprecated attribute is equivalent to GNU's. */
22493 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22495 if (cxx_dialect == cxx11)
22496 pedwarn (token->location, OPT_Wpedantic,
22497 "%<deprecated%> is a C++14 feature;"
22498 " use %<gnu::deprecated%>");
22499 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22503 /* Now parse the optional argument clause of the attribute. */
22505 if (token->type != CPP_OPEN_PAREN)
22506 return attribute;
22509 vec<tree, va_gc> *vec;
22510 int attr_flag = normal_attr;
22512 if (attr_ns == get_identifier ("gnu")
22513 && attribute_takes_identifier_p (attr_id))
22514 /* A GNU attribute that takes an identifier in parameter. */
22515 attr_flag = id_attr;
22517 vec = cp_parser_parenthesized_expression_list
22518 (parser, attr_flag, /*cast_p=*/false,
22519 /*allow_expansion_p=*/true,
22520 /*non_constant_p=*/NULL);
22521 if (vec == NULL)
22522 arguments = error_mark_node;
22523 else
22525 arguments = build_tree_list_vec (vec);
22526 release_tree_vector (vec);
22529 if (arguments == error_mark_node)
22530 attribute = error_mark_node;
22531 else
22532 TREE_VALUE (attribute) = arguments;
22535 return attribute;
22538 /* Check that the attribute ATTRIBUTE appears at most once in the
22539 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
22540 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
22541 isn't implemented yet in GCC. */
22543 static void
22544 cp_parser_check_std_attribute (tree attributes, tree attribute)
22546 if (attributes)
22548 tree name = get_attribute_name (attribute);
22549 if (is_attribute_p ("noreturn", name)
22550 && lookup_attribute ("noreturn", attributes))
22551 error ("attribute noreturn can appear at most once "
22552 "in an attribute-list");
22553 else if (is_attribute_p ("deprecated", name)
22554 && lookup_attribute ("deprecated", attributes))
22555 error ("attribute deprecated can appear at most once "
22556 "in an attribute-list");
22560 /* Parse a list of standard C++-11 attributes.
22562 attribute-list:
22563 attribute [opt]
22564 attribute-list , attribute[opt]
22565 attribute ...
22566 attribute-list , attribute ...
22569 static tree
22570 cp_parser_std_attribute_list (cp_parser *parser)
22572 tree attributes = NULL_TREE, attribute = NULL_TREE;
22573 cp_token *token = NULL;
22575 while (true)
22577 attribute = cp_parser_std_attribute (parser);
22578 if (attribute == error_mark_node)
22579 break;
22580 if (attribute != NULL_TREE)
22582 cp_parser_check_std_attribute (attributes, attribute);
22583 TREE_CHAIN (attribute) = attributes;
22584 attributes = attribute;
22586 token = cp_lexer_peek_token (parser->lexer);
22587 if (token->type == CPP_ELLIPSIS)
22589 cp_lexer_consume_token (parser->lexer);
22590 TREE_VALUE (attribute)
22591 = make_pack_expansion (TREE_VALUE (attribute));
22592 token = cp_lexer_peek_token (parser->lexer);
22594 if (token->type != CPP_COMMA)
22595 break;
22596 cp_lexer_consume_token (parser->lexer);
22598 attributes = nreverse (attributes);
22599 return attributes;
22602 /* Parse a standard C++-11 attribute specifier.
22604 attribute-specifier:
22605 [ [ attribute-list ] ]
22606 alignment-specifier
22608 alignment-specifier:
22609 alignas ( type-id ... [opt] )
22610 alignas ( alignment-expression ... [opt] ). */
22612 static tree
22613 cp_parser_std_attribute_spec (cp_parser *parser)
22615 tree attributes = NULL_TREE;
22616 cp_token *token = cp_lexer_peek_token (parser->lexer);
22618 if (token->type == CPP_OPEN_SQUARE
22619 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22621 cp_lexer_consume_token (parser->lexer);
22622 cp_lexer_consume_token (parser->lexer);
22624 attributes = cp_parser_std_attribute_list (parser);
22626 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22627 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22628 cp_parser_skip_to_end_of_statement (parser);
22629 else
22630 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22631 when we are sure that we have actually parsed them. */
22632 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22634 else
22636 tree alignas_expr;
22638 /* Look for an alignment-specifier. */
22640 token = cp_lexer_peek_token (parser->lexer);
22642 if (token->type != CPP_KEYWORD
22643 || token->keyword != RID_ALIGNAS)
22644 return NULL_TREE;
22646 cp_lexer_consume_token (parser->lexer);
22647 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22649 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22651 cp_parser_error (parser, "expected %<(%>");
22652 return error_mark_node;
22655 cp_parser_parse_tentatively (parser);
22656 alignas_expr = cp_parser_type_id (parser);
22658 if (!cp_parser_parse_definitely (parser))
22660 gcc_assert (alignas_expr == error_mark_node
22661 || alignas_expr == NULL_TREE);
22663 alignas_expr =
22664 cp_parser_assignment_expression (parser);
22665 if (alignas_expr == error_mark_node)
22666 cp_parser_skip_to_end_of_statement (parser);
22667 if (alignas_expr == NULL_TREE
22668 || alignas_expr == error_mark_node)
22669 return alignas_expr;
22672 alignas_expr = cxx_alignas_expr (alignas_expr);
22673 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22675 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22677 cp_lexer_consume_token (parser->lexer);
22678 alignas_expr = make_pack_expansion (alignas_expr);
22681 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22683 cp_parser_error (parser, "expected %<)%>");
22684 return error_mark_node;
22687 /* Build the C++-11 representation of an 'aligned'
22688 attribute. */
22689 attributes =
22690 build_tree_list (build_tree_list (get_identifier ("gnu"),
22691 get_identifier ("aligned")),
22692 alignas_expr);
22695 return attributes;
22698 /* Parse a standard C++-11 attribute-specifier-seq.
22700 attribute-specifier-seq:
22701 attribute-specifier-seq [opt] attribute-specifier
22704 static tree
22705 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22707 tree attr_specs = NULL;
22709 while (true)
22711 tree attr_spec = cp_parser_std_attribute_spec (parser);
22712 if (attr_spec == NULL_TREE)
22713 break;
22714 if (attr_spec == error_mark_node)
22715 return error_mark_node;
22717 TREE_CHAIN (attr_spec) = attr_specs;
22718 attr_specs = attr_spec;
22721 attr_specs = nreverse (attr_specs);
22722 return attr_specs;
22725 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22726 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22727 current value of the PEDANTIC flag, regardless of whether or not
22728 the `__extension__' keyword is present. The caller is responsible
22729 for restoring the value of the PEDANTIC flag. */
22731 static bool
22732 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22734 /* Save the old value of the PEDANTIC flag. */
22735 *saved_pedantic = pedantic;
22737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22739 /* Consume the `__extension__' token. */
22740 cp_lexer_consume_token (parser->lexer);
22741 /* We're not being pedantic while the `__extension__' keyword is
22742 in effect. */
22743 pedantic = 0;
22745 return true;
22748 return false;
22751 /* Parse a label declaration.
22753 label-declaration:
22754 __label__ label-declarator-seq ;
22756 label-declarator-seq:
22757 identifier , label-declarator-seq
22758 identifier */
22760 static void
22761 cp_parser_label_declaration (cp_parser* parser)
22763 /* Look for the `__label__' keyword. */
22764 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22766 while (true)
22768 tree identifier;
22770 /* Look for an identifier. */
22771 identifier = cp_parser_identifier (parser);
22772 /* If we failed, stop. */
22773 if (identifier == error_mark_node)
22774 break;
22775 /* Declare it as a label. */
22776 finish_label_decl (identifier);
22777 /* If the next token is a `;', stop. */
22778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22779 break;
22780 /* Look for the `,' separating the label declarations. */
22781 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22784 /* Look for the final `;'. */
22785 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22788 /* Support Functions */
22790 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22791 NAME should have one of the representations used for an
22792 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22793 is returned. If PARSER->SCOPE is a dependent type, then a
22794 SCOPE_REF is returned.
22796 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22797 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22798 was formed. Abstractly, such entities should not be passed to this
22799 function, because they do not need to be looked up, but it is
22800 simpler to check for this special case here, rather than at the
22801 call-sites.
22803 In cases not explicitly covered above, this function returns a
22804 DECL, OVERLOAD, or baselink representing the result of the lookup.
22805 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22806 is returned.
22808 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22809 (e.g., "struct") that was used. In that case bindings that do not
22810 refer to types are ignored.
22812 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22813 ignored.
22815 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22816 are ignored.
22818 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22819 types.
22821 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22822 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22823 NULL_TREE otherwise. */
22825 static tree
22826 cp_parser_lookup_name (cp_parser *parser, tree name,
22827 enum tag_types tag_type,
22828 bool is_template,
22829 bool is_namespace,
22830 bool check_dependency,
22831 tree *ambiguous_decls,
22832 location_t name_location)
22834 tree decl;
22835 tree object_type = parser->context->object_type;
22837 /* Assume that the lookup will be unambiguous. */
22838 if (ambiguous_decls)
22839 *ambiguous_decls = NULL_TREE;
22841 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22842 no longer valid. Note that if we are parsing tentatively, and
22843 the parse fails, OBJECT_TYPE will be automatically restored. */
22844 parser->context->object_type = NULL_TREE;
22846 if (name == error_mark_node)
22847 return error_mark_node;
22849 /* A template-id has already been resolved; there is no lookup to
22850 do. */
22851 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22852 return name;
22853 if (BASELINK_P (name))
22855 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22856 == TEMPLATE_ID_EXPR);
22857 return name;
22860 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22861 it should already have been checked to make sure that the name
22862 used matches the type being destroyed. */
22863 if (TREE_CODE (name) == BIT_NOT_EXPR)
22865 tree type;
22867 /* Figure out to which type this destructor applies. */
22868 if (parser->scope)
22869 type = parser->scope;
22870 else if (object_type)
22871 type = object_type;
22872 else
22873 type = current_class_type;
22874 /* If that's not a class type, there is no destructor. */
22875 if (!type || !CLASS_TYPE_P (type))
22876 return error_mark_node;
22877 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22878 lazily_declare_fn (sfk_destructor, type);
22879 if (!CLASSTYPE_DESTRUCTORS (type))
22880 return error_mark_node;
22881 /* If it was a class type, return the destructor. */
22882 return CLASSTYPE_DESTRUCTORS (type);
22885 /* By this point, the NAME should be an ordinary identifier. If
22886 the id-expression was a qualified name, the qualifying scope is
22887 stored in PARSER->SCOPE at this point. */
22888 gcc_assert (identifier_p (name));
22890 /* Perform the lookup. */
22891 if (parser->scope)
22893 bool dependent_p;
22895 if (parser->scope == error_mark_node)
22896 return error_mark_node;
22898 /* If the SCOPE is dependent, the lookup must be deferred until
22899 the template is instantiated -- unless we are explicitly
22900 looking up names in uninstantiated templates. Even then, we
22901 cannot look up the name if the scope is not a class type; it
22902 might, for example, be a template type parameter. */
22903 dependent_p = (TYPE_P (parser->scope)
22904 && dependent_scope_p (parser->scope));
22905 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22906 && dependent_p)
22907 /* Defer lookup. */
22908 decl = error_mark_node;
22909 else
22911 tree pushed_scope = NULL_TREE;
22913 /* If PARSER->SCOPE is a dependent type, then it must be a
22914 class type, and we must not be checking dependencies;
22915 otherwise, we would have processed this lookup above. So
22916 that PARSER->SCOPE is not considered a dependent base by
22917 lookup_member, we must enter the scope here. */
22918 if (dependent_p)
22919 pushed_scope = push_scope (parser->scope);
22921 /* If the PARSER->SCOPE is a template specialization, it
22922 may be instantiated during name lookup. In that case,
22923 errors may be issued. Even if we rollback the current
22924 tentative parse, those errors are valid. */
22925 decl = lookup_qualified_name (parser->scope, name,
22926 tag_type != none_type,
22927 /*complain=*/true);
22929 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22930 lookup result and the nested-name-specifier nominates a class C:
22931 * if the name specified after the nested-name-specifier, when
22932 looked up in C, is the injected-class-name of C (Clause 9), or
22933 * if the name specified after the nested-name-specifier is the
22934 same as the identifier or the simple-template-id's template-
22935 name in the last component of the nested-name-specifier,
22936 the name is instead considered to name the constructor of
22937 class C. [ Note: for example, the constructor is not an
22938 acceptable lookup result in an elaborated-type-specifier so
22939 the constructor would not be used in place of the
22940 injected-class-name. --end note ] Such a constructor name
22941 shall be used only in the declarator-id of a declaration that
22942 names a constructor or in a using-declaration. */
22943 if (tag_type == none_type
22944 && DECL_SELF_REFERENCE_P (decl)
22945 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22946 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22947 tag_type != none_type,
22948 /*complain=*/true);
22950 /* If we have a single function from a using decl, pull it out. */
22951 if (TREE_CODE (decl) == OVERLOAD
22952 && !really_overloaded_fn (decl))
22953 decl = OVL_FUNCTION (decl);
22955 if (pushed_scope)
22956 pop_scope (pushed_scope);
22959 /* If the scope is a dependent type and either we deferred lookup or
22960 we did lookup but didn't find the name, rememeber the name. */
22961 if (decl == error_mark_node && TYPE_P (parser->scope)
22962 && dependent_type_p (parser->scope))
22964 if (tag_type)
22966 tree type;
22968 /* The resolution to Core Issue 180 says that `struct
22969 A::B' should be considered a type-name, even if `A'
22970 is dependent. */
22971 type = make_typename_type (parser->scope, name, tag_type,
22972 /*complain=*/tf_error);
22973 if (type != error_mark_node)
22974 decl = TYPE_NAME (type);
22976 else if (is_template
22977 && (cp_parser_next_token_ends_template_argument_p (parser)
22978 || cp_lexer_next_token_is (parser->lexer,
22979 CPP_CLOSE_PAREN)))
22980 decl = make_unbound_class_template (parser->scope,
22981 name, NULL_TREE,
22982 /*complain=*/tf_error);
22983 else
22984 decl = build_qualified_name (/*type=*/NULL_TREE,
22985 parser->scope, name,
22986 is_template);
22988 parser->qualifying_scope = parser->scope;
22989 parser->object_scope = NULL_TREE;
22991 else if (object_type)
22993 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22994 OBJECT_TYPE is not a class. */
22995 if (CLASS_TYPE_P (object_type))
22996 /* If the OBJECT_TYPE is a template specialization, it may
22997 be instantiated during name lookup. In that case, errors
22998 may be issued. Even if we rollback the current tentative
22999 parse, those errors are valid. */
23000 decl = lookup_member (object_type,
23001 name,
23002 /*protect=*/0,
23003 tag_type != none_type,
23004 tf_warning_or_error);
23005 else
23006 decl = NULL_TREE;
23008 if (!decl)
23009 /* Look it up in the enclosing context. */
23010 decl = lookup_name_real (name, tag_type != none_type,
23011 /*nonclass=*/0,
23012 /*block_p=*/true, is_namespace, 0);
23013 parser->object_scope = object_type;
23014 parser->qualifying_scope = NULL_TREE;
23016 else
23018 decl = lookup_name_real (name, tag_type != none_type,
23019 /*nonclass=*/0,
23020 /*block_p=*/true, is_namespace, 0);
23021 parser->qualifying_scope = NULL_TREE;
23022 parser->object_scope = NULL_TREE;
23025 /* If the lookup failed, let our caller know. */
23026 if (!decl || decl == error_mark_node)
23027 return error_mark_node;
23029 /* Pull out the template from an injected-class-name (or multiple). */
23030 if (is_template)
23031 decl = maybe_get_template_decl_from_type_decl (decl);
23033 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23034 if (TREE_CODE (decl) == TREE_LIST)
23036 if (ambiguous_decls)
23037 *ambiguous_decls = decl;
23038 /* The error message we have to print is too complicated for
23039 cp_parser_error, so we incorporate its actions directly. */
23040 if (!cp_parser_simulate_error (parser))
23042 error_at (name_location, "reference to %qD is ambiguous",
23043 name);
23044 print_candidates (decl);
23046 return error_mark_node;
23049 gcc_assert (DECL_P (decl)
23050 || TREE_CODE (decl) == OVERLOAD
23051 || TREE_CODE (decl) == SCOPE_REF
23052 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23053 || BASELINK_P (decl));
23055 /* If we have resolved the name of a member declaration, check to
23056 see if the declaration is accessible. When the name resolves to
23057 set of overloaded functions, accessibility is checked when
23058 overload resolution is done.
23060 During an explicit instantiation, access is not checked at all,
23061 as per [temp.explicit]. */
23062 if (DECL_P (decl))
23063 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23065 maybe_record_typedef_use (decl);
23067 return decl;
23070 /* Like cp_parser_lookup_name, but for use in the typical case where
23071 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23072 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23074 static tree
23075 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23077 return cp_parser_lookup_name (parser, name,
23078 none_type,
23079 /*is_template=*/false,
23080 /*is_namespace=*/false,
23081 /*check_dependency=*/true,
23082 /*ambiguous_decls=*/NULL,
23083 location);
23086 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23087 the current context, return the TYPE_DECL. If TAG_NAME_P is
23088 true, the DECL indicates the class being defined in a class-head,
23089 or declared in an elaborated-type-specifier.
23091 Otherwise, return DECL. */
23093 static tree
23094 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23096 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23097 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23099 struct A {
23100 template <typename T> struct B;
23103 template <typename T> struct A::B {};
23105 Similarly, in an elaborated-type-specifier:
23107 namespace N { struct X{}; }
23109 struct A {
23110 template <typename T> friend struct N::X;
23113 However, if the DECL refers to a class type, and we are in
23114 the scope of the class, then the name lookup automatically
23115 finds the TYPE_DECL created by build_self_reference rather
23116 than a TEMPLATE_DECL. For example, in:
23118 template <class T> struct S {
23119 S s;
23122 there is no need to handle such case. */
23124 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23125 return DECL_TEMPLATE_RESULT (decl);
23127 return decl;
23130 /* If too many, or too few, template-parameter lists apply to the
23131 declarator, issue an error message. Returns TRUE if all went well,
23132 and FALSE otherwise. */
23134 static bool
23135 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23136 cp_declarator *declarator,
23137 location_t declarator_location)
23139 switch (declarator->kind)
23141 case cdk_id:
23143 unsigned num_templates = 0;
23144 tree scope = declarator->u.id.qualifying_scope;
23146 if (scope)
23147 num_templates = num_template_headers_for_class (scope);
23148 else if (TREE_CODE (declarator->u.id.unqualified_name)
23149 == TEMPLATE_ID_EXPR)
23150 /* If the DECLARATOR has the form `X<y>' then it uses one
23151 additional level of template parameters. */
23152 ++num_templates;
23154 return cp_parser_check_template_parameters
23155 (parser, num_templates, declarator_location, declarator);
23158 case cdk_function:
23159 case cdk_array:
23160 case cdk_pointer:
23161 case cdk_reference:
23162 case cdk_ptrmem:
23163 return (cp_parser_check_declarator_template_parameters
23164 (parser, declarator->declarator, declarator_location));
23166 case cdk_error:
23167 return true;
23169 default:
23170 gcc_unreachable ();
23172 return false;
23175 /* NUM_TEMPLATES were used in the current declaration. If that is
23176 invalid, return FALSE and issue an error messages. Otherwise,
23177 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23178 declarator and we can print more accurate diagnostics. */
23180 static bool
23181 cp_parser_check_template_parameters (cp_parser* parser,
23182 unsigned num_templates,
23183 location_t location,
23184 cp_declarator *declarator)
23186 /* If there are the same number of template classes and parameter
23187 lists, that's OK. */
23188 if (parser->num_template_parameter_lists == num_templates)
23189 return true;
23190 /* If there are more, but only one more, then we are referring to a
23191 member template. That's OK too. */
23192 if (parser->num_template_parameter_lists == num_templates + 1)
23193 return true;
23194 /* If there are more template classes than parameter lists, we have
23195 something like:
23197 template <class T> void S<T>::R<T>::f (); */
23198 if (parser->num_template_parameter_lists < num_templates)
23200 if (declarator && !current_function_decl)
23201 error_at (location, "specializing member %<%T::%E%> "
23202 "requires %<template<>%> syntax",
23203 declarator->u.id.qualifying_scope,
23204 declarator->u.id.unqualified_name);
23205 else if (declarator)
23206 error_at (location, "invalid declaration of %<%T::%E%>",
23207 declarator->u.id.qualifying_scope,
23208 declarator->u.id.unqualified_name);
23209 else
23210 error_at (location, "too few template-parameter-lists");
23211 return false;
23213 /* Otherwise, there are too many template parameter lists. We have
23214 something like:
23216 template <class T> template <class U> void S::f(); */
23217 error_at (location, "too many template-parameter-lists");
23218 return false;
23221 /* Parse an optional `::' token indicating that the following name is
23222 from the global namespace. If so, PARSER->SCOPE is set to the
23223 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23224 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23225 Returns the new value of PARSER->SCOPE, if the `::' token is
23226 present, and NULL_TREE otherwise. */
23228 static tree
23229 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23231 cp_token *token;
23233 /* Peek at the next token. */
23234 token = cp_lexer_peek_token (parser->lexer);
23235 /* If we're looking at a `::' token then we're starting from the
23236 global namespace, not our current location. */
23237 if (token->type == CPP_SCOPE)
23239 /* Consume the `::' token. */
23240 cp_lexer_consume_token (parser->lexer);
23241 /* Set the SCOPE so that we know where to start the lookup. */
23242 parser->scope = global_namespace;
23243 parser->qualifying_scope = global_namespace;
23244 parser->object_scope = NULL_TREE;
23246 return parser->scope;
23248 else if (!current_scope_valid_p)
23250 parser->scope = NULL_TREE;
23251 parser->qualifying_scope = NULL_TREE;
23252 parser->object_scope = NULL_TREE;
23255 return NULL_TREE;
23258 /* Returns TRUE if the upcoming token sequence is the start of a
23259 constructor declarator. If FRIEND_P is true, the declarator is
23260 preceded by the `friend' specifier. */
23262 static bool
23263 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23265 bool constructor_p;
23266 bool outside_class_specifier_p;
23267 tree nested_name_specifier;
23268 cp_token *next_token;
23270 /* The common case is that this is not a constructor declarator, so
23271 try to avoid doing lots of work if at all possible. It's not
23272 valid declare a constructor at function scope. */
23273 if (parser->in_function_body)
23274 return false;
23275 /* And only certain tokens can begin a constructor declarator. */
23276 next_token = cp_lexer_peek_token (parser->lexer);
23277 if (next_token->type != CPP_NAME
23278 && next_token->type != CPP_SCOPE
23279 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23280 && next_token->type != CPP_TEMPLATE_ID)
23281 return false;
23283 /* Parse tentatively; we are going to roll back all of the tokens
23284 consumed here. */
23285 cp_parser_parse_tentatively (parser);
23286 /* Assume that we are looking at a constructor declarator. */
23287 constructor_p = true;
23289 /* Look for the optional `::' operator. */
23290 cp_parser_global_scope_opt (parser,
23291 /*current_scope_valid_p=*/false);
23292 /* Look for the nested-name-specifier. */
23293 nested_name_specifier
23294 = (cp_parser_nested_name_specifier_opt (parser,
23295 /*typename_keyword_p=*/false,
23296 /*check_dependency_p=*/false,
23297 /*type_p=*/false,
23298 /*is_declaration=*/false));
23300 outside_class_specifier_p = (!at_class_scope_p ()
23301 || !TYPE_BEING_DEFINED (current_class_type)
23302 || friend_p);
23304 /* Outside of a class-specifier, there must be a
23305 nested-name-specifier. */
23306 if (!nested_name_specifier && outside_class_specifier_p)
23307 constructor_p = false;
23308 else if (nested_name_specifier == error_mark_node)
23309 constructor_p = false;
23311 /* If we have a class scope, this is easy; DR 147 says that S::S always
23312 names the constructor, and no other qualified name could. */
23313 if (constructor_p && nested_name_specifier
23314 && CLASS_TYPE_P (nested_name_specifier))
23316 tree id = cp_parser_unqualified_id (parser,
23317 /*template_keyword_p=*/false,
23318 /*check_dependency_p=*/false,
23319 /*declarator_p=*/true,
23320 /*optional_p=*/false);
23321 if (is_overloaded_fn (id))
23322 id = DECL_NAME (get_first_fn (id));
23323 if (!constructor_name_p (id, nested_name_specifier))
23324 constructor_p = false;
23326 /* If we still think that this might be a constructor-declarator,
23327 look for a class-name. */
23328 else if (constructor_p)
23330 /* If we have:
23332 template <typename T> struct S {
23333 S();
23336 we must recognize that the nested `S' names a class. */
23337 tree type_decl;
23338 type_decl = cp_parser_class_name (parser,
23339 /*typename_keyword_p=*/false,
23340 /*template_keyword_p=*/false,
23341 none_type,
23342 /*check_dependency_p=*/false,
23343 /*class_head_p=*/false,
23344 /*is_declaration=*/false);
23345 /* If there was no class-name, then this is not a constructor.
23346 Otherwise, if we are in a class-specifier and we aren't
23347 handling a friend declaration, check that its type matches
23348 current_class_type (c++/38313). Note: error_mark_node
23349 is left alone for error recovery purposes. */
23350 constructor_p = (!cp_parser_error_occurred (parser)
23351 && (outside_class_specifier_p
23352 || type_decl == error_mark_node
23353 || same_type_p (current_class_type,
23354 TREE_TYPE (type_decl))));
23356 /* If we're still considering a constructor, we have to see a `(',
23357 to begin the parameter-declaration-clause, followed by either a
23358 `)', an `...', or a decl-specifier. We need to check for a
23359 type-specifier to avoid being fooled into thinking that:
23361 S (f) (int);
23363 is a constructor. (It is actually a function named `f' that
23364 takes one parameter (of type `int') and returns a value of type
23365 `S'. */
23366 if (constructor_p
23367 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23368 constructor_p = false;
23370 if (constructor_p
23371 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23372 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23373 /* A parameter declaration begins with a decl-specifier,
23374 which is either the "attribute" keyword, a storage class
23375 specifier, or (usually) a type-specifier. */
23376 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23378 tree type;
23379 tree pushed_scope = NULL_TREE;
23380 unsigned saved_num_template_parameter_lists;
23382 /* Names appearing in the type-specifier should be looked up
23383 in the scope of the class. */
23384 if (current_class_type)
23385 type = NULL_TREE;
23386 else
23388 type = TREE_TYPE (type_decl);
23389 if (TREE_CODE (type) == TYPENAME_TYPE)
23391 type = resolve_typename_type (type,
23392 /*only_current_p=*/false);
23393 if (TREE_CODE (type) == TYPENAME_TYPE)
23395 cp_parser_abort_tentative_parse (parser);
23396 return false;
23399 pushed_scope = push_scope (type);
23402 /* Inside the constructor parameter list, surrounding
23403 template-parameter-lists do not apply. */
23404 saved_num_template_parameter_lists
23405 = parser->num_template_parameter_lists;
23406 parser->num_template_parameter_lists = 0;
23408 /* Look for the type-specifier. */
23409 cp_parser_type_specifier (parser,
23410 CP_PARSER_FLAGS_NONE,
23411 /*decl_specs=*/NULL,
23412 /*is_declarator=*/true,
23413 /*declares_class_or_enum=*/NULL,
23414 /*is_cv_qualifier=*/NULL);
23416 parser->num_template_parameter_lists
23417 = saved_num_template_parameter_lists;
23419 /* Leave the scope of the class. */
23420 if (pushed_scope)
23421 pop_scope (pushed_scope);
23423 constructor_p = !cp_parser_error_occurred (parser);
23427 /* We did not really want to consume any tokens. */
23428 cp_parser_abort_tentative_parse (parser);
23430 return constructor_p;
23433 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23434 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23435 they must be performed once we are in the scope of the function.
23437 Returns the function defined. */
23439 static tree
23440 cp_parser_function_definition_from_specifiers_and_declarator
23441 (cp_parser* parser,
23442 cp_decl_specifier_seq *decl_specifiers,
23443 tree attributes,
23444 const cp_declarator *declarator)
23446 tree fn;
23447 bool success_p;
23449 /* Begin the function-definition. */
23450 success_p = start_function (decl_specifiers, declarator, attributes);
23452 /* The things we're about to see are not directly qualified by any
23453 template headers we've seen thus far. */
23454 reset_specialization ();
23456 /* If there were names looked up in the decl-specifier-seq that we
23457 did not check, check them now. We must wait until we are in the
23458 scope of the function to perform the checks, since the function
23459 might be a friend. */
23460 perform_deferred_access_checks (tf_warning_or_error);
23462 if (success_p)
23464 cp_finalize_omp_declare_simd (parser, current_function_decl);
23465 parser->omp_declare_simd = NULL;
23468 if (!success_p)
23470 /* Skip the entire function. */
23471 cp_parser_skip_to_end_of_block_or_statement (parser);
23472 fn = error_mark_node;
23474 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23476 /* Seen already, skip it. An error message has already been output. */
23477 cp_parser_skip_to_end_of_block_or_statement (parser);
23478 fn = current_function_decl;
23479 current_function_decl = NULL_TREE;
23480 /* If this is a function from a class, pop the nested class. */
23481 if (current_class_name)
23482 pop_nested_class ();
23484 else
23486 timevar_id_t tv;
23487 if (DECL_DECLARED_INLINE_P (current_function_decl))
23488 tv = TV_PARSE_INLINE;
23489 else
23490 tv = TV_PARSE_FUNC;
23491 timevar_push (tv);
23492 fn = cp_parser_function_definition_after_declarator (parser,
23493 /*inline_p=*/false);
23494 timevar_pop (tv);
23497 return fn;
23500 /* Parse the part of a function-definition that follows the
23501 declarator. INLINE_P is TRUE iff this function is an inline
23502 function defined within a class-specifier.
23504 Returns the function defined. */
23506 static tree
23507 cp_parser_function_definition_after_declarator (cp_parser* parser,
23508 bool inline_p)
23510 tree fn;
23511 bool ctor_initializer_p = false;
23512 bool saved_in_unbraced_linkage_specification_p;
23513 bool saved_in_function_body;
23514 unsigned saved_num_template_parameter_lists;
23515 cp_token *token;
23516 bool fully_implicit_function_template_p
23517 = parser->fully_implicit_function_template_p;
23518 parser->fully_implicit_function_template_p = false;
23519 tree implicit_template_parms
23520 = parser->implicit_template_parms;
23521 parser->implicit_template_parms = 0;
23522 cp_binding_level* implicit_template_scope
23523 = parser->implicit_template_scope;
23524 parser->implicit_template_scope = 0;
23526 saved_in_function_body = parser->in_function_body;
23527 parser->in_function_body = true;
23528 /* If the next token is `return', then the code may be trying to
23529 make use of the "named return value" extension that G++ used to
23530 support. */
23531 token = cp_lexer_peek_token (parser->lexer);
23532 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23534 /* Consume the `return' keyword. */
23535 cp_lexer_consume_token (parser->lexer);
23536 /* Look for the identifier that indicates what value is to be
23537 returned. */
23538 cp_parser_identifier (parser);
23539 /* Issue an error message. */
23540 error_at (token->location,
23541 "named return values are no longer supported");
23542 /* Skip tokens until we reach the start of the function body. */
23543 while (true)
23545 cp_token *token = cp_lexer_peek_token (parser->lexer);
23546 if (token->type == CPP_OPEN_BRACE
23547 || token->type == CPP_EOF
23548 || token->type == CPP_PRAGMA_EOL)
23549 break;
23550 cp_lexer_consume_token (parser->lexer);
23553 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23554 anything declared inside `f'. */
23555 saved_in_unbraced_linkage_specification_p
23556 = parser->in_unbraced_linkage_specification_p;
23557 parser->in_unbraced_linkage_specification_p = false;
23558 /* Inside the function, surrounding template-parameter-lists do not
23559 apply. */
23560 saved_num_template_parameter_lists
23561 = parser->num_template_parameter_lists;
23562 parser->num_template_parameter_lists = 0;
23564 start_lambda_scope (current_function_decl);
23566 /* If the next token is `try', `__transaction_atomic', or
23567 `__transaction_relaxed`, then we are looking at either function-try-block
23568 or function-transaction-block. Note that all of these include the
23569 function-body. */
23570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23571 ctor_initializer_p = cp_parser_function_transaction (parser,
23572 RID_TRANSACTION_ATOMIC);
23573 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23574 RID_TRANSACTION_RELAXED))
23575 ctor_initializer_p = cp_parser_function_transaction (parser,
23576 RID_TRANSACTION_RELAXED);
23577 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23578 ctor_initializer_p = cp_parser_function_try_block (parser);
23579 else
23580 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23581 (parser, /*in_function_try_block=*/false);
23583 finish_lambda_scope ();
23585 /* Finish the function. */
23586 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23587 (inline_p ? 2 : 0));
23588 /* Generate code for it, if necessary. */
23589 expand_or_defer_fn (fn);
23590 /* Restore the saved values. */
23591 parser->in_unbraced_linkage_specification_p
23592 = saved_in_unbraced_linkage_specification_p;
23593 parser->num_template_parameter_lists
23594 = saved_num_template_parameter_lists;
23595 parser->in_function_body = saved_in_function_body;
23597 parser->fully_implicit_function_template_p
23598 = fully_implicit_function_template_p;
23599 parser->implicit_template_parms
23600 = implicit_template_parms;
23601 parser->implicit_template_scope
23602 = implicit_template_scope;
23604 if (parser->fully_implicit_function_template_p)
23605 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23607 return fn;
23610 /* Parse a template-declaration, assuming that the `export' (and
23611 `extern') keywords, if present, has already been scanned. MEMBER_P
23612 is as for cp_parser_template_declaration. */
23614 static void
23615 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23617 tree decl = NULL_TREE;
23618 vec<deferred_access_check, va_gc> *checks;
23619 tree parameter_list;
23620 bool friend_p = false;
23621 bool need_lang_pop;
23622 cp_token *token;
23624 /* Look for the `template' keyword. */
23625 token = cp_lexer_peek_token (parser->lexer);
23626 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23627 return;
23629 /* And the `<'. */
23630 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23631 return;
23632 if (at_class_scope_p () && current_function_decl)
23634 /* 14.5.2.2 [temp.mem]
23636 A local class shall not have member templates. */
23637 error_at (token->location,
23638 "invalid declaration of member template in local class");
23639 cp_parser_skip_to_end_of_block_or_statement (parser);
23640 return;
23642 /* [temp]
23644 A template ... shall not have C linkage. */
23645 if (current_lang_name == lang_name_c)
23647 error_at (token->location, "template with C linkage");
23648 /* Give it C++ linkage to avoid confusing other parts of the
23649 front end. */
23650 push_lang_context (lang_name_cplusplus);
23651 need_lang_pop = true;
23653 else
23654 need_lang_pop = false;
23656 /* We cannot perform access checks on the template parameter
23657 declarations until we know what is being declared, just as we
23658 cannot check the decl-specifier list. */
23659 push_deferring_access_checks (dk_deferred);
23661 /* If the next token is `>', then we have an invalid
23662 specialization. Rather than complain about an invalid template
23663 parameter, issue an error message here. */
23664 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23666 cp_parser_error (parser, "invalid explicit specialization");
23667 begin_specialization ();
23668 parameter_list = NULL_TREE;
23670 else
23672 /* Parse the template parameters. */
23673 parameter_list = cp_parser_template_parameter_list (parser);
23676 /* Get the deferred access checks from the parameter list. These
23677 will be checked once we know what is being declared, as for a
23678 member template the checks must be performed in the scope of the
23679 class containing the member. */
23680 checks = get_deferred_access_checks ();
23682 /* Look for the `>'. */
23683 cp_parser_skip_to_end_of_template_parameter_list (parser);
23684 /* We just processed one more parameter list. */
23685 ++parser->num_template_parameter_lists;
23686 /* If the next token is `template', there are more template
23687 parameters. */
23688 if (cp_lexer_next_token_is_keyword (parser->lexer,
23689 RID_TEMPLATE))
23690 cp_parser_template_declaration_after_export (parser, member_p);
23691 else if (cxx_dialect >= cxx11
23692 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23693 decl = cp_parser_alias_declaration (parser);
23694 else
23696 /* There are no access checks when parsing a template, as we do not
23697 know if a specialization will be a friend. */
23698 push_deferring_access_checks (dk_no_check);
23699 token = cp_lexer_peek_token (parser->lexer);
23700 decl = cp_parser_single_declaration (parser,
23701 checks,
23702 member_p,
23703 /*explicit_specialization_p=*/false,
23704 &friend_p);
23705 pop_deferring_access_checks ();
23707 /* If this is a member template declaration, let the front
23708 end know. */
23709 if (member_p && !friend_p && decl)
23711 if (TREE_CODE (decl) == TYPE_DECL)
23712 cp_parser_check_access_in_redeclaration (decl, token->location);
23714 decl = finish_member_template_decl (decl);
23716 else if (friend_p && decl
23717 && DECL_DECLARES_TYPE_P (decl))
23718 make_friend_class (current_class_type, TREE_TYPE (decl),
23719 /*complain=*/true);
23721 /* We are done with the current parameter list. */
23722 --parser->num_template_parameter_lists;
23724 pop_deferring_access_checks ();
23726 /* Finish up. */
23727 finish_template_decl (parameter_list);
23729 /* Check the template arguments for a literal operator template. */
23730 if (decl
23731 && DECL_DECLARES_FUNCTION_P (decl)
23732 && UDLIT_OPER_P (DECL_NAME (decl)))
23734 bool ok = true;
23735 if (parameter_list == NULL_TREE)
23736 ok = false;
23737 else
23739 int num_parms = TREE_VEC_LENGTH (parameter_list);
23740 if (num_parms == 1)
23742 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23743 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23744 if (TREE_TYPE (parm) != char_type_node
23745 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23746 ok = false;
23748 else if (num_parms == 2 && cxx_dialect >= cxx14)
23750 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23751 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23752 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23753 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23754 if (TREE_TYPE (parm) != TREE_TYPE (type)
23755 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23756 ok = false;
23758 else
23759 ok = false;
23761 if (!ok)
23763 if (cxx_dialect >= cxx14)
23764 error ("literal operator template %qD has invalid parameter list."
23765 " Expected non-type template argument pack <char...>"
23766 " or <typename CharT, CharT...>",
23767 decl);
23768 else
23769 error ("literal operator template %qD has invalid parameter list."
23770 " Expected non-type template argument pack <char...>",
23771 decl);
23774 /* Register member declarations. */
23775 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23776 finish_member_declaration (decl);
23777 /* For the erroneous case of a template with C linkage, we pushed an
23778 implicit C++ linkage scope; exit that scope now. */
23779 if (need_lang_pop)
23780 pop_lang_context ();
23781 /* If DECL is a function template, we must return to parse it later.
23782 (Even though there is no definition, there might be default
23783 arguments that need handling.) */
23784 if (member_p && decl
23785 && DECL_DECLARES_FUNCTION_P (decl))
23786 vec_safe_push (unparsed_funs_with_definitions, decl);
23789 /* Perform the deferred access checks from a template-parameter-list.
23790 CHECKS is a TREE_LIST of access checks, as returned by
23791 get_deferred_access_checks. */
23793 static void
23794 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23796 ++processing_template_parmlist;
23797 perform_access_checks (checks, tf_warning_or_error);
23798 --processing_template_parmlist;
23801 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23802 `function-definition' sequence that follows a template header.
23803 If MEMBER_P is true, this declaration appears in a class scope.
23805 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23806 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23808 static tree
23809 cp_parser_single_declaration (cp_parser* parser,
23810 vec<deferred_access_check, va_gc> *checks,
23811 bool member_p,
23812 bool explicit_specialization_p,
23813 bool* friend_p)
23815 int declares_class_or_enum;
23816 tree decl = NULL_TREE;
23817 cp_decl_specifier_seq decl_specifiers;
23818 bool function_definition_p = false;
23819 cp_token *decl_spec_token_start;
23821 /* This function is only used when processing a template
23822 declaration. */
23823 gcc_assert (innermost_scope_kind () == sk_template_parms
23824 || innermost_scope_kind () == sk_template_spec);
23826 /* Defer access checks until we know what is being declared. */
23827 push_deferring_access_checks (dk_deferred);
23829 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23830 alternative. */
23831 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23832 cp_parser_decl_specifier_seq (parser,
23833 CP_PARSER_FLAGS_OPTIONAL,
23834 &decl_specifiers,
23835 &declares_class_or_enum);
23836 if (friend_p)
23837 *friend_p = cp_parser_friend_p (&decl_specifiers);
23839 /* There are no template typedefs. */
23840 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23842 error_at (decl_spec_token_start->location,
23843 "template declaration of %<typedef%>");
23844 decl = error_mark_node;
23847 /* Gather up the access checks that occurred the
23848 decl-specifier-seq. */
23849 stop_deferring_access_checks ();
23851 /* Check for the declaration of a template class. */
23852 if (declares_class_or_enum)
23854 if (cp_parser_declares_only_class_p (parser))
23856 decl = shadow_tag (&decl_specifiers);
23858 /* In this case:
23860 struct C {
23861 friend template <typename T> struct A<T>::B;
23864 A<T>::B will be represented by a TYPENAME_TYPE, and
23865 therefore not recognized by shadow_tag. */
23866 if (friend_p && *friend_p
23867 && !decl
23868 && decl_specifiers.type
23869 && TYPE_P (decl_specifiers.type))
23870 decl = decl_specifiers.type;
23872 if (decl && decl != error_mark_node)
23873 decl = TYPE_NAME (decl);
23874 else
23875 decl = error_mark_node;
23877 /* Perform access checks for template parameters. */
23878 cp_parser_perform_template_parameter_access_checks (checks);
23882 /* Complain about missing 'typename' or other invalid type names. */
23883 if (!decl_specifiers.any_type_specifiers_p
23884 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23886 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23887 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23888 the rest of this declaration. */
23889 decl = error_mark_node;
23890 goto out;
23893 /* If it's not a template class, try for a template function. If
23894 the next token is a `;', then this declaration does not declare
23895 anything. But, if there were errors in the decl-specifiers, then
23896 the error might well have come from an attempted class-specifier.
23897 In that case, there's no need to warn about a missing declarator. */
23898 if (!decl
23899 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23900 || decl_specifiers.type != error_mark_node))
23902 decl = cp_parser_init_declarator (parser,
23903 &decl_specifiers,
23904 checks,
23905 /*function_definition_allowed_p=*/true,
23906 member_p,
23907 declares_class_or_enum,
23908 &function_definition_p,
23909 NULL, NULL);
23911 /* 7.1.1-1 [dcl.stc]
23913 A storage-class-specifier shall not be specified in an explicit
23914 specialization... */
23915 if (decl
23916 && explicit_specialization_p
23917 && decl_specifiers.storage_class != sc_none)
23919 error_at (decl_spec_token_start->location,
23920 "explicit template specialization cannot have a storage class");
23921 decl = error_mark_node;
23924 if (decl && VAR_P (decl))
23925 check_template_variable (decl);
23928 /* Look for a trailing `;' after the declaration. */
23929 if (!function_definition_p
23930 && (decl == error_mark_node
23931 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23932 cp_parser_skip_to_end_of_block_or_statement (parser);
23934 out:
23935 pop_deferring_access_checks ();
23937 /* Clear any current qualification; whatever comes next is the start
23938 of something new. */
23939 parser->scope = NULL_TREE;
23940 parser->qualifying_scope = NULL_TREE;
23941 parser->object_scope = NULL_TREE;
23943 return decl;
23946 /* Parse a cast-expression that is not the operand of a unary "&". */
23948 static tree
23949 cp_parser_simple_cast_expression (cp_parser *parser)
23951 return cp_parser_cast_expression (parser, /*address_p=*/false,
23952 /*cast_p=*/false, /*decltype*/false, NULL);
23955 /* Parse a functional cast to TYPE. Returns an expression
23956 representing the cast. */
23958 static tree
23959 cp_parser_functional_cast (cp_parser* parser, tree type)
23961 vec<tree, va_gc> *vec;
23962 tree expression_list;
23963 tree cast;
23964 bool nonconst_p;
23966 if (!type)
23967 type = error_mark_node;
23969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23971 cp_lexer_set_source_position (parser->lexer);
23972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23973 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23974 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23975 if (TREE_CODE (type) == TYPE_DECL)
23976 type = TREE_TYPE (type);
23977 return finish_compound_literal (type, expression_list,
23978 tf_warning_or_error);
23982 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23983 /*cast_p=*/true,
23984 /*allow_expansion_p=*/true,
23985 /*non_constant_p=*/NULL);
23986 if (vec == NULL)
23987 expression_list = error_mark_node;
23988 else
23990 expression_list = build_tree_list_vec (vec);
23991 release_tree_vector (vec);
23994 cast = build_functional_cast (type, expression_list,
23995 tf_warning_or_error);
23996 /* [expr.const]/1: In an integral constant expression "only type
23997 conversions to integral or enumeration type can be used". */
23998 if (TREE_CODE (type) == TYPE_DECL)
23999 type = TREE_TYPE (type);
24000 if (cast != error_mark_node
24001 && !cast_valid_in_integral_constant_expression_p (type)
24002 && cp_parser_non_integral_constant_expression (parser,
24003 NIC_CONSTRUCTOR))
24004 return error_mark_node;
24005 return cast;
24008 /* Save the tokens that make up the body of a member function defined
24009 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24010 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24011 specifiers applied to the declaration. Returns the FUNCTION_DECL
24012 for the member function. */
24014 static tree
24015 cp_parser_save_member_function_body (cp_parser* parser,
24016 cp_decl_specifier_seq *decl_specifiers,
24017 cp_declarator *declarator,
24018 tree attributes)
24020 cp_token *first;
24021 cp_token *last;
24022 tree fn;
24024 /* Create the FUNCTION_DECL. */
24025 fn = grokmethod (decl_specifiers, declarator, attributes);
24026 cp_finalize_omp_declare_simd (parser, fn);
24027 /* If something went badly wrong, bail out now. */
24028 if (fn == error_mark_node)
24030 /* If there's a function-body, skip it. */
24031 if (cp_parser_token_starts_function_definition_p
24032 (cp_lexer_peek_token (parser->lexer)))
24033 cp_parser_skip_to_end_of_block_or_statement (parser);
24034 return error_mark_node;
24037 /* Remember it, if there default args to post process. */
24038 cp_parser_save_default_args (parser, fn);
24040 /* Save away the tokens that make up the body of the
24041 function. */
24042 first = parser->lexer->next_token;
24043 /* Handle function try blocks. */
24044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24045 cp_lexer_consume_token (parser->lexer);
24046 /* We can have braced-init-list mem-initializers before the fn body. */
24047 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24049 cp_lexer_consume_token (parser->lexer);
24050 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24052 /* cache_group will stop after an un-nested { } pair, too. */
24053 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24054 break;
24056 /* variadic mem-inits have ... after the ')'. */
24057 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24058 cp_lexer_consume_token (parser->lexer);
24061 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24062 /* Handle function try blocks. */
24063 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24064 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24065 last = parser->lexer->next_token;
24067 /* Save away the inline definition; we will process it when the
24068 class is complete. */
24069 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24070 DECL_PENDING_INLINE_P (fn) = 1;
24072 /* We need to know that this was defined in the class, so that
24073 friend templates are handled correctly. */
24074 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24076 /* Add FN to the queue of functions to be parsed later. */
24077 vec_safe_push (unparsed_funs_with_definitions, fn);
24079 return fn;
24082 /* Save the tokens that make up the in-class initializer for a non-static
24083 data member. Returns a DEFAULT_ARG. */
24085 static tree
24086 cp_parser_save_nsdmi (cp_parser* parser)
24088 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24091 /* Parse a template-argument-list, as well as the trailing ">" (but
24092 not the opening "<"). See cp_parser_template_argument_list for the
24093 return value. */
24095 static tree
24096 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24098 tree arguments;
24099 tree saved_scope;
24100 tree saved_qualifying_scope;
24101 tree saved_object_scope;
24102 bool saved_greater_than_is_operator_p;
24103 int saved_unevaluated_operand;
24104 int saved_inhibit_evaluation_warnings;
24106 /* [temp.names]
24108 When parsing a template-id, the first non-nested `>' is taken as
24109 the end of the template-argument-list rather than a greater-than
24110 operator. */
24111 saved_greater_than_is_operator_p
24112 = parser->greater_than_is_operator_p;
24113 parser->greater_than_is_operator_p = false;
24114 /* Parsing the argument list may modify SCOPE, so we save it
24115 here. */
24116 saved_scope = parser->scope;
24117 saved_qualifying_scope = parser->qualifying_scope;
24118 saved_object_scope = parser->object_scope;
24119 /* We need to evaluate the template arguments, even though this
24120 template-id may be nested within a "sizeof". */
24121 saved_unevaluated_operand = cp_unevaluated_operand;
24122 cp_unevaluated_operand = 0;
24123 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24124 c_inhibit_evaluation_warnings = 0;
24125 /* Parse the template-argument-list itself. */
24126 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24127 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24128 arguments = NULL_TREE;
24129 else
24130 arguments = cp_parser_template_argument_list (parser);
24131 /* Look for the `>' that ends the template-argument-list. If we find
24132 a '>>' instead, it's probably just a typo. */
24133 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24135 if (cxx_dialect != cxx98)
24137 /* In C++0x, a `>>' in a template argument list or cast
24138 expression is considered to be two separate `>'
24139 tokens. So, change the current token to a `>', but don't
24140 consume it: it will be consumed later when the outer
24141 template argument list (or cast expression) is parsed.
24142 Note that this replacement of `>' for `>>' is necessary
24143 even if we are parsing tentatively: in the tentative
24144 case, after calling
24145 cp_parser_enclosed_template_argument_list we will always
24146 throw away all of the template arguments and the first
24147 closing `>', either because the template argument list
24148 was erroneous or because we are replacing those tokens
24149 with a CPP_TEMPLATE_ID token. The second `>' (which will
24150 not have been thrown away) is needed either to close an
24151 outer template argument list or to complete a new-style
24152 cast. */
24153 cp_token *token = cp_lexer_peek_token (parser->lexer);
24154 token->type = CPP_GREATER;
24156 else if (!saved_greater_than_is_operator_p)
24158 /* If we're in a nested template argument list, the '>>' has
24159 to be a typo for '> >'. We emit the error message, but we
24160 continue parsing and we push a '>' as next token, so that
24161 the argument list will be parsed correctly. Note that the
24162 global source location is still on the token before the
24163 '>>', so we need to say explicitly where we want it. */
24164 cp_token *token = cp_lexer_peek_token (parser->lexer);
24165 error_at (token->location, "%<>>%> should be %<> >%> "
24166 "within a nested template argument list");
24168 token->type = CPP_GREATER;
24170 else
24172 /* If this is not a nested template argument list, the '>>'
24173 is a typo for '>'. Emit an error message and continue.
24174 Same deal about the token location, but here we can get it
24175 right by consuming the '>>' before issuing the diagnostic. */
24176 cp_token *token = cp_lexer_consume_token (parser->lexer);
24177 error_at (token->location,
24178 "spurious %<>>%>, use %<>%> to terminate "
24179 "a template argument list");
24182 else
24183 cp_parser_skip_to_end_of_template_parameter_list (parser);
24184 /* The `>' token might be a greater-than operator again now. */
24185 parser->greater_than_is_operator_p
24186 = saved_greater_than_is_operator_p;
24187 /* Restore the SAVED_SCOPE. */
24188 parser->scope = saved_scope;
24189 parser->qualifying_scope = saved_qualifying_scope;
24190 parser->object_scope = saved_object_scope;
24191 cp_unevaluated_operand = saved_unevaluated_operand;
24192 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24194 return arguments;
24197 /* MEMBER_FUNCTION is a member function, or a friend. If default
24198 arguments, or the body of the function have not yet been parsed,
24199 parse them now. */
24201 static void
24202 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24204 timevar_push (TV_PARSE_INMETH);
24205 /* If this member is a template, get the underlying
24206 FUNCTION_DECL. */
24207 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24208 member_function = DECL_TEMPLATE_RESULT (member_function);
24210 /* There should not be any class definitions in progress at this
24211 point; the bodies of members are only parsed outside of all class
24212 definitions. */
24213 gcc_assert (parser->num_classes_being_defined == 0);
24214 /* While we're parsing the member functions we might encounter more
24215 classes. We want to handle them right away, but we don't want
24216 them getting mixed up with functions that are currently in the
24217 queue. */
24218 push_unparsed_function_queues (parser);
24220 /* Make sure that any template parameters are in scope. */
24221 maybe_begin_member_template_processing (member_function);
24223 /* If the body of the function has not yet been parsed, parse it
24224 now. */
24225 if (DECL_PENDING_INLINE_P (member_function))
24227 tree function_scope;
24228 cp_token_cache *tokens;
24230 /* The function is no longer pending; we are processing it. */
24231 tokens = DECL_PENDING_INLINE_INFO (member_function);
24232 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24233 DECL_PENDING_INLINE_P (member_function) = 0;
24235 /* If this is a local class, enter the scope of the containing
24236 function. */
24237 function_scope = current_function_decl;
24238 if (function_scope)
24239 push_function_context ();
24241 /* Push the body of the function onto the lexer stack. */
24242 cp_parser_push_lexer_for_tokens (parser, tokens);
24244 /* Let the front end know that we going to be defining this
24245 function. */
24246 start_preparsed_function (member_function, NULL_TREE,
24247 SF_PRE_PARSED | SF_INCLASS_INLINE);
24249 /* Don't do access checking if it is a templated function. */
24250 if (processing_template_decl)
24251 push_deferring_access_checks (dk_no_check);
24253 /* #pragma omp declare reduction needs special parsing. */
24254 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24256 parser->lexer->in_pragma = true;
24257 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24258 finish_function (/*inline*/2);
24259 cp_check_omp_declare_reduction (member_function);
24261 else
24262 /* Now, parse the body of the function. */
24263 cp_parser_function_definition_after_declarator (parser,
24264 /*inline_p=*/true);
24266 if (processing_template_decl)
24267 pop_deferring_access_checks ();
24269 /* Leave the scope of the containing function. */
24270 if (function_scope)
24271 pop_function_context ();
24272 cp_parser_pop_lexer (parser);
24275 /* Remove any template parameters from the symbol table. */
24276 maybe_end_member_template_processing ();
24278 /* Restore the queue. */
24279 pop_unparsed_function_queues (parser);
24280 timevar_pop (TV_PARSE_INMETH);
24283 /* If DECL contains any default args, remember it on the unparsed
24284 functions queue. */
24286 static void
24287 cp_parser_save_default_args (cp_parser* parser, tree decl)
24289 tree probe;
24291 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24292 probe;
24293 probe = TREE_CHAIN (probe))
24294 if (TREE_PURPOSE (probe))
24296 cp_default_arg_entry entry = {current_class_type, decl};
24297 vec_safe_push (unparsed_funs_with_default_args, entry);
24298 break;
24302 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24303 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24304 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24305 from the parameter-type-list. */
24307 static tree
24308 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24309 tree default_arg, tree parmtype)
24311 cp_token_cache *tokens;
24312 tree parsed_arg;
24313 bool dummy;
24315 if (default_arg == error_mark_node)
24316 return error_mark_node;
24318 /* Push the saved tokens for the default argument onto the parser's
24319 lexer stack. */
24320 tokens = DEFARG_TOKENS (default_arg);
24321 cp_parser_push_lexer_for_tokens (parser, tokens);
24323 start_lambda_scope (decl);
24325 /* Parse the default argument. */
24326 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24327 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24328 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24330 finish_lambda_scope ();
24332 if (parsed_arg == error_mark_node)
24333 cp_parser_skip_to_end_of_statement (parser);
24335 if (!processing_template_decl)
24337 /* In a non-template class, check conversions now. In a template,
24338 we'll wait and instantiate these as needed. */
24339 if (TREE_CODE (decl) == PARM_DECL)
24340 parsed_arg = check_default_argument (parmtype, parsed_arg,
24341 tf_warning_or_error);
24342 else
24343 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24346 /* If the token stream has not been completely used up, then
24347 there was extra junk after the end of the default
24348 argument. */
24349 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24351 if (TREE_CODE (decl) == PARM_DECL)
24352 cp_parser_error (parser, "expected %<,%>");
24353 else
24354 cp_parser_error (parser, "expected %<;%>");
24357 /* Revert to the main lexer. */
24358 cp_parser_pop_lexer (parser);
24360 return parsed_arg;
24363 /* FIELD is a non-static data member with an initializer which we saved for
24364 later; parse it now. */
24366 static void
24367 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24369 tree def;
24371 maybe_begin_member_template_processing (field);
24373 push_unparsed_function_queues (parser);
24374 def = cp_parser_late_parse_one_default_arg (parser, field,
24375 DECL_INITIAL (field),
24376 NULL_TREE);
24377 pop_unparsed_function_queues (parser);
24379 maybe_end_member_template_processing ();
24381 DECL_INITIAL (field) = def;
24384 /* FN is a FUNCTION_DECL which may contains a parameter with an
24385 unparsed DEFAULT_ARG. Parse the default args now. This function
24386 assumes that the current scope is the scope in which the default
24387 argument should be processed. */
24389 static void
24390 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24392 bool saved_local_variables_forbidden_p;
24393 tree parm, parmdecl;
24395 /* While we're parsing the default args, we might (due to the
24396 statement expression extension) encounter more classes. We want
24397 to handle them right away, but we don't want them getting mixed
24398 up with default args that are currently in the queue. */
24399 push_unparsed_function_queues (parser);
24401 /* Local variable names (and the `this' keyword) may not appear
24402 in a default argument. */
24403 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24404 parser->local_variables_forbidden_p = true;
24406 push_defarg_context (fn);
24408 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24409 parmdecl = DECL_ARGUMENTS (fn);
24410 parm && parm != void_list_node;
24411 parm = TREE_CHAIN (parm),
24412 parmdecl = DECL_CHAIN (parmdecl))
24414 tree default_arg = TREE_PURPOSE (parm);
24415 tree parsed_arg;
24416 vec<tree, va_gc> *insts;
24417 tree copy;
24418 unsigned ix;
24420 if (!default_arg)
24421 continue;
24423 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24424 /* This can happen for a friend declaration for a function
24425 already declared with default arguments. */
24426 continue;
24428 parsed_arg
24429 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24430 default_arg,
24431 TREE_VALUE (parm));
24432 if (parsed_arg == error_mark_node)
24434 continue;
24437 TREE_PURPOSE (parm) = parsed_arg;
24439 /* Update any instantiations we've already created. */
24440 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24441 vec_safe_iterate (insts, ix, &copy); ix++)
24442 TREE_PURPOSE (copy) = parsed_arg;
24445 pop_defarg_context ();
24447 /* Make sure no default arg is missing. */
24448 check_default_args (fn);
24450 /* Restore the state of local_variables_forbidden_p. */
24451 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24453 /* Restore the queue. */
24454 pop_unparsed_function_queues (parser);
24457 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24459 sizeof ... ( identifier )
24461 where the 'sizeof' token has already been consumed. */
24463 static tree
24464 cp_parser_sizeof_pack (cp_parser *parser)
24466 /* Consume the `...'. */
24467 cp_lexer_consume_token (parser->lexer);
24468 maybe_warn_variadic_templates ();
24470 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24471 if (paren)
24472 cp_lexer_consume_token (parser->lexer);
24473 else
24474 permerror (cp_lexer_peek_token (parser->lexer)->location,
24475 "%<sizeof...%> argument must be surrounded by parentheses");
24477 cp_token *token = cp_lexer_peek_token (parser->lexer);
24478 tree name = cp_parser_identifier (parser);
24479 if (name == error_mark_node)
24480 return error_mark_node;
24481 /* The name is not qualified. */
24482 parser->scope = NULL_TREE;
24483 parser->qualifying_scope = NULL_TREE;
24484 parser->object_scope = NULL_TREE;
24485 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24486 if (expr == error_mark_node)
24487 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24488 token->location);
24489 if (TREE_CODE (expr) == TYPE_DECL)
24490 expr = TREE_TYPE (expr);
24491 else if (TREE_CODE (expr) == CONST_DECL)
24492 expr = DECL_INITIAL (expr);
24493 expr = make_pack_expansion (expr);
24495 if (paren)
24496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24498 return expr;
24501 /* Parse the operand of `sizeof' (or a similar operator). Returns
24502 either a TYPE or an expression, depending on the form of the
24503 input. The KEYWORD indicates which kind of expression we have
24504 encountered. */
24506 static tree
24507 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24509 tree expr = NULL_TREE;
24510 const char *saved_message;
24511 char *tmp;
24512 bool saved_integral_constant_expression_p;
24513 bool saved_non_integral_constant_expression_p;
24515 /* If it's a `...', then we are computing the length of a parameter
24516 pack. */
24517 if (keyword == RID_SIZEOF
24518 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24519 return cp_parser_sizeof_pack (parser);
24521 /* Types cannot be defined in a `sizeof' expression. Save away the
24522 old message. */
24523 saved_message = parser->type_definition_forbidden_message;
24524 /* And create the new one. */
24525 tmp = concat ("types may not be defined in %<",
24526 IDENTIFIER_POINTER (ridpointers[keyword]),
24527 "%> expressions", NULL);
24528 parser->type_definition_forbidden_message = tmp;
24530 /* The restrictions on constant-expressions do not apply inside
24531 sizeof expressions. */
24532 saved_integral_constant_expression_p
24533 = parser->integral_constant_expression_p;
24534 saved_non_integral_constant_expression_p
24535 = parser->non_integral_constant_expression_p;
24536 parser->integral_constant_expression_p = false;
24538 /* Do not actually evaluate the expression. */
24539 ++cp_unevaluated_operand;
24540 ++c_inhibit_evaluation_warnings;
24541 /* If it's a `(', then we might be looking at the type-id
24542 construction. */
24543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24545 tree type = NULL_TREE;
24547 /* We can't be sure yet whether we're looking at a type-id or an
24548 expression. */
24549 cp_parser_parse_tentatively (parser);
24550 /* Note: as a GNU Extension, compound literals are considered
24551 postfix-expressions as they are in C99, so they are valid
24552 arguments to sizeof. See comment in cp_parser_cast_expression
24553 for details. */
24554 if (cp_parser_compound_literal_p (parser))
24555 cp_parser_simulate_error (parser);
24556 else
24558 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24559 parser->in_type_id_in_expr_p = true;
24560 /* Look for the type-id. */
24561 type = cp_parser_type_id (parser);
24562 /* Look for the closing `)'. */
24563 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24564 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24567 /* If all went well, then we're done. */
24568 if (cp_parser_parse_definitely (parser))
24570 cp_decl_specifier_seq decl_specs;
24572 /* Build a trivial decl-specifier-seq. */
24573 clear_decl_specs (&decl_specs);
24574 decl_specs.type = type;
24576 /* Call grokdeclarator to figure out what type this is. */
24577 expr = grokdeclarator (NULL,
24578 &decl_specs,
24579 TYPENAME,
24580 /*initialized=*/0,
24581 /*attrlist=*/NULL);
24585 /* If the type-id production did not work out, then we must be
24586 looking at the unary-expression production. */
24587 if (!expr)
24588 expr = cp_parser_unary_expression (parser);
24590 /* Go back to evaluating expressions. */
24591 --cp_unevaluated_operand;
24592 --c_inhibit_evaluation_warnings;
24594 /* Free the message we created. */
24595 free (tmp);
24596 /* And restore the old one. */
24597 parser->type_definition_forbidden_message = saved_message;
24598 parser->integral_constant_expression_p
24599 = saved_integral_constant_expression_p;
24600 parser->non_integral_constant_expression_p
24601 = saved_non_integral_constant_expression_p;
24603 return expr;
24606 /* If the current declaration has no declarator, return true. */
24608 static bool
24609 cp_parser_declares_only_class_p (cp_parser *parser)
24611 /* If the next token is a `;' or a `,' then there is no
24612 declarator. */
24613 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24614 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24617 /* Update the DECL_SPECS to reflect the storage class indicated by
24618 KEYWORD. */
24620 static void
24621 cp_parser_set_storage_class (cp_parser *parser,
24622 cp_decl_specifier_seq *decl_specs,
24623 enum rid keyword,
24624 cp_token *token)
24626 cp_storage_class storage_class;
24628 if (parser->in_unbraced_linkage_specification_p)
24630 error_at (token->location, "invalid use of %qD in linkage specification",
24631 ridpointers[keyword]);
24632 return;
24634 else if (decl_specs->storage_class != sc_none)
24636 decl_specs->conflicting_specifiers_p = true;
24637 return;
24640 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24641 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24642 && decl_specs->gnu_thread_keyword_p)
24644 pedwarn (decl_specs->locations[ds_thread], 0,
24645 "%<__thread%> before %qD", ridpointers[keyword]);
24648 switch (keyword)
24650 case RID_AUTO:
24651 storage_class = sc_auto;
24652 break;
24653 case RID_REGISTER:
24654 storage_class = sc_register;
24655 break;
24656 case RID_STATIC:
24657 storage_class = sc_static;
24658 break;
24659 case RID_EXTERN:
24660 storage_class = sc_extern;
24661 break;
24662 case RID_MUTABLE:
24663 storage_class = sc_mutable;
24664 break;
24665 default:
24666 gcc_unreachable ();
24668 decl_specs->storage_class = storage_class;
24669 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24671 /* A storage class specifier cannot be applied alongside a typedef
24672 specifier. If there is a typedef specifier present then set
24673 conflicting_specifiers_p which will trigger an error later
24674 on in grokdeclarator. */
24675 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24676 decl_specs->conflicting_specifiers_p = true;
24679 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24680 is true, the type is a class or enum definition. */
24682 static void
24683 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24684 tree type_spec,
24685 cp_token *token,
24686 bool type_definition_p)
24688 decl_specs->any_specifiers_p = true;
24690 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24691 (with, for example, in "typedef int wchar_t;") we remember that
24692 this is what happened. In system headers, we ignore these
24693 declarations so that G++ can work with system headers that are not
24694 C++-safe. */
24695 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24696 && !type_definition_p
24697 && (type_spec == boolean_type_node
24698 || type_spec == char16_type_node
24699 || type_spec == char32_type_node
24700 || type_spec == wchar_type_node)
24701 && (decl_specs->type
24702 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24703 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24704 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24705 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24707 decl_specs->redefined_builtin_type = type_spec;
24708 set_and_check_decl_spec_loc (decl_specs,
24709 ds_redefined_builtin_type_spec,
24710 token);
24711 if (!decl_specs->type)
24713 decl_specs->type = type_spec;
24714 decl_specs->type_definition_p = false;
24715 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24718 else if (decl_specs->type)
24719 decl_specs->multiple_types_p = true;
24720 else
24722 decl_specs->type = type_spec;
24723 decl_specs->type_definition_p = type_definition_p;
24724 decl_specs->redefined_builtin_type = NULL_TREE;
24725 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24729 /* True iff TOKEN is the GNU keyword __thread. */
24731 static bool
24732 token_is__thread (cp_token *token)
24734 gcc_assert (token->keyword == RID_THREAD);
24735 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24738 /* Set the location for a declarator specifier and check if it is
24739 duplicated.
24741 DECL_SPECS is the sequence of declarator specifiers onto which to
24742 set the location.
24744 DS is the single declarator specifier to set which location is to
24745 be set onto the existing sequence of declarators.
24747 LOCATION is the location for the declarator specifier to
24748 consider. */
24750 static void
24751 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24752 cp_decl_spec ds, cp_token *token)
24754 gcc_assert (ds < ds_last);
24756 if (decl_specs == NULL)
24757 return;
24759 source_location location = token->location;
24761 if (decl_specs->locations[ds] == 0)
24763 decl_specs->locations[ds] = location;
24764 if (ds == ds_thread)
24765 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24767 else
24769 if (ds == ds_long)
24771 if (decl_specs->locations[ds_long_long] != 0)
24772 error_at (location,
24773 "%<long long long%> is too long for GCC");
24774 else
24776 decl_specs->locations[ds_long_long] = location;
24777 pedwarn_cxx98 (location,
24778 OPT_Wlong_long,
24779 "ISO C++ 1998 does not support %<long long%>");
24782 else if (ds == ds_thread)
24784 bool gnu = token_is__thread (token);
24785 if (gnu != decl_specs->gnu_thread_keyword_p)
24786 error_at (location,
24787 "both %<__thread%> and %<thread_local%> specified");
24788 else
24789 error_at (location, "duplicate %qD", token->u.value);
24791 else
24793 static const char *const decl_spec_names[] = {
24794 "signed",
24795 "unsigned",
24796 "short",
24797 "long",
24798 "const",
24799 "volatile",
24800 "restrict",
24801 "inline",
24802 "virtual",
24803 "explicit",
24804 "friend",
24805 "typedef",
24806 "using",
24807 "constexpr",
24808 "__complex"
24810 error_at (location,
24811 "duplicate %qs", decl_spec_names[ds]);
24816 /* Return true iff the declarator specifier DS is present in the
24817 sequence of declarator specifiers DECL_SPECS. */
24819 bool
24820 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24821 cp_decl_spec ds)
24823 gcc_assert (ds < ds_last);
24825 if (decl_specs == NULL)
24826 return false;
24828 return decl_specs->locations[ds] != 0;
24831 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24832 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24834 static bool
24835 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24837 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24840 /* Issue an error message indicating that TOKEN_DESC was expected.
24841 If KEYWORD is true, it indicated this function is called by
24842 cp_parser_require_keword and the required token can only be
24843 a indicated keyword. */
24845 static void
24846 cp_parser_required_error (cp_parser *parser,
24847 required_token token_desc,
24848 bool keyword)
24850 switch (token_desc)
24852 case RT_NEW:
24853 cp_parser_error (parser, "expected %<new%>");
24854 return;
24855 case RT_DELETE:
24856 cp_parser_error (parser, "expected %<delete%>");
24857 return;
24858 case RT_RETURN:
24859 cp_parser_error (parser, "expected %<return%>");
24860 return;
24861 case RT_WHILE:
24862 cp_parser_error (parser, "expected %<while%>");
24863 return;
24864 case RT_EXTERN:
24865 cp_parser_error (parser, "expected %<extern%>");
24866 return;
24867 case RT_STATIC_ASSERT:
24868 cp_parser_error (parser, "expected %<static_assert%>");
24869 return;
24870 case RT_DECLTYPE:
24871 cp_parser_error (parser, "expected %<decltype%>");
24872 return;
24873 case RT_OPERATOR:
24874 cp_parser_error (parser, "expected %<operator%>");
24875 return;
24876 case RT_CLASS:
24877 cp_parser_error (parser, "expected %<class%>");
24878 return;
24879 case RT_TEMPLATE:
24880 cp_parser_error (parser, "expected %<template%>");
24881 return;
24882 case RT_NAMESPACE:
24883 cp_parser_error (parser, "expected %<namespace%>");
24884 return;
24885 case RT_USING:
24886 cp_parser_error (parser, "expected %<using%>");
24887 return;
24888 case RT_ASM:
24889 cp_parser_error (parser, "expected %<asm%>");
24890 return;
24891 case RT_TRY:
24892 cp_parser_error (parser, "expected %<try%>");
24893 return;
24894 case RT_CATCH:
24895 cp_parser_error (parser, "expected %<catch%>");
24896 return;
24897 case RT_THROW:
24898 cp_parser_error (parser, "expected %<throw%>");
24899 return;
24900 case RT_LABEL:
24901 cp_parser_error (parser, "expected %<__label__%>");
24902 return;
24903 case RT_AT_TRY:
24904 cp_parser_error (parser, "expected %<@try%>");
24905 return;
24906 case RT_AT_SYNCHRONIZED:
24907 cp_parser_error (parser, "expected %<@synchronized%>");
24908 return;
24909 case RT_AT_THROW:
24910 cp_parser_error (parser, "expected %<@throw%>");
24911 return;
24912 case RT_TRANSACTION_ATOMIC:
24913 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24914 return;
24915 case RT_TRANSACTION_RELAXED:
24916 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24917 return;
24918 default:
24919 break;
24921 if (!keyword)
24923 switch (token_desc)
24925 case RT_SEMICOLON:
24926 cp_parser_error (parser, "expected %<;%>");
24927 return;
24928 case RT_OPEN_PAREN:
24929 cp_parser_error (parser, "expected %<(%>");
24930 return;
24931 case RT_CLOSE_BRACE:
24932 cp_parser_error (parser, "expected %<}%>");
24933 return;
24934 case RT_OPEN_BRACE:
24935 cp_parser_error (parser, "expected %<{%>");
24936 return;
24937 case RT_CLOSE_SQUARE:
24938 cp_parser_error (parser, "expected %<]%>");
24939 return;
24940 case RT_OPEN_SQUARE:
24941 cp_parser_error (parser, "expected %<[%>");
24942 return;
24943 case RT_COMMA:
24944 cp_parser_error (parser, "expected %<,%>");
24945 return;
24946 case RT_SCOPE:
24947 cp_parser_error (parser, "expected %<::%>");
24948 return;
24949 case RT_LESS:
24950 cp_parser_error (parser, "expected %<<%>");
24951 return;
24952 case RT_GREATER:
24953 cp_parser_error (parser, "expected %<>%>");
24954 return;
24955 case RT_EQ:
24956 cp_parser_error (parser, "expected %<=%>");
24957 return;
24958 case RT_ELLIPSIS:
24959 cp_parser_error (parser, "expected %<...%>");
24960 return;
24961 case RT_MULT:
24962 cp_parser_error (parser, "expected %<*%>");
24963 return;
24964 case RT_COMPL:
24965 cp_parser_error (parser, "expected %<~%>");
24966 return;
24967 case RT_COLON:
24968 cp_parser_error (parser, "expected %<:%>");
24969 return;
24970 case RT_COLON_SCOPE:
24971 cp_parser_error (parser, "expected %<:%> or %<::%>");
24972 return;
24973 case RT_CLOSE_PAREN:
24974 cp_parser_error (parser, "expected %<)%>");
24975 return;
24976 case RT_COMMA_CLOSE_PAREN:
24977 cp_parser_error (parser, "expected %<,%> or %<)%>");
24978 return;
24979 case RT_PRAGMA_EOL:
24980 cp_parser_error (parser, "expected end of line");
24981 return;
24982 case RT_NAME:
24983 cp_parser_error (parser, "expected identifier");
24984 return;
24985 case RT_SELECT:
24986 cp_parser_error (parser, "expected selection-statement");
24987 return;
24988 case RT_INTERATION:
24989 cp_parser_error (parser, "expected iteration-statement");
24990 return;
24991 case RT_JUMP:
24992 cp_parser_error (parser, "expected jump-statement");
24993 return;
24994 case RT_CLASS_KEY:
24995 cp_parser_error (parser, "expected class-key");
24996 return;
24997 case RT_CLASS_TYPENAME_TEMPLATE:
24998 cp_parser_error (parser,
24999 "expected %<class%>, %<typename%>, or %<template%>");
25000 return;
25001 default:
25002 gcc_unreachable ();
25005 else
25006 gcc_unreachable ();
25011 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25012 issue an error message indicating that TOKEN_DESC was expected.
25014 Returns the token consumed, if the token had the appropriate type.
25015 Otherwise, returns NULL. */
25017 static cp_token *
25018 cp_parser_require (cp_parser* parser,
25019 enum cpp_ttype type,
25020 required_token token_desc)
25022 if (cp_lexer_next_token_is (parser->lexer, type))
25023 return cp_lexer_consume_token (parser->lexer);
25024 else
25026 /* Output the MESSAGE -- unless we're parsing tentatively. */
25027 if (!cp_parser_simulate_error (parser))
25028 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25029 return NULL;
25033 /* An error message is produced if the next token is not '>'.
25034 All further tokens are skipped until the desired token is
25035 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25037 static void
25038 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25040 /* Current level of '< ... >'. */
25041 unsigned level = 0;
25042 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25043 unsigned nesting_depth = 0;
25045 /* Are we ready, yet? If not, issue error message. */
25046 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25047 return;
25049 /* Skip tokens until the desired token is found. */
25050 while (true)
25052 /* Peek at the next token. */
25053 switch (cp_lexer_peek_token (parser->lexer)->type)
25055 case CPP_LESS:
25056 if (!nesting_depth)
25057 ++level;
25058 break;
25060 case CPP_RSHIFT:
25061 if (cxx_dialect == cxx98)
25062 /* C++0x views the `>>' operator as two `>' tokens, but
25063 C++98 does not. */
25064 break;
25065 else if (!nesting_depth && level-- == 0)
25067 /* We've hit a `>>' where the first `>' closes the
25068 template argument list, and the second `>' is
25069 spurious. Just consume the `>>' and stop; we've
25070 already produced at least one error. */
25071 cp_lexer_consume_token (parser->lexer);
25072 return;
25074 /* Fall through for C++0x, so we handle the second `>' in
25075 the `>>'. */
25077 case CPP_GREATER:
25078 if (!nesting_depth && level-- == 0)
25080 /* We've reached the token we want, consume it and stop. */
25081 cp_lexer_consume_token (parser->lexer);
25082 return;
25084 break;
25086 case CPP_OPEN_PAREN:
25087 case CPP_OPEN_SQUARE:
25088 ++nesting_depth;
25089 break;
25091 case CPP_CLOSE_PAREN:
25092 case CPP_CLOSE_SQUARE:
25093 if (nesting_depth-- == 0)
25094 return;
25095 break;
25097 case CPP_EOF:
25098 case CPP_PRAGMA_EOL:
25099 case CPP_SEMICOLON:
25100 case CPP_OPEN_BRACE:
25101 case CPP_CLOSE_BRACE:
25102 /* The '>' was probably forgotten, don't look further. */
25103 return;
25105 default:
25106 break;
25109 /* Consume this token. */
25110 cp_lexer_consume_token (parser->lexer);
25114 /* If the next token is the indicated keyword, consume it. Otherwise,
25115 issue an error message indicating that TOKEN_DESC was expected.
25117 Returns the token consumed, if the token had the appropriate type.
25118 Otherwise, returns NULL. */
25120 static cp_token *
25121 cp_parser_require_keyword (cp_parser* parser,
25122 enum rid keyword,
25123 required_token token_desc)
25125 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25127 if (token && token->keyword != keyword)
25129 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25130 return NULL;
25133 return token;
25136 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25137 function-definition. */
25139 static bool
25140 cp_parser_token_starts_function_definition_p (cp_token* token)
25142 return (/* An ordinary function-body begins with an `{'. */
25143 token->type == CPP_OPEN_BRACE
25144 /* A ctor-initializer begins with a `:'. */
25145 || token->type == CPP_COLON
25146 /* A function-try-block begins with `try'. */
25147 || token->keyword == RID_TRY
25148 /* A function-transaction-block begins with `__transaction_atomic'
25149 or `__transaction_relaxed'. */
25150 || token->keyword == RID_TRANSACTION_ATOMIC
25151 || token->keyword == RID_TRANSACTION_RELAXED
25152 /* The named return value extension begins with `return'. */
25153 || token->keyword == RID_RETURN);
25156 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25157 definition. */
25159 static bool
25160 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25162 cp_token *token;
25164 token = cp_lexer_peek_token (parser->lexer);
25165 return (token->type == CPP_OPEN_BRACE
25166 || (token->type == CPP_COLON
25167 && !parser->colon_doesnt_start_class_def_p));
25170 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25171 C++0x) ending a template-argument. */
25173 static bool
25174 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25176 cp_token *token;
25178 token = cp_lexer_peek_token (parser->lexer);
25179 return (token->type == CPP_COMMA
25180 || token->type == CPP_GREATER
25181 || token->type == CPP_ELLIPSIS
25182 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25185 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25186 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25188 static bool
25189 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25190 size_t n)
25192 cp_token *token;
25194 token = cp_lexer_peek_nth_token (parser->lexer, n);
25195 if (token->type == CPP_LESS)
25196 return true;
25197 /* Check for the sequence `<::' in the original code. It would be lexed as
25198 `[:', where `[' is a digraph, and there is no whitespace before
25199 `:'. */
25200 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25202 cp_token *token2;
25203 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25204 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25205 return true;
25207 return false;
25210 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25211 or none_type otherwise. */
25213 static enum tag_types
25214 cp_parser_token_is_class_key (cp_token* token)
25216 switch (token->keyword)
25218 case RID_CLASS:
25219 return class_type;
25220 case RID_STRUCT:
25221 return record_type;
25222 case RID_UNION:
25223 return union_type;
25225 default:
25226 return none_type;
25230 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25231 or none_type otherwise or if the token is null. */
25233 static enum tag_types
25234 cp_parser_token_is_type_parameter_key (cp_token* token)
25236 if (!token)
25237 return none_type;
25239 switch (token->keyword)
25241 case RID_CLASS:
25242 return class_type;
25243 case RID_TYPENAME:
25244 return typename_type;
25246 default:
25247 return none_type;
25251 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25253 static void
25254 cp_parser_check_class_key (enum tag_types class_key, tree type)
25256 if (type == error_mark_node)
25257 return;
25258 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25260 if (permerror (input_location, "%qs tag used in naming %q#T",
25261 class_key == union_type ? "union"
25262 : class_key == record_type ? "struct" : "class",
25263 type))
25264 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25265 "%q#T was previously declared here", type);
25269 /* Issue an error message if DECL is redeclared with different
25270 access than its original declaration [class.access.spec/3].
25271 This applies to nested classes and nested class templates.
25272 [class.mem/1]. */
25274 static void
25275 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25277 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25278 return;
25280 if ((TREE_PRIVATE (decl)
25281 != (current_access_specifier == access_private_node))
25282 || (TREE_PROTECTED (decl)
25283 != (current_access_specifier == access_protected_node)))
25284 error_at (location, "%qD redeclared with different access", decl);
25287 /* Look for the `template' keyword, as a syntactic disambiguator.
25288 Return TRUE iff it is present, in which case it will be
25289 consumed. */
25291 static bool
25292 cp_parser_optional_template_keyword (cp_parser *parser)
25294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25296 /* In C++98 the `template' keyword can only be used within templates;
25297 outside templates the parser can always figure out what is a
25298 template and what is not. In C++11, per the resolution of DR 468,
25299 `template' is allowed in cases where it is not strictly necessary. */
25300 if (!processing_template_decl
25301 && pedantic && cxx_dialect == cxx98)
25303 cp_token *token = cp_lexer_peek_token (parser->lexer);
25304 pedwarn (token->location, OPT_Wpedantic,
25305 "in C++98 %<template%> (as a disambiguator) is only "
25306 "allowed within templates");
25307 /* If this part of the token stream is rescanned, the same
25308 error message would be generated. So, we purge the token
25309 from the stream. */
25310 cp_lexer_purge_token (parser->lexer);
25311 return false;
25313 else
25315 /* Consume the `template' keyword. */
25316 cp_lexer_consume_token (parser->lexer);
25317 return true;
25320 return false;
25323 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25324 set PARSER->SCOPE, and perform other related actions. */
25326 static void
25327 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25329 int i;
25330 struct tree_check *check_value;
25331 deferred_access_check *chk;
25332 vec<deferred_access_check, va_gc> *checks;
25334 /* Get the stored value. */
25335 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25336 /* Perform any access checks that were deferred. */
25337 checks = check_value->checks;
25338 if (checks)
25340 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25341 perform_or_defer_access_check (chk->binfo,
25342 chk->decl,
25343 chk->diag_decl, tf_warning_or_error);
25345 /* Set the scope from the stored value. */
25346 parser->scope = check_value->value;
25347 parser->qualifying_scope = check_value->qualifying_scope;
25348 parser->object_scope = NULL_TREE;
25351 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25352 encounter the end of a block before what we were looking for. */
25354 static bool
25355 cp_parser_cache_group (cp_parser *parser,
25356 enum cpp_ttype end,
25357 unsigned depth)
25359 while (true)
25361 cp_token *token = cp_lexer_peek_token (parser->lexer);
25363 /* Abort a parenthesized expression if we encounter a semicolon. */
25364 if ((end == CPP_CLOSE_PAREN || depth == 0)
25365 && token->type == CPP_SEMICOLON)
25366 return true;
25367 /* If we've reached the end of the file, stop. */
25368 if (token->type == CPP_EOF
25369 || (end != CPP_PRAGMA_EOL
25370 && token->type == CPP_PRAGMA_EOL))
25371 return true;
25372 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25373 /* We've hit the end of an enclosing block, so there's been some
25374 kind of syntax error. */
25375 return true;
25377 /* Consume the token. */
25378 cp_lexer_consume_token (parser->lexer);
25379 /* See if it starts a new group. */
25380 if (token->type == CPP_OPEN_BRACE)
25382 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25383 /* In theory this should probably check end == '}', but
25384 cp_parser_save_member_function_body needs it to exit
25385 after either '}' or ')' when called with ')'. */
25386 if (depth == 0)
25387 return false;
25389 else if (token->type == CPP_OPEN_PAREN)
25391 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25392 if (depth == 0 && end == CPP_CLOSE_PAREN)
25393 return false;
25395 else if (token->type == CPP_PRAGMA)
25396 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25397 else if (token->type == end)
25398 return false;
25402 /* Like above, for caching a default argument or NSDMI. Both of these are
25403 terminated by a non-nested comma, but it can be unclear whether or not a
25404 comma is nested in a template argument list unless we do more parsing.
25405 In order to handle this ambiguity, when we encounter a ',' after a '<'
25406 we try to parse what follows as a parameter-declaration-list (in the
25407 case of a default argument) or a member-declarator (in the case of an
25408 NSDMI). If that succeeds, then we stop caching. */
25410 static tree
25411 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25413 unsigned depth = 0;
25414 int maybe_template_id = 0;
25415 cp_token *first_token;
25416 cp_token *token;
25417 tree default_argument;
25419 /* Add tokens until we have processed the entire default
25420 argument. We add the range [first_token, token). */
25421 first_token = cp_lexer_peek_token (parser->lexer);
25422 if (first_token->type == CPP_OPEN_BRACE)
25424 /* For list-initialization, this is straightforward. */
25425 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25426 token = cp_lexer_peek_token (parser->lexer);
25428 else while (true)
25430 bool done = false;
25432 /* Peek at the next token. */
25433 token = cp_lexer_peek_token (parser->lexer);
25434 /* What we do depends on what token we have. */
25435 switch (token->type)
25437 /* In valid code, a default argument must be
25438 immediately followed by a `,' `)', or `...'. */
25439 case CPP_COMMA:
25440 if (depth == 0 && maybe_template_id)
25442 /* If we've seen a '<', we might be in a
25443 template-argument-list. Until Core issue 325 is
25444 resolved, we don't know how this situation ought
25445 to be handled, so try to DTRT. We check whether
25446 what comes after the comma is a valid parameter
25447 declaration list. If it is, then the comma ends
25448 the default argument; otherwise the default
25449 argument continues. */
25450 bool error = false;
25451 cp_token *peek;
25453 /* Set ITALP so cp_parser_parameter_declaration_list
25454 doesn't decide to commit to this parse. */
25455 bool saved_italp = parser->in_template_argument_list_p;
25456 parser->in_template_argument_list_p = true;
25458 cp_parser_parse_tentatively (parser);
25460 if (nsdmi)
25462 /* Parse declarators until we reach a non-comma or
25463 somthing that cannot be an initializer.
25464 Just checking whether we're looking at a single
25465 declarator is insufficient. Consider:
25466 int var = tuple<T,U>::x;
25467 The template parameter 'U' looks exactly like a
25468 declarator. */
25471 int ctor_dtor_or_conv_p;
25472 cp_lexer_consume_token (parser->lexer);
25473 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25474 &ctor_dtor_or_conv_p,
25475 /*parenthesized_p=*/NULL,
25476 /*member_p=*/true,
25477 /*friend_p=*/false);
25478 peek = cp_lexer_peek_token (parser->lexer);
25479 if (cp_parser_error_occurred (parser))
25480 break;
25482 while (peek->type == CPP_COMMA);
25483 /* If we met an '=' or ';' then the original comma
25484 was the end of the NSDMI. Otherwise assume
25485 we're still in the NSDMI. */
25486 error = (peek->type != CPP_EQ
25487 && peek->type != CPP_SEMICOLON);
25489 else
25491 cp_lexer_consume_token (parser->lexer);
25492 begin_scope (sk_function_parms, NULL_TREE);
25493 cp_parser_parameter_declaration_list (parser, &error);
25494 pop_bindings_and_leave_scope ();
25496 if (!cp_parser_error_occurred (parser) && !error)
25497 done = true;
25498 cp_parser_abort_tentative_parse (parser);
25500 parser->in_template_argument_list_p = saved_italp;
25501 break;
25503 case CPP_CLOSE_PAREN:
25504 case CPP_ELLIPSIS:
25505 /* If we run into a non-nested `;', `}', or `]',
25506 then the code is invalid -- but the default
25507 argument is certainly over. */
25508 case CPP_SEMICOLON:
25509 case CPP_CLOSE_BRACE:
25510 case CPP_CLOSE_SQUARE:
25511 if (depth == 0
25512 /* Handle correctly int n = sizeof ... ( p ); */
25513 && token->type != CPP_ELLIPSIS)
25514 done = true;
25515 /* Update DEPTH, if necessary. */
25516 else if (token->type == CPP_CLOSE_PAREN
25517 || token->type == CPP_CLOSE_BRACE
25518 || token->type == CPP_CLOSE_SQUARE)
25519 --depth;
25520 break;
25522 case CPP_OPEN_PAREN:
25523 case CPP_OPEN_SQUARE:
25524 case CPP_OPEN_BRACE:
25525 ++depth;
25526 break;
25528 case CPP_LESS:
25529 if (depth == 0)
25530 /* This might be the comparison operator, or it might
25531 start a template argument list. */
25532 ++maybe_template_id;
25533 break;
25535 case CPP_RSHIFT:
25536 if (cxx_dialect == cxx98)
25537 break;
25538 /* Fall through for C++0x, which treats the `>>'
25539 operator like two `>' tokens in certain
25540 cases. */
25542 case CPP_GREATER:
25543 if (depth == 0)
25545 /* This might be an operator, or it might close a
25546 template argument list. But if a previous '<'
25547 started a template argument list, this will have
25548 closed it, so we can't be in one anymore. */
25549 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25550 if (maybe_template_id < 0)
25551 maybe_template_id = 0;
25553 break;
25555 /* If we run out of tokens, issue an error message. */
25556 case CPP_EOF:
25557 case CPP_PRAGMA_EOL:
25558 error_at (token->location, "file ends in default argument");
25559 done = true;
25560 break;
25562 case CPP_NAME:
25563 case CPP_SCOPE:
25564 /* In these cases, we should look for template-ids.
25565 For example, if the default argument is
25566 `X<int, double>()', we need to do name lookup to
25567 figure out whether or not `X' is a template; if
25568 so, the `,' does not end the default argument.
25570 That is not yet done. */
25571 break;
25573 default:
25574 break;
25577 /* If we've reached the end, stop. */
25578 if (done)
25579 break;
25581 /* Add the token to the token block. */
25582 token = cp_lexer_consume_token (parser->lexer);
25585 /* Create a DEFAULT_ARG to represent the unparsed default
25586 argument. */
25587 default_argument = make_node (DEFAULT_ARG);
25588 DEFARG_TOKENS (default_argument)
25589 = cp_token_cache_new (first_token, token);
25590 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25592 return default_argument;
25595 /* Begin parsing tentatively. We always save tokens while parsing
25596 tentatively so that if the tentative parsing fails we can restore the
25597 tokens. */
25599 static void
25600 cp_parser_parse_tentatively (cp_parser* parser)
25602 /* Enter a new parsing context. */
25603 parser->context = cp_parser_context_new (parser->context);
25604 /* Begin saving tokens. */
25605 cp_lexer_save_tokens (parser->lexer);
25606 /* In order to avoid repetitive access control error messages,
25607 access checks are queued up until we are no longer parsing
25608 tentatively. */
25609 push_deferring_access_checks (dk_deferred);
25612 /* Commit to the currently active tentative parse. */
25614 static void
25615 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25617 cp_parser_context *context;
25618 cp_lexer *lexer;
25620 /* Mark all of the levels as committed. */
25621 lexer = parser->lexer;
25622 for (context = parser->context; context->next; context = context->next)
25624 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25625 break;
25626 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25627 while (!cp_lexer_saving_tokens (lexer))
25628 lexer = lexer->next;
25629 cp_lexer_commit_tokens (lexer);
25633 /* Commit to the topmost currently active tentative parse.
25635 Note that this function shouldn't be called when there are
25636 irreversible side-effects while in a tentative state. For
25637 example, we shouldn't create a permanent entry in the symbol
25638 table, or issue an error message that might not apply if the
25639 tentative parse is aborted. */
25641 static void
25642 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25644 cp_parser_context *context = parser->context;
25645 cp_lexer *lexer = parser->lexer;
25647 if (context)
25649 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25650 return;
25651 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25653 while (!cp_lexer_saving_tokens (lexer))
25654 lexer = lexer->next;
25655 cp_lexer_commit_tokens (lexer);
25659 /* Abort the currently active tentative parse. All consumed tokens
25660 will be rolled back, and no diagnostics will be issued. */
25662 static void
25663 cp_parser_abort_tentative_parse (cp_parser* parser)
25665 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25666 || errorcount > 0);
25667 cp_parser_simulate_error (parser);
25668 /* Now, pretend that we want to see if the construct was
25669 successfully parsed. */
25670 cp_parser_parse_definitely (parser);
25673 /* Stop parsing tentatively. If a parse error has occurred, restore the
25674 token stream. Otherwise, commit to the tokens we have consumed.
25675 Returns true if no error occurred; false otherwise. */
25677 static bool
25678 cp_parser_parse_definitely (cp_parser* parser)
25680 bool error_occurred;
25681 cp_parser_context *context;
25683 /* Remember whether or not an error occurred, since we are about to
25684 destroy that information. */
25685 error_occurred = cp_parser_error_occurred (parser);
25686 /* Remove the topmost context from the stack. */
25687 context = parser->context;
25688 parser->context = context->next;
25689 /* If no parse errors occurred, commit to the tentative parse. */
25690 if (!error_occurred)
25692 /* Commit to the tokens read tentatively, unless that was
25693 already done. */
25694 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25695 cp_lexer_commit_tokens (parser->lexer);
25697 pop_to_parent_deferring_access_checks ();
25699 /* Otherwise, if errors occurred, roll back our state so that things
25700 are just as they were before we began the tentative parse. */
25701 else
25703 cp_lexer_rollback_tokens (parser->lexer);
25704 pop_deferring_access_checks ();
25706 /* Add the context to the front of the free list. */
25707 context->next = cp_parser_context_free_list;
25708 cp_parser_context_free_list = context;
25710 return !error_occurred;
25713 /* Returns true if we are parsing tentatively and are not committed to
25714 this tentative parse. */
25716 static bool
25717 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25719 return (cp_parser_parsing_tentatively (parser)
25720 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25723 /* Returns nonzero iff an error has occurred during the most recent
25724 tentative parse. */
25726 static bool
25727 cp_parser_error_occurred (cp_parser* parser)
25729 return (cp_parser_parsing_tentatively (parser)
25730 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25733 /* Returns nonzero if GNU extensions are allowed. */
25735 static bool
25736 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25738 return parser->allow_gnu_extensions_p;
25741 /* Objective-C++ Productions */
25744 /* Parse an Objective-C expression, which feeds into a primary-expression
25745 above.
25747 objc-expression:
25748 objc-message-expression
25749 objc-string-literal
25750 objc-encode-expression
25751 objc-protocol-expression
25752 objc-selector-expression
25754 Returns a tree representation of the expression. */
25756 static tree
25757 cp_parser_objc_expression (cp_parser* parser)
25759 /* Try to figure out what kind of declaration is present. */
25760 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25762 switch (kwd->type)
25764 case CPP_OPEN_SQUARE:
25765 return cp_parser_objc_message_expression (parser);
25767 case CPP_OBJC_STRING:
25768 kwd = cp_lexer_consume_token (parser->lexer);
25769 return objc_build_string_object (kwd->u.value);
25771 case CPP_KEYWORD:
25772 switch (kwd->keyword)
25774 case RID_AT_ENCODE:
25775 return cp_parser_objc_encode_expression (parser);
25777 case RID_AT_PROTOCOL:
25778 return cp_parser_objc_protocol_expression (parser);
25780 case RID_AT_SELECTOR:
25781 return cp_parser_objc_selector_expression (parser);
25783 default:
25784 break;
25786 default:
25787 error_at (kwd->location,
25788 "misplaced %<@%D%> Objective-C++ construct",
25789 kwd->u.value);
25790 cp_parser_skip_to_end_of_block_or_statement (parser);
25793 return error_mark_node;
25796 /* Parse an Objective-C message expression.
25798 objc-message-expression:
25799 [ objc-message-receiver objc-message-args ]
25801 Returns a representation of an Objective-C message. */
25803 static tree
25804 cp_parser_objc_message_expression (cp_parser* parser)
25806 tree receiver, messageargs;
25808 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25809 receiver = cp_parser_objc_message_receiver (parser);
25810 messageargs = cp_parser_objc_message_args (parser);
25811 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25813 return objc_build_message_expr (receiver, messageargs);
25816 /* Parse an objc-message-receiver.
25818 objc-message-receiver:
25819 expression
25820 simple-type-specifier
25822 Returns a representation of the type or expression. */
25824 static tree
25825 cp_parser_objc_message_receiver (cp_parser* parser)
25827 tree rcv;
25829 /* An Objective-C message receiver may be either (1) a type
25830 or (2) an expression. */
25831 cp_parser_parse_tentatively (parser);
25832 rcv = cp_parser_expression (parser);
25834 /* If that worked out, fine. */
25835 if (cp_parser_parse_definitely (parser))
25836 return rcv;
25838 cp_parser_parse_tentatively (parser);
25839 rcv = cp_parser_simple_type_specifier (parser,
25840 /*decl_specs=*/NULL,
25841 CP_PARSER_FLAGS_NONE);
25843 if (cp_parser_parse_definitely (parser))
25844 return objc_get_class_reference (rcv);
25846 cp_parser_error (parser, "objective-c++ message receiver expected");
25847 return error_mark_node;
25850 /* Parse the arguments and selectors comprising an Objective-C message.
25852 objc-message-args:
25853 objc-selector
25854 objc-selector-args
25855 objc-selector-args , objc-comma-args
25857 objc-selector-args:
25858 objc-selector [opt] : assignment-expression
25859 objc-selector-args objc-selector [opt] : assignment-expression
25861 objc-comma-args:
25862 assignment-expression
25863 objc-comma-args , assignment-expression
25865 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25866 selector arguments and TREE_VALUE containing a list of comma
25867 arguments. */
25869 static tree
25870 cp_parser_objc_message_args (cp_parser* parser)
25872 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25873 bool maybe_unary_selector_p = true;
25874 cp_token *token = cp_lexer_peek_token (parser->lexer);
25876 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25878 tree selector = NULL_TREE, arg;
25880 if (token->type != CPP_COLON)
25881 selector = cp_parser_objc_selector (parser);
25883 /* Detect if we have a unary selector. */
25884 if (maybe_unary_selector_p
25885 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25886 return build_tree_list (selector, NULL_TREE);
25888 maybe_unary_selector_p = false;
25889 cp_parser_require (parser, CPP_COLON, RT_COLON);
25890 arg = cp_parser_assignment_expression (parser);
25892 sel_args
25893 = chainon (sel_args,
25894 build_tree_list (selector, arg));
25896 token = cp_lexer_peek_token (parser->lexer);
25899 /* Handle non-selector arguments, if any. */
25900 while (token->type == CPP_COMMA)
25902 tree arg;
25904 cp_lexer_consume_token (parser->lexer);
25905 arg = cp_parser_assignment_expression (parser);
25907 addl_args
25908 = chainon (addl_args,
25909 build_tree_list (NULL_TREE, arg));
25911 token = cp_lexer_peek_token (parser->lexer);
25914 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25916 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25917 return build_tree_list (error_mark_node, error_mark_node);
25920 return build_tree_list (sel_args, addl_args);
25923 /* Parse an Objective-C encode expression.
25925 objc-encode-expression:
25926 @encode objc-typename
25928 Returns an encoded representation of the type argument. */
25930 static tree
25931 cp_parser_objc_encode_expression (cp_parser* parser)
25933 tree type;
25934 cp_token *token;
25936 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25937 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25938 token = cp_lexer_peek_token (parser->lexer);
25939 type = complete_type (cp_parser_type_id (parser));
25940 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25942 if (!type)
25944 error_at (token->location,
25945 "%<@encode%> must specify a type as an argument");
25946 return error_mark_node;
25949 /* This happens if we find @encode(T) (where T is a template
25950 typename or something dependent on a template typename) when
25951 parsing a template. In that case, we can't compile it
25952 immediately, but we rather create an AT_ENCODE_EXPR which will
25953 need to be instantiated when the template is used.
25955 if (dependent_type_p (type))
25957 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25958 TREE_READONLY (value) = 1;
25959 return value;
25962 return objc_build_encode_expr (type);
25965 /* Parse an Objective-C @defs expression. */
25967 static tree
25968 cp_parser_objc_defs_expression (cp_parser *parser)
25970 tree name;
25972 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25973 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25974 name = cp_parser_identifier (parser);
25975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25977 return objc_get_class_ivars (name);
25980 /* Parse an Objective-C protocol expression.
25982 objc-protocol-expression:
25983 @protocol ( identifier )
25985 Returns a representation of the protocol expression. */
25987 static tree
25988 cp_parser_objc_protocol_expression (cp_parser* parser)
25990 tree proto;
25992 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25994 proto = cp_parser_identifier (parser);
25995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25997 return objc_build_protocol_expr (proto);
26000 /* Parse an Objective-C selector expression.
26002 objc-selector-expression:
26003 @selector ( objc-method-signature )
26005 objc-method-signature:
26006 objc-selector
26007 objc-selector-seq
26009 objc-selector-seq:
26010 objc-selector :
26011 objc-selector-seq objc-selector :
26013 Returns a representation of the method selector. */
26015 static tree
26016 cp_parser_objc_selector_expression (cp_parser* parser)
26018 tree sel_seq = NULL_TREE;
26019 bool maybe_unary_selector_p = true;
26020 cp_token *token;
26021 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26023 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26024 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26025 token = cp_lexer_peek_token (parser->lexer);
26027 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26028 || token->type == CPP_SCOPE)
26030 tree selector = NULL_TREE;
26032 if (token->type != CPP_COLON
26033 || token->type == CPP_SCOPE)
26034 selector = cp_parser_objc_selector (parser);
26036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26037 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26039 /* Detect if we have a unary selector. */
26040 if (maybe_unary_selector_p)
26042 sel_seq = selector;
26043 goto finish_selector;
26045 else
26047 cp_parser_error (parser, "expected %<:%>");
26050 maybe_unary_selector_p = false;
26051 token = cp_lexer_consume_token (parser->lexer);
26053 if (token->type == CPP_SCOPE)
26055 sel_seq
26056 = chainon (sel_seq,
26057 build_tree_list (selector, NULL_TREE));
26058 sel_seq
26059 = chainon (sel_seq,
26060 build_tree_list (NULL_TREE, NULL_TREE));
26062 else
26063 sel_seq
26064 = chainon (sel_seq,
26065 build_tree_list (selector, NULL_TREE));
26067 token = cp_lexer_peek_token (parser->lexer);
26070 finish_selector:
26071 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26073 return objc_build_selector_expr (loc, sel_seq);
26076 /* Parse a list of identifiers.
26078 objc-identifier-list:
26079 identifier
26080 objc-identifier-list , identifier
26082 Returns a TREE_LIST of identifier nodes. */
26084 static tree
26085 cp_parser_objc_identifier_list (cp_parser* parser)
26087 tree identifier;
26088 tree list;
26089 cp_token *sep;
26091 identifier = cp_parser_identifier (parser);
26092 if (identifier == error_mark_node)
26093 return error_mark_node;
26095 list = build_tree_list (NULL_TREE, identifier);
26096 sep = cp_lexer_peek_token (parser->lexer);
26098 while (sep->type == CPP_COMMA)
26100 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26101 identifier = cp_parser_identifier (parser);
26102 if (identifier == error_mark_node)
26103 return list;
26105 list = chainon (list, build_tree_list (NULL_TREE,
26106 identifier));
26107 sep = cp_lexer_peek_token (parser->lexer);
26110 return list;
26113 /* Parse an Objective-C alias declaration.
26115 objc-alias-declaration:
26116 @compatibility_alias identifier identifier ;
26118 This function registers the alias mapping with the Objective-C front end.
26119 It returns nothing. */
26121 static void
26122 cp_parser_objc_alias_declaration (cp_parser* parser)
26124 tree alias, orig;
26126 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26127 alias = cp_parser_identifier (parser);
26128 orig = cp_parser_identifier (parser);
26129 objc_declare_alias (alias, orig);
26130 cp_parser_consume_semicolon_at_end_of_statement (parser);
26133 /* Parse an Objective-C class forward-declaration.
26135 objc-class-declaration:
26136 @class objc-identifier-list ;
26138 The function registers the forward declarations with the Objective-C
26139 front end. It returns nothing. */
26141 static void
26142 cp_parser_objc_class_declaration (cp_parser* parser)
26144 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26145 while (true)
26147 tree id;
26149 id = cp_parser_identifier (parser);
26150 if (id == error_mark_node)
26151 break;
26153 objc_declare_class (id);
26155 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26156 cp_lexer_consume_token (parser->lexer);
26157 else
26158 break;
26160 cp_parser_consume_semicolon_at_end_of_statement (parser);
26163 /* Parse a list of Objective-C protocol references.
26165 objc-protocol-refs-opt:
26166 objc-protocol-refs [opt]
26168 objc-protocol-refs:
26169 < objc-identifier-list >
26171 Returns a TREE_LIST of identifiers, if any. */
26173 static tree
26174 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26176 tree protorefs = NULL_TREE;
26178 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26180 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26181 protorefs = cp_parser_objc_identifier_list (parser);
26182 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26185 return protorefs;
26188 /* Parse a Objective-C visibility specification. */
26190 static void
26191 cp_parser_objc_visibility_spec (cp_parser* parser)
26193 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26195 switch (vis->keyword)
26197 case RID_AT_PRIVATE:
26198 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26199 break;
26200 case RID_AT_PROTECTED:
26201 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26202 break;
26203 case RID_AT_PUBLIC:
26204 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26205 break;
26206 case RID_AT_PACKAGE:
26207 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26208 break;
26209 default:
26210 return;
26213 /* Eat '@private'/'@protected'/'@public'. */
26214 cp_lexer_consume_token (parser->lexer);
26217 /* Parse an Objective-C method type. Return 'true' if it is a class
26218 (+) method, and 'false' if it is an instance (-) method. */
26220 static inline bool
26221 cp_parser_objc_method_type (cp_parser* parser)
26223 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26224 return true;
26225 else
26226 return false;
26229 /* Parse an Objective-C protocol qualifier. */
26231 static tree
26232 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26234 tree quals = NULL_TREE, node;
26235 cp_token *token = cp_lexer_peek_token (parser->lexer);
26237 node = token->u.value;
26239 while (node && identifier_p (node)
26240 && (node == ridpointers [(int) RID_IN]
26241 || node == ridpointers [(int) RID_OUT]
26242 || node == ridpointers [(int) RID_INOUT]
26243 || node == ridpointers [(int) RID_BYCOPY]
26244 || node == ridpointers [(int) RID_BYREF]
26245 || node == ridpointers [(int) RID_ONEWAY]))
26247 quals = tree_cons (NULL_TREE, node, quals);
26248 cp_lexer_consume_token (parser->lexer);
26249 token = cp_lexer_peek_token (parser->lexer);
26250 node = token->u.value;
26253 return quals;
26256 /* Parse an Objective-C typename. */
26258 static tree
26259 cp_parser_objc_typename (cp_parser* parser)
26261 tree type_name = NULL_TREE;
26263 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26265 tree proto_quals, cp_type = NULL_TREE;
26267 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26268 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26270 /* An ObjC type name may consist of just protocol qualifiers, in which
26271 case the type shall default to 'id'. */
26272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26274 cp_type = cp_parser_type_id (parser);
26276 /* If the type could not be parsed, an error has already
26277 been produced. For error recovery, behave as if it had
26278 not been specified, which will use the default type
26279 'id'. */
26280 if (cp_type == error_mark_node)
26282 cp_type = NULL_TREE;
26283 /* We need to skip to the closing parenthesis as
26284 cp_parser_type_id() does not seem to do it for
26285 us. */
26286 cp_parser_skip_to_closing_parenthesis (parser,
26287 /*recovering=*/true,
26288 /*or_comma=*/false,
26289 /*consume_paren=*/false);
26293 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26294 type_name = build_tree_list (proto_quals, cp_type);
26297 return type_name;
26300 /* Check to see if TYPE refers to an Objective-C selector name. */
26302 static bool
26303 cp_parser_objc_selector_p (enum cpp_ttype type)
26305 return (type == CPP_NAME || type == CPP_KEYWORD
26306 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26307 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26308 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26309 || type == CPP_XOR || type == CPP_XOR_EQ);
26312 /* Parse an Objective-C selector. */
26314 static tree
26315 cp_parser_objc_selector (cp_parser* parser)
26317 cp_token *token = cp_lexer_consume_token (parser->lexer);
26319 if (!cp_parser_objc_selector_p (token->type))
26321 error_at (token->location, "invalid Objective-C++ selector name");
26322 return error_mark_node;
26325 /* C++ operator names are allowed to appear in ObjC selectors. */
26326 switch (token->type)
26328 case CPP_AND_AND: return get_identifier ("and");
26329 case CPP_AND_EQ: return get_identifier ("and_eq");
26330 case CPP_AND: return get_identifier ("bitand");
26331 case CPP_OR: return get_identifier ("bitor");
26332 case CPP_COMPL: return get_identifier ("compl");
26333 case CPP_NOT: return get_identifier ("not");
26334 case CPP_NOT_EQ: return get_identifier ("not_eq");
26335 case CPP_OR_OR: return get_identifier ("or");
26336 case CPP_OR_EQ: return get_identifier ("or_eq");
26337 case CPP_XOR: return get_identifier ("xor");
26338 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26339 default: return token->u.value;
26343 /* Parse an Objective-C params list. */
26345 static tree
26346 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26348 tree params = NULL_TREE;
26349 bool maybe_unary_selector_p = true;
26350 cp_token *token = cp_lexer_peek_token (parser->lexer);
26352 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26354 tree selector = NULL_TREE, type_name, identifier;
26355 tree parm_attr = NULL_TREE;
26357 if (token->keyword == RID_ATTRIBUTE)
26358 break;
26360 if (token->type != CPP_COLON)
26361 selector = cp_parser_objc_selector (parser);
26363 /* Detect if we have a unary selector. */
26364 if (maybe_unary_selector_p
26365 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26367 params = selector; /* Might be followed by attributes. */
26368 break;
26371 maybe_unary_selector_p = false;
26372 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26374 /* Something went quite wrong. There should be a colon
26375 here, but there is not. Stop parsing parameters. */
26376 break;
26378 type_name = cp_parser_objc_typename (parser);
26379 /* New ObjC allows attributes on parameters too. */
26380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26381 parm_attr = cp_parser_attributes_opt (parser);
26382 identifier = cp_parser_identifier (parser);
26384 params
26385 = chainon (params,
26386 objc_build_keyword_decl (selector,
26387 type_name,
26388 identifier,
26389 parm_attr));
26391 token = cp_lexer_peek_token (parser->lexer);
26394 if (params == NULL_TREE)
26396 cp_parser_error (parser, "objective-c++ method declaration is expected");
26397 return error_mark_node;
26400 /* We allow tail attributes for the method. */
26401 if (token->keyword == RID_ATTRIBUTE)
26403 *attributes = cp_parser_attributes_opt (parser);
26404 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26405 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26406 return params;
26407 cp_parser_error (parser,
26408 "method attributes must be specified at the end");
26409 return error_mark_node;
26412 if (params == NULL_TREE)
26414 cp_parser_error (parser, "objective-c++ method declaration is expected");
26415 return error_mark_node;
26417 return params;
26420 /* Parse the non-keyword Objective-C params. */
26422 static tree
26423 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26424 tree* attributes)
26426 tree params = make_node (TREE_LIST);
26427 cp_token *token = cp_lexer_peek_token (parser->lexer);
26428 *ellipsisp = false; /* Initially, assume no ellipsis. */
26430 while (token->type == CPP_COMMA)
26432 cp_parameter_declarator *parmdecl;
26433 tree parm;
26435 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26436 token = cp_lexer_peek_token (parser->lexer);
26438 if (token->type == CPP_ELLIPSIS)
26440 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26441 *ellipsisp = true;
26442 token = cp_lexer_peek_token (parser->lexer);
26443 break;
26446 /* TODO: parse attributes for tail parameters. */
26447 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26448 parm = grokdeclarator (parmdecl->declarator,
26449 &parmdecl->decl_specifiers,
26450 PARM, /*initialized=*/0,
26451 /*attrlist=*/NULL);
26453 chainon (params, build_tree_list (NULL_TREE, parm));
26454 token = cp_lexer_peek_token (parser->lexer);
26457 /* We allow tail attributes for the method. */
26458 if (token->keyword == RID_ATTRIBUTE)
26460 if (*attributes == NULL_TREE)
26462 *attributes = cp_parser_attributes_opt (parser);
26463 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26464 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26465 return params;
26467 else
26468 /* We have an error, but parse the attributes, so that we can
26469 carry on. */
26470 *attributes = cp_parser_attributes_opt (parser);
26472 cp_parser_error (parser,
26473 "method attributes must be specified at the end");
26474 return error_mark_node;
26477 return params;
26480 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26482 static void
26483 cp_parser_objc_interstitial_code (cp_parser* parser)
26485 cp_token *token = cp_lexer_peek_token (parser->lexer);
26487 /* If the next token is `extern' and the following token is a string
26488 literal, then we have a linkage specification. */
26489 if (token->keyword == RID_EXTERN
26490 && cp_parser_is_pure_string_literal
26491 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26492 cp_parser_linkage_specification (parser);
26493 /* Handle #pragma, if any. */
26494 else if (token->type == CPP_PRAGMA)
26495 cp_parser_pragma (parser, pragma_objc_icode);
26496 /* Allow stray semicolons. */
26497 else if (token->type == CPP_SEMICOLON)
26498 cp_lexer_consume_token (parser->lexer);
26499 /* Mark methods as optional or required, when building protocols. */
26500 else if (token->keyword == RID_AT_OPTIONAL)
26502 cp_lexer_consume_token (parser->lexer);
26503 objc_set_method_opt (true);
26505 else if (token->keyword == RID_AT_REQUIRED)
26507 cp_lexer_consume_token (parser->lexer);
26508 objc_set_method_opt (false);
26510 else if (token->keyword == RID_NAMESPACE)
26511 cp_parser_namespace_definition (parser);
26512 /* Other stray characters must generate errors. */
26513 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26515 cp_lexer_consume_token (parser->lexer);
26516 error ("stray %qs between Objective-C++ methods",
26517 token->type == CPP_OPEN_BRACE ? "{" : "}");
26519 /* Finally, try to parse a block-declaration, or a function-definition. */
26520 else
26521 cp_parser_block_declaration (parser, /*statement_p=*/false);
26524 /* Parse a method signature. */
26526 static tree
26527 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26529 tree rettype, kwdparms, optparms;
26530 bool ellipsis = false;
26531 bool is_class_method;
26533 is_class_method = cp_parser_objc_method_type (parser);
26534 rettype = cp_parser_objc_typename (parser);
26535 *attributes = NULL_TREE;
26536 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26537 if (kwdparms == error_mark_node)
26538 return error_mark_node;
26539 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26540 if (optparms == error_mark_node)
26541 return error_mark_node;
26543 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26546 static bool
26547 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26549 tree tattr;
26550 cp_lexer_save_tokens (parser->lexer);
26551 tattr = cp_parser_attributes_opt (parser);
26552 gcc_assert (tattr) ;
26554 /* If the attributes are followed by a method introducer, this is not allowed.
26555 Dump the attributes and flag the situation. */
26556 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26557 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26558 return true;
26560 /* Otherwise, the attributes introduce some interstitial code, possibly so
26561 rewind to allow that check. */
26562 cp_lexer_rollback_tokens (parser->lexer);
26563 return false;
26566 /* Parse an Objective-C method prototype list. */
26568 static void
26569 cp_parser_objc_method_prototype_list (cp_parser* parser)
26571 cp_token *token = cp_lexer_peek_token (parser->lexer);
26573 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26575 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26577 tree attributes, sig;
26578 bool is_class_method;
26579 if (token->type == CPP_PLUS)
26580 is_class_method = true;
26581 else
26582 is_class_method = false;
26583 sig = cp_parser_objc_method_signature (parser, &attributes);
26584 if (sig == error_mark_node)
26586 cp_parser_skip_to_end_of_block_or_statement (parser);
26587 token = cp_lexer_peek_token (parser->lexer);
26588 continue;
26590 objc_add_method_declaration (is_class_method, sig, attributes);
26591 cp_parser_consume_semicolon_at_end_of_statement (parser);
26593 else if (token->keyword == RID_AT_PROPERTY)
26594 cp_parser_objc_at_property_declaration (parser);
26595 else if (token->keyword == RID_ATTRIBUTE
26596 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26597 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26598 OPT_Wattributes,
26599 "prefix attributes are ignored for methods");
26600 else
26601 /* Allow for interspersed non-ObjC++ code. */
26602 cp_parser_objc_interstitial_code (parser);
26604 token = cp_lexer_peek_token (parser->lexer);
26607 if (token->type != CPP_EOF)
26608 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26609 else
26610 cp_parser_error (parser, "expected %<@end%>");
26612 objc_finish_interface ();
26615 /* Parse an Objective-C method definition list. */
26617 static void
26618 cp_parser_objc_method_definition_list (cp_parser* parser)
26620 cp_token *token = cp_lexer_peek_token (parser->lexer);
26622 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26624 tree meth;
26626 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26628 cp_token *ptk;
26629 tree sig, attribute;
26630 bool is_class_method;
26631 if (token->type == CPP_PLUS)
26632 is_class_method = true;
26633 else
26634 is_class_method = false;
26635 push_deferring_access_checks (dk_deferred);
26636 sig = cp_parser_objc_method_signature (parser, &attribute);
26637 if (sig == error_mark_node)
26639 cp_parser_skip_to_end_of_block_or_statement (parser);
26640 token = cp_lexer_peek_token (parser->lexer);
26641 continue;
26643 objc_start_method_definition (is_class_method, sig, attribute,
26644 NULL_TREE);
26646 /* For historical reasons, we accept an optional semicolon. */
26647 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26648 cp_lexer_consume_token (parser->lexer);
26650 ptk = cp_lexer_peek_token (parser->lexer);
26651 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26652 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26654 perform_deferred_access_checks (tf_warning_or_error);
26655 stop_deferring_access_checks ();
26656 meth = cp_parser_function_definition_after_declarator (parser,
26657 false);
26658 pop_deferring_access_checks ();
26659 objc_finish_method_definition (meth);
26662 /* The following case will be removed once @synthesize is
26663 completely implemented. */
26664 else if (token->keyword == RID_AT_PROPERTY)
26665 cp_parser_objc_at_property_declaration (parser);
26666 else if (token->keyword == RID_AT_SYNTHESIZE)
26667 cp_parser_objc_at_synthesize_declaration (parser);
26668 else if (token->keyword == RID_AT_DYNAMIC)
26669 cp_parser_objc_at_dynamic_declaration (parser);
26670 else if (token->keyword == RID_ATTRIBUTE
26671 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26672 warning_at (token->location, OPT_Wattributes,
26673 "prefix attributes are ignored for methods");
26674 else
26675 /* Allow for interspersed non-ObjC++ code. */
26676 cp_parser_objc_interstitial_code (parser);
26678 token = cp_lexer_peek_token (parser->lexer);
26681 if (token->type != CPP_EOF)
26682 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26683 else
26684 cp_parser_error (parser, "expected %<@end%>");
26686 objc_finish_implementation ();
26689 /* Parse Objective-C ivars. */
26691 static void
26692 cp_parser_objc_class_ivars (cp_parser* parser)
26694 cp_token *token = cp_lexer_peek_token (parser->lexer);
26696 if (token->type != CPP_OPEN_BRACE)
26697 return; /* No ivars specified. */
26699 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26700 token = cp_lexer_peek_token (parser->lexer);
26702 while (token->type != CPP_CLOSE_BRACE
26703 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26705 cp_decl_specifier_seq declspecs;
26706 int decl_class_or_enum_p;
26707 tree prefix_attributes;
26709 cp_parser_objc_visibility_spec (parser);
26711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26712 break;
26714 cp_parser_decl_specifier_seq (parser,
26715 CP_PARSER_FLAGS_OPTIONAL,
26716 &declspecs,
26717 &decl_class_or_enum_p);
26719 /* auto, register, static, extern, mutable. */
26720 if (declspecs.storage_class != sc_none)
26722 cp_parser_error (parser, "invalid type for instance variable");
26723 declspecs.storage_class = sc_none;
26726 /* thread_local. */
26727 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26729 cp_parser_error (parser, "invalid type for instance variable");
26730 declspecs.locations[ds_thread] = 0;
26733 /* typedef. */
26734 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26736 cp_parser_error (parser, "invalid type for instance variable");
26737 declspecs.locations[ds_typedef] = 0;
26740 prefix_attributes = declspecs.attributes;
26741 declspecs.attributes = NULL_TREE;
26743 /* Keep going until we hit the `;' at the end of the
26744 declaration. */
26745 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26747 tree width = NULL_TREE, attributes, first_attribute, decl;
26748 cp_declarator *declarator = NULL;
26749 int ctor_dtor_or_conv_p;
26751 /* Check for a (possibly unnamed) bitfield declaration. */
26752 token = cp_lexer_peek_token (parser->lexer);
26753 if (token->type == CPP_COLON)
26754 goto eat_colon;
26756 if (token->type == CPP_NAME
26757 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26758 == CPP_COLON))
26760 /* Get the name of the bitfield. */
26761 declarator = make_id_declarator (NULL_TREE,
26762 cp_parser_identifier (parser),
26763 sfk_none);
26765 eat_colon:
26766 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26767 /* Get the width of the bitfield. */
26768 width
26769 = cp_parser_constant_expression (parser);
26771 else
26773 /* Parse the declarator. */
26774 declarator
26775 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26776 &ctor_dtor_or_conv_p,
26777 /*parenthesized_p=*/NULL,
26778 /*member_p=*/false,
26779 /*friend_p=*/false);
26782 /* Look for attributes that apply to the ivar. */
26783 attributes = cp_parser_attributes_opt (parser);
26784 /* Remember which attributes are prefix attributes and
26785 which are not. */
26786 first_attribute = attributes;
26787 /* Combine the attributes. */
26788 attributes = chainon (prefix_attributes, attributes);
26790 if (width)
26791 /* Create the bitfield declaration. */
26792 decl = grokbitfield (declarator, &declspecs,
26793 width,
26794 attributes);
26795 else
26796 decl = grokfield (declarator, &declspecs,
26797 NULL_TREE, /*init_const_expr_p=*/false,
26798 NULL_TREE, attributes);
26800 /* Add the instance variable. */
26801 if (decl != error_mark_node && decl != NULL_TREE)
26802 objc_add_instance_variable (decl);
26804 /* Reset PREFIX_ATTRIBUTES. */
26805 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26806 attributes = TREE_CHAIN (attributes);
26807 if (attributes)
26808 TREE_CHAIN (attributes) = NULL_TREE;
26810 token = cp_lexer_peek_token (parser->lexer);
26812 if (token->type == CPP_COMMA)
26814 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26815 continue;
26817 break;
26820 cp_parser_consume_semicolon_at_end_of_statement (parser);
26821 token = cp_lexer_peek_token (parser->lexer);
26824 if (token->keyword == RID_AT_END)
26825 cp_parser_error (parser, "expected %<}%>");
26827 /* Do not consume the RID_AT_END, so it will be read again as terminating
26828 the @interface of @implementation. */
26829 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26830 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26832 /* For historical reasons, we accept an optional semicolon. */
26833 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26834 cp_lexer_consume_token (parser->lexer);
26837 /* Parse an Objective-C protocol declaration. */
26839 static void
26840 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26842 tree proto, protorefs;
26843 cp_token *tok;
26845 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26848 tok = cp_lexer_peek_token (parser->lexer);
26849 error_at (tok->location, "identifier expected after %<@protocol%>");
26850 cp_parser_consume_semicolon_at_end_of_statement (parser);
26851 return;
26854 /* See if we have a forward declaration or a definition. */
26855 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26857 /* Try a forward declaration first. */
26858 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26860 while (true)
26862 tree id;
26864 id = cp_parser_identifier (parser);
26865 if (id == error_mark_node)
26866 break;
26868 objc_declare_protocol (id, attributes);
26870 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26871 cp_lexer_consume_token (parser->lexer);
26872 else
26873 break;
26875 cp_parser_consume_semicolon_at_end_of_statement (parser);
26878 /* Ok, we got a full-fledged definition (or at least should). */
26879 else
26881 proto = cp_parser_identifier (parser);
26882 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26883 objc_start_protocol (proto, protorefs, attributes);
26884 cp_parser_objc_method_prototype_list (parser);
26888 /* Parse an Objective-C superclass or category. */
26890 static void
26891 cp_parser_objc_superclass_or_category (cp_parser *parser,
26892 bool iface_p,
26893 tree *super,
26894 tree *categ, bool *is_class_extension)
26896 cp_token *next = cp_lexer_peek_token (parser->lexer);
26898 *super = *categ = NULL_TREE;
26899 *is_class_extension = false;
26900 if (next->type == CPP_COLON)
26902 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26903 *super = cp_parser_identifier (parser);
26905 else if (next->type == CPP_OPEN_PAREN)
26907 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26909 /* If there is no category name, and this is an @interface, we
26910 have a class extension. */
26911 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26913 *categ = NULL_TREE;
26914 *is_class_extension = true;
26916 else
26917 *categ = cp_parser_identifier (parser);
26919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26923 /* Parse an Objective-C class interface. */
26925 static void
26926 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26928 tree name, super, categ, protos;
26929 bool is_class_extension;
26931 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26932 name = cp_parser_identifier (parser);
26933 if (name == error_mark_node)
26935 /* It's hard to recover because even if valid @interface stuff
26936 is to follow, we can't compile it (or validate it) if we
26937 don't even know which class it refers to. Let's assume this
26938 was a stray '@interface' token in the stream and skip it.
26940 return;
26942 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26943 &is_class_extension);
26944 protos = cp_parser_objc_protocol_refs_opt (parser);
26946 /* We have either a class or a category on our hands. */
26947 if (categ || is_class_extension)
26948 objc_start_category_interface (name, categ, protos, attributes);
26949 else
26951 objc_start_class_interface (name, super, protos, attributes);
26952 /* Handle instance variable declarations, if any. */
26953 cp_parser_objc_class_ivars (parser);
26954 objc_continue_interface ();
26957 cp_parser_objc_method_prototype_list (parser);
26960 /* Parse an Objective-C class implementation. */
26962 static void
26963 cp_parser_objc_class_implementation (cp_parser* parser)
26965 tree name, super, categ;
26966 bool is_class_extension;
26968 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26969 name = cp_parser_identifier (parser);
26970 if (name == error_mark_node)
26972 /* It's hard to recover because even if valid @implementation
26973 stuff is to follow, we can't compile it (or validate it) if
26974 we don't even know which class it refers to. Let's assume
26975 this was a stray '@implementation' token in the stream and
26976 skip it.
26978 return;
26980 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26981 &is_class_extension);
26983 /* We have either a class or a category on our hands. */
26984 if (categ)
26985 objc_start_category_implementation (name, categ);
26986 else
26988 objc_start_class_implementation (name, super);
26989 /* Handle instance variable declarations, if any. */
26990 cp_parser_objc_class_ivars (parser);
26991 objc_continue_implementation ();
26994 cp_parser_objc_method_definition_list (parser);
26997 /* Consume the @end token and finish off the implementation. */
26999 static void
27000 cp_parser_objc_end_implementation (cp_parser* parser)
27002 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27003 objc_finish_implementation ();
27006 /* Parse an Objective-C declaration. */
27008 static void
27009 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27011 /* Try to figure out what kind of declaration is present. */
27012 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27014 if (attributes)
27015 switch (kwd->keyword)
27017 case RID_AT_ALIAS:
27018 case RID_AT_CLASS:
27019 case RID_AT_END:
27020 error_at (kwd->location, "attributes may not be specified before"
27021 " the %<@%D%> Objective-C++ keyword",
27022 kwd->u.value);
27023 attributes = NULL;
27024 break;
27025 case RID_AT_IMPLEMENTATION:
27026 warning_at (kwd->location, OPT_Wattributes,
27027 "prefix attributes are ignored before %<@%D%>",
27028 kwd->u.value);
27029 attributes = NULL;
27030 default:
27031 break;
27034 switch (kwd->keyword)
27036 case RID_AT_ALIAS:
27037 cp_parser_objc_alias_declaration (parser);
27038 break;
27039 case RID_AT_CLASS:
27040 cp_parser_objc_class_declaration (parser);
27041 break;
27042 case RID_AT_PROTOCOL:
27043 cp_parser_objc_protocol_declaration (parser, attributes);
27044 break;
27045 case RID_AT_INTERFACE:
27046 cp_parser_objc_class_interface (parser, attributes);
27047 break;
27048 case RID_AT_IMPLEMENTATION:
27049 cp_parser_objc_class_implementation (parser);
27050 break;
27051 case RID_AT_END:
27052 cp_parser_objc_end_implementation (parser);
27053 break;
27054 default:
27055 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27056 kwd->u.value);
27057 cp_parser_skip_to_end_of_block_or_statement (parser);
27061 /* Parse an Objective-C try-catch-finally statement.
27063 objc-try-catch-finally-stmt:
27064 @try compound-statement objc-catch-clause-seq [opt]
27065 objc-finally-clause [opt]
27067 objc-catch-clause-seq:
27068 objc-catch-clause objc-catch-clause-seq [opt]
27070 objc-catch-clause:
27071 @catch ( objc-exception-declaration ) compound-statement
27073 objc-finally-clause:
27074 @finally compound-statement
27076 objc-exception-declaration:
27077 parameter-declaration
27078 '...'
27080 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27082 Returns NULL_TREE.
27084 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27085 for C. Keep them in sync. */
27087 static tree
27088 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27090 location_t location;
27091 tree stmt;
27093 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27094 location = cp_lexer_peek_token (parser->lexer)->location;
27095 objc_maybe_warn_exceptions (location);
27096 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27097 node, lest it get absorbed into the surrounding block. */
27098 stmt = push_stmt_list ();
27099 cp_parser_compound_statement (parser, NULL, false, false);
27100 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27102 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27104 cp_parameter_declarator *parm;
27105 tree parameter_declaration = error_mark_node;
27106 bool seen_open_paren = false;
27108 cp_lexer_consume_token (parser->lexer);
27109 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27110 seen_open_paren = true;
27111 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27113 /* We have "@catch (...)" (where the '...' are literally
27114 what is in the code). Skip the '...'.
27115 parameter_declaration is set to NULL_TREE, and
27116 objc_being_catch_clauses() knows that that means
27117 '...'. */
27118 cp_lexer_consume_token (parser->lexer);
27119 parameter_declaration = NULL_TREE;
27121 else
27123 /* We have "@catch (NSException *exception)" or something
27124 like that. Parse the parameter declaration. */
27125 parm = cp_parser_parameter_declaration (parser, false, NULL);
27126 if (parm == NULL)
27127 parameter_declaration = error_mark_node;
27128 else
27129 parameter_declaration = grokdeclarator (parm->declarator,
27130 &parm->decl_specifiers,
27131 PARM, /*initialized=*/0,
27132 /*attrlist=*/NULL);
27134 if (seen_open_paren)
27135 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27136 else
27138 /* If there was no open parenthesis, we are recovering from
27139 an error, and we are trying to figure out what mistake
27140 the user has made. */
27142 /* If there is an immediate closing parenthesis, the user
27143 probably forgot the opening one (ie, they typed "@catch
27144 NSException *e)". Parse the closing parenthesis and keep
27145 going. */
27146 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27147 cp_lexer_consume_token (parser->lexer);
27149 /* If these is no immediate closing parenthesis, the user
27150 probably doesn't know that parenthesis are required at
27151 all (ie, they typed "@catch NSException *e"). So, just
27152 forget about the closing parenthesis and keep going. */
27154 objc_begin_catch_clause (parameter_declaration);
27155 cp_parser_compound_statement (parser, NULL, false, false);
27156 objc_finish_catch_clause ();
27158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27160 cp_lexer_consume_token (parser->lexer);
27161 location = cp_lexer_peek_token (parser->lexer)->location;
27162 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27163 node, lest it get absorbed into the surrounding block. */
27164 stmt = push_stmt_list ();
27165 cp_parser_compound_statement (parser, NULL, false, false);
27166 objc_build_finally_clause (location, pop_stmt_list (stmt));
27169 return objc_finish_try_stmt ();
27172 /* Parse an Objective-C synchronized statement.
27174 objc-synchronized-stmt:
27175 @synchronized ( expression ) compound-statement
27177 Returns NULL_TREE. */
27179 static tree
27180 cp_parser_objc_synchronized_statement (cp_parser *parser)
27182 location_t location;
27183 tree lock, stmt;
27185 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27187 location = cp_lexer_peek_token (parser->lexer)->location;
27188 objc_maybe_warn_exceptions (location);
27189 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27190 lock = cp_parser_expression (parser);
27191 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27193 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27194 node, lest it get absorbed into the surrounding block. */
27195 stmt = push_stmt_list ();
27196 cp_parser_compound_statement (parser, NULL, false, false);
27198 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27201 /* Parse an Objective-C throw statement.
27203 objc-throw-stmt:
27204 @throw assignment-expression [opt] ;
27206 Returns a constructed '@throw' statement. */
27208 static tree
27209 cp_parser_objc_throw_statement (cp_parser *parser)
27211 tree expr = NULL_TREE;
27212 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27214 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27217 expr = cp_parser_expression (parser);
27219 cp_parser_consume_semicolon_at_end_of_statement (parser);
27221 return objc_build_throw_stmt (loc, expr);
27224 /* Parse an Objective-C statement. */
27226 static tree
27227 cp_parser_objc_statement (cp_parser * parser)
27229 /* Try to figure out what kind of declaration is present. */
27230 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27232 switch (kwd->keyword)
27234 case RID_AT_TRY:
27235 return cp_parser_objc_try_catch_finally_statement (parser);
27236 case RID_AT_SYNCHRONIZED:
27237 return cp_parser_objc_synchronized_statement (parser);
27238 case RID_AT_THROW:
27239 return cp_parser_objc_throw_statement (parser);
27240 default:
27241 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27242 kwd->u.value);
27243 cp_parser_skip_to_end_of_block_or_statement (parser);
27246 return error_mark_node;
27249 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27250 look ahead to see if an objc keyword follows the attributes. This
27251 is to detect the use of prefix attributes on ObjC @interface and
27252 @protocol. */
27254 static bool
27255 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27257 cp_lexer_save_tokens (parser->lexer);
27258 *attrib = cp_parser_attributes_opt (parser);
27259 gcc_assert (*attrib);
27260 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27262 cp_lexer_commit_tokens (parser->lexer);
27263 return true;
27265 cp_lexer_rollback_tokens (parser->lexer);
27266 return false;
27269 /* This routine is a minimal replacement for
27270 c_parser_struct_declaration () used when parsing the list of
27271 types/names or ObjC++ properties. For example, when parsing the
27272 code
27274 @property (readonly) int a, b, c;
27276 this function is responsible for parsing "int a, int b, int c" and
27277 returning the declarations as CHAIN of DECLs.
27279 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27280 similar parsing. */
27281 static tree
27282 cp_parser_objc_struct_declaration (cp_parser *parser)
27284 tree decls = NULL_TREE;
27285 cp_decl_specifier_seq declspecs;
27286 int decl_class_or_enum_p;
27287 tree prefix_attributes;
27289 cp_parser_decl_specifier_seq (parser,
27290 CP_PARSER_FLAGS_NONE,
27291 &declspecs,
27292 &decl_class_or_enum_p);
27294 if (declspecs.type == error_mark_node)
27295 return error_mark_node;
27297 /* auto, register, static, extern, mutable. */
27298 if (declspecs.storage_class != sc_none)
27300 cp_parser_error (parser, "invalid type for property");
27301 declspecs.storage_class = sc_none;
27304 /* thread_local. */
27305 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27307 cp_parser_error (parser, "invalid type for property");
27308 declspecs.locations[ds_thread] = 0;
27311 /* typedef. */
27312 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27314 cp_parser_error (parser, "invalid type for property");
27315 declspecs.locations[ds_typedef] = 0;
27318 prefix_attributes = declspecs.attributes;
27319 declspecs.attributes = NULL_TREE;
27321 /* Keep going until we hit the `;' at the end of the declaration. */
27322 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27324 tree attributes, first_attribute, decl;
27325 cp_declarator *declarator;
27326 cp_token *token;
27328 /* Parse the declarator. */
27329 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27330 NULL, NULL, false, false);
27332 /* Look for attributes that apply to the ivar. */
27333 attributes = cp_parser_attributes_opt (parser);
27334 /* Remember which attributes are prefix attributes and
27335 which are not. */
27336 first_attribute = attributes;
27337 /* Combine the attributes. */
27338 attributes = chainon (prefix_attributes, attributes);
27340 decl = grokfield (declarator, &declspecs,
27341 NULL_TREE, /*init_const_expr_p=*/false,
27342 NULL_TREE, attributes);
27344 if (decl == error_mark_node || decl == NULL_TREE)
27345 return error_mark_node;
27347 /* Reset PREFIX_ATTRIBUTES. */
27348 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27349 attributes = TREE_CHAIN (attributes);
27350 if (attributes)
27351 TREE_CHAIN (attributes) = NULL_TREE;
27353 DECL_CHAIN (decl) = decls;
27354 decls = decl;
27356 token = cp_lexer_peek_token (parser->lexer);
27357 if (token->type == CPP_COMMA)
27359 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27360 continue;
27362 else
27363 break;
27365 return decls;
27368 /* Parse an Objective-C @property declaration. The syntax is:
27370 objc-property-declaration:
27371 '@property' objc-property-attributes[opt] struct-declaration ;
27373 objc-property-attributes:
27374 '(' objc-property-attribute-list ')'
27376 objc-property-attribute-list:
27377 objc-property-attribute
27378 objc-property-attribute-list, objc-property-attribute
27380 objc-property-attribute
27381 'getter' = identifier
27382 'setter' = identifier
27383 'readonly'
27384 'readwrite'
27385 'assign'
27386 'retain'
27387 'copy'
27388 'nonatomic'
27390 For example:
27391 @property NSString *name;
27392 @property (readonly) id object;
27393 @property (retain, nonatomic, getter=getTheName) id name;
27394 @property int a, b, c;
27396 PS: This function is identical to
27397 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27398 static void
27399 cp_parser_objc_at_property_declaration (cp_parser *parser)
27401 /* The following variables hold the attributes of the properties as
27402 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27403 seen. When we see an attribute, we set them to 'true' (if they
27404 are boolean properties) or to the identifier (if they have an
27405 argument, ie, for getter and setter). Note that here we only
27406 parse the list of attributes, check the syntax and accumulate the
27407 attributes that we find. objc_add_property_declaration() will
27408 then process the information. */
27409 bool property_assign = false;
27410 bool property_copy = false;
27411 tree property_getter_ident = NULL_TREE;
27412 bool property_nonatomic = false;
27413 bool property_readonly = false;
27414 bool property_readwrite = false;
27415 bool property_retain = false;
27416 tree property_setter_ident = NULL_TREE;
27418 /* 'properties' is the list of properties that we read. Usually a
27419 single one, but maybe more (eg, in "@property int a, b, c;" there
27420 are three). */
27421 tree properties;
27422 location_t loc;
27424 loc = cp_lexer_peek_token (parser->lexer)->location;
27426 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27428 /* Parse the optional attribute list... */
27429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27431 /* Eat the '('. */
27432 cp_lexer_consume_token (parser->lexer);
27434 while (true)
27436 bool syntax_error = false;
27437 cp_token *token = cp_lexer_peek_token (parser->lexer);
27438 enum rid keyword;
27440 if (token->type != CPP_NAME)
27442 cp_parser_error (parser, "expected identifier");
27443 break;
27445 keyword = C_RID_CODE (token->u.value);
27446 cp_lexer_consume_token (parser->lexer);
27447 switch (keyword)
27449 case RID_ASSIGN: property_assign = true; break;
27450 case RID_COPY: property_copy = true; break;
27451 case RID_NONATOMIC: property_nonatomic = true; break;
27452 case RID_READONLY: property_readonly = true; break;
27453 case RID_READWRITE: property_readwrite = true; break;
27454 case RID_RETAIN: property_retain = true; break;
27456 case RID_GETTER:
27457 case RID_SETTER:
27458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27460 if (keyword == RID_GETTER)
27461 cp_parser_error (parser,
27462 "missing %<=%> (after %<getter%> attribute)");
27463 else
27464 cp_parser_error (parser,
27465 "missing %<=%> (after %<setter%> attribute)");
27466 syntax_error = true;
27467 break;
27469 cp_lexer_consume_token (parser->lexer); /* eat the = */
27470 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27472 cp_parser_error (parser, "expected identifier");
27473 syntax_error = true;
27474 break;
27476 if (keyword == RID_SETTER)
27478 if (property_setter_ident != NULL_TREE)
27480 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27481 cp_lexer_consume_token (parser->lexer);
27483 else
27484 property_setter_ident = cp_parser_objc_selector (parser);
27485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27486 cp_parser_error (parser, "setter name must terminate with %<:%>");
27487 else
27488 cp_lexer_consume_token (parser->lexer);
27490 else
27492 if (property_getter_ident != NULL_TREE)
27494 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27495 cp_lexer_consume_token (parser->lexer);
27497 else
27498 property_getter_ident = cp_parser_objc_selector (parser);
27500 break;
27501 default:
27502 cp_parser_error (parser, "unknown property attribute");
27503 syntax_error = true;
27504 break;
27507 if (syntax_error)
27508 break;
27510 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27511 cp_lexer_consume_token (parser->lexer);
27512 else
27513 break;
27516 /* FIXME: "@property (setter, assign);" will generate a spurious
27517 "error: expected ‘)’ before ‘,’ token". This is because
27518 cp_parser_require, unlike the C counterpart, will produce an
27519 error even if we are in error recovery. */
27520 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27522 cp_parser_skip_to_closing_parenthesis (parser,
27523 /*recovering=*/true,
27524 /*or_comma=*/false,
27525 /*consume_paren=*/true);
27529 /* ... and the property declaration(s). */
27530 properties = cp_parser_objc_struct_declaration (parser);
27532 if (properties == error_mark_node)
27534 cp_parser_skip_to_end_of_statement (parser);
27535 /* If the next token is now a `;', consume it. */
27536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27537 cp_lexer_consume_token (parser->lexer);
27538 return;
27541 if (properties == NULL_TREE)
27542 cp_parser_error (parser, "expected identifier");
27543 else
27545 /* Comma-separated properties are chained together in
27546 reverse order; add them one by one. */
27547 properties = nreverse (properties);
27549 for (; properties; properties = TREE_CHAIN (properties))
27550 objc_add_property_declaration (loc, copy_node (properties),
27551 property_readonly, property_readwrite,
27552 property_assign, property_retain,
27553 property_copy, property_nonatomic,
27554 property_getter_ident, property_setter_ident);
27557 cp_parser_consume_semicolon_at_end_of_statement (parser);
27560 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27562 objc-synthesize-declaration:
27563 @synthesize objc-synthesize-identifier-list ;
27565 objc-synthesize-identifier-list:
27566 objc-synthesize-identifier
27567 objc-synthesize-identifier-list, objc-synthesize-identifier
27569 objc-synthesize-identifier
27570 identifier
27571 identifier = identifier
27573 For example:
27574 @synthesize MyProperty;
27575 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27577 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27578 for C. Keep them in sync.
27580 static void
27581 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27583 tree list = NULL_TREE;
27584 location_t loc;
27585 loc = cp_lexer_peek_token (parser->lexer)->location;
27587 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27588 while (true)
27590 tree property, ivar;
27591 property = cp_parser_identifier (parser);
27592 if (property == error_mark_node)
27594 cp_parser_consume_semicolon_at_end_of_statement (parser);
27595 return;
27597 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27599 cp_lexer_consume_token (parser->lexer);
27600 ivar = cp_parser_identifier (parser);
27601 if (ivar == error_mark_node)
27603 cp_parser_consume_semicolon_at_end_of_statement (parser);
27604 return;
27607 else
27608 ivar = NULL_TREE;
27609 list = chainon (list, build_tree_list (ivar, property));
27610 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27611 cp_lexer_consume_token (parser->lexer);
27612 else
27613 break;
27615 cp_parser_consume_semicolon_at_end_of_statement (parser);
27616 objc_add_synthesize_declaration (loc, list);
27619 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27621 objc-dynamic-declaration:
27622 @dynamic identifier-list ;
27624 For example:
27625 @dynamic MyProperty;
27626 @dynamic MyProperty, AnotherProperty;
27628 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27629 for C. Keep them in sync.
27631 static void
27632 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27634 tree list = NULL_TREE;
27635 location_t loc;
27636 loc = cp_lexer_peek_token (parser->lexer)->location;
27638 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27639 while (true)
27641 tree property;
27642 property = cp_parser_identifier (parser);
27643 if (property == error_mark_node)
27645 cp_parser_consume_semicolon_at_end_of_statement (parser);
27646 return;
27648 list = chainon (list, build_tree_list (NULL, property));
27649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27650 cp_lexer_consume_token (parser->lexer);
27651 else
27652 break;
27654 cp_parser_consume_semicolon_at_end_of_statement (parser);
27655 objc_add_dynamic_declaration (loc, list);
27659 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27661 /* Returns name of the next clause.
27662 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27663 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27664 returned and the token is consumed. */
27666 static pragma_omp_clause
27667 cp_parser_omp_clause_name (cp_parser *parser)
27669 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27672 result = PRAGMA_OMP_CLAUSE_IF;
27673 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27674 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27675 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27676 result = PRAGMA_OACC_CLAUSE_DELETE;
27677 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27678 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27679 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27680 result = PRAGMA_OMP_CLAUSE_FOR;
27681 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27683 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27684 const char *p = IDENTIFIER_POINTER (id);
27686 switch (p[0])
27688 case 'a':
27689 if (!strcmp ("aligned", p))
27690 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27691 else if (!strcmp ("async", p))
27692 result = PRAGMA_OACC_CLAUSE_ASYNC;
27693 break;
27694 case 'c':
27695 if (!strcmp ("collapse", p))
27696 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27697 else if (!strcmp ("copy", p))
27698 result = PRAGMA_OACC_CLAUSE_COPY;
27699 else if (!strcmp ("copyin", p))
27700 result = PRAGMA_OMP_CLAUSE_COPYIN;
27701 else if (!strcmp ("copyout", p))
27702 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27703 else if (!strcmp ("copyprivate", p))
27704 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27705 else if (!strcmp ("create", p))
27706 result = PRAGMA_OACC_CLAUSE_CREATE;
27707 break;
27708 case 'd':
27709 if (!strcmp ("depend", p))
27710 result = PRAGMA_OMP_CLAUSE_DEPEND;
27711 else if (!strcmp ("device", p))
27712 result = PRAGMA_OMP_CLAUSE_DEVICE;
27713 else if (!strcmp ("deviceptr", p))
27714 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27715 else if (!strcmp ("dist_schedule", p))
27716 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27717 break;
27718 case 'f':
27719 if (!strcmp ("final", p))
27720 result = PRAGMA_OMP_CLAUSE_FINAL;
27721 else if (!strcmp ("firstprivate", p))
27722 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27723 else if (!strcmp ("from", p))
27724 result = PRAGMA_OMP_CLAUSE_FROM;
27725 break;
27726 case 'h':
27727 if (!strcmp ("host", p))
27728 result = PRAGMA_OACC_CLAUSE_HOST;
27729 break;
27730 case 'i':
27731 if (!strcmp ("inbranch", p))
27732 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27733 break;
27734 case 'l':
27735 if (!strcmp ("lastprivate", p))
27736 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27737 else if (!strcmp ("linear", p))
27738 result = PRAGMA_OMP_CLAUSE_LINEAR;
27739 break;
27740 case 'm':
27741 if (!strcmp ("map", p))
27742 result = PRAGMA_OMP_CLAUSE_MAP;
27743 else if (!strcmp ("mergeable", p))
27744 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27745 else if (flag_cilkplus && !strcmp ("mask", p))
27746 result = PRAGMA_CILK_CLAUSE_MASK;
27747 break;
27748 case 'n':
27749 if (!strcmp ("notinbranch", p))
27750 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27751 else if (!strcmp ("nowait", p))
27752 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27753 else if (flag_cilkplus && !strcmp ("nomask", p))
27754 result = PRAGMA_CILK_CLAUSE_NOMASK;
27755 else if (!strcmp ("num_gangs", p))
27756 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27757 else if (!strcmp ("num_teams", p))
27758 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27759 else if (!strcmp ("num_threads", p))
27760 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27761 else if (!strcmp ("num_workers", p))
27762 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27763 break;
27764 case 'o':
27765 if (!strcmp ("ordered", p))
27766 result = PRAGMA_OMP_CLAUSE_ORDERED;
27767 break;
27768 case 'p':
27769 if (!strcmp ("parallel", p))
27770 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27771 else if (!strcmp ("present", p))
27772 result = PRAGMA_OACC_CLAUSE_PRESENT;
27773 else if (!strcmp ("present_or_copy", p)
27774 || !strcmp ("pcopy", p))
27775 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27776 else if (!strcmp ("present_or_copyin", p)
27777 || !strcmp ("pcopyin", p))
27778 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27779 else if (!strcmp ("present_or_copyout", p)
27780 || !strcmp ("pcopyout", p))
27781 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27782 else if (!strcmp ("present_or_create", p)
27783 || !strcmp ("pcreate", p))
27784 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27785 else if (!strcmp ("proc_bind", p))
27786 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27787 break;
27788 case 'r':
27789 if (!strcmp ("reduction", p))
27790 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27791 break;
27792 case 's':
27793 if (!strcmp ("safelen", p))
27794 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27795 else if (!strcmp ("schedule", p))
27796 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27797 else if (!strcmp ("sections", p))
27798 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27799 else if (!strcmp ("self", p))
27800 result = PRAGMA_OACC_CLAUSE_SELF;
27801 else if (!strcmp ("shared", p))
27802 result = PRAGMA_OMP_CLAUSE_SHARED;
27803 else if (!strcmp ("simdlen", p))
27804 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27805 break;
27806 case 't':
27807 if (!strcmp ("taskgroup", p))
27808 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27809 else if (!strcmp ("thread_limit", p))
27810 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27811 else if (!strcmp ("to", p))
27812 result = PRAGMA_OMP_CLAUSE_TO;
27813 break;
27814 case 'u':
27815 if (!strcmp ("uniform", p))
27816 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27817 else if (!strcmp ("untied", p))
27818 result = PRAGMA_OMP_CLAUSE_UNTIED;
27819 break;
27820 case 'v':
27821 if (!strcmp ("vector_length", p))
27822 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27823 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27824 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27825 break;
27826 case 'w':
27827 if (!strcmp ("wait", p))
27828 result = PRAGMA_OACC_CLAUSE_WAIT;
27829 break;
27833 if (result != PRAGMA_OMP_CLAUSE_NONE)
27834 cp_lexer_consume_token (parser->lexer);
27836 return result;
27839 /* Validate that a clause of the given type does not already exist. */
27841 static void
27842 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27843 const char *name, location_t location)
27845 tree c;
27847 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27848 if (OMP_CLAUSE_CODE (c) == code)
27850 error_at (location, "too many %qs clauses", name);
27851 break;
27855 /* OpenMP 2.5:
27856 variable-list:
27857 identifier
27858 variable-list , identifier
27860 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27861 colon). An opening parenthesis will have been consumed by the caller.
27863 If KIND is nonzero, create the appropriate node and install the decl
27864 in OMP_CLAUSE_DECL and add the node to the head of the list.
27866 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27867 return the list created.
27869 COLON can be NULL if only closing parenthesis should end the list,
27870 or pointer to bool which will receive false if the list is terminated
27871 by closing parenthesis or true if the list is terminated by colon. */
27873 static tree
27874 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27875 tree list, bool *colon)
27877 cp_token *token;
27878 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27879 if (colon)
27881 parser->colon_corrects_to_scope_p = false;
27882 *colon = false;
27884 while (1)
27886 tree name, decl;
27888 token = cp_lexer_peek_token (parser->lexer);
27889 name = cp_parser_id_expression (parser, /*template_p=*/false,
27890 /*check_dependency_p=*/true,
27891 /*template_p=*/NULL,
27892 /*declarator_p=*/false,
27893 /*optional_p=*/false);
27894 if (name == error_mark_node)
27895 goto skip_comma;
27897 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27898 if (decl == error_mark_node)
27899 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27900 token->location);
27901 else if (kind != 0)
27903 switch (kind)
27905 case OMP_CLAUSE__CACHE_:
27906 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27908 error_at (token->location, "expected %<[%>");
27909 decl = error_mark_node;
27910 break;
27912 /* FALL THROUGH. */
27913 case OMP_CLAUSE_MAP:
27914 case OMP_CLAUSE_FROM:
27915 case OMP_CLAUSE_TO:
27916 case OMP_CLAUSE_DEPEND:
27917 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27919 tree low_bound = NULL_TREE, length = NULL_TREE;
27921 parser->colon_corrects_to_scope_p = false;
27922 cp_lexer_consume_token (parser->lexer);
27923 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27924 low_bound = cp_parser_expression (parser);
27925 if (!colon)
27926 parser->colon_corrects_to_scope_p
27927 = saved_colon_corrects_to_scope_p;
27928 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27929 length = integer_one_node;
27930 else
27932 /* Look for `:'. */
27933 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27934 goto skip_comma;
27935 if (!cp_lexer_next_token_is (parser->lexer,
27936 CPP_CLOSE_SQUARE))
27937 length = cp_parser_expression (parser);
27939 /* Look for the closing `]'. */
27940 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27941 RT_CLOSE_SQUARE))
27942 goto skip_comma;
27944 if (kind == OMP_CLAUSE__CACHE_)
27946 if (TREE_CODE (low_bound) != INTEGER_CST
27947 && !TREE_READONLY (low_bound))
27949 error_at (token->location,
27950 "%qD is not a constant", low_bound);
27951 decl = error_mark_node;
27954 if (TREE_CODE (length) != INTEGER_CST
27955 && !TREE_READONLY (length))
27957 error_at (token->location,
27958 "%qD is not a constant", length);
27959 decl = error_mark_node;
27963 decl = tree_cons (low_bound, length, decl);
27965 break;
27966 default:
27967 break;
27970 tree u = build_omp_clause (token->location, kind);
27971 OMP_CLAUSE_DECL (u) = decl;
27972 OMP_CLAUSE_CHAIN (u) = list;
27973 list = u;
27975 else
27976 list = tree_cons (decl, NULL_TREE, list);
27978 get_comma:
27979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27980 break;
27981 cp_lexer_consume_token (parser->lexer);
27984 if (colon)
27985 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27987 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27989 *colon = true;
27990 cp_parser_require (parser, CPP_COLON, RT_COLON);
27991 return list;
27994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27996 int ending;
27998 /* Try to resync to an unnested comma. Copied from
27999 cp_parser_parenthesized_expression_list. */
28000 skip_comma:
28001 if (colon)
28002 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28003 ending = cp_parser_skip_to_closing_parenthesis (parser,
28004 /*recovering=*/true,
28005 /*or_comma=*/true,
28006 /*consume_paren=*/true);
28007 if (ending < 0)
28008 goto get_comma;
28011 return list;
28014 /* Similarly, but expect leading and trailing parenthesis. This is a very
28015 common case for omp clauses. */
28017 static tree
28018 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28020 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28021 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28022 return list;
28025 /* OpenACC 2.0:
28026 copy ( variable-list )
28027 copyin ( variable-list )
28028 copyout ( variable-list )
28029 create ( variable-list )
28030 delete ( variable-list )
28031 present ( variable-list )
28032 present_or_copy ( variable-list )
28033 pcopy ( variable-list )
28034 present_or_copyin ( variable-list )
28035 pcopyin ( variable-list )
28036 present_or_copyout ( variable-list )
28037 pcopyout ( variable-list )
28038 present_or_create ( variable-list )
28039 pcreate ( variable-list ) */
28041 static tree
28042 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28043 tree list)
28045 enum gomp_map_kind kind;
28046 switch (c_kind)
28048 case PRAGMA_OACC_CLAUSE_COPY:
28049 kind = GOMP_MAP_FORCE_TOFROM;
28050 break;
28051 case PRAGMA_OACC_CLAUSE_COPYIN:
28052 kind = GOMP_MAP_FORCE_TO;
28053 break;
28054 case PRAGMA_OACC_CLAUSE_COPYOUT:
28055 kind = GOMP_MAP_FORCE_FROM;
28056 break;
28057 case PRAGMA_OACC_CLAUSE_CREATE:
28058 kind = GOMP_MAP_FORCE_ALLOC;
28059 break;
28060 case PRAGMA_OACC_CLAUSE_DELETE:
28061 kind = GOMP_MAP_FORCE_DEALLOC;
28062 break;
28063 case PRAGMA_OACC_CLAUSE_DEVICE:
28064 kind = GOMP_MAP_FORCE_TO;
28065 break;
28066 case PRAGMA_OACC_CLAUSE_HOST:
28067 case PRAGMA_OACC_CLAUSE_SELF:
28068 kind = GOMP_MAP_FORCE_FROM;
28069 break;
28070 case PRAGMA_OACC_CLAUSE_PRESENT:
28071 kind = GOMP_MAP_FORCE_PRESENT;
28072 break;
28073 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28074 kind = GOMP_MAP_TOFROM;
28075 break;
28076 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28077 kind = GOMP_MAP_TO;
28078 break;
28079 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28080 kind = GOMP_MAP_FROM;
28081 break;
28082 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28083 kind = GOMP_MAP_ALLOC;
28084 break;
28085 default:
28086 gcc_unreachable ();
28088 tree nl, c;
28089 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28091 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28092 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28094 return nl;
28097 /* OpenACC 2.0:
28098 deviceptr ( variable-list ) */
28100 static tree
28101 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28103 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28104 tree vars, t;
28106 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28107 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28108 variable-list must only allow for pointer variables. */
28109 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28110 for (t = vars; t; t = TREE_CHAIN (t))
28112 tree v = TREE_PURPOSE (t);
28114 /* FIXME diagnostics: Ideally we should keep individual
28115 locations for all the variables in the var list to make the
28116 following errors more precise. Perhaps
28117 c_parser_omp_var_list_parens should construct a list of
28118 locations to go along with the var list. */
28120 if (!VAR_P (v))
28121 error_at (loc, "%qD is not a variable", v);
28122 else if (TREE_TYPE (v) == error_mark_node)
28124 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28125 error_at (loc, "%qD is not a pointer variable", v);
28127 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28128 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28129 OMP_CLAUSE_DECL (u) = v;
28130 OMP_CLAUSE_CHAIN (u) = list;
28131 list = u;
28134 return list;
28137 /* OpenACC:
28138 vector_length ( expression ) */
28140 static tree
28141 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28143 tree t, c;
28144 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28145 bool error = false;
28147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28148 return list;
28150 t = cp_parser_condition (parser);
28151 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28153 error_at (location, "expected positive integer expression");
28154 error = true;
28157 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28160 /*or_comma=*/false,
28161 /*consume_paren=*/true);
28162 return list;
28165 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28166 location);
28168 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28169 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28170 OMP_CLAUSE_CHAIN (c) = list;
28171 list = c;
28173 return list;
28176 /* OpenACC 2.0
28177 Parse wait clause or directive parameters. */
28179 static tree
28180 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28182 vec<tree, va_gc> *args;
28183 tree t, args_tree;
28185 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28186 /*cast_p=*/false,
28187 /*allow_expansion_p=*/true,
28188 /*non_constant_p=*/NULL);
28190 if (args == NULL || args->length () == 0)
28192 cp_parser_error (parser, "expected integer expression before ')'");
28193 if (args != NULL)
28194 release_tree_vector (args);
28195 return list;
28198 args_tree = build_tree_list_vec (args);
28200 release_tree_vector (args);
28202 for (t = args_tree; t; t = TREE_CHAIN (t))
28204 tree targ = TREE_VALUE (t);
28206 if (targ != error_mark_node)
28208 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28209 error ("%<wait%> expression must be integral");
28210 else
28212 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28214 mark_rvalue_use (targ);
28215 OMP_CLAUSE_DECL (c) = targ;
28216 OMP_CLAUSE_CHAIN (c) = list;
28217 list = c;
28222 return list;
28225 /* OpenACC:
28226 wait ( int-expr-list ) */
28228 static tree
28229 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28231 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28233 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28234 return list;
28236 list = cp_parser_oacc_wait_list (parser, location, list);
28238 return list;
28241 /* OpenMP 3.0:
28242 collapse ( constant-expression ) */
28244 static tree
28245 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28247 tree c, num;
28248 location_t loc;
28249 HOST_WIDE_INT n;
28251 loc = cp_lexer_peek_token (parser->lexer)->location;
28252 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28253 return list;
28255 num = cp_parser_constant_expression (parser);
28257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28259 /*or_comma=*/false,
28260 /*consume_paren=*/true);
28262 if (num == error_mark_node)
28263 return list;
28264 num = fold_non_dependent_expr (num);
28265 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28266 || !tree_fits_shwi_p (num)
28267 || (n = tree_to_shwi (num)) <= 0
28268 || (int) n != n)
28270 error_at (loc, "collapse argument needs positive constant integer expression");
28271 return list;
28274 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28275 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28276 OMP_CLAUSE_CHAIN (c) = list;
28277 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28279 return c;
28282 /* OpenMP 2.5:
28283 default ( shared | none ) */
28285 static tree
28286 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28288 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28289 tree c;
28291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28292 return list;
28293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28296 const char *p = IDENTIFIER_POINTER (id);
28298 switch (p[0])
28300 case 'n':
28301 if (strcmp ("none", p) != 0)
28302 goto invalid_kind;
28303 kind = OMP_CLAUSE_DEFAULT_NONE;
28304 break;
28306 case 's':
28307 if (strcmp ("shared", p) != 0)
28308 goto invalid_kind;
28309 kind = OMP_CLAUSE_DEFAULT_SHARED;
28310 break;
28312 default:
28313 goto invalid_kind;
28316 cp_lexer_consume_token (parser->lexer);
28318 else
28320 invalid_kind:
28321 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28324 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28326 /*or_comma=*/false,
28327 /*consume_paren=*/true);
28329 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28330 return list;
28332 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28333 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28334 OMP_CLAUSE_CHAIN (c) = list;
28335 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28337 return c;
28340 /* OpenMP 3.1:
28341 final ( expression ) */
28343 static tree
28344 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28346 tree t, c;
28348 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28349 return list;
28351 t = cp_parser_condition (parser);
28353 if (t == error_mark_node
28354 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28355 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28356 /*or_comma=*/false,
28357 /*consume_paren=*/true);
28359 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28361 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28362 OMP_CLAUSE_FINAL_EXPR (c) = t;
28363 OMP_CLAUSE_CHAIN (c) = list;
28365 return c;
28368 /* OpenMP 2.5:
28369 if ( expression ) */
28371 static tree
28372 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28374 tree t, c;
28376 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28377 return list;
28379 t = cp_parser_condition (parser);
28381 if (t == error_mark_node
28382 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28383 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28384 /*or_comma=*/false,
28385 /*consume_paren=*/true);
28387 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28389 c = build_omp_clause (location, OMP_CLAUSE_IF);
28390 OMP_CLAUSE_IF_EXPR (c) = t;
28391 OMP_CLAUSE_CHAIN (c) = list;
28393 return c;
28396 /* OpenMP 3.1:
28397 mergeable */
28399 static tree
28400 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28401 tree list, location_t location)
28403 tree c;
28405 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28406 location);
28408 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28409 OMP_CLAUSE_CHAIN (c) = list;
28410 return c;
28413 /* OpenMP 2.5:
28414 nowait */
28416 static tree
28417 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28418 tree list, location_t location)
28420 tree c;
28422 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28424 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28425 OMP_CLAUSE_CHAIN (c) = list;
28426 return c;
28429 /* OpenACC:
28430 num_gangs ( expression ) */
28432 static tree
28433 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28435 tree t, c;
28436 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28439 return list;
28441 t = cp_parser_condition (parser);
28443 if (t == error_mark_node
28444 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28446 /*or_comma=*/false,
28447 /*consume_paren=*/true);
28449 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28451 error_at (location, "expected positive integer expression");
28452 return list;
28455 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28457 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28458 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28459 OMP_CLAUSE_CHAIN (c) = list;
28460 list = c;
28462 return list;
28465 /* OpenMP 2.5:
28466 num_threads ( expression ) */
28468 static tree
28469 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28470 location_t location)
28472 tree t, c;
28474 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28475 return list;
28477 t = cp_parser_expression (parser);
28479 if (t == error_mark_node
28480 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28481 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28482 /*or_comma=*/false,
28483 /*consume_paren=*/true);
28485 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28486 "num_threads", location);
28488 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28489 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28490 OMP_CLAUSE_CHAIN (c) = list;
28492 return c;
28495 /* OpenACC:
28496 num_workers ( expression ) */
28498 static tree
28499 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28501 tree t, c;
28502 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28504 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28505 return list;
28507 t = cp_parser_condition (parser);
28509 if (t == error_mark_node
28510 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28512 /*or_comma=*/false,
28513 /*consume_paren=*/true);
28515 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28517 error_at (location, "expected positive integer expression");
28518 return list;
28521 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28522 location);
28524 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28525 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28526 OMP_CLAUSE_CHAIN (c) = list;
28527 list = c;
28529 return list;
28532 /* OpenMP 2.5:
28533 ordered */
28535 static tree
28536 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28537 tree list, location_t location)
28539 tree c;
28541 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28542 "ordered", location);
28544 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28545 OMP_CLAUSE_CHAIN (c) = list;
28546 return c;
28549 /* OpenMP 2.5:
28550 reduction ( reduction-operator : variable-list )
28552 reduction-operator:
28553 One of: + * - & ^ | && ||
28555 OpenMP 3.1:
28557 reduction-operator:
28558 One of: + * - & ^ | && || min max
28560 OpenMP 4.0:
28562 reduction-operator:
28563 One of: + * - & ^ | && ||
28564 id-expression */
28566 static tree
28567 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28569 enum tree_code code = ERROR_MARK;
28570 tree nlist, c, id = NULL_TREE;
28572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28573 return list;
28575 switch (cp_lexer_peek_token (parser->lexer)->type)
28577 case CPP_PLUS: code = PLUS_EXPR; break;
28578 case CPP_MULT: code = MULT_EXPR; break;
28579 case CPP_MINUS: code = MINUS_EXPR; break;
28580 case CPP_AND: code = BIT_AND_EXPR; break;
28581 case CPP_XOR: code = BIT_XOR_EXPR; break;
28582 case CPP_OR: code = BIT_IOR_EXPR; break;
28583 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28584 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28585 default: break;
28588 if (code != ERROR_MARK)
28589 cp_lexer_consume_token (parser->lexer);
28590 else
28592 bool saved_colon_corrects_to_scope_p;
28593 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28594 parser->colon_corrects_to_scope_p = false;
28595 id = cp_parser_id_expression (parser, /*template_p=*/false,
28596 /*check_dependency_p=*/true,
28597 /*template_p=*/NULL,
28598 /*declarator_p=*/false,
28599 /*optional_p=*/false);
28600 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28601 if (identifier_p (id))
28603 const char *p = IDENTIFIER_POINTER (id);
28605 if (strcmp (p, "min") == 0)
28606 code = MIN_EXPR;
28607 else if (strcmp (p, "max") == 0)
28608 code = MAX_EXPR;
28609 else if (id == ansi_opname (PLUS_EXPR))
28610 code = PLUS_EXPR;
28611 else if (id == ansi_opname (MULT_EXPR))
28612 code = MULT_EXPR;
28613 else if (id == ansi_opname (MINUS_EXPR))
28614 code = MINUS_EXPR;
28615 else if (id == ansi_opname (BIT_AND_EXPR))
28616 code = BIT_AND_EXPR;
28617 else if (id == ansi_opname (BIT_IOR_EXPR))
28618 code = BIT_IOR_EXPR;
28619 else if (id == ansi_opname (BIT_XOR_EXPR))
28620 code = BIT_XOR_EXPR;
28621 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28622 code = TRUTH_ANDIF_EXPR;
28623 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28624 code = TRUTH_ORIF_EXPR;
28625 id = omp_reduction_id (code, id, NULL_TREE);
28626 tree scope = parser->scope;
28627 if (scope)
28628 id = build_qualified_name (NULL_TREE, scope, id, false);
28629 parser->scope = NULL_TREE;
28630 parser->qualifying_scope = NULL_TREE;
28631 parser->object_scope = NULL_TREE;
28633 else
28635 error ("invalid reduction-identifier");
28636 resync_fail:
28637 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28638 /*or_comma=*/false,
28639 /*consume_paren=*/true);
28640 return list;
28644 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28645 goto resync_fail;
28647 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28648 NULL);
28649 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28651 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28652 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28655 return nlist;
28658 /* OpenMP 2.5:
28659 schedule ( schedule-kind )
28660 schedule ( schedule-kind , expression )
28662 schedule-kind:
28663 static | dynamic | guided | runtime | auto */
28665 static tree
28666 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28668 tree c, t;
28670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28671 return list;
28673 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28678 const char *p = IDENTIFIER_POINTER (id);
28680 switch (p[0])
28682 case 'd':
28683 if (strcmp ("dynamic", p) != 0)
28684 goto invalid_kind;
28685 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28686 break;
28688 case 'g':
28689 if (strcmp ("guided", p) != 0)
28690 goto invalid_kind;
28691 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28692 break;
28694 case 'r':
28695 if (strcmp ("runtime", p) != 0)
28696 goto invalid_kind;
28697 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28698 break;
28700 default:
28701 goto invalid_kind;
28704 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28705 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28706 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28707 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28708 else
28709 goto invalid_kind;
28710 cp_lexer_consume_token (parser->lexer);
28712 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28714 cp_token *token;
28715 cp_lexer_consume_token (parser->lexer);
28717 token = cp_lexer_peek_token (parser->lexer);
28718 t = cp_parser_assignment_expression (parser);
28720 if (t == error_mark_node)
28721 goto resync_fail;
28722 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28723 error_at (token->location, "schedule %<runtime%> does not take "
28724 "a %<chunk_size%> parameter");
28725 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28726 error_at (token->location, "schedule %<auto%> does not take "
28727 "a %<chunk_size%> parameter");
28728 else
28729 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28731 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28732 goto resync_fail;
28734 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28735 goto resync_fail;
28737 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28738 OMP_CLAUSE_CHAIN (c) = list;
28739 return c;
28741 invalid_kind:
28742 cp_parser_error (parser, "invalid schedule kind");
28743 resync_fail:
28744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28745 /*or_comma=*/false,
28746 /*consume_paren=*/true);
28747 return list;
28750 /* OpenMP 3.0:
28751 untied */
28753 static tree
28754 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28755 tree list, location_t location)
28757 tree c;
28759 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28761 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28762 OMP_CLAUSE_CHAIN (c) = list;
28763 return c;
28766 /* OpenMP 4.0:
28767 inbranch
28768 notinbranch */
28770 static tree
28771 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28772 tree list, location_t location)
28774 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28775 tree c = build_omp_clause (location, code);
28776 OMP_CLAUSE_CHAIN (c) = list;
28777 return c;
28780 /* OpenMP 4.0:
28781 parallel
28783 sections
28784 taskgroup */
28786 static tree
28787 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28788 enum omp_clause_code code,
28789 tree list, location_t location)
28791 tree c = build_omp_clause (location, code);
28792 OMP_CLAUSE_CHAIN (c) = list;
28793 return c;
28796 /* OpenMP 4.0:
28797 num_teams ( expression ) */
28799 static tree
28800 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28801 location_t location)
28803 tree t, c;
28805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28806 return list;
28808 t = cp_parser_expression (parser);
28810 if (t == error_mark_node
28811 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28813 /*or_comma=*/false,
28814 /*consume_paren=*/true);
28816 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28817 "num_teams", location);
28819 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28820 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28821 OMP_CLAUSE_CHAIN (c) = list;
28823 return c;
28826 /* OpenMP 4.0:
28827 thread_limit ( expression ) */
28829 static tree
28830 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28831 location_t location)
28833 tree t, c;
28835 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28836 return list;
28838 t = cp_parser_expression (parser);
28840 if (t == error_mark_node
28841 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28843 /*or_comma=*/false,
28844 /*consume_paren=*/true);
28846 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28847 "thread_limit", location);
28849 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28850 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28851 OMP_CLAUSE_CHAIN (c) = list;
28853 return c;
28856 /* OpenMP 4.0:
28857 aligned ( variable-list )
28858 aligned ( variable-list : constant-expression ) */
28860 static tree
28861 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28863 tree nlist, c, alignment = NULL_TREE;
28864 bool colon;
28866 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28867 return list;
28869 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28870 &colon);
28872 if (colon)
28874 alignment = cp_parser_constant_expression (parser);
28876 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28878 /*or_comma=*/false,
28879 /*consume_paren=*/true);
28881 if (alignment == error_mark_node)
28882 alignment = NULL_TREE;
28885 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28886 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28888 return nlist;
28891 /* OpenMP 4.0:
28892 linear ( variable-list )
28893 linear ( variable-list : expression ) */
28895 static tree
28896 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28897 bool is_cilk_simd_fn)
28899 tree nlist, c, step = integer_one_node;
28900 bool colon;
28902 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28903 return list;
28905 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28906 &colon);
28908 if (colon)
28910 step = cp_parser_expression (parser);
28912 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28914 sorry ("using parameters for %<linear%> step is not supported yet");
28915 step = integer_one_node;
28917 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28919 /*or_comma=*/false,
28920 /*consume_paren=*/true);
28922 if (step == error_mark_node)
28923 return list;
28926 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28927 OMP_CLAUSE_LINEAR_STEP (c) = step;
28929 return nlist;
28932 /* OpenMP 4.0:
28933 safelen ( constant-expression ) */
28935 static tree
28936 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28937 location_t location)
28939 tree t, c;
28941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28942 return list;
28944 t = cp_parser_constant_expression (parser);
28946 if (t == error_mark_node
28947 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28948 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28949 /*or_comma=*/false,
28950 /*consume_paren=*/true);
28952 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28954 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28955 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28956 OMP_CLAUSE_CHAIN (c) = list;
28958 return c;
28961 /* OpenMP 4.0:
28962 simdlen ( constant-expression ) */
28964 static tree
28965 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28966 location_t location)
28968 tree t, c;
28970 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28971 return list;
28973 t = cp_parser_constant_expression (parser);
28975 if (t == error_mark_node
28976 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28977 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28978 /*or_comma=*/false,
28979 /*consume_paren=*/true);
28981 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28983 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28984 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28985 OMP_CLAUSE_CHAIN (c) = list;
28987 return c;
28990 /* OpenMP 4.0:
28991 depend ( depend-kind : variable-list )
28993 depend-kind:
28994 in | out | inout */
28996 static tree
28997 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28999 tree nlist, c;
29000 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29002 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29003 return list;
29005 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29007 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29008 const char *p = IDENTIFIER_POINTER (id);
29010 if (strcmp ("in", p) == 0)
29011 kind = OMP_CLAUSE_DEPEND_IN;
29012 else if (strcmp ("inout", p) == 0)
29013 kind = OMP_CLAUSE_DEPEND_INOUT;
29014 else if (strcmp ("out", p) == 0)
29015 kind = OMP_CLAUSE_DEPEND_OUT;
29016 else
29017 goto invalid_kind;
29019 else
29020 goto invalid_kind;
29022 cp_lexer_consume_token (parser->lexer);
29023 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29024 goto resync_fail;
29026 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29027 NULL);
29029 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29030 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29032 return nlist;
29034 invalid_kind:
29035 cp_parser_error (parser, "invalid depend kind");
29036 resync_fail:
29037 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29038 /*or_comma=*/false,
29039 /*consume_paren=*/true);
29040 return list;
29043 /* OpenMP 4.0:
29044 map ( map-kind : variable-list )
29045 map ( variable-list )
29047 map-kind:
29048 alloc | to | from | tofrom */
29050 static tree
29051 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29053 tree nlist, c;
29054 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29056 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29057 return list;
29059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29060 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29062 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29063 const char *p = IDENTIFIER_POINTER (id);
29065 if (strcmp ("alloc", p) == 0)
29066 kind = GOMP_MAP_ALLOC;
29067 else if (strcmp ("to", p) == 0)
29068 kind = GOMP_MAP_TO;
29069 else if (strcmp ("from", p) == 0)
29070 kind = GOMP_MAP_FROM;
29071 else if (strcmp ("tofrom", p) == 0)
29072 kind = GOMP_MAP_TOFROM;
29073 else
29075 cp_parser_error (parser, "invalid map kind");
29076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29077 /*or_comma=*/false,
29078 /*consume_paren=*/true);
29079 return list;
29081 cp_lexer_consume_token (parser->lexer);
29082 cp_lexer_consume_token (parser->lexer);
29085 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29086 NULL);
29088 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29089 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29091 return nlist;
29094 /* OpenMP 4.0:
29095 device ( expression ) */
29097 static tree
29098 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29099 location_t location)
29101 tree t, c;
29103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29104 return list;
29106 t = cp_parser_expression (parser);
29108 if (t == error_mark_node
29109 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29111 /*or_comma=*/false,
29112 /*consume_paren=*/true);
29114 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29115 "device", location);
29117 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29118 OMP_CLAUSE_DEVICE_ID (c) = t;
29119 OMP_CLAUSE_CHAIN (c) = list;
29121 return c;
29124 /* OpenMP 4.0:
29125 dist_schedule ( static )
29126 dist_schedule ( static , expression ) */
29128 static tree
29129 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29130 location_t location)
29132 tree c, t;
29134 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29135 return list;
29137 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29139 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29140 goto invalid_kind;
29141 cp_lexer_consume_token (parser->lexer);
29143 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29145 cp_lexer_consume_token (parser->lexer);
29147 t = cp_parser_assignment_expression (parser);
29149 if (t == error_mark_node)
29150 goto resync_fail;
29151 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29154 goto resync_fail;
29156 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29157 goto resync_fail;
29159 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29160 location);
29161 OMP_CLAUSE_CHAIN (c) = list;
29162 return c;
29164 invalid_kind:
29165 cp_parser_error (parser, "invalid dist_schedule kind");
29166 resync_fail:
29167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29168 /*or_comma=*/false,
29169 /*consume_paren=*/true);
29170 return list;
29173 /* OpenMP 4.0:
29174 proc_bind ( proc-bind-kind )
29176 proc-bind-kind:
29177 master | close | spread */
29179 static tree
29180 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29181 location_t location)
29183 tree c;
29184 enum omp_clause_proc_bind_kind kind;
29186 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29187 return list;
29189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29192 const char *p = IDENTIFIER_POINTER (id);
29194 if (strcmp ("master", p) == 0)
29195 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29196 else if (strcmp ("close", p) == 0)
29197 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29198 else if (strcmp ("spread", p) == 0)
29199 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29200 else
29201 goto invalid_kind;
29203 else
29204 goto invalid_kind;
29206 cp_lexer_consume_token (parser->lexer);
29207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29208 goto resync_fail;
29210 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29211 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29212 location);
29213 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29214 OMP_CLAUSE_CHAIN (c) = list;
29215 return c;
29217 invalid_kind:
29218 cp_parser_error (parser, "invalid depend kind");
29219 resync_fail:
29220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29221 /*or_comma=*/false,
29222 /*consume_paren=*/true);
29223 return list;
29226 /* OpenACC:
29227 async [( int-expr )] */
29229 static tree
29230 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29232 tree c, t;
29233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29235 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29237 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29239 cp_lexer_consume_token (parser->lexer);
29241 t = cp_parser_expression (parser);
29242 if (t == error_mark_node
29243 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29244 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29245 /*or_comma=*/false,
29246 /*consume_paren=*/true);
29249 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29251 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29252 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29253 OMP_CLAUSE_CHAIN (c) = list;
29254 list = c;
29256 return list;
29259 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29260 is a bitmask in MASK. Return the list of clauses found. */
29262 static tree
29263 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29264 const char *where, cp_token *pragma_tok,
29265 bool finish_p = true)
29267 tree clauses = NULL;
29268 bool first = true;
29270 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29272 location_t here;
29273 pragma_omp_clause c_kind;
29274 const char *c_name;
29275 tree prev = clauses;
29277 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29278 cp_lexer_consume_token (parser->lexer);
29280 here = cp_lexer_peek_token (parser->lexer)->location;
29281 c_kind = cp_parser_omp_clause_name (parser);
29283 switch (c_kind)
29285 case PRAGMA_OACC_CLAUSE_ASYNC:
29286 clauses = cp_parser_oacc_clause_async (parser, clauses);
29287 c_name = "async";
29288 break;
29289 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29290 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29291 c_name = "collapse";
29292 break;
29293 case PRAGMA_OACC_CLAUSE_COPY:
29294 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29295 c_name = "copy";
29296 break;
29297 case PRAGMA_OACC_CLAUSE_COPYIN:
29298 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29299 c_name = "copyin";
29300 break;
29301 case PRAGMA_OACC_CLAUSE_COPYOUT:
29302 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29303 c_name = "copyout";
29304 break;
29305 case PRAGMA_OACC_CLAUSE_CREATE:
29306 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29307 c_name = "create";
29308 break;
29309 case PRAGMA_OACC_CLAUSE_DELETE:
29310 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29311 c_name = "delete";
29312 break;
29313 case PRAGMA_OACC_CLAUSE_DEVICE:
29314 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29315 c_name = "device";
29316 break;
29317 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29318 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29319 c_name = "deviceptr";
29320 break;
29321 case PRAGMA_OACC_CLAUSE_HOST:
29322 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29323 c_name = "host";
29324 break;
29325 case PRAGMA_OACC_CLAUSE_IF:
29326 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29327 c_name = "if";
29328 break;
29329 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29330 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29331 c_name = "num_gangs";
29332 break;
29333 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29334 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29335 c_name = "num_workers";
29336 break;
29337 case PRAGMA_OACC_CLAUSE_PRESENT:
29338 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29339 c_name = "present";
29340 break;
29341 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29342 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29343 c_name = "present_or_copy";
29344 break;
29345 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29346 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29347 c_name = "present_or_copyin";
29348 break;
29349 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29350 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29351 c_name = "present_or_copyout";
29352 break;
29353 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29354 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29355 c_name = "present_or_create";
29356 break;
29357 case PRAGMA_OACC_CLAUSE_REDUCTION:
29358 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29359 c_name = "reduction";
29360 break;
29361 case PRAGMA_OACC_CLAUSE_SELF:
29362 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29363 c_name = "self";
29364 break;
29365 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29366 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29367 c_name = "vector_length";
29368 break;
29369 case PRAGMA_OACC_CLAUSE_WAIT:
29370 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29371 c_name = "wait";
29372 break;
29373 default:
29374 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29375 goto saw_error;
29378 first = false;
29380 if (((mask >> c_kind) & 1) == 0)
29382 /* Remove the invalid clause(s) from the list to avoid
29383 confusing the rest of the compiler. */
29384 clauses = prev;
29385 error_at (here, "%qs is not valid for %qs", c_name, where);
29389 saw_error:
29390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29392 if (finish_p)
29393 return finish_omp_clauses (clauses);
29395 return clauses;
29398 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29399 is a bitmask in MASK. Return the list of clauses found; the result
29400 of clause default goes in *pdefault. */
29402 static tree
29403 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29404 const char *where, cp_token *pragma_tok,
29405 bool finish_p = true)
29407 tree clauses = NULL;
29408 bool first = true;
29409 cp_token *token = NULL;
29410 bool cilk_simd_fn = false;
29412 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29414 pragma_omp_clause c_kind;
29415 const char *c_name;
29416 tree prev = clauses;
29418 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29419 cp_lexer_consume_token (parser->lexer);
29421 token = cp_lexer_peek_token (parser->lexer);
29422 c_kind = cp_parser_omp_clause_name (parser);
29424 switch (c_kind)
29426 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29427 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29428 token->location);
29429 c_name = "collapse";
29430 break;
29431 case PRAGMA_OMP_CLAUSE_COPYIN:
29432 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29433 c_name = "copyin";
29434 break;
29435 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29436 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29437 clauses);
29438 c_name = "copyprivate";
29439 break;
29440 case PRAGMA_OMP_CLAUSE_DEFAULT:
29441 clauses = cp_parser_omp_clause_default (parser, clauses,
29442 token->location);
29443 c_name = "default";
29444 break;
29445 case PRAGMA_OMP_CLAUSE_FINAL:
29446 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29447 c_name = "final";
29448 break;
29449 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29450 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29451 clauses);
29452 c_name = "firstprivate";
29453 break;
29454 case PRAGMA_OMP_CLAUSE_IF:
29455 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29456 c_name = "if";
29457 break;
29458 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29459 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29460 clauses);
29461 c_name = "lastprivate";
29462 break;
29463 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29464 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29465 token->location);
29466 c_name = "mergeable";
29467 break;
29468 case PRAGMA_OMP_CLAUSE_NOWAIT:
29469 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29470 c_name = "nowait";
29471 break;
29472 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29473 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29474 token->location);
29475 c_name = "num_threads";
29476 break;
29477 case PRAGMA_OMP_CLAUSE_ORDERED:
29478 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29479 token->location);
29480 c_name = "ordered";
29481 break;
29482 case PRAGMA_OMP_CLAUSE_PRIVATE:
29483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29484 clauses);
29485 c_name = "private";
29486 break;
29487 case PRAGMA_OMP_CLAUSE_REDUCTION:
29488 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29489 c_name = "reduction";
29490 break;
29491 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29492 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29493 token->location);
29494 c_name = "schedule";
29495 break;
29496 case PRAGMA_OMP_CLAUSE_SHARED:
29497 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29498 clauses);
29499 c_name = "shared";
29500 break;
29501 case PRAGMA_OMP_CLAUSE_UNTIED:
29502 clauses = cp_parser_omp_clause_untied (parser, clauses,
29503 token->location);
29504 c_name = "untied";
29505 break;
29506 case PRAGMA_OMP_CLAUSE_INBRANCH:
29507 case PRAGMA_CILK_CLAUSE_MASK:
29508 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29509 clauses, token->location);
29510 c_name = "inbranch";
29511 break;
29512 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29513 case PRAGMA_CILK_CLAUSE_NOMASK:
29514 clauses = cp_parser_omp_clause_branch (parser,
29515 OMP_CLAUSE_NOTINBRANCH,
29516 clauses, token->location);
29517 c_name = "notinbranch";
29518 break;
29519 case PRAGMA_OMP_CLAUSE_PARALLEL:
29520 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29521 clauses, token->location);
29522 c_name = "parallel";
29523 if (!first)
29525 clause_not_first:
29526 error_at (token->location, "%qs must be the first clause of %qs",
29527 c_name, where);
29528 clauses = prev;
29530 break;
29531 case PRAGMA_OMP_CLAUSE_FOR:
29532 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29533 clauses, token->location);
29534 c_name = "for";
29535 if (!first)
29536 goto clause_not_first;
29537 break;
29538 case PRAGMA_OMP_CLAUSE_SECTIONS:
29539 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29540 clauses, token->location);
29541 c_name = "sections";
29542 if (!first)
29543 goto clause_not_first;
29544 break;
29545 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29546 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29547 clauses, token->location);
29548 c_name = "taskgroup";
29549 if (!first)
29550 goto clause_not_first;
29551 break;
29552 case PRAGMA_OMP_CLAUSE_TO:
29553 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29554 clauses);
29555 c_name = "to";
29556 break;
29557 case PRAGMA_OMP_CLAUSE_FROM:
29558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29559 clauses);
29560 c_name = "from";
29561 break;
29562 case PRAGMA_OMP_CLAUSE_UNIFORM:
29563 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29564 clauses);
29565 c_name = "uniform";
29566 break;
29567 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29568 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29569 token->location);
29570 c_name = "num_teams";
29571 break;
29572 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29573 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29574 token->location);
29575 c_name = "thread_limit";
29576 break;
29577 case PRAGMA_OMP_CLAUSE_ALIGNED:
29578 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29579 c_name = "aligned";
29580 break;
29581 case PRAGMA_OMP_CLAUSE_LINEAR:
29582 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29583 cilk_simd_fn = true;
29584 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29585 c_name = "linear";
29586 break;
29587 case PRAGMA_OMP_CLAUSE_DEPEND:
29588 clauses = cp_parser_omp_clause_depend (parser, clauses);
29589 c_name = "depend";
29590 break;
29591 case PRAGMA_OMP_CLAUSE_MAP:
29592 clauses = cp_parser_omp_clause_map (parser, clauses);
29593 c_name = "map";
29594 break;
29595 case PRAGMA_OMP_CLAUSE_DEVICE:
29596 clauses = cp_parser_omp_clause_device (parser, clauses,
29597 token->location);
29598 c_name = "device";
29599 break;
29600 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29601 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29602 token->location);
29603 c_name = "dist_schedule";
29604 break;
29605 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29606 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29607 token->location);
29608 c_name = "proc_bind";
29609 break;
29610 case PRAGMA_OMP_CLAUSE_SAFELEN:
29611 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29612 token->location);
29613 c_name = "safelen";
29614 break;
29615 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29616 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29617 token->location);
29618 c_name = "simdlen";
29619 break;
29620 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29621 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29622 c_name = "simdlen";
29623 break;
29624 default:
29625 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29626 goto saw_error;
29629 first = false;
29631 if (((mask >> c_kind) & 1) == 0)
29633 /* Remove the invalid clause(s) from the list to avoid
29634 confusing the rest of the compiler. */
29635 clauses = prev;
29636 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29639 saw_error:
29640 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29641 no reason to skip to the end. */
29642 if (!(flag_cilkplus && pragma_tok == NULL))
29643 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29644 if (finish_p)
29645 return finish_omp_clauses (clauses);
29646 return clauses;
29649 /* OpenMP 2.5:
29650 structured-block:
29651 statement
29653 In practice, we're also interested in adding the statement to an
29654 outer node. So it is convenient if we work around the fact that
29655 cp_parser_statement calls add_stmt. */
29657 static unsigned
29658 cp_parser_begin_omp_structured_block (cp_parser *parser)
29660 unsigned save = parser->in_statement;
29662 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29663 This preserves the "not within loop or switch" style error messages
29664 for nonsense cases like
29665 void foo() {
29666 #pragma omp single
29667 break;
29670 if (parser->in_statement)
29671 parser->in_statement = IN_OMP_BLOCK;
29673 return save;
29676 static void
29677 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29679 parser->in_statement = save;
29682 static tree
29683 cp_parser_omp_structured_block (cp_parser *parser)
29685 tree stmt = begin_omp_structured_block ();
29686 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29688 cp_parser_statement (parser, NULL_TREE, false, NULL);
29690 cp_parser_end_omp_structured_block (parser, save);
29691 return finish_omp_structured_block (stmt);
29694 /* OpenMP 2.5:
29695 # pragma omp atomic new-line
29696 expression-stmt
29698 expression-stmt:
29699 x binop= expr | x++ | ++x | x-- | --x
29700 binop:
29701 +, *, -, /, &, ^, |, <<, >>
29703 where x is an lvalue expression with scalar type.
29705 OpenMP 3.1:
29706 # pragma omp atomic new-line
29707 update-stmt
29709 # pragma omp atomic read new-line
29710 read-stmt
29712 # pragma omp atomic write new-line
29713 write-stmt
29715 # pragma omp atomic update new-line
29716 update-stmt
29718 # pragma omp atomic capture new-line
29719 capture-stmt
29721 # pragma omp atomic capture new-line
29722 capture-block
29724 read-stmt:
29725 v = x
29726 write-stmt:
29727 x = expr
29728 update-stmt:
29729 expression-stmt | x = x binop expr
29730 capture-stmt:
29731 v = expression-stmt
29732 capture-block:
29733 { v = x; update-stmt; } | { update-stmt; v = x; }
29735 OpenMP 4.0:
29736 update-stmt:
29737 expression-stmt | x = x binop expr | x = expr binop x
29738 capture-stmt:
29739 v = update-stmt
29740 capture-block:
29741 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29743 where x and v are lvalue expressions with scalar type. */
29745 static void
29746 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29748 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29749 tree rhs1 = NULL_TREE, orig_lhs;
29750 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29751 bool structured_block = false;
29752 bool seq_cst = false;
29754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29757 const char *p = IDENTIFIER_POINTER (id);
29759 if (!strcmp (p, "seq_cst"))
29761 seq_cst = true;
29762 cp_lexer_consume_token (parser->lexer);
29763 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29764 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29765 cp_lexer_consume_token (parser->lexer);
29768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29771 const char *p = IDENTIFIER_POINTER (id);
29773 if (!strcmp (p, "read"))
29774 code = OMP_ATOMIC_READ;
29775 else if (!strcmp (p, "write"))
29776 code = NOP_EXPR;
29777 else if (!strcmp (p, "update"))
29778 code = OMP_ATOMIC;
29779 else if (!strcmp (p, "capture"))
29780 code = OMP_ATOMIC_CAPTURE_NEW;
29781 else
29782 p = NULL;
29783 if (p)
29784 cp_lexer_consume_token (parser->lexer);
29786 if (!seq_cst)
29788 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29789 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29790 cp_lexer_consume_token (parser->lexer);
29792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29794 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29795 const char *p = IDENTIFIER_POINTER (id);
29797 if (!strcmp (p, "seq_cst"))
29799 seq_cst = true;
29800 cp_lexer_consume_token (parser->lexer);
29804 cp_parser_require_pragma_eol (parser, pragma_tok);
29806 switch (code)
29808 case OMP_ATOMIC_READ:
29809 case NOP_EXPR: /* atomic write */
29810 v = cp_parser_unary_expression (parser);
29811 if (v == error_mark_node)
29812 goto saw_error;
29813 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29814 goto saw_error;
29815 if (code == NOP_EXPR)
29816 lhs = cp_parser_expression (parser);
29817 else
29818 lhs = cp_parser_unary_expression (parser);
29819 if (lhs == error_mark_node)
29820 goto saw_error;
29821 if (code == NOP_EXPR)
29823 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29824 opcode. */
29825 code = OMP_ATOMIC;
29826 rhs = lhs;
29827 lhs = v;
29828 v = NULL_TREE;
29830 goto done;
29831 case OMP_ATOMIC_CAPTURE_NEW:
29832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29834 cp_lexer_consume_token (parser->lexer);
29835 structured_block = true;
29837 else
29839 v = cp_parser_unary_expression (parser);
29840 if (v == error_mark_node)
29841 goto saw_error;
29842 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29843 goto saw_error;
29845 default:
29846 break;
29849 restart:
29850 lhs = cp_parser_unary_expression (parser);
29851 orig_lhs = lhs;
29852 switch (TREE_CODE (lhs))
29854 case ERROR_MARK:
29855 goto saw_error;
29857 case POSTINCREMENT_EXPR:
29858 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29859 code = OMP_ATOMIC_CAPTURE_OLD;
29860 /* FALLTHROUGH */
29861 case PREINCREMENT_EXPR:
29862 lhs = TREE_OPERAND (lhs, 0);
29863 opcode = PLUS_EXPR;
29864 rhs = integer_one_node;
29865 break;
29867 case POSTDECREMENT_EXPR:
29868 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29869 code = OMP_ATOMIC_CAPTURE_OLD;
29870 /* FALLTHROUGH */
29871 case PREDECREMENT_EXPR:
29872 lhs = TREE_OPERAND (lhs, 0);
29873 opcode = MINUS_EXPR;
29874 rhs = integer_one_node;
29875 break;
29877 case COMPOUND_EXPR:
29878 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29879 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29880 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29881 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29882 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29883 (TREE_OPERAND (lhs, 1), 0), 0)))
29884 == BOOLEAN_TYPE)
29885 /* Undo effects of boolean_increment for post {in,de}crement. */
29886 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29887 /* FALLTHRU */
29888 case MODIFY_EXPR:
29889 if (TREE_CODE (lhs) == MODIFY_EXPR
29890 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29892 /* Undo effects of boolean_increment. */
29893 if (integer_onep (TREE_OPERAND (lhs, 1)))
29895 /* This is pre or post increment. */
29896 rhs = TREE_OPERAND (lhs, 1);
29897 lhs = TREE_OPERAND (lhs, 0);
29898 opcode = NOP_EXPR;
29899 if (code == OMP_ATOMIC_CAPTURE_NEW
29900 && !structured_block
29901 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29902 code = OMP_ATOMIC_CAPTURE_OLD;
29903 break;
29906 /* FALLTHRU */
29907 default:
29908 switch (cp_lexer_peek_token (parser->lexer)->type)
29910 case CPP_MULT_EQ:
29911 opcode = MULT_EXPR;
29912 break;
29913 case CPP_DIV_EQ:
29914 opcode = TRUNC_DIV_EXPR;
29915 break;
29916 case CPP_PLUS_EQ:
29917 opcode = PLUS_EXPR;
29918 break;
29919 case CPP_MINUS_EQ:
29920 opcode = MINUS_EXPR;
29921 break;
29922 case CPP_LSHIFT_EQ:
29923 opcode = LSHIFT_EXPR;
29924 break;
29925 case CPP_RSHIFT_EQ:
29926 opcode = RSHIFT_EXPR;
29927 break;
29928 case CPP_AND_EQ:
29929 opcode = BIT_AND_EXPR;
29930 break;
29931 case CPP_OR_EQ:
29932 opcode = BIT_IOR_EXPR;
29933 break;
29934 case CPP_XOR_EQ:
29935 opcode = BIT_XOR_EXPR;
29936 break;
29937 case CPP_EQ:
29938 enum cp_parser_prec oprec;
29939 cp_token *token;
29940 cp_lexer_consume_token (parser->lexer);
29941 cp_parser_parse_tentatively (parser);
29942 rhs1 = cp_parser_simple_cast_expression (parser);
29943 if (rhs1 == error_mark_node)
29945 cp_parser_abort_tentative_parse (parser);
29946 cp_parser_simple_cast_expression (parser);
29947 goto saw_error;
29949 token = cp_lexer_peek_token (parser->lexer);
29950 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29952 cp_parser_abort_tentative_parse (parser);
29953 cp_parser_parse_tentatively (parser);
29954 rhs = cp_parser_binary_expression (parser, false, true,
29955 PREC_NOT_OPERATOR, NULL);
29956 if (rhs == error_mark_node)
29958 cp_parser_abort_tentative_parse (parser);
29959 cp_parser_binary_expression (parser, false, true,
29960 PREC_NOT_OPERATOR, NULL);
29961 goto saw_error;
29963 switch (TREE_CODE (rhs))
29965 case MULT_EXPR:
29966 case TRUNC_DIV_EXPR:
29967 case RDIV_EXPR:
29968 case PLUS_EXPR:
29969 case MINUS_EXPR:
29970 case LSHIFT_EXPR:
29971 case RSHIFT_EXPR:
29972 case BIT_AND_EXPR:
29973 case BIT_IOR_EXPR:
29974 case BIT_XOR_EXPR:
29975 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29977 if (cp_parser_parse_definitely (parser))
29979 opcode = TREE_CODE (rhs);
29980 rhs1 = TREE_OPERAND (rhs, 0);
29981 rhs = TREE_OPERAND (rhs, 1);
29982 goto stmt_done;
29984 else
29985 goto saw_error;
29987 break;
29988 default:
29989 break;
29991 cp_parser_abort_tentative_parse (parser);
29992 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29994 rhs = cp_parser_expression (parser);
29995 if (rhs == error_mark_node)
29996 goto saw_error;
29997 opcode = NOP_EXPR;
29998 rhs1 = NULL_TREE;
29999 goto stmt_done;
30001 cp_parser_error (parser,
30002 "invalid form of %<#pragma omp atomic%>");
30003 goto saw_error;
30005 if (!cp_parser_parse_definitely (parser))
30006 goto saw_error;
30007 switch (token->type)
30009 case CPP_SEMICOLON:
30010 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30012 code = OMP_ATOMIC_CAPTURE_OLD;
30013 v = lhs;
30014 lhs = NULL_TREE;
30015 lhs1 = rhs1;
30016 rhs1 = NULL_TREE;
30017 cp_lexer_consume_token (parser->lexer);
30018 goto restart;
30020 else if (structured_block)
30022 opcode = NOP_EXPR;
30023 rhs = rhs1;
30024 rhs1 = NULL_TREE;
30025 goto stmt_done;
30027 cp_parser_error (parser,
30028 "invalid form of %<#pragma omp atomic%>");
30029 goto saw_error;
30030 case CPP_MULT:
30031 opcode = MULT_EXPR;
30032 break;
30033 case CPP_DIV:
30034 opcode = TRUNC_DIV_EXPR;
30035 break;
30036 case CPP_PLUS:
30037 opcode = PLUS_EXPR;
30038 break;
30039 case CPP_MINUS:
30040 opcode = MINUS_EXPR;
30041 break;
30042 case CPP_LSHIFT:
30043 opcode = LSHIFT_EXPR;
30044 break;
30045 case CPP_RSHIFT:
30046 opcode = RSHIFT_EXPR;
30047 break;
30048 case CPP_AND:
30049 opcode = BIT_AND_EXPR;
30050 break;
30051 case CPP_OR:
30052 opcode = BIT_IOR_EXPR;
30053 break;
30054 case CPP_XOR:
30055 opcode = BIT_XOR_EXPR;
30056 break;
30057 default:
30058 cp_parser_error (parser,
30059 "invalid operator for %<#pragma omp atomic%>");
30060 goto saw_error;
30062 oprec = TOKEN_PRECEDENCE (token);
30063 gcc_assert (oprec != PREC_NOT_OPERATOR);
30064 if (commutative_tree_code (opcode))
30065 oprec = (enum cp_parser_prec) (oprec - 1);
30066 cp_lexer_consume_token (parser->lexer);
30067 rhs = cp_parser_binary_expression (parser, false, false,
30068 oprec, NULL);
30069 if (rhs == error_mark_node)
30070 goto saw_error;
30071 goto stmt_done;
30072 /* FALLTHROUGH */
30073 default:
30074 cp_parser_error (parser,
30075 "invalid operator for %<#pragma omp atomic%>");
30076 goto saw_error;
30078 cp_lexer_consume_token (parser->lexer);
30080 rhs = cp_parser_expression (parser);
30081 if (rhs == error_mark_node)
30082 goto saw_error;
30083 break;
30085 stmt_done:
30086 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30088 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30089 goto saw_error;
30090 v = cp_parser_unary_expression (parser);
30091 if (v == error_mark_node)
30092 goto saw_error;
30093 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30094 goto saw_error;
30095 lhs1 = cp_parser_unary_expression (parser);
30096 if (lhs1 == error_mark_node)
30097 goto saw_error;
30099 if (structured_block)
30101 cp_parser_consume_semicolon_at_end_of_statement (parser);
30102 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30104 done:
30105 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30106 if (!structured_block)
30107 cp_parser_consume_semicolon_at_end_of_statement (parser);
30108 return;
30110 saw_error:
30111 cp_parser_skip_to_end_of_block_or_statement (parser);
30112 if (structured_block)
30114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30115 cp_lexer_consume_token (parser->lexer);
30116 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30118 cp_parser_skip_to_end_of_block_or_statement (parser);
30119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30120 cp_lexer_consume_token (parser->lexer);
30126 /* OpenMP 2.5:
30127 # pragma omp barrier new-line */
30129 static void
30130 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30132 cp_parser_require_pragma_eol (parser, pragma_tok);
30133 finish_omp_barrier ();
30136 /* OpenMP 2.5:
30137 # pragma omp critical [(name)] new-line
30138 structured-block */
30140 static tree
30141 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30143 tree stmt, name = NULL;
30145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30147 cp_lexer_consume_token (parser->lexer);
30149 name = cp_parser_identifier (parser);
30151 if (name == error_mark_node
30152 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30154 /*or_comma=*/false,
30155 /*consume_paren=*/true);
30156 if (name == error_mark_node)
30157 name = NULL;
30159 cp_parser_require_pragma_eol (parser, pragma_tok);
30161 stmt = cp_parser_omp_structured_block (parser);
30162 return c_finish_omp_critical (input_location, stmt, name);
30165 /* OpenMP 2.5:
30166 # pragma omp flush flush-vars[opt] new-line
30168 flush-vars:
30169 ( variable-list ) */
30171 static void
30172 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30174 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30175 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30176 cp_parser_require_pragma_eol (parser, pragma_tok);
30178 finish_omp_flush ();
30181 /* Helper function, to parse omp for increment expression. */
30183 static tree
30184 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30186 tree cond = cp_parser_binary_expression (parser, false, true,
30187 PREC_NOT_OPERATOR, NULL);
30188 if (cond == error_mark_node
30189 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30191 cp_parser_skip_to_end_of_statement (parser);
30192 return error_mark_node;
30195 switch (TREE_CODE (cond))
30197 case GT_EXPR:
30198 case GE_EXPR:
30199 case LT_EXPR:
30200 case LE_EXPR:
30201 break;
30202 case NE_EXPR:
30203 if (code == CILK_SIMD || code == CILK_FOR)
30204 break;
30205 /* Fall through: OpenMP disallows NE_EXPR. */
30206 default:
30207 return error_mark_node;
30210 /* If decl is an iterator, preserve LHS and RHS of the relational
30211 expr until finish_omp_for. */
30212 if (decl
30213 && (type_dependent_expression_p (decl)
30214 || CLASS_TYPE_P (TREE_TYPE (decl))))
30215 return cond;
30217 return build_x_binary_op (input_location, TREE_CODE (cond),
30218 TREE_OPERAND (cond, 0), ERROR_MARK,
30219 TREE_OPERAND (cond, 1), ERROR_MARK,
30220 /*overload=*/NULL, tf_warning_or_error);
30223 /* Helper function, to parse omp for increment expression. */
30225 static tree
30226 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30228 cp_token *token = cp_lexer_peek_token (parser->lexer);
30229 enum tree_code op;
30230 tree lhs, rhs;
30231 cp_id_kind idk;
30232 bool decl_first;
30234 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30236 op = (token->type == CPP_PLUS_PLUS
30237 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30238 cp_lexer_consume_token (parser->lexer);
30239 lhs = cp_parser_simple_cast_expression (parser);
30240 if (lhs != decl)
30241 return error_mark_node;
30242 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30245 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30246 if (lhs != decl)
30247 return error_mark_node;
30249 token = cp_lexer_peek_token (parser->lexer);
30250 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30252 op = (token->type == CPP_PLUS_PLUS
30253 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30254 cp_lexer_consume_token (parser->lexer);
30255 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30258 op = cp_parser_assignment_operator_opt (parser);
30259 if (op == ERROR_MARK)
30260 return error_mark_node;
30262 if (op != NOP_EXPR)
30264 rhs = cp_parser_assignment_expression (parser);
30265 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30266 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30269 lhs = cp_parser_binary_expression (parser, false, false,
30270 PREC_ADDITIVE_EXPRESSION, NULL);
30271 token = cp_lexer_peek_token (parser->lexer);
30272 decl_first = lhs == decl;
30273 if (decl_first)
30274 lhs = NULL_TREE;
30275 if (token->type != CPP_PLUS
30276 && token->type != CPP_MINUS)
30277 return error_mark_node;
30281 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30282 cp_lexer_consume_token (parser->lexer);
30283 rhs = cp_parser_binary_expression (parser, false, false,
30284 PREC_ADDITIVE_EXPRESSION, NULL);
30285 token = cp_lexer_peek_token (parser->lexer);
30286 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30288 if (lhs == NULL_TREE)
30290 if (op == PLUS_EXPR)
30291 lhs = rhs;
30292 else
30293 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30294 tf_warning_or_error);
30296 else
30297 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30298 ERROR_MARK, NULL, tf_warning_or_error);
30301 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30303 if (!decl_first)
30305 if (rhs != decl || op == MINUS_EXPR)
30306 return error_mark_node;
30307 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30309 else
30310 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30312 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30315 /* Parse the initialization statement of either an OpenMP for loop or
30316 a Cilk Plus for loop.
30318 Return true if the resulting construct should have an
30319 OMP_CLAUSE_PRIVATE added to it. */
30321 static bool
30322 cp_parser_omp_for_loop_init (cp_parser *parser,
30323 enum tree_code code,
30324 tree &this_pre_body,
30325 vec<tree, va_gc> *for_block,
30326 tree &init,
30327 tree &decl,
30328 tree &real_decl)
30330 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30331 return false;
30333 bool add_private_clause = false;
30335 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30337 init-expr:
30338 var = lb
30339 integer-type var = lb
30340 random-access-iterator-type var = lb
30341 pointer-type var = lb
30343 cp_decl_specifier_seq type_specifiers;
30345 /* First, try to parse as an initialized declaration. See
30346 cp_parser_condition, from whence the bulk of this is copied. */
30348 cp_parser_parse_tentatively (parser);
30349 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30350 /*is_trailing_return=*/false,
30351 &type_specifiers);
30352 if (cp_parser_parse_definitely (parser))
30354 /* If parsing a type specifier seq succeeded, then this
30355 MUST be a initialized declaration. */
30356 tree asm_specification, attributes;
30357 cp_declarator *declarator;
30359 declarator = cp_parser_declarator (parser,
30360 CP_PARSER_DECLARATOR_NAMED,
30361 /*ctor_dtor_or_conv_p=*/NULL,
30362 /*parenthesized_p=*/NULL,
30363 /*member_p=*/false,
30364 /*friend_p=*/false);
30365 attributes = cp_parser_attributes_opt (parser);
30366 asm_specification = cp_parser_asm_specification_opt (parser);
30368 if (declarator == cp_error_declarator)
30369 cp_parser_skip_to_end_of_statement (parser);
30371 else
30373 tree pushed_scope, auto_node;
30375 decl = start_decl (declarator, &type_specifiers,
30376 SD_INITIALIZED, attributes,
30377 /*prefix_attributes=*/NULL_TREE,
30378 &pushed_scope);
30380 auto_node = type_uses_auto (TREE_TYPE (decl));
30381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30383 if (cp_lexer_next_token_is (parser->lexer,
30384 CPP_OPEN_PAREN))
30386 if (code != CILK_SIMD && code != CILK_FOR)
30387 error ("parenthesized initialization is not allowed in "
30388 "OpenMP %<for%> loop");
30389 else
30390 error ("parenthesized initialization is "
30391 "not allowed in for-loop");
30393 else
30394 /* Trigger an error. */
30395 cp_parser_require (parser, CPP_EQ, RT_EQ);
30397 init = error_mark_node;
30398 cp_parser_skip_to_end_of_statement (parser);
30400 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30401 || type_dependent_expression_p (decl)
30402 || auto_node)
30404 bool is_direct_init, is_non_constant_init;
30406 init = cp_parser_initializer (parser,
30407 &is_direct_init,
30408 &is_non_constant_init);
30410 if (auto_node)
30412 TREE_TYPE (decl)
30413 = do_auto_deduction (TREE_TYPE (decl), init,
30414 auto_node);
30416 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30417 && !type_dependent_expression_p (decl))
30418 goto non_class;
30421 cp_finish_decl (decl, init, !is_non_constant_init,
30422 asm_specification,
30423 LOOKUP_ONLYCONVERTING);
30424 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30426 vec_safe_push (for_block, this_pre_body);
30427 init = NULL_TREE;
30429 else
30430 init = pop_stmt_list (this_pre_body);
30431 this_pre_body = NULL_TREE;
30433 else
30435 /* Consume '='. */
30436 cp_lexer_consume_token (parser->lexer);
30437 init = cp_parser_assignment_expression (parser);
30439 non_class:
30440 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30441 init = error_mark_node;
30442 else
30443 cp_finish_decl (decl, NULL_TREE,
30444 /*init_const_expr_p=*/false,
30445 asm_specification,
30446 LOOKUP_ONLYCONVERTING);
30449 if (pushed_scope)
30450 pop_scope (pushed_scope);
30453 else
30455 cp_id_kind idk;
30456 /* If parsing a type specifier sequence failed, then
30457 this MUST be a simple expression. */
30458 if (code == CILK_FOR)
30459 error ("%<_Cilk_for%> allows expression instead of declaration only "
30460 "in C, not in C++");
30461 cp_parser_parse_tentatively (parser);
30462 decl = cp_parser_primary_expression (parser, false, false,
30463 false, &idk);
30464 if (!cp_parser_error_occurred (parser)
30465 && decl
30466 && DECL_P (decl)
30467 && CLASS_TYPE_P (TREE_TYPE (decl)))
30469 tree rhs;
30471 cp_parser_parse_definitely (parser);
30472 cp_parser_require (parser, CPP_EQ, RT_EQ);
30473 rhs = cp_parser_assignment_expression (parser);
30474 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30475 decl, NOP_EXPR,
30476 rhs,
30477 tf_warning_or_error));
30478 add_private_clause = true;
30480 else
30482 decl = NULL;
30483 cp_parser_abort_tentative_parse (parser);
30484 init = cp_parser_expression (parser);
30485 if (init)
30487 if (TREE_CODE (init) == MODIFY_EXPR
30488 || TREE_CODE (init) == MODOP_EXPR)
30489 real_decl = TREE_OPERAND (init, 0);
30493 return add_private_clause;
30496 /* Parse the restricted form of the for statement allowed by OpenMP. */
30498 static tree
30499 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30500 tree *cclauses)
30502 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30503 tree real_decl, initv, condv, incrv, declv;
30504 tree this_pre_body, cl;
30505 location_t loc_first;
30506 bool collapse_err = false;
30507 int i, collapse = 1, nbraces = 0;
30508 vec<tree, va_gc> *for_block = make_tree_vector ();
30510 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30511 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30512 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30514 gcc_assert (collapse >= 1);
30516 declv = make_tree_vec (collapse);
30517 initv = make_tree_vec (collapse);
30518 condv = make_tree_vec (collapse);
30519 incrv = make_tree_vec (collapse);
30521 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30523 for (i = 0; i < collapse; i++)
30525 int bracecount = 0;
30526 bool add_private_clause = false;
30527 location_t loc;
30529 if (code != CILK_FOR
30530 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30532 cp_parser_error (parser, "for statement expected");
30533 return NULL;
30535 if (code == CILK_FOR
30536 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30538 cp_parser_error (parser, "_Cilk_for statement expected");
30539 return NULL;
30541 loc = cp_lexer_consume_token (parser->lexer)->location;
30543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30544 return NULL;
30546 init = decl = real_decl = NULL;
30547 this_pre_body = push_stmt_list ();
30549 add_private_clause
30550 |= cp_parser_omp_for_loop_init (parser, code,
30551 this_pre_body, for_block,
30552 init, decl, real_decl);
30554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30555 if (this_pre_body)
30557 this_pre_body = pop_stmt_list (this_pre_body);
30558 if (pre_body)
30560 tree t = pre_body;
30561 pre_body = push_stmt_list ();
30562 add_stmt (t);
30563 add_stmt (this_pre_body);
30564 pre_body = pop_stmt_list (pre_body);
30566 else
30567 pre_body = this_pre_body;
30570 if (decl)
30571 real_decl = decl;
30572 if (cclauses != NULL
30573 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30574 && real_decl != NULL_TREE)
30576 tree *c;
30577 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30578 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30579 && OMP_CLAUSE_DECL (*c) == real_decl)
30581 error_at (loc, "iteration variable %qD"
30582 " should not be firstprivate", real_decl);
30583 *c = OMP_CLAUSE_CHAIN (*c);
30585 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30586 && OMP_CLAUSE_DECL (*c) == real_decl)
30588 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
30589 tree l = *c;
30590 *c = OMP_CLAUSE_CHAIN (*c);
30591 if (code == OMP_SIMD)
30593 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30594 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30596 else
30598 OMP_CLAUSE_CHAIN (l) = clauses;
30599 clauses = l;
30601 add_private_clause = false;
30603 else
30605 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30606 && OMP_CLAUSE_DECL (*c) == real_decl)
30607 add_private_clause = false;
30608 c = &OMP_CLAUSE_CHAIN (*c);
30612 if (add_private_clause)
30614 tree c;
30615 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30617 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30618 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30619 && OMP_CLAUSE_DECL (c) == decl)
30620 break;
30621 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30622 && OMP_CLAUSE_DECL (c) == decl)
30623 error_at (loc, "iteration variable %qD "
30624 "should not be firstprivate",
30625 decl);
30626 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30627 && OMP_CLAUSE_DECL (c) == decl)
30628 error_at (loc, "iteration variable %qD should not be reduction",
30629 decl);
30631 if (c == NULL)
30633 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30634 OMP_CLAUSE_DECL (c) = decl;
30635 c = finish_omp_clauses (c);
30636 if (c)
30638 OMP_CLAUSE_CHAIN (c) = clauses;
30639 clauses = c;
30644 cond = NULL;
30645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30646 cond = cp_parser_omp_for_cond (parser, decl, code);
30647 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30649 incr = NULL;
30650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30652 /* If decl is an iterator, preserve the operator on decl
30653 until finish_omp_for. */
30654 if (real_decl
30655 && ((processing_template_decl
30656 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30657 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30658 incr = cp_parser_omp_for_incr (parser, real_decl);
30659 else
30660 incr = cp_parser_expression (parser);
30661 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30662 SET_EXPR_LOCATION (incr, input_location);
30665 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30666 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30667 /*or_comma=*/false,
30668 /*consume_paren=*/true);
30670 TREE_VEC_ELT (declv, i) = decl;
30671 TREE_VEC_ELT (initv, i) = init;
30672 TREE_VEC_ELT (condv, i) = cond;
30673 TREE_VEC_ELT (incrv, i) = incr;
30675 if (i == collapse - 1)
30676 break;
30678 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30679 in between the collapsed for loops to be still considered perfectly
30680 nested. Hopefully the final version clarifies this.
30681 For now handle (multiple) {'s and empty statements. */
30682 cp_parser_parse_tentatively (parser);
30685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30686 break;
30687 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30689 cp_lexer_consume_token (parser->lexer);
30690 bracecount++;
30692 else if (bracecount
30693 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30694 cp_lexer_consume_token (parser->lexer);
30695 else
30697 loc = cp_lexer_peek_token (parser->lexer)->location;
30698 error_at (loc, "not enough collapsed for loops");
30699 collapse_err = true;
30700 cp_parser_abort_tentative_parse (parser);
30701 declv = NULL_TREE;
30702 break;
30705 while (1);
30707 if (declv)
30709 cp_parser_parse_definitely (parser);
30710 nbraces += bracecount;
30714 /* Note that we saved the original contents of this flag when we entered
30715 the structured block, and so we don't need to re-save it here. */
30716 if (code == CILK_SIMD || code == CILK_FOR)
30717 parser->in_statement = IN_CILK_SIMD_FOR;
30718 else
30719 parser->in_statement = IN_OMP_FOR;
30721 /* Note that the grammar doesn't call for a structured block here,
30722 though the loop as a whole is a structured block. */
30723 body = push_stmt_list ();
30724 cp_parser_statement (parser, NULL_TREE, false, NULL);
30725 body = pop_stmt_list (body);
30727 if (declv == NULL_TREE)
30728 ret = NULL_TREE;
30729 else
30730 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30731 pre_body, clauses);
30733 while (nbraces)
30735 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30737 cp_lexer_consume_token (parser->lexer);
30738 nbraces--;
30740 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30741 cp_lexer_consume_token (parser->lexer);
30742 else
30744 if (!collapse_err)
30746 error_at (cp_lexer_peek_token (parser->lexer)->location,
30747 "collapsed loops not perfectly nested");
30749 collapse_err = true;
30750 cp_parser_statement_seq_opt (parser, NULL);
30751 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30752 break;
30756 while (!for_block->is_empty ())
30757 add_stmt (pop_stmt_list (for_block->pop ()));
30758 release_tree_vector (for_block);
30760 return ret;
30763 /* Helper function for OpenMP parsing, split clauses and call
30764 finish_omp_clauses on each of the set of clauses afterwards. */
30766 static void
30767 cp_omp_split_clauses (location_t loc, enum tree_code code,
30768 omp_clause_mask mask, tree clauses, tree *cclauses)
30770 int i;
30771 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30772 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30773 if (cclauses[i])
30774 cclauses[i] = finish_omp_clauses (cclauses[i]);
30777 /* OpenMP 4.0:
30778 #pragma omp simd simd-clause[optseq] new-line
30779 for-loop */
30781 #define OMP_SIMD_CLAUSE_MASK \
30782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30790 static tree
30791 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30792 char *p_name, omp_clause_mask mask, tree *cclauses)
30794 tree clauses, sb, ret;
30795 unsigned int save;
30796 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30798 strcat (p_name, " simd");
30799 mask |= OMP_SIMD_CLAUSE_MASK;
30800 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30802 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30803 cclauses == NULL);
30804 if (cclauses)
30806 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30807 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30810 sb = begin_omp_structured_block ();
30811 save = cp_parser_begin_omp_structured_block (parser);
30813 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30815 cp_parser_end_omp_structured_block (parser, save);
30816 add_stmt (finish_omp_structured_block (sb));
30818 return ret;
30821 /* OpenMP 2.5:
30822 #pragma omp for for-clause[optseq] new-line
30823 for-loop
30825 OpenMP 4.0:
30826 #pragma omp for simd for-simd-clause[optseq] new-line
30827 for-loop */
30829 #define OMP_FOR_CLAUSE_MASK \
30830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30839 static tree
30840 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30841 char *p_name, omp_clause_mask mask, tree *cclauses)
30843 tree clauses, sb, ret;
30844 unsigned int save;
30845 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30847 strcat (p_name, " for");
30848 mask |= OMP_FOR_CLAUSE_MASK;
30849 if (cclauses)
30850 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30852 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30854 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30855 const char *p = IDENTIFIER_POINTER (id);
30857 if (strcmp (p, "simd") == 0)
30859 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30860 if (cclauses == NULL)
30861 cclauses = cclauses_buf;
30863 cp_lexer_consume_token (parser->lexer);
30864 if (!flag_openmp) /* flag_openmp_simd */
30865 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30866 cclauses);
30867 sb = begin_omp_structured_block ();
30868 save = cp_parser_begin_omp_structured_block (parser);
30869 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30870 cclauses);
30871 cp_parser_end_omp_structured_block (parser, save);
30872 tree body = finish_omp_structured_block (sb);
30873 if (ret == NULL)
30874 return ret;
30875 ret = make_node (OMP_FOR);
30876 TREE_TYPE (ret) = void_type_node;
30877 OMP_FOR_BODY (ret) = body;
30878 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30879 SET_EXPR_LOCATION (ret, loc);
30880 add_stmt (ret);
30881 return ret;
30884 if (!flag_openmp) /* flag_openmp_simd */
30886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30887 return NULL_TREE;
30890 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30891 cclauses == NULL);
30892 if (cclauses)
30894 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30895 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30898 sb = begin_omp_structured_block ();
30899 save = cp_parser_begin_omp_structured_block (parser);
30901 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30903 cp_parser_end_omp_structured_block (parser, save);
30904 add_stmt (finish_omp_structured_block (sb));
30906 return ret;
30909 /* OpenMP 2.5:
30910 # pragma omp master new-line
30911 structured-block */
30913 static tree
30914 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30916 cp_parser_require_pragma_eol (parser, pragma_tok);
30917 return c_finish_omp_master (input_location,
30918 cp_parser_omp_structured_block (parser));
30921 /* OpenMP 2.5:
30922 # pragma omp ordered new-line
30923 structured-block */
30925 static tree
30926 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30928 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30929 cp_parser_require_pragma_eol (parser, pragma_tok);
30930 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30933 /* OpenMP 2.5:
30935 section-scope:
30936 { section-sequence }
30938 section-sequence:
30939 section-directive[opt] structured-block
30940 section-sequence section-directive structured-block */
30942 static tree
30943 cp_parser_omp_sections_scope (cp_parser *parser)
30945 tree stmt, substmt;
30946 bool error_suppress = false;
30947 cp_token *tok;
30949 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30950 return NULL_TREE;
30952 stmt = push_stmt_list ();
30954 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30956 substmt = cp_parser_omp_structured_block (parser);
30957 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30958 add_stmt (substmt);
30961 while (1)
30963 tok = cp_lexer_peek_token (parser->lexer);
30964 if (tok->type == CPP_CLOSE_BRACE)
30965 break;
30966 if (tok->type == CPP_EOF)
30967 break;
30969 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30971 cp_lexer_consume_token (parser->lexer);
30972 cp_parser_require_pragma_eol (parser, tok);
30973 error_suppress = false;
30975 else if (!error_suppress)
30977 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30978 error_suppress = true;
30981 substmt = cp_parser_omp_structured_block (parser);
30982 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30983 add_stmt (substmt);
30985 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30987 substmt = pop_stmt_list (stmt);
30989 stmt = make_node (OMP_SECTIONS);
30990 TREE_TYPE (stmt) = void_type_node;
30991 OMP_SECTIONS_BODY (stmt) = substmt;
30993 add_stmt (stmt);
30994 return stmt;
30997 /* OpenMP 2.5:
30998 # pragma omp sections sections-clause[optseq] newline
30999 sections-scope */
31001 #define OMP_SECTIONS_CLAUSE_MASK \
31002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31008 static tree
31009 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
31010 char *p_name, omp_clause_mask mask, tree *cclauses)
31012 tree clauses, ret;
31013 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31015 strcat (p_name, " sections");
31016 mask |= OMP_SECTIONS_CLAUSE_MASK;
31017 if (cclauses)
31018 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31020 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31021 cclauses == NULL);
31022 if (cclauses)
31024 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31028 ret = cp_parser_omp_sections_scope (parser);
31029 if (ret)
31030 OMP_SECTIONS_CLAUSES (ret) = clauses;
31032 return ret;
31035 /* OpenMP 2.5:
31036 # pragma omp parallel parallel-clause[optseq] new-line
31037 structured-block
31038 # pragma omp parallel for parallel-for-clause[optseq] new-line
31039 structured-block
31040 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31041 structured-block
31043 OpenMP 4.0:
31044 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31045 structured-block */
31047 #define OMP_PARALLEL_CLAUSE_MASK \
31048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31058 static tree
31059 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31060 char *p_name, omp_clause_mask mask, tree *cclauses)
31062 tree stmt, clauses, block;
31063 unsigned int save;
31064 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31066 strcat (p_name, " parallel");
31067 mask |= OMP_PARALLEL_CLAUSE_MASK;
31069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31071 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31072 if (cclauses == NULL)
31073 cclauses = cclauses_buf;
31075 cp_lexer_consume_token (parser->lexer);
31076 if (!flag_openmp) /* flag_openmp_simd */
31077 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31078 block = begin_omp_parallel ();
31079 save = cp_parser_begin_omp_structured_block (parser);
31080 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31081 cp_parser_end_omp_structured_block (parser, save);
31082 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31083 block);
31084 if (ret == NULL_TREE)
31085 return ret;
31086 OMP_PARALLEL_COMBINED (stmt) = 1;
31087 return stmt;
31089 else if (cclauses)
31091 error_at (loc, "expected %<for%> after %qs", p_name);
31092 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31093 return NULL_TREE;
31095 else if (!flag_openmp) /* flag_openmp_simd */
31097 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31098 return NULL_TREE;
31100 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31103 const char *p = IDENTIFIER_POINTER (id);
31104 if (strcmp (p, "sections") == 0)
31106 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31107 cclauses = cclauses_buf;
31109 cp_lexer_consume_token (parser->lexer);
31110 block = begin_omp_parallel ();
31111 save = cp_parser_begin_omp_structured_block (parser);
31112 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31113 cp_parser_end_omp_structured_block (parser, save);
31114 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31115 block);
31116 OMP_PARALLEL_COMBINED (stmt) = 1;
31117 return stmt;
31121 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31123 block = begin_omp_parallel ();
31124 save = cp_parser_begin_omp_structured_block (parser);
31125 cp_parser_statement (parser, NULL_TREE, false, NULL);
31126 cp_parser_end_omp_structured_block (parser, save);
31127 stmt = finish_omp_parallel (clauses, block);
31128 return stmt;
31131 /* OpenMP 2.5:
31132 # pragma omp single single-clause[optseq] new-line
31133 structured-block */
31135 #define OMP_SINGLE_CLAUSE_MASK \
31136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31141 static tree
31142 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31144 tree stmt = make_node (OMP_SINGLE);
31145 TREE_TYPE (stmt) = void_type_node;
31147 OMP_SINGLE_CLAUSES (stmt)
31148 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31149 "#pragma omp single", pragma_tok);
31150 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31152 return add_stmt (stmt);
31155 /* OpenMP 3.0:
31156 # pragma omp task task-clause[optseq] new-line
31157 structured-block */
31159 #define OMP_TASK_CLAUSE_MASK \
31160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31170 static tree
31171 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31173 tree clauses, block;
31174 unsigned int save;
31176 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31177 "#pragma omp task", pragma_tok);
31178 block = begin_omp_task ();
31179 save = cp_parser_begin_omp_structured_block (parser);
31180 cp_parser_statement (parser, NULL_TREE, false, NULL);
31181 cp_parser_end_omp_structured_block (parser, save);
31182 return finish_omp_task (clauses, block);
31185 /* OpenMP 3.0:
31186 # pragma omp taskwait new-line */
31188 static void
31189 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31191 cp_parser_require_pragma_eol (parser, pragma_tok);
31192 finish_omp_taskwait ();
31195 /* OpenMP 3.1:
31196 # pragma omp taskyield new-line */
31198 static void
31199 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31201 cp_parser_require_pragma_eol (parser, pragma_tok);
31202 finish_omp_taskyield ();
31205 /* OpenMP 4.0:
31206 # pragma omp taskgroup new-line
31207 structured-block */
31209 static tree
31210 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31212 cp_parser_require_pragma_eol (parser, pragma_tok);
31213 return c_finish_omp_taskgroup (input_location,
31214 cp_parser_omp_structured_block (parser));
31218 /* OpenMP 2.5:
31219 # pragma omp threadprivate (variable-list) */
31221 static void
31222 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31224 tree vars;
31226 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31227 cp_parser_require_pragma_eol (parser, pragma_tok);
31229 finish_omp_threadprivate (vars);
31232 /* OpenMP 4.0:
31233 # pragma omp cancel cancel-clause[optseq] new-line */
31235 #define OMP_CANCEL_CLAUSE_MASK \
31236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31242 static void
31243 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31245 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31246 "#pragma omp cancel", pragma_tok);
31247 finish_omp_cancel (clauses);
31250 /* OpenMP 4.0:
31251 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31253 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31259 static void
31260 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31262 tree clauses;
31263 bool point_seen = false;
31265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31268 const char *p = IDENTIFIER_POINTER (id);
31270 if (strcmp (p, "point") == 0)
31272 cp_lexer_consume_token (parser->lexer);
31273 point_seen = true;
31276 if (!point_seen)
31278 cp_parser_error (parser, "expected %<point%>");
31279 cp_parser_require_pragma_eol (parser, pragma_tok);
31280 return;
31283 clauses = cp_parser_omp_all_clauses (parser,
31284 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31285 "#pragma omp cancellation point",
31286 pragma_tok);
31287 finish_omp_cancellation_point (clauses);
31290 /* OpenMP 4.0:
31291 #pragma omp distribute distribute-clause[optseq] new-line
31292 for-loop */
31294 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31295 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31300 static tree
31301 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31302 char *p_name, omp_clause_mask mask, tree *cclauses)
31304 tree clauses, sb, ret;
31305 unsigned int save;
31306 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31308 strcat (p_name, " distribute");
31309 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31311 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31314 const char *p = IDENTIFIER_POINTER (id);
31315 bool simd = false;
31316 bool parallel = false;
31318 if (strcmp (p, "simd") == 0)
31319 simd = true;
31320 else
31321 parallel = strcmp (p, "parallel") == 0;
31322 if (parallel || simd)
31324 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31325 if (cclauses == NULL)
31326 cclauses = cclauses_buf;
31327 cp_lexer_consume_token (parser->lexer);
31328 if (!flag_openmp) /* flag_openmp_simd */
31330 if (simd)
31331 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31332 cclauses);
31333 else
31334 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31335 cclauses);
31337 sb = begin_omp_structured_block ();
31338 save = cp_parser_begin_omp_structured_block (parser);
31339 if (simd)
31340 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31341 cclauses);
31342 else
31343 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31344 cclauses);
31345 cp_parser_end_omp_structured_block (parser, save);
31346 tree body = finish_omp_structured_block (sb);
31347 if (ret == NULL)
31348 return ret;
31349 ret = make_node (OMP_DISTRIBUTE);
31350 TREE_TYPE (ret) = void_type_node;
31351 OMP_FOR_BODY (ret) = body;
31352 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31353 SET_EXPR_LOCATION (ret, loc);
31354 add_stmt (ret);
31355 return ret;
31358 if (!flag_openmp) /* flag_openmp_simd */
31360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31361 return NULL_TREE;
31364 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31365 cclauses == NULL);
31366 if (cclauses)
31368 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31369 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31372 sb = begin_omp_structured_block ();
31373 save = cp_parser_begin_omp_structured_block (parser);
31375 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31377 cp_parser_end_omp_structured_block (parser, save);
31378 add_stmt (finish_omp_structured_block (sb));
31380 return ret;
31383 /* OpenMP 4.0:
31384 # pragma omp teams teams-clause[optseq] new-line
31385 structured-block */
31387 #define OMP_TEAMS_CLAUSE_MASK \
31388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31396 static tree
31397 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31398 char *p_name, omp_clause_mask mask, tree *cclauses)
31400 tree clauses, sb, ret;
31401 unsigned int save;
31402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31404 strcat (p_name, " teams");
31405 mask |= OMP_TEAMS_CLAUSE_MASK;
31407 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31409 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31410 const char *p = IDENTIFIER_POINTER (id);
31411 if (strcmp (p, "distribute") == 0)
31413 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31414 if (cclauses == NULL)
31415 cclauses = cclauses_buf;
31417 cp_lexer_consume_token (parser->lexer);
31418 if (!flag_openmp) /* flag_openmp_simd */
31419 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31420 cclauses);
31421 sb = begin_omp_structured_block ();
31422 save = cp_parser_begin_omp_structured_block (parser);
31423 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31424 cclauses);
31425 cp_parser_end_omp_structured_block (parser, save);
31426 tree body = finish_omp_structured_block (sb);
31427 if (ret == NULL)
31428 return ret;
31429 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31430 ret = make_node (OMP_TEAMS);
31431 TREE_TYPE (ret) = void_type_node;
31432 OMP_TEAMS_CLAUSES (ret) = clauses;
31433 OMP_TEAMS_BODY (ret) = body;
31434 OMP_TEAMS_COMBINED (ret) = 1;
31435 return add_stmt (ret);
31438 if (!flag_openmp) /* flag_openmp_simd */
31440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31441 return NULL_TREE;
31444 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31445 cclauses == NULL);
31446 if (cclauses)
31448 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31449 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31452 tree stmt = make_node (OMP_TEAMS);
31453 TREE_TYPE (stmt) = void_type_node;
31454 OMP_TEAMS_CLAUSES (stmt) = clauses;
31455 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31457 return add_stmt (stmt);
31460 /* OpenMP 4.0:
31461 # pragma omp target data target-data-clause[optseq] new-line
31462 structured-block */
31464 #define OMP_TARGET_DATA_CLAUSE_MASK \
31465 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31469 static tree
31470 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31472 tree stmt = make_node (OMP_TARGET_DATA);
31473 TREE_TYPE (stmt) = void_type_node;
31475 OMP_TARGET_DATA_CLAUSES (stmt)
31476 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31477 "#pragma omp target data", pragma_tok);
31478 keep_next_level (true);
31479 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31481 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31482 return add_stmt (stmt);
31485 /* OpenMP 4.0:
31486 # pragma omp target update target-update-clause[optseq] new-line */
31488 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31494 static bool
31495 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31496 enum pragma_context context)
31498 if (context == pragma_stmt)
31500 error_at (pragma_tok->location,
31501 "%<#pragma omp target update%> may only be "
31502 "used in compound statements");
31503 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31504 return false;
31507 tree clauses
31508 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31509 "#pragma omp target update", pragma_tok);
31510 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31511 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31513 error_at (pragma_tok->location,
31514 "%<#pragma omp target update%> must contain at least one "
31515 "%<from%> or %<to%> clauses");
31516 return false;
31519 tree stmt = make_node (OMP_TARGET_UPDATE);
31520 TREE_TYPE (stmt) = void_type_node;
31521 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31522 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31523 add_stmt (stmt);
31524 return false;
31527 /* OpenMP 4.0:
31528 # pragma omp target target-clause[optseq] new-line
31529 structured-block */
31531 #define OMP_TARGET_CLAUSE_MASK \
31532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31536 static bool
31537 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31538 enum pragma_context context)
31540 if (context != pragma_stmt && context != pragma_compound)
31542 cp_parser_error (parser, "expected declaration specifiers");
31543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31544 return false;
31547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31550 const char *p = IDENTIFIER_POINTER (id);
31552 if (strcmp (p, "teams") == 0)
31554 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31555 char p_name[sizeof ("#pragma omp target teams distribute "
31556 "parallel for simd")];
31558 cp_lexer_consume_token (parser->lexer);
31559 strcpy (p_name, "#pragma omp target");
31560 if (!flag_openmp) /* flag_openmp_simd */
31562 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31563 OMP_TARGET_CLAUSE_MASK,
31564 cclauses);
31565 return stmt != NULL_TREE;
31567 keep_next_level (true);
31568 tree sb = begin_omp_structured_block ();
31569 unsigned save = cp_parser_begin_omp_structured_block (parser);
31570 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31571 OMP_TARGET_CLAUSE_MASK, cclauses);
31572 cp_parser_end_omp_structured_block (parser, save);
31573 tree body = finish_omp_structured_block (sb);
31574 if (ret == NULL_TREE)
31575 return false;
31576 tree stmt = make_node (OMP_TARGET);
31577 TREE_TYPE (stmt) = void_type_node;
31578 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31579 OMP_TARGET_BODY (stmt) = body;
31580 add_stmt (stmt);
31581 return true;
31583 else if (!flag_openmp) /* flag_openmp_simd */
31585 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31586 return false;
31588 else if (strcmp (p, "data") == 0)
31590 cp_lexer_consume_token (parser->lexer);
31591 cp_parser_omp_target_data (parser, pragma_tok);
31592 return true;
31594 else if (strcmp (p, "update") == 0)
31596 cp_lexer_consume_token (parser->lexer);
31597 return cp_parser_omp_target_update (parser, pragma_tok, context);
31601 tree stmt = make_node (OMP_TARGET);
31602 TREE_TYPE (stmt) = void_type_node;
31604 OMP_TARGET_CLAUSES (stmt)
31605 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31606 "#pragma omp target", pragma_tok);
31607 keep_next_level (true);
31608 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31610 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31611 add_stmt (stmt);
31612 return true;
31615 /* OpenACC 2.0:
31616 # pragma acc cache (variable-list) new-line
31619 static tree
31620 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31622 tree stmt, clauses;
31624 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31625 clauses = finish_omp_clauses (clauses);
31627 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31629 stmt = make_node (OACC_CACHE);
31630 TREE_TYPE (stmt) = void_type_node;
31631 OACC_CACHE_CLAUSES (stmt) = clauses;
31632 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31633 add_stmt (stmt);
31635 return stmt;
31638 /* OpenACC 2.0:
31639 # pragma acc data oacc-data-clause[optseq] new-line
31640 structured-block */
31642 #define OACC_DATA_CLAUSE_MASK \
31643 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31655 static tree
31656 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31658 tree stmt, clauses, block;
31659 unsigned int save;
31661 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31662 "#pragma acc data", pragma_tok);
31664 block = begin_omp_parallel ();
31665 save = cp_parser_begin_omp_structured_block (parser);
31666 cp_parser_statement (parser, NULL_TREE, false, NULL);
31667 cp_parser_end_omp_structured_block (parser, save);
31668 stmt = finish_oacc_data (clauses, block);
31669 return stmt;
31672 /* OpenACC 2.0:
31673 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31677 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31679 LOC is the location of the #pragma token.
31682 #define OACC_ENTER_DATA_CLAUSE_MASK \
31683 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31691 #define OACC_EXIT_DATA_CLAUSE_MASK \
31692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31698 static tree
31699 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31700 bool enter)
31702 tree stmt, clauses;
31704 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31705 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31707 cp_parser_error (parser, enter
31708 ? "expected %<data%> in %<#pragma acc enter data%>"
31709 : "expected %<data%> in %<#pragma acc exit data%>");
31710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31711 return NULL_TREE;
31714 const char *p =
31715 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31716 if (strcmp (p, "data") != 0)
31718 cp_parser_error (parser, "invalid pragma");
31719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31720 return NULL_TREE;
31723 cp_lexer_consume_token (parser->lexer);
31725 if (enter)
31726 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31727 "#pragma acc enter data", pragma_tok);
31728 else
31729 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31730 "#pragma acc exit data", pragma_tok);
31732 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31734 error_at (pragma_tok->location,
31735 "%<#pragma acc enter data%> has no data movement clause");
31736 return NULL_TREE;
31739 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31740 TREE_TYPE (stmt) = void_type_node;
31741 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31742 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31743 add_stmt (stmt);
31744 return stmt;
31747 /* OpenACC 2.0:
31748 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31749 structured-block */
31751 #define OACC_KERNELS_CLAUSE_MASK \
31752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31766 static tree
31767 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31769 tree stmt, clauses, block;
31770 unsigned int save;
31772 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31773 "#pragma acc kernels", pragma_tok);
31775 block = begin_omp_parallel ();
31776 save = cp_parser_begin_omp_structured_block (parser);
31777 cp_parser_statement (parser, NULL_TREE, false, NULL);
31778 cp_parser_end_omp_structured_block (parser, save);
31779 stmt = finish_oacc_kernels (clauses, block);
31780 return stmt;
31783 /* OpenACC 2.0:
31784 # pragma acc loop oacc-loop-clause[optseq] new-line
31785 structured-block */
31787 #define OACC_LOOP_CLAUSE_MASK \
31788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31791 static tree
31792 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31794 tree stmt, clauses, block;
31795 int save;
31797 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31798 "#pragma acc loop", pragma_tok);
31800 block = begin_omp_structured_block ();
31801 save = cp_parser_begin_omp_structured_block (parser);
31802 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31803 cp_parser_end_omp_structured_block (parser, save);
31804 add_stmt (finish_omp_structured_block (block));
31805 return stmt;
31808 /* OpenACC 2.0:
31809 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31810 structured-block */
31812 #define OACC_PARALLEL_CLAUSE_MASK \
31813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31831 static tree
31832 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31834 tree stmt, clauses, block;
31835 unsigned int save;
31837 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31838 "#pragma acc parallel", pragma_tok);
31840 block = begin_omp_parallel ();
31841 save = cp_parser_begin_omp_structured_block (parser);
31842 cp_parser_statement (parser, NULL_TREE, false, NULL);
31843 cp_parser_end_omp_structured_block (parser, save);
31844 stmt = finish_oacc_parallel (clauses, block);
31845 return stmt;
31848 /* OpenACC 2.0:
31849 # pragma acc update oacc-update-clause[optseq] new-line
31852 #define OACC_UPDATE_CLAUSE_MASK \
31853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31860 static tree
31861 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31863 tree stmt, clauses;
31865 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31866 "#pragma acc update", pragma_tok);
31868 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31870 error_at (pragma_tok->location,
31871 "%<#pragma acc update%> must contain at least one "
31872 "%<device%> or %<host/self%> clause");
31873 return NULL_TREE;
31876 stmt = make_node (OACC_UPDATE);
31877 TREE_TYPE (stmt) = void_type_node;
31878 OACC_UPDATE_CLAUSES (stmt) = clauses;
31879 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31880 add_stmt (stmt);
31881 return stmt;
31884 /* OpenACC 2.0:
31885 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31887 LOC is the location of the #pragma token.
31890 #define OACC_WAIT_CLAUSE_MASK \
31891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31893 static tree
31894 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31896 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31897 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31899 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31900 list = cp_parser_oacc_wait_list (parser, loc, list);
31902 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31903 "#pragma acc wait", pragma_tok);
31905 stmt = c_finish_oacc_wait (loc, list, clauses);
31907 return stmt;
31910 /* OpenMP 4.0:
31911 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31913 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31921 static void
31922 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31923 enum pragma_context context)
31925 bool first_p = parser->omp_declare_simd == NULL;
31926 cp_omp_declare_simd_data data;
31927 if (first_p)
31929 data.error_seen = false;
31930 data.fndecl_seen = false;
31931 data.tokens = vNULL;
31932 parser->omp_declare_simd = &data;
31934 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31935 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31936 cp_lexer_consume_token (parser->lexer);
31937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31938 parser->omp_declare_simd->error_seen = true;
31939 cp_parser_require_pragma_eol (parser, pragma_tok);
31940 struct cp_token_cache *cp
31941 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31942 parser->omp_declare_simd->tokens.safe_push (cp);
31943 if (first_p)
31945 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31946 cp_parser_pragma (parser, context);
31947 switch (context)
31949 case pragma_external:
31950 cp_parser_declaration (parser);
31951 break;
31952 case pragma_member:
31953 cp_parser_member_declaration (parser);
31954 break;
31955 case pragma_objc_icode:
31956 cp_parser_block_declaration (parser, /*statement_p=*/false);
31957 break;
31958 default:
31959 cp_parser_declaration_statement (parser);
31960 break;
31962 if (parser->omp_declare_simd
31963 && !parser->omp_declare_simd->error_seen
31964 && !parser->omp_declare_simd->fndecl_seen)
31965 error_at (pragma_tok->location,
31966 "%<#pragma omp declare simd%> not immediately followed by "
31967 "function declaration or definition");
31968 data.tokens.release ();
31969 parser->omp_declare_simd = NULL;
31973 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31974 This function is modelled similar to the late parsing of omp declare
31975 simd. */
31977 static tree
31978 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31980 struct cp_token_cache *ce;
31981 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31982 int ii = 0;
31984 if (parser->omp_declare_simd != NULL)
31986 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31987 " marked as a Cilk Plus SIMD-enabled function");
31988 XDELETE (parser->cilk_simd_fn_info);
31989 parser->cilk_simd_fn_info = NULL;
31990 return attrs;
31992 if (!info->error_seen && info->fndecl_seen)
31994 error ("vector attribute not immediately followed by a single function"
31995 " declaration or definition");
31996 info->error_seen = true;
31998 if (info->error_seen)
31999 return attrs;
32001 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
32003 tree c, cl;
32005 cp_parser_push_lexer_for_tokens (parser, ce);
32006 parser->lexer->in_pragma = true;
32007 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
32008 "SIMD-enabled functions attribute",
32009 NULL);
32010 cp_parser_pop_lexer (parser);
32011 if (cl)
32012 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32014 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
32015 TREE_CHAIN (c) = attrs;
32016 attrs = c;
32018 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32019 TREE_CHAIN (c) = attrs;
32020 if (processing_template_decl)
32021 ATTR_IS_DEPENDENT (c) = 1;
32022 attrs = c;
32024 info->fndecl_seen = true;
32025 XDELETE (parser->cilk_simd_fn_info);
32026 parser->cilk_simd_fn_info = NULL;
32027 return attrs;
32030 /* Finalize #pragma omp declare simd clauses after direct declarator has
32031 been parsed, and put that into "omp declare simd" attribute. */
32033 static tree
32034 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
32036 struct cp_token_cache *ce;
32037 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32038 int i;
32040 if (!data->error_seen && data->fndecl_seen)
32042 error ("%<#pragma omp declare simd%> not immediately followed by "
32043 "a single function declaration or definition");
32044 data->error_seen = true;
32045 return attrs;
32047 if (data->error_seen)
32048 return attrs;
32050 FOR_EACH_VEC_ELT (data->tokens, i, ce)
32052 tree c, cl;
32054 cp_parser_push_lexer_for_tokens (parser, ce);
32055 parser->lexer->in_pragma = true;
32056 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32057 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32058 cp_lexer_consume_token (parser->lexer);
32059 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32060 "#pragma omp declare simd", pragma_tok);
32061 cp_parser_pop_lexer (parser);
32062 if (cl)
32063 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32064 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32065 TREE_CHAIN (c) = attrs;
32066 if (processing_template_decl)
32067 ATTR_IS_DEPENDENT (c) = 1;
32068 attrs = c;
32071 data->fndecl_seen = true;
32072 return attrs;
32076 /* OpenMP 4.0:
32077 # pragma omp declare target new-line
32078 declarations and definitions
32079 # pragma omp end declare target new-line */
32081 static void
32082 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32084 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32085 scope_chain->omp_declare_target_attribute++;
32088 static void
32089 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32091 const char *p = "";
32092 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32095 p = IDENTIFIER_POINTER (id);
32097 if (strcmp (p, "declare") == 0)
32099 cp_lexer_consume_token (parser->lexer);
32100 p = "";
32101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32104 p = IDENTIFIER_POINTER (id);
32106 if (strcmp (p, "target") == 0)
32107 cp_lexer_consume_token (parser->lexer);
32108 else
32110 cp_parser_error (parser, "expected %<target%>");
32111 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32112 return;
32115 else
32117 cp_parser_error (parser, "expected %<declare%>");
32118 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32119 return;
32121 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32122 if (!scope_chain->omp_declare_target_attribute)
32123 error_at (pragma_tok->location,
32124 "%<#pragma omp end declare target%> without corresponding "
32125 "%<#pragma omp declare target%>");
32126 else
32127 scope_chain->omp_declare_target_attribute--;
32130 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32131 expression and optional initializer clause of
32132 #pragma omp declare reduction. We store the expression(s) as
32133 either 3, 6 or 7 special statements inside of the artificial function's
32134 body. The first two statements are DECL_EXPRs for the artificial
32135 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32136 expression that uses those variables.
32137 If there was any INITIALIZER clause, this is followed by further statements,
32138 the fourth and fifth statements are DECL_EXPRs for the artificial
32139 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32140 constructor variant (first token after open paren is not omp_priv),
32141 then the sixth statement is a statement with the function call expression
32142 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32143 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32144 to initialize the OMP_PRIV artificial variable and there is seventh
32145 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32147 static bool
32148 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32150 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32151 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32152 type = TREE_TYPE (type);
32153 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32154 DECL_ARTIFICIAL (omp_out) = 1;
32155 pushdecl (omp_out);
32156 add_decl_expr (omp_out);
32157 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32158 DECL_ARTIFICIAL (omp_in) = 1;
32159 pushdecl (omp_in);
32160 add_decl_expr (omp_in);
32161 tree combiner;
32162 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32164 keep_next_level (true);
32165 tree block = begin_omp_structured_block ();
32166 combiner = cp_parser_expression (parser);
32167 finish_expr_stmt (combiner);
32168 block = finish_omp_structured_block (block);
32169 add_stmt (block);
32171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32172 return false;
32174 const char *p = "";
32175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32178 p = IDENTIFIER_POINTER (id);
32181 if (strcmp (p, "initializer") == 0)
32183 cp_lexer_consume_token (parser->lexer);
32184 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32185 return false;
32187 p = "";
32188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32191 p = IDENTIFIER_POINTER (id);
32194 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32195 DECL_ARTIFICIAL (omp_priv) = 1;
32196 pushdecl (omp_priv);
32197 add_decl_expr (omp_priv);
32198 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32199 DECL_ARTIFICIAL (omp_orig) = 1;
32200 pushdecl (omp_orig);
32201 add_decl_expr (omp_orig);
32203 keep_next_level (true);
32204 block = begin_omp_structured_block ();
32206 bool ctor = false;
32207 if (strcmp (p, "omp_priv") == 0)
32209 bool is_direct_init, is_non_constant_init;
32210 ctor = true;
32211 cp_lexer_consume_token (parser->lexer);
32212 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32213 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32214 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32215 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32216 == CPP_CLOSE_PAREN
32217 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32218 == CPP_CLOSE_PAREN))
32220 finish_omp_structured_block (block);
32221 error ("invalid initializer clause");
32222 return false;
32224 initializer = cp_parser_initializer (parser, &is_direct_init,
32225 &is_non_constant_init);
32226 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32227 NULL_TREE, LOOKUP_ONLYCONVERTING);
32229 else
32231 cp_parser_parse_tentatively (parser);
32232 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32233 /*check_dependency_p=*/true,
32234 /*template_p=*/NULL,
32235 /*declarator_p=*/false,
32236 /*optional_p=*/false);
32237 vec<tree, va_gc> *args;
32238 if (fn_name == error_mark_node
32239 || cp_parser_error_occurred (parser)
32240 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32241 || ((args = cp_parser_parenthesized_expression_list
32242 (parser, non_attr, /*cast_p=*/false,
32243 /*allow_expansion_p=*/true,
32244 /*non_constant_p=*/NULL)),
32245 cp_parser_error_occurred (parser)))
32247 finish_omp_structured_block (block);
32248 cp_parser_abort_tentative_parse (parser);
32249 cp_parser_error (parser, "expected id-expression (arguments)");
32250 return false;
32252 unsigned int i;
32253 tree arg;
32254 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32255 if (arg == omp_priv
32256 || (TREE_CODE (arg) == ADDR_EXPR
32257 && TREE_OPERAND (arg, 0) == omp_priv))
32258 break;
32259 cp_parser_abort_tentative_parse (parser);
32260 if (arg == NULL_TREE)
32261 error ("one of the initializer call arguments should be %<omp_priv%>"
32262 " or %<&omp_priv%>");
32263 initializer = cp_parser_postfix_expression (parser, false, false, false,
32264 false, NULL);
32265 finish_expr_stmt (initializer);
32268 block = finish_omp_structured_block (block);
32269 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32270 add_stmt (block);
32272 if (ctor)
32273 add_decl_expr (omp_orig);
32275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32276 return false;
32279 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32280 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32282 return true;
32285 /* OpenMP 4.0
32286 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32287 initializer-clause[opt] new-line
32289 initializer-clause:
32290 initializer (omp_priv initializer)
32291 initializer (function-name (argument-list)) */
32293 static void
32294 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32295 enum pragma_context)
32297 auto_vec<tree> types;
32298 enum tree_code reduc_code = ERROR_MARK;
32299 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32300 unsigned int i;
32301 cp_token *first_token;
32302 cp_token_cache *cp;
32303 int errs;
32304 void *p;
32306 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32307 p = obstack_alloc (&declarator_obstack, 0);
32309 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32310 goto fail;
32312 switch (cp_lexer_peek_token (parser->lexer)->type)
32314 case CPP_PLUS:
32315 reduc_code = PLUS_EXPR;
32316 break;
32317 case CPP_MULT:
32318 reduc_code = MULT_EXPR;
32319 break;
32320 case CPP_MINUS:
32321 reduc_code = MINUS_EXPR;
32322 break;
32323 case CPP_AND:
32324 reduc_code = BIT_AND_EXPR;
32325 break;
32326 case CPP_XOR:
32327 reduc_code = BIT_XOR_EXPR;
32328 break;
32329 case CPP_OR:
32330 reduc_code = BIT_IOR_EXPR;
32331 break;
32332 case CPP_AND_AND:
32333 reduc_code = TRUTH_ANDIF_EXPR;
32334 break;
32335 case CPP_OR_OR:
32336 reduc_code = TRUTH_ORIF_EXPR;
32337 break;
32338 case CPP_NAME:
32339 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32340 break;
32341 default:
32342 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32343 "%<|%>, %<&&%>, %<||%> or identifier");
32344 goto fail;
32347 if (reduc_code != ERROR_MARK)
32348 cp_lexer_consume_token (parser->lexer);
32350 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32351 if (reduc_id == error_mark_node)
32352 goto fail;
32354 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32355 goto fail;
32357 /* Types may not be defined in declare reduction type list. */
32358 const char *saved_message;
32359 saved_message = parser->type_definition_forbidden_message;
32360 parser->type_definition_forbidden_message
32361 = G_("types may not be defined in declare reduction type list");
32362 bool saved_colon_corrects_to_scope_p;
32363 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32364 parser->colon_corrects_to_scope_p = false;
32365 bool saved_colon_doesnt_start_class_def_p;
32366 saved_colon_doesnt_start_class_def_p
32367 = parser->colon_doesnt_start_class_def_p;
32368 parser->colon_doesnt_start_class_def_p = true;
32370 while (true)
32372 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32373 type = cp_parser_type_id (parser);
32374 if (type == error_mark_node)
32376 else if (ARITHMETIC_TYPE_P (type)
32377 && (orig_reduc_id == NULL_TREE
32378 || (TREE_CODE (type) != COMPLEX_TYPE
32379 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32380 "min") == 0
32381 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32382 "max") == 0))))
32383 error_at (loc, "predeclared arithmetic type %qT in "
32384 "%<#pragma omp declare reduction%>", type);
32385 else if (TREE_CODE (type) == FUNCTION_TYPE
32386 || TREE_CODE (type) == METHOD_TYPE
32387 || TREE_CODE (type) == ARRAY_TYPE)
32388 error_at (loc, "function or array type %qT in "
32389 "%<#pragma omp declare reduction%>", type);
32390 else if (TREE_CODE (type) == REFERENCE_TYPE)
32391 error_at (loc, "reference type %qT in "
32392 "%<#pragma omp declare reduction%>", type);
32393 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32394 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32395 "%<#pragma omp declare reduction%>", type);
32396 else
32397 types.safe_push (type);
32399 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32400 cp_lexer_consume_token (parser->lexer);
32401 else
32402 break;
32405 /* Restore the saved message. */
32406 parser->type_definition_forbidden_message = saved_message;
32407 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32408 parser->colon_doesnt_start_class_def_p
32409 = saved_colon_doesnt_start_class_def_p;
32411 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32412 || types.is_empty ())
32414 fail:
32415 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32416 goto done;
32419 first_token = cp_lexer_peek_token (parser->lexer);
32420 cp = NULL;
32421 errs = errorcount;
32422 FOR_EACH_VEC_ELT (types, i, type)
32424 tree fntype
32425 = build_function_type_list (void_type_node,
32426 cp_build_reference_type (type, false),
32427 NULL_TREE);
32428 tree this_reduc_id = reduc_id;
32429 if (!dependent_type_p (type))
32430 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32431 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32432 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32433 DECL_ARTIFICIAL (fndecl) = 1;
32434 DECL_EXTERNAL (fndecl) = 1;
32435 DECL_DECLARED_INLINE_P (fndecl) = 1;
32436 DECL_IGNORED_P (fndecl) = 1;
32437 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32438 DECL_ATTRIBUTES (fndecl)
32439 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32440 DECL_ATTRIBUTES (fndecl));
32441 if (processing_template_decl)
32442 fndecl = push_template_decl (fndecl);
32443 bool block_scope = false;
32444 tree block = NULL_TREE;
32445 if (current_function_decl)
32447 block_scope = true;
32448 DECL_CONTEXT (fndecl) = global_namespace;
32449 if (!processing_template_decl)
32450 pushdecl (fndecl);
32452 else if (current_class_type)
32454 if (cp == NULL)
32456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32457 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32458 cp_lexer_consume_token (parser->lexer);
32459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32460 goto fail;
32461 cp = cp_token_cache_new (first_token,
32462 cp_lexer_peek_nth_token (parser->lexer,
32463 2));
32465 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32466 finish_member_declaration (fndecl);
32467 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32468 DECL_PENDING_INLINE_P (fndecl) = 1;
32469 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32470 continue;
32472 else
32474 DECL_CONTEXT (fndecl) = current_namespace;
32475 pushdecl (fndecl);
32477 if (!block_scope)
32478 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32479 else
32480 block = begin_omp_structured_block ();
32481 if (cp)
32483 cp_parser_push_lexer_for_tokens (parser, cp);
32484 parser->lexer->in_pragma = true;
32486 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32488 if (!block_scope)
32489 finish_function (0);
32490 else
32491 DECL_CONTEXT (fndecl) = current_function_decl;
32492 if (cp)
32493 cp_parser_pop_lexer (parser);
32494 goto fail;
32496 if (cp)
32497 cp_parser_pop_lexer (parser);
32498 if (!block_scope)
32499 finish_function (0);
32500 else
32502 DECL_CONTEXT (fndecl) = current_function_decl;
32503 block = finish_omp_structured_block (block);
32504 if (TREE_CODE (block) == BIND_EXPR)
32505 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32506 else if (TREE_CODE (block) == STATEMENT_LIST)
32507 DECL_SAVED_TREE (fndecl) = block;
32508 if (processing_template_decl)
32509 add_decl_expr (fndecl);
32511 cp_check_omp_declare_reduction (fndecl);
32512 if (cp == NULL && types.length () > 1)
32513 cp = cp_token_cache_new (first_token,
32514 cp_lexer_peek_nth_token (parser->lexer, 2));
32515 if (errs != errorcount)
32516 break;
32519 cp_parser_require_pragma_eol (parser, pragma_tok);
32521 done:
32522 /* Free any declarators allocated. */
32523 obstack_free (&declarator_obstack, p);
32526 /* OpenMP 4.0
32527 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32528 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32529 initializer-clause[opt] new-line
32530 #pragma omp declare target new-line */
32532 static void
32533 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32534 enum pragma_context context)
32536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32539 const char *p = IDENTIFIER_POINTER (id);
32541 if (strcmp (p, "simd") == 0)
32543 cp_lexer_consume_token (parser->lexer);
32544 cp_parser_omp_declare_simd (parser, pragma_tok,
32545 context);
32546 return;
32548 cp_ensure_no_omp_declare_simd (parser);
32549 if (strcmp (p, "reduction") == 0)
32551 cp_lexer_consume_token (parser->lexer);
32552 cp_parser_omp_declare_reduction (parser, pragma_tok,
32553 context);
32554 return;
32556 if (!flag_openmp) /* flag_openmp_simd */
32558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32559 return;
32561 if (strcmp (p, "target") == 0)
32563 cp_lexer_consume_token (parser->lexer);
32564 cp_parser_omp_declare_target (parser, pragma_tok);
32565 return;
32568 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32569 "or %<target%>");
32570 cp_parser_require_pragma_eol (parser, pragma_tok);
32573 /* Main entry point to OpenMP statement pragmas. */
32575 static void
32576 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32578 tree stmt;
32579 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32580 omp_clause_mask mask (0);
32582 switch (pragma_tok->pragma_kind)
32584 case PRAGMA_OACC_CACHE:
32585 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32586 break;
32587 case PRAGMA_OACC_DATA:
32588 stmt = cp_parser_oacc_data (parser, pragma_tok);
32589 break;
32590 case PRAGMA_OACC_ENTER_DATA:
32591 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32592 break;
32593 case PRAGMA_OACC_EXIT_DATA:
32594 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32595 break;
32596 case PRAGMA_OACC_KERNELS:
32597 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32598 break;
32599 case PRAGMA_OACC_LOOP:
32600 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32601 break;
32602 case PRAGMA_OACC_PARALLEL:
32603 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32604 break;
32605 case PRAGMA_OACC_UPDATE:
32606 stmt = cp_parser_oacc_update (parser, pragma_tok);
32607 break;
32608 case PRAGMA_OACC_WAIT:
32609 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32610 break;
32611 case PRAGMA_OMP_ATOMIC:
32612 cp_parser_omp_atomic (parser, pragma_tok);
32613 return;
32614 case PRAGMA_OMP_CRITICAL:
32615 stmt = cp_parser_omp_critical (parser, pragma_tok);
32616 break;
32617 case PRAGMA_OMP_DISTRIBUTE:
32618 strcpy (p_name, "#pragma omp");
32619 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32620 break;
32621 case PRAGMA_OMP_FOR:
32622 strcpy (p_name, "#pragma omp");
32623 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32624 break;
32625 case PRAGMA_OMP_MASTER:
32626 stmt = cp_parser_omp_master (parser, pragma_tok);
32627 break;
32628 case PRAGMA_OMP_ORDERED:
32629 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32630 break;
32631 case PRAGMA_OMP_PARALLEL:
32632 strcpy (p_name, "#pragma omp");
32633 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32634 break;
32635 case PRAGMA_OMP_SECTIONS:
32636 strcpy (p_name, "#pragma omp");
32637 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32638 break;
32639 case PRAGMA_OMP_SIMD:
32640 strcpy (p_name, "#pragma omp");
32641 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32642 break;
32643 case PRAGMA_OMP_SINGLE:
32644 stmt = cp_parser_omp_single (parser, pragma_tok);
32645 break;
32646 case PRAGMA_OMP_TASK:
32647 stmt = cp_parser_omp_task (parser, pragma_tok);
32648 break;
32649 case PRAGMA_OMP_TASKGROUP:
32650 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32651 break;
32652 case PRAGMA_OMP_TEAMS:
32653 strcpy (p_name, "#pragma omp");
32654 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32655 break;
32656 default:
32657 gcc_unreachable ();
32660 if (stmt)
32661 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32664 /* Transactional Memory parsing routines. */
32666 /* Parse a transaction attribute.
32668 txn-attribute:
32669 attribute
32670 [ [ identifier ] ]
32672 ??? Simplify this when C++0x bracket attributes are
32673 implemented properly. */
32675 static tree
32676 cp_parser_txn_attribute_opt (cp_parser *parser)
32678 cp_token *token;
32679 tree attr_name, attr = NULL;
32681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32682 return cp_parser_attributes_opt (parser);
32684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32685 return NULL_TREE;
32686 cp_lexer_consume_token (parser->lexer);
32687 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32688 goto error1;
32690 token = cp_lexer_peek_token (parser->lexer);
32691 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32693 token = cp_lexer_consume_token (parser->lexer);
32695 attr_name = (token->type == CPP_KEYWORD
32696 /* For keywords, use the canonical spelling,
32697 not the parsed identifier. */
32698 ? ridpointers[(int) token->keyword]
32699 : token->u.value);
32700 attr = build_tree_list (attr_name, NULL_TREE);
32702 else
32703 cp_parser_error (parser, "expected identifier");
32705 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32706 error1:
32707 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32708 return attr;
32711 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32713 transaction-statement:
32714 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32715 compound-statement
32716 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32719 static tree
32720 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32722 unsigned char old_in = parser->in_transaction;
32723 unsigned char this_in = 1, new_in;
32724 cp_token *token;
32725 tree stmt, attrs, noex;
32727 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32728 || keyword == RID_TRANSACTION_RELAXED);
32729 token = cp_parser_require_keyword (parser, keyword,
32730 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32731 : RT_TRANSACTION_RELAXED));
32732 gcc_assert (token != NULL);
32734 if (keyword == RID_TRANSACTION_RELAXED)
32735 this_in |= TM_STMT_ATTR_RELAXED;
32736 else
32738 attrs = cp_parser_txn_attribute_opt (parser);
32739 if (attrs)
32740 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32743 /* Parse a noexcept specification. */
32744 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32746 /* Keep track if we're in the lexical scope of an outer transaction. */
32747 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32749 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32751 parser->in_transaction = new_in;
32752 cp_parser_compound_statement (parser, NULL, false, false);
32753 parser->in_transaction = old_in;
32755 finish_transaction_stmt (stmt, NULL, this_in, noex);
32757 return stmt;
32760 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32762 transaction-expression:
32763 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32764 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32767 static tree
32768 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32770 unsigned char old_in = parser->in_transaction;
32771 unsigned char this_in = 1;
32772 cp_token *token;
32773 tree expr, noex;
32774 bool noex_expr;
32776 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32777 || keyword == RID_TRANSACTION_RELAXED);
32779 if (!flag_tm)
32780 error (keyword == RID_TRANSACTION_RELAXED
32781 ? G_("%<__transaction_relaxed%> without transactional memory "
32782 "support enabled")
32783 : G_("%<__transaction_atomic%> without transactional memory "
32784 "support enabled"));
32786 token = cp_parser_require_keyword (parser, keyword,
32787 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32788 : RT_TRANSACTION_RELAXED));
32789 gcc_assert (token != NULL);
32791 if (keyword == RID_TRANSACTION_RELAXED)
32792 this_in |= TM_STMT_ATTR_RELAXED;
32794 /* Set this early. This might mean that we allow transaction_cancel in
32795 an expression that we find out later actually has to be a constexpr.
32796 However, we expect that cxx_constant_value will be able to deal with
32797 this; also, if the noexcept has no constexpr, then what we parse next
32798 really is a transaction's body. */
32799 parser->in_transaction = this_in;
32801 /* Parse a noexcept specification. */
32802 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32803 true);
32805 if (!noex || !noex_expr
32806 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32808 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32810 expr = cp_parser_expression (parser);
32811 expr = finish_parenthesized_expr (expr);
32813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32815 else
32817 /* The only expression that is available got parsed for the noexcept
32818 already. noexcept is true then. */
32819 expr = noex;
32820 noex = boolean_true_node;
32823 expr = build_transaction_expr (token->location, expr, this_in, noex);
32824 parser->in_transaction = old_in;
32826 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32827 return error_mark_node;
32829 return (flag_tm ? expr : error_mark_node);
32832 /* Parse a function-transaction-block.
32834 function-transaction-block:
32835 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32836 function-body
32837 __transaction_atomic txn-attribute[opt] function-try-block
32838 __transaction_relaxed ctor-initializer[opt] function-body
32839 __transaction_relaxed function-try-block
32842 static bool
32843 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32845 unsigned char old_in = parser->in_transaction;
32846 unsigned char new_in = 1;
32847 tree compound_stmt, stmt, attrs;
32848 bool ctor_initializer_p;
32849 cp_token *token;
32851 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32852 || keyword == RID_TRANSACTION_RELAXED);
32853 token = cp_parser_require_keyword (parser, keyword,
32854 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32855 : RT_TRANSACTION_RELAXED));
32856 gcc_assert (token != NULL);
32858 if (keyword == RID_TRANSACTION_RELAXED)
32859 new_in |= TM_STMT_ATTR_RELAXED;
32860 else
32862 attrs = cp_parser_txn_attribute_opt (parser);
32863 if (attrs)
32864 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32867 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32869 parser->in_transaction = new_in;
32871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32872 ctor_initializer_p = cp_parser_function_try_block (parser);
32873 else
32874 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32875 (parser, /*in_function_try_block=*/false);
32877 parser->in_transaction = old_in;
32879 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32881 return ctor_initializer_p;
32884 /* Parse a __transaction_cancel statement.
32886 cancel-statement:
32887 __transaction_cancel txn-attribute[opt] ;
32888 __transaction_cancel txn-attribute[opt] throw-expression ;
32890 ??? Cancel and throw is not yet implemented. */
32892 static tree
32893 cp_parser_transaction_cancel (cp_parser *parser)
32895 cp_token *token;
32896 bool is_outer = false;
32897 tree stmt, attrs;
32899 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32900 RT_TRANSACTION_CANCEL);
32901 gcc_assert (token != NULL);
32903 attrs = cp_parser_txn_attribute_opt (parser);
32904 if (attrs)
32905 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32907 /* ??? Parse cancel-and-throw here. */
32909 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32911 if (!flag_tm)
32913 error_at (token->location, "%<__transaction_cancel%> without "
32914 "transactional memory support enabled");
32915 return error_mark_node;
32917 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32919 error_at (token->location, "%<__transaction_cancel%> within a "
32920 "%<__transaction_relaxed%>");
32921 return error_mark_node;
32923 else if (is_outer)
32925 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32926 && !is_tm_may_cancel_outer (current_function_decl))
32928 error_at (token->location, "outer %<__transaction_cancel%> not "
32929 "within outer %<__transaction_atomic%>");
32930 error_at (token->location,
32931 " or a %<transaction_may_cancel_outer%> function");
32932 return error_mark_node;
32935 else if (parser->in_transaction == 0)
32937 error_at (token->location, "%<__transaction_cancel%> not within "
32938 "%<__transaction_atomic%>");
32939 return error_mark_node;
32942 stmt = build_tm_abort_call (token->location, is_outer);
32943 add_stmt (stmt);
32945 return stmt;
32948 /* The parser. */
32950 static GTY (()) cp_parser *the_parser;
32953 /* Special handling for the first token or line in the file. The first
32954 thing in the file might be #pragma GCC pch_preprocess, which loads a
32955 PCH file, which is a GC collection point. So we need to handle this
32956 first pragma without benefit of an existing lexer structure.
32958 Always returns one token to the caller in *FIRST_TOKEN. This is
32959 either the true first token of the file, or the first token after
32960 the initial pragma. */
32962 static void
32963 cp_parser_initial_pragma (cp_token *first_token)
32965 tree name = NULL;
32967 cp_lexer_get_preprocessor_token (NULL, first_token);
32968 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32969 return;
32971 cp_lexer_get_preprocessor_token (NULL, first_token);
32972 if (first_token->type == CPP_STRING)
32974 name = first_token->u.value;
32976 cp_lexer_get_preprocessor_token (NULL, first_token);
32977 if (first_token->type != CPP_PRAGMA_EOL)
32978 error_at (first_token->location,
32979 "junk at end of %<#pragma GCC pch_preprocess%>");
32981 else
32982 error_at (first_token->location, "expected string literal");
32984 /* Skip to the end of the pragma. */
32985 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32986 cp_lexer_get_preprocessor_token (NULL, first_token);
32988 /* Now actually load the PCH file. */
32989 if (name)
32990 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32992 /* Read one more token to return to our caller. We have to do this
32993 after reading the PCH file in, since its pointers have to be
32994 live. */
32995 cp_lexer_get_preprocessor_token (NULL, first_token);
32998 /* Parses the grainsize pragma for the _Cilk_for statement.
32999 Syntax:
33000 #pragma cilk grainsize = <VALUE>. */
33002 static void
33003 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
33005 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
33007 tree exp = cp_parser_binary_expression (parser, false, false,
33008 PREC_NOT_OPERATOR, NULL);
33009 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33010 if (!exp || exp == error_mark_node)
33012 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
33013 return;
33016 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
33017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33018 cp_parser_cilk_for (parser, exp);
33019 else
33020 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
33021 "%<#pragma cilk grainsize%> is not followed by "
33022 "%<_Cilk_for%>");
33023 return;
33025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33028 /* Normal parsing of a pragma token. Here we can (and must) use the
33029 regular lexer. */
33031 static bool
33032 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33034 cp_token *pragma_tok;
33035 unsigned int id;
33037 pragma_tok = cp_lexer_consume_token (parser->lexer);
33038 gcc_assert (pragma_tok->type == CPP_PRAGMA);
33039 parser->lexer->in_pragma = true;
33041 id = pragma_tok->pragma_kind;
33042 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33043 cp_ensure_no_omp_declare_simd (parser);
33044 switch (id)
33046 case PRAGMA_GCC_PCH_PREPROCESS:
33047 error_at (pragma_tok->location,
33048 "%<#pragma GCC pch_preprocess%> must be first");
33049 break;
33051 case PRAGMA_OMP_BARRIER:
33052 switch (context)
33054 case pragma_compound:
33055 cp_parser_omp_barrier (parser, pragma_tok);
33056 return false;
33057 case pragma_stmt:
33058 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33059 "used in compound statements");
33060 break;
33061 default:
33062 goto bad_stmt;
33064 break;
33066 case PRAGMA_OMP_FLUSH:
33067 switch (context)
33069 case pragma_compound:
33070 cp_parser_omp_flush (parser, pragma_tok);
33071 return false;
33072 case pragma_stmt:
33073 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33074 "used in compound statements");
33075 break;
33076 default:
33077 goto bad_stmt;
33079 break;
33081 case PRAGMA_OMP_TASKWAIT:
33082 switch (context)
33084 case pragma_compound:
33085 cp_parser_omp_taskwait (parser, pragma_tok);
33086 return false;
33087 case pragma_stmt:
33088 error_at (pragma_tok->location,
33089 "%<#pragma omp taskwait%> may only be "
33090 "used in compound statements");
33091 break;
33092 default:
33093 goto bad_stmt;
33095 break;
33097 case PRAGMA_OMP_TASKYIELD:
33098 switch (context)
33100 case pragma_compound:
33101 cp_parser_omp_taskyield (parser, pragma_tok);
33102 return false;
33103 case pragma_stmt:
33104 error_at (pragma_tok->location,
33105 "%<#pragma omp taskyield%> may only be "
33106 "used in compound statements");
33107 break;
33108 default:
33109 goto bad_stmt;
33111 break;
33113 case PRAGMA_OMP_CANCEL:
33114 switch (context)
33116 case pragma_compound:
33117 cp_parser_omp_cancel (parser, pragma_tok);
33118 return false;
33119 case pragma_stmt:
33120 error_at (pragma_tok->location,
33121 "%<#pragma omp cancel%> may only be "
33122 "used in compound statements");
33123 break;
33124 default:
33125 goto bad_stmt;
33127 break;
33129 case PRAGMA_OMP_CANCELLATION_POINT:
33130 switch (context)
33132 case pragma_compound:
33133 cp_parser_omp_cancellation_point (parser, pragma_tok);
33134 return false;
33135 case pragma_stmt:
33136 error_at (pragma_tok->location,
33137 "%<#pragma omp cancellation point%> may only be "
33138 "used in compound statements");
33139 break;
33140 default:
33141 goto bad_stmt;
33143 break;
33145 case PRAGMA_OMP_THREADPRIVATE:
33146 cp_parser_omp_threadprivate (parser, pragma_tok);
33147 return false;
33149 case PRAGMA_OMP_DECLARE_REDUCTION:
33150 cp_parser_omp_declare (parser, pragma_tok, context);
33151 return false;
33153 case PRAGMA_OACC_CACHE:
33154 case PRAGMA_OACC_DATA:
33155 case PRAGMA_OACC_ENTER_DATA:
33156 case PRAGMA_OACC_EXIT_DATA:
33157 case PRAGMA_OACC_KERNELS:
33158 case PRAGMA_OACC_PARALLEL:
33159 case PRAGMA_OACC_LOOP:
33160 case PRAGMA_OACC_UPDATE:
33161 case PRAGMA_OACC_WAIT:
33162 case PRAGMA_OMP_ATOMIC:
33163 case PRAGMA_OMP_CRITICAL:
33164 case PRAGMA_OMP_DISTRIBUTE:
33165 case PRAGMA_OMP_FOR:
33166 case PRAGMA_OMP_MASTER:
33167 case PRAGMA_OMP_ORDERED:
33168 case PRAGMA_OMP_PARALLEL:
33169 case PRAGMA_OMP_SECTIONS:
33170 case PRAGMA_OMP_SIMD:
33171 case PRAGMA_OMP_SINGLE:
33172 case PRAGMA_OMP_TASK:
33173 case PRAGMA_OMP_TASKGROUP:
33174 case PRAGMA_OMP_TEAMS:
33175 if (context != pragma_stmt && context != pragma_compound)
33176 goto bad_stmt;
33177 cp_parser_omp_construct (parser, pragma_tok);
33178 return true;
33180 case PRAGMA_OMP_TARGET:
33181 return cp_parser_omp_target (parser, pragma_tok, context);
33183 case PRAGMA_OMP_END_DECLARE_TARGET:
33184 cp_parser_omp_end_declare_target (parser, pragma_tok);
33185 return false;
33187 case PRAGMA_OMP_SECTION:
33188 error_at (pragma_tok->location,
33189 "%<#pragma omp section%> may only be used in "
33190 "%<#pragma omp sections%> construct");
33191 break;
33193 case PRAGMA_IVDEP:
33195 if (context == pragma_external)
33197 error_at (pragma_tok->location,
33198 "%<#pragma GCC ivdep%> must be inside a function");
33199 break;
33201 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33202 cp_token *tok;
33203 tok = cp_lexer_peek_token (the_parser->lexer);
33204 if (tok->type != CPP_KEYWORD
33205 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33206 && tok->keyword != RID_DO))
33208 cp_parser_error (parser, "for, while or do statement expected");
33209 return false;
33211 cp_parser_iteration_statement (parser, true);
33212 return true;
33215 case PRAGMA_CILK_SIMD:
33216 if (context == pragma_external)
33218 error_at (pragma_tok->location,
33219 "%<#pragma simd%> must be inside a function");
33220 break;
33222 cp_parser_cilk_simd (parser, pragma_tok);
33223 return true;
33225 case PRAGMA_CILK_GRAINSIZE:
33226 if (context == pragma_external)
33228 error_at (pragma_tok->location,
33229 "%<#pragma cilk grainsize%> must be inside a function");
33230 break;
33233 /* Ignore the pragma if Cilk Plus is not enabled. */
33234 if (flag_cilkplus)
33236 cp_parser_cilk_grainsize (parser, pragma_tok);
33237 return true;
33239 else
33241 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33242 "%<#pragma cilk grainsize%>");
33243 break;
33246 default:
33247 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33248 c_invoke_pragma_handler (id);
33249 break;
33251 bad_stmt:
33252 cp_parser_error (parser, "expected declaration specifiers");
33253 break;
33256 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33257 return false;
33260 /* The interface the pragma parsers have to the lexer. */
33262 enum cpp_ttype
33263 pragma_lex (tree *value)
33265 cp_token *tok;
33266 enum cpp_ttype ret;
33268 tok = cp_lexer_peek_token (the_parser->lexer);
33270 ret = tok->type;
33271 *value = tok->u.value;
33273 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33274 ret = CPP_EOF;
33275 else if (ret == CPP_STRING)
33276 *value = cp_parser_string_literal (the_parser, false, false);
33277 else
33279 cp_lexer_consume_token (the_parser->lexer);
33280 if (ret == CPP_KEYWORD)
33281 ret = CPP_NAME;
33284 return ret;
33288 /* External interface. */
33290 /* Parse one entire translation unit. */
33292 void
33293 c_parse_file (void)
33295 static bool already_called = false;
33297 if (already_called)
33298 fatal_error (input_location,
33299 "inter-module optimizations not implemented for C++");
33300 already_called = true;
33302 the_parser = cp_parser_new ();
33303 push_deferring_access_checks (flag_access_control
33304 ? dk_no_deferred : dk_no_check);
33305 cp_parser_translation_unit (the_parser);
33306 the_parser = NULL;
33309 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33310 vectorlength clause:
33311 Syntax:
33312 vectorlength ( constant-expression ) */
33314 static tree
33315 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33316 bool is_simd_fn)
33318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33319 tree expr;
33320 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33321 safelen clause. Thus, vectorlength is represented as OMP 4.0
33322 safelen. For SIMD-enabled function it is represented by OMP 4.0
33323 simdlen. */
33324 if (!is_simd_fn)
33325 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33326 loc);
33327 else
33328 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33329 loc);
33331 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33332 return error_mark_node;
33334 expr = cp_parser_constant_expression (parser);
33335 expr = maybe_constant_value (expr);
33337 /* If expr == error_mark_node, then don't emit any errors nor
33338 create a clause. if any of the above functions returns
33339 error mark node then they would have emitted an error message. */
33340 if (expr == error_mark_node)
33342 else if (!TREE_TYPE (expr)
33343 || !TREE_CONSTANT (expr)
33344 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33345 error_at (loc, "vectorlength must be an integer constant");
33346 else if (TREE_CONSTANT (expr)
33347 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33348 error_at (loc, "vectorlength must be a power of 2");
33349 else
33351 tree c;
33352 if (!is_simd_fn)
33354 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33355 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33356 OMP_CLAUSE_CHAIN (c) = clauses;
33357 clauses = c;
33359 else
33361 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33362 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33363 OMP_CLAUSE_CHAIN (c) = clauses;
33364 clauses = c;
33368 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33369 return error_mark_node;
33370 return clauses;
33373 /* Handles the Cilk Plus #pragma simd linear clause.
33374 Syntax:
33375 linear ( simd-linear-variable-list )
33377 simd-linear-variable-list:
33378 simd-linear-variable
33379 simd-linear-variable-list , simd-linear-variable
33381 simd-linear-variable:
33382 id-expression
33383 id-expression : simd-linear-step
33385 simd-linear-step:
33386 conditional-expression */
33388 static tree
33389 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33391 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33394 return clauses;
33395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33397 cp_parser_error (parser, "expected identifier");
33398 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33399 return error_mark_node;
33402 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33403 parser->colon_corrects_to_scope_p = false;
33404 while (1)
33406 cp_token *token = cp_lexer_peek_token (parser->lexer);
33407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33409 cp_parser_error (parser, "expected variable-name");
33410 clauses = error_mark_node;
33411 break;
33414 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33415 false, false);
33416 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33417 token->location);
33418 if (decl == error_mark_node)
33420 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33421 token->location);
33422 clauses = error_mark_node;
33424 else
33426 tree e = NULL_TREE;
33427 tree step_size = integer_one_node;
33429 /* If present, parse the linear step. Otherwise, assume the default
33430 value of 1. */
33431 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33433 cp_lexer_consume_token (parser->lexer);
33435 e = cp_parser_assignment_expression (parser);
33436 e = maybe_constant_value (e);
33438 if (e == error_mark_node)
33440 /* If an error has occurred, then the whole pragma is
33441 considered ill-formed. Thus, no reason to keep
33442 parsing. */
33443 clauses = error_mark_node;
33444 break;
33446 else if (type_dependent_expression_p (e)
33447 || value_dependent_expression_p (e)
33448 || (TREE_TYPE (e)
33449 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33450 && (TREE_CONSTANT (e)
33451 || DECL_P (e))))
33452 step_size = e;
33453 else
33454 cp_parser_error (parser,
33455 "step size must be an integer constant "
33456 "expression or an integer variable");
33459 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33460 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33461 OMP_CLAUSE_DECL (l) = decl;
33462 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33463 OMP_CLAUSE_CHAIN (l) = clauses;
33464 clauses = l;
33466 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33467 cp_lexer_consume_token (parser->lexer);
33468 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33469 break;
33470 else
33472 error_at (cp_lexer_peek_token (parser->lexer)->location,
33473 "expected %<,%> or %<)%> after %qE", decl);
33474 clauses = error_mark_node;
33475 break;
33478 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33479 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33480 return clauses;
33483 /* Returns the name of the next clause. If the clause is not
33484 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33485 token is not consumed. Otherwise, the appropriate enum from the
33486 pragma_simd_clause is returned and the token is consumed. */
33488 static pragma_omp_clause
33489 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33491 pragma_omp_clause clause_type;
33492 cp_token *token = cp_lexer_peek_token (parser->lexer);
33494 if (token->keyword == RID_PRIVATE)
33495 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33496 else if (!token->u.value || token->type != CPP_NAME)
33497 return PRAGMA_CILK_CLAUSE_NONE;
33498 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33499 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33500 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33501 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33502 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33503 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33504 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33505 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33506 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33507 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33508 else
33509 return PRAGMA_CILK_CLAUSE_NONE;
33511 cp_lexer_consume_token (parser->lexer);
33512 return clause_type;
33515 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33517 static tree
33518 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33520 tree clauses = NULL_TREE;
33522 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33523 && clauses != error_mark_node)
33525 pragma_omp_clause c_kind;
33526 c_kind = cp_parser_cilk_simd_clause_name (parser);
33527 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33528 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33529 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33530 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33531 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33532 /* Use the OpenMP 4.0 equivalent function. */
33533 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33534 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33535 /* Use the OpenMP 4.0 equivalent function. */
33536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33537 clauses);
33538 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33539 /* Use the OMP 4.0 equivalent function. */
33540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33541 clauses);
33542 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33543 /* Use the OMP 4.0 equivalent function. */
33544 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33545 else
33547 clauses = error_mark_node;
33548 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33549 break;
33553 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33555 if (clauses == error_mark_node)
33556 return error_mark_node;
33557 else
33558 return c_finish_cilk_clauses (clauses);
33561 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33563 static void
33564 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33566 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33568 if (clauses == error_mark_node)
33569 return;
33571 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33573 error_at (cp_lexer_peek_token (parser->lexer)->location,
33574 "for statement expected");
33575 return;
33578 tree sb = begin_omp_structured_block ();
33579 int save = cp_parser_begin_omp_structured_block (parser);
33580 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33581 if (ret)
33582 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33583 cp_parser_end_omp_structured_block (parser, save);
33584 add_stmt (finish_omp_structured_block (sb));
33587 /* Main entry-point for parsing Cilk Plus _Cilk_for
33588 loops. The return value is error_mark_node
33589 when errors happen and CILK_FOR tree on success. */
33591 static tree
33592 cp_parser_cilk_for (cp_parser *parser, tree grain)
33594 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33595 gcc_unreachable ();
33597 tree sb = begin_omp_structured_block ();
33598 int save = cp_parser_begin_omp_structured_block (parser);
33600 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33601 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33602 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33603 clauses = finish_omp_clauses (clauses);
33605 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33606 if (ret)
33607 cpp_validate_cilk_plus_loop (ret);
33608 else
33609 ret = error_mark_node;
33611 cp_parser_end_omp_structured_block (parser, save);
33612 add_stmt (finish_omp_structured_block (sb));
33613 return ret;
33616 /* Create an identifier for a generic parameter type (a synthesized
33617 template parameter implied by `auto' or a concept identifier). */
33619 static GTY(()) int generic_parm_count;
33620 static tree
33621 make_generic_type_name ()
33623 char buf[32];
33624 sprintf (buf, "auto:%d", ++generic_parm_count);
33625 return get_identifier (buf);
33628 /* Predicate that behaves as is_auto_or_concept but matches the parent
33629 node of the generic type rather than the generic type itself. This
33630 allows for type transformation in add_implicit_template_parms. */
33632 static inline bool
33633 tree_type_is_auto_or_concept (const_tree t)
33635 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33638 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33639 (creating a new template parameter list if necessary). Returns the newly
33640 created template type parm. */
33642 tree
33643 synthesize_implicit_template_parm (cp_parser *parser)
33645 gcc_assert (current_binding_level->kind == sk_function_parms);
33647 /* We are either continuing a function template that already contains implicit
33648 template parameters, creating a new fully-implicit function template, or
33649 extending an existing explicit function template with implicit template
33650 parameters. */
33652 cp_binding_level *const entry_scope = current_binding_level;
33654 bool become_template = false;
33655 cp_binding_level *parent_scope = 0;
33657 if (parser->implicit_template_scope)
33659 gcc_assert (parser->implicit_template_parms);
33661 current_binding_level = parser->implicit_template_scope;
33663 else
33665 /* Roll back to the existing template parameter scope (in the case of
33666 extending an explicit function template) or introduce a new template
33667 parameter scope ahead of the function parameter scope (or class scope
33668 in the case of out-of-line member definitions). The function scope is
33669 added back after template parameter synthesis below. */
33671 cp_binding_level *scope = entry_scope;
33673 while (scope->kind == sk_function_parms)
33675 parent_scope = scope;
33676 scope = scope->level_chain;
33678 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33680 /* If not defining a class, then any class scope is a scope level in
33681 an out-of-line member definition. In this case simply wind back
33682 beyond the first such scope to inject the template parameter list.
33683 Otherwise wind back to the class being defined. The latter can
33684 occur in class member friend declarations such as:
33686 class A {
33687 void foo (auto);
33689 class B {
33690 friend void A::foo (auto);
33693 The template parameter list synthesized for the friend declaration
33694 must be injected in the scope of 'B'. This can also occur in
33695 erroneous cases such as:
33697 struct A {
33698 struct B {
33699 void foo (auto);
33701 void B::foo (auto) {}
33704 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33705 but, nevertheless, the template parameter list synthesized for the
33706 declarator should be injected into the scope of 'A' as if the
33707 ill-formed template was specified explicitly. */
33709 while (scope->kind == sk_class && !scope->defining_class_p)
33711 parent_scope = scope;
33712 scope = scope->level_chain;
33716 current_binding_level = scope;
33718 if (scope->kind != sk_template_parms
33719 || !function_being_declared_is_template_p (parser))
33721 /* Introduce a new template parameter list for implicit template
33722 parameters. */
33724 become_template = true;
33726 parser->implicit_template_scope
33727 = begin_scope (sk_template_parms, NULL);
33729 ++processing_template_decl;
33731 parser->fully_implicit_function_template_p = true;
33732 ++parser->num_template_parameter_lists;
33734 else
33736 /* Synthesize implicit template parameters at the end of the explicit
33737 template parameter list. */
33739 gcc_assert (current_template_parms);
33741 parser->implicit_template_scope = scope;
33743 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33744 parser->implicit_template_parms
33745 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33749 /* Synthesize a new template parameter and track the current template
33750 parameter chain with implicit_template_parms. */
33752 tree synth_id = make_generic_type_name ();
33753 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33754 synth_id);
33755 tree new_parm
33756 = process_template_parm (parser->implicit_template_parms,
33757 input_location,
33758 build_tree_list (NULL_TREE, synth_tmpl_parm),
33759 /*non_type=*/false,
33760 /*param_pack=*/false);
33763 if (parser->implicit_template_parms)
33764 parser->implicit_template_parms
33765 = TREE_CHAIN (parser->implicit_template_parms);
33766 else
33767 parser->implicit_template_parms = new_parm;
33769 tree new_type = TREE_TYPE (getdecls ());
33771 /* If creating a fully implicit function template, start the new implicit
33772 template parameter list with this synthesized type, otherwise grow the
33773 current template parameter list. */
33775 if (become_template)
33777 parent_scope->level_chain = current_binding_level;
33779 tree new_parms = make_tree_vec (1);
33780 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33781 current_template_parms = tree_cons (size_int (processing_template_decl),
33782 new_parms, current_template_parms);
33784 else
33786 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33787 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33788 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33789 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33792 current_binding_level = entry_scope;
33794 return new_type;
33797 /* Finish the declaration of a fully implicit function template. Such a
33798 template has no explicit template parameter list so has not been through the
33799 normal template head and tail processing. synthesize_implicit_template_parm
33800 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33801 provided if the declaration is a class member such that its template
33802 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33803 form is returned. Otherwise NULL_TREE is returned. */
33805 tree
33806 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33808 gcc_assert (parser->fully_implicit_function_template_p);
33810 if (member_decl_opt && member_decl_opt != error_mark_node
33811 && DECL_VIRTUAL_P (member_decl_opt))
33813 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33814 "implicit templates may not be %<virtual%>");
33815 DECL_VIRTUAL_P (member_decl_opt) = false;
33818 if (member_decl_opt)
33819 member_decl_opt = finish_member_template_decl (member_decl_opt);
33820 end_template_decl ();
33822 parser->fully_implicit_function_template_p = false;
33823 --parser->num_template_parameter_lists;
33825 return member_decl_opt;
33828 #include "gt-cp-parser.h"