Replace enum omp_clause_map_kind with enum gomp_map_kind.
[official-gcc.git] / gcc / cp / parser.c
blobeaa6eeea81de0d3cffcd9a14f6b1e68e236c3eb5
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 "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "trans-mem.h"
41 #include "cp-tree.h"
42 #include "intl.h"
43 #include "c-family/c-pragma.h"
44 #include "decl.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "target.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "tree-pretty-print.h"
60 #include "parser.h"
61 #include "type-utils.h"
62 #include "omp-low.h"
63 #include "gomp-constants.h"
66 /* The lexer. */
68 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
69 and c-lex.c) and the C++ parser. */
71 static cp_token eof_token =
73 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
76 /* The various kinds of non integral constant we encounter. */
77 typedef enum non_integral_constant {
78 NIC_NONE,
79 /* floating-point literal */
80 NIC_FLOAT,
81 /* %<this%> */
82 NIC_THIS,
83 /* %<__FUNCTION__%> */
84 NIC_FUNC_NAME,
85 /* %<__PRETTY_FUNCTION__%> */
86 NIC_PRETTY_FUNC,
87 /* %<__func__%> */
88 NIC_C99_FUNC,
89 /* "%<va_arg%> */
90 NIC_VA_ARG,
91 /* a cast */
92 NIC_CAST,
93 /* %<typeid%> operator */
94 NIC_TYPEID,
95 /* non-constant compound literals */
96 NIC_NCC,
97 /* a function call */
98 NIC_FUNC_CALL,
99 /* an increment */
100 NIC_INC,
101 /* an decrement */
102 NIC_DEC,
103 /* an array reference */
104 NIC_ARRAY_REF,
105 /* %<->%> */
106 NIC_ARROW,
107 /* %<.%> */
108 NIC_POINT,
109 /* the address of a label */
110 NIC_ADDR_LABEL,
111 /* %<*%> */
112 NIC_STAR,
113 /* %<&%> */
114 NIC_ADDR,
115 /* %<++%> */
116 NIC_PREINCREMENT,
117 /* %<--%> */
118 NIC_PREDECREMENT,
119 /* %<new%> */
120 NIC_NEW,
121 /* %<delete%> */
122 NIC_DEL,
123 /* calls to overloaded operators */
124 NIC_OVERLOADED,
125 /* an assignment */
126 NIC_ASSIGNMENT,
127 /* a comma operator */
128 NIC_COMMA,
129 /* a call to a constructor */
130 NIC_CONSTRUCTOR,
131 /* a transaction expression */
132 NIC_TRANSACTION
133 } non_integral_constant;
135 /* The various kinds of errors about name-lookup failing. */
136 typedef enum name_lookup_error {
137 /* NULL */
138 NLE_NULL,
139 /* is not a type */
140 NLE_TYPE,
141 /* is not a class or namespace */
142 NLE_CXX98,
143 /* is not a class, namespace, or enumeration */
144 NLE_NOT_CXX98
145 } name_lookup_error;
147 /* The various kinds of required token */
148 typedef enum required_token {
149 RT_NONE,
150 RT_SEMICOLON, /* ';' */
151 RT_OPEN_PAREN, /* '(' */
152 RT_CLOSE_BRACE, /* '}' */
153 RT_OPEN_BRACE, /* '{' */
154 RT_CLOSE_SQUARE, /* ']' */
155 RT_OPEN_SQUARE, /* '[' */
156 RT_COMMA, /* ',' */
157 RT_SCOPE, /* '::' */
158 RT_LESS, /* '<' */
159 RT_GREATER, /* '>' */
160 RT_EQ, /* '=' */
161 RT_ELLIPSIS, /* '...' */
162 RT_MULT, /* '*' */
163 RT_COMPL, /* '~' */
164 RT_COLON, /* ':' */
165 RT_COLON_SCOPE, /* ':' or '::' */
166 RT_CLOSE_PAREN, /* ')' */
167 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
168 RT_PRAGMA_EOL, /* end of line */
169 RT_NAME, /* identifier */
171 /* The type is CPP_KEYWORD */
172 RT_NEW, /* new */
173 RT_DELETE, /* delete */
174 RT_RETURN, /* return */
175 RT_WHILE, /* while */
176 RT_EXTERN, /* extern */
177 RT_STATIC_ASSERT, /* static_assert */
178 RT_DECLTYPE, /* decltype */
179 RT_OPERATOR, /* operator */
180 RT_CLASS, /* class */
181 RT_TEMPLATE, /* template */
182 RT_NAMESPACE, /* namespace */
183 RT_USING, /* using */
184 RT_ASM, /* asm */
185 RT_TRY, /* try */
186 RT_CATCH, /* catch */
187 RT_THROW, /* throw */
188 RT_LABEL, /* __label__ */
189 RT_AT_TRY, /* @try */
190 RT_AT_SYNCHRONIZED, /* @synchronized */
191 RT_AT_THROW, /* @throw */
193 RT_SELECT, /* selection-statement */
194 RT_INTERATION, /* iteration-statement */
195 RT_JUMP, /* jump-statement */
196 RT_CLASS_KEY, /* class-key */
197 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
198 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
199 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
200 RT_TRANSACTION_CANCEL /* __transaction_cancel */
201 } required_token;
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
251 static void cp_parser_initial_pragma
252 (cp_token *);
254 static tree cp_literal_operator_id
255 (const char *);
257 static void cp_parser_cilk_simd
258 (cp_parser *, cp_token *);
259 static tree cp_parser_cilk_for
260 (cp_parser *, tree);
261 static bool cp_parser_omp_declare_reduction_exprs
262 (tree, cp_parser *);
263 static tree cp_parser_cilk_simd_vectorlength
264 (cp_parser *, tree, bool);
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
270 /* Variables. */
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream;
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand;
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287 cp_token *start_token, unsigned num,
288 cp_token *curr_token)
290 unsigned i, nprinted;
291 cp_token *token;
292 bool do_print;
294 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
296 if (buffer == NULL)
297 return;
299 if (num == 0)
300 num = buffer->length ();
302 if (start_token == NULL)
303 start_token = buffer->address ();
305 if (start_token > buffer->address ())
307 cp_lexer_print_token (file, &(*buffer)[0]);
308 fprintf (file, " ... ");
311 do_print = false;
312 nprinted = 0;
313 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
315 if (token == start_token)
316 do_print = true;
318 if (!do_print)
319 continue;
321 nprinted++;
322 if (token == curr_token)
323 fprintf (file, "[[");
325 cp_lexer_print_token (file, token);
327 if (token == curr_token)
328 fprintf (file, "]]");
330 switch (token->type)
332 case CPP_SEMICOLON:
333 case CPP_OPEN_BRACE:
334 case CPP_CLOSE_BRACE:
335 case CPP_EOF:
336 fputc ('\n', file);
337 break;
339 default:
340 fputc (' ', file);
344 if (i == num && i < buffer->length ())
346 fprintf (file, " ... ");
347 cp_lexer_print_token (file, &buffer->last ());
350 fprintf (file, "\n");
354 /* Dump all tokens in BUFFER to stderr. */
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
359 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
365 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
371 if (ptr)
372 debug (*ptr);
373 else
374 fprintf (stderr, "<nil>\n");
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
384 if (t)
386 fprintf (file, "%s: ", desc);
387 print_node_brief (file, "", t, 0);
392 /* Dump parser context C to FILE. */
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
397 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399 print_node_brief (file, "", c->object_type, 0);
400 fprintf (file, "}\n");
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
409 unsigned i;
410 cp_parser_context *c;
412 fprintf (file, "Parsing context stack:\n");
413 for (i = 0, c = first; c; c = c->next, i++)
415 fprintf (file, "\t#%u: ", i);
416 cp_debug_print_context (file, c);
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
426 if (flag)
427 fprintf (file, "%s: true\n", desc);
431 /* Print an unparsed function entry UF to FILE. */
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
436 unsigned i;
437 cp_default_arg_entry *default_arg_fn;
438 tree fn;
440 fprintf (file, "\tFunctions with default args:\n");
441 for (i = 0;
442 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443 i++)
445 fprintf (file, "\t\tClass type: ");
446 print_node_brief (file, "", default_arg_fn->class_type, 0);
447 fprintf (file, "\t\tDeclaration: ");
448 print_node_brief (file, "", default_arg_fn->decl, 0);
449 fprintf (file, "\n");
452 fprintf (file, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
456 print_node_brief (file, "", fn, 0);
457 fprintf (file, " ");
459 fprintf (file, "\n");
461 fprintf (file, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
468 fprintf (file, "\n");
472 /* Print the stack of unparsed member functions S to FILE. */
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476 vec<cp_unparsed_functions_entry, va_gc> *s)
478 unsigned i;
479 cp_unparsed_functions_entry *uf;
481 fprintf (file, "Unparsed functions\n");
482 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
484 fprintf (file, "#%u:\n", i);
485 cp_debug_print_unparsed_function (file, uf);
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
496 cp_token *next_token, *first_token, *start_token;
498 if (file == NULL)
499 file = stderr;
501 next_token = parser->lexer->next_token;
502 first_token = parser->lexer->buffer->address ();
503 start_token = (next_token > first_token + window_size / 2)
504 ? next_token - window_size / 2
505 : first_token;
506 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507 next_token);
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
517 const size_t window_size = 20;
518 cp_token *token;
519 expanded_location eloc;
521 if (file == NULL)
522 file = stderr;
524 fprintf (file, "Parser state\n\n");
525 fprintf (file, "Number of tokens: %u\n",
526 vec_safe_length (parser->lexer->buffer));
527 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528 cp_debug_print_tree_if_set (file, "Object scope",
529 parser->object_scope);
530 cp_debug_print_tree_if_set (file, "Qualifying scope",
531 parser->qualifying_scope);
532 cp_debug_print_context_stack (file, parser->context);
533 cp_debug_print_flag (file, "Allow GNU extensions",
534 parser->allow_gnu_extensions_p);
535 cp_debug_print_flag (file, "'>' token is greater-than",
536 parser->greater_than_is_operator_p);
537 cp_debug_print_flag (file, "Default args allowed in current "
538 "parameter list", parser->default_arg_ok_p);
539 cp_debug_print_flag (file, "Parsing integral constant-expression",
540 parser->integral_constant_expression_p);
541 cp_debug_print_flag (file, "Allow non-constant expression in current "
542 "constant-expression",
543 parser->allow_non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Seen non-constant expression",
545 parser->non_integral_constant_expression_p);
546 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547 "current context",
548 parser->local_variables_forbidden_p);
549 cp_debug_print_flag (file, "In unbraced linkage specification",
550 parser->in_unbraced_linkage_specification_p);
551 cp_debug_print_flag (file, "Parsing a declarator",
552 parser->in_declarator_p);
553 cp_debug_print_flag (file, "In template argument list",
554 parser->in_template_argument_list_p);
555 cp_debug_print_flag (file, "Parsing an iteration statement",
556 parser->in_statement & IN_ITERATION_STMT);
557 cp_debug_print_flag (file, "Parsing a switch statement",
558 parser->in_statement & IN_SWITCH_STMT);
559 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560 parser->in_statement & IN_OMP_BLOCK);
561 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562 parser->in_statement & IN_CILK_SIMD_FOR);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570 parser->implicit_extern_c);
571 cp_debug_print_flag (file, "String expressions should be translated "
572 "to execution character set",
573 parser->translate_strings_p);
574 cp_debug_print_flag (file, "Parsing function body outside of a "
575 "local class", parser->in_function_body);
576 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577 parser->colon_corrects_to_scope_p);
578 cp_debug_print_flag (file, "Colon doesn't start a class definition",
579 parser->colon_doesnt_start_class_def_p);
580 if (parser->type_definition_forbidden_message)
581 fprintf (file, "Error message for forbidden type definitions: %s\n",
582 parser->type_definition_forbidden_message);
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
602 cp_debug_parser (stderr, &ref);
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
616 static cp_lexer *
617 cp_lexer_alloc (void)
619 cp_lexer *lexer;
621 c_common_no_more_pch ();
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
634 return lexer;
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
641 static cp_lexer *
642 cp_lexer_new_main (void)
644 cp_lexer *lexer;
645 cp_token token;
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
652 lexer = cp_lexer_alloc ();
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
703 /* Frees all resources associated with LEXER. */
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
713 /* Returns nonzero if debugging information should be output. */
715 static inline bool
716 cp_lexer_debugging_p (cp_lexer *lexer)
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 return cp_lexer_token_at (lexer, tp);
759 /* nonzero if we are presently saving tokens. */
761 static inline int
762 cp_lexer_saving_tokens (const cp_lexer* lexer)
764 return lexer->saved_tokens.length () != 0;
767 /* Store the next token from the preprocessor in *TOKEN. Return true
768 if we reach EOF. If LEXER is NULL, assume we are handling an
769 initial #pragma pch_preprocess, and thus want the lexer to return
770 processed strings. */
772 static void
773 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
775 static int is_extern_c = 0;
777 /* Get a new token from the preprocessor. */
778 token->type
779 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
780 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
781 token->keyword = RID_MAX;
782 token->pragma_kind = PRAGMA_NONE;
783 token->purged_p = false;
784 token->error_reported = false;
786 /* On some systems, some header files are surrounded by an
787 implicit extern "C" block. Set a flag in the token if it
788 comes from such a header. */
789 is_extern_c += pending_lang_change;
790 pending_lang_change = 0;
791 token->implicit_extern_c = is_extern_c > 0;
793 /* Check to see if this token is a keyword. */
794 if (token->type == CPP_NAME)
796 if (C_IS_RESERVED_WORD (token->u.value))
798 /* Mark this token as a keyword. */
799 token->type = CPP_KEYWORD;
800 /* Record which keyword. */
801 token->keyword = C_RID_CODE (token->u.value);
803 else
805 if (warn_cxx0x_compat
806 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
807 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
809 /* Warn about the C++0x keyword (but still treat it as
810 an identifier). */
811 warning (OPT_Wc__0x_compat,
812 "identifier %qE is a keyword in C++11",
813 token->u.value);
815 /* Clear out the C_RID_CODE so we don't warn about this
816 particular identifier-turned-keyword again. */
817 C_SET_RID_CODE (token->u.value, RID_MAX);
820 token->keyword = RID_MAX;
823 else if (token->type == CPP_AT_NAME)
825 /* This only happens in Objective-C++; it must be a keyword. */
826 token->type = CPP_KEYWORD;
827 switch (C_RID_CODE (token->u.value))
829 /* Replace 'class' with '@class', 'private' with '@private',
830 etc. This prevents confusion with the C++ keyword
831 'class', and makes the tokens consistent with other
832 Objective-C 'AT' keywords. For example '@class' is
833 reported as RID_AT_CLASS which is consistent with
834 '@synchronized', which is reported as
835 RID_AT_SYNCHRONIZED.
837 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
838 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
839 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
840 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
841 case RID_THROW: token->keyword = RID_AT_THROW; break;
842 case RID_TRY: token->keyword = RID_AT_TRY; break;
843 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
844 default: token->keyword = C_RID_CODE (token->u.value);
847 else if (token->type == CPP_PRAGMA)
849 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
850 token->pragma_kind = ((enum pragma_kind)
851 TREE_INT_CST_LOW (token->u.value));
852 token->u.value = NULL_TREE;
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if the next token is a keyword for a decl-specifier. */
935 static bool
936 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
938 cp_token *token;
940 token = cp_lexer_peek_token (lexer);
941 switch (token->keyword)
943 /* auto specifier: storage-class-specifier in C++,
944 simple-type-specifier in C++0x. */
945 case RID_AUTO:
946 /* Storage classes. */
947 case RID_REGISTER:
948 case RID_STATIC:
949 case RID_EXTERN:
950 case RID_MUTABLE:
951 case RID_THREAD:
952 /* Elaborated type specifiers. */
953 case RID_ENUM:
954 case RID_CLASS:
955 case RID_STRUCT:
956 case RID_UNION:
957 case RID_TYPENAME:
958 /* Simple type specifiers. */
959 case RID_CHAR:
960 case RID_CHAR16:
961 case RID_CHAR32:
962 case RID_WCHAR:
963 case RID_BOOL:
964 case RID_SHORT:
965 case RID_INT:
966 case RID_LONG:
967 case RID_SIGNED:
968 case RID_UNSIGNED:
969 case RID_FLOAT:
970 case RID_DOUBLE:
971 case RID_VOID:
972 /* GNU extensions. */
973 case RID_ATTRIBUTE:
974 case RID_TYPEOF:
975 /* C++0x extensions. */
976 case RID_DECLTYPE:
977 case RID_UNDERLYING_TYPE:
978 return true;
980 default:
981 if (token->keyword >= RID_FIRST_INT_N
982 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
983 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
984 return true;
985 return false;
989 /* Returns TRUE iff the token T begins a decltype type. */
991 static bool
992 token_is_decltype (cp_token *t)
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
998 /* Returns TRUE iff the next token begins a decltype type. */
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1007 /* Return a pointer to the Nth token in the token stream. If N is 1,
1008 then this is precisely equivalent to cp_lexer_peek_token (except
1009 that it is not inline). One would like to disallow that case, but
1010 there is one case (cp_parser_nth_token_starts_template_id) where
1011 the caller passes a variable for N and it might be 1. */
1013 static cp_token *
1014 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1016 cp_token *token;
1018 /* N is 1-based, not zero-based. */
1019 gcc_assert (n > 0);
1021 if (cp_lexer_debugging_p (lexer))
1022 fprintf (cp_lexer_debug_stream,
1023 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1025 --n;
1026 token = lexer->next_token;
1027 gcc_assert (!n || token != &eof_token);
1028 while (n != 0)
1030 ++token;
1031 if (token == lexer->last_token)
1033 token = &eof_token;
1034 break;
1037 if (!token->purged_p)
1038 --n;
1041 if (cp_lexer_debugging_p (lexer))
1043 cp_lexer_print_token (cp_lexer_debug_stream, token);
1044 putc ('\n', cp_lexer_debug_stream);
1047 return token;
1050 /* Return the next token, and advance the lexer's next_token pointer
1051 to point to the next non-purged token. */
1053 static cp_token *
1054 cp_lexer_consume_token (cp_lexer* lexer)
1056 cp_token *token = lexer->next_token;
1058 gcc_assert (token != &eof_token);
1059 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1063 lexer->next_token++;
1064 if (lexer->next_token == lexer->last_token)
1066 lexer->next_token = &eof_token;
1067 break;
1071 while (lexer->next_token->purged_p);
1073 cp_lexer_set_source_position_from_token (token);
1075 /* Provide debugging output. */
1076 if (cp_lexer_debugging_p (lexer))
1078 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1079 cp_lexer_print_token (cp_lexer_debug_stream, token);
1080 putc ('\n', cp_lexer_debug_stream);
1083 return token;
1086 /* Permanently remove the next token from the token stream, and
1087 advance the next_token pointer to refer to the next non-purged
1088 token. */
1090 static void
1091 cp_lexer_purge_token (cp_lexer *lexer)
1093 cp_token *tok = lexer->next_token;
1095 gcc_assert (tok != &eof_token);
1096 tok->purged_p = true;
1097 tok->location = UNKNOWN_LOCATION;
1098 tok->u.value = NULL_TREE;
1099 tok->keyword = RID_MAX;
1103 tok++;
1104 if (tok == lexer->last_token)
1106 tok = &eof_token;
1107 break;
1110 while (tok->purged_p);
1111 lexer->next_token = tok;
1114 /* Permanently remove all tokens after TOK, up to, but not
1115 including, the token that will be returned next by
1116 cp_lexer_peek_token. */
1118 static void
1119 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1121 cp_token *peek = lexer->next_token;
1123 if (peek == &eof_token)
1124 peek = lexer->last_token;
1126 gcc_assert (tok < peek);
1128 for ( tok += 1; tok != peek; tok += 1)
1130 tok->purged_p = true;
1131 tok->location = UNKNOWN_LOCATION;
1132 tok->u.value = NULL_TREE;
1133 tok->keyword = RID_MAX;
1137 /* Begin saving tokens. All tokens consumed after this point will be
1138 preserved. */
1140 static void
1141 cp_lexer_save_tokens (cp_lexer* lexer)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1147 lexer->saved_tokens.safe_push (lexer->next_token);
1150 /* Commit to the portion of the token stream most recently saved. */
1152 static void
1153 cp_lexer_commit_tokens (cp_lexer* lexer)
1155 /* Provide debugging output. */
1156 if (cp_lexer_debugging_p (lexer))
1157 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1159 lexer->saved_tokens.pop ();
1162 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1163 to the token stream. Stop saving tokens. */
1165 static void
1166 cp_lexer_rollback_tokens (cp_lexer* lexer)
1168 /* Provide debugging output. */
1169 if (cp_lexer_debugging_p (lexer))
1170 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1172 lexer->next_token = lexer->saved_tokens.pop ();
1175 /* RAII wrapper around the above functions, with sanity checking. Creating
1176 a variable saves tokens, which are committed when the variable is
1177 destroyed unless they are explicitly rolled back by calling the rollback
1178 member function. */
1180 struct saved_token_sentinel
1182 cp_lexer *lexer;
1183 unsigned len;
1184 bool commit;
1185 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1187 len = lexer->saved_tokens.length ();
1188 cp_lexer_save_tokens (lexer);
1190 void rollback ()
1192 cp_lexer_rollback_tokens (lexer);
1193 commit = false;
1195 ~saved_token_sentinel()
1197 if (commit)
1198 cp_lexer_commit_tokens (lexer);
1199 gcc_assert (lexer->saved_tokens.length () == len);
1203 /* Print a representation of the TOKEN on the STREAM. */
1205 static void
1206 cp_lexer_print_token (FILE * stream, cp_token *token)
1208 /* We don't use cpp_type2name here because the parser defines
1209 a few tokens of its own. */
1210 static const char *const token_names[] = {
1211 /* cpplib-defined token types */
1212 #define OP(e, s) #e,
1213 #define TK(e, s) #e,
1214 TTYPE_TABLE
1215 #undef OP
1216 #undef TK
1217 /* C++ parser token types - see "Manifest constants", above. */
1218 "KEYWORD",
1219 "TEMPLATE_ID",
1220 "NESTED_NAME_SPECIFIER",
1223 /* For some tokens, print the associated data. */
1224 switch (token->type)
1226 case CPP_KEYWORD:
1227 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1228 For example, `struct' is mapped to an INTEGER_CST. */
1229 if (!identifier_p (token->u.value))
1230 break;
1231 /* else fall through */
1232 case CPP_NAME:
1233 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1234 break;
1236 case CPP_STRING:
1237 case CPP_STRING16:
1238 case CPP_STRING32:
1239 case CPP_WSTRING:
1240 case CPP_UTF8STRING:
1241 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1242 break;
1244 case CPP_NUMBER:
1245 print_generic_expr (stream, token->u.value, 0);
1246 break;
1248 default:
1249 /* If we have a name for the token, print it out. Otherwise, we
1250 simply give the numeric code. */
1251 if (token->type < ARRAY_SIZE(token_names))
1252 fputs (token_names[token->type], stream);
1253 else
1254 fprintf (stream, "[%d]", token->type);
1255 break;
1259 DEBUG_FUNCTION void
1260 debug (cp_token &ref)
1262 cp_lexer_print_token (stderr, &ref);
1263 fprintf (stderr, "\n");
1266 DEBUG_FUNCTION void
1267 debug (cp_token *ptr)
1269 if (ptr)
1270 debug (*ptr);
1271 else
1272 fprintf (stderr, "<nil>\n");
1276 /* Start emitting debugging information. */
1278 static void
1279 cp_lexer_start_debugging (cp_lexer* lexer)
1281 lexer->debugging_p = true;
1282 cp_lexer_debug_stream = stderr;
1285 /* Stop emitting debugging information. */
1287 static void
1288 cp_lexer_stop_debugging (cp_lexer* lexer)
1290 lexer->debugging_p = false;
1291 cp_lexer_debug_stream = NULL;
1294 /* Create a new cp_token_cache, representing a range of tokens. */
1296 static cp_token_cache *
1297 cp_token_cache_new (cp_token *first, cp_token *last)
1299 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1300 cache->first = first;
1301 cache->last = last;
1302 return cache;
1305 /* Diagnose if #pragma omp declare simd isn't followed immediately
1306 by function declaration or definition. */
1308 static inline void
1309 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1311 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1313 error ("%<#pragma omp declare simd%> not immediately followed by "
1314 "function declaration or definition");
1315 parser->omp_declare_simd = NULL;
1319 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1320 and put that into "omp declare simd" attribute. */
1322 static inline void
1323 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1325 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1327 if (fndecl == error_mark_node)
1329 parser->omp_declare_simd = NULL;
1330 return;
1332 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1334 cp_ensure_no_omp_declare_simd (parser);
1335 return;
1340 /* Decl-specifiers. */
1342 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1344 static void
1345 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1347 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1350 /* Declarators. */
1352 /* Nothing other than the parser should be creating declarators;
1353 declarators are a semi-syntactic representation of C++ entities.
1354 Other parts of the front end that need to create entities (like
1355 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1357 static cp_declarator *make_call_declarator
1358 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1359 static cp_declarator *make_array_declarator
1360 (cp_declarator *, tree);
1361 static cp_declarator *make_pointer_declarator
1362 (cp_cv_quals, cp_declarator *, tree);
1363 static cp_declarator *make_reference_declarator
1364 (cp_cv_quals, cp_declarator *, bool, tree);
1365 static cp_parameter_declarator *make_parameter_declarator
1366 (cp_decl_specifier_seq *, cp_declarator *, tree);
1367 static cp_declarator *make_ptrmem_declarator
1368 (cp_cv_quals, tree, cp_declarator *, tree);
1370 /* An erroneous declarator. */
1371 static cp_declarator *cp_error_declarator;
1373 /* The obstack on which declarators and related data structures are
1374 allocated. */
1375 static struct obstack declarator_obstack;
1377 /* Alloc BYTES from the declarator memory pool. */
1379 static inline void *
1380 alloc_declarator (size_t bytes)
1382 return obstack_alloc (&declarator_obstack, bytes);
1385 /* Allocate a declarator of the indicated KIND. Clear fields that are
1386 common to all declarators. */
1388 static cp_declarator *
1389 make_declarator (cp_declarator_kind kind)
1391 cp_declarator *declarator;
1393 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1394 declarator->kind = kind;
1395 declarator->attributes = NULL_TREE;
1396 declarator->std_attributes = NULL_TREE;
1397 declarator->declarator = NULL;
1398 declarator->parameter_pack_p = false;
1399 declarator->id_loc = UNKNOWN_LOCATION;
1401 return declarator;
1404 /* Make a declarator for a generalized identifier. If
1405 QUALIFYING_SCOPE is non-NULL, the identifier is
1406 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1407 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1408 is, if any. */
1410 static cp_declarator *
1411 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1412 special_function_kind sfk)
1414 cp_declarator *declarator;
1416 /* It is valid to write:
1418 class C { void f(); };
1419 typedef C D;
1420 void D::f();
1422 The standard is not clear about whether `typedef const C D' is
1423 legal; as of 2002-09-15 the committee is considering that
1424 question. EDG 3.0 allows that syntax. Therefore, we do as
1425 well. */
1426 if (qualifying_scope && TYPE_P (qualifying_scope))
1427 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1429 gcc_assert (identifier_p (unqualified_name)
1430 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1431 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1433 declarator = make_declarator (cdk_id);
1434 declarator->u.id.qualifying_scope = qualifying_scope;
1435 declarator->u.id.unqualified_name = unqualified_name;
1436 declarator->u.id.sfk = sfk;
1438 return declarator;
1441 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1442 of modifiers such as const or volatile to apply to the pointer
1443 type, represented as identifiers. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1446 cp_declarator *
1447 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1448 tree attributes)
1450 cp_declarator *declarator;
1452 declarator = make_declarator (cdk_pointer);
1453 declarator->declarator = target;
1454 declarator->u.pointer.qualifiers = cv_qualifiers;
1455 declarator->u.pointer.class_type = NULL_TREE;
1456 if (target)
1458 declarator->id_loc = target->id_loc;
1459 declarator->parameter_pack_p = target->parameter_pack_p;
1460 target->parameter_pack_p = false;
1462 else
1463 declarator->parameter_pack_p = false;
1465 declarator->std_attributes = attributes;
1467 return declarator;
1470 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1471 represent the attributes that appertain to the pointer or
1472 reference. */
1474 cp_declarator *
1475 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1476 bool rvalue_ref, tree attributes)
1478 cp_declarator *declarator;
1480 declarator = make_declarator (cdk_reference);
1481 declarator->declarator = target;
1482 declarator->u.reference.qualifiers = cv_qualifiers;
1483 declarator->u.reference.rvalue_ref = rvalue_ref;
1484 if (target)
1486 declarator->id_loc = target->id_loc;
1487 declarator->parameter_pack_p = target->parameter_pack_p;
1488 target->parameter_pack_p = false;
1490 else
1491 declarator->parameter_pack_p = false;
1493 declarator->std_attributes = attributes;
1495 return declarator;
1498 /* Like make_pointer_declarator -- but for a pointer to a non-static
1499 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1500 appertain to the pointer or reference. */
1502 cp_declarator *
1503 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1504 cp_declarator *pointee,
1505 tree attributes)
1507 cp_declarator *declarator;
1509 declarator = make_declarator (cdk_ptrmem);
1510 declarator->declarator = pointee;
1511 declarator->u.pointer.qualifiers = cv_qualifiers;
1512 declarator->u.pointer.class_type = class_type;
1514 if (pointee)
1516 declarator->parameter_pack_p = pointee->parameter_pack_p;
1517 pointee->parameter_pack_p = false;
1519 else
1520 declarator->parameter_pack_p = false;
1522 declarator->std_attributes = attributes;
1524 return declarator;
1527 /* Make a declarator for the function given by TARGET, with the
1528 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1529 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1530 indicates what exceptions can be thrown. */
1532 cp_declarator *
1533 make_call_declarator (cp_declarator *target,
1534 tree parms,
1535 cp_cv_quals cv_qualifiers,
1536 cp_virt_specifiers virt_specifiers,
1537 cp_ref_qualifier ref_qualifier,
1538 tree exception_specification,
1539 tree late_return_type)
1541 cp_declarator *declarator;
1543 declarator = make_declarator (cdk_function);
1544 declarator->declarator = target;
1545 declarator->u.function.parameters = parms;
1546 declarator->u.function.qualifiers = cv_qualifiers;
1547 declarator->u.function.virt_specifiers = virt_specifiers;
1548 declarator->u.function.ref_qualifier = ref_qualifier;
1549 declarator->u.function.exception_specification = exception_specification;
1550 declarator->u.function.late_return_type = late_return_type;
1551 if (target)
1553 declarator->id_loc = target->id_loc;
1554 declarator->parameter_pack_p = target->parameter_pack_p;
1555 target->parameter_pack_p = false;
1557 else
1558 declarator->parameter_pack_p = false;
1560 return declarator;
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1569 cp_declarator *declarator;
1571 declarator = make_declarator (cdk_array);
1572 declarator->declarator = element;
1573 declarator->u.array.bounds = bounds;
1574 if (element)
1576 declarator->id_loc = element->id_loc;
1577 declarator->parameter_pack_p = element->parameter_pack_p;
1578 element->parameter_pack_p = false;
1580 else
1581 declarator->parameter_pack_p = false;
1583 return declarator;
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1588 static bool
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1591 /* Search for a declarator name, or any other declarator that goes
1592 after the point where the ellipsis could appear in a parameter
1593 pack. If we find any of these, then this declarator can not be
1594 made into a parameter pack. */
1595 bool found = false;
1596 while (declarator && !found)
1598 switch ((int)declarator->kind)
1600 case cdk_id:
1601 case cdk_array:
1602 found = true;
1603 break;
1605 case cdk_error:
1606 return true;
1608 default:
1609 declarator = declarator->declarator;
1610 break;
1614 return !found;
1617 cp_parameter_declarator *no_parameters;
1619 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1620 DECLARATOR and DEFAULT_ARGUMENT. */
1622 cp_parameter_declarator *
1623 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1624 cp_declarator *declarator,
1625 tree default_argument)
1627 cp_parameter_declarator *parameter;
1629 parameter = ((cp_parameter_declarator *)
1630 alloc_declarator (sizeof (cp_parameter_declarator)));
1631 parameter->next = NULL;
1632 if (decl_specifiers)
1633 parameter->decl_specifiers = *decl_specifiers;
1634 else
1635 clear_decl_specs (&parameter->decl_specifiers);
1636 parameter->declarator = declarator;
1637 parameter->default_argument = default_argument;
1638 parameter->ellipsis_p = false;
1640 return parameter;
1643 /* Returns true iff DECLARATOR is a declaration for a function. */
1645 static bool
1646 function_declarator_p (const cp_declarator *declarator)
1648 while (declarator)
1650 if (declarator->kind == cdk_function
1651 && declarator->declarator->kind == cdk_id)
1652 return true;
1653 if (declarator->kind == cdk_id
1654 || declarator->kind == cdk_error)
1655 return false;
1656 declarator = declarator->declarator;
1658 return false;
1661 /* The parser. */
1663 /* Overview
1664 --------
1666 A cp_parser parses the token stream as specified by the C++
1667 grammar. Its job is purely parsing, not semantic analysis. For
1668 example, the parser breaks the token stream into declarators,
1669 expressions, statements, and other similar syntactic constructs.
1670 It does not check that the types of the expressions on either side
1671 of an assignment-statement are compatible, or that a function is
1672 not declared with a parameter of type `void'.
1674 The parser invokes routines elsewhere in the compiler to perform
1675 semantic analysis and to build up the abstract syntax tree for the
1676 code processed.
1678 The parser (and the template instantiation code, which is, in a
1679 way, a close relative of parsing) are the only parts of the
1680 compiler that should be calling push_scope and pop_scope, or
1681 related functions. The parser (and template instantiation code)
1682 keeps track of what scope is presently active; everything else
1683 should simply honor that. (The code that generates static
1684 initializers may also need to set the scope, in order to check
1685 access control correctly when emitting the initializers.)
1687 Methodology
1688 -----------
1690 The parser is of the standard recursive-descent variety. Upcoming
1691 tokens in the token stream are examined in order to determine which
1692 production to use when parsing a non-terminal. Some C++ constructs
1693 require arbitrary look ahead to disambiguate. For example, it is
1694 impossible, in the general case, to tell whether a statement is an
1695 expression or declaration without scanning the entire statement.
1696 Therefore, the parser is capable of "parsing tentatively." When the
1697 parser is not sure what construct comes next, it enters this mode.
1698 Then, while we attempt to parse the construct, the parser queues up
1699 error messages, rather than issuing them immediately, and saves the
1700 tokens it consumes. If the construct is parsed successfully, the
1701 parser "commits", i.e., it issues any queued error messages and
1702 the tokens that were being preserved are permanently discarded.
1703 If, however, the construct is not parsed successfully, the parser
1704 rolls back its state completely so that it can resume parsing using
1705 a different alternative.
1707 Future Improvements
1708 -------------------
1710 The performance of the parser could probably be improved substantially.
1711 We could often eliminate the need to parse tentatively by looking ahead
1712 a little bit. In some places, this approach might not entirely eliminate
1713 the need to parse tentatively, but it might still speed up the average
1714 case. */
1716 /* Flags that are passed to some parsing functions. These values can
1717 be bitwise-ored together. */
1719 enum
1721 /* No flags. */
1722 CP_PARSER_FLAGS_NONE = 0x0,
1723 /* The construct is optional. If it is not present, then no error
1724 should be issued. */
1725 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1726 /* When parsing a type-specifier, treat user-defined type-names
1727 as non-type identifiers. */
1728 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1729 /* When parsing a type-specifier, do not try to parse a class-specifier
1730 or enum-specifier. */
1731 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1732 /* When parsing a decl-specifier-seq, only allow type-specifier or
1733 constexpr. */
1734 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1737 /* This type is used for parameters and variables which hold
1738 combinations of the above flags. */
1739 typedef int cp_parser_flags;
1741 /* The different kinds of declarators we want to parse. */
1743 typedef enum cp_parser_declarator_kind
1745 /* We want an abstract declarator. */
1746 CP_PARSER_DECLARATOR_ABSTRACT,
1747 /* We want a named declarator. */
1748 CP_PARSER_DECLARATOR_NAMED,
1749 /* We don't mind, but the name must be an unqualified-id. */
1750 CP_PARSER_DECLARATOR_EITHER
1751 } cp_parser_declarator_kind;
1753 /* The precedence values used to parse binary expressions. The minimum value
1754 of PREC must be 1, because zero is reserved to quickly discriminate
1755 binary operators from other tokens. */
1757 enum cp_parser_prec
1759 PREC_NOT_OPERATOR,
1760 PREC_LOGICAL_OR_EXPRESSION,
1761 PREC_LOGICAL_AND_EXPRESSION,
1762 PREC_INCLUSIVE_OR_EXPRESSION,
1763 PREC_EXCLUSIVE_OR_EXPRESSION,
1764 PREC_AND_EXPRESSION,
1765 PREC_EQUALITY_EXPRESSION,
1766 PREC_RELATIONAL_EXPRESSION,
1767 PREC_SHIFT_EXPRESSION,
1768 PREC_ADDITIVE_EXPRESSION,
1769 PREC_MULTIPLICATIVE_EXPRESSION,
1770 PREC_PM_EXPRESSION,
1771 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1774 /* A mapping from a token type to a corresponding tree node type, with a
1775 precedence value. */
1777 typedef struct cp_parser_binary_operations_map_node
1779 /* The token type. */
1780 enum cpp_ttype token_type;
1781 /* The corresponding tree code. */
1782 enum tree_code tree_type;
1783 /* The precedence of this operator. */
1784 enum cp_parser_prec prec;
1785 } cp_parser_binary_operations_map_node;
1787 typedef struct cp_parser_expression_stack_entry
1789 /* Left hand side of the binary operation we are currently
1790 parsing. */
1791 tree lhs;
1792 /* Original tree code for left hand side, if it was a binary
1793 expression itself (used for -Wparentheses). */
1794 enum tree_code lhs_type;
1795 /* Tree code for the binary operation we are parsing. */
1796 enum tree_code tree_type;
1797 /* Precedence of the binary operation we are parsing. */
1798 enum cp_parser_prec prec;
1799 /* Location of the binary operation we are parsing. */
1800 location_t loc;
1801 } cp_parser_expression_stack_entry;
1803 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1804 entries because precedence levels on the stack are monotonically
1805 increasing. */
1806 typedef struct cp_parser_expression_stack_entry
1807 cp_parser_expression_stack[NUM_PREC_VALUES];
1809 /* Prototypes. */
1811 /* Constructors and destructors. */
1813 static cp_parser_context *cp_parser_context_new
1814 (cp_parser_context *);
1816 /* Class variables. */
1818 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1820 /* The operator-precedence table used by cp_parser_binary_expression.
1821 Transformed into an associative array (binops_by_token) by
1822 cp_parser_new. */
1824 static const cp_parser_binary_operations_map_node binops[] = {
1825 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1826 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1828 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1829 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1830 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1832 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1833 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1835 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1836 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1838 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1839 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1840 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1841 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1843 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1844 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1846 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1848 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1850 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1852 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1854 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1857 /* The same as binops, but initialized by cp_parser_new so that
1858 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1859 for speed. */
1860 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1862 /* Constructors and destructors. */
1864 /* Construct a new context. The context below this one on the stack
1865 is given by NEXT. */
1867 static cp_parser_context *
1868 cp_parser_context_new (cp_parser_context* next)
1870 cp_parser_context *context;
1872 /* Allocate the storage. */
1873 if (cp_parser_context_free_list != NULL)
1875 /* Pull the first entry from the free list. */
1876 context = cp_parser_context_free_list;
1877 cp_parser_context_free_list = context->next;
1878 memset (context, 0, sizeof (*context));
1880 else
1881 context = ggc_cleared_alloc<cp_parser_context> ();
1883 /* No errors have occurred yet in this context. */
1884 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1885 /* If this is not the bottommost context, copy information that we
1886 need from the previous context. */
1887 if (next)
1889 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1890 expression, then we are parsing one in this context, too. */
1891 context->object_type = next->object_type;
1892 /* Thread the stack. */
1893 context->next = next;
1896 return context;
1899 /* Managing the unparsed function queues. */
1901 #define unparsed_funs_with_default_args \
1902 parser->unparsed_queues->last ().funs_with_default_args
1903 #define unparsed_funs_with_definitions \
1904 parser->unparsed_queues->last ().funs_with_definitions
1905 #define unparsed_nsdmis \
1906 parser->unparsed_queues->last ().nsdmis
1907 #define unparsed_classes \
1908 parser->unparsed_queues->last ().classes
1910 static void
1911 push_unparsed_function_queues (cp_parser *parser)
1913 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1914 vec_safe_push (parser->unparsed_queues, e);
1917 static void
1918 pop_unparsed_function_queues (cp_parser *parser)
1920 release_tree_vector (unparsed_funs_with_definitions);
1921 parser->unparsed_queues->pop ();
1924 /* Prototypes. */
1926 /* Constructors and destructors. */
1928 static cp_parser *cp_parser_new
1929 (void);
1931 /* Routines to parse various constructs.
1933 Those that return `tree' will return the error_mark_node (rather
1934 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1935 Sometimes, they will return an ordinary node if error-recovery was
1936 attempted, even though a parse error occurred. So, to check
1937 whether or not a parse error occurred, you should always use
1938 cp_parser_error_occurred. If the construct is optional (indicated
1939 either by an `_opt' in the name of the function that does the
1940 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1941 the construct is not present. */
1943 /* Lexical conventions [gram.lex] */
1945 static tree cp_parser_identifier
1946 (cp_parser *);
1947 static tree cp_parser_string_literal
1948 (cp_parser *, bool, bool, bool);
1949 static tree cp_parser_userdef_char_literal
1950 (cp_parser *);
1951 static tree cp_parser_userdef_string_literal
1952 (tree);
1953 static tree cp_parser_userdef_numeric_literal
1954 (cp_parser *);
1956 /* Basic concepts [gram.basic] */
1958 static bool cp_parser_translation_unit
1959 (cp_parser *);
1961 /* Expressions [gram.expr] */
1963 static tree cp_parser_primary_expression
1964 (cp_parser *, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_id_expression
1966 (cp_parser *, bool, bool, bool *, bool, bool);
1967 static tree cp_parser_unqualified_id
1968 (cp_parser *, bool, bool, bool, bool);
1969 static tree cp_parser_nested_name_specifier_opt
1970 (cp_parser *, bool, bool, bool, bool);
1971 static tree cp_parser_nested_name_specifier
1972 (cp_parser *, bool, bool, bool, bool);
1973 static tree cp_parser_qualifying_entity
1974 (cp_parser *, bool, bool, bool, bool, bool);
1975 static tree cp_parser_postfix_expression
1976 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1977 static tree cp_parser_postfix_open_square_expression
1978 (cp_parser *, tree, bool, bool);
1979 static tree cp_parser_postfix_dot_deref_expression
1980 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1981 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1982 (cp_parser *, int, bool, bool, bool *, bool = false);
1983 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1984 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1985 static void cp_parser_pseudo_destructor_name
1986 (cp_parser *, tree, tree *, tree *);
1987 static tree cp_parser_unary_expression
1988 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1989 static enum tree_code cp_parser_unary_operator
1990 (cp_token *);
1991 static tree cp_parser_new_expression
1992 (cp_parser *);
1993 static vec<tree, va_gc> *cp_parser_new_placement
1994 (cp_parser *);
1995 static tree cp_parser_new_type_id
1996 (cp_parser *, tree *);
1997 static cp_declarator *cp_parser_new_declarator_opt
1998 (cp_parser *);
1999 static cp_declarator *cp_parser_direct_new_declarator
2000 (cp_parser *);
2001 static vec<tree, va_gc> *cp_parser_new_initializer
2002 (cp_parser *);
2003 static tree cp_parser_delete_expression
2004 (cp_parser *);
2005 static tree cp_parser_cast_expression
2006 (cp_parser *, bool, bool, bool, cp_id_kind *);
2007 static tree cp_parser_binary_expression
2008 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2009 static tree cp_parser_question_colon_clause
2010 (cp_parser *, tree);
2011 static tree cp_parser_assignment_expression
2012 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2013 static enum tree_code cp_parser_assignment_operator_opt
2014 (cp_parser *);
2015 static tree cp_parser_expression
2016 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2017 static tree cp_parser_constant_expression
2018 (cp_parser *, bool = false, bool * = NULL);
2019 static tree cp_parser_builtin_offsetof
2020 (cp_parser *);
2021 static tree cp_parser_lambda_expression
2022 (cp_parser *);
2023 static void cp_parser_lambda_introducer
2024 (cp_parser *, tree);
2025 static bool cp_parser_lambda_declarator_opt
2026 (cp_parser *, tree);
2027 static void cp_parser_lambda_body
2028 (cp_parser *, tree);
2030 /* Statements [gram.stmt.stmt] */
2032 static void cp_parser_statement
2033 (cp_parser *, tree, bool, bool *);
2034 static void cp_parser_label_for_labeled_statement
2035 (cp_parser *, tree);
2036 static tree cp_parser_expression_statement
2037 (cp_parser *, tree);
2038 static tree cp_parser_compound_statement
2039 (cp_parser *, tree, bool, bool);
2040 static void cp_parser_statement_seq_opt
2041 (cp_parser *, tree);
2042 static tree cp_parser_selection_statement
2043 (cp_parser *, bool *);
2044 static tree cp_parser_condition
2045 (cp_parser *);
2046 static tree cp_parser_iteration_statement
2047 (cp_parser *, bool);
2048 static bool cp_parser_for_init_statement
2049 (cp_parser *, tree *decl);
2050 static tree cp_parser_for
2051 (cp_parser *, bool);
2052 static tree cp_parser_c_for
2053 (cp_parser *, tree, tree, bool);
2054 static tree cp_parser_range_for
2055 (cp_parser *, tree, tree, tree, bool);
2056 static void do_range_for_auto_deduction
2057 (tree, tree);
2058 static tree cp_parser_perform_range_for_lookup
2059 (tree, tree *, tree *);
2060 static tree cp_parser_range_for_member_function
2061 (tree, tree);
2062 static tree cp_parser_jump_statement
2063 (cp_parser *);
2064 static void cp_parser_declaration_statement
2065 (cp_parser *);
2067 static tree cp_parser_implicitly_scoped_statement
2068 (cp_parser *, bool *);
2069 static void cp_parser_already_scoped_statement
2070 (cp_parser *);
2072 /* Declarations [gram.dcl.dcl] */
2074 static void cp_parser_declaration_seq_opt
2075 (cp_parser *);
2076 static void cp_parser_declaration
2077 (cp_parser *);
2078 static void cp_parser_block_declaration
2079 (cp_parser *, bool);
2080 static void cp_parser_simple_declaration
2081 (cp_parser *, bool, tree *);
2082 static void cp_parser_decl_specifier_seq
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2084 static tree cp_parser_storage_class_specifier_opt
2085 (cp_parser *);
2086 static tree cp_parser_function_specifier_opt
2087 (cp_parser *, cp_decl_specifier_seq *);
2088 static tree cp_parser_type_specifier
2089 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2090 int *, bool *);
2091 static tree cp_parser_simple_type_specifier
2092 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2093 static tree cp_parser_type_name
2094 (cp_parser *);
2095 static tree cp_parser_nonclass_name
2096 (cp_parser* parser);
2097 static tree cp_parser_elaborated_type_specifier
2098 (cp_parser *, bool, bool);
2099 static tree cp_parser_enum_specifier
2100 (cp_parser *);
2101 static void cp_parser_enumerator_list
2102 (cp_parser *, tree);
2103 static void cp_parser_enumerator_definition
2104 (cp_parser *, tree);
2105 static tree cp_parser_namespace_name
2106 (cp_parser *);
2107 static void cp_parser_namespace_definition
2108 (cp_parser *);
2109 static void cp_parser_namespace_body
2110 (cp_parser *);
2111 static tree cp_parser_qualified_namespace_specifier
2112 (cp_parser *);
2113 static void cp_parser_namespace_alias_definition
2114 (cp_parser *);
2115 static bool cp_parser_using_declaration
2116 (cp_parser *, bool);
2117 static void cp_parser_using_directive
2118 (cp_parser *);
2119 static tree cp_parser_alias_declaration
2120 (cp_parser *);
2121 static void cp_parser_asm_definition
2122 (cp_parser *);
2123 static void cp_parser_linkage_specification
2124 (cp_parser *);
2125 static void cp_parser_static_assert
2126 (cp_parser *, bool);
2127 static tree cp_parser_decltype
2128 (cp_parser *);
2130 /* Declarators [gram.dcl.decl] */
2132 static tree cp_parser_init_declarator
2133 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2134 bool, bool, int, bool *, tree *, location_t *);
2135 static cp_declarator *cp_parser_declarator
2136 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2137 static cp_declarator *cp_parser_direct_declarator
2138 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2139 static enum tree_code cp_parser_ptr_operator
2140 (cp_parser *, tree *, cp_cv_quals *, tree *);
2141 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142 (cp_parser *);
2143 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144 (cp_parser *);
2145 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_late_return_type_opt
2148 (cp_parser *, cp_declarator *, cp_cv_quals);
2149 static tree cp_parser_declarator_id
2150 (cp_parser *, bool);
2151 static tree cp_parser_type_id
2152 (cp_parser *);
2153 static tree cp_parser_template_type_arg
2154 (cp_parser *);
2155 static tree cp_parser_trailing_type_id (cp_parser *);
2156 static tree cp_parser_type_id_1
2157 (cp_parser *, bool, bool);
2158 static void cp_parser_type_specifier_seq
2159 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2160 static tree cp_parser_parameter_declaration_clause
2161 (cp_parser *);
2162 static tree cp_parser_parameter_declaration_list
2163 (cp_parser *, bool *);
2164 static cp_parameter_declarator *cp_parser_parameter_declaration
2165 (cp_parser *, bool, bool *);
2166 static tree cp_parser_default_argument
2167 (cp_parser *, bool);
2168 static void cp_parser_function_body
2169 (cp_parser *, bool);
2170 static tree cp_parser_initializer
2171 (cp_parser *, bool *, bool *);
2172 static tree cp_parser_initializer_clause
2173 (cp_parser *, bool *);
2174 static tree cp_parser_braced_list
2175 (cp_parser*, bool*);
2176 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2177 (cp_parser *, bool *);
2179 static bool cp_parser_ctor_initializer_opt_and_function_body
2180 (cp_parser *, bool);
2182 static tree cp_parser_late_parsing_omp_declare_simd
2183 (cp_parser *, tree);
2185 static tree cp_parser_late_parsing_cilk_simd_fn_info
2186 (cp_parser *, tree);
2188 static tree synthesize_implicit_template_parm
2189 (cp_parser *);
2190 static tree finish_fully_implicit_template
2191 (cp_parser *, tree);
2193 /* Classes [gram.class] */
2195 static tree cp_parser_class_name
2196 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2197 static tree cp_parser_class_specifier
2198 (cp_parser *);
2199 static tree cp_parser_class_head
2200 (cp_parser *, bool *);
2201 static enum tag_types cp_parser_class_key
2202 (cp_parser *);
2203 static void cp_parser_type_parameter_key
2204 (cp_parser* parser);
2205 static void cp_parser_member_specification_opt
2206 (cp_parser *);
2207 static void cp_parser_member_declaration
2208 (cp_parser *);
2209 static tree cp_parser_pure_specifier
2210 (cp_parser *);
2211 static tree cp_parser_constant_initializer
2212 (cp_parser *);
2214 /* Derived classes [gram.class.derived] */
2216 static tree cp_parser_base_clause
2217 (cp_parser *);
2218 static tree cp_parser_base_specifier
2219 (cp_parser *);
2221 /* Special member functions [gram.special] */
2223 static tree cp_parser_conversion_function_id
2224 (cp_parser *);
2225 static tree cp_parser_conversion_type_id
2226 (cp_parser *);
2227 static cp_declarator *cp_parser_conversion_declarator_opt
2228 (cp_parser *);
2229 static bool cp_parser_ctor_initializer_opt
2230 (cp_parser *);
2231 static void cp_parser_mem_initializer_list
2232 (cp_parser *);
2233 static tree cp_parser_mem_initializer
2234 (cp_parser *);
2235 static tree cp_parser_mem_initializer_id
2236 (cp_parser *);
2238 /* Overloading [gram.over] */
2240 static tree cp_parser_operator_function_id
2241 (cp_parser *);
2242 static tree cp_parser_operator
2243 (cp_parser *);
2245 /* Templates [gram.temp] */
2247 static void cp_parser_template_declaration
2248 (cp_parser *, bool);
2249 static tree cp_parser_template_parameter_list
2250 (cp_parser *);
2251 static tree cp_parser_template_parameter
2252 (cp_parser *, bool *, bool *);
2253 static tree cp_parser_type_parameter
2254 (cp_parser *, bool *);
2255 static tree cp_parser_template_id
2256 (cp_parser *, bool, bool, enum tag_types, bool);
2257 static tree cp_parser_template_name
2258 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2259 static tree cp_parser_template_argument_list
2260 (cp_parser *);
2261 static tree cp_parser_template_argument
2262 (cp_parser *);
2263 static void cp_parser_explicit_instantiation
2264 (cp_parser *);
2265 static void cp_parser_explicit_specialization
2266 (cp_parser *);
2268 /* Exception handling [gram.exception] */
2270 static tree cp_parser_try_block
2271 (cp_parser *);
2272 static bool cp_parser_function_try_block
2273 (cp_parser *);
2274 static void cp_parser_handler_seq
2275 (cp_parser *);
2276 static void cp_parser_handler
2277 (cp_parser *);
2278 static tree cp_parser_exception_declaration
2279 (cp_parser *);
2280 static tree cp_parser_throw_expression
2281 (cp_parser *);
2282 static tree cp_parser_exception_specification_opt
2283 (cp_parser *);
2284 static tree cp_parser_type_id_list
2285 (cp_parser *);
2287 /* GNU Extensions */
2289 static tree cp_parser_asm_specification_opt
2290 (cp_parser *);
2291 static tree cp_parser_asm_operand_list
2292 (cp_parser *);
2293 static tree cp_parser_asm_clobber_list
2294 (cp_parser *);
2295 static tree cp_parser_asm_label_list
2296 (cp_parser *);
2297 static bool cp_next_tokens_can_be_attribute_p
2298 (cp_parser *);
2299 static bool cp_next_tokens_can_be_gnu_attribute_p
2300 (cp_parser *);
2301 static bool cp_next_tokens_can_be_std_attribute_p
2302 (cp_parser *);
2303 static bool cp_nth_tokens_can_be_std_attribute_p
2304 (cp_parser *, size_t);
2305 static bool cp_nth_tokens_can_be_gnu_attribute_p
2306 (cp_parser *, size_t);
2307 static bool cp_nth_tokens_can_be_attribute_p
2308 (cp_parser *, size_t);
2309 static tree cp_parser_attributes_opt
2310 (cp_parser *);
2311 static tree cp_parser_gnu_attributes_opt
2312 (cp_parser *);
2313 static tree cp_parser_gnu_attribute_list
2314 (cp_parser *);
2315 static tree cp_parser_std_attribute
2316 (cp_parser *);
2317 static tree cp_parser_std_attribute_spec
2318 (cp_parser *);
2319 static tree cp_parser_std_attribute_spec_seq
2320 (cp_parser *);
2321 static bool cp_parser_extension_opt
2322 (cp_parser *, int *);
2323 static void cp_parser_label_declaration
2324 (cp_parser *);
2326 /* Transactional Memory Extensions */
2328 static tree cp_parser_transaction
2329 (cp_parser *, enum rid);
2330 static tree cp_parser_transaction_expression
2331 (cp_parser *, enum rid);
2332 static bool cp_parser_function_transaction
2333 (cp_parser *, enum rid);
2334 static tree cp_parser_transaction_cancel
2335 (cp_parser *);
2337 enum pragma_context {
2338 pragma_external,
2339 pragma_member,
2340 pragma_objc_icode,
2341 pragma_stmt,
2342 pragma_compound
2344 static bool cp_parser_pragma
2345 (cp_parser *, enum pragma_context);
2347 /* Objective-C++ Productions */
2349 static tree cp_parser_objc_message_receiver
2350 (cp_parser *);
2351 static tree cp_parser_objc_message_args
2352 (cp_parser *);
2353 static tree cp_parser_objc_message_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_encode_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_defs_expression
2358 (cp_parser *);
2359 static tree cp_parser_objc_protocol_expression
2360 (cp_parser *);
2361 static tree cp_parser_objc_selector_expression
2362 (cp_parser *);
2363 static tree cp_parser_objc_expression
2364 (cp_parser *);
2365 static bool cp_parser_objc_selector_p
2366 (enum cpp_ttype);
2367 static tree cp_parser_objc_selector
2368 (cp_parser *);
2369 static tree cp_parser_objc_protocol_refs_opt
2370 (cp_parser *);
2371 static void cp_parser_objc_declaration
2372 (cp_parser *, tree);
2373 static tree cp_parser_objc_statement
2374 (cp_parser *);
2375 static bool cp_parser_objc_valid_prefix_attributes
2376 (cp_parser *, tree *);
2377 static void cp_parser_objc_at_property_declaration
2378 (cp_parser *) ;
2379 static void cp_parser_objc_at_synthesize_declaration
2380 (cp_parser *) ;
2381 static void cp_parser_objc_at_dynamic_declaration
2382 (cp_parser *) ;
2383 static tree cp_parser_objc_struct_declaration
2384 (cp_parser *) ;
2386 /* Utility Routines */
2388 static tree cp_parser_lookup_name
2389 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2390 static tree cp_parser_lookup_name_simple
2391 (cp_parser *, tree, location_t);
2392 static tree cp_parser_maybe_treat_template_as_class
2393 (tree, bool);
2394 static bool cp_parser_check_declarator_template_parameters
2395 (cp_parser *, cp_declarator *, location_t);
2396 static bool cp_parser_check_template_parameters
2397 (cp_parser *, unsigned, location_t, cp_declarator *);
2398 static tree cp_parser_simple_cast_expression
2399 (cp_parser *);
2400 static tree cp_parser_global_scope_opt
2401 (cp_parser *, bool);
2402 static bool cp_parser_constructor_declarator_p
2403 (cp_parser *, bool);
2404 static tree cp_parser_function_definition_from_specifiers_and_declarator
2405 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2406 static tree cp_parser_function_definition_after_declarator
2407 (cp_parser *, bool);
2408 static void cp_parser_template_declaration_after_export
2409 (cp_parser *, bool);
2410 static void cp_parser_perform_template_parameter_access_checks
2411 (vec<deferred_access_check, va_gc> *);
2412 static tree cp_parser_single_declaration
2413 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2414 static tree cp_parser_functional_cast
2415 (cp_parser *, tree);
2416 static tree cp_parser_save_member_function_body
2417 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2418 static tree cp_parser_save_nsdmi
2419 (cp_parser *);
2420 static tree cp_parser_enclosed_template_argument_list
2421 (cp_parser *);
2422 static void cp_parser_save_default_args
2423 (cp_parser *, tree);
2424 static void cp_parser_late_parsing_for_member
2425 (cp_parser *, tree);
2426 static tree cp_parser_late_parse_one_default_arg
2427 (cp_parser *, tree, tree, tree);
2428 static void cp_parser_late_parsing_nsdmi
2429 (cp_parser *, tree);
2430 static void cp_parser_late_parsing_default_args
2431 (cp_parser *, tree);
2432 static tree cp_parser_sizeof_operand
2433 (cp_parser *, enum rid);
2434 static tree cp_parser_trait_expr
2435 (cp_parser *, enum rid);
2436 static bool cp_parser_declares_only_class_p
2437 (cp_parser *);
2438 static void cp_parser_set_storage_class
2439 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2440 static void cp_parser_set_decl_spec_type
2441 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2442 static void set_and_check_decl_spec_loc
2443 (cp_decl_specifier_seq *decl_specs,
2444 cp_decl_spec ds, cp_token *);
2445 static bool cp_parser_friend_p
2446 (const cp_decl_specifier_seq *);
2447 static void cp_parser_required_error
2448 (cp_parser *, required_token, bool);
2449 static cp_token *cp_parser_require
2450 (cp_parser *, enum cpp_ttype, required_token);
2451 static cp_token *cp_parser_require_keyword
2452 (cp_parser *, enum rid, required_token);
2453 static bool cp_parser_token_starts_function_definition_p
2454 (cp_token *);
2455 static bool cp_parser_next_token_starts_class_definition_p
2456 (cp_parser *);
2457 static bool cp_parser_next_token_ends_template_argument_p
2458 (cp_parser *);
2459 static bool cp_parser_nth_token_starts_template_argument_list_p
2460 (cp_parser *, size_t);
2461 static enum tag_types cp_parser_token_is_class_key
2462 (cp_token *);
2463 static enum tag_types cp_parser_token_is_type_parameter_key
2464 (cp_token *);
2465 static void cp_parser_check_class_key
2466 (enum tag_types, tree type);
2467 static void cp_parser_check_access_in_redeclaration
2468 (tree type, location_t location);
2469 static bool cp_parser_optional_template_keyword
2470 (cp_parser *);
2471 static void cp_parser_pre_parsed_nested_name_specifier
2472 (cp_parser *);
2473 static bool cp_parser_cache_group
2474 (cp_parser *, enum cpp_ttype, unsigned);
2475 static tree cp_parser_cache_defarg
2476 (cp_parser *parser, bool nsdmi);
2477 static void cp_parser_parse_tentatively
2478 (cp_parser *);
2479 static void cp_parser_commit_to_tentative_parse
2480 (cp_parser *);
2481 static void cp_parser_commit_to_topmost_tentative_parse
2482 (cp_parser *);
2483 static void cp_parser_abort_tentative_parse
2484 (cp_parser *);
2485 static bool cp_parser_parse_definitely
2486 (cp_parser *);
2487 static inline bool cp_parser_parsing_tentatively
2488 (cp_parser *);
2489 static bool cp_parser_uncommitted_to_tentative_parse_p
2490 (cp_parser *);
2491 static void cp_parser_error
2492 (cp_parser *, const char *);
2493 static void cp_parser_name_lookup_error
2494 (cp_parser *, tree, tree, name_lookup_error, location_t);
2495 static bool cp_parser_simulate_error
2496 (cp_parser *);
2497 static bool cp_parser_check_type_definition
2498 (cp_parser *);
2499 static void cp_parser_check_for_definition_in_return_type
2500 (cp_declarator *, tree, location_t type_location);
2501 static void cp_parser_check_for_invalid_template_id
2502 (cp_parser *, tree, enum tag_types, location_t location);
2503 static bool cp_parser_non_integral_constant_expression
2504 (cp_parser *, non_integral_constant);
2505 static void cp_parser_diagnose_invalid_type_name
2506 (cp_parser *, tree, location_t);
2507 static bool cp_parser_parse_and_diagnose_invalid_type_name
2508 (cp_parser *);
2509 static int cp_parser_skip_to_closing_parenthesis
2510 (cp_parser *, bool, bool, bool);
2511 static void cp_parser_skip_to_end_of_statement
2512 (cp_parser *);
2513 static void cp_parser_consume_semicolon_at_end_of_statement
2514 (cp_parser *);
2515 static void cp_parser_skip_to_end_of_block_or_statement
2516 (cp_parser *);
2517 static bool cp_parser_skip_to_closing_brace
2518 (cp_parser *);
2519 static void cp_parser_skip_to_end_of_template_parameter_list
2520 (cp_parser *);
2521 static void cp_parser_skip_to_pragma_eol
2522 (cp_parser*, cp_token *);
2523 static bool cp_parser_error_occurred
2524 (cp_parser *);
2525 static bool cp_parser_allow_gnu_extensions_p
2526 (cp_parser *);
2527 static bool cp_parser_is_pure_string_literal
2528 (cp_token *);
2529 static bool cp_parser_is_string_literal
2530 (cp_token *);
2531 static bool cp_parser_is_keyword
2532 (cp_token *, enum rid);
2533 static tree cp_parser_make_typename_type
2534 (cp_parser *, tree, location_t location);
2535 static cp_declarator * cp_parser_make_indirect_declarator
2536 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2537 static bool cp_parser_compound_literal_p
2538 (cp_parser *);
2539 static bool cp_parser_array_designator_p
2540 (cp_parser *);
2541 static bool cp_parser_skip_to_closing_square_bracket
2542 (cp_parser *);
2544 /* Returns nonzero if we are parsing tentatively. */
2546 static inline bool
2547 cp_parser_parsing_tentatively (cp_parser* parser)
2549 return parser->context->next != NULL;
2552 /* Returns nonzero if TOKEN is a string literal. */
2554 static bool
2555 cp_parser_is_pure_string_literal (cp_token* token)
2557 return (token->type == CPP_STRING ||
2558 token->type == CPP_STRING16 ||
2559 token->type == CPP_STRING32 ||
2560 token->type == CPP_WSTRING ||
2561 token->type == CPP_UTF8STRING);
2564 /* Returns nonzero if TOKEN is a string literal
2565 of a user-defined string literal. */
2567 static bool
2568 cp_parser_is_string_literal (cp_token* token)
2570 return (cp_parser_is_pure_string_literal (token) ||
2571 token->type == CPP_STRING_USERDEF ||
2572 token->type == CPP_STRING16_USERDEF ||
2573 token->type == CPP_STRING32_USERDEF ||
2574 token->type == CPP_WSTRING_USERDEF ||
2575 token->type == CPP_UTF8STRING_USERDEF);
2578 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2580 static bool
2581 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2583 return token->keyword == keyword;
2586 /* If not parsing tentatively, issue a diagnostic of the form
2587 FILE:LINE: MESSAGE before TOKEN
2588 where TOKEN is the next token in the input stream. MESSAGE
2589 (specified by the caller) is usually of the form "expected
2590 OTHER-TOKEN". */
2592 static void
2593 cp_parser_error (cp_parser* parser, const char* gmsgid)
2595 if (!cp_parser_simulate_error (parser))
2597 cp_token *token = cp_lexer_peek_token (parser->lexer);
2598 /* This diagnostic makes more sense if it is tagged to the line
2599 of the token we just peeked at. */
2600 cp_lexer_set_source_position_from_token (token);
2602 if (token->type == CPP_PRAGMA)
2604 error_at (token->location,
2605 "%<#pragma%> is not allowed here");
2606 cp_parser_skip_to_pragma_eol (parser, token);
2607 return;
2610 c_parse_error (gmsgid,
2611 /* Because c_parser_error does not understand
2612 CPP_KEYWORD, keywords are treated like
2613 identifiers. */
2614 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2615 token->u.value, token->flags);
2619 /* Issue an error about name-lookup failing. NAME is the
2620 IDENTIFIER_NODE DECL is the result of
2621 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2622 the thing that we hoped to find. */
2624 static void
2625 cp_parser_name_lookup_error (cp_parser* parser,
2626 tree name,
2627 tree decl,
2628 name_lookup_error desired,
2629 location_t location)
2631 /* If name lookup completely failed, tell the user that NAME was not
2632 declared. */
2633 if (decl == error_mark_node)
2635 if (parser->scope && parser->scope != global_namespace)
2636 error_at (location, "%<%E::%E%> has not been declared",
2637 parser->scope, name);
2638 else if (parser->scope == global_namespace)
2639 error_at (location, "%<::%E%> has not been declared", name);
2640 else if (parser->object_scope
2641 && !CLASS_TYPE_P (parser->object_scope))
2642 error_at (location, "request for member %qE in non-class type %qT",
2643 name, parser->object_scope);
2644 else if (parser->object_scope)
2645 error_at (location, "%<%T::%E%> has not been declared",
2646 parser->object_scope, name);
2647 else
2648 error_at (location, "%qE has not been declared", name);
2650 else if (parser->scope && parser->scope != global_namespace)
2652 switch (desired)
2654 case NLE_TYPE:
2655 error_at (location, "%<%E::%E%> is not a type",
2656 parser->scope, name);
2657 break;
2658 case NLE_CXX98:
2659 error_at (location, "%<%E::%E%> is not a class or namespace",
2660 parser->scope, name);
2661 break;
2662 case NLE_NOT_CXX98:
2663 error_at (location,
2664 "%<%E::%E%> is not a class, namespace, or enumeration",
2665 parser->scope, name);
2666 break;
2667 default:
2668 gcc_unreachable ();
2672 else if (parser->scope == global_namespace)
2674 switch (desired)
2676 case NLE_TYPE:
2677 error_at (location, "%<::%E%> is not a type", name);
2678 break;
2679 case NLE_CXX98:
2680 error_at (location, "%<::%E%> is not a class or namespace", name);
2681 break;
2682 case NLE_NOT_CXX98:
2683 error_at (location,
2684 "%<::%E%> is not a class, namespace, or enumeration",
2685 name);
2686 break;
2687 default:
2688 gcc_unreachable ();
2691 else
2693 switch (desired)
2695 case NLE_TYPE:
2696 error_at (location, "%qE is not a type", name);
2697 break;
2698 case NLE_CXX98:
2699 error_at (location, "%qE is not a class or namespace", name);
2700 break;
2701 case NLE_NOT_CXX98:
2702 error_at (location,
2703 "%qE is not a class, namespace, or enumeration", name);
2704 break;
2705 default:
2706 gcc_unreachable ();
2711 /* If we are parsing tentatively, remember that an error has occurred
2712 during this tentative parse. Returns true if the error was
2713 simulated; false if a message should be issued by the caller. */
2715 static bool
2716 cp_parser_simulate_error (cp_parser* parser)
2718 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2720 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2721 return true;
2723 return false;
2726 /* This function is called when a type is defined. If type
2727 definitions are forbidden at this point, an error message is
2728 issued. */
2730 static bool
2731 cp_parser_check_type_definition (cp_parser* parser)
2733 /* If types are forbidden here, issue a message. */
2734 if (parser->type_definition_forbidden_message)
2736 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2737 in the message need to be interpreted. */
2738 error (parser->type_definition_forbidden_message);
2739 return false;
2741 return true;
2744 /* This function is called when the DECLARATOR is processed. The TYPE
2745 was a type defined in the decl-specifiers. If it is invalid to
2746 define a type in the decl-specifiers for DECLARATOR, an error is
2747 issued. TYPE_LOCATION is the location of TYPE and is used
2748 for error reporting. */
2750 static void
2751 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2752 tree type, location_t type_location)
2754 /* [dcl.fct] forbids type definitions in return types.
2755 Unfortunately, it's not easy to know whether or not we are
2756 processing a return type until after the fact. */
2757 while (declarator
2758 && (declarator->kind == cdk_pointer
2759 || declarator->kind == cdk_reference
2760 || declarator->kind == cdk_ptrmem))
2761 declarator = declarator->declarator;
2762 if (declarator
2763 && declarator->kind == cdk_function)
2765 error_at (type_location,
2766 "new types may not be defined in a return type");
2767 inform (type_location,
2768 "(perhaps a semicolon is missing after the definition of %qT)",
2769 type);
2773 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2774 "<" in any valid C++ program. If the next token is indeed "<",
2775 issue a message warning the user about what appears to be an
2776 invalid attempt to form a template-id. LOCATION is the location
2777 of the type-specifier (TYPE) */
2779 static void
2780 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2781 tree type,
2782 enum tag_types tag_type,
2783 location_t location)
2785 cp_token_position start = 0;
2787 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2789 if (TYPE_P (type))
2790 error_at (location, "%qT is not a template", type);
2791 else if (identifier_p (type))
2793 if (tag_type != none_type)
2794 error_at (location, "%qE is not a class template", type);
2795 else
2796 error_at (location, "%qE is not a template", type);
2798 else
2799 error_at (location, "invalid template-id");
2800 /* Remember the location of the invalid "<". */
2801 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2802 start = cp_lexer_token_position (parser->lexer, true);
2803 /* Consume the "<". */
2804 cp_lexer_consume_token (parser->lexer);
2805 /* Parse the template arguments. */
2806 cp_parser_enclosed_template_argument_list (parser);
2807 /* Permanently remove the invalid template arguments so that
2808 this error message is not issued again. */
2809 if (start)
2810 cp_lexer_purge_tokens_after (parser->lexer, start);
2814 /* If parsing an integral constant-expression, issue an error message
2815 about the fact that THING appeared and return true. Otherwise,
2816 return false. In either case, set
2817 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2819 static bool
2820 cp_parser_non_integral_constant_expression (cp_parser *parser,
2821 non_integral_constant thing)
2823 parser->non_integral_constant_expression_p = true;
2824 if (parser->integral_constant_expression_p)
2826 if (!parser->allow_non_integral_constant_expression_p)
2828 const char *msg = NULL;
2829 switch (thing)
2831 case NIC_FLOAT:
2832 error ("floating-point literal "
2833 "cannot appear in a constant-expression");
2834 return true;
2835 case NIC_CAST:
2836 error ("a cast to a type other than an integral or "
2837 "enumeration type cannot appear in a "
2838 "constant-expression");
2839 return true;
2840 case NIC_TYPEID:
2841 error ("%<typeid%> operator "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_NCC:
2845 error ("non-constant compound literals "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_FUNC_CALL:
2849 error ("a function call "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_INC:
2853 error ("an increment "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_DEC:
2857 error ("an decrement "
2858 "cannot appear in a constant-expression");
2859 return true;
2860 case NIC_ARRAY_REF:
2861 error ("an array reference "
2862 "cannot appear in a constant-expression");
2863 return true;
2864 case NIC_ADDR_LABEL:
2865 error ("the address of a label "
2866 "cannot appear in a constant-expression");
2867 return true;
2868 case NIC_OVERLOADED:
2869 error ("calls to overloaded operators "
2870 "cannot appear in a constant-expression");
2871 return true;
2872 case NIC_ASSIGNMENT:
2873 error ("an assignment cannot appear in a constant-expression");
2874 return true;
2875 case NIC_COMMA:
2876 error ("a comma operator "
2877 "cannot appear in a constant-expression");
2878 return true;
2879 case NIC_CONSTRUCTOR:
2880 error ("a call to a constructor "
2881 "cannot appear in a constant-expression");
2882 return true;
2883 case NIC_TRANSACTION:
2884 error ("a transaction expression "
2885 "cannot appear in a constant-expression");
2886 return true;
2887 case NIC_THIS:
2888 msg = "this";
2889 break;
2890 case NIC_FUNC_NAME:
2891 msg = "__FUNCTION__";
2892 break;
2893 case NIC_PRETTY_FUNC:
2894 msg = "__PRETTY_FUNCTION__";
2895 break;
2896 case NIC_C99_FUNC:
2897 msg = "__func__";
2898 break;
2899 case NIC_VA_ARG:
2900 msg = "va_arg";
2901 break;
2902 case NIC_ARROW:
2903 msg = "->";
2904 break;
2905 case NIC_POINT:
2906 msg = ".";
2907 break;
2908 case NIC_STAR:
2909 msg = "*";
2910 break;
2911 case NIC_ADDR:
2912 msg = "&";
2913 break;
2914 case NIC_PREINCREMENT:
2915 msg = "++";
2916 break;
2917 case NIC_PREDECREMENT:
2918 msg = "--";
2919 break;
2920 case NIC_NEW:
2921 msg = "new";
2922 break;
2923 case NIC_DEL:
2924 msg = "delete";
2925 break;
2926 default:
2927 gcc_unreachable ();
2929 if (msg)
2930 error ("%qs cannot appear in a constant-expression", msg);
2931 return true;
2934 return false;
2937 /* Emit a diagnostic for an invalid type name. This function commits
2938 to the current active tentative parse, if any. (Otherwise, the
2939 problematic construct might be encountered again later, resulting
2940 in duplicate error messages.) LOCATION is the location of ID. */
2942 static void
2943 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2944 location_t location)
2946 tree decl, ambiguous_decls;
2947 cp_parser_commit_to_tentative_parse (parser);
2948 /* Try to lookup the identifier. */
2949 decl = cp_parser_lookup_name (parser, id, none_type,
2950 /*is_template=*/false,
2951 /*is_namespace=*/false,
2952 /*check_dependency=*/true,
2953 &ambiguous_decls, location);
2954 if (ambiguous_decls)
2955 /* If the lookup was ambiguous, an error will already have
2956 been issued. */
2957 return;
2958 /* If the lookup found a template-name, it means that the user forgot
2959 to specify an argument list. Emit a useful error message. */
2960 if (TREE_CODE (decl) == TEMPLATE_DECL)
2961 error_at (location,
2962 "invalid use of template-name %qE without an argument list",
2963 decl);
2964 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2965 error_at (location, "invalid use of destructor %qD as a type", id);
2966 else if (TREE_CODE (decl) == TYPE_DECL)
2967 /* Something like 'unsigned A a;' */
2968 error_at (location, "invalid combination of multiple type-specifiers");
2969 else if (!parser->scope)
2971 /* Issue an error message. */
2972 error_at (location, "%qE does not name a type", id);
2973 /* If we're in a template class, it's possible that the user was
2974 referring to a type from a base class. For example:
2976 template <typename T> struct A { typedef T X; };
2977 template <typename T> struct B : public A<T> { X x; };
2979 The user should have said "typename A<T>::X". */
2980 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2981 inform (location, "C++11 %<constexpr%> only available with "
2982 "-std=c++11 or -std=gnu++11");
2983 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2984 inform (location, "C++11 %<noexcept%> only available with "
2985 "-std=c++11 or -std=gnu++11");
2986 else if (cxx_dialect < cxx11
2987 && TREE_CODE (id) == IDENTIFIER_NODE
2988 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2989 inform (location, "C++11 %<thread_local%> only available with "
2990 "-std=c++11 or -std=gnu++11");
2991 else if (processing_template_decl && current_class_type
2992 && TYPE_BINFO (current_class_type))
2994 tree b;
2996 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2998 b = TREE_CHAIN (b))
3000 tree base_type = BINFO_TYPE (b);
3001 if (CLASS_TYPE_P (base_type)
3002 && dependent_type_p (base_type))
3004 tree field;
3005 /* Go from a particular instantiation of the
3006 template (which will have an empty TYPE_FIELDs),
3007 to the main version. */
3008 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3009 for (field = TYPE_FIELDS (base_type);
3010 field;
3011 field = DECL_CHAIN (field))
3012 if (TREE_CODE (field) == TYPE_DECL
3013 && DECL_NAME (field) == id)
3015 inform (location,
3016 "(perhaps %<typename %T::%E%> was intended)",
3017 BINFO_TYPE (b), id);
3018 break;
3020 if (field)
3021 break;
3026 /* Here we diagnose qualified-ids where the scope is actually correct,
3027 but the identifier does not resolve to a valid type name. */
3028 else if (parser->scope != error_mark_node)
3030 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3032 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3033 error_at (location_of (id),
3034 "%qE in namespace %qE does not name a template type",
3035 id, parser->scope);
3036 else
3037 error_at (location_of (id),
3038 "%qE in namespace %qE does not name a type",
3039 id, parser->scope);
3041 else if (CLASS_TYPE_P (parser->scope)
3042 && constructor_name_p (id, parser->scope))
3044 /* A<T>::A<T>() */
3045 error_at (location, "%<%T::%E%> names the constructor, not"
3046 " the type", parser->scope, id);
3047 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3048 error_at (location, "and %qT has no template constructors",
3049 parser->scope);
3051 else if (TYPE_P (parser->scope)
3052 && dependent_scope_p (parser->scope))
3053 error_at (location, "need %<typename%> before %<%T::%E%> because "
3054 "%qT is a dependent scope",
3055 parser->scope, id, parser->scope);
3056 else if (TYPE_P (parser->scope))
3058 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3059 error_at (location_of (id),
3060 "%qE in %q#T does not name a template type",
3061 id, parser->scope);
3062 else
3063 error_at (location_of (id),
3064 "%qE in %q#T does not name a type",
3065 id, parser->scope);
3067 else
3068 gcc_unreachable ();
3072 /* Check for a common situation where a type-name should be present,
3073 but is not, and issue a sensible error message. Returns true if an
3074 invalid type-name was detected.
3076 The situation handled by this function are variable declarations of the
3077 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3078 Usually, `ID' should name a type, but if we got here it means that it
3079 does not. We try to emit the best possible error message depending on
3080 how exactly the id-expression looks like. */
3082 static bool
3083 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3085 tree id;
3086 cp_token *token = cp_lexer_peek_token (parser->lexer);
3088 /* Avoid duplicate error about ambiguous lookup. */
3089 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3091 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3092 if (next->type == CPP_NAME && next->error_reported)
3093 goto out;
3096 cp_parser_parse_tentatively (parser);
3097 id = cp_parser_id_expression (parser,
3098 /*template_keyword_p=*/false,
3099 /*check_dependency_p=*/true,
3100 /*template_p=*/NULL,
3101 /*declarator_p=*/true,
3102 /*optional_p=*/false);
3103 /* If the next token is a (, this is a function with no explicit return
3104 type, i.e. constructor, destructor or conversion op. */
3105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3106 || TREE_CODE (id) == TYPE_DECL)
3108 cp_parser_abort_tentative_parse (parser);
3109 return false;
3111 if (!cp_parser_parse_definitely (parser))
3112 return false;
3114 /* Emit a diagnostic for the invalid type. */
3115 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3116 out:
3117 /* If we aren't in the middle of a declarator (i.e. in a
3118 parameter-declaration-clause), skip to the end of the declaration;
3119 there's no point in trying to process it. */
3120 if (!parser->in_declarator_p)
3121 cp_parser_skip_to_end_of_block_or_statement (parser);
3122 return true;
3125 /* Consume tokens up to, and including, the next non-nested closing `)'.
3126 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3127 are doing error recovery. Returns -1 if OR_COMMA is true and we
3128 found an unnested comma. */
3130 static int
3131 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3132 bool recovering,
3133 bool or_comma,
3134 bool consume_paren)
3136 unsigned paren_depth = 0;
3137 unsigned brace_depth = 0;
3138 unsigned square_depth = 0;
3140 if (recovering && !or_comma
3141 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3142 return 0;
3144 while (true)
3146 cp_token * token = cp_lexer_peek_token (parser->lexer);
3148 switch (token->type)
3150 case CPP_EOF:
3151 case CPP_PRAGMA_EOL:
3152 /* If we've run out of tokens, then there is no closing `)'. */
3153 return 0;
3155 /* This is good for lambda expression capture-lists. */
3156 case CPP_OPEN_SQUARE:
3157 ++square_depth;
3158 break;
3159 case CPP_CLOSE_SQUARE:
3160 if (!square_depth--)
3161 return 0;
3162 break;
3164 case CPP_SEMICOLON:
3165 /* This matches the processing in skip_to_end_of_statement. */
3166 if (!brace_depth)
3167 return 0;
3168 break;
3170 case CPP_OPEN_BRACE:
3171 ++brace_depth;
3172 break;
3173 case CPP_CLOSE_BRACE:
3174 if (!brace_depth--)
3175 return 0;
3176 break;
3178 case CPP_COMMA:
3179 if (recovering && or_comma && !brace_depth && !paren_depth
3180 && !square_depth)
3181 return -1;
3182 break;
3184 case CPP_OPEN_PAREN:
3185 if (!brace_depth)
3186 ++paren_depth;
3187 break;
3189 case CPP_CLOSE_PAREN:
3190 if (!brace_depth && !paren_depth--)
3192 if (consume_paren)
3193 cp_lexer_consume_token (parser->lexer);
3194 return 1;
3196 break;
3198 default:
3199 break;
3202 /* Consume the token. */
3203 cp_lexer_consume_token (parser->lexer);
3207 /* Consume tokens until we reach the end of the current statement.
3208 Normally, that will be just before consuming a `;'. However, if a
3209 non-nested `}' comes first, then we stop before consuming that. */
3211 static void
3212 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3214 unsigned nesting_depth = 0;
3216 /* Unwind generic function template scope if necessary. */
3217 if (parser->fully_implicit_function_template_p)
3218 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3220 while (true)
3222 cp_token *token = cp_lexer_peek_token (parser->lexer);
3224 switch (token->type)
3226 case CPP_EOF:
3227 case CPP_PRAGMA_EOL:
3228 /* If we've run out of tokens, stop. */
3229 return;
3231 case CPP_SEMICOLON:
3232 /* If the next token is a `;', we have reached the end of the
3233 statement. */
3234 if (!nesting_depth)
3235 return;
3236 break;
3238 case CPP_CLOSE_BRACE:
3239 /* If this is a non-nested '}', stop before consuming it.
3240 That way, when confronted with something like:
3242 { 3 + }
3244 we stop before consuming the closing '}', even though we
3245 have not yet reached a `;'. */
3246 if (nesting_depth == 0)
3247 return;
3249 /* If it is the closing '}' for a block that we have
3250 scanned, stop -- but only after consuming the token.
3251 That way given:
3253 void f g () { ... }
3254 typedef int I;
3256 we will stop after the body of the erroneously declared
3257 function, but before consuming the following `typedef'
3258 declaration. */
3259 if (--nesting_depth == 0)
3261 cp_lexer_consume_token (parser->lexer);
3262 return;
3265 case CPP_OPEN_BRACE:
3266 ++nesting_depth;
3267 break;
3269 default:
3270 break;
3273 /* Consume the token. */
3274 cp_lexer_consume_token (parser->lexer);
3278 /* This function is called at the end of a statement or declaration.
3279 If the next token is a semicolon, it is consumed; otherwise, error
3280 recovery is attempted. */
3282 static void
3283 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3285 /* Look for the trailing `;'. */
3286 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3288 /* If there is additional (erroneous) input, skip to the end of
3289 the statement. */
3290 cp_parser_skip_to_end_of_statement (parser);
3291 /* If the next token is now a `;', consume it. */
3292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3293 cp_lexer_consume_token (parser->lexer);
3297 /* Skip tokens until we have consumed an entire block, or until we
3298 have consumed a non-nested `;'. */
3300 static void
3301 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3303 int nesting_depth = 0;
3305 /* Unwind generic function template scope if necessary. */
3306 if (parser->fully_implicit_function_template_p)
3307 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3309 while (nesting_depth >= 0)
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 switch (token->type)
3315 case CPP_EOF:
3316 case CPP_PRAGMA_EOL:
3317 /* If we've run out of tokens, stop. */
3318 return;
3320 case CPP_SEMICOLON:
3321 /* Stop if this is an unnested ';'. */
3322 if (!nesting_depth)
3323 nesting_depth = -1;
3324 break;
3326 case CPP_CLOSE_BRACE:
3327 /* Stop if this is an unnested '}', or closes the outermost
3328 nesting level. */
3329 nesting_depth--;
3330 if (nesting_depth < 0)
3331 return;
3332 if (!nesting_depth)
3333 nesting_depth = -1;
3334 break;
3336 case CPP_OPEN_BRACE:
3337 /* Nest. */
3338 nesting_depth++;
3339 break;
3341 default:
3342 break;
3345 /* Consume the token. */
3346 cp_lexer_consume_token (parser->lexer);
3350 /* Skip tokens until a non-nested closing curly brace is the next
3351 token, or there are no more tokens. Return true in the first case,
3352 false otherwise. */
3354 static bool
3355 cp_parser_skip_to_closing_brace (cp_parser *parser)
3357 unsigned nesting_depth = 0;
3359 while (true)
3361 cp_token *token = cp_lexer_peek_token (parser->lexer);
3363 switch (token->type)
3365 case CPP_EOF:
3366 case CPP_PRAGMA_EOL:
3367 /* If we've run out of tokens, stop. */
3368 return false;
3370 case CPP_CLOSE_BRACE:
3371 /* If the next token is a non-nested `}', then we have reached
3372 the end of the current block. */
3373 if (nesting_depth-- == 0)
3374 return true;
3375 break;
3377 case CPP_OPEN_BRACE:
3378 /* If it the next token is a `{', then we are entering a new
3379 block. Consume the entire block. */
3380 ++nesting_depth;
3381 break;
3383 default:
3384 break;
3387 /* Consume the token. */
3388 cp_lexer_consume_token (parser->lexer);
3392 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3393 parameter is the PRAGMA token, allowing us to purge the entire pragma
3394 sequence. */
3396 static void
3397 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3399 cp_token *token;
3401 parser->lexer->in_pragma = false;
3404 token = cp_lexer_consume_token (parser->lexer);
3405 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3407 /* Ensure that the pragma is not parsed again. */
3408 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3411 /* Require pragma end of line, resyncing with it as necessary. The
3412 arguments are as for cp_parser_skip_to_pragma_eol. */
3414 static void
3415 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3417 parser->lexer->in_pragma = false;
3418 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3422 /* This is a simple wrapper around make_typename_type. When the id is
3423 an unresolved identifier node, we can provide a superior diagnostic
3424 using cp_parser_diagnose_invalid_type_name. */
3426 static tree
3427 cp_parser_make_typename_type (cp_parser *parser, tree id,
3428 location_t id_location)
3430 tree result;
3431 if (identifier_p (id))
3433 result = make_typename_type (parser->scope, id, typename_type,
3434 /*complain=*/tf_none);
3435 if (result == error_mark_node)
3436 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3437 return result;
3439 return make_typename_type (parser->scope, id, typename_type, tf_error);
3442 /* This is a wrapper around the
3443 make_{pointer,ptrmem,reference}_declarator functions that decides
3444 which one to call based on the CODE and CLASS_TYPE arguments. The
3445 CODE argument should be one of the values returned by
3446 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3447 appertain to the pointer or reference. */
3449 static cp_declarator *
3450 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3451 cp_cv_quals cv_qualifiers,
3452 cp_declarator *target,
3453 tree attributes)
3455 if (code == ERROR_MARK)
3456 return cp_error_declarator;
3458 if (code == INDIRECT_REF)
3459 if (class_type == NULL_TREE)
3460 return make_pointer_declarator (cv_qualifiers, target, attributes);
3461 else
3462 return make_ptrmem_declarator (cv_qualifiers, class_type,
3463 target, attributes);
3464 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3465 return make_reference_declarator (cv_qualifiers, target,
3466 false, attributes);
3467 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3468 return make_reference_declarator (cv_qualifiers, target,
3469 true, attributes);
3470 gcc_unreachable ();
3473 /* Create a new C++ parser. */
3475 static cp_parser *
3476 cp_parser_new (void)
3478 cp_parser *parser;
3479 cp_lexer *lexer;
3480 unsigned i;
3482 /* cp_lexer_new_main is called before doing GC allocation because
3483 cp_lexer_new_main might load a PCH file. */
3484 lexer = cp_lexer_new_main ();
3486 /* Initialize the binops_by_token so that we can get the tree
3487 directly from the token. */
3488 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3489 binops_by_token[binops[i].token_type] = binops[i];
3491 parser = ggc_cleared_alloc<cp_parser> ();
3492 parser->lexer = lexer;
3493 parser->context = cp_parser_context_new (NULL);
3495 /* For now, we always accept GNU extensions. */
3496 parser->allow_gnu_extensions_p = 1;
3498 /* The `>' token is a greater-than operator, not the end of a
3499 template-id. */
3500 parser->greater_than_is_operator_p = true;
3502 parser->default_arg_ok_p = true;
3504 /* We are not parsing a constant-expression. */
3505 parser->integral_constant_expression_p = false;
3506 parser->allow_non_integral_constant_expression_p = false;
3507 parser->non_integral_constant_expression_p = false;
3509 /* Local variable names are not forbidden. */
3510 parser->local_variables_forbidden_p = false;
3512 /* We are not processing an `extern "C"' declaration. */
3513 parser->in_unbraced_linkage_specification_p = false;
3515 /* We are not processing a declarator. */
3516 parser->in_declarator_p = false;
3518 /* We are not processing a template-argument-list. */
3519 parser->in_template_argument_list_p = false;
3521 /* We are not in an iteration statement. */
3522 parser->in_statement = 0;
3524 /* We are not in a switch statement. */
3525 parser->in_switch_statement_p = false;
3527 /* We are not parsing a type-id inside an expression. */
3528 parser->in_type_id_in_expr_p = false;
3530 /* Declarations aren't implicitly extern "C". */
3531 parser->implicit_extern_c = false;
3533 /* String literals should be translated to the execution character set. */
3534 parser->translate_strings_p = true;
3536 /* We are not parsing a function body. */
3537 parser->in_function_body = false;
3539 /* We can correct until told otherwise. */
3540 parser->colon_corrects_to_scope_p = true;
3542 /* The unparsed function queue is empty. */
3543 push_unparsed_function_queues (parser);
3545 /* There are no classes being defined. */
3546 parser->num_classes_being_defined = 0;
3548 /* No template parameters apply. */
3549 parser->num_template_parameter_lists = 0;
3551 /* Not declaring an implicit function template. */
3552 parser->auto_is_implicit_function_template_parm_p = false;
3553 parser->fully_implicit_function_template_p = false;
3554 parser->implicit_template_parms = 0;
3555 parser->implicit_template_scope = 0;
3557 return parser;
3560 /* Create a cp_lexer structure which will emit the tokens in CACHE
3561 and push it onto the parser's lexer stack. This is used for delayed
3562 parsing of in-class method bodies and default arguments, and should
3563 not be confused with tentative parsing. */
3564 static void
3565 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3567 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3568 lexer->next = parser->lexer;
3569 parser->lexer = lexer;
3571 /* Move the current source position to that of the first token in the
3572 new lexer. */
3573 cp_lexer_set_source_position_from_token (lexer->next_token);
3576 /* Pop the top lexer off the parser stack. This is never used for the
3577 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3578 static void
3579 cp_parser_pop_lexer (cp_parser *parser)
3581 cp_lexer *lexer = parser->lexer;
3582 parser->lexer = lexer->next;
3583 cp_lexer_destroy (lexer);
3585 /* Put the current source position back where it was before this
3586 lexer was pushed. */
3587 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3590 /* Lexical conventions [gram.lex] */
3592 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3593 identifier. */
3595 static tree
3596 cp_parser_identifier (cp_parser* parser)
3598 cp_token *token;
3600 /* Look for the identifier. */
3601 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3602 /* Return the value. */
3603 return token ? token->u.value : error_mark_node;
3606 /* Parse a sequence of adjacent string constants. Returns a
3607 TREE_STRING representing the combined, nul-terminated string
3608 constant. If TRANSLATE is true, translate the string to the
3609 execution character set. If WIDE_OK is true, a wide string is
3610 invalid here.
3612 C++98 [lex.string] says that if a narrow string literal token is
3613 adjacent to a wide string literal token, the behavior is undefined.
3614 However, C99 6.4.5p4 says that this results in a wide string literal.
3615 We follow C99 here, for consistency with the C front end.
3617 This code is largely lifted from lex_string() in c-lex.c.
3619 FUTURE: ObjC++ will need to handle @-strings here. */
3620 static tree
3621 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3622 bool lookup_udlit = true)
3624 tree value;
3625 size_t count;
3626 struct obstack str_ob;
3627 cpp_string str, istr, *strs;
3628 cp_token *tok;
3629 enum cpp_ttype type, curr_type;
3630 int have_suffix_p = 0;
3631 tree string_tree;
3632 tree suffix_id = NULL_TREE;
3633 bool curr_tok_is_userdef_p = false;
3635 tok = cp_lexer_peek_token (parser->lexer);
3636 if (!cp_parser_is_string_literal (tok))
3638 cp_parser_error (parser, "expected string-literal");
3639 return error_mark_node;
3642 if (cpp_userdef_string_p (tok->type))
3644 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3645 curr_type = cpp_userdef_string_remove_type (tok->type);
3646 curr_tok_is_userdef_p = true;
3648 else
3650 string_tree = tok->u.value;
3651 curr_type = tok->type;
3653 type = curr_type;
3655 /* Try to avoid the overhead of creating and destroying an obstack
3656 for the common case of just one string. */
3657 if (!cp_parser_is_string_literal
3658 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3660 cp_lexer_consume_token (parser->lexer);
3662 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3663 str.len = TREE_STRING_LENGTH (string_tree);
3664 count = 1;
3666 if (curr_tok_is_userdef_p)
3668 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3669 have_suffix_p = 1;
3670 curr_type = cpp_userdef_string_remove_type (tok->type);
3672 else
3673 curr_type = tok->type;
3675 strs = &str;
3677 else
3679 gcc_obstack_init (&str_ob);
3680 count = 0;
3684 cp_lexer_consume_token (parser->lexer);
3685 count++;
3686 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3687 str.len = TREE_STRING_LENGTH (string_tree);
3689 if (curr_tok_is_userdef_p)
3691 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3692 if (have_suffix_p == 0)
3694 suffix_id = curr_suffix_id;
3695 have_suffix_p = 1;
3697 else if (have_suffix_p == 1
3698 && curr_suffix_id != suffix_id)
3700 error ("inconsistent user-defined literal suffixes"
3701 " %qD and %qD in string literal",
3702 suffix_id, curr_suffix_id);
3703 have_suffix_p = -1;
3705 curr_type = cpp_userdef_string_remove_type (tok->type);
3707 else
3708 curr_type = tok->type;
3710 if (type != curr_type)
3712 if (type == CPP_STRING)
3713 type = curr_type;
3714 else if (curr_type != CPP_STRING)
3715 error_at (tok->location,
3716 "unsupported non-standard concatenation "
3717 "of string literals");
3720 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3722 tok = cp_lexer_peek_token (parser->lexer);
3723 if (cpp_userdef_string_p (tok->type))
3725 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3726 curr_type = cpp_userdef_string_remove_type (tok->type);
3727 curr_tok_is_userdef_p = true;
3729 else
3731 string_tree = tok->u.value;
3732 curr_type = tok->type;
3733 curr_tok_is_userdef_p = false;
3736 while (cp_parser_is_string_literal (tok));
3738 strs = (cpp_string *) obstack_finish (&str_ob);
3741 if (type != CPP_STRING && !wide_ok)
3743 cp_parser_error (parser, "a wide string is invalid in this context");
3744 type = CPP_STRING;
3747 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3748 (parse_in, strs, count, &istr, type))
3750 value = build_string (istr.len, (const char *)istr.text);
3751 free (CONST_CAST (unsigned char *, istr.text));
3753 switch (type)
3755 default:
3756 case CPP_STRING:
3757 case CPP_UTF8STRING:
3758 TREE_TYPE (value) = char_array_type_node;
3759 break;
3760 case CPP_STRING16:
3761 TREE_TYPE (value) = char16_array_type_node;
3762 break;
3763 case CPP_STRING32:
3764 TREE_TYPE (value) = char32_array_type_node;
3765 break;
3766 case CPP_WSTRING:
3767 TREE_TYPE (value) = wchar_array_type_node;
3768 break;
3771 value = fix_string_type (value);
3773 if (have_suffix_p)
3775 tree literal = build_userdef_literal (suffix_id, value,
3776 OT_NONE, NULL_TREE);
3777 if (lookup_udlit)
3778 value = cp_parser_userdef_string_literal (literal);
3779 else
3780 value = literal;
3783 else
3784 /* cpp_interpret_string has issued an error. */
3785 value = error_mark_node;
3787 if (count > 1)
3788 obstack_free (&str_ob, 0);
3790 return value;
3793 /* Look up a literal operator with the name and the exact arguments. */
3795 static tree
3796 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3798 tree decl, fns;
3799 decl = lookup_name (name);
3800 if (!decl || !is_overloaded_fn (decl))
3801 return error_mark_node;
3803 for (fns = decl; fns; fns = OVL_NEXT (fns))
3805 unsigned int ix;
3806 bool found = true;
3807 tree fn = OVL_CURRENT (fns);
3808 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3809 if (parmtypes != NULL_TREE)
3811 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3812 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3814 tree tparm = TREE_VALUE (parmtypes);
3815 tree targ = TREE_TYPE ((*args)[ix]);
3816 bool ptr = TYPE_PTR_P (tparm);
3817 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3818 if ((ptr || arr || !same_type_p (tparm, targ))
3819 && (!ptr || !arr
3820 || !same_type_p (TREE_TYPE (tparm),
3821 TREE_TYPE (targ))))
3822 found = false;
3824 if (found
3825 && ix == vec_safe_length (args)
3826 /* May be this should be sufficient_parms_p instead,
3827 depending on how exactly should user-defined literals
3828 work in presence of default arguments on the literal
3829 operator parameters. */
3830 && parmtypes == void_list_node)
3831 return fn;
3835 return error_mark_node;
3838 /* Parse a user-defined char constant. Returns a call to a user-defined
3839 literal operator taking the character as an argument. */
3841 static tree
3842 cp_parser_userdef_char_literal (cp_parser *parser)
3844 cp_token *token = cp_lexer_consume_token (parser->lexer);
3845 tree literal = token->u.value;
3846 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3847 tree value = USERDEF_LITERAL_VALUE (literal);
3848 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3849 tree decl, result;
3851 /* Build up a call to the user-defined operator */
3852 /* Lookup the name we got back from the id-expression. */
3853 vec<tree, va_gc> *args = make_tree_vector ();
3854 vec_safe_push (args, value);
3855 decl = lookup_literal_operator (name, args);
3856 if (!decl || decl == error_mark_node)
3858 error ("unable to find character literal operator %qD with %qT argument",
3859 name, TREE_TYPE (value));
3860 release_tree_vector (args);
3861 return error_mark_node;
3863 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3864 release_tree_vector (args);
3865 if (result != error_mark_node)
3866 return result;
3868 error ("unable to find character literal operator %qD with %qT argument",
3869 name, TREE_TYPE (value));
3870 return error_mark_node;
3873 /* A subroutine of cp_parser_userdef_numeric_literal to
3874 create a char... template parameter pack from a string node. */
3876 static tree
3877 make_char_string_pack (tree value)
3879 tree charvec;
3880 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3881 const char *str = TREE_STRING_POINTER (value);
3882 int i, len = TREE_STRING_LENGTH (value) - 1;
3883 tree argvec = make_tree_vec (1);
3885 /* Fill in CHARVEC with all of the parameters. */
3886 charvec = make_tree_vec (len);
3887 for (i = 0; i < len; ++i)
3888 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3890 /* Build the argument packs. */
3891 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3892 TREE_TYPE (argpack) = char_type_node;
3894 TREE_VEC_ELT (argvec, 0) = argpack;
3896 return argvec;
3899 /* A subroutine of cp_parser_userdef_numeric_literal to
3900 create a char... template parameter pack from a string node. */
3902 static tree
3903 make_string_pack (tree value)
3905 tree charvec;
3906 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3907 const unsigned char *str
3908 = (const unsigned char *) TREE_STRING_POINTER (value);
3909 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3910 int len = TREE_STRING_LENGTH (value) / sz - 1;
3911 tree argvec = make_tree_vec (2);
3913 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3914 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3916 /* First template parm is character type. */
3917 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3919 /* Fill in CHARVEC with all of the parameters. */
3920 charvec = make_tree_vec (len);
3921 for (int i = 0; i < len; ++i)
3922 TREE_VEC_ELT (charvec, i)
3923 = double_int_to_tree (str_char_type_node,
3924 double_int::from_buffer (str + i * sz, sz));
3926 /* Build the argument packs. */
3927 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3928 TREE_TYPE (argpack) = str_char_type_node;
3930 TREE_VEC_ELT (argvec, 1) = argpack;
3932 return argvec;
3935 /* Parse a user-defined numeric constant. returns a call to a user-defined
3936 literal operator. */
3938 static tree
3939 cp_parser_userdef_numeric_literal (cp_parser *parser)
3941 cp_token *token = cp_lexer_consume_token (parser->lexer);
3942 tree literal = token->u.value;
3943 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3944 tree value = USERDEF_LITERAL_VALUE (literal);
3945 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3946 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3947 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3948 tree decl, result;
3949 vec<tree, va_gc> *args;
3951 /* Look for a literal operator taking the exact type of numeric argument
3952 as the literal value. */
3953 args = make_tree_vector ();
3954 vec_safe_push (args, value);
3955 decl = lookup_literal_operator (name, args);
3956 if (decl && decl != error_mark_node)
3958 result = finish_call_expr (decl, &args, false, true, tf_none);
3959 if (result != error_mark_node)
3961 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3962 warning_at (token->location, OPT_Woverflow,
3963 "integer literal exceeds range of %qT type",
3964 long_long_unsigned_type_node);
3965 else
3967 if (overflow > 0)
3968 warning_at (token->location, OPT_Woverflow,
3969 "floating literal exceeds range of %qT type",
3970 long_double_type_node);
3971 else if (overflow < 0)
3972 warning_at (token->location, OPT_Woverflow,
3973 "floating literal truncated to zero");
3975 release_tree_vector (args);
3976 return result;
3979 release_tree_vector (args);
3981 /* If the numeric argument didn't work, look for a raw literal
3982 operator taking a const char* argument consisting of the number
3983 in string format. */
3984 args = make_tree_vector ();
3985 vec_safe_push (args, num_string);
3986 decl = lookup_literal_operator (name, args);
3987 if (decl && decl != error_mark_node)
3989 result = finish_call_expr (decl, &args, false, true, tf_none);
3990 if (result != error_mark_node)
3992 release_tree_vector (args);
3993 return result;
3996 release_tree_vector (args);
3998 /* If the raw literal didn't work, look for a non-type template
3999 function with parameter pack char.... Call the function with
4000 template parameter characters representing the number. */
4001 args = make_tree_vector ();
4002 decl = lookup_literal_operator (name, args);
4003 if (decl && decl != error_mark_node)
4005 tree tmpl_args = make_char_string_pack (num_string);
4006 decl = lookup_template_function (decl, tmpl_args);
4007 result = finish_call_expr (decl, &args, false, true, tf_none);
4008 if (result != error_mark_node)
4010 release_tree_vector (args);
4011 return result;
4014 release_tree_vector (args);
4016 error ("unable to find numeric literal operator %qD", name);
4017 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4018 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4019 "to enable more built-in suffixes");
4020 return error_mark_node;
4023 /* Parse a user-defined string constant. Returns a call to a user-defined
4024 literal operator taking a character pointer and the length of the string
4025 as arguments. */
4027 static tree
4028 cp_parser_userdef_string_literal (tree literal)
4030 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4031 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4032 tree value = USERDEF_LITERAL_VALUE (literal);
4033 int len = TREE_STRING_LENGTH (value)
4034 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4035 tree decl, result;
4036 vec<tree, va_gc> *args;
4038 /* Look for a template function with typename parameter CharT
4039 and parameter pack CharT... Call the function with
4040 template parameter characters representing the string. */
4041 args = make_tree_vector ();
4042 decl = lookup_literal_operator (name, args);
4043 if (decl && decl != error_mark_node)
4045 tree tmpl_args = make_string_pack (value);
4046 decl = lookup_template_function (decl, tmpl_args);
4047 result = finish_call_expr (decl, &args, false, true, tf_none);
4048 if (result != error_mark_node)
4050 release_tree_vector (args);
4051 return result;
4054 release_tree_vector (args);
4056 /* Build up a call to the user-defined operator */
4057 /* Lookup the name we got back from the id-expression. */
4058 args = make_tree_vector ();
4059 vec_safe_push (args, value);
4060 vec_safe_push (args, build_int_cst (size_type_node, len));
4061 decl = lookup_name (name);
4062 if (!decl || decl == error_mark_node)
4064 error ("unable to find string literal operator %qD", name);
4065 release_tree_vector (args);
4066 return error_mark_node;
4068 result = finish_call_expr (decl, &args, false, true, tf_none);
4069 release_tree_vector (args);
4070 if (result != error_mark_node)
4071 return result;
4073 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4074 name, TREE_TYPE (value), size_type_node);
4075 return error_mark_node;
4079 /* Basic concepts [gram.basic] */
4081 /* Parse a translation-unit.
4083 translation-unit:
4084 declaration-seq [opt]
4086 Returns TRUE if all went well. */
4088 static bool
4089 cp_parser_translation_unit (cp_parser* parser)
4091 /* The address of the first non-permanent object on the declarator
4092 obstack. */
4093 static void *declarator_obstack_base;
4095 bool success;
4097 /* Create the declarator obstack, if necessary. */
4098 if (!cp_error_declarator)
4100 gcc_obstack_init (&declarator_obstack);
4101 /* Create the error declarator. */
4102 cp_error_declarator = make_declarator (cdk_error);
4103 /* Create the empty parameter list. */
4104 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4105 /* Remember where the base of the declarator obstack lies. */
4106 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4109 cp_parser_declaration_seq_opt (parser);
4111 /* If there are no tokens left then all went well. */
4112 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4114 /* Get rid of the token array; we don't need it any more. */
4115 cp_lexer_destroy (parser->lexer);
4116 parser->lexer = NULL;
4118 /* This file might have been a context that's implicitly extern
4119 "C". If so, pop the lang context. (Only relevant for PCH.) */
4120 if (parser->implicit_extern_c)
4122 pop_lang_context ();
4123 parser->implicit_extern_c = false;
4126 /* Finish up. */
4127 finish_translation_unit ();
4129 success = true;
4131 else
4133 cp_parser_error (parser, "expected declaration");
4134 success = false;
4137 /* Make sure the declarator obstack was fully cleaned up. */
4138 gcc_assert (obstack_next_free (&declarator_obstack)
4139 == declarator_obstack_base);
4141 /* All went well. */
4142 return success;
4145 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4146 decltype context. */
4148 static inline tsubst_flags_t
4149 complain_flags (bool decltype_p)
4151 tsubst_flags_t complain = tf_warning_or_error;
4152 if (decltype_p)
4153 complain |= tf_decltype;
4154 return complain;
4157 /* We're about to parse a collection of statements. If we're currently
4158 parsing tentatively, set up a firewall so that any nested
4159 cp_parser_commit_to_tentative_parse won't affect the current context. */
4161 static cp_token_position
4162 cp_parser_start_tentative_firewall (cp_parser *parser)
4164 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4165 return 0;
4167 cp_parser_parse_tentatively (parser);
4168 cp_parser_commit_to_topmost_tentative_parse (parser);
4169 return cp_lexer_token_position (parser->lexer, false);
4172 /* We've finished parsing the collection of statements. Wrap up the
4173 firewall and replace the relevant tokens with the parsed form. */
4175 static void
4176 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4177 tree expr)
4179 if (!start)
4180 return;
4182 /* Finish the firewall level. */
4183 cp_parser_parse_definitely (parser);
4184 /* And remember the result of the parse for when we try again. */
4185 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4186 token->type = CPP_PREPARSED_EXPR;
4187 token->u.value = expr;
4188 token->keyword = RID_MAX;
4189 cp_lexer_purge_tokens_after (parser->lexer, start);
4192 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4193 enclosing parentheses. */
4195 static tree
4196 cp_parser_statement_expr (cp_parser *parser)
4198 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4200 /* Consume the '('. */
4201 cp_lexer_consume_token (parser->lexer);
4202 /* Start the statement-expression. */
4203 tree expr = begin_stmt_expr ();
4204 /* Parse the compound-statement. */
4205 cp_parser_compound_statement (parser, expr, false, false);
4206 /* Finish up. */
4207 expr = finish_stmt_expr (expr, false);
4208 /* Consume the ')'. */
4209 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4210 cp_parser_skip_to_end_of_statement (parser);
4212 cp_parser_end_tentative_firewall (parser, start, expr);
4213 return expr;
4216 /* Expressions [gram.expr] */
4218 /* Parse a primary-expression.
4220 primary-expression:
4221 literal
4222 this
4223 ( expression )
4224 id-expression
4225 lambda-expression (C++11)
4227 GNU Extensions:
4229 primary-expression:
4230 ( compound-statement )
4231 __builtin_va_arg ( assignment-expression , type-id )
4232 __builtin_offsetof ( type-id , offsetof-expression )
4234 C++ Extensions:
4235 __has_nothrow_assign ( type-id )
4236 __has_nothrow_constructor ( type-id )
4237 __has_nothrow_copy ( type-id )
4238 __has_trivial_assign ( type-id )
4239 __has_trivial_constructor ( type-id )
4240 __has_trivial_copy ( type-id )
4241 __has_trivial_destructor ( type-id )
4242 __has_virtual_destructor ( type-id )
4243 __is_abstract ( type-id )
4244 __is_base_of ( type-id , type-id )
4245 __is_class ( type-id )
4246 __is_empty ( type-id )
4247 __is_enum ( type-id )
4248 __is_final ( type-id )
4249 __is_literal_type ( type-id )
4250 __is_pod ( type-id )
4251 __is_polymorphic ( type-id )
4252 __is_std_layout ( type-id )
4253 __is_trivial ( type-id )
4254 __is_union ( type-id )
4256 Objective-C++ Extension:
4258 primary-expression:
4259 objc-expression
4261 literal:
4262 __null
4264 ADDRESS_P is true iff this expression was immediately preceded by
4265 "&" and therefore might denote a pointer-to-member. CAST_P is true
4266 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4267 true iff this expression is a template argument.
4269 Returns a representation of the expression. Upon return, *IDK
4270 indicates what kind of id-expression (if any) was present. */
4272 static tree
4273 cp_parser_primary_expression (cp_parser *parser,
4274 bool address_p,
4275 bool cast_p,
4276 bool template_arg_p,
4277 bool decltype_p,
4278 cp_id_kind *idk)
4280 cp_token *token = NULL;
4282 /* Assume the primary expression is not an id-expression. */
4283 *idk = CP_ID_KIND_NONE;
4285 /* Peek at the next token. */
4286 token = cp_lexer_peek_token (parser->lexer);
4287 switch ((int) token->type)
4289 /* literal:
4290 integer-literal
4291 character-literal
4292 floating-literal
4293 string-literal
4294 boolean-literal
4295 pointer-literal
4296 user-defined-literal */
4297 case CPP_CHAR:
4298 case CPP_CHAR16:
4299 case CPP_CHAR32:
4300 case CPP_WCHAR:
4301 case CPP_NUMBER:
4302 case CPP_PREPARSED_EXPR:
4303 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4304 return cp_parser_userdef_numeric_literal (parser);
4305 token = cp_lexer_consume_token (parser->lexer);
4306 if (TREE_CODE (token->u.value) == FIXED_CST)
4308 error_at (token->location,
4309 "fixed-point types not supported in C++");
4310 return error_mark_node;
4312 /* Floating-point literals are only allowed in an integral
4313 constant expression if they are cast to an integral or
4314 enumeration type. */
4315 if (TREE_CODE (token->u.value) == REAL_CST
4316 && parser->integral_constant_expression_p
4317 && pedantic)
4319 /* CAST_P will be set even in invalid code like "int(2.7 +
4320 ...)". Therefore, we have to check that the next token
4321 is sure to end the cast. */
4322 if (cast_p)
4324 cp_token *next_token;
4326 next_token = cp_lexer_peek_token (parser->lexer);
4327 if (/* The comma at the end of an
4328 enumerator-definition. */
4329 next_token->type != CPP_COMMA
4330 /* The curly brace at the end of an enum-specifier. */
4331 && next_token->type != CPP_CLOSE_BRACE
4332 /* The end of a statement. */
4333 && next_token->type != CPP_SEMICOLON
4334 /* The end of the cast-expression. */
4335 && next_token->type != CPP_CLOSE_PAREN
4336 /* The end of an array bound. */
4337 && next_token->type != CPP_CLOSE_SQUARE
4338 /* The closing ">" in a template-argument-list. */
4339 && (next_token->type != CPP_GREATER
4340 || parser->greater_than_is_operator_p)
4341 /* C++0x only: A ">>" treated like two ">" tokens,
4342 in a template-argument-list. */
4343 && (next_token->type != CPP_RSHIFT
4344 || (cxx_dialect == cxx98)
4345 || parser->greater_than_is_operator_p))
4346 cast_p = false;
4349 /* If we are within a cast, then the constraint that the
4350 cast is to an integral or enumeration type will be
4351 checked at that point. If we are not within a cast, then
4352 this code is invalid. */
4353 if (!cast_p)
4354 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4356 return token->u.value;
4358 case CPP_CHAR_USERDEF:
4359 case CPP_CHAR16_USERDEF:
4360 case CPP_CHAR32_USERDEF:
4361 case CPP_WCHAR_USERDEF:
4362 return cp_parser_userdef_char_literal (parser);
4364 case CPP_STRING:
4365 case CPP_STRING16:
4366 case CPP_STRING32:
4367 case CPP_WSTRING:
4368 case CPP_UTF8STRING:
4369 case CPP_STRING_USERDEF:
4370 case CPP_STRING16_USERDEF:
4371 case CPP_STRING32_USERDEF:
4372 case CPP_WSTRING_USERDEF:
4373 case CPP_UTF8STRING_USERDEF:
4374 /* ??? Should wide strings be allowed when parser->translate_strings_p
4375 is false (i.e. in attributes)? If not, we can kill the third
4376 argument to cp_parser_string_literal. */
4377 return cp_parser_string_literal (parser,
4378 parser->translate_strings_p,
4379 true);
4381 case CPP_OPEN_PAREN:
4382 /* If we see `( { ' then we are looking at the beginning of
4383 a GNU statement-expression. */
4384 if (cp_parser_allow_gnu_extensions_p (parser)
4385 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4387 /* Statement-expressions are not allowed by the standard. */
4388 pedwarn (token->location, OPT_Wpedantic,
4389 "ISO C++ forbids braced-groups within expressions");
4391 /* And they're not allowed outside of a function-body; you
4392 cannot, for example, write:
4394 int i = ({ int j = 3; j + 1; });
4396 at class or namespace scope. */
4397 if (!parser->in_function_body
4398 || parser->in_template_argument_list_p)
4400 error_at (token->location,
4401 "statement-expressions are not allowed outside "
4402 "functions nor in template-argument lists");
4403 cp_parser_skip_to_end_of_block_or_statement (parser);
4404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4405 cp_lexer_consume_token (parser->lexer);
4406 return error_mark_node;
4408 else
4409 return cp_parser_statement_expr (parser);
4411 /* Otherwise it's a normal parenthesized expression. */
4413 tree expr;
4414 bool saved_greater_than_is_operator_p;
4416 /* Consume the `('. */
4417 cp_lexer_consume_token (parser->lexer);
4418 /* Within a parenthesized expression, a `>' token is always
4419 the greater-than operator. */
4420 saved_greater_than_is_operator_p
4421 = parser->greater_than_is_operator_p;
4422 parser->greater_than_is_operator_p = true;
4424 /* Parse the parenthesized expression. */
4425 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4426 /* Let the front end know that this expression was
4427 enclosed in parentheses. This matters in case, for
4428 example, the expression is of the form `A::B', since
4429 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4430 not. */
4431 expr = finish_parenthesized_expr (expr);
4432 /* DR 705: Wrapping an unqualified name in parentheses
4433 suppresses arg-dependent lookup. We want to pass back
4434 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4435 (c++/37862), but none of the others. */
4436 if (*idk != CP_ID_KIND_QUALIFIED)
4437 *idk = CP_ID_KIND_NONE;
4439 /* The `>' token might be the end of a template-id or
4440 template-parameter-list now. */
4441 parser->greater_than_is_operator_p
4442 = saved_greater_than_is_operator_p;
4443 /* Consume the `)'. */
4444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4445 cp_parser_skip_to_end_of_statement (parser);
4447 return expr;
4450 case CPP_OPEN_SQUARE:
4452 if (c_dialect_objc ())
4454 /* We might have an Objective-C++ message. */
4455 cp_parser_parse_tentatively (parser);
4456 tree msg = cp_parser_objc_message_expression (parser);
4457 /* If that works out, we're done ... */
4458 if (cp_parser_parse_definitely (parser))
4459 return msg;
4460 /* ... else, fall though to see if it's a lambda. */
4462 tree lam = cp_parser_lambda_expression (parser);
4463 /* Don't warn about a failed tentative parse. */
4464 if (cp_parser_error_occurred (parser))
4465 return error_mark_node;
4466 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4467 return lam;
4470 case CPP_OBJC_STRING:
4471 if (c_dialect_objc ())
4472 /* We have an Objective-C++ string literal. */
4473 return cp_parser_objc_expression (parser);
4474 cp_parser_error (parser, "expected primary-expression");
4475 return error_mark_node;
4477 case CPP_KEYWORD:
4478 switch (token->keyword)
4480 /* These two are the boolean literals. */
4481 case RID_TRUE:
4482 cp_lexer_consume_token (parser->lexer);
4483 return boolean_true_node;
4484 case RID_FALSE:
4485 cp_lexer_consume_token (parser->lexer);
4486 return boolean_false_node;
4488 /* The `__null' literal. */
4489 case RID_NULL:
4490 cp_lexer_consume_token (parser->lexer);
4491 return null_node;
4493 /* The `nullptr' literal. */
4494 case RID_NULLPTR:
4495 cp_lexer_consume_token (parser->lexer);
4496 return nullptr_node;
4498 /* Recognize the `this' keyword. */
4499 case RID_THIS:
4500 cp_lexer_consume_token (parser->lexer);
4501 if (parser->local_variables_forbidden_p)
4503 error_at (token->location,
4504 "%<this%> may not be used in this context");
4505 return error_mark_node;
4507 /* Pointers cannot appear in constant-expressions. */
4508 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4509 return error_mark_node;
4510 return finish_this_expr ();
4512 /* The `operator' keyword can be the beginning of an
4513 id-expression. */
4514 case RID_OPERATOR:
4515 goto id_expression;
4517 case RID_FUNCTION_NAME:
4518 case RID_PRETTY_FUNCTION_NAME:
4519 case RID_C99_FUNCTION_NAME:
4521 non_integral_constant name;
4523 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4524 __func__ are the names of variables -- but they are
4525 treated specially. Therefore, they are handled here,
4526 rather than relying on the generic id-expression logic
4527 below. Grammatically, these names are id-expressions.
4529 Consume the token. */
4530 token = cp_lexer_consume_token (parser->lexer);
4532 switch (token->keyword)
4534 case RID_FUNCTION_NAME:
4535 name = NIC_FUNC_NAME;
4536 break;
4537 case RID_PRETTY_FUNCTION_NAME:
4538 name = NIC_PRETTY_FUNC;
4539 break;
4540 case RID_C99_FUNCTION_NAME:
4541 name = NIC_C99_FUNC;
4542 break;
4543 default:
4544 gcc_unreachable ();
4547 if (cp_parser_non_integral_constant_expression (parser, name))
4548 return error_mark_node;
4550 /* Look up the name. */
4551 return finish_fname (token->u.value);
4554 case RID_VA_ARG:
4556 tree expression;
4557 tree type;
4558 source_location type_location;
4560 /* The `__builtin_va_arg' construct is used to handle
4561 `va_arg'. Consume the `__builtin_va_arg' token. */
4562 cp_lexer_consume_token (parser->lexer);
4563 /* Look for the opening `('. */
4564 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4565 /* Now, parse the assignment-expression. */
4566 expression = cp_parser_assignment_expression (parser);
4567 /* Look for the `,'. */
4568 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4569 type_location = cp_lexer_peek_token (parser->lexer)->location;
4570 /* Parse the type-id. */
4571 type = cp_parser_type_id (parser);
4572 /* Look for the closing `)'. */
4573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4574 /* Using `va_arg' in a constant-expression is not
4575 allowed. */
4576 if (cp_parser_non_integral_constant_expression (parser,
4577 NIC_VA_ARG))
4578 return error_mark_node;
4579 return build_x_va_arg (type_location, expression, type);
4582 case RID_OFFSETOF:
4583 return cp_parser_builtin_offsetof (parser);
4585 case RID_HAS_NOTHROW_ASSIGN:
4586 case RID_HAS_NOTHROW_CONSTRUCTOR:
4587 case RID_HAS_NOTHROW_COPY:
4588 case RID_HAS_TRIVIAL_ASSIGN:
4589 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4590 case RID_HAS_TRIVIAL_COPY:
4591 case RID_HAS_TRIVIAL_DESTRUCTOR:
4592 case RID_HAS_VIRTUAL_DESTRUCTOR:
4593 case RID_IS_ABSTRACT:
4594 case RID_IS_BASE_OF:
4595 case RID_IS_CLASS:
4596 case RID_IS_EMPTY:
4597 case RID_IS_ENUM:
4598 case RID_IS_FINAL:
4599 case RID_IS_LITERAL_TYPE:
4600 case RID_IS_POD:
4601 case RID_IS_POLYMORPHIC:
4602 case RID_IS_STD_LAYOUT:
4603 case RID_IS_TRIVIAL:
4604 case RID_IS_TRIVIALLY_ASSIGNABLE:
4605 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4606 case RID_IS_TRIVIALLY_COPYABLE:
4607 case RID_IS_UNION:
4608 return cp_parser_trait_expr (parser, token->keyword);
4610 /* Objective-C++ expressions. */
4611 case RID_AT_ENCODE:
4612 case RID_AT_PROTOCOL:
4613 case RID_AT_SELECTOR:
4614 return cp_parser_objc_expression (parser);
4616 case RID_TEMPLATE:
4617 if (parser->in_function_body
4618 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4619 == CPP_LESS))
4621 error_at (token->location,
4622 "a template declaration cannot appear at block scope");
4623 cp_parser_skip_to_end_of_block_or_statement (parser);
4624 return error_mark_node;
4626 default:
4627 cp_parser_error (parser, "expected primary-expression");
4628 return error_mark_node;
4631 /* An id-expression can start with either an identifier, a
4632 `::' as the beginning of a qualified-id, or the "operator"
4633 keyword. */
4634 case CPP_NAME:
4635 case CPP_SCOPE:
4636 case CPP_TEMPLATE_ID:
4637 case CPP_NESTED_NAME_SPECIFIER:
4639 tree id_expression;
4640 tree decl;
4641 const char *error_msg;
4642 bool template_p;
4643 bool done;
4644 cp_token *id_expr_token;
4646 id_expression:
4647 /* Parse the id-expression. */
4648 id_expression
4649 = cp_parser_id_expression (parser,
4650 /*template_keyword_p=*/false,
4651 /*check_dependency_p=*/true,
4652 &template_p,
4653 /*declarator_p=*/false,
4654 /*optional_p=*/false);
4655 if (id_expression == error_mark_node)
4656 return error_mark_node;
4657 id_expr_token = token;
4658 token = cp_lexer_peek_token (parser->lexer);
4659 done = (token->type != CPP_OPEN_SQUARE
4660 && token->type != CPP_OPEN_PAREN
4661 && token->type != CPP_DOT
4662 && token->type != CPP_DEREF
4663 && token->type != CPP_PLUS_PLUS
4664 && token->type != CPP_MINUS_MINUS);
4665 /* If we have a template-id, then no further lookup is
4666 required. If the template-id was for a template-class, we
4667 will sometimes have a TYPE_DECL at this point. */
4668 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4669 || TREE_CODE (id_expression) == TYPE_DECL)
4670 decl = id_expression;
4671 /* Look up the name. */
4672 else
4674 tree ambiguous_decls;
4676 /* If we already know that this lookup is ambiguous, then
4677 we've already issued an error message; there's no reason
4678 to check again. */
4679 if (id_expr_token->type == CPP_NAME
4680 && id_expr_token->error_reported)
4682 cp_parser_simulate_error (parser);
4683 return error_mark_node;
4686 decl = cp_parser_lookup_name (parser, id_expression,
4687 none_type,
4688 template_p,
4689 /*is_namespace=*/false,
4690 /*check_dependency=*/true,
4691 &ambiguous_decls,
4692 id_expr_token->location);
4693 /* If the lookup was ambiguous, an error will already have
4694 been issued. */
4695 if (ambiguous_decls)
4696 return error_mark_node;
4698 /* In Objective-C++, we may have an Objective-C 2.0
4699 dot-syntax for classes here. */
4700 if (c_dialect_objc ()
4701 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4702 && TREE_CODE (decl) == TYPE_DECL
4703 && objc_is_class_name (decl))
4705 tree component;
4706 cp_lexer_consume_token (parser->lexer);
4707 component = cp_parser_identifier (parser);
4708 if (component == error_mark_node)
4709 return error_mark_node;
4711 return objc_build_class_component_ref (id_expression, component);
4714 /* In Objective-C++, an instance variable (ivar) may be preferred
4715 to whatever cp_parser_lookup_name() found. */
4716 decl = objc_lookup_ivar (decl, id_expression);
4718 /* If name lookup gives us a SCOPE_REF, then the
4719 qualifying scope was dependent. */
4720 if (TREE_CODE (decl) == SCOPE_REF)
4722 /* At this point, we do not know if DECL is a valid
4723 integral constant expression. We assume that it is
4724 in fact such an expression, so that code like:
4726 template <int N> struct A {
4727 int a[B<N>::i];
4730 is accepted. At template-instantiation time, we
4731 will check that B<N>::i is actually a constant. */
4732 return decl;
4734 /* Check to see if DECL is a local variable in a context
4735 where that is forbidden. */
4736 if (parser->local_variables_forbidden_p
4737 && local_variable_p (decl))
4739 /* It might be that we only found DECL because we are
4740 trying to be generous with pre-ISO scoping rules.
4741 For example, consider:
4743 int i;
4744 void g() {
4745 for (int i = 0; i < 10; ++i) {}
4746 extern void f(int j = i);
4749 Here, name look up will originally find the out
4750 of scope `i'. We need to issue a warning message,
4751 but then use the global `i'. */
4752 decl = check_for_out_of_scope_variable (decl);
4753 if (local_variable_p (decl))
4755 error_at (id_expr_token->location,
4756 "local variable %qD may not appear in this context",
4757 decl);
4758 return error_mark_node;
4763 decl = (finish_id_expression
4764 (id_expression, decl, parser->scope,
4765 idk,
4766 parser->integral_constant_expression_p,
4767 parser->allow_non_integral_constant_expression_p,
4768 &parser->non_integral_constant_expression_p,
4769 template_p, done, address_p,
4770 template_arg_p,
4771 &error_msg,
4772 id_expr_token->location));
4773 if (error_msg)
4774 cp_parser_error (parser, error_msg);
4775 return decl;
4778 /* Anything else is an error. */
4779 default:
4780 cp_parser_error (parser, "expected primary-expression");
4781 return error_mark_node;
4785 static inline tree
4786 cp_parser_primary_expression (cp_parser *parser,
4787 bool address_p,
4788 bool cast_p,
4789 bool template_arg_p,
4790 cp_id_kind *idk)
4792 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4793 /*decltype*/false, idk);
4796 /* Parse an id-expression.
4798 id-expression:
4799 unqualified-id
4800 qualified-id
4802 qualified-id:
4803 :: [opt] nested-name-specifier template [opt] unqualified-id
4804 :: identifier
4805 :: operator-function-id
4806 :: template-id
4808 Return a representation of the unqualified portion of the
4809 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4810 a `::' or nested-name-specifier.
4812 Often, if the id-expression was a qualified-id, the caller will
4813 want to make a SCOPE_REF to represent the qualified-id. This
4814 function does not do this in order to avoid wastefully creating
4815 SCOPE_REFs when they are not required.
4817 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4818 `template' keyword.
4820 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4821 uninstantiated templates.
4823 If *TEMPLATE_P is non-NULL, it is set to true iff the
4824 `template' keyword is used to explicitly indicate that the entity
4825 named is a template.
4827 If DECLARATOR_P is true, the id-expression is appearing as part of
4828 a declarator, rather than as part of an expression. */
4830 static tree
4831 cp_parser_id_expression (cp_parser *parser,
4832 bool template_keyword_p,
4833 bool check_dependency_p,
4834 bool *template_p,
4835 bool declarator_p,
4836 bool optional_p)
4838 bool global_scope_p;
4839 bool nested_name_specifier_p;
4841 /* Assume the `template' keyword was not used. */
4842 if (template_p)
4843 *template_p = template_keyword_p;
4845 /* Look for the optional `::' operator. */
4846 global_scope_p
4847 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4848 != NULL_TREE);
4849 /* Look for the optional nested-name-specifier. */
4850 nested_name_specifier_p
4851 = (cp_parser_nested_name_specifier_opt (parser,
4852 /*typename_keyword_p=*/false,
4853 check_dependency_p,
4854 /*type_p=*/false,
4855 declarator_p)
4856 != NULL_TREE);
4857 /* If there is a nested-name-specifier, then we are looking at
4858 the first qualified-id production. */
4859 if (nested_name_specifier_p)
4861 tree saved_scope;
4862 tree saved_object_scope;
4863 tree saved_qualifying_scope;
4864 tree unqualified_id;
4865 bool is_template;
4867 /* See if the next token is the `template' keyword. */
4868 if (!template_p)
4869 template_p = &is_template;
4870 *template_p = cp_parser_optional_template_keyword (parser);
4871 /* Name lookup we do during the processing of the
4872 unqualified-id might obliterate SCOPE. */
4873 saved_scope = parser->scope;
4874 saved_object_scope = parser->object_scope;
4875 saved_qualifying_scope = parser->qualifying_scope;
4876 /* Process the final unqualified-id. */
4877 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4878 check_dependency_p,
4879 declarator_p,
4880 /*optional_p=*/false);
4881 /* Restore the SAVED_SCOPE for our caller. */
4882 parser->scope = saved_scope;
4883 parser->object_scope = saved_object_scope;
4884 parser->qualifying_scope = saved_qualifying_scope;
4886 return unqualified_id;
4888 /* Otherwise, if we are in global scope, then we are looking at one
4889 of the other qualified-id productions. */
4890 else if (global_scope_p)
4892 cp_token *token;
4893 tree id;
4895 /* Peek at the next token. */
4896 token = cp_lexer_peek_token (parser->lexer);
4898 /* If it's an identifier, and the next token is not a "<", then
4899 we can avoid the template-id case. This is an optimization
4900 for this common case. */
4901 if (token->type == CPP_NAME
4902 && !cp_parser_nth_token_starts_template_argument_list_p
4903 (parser, 2))
4904 return cp_parser_identifier (parser);
4906 cp_parser_parse_tentatively (parser);
4907 /* Try a template-id. */
4908 id = cp_parser_template_id (parser,
4909 /*template_keyword_p=*/false,
4910 /*check_dependency_p=*/true,
4911 none_type,
4912 declarator_p);
4913 /* If that worked, we're done. */
4914 if (cp_parser_parse_definitely (parser))
4915 return id;
4917 /* Peek at the next token. (Changes in the token buffer may
4918 have invalidated the pointer obtained above.) */
4919 token = cp_lexer_peek_token (parser->lexer);
4921 switch (token->type)
4923 case CPP_NAME:
4924 return cp_parser_identifier (parser);
4926 case CPP_KEYWORD:
4927 if (token->keyword == RID_OPERATOR)
4928 return cp_parser_operator_function_id (parser);
4929 /* Fall through. */
4931 default:
4932 cp_parser_error (parser, "expected id-expression");
4933 return error_mark_node;
4936 else
4937 return cp_parser_unqualified_id (parser, template_keyword_p,
4938 /*check_dependency_p=*/true,
4939 declarator_p,
4940 optional_p);
4943 /* Parse an unqualified-id.
4945 unqualified-id:
4946 identifier
4947 operator-function-id
4948 conversion-function-id
4949 ~ class-name
4950 template-id
4952 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4953 keyword, in a construct like `A::template ...'.
4955 Returns a representation of unqualified-id. For the `identifier'
4956 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4957 production a BIT_NOT_EXPR is returned; the operand of the
4958 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4959 other productions, see the documentation accompanying the
4960 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4961 names are looked up in uninstantiated templates. If DECLARATOR_P
4962 is true, the unqualified-id is appearing as part of a declarator,
4963 rather than as part of an expression. */
4965 static tree
4966 cp_parser_unqualified_id (cp_parser* parser,
4967 bool template_keyword_p,
4968 bool check_dependency_p,
4969 bool declarator_p,
4970 bool optional_p)
4972 cp_token *token;
4974 /* Peek at the next token. */
4975 token = cp_lexer_peek_token (parser->lexer);
4977 switch ((int) token->type)
4979 case CPP_NAME:
4981 tree id;
4983 /* We don't know yet whether or not this will be a
4984 template-id. */
4985 cp_parser_parse_tentatively (parser);
4986 /* Try a template-id. */
4987 id = cp_parser_template_id (parser, template_keyword_p,
4988 check_dependency_p,
4989 none_type,
4990 declarator_p);
4991 /* If it worked, we're done. */
4992 if (cp_parser_parse_definitely (parser))
4993 return id;
4994 /* Otherwise, it's an ordinary identifier. */
4995 return cp_parser_identifier (parser);
4998 case CPP_TEMPLATE_ID:
4999 return cp_parser_template_id (parser, template_keyword_p,
5000 check_dependency_p,
5001 none_type,
5002 declarator_p);
5004 case CPP_COMPL:
5006 tree type_decl;
5007 tree qualifying_scope;
5008 tree object_scope;
5009 tree scope;
5010 bool done;
5012 /* Consume the `~' token. */
5013 cp_lexer_consume_token (parser->lexer);
5014 /* Parse the class-name. The standard, as written, seems to
5015 say that:
5017 template <typename T> struct S { ~S (); };
5018 template <typename T> S<T>::~S() {}
5020 is invalid, since `~' must be followed by a class-name, but
5021 `S<T>' is dependent, and so not known to be a class.
5022 That's not right; we need to look in uninstantiated
5023 templates. A further complication arises from:
5025 template <typename T> void f(T t) {
5026 t.T::~T();
5029 Here, it is not possible to look up `T' in the scope of `T'
5030 itself. We must look in both the current scope, and the
5031 scope of the containing complete expression.
5033 Yet another issue is:
5035 struct S {
5036 int S;
5037 ~S();
5040 S::~S() {}
5042 The standard does not seem to say that the `S' in `~S'
5043 should refer to the type `S' and not the data member
5044 `S::S'. */
5046 /* DR 244 says that we look up the name after the "~" in the
5047 same scope as we looked up the qualifying name. That idea
5048 isn't fully worked out; it's more complicated than that. */
5049 scope = parser->scope;
5050 object_scope = parser->object_scope;
5051 qualifying_scope = parser->qualifying_scope;
5053 /* Check for invalid scopes. */
5054 if (scope == error_mark_node)
5056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5057 cp_lexer_consume_token (parser->lexer);
5058 return error_mark_node;
5060 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5062 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5063 error_at (token->location,
5064 "scope %qT before %<~%> is not a class-name",
5065 scope);
5066 cp_parser_simulate_error (parser);
5067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5068 cp_lexer_consume_token (parser->lexer);
5069 return error_mark_node;
5071 gcc_assert (!scope || TYPE_P (scope));
5073 /* If the name is of the form "X::~X" it's OK even if X is a
5074 typedef. */
5075 token = cp_lexer_peek_token (parser->lexer);
5076 if (scope
5077 && token->type == CPP_NAME
5078 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5079 != CPP_LESS)
5080 && (token->u.value == TYPE_IDENTIFIER (scope)
5081 || (CLASS_TYPE_P (scope)
5082 && constructor_name_p (token->u.value, scope))))
5084 cp_lexer_consume_token (parser->lexer);
5085 return build_nt (BIT_NOT_EXPR, scope);
5088 /* ~auto means the destructor of whatever the object is. */
5089 if (cp_parser_is_keyword (token, RID_AUTO))
5091 if (cxx_dialect < cxx14)
5092 pedwarn (input_location, 0,
5093 "%<~auto%> only available with "
5094 "-std=c++14 or -std=gnu++14");
5095 cp_lexer_consume_token (parser->lexer);
5096 return build_nt (BIT_NOT_EXPR, make_auto ());
5099 /* If there was an explicit qualification (S::~T), first look
5100 in the scope given by the qualification (i.e., S).
5102 Note: in the calls to cp_parser_class_name below we pass
5103 typename_type so that lookup finds the injected-class-name
5104 rather than the constructor. */
5105 done = false;
5106 type_decl = NULL_TREE;
5107 if (scope)
5109 cp_parser_parse_tentatively (parser);
5110 type_decl = cp_parser_class_name (parser,
5111 /*typename_keyword_p=*/false,
5112 /*template_keyword_p=*/false,
5113 typename_type,
5114 /*check_dependency=*/false,
5115 /*class_head_p=*/false,
5116 declarator_p);
5117 if (cp_parser_parse_definitely (parser))
5118 done = true;
5120 /* In "N::S::~S", look in "N" as well. */
5121 if (!done && scope && qualifying_scope)
5123 cp_parser_parse_tentatively (parser);
5124 parser->scope = qualifying_scope;
5125 parser->object_scope = NULL_TREE;
5126 parser->qualifying_scope = NULL_TREE;
5127 type_decl
5128 = cp_parser_class_name (parser,
5129 /*typename_keyword_p=*/false,
5130 /*template_keyword_p=*/false,
5131 typename_type,
5132 /*check_dependency=*/false,
5133 /*class_head_p=*/false,
5134 declarator_p);
5135 if (cp_parser_parse_definitely (parser))
5136 done = true;
5138 /* In "p->S::~T", look in the scope given by "*p" as well. */
5139 else if (!done && object_scope)
5141 cp_parser_parse_tentatively (parser);
5142 parser->scope = object_scope;
5143 parser->object_scope = NULL_TREE;
5144 parser->qualifying_scope = NULL_TREE;
5145 type_decl
5146 = cp_parser_class_name (parser,
5147 /*typename_keyword_p=*/false,
5148 /*template_keyword_p=*/false,
5149 typename_type,
5150 /*check_dependency=*/false,
5151 /*class_head_p=*/false,
5152 declarator_p);
5153 if (cp_parser_parse_definitely (parser))
5154 done = true;
5156 /* Look in the surrounding context. */
5157 if (!done)
5159 parser->scope = NULL_TREE;
5160 parser->object_scope = NULL_TREE;
5161 parser->qualifying_scope = NULL_TREE;
5162 if (processing_template_decl)
5163 cp_parser_parse_tentatively (parser);
5164 type_decl
5165 = cp_parser_class_name (parser,
5166 /*typename_keyword_p=*/false,
5167 /*template_keyword_p=*/false,
5168 typename_type,
5169 /*check_dependency=*/false,
5170 /*class_head_p=*/false,
5171 declarator_p);
5172 if (processing_template_decl
5173 && ! cp_parser_parse_definitely (parser))
5175 /* We couldn't find a type with this name, so just accept
5176 it and check for a match at instantiation time. */
5177 type_decl = cp_parser_identifier (parser);
5178 if (type_decl != error_mark_node)
5179 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5180 return type_decl;
5183 /* If an error occurred, assume that the name of the
5184 destructor is the same as the name of the qualifying
5185 class. That allows us to keep parsing after running
5186 into ill-formed destructor names. */
5187 if (type_decl == error_mark_node && scope)
5188 return build_nt (BIT_NOT_EXPR, scope);
5189 else if (type_decl == error_mark_node)
5190 return error_mark_node;
5192 /* Check that destructor name and scope match. */
5193 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5195 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5196 error_at (token->location,
5197 "declaration of %<~%T%> as member of %qT",
5198 type_decl, scope);
5199 cp_parser_simulate_error (parser);
5200 return error_mark_node;
5203 /* [class.dtor]
5205 A typedef-name that names a class shall not be used as the
5206 identifier in the declarator for a destructor declaration. */
5207 if (declarator_p
5208 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5209 && !DECL_SELF_REFERENCE_P (type_decl)
5210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5211 error_at (token->location,
5212 "typedef-name %qD used as destructor declarator",
5213 type_decl);
5215 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5218 case CPP_KEYWORD:
5219 if (token->keyword == RID_OPERATOR)
5221 tree id;
5223 /* This could be a template-id, so we try that first. */
5224 cp_parser_parse_tentatively (parser);
5225 /* Try a template-id. */
5226 id = cp_parser_template_id (parser, template_keyword_p,
5227 /*check_dependency_p=*/true,
5228 none_type,
5229 declarator_p);
5230 /* If that worked, we're done. */
5231 if (cp_parser_parse_definitely (parser))
5232 return id;
5233 /* We still don't know whether we're looking at an
5234 operator-function-id or a conversion-function-id. */
5235 cp_parser_parse_tentatively (parser);
5236 /* Try an operator-function-id. */
5237 id = cp_parser_operator_function_id (parser);
5238 /* If that didn't work, try a conversion-function-id. */
5239 if (!cp_parser_parse_definitely (parser))
5240 id = cp_parser_conversion_function_id (parser);
5241 else if (UDLIT_OPER_P (id))
5243 /* 17.6.3.3.5 */
5244 const char *name = UDLIT_OP_SUFFIX (id);
5245 if (name[0] != '_' && !in_system_header_at (input_location)
5246 && declarator_p)
5247 warning (0, "literal operator suffixes not preceded by %<_%>"
5248 " are reserved for future standardization");
5251 return id;
5253 /* Fall through. */
5255 default:
5256 if (optional_p)
5257 return NULL_TREE;
5258 cp_parser_error (parser, "expected unqualified-id");
5259 return error_mark_node;
5263 /* Parse an (optional) nested-name-specifier.
5265 nested-name-specifier: [C++98]
5266 class-or-namespace-name :: nested-name-specifier [opt]
5267 class-or-namespace-name :: template nested-name-specifier [opt]
5269 nested-name-specifier: [C++0x]
5270 type-name ::
5271 namespace-name ::
5272 nested-name-specifier identifier ::
5273 nested-name-specifier template [opt] simple-template-id ::
5275 PARSER->SCOPE should be set appropriately before this function is
5276 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5277 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5278 in name lookups.
5280 Sets PARSER->SCOPE to the class (TYPE) or namespace
5281 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5282 it unchanged if there is no nested-name-specifier. Returns the new
5283 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5285 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5286 part of a declaration and/or decl-specifier. */
5288 static tree
5289 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5290 bool typename_keyword_p,
5291 bool check_dependency_p,
5292 bool type_p,
5293 bool is_declaration)
5295 bool success = false;
5296 cp_token_position start = 0;
5297 cp_token *token;
5299 /* Remember where the nested-name-specifier starts. */
5300 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5302 start = cp_lexer_token_position (parser->lexer, false);
5303 push_deferring_access_checks (dk_deferred);
5306 while (true)
5308 tree new_scope;
5309 tree old_scope;
5310 tree saved_qualifying_scope;
5311 bool template_keyword_p;
5313 /* Spot cases that cannot be the beginning of a
5314 nested-name-specifier. */
5315 token = cp_lexer_peek_token (parser->lexer);
5317 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5318 the already parsed nested-name-specifier. */
5319 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5321 /* Grab the nested-name-specifier and continue the loop. */
5322 cp_parser_pre_parsed_nested_name_specifier (parser);
5323 /* If we originally encountered this nested-name-specifier
5324 with IS_DECLARATION set to false, we will not have
5325 resolved TYPENAME_TYPEs, so we must do so here. */
5326 if (is_declaration
5327 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5329 new_scope = resolve_typename_type (parser->scope,
5330 /*only_current_p=*/false);
5331 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5332 parser->scope = new_scope;
5334 success = true;
5335 continue;
5338 /* Spot cases that cannot be the beginning of a
5339 nested-name-specifier. On the second and subsequent times
5340 through the loop, we look for the `template' keyword. */
5341 if (success && token->keyword == RID_TEMPLATE)
5343 /* A template-id can start a nested-name-specifier. */
5344 else if (token->type == CPP_TEMPLATE_ID)
5346 /* DR 743: decltype can be used in a nested-name-specifier. */
5347 else if (token_is_decltype (token))
5349 else
5351 /* If the next token is not an identifier, then it is
5352 definitely not a type-name or namespace-name. */
5353 if (token->type != CPP_NAME)
5354 break;
5355 /* If the following token is neither a `<' (to begin a
5356 template-id), nor a `::', then we are not looking at a
5357 nested-name-specifier. */
5358 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5360 if (token->type == CPP_COLON
5361 && parser->colon_corrects_to_scope_p
5362 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5364 error_at (token->location,
5365 "found %<:%> in nested-name-specifier, expected %<::%>");
5366 token->type = CPP_SCOPE;
5369 if (token->type != CPP_SCOPE
5370 && !cp_parser_nth_token_starts_template_argument_list_p
5371 (parser, 2))
5372 break;
5375 /* The nested-name-specifier is optional, so we parse
5376 tentatively. */
5377 cp_parser_parse_tentatively (parser);
5379 /* Look for the optional `template' keyword, if this isn't the
5380 first time through the loop. */
5381 if (success)
5382 template_keyword_p = cp_parser_optional_template_keyword (parser);
5383 else
5384 template_keyword_p = false;
5386 /* Save the old scope since the name lookup we are about to do
5387 might destroy it. */
5388 old_scope = parser->scope;
5389 saved_qualifying_scope = parser->qualifying_scope;
5390 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5391 look up names in "X<T>::I" in order to determine that "Y" is
5392 a template. So, if we have a typename at this point, we make
5393 an effort to look through it. */
5394 if (is_declaration
5395 && !typename_keyword_p
5396 && parser->scope
5397 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5398 parser->scope = resolve_typename_type (parser->scope,
5399 /*only_current_p=*/false);
5400 /* Parse the qualifying entity. */
5401 new_scope
5402 = cp_parser_qualifying_entity (parser,
5403 typename_keyword_p,
5404 template_keyword_p,
5405 check_dependency_p,
5406 type_p,
5407 is_declaration);
5408 /* Look for the `::' token. */
5409 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5411 /* If we found what we wanted, we keep going; otherwise, we're
5412 done. */
5413 if (!cp_parser_parse_definitely (parser))
5415 bool error_p = false;
5417 /* Restore the OLD_SCOPE since it was valid before the
5418 failed attempt at finding the last
5419 class-or-namespace-name. */
5420 parser->scope = old_scope;
5421 parser->qualifying_scope = saved_qualifying_scope;
5423 /* If the next token is a decltype, and the one after that is a
5424 `::', then the decltype has failed to resolve to a class or
5425 enumeration type. Give this error even when parsing
5426 tentatively since it can't possibly be valid--and we're going
5427 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5428 won't get another chance.*/
5429 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5430 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5431 == CPP_SCOPE))
5433 token = cp_lexer_consume_token (parser->lexer);
5434 error_at (token->location, "decltype evaluates to %qT, "
5435 "which is not a class or enumeration type",
5436 token->u.value);
5437 parser->scope = error_mark_node;
5438 error_p = true;
5439 /* As below. */
5440 success = true;
5441 cp_lexer_consume_token (parser->lexer);
5444 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5447 /* If we have a non-type template-id followed by ::, it can't
5448 possibly be valid. */
5449 token = cp_lexer_peek_token (parser->lexer);
5450 tree tid = token->u.tree_check_value->value;
5451 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5452 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5454 tree tmpl = NULL_TREE;
5455 if (is_overloaded_fn (tid))
5457 tree fns = get_fns (tid);
5458 if (!OVL_CHAIN (fns))
5459 tmpl = OVL_CURRENT (fns);
5460 error_at (token->location, "function template-id %qD "
5461 "in nested-name-specifier", tid);
5463 else
5465 /* Variable template. */
5466 tmpl = TREE_OPERAND (tid, 0);
5467 gcc_assert (variable_template_p (tmpl));
5468 error_at (token->location, "variable template-id %qD "
5469 "in nested-name-specifier", tid);
5471 if (tmpl)
5472 inform (DECL_SOURCE_LOCATION (tmpl),
5473 "%qD declared here", tmpl);
5475 parser->scope = error_mark_node;
5476 error_p = true;
5477 /* As below. */
5478 success = true;
5479 cp_lexer_consume_token (parser->lexer);
5480 cp_lexer_consume_token (parser->lexer);
5484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5485 break;
5486 /* If the next token is an identifier, and the one after
5487 that is a `::', then any valid interpretation would have
5488 found a class-or-namespace-name. */
5489 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5490 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5491 == CPP_SCOPE)
5492 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5493 != CPP_COMPL))
5495 token = cp_lexer_consume_token (parser->lexer);
5496 if (!error_p)
5498 if (!token->error_reported)
5500 tree decl;
5501 tree ambiguous_decls;
5503 decl = cp_parser_lookup_name (parser, token->u.value,
5504 none_type,
5505 /*is_template=*/false,
5506 /*is_namespace=*/false,
5507 /*check_dependency=*/true,
5508 &ambiguous_decls,
5509 token->location);
5510 if (TREE_CODE (decl) == TEMPLATE_DECL)
5511 error_at (token->location,
5512 "%qD used without template parameters",
5513 decl);
5514 else if (ambiguous_decls)
5516 // cp_parser_lookup_name has the same diagnostic,
5517 // thus make sure to emit it at most once.
5518 if (cp_parser_uncommitted_to_tentative_parse_p
5519 (parser))
5521 error_at (token->location,
5522 "reference to %qD is ambiguous",
5523 token->u.value);
5524 print_candidates (ambiguous_decls);
5526 decl = error_mark_node;
5528 else
5530 if (cxx_dialect != cxx98)
5531 cp_parser_name_lookup_error
5532 (parser, token->u.value, decl, NLE_NOT_CXX98,
5533 token->location);
5534 else
5535 cp_parser_name_lookup_error
5536 (parser, token->u.value, decl, NLE_CXX98,
5537 token->location);
5540 parser->scope = error_mark_node;
5541 error_p = true;
5542 /* Treat this as a successful nested-name-specifier
5543 due to:
5545 [basic.lookup.qual]
5547 If the name found is not a class-name (clause
5548 _class_) or namespace-name (_namespace.def_), the
5549 program is ill-formed. */
5550 success = true;
5552 cp_lexer_consume_token (parser->lexer);
5554 break;
5556 /* We've found one valid nested-name-specifier. */
5557 success = true;
5558 /* Name lookup always gives us a DECL. */
5559 if (TREE_CODE (new_scope) == TYPE_DECL)
5560 new_scope = TREE_TYPE (new_scope);
5561 /* Uses of "template" must be followed by actual templates. */
5562 if (template_keyword_p
5563 && !(CLASS_TYPE_P (new_scope)
5564 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5565 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5566 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5567 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5568 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5569 == TEMPLATE_ID_EXPR)))
5570 permerror (input_location, TYPE_P (new_scope)
5571 ? G_("%qT is not a template")
5572 : G_("%qD is not a template"),
5573 new_scope);
5574 /* If it is a class scope, try to complete it; we are about to
5575 be looking up names inside the class. */
5576 if (TYPE_P (new_scope)
5577 /* Since checking types for dependency can be expensive,
5578 avoid doing it if the type is already complete. */
5579 && !COMPLETE_TYPE_P (new_scope)
5580 /* Do not try to complete dependent types. */
5581 && !dependent_type_p (new_scope))
5583 new_scope = complete_type (new_scope);
5584 /* If it is a typedef to current class, use the current
5585 class instead, as the typedef won't have any names inside
5586 it yet. */
5587 if (!COMPLETE_TYPE_P (new_scope)
5588 && currently_open_class (new_scope))
5589 new_scope = TYPE_MAIN_VARIANT (new_scope);
5591 /* Make sure we look in the right scope the next time through
5592 the loop. */
5593 parser->scope = new_scope;
5596 /* If parsing tentatively, replace the sequence of tokens that makes
5597 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5598 token. That way, should we re-parse the token stream, we will
5599 not have to repeat the effort required to do the parse, nor will
5600 we issue duplicate error messages. */
5601 if (success && start)
5603 cp_token *token;
5605 token = cp_lexer_token_at (parser->lexer, start);
5606 /* Reset the contents of the START token. */
5607 token->type = CPP_NESTED_NAME_SPECIFIER;
5608 /* Retrieve any deferred checks. Do not pop this access checks yet
5609 so the memory will not be reclaimed during token replacing below. */
5610 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5611 token->u.tree_check_value->value = parser->scope;
5612 token->u.tree_check_value->checks = get_deferred_access_checks ();
5613 token->u.tree_check_value->qualifying_scope =
5614 parser->qualifying_scope;
5615 token->keyword = RID_MAX;
5617 /* Purge all subsequent tokens. */
5618 cp_lexer_purge_tokens_after (parser->lexer, start);
5621 if (start)
5622 pop_to_parent_deferring_access_checks ();
5624 return success ? parser->scope : NULL_TREE;
5627 /* Parse a nested-name-specifier. See
5628 cp_parser_nested_name_specifier_opt for details. This function
5629 behaves identically, except that it will an issue an error if no
5630 nested-name-specifier is present. */
5632 static tree
5633 cp_parser_nested_name_specifier (cp_parser *parser,
5634 bool typename_keyword_p,
5635 bool check_dependency_p,
5636 bool type_p,
5637 bool is_declaration)
5639 tree scope;
5641 /* Look for the nested-name-specifier. */
5642 scope = cp_parser_nested_name_specifier_opt (parser,
5643 typename_keyword_p,
5644 check_dependency_p,
5645 type_p,
5646 is_declaration);
5647 /* If it was not present, issue an error message. */
5648 if (!scope)
5650 cp_parser_error (parser, "expected nested-name-specifier");
5651 parser->scope = NULL_TREE;
5654 return scope;
5657 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5658 this is either a class-name or a namespace-name (which corresponds
5659 to the class-or-namespace-name production in the grammar). For
5660 C++0x, it can also be a type-name that refers to an enumeration
5661 type or a simple-template-id.
5663 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5664 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5665 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5666 TYPE_P is TRUE iff the next name should be taken as a class-name,
5667 even the same name is declared to be another entity in the same
5668 scope.
5670 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5671 specified by the class-or-namespace-name. If neither is found the
5672 ERROR_MARK_NODE is returned. */
5674 static tree
5675 cp_parser_qualifying_entity (cp_parser *parser,
5676 bool typename_keyword_p,
5677 bool template_keyword_p,
5678 bool check_dependency_p,
5679 bool type_p,
5680 bool is_declaration)
5682 tree saved_scope;
5683 tree saved_qualifying_scope;
5684 tree saved_object_scope;
5685 tree scope;
5686 bool only_class_p;
5687 bool successful_parse_p;
5689 /* DR 743: decltype can appear in a nested-name-specifier. */
5690 if (cp_lexer_next_token_is_decltype (parser->lexer))
5692 scope = cp_parser_decltype (parser);
5693 if (TREE_CODE (scope) != ENUMERAL_TYPE
5694 && !MAYBE_CLASS_TYPE_P (scope))
5696 cp_parser_simulate_error (parser);
5697 return error_mark_node;
5699 if (TYPE_NAME (scope))
5700 scope = TYPE_NAME (scope);
5701 return scope;
5704 /* Before we try to parse the class-name, we must save away the
5705 current PARSER->SCOPE since cp_parser_class_name will destroy
5706 it. */
5707 saved_scope = parser->scope;
5708 saved_qualifying_scope = parser->qualifying_scope;
5709 saved_object_scope = parser->object_scope;
5710 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5711 there is no need to look for a namespace-name. */
5712 only_class_p = template_keyword_p
5713 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5714 if (!only_class_p)
5715 cp_parser_parse_tentatively (parser);
5716 scope = cp_parser_class_name (parser,
5717 typename_keyword_p,
5718 template_keyword_p,
5719 type_p ? class_type : none_type,
5720 check_dependency_p,
5721 /*class_head_p=*/false,
5722 is_declaration);
5723 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5724 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5725 if (!only_class_p
5726 && cxx_dialect != cxx98
5727 && !successful_parse_p)
5729 /* Restore the saved scope. */
5730 parser->scope = saved_scope;
5731 parser->qualifying_scope = saved_qualifying_scope;
5732 parser->object_scope = saved_object_scope;
5734 /* Parse tentatively. */
5735 cp_parser_parse_tentatively (parser);
5737 /* Parse a type-name */
5738 scope = cp_parser_type_name (parser);
5740 /* "If the name found does not designate a namespace or a class,
5741 enumeration, or dependent type, the program is ill-formed."
5743 We cover classes and dependent types above and namespaces below,
5744 so this code is only looking for enums. */
5745 if (!scope || TREE_CODE (scope) != TYPE_DECL
5746 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5747 cp_parser_simulate_error (parser);
5749 successful_parse_p = cp_parser_parse_definitely (parser);
5751 /* If that didn't work, try for a namespace-name. */
5752 if (!only_class_p && !successful_parse_p)
5754 /* Restore the saved scope. */
5755 parser->scope = saved_scope;
5756 parser->qualifying_scope = saved_qualifying_scope;
5757 parser->object_scope = saved_object_scope;
5758 /* If we are not looking at an identifier followed by the scope
5759 resolution operator, then this is not part of a
5760 nested-name-specifier. (Note that this function is only used
5761 to parse the components of a nested-name-specifier.) */
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5763 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5764 return error_mark_node;
5765 scope = cp_parser_namespace_name (parser);
5768 return scope;
5771 /* Return true if we are looking at a compound-literal, false otherwise. */
5773 static bool
5774 cp_parser_compound_literal_p (cp_parser *parser)
5776 /* Consume the `('. */
5777 cp_lexer_consume_token (parser->lexer);
5779 cp_lexer_save_tokens (parser->lexer);
5781 /* Skip tokens until the next token is a closing parenthesis.
5782 If we find the closing `)', and the next token is a `{', then
5783 we are looking at a compound-literal. */
5784 bool compound_literal_p
5785 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5786 /*consume_paren=*/true)
5787 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5789 /* Roll back the tokens we skipped. */
5790 cp_lexer_rollback_tokens (parser->lexer);
5792 return compound_literal_p;
5795 /* Parse a postfix-expression.
5797 postfix-expression:
5798 primary-expression
5799 postfix-expression [ expression ]
5800 postfix-expression ( expression-list [opt] )
5801 simple-type-specifier ( expression-list [opt] )
5802 typename :: [opt] nested-name-specifier identifier
5803 ( expression-list [opt] )
5804 typename :: [opt] nested-name-specifier template [opt] template-id
5805 ( expression-list [opt] )
5806 postfix-expression . template [opt] id-expression
5807 postfix-expression -> template [opt] id-expression
5808 postfix-expression . pseudo-destructor-name
5809 postfix-expression -> pseudo-destructor-name
5810 postfix-expression ++
5811 postfix-expression --
5812 dynamic_cast < type-id > ( expression )
5813 static_cast < type-id > ( expression )
5814 reinterpret_cast < type-id > ( expression )
5815 const_cast < type-id > ( expression )
5816 typeid ( expression )
5817 typeid ( type-id )
5819 GNU Extension:
5821 postfix-expression:
5822 ( type-id ) { initializer-list , [opt] }
5824 This extension is a GNU version of the C99 compound-literal
5825 construct. (The C99 grammar uses `type-name' instead of `type-id',
5826 but they are essentially the same concept.)
5828 If ADDRESS_P is true, the postfix expression is the operand of the
5829 `&' operator. CAST_P is true if this expression is the target of a
5830 cast.
5832 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5833 class member access expressions [expr.ref].
5835 Returns a representation of the expression. */
5837 static tree
5838 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5839 bool member_access_only_p, bool decltype_p,
5840 cp_id_kind * pidk_return)
5842 cp_token *token;
5843 location_t loc;
5844 enum rid keyword;
5845 cp_id_kind idk = CP_ID_KIND_NONE;
5846 tree postfix_expression = NULL_TREE;
5847 bool is_member_access = false;
5848 int saved_in_statement = -1;
5850 /* Peek at the next token. */
5851 token = cp_lexer_peek_token (parser->lexer);
5852 loc = token->location;
5853 /* Some of the productions are determined by keywords. */
5854 keyword = token->keyword;
5855 switch (keyword)
5857 case RID_DYNCAST:
5858 case RID_STATCAST:
5859 case RID_REINTCAST:
5860 case RID_CONSTCAST:
5862 tree type;
5863 tree expression;
5864 const char *saved_message;
5865 bool saved_in_type_id_in_expr_p;
5867 /* All of these can be handled in the same way from the point
5868 of view of parsing. Begin by consuming the token
5869 identifying the cast. */
5870 cp_lexer_consume_token (parser->lexer);
5872 /* New types cannot be defined in the cast. */
5873 saved_message = parser->type_definition_forbidden_message;
5874 parser->type_definition_forbidden_message
5875 = G_("types may not be defined in casts");
5877 /* Look for the opening `<'. */
5878 cp_parser_require (parser, CPP_LESS, RT_LESS);
5879 /* Parse the type to which we are casting. */
5880 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5881 parser->in_type_id_in_expr_p = true;
5882 type = cp_parser_type_id (parser);
5883 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5884 /* Look for the closing `>'. */
5885 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5886 /* Restore the old message. */
5887 parser->type_definition_forbidden_message = saved_message;
5889 bool saved_greater_than_is_operator_p
5890 = parser->greater_than_is_operator_p;
5891 parser->greater_than_is_operator_p = true;
5893 /* And the expression which is being cast. */
5894 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5895 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5898 parser->greater_than_is_operator_p
5899 = saved_greater_than_is_operator_p;
5901 /* Only type conversions to integral or enumeration types
5902 can be used in constant-expressions. */
5903 if (!cast_valid_in_integral_constant_expression_p (type)
5904 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5905 return error_mark_node;
5907 switch (keyword)
5909 case RID_DYNCAST:
5910 postfix_expression
5911 = build_dynamic_cast (type, expression, tf_warning_or_error);
5912 break;
5913 case RID_STATCAST:
5914 postfix_expression
5915 = build_static_cast (type, expression, tf_warning_or_error);
5916 break;
5917 case RID_REINTCAST:
5918 postfix_expression
5919 = build_reinterpret_cast (type, expression,
5920 tf_warning_or_error);
5921 break;
5922 case RID_CONSTCAST:
5923 postfix_expression
5924 = build_const_cast (type, expression, tf_warning_or_error);
5925 break;
5926 default:
5927 gcc_unreachable ();
5930 break;
5932 case RID_TYPEID:
5934 tree type;
5935 const char *saved_message;
5936 bool saved_in_type_id_in_expr_p;
5938 /* Consume the `typeid' token. */
5939 cp_lexer_consume_token (parser->lexer);
5940 /* Look for the `(' token. */
5941 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5942 /* Types cannot be defined in a `typeid' expression. */
5943 saved_message = parser->type_definition_forbidden_message;
5944 parser->type_definition_forbidden_message
5945 = G_("types may not be defined in a %<typeid%> expression");
5946 /* We can't be sure yet whether we're looking at a type-id or an
5947 expression. */
5948 cp_parser_parse_tentatively (parser);
5949 /* Try a type-id first. */
5950 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5951 parser->in_type_id_in_expr_p = true;
5952 type = cp_parser_type_id (parser);
5953 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5954 /* Look for the `)' token. Otherwise, we can't be sure that
5955 we're not looking at an expression: consider `typeid (int
5956 (3))', for example. */
5957 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5958 /* If all went well, simply lookup the type-id. */
5959 if (cp_parser_parse_definitely (parser))
5960 postfix_expression = get_typeid (type, tf_warning_or_error);
5961 /* Otherwise, fall back to the expression variant. */
5962 else
5964 tree expression;
5966 /* Look for an expression. */
5967 expression = cp_parser_expression (parser, & idk);
5968 /* Compute its typeid. */
5969 postfix_expression = build_typeid (expression, tf_warning_or_error);
5970 /* Look for the `)' token. */
5971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5973 /* Restore the saved message. */
5974 parser->type_definition_forbidden_message = saved_message;
5975 /* `typeid' may not appear in an integral constant expression. */
5976 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5977 return error_mark_node;
5979 break;
5981 case RID_TYPENAME:
5983 tree type;
5984 /* The syntax permitted here is the same permitted for an
5985 elaborated-type-specifier. */
5986 type = cp_parser_elaborated_type_specifier (parser,
5987 /*is_friend=*/false,
5988 /*is_declaration=*/false);
5989 postfix_expression = cp_parser_functional_cast (parser, type);
5991 break;
5993 case RID_CILK_SPAWN:
5995 cp_lexer_consume_token (parser->lexer);
5996 token = cp_lexer_peek_token (parser->lexer);
5997 if (token->type == CPP_SEMICOLON)
5999 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6000 "an expression");
6001 postfix_expression = error_mark_node;
6002 break;
6004 else if (!current_function_decl)
6006 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6007 "inside a function");
6008 postfix_expression = error_mark_node;
6009 break;
6011 else
6013 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6014 saved_in_statement = parser->in_statement;
6015 parser->in_statement |= IN_CILK_SPAWN;
6017 cfun->calls_cilk_spawn = 1;
6018 postfix_expression =
6019 cp_parser_postfix_expression (parser, false, false,
6020 false, false, &idk);
6021 if (!flag_cilkplus)
6023 error_at (token->location, "-fcilkplus must be enabled to use"
6024 " %<_Cilk_spawn%>");
6025 cfun->calls_cilk_spawn = 0;
6027 else if (saved_in_statement & IN_CILK_SPAWN)
6029 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6030 "are not permitted");
6031 postfix_expression = error_mark_node;
6032 cfun->calls_cilk_spawn = 0;
6034 else
6036 postfix_expression = build_cilk_spawn (token->location,
6037 postfix_expression);
6038 if (postfix_expression != error_mark_node)
6039 SET_EXPR_LOCATION (postfix_expression, input_location);
6040 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6042 break;
6045 case RID_BUILTIN_SHUFFLE:
6047 vec<tree, va_gc> *vec;
6048 unsigned int i;
6049 tree p;
6051 cp_lexer_consume_token (parser->lexer);
6052 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6053 /*cast_p=*/false, /*allow_expansion_p=*/true,
6054 /*non_constant_p=*/NULL);
6055 if (vec == NULL)
6056 return error_mark_node;
6058 FOR_EACH_VEC_ELT (*vec, i, p)
6059 mark_exp_read (p);
6061 if (vec->length () == 2)
6062 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6063 tf_warning_or_error);
6064 else if (vec->length () == 3)
6065 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6066 tf_warning_or_error);
6067 else
6069 error_at (loc, "wrong number of arguments to "
6070 "%<__builtin_shuffle%>");
6071 return error_mark_node;
6073 break;
6076 default:
6078 tree type;
6080 /* If the next thing is a simple-type-specifier, we may be
6081 looking at a functional cast. We could also be looking at
6082 an id-expression. So, we try the functional cast, and if
6083 that doesn't work we fall back to the primary-expression. */
6084 cp_parser_parse_tentatively (parser);
6085 /* Look for the simple-type-specifier. */
6086 type = cp_parser_simple_type_specifier (parser,
6087 /*decl_specs=*/NULL,
6088 CP_PARSER_FLAGS_NONE);
6089 /* Parse the cast itself. */
6090 if (!cp_parser_error_occurred (parser))
6091 postfix_expression
6092 = cp_parser_functional_cast (parser, type);
6093 /* If that worked, we're done. */
6094 if (cp_parser_parse_definitely (parser))
6095 break;
6097 /* If the functional-cast didn't work out, try a
6098 compound-literal. */
6099 if (cp_parser_allow_gnu_extensions_p (parser)
6100 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6102 tree initializer = NULL_TREE;
6104 cp_parser_parse_tentatively (parser);
6106 /* Avoid calling cp_parser_type_id pointlessly, see comment
6107 in cp_parser_cast_expression about c++/29234. */
6108 if (!cp_parser_compound_literal_p (parser))
6109 cp_parser_simulate_error (parser);
6110 else
6112 /* Parse the type. */
6113 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6114 parser->in_type_id_in_expr_p = true;
6115 type = cp_parser_type_id (parser);
6116 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6117 /* Look for the `)'. */
6118 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6121 /* If things aren't going well, there's no need to
6122 keep going. */
6123 if (!cp_parser_error_occurred (parser))
6125 bool non_constant_p;
6126 /* Parse the brace-enclosed initializer list. */
6127 initializer = cp_parser_braced_list (parser,
6128 &non_constant_p);
6130 /* If that worked, we're definitely looking at a
6131 compound-literal expression. */
6132 if (cp_parser_parse_definitely (parser))
6134 /* Warn the user that a compound literal is not
6135 allowed in standard C++. */
6136 pedwarn (input_location, OPT_Wpedantic,
6137 "ISO C++ forbids compound-literals");
6138 /* For simplicity, we disallow compound literals in
6139 constant-expressions. We could
6140 allow compound literals of integer type, whose
6141 initializer was a constant, in constant
6142 expressions. Permitting that usage, as a further
6143 extension, would not change the meaning of any
6144 currently accepted programs. (Of course, as
6145 compound literals are not part of ISO C++, the
6146 standard has nothing to say.) */
6147 if (cp_parser_non_integral_constant_expression (parser,
6148 NIC_NCC))
6150 postfix_expression = error_mark_node;
6151 break;
6153 /* Form the representation of the compound-literal. */
6154 postfix_expression
6155 = finish_compound_literal (type, initializer,
6156 tf_warning_or_error);
6157 break;
6161 /* It must be a primary-expression. */
6162 postfix_expression
6163 = cp_parser_primary_expression (parser, address_p, cast_p,
6164 /*template_arg_p=*/false,
6165 decltype_p,
6166 &idk);
6168 break;
6171 /* Note that we don't need to worry about calling build_cplus_new on a
6172 class-valued CALL_EXPR in decltype when it isn't the end of the
6173 postfix-expression; unary_complex_lvalue will take care of that for
6174 all these cases. */
6176 /* Keep looping until the postfix-expression is complete. */
6177 while (true)
6179 if (idk == CP_ID_KIND_UNQUALIFIED
6180 && identifier_p (postfix_expression)
6181 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6182 /* It is not a Koenig lookup function call. */
6183 postfix_expression
6184 = unqualified_name_lookup_error (postfix_expression);
6186 /* Peek at the next token. */
6187 token = cp_lexer_peek_token (parser->lexer);
6189 switch (token->type)
6191 case CPP_OPEN_SQUARE:
6192 if (cp_next_tokens_can_be_std_attribute_p (parser))
6194 cp_parser_error (parser,
6195 "two consecutive %<[%> shall "
6196 "only introduce an attribute");
6197 return error_mark_node;
6199 postfix_expression
6200 = cp_parser_postfix_open_square_expression (parser,
6201 postfix_expression,
6202 false,
6203 decltype_p);
6204 idk = CP_ID_KIND_NONE;
6205 is_member_access = false;
6206 break;
6208 case CPP_OPEN_PAREN:
6209 /* postfix-expression ( expression-list [opt] ) */
6211 bool koenig_p;
6212 bool is_builtin_constant_p;
6213 bool saved_integral_constant_expression_p = false;
6214 bool saved_non_integral_constant_expression_p = false;
6215 tsubst_flags_t complain = complain_flags (decltype_p);
6216 vec<tree, va_gc> *args;
6218 is_member_access = false;
6220 is_builtin_constant_p
6221 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6222 if (is_builtin_constant_p)
6224 /* The whole point of __builtin_constant_p is to allow
6225 non-constant expressions to appear as arguments. */
6226 saved_integral_constant_expression_p
6227 = parser->integral_constant_expression_p;
6228 saved_non_integral_constant_expression_p
6229 = parser->non_integral_constant_expression_p;
6230 parser->integral_constant_expression_p = false;
6232 args = (cp_parser_parenthesized_expression_list
6233 (parser, non_attr,
6234 /*cast_p=*/false, /*allow_expansion_p=*/true,
6235 /*non_constant_p=*/NULL,
6236 /*want_literal_zero_p=*/warn_memset_transposed_args));
6237 if (is_builtin_constant_p)
6239 parser->integral_constant_expression_p
6240 = saved_integral_constant_expression_p;
6241 parser->non_integral_constant_expression_p
6242 = saved_non_integral_constant_expression_p;
6245 if (args == NULL)
6247 postfix_expression = error_mark_node;
6248 break;
6251 /* Function calls are not permitted in
6252 constant-expressions. */
6253 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6254 && cp_parser_non_integral_constant_expression (parser,
6255 NIC_FUNC_CALL))
6257 postfix_expression = error_mark_node;
6258 release_tree_vector (args);
6259 break;
6262 koenig_p = false;
6263 if (idk == CP_ID_KIND_UNQUALIFIED
6264 || idk == CP_ID_KIND_TEMPLATE_ID)
6266 if (identifier_p (postfix_expression))
6268 if (!args->is_empty ())
6270 koenig_p = true;
6271 if (!any_type_dependent_arguments_p (args))
6272 postfix_expression
6273 = perform_koenig_lookup (postfix_expression, args,
6274 complain);
6276 else
6277 postfix_expression
6278 = unqualified_fn_lookup_error (postfix_expression);
6280 /* We do not perform argument-dependent lookup if
6281 normal lookup finds a non-function, in accordance
6282 with the expected resolution of DR 218. */
6283 else if (!args->is_empty ()
6284 && is_overloaded_fn (postfix_expression))
6286 tree fn = get_first_fn (postfix_expression);
6287 fn = STRIP_TEMPLATE (fn);
6289 /* Do not do argument dependent lookup if regular
6290 lookup finds a member function or a block-scope
6291 function declaration. [basic.lookup.argdep]/3 */
6292 if (!DECL_FUNCTION_MEMBER_P (fn)
6293 && !DECL_LOCAL_FUNCTION_P (fn))
6295 koenig_p = true;
6296 if (!any_type_dependent_arguments_p (args))
6297 postfix_expression
6298 = perform_koenig_lookup (postfix_expression, args,
6299 complain);
6304 if (warn_memset_transposed_args)
6306 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6307 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6308 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6309 && vec_safe_length (args) == 3
6310 && integer_zerop ((*args)[2])
6311 && LITERAL_ZERO_P ((*args)[2])
6312 && !(integer_zerop ((*args)[1])
6313 && LITERAL_ZERO_P ((*args)[1])))
6314 warning (OPT_Wmemset_transposed_args,
6315 "%<memset%> used with constant zero length "
6316 "parameter; this could be due to transposed "
6317 "parameters");
6319 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6320 to avoid leaking those into folder and middle-end. */
6321 unsigned int i;
6322 tree arg;
6323 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6324 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6325 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6328 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6330 tree instance = TREE_OPERAND (postfix_expression, 0);
6331 tree fn = TREE_OPERAND (postfix_expression, 1);
6333 if (processing_template_decl
6334 && (type_dependent_expression_p (instance)
6335 || (!BASELINK_P (fn)
6336 && TREE_CODE (fn) != FIELD_DECL)
6337 || type_dependent_expression_p (fn)
6338 || any_type_dependent_arguments_p (args)))
6340 postfix_expression
6341 = build_nt_call_vec (postfix_expression, args);
6342 release_tree_vector (args);
6343 break;
6346 if (BASELINK_P (fn))
6348 postfix_expression
6349 = (build_new_method_call
6350 (instance, fn, &args, NULL_TREE,
6351 (idk == CP_ID_KIND_QUALIFIED
6352 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6353 : LOOKUP_NORMAL),
6354 /*fn_p=*/NULL,
6355 complain));
6357 else
6358 postfix_expression
6359 = finish_call_expr (postfix_expression, &args,
6360 /*disallow_virtual=*/false,
6361 /*koenig_p=*/false,
6362 complain);
6364 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6365 || TREE_CODE (postfix_expression) == MEMBER_REF
6366 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6367 postfix_expression = (build_offset_ref_call_from_tree
6368 (postfix_expression, &args,
6369 complain));
6370 else if (idk == CP_ID_KIND_QUALIFIED)
6371 /* A call to a static class member, or a namespace-scope
6372 function. */
6373 postfix_expression
6374 = finish_call_expr (postfix_expression, &args,
6375 /*disallow_virtual=*/true,
6376 koenig_p,
6377 complain);
6378 else
6379 /* All other function calls. */
6380 postfix_expression
6381 = finish_call_expr (postfix_expression, &args,
6382 /*disallow_virtual=*/false,
6383 koenig_p,
6384 complain);
6386 protected_set_expr_location (postfix_expression, token->location);
6388 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6389 idk = CP_ID_KIND_NONE;
6391 release_tree_vector (args);
6393 break;
6395 case CPP_DOT:
6396 case CPP_DEREF:
6397 /* postfix-expression . template [opt] id-expression
6398 postfix-expression . pseudo-destructor-name
6399 postfix-expression -> template [opt] id-expression
6400 postfix-expression -> pseudo-destructor-name */
6402 /* Consume the `.' or `->' operator. */
6403 cp_lexer_consume_token (parser->lexer);
6405 postfix_expression
6406 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6407 postfix_expression,
6408 false, &idk, loc);
6410 is_member_access = true;
6411 break;
6413 case CPP_PLUS_PLUS:
6414 /* postfix-expression ++ */
6415 /* Consume the `++' token. */
6416 cp_lexer_consume_token (parser->lexer);
6417 /* Generate a representation for the complete expression. */
6418 postfix_expression
6419 = finish_increment_expr (postfix_expression,
6420 POSTINCREMENT_EXPR);
6421 /* Increments may not appear in constant-expressions. */
6422 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6423 postfix_expression = error_mark_node;
6424 idk = CP_ID_KIND_NONE;
6425 is_member_access = false;
6426 break;
6428 case CPP_MINUS_MINUS:
6429 /* postfix-expression -- */
6430 /* Consume the `--' token. */
6431 cp_lexer_consume_token (parser->lexer);
6432 /* Generate a representation for the complete expression. */
6433 postfix_expression
6434 = finish_increment_expr (postfix_expression,
6435 POSTDECREMENT_EXPR);
6436 /* Decrements may not appear in constant-expressions. */
6437 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6438 postfix_expression = error_mark_node;
6439 idk = CP_ID_KIND_NONE;
6440 is_member_access = false;
6441 break;
6443 default:
6444 if (pidk_return != NULL)
6445 * pidk_return = idk;
6446 if (member_access_only_p)
6447 return is_member_access? postfix_expression : error_mark_node;
6448 else
6449 return postfix_expression;
6453 /* We should never get here. */
6454 gcc_unreachable ();
6455 return error_mark_node;
6458 /* This function parses Cilk Plus array notations. If a normal array expr. is
6459 parsed then the array index is passed back to the caller through *INIT_INDEX
6460 and the function returns a NULL_TREE. If array notation expr. is parsed,
6461 then *INIT_INDEX is ignored by the caller and the function returns
6462 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6463 error_mark_node. */
6465 static tree
6466 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6467 tree array_value)
6469 cp_token *token = NULL;
6470 tree length_index, stride = NULL_TREE, value_tree, array_type;
6471 if (!array_value || array_value == error_mark_node)
6473 cp_parser_skip_to_end_of_statement (parser);
6474 return error_mark_node;
6477 array_type = TREE_TYPE (array_value);
6479 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6480 parser->colon_corrects_to_scope_p = false;
6481 token = cp_lexer_peek_token (parser->lexer);
6483 if (!token)
6485 cp_parser_error (parser, "expected %<:%> or numeral");
6486 return error_mark_node;
6488 else if (token->type == CPP_COLON)
6490 /* Consume the ':'. */
6491 cp_lexer_consume_token (parser->lexer);
6493 /* If we are here, then we have a case like this A[:]. */
6494 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6496 cp_parser_error (parser, "expected %<]%>");
6497 cp_parser_skip_to_end_of_statement (parser);
6498 return error_mark_node;
6500 *init_index = NULL_TREE;
6501 stride = NULL_TREE;
6502 length_index = NULL_TREE;
6504 else
6506 /* If we are here, then there are three valid possibilities:
6507 1. ARRAY [ EXP ]
6508 2. ARRAY [ EXP : EXP ]
6509 3. ARRAY [ EXP : EXP : EXP ] */
6511 *init_index = cp_parser_expression (parser);
6512 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6514 /* This indicates that we have a normal array expression. */
6515 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6516 return NULL_TREE;
6519 /* Consume the ':'. */
6520 cp_lexer_consume_token (parser->lexer);
6521 length_index = cp_parser_expression (parser);
6522 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6524 cp_lexer_consume_token (parser->lexer);
6525 stride = cp_parser_expression (parser);
6528 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6530 if (*init_index == error_mark_node || length_index == error_mark_node
6531 || stride == error_mark_node || array_type == error_mark_node)
6533 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6534 cp_lexer_consume_token (parser->lexer);
6535 return error_mark_node;
6537 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6539 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6540 length_index, stride, array_type);
6541 return value_tree;
6544 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6545 by cp_parser_builtin_offsetof. We're looking for
6547 postfix-expression [ expression ]
6548 postfix-expression [ braced-init-list ] (C++11)
6550 FOR_OFFSETOF is set if we're being called in that context, which
6551 changes how we deal with integer constant expressions. */
6553 static tree
6554 cp_parser_postfix_open_square_expression (cp_parser *parser,
6555 tree postfix_expression,
6556 bool for_offsetof,
6557 bool decltype_p)
6559 tree index = NULL_TREE;
6560 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6561 bool saved_greater_than_is_operator_p;
6563 /* Consume the `[' token. */
6564 cp_lexer_consume_token (parser->lexer);
6566 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6567 parser->greater_than_is_operator_p = true;
6569 /* Parse the index expression. */
6570 /* ??? For offsetof, there is a question of what to allow here. If
6571 offsetof is not being used in an integral constant expression context,
6572 then we *could* get the right answer by computing the value at runtime.
6573 If we are in an integral constant expression context, then we might
6574 could accept any constant expression; hard to say without analysis.
6575 Rather than open the barn door too wide right away, allow only integer
6576 constant expressions here. */
6577 if (for_offsetof)
6578 index = cp_parser_constant_expression (parser);
6579 else
6581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6583 bool expr_nonconst_p;
6584 cp_lexer_set_source_position (parser->lexer);
6585 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6586 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6587 if (flag_cilkplus
6588 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6590 error_at (cp_lexer_peek_token (parser->lexer)->location,
6591 "braced list index is not allowed with array "
6592 "notation");
6593 cp_parser_skip_to_end_of_statement (parser);
6594 return error_mark_node;
6597 else if (flag_cilkplus)
6599 /* Here are have these two options:
6600 ARRAY[EXP : EXP] - Array notation expr with default
6601 stride of 1.
6602 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6603 stride. */
6604 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6605 postfix_expression);
6606 if (an_exp)
6607 return an_exp;
6609 else
6610 index = cp_parser_expression (parser);
6613 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6615 /* Look for the closing `]'. */
6616 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6618 /* Build the ARRAY_REF. */
6619 postfix_expression = grok_array_decl (loc, postfix_expression,
6620 index, decltype_p);
6622 /* When not doing offsetof, array references are not permitted in
6623 constant-expressions. */
6624 if (!for_offsetof
6625 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6626 postfix_expression = error_mark_node;
6628 return postfix_expression;
6631 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6632 by cp_parser_builtin_offsetof. We're looking for
6634 postfix-expression . template [opt] id-expression
6635 postfix-expression . pseudo-destructor-name
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression -> pseudo-destructor-name
6639 FOR_OFFSETOF is set if we're being called in that context. That sorta
6640 limits what of the above we'll actually accept, but nevermind.
6641 TOKEN_TYPE is the "." or "->" token, which will already have been
6642 removed from the stream. */
6644 static tree
6645 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6646 enum cpp_ttype token_type,
6647 tree postfix_expression,
6648 bool for_offsetof, cp_id_kind *idk,
6649 location_t location)
6651 tree name;
6652 bool dependent_p;
6653 bool pseudo_destructor_p;
6654 tree scope = NULL_TREE;
6656 /* If this is a `->' operator, dereference the pointer. */
6657 if (token_type == CPP_DEREF)
6658 postfix_expression = build_x_arrow (location, postfix_expression,
6659 tf_warning_or_error);
6660 /* Check to see whether or not the expression is type-dependent. */
6661 dependent_p = type_dependent_expression_p (postfix_expression);
6662 /* The identifier following the `->' or `.' is not qualified. */
6663 parser->scope = NULL_TREE;
6664 parser->qualifying_scope = NULL_TREE;
6665 parser->object_scope = NULL_TREE;
6666 *idk = CP_ID_KIND_NONE;
6668 /* Enter the scope corresponding to the type of the object
6669 given by the POSTFIX_EXPRESSION. */
6670 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6672 scope = TREE_TYPE (postfix_expression);
6673 /* According to the standard, no expression should ever have
6674 reference type. Unfortunately, we do not currently match
6675 the standard in this respect in that our internal representation
6676 of an expression may have reference type even when the standard
6677 says it does not. Therefore, we have to manually obtain the
6678 underlying type here. */
6679 scope = non_reference (scope);
6680 /* The type of the POSTFIX_EXPRESSION must be complete. */
6681 if (scope == unknown_type_node)
6683 error_at (location, "%qE does not have class type",
6684 postfix_expression);
6685 scope = NULL_TREE;
6687 /* Unlike the object expression in other contexts, *this is not
6688 required to be of complete type for purposes of class member
6689 access (5.2.5) outside the member function body. */
6690 else if (postfix_expression != current_class_ref
6691 && !(processing_template_decl && scope == current_class_type))
6692 scope = complete_type_or_else (scope, NULL_TREE);
6693 /* Let the name lookup machinery know that we are processing a
6694 class member access expression. */
6695 parser->context->object_type = scope;
6696 /* If something went wrong, we want to be able to discern that case,
6697 as opposed to the case where there was no SCOPE due to the type
6698 of expression being dependent. */
6699 if (!scope)
6700 scope = error_mark_node;
6701 /* If the SCOPE was erroneous, make the various semantic analysis
6702 functions exit quickly -- and without issuing additional error
6703 messages. */
6704 if (scope == error_mark_node)
6705 postfix_expression = error_mark_node;
6708 /* Assume this expression is not a pseudo-destructor access. */
6709 pseudo_destructor_p = false;
6711 /* If the SCOPE is a scalar type, then, if this is a valid program,
6712 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6713 is type dependent, it can be pseudo-destructor-name or something else.
6714 Try to parse it as pseudo-destructor-name first. */
6715 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6717 tree s;
6718 tree type;
6720 cp_parser_parse_tentatively (parser);
6721 /* Parse the pseudo-destructor-name. */
6722 s = NULL_TREE;
6723 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6724 &s, &type);
6725 if (dependent_p
6726 && (cp_parser_error_occurred (parser)
6727 || !SCALAR_TYPE_P (type)))
6728 cp_parser_abort_tentative_parse (parser);
6729 else if (cp_parser_parse_definitely (parser))
6731 pseudo_destructor_p = true;
6732 postfix_expression
6733 = finish_pseudo_destructor_expr (postfix_expression,
6734 s, type, location);
6738 if (!pseudo_destructor_p)
6740 /* If the SCOPE is not a scalar type, we are looking at an
6741 ordinary class member access expression, rather than a
6742 pseudo-destructor-name. */
6743 bool template_p;
6744 cp_token *token = cp_lexer_peek_token (parser->lexer);
6745 /* Parse the id-expression. */
6746 name = (cp_parser_id_expression
6747 (parser,
6748 cp_parser_optional_template_keyword (parser),
6749 /*check_dependency_p=*/true,
6750 &template_p,
6751 /*declarator_p=*/false,
6752 /*optional_p=*/false));
6753 /* In general, build a SCOPE_REF if the member name is qualified.
6754 However, if the name was not dependent and has already been
6755 resolved; there is no need to build the SCOPE_REF. For example;
6757 struct X { void f(); };
6758 template <typename T> void f(T* t) { t->X::f(); }
6760 Even though "t" is dependent, "X::f" is not and has been resolved
6761 to a BASELINK; there is no need to include scope information. */
6763 /* But we do need to remember that there was an explicit scope for
6764 virtual function calls. */
6765 if (parser->scope)
6766 *idk = CP_ID_KIND_QUALIFIED;
6768 /* If the name is a template-id that names a type, we will get a
6769 TYPE_DECL here. That is invalid code. */
6770 if (TREE_CODE (name) == TYPE_DECL)
6772 error_at (token->location, "invalid use of %qD", name);
6773 postfix_expression = error_mark_node;
6775 else
6777 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6779 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6781 error_at (token->location, "%<%D::%D%> is not a class member",
6782 parser->scope, name);
6783 postfix_expression = error_mark_node;
6785 else
6786 name = build_qualified_name (/*type=*/NULL_TREE,
6787 parser->scope,
6788 name,
6789 template_p);
6790 parser->scope = NULL_TREE;
6791 parser->qualifying_scope = NULL_TREE;
6792 parser->object_scope = NULL_TREE;
6794 if (parser->scope && name && BASELINK_P (name))
6795 adjust_result_of_qualified_name_lookup
6796 (name, parser->scope, scope);
6797 postfix_expression
6798 = finish_class_member_access_expr (postfix_expression, name,
6799 template_p,
6800 tf_warning_or_error);
6804 /* We no longer need to look up names in the scope of the object on
6805 the left-hand side of the `.' or `->' operator. */
6806 parser->context->object_type = NULL_TREE;
6808 /* Outside of offsetof, these operators may not appear in
6809 constant-expressions. */
6810 if (!for_offsetof
6811 && (cp_parser_non_integral_constant_expression
6812 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6813 postfix_expression = error_mark_node;
6815 return postfix_expression;
6818 /* Cache of LITERAL_ZERO_P constants. */
6820 static GTY(()) tree literal_zeros[itk_none];
6822 /* Parse a parenthesized expression-list.
6824 expression-list:
6825 assignment-expression
6826 expression-list, assignment-expression
6828 attribute-list:
6829 expression-list
6830 identifier
6831 identifier, expression-list
6833 CAST_P is true if this expression is the target of a cast.
6835 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6836 argument pack.
6838 Returns a vector of trees. Each element is a representation of an
6839 assignment-expression. NULL is returned if the ( and or ) are
6840 missing. An empty, but allocated, vector is returned on no
6841 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6842 if we are parsing an attribute list for an attribute that wants a
6843 plain identifier argument, normal_attr for an attribute that wants
6844 an expression, or non_attr if we aren't parsing an attribute list. If
6845 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6846 not all of the expressions in the list were constant.
6847 WANT_LITERAL_ZERO_P is true if the caller is interested in
6848 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6849 immediately, this can be removed. */
6851 static vec<tree, va_gc> *
6852 cp_parser_parenthesized_expression_list (cp_parser* parser,
6853 int is_attribute_list,
6854 bool cast_p,
6855 bool allow_expansion_p,
6856 bool *non_constant_p,
6857 bool want_literal_zero_p)
6859 vec<tree, va_gc> *expression_list;
6860 bool fold_expr_p = is_attribute_list != non_attr;
6861 tree identifier = NULL_TREE;
6862 bool saved_greater_than_is_operator_p;
6864 /* Assume all the expressions will be constant. */
6865 if (non_constant_p)
6866 *non_constant_p = false;
6868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6869 return NULL;
6871 expression_list = make_tree_vector ();
6873 /* Within a parenthesized expression, a `>' token is always
6874 the greater-than operator. */
6875 saved_greater_than_is_operator_p
6876 = parser->greater_than_is_operator_p;
6877 parser->greater_than_is_operator_p = true;
6879 /* Consume expressions until there are no more. */
6880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6881 while (true)
6883 tree expr;
6885 /* At the beginning of attribute lists, check to see if the
6886 next token is an identifier. */
6887 if (is_attribute_list == id_attr
6888 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6890 cp_token *token;
6892 /* Consume the identifier. */
6893 token = cp_lexer_consume_token (parser->lexer);
6894 /* Save the identifier. */
6895 identifier = token->u.value;
6897 else
6899 bool expr_non_constant_p;
6901 /* Parse the next assignment-expression. */
6902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6904 /* A braced-init-list. */
6905 cp_lexer_set_source_position (parser->lexer);
6906 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6907 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6908 if (non_constant_p && expr_non_constant_p)
6909 *non_constant_p = true;
6911 else if (non_constant_p)
6913 expr = (cp_parser_constant_expression
6914 (parser, /*allow_non_constant_p=*/true,
6915 &expr_non_constant_p));
6916 if (expr_non_constant_p)
6917 *non_constant_p = true;
6919 else
6921 expr = NULL_TREE;
6922 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6923 switch (tok->type)
6925 case CPP_NUMBER:
6926 case CPP_CHAR:
6927 case CPP_WCHAR:
6928 case CPP_CHAR16:
6929 case CPP_CHAR32:
6930 /* If a parameter is literal zero alone, remember it
6931 for -Wmemset-transposed-args warning. */
6932 if (integer_zerop (tok->u.value)
6933 && !TREE_OVERFLOW (tok->u.value)
6934 && want_literal_zero_p
6935 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6936 == CPP_COMMA
6937 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6938 == CPP_CLOSE_PAREN))
6940 unsigned int i;
6941 for (i = 0; i < itk_none; ++i)
6942 if (TREE_TYPE (tok->u.value) == integer_types[i])
6943 break;
6944 if (i < itk_none && literal_zeros[i])
6945 expr = literal_zeros[i];
6946 else
6948 expr = copy_node (tok->u.value);
6949 LITERAL_ZERO_P (expr) = 1;
6950 if (i < itk_none)
6951 literal_zeros[i] = expr;
6953 /* Consume the 0 token (or '\0', 0LL etc.). */
6954 cp_lexer_consume_token (parser->lexer);
6956 break;
6957 default:
6958 break;
6960 if (expr == NULL_TREE)
6961 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6962 cast_p);
6965 if (fold_expr_p)
6966 expr = instantiate_non_dependent_expr (expr);
6968 /* If we have an ellipsis, then this is an expression
6969 expansion. */
6970 if (allow_expansion_p
6971 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6973 /* Consume the `...'. */
6974 cp_lexer_consume_token (parser->lexer);
6976 /* Build the argument pack. */
6977 expr = make_pack_expansion (expr);
6980 /* Add it to the list. We add error_mark_node
6981 expressions to the list, so that we can still tell if
6982 the correct form for a parenthesized expression-list
6983 is found. That gives better errors. */
6984 vec_safe_push (expression_list, expr);
6986 if (expr == error_mark_node)
6987 goto skip_comma;
6990 /* After the first item, attribute lists look the same as
6991 expression lists. */
6992 is_attribute_list = non_attr;
6994 get_comma:;
6995 /* If the next token isn't a `,', then we are done. */
6996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6997 break;
6999 /* Otherwise, consume the `,' and keep going. */
7000 cp_lexer_consume_token (parser->lexer);
7003 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7005 int ending;
7007 skip_comma:;
7008 /* We try and resync to an unnested comma, as that will give the
7009 user better diagnostics. */
7010 ending = cp_parser_skip_to_closing_parenthesis (parser,
7011 /*recovering=*/true,
7012 /*or_comma=*/true,
7013 /*consume_paren=*/true);
7014 if (ending < 0)
7015 goto get_comma;
7016 if (!ending)
7018 parser->greater_than_is_operator_p
7019 = saved_greater_than_is_operator_p;
7020 return NULL;
7024 parser->greater_than_is_operator_p
7025 = saved_greater_than_is_operator_p;
7027 if (identifier)
7028 vec_safe_insert (expression_list, 0, identifier);
7030 return expression_list;
7033 /* Parse a pseudo-destructor-name.
7035 pseudo-destructor-name:
7036 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7037 :: [opt] nested-name-specifier template template-id :: ~ type-name
7038 :: [opt] nested-name-specifier [opt] ~ type-name
7040 If either of the first two productions is used, sets *SCOPE to the
7041 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7042 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7043 or ERROR_MARK_NODE if the parse fails. */
7045 static void
7046 cp_parser_pseudo_destructor_name (cp_parser* parser,
7047 tree object,
7048 tree* scope,
7049 tree* type)
7051 bool nested_name_specifier_p;
7053 /* Handle ~auto. */
7054 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7055 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7056 && !type_dependent_expression_p (object))
7058 if (cxx_dialect < cxx14)
7059 pedwarn (input_location, 0,
7060 "%<~auto%> only available with "
7061 "-std=c++14 or -std=gnu++14");
7062 cp_lexer_consume_token (parser->lexer);
7063 cp_lexer_consume_token (parser->lexer);
7064 *scope = NULL_TREE;
7065 *type = TREE_TYPE (object);
7066 return;
7069 /* Assume that things will not work out. */
7070 *type = error_mark_node;
7072 /* Look for the optional `::' operator. */
7073 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7074 /* Look for the optional nested-name-specifier. */
7075 nested_name_specifier_p
7076 = (cp_parser_nested_name_specifier_opt (parser,
7077 /*typename_keyword_p=*/false,
7078 /*check_dependency_p=*/true,
7079 /*type_p=*/false,
7080 /*is_declaration=*/false)
7081 != NULL_TREE);
7082 /* Now, if we saw a nested-name-specifier, we might be doing the
7083 second production. */
7084 if (nested_name_specifier_p
7085 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7087 /* Consume the `template' keyword. */
7088 cp_lexer_consume_token (parser->lexer);
7089 /* Parse the template-id. */
7090 cp_parser_template_id (parser,
7091 /*template_keyword_p=*/true,
7092 /*check_dependency_p=*/false,
7093 class_type,
7094 /*is_declaration=*/true);
7095 /* Look for the `::' token. */
7096 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7098 /* If the next token is not a `~', then there might be some
7099 additional qualification. */
7100 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7102 /* At this point, we're looking for "type-name :: ~". The type-name
7103 must not be a class-name, since this is a pseudo-destructor. So,
7104 it must be either an enum-name, or a typedef-name -- both of which
7105 are just identifiers. So, we peek ahead to check that the "::"
7106 and "~" tokens are present; if they are not, then we can avoid
7107 calling type_name. */
7108 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7109 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7110 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7112 cp_parser_error (parser, "non-scalar type");
7113 return;
7116 /* Look for the type-name. */
7117 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7118 if (*scope == error_mark_node)
7119 return;
7121 /* Look for the `::' token. */
7122 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7124 else
7125 *scope = NULL_TREE;
7127 /* Look for the `~'. */
7128 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7130 /* Once we see the ~, this has to be a pseudo-destructor. */
7131 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7132 cp_parser_commit_to_topmost_tentative_parse (parser);
7134 /* Look for the type-name again. We are not responsible for
7135 checking that it matches the first type-name. */
7136 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7139 /* Parse a unary-expression.
7141 unary-expression:
7142 postfix-expression
7143 ++ cast-expression
7144 -- cast-expression
7145 unary-operator cast-expression
7146 sizeof unary-expression
7147 sizeof ( type-id )
7148 alignof ( type-id ) [C++0x]
7149 new-expression
7150 delete-expression
7152 GNU Extensions:
7154 unary-expression:
7155 __extension__ cast-expression
7156 __alignof__ unary-expression
7157 __alignof__ ( type-id )
7158 alignof unary-expression [C++0x]
7159 __real__ cast-expression
7160 __imag__ cast-expression
7161 && identifier
7162 sizeof ( type-id ) { initializer-list , [opt] }
7163 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7164 __alignof__ ( type-id ) { initializer-list , [opt] }
7166 ADDRESS_P is true iff the unary-expression is appearing as the
7167 operand of the `&' operator. CAST_P is true if this expression is
7168 the target of a cast.
7170 Returns a representation of the expression. */
7172 static tree
7173 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7174 bool address_p, bool cast_p, bool decltype_p)
7176 cp_token *token;
7177 enum tree_code unary_operator;
7179 /* Peek at the next token. */
7180 token = cp_lexer_peek_token (parser->lexer);
7181 /* Some keywords give away the kind of expression. */
7182 if (token->type == CPP_KEYWORD)
7184 enum rid keyword = token->keyword;
7186 switch (keyword)
7188 case RID_ALIGNOF:
7189 case RID_SIZEOF:
7191 tree operand, ret;
7192 enum tree_code op;
7193 location_t first_loc;
7195 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7196 /* Consume the token. */
7197 cp_lexer_consume_token (parser->lexer);
7198 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7199 /* Parse the operand. */
7200 operand = cp_parser_sizeof_operand (parser, keyword);
7202 if (TYPE_P (operand))
7203 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7204 else
7206 /* ISO C++ defines alignof only with types, not with
7207 expressions. So pedwarn if alignof is used with a non-
7208 type expression. However, __alignof__ is ok. */
7209 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7210 pedwarn (token->location, OPT_Wpedantic,
7211 "ISO C++ does not allow %<alignof%> "
7212 "with a non-type");
7214 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7216 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7217 SIZEOF_EXPR with the original operand. */
7218 if (op == SIZEOF_EXPR && ret != error_mark_node)
7220 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7222 if (!processing_template_decl && TYPE_P (operand))
7224 ret = build_min (SIZEOF_EXPR, size_type_node,
7225 build1 (NOP_EXPR, operand,
7226 error_mark_node));
7227 SIZEOF_EXPR_TYPE_P (ret) = 1;
7229 else
7230 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7231 TREE_SIDE_EFFECTS (ret) = 0;
7232 TREE_READONLY (ret) = 1;
7234 SET_EXPR_LOCATION (ret, first_loc);
7236 return ret;
7239 case RID_NEW:
7240 return cp_parser_new_expression (parser);
7242 case RID_DELETE:
7243 return cp_parser_delete_expression (parser);
7245 case RID_EXTENSION:
7247 /* The saved value of the PEDANTIC flag. */
7248 int saved_pedantic;
7249 tree expr;
7251 /* Save away the PEDANTIC flag. */
7252 cp_parser_extension_opt (parser, &saved_pedantic);
7253 /* Parse the cast-expression. */
7254 expr = cp_parser_simple_cast_expression (parser);
7255 /* Restore the PEDANTIC flag. */
7256 pedantic = saved_pedantic;
7258 return expr;
7261 case RID_REALPART:
7262 case RID_IMAGPART:
7264 tree expression;
7266 /* Consume the `__real__' or `__imag__' token. */
7267 cp_lexer_consume_token (parser->lexer);
7268 /* Parse the cast-expression. */
7269 expression = cp_parser_simple_cast_expression (parser);
7270 /* Create the complete representation. */
7271 return build_x_unary_op (token->location,
7272 (keyword == RID_REALPART
7273 ? REALPART_EXPR : IMAGPART_EXPR),
7274 expression,
7275 tf_warning_or_error);
7277 break;
7279 case RID_TRANSACTION_ATOMIC:
7280 case RID_TRANSACTION_RELAXED:
7281 return cp_parser_transaction_expression (parser, keyword);
7283 case RID_NOEXCEPT:
7285 tree expr;
7286 const char *saved_message;
7287 bool saved_integral_constant_expression_p;
7288 bool saved_non_integral_constant_expression_p;
7289 bool saved_greater_than_is_operator_p;
7291 cp_lexer_consume_token (parser->lexer);
7292 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7294 saved_message = parser->type_definition_forbidden_message;
7295 parser->type_definition_forbidden_message
7296 = G_("types may not be defined in %<noexcept%> expressions");
7298 saved_integral_constant_expression_p
7299 = parser->integral_constant_expression_p;
7300 saved_non_integral_constant_expression_p
7301 = parser->non_integral_constant_expression_p;
7302 parser->integral_constant_expression_p = false;
7304 saved_greater_than_is_operator_p
7305 = parser->greater_than_is_operator_p;
7306 parser->greater_than_is_operator_p = true;
7308 ++cp_unevaluated_operand;
7309 ++c_inhibit_evaluation_warnings;
7310 ++cp_noexcept_operand;
7311 expr = cp_parser_expression (parser);
7312 --cp_noexcept_operand;
7313 --c_inhibit_evaluation_warnings;
7314 --cp_unevaluated_operand;
7316 parser->greater_than_is_operator_p
7317 = saved_greater_than_is_operator_p;
7319 parser->integral_constant_expression_p
7320 = saved_integral_constant_expression_p;
7321 parser->non_integral_constant_expression_p
7322 = saved_non_integral_constant_expression_p;
7324 parser->type_definition_forbidden_message = saved_message;
7326 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7327 return finish_noexcept_expr (expr, tf_warning_or_error);
7330 default:
7331 break;
7335 /* Look for the `:: new' and `:: delete', which also signal the
7336 beginning of a new-expression, or delete-expression,
7337 respectively. If the next token is `::', then it might be one of
7338 these. */
7339 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7341 enum rid keyword;
7343 /* See if the token after the `::' is one of the keywords in
7344 which we're interested. */
7345 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7346 /* If it's `new', we have a new-expression. */
7347 if (keyword == RID_NEW)
7348 return cp_parser_new_expression (parser);
7349 /* Similarly, for `delete'. */
7350 else if (keyword == RID_DELETE)
7351 return cp_parser_delete_expression (parser);
7354 /* Look for a unary operator. */
7355 unary_operator = cp_parser_unary_operator (token);
7356 /* The `++' and `--' operators can be handled similarly, even though
7357 they are not technically unary-operators in the grammar. */
7358 if (unary_operator == ERROR_MARK)
7360 if (token->type == CPP_PLUS_PLUS)
7361 unary_operator = PREINCREMENT_EXPR;
7362 else if (token->type == CPP_MINUS_MINUS)
7363 unary_operator = PREDECREMENT_EXPR;
7364 /* Handle the GNU address-of-label extension. */
7365 else if (cp_parser_allow_gnu_extensions_p (parser)
7366 && token->type == CPP_AND_AND)
7368 tree identifier;
7369 tree expression;
7370 location_t loc = token->location;
7372 /* Consume the '&&' token. */
7373 cp_lexer_consume_token (parser->lexer);
7374 /* Look for the identifier. */
7375 identifier = cp_parser_identifier (parser);
7376 /* Create an expression representing the address. */
7377 expression = finish_label_address_expr (identifier, loc);
7378 if (cp_parser_non_integral_constant_expression (parser,
7379 NIC_ADDR_LABEL))
7380 expression = error_mark_node;
7381 return expression;
7384 if (unary_operator != ERROR_MARK)
7386 tree cast_expression;
7387 tree expression = error_mark_node;
7388 non_integral_constant non_constant_p = NIC_NONE;
7389 location_t loc = token->location;
7390 tsubst_flags_t complain = complain_flags (decltype_p);
7392 /* Consume the operator token. */
7393 token = cp_lexer_consume_token (parser->lexer);
7394 /* Parse the cast-expression. */
7395 cast_expression
7396 = cp_parser_cast_expression (parser,
7397 unary_operator == ADDR_EXPR,
7398 /*cast_p=*/false,
7399 /*decltype*/false,
7400 pidk);
7401 /* Now, build an appropriate representation. */
7402 switch (unary_operator)
7404 case INDIRECT_REF:
7405 non_constant_p = NIC_STAR;
7406 expression = build_x_indirect_ref (loc, cast_expression,
7407 RO_UNARY_STAR,
7408 complain);
7409 break;
7411 case ADDR_EXPR:
7412 non_constant_p = NIC_ADDR;
7413 /* Fall through. */
7414 case BIT_NOT_EXPR:
7415 expression = build_x_unary_op (loc, unary_operator,
7416 cast_expression,
7417 complain);
7418 break;
7420 case PREINCREMENT_EXPR:
7421 case PREDECREMENT_EXPR:
7422 non_constant_p = unary_operator == PREINCREMENT_EXPR
7423 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7424 /* Fall through. */
7425 case UNARY_PLUS_EXPR:
7426 case NEGATE_EXPR:
7427 case TRUTH_NOT_EXPR:
7428 expression = finish_unary_op_expr (loc, unary_operator,
7429 cast_expression, complain);
7430 break;
7432 default:
7433 gcc_unreachable ();
7436 if (non_constant_p != NIC_NONE
7437 && cp_parser_non_integral_constant_expression (parser,
7438 non_constant_p))
7439 expression = error_mark_node;
7441 return expression;
7444 return cp_parser_postfix_expression (parser, address_p, cast_p,
7445 /*member_access_only_p=*/false,
7446 decltype_p,
7447 pidk);
7450 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7451 unary-operator, the corresponding tree code is returned. */
7453 static enum tree_code
7454 cp_parser_unary_operator (cp_token* token)
7456 switch (token->type)
7458 case CPP_MULT:
7459 return INDIRECT_REF;
7461 case CPP_AND:
7462 return ADDR_EXPR;
7464 case CPP_PLUS:
7465 return UNARY_PLUS_EXPR;
7467 case CPP_MINUS:
7468 return NEGATE_EXPR;
7470 case CPP_NOT:
7471 return TRUTH_NOT_EXPR;
7473 case CPP_COMPL:
7474 return BIT_NOT_EXPR;
7476 default:
7477 return ERROR_MARK;
7481 /* Parse a new-expression.
7483 new-expression:
7484 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7485 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7487 Returns a representation of the expression. */
7489 static tree
7490 cp_parser_new_expression (cp_parser* parser)
7492 bool global_scope_p;
7493 vec<tree, va_gc> *placement;
7494 tree type;
7495 vec<tree, va_gc> *initializer;
7496 tree nelts = NULL_TREE;
7497 tree ret;
7499 /* Look for the optional `::' operator. */
7500 global_scope_p
7501 = (cp_parser_global_scope_opt (parser,
7502 /*current_scope_valid_p=*/false)
7503 != NULL_TREE);
7504 /* Look for the `new' operator. */
7505 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7506 /* There's no easy way to tell a new-placement from the
7507 `( type-id )' construct. */
7508 cp_parser_parse_tentatively (parser);
7509 /* Look for a new-placement. */
7510 placement = cp_parser_new_placement (parser);
7511 /* If that didn't work out, there's no new-placement. */
7512 if (!cp_parser_parse_definitely (parser))
7514 if (placement != NULL)
7515 release_tree_vector (placement);
7516 placement = NULL;
7519 /* If the next token is a `(', then we have a parenthesized
7520 type-id. */
7521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7523 cp_token *token;
7524 const char *saved_message = parser->type_definition_forbidden_message;
7526 /* Consume the `('. */
7527 cp_lexer_consume_token (parser->lexer);
7529 /* Parse the type-id. */
7530 parser->type_definition_forbidden_message
7531 = G_("types may not be defined in a new-expression");
7532 type = cp_parser_type_id (parser);
7533 parser->type_definition_forbidden_message = saved_message;
7535 /* Look for the closing `)'. */
7536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7537 token = cp_lexer_peek_token (parser->lexer);
7538 /* There should not be a direct-new-declarator in this production,
7539 but GCC used to allowed this, so we check and emit a sensible error
7540 message for this case. */
7541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7543 error_at (token->location,
7544 "array bound forbidden after parenthesized type-id");
7545 inform (token->location,
7546 "try removing the parentheses around the type-id");
7547 cp_parser_direct_new_declarator (parser);
7550 /* Otherwise, there must be a new-type-id. */
7551 else
7552 type = cp_parser_new_type_id (parser, &nelts);
7554 /* If the next token is a `(' or '{', then we have a new-initializer. */
7555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7556 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7557 initializer = cp_parser_new_initializer (parser);
7558 else
7559 initializer = NULL;
7561 /* A new-expression may not appear in an integral constant
7562 expression. */
7563 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7564 ret = error_mark_node;
7565 else
7567 /* Create a representation of the new-expression. */
7568 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7569 tf_warning_or_error);
7572 if (placement != NULL)
7573 release_tree_vector (placement);
7574 if (initializer != NULL)
7575 release_tree_vector (initializer);
7577 return ret;
7580 /* Parse a new-placement.
7582 new-placement:
7583 ( expression-list )
7585 Returns the same representation as for an expression-list. */
7587 static vec<tree, va_gc> *
7588 cp_parser_new_placement (cp_parser* parser)
7590 vec<tree, va_gc> *expression_list;
7592 /* Parse the expression-list. */
7593 expression_list = (cp_parser_parenthesized_expression_list
7594 (parser, non_attr, /*cast_p=*/false,
7595 /*allow_expansion_p=*/true,
7596 /*non_constant_p=*/NULL));
7598 return expression_list;
7601 /* Parse a new-type-id.
7603 new-type-id:
7604 type-specifier-seq new-declarator [opt]
7606 Returns the TYPE allocated. If the new-type-id indicates an array
7607 type, *NELTS is set to the number of elements in the last array
7608 bound; the TYPE will not include the last array bound. */
7610 static tree
7611 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7613 cp_decl_specifier_seq type_specifier_seq;
7614 cp_declarator *new_declarator;
7615 cp_declarator *declarator;
7616 cp_declarator *outer_declarator;
7617 const char *saved_message;
7619 /* The type-specifier sequence must not contain type definitions.
7620 (It cannot contain declarations of new types either, but if they
7621 are not definitions we will catch that because they are not
7622 complete.) */
7623 saved_message = parser->type_definition_forbidden_message;
7624 parser->type_definition_forbidden_message
7625 = G_("types may not be defined in a new-type-id");
7626 /* Parse the type-specifier-seq. */
7627 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7628 /*is_trailing_return=*/false,
7629 &type_specifier_seq);
7630 /* Restore the old message. */
7631 parser->type_definition_forbidden_message = saved_message;
7633 if (type_specifier_seq.type == error_mark_node)
7634 return error_mark_node;
7636 /* Parse the new-declarator. */
7637 new_declarator = cp_parser_new_declarator_opt (parser);
7639 /* Determine the number of elements in the last array dimension, if
7640 any. */
7641 *nelts = NULL_TREE;
7642 /* Skip down to the last array dimension. */
7643 declarator = new_declarator;
7644 outer_declarator = NULL;
7645 while (declarator && (declarator->kind == cdk_pointer
7646 || declarator->kind == cdk_ptrmem))
7648 outer_declarator = declarator;
7649 declarator = declarator->declarator;
7651 while (declarator
7652 && declarator->kind == cdk_array
7653 && declarator->declarator
7654 && declarator->declarator->kind == cdk_array)
7656 outer_declarator = declarator;
7657 declarator = declarator->declarator;
7660 if (declarator && declarator->kind == cdk_array)
7662 *nelts = declarator->u.array.bounds;
7663 if (*nelts == error_mark_node)
7664 *nelts = integer_one_node;
7666 if (outer_declarator)
7667 outer_declarator->declarator = declarator->declarator;
7668 else
7669 new_declarator = NULL;
7672 return groktypename (&type_specifier_seq, new_declarator, false);
7675 /* Parse an (optional) new-declarator.
7677 new-declarator:
7678 ptr-operator new-declarator [opt]
7679 direct-new-declarator
7681 Returns the declarator. */
7683 static cp_declarator *
7684 cp_parser_new_declarator_opt (cp_parser* parser)
7686 enum tree_code code;
7687 tree type, std_attributes = NULL_TREE;
7688 cp_cv_quals cv_quals;
7690 /* We don't know if there's a ptr-operator next, or not. */
7691 cp_parser_parse_tentatively (parser);
7692 /* Look for a ptr-operator. */
7693 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7694 /* If that worked, look for more new-declarators. */
7695 if (cp_parser_parse_definitely (parser))
7697 cp_declarator *declarator;
7699 /* Parse another optional declarator. */
7700 declarator = cp_parser_new_declarator_opt (parser);
7702 declarator = cp_parser_make_indirect_declarator
7703 (code, type, cv_quals, declarator, std_attributes);
7705 return declarator;
7708 /* If the next token is a `[', there is a direct-new-declarator. */
7709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7710 return cp_parser_direct_new_declarator (parser);
7712 return NULL;
7715 /* Parse a direct-new-declarator.
7717 direct-new-declarator:
7718 [ expression ]
7719 direct-new-declarator [constant-expression]
7723 static cp_declarator *
7724 cp_parser_direct_new_declarator (cp_parser* parser)
7726 cp_declarator *declarator = NULL;
7728 while (true)
7730 tree expression;
7731 cp_token *token;
7733 /* Look for the opening `['. */
7734 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7736 token = cp_lexer_peek_token (parser->lexer);
7737 expression = cp_parser_expression (parser);
7738 /* The standard requires that the expression have integral
7739 type. DR 74 adds enumeration types. We believe that the
7740 real intent is that these expressions be handled like the
7741 expression in a `switch' condition, which also allows
7742 classes with a single conversion to integral or
7743 enumeration type. */
7744 if (!processing_template_decl)
7746 expression
7747 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7748 expression,
7749 /*complain=*/true);
7750 if (!expression)
7752 error_at (token->location,
7753 "expression in new-declarator must have integral "
7754 "or enumeration type");
7755 expression = error_mark_node;
7759 /* Look for the closing `]'. */
7760 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7762 /* Add this bound to the declarator. */
7763 declarator = make_array_declarator (declarator, expression);
7765 /* If the next token is not a `[', then there are no more
7766 bounds. */
7767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7768 break;
7771 return declarator;
7774 /* Parse a new-initializer.
7776 new-initializer:
7777 ( expression-list [opt] )
7778 braced-init-list
7780 Returns a representation of the expression-list. */
7782 static vec<tree, va_gc> *
7783 cp_parser_new_initializer (cp_parser* parser)
7785 vec<tree, va_gc> *expression_list;
7787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7789 tree t;
7790 bool expr_non_constant_p;
7791 cp_lexer_set_source_position (parser->lexer);
7792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7793 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7794 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7795 expression_list = make_tree_vector_single (t);
7797 else
7798 expression_list = (cp_parser_parenthesized_expression_list
7799 (parser, non_attr, /*cast_p=*/false,
7800 /*allow_expansion_p=*/true,
7801 /*non_constant_p=*/NULL));
7803 return expression_list;
7806 /* Parse a delete-expression.
7808 delete-expression:
7809 :: [opt] delete cast-expression
7810 :: [opt] delete [ ] cast-expression
7812 Returns a representation of the expression. */
7814 static tree
7815 cp_parser_delete_expression (cp_parser* parser)
7817 bool global_scope_p;
7818 bool array_p;
7819 tree expression;
7821 /* Look for the optional `::' operator. */
7822 global_scope_p
7823 = (cp_parser_global_scope_opt (parser,
7824 /*current_scope_valid_p=*/false)
7825 != NULL_TREE);
7826 /* Look for the `delete' keyword. */
7827 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7828 /* See if the array syntax is in use. */
7829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7831 /* Consume the `[' token. */
7832 cp_lexer_consume_token (parser->lexer);
7833 /* Look for the `]' token. */
7834 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7835 /* Remember that this is the `[]' construct. */
7836 array_p = true;
7838 else
7839 array_p = false;
7841 /* Parse the cast-expression. */
7842 expression = cp_parser_simple_cast_expression (parser);
7844 /* A delete-expression may not appear in an integral constant
7845 expression. */
7846 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7847 return error_mark_node;
7849 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7850 tf_warning_or_error);
7853 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7854 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7855 0 otherwise. */
7857 static int
7858 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7860 cp_token *token = cp_lexer_peek_token (parser->lexer);
7861 switch (token->type)
7863 case CPP_COMMA:
7864 case CPP_SEMICOLON:
7865 case CPP_QUERY:
7866 case CPP_COLON:
7867 case CPP_CLOSE_SQUARE:
7868 case CPP_CLOSE_PAREN:
7869 case CPP_CLOSE_BRACE:
7870 case CPP_OPEN_BRACE:
7871 case CPP_DOT:
7872 case CPP_DOT_STAR:
7873 case CPP_DEREF:
7874 case CPP_DEREF_STAR:
7875 case CPP_DIV:
7876 case CPP_MOD:
7877 case CPP_LSHIFT:
7878 case CPP_RSHIFT:
7879 case CPP_LESS:
7880 case CPP_GREATER:
7881 case CPP_LESS_EQ:
7882 case CPP_GREATER_EQ:
7883 case CPP_EQ_EQ:
7884 case CPP_NOT_EQ:
7885 case CPP_EQ:
7886 case CPP_MULT_EQ:
7887 case CPP_DIV_EQ:
7888 case CPP_MOD_EQ:
7889 case CPP_PLUS_EQ:
7890 case CPP_MINUS_EQ:
7891 case CPP_RSHIFT_EQ:
7892 case CPP_LSHIFT_EQ:
7893 case CPP_AND_EQ:
7894 case CPP_XOR_EQ:
7895 case CPP_OR_EQ:
7896 case CPP_XOR:
7897 case CPP_OR:
7898 case CPP_OR_OR:
7899 case CPP_EOF:
7900 case CPP_ELLIPSIS:
7901 return 0;
7903 case CPP_OPEN_PAREN:
7904 /* In ((type ()) () the last () isn't a valid cast-expression,
7905 so the whole must be parsed as postfix-expression. */
7906 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7907 != CPP_CLOSE_PAREN;
7909 case CPP_OPEN_SQUARE:
7910 /* '[' may start a primary-expression in obj-c++ and in C++11,
7911 as a lambda-expression, eg, '(void)[]{}'. */
7912 if (cxx_dialect >= cxx11)
7913 return -1;
7914 return c_dialect_objc ();
7916 case CPP_PLUS_PLUS:
7917 case CPP_MINUS_MINUS:
7918 /* '++' and '--' may or may not start a cast-expression:
7920 struct T { void operator++(int); };
7921 void f() { (T())++; }
7925 int a;
7926 (int)++a; */
7927 return -1;
7929 default:
7930 return 1;
7934 /* Parse a cast-expression.
7936 cast-expression:
7937 unary-expression
7938 ( type-id ) cast-expression
7940 ADDRESS_P is true iff the unary-expression is appearing as the
7941 operand of the `&' operator. CAST_P is true if this expression is
7942 the target of a cast.
7944 Returns a representation of the expression. */
7946 static tree
7947 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7948 bool decltype_p, cp_id_kind * pidk)
7950 /* If it's a `(', then we might be looking at a cast. */
7951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7953 tree type = NULL_TREE;
7954 tree expr = NULL_TREE;
7955 int cast_expression = 0;
7956 const char *saved_message;
7958 /* There's no way to know yet whether or not this is a cast.
7959 For example, `(int (3))' is a unary-expression, while `(int)
7960 3' is a cast. So, we resort to parsing tentatively. */
7961 cp_parser_parse_tentatively (parser);
7962 /* Types may not be defined in a cast. */
7963 saved_message = parser->type_definition_forbidden_message;
7964 parser->type_definition_forbidden_message
7965 = G_("types may not be defined in casts");
7966 /* Consume the `('. */
7967 cp_lexer_consume_token (parser->lexer);
7968 /* A very tricky bit is that `(struct S) { 3 }' is a
7969 compound-literal (which we permit in C++ as an extension).
7970 But, that construct is not a cast-expression -- it is a
7971 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7972 is legal; if the compound-literal were a cast-expression,
7973 you'd need an extra set of parentheses.) But, if we parse
7974 the type-id, and it happens to be a class-specifier, then we
7975 will commit to the parse at that point, because we cannot
7976 undo the action that is done when creating a new class. So,
7977 then we cannot back up and do a postfix-expression.
7979 Another tricky case is the following (c++/29234):
7981 struct S { void operator () (); };
7983 void foo ()
7985 ( S()() );
7988 As a type-id we parse the parenthesized S()() as a function
7989 returning a function, groktypename complains and we cannot
7990 back up in this case either.
7992 Therefore, we scan ahead to the closing `)', and check to see
7993 if the tokens after the `)' can start a cast-expression. Otherwise
7994 we are dealing with an unary-expression, a postfix-expression
7995 or something else.
7997 Yet another tricky case, in C++11, is the following (c++/54891):
7999 (void)[]{};
8001 The issue is that usually, besides the case of lambda-expressions,
8002 the parenthesized type-id cannot be followed by '[', and, eg, we
8003 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8004 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8005 we don't commit, we try a cast-expression, then an unary-expression.
8007 Save tokens so that we can put them back. */
8008 cp_lexer_save_tokens (parser->lexer);
8010 /* We may be looking at a cast-expression. */
8011 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8012 /*consume_paren=*/true))
8013 cast_expression
8014 = cp_parser_tokens_start_cast_expression (parser);
8016 /* Roll back the tokens we skipped. */
8017 cp_lexer_rollback_tokens (parser->lexer);
8018 /* If we aren't looking at a cast-expression, simulate an error so
8019 that the call to cp_parser_error_occurred below returns true. */
8020 if (!cast_expression)
8021 cp_parser_simulate_error (parser);
8022 else
8024 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8025 parser->in_type_id_in_expr_p = true;
8026 /* Look for the type-id. */
8027 type = cp_parser_type_id (parser);
8028 /* Look for the closing `)'. */
8029 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8030 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8033 /* Restore the saved message. */
8034 parser->type_definition_forbidden_message = saved_message;
8036 /* At this point this can only be either a cast or a
8037 parenthesized ctor such as `(T ())' that looks like a cast to
8038 function returning T. */
8039 if (!cp_parser_error_occurred (parser))
8041 /* Only commit if the cast-expression doesn't start with
8042 '++', '--', or '[' in C++11. */
8043 if (cast_expression > 0)
8044 cp_parser_commit_to_topmost_tentative_parse (parser);
8046 expr = cp_parser_cast_expression (parser,
8047 /*address_p=*/false,
8048 /*cast_p=*/true,
8049 /*decltype_p=*/false,
8050 pidk);
8052 if (cp_parser_parse_definitely (parser))
8054 /* Warn about old-style casts, if so requested. */
8055 if (warn_old_style_cast
8056 && !in_system_header_at (input_location)
8057 && !VOID_TYPE_P (type)
8058 && current_lang_name != lang_name_c)
8059 warning (OPT_Wold_style_cast, "use of old-style cast");
8061 /* Only type conversions to integral or enumeration types
8062 can be used in constant-expressions. */
8063 if (!cast_valid_in_integral_constant_expression_p (type)
8064 && cp_parser_non_integral_constant_expression (parser,
8065 NIC_CAST))
8066 return error_mark_node;
8068 /* Perform the cast. */
8069 expr = build_c_cast (input_location, type, expr);
8070 return expr;
8073 else
8074 cp_parser_abort_tentative_parse (parser);
8077 /* If we get here, then it's not a cast, so it must be a
8078 unary-expression. */
8079 return cp_parser_unary_expression (parser, pidk, address_p,
8080 cast_p, decltype_p);
8083 /* Parse a binary expression of the general form:
8085 pm-expression:
8086 cast-expression
8087 pm-expression .* cast-expression
8088 pm-expression ->* cast-expression
8090 multiplicative-expression:
8091 pm-expression
8092 multiplicative-expression * pm-expression
8093 multiplicative-expression / pm-expression
8094 multiplicative-expression % pm-expression
8096 additive-expression:
8097 multiplicative-expression
8098 additive-expression + multiplicative-expression
8099 additive-expression - multiplicative-expression
8101 shift-expression:
8102 additive-expression
8103 shift-expression << additive-expression
8104 shift-expression >> additive-expression
8106 relational-expression:
8107 shift-expression
8108 relational-expression < shift-expression
8109 relational-expression > shift-expression
8110 relational-expression <= shift-expression
8111 relational-expression >= shift-expression
8113 GNU Extension:
8115 relational-expression:
8116 relational-expression <? shift-expression
8117 relational-expression >? shift-expression
8119 equality-expression:
8120 relational-expression
8121 equality-expression == relational-expression
8122 equality-expression != relational-expression
8124 and-expression:
8125 equality-expression
8126 and-expression & equality-expression
8128 exclusive-or-expression:
8129 and-expression
8130 exclusive-or-expression ^ and-expression
8132 inclusive-or-expression:
8133 exclusive-or-expression
8134 inclusive-or-expression | exclusive-or-expression
8136 logical-and-expression:
8137 inclusive-or-expression
8138 logical-and-expression && inclusive-or-expression
8140 logical-or-expression:
8141 logical-and-expression
8142 logical-or-expression || logical-and-expression
8144 All these are implemented with a single function like:
8146 binary-expression:
8147 simple-cast-expression
8148 binary-expression <token> binary-expression
8150 CAST_P is true if this expression is the target of a cast.
8152 The binops_by_token map is used to get the tree codes for each <token> type.
8153 binary-expressions are associated according to a precedence table. */
8155 #define TOKEN_PRECEDENCE(token) \
8156 (((token->type == CPP_GREATER \
8157 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8158 && !parser->greater_than_is_operator_p) \
8159 ? PREC_NOT_OPERATOR \
8160 : binops_by_token[token->type].prec)
8162 static tree
8163 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8164 bool no_toplevel_fold_p,
8165 bool decltype_p,
8166 enum cp_parser_prec prec,
8167 cp_id_kind * pidk)
8169 cp_parser_expression_stack stack;
8170 cp_parser_expression_stack_entry *sp = &stack[0];
8171 cp_parser_expression_stack_entry current;
8172 tree rhs;
8173 cp_token *token;
8174 enum tree_code rhs_type;
8175 enum cp_parser_prec new_prec, lookahead_prec;
8176 tree overload;
8178 /* Parse the first expression. */
8179 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8180 ? TRUTH_NOT_EXPR : ERROR_MARK);
8181 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8182 cast_p, decltype_p, pidk);
8183 current.prec = prec;
8185 if (cp_parser_error_occurred (parser))
8186 return error_mark_node;
8188 for (;;)
8190 /* Get an operator token. */
8191 token = cp_lexer_peek_token (parser->lexer);
8193 if (warn_cxx0x_compat
8194 && token->type == CPP_RSHIFT
8195 && !parser->greater_than_is_operator_p)
8197 if (warning_at (token->location, OPT_Wc__0x_compat,
8198 "%<>>%> operator is treated"
8199 " as two right angle brackets in C++11"))
8200 inform (token->location,
8201 "suggest parentheses around %<>>%> expression");
8204 new_prec = TOKEN_PRECEDENCE (token);
8206 /* Popping an entry off the stack means we completed a subexpression:
8207 - either we found a token which is not an operator (`>' where it is not
8208 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8209 will happen repeatedly;
8210 - or, we found an operator which has lower priority. This is the case
8211 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8212 parsing `3 * 4'. */
8213 if (new_prec <= current.prec)
8215 if (sp == stack)
8216 break;
8217 else
8218 goto pop;
8221 get_rhs:
8222 current.tree_type = binops_by_token[token->type].tree_type;
8223 current.loc = token->location;
8225 /* We used the operator token. */
8226 cp_lexer_consume_token (parser->lexer);
8228 /* For "false && x" or "true || x", x will never be executed;
8229 disable warnings while evaluating it. */
8230 if (current.tree_type == TRUTH_ANDIF_EXPR)
8231 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8232 else if (current.tree_type == TRUTH_ORIF_EXPR)
8233 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8235 /* Extract another operand. It may be the RHS of this expression
8236 or the LHS of a new, higher priority expression. */
8237 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8238 ? TRUTH_NOT_EXPR : ERROR_MARK);
8239 rhs = cp_parser_simple_cast_expression (parser);
8241 /* Get another operator token. Look up its precedence to avoid
8242 building a useless (immediately popped) stack entry for common
8243 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8244 token = cp_lexer_peek_token (parser->lexer);
8245 lookahead_prec = TOKEN_PRECEDENCE (token);
8246 if (lookahead_prec > new_prec)
8248 /* ... and prepare to parse the RHS of the new, higher priority
8249 expression. Since precedence levels on the stack are
8250 monotonically increasing, we do not have to care about
8251 stack overflows. */
8252 *sp = current;
8253 ++sp;
8254 current.lhs = rhs;
8255 current.lhs_type = rhs_type;
8256 current.prec = new_prec;
8257 new_prec = lookahead_prec;
8258 goto get_rhs;
8260 pop:
8261 lookahead_prec = new_prec;
8262 /* If the stack is not empty, we have parsed into LHS the right side
8263 (`4' in the example above) of an expression we had suspended.
8264 We can use the information on the stack to recover the LHS (`3')
8265 from the stack together with the tree code (`MULT_EXPR'), and
8266 the precedence of the higher level subexpression
8267 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8268 which will be used to actually build the additive expression. */
8269 rhs = current.lhs;
8270 rhs_type = current.lhs_type;
8271 --sp;
8272 current = *sp;
8275 /* Undo the disabling of warnings done above. */
8276 if (current.tree_type == TRUTH_ANDIF_EXPR)
8277 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8278 else if (current.tree_type == TRUTH_ORIF_EXPR)
8279 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8281 if (warn_logical_not_paren
8282 && current.lhs_type == TRUTH_NOT_EXPR)
8283 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8285 overload = NULL;
8286 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8287 ERROR_MARK for everything that is not a binary expression.
8288 This makes warn_about_parentheses miss some warnings that
8289 involve unary operators. For unary expressions we should
8290 pass the correct tree_code unless the unary expression was
8291 surrounded by parentheses.
8293 if (no_toplevel_fold_p
8294 && lookahead_prec <= current.prec
8295 && sp == stack)
8296 current.lhs = build2 (current.tree_type,
8297 TREE_CODE_CLASS (current.tree_type)
8298 == tcc_comparison
8299 ? boolean_type_node : TREE_TYPE (current.lhs),
8300 current.lhs, rhs);
8301 else
8302 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8303 current.lhs, current.lhs_type,
8304 rhs, rhs_type, &overload,
8305 complain_flags (decltype_p));
8306 current.lhs_type = current.tree_type;
8307 if (EXPR_P (current.lhs))
8308 SET_EXPR_LOCATION (current.lhs, current.loc);
8310 /* If the binary operator required the use of an overloaded operator,
8311 then this expression cannot be an integral constant-expression.
8312 An overloaded operator can be used even if both operands are
8313 otherwise permissible in an integral constant-expression if at
8314 least one of the operands is of enumeration type. */
8316 if (overload
8317 && cp_parser_non_integral_constant_expression (parser,
8318 NIC_OVERLOADED))
8319 return error_mark_node;
8322 return current.lhs;
8325 static tree
8326 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8327 bool no_toplevel_fold_p,
8328 enum cp_parser_prec prec,
8329 cp_id_kind * pidk)
8331 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8332 /*decltype*/false, prec, pidk);
8335 /* Parse the `? expression : assignment-expression' part of a
8336 conditional-expression. The LOGICAL_OR_EXPR is the
8337 logical-or-expression that started the conditional-expression.
8338 Returns a representation of the entire conditional-expression.
8340 This routine is used by cp_parser_assignment_expression.
8342 ? expression : assignment-expression
8344 GNU Extensions:
8346 ? : assignment-expression */
8348 static tree
8349 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8351 tree expr;
8352 tree assignment_expr;
8353 struct cp_token *token;
8354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8356 /* Consume the `?' token. */
8357 cp_lexer_consume_token (parser->lexer);
8358 token = cp_lexer_peek_token (parser->lexer);
8359 if (cp_parser_allow_gnu_extensions_p (parser)
8360 && token->type == CPP_COLON)
8362 pedwarn (token->location, OPT_Wpedantic,
8363 "ISO C++ does not allow ?: with omitted middle operand");
8364 /* Implicit true clause. */
8365 expr = NULL_TREE;
8366 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8367 warn_for_omitted_condop (token->location, logical_or_expr);
8369 else
8371 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8372 parser->colon_corrects_to_scope_p = false;
8373 /* Parse the expression. */
8374 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8375 expr = cp_parser_expression (parser);
8376 c_inhibit_evaluation_warnings +=
8377 ((logical_or_expr == truthvalue_true_node)
8378 - (logical_or_expr == truthvalue_false_node));
8379 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8382 /* The next token should be a `:'. */
8383 cp_parser_require (parser, CPP_COLON, RT_COLON);
8384 /* Parse the assignment-expression. */
8385 assignment_expr = cp_parser_assignment_expression (parser);
8386 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8388 /* Build the conditional-expression. */
8389 return build_x_conditional_expr (loc, logical_or_expr,
8390 expr,
8391 assignment_expr,
8392 tf_warning_or_error);
8395 /* Parse an assignment-expression.
8397 assignment-expression:
8398 conditional-expression
8399 logical-or-expression assignment-operator assignment_expression
8400 throw-expression
8402 CAST_P is true if this expression is the target of a cast.
8403 DECLTYPE_P is true if this expression is the operand of decltype.
8405 Returns a representation for the expression. */
8407 static tree
8408 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8409 bool cast_p, bool decltype_p)
8411 tree expr;
8413 /* If the next token is the `throw' keyword, then we're looking at
8414 a throw-expression. */
8415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8416 expr = cp_parser_throw_expression (parser);
8417 /* Otherwise, it must be that we are looking at a
8418 logical-or-expression. */
8419 else
8421 /* Parse the binary expressions (logical-or-expression). */
8422 expr = cp_parser_binary_expression (parser, cast_p, false,
8423 decltype_p,
8424 PREC_NOT_OPERATOR, pidk);
8425 /* If the next token is a `?' then we're actually looking at a
8426 conditional-expression. */
8427 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8428 return cp_parser_question_colon_clause (parser, expr);
8429 else
8431 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8433 /* If it's an assignment-operator, we're using the second
8434 production. */
8435 enum tree_code assignment_operator
8436 = cp_parser_assignment_operator_opt (parser);
8437 if (assignment_operator != ERROR_MARK)
8439 bool non_constant_p;
8440 location_t saved_input_location;
8442 /* Parse the right-hand side of the assignment. */
8443 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8445 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8448 /* An assignment may not appear in a
8449 constant-expression. */
8450 if (cp_parser_non_integral_constant_expression (parser,
8451 NIC_ASSIGNMENT))
8452 return error_mark_node;
8453 /* Build the assignment expression. Its default
8454 location is the location of the '=' token. */
8455 saved_input_location = input_location;
8456 input_location = loc;
8457 expr = build_x_modify_expr (loc, expr,
8458 assignment_operator,
8459 rhs,
8460 complain_flags (decltype_p));
8461 input_location = saved_input_location;
8466 return expr;
8469 /* Parse an (optional) assignment-operator.
8471 assignment-operator: one of
8472 = *= /= %= += -= >>= <<= &= ^= |=
8474 GNU Extension:
8476 assignment-operator: one of
8477 <?= >?=
8479 If the next token is an assignment operator, the corresponding tree
8480 code is returned, and the token is consumed. For example, for
8481 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8482 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8483 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8484 operator, ERROR_MARK is returned. */
8486 static enum tree_code
8487 cp_parser_assignment_operator_opt (cp_parser* parser)
8489 enum tree_code op;
8490 cp_token *token;
8492 /* Peek at the next token. */
8493 token = cp_lexer_peek_token (parser->lexer);
8495 switch (token->type)
8497 case CPP_EQ:
8498 op = NOP_EXPR;
8499 break;
8501 case CPP_MULT_EQ:
8502 op = MULT_EXPR;
8503 break;
8505 case CPP_DIV_EQ:
8506 op = TRUNC_DIV_EXPR;
8507 break;
8509 case CPP_MOD_EQ:
8510 op = TRUNC_MOD_EXPR;
8511 break;
8513 case CPP_PLUS_EQ:
8514 op = PLUS_EXPR;
8515 break;
8517 case CPP_MINUS_EQ:
8518 op = MINUS_EXPR;
8519 break;
8521 case CPP_RSHIFT_EQ:
8522 op = RSHIFT_EXPR;
8523 break;
8525 case CPP_LSHIFT_EQ:
8526 op = LSHIFT_EXPR;
8527 break;
8529 case CPP_AND_EQ:
8530 op = BIT_AND_EXPR;
8531 break;
8533 case CPP_XOR_EQ:
8534 op = BIT_XOR_EXPR;
8535 break;
8537 case CPP_OR_EQ:
8538 op = BIT_IOR_EXPR;
8539 break;
8541 default:
8542 /* Nothing else is an assignment operator. */
8543 op = ERROR_MARK;
8546 /* If it was an assignment operator, consume it. */
8547 if (op != ERROR_MARK)
8548 cp_lexer_consume_token (parser->lexer);
8550 return op;
8553 /* Parse an expression.
8555 expression:
8556 assignment-expression
8557 expression , assignment-expression
8559 CAST_P is true if this expression is the target of a cast.
8560 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8561 except possibly parenthesized or on the RHS of a comma (N3276).
8563 Returns a representation of the expression. */
8565 static tree
8566 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8567 bool cast_p, bool decltype_p)
8569 tree expression = NULL_TREE;
8570 location_t loc = UNKNOWN_LOCATION;
8572 while (true)
8574 tree assignment_expression;
8576 /* Parse the next assignment-expression. */
8577 assignment_expression
8578 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8580 /* We don't create a temporary for a call that is the immediate operand
8581 of decltype or on the RHS of a comma. But when we see a comma, we
8582 need to create a temporary for a call on the LHS. */
8583 if (decltype_p && !processing_template_decl
8584 && TREE_CODE (assignment_expression) == CALL_EXPR
8585 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8586 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8587 assignment_expression
8588 = build_cplus_new (TREE_TYPE (assignment_expression),
8589 assignment_expression, tf_warning_or_error);
8591 /* If this is the first assignment-expression, we can just
8592 save it away. */
8593 if (!expression)
8594 expression = assignment_expression;
8595 else
8596 expression = build_x_compound_expr (loc, expression,
8597 assignment_expression,
8598 complain_flags (decltype_p));
8599 /* If the next token is not a comma, then we are done with the
8600 expression. */
8601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8602 break;
8603 /* Consume the `,'. */
8604 loc = cp_lexer_peek_token (parser->lexer)->location;
8605 cp_lexer_consume_token (parser->lexer);
8606 /* A comma operator cannot appear in a constant-expression. */
8607 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8608 expression = error_mark_node;
8611 return expression;
8614 /* Parse a constant-expression.
8616 constant-expression:
8617 conditional-expression
8619 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8620 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8621 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8622 is false, NON_CONSTANT_P should be NULL. */
8624 static tree
8625 cp_parser_constant_expression (cp_parser* parser,
8626 bool allow_non_constant_p,
8627 bool *non_constant_p)
8629 bool saved_integral_constant_expression_p;
8630 bool saved_allow_non_integral_constant_expression_p;
8631 bool saved_non_integral_constant_expression_p;
8632 tree expression;
8634 /* It might seem that we could simply parse the
8635 conditional-expression, and then check to see if it were
8636 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8637 one that the compiler can figure out is constant, possibly after
8638 doing some simplifications or optimizations. The standard has a
8639 precise definition of constant-expression, and we must honor
8640 that, even though it is somewhat more restrictive.
8642 For example:
8644 int i[(2, 3)];
8646 is not a legal declaration, because `(2, 3)' is not a
8647 constant-expression. The `,' operator is forbidden in a
8648 constant-expression. However, GCC's constant-folding machinery
8649 will fold this operation to an INTEGER_CST for `3'. */
8651 /* Save the old settings. */
8652 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8653 saved_allow_non_integral_constant_expression_p
8654 = parser->allow_non_integral_constant_expression_p;
8655 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8656 /* We are now parsing a constant-expression. */
8657 parser->integral_constant_expression_p = true;
8658 parser->allow_non_integral_constant_expression_p
8659 = (allow_non_constant_p || cxx_dialect >= cxx11);
8660 parser->non_integral_constant_expression_p = false;
8661 /* Although the grammar says "conditional-expression", we parse an
8662 "assignment-expression", which also permits "throw-expression"
8663 and the use of assignment operators. In the case that
8664 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8665 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8666 actually essential that we look for an assignment-expression.
8667 For example, cp_parser_initializer_clauses uses this function to
8668 determine whether a particular assignment-expression is in fact
8669 constant. */
8670 expression = cp_parser_assignment_expression (parser);
8671 /* Restore the old settings. */
8672 parser->integral_constant_expression_p
8673 = saved_integral_constant_expression_p;
8674 parser->allow_non_integral_constant_expression_p
8675 = saved_allow_non_integral_constant_expression_p;
8676 if (cxx_dialect >= cxx11)
8678 /* Require an rvalue constant expression here; that's what our
8679 callers expect. Reference constant expressions are handled
8680 separately in e.g. cp_parser_template_argument. */
8681 bool is_const = potential_rvalue_constant_expression (expression);
8682 parser->non_integral_constant_expression_p = !is_const;
8683 if (!is_const && !allow_non_constant_p)
8684 require_potential_rvalue_constant_expression (expression);
8686 if (allow_non_constant_p)
8687 *non_constant_p = parser->non_integral_constant_expression_p;
8688 parser->non_integral_constant_expression_p
8689 = saved_non_integral_constant_expression_p;
8691 return expression;
8694 /* Parse __builtin_offsetof.
8696 offsetof-expression:
8697 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8699 offsetof-member-designator:
8700 id-expression
8701 | offsetof-member-designator "." id-expression
8702 | offsetof-member-designator "[" expression "]"
8703 | offsetof-member-designator "->" id-expression */
8705 static tree
8706 cp_parser_builtin_offsetof (cp_parser *parser)
8708 int save_ice_p, save_non_ice_p;
8709 tree type, expr;
8710 cp_id_kind dummy;
8711 cp_token *token;
8713 /* We're about to accept non-integral-constant things, but will
8714 definitely yield an integral constant expression. Save and
8715 restore these values around our local parsing. */
8716 save_ice_p = parser->integral_constant_expression_p;
8717 save_non_ice_p = parser->non_integral_constant_expression_p;
8719 /* Consume the "__builtin_offsetof" token. */
8720 cp_lexer_consume_token (parser->lexer);
8721 /* Consume the opening `('. */
8722 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8723 /* Parse the type-id. */
8724 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8725 type = cp_parser_type_id (parser);
8726 /* Look for the `,'. */
8727 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8728 token = cp_lexer_peek_token (parser->lexer);
8730 /* Build the (type *)null that begins the traditional offsetof macro. */
8731 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8732 tf_warning_or_error);
8734 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8735 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8736 true, &dummy, token->location);
8737 while (true)
8739 token = cp_lexer_peek_token (parser->lexer);
8740 switch (token->type)
8742 case CPP_OPEN_SQUARE:
8743 /* offsetof-member-designator "[" expression "]" */
8744 expr = cp_parser_postfix_open_square_expression (parser, expr,
8745 true, false);
8746 break;
8748 case CPP_DEREF:
8749 /* offsetof-member-designator "->" identifier */
8750 expr = grok_array_decl (token->location, expr,
8751 integer_zero_node, false);
8752 /* FALLTHRU */
8754 case CPP_DOT:
8755 /* offsetof-member-designator "." identifier */
8756 cp_lexer_consume_token (parser->lexer);
8757 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8758 expr, true, &dummy,
8759 token->location);
8760 break;
8762 case CPP_CLOSE_PAREN:
8763 /* Consume the ")" token. */
8764 cp_lexer_consume_token (parser->lexer);
8765 goto success;
8767 default:
8768 /* Error. We know the following require will fail, but
8769 that gives the proper error message. */
8770 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8771 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8772 expr = error_mark_node;
8773 goto failure;
8777 success:
8778 expr = finish_offsetof (expr, loc);
8780 failure:
8781 parser->integral_constant_expression_p = save_ice_p;
8782 parser->non_integral_constant_expression_p = save_non_ice_p;
8784 return expr;
8787 /* Parse a trait expression.
8789 Returns a representation of the expression, the underlying type
8790 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8792 static tree
8793 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8795 cp_trait_kind kind;
8796 tree type1, type2 = NULL_TREE;
8797 bool binary = false;
8798 bool variadic = false;
8800 switch (keyword)
8802 case RID_HAS_NOTHROW_ASSIGN:
8803 kind = CPTK_HAS_NOTHROW_ASSIGN;
8804 break;
8805 case RID_HAS_NOTHROW_CONSTRUCTOR:
8806 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8807 break;
8808 case RID_HAS_NOTHROW_COPY:
8809 kind = CPTK_HAS_NOTHROW_COPY;
8810 break;
8811 case RID_HAS_TRIVIAL_ASSIGN:
8812 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8813 break;
8814 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8815 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8816 break;
8817 case RID_HAS_TRIVIAL_COPY:
8818 kind = CPTK_HAS_TRIVIAL_COPY;
8819 break;
8820 case RID_HAS_TRIVIAL_DESTRUCTOR:
8821 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8822 break;
8823 case RID_HAS_VIRTUAL_DESTRUCTOR:
8824 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8825 break;
8826 case RID_IS_ABSTRACT:
8827 kind = CPTK_IS_ABSTRACT;
8828 break;
8829 case RID_IS_BASE_OF:
8830 kind = CPTK_IS_BASE_OF;
8831 binary = true;
8832 break;
8833 case RID_IS_CLASS:
8834 kind = CPTK_IS_CLASS;
8835 break;
8836 case RID_IS_EMPTY:
8837 kind = CPTK_IS_EMPTY;
8838 break;
8839 case RID_IS_ENUM:
8840 kind = CPTK_IS_ENUM;
8841 break;
8842 case RID_IS_FINAL:
8843 kind = CPTK_IS_FINAL;
8844 break;
8845 case RID_IS_LITERAL_TYPE:
8846 kind = CPTK_IS_LITERAL_TYPE;
8847 break;
8848 case RID_IS_POD:
8849 kind = CPTK_IS_POD;
8850 break;
8851 case RID_IS_POLYMORPHIC:
8852 kind = CPTK_IS_POLYMORPHIC;
8853 break;
8854 case RID_IS_STD_LAYOUT:
8855 kind = CPTK_IS_STD_LAYOUT;
8856 break;
8857 case RID_IS_TRIVIAL:
8858 kind = CPTK_IS_TRIVIAL;
8859 break;
8860 case RID_IS_TRIVIALLY_ASSIGNABLE:
8861 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8862 binary = true;
8863 break;
8864 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8865 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8866 variadic = true;
8867 break;
8868 case RID_IS_TRIVIALLY_COPYABLE:
8869 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8870 break;
8871 case RID_IS_UNION:
8872 kind = CPTK_IS_UNION;
8873 break;
8874 case RID_UNDERLYING_TYPE:
8875 kind = CPTK_UNDERLYING_TYPE;
8876 break;
8877 case RID_BASES:
8878 kind = CPTK_BASES;
8879 break;
8880 case RID_DIRECT_BASES:
8881 kind = CPTK_DIRECT_BASES;
8882 break;
8883 default:
8884 gcc_unreachable ();
8887 /* Consume the token. */
8888 cp_lexer_consume_token (parser->lexer);
8890 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8892 type1 = cp_parser_type_id (parser);
8894 if (type1 == error_mark_node)
8895 return error_mark_node;
8897 if (binary)
8899 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8901 type2 = cp_parser_type_id (parser);
8903 if (type2 == error_mark_node)
8904 return error_mark_node;
8906 else if (variadic)
8908 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8910 cp_lexer_consume_token (parser->lexer);
8911 tree elt = cp_parser_type_id (parser);
8912 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8914 cp_lexer_consume_token (parser->lexer);
8915 elt = make_pack_expansion (elt);
8917 if (elt == error_mark_node)
8918 return error_mark_node;
8919 type2 = tree_cons (NULL_TREE, elt, type2);
8923 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8925 /* Complete the trait expression, which may mean either processing
8926 the trait expr now or saving it for template instantiation. */
8927 switch(kind)
8929 case CPTK_UNDERLYING_TYPE:
8930 return finish_underlying_type (type1);
8931 case CPTK_BASES:
8932 return finish_bases (type1, false);
8933 case CPTK_DIRECT_BASES:
8934 return finish_bases (type1, true);
8935 default:
8936 return finish_trait_expr (kind, type1, type2);
8940 /* Lambdas that appear in variable initializer or default argument scope
8941 get that in their mangling, so we need to record it. We might as well
8942 use the count for function and namespace scopes as well. */
8943 static GTY(()) tree lambda_scope;
8944 static GTY(()) int lambda_count;
8945 typedef struct GTY(()) tree_int
8947 tree t;
8948 int i;
8949 } tree_int;
8950 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8952 static void
8953 start_lambda_scope (tree decl)
8955 tree_int ti;
8956 gcc_assert (decl);
8957 /* Once we're inside a function, we ignore other scopes and just push
8958 the function again so that popping works properly. */
8959 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8960 decl = current_function_decl;
8961 ti.t = lambda_scope;
8962 ti.i = lambda_count;
8963 vec_safe_push (lambda_scope_stack, ti);
8964 if (lambda_scope != decl)
8966 /* Don't reset the count if we're still in the same function. */
8967 lambda_scope = decl;
8968 lambda_count = 0;
8972 static void
8973 record_lambda_scope (tree lambda)
8975 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8976 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8979 static void
8980 finish_lambda_scope (void)
8982 tree_int *p = &lambda_scope_stack->last ();
8983 if (lambda_scope != p->t)
8985 lambda_scope = p->t;
8986 lambda_count = p->i;
8988 lambda_scope_stack->pop ();
8991 /* Parse a lambda expression.
8993 lambda-expression:
8994 lambda-introducer lambda-declarator [opt] compound-statement
8996 Returns a representation of the expression. */
8998 static tree
8999 cp_parser_lambda_expression (cp_parser* parser)
9001 tree lambda_expr = build_lambda_expr ();
9002 tree type;
9003 bool ok = true;
9004 cp_token *token = cp_lexer_peek_token (parser->lexer);
9005 cp_token_position start = 0;
9007 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9009 if (cp_unevaluated_operand)
9011 if (!token->error_reported)
9013 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9014 "lambda-expression in unevaluated context");
9015 token->error_reported = true;
9017 ok = false;
9019 else if (parser->in_template_argument_list_p)
9021 if (!token->error_reported)
9023 error_at (token->location, "lambda-expression in template-argument");
9024 token->error_reported = true;
9026 ok = false;
9029 /* We may be in the middle of deferred access check. Disable
9030 it now. */
9031 push_deferring_access_checks (dk_no_deferred);
9033 cp_parser_lambda_introducer (parser, lambda_expr);
9035 type = begin_lambda_type (lambda_expr);
9036 if (type == error_mark_node)
9037 return error_mark_node;
9039 record_lambda_scope (lambda_expr);
9041 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9042 determine_visibility (TYPE_NAME (type));
9044 /* Now that we've started the type, add the capture fields for any
9045 explicit captures. */
9046 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9049 /* Inside the class, surrounding template-parameter-lists do not apply. */
9050 unsigned int saved_num_template_parameter_lists
9051 = parser->num_template_parameter_lists;
9052 unsigned char in_statement = parser->in_statement;
9053 bool in_switch_statement_p = parser->in_switch_statement_p;
9054 bool fully_implicit_function_template_p
9055 = parser->fully_implicit_function_template_p;
9056 tree implicit_template_parms = parser->implicit_template_parms;
9057 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9058 bool auto_is_implicit_function_template_parm_p
9059 = parser->auto_is_implicit_function_template_parm_p;
9061 parser->num_template_parameter_lists = 0;
9062 parser->in_statement = 0;
9063 parser->in_switch_statement_p = false;
9064 parser->fully_implicit_function_template_p = false;
9065 parser->implicit_template_parms = 0;
9066 parser->implicit_template_scope = 0;
9067 parser->auto_is_implicit_function_template_parm_p = false;
9069 /* By virtue of defining a local class, a lambda expression has access to
9070 the private variables of enclosing classes. */
9072 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9074 if (ok)
9076 if (!cp_parser_error_occurred (parser)
9077 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9078 && cp_parser_start_tentative_firewall (parser))
9079 start = token;
9080 cp_parser_lambda_body (parser, lambda_expr);
9082 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9084 if (cp_parser_skip_to_closing_brace (parser))
9085 cp_lexer_consume_token (parser->lexer);
9088 /* The capture list was built up in reverse order; fix that now. */
9089 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9090 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9092 if (ok)
9093 maybe_add_lambda_conv_op (type);
9095 type = finish_struct (type, /*attributes=*/NULL_TREE);
9097 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9098 parser->in_statement = in_statement;
9099 parser->in_switch_statement_p = in_switch_statement_p;
9100 parser->fully_implicit_function_template_p
9101 = fully_implicit_function_template_p;
9102 parser->implicit_template_parms = implicit_template_parms;
9103 parser->implicit_template_scope = implicit_template_scope;
9104 parser->auto_is_implicit_function_template_parm_p
9105 = auto_is_implicit_function_template_parm_p;
9108 pop_deferring_access_checks ();
9110 /* This field is only used during parsing of the lambda. */
9111 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9113 /* This lambda shouldn't have any proxies left at this point. */
9114 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9115 /* And now that we're done, push proxies for an enclosing lambda. */
9116 insert_pending_capture_proxies ();
9118 if (ok)
9119 lambda_expr = build_lambda_object (lambda_expr);
9120 else
9121 lambda_expr = error_mark_node;
9123 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9125 return lambda_expr;
9128 /* Parse the beginning of a lambda expression.
9130 lambda-introducer:
9131 [ lambda-capture [opt] ]
9133 LAMBDA_EXPR is the current representation of the lambda expression. */
9135 static void
9136 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9138 /* Need commas after the first capture. */
9139 bool first = true;
9141 /* Eat the leading `['. */
9142 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9144 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9145 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9146 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9147 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9148 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9149 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9151 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9153 cp_lexer_consume_token (parser->lexer);
9154 first = false;
9157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9159 cp_token* capture_token;
9160 tree capture_id;
9161 tree capture_init_expr;
9162 cp_id_kind idk = CP_ID_KIND_NONE;
9163 bool explicit_init_p = false;
9165 enum capture_kind_type
9167 BY_COPY,
9168 BY_REFERENCE
9170 enum capture_kind_type capture_kind = BY_COPY;
9172 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9174 error ("expected end of capture-list");
9175 return;
9178 if (first)
9179 first = false;
9180 else
9181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9183 /* Possibly capture `this'. */
9184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9187 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9188 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9189 "with by-copy capture default");
9190 cp_lexer_consume_token (parser->lexer);
9191 add_capture (lambda_expr,
9192 /*id=*/this_identifier,
9193 /*initializer=*/finish_this_expr(),
9194 /*by_reference_p=*/false,
9195 explicit_init_p);
9196 continue;
9199 /* Remember whether we want to capture as a reference or not. */
9200 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9202 capture_kind = BY_REFERENCE;
9203 cp_lexer_consume_token (parser->lexer);
9206 /* Get the identifier. */
9207 capture_token = cp_lexer_peek_token (parser->lexer);
9208 capture_id = cp_parser_identifier (parser);
9210 if (capture_id == error_mark_node)
9211 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9212 delimiters, but I modified this to stop on unnested ']' as well. It
9213 was already changed to stop on unnested '}', so the
9214 "closing_parenthesis" name is no more misleading with my change. */
9216 cp_parser_skip_to_closing_parenthesis (parser,
9217 /*recovering=*/true,
9218 /*or_comma=*/true,
9219 /*consume_paren=*/true);
9220 break;
9223 /* Find the initializer for this capture. */
9224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9225 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9226 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9228 bool direct, non_constant;
9229 /* An explicit initializer exists. */
9230 if (cxx_dialect < cxx14)
9231 pedwarn (input_location, 0,
9232 "lambda capture initializers "
9233 "only available with -std=c++14 or -std=gnu++14");
9234 capture_init_expr = cp_parser_initializer (parser, &direct,
9235 &non_constant);
9236 explicit_init_p = true;
9237 if (capture_init_expr == NULL_TREE)
9239 error ("empty initializer for lambda init-capture");
9240 capture_init_expr = error_mark_node;
9243 else
9245 const char* error_msg;
9247 /* Turn the identifier into an id-expression. */
9248 capture_init_expr
9249 = cp_parser_lookup_name_simple (parser, capture_id,
9250 capture_token->location);
9252 if (capture_init_expr == error_mark_node)
9254 unqualified_name_lookup_error (capture_id);
9255 continue;
9257 else if (DECL_P (capture_init_expr)
9258 && (!VAR_P (capture_init_expr)
9259 && TREE_CODE (capture_init_expr) != PARM_DECL))
9261 error_at (capture_token->location,
9262 "capture of non-variable %qD ",
9263 capture_init_expr);
9264 inform (0, "%q+#D declared here", capture_init_expr);
9265 continue;
9267 if (VAR_P (capture_init_expr)
9268 && decl_storage_duration (capture_init_expr) != dk_auto)
9270 if (pedwarn (capture_token->location, 0, "capture of variable "
9271 "%qD with non-automatic storage duration",
9272 capture_init_expr))
9273 inform (0, "%q+#D declared here", capture_init_expr);
9274 continue;
9277 capture_init_expr
9278 = finish_id_expression
9279 (capture_id,
9280 capture_init_expr,
9281 parser->scope,
9282 &idk,
9283 /*integral_constant_expression_p=*/false,
9284 /*allow_non_integral_constant_expression_p=*/false,
9285 /*non_integral_constant_expression_p=*/NULL,
9286 /*template_p=*/false,
9287 /*done=*/true,
9288 /*address_p=*/false,
9289 /*template_arg_p=*/false,
9290 &error_msg,
9291 capture_token->location);
9293 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9295 cp_lexer_consume_token (parser->lexer);
9296 capture_init_expr = make_pack_expansion (capture_init_expr);
9298 else
9299 check_for_bare_parameter_packs (capture_init_expr);
9302 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9303 && !explicit_init_p)
9305 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9306 && capture_kind == BY_COPY)
9307 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9308 "of %qD redundant with by-copy capture default",
9309 capture_id);
9310 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9311 && capture_kind == BY_REFERENCE)
9312 pedwarn (capture_token->location, 0, "explicit by-reference "
9313 "capture of %qD redundant with by-reference capture "
9314 "default", capture_id);
9317 add_capture (lambda_expr,
9318 capture_id,
9319 capture_init_expr,
9320 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9321 explicit_init_p);
9324 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9327 /* Parse the (optional) middle of a lambda expression.
9329 lambda-declarator:
9330 < template-parameter-list [opt] >
9331 ( parameter-declaration-clause [opt] )
9332 attribute-specifier [opt]
9333 mutable [opt]
9334 exception-specification [opt]
9335 lambda-return-type-clause [opt]
9337 LAMBDA_EXPR is the current representation of the lambda expression. */
9339 static bool
9340 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9342 /* 5.1.1.4 of the standard says:
9343 If a lambda-expression does not include a lambda-declarator, it is as if
9344 the lambda-declarator were ().
9345 This means an empty parameter list, no attributes, and no exception
9346 specification. */
9347 tree param_list = void_list_node;
9348 tree attributes = NULL_TREE;
9349 tree exception_spec = NULL_TREE;
9350 tree template_param_list = NULL_TREE;
9352 /* The template-parameter-list is optional, but must begin with
9353 an opening angle if present. */
9354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9356 if (cxx_dialect < cxx14)
9357 pedwarn (parser->lexer->next_token->location, 0,
9358 "lambda templates are only available with "
9359 "-std=c++14 or -std=gnu++14");
9361 cp_lexer_consume_token (parser->lexer);
9363 template_param_list = cp_parser_template_parameter_list (parser);
9365 cp_parser_skip_to_end_of_template_parameter_list (parser);
9367 /* We just processed one more parameter list. */
9368 ++parser->num_template_parameter_lists;
9371 /* The parameter-declaration-clause is optional (unless
9372 template-parameter-list was given), but must begin with an
9373 opening parenthesis if present. */
9374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9376 cp_lexer_consume_token (parser->lexer);
9378 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9380 /* Parse parameters. */
9381 param_list = cp_parser_parameter_declaration_clause (parser);
9383 /* Default arguments shall not be specified in the
9384 parameter-declaration-clause of a lambda-declarator. */
9385 for (tree t = param_list; t; t = TREE_CHAIN (t))
9386 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9387 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9388 "default argument specified for lambda parameter");
9390 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9392 attributes = cp_parser_attributes_opt (parser);
9394 /* Parse optional `mutable' keyword. */
9395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9397 cp_lexer_consume_token (parser->lexer);
9398 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9401 /* Parse optional exception specification. */
9402 exception_spec = cp_parser_exception_specification_opt (parser);
9404 /* Parse optional trailing return type. */
9405 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9407 cp_lexer_consume_token (parser->lexer);
9408 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9409 = cp_parser_trailing_type_id (parser);
9412 /* The function parameters must be in scope all the way until after the
9413 trailing-return-type in case of decltype. */
9414 pop_bindings_and_leave_scope ();
9416 else if (template_param_list != NULL_TREE) // generate diagnostic
9417 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9419 /* Create the function call operator.
9421 Messing with declarators like this is no uglier than building up the
9422 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9423 other code. */
9425 cp_decl_specifier_seq return_type_specs;
9426 cp_declarator* declarator;
9427 tree fco;
9428 int quals;
9429 void *p;
9431 clear_decl_specs (&return_type_specs);
9432 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9433 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9434 else
9435 /* Maybe we will deduce the return type later. */
9436 return_type_specs.type = make_auto ();
9438 p = obstack_alloc (&declarator_obstack, 0);
9440 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9441 sfk_none);
9443 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9444 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9445 declarator = make_call_declarator (declarator, param_list, quals,
9446 VIRT_SPEC_UNSPECIFIED,
9447 REF_QUAL_NONE,
9448 exception_spec,
9449 /*late_return_type=*/NULL_TREE);
9450 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9452 fco = grokmethod (&return_type_specs,
9453 declarator,
9454 attributes);
9455 if (fco != error_mark_node)
9457 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9458 DECL_ARTIFICIAL (fco) = 1;
9459 /* Give the object parameter a different name. */
9460 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9461 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9462 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9464 if (template_param_list)
9466 fco = finish_member_template_decl (fco);
9467 finish_template_decl (template_param_list);
9468 --parser->num_template_parameter_lists;
9470 else if (parser->fully_implicit_function_template_p)
9471 fco = finish_fully_implicit_template (parser, fco);
9473 finish_member_declaration (fco);
9475 obstack_free (&declarator_obstack, p);
9477 return (fco != error_mark_node);
9481 /* Parse the body of a lambda expression, which is simply
9483 compound-statement
9485 but which requires special handling.
9486 LAMBDA_EXPR is the current representation of the lambda expression. */
9488 static void
9489 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9491 bool nested = (current_function_decl != NULL_TREE);
9492 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9493 if (nested)
9494 push_function_context ();
9495 else
9496 /* Still increment function_depth so that we don't GC in the
9497 middle of an expression. */
9498 ++function_depth;
9499 /* Clear this in case we're in the middle of a default argument. */
9500 parser->local_variables_forbidden_p = false;
9502 /* Finish the function call operator
9503 - class_specifier
9504 + late_parsing_for_member
9505 + function_definition_after_declarator
9506 + ctor_initializer_opt_and_function_body */
9508 tree fco = lambda_function (lambda_expr);
9509 tree body;
9510 bool done = false;
9511 tree compound_stmt;
9512 tree cap;
9514 /* Let the front end know that we are going to be defining this
9515 function. */
9516 start_preparsed_function (fco,
9517 NULL_TREE,
9518 SF_PRE_PARSED | SF_INCLASS_INLINE);
9520 start_lambda_scope (fco);
9521 body = begin_function_body ();
9523 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9524 goto out;
9526 /* Push the proxies for any explicit captures. */
9527 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9528 cap = TREE_CHAIN (cap))
9529 build_capture_proxy (TREE_PURPOSE (cap));
9531 compound_stmt = begin_compound_stmt (0);
9533 /* 5.1.1.4 of the standard says:
9534 If a lambda-expression does not include a trailing-return-type, it
9535 is as if the trailing-return-type denotes the following type:
9536 * if the compound-statement is of the form
9537 { return attribute-specifier [opt] expression ; }
9538 the type of the returned expression after lvalue-to-rvalue
9539 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9540 (_conv.array_ 4.2), and function-to-pointer conversion
9541 (_conv.func_ 4.3);
9542 * otherwise, void. */
9544 /* In a lambda that has neither a lambda-return-type-clause
9545 nor a deducible form, errors should be reported for return statements
9546 in the body. Since we used void as the placeholder return type, parsing
9547 the body as usual will give such desired behavior. */
9548 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9549 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9550 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9552 tree expr = NULL_TREE;
9553 cp_id_kind idk = CP_ID_KIND_NONE;
9555 /* Parse tentatively in case there's more after the initial return
9556 statement. */
9557 cp_parser_parse_tentatively (parser);
9559 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9561 expr = cp_parser_expression (parser, &idk);
9563 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9564 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9566 if (cp_parser_parse_definitely (parser))
9568 if (!processing_template_decl)
9569 apply_deduced_return_type (fco, lambda_return_type (expr));
9571 /* Will get error here if type not deduced yet. */
9572 finish_return_stmt (expr);
9574 done = true;
9578 if (!done)
9580 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9581 cp_parser_label_declaration (parser);
9582 cp_parser_statement_seq_opt (parser, NULL_TREE);
9583 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9586 finish_compound_stmt (compound_stmt);
9588 out:
9589 finish_function_body (body);
9590 finish_lambda_scope ();
9592 /* Finish the function and generate code for it if necessary. */
9593 tree fn = finish_function (/*inline*/2);
9595 /* Only expand if the call op is not a template. */
9596 if (!DECL_TEMPLATE_INFO (fco))
9597 expand_or_defer_fn (fn);
9600 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9601 if (nested)
9602 pop_function_context();
9603 else
9604 --function_depth;
9607 /* Statements [gram.stmt.stmt] */
9609 /* Parse a statement.
9611 statement:
9612 labeled-statement
9613 expression-statement
9614 compound-statement
9615 selection-statement
9616 iteration-statement
9617 jump-statement
9618 declaration-statement
9619 try-block
9621 C++11:
9623 statement:
9624 labeled-statement
9625 attribute-specifier-seq (opt) expression-statement
9626 attribute-specifier-seq (opt) compound-statement
9627 attribute-specifier-seq (opt) selection-statement
9628 attribute-specifier-seq (opt) iteration-statement
9629 attribute-specifier-seq (opt) jump-statement
9630 declaration-statement
9631 attribute-specifier-seq (opt) try-block
9633 TM Extension:
9635 statement:
9636 atomic-statement
9638 IN_COMPOUND is true when the statement is nested inside a
9639 cp_parser_compound_statement; this matters for certain pragmas.
9641 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9642 is a (possibly labeled) if statement which is not enclosed in braces
9643 and has an else clause. This is used to implement -Wparentheses. */
9645 static void
9646 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9647 bool in_compound, bool *if_p)
9649 tree statement, std_attrs = NULL_TREE;
9650 cp_token *token;
9651 location_t statement_location, attrs_location;
9653 restart:
9654 if (if_p != NULL)
9655 *if_p = false;
9656 /* There is no statement yet. */
9657 statement = NULL_TREE;
9659 saved_token_sentinel saved_tokens (parser->lexer);
9660 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9661 if (c_dialect_objc ())
9662 /* In obj-c++, seeing '[[' might be the either the beginning of
9663 c++11 attributes, or a nested objc-message-expression. So
9664 let's parse the c++11 attributes tentatively. */
9665 cp_parser_parse_tentatively (parser);
9666 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9667 if (c_dialect_objc ())
9669 if (!cp_parser_parse_definitely (parser))
9670 std_attrs = NULL_TREE;
9673 /* Peek at the next token. */
9674 token = cp_lexer_peek_token (parser->lexer);
9675 /* Remember the location of the first token in the statement. */
9676 statement_location = token->location;
9677 /* If this is a keyword, then that will often determine what kind of
9678 statement we have. */
9679 if (token->type == CPP_KEYWORD)
9681 enum rid keyword = token->keyword;
9683 switch (keyword)
9685 case RID_CASE:
9686 case RID_DEFAULT:
9687 /* Looks like a labeled-statement with a case label.
9688 Parse the label, and then use tail recursion to parse
9689 the statement. */
9690 cp_parser_label_for_labeled_statement (parser, std_attrs);
9691 goto restart;
9693 case RID_IF:
9694 case RID_SWITCH:
9695 statement = cp_parser_selection_statement (parser, if_p);
9696 break;
9698 case RID_WHILE:
9699 case RID_DO:
9700 case RID_FOR:
9701 statement = cp_parser_iteration_statement (parser, false);
9702 break;
9704 case RID_CILK_FOR:
9705 if (!flag_cilkplus)
9707 error_at (cp_lexer_peek_token (parser->lexer)->location,
9708 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9709 cp_lexer_consume_token (parser->lexer);
9710 statement = error_mark_node;
9712 else
9713 statement = cp_parser_cilk_for (parser, integer_zero_node);
9714 break;
9716 case RID_BREAK:
9717 case RID_CONTINUE:
9718 case RID_RETURN:
9719 case RID_GOTO:
9720 statement = cp_parser_jump_statement (parser);
9721 break;
9723 case RID_CILK_SYNC:
9724 cp_lexer_consume_token (parser->lexer);
9725 if (flag_cilkplus)
9727 tree sync_expr = build_cilk_sync ();
9728 SET_EXPR_LOCATION (sync_expr,
9729 token->location);
9730 statement = finish_expr_stmt (sync_expr);
9732 else
9734 error_at (token->location, "-fcilkplus must be enabled to use"
9735 " %<_Cilk_sync%>");
9736 statement = error_mark_node;
9738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9739 break;
9741 /* Objective-C++ exception-handling constructs. */
9742 case RID_AT_TRY:
9743 case RID_AT_CATCH:
9744 case RID_AT_FINALLY:
9745 case RID_AT_SYNCHRONIZED:
9746 case RID_AT_THROW:
9747 statement = cp_parser_objc_statement (parser);
9748 break;
9750 case RID_TRY:
9751 statement = cp_parser_try_block (parser);
9752 break;
9754 case RID_NAMESPACE:
9755 /* This must be a namespace alias definition. */
9756 cp_parser_declaration_statement (parser);
9757 return;
9759 case RID_TRANSACTION_ATOMIC:
9760 case RID_TRANSACTION_RELAXED:
9761 statement = cp_parser_transaction (parser, keyword);
9762 break;
9763 case RID_TRANSACTION_CANCEL:
9764 statement = cp_parser_transaction_cancel (parser);
9765 break;
9767 default:
9768 /* It might be a keyword like `int' that can start a
9769 declaration-statement. */
9770 break;
9773 else if (token->type == CPP_NAME)
9775 /* If the next token is a `:', then we are looking at a
9776 labeled-statement. */
9777 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9778 if (token->type == CPP_COLON)
9780 /* Looks like a labeled-statement with an ordinary label.
9781 Parse the label, and then use tail recursion to parse
9782 the statement. */
9784 cp_parser_label_for_labeled_statement (parser, std_attrs);
9785 goto restart;
9788 /* Anything that starts with a `{' must be a compound-statement. */
9789 else if (token->type == CPP_OPEN_BRACE)
9790 statement = cp_parser_compound_statement (parser, NULL, false, false);
9791 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9792 a statement all its own. */
9793 else if (token->type == CPP_PRAGMA)
9795 /* Only certain OpenMP pragmas are attached to statements, and thus
9796 are considered statements themselves. All others are not. In
9797 the context of a compound, accept the pragma as a "statement" and
9798 return so that we can check for a close brace. Otherwise we
9799 require a real statement and must go back and read one. */
9800 if (in_compound)
9801 cp_parser_pragma (parser, pragma_compound);
9802 else if (!cp_parser_pragma (parser, pragma_stmt))
9803 goto restart;
9804 return;
9806 else if (token->type == CPP_EOF)
9808 cp_parser_error (parser, "expected statement");
9809 return;
9812 /* Everything else must be a declaration-statement or an
9813 expression-statement. Try for the declaration-statement
9814 first, unless we are looking at a `;', in which case we know that
9815 we have an expression-statement. */
9816 if (!statement)
9818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9820 if (std_attrs != NULL_TREE)
9822 /* Attributes should be parsed as part of the the
9823 declaration, so let's un-parse them. */
9824 saved_tokens.rollback();
9825 std_attrs = NULL_TREE;
9828 cp_parser_parse_tentatively (parser);
9829 /* Try to parse the declaration-statement. */
9830 cp_parser_declaration_statement (parser);
9831 /* If that worked, we're done. */
9832 if (cp_parser_parse_definitely (parser))
9833 return;
9835 /* Look for an expression-statement instead. */
9836 statement = cp_parser_expression_statement (parser, in_statement_expr);
9839 /* Set the line number for the statement. */
9840 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9841 SET_EXPR_LOCATION (statement, statement_location);
9843 /* Note that for now, we don't do anything with c++11 statements
9844 parsed at this level. */
9845 if (std_attrs != NULL_TREE)
9846 warning_at (attrs_location,
9847 OPT_Wattributes,
9848 "attributes at the beginning of statement are ignored");
9851 /* Parse the label for a labeled-statement, i.e.
9853 identifier :
9854 case constant-expression :
9855 default :
9857 GNU Extension:
9858 case constant-expression ... constant-expression : statement
9860 When a label is parsed without errors, the label is added to the
9861 parse tree by the finish_* functions, so this function doesn't
9862 have to return the label. */
9864 static void
9865 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9867 cp_token *token;
9868 tree label = NULL_TREE;
9869 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9871 /* The next token should be an identifier. */
9872 token = cp_lexer_peek_token (parser->lexer);
9873 if (token->type != CPP_NAME
9874 && token->type != CPP_KEYWORD)
9876 cp_parser_error (parser, "expected labeled-statement");
9877 return;
9880 parser->colon_corrects_to_scope_p = false;
9881 switch (token->keyword)
9883 case RID_CASE:
9885 tree expr, expr_hi;
9886 cp_token *ellipsis;
9888 /* Consume the `case' token. */
9889 cp_lexer_consume_token (parser->lexer);
9890 /* Parse the constant-expression. */
9891 expr = cp_parser_constant_expression (parser);
9892 if (check_for_bare_parameter_packs (expr))
9893 expr = error_mark_node;
9895 ellipsis = cp_lexer_peek_token (parser->lexer);
9896 if (ellipsis->type == CPP_ELLIPSIS)
9898 /* Consume the `...' token. */
9899 cp_lexer_consume_token (parser->lexer);
9900 expr_hi = cp_parser_constant_expression (parser);
9901 if (check_for_bare_parameter_packs (expr_hi))
9902 expr_hi = error_mark_node;
9904 /* We don't need to emit warnings here, as the common code
9905 will do this for us. */
9907 else
9908 expr_hi = NULL_TREE;
9910 if (parser->in_switch_statement_p)
9911 finish_case_label (token->location, expr, expr_hi);
9912 else
9913 error_at (token->location,
9914 "case label %qE not within a switch statement",
9915 expr);
9917 break;
9919 case RID_DEFAULT:
9920 /* Consume the `default' token. */
9921 cp_lexer_consume_token (parser->lexer);
9923 if (parser->in_switch_statement_p)
9924 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9925 else
9926 error_at (token->location, "case label not within a switch statement");
9927 break;
9929 default:
9930 /* Anything else must be an ordinary label. */
9931 label = finish_label_stmt (cp_parser_identifier (parser));
9932 break;
9935 /* Require the `:' token. */
9936 cp_parser_require (parser, CPP_COLON, RT_COLON);
9938 /* An ordinary label may optionally be followed by attributes.
9939 However, this is only permitted if the attributes are then
9940 followed by a semicolon. This is because, for backward
9941 compatibility, when parsing
9942 lab: __attribute__ ((unused)) int i;
9943 we want the attribute to attach to "i", not "lab". */
9944 if (label != NULL_TREE
9945 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9947 tree attrs;
9948 cp_parser_parse_tentatively (parser);
9949 attrs = cp_parser_gnu_attributes_opt (parser);
9950 if (attrs == NULL_TREE
9951 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9952 cp_parser_abort_tentative_parse (parser);
9953 else if (!cp_parser_parse_definitely (parser))
9955 else
9956 attributes = chainon (attributes, attrs);
9959 if (attributes != NULL_TREE)
9960 cplus_decl_attributes (&label, attributes, 0);
9962 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9965 /* Parse an expression-statement.
9967 expression-statement:
9968 expression [opt] ;
9970 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9971 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9972 indicates whether this expression-statement is part of an
9973 expression statement. */
9975 static tree
9976 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9978 tree statement = NULL_TREE;
9979 cp_token *token = cp_lexer_peek_token (parser->lexer);
9981 /* If the next token is a ';', then there is no expression
9982 statement. */
9983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9985 statement = cp_parser_expression (parser);
9986 if (statement == error_mark_node
9987 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9989 cp_parser_skip_to_end_of_block_or_statement (parser);
9990 return error_mark_node;
9994 /* Give a helpful message for "A<T>::type t;" and the like. */
9995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9996 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9998 if (TREE_CODE (statement) == SCOPE_REF)
9999 error_at (token->location, "need %<typename%> before %qE because "
10000 "%qT is a dependent scope",
10001 statement, TREE_OPERAND (statement, 0));
10002 else if (is_overloaded_fn (statement)
10003 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10005 /* A::A a; */
10006 tree fn = get_first_fn (statement);
10007 error_at (token->location,
10008 "%<%T::%D%> names the constructor, not the type",
10009 DECL_CONTEXT (fn), DECL_NAME (fn));
10013 /* Consume the final `;'. */
10014 cp_parser_consume_semicolon_at_end_of_statement (parser);
10016 if (in_statement_expr
10017 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10018 /* This is the final expression statement of a statement
10019 expression. */
10020 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10021 else if (statement)
10022 statement = finish_expr_stmt (statement);
10024 return statement;
10027 /* Parse a compound-statement.
10029 compound-statement:
10030 { statement-seq [opt] }
10032 GNU extension:
10034 compound-statement:
10035 { label-declaration-seq [opt] statement-seq [opt] }
10037 label-declaration-seq:
10038 label-declaration
10039 label-declaration-seq label-declaration
10041 Returns a tree representing the statement. */
10043 static tree
10044 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10045 bool in_try, bool function_body)
10047 tree compound_stmt;
10049 /* Consume the `{'. */
10050 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10051 return error_mark_node;
10052 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10053 && !function_body && cxx_dialect < cxx14)
10054 pedwarn (input_location, OPT_Wpedantic,
10055 "compound-statement in constexpr function");
10056 /* Begin the compound-statement. */
10057 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10058 /* If the next keyword is `__label__' we have a label declaration. */
10059 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10060 cp_parser_label_declaration (parser);
10061 /* Parse an (optional) statement-seq. */
10062 cp_parser_statement_seq_opt (parser, in_statement_expr);
10063 /* Finish the compound-statement. */
10064 finish_compound_stmt (compound_stmt);
10065 /* Consume the `}'. */
10066 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10068 return compound_stmt;
10071 /* Parse an (optional) statement-seq.
10073 statement-seq:
10074 statement
10075 statement-seq [opt] statement */
10077 static void
10078 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10080 /* Scan statements until there aren't any more. */
10081 while (true)
10083 cp_token *token = cp_lexer_peek_token (parser->lexer);
10085 /* If we are looking at a `}', then we have run out of
10086 statements; the same is true if we have reached the end
10087 of file, or have stumbled upon a stray '@end'. */
10088 if (token->type == CPP_CLOSE_BRACE
10089 || token->type == CPP_EOF
10090 || token->type == CPP_PRAGMA_EOL
10091 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10092 break;
10094 /* If we are in a compound statement and find 'else' then
10095 something went wrong. */
10096 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10098 if (parser->in_statement & IN_IF_STMT)
10099 break;
10100 else
10102 token = cp_lexer_consume_token (parser->lexer);
10103 error_at (token->location, "%<else%> without a previous %<if%>");
10107 /* Parse the statement. */
10108 cp_parser_statement (parser, in_statement_expr, true, NULL);
10112 /* Parse a selection-statement.
10114 selection-statement:
10115 if ( condition ) statement
10116 if ( condition ) statement else statement
10117 switch ( condition ) statement
10119 Returns the new IF_STMT or SWITCH_STMT.
10121 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10122 is a (possibly labeled) if statement which is not enclosed in
10123 braces and has an else clause. This is used to implement
10124 -Wparentheses. */
10126 static tree
10127 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10129 cp_token *token;
10130 enum rid keyword;
10132 if (if_p != NULL)
10133 *if_p = false;
10135 /* Peek at the next token. */
10136 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10138 /* See what kind of keyword it is. */
10139 keyword = token->keyword;
10140 switch (keyword)
10142 case RID_IF:
10143 case RID_SWITCH:
10145 tree statement;
10146 tree condition;
10148 /* Look for the `('. */
10149 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10151 cp_parser_skip_to_end_of_statement (parser);
10152 return error_mark_node;
10155 /* Begin the selection-statement. */
10156 if (keyword == RID_IF)
10157 statement = begin_if_stmt ();
10158 else
10159 statement = begin_switch_stmt ();
10161 /* Parse the condition. */
10162 condition = cp_parser_condition (parser);
10163 /* Look for the `)'. */
10164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10165 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10166 /*consume_paren=*/true);
10168 if (keyword == RID_IF)
10170 bool nested_if;
10171 unsigned char in_statement;
10173 /* Add the condition. */
10174 finish_if_stmt_cond (condition, statement);
10176 /* Parse the then-clause. */
10177 in_statement = parser->in_statement;
10178 parser->in_statement |= IN_IF_STMT;
10179 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10181 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10182 add_stmt (build_empty_stmt (loc));
10183 cp_lexer_consume_token (parser->lexer);
10184 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10185 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10186 "empty body in an %<if%> statement");
10187 nested_if = false;
10189 else
10190 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10191 parser->in_statement = in_statement;
10193 finish_then_clause (statement);
10195 /* If the next token is `else', parse the else-clause. */
10196 if (cp_lexer_next_token_is_keyword (parser->lexer,
10197 RID_ELSE))
10199 /* Consume the `else' keyword. */
10200 cp_lexer_consume_token (parser->lexer);
10201 begin_else_clause (statement);
10202 /* Parse the else-clause. */
10203 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10205 location_t loc;
10206 loc = cp_lexer_peek_token (parser->lexer)->location;
10207 warning_at (loc,
10208 OPT_Wempty_body, "suggest braces around "
10209 "empty body in an %<else%> statement");
10210 add_stmt (build_empty_stmt (loc));
10211 cp_lexer_consume_token (parser->lexer);
10213 else
10214 cp_parser_implicitly_scoped_statement (parser, NULL);
10216 finish_else_clause (statement);
10218 /* If we are currently parsing a then-clause, then
10219 IF_P will not be NULL. We set it to true to
10220 indicate that this if statement has an else clause.
10221 This may trigger the Wparentheses warning below
10222 when we get back up to the parent if statement. */
10223 if (if_p != NULL)
10224 *if_p = true;
10226 else
10228 /* This if statement does not have an else clause. If
10229 NESTED_IF is true, then the then-clause is an if
10230 statement which does have an else clause. We warn
10231 about the potential ambiguity. */
10232 if (nested_if)
10233 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10234 "suggest explicit braces to avoid ambiguous"
10235 " %<else%>");
10238 /* Now we're all done with the if-statement. */
10239 finish_if_stmt (statement);
10241 else
10243 bool in_switch_statement_p;
10244 unsigned char in_statement;
10246 /* Add the condition. */
10247 finish_switch_cond (condition, statement);
10249 /* Parse the body of the switch-statement. */
10250 in_switch_statement_p = parser->in_switch_statement_p;
10251 in_statement = parser->in_statement;
10252 parser->in_switch_statement_p = true;
10253 parser->in_statement |= IN_SWITCH_STMT;
10254 cp_parser_implicitly_scoped_statement (parser, NULL);
10255 parser->in_switch_statement_p = in_switch_statement_p;
10256 parser->in_statement = in_statement;
10258 /* Now we're all done with the switch-statement. */
10259 finish_switch_stmt (statement);
10262 return statement;
10264 break;
10266 default:
10267 cp_parser_error (parser, "expected selection-statement");
10268 return error_mark_node;
10272 /* Parse a condition.
10274 condition:
10275 expression
10276 type-specifier-seq declarator = initializer-clause
10277 type-specifier-seq declarator braced-init-list
10279 GNU Extension:
10281 condition:
10282 type-specifier-seq declarator asm-specification [opt]
10283 attributes [opt] = assignment-expression
10285 Returns the expression that should be tested. */
10287 static tree
10288 cp_parser_condition (cp_parser* parser)
10290 cp_decl_specifier_seq type_specifiers;
10291 const char *saved_message;
10292 int declares_class_or_enum;
10294 /* Try the declaration first. */
10295 cp_parser_parse_tentatively (parser);
10296 /* New types are not allowed in the type-specifier-seq for a
10297 condition. */
10298 saved_message = parser->type_definition_forbidden_message;
10299 parser->type_definition_forbidden_message
10300 = G_("types may not be defined in conditions");
10301 /* Parse the type-specifier-seq. */
10302 cp_parser_decl_specifier_seq (parser,
10303 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10304 &type_specifiers,
10305 &declares_class_or_enum);
10306 /* Restore the saved message. */
10307 parser->type_definition_forbidden_message = saved_message;
10308 /* If all is well, we might be looking at a declaration. */
10309 if (!cp_parser_error_occurred (parser))
10311 tree decl;
10312 tree asm_specification;
10313 tree attributes;
10314 cp_declarator *declarator;
10315 tree initializer = NULL_TREE;
10317 /* Parse the declarator. */
10318 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10319 /*ctor_dtor_or_conv_p=*/NULL,
10320 /*parenthesized_p=*/NULL,
10321 /*member_p=*/false,
10322 /*friend_p=*/false);
10323 /* Parse the attributes. */
10324 attributes = cp_parser_attributes_opt (parser);
10325 /* Parse the asm-specification. */
10326 asm_specification = cp_parser_asm_specification_opt (parser);
10327 /* If the next token is not an `=' or '{', then we might still be
10328 looking at an expression. For example:
10330 if (A(a).x)
10332 looks like a decl-specifier-seq and a declarator -- but then
10333 there is no `=', so this is an expression. */
10334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10335 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10336 cp_parser_simulate_error (parser);
10338 /* If we did see an `=' or '{', then we are looking at a declaration
10339 for sure. */
10340 if (cp_parser_parse_definitely (parser))
10342 tree pushed_scope;
10343 bool non_constant_p;
10344 bool flags = LOOKUP_ONLYCONVERTING;
10346 /* Create the declaration. */
10347 decl = start_decl (declarator, &type_specifiers,
10348 /*initialized_p=*/true,
10349 attributes, /*prefix_attributes=*/NULL_TREE,
10350 &pushed_scope);
10352 /* Parse the initializer. */
10353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10355 initializer = cp_parser_braced_list (parser, &non_constant_p);
10356 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10357 flags = 0;
10359 else
10361 /* Consume the `='. */
10362 cp_parser_require (parser, CPP_EQ, RT_EQ);
10363 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10365 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10366 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10368 /* Process the initializer. */
10369 cp_finish_decl (decl,
10370 initializer, !non_constant_p,
10371 asm_specification,
10372 flags);
10374 if (pushed_scope)
10375 pop_scope (pushed_scope);
10377 return convert_from_reference (decl);
10380 /* If we didn't even get past the declarator successfully, we are
10381 definitely not looking at a declaration. */
10382 else
10383 cp_parser_abort_tentative_parse (parser);
10385 /* Otherwise, we are looking at an expression. */
10386 return cp_parser_expression (parser);
10389 /* Parses a for-statement or range-for-statement until the closing ')',
10390 not included. */
10392 static tree
10393 cp_parser_for (cp_parser *parser, bool ivdep)
10395 tree init, scope, decl;
10396 bool is_range_for;
10398 /* Begin the for-statement. */
10399 scope = begin_for_scope (&init);
10401 /* Parse the initialization. */
10402 is_range_for = cp_parser_for_init_statement (parser, &decl);
10404 if (is_range_for)
10405 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10406 else
10407 return cp_parser_c_for (parser, scope, init, ivdep);
10410 static tree
10411 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10413 /* Normal for loop */
10414 tree condition = NULL_TREE;
10415 tree expression = NULL_TREE;
10416 tree stmt;
10418 stmt = begin_for_stmt (scope, init);
10419 /* The for-init-statement has already been parsed in
10420 cp_parser_for_init_statement, so no work is needed here. */
10421 finish_for_init_stmt (stmt);
10423 /* If there's a condition, process it. */
10424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10425 condition = cp_parser_condition (parser);
10426 else if (ivdep)
10428 cp_parser_error (parser, "missing loop condition in loop with "
10429 "%<GCC ivdep%> pragma");
10430 condition = error_mark_node;
10432 finish_for_cond (condition, stmt, ivdep);
10433 /* Look for the `;'. */
10434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10436 /* If there's an expression, process it. */
10437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10438 expression = cp_parser_expression (parser);
10439 finish_for_expr (expression, stmt);
10441 return stmt;
10444 /* Tries to parse a range-based for-statement:
10446 range-based-for:
10447 decl-specifier-seq declarator : expression
10449 The decl-specifier-seq declarator and the `:' are already parsed by
10450 cp_parser_for_init_statement. If processing_template_decl it returns a
10451 newly created RANGE_FOR_STMT; if not, it is converted to a
10452 regular FOR_STMT. */
10454 static tree
10455 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10456 bool ivdep)
10458 tree stmt, range_expr;
10460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10462 bool expr_non_constant_p;
10463 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10465 else
10466 range_expr = cp_parser_expression (parser);
10468 /* If in template, STMT is converted to a normal for-statement
10469 at instantiation. If not, it is done just ahead. */
10470 if (processing_template_decl)
10472 if (check_for_bare_parameter_packs (range_expr))
10473 range_expr = error_mark_node;
10474 stmt = begin_range_for_stmt (scope, init);
10475 if (ivdep)
10476 RANGE_FOR_IVDEP (stmt) = 1;
10477 finish_range_for_decl (stmt, range_decl, range_expr);
10478 if (!type_dependent_expression_p (range_expr)
10479 /* do_auto_deduction doesn't mess with template init-lists. */
10480 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10481 do_range_for_auto_deduction (range_decl, range_expr);
10483 else
10485 stmt = begin_for_stmt (scope, init);
10486 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10488 return stmt;
10491 /* Subroutine of cp_convert_range_for: given the initializer expression,
10492 builds up the range temporary. */
10494 static tree
10495 build_range_temp (tree range_expr)
10497 tree range_type, range_temp;
10499 /* Find out the type deduced by the declaration
10500 `auto &&__range = range_expr'. */
10501 range_type = cp_build_reference_type (make_auto (), true);
10502 range_type = do_auto_deduction (range_type, range_expr,
10503 type_uses_auto (range_type));
10505 /* Create the __range variable. */
10506 range_temp = build_decl (input_location, VAR_DECL,
10507 get_identifier ("__for_range"), range_type);
10508 TREE_USED (range_temp) = 1;
10509 DECL_ARTIFICIAL (range_temp) = 1;
10511 return range_temp;
10514 /* Used by cp_parser_range_for in template context: we aren't going to
10515 do a full conversion yet, but we still need to resolve auto in the
10516 type of the for-range-declaration if present. This is basically
10517 a shortcut version of cp_convert_range_for. */
10519 static void
10520 do_range_for_auto_deduction (tree decl, tree range_expr)
10522 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10523 if (auto_node)
10525 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10526 range_temp = convert_from_reference (build_range_temp (range_expr));
10527 iter_type = (cp_parser_perform_range_for_lookup
10528 (range_temp, &begin_dummy, &end_dummy));
10529 if (iter_type)
10531 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10532 iter_type);
10533 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10534 tf_warning_or_error);
10535 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10536 iter_decl, auto_node);
10541 /* Converts a range-based for-statement into a normal
10542 for-statement, as per the definition.
10544 for (RANGE_DECL : RANGE_EXPR)
10545 BLOCK
10547 should be equivalent to:
10550 auto &&__range = RANGE_EXPR;
10551 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10552 __begin != __end;
10553 ++__begin)
10555 RANGE_DECL = *__begin;
10556 BLOCK
10560 If RANGE_EXPR is an array:
10561 BEGIN_EXPR = __range
10562 END_EXPR = __range + ARRAY_SIZE(__range)
10563 Else if RANGE_EXPR has a member 'begin' or 'end':
10564 BEGIN_EXPR = __range.begin()
10565 END_EXPR = __range.end()
10566 Else:
10567 BEGIN_EXPR = begin(__range)
10568 END_EXPR = end(__range);
10570 If __range has a member 'begin' but not 'end', or vice versa, we must
10571 still use the second alternative (it will surely fail, however).
10572 When calling begin()/end() in the third alternative we must use
10573 argument dependent lookup, but always considering 'std' as an associated
10574 namespace. */
10576 tree
10577 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10578 bool ivdep)
10580 tree begin, end;
10581 tree iter_type, begin_expr, end_expr;
10582 tree condition, expression;
10584 if (range_decl == error_mark_node || range_expr == error_mark_node)
10585 /* If an error happened previously do nothing or else a lot of
10586 unhelpful errors would be issued. */
10587 begin_expr = end_expr = iter_type = error_mark_node;
10588 else
10590 tree range_temp;
10592 if (TREE_CODE (range_expr) == VAR_DECL
10593 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10594 /* Can't bind a reference to an array of runtime bound. */
10595 range_temp = range_expr;
10596 else
10598 range_temp = build_range_temp (range_expr);
10599 pushdecl (range_temp);
10600 cp_finish_decl (range_temp, range_expr,
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10603 range_temp = convert_from_reference (range_temp);
10605 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10606 &begin_expr, &end_expr);
10609 /* The new for initialization statement. */
10610 begin = build_decl (input_location, VAR_DECL,
10611 get_identifier ("__for_begin"), iter_type);
10612 TREE_USED (begin) = 1;
10613 DECL_ARTIFICIAL (begin) = 1;
10614 pushdecl (begin);
10615 cp_finish_decl (begin, begin_expr,
10616 /*is_constant_init*/false, NULL_TREE,
10617 LOOKUP_ONLYCONVERTING);
10619 end = build_decl (input_location, VAR_DECL,
10620 get_identifier ("__for_end"), iter_type);
10621 TREE_USED (end) = 1;
10622 DECL_ARTIFICIAL (end) = 1;
10623 pushdecl (end);
10624 cp_finish_decl (end, end_expr,
10625 /*is_constant_init*/false, NULL_TREE,
10626 LOOKUP_ONLYCONVERTING);
10628 finish_for_init_stmt (statement);
10630 /* The new for condition. */
10631 condition = build_x_binary_op (input_location, NE_EXPR,
10632 begin, ERROR_MARK,
10633 end, ERROR_MARK,
10634 NULL, tf_warning_or_error);
10635 finish_for_cond (condition, statement, ivdep);
10637 /* The new increment expression. */
10638 expression = finish_unary_op_expr (input_location,
10639 PREINCREMENT_EXPR, begin,
10640 tf_warning_or_error);
10641 finish_for_expr (expression, statement);
10643 /* The declaration is initialized with *__begin inside the loop body. */
10644 cp_finish_decl (range_decl,
10645 build_x_indirect_ref (input_location, begin, RO_NULL,
10646 tf_warning_or_error),
10647 /*is_constant_init*/false, NULL_TREE,
10648 LOOKUP_ONLYCONVERTING);
10650 return statement;
10653 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10654 We need to solve both at the same time because the method used
10655 depends on the existence of members begin or end.
10656 Returns the type deduced for the iterator expression. */
10658 static tree
10659 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10661 if (error_operand_p (range))
10663 *begin = *end = error_mark_node;
10664 return error_mark_node;
10667 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10669 error ("range-based %<for%> expression of type %qT "
10670 "has incomplete type", TREE_TYPE (range));
10671 *begin = *end = error_mark_node;
10672 return error_mark_node;
10674 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10676 /* If RANGE is an array, we will use pointer arithmetic. */
10677 *begin = range;
10678 *end = build_binary_op (input_location, PLUS_EXPR,
10679 range,
10680 array_type_nelts_top (TREE_TYPE (range)),
10682 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10684 else
10686 /* If it is not an array, we must do a bit of magic. */
10687 tree id_begin, id_end;
10688 tree member_begin, member_end;
10690 *begin = *end = error_mark_node;
10692 id_begin = get_identifier ("begin");
10693 id_end = get_identifier ("end");
10694 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10695 /*protect=*/2, /*want_type=*/false,
10696 tf_warning_or_error);
10697 member_end = lookup_member (TREE_TYPE (range), id_end,
10698 /*protect=*/2, /*want_type=*/false,
10699 tf_warning_or_error);
10701 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10703 /* Use the member functions. */
10704 if (member_begin != NULL_TREE)
10705 *begin = cp_parser_range_for_member_function (range, id_begin);
10706 else
10707 error ("range-based %<for%> expression of type %qT has an "
10708 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10710 if (member_end != NULL_TREE)
10711 *end = cp_parser_range_for_member_function (range, id_end);
10712 else
10713 error ("range-based %<for%> expression of type %qT has a "
10714 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10716 else
10718 /* Use global functions with ADL. */
10719 vec<tree, va_gc> *vec;
10720 vec = make_tree_vector ();
10722 vec_safe_push (vec, range);
10724 member_begin = perform_koenig_lookup (id_begin, vec,
10725 tf_warning_or_error);
10726 *begin = finish_call_expr (member_begin, &vec, false, true,
10727 tf_warning_or_error);
10728 member_end = perform_koenig_lookup (id_end, vec,
10729 tf_warning_or_error);
10730 *end = finish_call_expr (member_end, &vec, false, true,
10731 tf_warning_or_error);
10733 release_tree_vector (vec);
10736 /* Last common checks. */
10737 if (*begin == error_mark_node || *end == error_mark_node)
10739 /* If one of the expressions is an error do no more checks. */
10740 *begin = *end = error_mark_node;
10741 return error_mark_node;
10743 else if (type_dependent_expression_p (*begin)
10744 || type_dependent_expression_p (*end))
10745 /* Can happen, when, eg, in a template context, Koenig lookup
10746 can't resolve begin/end (c++/58503). */
10747 return NULL_TREE;
10748 else
10750 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10751 /* The unqualified type of the __begin and __end temporaries should
10752 be the same, as required by the multiple auto declaration. */
10753 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10754 error ("inconsistent begin/end types in range-based %<for%> "
10755 "statement: %qT and %qT",
10756 TREE_TYPE (*begin), TREE_TYPE (*end));
10757 return iter_type;
10762 /* Helper function for cp_parser_perform_range_for_lookup.
10763 Builds a tree for RANGE.IDENTIFIER(). */
10765 static tree
10766 cp_parser_range_for_member_function (tree range, tree identifier)
10768 tree member, res;
10769 vec<tree, va_gc> *vec;
10771 member = finish_class_member_access_expr (range, identifier,
10772 false, tf_warning_or_error);
10773 if (member == error_mark_node)
10774 return error_mark_node;
10776 vec = make_tree_vector ();
10777 res = finish_call_expr (member, &vec,
10778 /*disallow_virtual=*/false,
10779 /*koenig_p=*/false,
10780 tf_warning_or_error);
10781 release_tree_vector (vec);
10782 return res;
10785 /* Parse an iteration-statement.
10787 iteration-statement:
10788 while ( condition ) statement
10789 do statement while ( expression ) ;
10790 for ( for-init-statement condition [opt] ; expression [opt] )
10791 statement
10793 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10795 static tree
10796 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10798 cp_token *token;
10799 enum rid keyword;
10800 tree statement;
10801 unsigned char in_statement;
10803 /* Peek at the next token. */
10804 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10805 if (!token)
10806 return error_mark_node;
10808 /* Remember whether or not we are already within an iteration
10809 statement. */
10810 in_statement = parser->in_statement;
10812 /* See what kind of keyword it is. */
10813 keyword = token->keyword;
10814 switch (keyword)
10816 case RID_WHILE:
10818 tree condition;
10820 /* Begin the while-statement. */
10821 statement = begin_while_stmt ();
10822 /* Look for the `('. */
10823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10824 /* Parse the condition. */
10825 condition = cp_parser_condition (parser);
10826 finish_while_stmt_cond (condition, statement, ivdep);
10827 /* Look for the `)'. */
10828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10829 /* Parse the dependent statement. */
10830 parser->in_statement = IN_ITERATION_STMT;
10831 cp_parser_already_scoped_statement (parser);
10832 parser->in_statement = in_statement;
10833 /* We're done with the while-statement. */
10834 finish_while_stmt (statement);
10836 break;
10838 case RID_DO:
10840 tree expression;
10842 /* Begin the do-statement. */
10843 statement = begin_do_stmt ();
10844 /* Parse the body of the do-statement. */
10845 parser->in_statement = IN_ITERATION_STMT;
10846 cp_parser_implicitly_scoped_statement (parser, NULL);
10847 parser->in_statement = in_statement;
10848 finish_do_body (statement);
10849 /* Look for the `while' keyword. */
10850 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10851 /* Look for the `('. */
10852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10853 /* Parse the expression. */
10854 expression = cp_parser_expression (parser);
10855 /* We're done with the do-statement. */
10856 finish_do_stmt (expression, statement, ivdep);
10857 /* Look for the `)'. */
10858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10859 /* Look for the `;'. */
10860 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10862 break;
10864 case RID_FOR:
10866 /* Look for the `('. */
10867 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10869 statement = cp_parser_for (parser, ivdep);
10871 /* Look for the `)'. */
10872 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10874 /* Parse the body of the for-statement. */
10875 parser->in_statement = IN_ITERATION_STMT;
10876 cp_parser_already_scoped_statement (parser);
10877 parser->in_statement = in_statement;
10879 /* We're done with the for-statement. */
10880 finish_for_stmt (statement);
10882 break;
10884 default:
10885 cp_parser_error (parser, "expected iteration-statement");
10886 statement = error_mark_node;
10887 break;
10890 return statement;
10893 /* Parse a for-init-statement or the declarator of a range-based-for.
10894 Returns true if a range-based-for declaration is seen.
10896 for-init-statement:
10897 expression-statement
10898 simple-declaration */
10900 static bool
10901 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10903 /* If the next token is a `;', then we have an empty
10904 expression-statement. Grammatically, this is also a
10905 simple-declaration, but an invalid one, because it does not
10906 declare anything. Therefore, if we did not handle this case
10907 specially, we would issue an error message about an invalid
10908 declaration. */
10909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10911 bool is_range_for = false;
10912 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10915 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10917 /* N3994 -- for (id : init) ... */
10918 if (cxx_dialect < cxx1z)
10919 pedwarn (input_location, 0, "range-based for loop without a "
10920 "type-specifier only available with "
10921 "-std=c++1z or -std=gnu++1z");
10922 tree name = cp_parser_identifier (parser);
10923 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10924 *decl = build_decl (input_location, VAR_DECL, name, type);
10925 pushdecl (*decl);
10926 cp_lexer_consume_token (parser->lexer);
10927 return true;
10930 /* A colon is used in range-based for. */
10931 parser->colon_corrects_to_scope_p = false;
10933 /* We're going to speculatively look for a declaration, falling back
10934 to an expression, if necessary. */
10935 cp_parser_parse_tentatively (parser);
10936 /* Parse the declaration. */
10937 cp_parser_simple_declaration (parser,
10938 /*function_definition_allowed_p=*/false,
10939 decl);
10940 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10941 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10943 /* It is a range-for, consume the ':' */
10944 cp_lexer_consume_token (parser->lexer);
10945 is_range_for = true;
10946 if (cxx_dialect < cxx11)
10948 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10949 "range-based %<for%> loops only available with "
10950 "-std=c++11 or -std=gnu++11");
10951 *decl = error_mark_node;
10954 else
10955 /* The ';' is not consumed yet because we told
10956 cp_parser_simple_declaration not to. */
10957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10959 if (cp_parser_parse_definitely (parser))
10960 return is_range_for;
10961 /* If the tentative parse failed, then we shall need to look for an
10962 expression-statement. */
10964 /* If we are here, it is an expression-statement. */
10965 cp_parser_expression_statement (parser, NULL_TREE);
10966 return false;
10969 /* Parse a jump-statement.
10971 jump-statement:
10972 break ;
10973 continue ;
10974 return expression [opt] ;
10975 return braced-init-list ;
10976 goto identifier ;
10978 GNU extension:
10980 jump-statement:
10981 goto * expression ;
10983 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10985 static tree
10986 cp_parser_jump_statement (cp_parser* parser)
10988 tree statement = error_mark_node;
10989 cp_token *token;
10990 enum rid keyword;
10991 unsigned char in_statement;
10993 /* Peek at the next token. */
10994 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10995 if (!token)
10996 return error_mark_node;
10998 /* See what kind of keyword it is. */
10999 keyword = token->keyword;
11000 switch (keyword)
11002 case RID_BREAK:
11003 in_statement = parser->in_statement & ~IN_IF_STMT;
11004 switch (in_statement)
11006 case 0:
11007 error_at (token->location, "break statement not within loop or switch");
11008 break;
11009 default:
11010 gcc_assert ((in_statement & IN_SWITCH_STMT)
11011 || in_statement == IN_ITERATION_STMT);
11012 statement = finish_break_stmt ();
11013 if (in_statement == IN_ITERATION_STMT)
11014 break_maybe_infinite_loop ();
11015 break;
11016 case IN_OMP_BLOCK:
11017 error_at (token->location, "invalid exit from OpenMP structured block");
11018 break;
11019 case IN_OMP_FOR:
11020 error_at (token->location, "break statement used with OpenMP for loop");
11021 break;
11022 case IN_CILK_SIMD_FOR:
11023 error_at (token->location, "break statement used with Cilk Plus for loop");
11024 break;
11026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11027 break;
11029 case RID_CONTINUE:
11030 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11032 case 0:
11033 error_at (token->location, "continue statement not within a loop");
11034 break;
11035 case IN_CILK_SIMD_FOR:
11036 error_at (token->location,
11037 "continue statement within %<#pragma simd%> loop body");
11038 /* Fall through. */
11039 case IN_ITERATION_STMT:
11040 case IN_OMP_FOR:
11041 statement = finish_continue_stmt ();
11042 break;
11043 case IN_OMP_BLOCK:
11044 error_at (token->location, "invalid exit from OpenMP structured block");
11045 break;
11046 default:
11047 gcc_unreachable ();
11049 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11050 break;
11052 case RID_RETURN:
11054 tree expr;
11055 bool expr_non_constant_p;
11057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11059 cp_lexer_set_source_position (parser->lexer);
11060 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11061 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11063 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11064 expr = cp_parser_expression (parser);
11065 else
11066 /* If the next token is a `;', then there is no
11067 expression. */
11068 expr = NULL_TREE;
11069 /* Build the return-statement. */
11070 statement = finish_return_stmt (expr);
11071 /* Look for the final `;'. */
11072 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11074 break;
11076 case RID_GOTO:
11077 if (parser->in_function_body
11078 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11080 error ("%<goto%> in %<constexpr%> function");
11081 cp_function_chain->invalid_constexpr = true;
11084 /* Create the goto-statement. */
11085 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11087 /* Issue a warning about this use of a GNU extension. */
11088 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11089 /* Consume the '*' token. */
11090 cp_lexer_consume_token (parser->lexer);
11091 /* Parse the dependent expression. */
11092 finish_goto_stmt (cp_parser_expression (parser));
11094 else
11095 finish_goto_stmt (cp_parser_identifier (parser));
11096 /* Look for the final `;'. */
11097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11098 break;
11100 default:
11101 cp_parser_error (parser, "expected jump-statement");
11102 break;
11105 return statement;
11108 /* Parse a declaration-statement.
11110 declaration-statement:
11111 block-declaration */
11113 static void
11114 cp_parser_declaration_statement (cp_parser* parser)
11116 void *p;
11118 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11119 p = obstack_alloc (&declarator_obstack, 0);
11121 /* Parse the block-declaration. */
11122 cp_parser_block_declaration (parser, /*statement_p=*/true);
11124 /* Free any declarators allocated. */
11125 obstack_free (&declarator_obstack, p);
11128 /* Some dependent statements (like `if (cond) statement'), are
11129 implicitly in their own scope. In other words, if the statement is
11130 a single statement (as opposed to a compound-statement), it is
11131 none-the-less treated as if it were enclosed in braces. Any
11132 declarations appearing in the dependent statement are out of scope
11133 after control passes that point. This function parses a statement,
11134 but ensures that is in its own scope, even if it is not a
11135 compound-statement.
11137 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11138 is a (possibly labeled) if statement which is not enclosed in
11139 braces and has an else clause. This is used to implement
11140 -Wparentheses.
11142 Returns the new statement. */
11144 static tree
11145 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11147 tree statement;
11149 if (if_p != NULL)
11150 *if_p = false;
11152 /* Mark if () ; with a special NOP_EXPR. */
11153 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11155 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11156 cp_lexer_consume_token (parser->lexer);
11157 statement = add_stmt (build_empty_stmt (loc));
11159 /* if a compound is opened, we simply parse the statement directly. */
11160 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11161 statement = cp_parser_compound_statement (parser, NULL, false, false);
11162 /* If the token is not a `{', then we must take special action. */
11163 else
11165 /* Create a compound-statement. */
11166 statement = begin_compound_stmt (0);
11167 /* Parse the dependent-statement. */
11168 cp_parser_statement (parser, NULL_TREE, false, if_p);
11169 /* Finish the dummy compound-statement. */
11170 finish_compound_stmt (statement);
11173 /* Return the statement. */
11174 return statement;
11177 /* For some dependent statements (like `while (cond) statement'), we
11178 have already created a scope. Therefore, even if the dependent
11179 statement is a compound-statement, we do not want to create another
11180 scope. */
11182 static void
11183 cp_parser_already_scoped_statement (cp_parser* parser)
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))
11187 cp_parser_statement (parser, NULL_TREE, false, NULL);
11188 else
11190 /* Avoid calling cp_parser_compound_statement, so that we
11191 don't create a new scope. Do everything else by hand. */
11192 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11193 /* If the next keyword is `__label__' we have a label declaration. */
11194 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11195 cp_parser_label_declaration (parser);
11196 /* Parse an (optional) statement-seq. */
11197 cp_parser_statement_seq_opt (parser, NULL_TREE);
11198 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11202 /* Declarations [gram.dcl.dcl] */
11204 /* Parse an optional declaration-sequence.
11206 declaration-seq:
11207 declaration
11208 declaration-seq declaration */
11210 static void
11211 cp_parser_declaration_seq_opt (cp_parser* parser)
11213 while (true)
11215 cp_token *token;
11217 token = cp_lexer_peek_token (parser->lexer);
11219 if (token->type == CPP_CLOSE_BRACE
11220 || token->type == CPP_EOF
11221 || token->type == CPP_PRAGMA_EOL)
11222 break;
11224 if (token->type == CPP_SEMICOLON)
11226 /* A declaration consisting of a single semicolon is
11227 invalid. Allow it unless we're being pedantic. */
11228 cp_lexer_consume_token (parser->lexer);
11229 if (!in_system_header_at (input_location))
11230 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11231 continue;
11234 /* If we're entering or exiting a region that's implicitly
11235 extern "C", modify the lang context appropriately. */
11236 if (!parser->implicit_extern_c && token->implicit_extern_c)
11238 push_lang_context (lang_name_c);
11239 parser->implicit_extern_c = true;
11241 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11243 pop_lang_context ();
11244 parser->implicit_extern_c = false;
11247 if (token->type == CPP_PRAGMA)
11249 /* A top-level declaration can consist solely of a #pragma.
11250 A nested declaration cannot, so this is done here and not
11251 in cp_parser_declaration. (A #pragma at block scope is
11252 handled in cp_parser_statement.) */
11253 cp_parser_pragma (parser, pragma_external);
11254 continue;
11257 /* Parse the declaration itself. */
11258 cp_parser_declaration (parser);
11262 /* Parse a declaration.
11264 declaration:
11265 block-declaration
11266 function-definition
11267 template-declaration
11268 explicit-instantiation
11269 explicit-specialization
11270 linkage-specification
11271 namespace-definition
11273 GNU extension:
11275 declaration:
11276 __extension__ declaration */
11278 static void
11279 cp_parser_declaration (cp_parser* parser)
11281 cp_token token1;
11282 cp_token token2;
11283 int saved_pedantic;
11284 void *p;
11285 tree attributes = NULL_TREE;
11287 /* Check for the `__extension__' keyword. */
11288 if (cp_parser_extension_opt (parser, &saved_pedantic))
11290 /* Parse the qualified declaration. */
11291 cp_parser_declaration (parser);
11292 /* Restore the PEDANTIC flag. */
11293 pedantic = saved_pedantic;
11295 return;
11298 /* Try to figure out what kind of declaration is present. */
11299 token1 = *cp_lexer_peek_token (parser->lexer);
11301 if (token1.type != CPP_EOF)
11302 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11303 else
11305 token2.type = CPP_EOF;
11306 token2.keyword = RID_MAX;
11309 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11310 p = obstack_alloc (&declarator_obstack, 0);
11312 /* If the next token is `extern' and the following token is a string
11313 literal, then we have a linkage specification. */
11314 if (token1.keyword == RID_EXTERN
11315 && cp_parser_is_pure_string_literal (&token2))
11316 cp_parser_linkage_specification (parser);
11317 /* If the next token is `template', then we have either a template
11318 declaration, an explicit instantiation, or an explicit
11319 specialization. */
11320 else if (token1.keyword == RID_TEMPLATE)
11322 /* `template <>' indicates a template specialization. */
11323 if (token2.type == CPP_LESS
11324 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11325 cp_parser_explicit_specialization (parser);
11326 /* `template <' indicates a template declaration. */
11327 else if (token2.type == CPP_LESS)
11328 cp_parser_template_declaration (parser, /*member_p=*/false);
11329 /* Anything else must be an explicit instantiation. */
11330 else
11331 cp_parser_explicit_instantiation (parser);
11333 /* If the next token is `export', then we have a template
11334 declaration. */
11335 else if (token1.keyword == RID_EXPORT)
11336 cp_parser_template_declaration (parser, /*member_p=*/false);
11337 /* If the next token is `extern', 'static' or 'inline' and the one
11338 after that is `template', we have a GNU extended explicit
11339 instantiation directive. */
11340 else if (cp_parser_allow_gnu_extensions_p (parser)
11341 && (token1.keyword == RID_EXTERN
11342 || token1.keyword == RID_STATIC
11343 || token1.keyword == RID_INLINE)
11344 && token2.keyword == RID_TEMPLATE)
11345 cp_parser_explicit_instantiation (parser);
11346 /* If the next token is `namespace', check for a named or unnamed
11347 namespace definition. */
11348 else if (token1.keyword == RID_NAMESPACE
11349 && (/* A named namespace definition. */
11350 (token2.type == CPP_NAME
11351 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11352 != CPP_EQ))
11353 /* An unnamed namespace definition. */
11354 || token2.type == CPP_OPEN_BRACE
11355 || token2.keyword == RID_ATTRIBUTE))
11356 cp_parser_namespace_definition (parser);
11357 /* An inline (associated) namespace definition. */
11358 else if (token1.keyword == RID_INLINE
11359 && token2.keyword == RID_NAMESPACE)
11360 cp_parser_namespace_definition (parser);
11361 /* Objective-C++ declaration/definition. */
11362 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11363 cp_parser_objc_declaration (parser, NULL_TREE);
11364 else if (c_dialect_objc ()
11365 && token1.keyword == RID_ATTRIBUTE
11366 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11367 cp_parser_objc_declaration (parser, attributes);
11368 /* We must have either a block declaration or a function
11369 definition. */
11370 else
11371 /* Try to parse a block-declaration, or a function-definition. */
11372 cp_parser_block_declaration (parser, /*statement_p=*/false);
11374 /* Free any declarators allocated. */
11375 obstack_free (&declarator_obstack, p);
11378 /* Parse a block-declaration.
11380 block-declaration:
11381 simple-declaration
11382 asm-definition
11383 namespace-alias-definition
11384 using-declaration
11385 using-directive
11387 GNU Extension:
11389 block-declaration:
11390 __extension__ block-declaration
11392 C++0x Extension:
11394 block-declaration:
11395 static_assert-declaration
11397 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11398 part of a declaration-statement. */
11400 static void
11401 cp_parser_block_declaration (cp_parser *parser,
11402 bool statement_p)
11404 cp_token *token1;
11405 int saved_pedantic;
11407 /* Check for the `__extension__' keyword. */
11408 if (cp_parser_extension_opt (parser, &saved_pedantic))
11410 /* Parse the qualified declaration. */
11411 cp_parser_block_declaration (parser, statement_p);
11412 /* Restore the PEDANTIC flag. */
11413 pedantic = saved_pedantic;
11415 return;
11418 /* Peek at the next token to figure out which kind of declaration is
11419 present. */
11420 token1 = cp_lexer_peek_token (parser->lexer);
11422 /* If the next keyword is `asm', we have an asm-definition. */
11423 if (token1->keyword == RID_ASM)
11425 if (statement_p)
11426 cp_parser_commit_to_tentative_parse (parser);
11427 cp_parser_asm_definition (parser);
11429 /* If the next keyword is `namespace', we have a
11430 namespace-alias-definition. */
11431 else if (token1->keyword == RID_NAMESPACE)
11432 cp_parser_namespace_alias_definition (parser);
11433 /* If the next keyword is `using', we have a
11434 using-declaration, a using-directive, or an alias-declaration. */
11435 else if (token1->keyword == RID_USING)
11437 cp_token *token2;
11439 if (statement_p)
11440 cp_parser_commit_to_tentative_parse (parser);
11441 /* If the token after `using' is `namespace', then we have a
11442 using-directive. */
11443 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11444 if (token2->keyword == RID_NAMESPACE)
11445 cp_parser_using_directive (parser);
11446 /* If the second token after 'using' is '=', then we have an
11447 alias-declaration. */
11448 else if (cxx_dialect >= cxx11
11449 && token2->type == CPP_NAME
11450 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11451 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11452 cp_parser_alias_declaration (parser);
11453 /* Otherwise, it's a using-declaration. */
11454 else
11455 cp_parser_using_declaration (parser,
11456 /*access_declaration_p=*/false);
11458 /* If the next keyword is `__label__' we have a misplaced label
11459 declaration. */
11460 else if (token1->keyword == RID_LABEL)
11462 cp_lexer_consume_token (parser->lexer);
11463 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11464 cp_parser_skip_to_end_of_statement (parser);
11465 /* If the next token is now a `;', consume it. */
11466 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11467 cp_lexer_consume_token (parser->lexer);
11469 /* If the next token is `static_assert' we have a static assertion. */
11470 else if (token1->keyword == RID_STATIC_ASSERT)
11471 cp_parser_static_assert (parser, /*member_p=*/false);
11472 /* Anything else must be a simple-declaration. */
11473 else
11474 cp_parser_simple_declaration (parser, !statement_p,
11475 /*maybe_range_for_decl*/NULL);
11478 /* Parse a simple-declaration.
11480 simple-declaration:
11481 decl-specifier-seq [opt] init-declarator-list [opt] ;
11483 init-declarator-list:
11484 init-declarator
11485 init-declarator-list , init-declarator
11487 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11488 function-definition as a simple-declaration.
11490 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11491 parsed declaration if it is an uninitialized single declarator not followed
11492 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11493 if present, will not be consumed. */
11495 static void
11496 cp_parser_simple_declaration (cp_parser* parser,
11497 bool function_definition_allowed_p,
11498 tree *maybe_range_for_decl)
11500 cp_decl_specifier_seq decl_specifiers;
11501 int declares_class_or_enum;
11502 bool saw_declarator;
11503 location_t comma_loc = UNKNOWN_LOCATION;
11504 location_t init_loc = UNKNOWN_LOCATION;
11506 if (maybe_range_for_decl)
11507 *maybe_range_for_decl = NULL_TREE;
11509 /* Defer access checks until we know what is being declared; the
11510 checks for names appearing in the decl-specifier-seq should be
11511 done as if we were in the scope of the thing being declared. */
11512 push_deferring_access_checks (dk_deferred);
11514 /* Parse the decl-specifier-seq. We have to keep track of whether
11515 or not the decl-specifier-seq declares a named class or
11516 enumeration type, since that is the only case in which the
11517 init-declarator-list is allowed to be empty.
11519 [dcl.dcl]
11521 In a simple-declaration, the optional init-declarator-list can be
11522 omitted only when declaring a class or enumeration, that is when
11523 the decl-specifier-seq contains either a class-specifier, an
11524 elaborated-type-specifier, or an enum-specifier. */
11525 cp_parser_decl_specifier_seq (parser,
11526 CP_PARSER_FLAGS_OPTIONAL,
11527 &decl_specifiers,
11528 &declares_class_or_enum);
11529 /* We no longer need to defer access checks. */
11530 stop_deferring_access_checks ();
11532 /* In a block scope, a valid declaration must always have a
11533 decl-specifier-seq. By not trying to parse declarators, we can
11534 resolve the declaration/expression ambiguity more quickly. */
11535 if (!function_definition_allowed_p
11536 && !decl_specifiers.any_specifiers_p)
11538 cp_parser_error (parser, "expected declaration");
11539 goto done;
11542 /* If the next two tokens are both identifiers, the code is
11543 erroneous. The usual cause of this situation is code like:
11545 T t;
11547 where "T" should name a type -- but does not. */
11548 if (!decl_specifiers.any_type_specifiers_p
11549 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11551 /* If parsing tentatively, we should commit; we really are
11552 looking at a declaration. */
11553 cp_parser_commit_to_tentative_parse (parser);
11554 /* Give up. */
11555 goto done;
11558 /* If we have seen at least one decl-specifier, and the next token
11559 is not a parenthesis, then we must be looking at a declaration.
11560 (After "int (" we might be looking at a functional cast.) */
11561 if (decl_specifiers.any_specifiers_p
11562 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11563 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11564 && !cp_parser_error_occurred (parser))
11565 cp_parser_commit_to_tentative_parse (parser);
11567 /* Keep going until we hit the `;' at the end of the simple
11568 declaration. */
11569 saw_declarator = false;
11570 while (cp_lexer_next_token_is_not (parser->lexer,
11571 CPP_SEMICOLON))
11573 cp_token *token;
11574 bool function_definition_p;
11575 tree decl;
11577 if (saw_declarator)
11579 /* If we are processing next declarator, comma is expected */
11580 token = cp_lexer_peek_token (parser->lexer);
11581 gcc_assert (token->type == CPP_COMMA);
11582 cp_lexer_consume_token (parser->lexer);
11583 if (maybe_range_for_decl)
11585 *maybe_range_for_decl = error_mark_node;
11586 if (comma_loc == UNKNOWN_LOCATION)
11587 comma_loc = token->location;
11590 else
11591 saw_declarator = true;
11593 /* Parse the init-declarator. */
11594 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11595 /*checks=*/NULL,
11596 function_definition_allowed_p,
11597 /*member_p=*/false,
11598 declares_class_or_enum,
11599 &function_definition_p,
11600 maybe_range_for_decl,
11601 &init_loc);
11602 /* If an error occurred while parsing tentatively, exit quickly.
11603 (That usually happens when in the body of a function; each
11604 statement is treated as a declaration-statement until proven
11605 otherwise.) */
11606 if (cp_parser_error_occurred (parser))
11607 goto done;
11608 /* Handle function definitions specially. */
11609 if (function_definition_p)
11611 /* If the next token is a `,', then we are probably
11612 processing something like:
11614 void f() {}, *p;
11616 which is erroneous. */
11617 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11619 cp_token *token = cp_lexer_peek_token (parser->lexer);
11620 error_at (token->location,
11621 "mixing"
11622 " declarations and function-definitions is forbidden");
11624 /* Otherwise, we're done with the list of declarators. */
11625 else
11627 pop_deferring_access_checks ();
11628 return;
11631 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11632 *maybe_range_for_decl = decl;
11633 /* The next token should be either a `,' or a `;'. */
11634 token = cp_lexer_peek_token (parser->lexer);
11635 /* If it's a `,', there are more declarators to come. */
11636 if (token->type == CPP_COMMA)
11637 /* will be consumed next time around */;
11638 /* If it's a `;', we are done. */
11639 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11640 break;
11641 /* Anything else is an error. */
11642 else
11644 /* If we have already issued an error message we don't need
11645 to issue another one. */
11646 if (decl != error_mark_node
11647 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11648 cp_parser_error (parser, "expected %<,%> or %<;%>");
11649 /* Skip tokens until we reach the end of the statement. */
11650 cp_parser_skip_to_end_of_statement (parser);
11651 /* If the next token is now a `;', consume it. */
11652 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11653 cp_lexer_consume_token (parser->lexer);
11654 goto done;
11656 /* After the first time around, a function-definition is not
11657 allowed -- even if it was OK at first. For example:
11659 int i, f() {}
11661 is not valid. */
11662 function_definition_allowed_p = false;
11665 /* Issue an error message if no declarators are present, and the
11666 decl-specifier-seq does not itself declare a class or
11667 enumeration: [dcl.dcl]/3. */
11668 if (!saw_declarator)
11670 if (cp_parser_declares_only_class_p (parser))
11672 if (!declares_class_or_enum
11673 && decl_specifiers.type
11674 && OVERLOAD_TYPE_P (decl_specifiers.type))
11675 /* Ensure an error is issued anyway when finish_decltype_type,
11676 called via cp_parser_decl_specifier_seq, returns a class or
11677 an enumeration (c++/51786). */
11678 decl_specifiers.type = NULL_TREE;
11679 shadow_tag (&decl_specifiers);
11681 /* Perform any deferred access checks. */
11682 perform_deferred_access_checks (tf_warning_or_error);
11685 /* Consume the `;'. */
11686 if (!maybe_range_for_decl)
11687 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11688 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11690 if (init_loc != UNKNOWN_LOCATION)
11691 error_at (init_loc, "initializer in range-based %<for%> loop");
11692 if (comma_loc != UNKNOWN_LOCATION)
11693 error_at (comma_loc,
11694 "multiple declarations in range-based %<for%> loop");
11697 done:
11698 pop_deferring_access_checks ();
11701 /* Parse a decl-specifier-seq.
11703 decl-specifier-seq:
11704 decl-specifier-seq [opt] decl-specifier
11705 decl-specifier attribute-specifier-seq [opt] (C++11)
11707 decl-specifier:
11708 storage-class-specifier
11709 type-specifier
11710 function-specifier
11711 friend
11712 typedef
11714 GNU Extension:
11716 decl-specifier:
11717 attributes
11719 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11721 The parser flags FLAGS is used to control type-specifier parsing.
11723 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11724 flags:
11726 1: one of the decl-specifiers is an elaborated-type-specifier
11727 (i.e., a type declaration)
11728 2: one of the decl-specifiers is an enum-specifier or a
11729 class-specifier (i.e., a type definition)
11733 static void
11734 cp_parser_decl_specifier_seq (cp_parser* parser,
11735 cp_parser_flags flags,
11736 cp_decl_specifier_seq *decl_specs,
11737 int* declares_class_or_enum)
11739 bool constructor_possible_p = !parser->in_declarator_p;
11740 bool found_decl_spec = false;
11741 cp_token *start_token = NULL;
11742 cp_decl_spec ds;
11744 /* Clear DECL_SPECS. */
11745 clear_decl_specs (decl_specs);
11747 /* Assume no class or enumeration type is declared. */
11748 *declares_class_or_enum = 0;
11750 /* Keep reading specifiers until there are no more to read. */
11751 while (true)
11753 bool constructor_p;
11754 cp_token *token;
11755 ds = ds_last;
11757 /* Peek at the next token. */
11758 token = cp_lexer_peek_token (parser->lexer);
11760 /* Save the first token of the decl spec list for error
11761 reporting. */
11762 if (!start_token)
11763 start_token = token;
11764 /* Handle attributes. */
11765 if (cp_next_tokens_can_be_attribute_p (parser))
11767 /* Parse the attributes. */
11768 tree attrs = cp_parser_attributes_opt (parser);
11770 /* In a sequence of declaration specifiers, c++11 attributes
11771 appertain to the type that precede them. In that case
11772 [dcl.spec]/1 says:
11774 The attribute-specifier-seq affects the type only for
11775 the declaration it appears in, not other declarations
11776 involving the same type.
11778 But for now let's force the user to position the
11779 attribute either at the beginning of the declaration or
11780 after the declarator-id, which would clearly mean that it
11781 applies to the declarator. */
11782 if (cxx11_attribute_p (attrs))
11784 if (!found_decl_spec)
11785 /* The c++11 attribute is at the beginning of the
11786 declaration. It appertains to the entity being
11787 declared. */;
11788 else
11790 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11792 /* This is an attribute following a
11793 class-specifier. */
11794 if (decl_specs->type_definition_p)
11795 warn_misplaced_attr_for_class_type (token->location,
11796 decl_specs->type);
11797 attrs = NULL_TREE;
11799 else
11801 decl_specs->std_attributes
11802 = chainon (decl_specs->std_attributes,
11803 attrs);
11804 if (decl_specs->locations[ds_std_attribute] == 0)
11805 decl_specs->locations[ds_std_attribute] = token->location;
11807 continue;
11811 decl_specs->attributes
11812 = chainon (decl_specs->attributes,
11813 attrs);
11814 if (decl_specs->locations[ds_attribute] == 0)
11815 decl_specs->locations[ds_attribute] = token->location;
11816 continue;
11818 /* Assume we will find a decl-specifier keyword. */
11819 found_decl_spec = true;
11820 /* If the next token is an appropriate keyword, we can simply
11821 add it to the list. */
11822 switch (token->keyword)
11824 /* decl-specifier:
11825 friend
11826 constexpr */
11827 case RID_FRIEND:
11828 if (!at_class_scope_p ())
11830 error_at (token->location, "%<friend%> used outside of class");
11831 cp_lexer_purge_token (parser->lexer);
11833 else
11835 ds = ds_friend;
11836 /* Consume the token. */
11837 cp_lexer_consume_token (parser->lexer);
11839 break;
11841 case RID_CONSTEXPR:
11842 ds = ds_constexpr;
11843 cp_lexer_consume_token (parser->lexer);
11844 break;
11846 /* function-specifier:
11847 inline
11848 virtual
11849 explicit */
11850 case RID_INLINE:
11851 case RID_VIRTUAL:
11852 case RID_EXPLICIT:
11853 cp_parser_function_specifier_opt (parser, decl_specs);
11854 break;
11856 /* decl-specifier:
11857 typedef */
11858 case RID_TYPEDEF:
11859 ds = ds_typedef;
11860 /* Consume the token. */
11861 cp_lexer_consume_token (parser->lexer);
11862 /* A constructor declarator cannot appear in a typedef. */
11863 constructor_possible_p = false;
11864 /* The "typedef" keyword can only occur in a declaration; we
11865 may as well commit at this point. */
11866 cp_parser_commit_to_tentative_parse (parser);
11868 if (decl_specs->storage_class != sc_none)
11869 decl_specs->conflicting_specifiers_p = true;
11870 break;
11872 /* storage-class-specifier:
11873 auto
11874 register
11875 static
11876 extern
11877 mutable
11879 GNU Extension:
11880 thread */
11881 case RID_AUTO:
11882 if (cxx_dialect == cxx98)
11884 /* Consume the token. */
11885 cp_lexer_consume_token (parser->lexer);
11887 /* Complain about `auto' as a storage specifier, if
11888 we're complaining about C++0x compatibility. */
11889 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11890 " changes meaning in C++11; please remove it");
11892 /* Set the storage class anyway. */
11893 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11894 token);
11896 else
11897 /* C++0x auto type-specifier. */
11898 found_decl_spec = false;
11899 break;
11901 case RID_REGISTER:
11902 case RID_STATIC:
11903 case RID_EXTERN:
11904 case RID_MUTABLE:
11905 /* Consume the token. */
11906 cp_lexer_consume_token (parser->lexer);
11907 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11908 token);
11909 break;
11910 case RID_THREAD:
11911 /* Consume the token. */
11912 ds = ds_thread;
11913 cp_lexer_consume_token (parser->lexer);
11914 break;
11916 default:
11917 /* We did not yet find a decl-specifier yet. */
11918 found_decl_spec = false;
11919 break;
11922 if (found_decl_spec
11923 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11924 && token->keyword != RID_CONSTEXPR)
11925 error ("decl-specifier invalid in condition");
11927 if (ds != ds_last)
11928 set_and_check_decl_spec_loc (decl_specs, ds, token);
11930 /* Constructors are a special case. The `S' in `S()' is not a
11931 decl-specifier; it is the beginning of the declarator. */
11932 constructor_p
11933 = (!found_decl_spec
11934 && constructor_possible_p
11935 && (cp_parser_constructor_declarator_p
11936 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11938 /* If we don't have a DECL_SPEC yet, then we must be looking at
11939 a type-specifier. */
11940 if (!found_decl_spec && !constructor_p)
11942 int decl_spec_declares_class_or_enum;
11943 bool is_cv_qualifier;
11944 tree type_spec;
11946 type_spec
11947 = cp_parser_type_specifier (parser, flags,
11948 decl_specs,
11949 /*is_declaration=*/true,
11950 &decl_spec_declares_class_or_enum,
11951 &is_cv_qualifier);
11952 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11954 /* If this type-specifier referenced a user-defined type
11955 (a typedef, class-name, etc.), then we can't allow any
11956 more such type-specifiers henceforth.
11958 [dcl.spec]
11960 The longest sequence of decl-specifiers that could
11961 possibly be a type name is taken as the
11962 decl-specifier-seq of a declaration. The sequence shall
11963 be self-consistent as described below.
11965 [dcl.type]
11967 As a general rule, at most one type-specifier is allowed
11968 in the complete decl-specifier-seq of a declaration. The
11969 only exceptions are the following:
11971 -- const or volatile can be combined with any other
11972 type-specifier.
11974 -- signed or unsigned can be combined with char, long,
11975 short, or int.
11977 -- ..
11979 Example:
11981 typedef char* Pc;
11982 void g (const int Pc);
11984 Here, Pc is *not* part of the decl-specifier seq; it's
11985 the declarator. Therefore, once we see a type-specifier
11986 (other than a cv-qualifier), we forbid any additional
11987 user-defined types. We *do* still allow things like `int
11988 int' to be considered a decl-specifier-seq, and issue the
11989 error message later. */
11990 if (type_spec && !is_cv_qualifier)
11991 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11992 /* A constructor declarator cannot follow a type-specifier. */
11993 if (type_spec)
11995 constructor_possible_p = false;
11996 found_decl_spec = true;
11997 if (!is_cv_qualifier)
11998 decl_specs->any_type_specifiers_p = true;
12002 /* If we still do not have a DECL_SPEC, then there are no more
12003 decl-specifiers. */
12004 if (!found_decl_spec)
12005 break;
12007 decl_specs->any_specifiers_p = true;
12008 /* After we see one decl-specifier, further decl-specifiers are
12009 always optional. */
12010 flags |= CP_PARSER_FLAGS_OPTIONAL;
12013 /* Don't allow a friend specifier with a class definition. */
12014 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12015 && (*declares_class_or_enum & 2))
12016 error_at (decl_specs->locations[ds_friend],
12017 "class definition may not be declared a friend");
12020 /* Parse an (optional) storage-class-specifier.
12022 storage-class-specifier:
12023 auto
12024 register
12025 static
12026 extern
12027 mutable
12029 GNU Extension:
12031 storage-class-specifier:
12032 thread
12034 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12036 static tree
12037 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12039 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12041 case RID_AUTO:
12042 if (cxx_dialect != cxx98)
12043 return NULL_TREE;
12044 /* Fall through for C++98. */
12046 case RID_REGISTER:
12047 case RID_STATIC:
12048 case RID_EXTERN:
12049 case RID_MUTABLE:
12050 case RID_THREAD:
12051 /* Consume the token. */
12052 return cp_lexer_consume_token (parser->lexer)->u.value;
12054 default:
12055 return NULL_TREE;
12059 /* Parse an (optional) function-specifier.
12061 function-specifier:
12062 inline
12063 virtual
12064 explicit
12066 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12067 Updates DECL_SPECS, if it is non-NULL. */
12069 static tree
12070 cp_parser_function_specifier_opt (cp_parser* parser,
12071 cp_decl_specifier_seq *decl_specs)
12073 cp_token *token = cp_lexer_peek_token (parser->lexer);
12074 switch (token->keyword)
12076 case RID_INLINE:
12077 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12078 break;
12080 case RID_VIRTUAL:
12081 /* 14.5.2.3 [temp.mem]
12083 A member function template shall not be virtual. */
12084 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12085 error_at (token->location, "templates may not be %<virtual%>");
12086 else
12087 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12088 break;
12090 case RID_EXPLICIT:
12091 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12092 break;
12094 default:
12095 return NULL_TREE;
12098 /* Consume the token. */
12099 return cp_lexer_consume_token (parser->lexer)->u.value;
12102 /* Parse a linkage-specification.
12104 linkage-specification:
12105 extern string-literal { declaration-seq [opt] }
12106 extern string-literal declaration */
12108 static void
12109 cp_parser_linkage_specification (cp_parser* parser)
12111 tree linkage;
12113 /* Look for the `extern' keyword. */
12114 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12116 /* Look for the string-literal. */
12117 linkage = cp_parser_string_literal (parser, false, false);
12119 /* Transform the literal into an identifier. If the literal is a
12120 wide-character string, or contains embedded NULs, then we can't
12121 handle it as the user wants. */
12122 if (strlen (TREE_STRING_POINTER (linkage))
12123 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12125 cp_parser_error (parser, "invalid linkage-specification");
12126 /* Assume C++ linkage. */
12127 linkage = lang_name_cplusplus;
12129 else
12130 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12132 /* We're now using the new linkage. */
12133 push_lang_context (linkage);
12135 /* If the next token is a `{', then we're using the first
12136 production. */
12137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12139 cp_ensure_no_omp_declare_simd (parser);
12141 /* Consume the `{' token. */
12142 cp_lexer_consume_token (parser->lexer);
12143 /* Parse the declarations. */
12144 cp_parser_declaration_seq_opt (parser);
12145 /* Look for the closing `}'. */
12146 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12148 /* Otherwise, there's just one declaration. */
12149 else
12151 bool saved_in_unbraced_linkage_specification_p;
12153 saved_in_unbraced_linkage_specification_p
12154 = parser->in_unbraced_linkage_specification_p;
12155 parser->in_unbraced_linkage_specification_p = true;
12156 cp_parser_declaration (parser);
12157 parser->in_unbraced_linkage_specification_p
12158 = saved_in_unbraced_linkage_specification_p;
12161 /* We're done with the linkage-specification. */
12162 pop_lang_context ();
12165 /* Parse a static_assert-declaration.
12167 static_assert-declaration:
12168 static_assert ( constant-expression , string-literal ) ;
12170 If MEMBER_P, this static_assert is a class member. */
12172 static void
12173 cp_parser_static_assert(cp_parser *parser, bool member_p)
12175 tree condition;
12176 tree message;
12177 cp_token *token;
12178 location_t saved_loc;
12179 bool dummy;
12181 /* Peek at the `static_assert' token so we can keep track of exactly
12182 where the static assertion started. */
12183 token = cp_lexer_peek_token (parser->lexer);
12184 saved_loc = token->location;
12186 /* Look for the `static_assert' keyword. */
12187 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12188 RT_STATIC_ASSERT))
12189 return;
12191 /* We know we are in a static assertion; commit to any tentative
12192 parse. */
12193 if (cp_parser_parsing_tentatively (parser))
12194 cp_parser_commit_to_tentative_parse (parser);
12196 /* Parse the `(' starting the static assertion condition. */
12197 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12199 /* Parse the constant-expression. Allow a non-constant expression
12200 here in order to give better diagnostics in finish_static_assert. */
12201 condition =
12202 cp_parser_constant_expression (parser,
12203 /*allow_non_constant_p=*/true,
12204 /*non_constant_p=*/&dummy);
12206 /* Parse the separating `,'. */
12207 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12209 /* Parse the string-literal message. */
12210 message = cp_parser_string_literal (parser,
12211 /*translate=*/false,
12212 /*wide_ok=*/true);
12214 /* A `)' completes the static assertion. */
12215 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12216 cp_parser_skip_to_closing_parenthesis (parser,
12217 /*recovering=*/true,
12218 /*or_comma=*/false,
12219 /*consume_paren=*/true);
12221 /* A semicolon terminates the declaration. */
12222 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12224 /* Complete the static assertion, which may mean either processing
12225 the static assert now or saving it for template instantiation. */
12226 finish_static_assert (condition, message, saved_loc, member_p);
12229 /* Parse the expression in decltype ( expression ). */
12231 static tree
12232 cp_parser_decltype_expr (cp_parser *parser,
12233 bool &id_expression_or_member_access_p)
12235 cp_token *id_expr_start_token;
12236 tree expr;
12238 /* First, try parsing an id-expression. */
12239 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12240 cp_parser_parse_tentatively (parser);
12241 expr = cp_parser_id_expression (parser,
12242 /*template_keyword_p=*/false,
12243 /*check_dependency_p=*/true,
12244 /*template_p=*/NULL,
12245 /*declarator_p=*/false,
12246 /*optional_p=*/false);
12248 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12250 bool non_integral_constant_expression_p = false;
12251 tree id_expression = expr;
12252 cp_id_kind idk;
12253 const char *error_msg;
12255 if (identifier_p (expr))
12256 /* Lookup the name we got back from the id-expression. */
12257 expr = cp_parser_lookup_name_simple (parser, expr,
12258 id_expr_start_token->location);
12260 if (expr
12261 && expr != error_mark_node
12262 && TREE_CODE (expr) != TYPE_DECL
12263 && (TREE_CODE (expr) != BIT_NOT_EXPR
12264 || !TYPE_P (TREE_OPERAND (expr, 0)))
12265 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12267 /* Complete lookup of the id-expression. */
12268 expr = (finish_id_expression
12269 (id_expression, expr, parser->scope, &idk,
12270 /*integral_constant_expression_p=*/false,
12271 /*allow_non_integral_constant_expression_p=*/true,
12272 &non_integral_constant_expression_p,
12273 /*template_p=*/false,
12274 /*done=*/true,
12275 /*address_p=*/false,
12276 /*template_arg_p=*/false,
12277 &error_msg,
12278 id_expr_start_token->location));
12280 if (expr == error_mark_node)
12281 /* We found an id-expression, but it was something that we
12282 should not have found. This is an error, not something
12283 we can recover from, so note that we found an
12284 id-expression and we'll recover as gracefully as
12285 possible. */
12286 id_expression_or_member_access_p = true;
12289 if (expr
12290 && expr != error_mark_node
12291 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12292 /* We have an id-expression. */
12293 id_expression_or_member_access_p = true;
12296 if (!id_expression_or_member_access_p)
12298 /* Abort the id-expression parse. */
12299 cp_parser_abort_tentative_parse (parser);
12301 /* Parsing tentatively, again. */
12302 cp_parser_parse_tentatively (parser);
12304 /* Parse a class member access. */
12305 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12306 /*cast_p=*/false, /*decltype*/true,
12307 /*member_access_only_p=*/true, NULL);
12309 if (expr
12310 && expr != error_mark_node
12311 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12312 /* We have an id-expression. */
12313 id_expression_or_member_access_p = true;
12316 if (id_expression_or_member_access_p)
12317 /* We have parsed the complete id-expression or member access. */
12318 cp_parser_parse_definitely (parser);
12319 else
12321 /* Abort our attempt to parse an id-expression or member access
12322 expression. */
12323 cp_parser_abort_tentative_parse (parser);
12325 /* Parse a full expression. */
12326 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12327 /*decltype_p=*/true);
12330 return expr;
12333 /* Parse a `decltype' type. Returns the type.
12335 simple-type-specifier:
12336 decltype ( expression )
12337 C++14 proposal:
12338 decltype ( auto ) */
12340 static tree
12341 cp_parser_decltype (cp_parser *parser)
12343 tree expr;
12344 bool id_expression_or_member_access_p = false;
12345 const char *saved_message;
12346 bool saved_integral_constant_expression_p;
12347 bool saved_non_integral_constant_expression_p;
12348 bool saved_greater_than_is_operator_p;
12349 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12351 if (start_token->type == CPP_DECLTYPE)
12353 /* Already parsed. */
12354 cp_lexer_consume_token (parser->lexer);
12355 return start_token->u.value;
12358 /* Look for the `decltype' token. */
12359 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12360 return error_mark_node;
12362 /* Parse the opening `('. */
12363 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12364 return error_mark_node;
12366 /* decltype (auto) */
12367 if (cxx_dialect >= cxx14
12368 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12370 cp_lexer_consume_token (parser->lexer);
12371 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12372 return error_mark_node;
12373 expr = make_decltype_auto ();
12374 AUTO_IS_DECLTYPE (expr) = true;
12375 goto rewrite;
12378 /* Types cannot be defined in a `decltype' expression. Save away the
12379 old message. */
12380 saved_message = parser->type_definition_forbidden_message;
12382 /* And create the new one. */
12383 parser->type_definition_forbidden_message
12384 = G_("types may not be defined in %<decltype%> expressions");
12386 /* The restrictions on constant-expressions do not apply inside
12387 decltype expressions. */
12388 saved_integral_constant_expression_p
12389 = parser->integral_constant_expression_p;
12390 saved_non_integral_constant_expression_p
12391 = parser->non_integral_constant_expression_p;
12392 parser->integral_constant_expression_p = false;
12394 /* Within a parenthesized expression, a `>' token is always
12395 the greater-than operator. */
12396 saved_greater_than_is_operator_p
12397 = parser->greater_than_is_operator_p;
12398 parser->greater_than_is_operator_p = true;
12400 /* Do not actually evaluate the expression. */
12401 ++cp_unevaluated_operand;
12403 /* Do not warn about problems with the expression. */
12404 ++c_inhibit_evaluation_warnings;
12406 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12408 /* Go back to evaluating expressions. */
12409 --cp_unevaluated_operand;
12410 --c_inhibit_evaluation_warnings;
12412 /* The `>' token might be the end of a template-id or
12413 template-parameter-list now. */
12414 parser->greater_than_is_operator_p
12415 = saved_greater_than_is_operator_p;
12417 /* Restore the old message and the integral constant expression
12418 flags. */
12419 parser->type_definition_forbidden_message = saved_message;
12420 parser->integral_constant_expression_p
12421 = saved_integral_constant_expression_p;
12422 parser->non_integral_constant_expression_p
12423 = saved_non_integral_constant_expression_p;
12425 /* Parse to the closing `)'. */
12426 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12428 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12429 /*consume_paren=*/true);
12430 return error_mark_node;
12433 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12434 tf_warning_or_error);
12436 rewrite:
12437 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12438 it again. */
12439 start_token->type = CPP_DECLTYPE;
12440 start_token->u.value = expr;
12441 start_token->keyword = RID_MAX;
12442 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12444 return expr;
12447 /* Special member functions [gram.special] */
12449 /* Parse a conversion-function-id.
12451 conversion-function-id:
12452 operator conversion-type-id
12454 Returns an IDENTIFIER_NODE representing the operator. */
12456 static tree
12457 cp_parser_conversion_function_id (cp_parser* parser)
12459 tree type;
12460 tree saved_scope;
12461 tree saved_qualifying_scope;
12462 tree saved_object_scope;
12463 tree pushed_scope = NULL_TREE;
12465 /* Look for the `operator' token. */
12466 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12467 return error_mark_node;
12468 /* When we parse the conversion-type-id, the current scope will be
12469 reset. However, we need that information in able to look up the
12470 conversion function later, so we save it here. */
12471 saved_scope = parser->scope;
12472 saved_qualifying_scope = parser->qualifying_scope;
12473 saved_object_scope = parser->object_scope;
12474 /* We must enter the scope of the class so that the names of
12475 entities declared within the class are available in the
12476 conversion-type-id. For example, consider:
12478 struct S {
12479 typedef int I;
12480 operator I();
12483 S::operator I() { ... }
12485 In order to see that `I' is a type-name in the definition, we
12486 must be in the scope of `S'. */
12487 if (saved_scope)
12488 pushed_scope = push_scope (saved_scope);
12489 /* Parse the conversion-type-id. */
12490 type = cp_parser_conversion_type_id (parser);
12491 /* Leave the scope of the class, if any. */
12492 if (pushed_scope)
12493 pop_scope (pushed_scope);
12494 /* Restore the saved scope. */
12495 parser->scope = saved_scope;
12496 parser->qualifying_scope = saved_qualifying_scope;
12497 parser->object_scope = saved_object_scope;
12498 /* If the TYPE is invalid, indicate failure. */
12499 if (type == error_mark_node)
12500 return error_mark_node;
12501 return mangle_conv_op_name_for_type (type);
12504 /* Parse a conversion-type-id:
12506 conversion-type-id:
12507 type-specifier-seq conversion-declarator [opt]
12509 Returns the TYPE specified. */
12511 static tree
12512 cp_parser_conversion_type_id (cp_parser* parser)
12514 tree attributes;
12515 cp_decl_specifier_seq type_specifiers;
12516 cp_declarator *declarator;
12517 tree type_specified;
12518 const char *saved_message;
12520 /* Parse the attributes. */
12521 attributes = cp_parser_attributes_opt (parser);
12523 saved_message = parser->type_definition_forbidden_message;
12524 parser->type_definition_forbidden_message
12525 = G_("types may not be defined in a conversion-type-id");
12527 /* Parse the type-specifiers. */
12528 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12529 /*is_trailing_return=*/false,
12530 &type_specifiers);
12532 parser->type_definition_forbidden_message = saved_message;
12534 /* If that didn't work, stop. */
12535 if (type_specifiers.type == error_mark_node)
12536 return error_mark_node;
12537 /* Parse the conversion-declarator. */
12538 declarator = cp_parser_conversion_declarator_opt (parser);
12540 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12541 /*initialized=*/0, &attributes);
12542 if (attributes)
12543 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12545 /* Don't give this error when parsing tentatively. This happens to
12546 work because we always parse this definitively once. */
12547 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12548 && type_uses_auto (type_specified))
12550 if (cxx_dialect < cxx14)
12552 error ("invalid use of %<auto%> in conversion operator");
12553 return error_mark_node;
12555 else if (template_parm_scope_p ())
12556 warning (0, "use of %<auto%> in member template "
12557 "conversion operator can never be deduced");
12560 return type_specified;
12563 /* Parse an (optional) conversion-declarator.
12565 conversion-declarator:
12566 ptr-operator conversion-declarator [opt]
12570 static cp_declarator *
12571 cp_parser_conversion_declarator_opt (cp_parser* parser)
12573 enum tree_code code;
12574 tree class_type, std_attributes = NULL_TREE;
12575 cp_cv_quals cv_quals;
12577 /* We don't know if there's a ptr-operator next, or not. */
12578 cp_parser_parse_tentatively (parser);
12579 /* Try the ptr-operator. */
12580 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12581 &std_attributes);
12582 /* If it worked, look for more conversion-declarators. */
12583 if (cp_parser_parse_definitely (parser))
12585 cp_declarator *declarator;
12587 /* Parse another optional declarator. */
12588 declarator = cp_parser_conversion_declarator_opt (parser);
12590 declarator = cp_parser_make_indirect_declarator
12591 (code, class_type, cv_quals, declarator, std_attributes);
12593 return declarator;
12596 return NULL;
12599 /* Parse an (optional) ctor-initializer.
12601 ctor-initializer:
12602 : mem-initializer-list
12604 Returns TRUE iff the ctor-initializer was actually present. */
12606 static bool
12607 cp_parser_ctor_initializer_opt (cp_parser* parser)
12609 /* If the next token is not a `:', then there is no
12610 ctor-initializer. */
12611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12613 /* Do default initialization of any bases and members. */
12614 if (DECL_CONSTRUCTOR_P (current_function_decl))
12615 finish_mem_initializers (NULL_TREE);
12617 return false;
12620 /* Consume the `:' token. */
12621 cp_lexer_consume_token (parser->lexer);
12622 /* And the mem-initializer-list. */
12623 cp_parser_mem_initializer_list (parser);
12625 return true;
12628 /* Parse a mem-initializer-list.
12630 mem-initializer-list:
12631 mem-initializer ... [opt]
12632 mem-initializer ... [opt] , mem-initializer-list */
12634 static void
12635 cp_parser_mem_initializer_list (cp_parser* parser)
12637 tree mem_initializer_list = NULL_TREE;
12638 tree target_ctor = error_mark_node;
12639 cp_token *token = cp_lexer_peek_token (parser->lexer);
12641 /* Let the semantic analysis code know that we are starting the
12642 mem-initializer-list. */
12643 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12644 error_at (token->location,
12645 "only constructors take member initializers");
12647 /* Loop through the list. */
12648 while (true)
12650 tree mem_initializer;
12652 token = cp_lexer_peek_token (parser->lexer);
12653 /* Parse the mem-initializer. */
12654 mem_initializer = cp_parser_mem_initializer (parser);
12655 /* If the next token is a `...', we're expanding member initializers. */
12656 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12658 /* Consume the `...'. */
12659 cp_lexer_consume_token (parser->lexer);
12661 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12662 can be expanded but members cannot. */
12663 if (mem_initializer != error_mark_node
12664 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12666 error_at (token->location,
12667 "cannot expand initializer for member %<%D%>",
12668 TREE_PURPOSE (mem_initializer));
12669 mem_initializer = error_mark_node;
12672 /* Construct the pack expansion type. */
12673 if (mem_initializer != error_mark_node)
12674 mem_initializer = make_pack_expansion (mem_initializer);
12676 if (target_ctor != error_mark_node
12677 && mem_initializer != error_mark_node)
12679 error ("mem-initializer for %qD follows constructor delegation",
12680 TREE_PURPOSE (mem_initializer));
12681 mem_initializer = error_mark_node;
12683 /* Look for a target constructor. */
12684 if (mem_initializer != error_mark_node
12685 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12686 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12688 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12689 if (mem_initializer_list)
12691 error ("constructor delegation follows mem-initializer for %qD",
12692 TREE_PURPOSE (mem_initializer_list));
12693 mem_initializer = error_mark_node;
12695 target_ctor = mem_initializer;
12697 /* Add it to the list, unless it was erroneous. */
12698 if (mem_initializer != error_mark_node)
12700 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12701 mem_initializer_list = mem_initializer;
12703 /* If the next token is not a `,', we're done. */
12704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12705 break;
12706 /* Consume the `,' token. */
12707 cp_lexer_consume_token (parser->lexer);
12710 /* Perform semantic analysis. */
12711 if (DECL_CONSTRUCTOR_P (current_function_decl))
12712 finish_mem_initializers (mem_initializer_list);
12715 /* Parse a mem-initializer.
12717 mem-initializer:
12718 mem-initializer-id ( expression-list [opt] )
12719 mem-initializer-id braced-init-list
12721 GNU extension:
12723 mem-initializer:
12724 ( expression-list [opt] )
12726 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12727 class) or FIELD_DECL (for a non-static data member) to initialize;
12728 the TREE_VALUE is the expression-list. An empty initialization
12729 list is represented by void_list_node. */
12731 static tree
12732 cp_parser_mem_initializer (cp_parser* parser)
12734 tree mem_initializer_id;
12735 tree expression_list;
12736 tree member;
12737 cp_token *token = cp_lexer_peek_token (parser->lexer);
12739 /* Find out what is being initialized. */
12740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12742 permerror (token->location,
12743 "anachronistic old-style base class initializer");
12744 mem_initializer_id = NULL_TREE;
12746 else
12748 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12749 if (mem_initializer_id == error_mark_node)
12750 return mem_initializer_id;
12752 member = expand_member_init (mem_initializer_id);
12753 if (member && !DECL_P (member))
12754 in_base_initializer = 1;
12756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12758 bool expr_non_constant_p;
12759 cp_lexer_set_source_position (parser->lexer);
12760 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12761 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12762 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12763 expression_list = build_tree_list (NULL_TREE, expression_list);
12765 else
12767 vec<tree, va_gc> *vec;
12768 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12769 /*cast_p=*/false,
12770 /*allow_expansion_p=*/true,
12771 /*non_constant_p=*/NULL);
12772 if (vec == NULL)
12773 return error_mark_node;
12774 expression_list = build_tree_list_vec (vec);
12775 release_tree_vector (vec);
12778 if (expression_list == error_mark_node)
12779 return error_mark_node;
12780 if (!expression_list)
12781 expression_list = void_type_node;
12783 in_base_initializer = 0;
12785 return member ? build_tree_list (member, expression_list) : error_mark_node;
12788 /* Parse a mem-initializer-id.
12790 mem-initializer-id:
12791 :: [opt] nested-name-specifier [opt] class-name
12792 identifier
12794 Returns a TYPE indicating the class to be initializer for the first
12795 production. Returns an IDENTIFIER_NODE indicating the data member
12796 to be initialized for the second production. */
12798 static tree
12799 cp_parser_mem_initializer_id (cp_parser* parser)
12801 bool global_scope_p;
12802 bool nested_name_specifier_p;
12803 bool template_p = false;
12804 tree id;
12806 cp_token *token = cp_lexer_peek_token (parser->lexer);
12808 /* `typename' is not allowed in this context ([temp.res]). */
12809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12811 error_at (token->location,
12812 "keyword %<typename%> not allowed in this context (a qualified "
12813 "member initializer is implicitly a type)");
12814 cp_lexer_consume_token (parser->lexer);
12816 /* Look for the optional `::' operator. */
12817 global_scope_p
12818 = (cp_parser_global_scope_opt (parser,
12819 /*current_scope_valid_p=*/false)
12820 != NULL_TREE);
12821 /* Look for the optional nested-name-specifier. The simplest way to
12822 implement:
12824 [temp.res]
12826 The keyword `typename' is not permitted in a base-specifier or
12827 mem-initializer; in these contexts a qualified name that
12828 depends on a template-parameter is implicitly assumed to be a
12829 type name.
12831 is to assume that we have seen the `typename' keyword at this
12832 point. */
12833 nested_name_specifier_p
12834 = (cp_parser_nested_name_specifier_opt (parser,
12835 /*typename_keyword_p=*/true,
12836 /*check_dependency_p=*/true,
12837 /*type_p=*/true,
12838 /*is_declaration=*/true)
12839 != NULL_TREE);
12840 if (nested_name_specifier_p)
12841 template_p = cp_parser_optional_template_keyword (parser);
12842 /* If there is a `::' operator or a nested-name-specifier, then we
12843 are definitely looking for a class-name. */
12844 if (global_scope_p || nested_name_specifier_p)
12845 return cp_parser_class_name (parser,
12846 /*typename_keyword_p=*/true,
12847 /*template_keyword_p=*/template_p,
12848 typename_type,
12849 /*check_dependency_p=*/true,
12850 /*class_head_p=*/false,
12851 /*is_declaration=*/true);
12852 /* Otherwise, we could also be looking for an ordinary identifier. */
12853 cp_parser_parse_tentatively (parser);
12854 /* Try a class-name. */
12855 id = cp_parser_class_name (parser,
12856 /*typename_keyword_p=*/true,
12857 /*template_keyword_p=*/false,
12858 none_type,
12859 /*check_dependency_p=*/true,
12860 /*class_head_p=*/false,
12861 /*is_declaration=*/true);
12862 /* If we found one, we're done. */
12863 if (cp_parser_parse_definitely (parser))
12864 return id;
12865 /* Otherwise, look for an ordinary identifier. */
12866 return cp_parser_identifier (parser);
12869 /* Overloading [gram.over] */
12871 /* Parse an operator-function-id.
12873 operator-function-id:
12874 operator operator
12876 Returns an IDENTIFIER_NODE for the operator which is a
12877 human-readable spelling of the identifier, e.g., `operator +'. */
12879 static tree
12880 cp_parser_operator_function_id (cp_parser* parser)
12882 /* Look for the `operator' keyword. */
12883 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12884 return error_mark_node;
12885 /* And then the name of the operator itself. */
12886 return cp_parser_operator (parser);
12889 /* Return an identifier node for a user-defined literal operator.
12890 The suffix identifier is chained to the operator name identifier. */
12892 static tree
12893 cp_literal_operator_id (const char* name)
12895 tree identifier;
12896 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12897 + strlen (name) + 10);
12898 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12899 identifier = get_identifier (buffer);
12901 return identifier;
12904 /* Parse an operator.
12906 operator:
12907 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12908 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12909 || ++ -- , ->* -> () []
12911 GNU Extensions:
12913 operator:
12914 <? >? <?= >?=
12916 Returns an IDENTIFIER_NODE for the operator which is a
12917 human-readable spelling of the identifier, e.g., `operator +'. */
12919 static tree
12920 cp_parser_operator (cp_parser* parser)
12922 tree id = NULL_TREE;
12923 cp_token *token;
12924 bool utf8 = false;
12926 /* Peek at the next token. */
12927 token = cp_lexer_peek_token (parser->lexer);
12928 /* Figure out which operator we have. */
12929 switch (token->type)
12931 case CPP_KEYWORD:
12933 enum tree_code op;
12935 /* The keyword should be either `new' or `delete'. */
12936 if (token->keyword == RID_NEW)
12937 op = NEW_EXPR;
12938 else if (token->keyword == RID_DELETE)
12939 op = DELETE_EXPR;
12940 else
12941 break;
12943 /* Consume the `new' or `delete' token. */
12944 cp_lexer_consume_token (parser->lexer);
12946 /* Peek at the next token. */
12947 token = cp_lexer_peek_token (parser->lexer);
12948 /* If it's a `[' token then this is the array variant of the
12949 operator. */
12950 if (token->type == CPP_OPEN_SQUARE)
12952 /* Consume the `[' token. */
12953 cp_lexer_consume_token (parser->lexer);
12954 /* Look for the `]' token. */
12955 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12956 id = ansi_opname (op == NEW_EXPR
12957 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12959 /* Otherwise, we have the non-array variant. */
12960 else
12961 id = ansi_opname (op);
12963 return id;
12966 case CPP_PLUS:
12967 id = ansi_opname (PLUS_EXPR);
12968 break;
12970 case CPP_MINUS:
12971 id = ansi_opname (MINUS_EXPR);
12972 break;
12974 case CPP_MULT:
12975 id = ansi_opname (MULT_EXPR);
12976 break;
12978 case CPP_DIV:
12979 id = ansi_opname (TRUNC_DIV_EXPR);
12980 break;
12982 case CPP_MOD:
12983 id = ansi_opname (TRUNC_MOD_EXPR);
12984 break;
12986 case CPP_XOR:
12987 id = ansi_opname (BIT_XOR_EXPR);
12988 break;
12990 case CPP_AND:
12991 id = ansi_opname (BIT_AND_EXPR);
12992 break;
12994 case CPP_OR:
12995 id = ansi_opname (BIT_IOR_EXPR);
12996 break;
12998 case CPP_COMPL:
12999 id = ansi_opname (BIT_NOT_EXPR);
13000 break;
13002 case CPP_NOT:
13003 id = ansi_opname (TRUTH_NOT_EXPR);
13004 break;
13006 case CPP_EQ:
13007 id = ansi_assopname (NOP_EXPR);
13008 break;
13010 case CPP_LESS:
13011 id = ansi_opname (LT_EXPR);
13012 break;
13014 case CPP_GREATER:
13015 id = ansi_opname (GT_EXPR);
13016 break;
13018 case CPP_PLUS_EQ:
13019 id = ansi_assopname (PLUS_EXPR);
13020 break;
13022 case CPP_MINUS_EQ:
13023 id = ansi_assopname (MINUS_EXPR);
13024 break;
13026 case CPP_MULT_EQ:
13027 id = ansi_assopname (MULT_EXPR);
13028 break;
13030 case CPP_DIV_EQ:
13031 id = ansi_assopname (TRUNC_DIV_EXPR);
13032 break;
13034 case CPP_MOD_EQ:
13035 id = ansi_assopname (TRUNC_MOD_EXPR);
13036 break;
13038 case CPP_XOR_EQ:
13039 id = ansi_assopname (BIT_XOR_EXPR);
13040 break;
13042 case CPP_AND_EQ:
13043 id = ansi_assopname (BIT_AND_EXPR);
13044 break;
13046 case CPP_OR_EQ:
13047 id = ansi_assopname (BIT_IOR_EXPR);
13048 break;
13050 case CPP_LSHIFT:
13051 id = ansi_opname (LSHIFT_EXPR);
13052 break;
13054 case CPP_RSHIFT:
13055 id = ansi_opname (RSHIFT_EXPR);
13056 break;
13058 case CPP_LSHIFT_EQ:
13059 id = ansi_assopname (LSHIFT_EXPR);
13060 break;
13062 case CPP_RSHIFT_EQ:
13063 id = ansi_assopname (RSHIFT_EXPR);
13064 break;
13066 case CPP_EQ_EQ:
13067 id = ansi_opname (EQ_EXPR);
13068 break;
13070 case CPP_NOT_EQ:
13071 id = ansi_opname (NE_EXPR);
13072 break;
13074 case CPP_LESS_EQ:
13075 id = ansi_opname (LE_EXPR);
13076 break;
13078 case CPP_GREATER_EQ:
13079 id = ansi_opname (GE_EXPR);
13080 break;
13082 case CPP_AND_AND:
13083 id = ansi_opname (TRUTH_ANDIF_EXPR);
13084 break;
13086 case CPP_OR_OR:
13087 id = ansi_opname (TRUTH_ORIF_EXPR);
13088 break;
13090 case CPP_PLUS_PLUS:
13091 id = ansi_opname (POSTINCREMENT_EXPR);
13092 break;
13094 case CPP_MINUS_MINUS:
13095 id = ansi_opname (PREDECREMENT_EXPR);
13096 break;
13098 case CPP_COMMA:
13099 id = ansi_opname (COMPOUND_EXPR);
13100 break;
13102 case CPP_DEREF_STAR:
13103 id = ansi_opname (MEMBER_REF);
13104 break;
13106 case CPP_DEREF:
13107 id = ansi_opname (COMPONENT_REF);
13108 break;
13110 case CPP_OPEN_PAREN:
13111 /* Consume the `('. */
13112 cp_lexer_consume_token (parser->lexer);
13113 /* Look for the matching `)'. */
13114 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13115 return ansi_opname (CALL_EXPR);
13117 case CPP_OPEN_SQUARE:
13118 /* Consume the `['. */
13119 cp_lexer_consume_token (parser->lexer);
13120 /* Look for the matching `]'. */
13121 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13122 return ansi_opname (ARRAY_REF);
13124 case CPP_UTF8STRING:
13125 case CPP_UTF8STRING_USERDEF:
13126 utf8 = true;
13127 case CPP_STRING:
13128 case CPP_WSTRING:
13129 case CPP_STRING16:
13130 case CPP_STRING32:
13131 case CPP_STRING_USERDEF:
13132 case CPP_WSTRING_USERDEF:
13133 case CPP_STRING16_USERDEF:
13134 case CPP_STRING32_USERDEF:
13136 tree str, string_tree;
13137 int sz, len;
13139 if (cxx_dialect == cxx98)
13140 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13142 /* Consume the string. */
13143 str = cp_parser_string_literal (parser, /*translate=*/true,
13144 /*wide_ok=*/true, /*lookup_udlit=*/false);
13145 if (str == error_mark_node)
13146 return error_mark_node;
13147 else if (TREE_CODE (str) == USERDEF_LITERAL)
13149 string_tree = USERDEF_LITERAL_VALUE (str);
13150 id = USERDEF_LITERAL_SUFFIX_ID (str);
13152 else
13154 string_tree = str;
13155 /* Look for the suffix identifier. */
13156 token = cp_lexer_peek_token (parser->lexer);
13157 if (token->type == CPP_NAME)
13158 id = cp_parser_identifier (parser);
13159 else if (token->type == CPP_KEYWORD)
13161 error ("unexpected keyword;"
13162 " remove space between quotes and suffix identifier");
13163 return error_mark_node;
13165 else
13167 error ("expected suffix identifier");
13168 return error_mark_node;
13171 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13172 (TREE_TYPE (TREE_TYPE (string_tree))));
13173 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13174 if (len != 0)
13176 error ("expected empty string after %<operator%> keyword");
13177 return error_mark_node;
13179 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13180 != char_type_node)
13182 error ("invalid encoding prefix in literal operator");
13183 return error_mark_node;
13185 if (id != error_mark_node)
13187 const char *name = IDENTIFIER_POINTER (id);
13188 id = cp_literal_operator_id (name);
13190 return id;
13193 default:
13194 /* Anything else is an error. */
13195 break;
13198 /* If we have selected an identifier, we need to consume the
13199 operator token. */
13200 if (id)
13201 cp_lexer_consume_token (parser->lexer);
13202 /* Otherwise, no valid operator name was present. */
13203 else
13205 cp_parser_error (parser, "expected operator");
13206 id = error_mark_node;
13209 return id;
13212 /* Parse a template-declaration.
13214 template-declaration:
13215 export [opt] template < template-parameter-list > declaration
13217 If MEMBER_P is TRUE, this template-declaration occurs within a
13218 class-specifier.
13220 The grammar rule given by the standard isn't correct. What
13221 is really meant is:
13223 template-declaration:
13224 export [opt] template-parameter-list-seq
13225 decl-specifier-seq [opt] init-declarator [opt] ;
13226 export [opt] template-parameter-list-seq
13227 function-definition
13229 template-parameter-list-seq:
13230 template-parameter-list-seq [opt]
13231 template < template-parameter-list > */
13233 static void
13234 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13236 /* Check for `export'. */
13237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13239 /* Consume the `export' token. */
13240 cp_lexer_consume_token (parser->lexer);
13241 /* Warn that we do not support `export'. */
13242 warning (0, "keyword %<export%> not implemented, and will be ignored");
13245 cp_parser_template_declaration_after_export (parser, member_p);
13248 /* Parse a template-parameter-list.
13250 template-parameter-list:
13251 template-parameter
13252 template-parameter-list , template-parameter
13254 Returns a TREE_LIST. Each node represents a template parameter.
13255 The nodes are connected via their TREE_CHAINs. */
13257 static tree
13258 cp_parser_template_parameter_list (cp_parser* parser)
13260 tree parameter_list = NULL_TREE;
13262 begin_template_parm_list ();
13264 /* The loop below parses the template parms. We first need to know
13265 the total number of template parms to be able to compute proper
13266 canonical types of each dependent type. So after the loop, when
13267 we know the total number of template parms,
13268 end_template_parm_list computes the proper canonical types and
13269 fixes up the dependent types accordingly. */
13270 while (true)
13272 tree parameter;
13273 bool is_non_type;
13274 bool is_parameter_pack;
13275 location_t parm_loc;
13277 /* Parse the template-parameter. */
13278 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13279 parameter = cp_parser_template_parameter (parser,
13280 &is_non_type,
13281 &is_parameter_pack);
13282 /* Add it to the list. */
13283 if (parameter != error_mark_node)
13284 parameter_list = process_template_parm (parameter_list,
13285 parm_loc,
13286 parameter,
13287 is_non_type,
13288 is_parameter_pack);
13289 else
13291 tree err_parm = build_tree_list (parameter, parameter);
13292 parameter_list = chainon (parameter_list, err_parm);
13295 /* If the next token is not a `,', we're done. */
13296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13297 break;
13298 /* Otherwise, consume the `,' token. */
13299 cp_lexer_consume_token (parser->lexer);
13302 return end_template_parm_list (parameter_list);
13305 /* Parse a template-parameter.
13307 template-parameter:
13308 type-parameter
13309 parameter-declaration
13311 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13312 the parameter. The TREE_PURPOSE is the default value, if any.
13313 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13314 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13315 set to true iff this parameter is a parameter pack. */
13317 static tree
13318 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13319 bool *is_parameter_pack)
13321 cp_token *token;
13322 cp_parameter_declarator *parameter_declarator;
13323 cp_declarator *id_declarator;
13324 tree parm;
13326 /* Assume it is a type parameter or a template parameter. */
13327 *is_non_type = false;
13328 /* Assume it not a parameter pack. */
13329 *is_parameter_pack = false;
13330 /* Peek at the next token. */
13331 token = cp_lexer_peek_token (parser->lexer);
13332 /* If it is `class' or `template', we have a type-parameter. */
13333 if (token->keyword == RID_TEMPLATE)
13334 return cp_parser_type_parameter (parser, is_parameter_pack);
13335 /* If it is `class' or `typename' we do not know yet whether it is a
13336 type parameter or a non-type parameter. Consider:
13338 template <typename T, typename T::X X> ...
13342 template <class C, class D*> ...
13344 Here, the first parameter is a type parameter, and the second is
13345 a non-type parameter. We can tell by looking at the token after
13346 the identifier -- if it is a `,', `=', or `>' then we have a type
13347 parameter. */
13348 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13350 /* Peek at the token after `class' or `typename'. */
13351 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13352 /* If it's an ellipsis, we have a template type parameter
13353 pack. */
13354 if (token->type == CPP_ELLIPSIS)
13355 return cp_parser_type_parameter (parser, is_parameter_pack);
13356 /* If it's an identifier, skip it. */
13357 if (token->type == CPP_NAME)
13358 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13359 /* Now, see if the token looks like the end of a template
13360 parameter. */
13361 if (token->type == CPP_COMMA
13362 || token->type == CPP_EQ
13363 || token->type == CPP_GREATER)
13364 return cp_parser_type_parameter (parser, is_parameter_pack);
13367 /* Otherwise, it is a non-type parameter.
13369 [temp.param]
13371 When parsing a default template-argument for a non-type
13372 template-parameter, the first non-nested `>' is taken as the end
13373 of the template parameter-list rather than a greater-than
13374 operator. */
13375 *is_non_type = true;
13376 parameter_declarator
13377 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13378 /*parenthesized_p=*/NULL);
13380 if (!parameter_declarator)
13381 return error_mark_node;
13383 /* If the parameter declaration is marked as a parameter pack, set
13384 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13385 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13386 grokdeclarator. */
13387 if (parameter_declarator->declarator
13388 && parameter_declarator->declarator->parameter_pack_p)
13390 *is_parameter_pack = true;
13391 parameter_declarator->declarator->parameter_pack_p = false;
13394 if (parameter_declarator->default_argument)
13396 /* Can happen in some cases of erroneous input (c++/34892). */
13397 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13398 /* Consume the `...' for better error recovery. */
13399 cp_lexer_consume_token (parser->lexer);
13401 /* If the next token is an ellipsis, and we don't already have it
13402 marked as a parameter pack, then we have a parameter pack (that
13403 has no declarator). */
13404 else if (!*is_parameter_pack
13405 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13406 && (declarator_can_be_parameter_pack
13407 (parameter_declarator->declarator)))
13409 /* Consume the `...'. */
13410 cp_lexer_consume_token (parser->lexer);
13411 maybe_warn_variadic_templates ();
13413 *is_parameter_pack = true;
13415 /* We might end up with a pack expansion as the type of the non-type
13416 template parameter, in which case this is a non-type template
13417 parameter pack. */
13418 else if (parameter_declarator->decl_specifiers.type
13419 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13421 *is_parameter_pack = true;
13422 parameter_declarator->decl_specifiers.type =
13423 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13426 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13428 /* Parameter packs cannot have default arguments. However, a
13429 user may try to do so, so we'll parse them and give an
13430 appropriate diagnostic here. */
13432 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13434 /* Find the name of the parameter pack. */
13435 id_declarator = parameter_declarator->declarator;
13436 while (id_declarator && id_declarator->kind != cdk_id)
13437 id_declarator = id_declarator->declarator;
13439 if (id_declarator && id_declarator->kind == cdk_id)
13440 error_at (start_token->location,
13441 "template parameter pack %qD cannot have a default argument",
13442 id_declarator->u.id.unqualified_name);
13443 else
13444 error_at (start_token->location,
13445 "template parameter pack cannot have a default argument");
13447 /* Parse the default argument, but throw away the result. */
13448 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13451 parm = grokdeclarator (parameter_declarator->declarator,
13452 &parameter_declarator->decl_specifiers,
13453 TPARM, /*initialized=*/0,
13454 /*attrlist=*/NULL);
13455 if (parm == error_mark_node)
13456 return error_mark_node;
13458 return build_tree_list (parameter_declarator->default_argument, parm);
13461 /* Parse a type-parameter.
13463 type-parameter:
13464 class identifier [opt]
13465 class identifier [opt] = type-id
13466 typename identifier [opt]
13467 typename identifier [opt] = type-id
13468 template < template-parameter-list > class identifier [opt]
13469 template < template-parameter-list > class identifier [opt]
13470 = id-expression
13472 GNU Extension (variadic templates):
13474 type-parameter:
13475 class ... identifier [opt]
13476 typename ... identifier [opt]
13478 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13479 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13480 the declaration of the parameter.
13482 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13484 static tree
13485 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13487 cp_token *token;
13488 tree parameter;
13490 /* Look for a keyword to tell us what kind of parameter this is. */
13491 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13492 if (!token)
13493 return error_mark_node;
13495 switch (token->keyword)
13497 case RID_CLASS:
13498 case RID_TYPENAME:
13500 tree identifier;
13501 tree default_argument;
13503 /* If the next token is an ellipsis, we have a template
13504 argument pack. */
13505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13507 /* Consume the `...' token. */
13508 cp_lexer_consume_token (parser->lexer);
13509 maybe_warn_variadic_templates ();
13511 *is_parameter_pack = true;
13514 /* If the next token is an identifier, then it names the
13515 parameter. */
13516 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13517 identifier = cp_parser_identifier (parser);
13518 else
13519 identifier = NULL_TREE;
13521 /* Create the parameter. */
13522 parameter = finish_template_type_parm (class_type_node, identifier);
13524 /* If the next token is an `=', we have a default argument. */
13525 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13527 /* Consume the `=' token. */
13528 cp_lexer_consume_token (parser->lexer);
13529 /* Parse the default-argument. */
13530 push_deferring_access_checks (dk_no_deferred);
13531 default_argument = cp_parser_type_id (parser);
13533 /* Template parameter packs cannot have default
13534 arguments. */
13535 if (*is_parameter_pack)
13537 if (identifier)
13538 error_at (token->location,
13539 "template parameter pack %qD cannot have a "
13540 "default argument", identifier);
13541 else
13542 error_at (token->location,
13543 "template parameter packs cannot have "
13544 "default arguments");
13545 default_argument = NULL_TREE;
13547 else if (check_for_bare_parameter_packs (default_argument))
13548 default_argument = error_mark_node;
13549 pop_deferring_access_checks ();
13551 else
13552 default_argument = NULL_TREE;
13554 /* Create the combined representation of the parameter and the
13555 default argument. */
13556 parameter = build_tree_list (default_argument, parameter);
13558 break;
13560 case RID_TEMPLATE:
13562 tree identifier;
13563 tree default_argument;
13565 /* Look for the `<'. */
13566 cp_parser_require (parser, CPP_LESS, RT_LESS);
13567 /* Parse the template-parameter-list. */
13568 cp_parser_template_parameter_list (parser);
13569 /* Look for the `>'. */
13570 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13571 /* Look for the `class' or 'typename' keywords. */
13572 cp_parser_type_parameter_key (parser);
13573 /* If the next token is an ellipsis, we have a template
13574 argument pack. */
13575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13577 /* Consume the `...' token. */
13578 cp_lexer_consume_token (parser->lexer);
13579 maybe_warn_variadic_templates ();
13581 *is_parameter_pack = true;
13583 /* If the next token is an `=', then there is a
13584 default-argument. If the next token is a `>', we are at
13585 the end of the parameter-list. If the next token is a `,',
13586 then we are at the end of this parameter. */
13587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13588 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13589 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13591 identifier = cp_parser_identifier (parser);
13592 /* Treat invalid names as if the parameter were nameless. */
13593 if (identifier == error_mark_node)
13594 identifier = NULL_TREE;
13596 else
13597 identifier = NULL_TREE;
13599 /* Create the template parameter. */
13600 parameter = finish_template_template_parm (class_type_node,
13601 identifier);
13603 /* If the next token is an `=', then there is a
13604 default-argument. */
13605 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13607 bool is_template;
13609 /* Consume the `='. */
13610 cp_lexer_consume_token (parser->lexer);
13611 /* Parse the id-expression. */
13612 push_deferring_access_checks (dk_no_deferred);
13613 /* save token before parsing the id-expression, for error
13614 reporting */
13615 token = cp_lexer_peek_token (parser->lexer);
13616 default_argument
13617 = cp_parser_id_expression (parser,
13618 /*template_keyword_p=*/false,
13619 /*check_dependency_p=*/true,
13620 /*template_p=*/&is_template,
13621 /*declarator_p=*/false,
13622 /*optional_p=*/false);
13623 if (TREE_CODE (default_argument) == TYPE_DECL)
13624 /* If the id-expression was a template-id that refers to
13625 a template-class, we already have the declaration here,
13626 so no further lookup is needed. */
13628 else
13629 /* Look up the name. */
13630 default_argument
13631 = cp_parser_lookup_name (parser, default_argument,
13632 none_type,
13633 /*is_template=*/is_template,
13634 /*is_namespace=*/false,
13635 /*check_dependency=*/true,
13636 /*ambiguous_decls=*/NULL,
13637 token->location);
13638 /* See if the default argument is valid. */
13639 default_argument
13640 = check_template_template_default_arg (default_argument);
13642 /* Template parameter packs cannot have default
13643 arguments. */
13644 if (*is_parameter_pack)
13646 if (identifier)
13647 error_at (token->location,
13648 "template parameter pack %qD cannot "
13649 "have a default argument",
13650 identifier);
13651 else
13652 error_at (token->location, "template parameter packs cannot "
13653 "have default arguments");
13654 default_argument = NULL_TREE;
13656 pop_deferring_access_checks ();
13658 else
13659 default_argument = NULL_TREE;
13661 /* Create the combined representation of the parameter and the
13662 default argument. */
13663 parameter = build_tree_list (default_argument, parameter);
13665 break;
13667 default:
13668 gcc_unreachable ();
13669 break;
13672 return parameter;
13675 /* Parse a template-id.
13677 template-id:
13678 template-name < template-argument-list [opt] >
13680 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13681 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13682 returned. Otherwise, if the template-name names a function, or set
13683 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13684 names a class, returns a TYPE_DECL for the specialization.
13686 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13687 uninstantiated templates. */
13689 static tree
13690 cp_parser_template_id (cp_parser *parser,
13691 bool template_keyword_p,
13692 bool check_dependency_p,
13693 enum tag_types tag_type,
13694 bool is_declaration)
13696 int i;
13697 tree templ;
13698 tree arguments;
13699 tree template_id;
13700 cp_token_position start_of_id = 0;
13701 deferred_access_check *chk;
13702 vec<deferred_access_check, va_gc> *access_check;
13703 cp_token *next_token = NULL, *next_token_2 = NULL;
13704 bool is_identifier;
13706 /* If the next token corresponds to a template-id, there is no need
13707 to reparse it. */
13708 next_token = cp_lexer_peek_token (parser->lexer);
13709 if (next_token->type == CPP_TEMPLATE_ID)
13711 struct tree_check *check_value;
13713 /* Get the stored value. */
13714 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13715 /* Perform any access checks that were deferred. */
13716 access_check = check_value->checks;
13717 if (access_check)
13719 FOR_EACH_VEC_ELT (*access_check, i, chk)
13720 perform_or_defer_access_check (chk->binfo,
13721 chk->decl,
13722 chk->diag_decl,
13723 tf_warning_or_error);
13725 /* Return the stored value. */
13726 return check_value->value;
13729 /* Avoid performing name lookup if there is no possibility of
13730 finding a template-id. */
13731 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13732 || (next_token->type == CPP_NAME
13733 && !cp_parser_nth_token_starts_template_argument_list_p
13734 (parser, 2)))
13736 cp_parser_error (parser, "expected template-id");
13737 return error_mark_node;
13740 /* Remember where the template-id starts. */
13741 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13742 start_of_id = cp_lexer_token_position (parser->lexer, false);
13744 push_deferring_access_checks (dk_deferred);
13746 /* Parse the template-name. */
13747 is_identifier = false;
13748 templ = cp_parser_template_name (parser, template_keyword_p,
13749 check_dependency_p,
13750 is_declaration,
13751 tag_type,
13752 &is_identifier);
13753 if (templ == error_mark_node || is_identifier)
13755 pop_deferring_access_checks ();
13756 return templ;
13759 /* If we find the sequence `[:' after a template-name, it's probably
13760 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13761 parse correctly the argument list. */
13762 next_token = cp_lexer_peek_token (parser->lexer);
13763 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13764 if (next_token->type == CPP_OPEN_SQUARE
13765 && next_token->flags & DIGRAPH
13766 && next_token_2->type == CPP_COLON
13767 && !(next_token_2->flags & PREV_WHITE))
13769 cp_parser_parse_tentatively (parser);
13770 /* Change `:' into `::'. */
13771 next_token_2->type = CPP_SCOPE;
13772 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13773 CPP_LESS. */
13774 cp_lexer_consume_token (parser->lexer);
13776 /* Parse the arguments. */
13777 arguments = cp_parser_enclosed_template_argument_list (parser);
13778 if (!cp_parser_parse_definitely (parser))
13780 /* If we couldn't parse an argument list, then we revert our changes
13781 and return simply an error. Maybe this is not a template-id
13782 after all. */
13783 next_token_2->type = CPP_COLON;
13784 cp_parser_error (parser, "expected %<<%>");
13785 pop_deferring_access_checks ();
13786 return error_mark_node;
13788 /* Otherwise, emit an error about the invalid digraph, but continue
13789 parsing because we got our argument list. */
13790 if (permerror (next_token->location,
13791 "%<<::%> cannot begin a template-argument list"))
13793 static bool hint = false;
13794 inform (next_token->location,
13795 "%<<:%> is an alternate spelling for %<[%>."
13796 " Insert whitespace between %<<%> and %<::%>");
13797 if (!hint && !flag_permissive)
13799 inform (next_token->location, "(if you use %<-fpermissive%> "
13800 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13801 "accept your code)");
13802 hint = true;
13806 else
13808 /* Look for the `<' that starts the template-argument-list. */
13809 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13811 pop_deferring_access_checks ();
13812 return error_mark_node;
13814 /* Parse the arguments. */
13815 arguments = cp_parser_enclosed_template_argument_list (parser);
13818 /* Build a representation of the specialization. */
13819 if (identifier_p (templ))
13820 template_id = build_min_nt_loc (next_token->location,
13821 TEMPLATE_ID_EXPR,
13822 templ, arguments);
13823 else if (DECL_TYPE_TEMPLATE_P (templ)
13824 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13826 bool entering_scope;
13827 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13828 template (rather than some instantiation thereof) only if
13829 is not nested within some other construct. For example, in
13830 "template <typename T> void f(T) { A<T>::", A<T> is just an
13831 instantiation of A. */
13832 entering_scope = (template_parm_scope_p ()
13833 && cp_lexer_next_token_is (parser->lexer,
13834 CPP_SCOPE));
13835 template_id
13836 = finish_template_type (templ, arguments, entering_scope);
13838 else if (variable_template_p (templ))
13840 template_id = lookup_template_variable (templ, arguments);
13842 else
13844 /* If it's not a class-template or a template-template, it should be
13845 a function-template. */
13846 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13847 || TREE_CODE (templ) == OVERLOAD
13848 || BASELINK_P (templ)));
13850 template_id = lookup_template_function (templ, arguments);
13853 /* If parsing tentatively, replace the sequence of tokens that makes
13854 up the template-id with a CPP_TEMPLATE_ID token. That way,
13855 should we re-parse the token stream, we will not have to repeat
13856 the effort required to do the parse, nor will we issue duplicate
13857 error messages about problems during instantiation of the
13858 template. */
13859 if (start_of_id
13860 /* Don't do this if we had a parse error in a declarator; re-parsing
13861 might succeed if a name changes meaning (60361). */
13862 && !(cp_parser_error_occurred (parser)
13863 && cp_parser_parsing_tentatively (parser)
13864 && parser->in_declarator_p))
13866 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13868 /* Reset the contents of the START_OF_ID token. */
13869 token->type = CPP_TEMPLATE_ID;
13870 /* Retrieve any deferred checks. Do not pop this access checks yet
13871 so the memory will not be reclaimed during token replacing below. */
13872 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13873 token->u.tree_check_value->value = template_id;
13874 token->u.tree_check_value->checks = get_deferred_access_checks ();
13875 token->keyword = RID_MAX;
13877 /* Purge all subsequent tokens. */
13878 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13880 /* ??? Can we actually assume that, if template_id ==
13881 error_mark_node, we will have issued a diagnostic to the
13882 user, as opposed to simply marking the tentative parse as
13883 failed? */
13884 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13885 error_at (token->location, "parse error in template argument list");
13888 pop_to_parent_deferring_access_checks ();
13889 return template_id;
13892 /* Parse a template-name.
13894 template-name:
13895 identifier
13897 The standard should actually say:
13899 template-name:
13900 identifier
13901 operator-function-id
13903 A defect report has been filed about this issue.
13905 A conversion-function-id cannot be a template name because they cannot
13906 be part of a template-id. In fact, looking at this code:
13908 a.operator K<int>()
13910 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13911 It is impossible to call a templated conversion-function-id with an
13912 explicit argument list, since the only allowed template parameter is
13913 the type to which it is converting.
13915 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13916 `template' keyword, in a construction like:
13918 T::template f<3>()
13920 In that case `f' is taken to be a template-name, even though there
13921 is no way of knowing for sure.
13923 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13924 name refers to a set of overloaded functions, at least one of which
13925 is a template, or an IDENTIFIER_NODE with the name of the template,
13926 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13927 names are looked up inside uninstantiated templates. */
13929 static tree
13930 cp_parser_template_name (cp_parser* parser,
13931 bool template_keyword_p,
13932 bool check_dependency_p,
13933 bool is_declaration,
13934 enum tag_types tag_type,
13935 bool *is_identifier)
13937 tree identifier;
13938 tree decl;
13939 tree fns;
13940 cp_token *token = cp_lexer_peek_token (parser->lexer);
13942 /* If the next token is `operator', then we have either an
13943 operator-function-id or a conversion-function-id. */
13944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13946 /* We don't know whether we're looking at an
13947 operator-function-id or a conversion-function-id. */
13948 cp_parser_parse_tentatively (parser);
13949 /* Try an operator-function-id. */
13950 identifier = cp_parser_operator_function_id (parser);
13951 /* If that didn't work, try a conversion-function-id. */
13952 if (!cp_parser_parse_definitely (parser))
13954 cp_parser_error (parser, "expected template-name");
13955 return error_mark_node;
13958 /* Look for the identifier. */
13959 else
13960 identifier = cp_parser_identifier (parser);
13962 /* If we didn't find an identifier, we don't have a template-id. */
13963 if (identifier == error_mark_node)
13964 return error_mark_node;
13966 /* If the name immediately followed the `template' keyword, then it
13967 is a template-name. However, if the next token is not `<', then
13968 we do not treat it as a template-name, since it is not being used
13969 as part of a template-id. This enables us to handle constructs
13970 like:
13972 template <typename T> struct S { S(); };
13973 template <typename T> S<T>::S();
13975 correctly. We would treat `S' as a template -- if it were `S<T>'
13976 -- but we do not if there is no `<'. */
13978 if (processing_template_decl
13979 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13981 /* In a declaration, in a dependent context, we pretend that the
13982 "template" keyword was present in order to improve error
13983 recovery. For example, given:
13985 template <typename T> void f(T::X<int>);
13987 we want to treat "X<int>" as a template-id. */
13988 if (is_declaration
13989 && !template_keyword_p
13990 && parser->scope && TYPE_P (parser->scope)
13991 && check_dependency_p
13992 && dependent_scope_p (parser->scope)
13993 /* Do not do this for dtors (or ctors), since they never
13994 need the template keyword before their name. */
13995 && !constructor_name_p (identifier, parser->scope))
13997 cp_token_position start = 0;
13999 /* Explain what went wrong. */
14000 error_at (token->location, "non-template %qD used as template",
14001 identifier);
14002 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14003 parser->scope, identifier);
14004 /* If parsing tentatively, find the location of the "<" token. */
14005 if (cp_parser_simulate_error (parser))
14006 start = cp_lexer_token_position (parser->lexer, true);
14007 /* Parse the template arguments so that we can issue error
14008 messages about them. */
14009 cp_lexer_consume_token (parser->lexer);
14010 cp_parser_enclosed_template_argument_list (parser);
14011 /* Skip tokens until we find a good place from which to
14012 continue parsing. */
14013 cp_parser_skip_to_closing_parenthesis (parser,
14014 /*recovering=*/true,
14015 /*or_comma=*/true,
14016 /*consume_paren=*/false);
14017 /* If parsing tentatively, permanently remove the
14018 template argument list. That will prevent duplicate
14019 error messages from being issued about the missing
14020 "template" keyword. */
14021 if (start)
14022 cp_lexer_purge_tokens_after (parser->lexer, start);
14023 if (is_identifier)
14024 *is_identifier = true;
14025 return identifier;
14028 /* If the "template" keyword is present, then there is generally
14029 no point in doing name-lookup, so we just return IDENTIFIER.
14030 But, if the qualifying scope is non-dependent then we can
14031 (and must) do name-lookup normally. */
14032 if (template_keyword_p
14033 && (!parser->scope
14034 || (TYPE_P (parser->scope)
14035 && dependent_type_p (parser->scope))))
14036 return identifier;
14039 /* Look up the name. */
14040 decl = cp_parser_lookup_name (parser, identifier,
14041 tag_type,
14042 /*is_template=*/true,
14043 /*is_namespace=*/false,
14044 check_dependency_p,
14045 /*ambiguous_decls=*/NULL,
14046 token->location);
14048 /* If DECL is a template, then the name was a template-name. */
14049 if (TREE_CODE (decl) == TEMPLATE_DECL)
14051 if (TREE_DEPRECATED (decl)
14052 && deprecated_state != DEPRECATED_SUPPRESS)
14053 warn_deprecated_use (decl, NULL_TREE);
14055 else
14057 tree fn = NULL_TREE;
14059 /* The standard does not explicitly indicate whether a name that
14060 names a set of overloaded declarations, some of which are
14061 templates, is a template-name. However, such a name should
14062 be a template-name; otherwise, there is no way to form a
14063 template-id for the overloaded templates. */
14064 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14065 if (TREE_CODE (fns) == OVERLOAD)
14066 for (fn = fns; fn; fn = OVL_NEXT (fn))
14067 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14068 break;
14070 if (!fn)
14072 /* The name does not name a template. */
14073 cp_parser_error (parser, "expected template-name");
14074 return error_mark_node;
14078 /* If DECL is dependent, and refers to a function, then just return
14079 its name; we will look it up again during template instantiation. */
14080 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14082 tree scope = ovl_scope (decl);
14083 if (TYPE_P (scope) && dependent_type_p (scope))
14084 return identifier;
14087 return decl;
14090 /* Parse a template-argument-list.
14092 template-argument-list:
14093 template-argument ... [opt]
14094 template-argument-list , template-argument ... [opt]
14096 Returns a TREE_VEC containing the arguments. */
14098 static tree
14099 cp_parser_template_argument_list (cp_parser* parser)
14101 tree fixed_args[10];
14102 unsigned n_args = 0;
14103 unsigned alloced = 10;
14104 tree *arg_ary = fixed_args;
14105 tree vec;
14106 bool saved_in_template_argument_list_p;
14107 bool saved_ice_p;
14108 bool saved_non_ice_p;
14110 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14111 parser->in_template_argument_list_p = true;
14112 /* Even if the template-id appears in an integral
14113 constant-expression, the contents of the argument list do
14114 not. */
14115 saved_ice_p = parser->integral_constant_expression_p;
14116 parser->integral_constant_expression_p = false;
14117 saved_non_ice_p = parser->non_integral_constant_expression_p;
14118 parser->non_integral_constant_expression_p = false;
14120 /* Parse the arguments. */
14123 tree argument;
14125 if (n_args)
14126 /* Consume the comma. */
14127 cp_lexer_consume_token (parser->lexer);
14129 /* Parse the template-argument. */
14130 argument = cp_parser_template_argument (parser);
14132 /* If the next token is an ellipsis, we're expanding a template
14133 argument pack. */
14134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14136 if (argument == error_mark_node)
14138 cp_token *token = cp_lexer_peek_token (parser->lexer);
14139 error_at (token->location,
14140 "expected parameter pack before %<...%>");
14142 /* Consume the `...' token. */
14143 cp_lexer_consume_token (parser->lexer);
14145 /* Make the argument into a TYPE_PACK_EXPANSION or
14146 EXPR_PACK_EXPANSION. */
14147 argument = make_pack_expansion (argument);
14150 if (n_args == alloced)
14152 alloced *= 2;
14154 if (arg_ary == fixed_args)
14156 arg_ary = XNEWVEC (tree, alloced);
14157 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14159 else
14160 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14162 arg_ary[n_args++] = argument;
14164 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14166 vec = make_tree_vec (n_args);
14168 while (n_args--)
14169 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14171 if (arg_ary != fixed_args)
14172 free (arg_ary);
14173 parser->non_integral_constant_expression_p = saved_non_ice_p;
14174 parser->integral_constant_expression_p = saved_ice_p;
14175 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14176 #ifdef ENABLE_CHECKING
14177 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14178 #endif
14179 return vec;
14182 /* Parse a template-argument.
14184 template-argument:
14185 assignment-expression
14186 type-id
14187 id-expression
14189 The representation is that of an assignment-expression, type-id, or
14190 id-expression -- except that the qualified id-expression is
14191 evaluated, so that the value returned is either a DECL or an
14192 OVERLOAD.
14194 Although the standard says "assignment-expression", it forbids
14195 throw-expressions or assignments in the template argument.
14196 Therefore, we use "conditional-expression" instead. */
14198 static tree
14199 cp_parser_template_argument (cp_parser* parser)
14201 tree argument;
14202 bool template_p;
14203 bool address_p;
14204 bool maybe_type_id = false;
14205 cp_token *token = NULL, *argument_start_token = NULL;
14206 location_t loc = 0;
14207 cp_id_kind idk;
14209 /* There's really no way to know what we're looking at, so we just
14210 try each alternative in order.
14212 [temp.arg]
14214 In a template-argument, an ambiguity between a type-id and an
14215 expression is resolved to a type-id, regardless of the form of
14216 the corresponding template-parameter.
14218 Therefore, we try a type-id first. */
14219 cp_parser_parse_tentatively (parser);
14220 argument = cp_parser_template_type_arg (parser);
14221 /* If there was no error parsing the type-id but the next token is a
14222 '>>', our behavior depends on which dialect of C++ we're
14223 parsing. In C++98, we probably found a typo for '> >'. But there
14224 are type-id which are also valid expressions. For instance:
14226 struct X { int operator >> (int); };
14227 template <int V> struct Foo {};
14228 Foo<X () >> 5> r;
14230 Here 'X()' is a valid type-id of a function type, but the user just
14231 wanted to write the expression "X() >> 5". Thus, we remember that we
14232 found a valid type-id, but we still try to parse the argument as an
14233 expression to see what happens.
14235 In C++0x, the '>>' will be considered two separate '>'
14236 tokens. */
14237 if (!cp_parser_error_occurred (parser)
14238 && cxx_dialect == cxx98
14239 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14241 maybe_type_id = true;
14242 cp_parser_abort_tentative_parse (parser);
14244 else
14246 /* If the next token isn't a `,' or a `>', then this argument wasn't
14247 really finished. This means that the argument is not a valid
14248 type-id. */
14249 if (!cp_parser_next_token_ends_template_argument_p (parser))
14250 cp_parser_error (parser, "expected template-argument");
14251 /* If that worked, we're done. */
14252 if (cp_parser_parse_definitely (parser))
14253 return argument;
14255 /* We're still not sure what the argument will be. */
14256 cp_parser_parse_tentatively (parser);
14257 /* Try a template. */
14258 argument_start_token = cp_lexer_peek_token (parser->lexer);
14259 argument = cp_parser_id_expression (parser,
14260 /*template_keyword_p=*/false,
14261 /*check_dependency_p=*/true,
14262 &template_p,
14263 /*declarator_p=*/false,
14264 /*optional_p=*/false);
14265 /* If the next token isn't a `,' or a `>', then this argument wasn't
14266 really finished. */
14267 if (!cp_parser_next_token_ends_template_argument_p (parser))
14268 cp_parser_error (parser, "expected template-argument");
14269 if (!cp_parser_error_occurred (parser))
14271 /* Figure out what is being referred to. If the id-expression
14272 was for a class template specialization, then we will have a
14273 TYPE_DECL at this point. There is no need to do name lookup
14274 at this point in that case. */
14275 if (TREE_CODE (argument) != TYPE_DECL)
14276 argument = cp_parser_lookup_name (parser, argument,
14277 none_type,
14278 /*is_template=*/template_p,
14279 /*is_namespace=*/false,
14280 /*check_dependency=*/true,
14281 /*ambiguous_decls=*/NULL,
14282 argument_start_token->location);
14283 if (TREE_CODE (argument) != TEMPLATE_DECL
14284 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14285 cp_parser_error (parser, "expected template-name");
14287 if (cp_parser_parse_definitely (parser))
14289 if (TREE_DEPRECATED (argument))
14290 warn_deprecated_use (argument, NULL_TREE);
14291 return argument;
14293 /* It must be a non-type argument. There permitted cases are given
14294 in [temp.arg.nontype]:
14296 -- an integral constant-expression of integral or enumeration
14297 type; or
14299 -- the name of a non-type template-parameter; or
14301 -- the name of an object or function with external linkage...
14303 -- the address of an object or function with external linkage...
14305 -- a pointer to member... */
14306 /* Look for a non-type template parameter. */
14307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14309 cp_parser_parse_tentatively (parser);
14310 argument = cp_parser_primary_expression (parser,
14311 /*address_p=*/false,
14312 /*cast_p=*/false,
14313 /*template_arg_p=*/true,
14314 &idk);
14315 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14316 || !cp_parser_next_token_ends_template_argument_p (parser))
14317 cp_parser_simulate_error (parser);
14318 if (cp_parser_parse_definitely (parser))
14319 return argument;
14322 /* If the next token is "&", the argument must be the address of an
14323 object or function with external linkage. */
14324 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14325 if (address_p)
14327 loc = cp_lexer_peek_token (parser->lexer)->location;
14328 cp_lexer_consume_token (parser->lexer);
14330 /* See if we might have an id-expression. */
14331 token = cp_lexer_peek_token (parser->lexer);
14332 if (token->type == CPP_NAME
14333 || token->keyword == RID_OPERATOR
14334 || token->type == CPP_SCOPE
14335 || token->type == CPP_TEMPLATE_ID
14336 || token->type == CPP_NESTED_NAME_SPECIFIER)
14338 cp_parser_parse_tentatively (parser);
14339 argument = cp_parser_primary_expression (parser,
14340 address_p,
14341 /*cast_p=*/false,
14342 /*template_arg_p=*/true,
14343 &idk);
14344 if (cp_parser_error_occurred (parser)
14345 || !cp_parser_next_token_ends_template_argument_p (parser))
14346 cp_parser_abort_tentative_parse (parser);
14347 else
14349 tree probe;
14351 if (INDIRECT_REF_P (argument))
14353 /* Strip the dereference temporarily. */
14354 gcc_assert (REFERENCE_REF_P (argument));
14355 argument = TREE_OPERAND (argument, 0);
14358 /* If we're in a template, we represent a qualified-id referring
14359 to a static data member as a SCOPE_REF even if the scope isn't
14360 dependent so that we can check access control later. */
14361 probe = argument;
14362 if (TREE_CODE (probe) == SCOPE_REF)
14363 probe = TREE_OPERAND (probe, 1);
14364 if (VAR_P (probe))
14366 /* A variable without external linkage might still be a
14367 valid constant-expression, so no error is issued here
14368 if the external-linkage check fails. */
14369 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14370 cp_parser_simulate_error (parser);
14372 else if (is_overloaded_fn (argument))
14373 /* All overloaded functions are allowed; if the external
14374 linkage test does not pass, an error will be issued
14375 later. */
14377 else if (address_p
14378 && (TREE_CODE (argument) == OFFSET_REF
14379 || TREE_CODE (argument) == SCOPE_REF))
14380 /* A pointer-to-member. */
14382 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14384 else
14385 cp_parser_simulate_error (parser);
14387 if (cp_parser_parse_definitely (parser))
14389 if (address_p)
14390 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14391 tf_warning_or_error);
14392 else
14393 argument = convert_from_reference (argument);
14394 return argument;
14398 /* If the argument started with "&", there are no other valid
14399 alternatives at this point. */
14400 if (address_p)
14402 cp_parser_error (parser, "invalid non-type template argument");
14403 return error_mark_node;
14406 /* If the argument wasn't successfully parsed as a type-id followed
14407 by '>>', the argument can only be a constant expression now.
14408 Otherwise, we try parsing the constant-expression tentatively,
14409 because the argument could really be a type-id. */
14410 if (maybe_type_id)
14411 cp_parser_parse_tentatively (parser);
14412 argument = cp_parser_constant_expression (parser);
14414 if (!maybe_type_id)
14415 return argument;
14416 if (!cp_parser_next_token_ends_template_argument_p (parser))
14417 cp_parser_error (parser, "expected template-argument");
14418 if (cp_parser_parse_definitely (parser))
14419 return argument;
14420 /* We did our best to parse the argument as a non type-id, but that
14421 was the only alternative that matched (albeit with a '>' after
14422 it). We can assume it's just a typo from the user, and a
14423 diagnostic will then be issued. */
14424 return cp_parser_template_type_arg (parser);
14427 /* Parse an explicit-instantiation.
14429 explicit-instantiation:
14430 template declaration
14432 Although the standard says `declaration', what it really means is:
14434 explicit-instantiation:
14435 template decl-specifier-seq [opt] declarator [opt] ;
14437 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14438 supposed to be allowed. A defect report has been filed about this
14439 issue.
14441 GNU Extension:
14443 explicit-instantiation:
14444 storage-class-specifier template
14445 decl-specifier-seq [opt] declarator [opt] ;
14446 function-specifier template
14447 decl-specifier-seq [opt] declarator [opt] ; */
14449 static void
14450 cp_parser_explicit_instantiation (cp_parser* parser)
14452 int declares_class_or_enum;
14453 cp_decl_specifier_seq decl_specifiers;
14454 tree extension_specifier = NULL_TREE;
14456 timevar_push (TV_TEMPLATE_INST);
14458 /* Look for an (optional) storage-class-specifier or
14459 function-specifier. */
14460 if (cp_parser_allow_gnu_extensions_p (parser))
14462 extension_specifier
14463 = cp_parser_storage_class_specifier_opt (parser);
14464 if (!extension_specifier)
14465 extension_specifier
14466 = cp_parser_function_specifier_opt (parser,
14467 /*decl_specs=*/NULL);
14470 /* Look for the `template' keyword. */
14471 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14472 /* Let the front end know that we are processing an explicit
14473 instantiation. */
14474 begin_explicit_instantiation ();
14475 /* [temp.explicit] says that we are supposed to ignore access
14476 control while processing explicit instantiation directives. */
14477 push_deferring_access_checks (dk_no_check);
14478 /* Parse a decl-specifier-seq. */
14479 cp_parser_decl_specifier_seq (parser,
14480 CP_PARSER_FLAGS_OPTIONAL,
14481 &decl_specifiers,
14482 &declares_class_or_enum);
14483 /* If there was exactly one decl-specifier, and it declared a class,
14484 and there's no declarator, then we have an explicit type
14485 instantiation. */
14486 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14488 tree type;
14490 type = check_tag_decl (&decl_specifiers,
14491 /*explicit_type_instantiation_p=*/true);
14492 /* Turn access control back on for names used during
14493 template instantiation. */
14494 pop_deferring_access_checks ();
14495 if (type)
14496 do_type_instantiation (type, extension_specifier,
14497 /*complain=*/tf_error);
14499 else
14501 cp_declarator *declarator;
14502 tree decl;
14504 /* Parse the declarator. */
14505 declarator
14506 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14507 /*ctor_dtor_or_conv_p=*/NULL,
14508 /*parenthesized_p=*/NULL,
14509 /*member_p=*/false,
14510 /*friend_p=*/false);
14511 if (declares_class_or_enum & 2)
14512 cp_parser_check_for_definition_in_return_type (declarator,
14513 decl_specifiers.type,
14514 decl_specifiers.locations[ds_type_spec]);
14515 if (declarator != cp_error_declarator)
14517 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14518 permerror (decl_specifiers.locations[ds_inline],
14519 "explicit instantiation shall not use"
14520 " %<inline%> specifier");
14521 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14522 permerror (decl_specifiers.locations[ds_constexpr],
14523 "explicit instantiation shall not use"
14524 " %<constexpr%> specifier");
14526 decl = grokdeclarator (declarator, &decl_specifiers,
14527 NORMAL, 0, &decl_specifiers.attributes);
14528 /* Turn access control back on for names used during
14529 template instantiation. */
14530 pop_deferring_access_checks ();
14531 /* Do the explicit instantiation. */
14532 do_decl_instantiation (decl, extension_specifier);
14534 else
14536 pop_deferring_access_checks ();
14537 /* Skip the body of the explicit instantiation. */
14538 cp_parser_skip_to_end_of_statement (parser);
14541 /* We're done with the instantiation. */
14542 end_explicit_instantiation ();
14544 cp_parser_consume_semicolon_at_end_of_statement (parser);
14546 timevar_pop (TV_TEMPLATE_INST);
14549 /* Parse an explicit-specialization.
14551 explicit-specialization:
14552 template < > declaration
14554 Although the standard says `declaration', what it really means is:
14556 explicit-specialization:
14557 template <> decl-specifier [opt] init-declarator [opt] ;
14558 template <> function-definition
14559 template <> explicit-specialization
14560 template <> template-declaration */
14562 static void
14563 cp_parser_explicit_specialization (cp_parser* parser)
14565 bool need_lang_pop;
14566 cp_token *token = cp_lexer_peek_token (parser->lexer);
14568 /* Look for the `template' keyword. */
14569 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14570 /* Look for the `<'. */
14571 cp_parser_require (parser, CPP_LESS, RT_LESS);
14572 /* Look for the `>'. */
14573 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14574 /* We have processed another parameter list. */
14575 ++parser->num_template_parameter_lists;
14576 /* [temp]
14578 A template ... explicit specialization ... shall not have C
14579 linkage. */
14580 if (current_lang_name == lang_name_c)
14582 error_at (token->location, "template specialization with C linkage");
14583 /* Give it C++ linkage to avoid confusing other parts of the
14584 front end. */
14585 push_lang_context (lang_name_cplusplus);
14586 need_lang_pop = true;
14588 else
14589 need_lang_pop = false;
14590 /* Let the front end know that we are beginning a specialization. */
14591 if (!begin_specialization ())
14593 end_specialization ();
14594 return;
14597 /* If the next keyword is `template', we need to figure out whether
14598 or not we're looking a template-declaration. */
14599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14601 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14602 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14603 cp_parser_template_declaration_after_export (parser,
14604 /*member_p=*/false);
14605 else
14606 cp_parser_explicit_specialization (parser);
14608 else
14609 /* Parse the dependent declaration. */
14610 cp_parser_single_declaration (parser,
14611 /*checks=*/NULL,
14612 /*member_p=*/false,
14613 /*explicit_specialization_p=*/true,
14614 /*friend_p=*/NULL);
14615 /* We're done with the specialization. */
14616 end_specialization ();
14617 /* For the erroneous case of a template with C linkage, we pushed an
14618 implicit C++ linkage scope; exit that scope now. */
14619 if (need_lang_pop)
14620 pop_lang_context ();
14621 /* We're done with this parameter list. */
14622 --parser->num_template_parameter_lists;
14625 /* Parse a type-specifier.
14627 type-specifier:
14628 simple-type-specifier
14629 class-specifier
14630 enum-specifier
14631 elaborated-type-specifier
14632 cv-qualifier
14634 GNU Extension:
14636 type-specifier:
14637 __complex__
14639 Returns a representation of the type-specifier. For a
14640 class-specifier, enum-specifier, or elaborated-type-specifier, a
14641 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14643 The parser flags FLAGS is used to control type-specifier parsing.
14645 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14646 in a decl-specifier-seq.
14648 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14649 class-specifier, enum-specifier, or elaborated-type-specifier, then
14650 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14651 if a type is declared; 2 if it is defined. Otherwise, it is set to
14652 zero.
14654 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14655 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14656 is set to FALSE. */
14658 static tree
14659 cp_parser_type_specifier (cp_parser* parser,
14660 cp_parser_flags flags,
14661 cp_decl_specifier_seq *decl_specs,
14662 bool is_declaration,
14663 int* declares_class_or_enum,
14664 bool* is_cv_qualifier)
14666 tree type_spec = NULL_TREE;
14667 cp_token *token;
14668 enum rid keyword;
14669 cp_decl_spec ds = ds_last;
14671 /* Assume this type-specifier does not declare a new type. */
14672 if (declares_class_or_enum)
14673 *declares_class_or_enum = 0;
14674 /* And that it does not specify a cv-qualifier. */
14675 if (is_cv_qualifier)
14676 *is_cv_qualifier = false;
14677 /* Peek at the next token. */
14678 token = cp_lexer_peek_token (parser->lexer);
14680 /* If we're looking at a keyword, we can use that to guide the
14681 production we choose. */
14682 keyword = token->keyword;
14683 switch (keyword)
14685 case RID_ENUM:
14686 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14687 goto elaborated_type_specifier;
14689 /* Look for the enum-specifier. */
14690 type_spec = cp_parser_enum_specifier (parser);
14691 /* If that worked, we're done. */
14692 if (type_spec)
14694 if (declares_class_or_enum)
14695 *declares_class_or_enum = 2;
14696 if (decl_specs)
14697 cp_parser_set_decl_spec_type (decl_specs,
14698 type_spec,
14699 token,
14700 /*type_definition_p=*/true);
14701 return type_spec;
14703 else
14704 goto elaborated_type_specifier;
14706 /* Any of these indicate either a class-specifier, or an
14707 elaborated-type-specifier. */
14708 case RID_CLASS:
14709 case RID_STRUCT:
14710 case RID_UNION:
14711 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14712 goto elaborated_type_specifier;
14714 /* Parse tentatively so that we can back up if we don't find a
14715 class-specifier. */
14716 cp_parser_parse_tentatively (parser);
14717 /* Look for the class-specifier. */
14718 type_spec = cp_parser_class_specifier (parser);
14719 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14720 /* If that worked, we're done. */
14721 if (cp_parser_parse_definitely (parser))
14723 if (declares_class_or_enum)
14724 *declares_class_or_enum = 2;
14725 if (decl_specs)
14726 cp_parser_set_decl_spec_type (decl_specs,
14727 type_spec,
14728 token,
14729 /*type_definition_p=*/true);
14730 return type_spec;
14733 /* Fall through. */
14734 elaborated_type_specifier:
14735 /* We're declaring (not defining) a class or enum. */
14736 if (declares_class_or_enum)
14737 *declares_class_or_enum = 1;
14739 /* Fall through. */
14740 case RID_TYPENAME:
14741 /* Look for an elaborated-type-specifier. */
14742 type_spec
14743 = (cp_parser_elaborated_type_specifier
14744 (parser,
14745 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14746 is_declaration));
14747 if (decl_specs)
14748 cp_parser_set_decl_spec_type (decl_specs,
14749 type_spec,
14750 token,
14751 /*type_definition_p=*/false);
14752 return type_spec;
14754 case RID_CONST:
14755 ds = ds_const;
14756 if (is_cv_qualifier)
14757 *is_cv_qualifier = true;
14758 break;
14760 case RID_VOLATILE:
14761 ds = ds_volatile;
14762 if (is_cv_qualifier)
14763 *is_cv_qualifier = true;
14764 break;
14766 case RID_RESTRICT:
14767 ds = ds_restrict;
14768 if (is_cv_qualifier)
14769 *is_cv_qualifier = true;
14770 break;
14772 case RID_COMPLEX:
14773 /* The `__complex__' keyword is a GNU extension. */
14774 ds = ds_complex;
14775 break;
14777 default:
14778 break;
14781 /* Handle simple keywords. */
14782 if (ds != ds_last)
14784 if (decl_specs)
14786 set_and_check_decl_spec_loc (decl_specs, ds, token);
14787 decl_specs->any_specifiers_p = true;
14789 return cp_lexer_consume_token (parser->lexer)->u.value;
14792 /* If we do not already have a type-specifier, assume we are looking
14793 at a simple-type-specifier. */
14794 type_spec = cp_parser_simple_type_specifier (parser,
14795 decl_specs,
14796 flags);
14798 /* If we didn't find a type-specifier, and a type-specifier was not
14799 optional in this context, issue an error message. */
14800 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14802 cp_parser_error (parser, "expected type specifier");
14803 return error_mark_node;
14806 return type_spec;
14809 /* Parse a simple-type-specifier.
14811 simple-type-specifier:
14812 :: [opt] nested-name-specifier [opt] type-name
14813 :: [opt] nested-name-specifier template template-id
14814 char
14815 wchar_t
14816 bool
14817 short
14819 long
14820 signed
14821 unsigned
14822 float
14823 double
14824 void
14826 C++0x Extension:
14828 simple-type-specifier:
14829 auto
14830 decltype ( expression )
14831 char16_t
14832 char32_t
14833 __underlying_type ( type-id )
14835 GNU Extension:
14837 simple-type-specifier:
14838 __int128
14839 __typeof__ unary-expression
14840 __typeof__ ( type-id )
14841 __typeof__ ( type-id ) { initializer-list , [opt] }
14843 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14844 appropriately updated. */
14846 static tree
14847 cp_parser_simple_type_specifier (cp_parser* parser,
14848 cp_decl_specifier_seq *decl_specs,
14849 cp_parser_flags flags)
14851 tree type = NULL_TREE;
14852 cp_token *token;
14853 int idx;
14855 /* Peek at the next token. */
14856 token = cp_lexer_peek_token (parser->lexer);
14858 /* If we're looking at a keyword, things are easy. */
14859 switch (token->keyword)
14861 case RID_CHAR:
14862 if (decl_specs)
14863 decl_specs->explicit_char_p = true;
14864 type = char_type_node;
14865 break;
14866 case RID_CHAR16:
14867 type = char16_type_node;
14868 break;
14869 case RID_CHAR32:
14870 type = char32_type_node;
14871 break;
14872 case RID_WCHAR:
14873 type = wchar_type_node;
14874 break;
14875 case RID_BOOL:
14876 type = boolean_type_node;
14877 break;
14878 case RID_SHORT:
14879 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14880 type = short_integer_type_node;
14881 break;
14882 case RID_INT:
14883 if (decl_specs)
14884 decl_specs->explicit_int_p = true;
14885 type = integer_type_node;
14886 break;
14887 case RID_INT_N_0:
14888 case RID_INT_N_1:
14889 case RID_INT_N_2:
14890 case RID_INT_N_3:
14891 idx = token->keyword - RID_INT_N_0;
14892 if (! int_n_enabled_p [idx])
14893 break;
14894 if (decl_specs)
14896 decl_specs->explicit_intN_p = true;
14897 decl_specs->int_n_idx = idx;
14899 type = int_n_trees [idx].signed_type;
14900 break;
14901 case RID_LONG:
14902 if (decl_specs)
14903 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14904 type = long_integer_type_node;
14905 break;
14906 case RID_SIGNED:
14907 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14908 type = integer_type_node;
14909 break;
14910 case RID_UNSIGNED:
14911 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14912 type = unsigned_type_node;
14913 break;
14914 case RID_FLOAT:
14915 type = float_type_node;
14916 break;
14917 case RID_DOUBLE:
14918 type = double_type_node;
14919 break;
14920 case RID_VOID:
14921 type = void_type_node;
14922 break;
14924 case RID_AUTO:
14925 maybe_warn_cpp0x (CPP0X_AUTO);
14926 if (parser->auto_is_implicit_function_template_parm_p)
14928 if (cxx_dialect >= cxx14)
14929 type = synthesize_implicit_template_parm (parser);
14930 else
14931 type = error_mark_node;
14933 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14935 if (cxx_dialect < cxx14)
14936 error_at (token->location,
14937 "use of %<auto%> in lambda parameter declaration "
14938 "only available with "
14939 "-std=c++14 or -std=gnu++14");
14941 else if (cxx_dialect < cxx14)
14942 error_at (token->location,
14943 "use of %<auto%> in parameter declaration "
14944 "only available with "
14945 "-std=c++14 or -std=gnu++14");
14946 else
14947 pedwarn (token->location, OPT_Wpedantic,
14948 "ISO C++ forbids use of %<auto%> in parameter "
14949 "declaration");
14951 else
14952 type = make_auto ();
14953 break;
14955 case RID_DECLTYPE:
14956 /* Since DR 743, decltype can either be a simple-type-specifier by
14957 itself or begin a nested-name-specifier. Parsing it will replace
14958 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14959 handling below decide what to do. */
14960 cp_parser_decltype (parser);
14961 cp_lexer_set_token_position (parser->lexer, token);
14962 break;
14964 case RID_TYPEOF:
14965 /* Consume the `typeof' token. */
14966 cp_lexer_consume_token (parser->lexer);
14967 /* Parse the operand to `typeof'. */
14968 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14969 /* If it is not already a TYPE, take its type. */
14970 if (!TYPE_P (type))
14971 type = finish_typeof (type);
14973 if (decl_specs)
14974 cp_parser_set_decl_spec_type (decl_specs, type,
14975 token,
14976 /*type_definition_p=*/false);
14978 return type;
14980 case RID_UNDERLYING_TYPE:
14981 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14982 if (decl_specs)
14983 cp_parser_set_decl_spec_type (decl_specs, type,
14984 token,
14985 /*type_definition_p=*/false);
14987 return type;
14989 case RID_BASES:
14990 case RID_DIRECT_BASES:
14991 type = cp_parser_trait_expr (parser, token->keyword);
14992 if (decl_specs)
14993 cp_parser_set_decl_spec_type (decl_specs, type,
14994 token,
14995 /*type_definition_p=*/false);
14996 return type;
14997 default:
14998 break;
15001 /* If token is an already-parsed decltype not followed by ::,
15002 it's a simple-type-specifier. */
15003 if (token->type == CPP_DECLTYPE
15004 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15006 type = token->u.value;
15007 if (decl_specs)
15009 cp_parser_set_decl_spec_type (decl_specs, type,
15010 token,
15011 /*type_definition_p=*/false);
15012 /* Remember that we are handling a decltype in order to
15013 implement the resolution of DR 1510 when the argument
15014 isn't instantiation dependent. */
15015 decl_specs->decltype_p = true;
15017 cp_lexer_consume_token (parser->lexer);
15018 return type;
15021 /* If the type-specifier was for a built-in type, we're done. */
15022 if (type)
15024 /* Record the type. */
15025 if (decl_specs
15026 && (token->keyword != RID_SIGNED
15027 && token->keyword != RID_UNSIGNED
15028 && token->keyword != RID_SHORT
15029 && token->keyword != RID_LONG))
15030 cp_parser_set_decl_spec_type (decl_specs,
15031 type,
15032 token,
15033 /*type_definition_p=*/false);
15034 if (decl_specs)
15035 decl_specs->any_specifiers_p = true;
15037 /* Consume the token. */
15038 cp_lexer_consume_token (parser->lexer);
15040 if (type == error_mark_node)
15041 return error_mark_node;
15043 /* There is no valid C++ program where a non-template type is
15044 followed by a "<". That usually indicates that the user thought
15045 that the type was a template. */
15046 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15047 token->location);
15049 return TYPE_NAME (type);
15052 /* The type-specifier must be a user-defined type. */
15053 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15055 bool qualified_p;
15056 bool global_p;
15058 /* Don't gobble tokens or issue error messages if this is an
15059 optional type-specifier. */
15060 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15061 cp_parser_parse_tentatively (parser);
15063 /* Look for the optional `::' operator. */
15064 global_p
15065 = (cp_parser_global_scope_opt (parser,
15066 /*current_scope_valid_p=*/false)
15067 != NULL_TREE);
15068 /* Look for the nested-name specifier. */
15069 qualified_p
15070 = (cp_parser_nested_name_specifier_opt (parser,
15071 /*typename_keyword_p=*/false,
15072 /*check_dependency_p=*/true,
15073 /*type_p=*/false,
15074 /*is_declaration=*/false)
15075 != NULL_TREE);
15076 token = cp_lexer_peek_token (parser->lexer);
15077 /* If we have seen a nested-name-specifier, and the next token
15078 is `template', then we are using the template-id production. */
15079 if (parser->scope
15080 && cp_parser_optional_template_keyword (parser))
15082 /* Look for the template-id. */
15083 type = cp_parser_template_id (parser,
15084 /*template_keyword_p=*/true,
15085 /*check_dependency_p=*/true,
15086 none_type,
15087 /*is_declaration=*/false);
15088 /* If the template-id did not name a type, we are out of
15089 luck. */
15090 if (TREE_CODE (type) != TYPE_DECL)
15092 cp_parser_error (parser, "expected template-id for type");
15093 type = NULL_TREE;
15096 /* Otherwise, look for a type-name. */
15097 else
15098 type = cp_parser_type_name (parser);
15099 /* Keep track of all name-lookups performed in class scopes. */
15100 if (type
15101 && !global_p
15102 && !qualified_p
15103 && TREE_CODE (type) == TYPE_DECL
15104 && identifier_p (DECL_NAME (type)))
15105 maybe_note_name_used_in_class (DECL_NAME (type), type);
15106 /* If it didn't work out, we don't have a TYPE. */
15107 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15108 && !cp_parser_parse_definitely (parser))
15109 type = NULL_TREE;
15110 if (type && decl_specs)
15111 cp_parser_set_decl_spec_type (decl_specs, type,
15112 token,
15113 /*type_definition_p=*/false);
15116 /* If we didn't get a type-name, issue an error message. */
15117 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15119 cp_parser_error (parser, "expected type-name");
15120 return error_mark_node;
15123 if (type && type != error_mark_node)
15125 /* See if TYPE is an Objective-C type, and if so, parse and
15126 accept any protocol references following it. Do this before
15127 the cp_parser_check_for_invalid_template_id() call, because
15128 Objective-C types can be followed by '<...>' which would
15129 enclose protocol names rather than template arguments, and so
15130 everything is fine. */
15131 if (c_dialect_objc () && !parser->scope
15132 && (objc_is_id (type) || objc_is_class_name (type)))
15134 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15135 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15137 /* Clobber the "unqualified" type previously entered into
15138 DECL_SPECS with the new, improved protocol-qualified version. */
15139 if (decl_specs)
15140 decl_specs->type = qual_type;
15142 return qual_type;
15145 /* There is no valid C++ program where a non-template type is
15146 followed by a "<". That usually indicates that the user
15147 thought that the type was a template. */
15148 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15149 none_type,
15150 token->location);
15153 return type;
15156 /* Parse a type-name.
15158 type-name:
15159 class-name
15160 enum-name
15161 typedef-name
15162 simple-template-id [in c++0x]
15164 enum-name:
15165 identifier
15167 typedef-name:
15168 identifier
15170 Returns a TYPE_DECL for the type. */
15172 static tree
15173 cp_parser_type_name (cp_parser* parser)
15175 tree type_decl;
15177 /* We can't know yet whether it is a class-name or not. */
15178 cp_parser_parse_tentatively (parser);
15179 /* Try a class-name. */
15180 type_decl = cp_parser_class_name (parser,
15181 /*typename_keyword_p=*/false,
15182 /*template_keyword_p=*/false,
15183 none_type,
15184 /*check_dependency_p=*/true,
15185 /*class_head_p=*/false,
15186 /*is_declaration=*/false);
15187 /* If it's not a class-name, keep looking. */
15188 if (!cp_parser_parse_definitely (parser))
15190 if (cxx_dialect < cxx11)
15191 /* It must be a typedef-name or an enum-name. */
15192 return cp_parser_nonclass_name (parser);
15194 cp_parser_parse_tentatively (parser);
15195 /* It is either a simple-template-id representing an
15196 instantiation of an alias template... */
15197 type_decl = cp_parser_template_id (parser,
15198 /*template_keyword_p=*/false,
15199 /*check_dependency_p=*/true,
15200 none_type,
15201 /*is_declaration=*/false);
15202 /* Note that this must be an instantiation of an alias template
15203 because [temp.names]/6 says:
15205 A template-id that names an alias template specialization
15206 is a type-name.
15208 Whereas [temp.names]/7 says:
15210 A simple-template-id that names a class template
15211 specialization is a class-name. */
15212 if (type_decl != NULL_TREE
15213 && TREE_CODE (type_decl) == TYPE_DECL
15214 && TYPE_DECL_ALIAS_P (type_decl))
15215 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15216 else
15217 cp_parser_simulate_error (parser);
15219 if (!cp_parser_parse_definitely (parser))
15220 /* ... Or a typedef-name or an enum-name. */
15221 return cp_parser_nonclass_name (parser);
15224 return type_decl;
15227 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15229 enum-name:
15230 identifier
15232 typedef-name:
15233 identifier
15235 Returns a TYPE_DECL for the type. */
15237 static tree
15238 cp_parser_nonclass_name (cp_parser* parser)
15240 tree type_decl;
15241 tree identifier;
15243 cp_token *token = cp_lexer_peek_token (parser->lexer);
15244 identifier = cp_parser_identifier (parser);
15245 if (identifier == error_mark_node)
15246 return error_mark_node;
15248 /* Look up the type-name. */
15249 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15251 type_decl = strip_using_decl (type_decl);
15253 if (TREE_CODE (type_decl) != TYPE_DECL
15254 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15256 /* See if this is an Objective-C type. */
15257 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15258 tree type = objc_get_protocol_qualified_type (identifier, protos);
15259 if (type)
15260 type_decl = TYPE_NAME (type);
15263 /* Issue an error if we did not find a type-name. */
15264 if (TREE_CODE (type_decl) != TYPE_DECL
15265 /* In Objective-C, we have the complication that class names are
15266 normally type names and start declarations (eg, the
15267 "NSObject" in "NSObject *object;"), but can be used in an
15268 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15269 is an expression. So, a classname followed by a dot is not a
15270 valid type-name. */
15271 || (objc_is_class_name (TREE_TYPE (type_decl))
15272 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15274 if (!cp_parser_simulate_error (parser))
15275 cp_parser_name_lookup_error (parser, identifier, type_decl,
15276 NLE_TYPE, token->location);
15277 return error_mark_node;
15279 /* Remember that the name was used in the definition of the
15280 current class so that we can check later to see if the
15281 meaning would have been different after the class was
15282 entirely defined. */
15283 else if (type_decl != error_mark_node
15284 && !parser->scope)
15285 maybe_note_name_used_in_class (identifier, type_decl);
15287 return type_decl;
15290 /* Parse an elaborated-type-specifier. Note that the grammar given
15291 here incorporates the resolution to DR68.
15293 elaborated-type-specifier:
15294 class-key :: [opt] nested-name-specifier [opt] identifier
15295 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15296 enum-key :: [opt] nested-name-specifier [opt] identifier
15297 typename :: [opt] nested-name-specifier identifier
15298 typename :: [opt] nested-name-specifier template [opt]
15299 template-id
15301 GNU extension:
15303 elaborated-type-specifier:
15304 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15305 class-key attributes :: [opt] nested-name-specifier [opt]
15306 template [opt] template-id
15307 enum attributes :: [opt] nested-name-specifier [opt] identifier
15309 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15310 declared `friend'. If IS_DECLARATION is TRUE, then this
15311 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15312 something is being declared.
15314 Returns the TYPE specified. */
15316 static tree
15317 cp_parser_elaborated_type_specifier (cp_parser* parser,
15318 bool is_friend,
15319 bool is_declaration)
15321 enum tag_types tag_type;
15322 tree identifier;
15323 tree type = NULL_TREE;
15324 tree attributes = NULL_TREE;
15325 tree globalscope;
15326 cp_token *token = NULL;
15328 /* See if we're looking at the `enum' keyword. */
15329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15331 /* Consume the `enum' token. */
15332 cp_lexer_consume_token (parser->lexer);
15333 /* Remember that it's an enumeration type. */
15334 tag_type = enum_type;
15335 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15336 enums) is used here. */
15337 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15338 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15340 pedwarn (input_location, 0, "elaborated-type-specifier "
15341 "for a scoped enum must not use the %<%D%> keyword",
15342 cp_lexer_peek_token (parser->lexer)->u.value);
15343 /* Consume the `struct' or `class' and parse it anyway. */
15344 cp_lexer_consume_token (parser->lexer);
15346 /* Parse the attributes. */
15347 attributes = cp_parser_attributes_opt (parser);
15349 /* Or, it might be `typename'. */
15350 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15351 RID_TYPENAME))
15353 /* Consume the `typename' token. */
15354 cp_lexer_consume_token (parser->lexer);
15355 /* Remember that it's a `typename' type. */
15356 tag_type = typename_type;
15358 /* Otherwise it must be a class-key. */
15359 else
15361 tag_type = cp_parser_class_key (parser);
15362 if (tag_type == none_type)
15363 return error_mark_node;
15364 /* Parse the attributes. */
15365 attributes = cp_parser_attributes_opt (parser);
15368 /* Look for the `::' operator. */
15369 globalscope = cp_parser_global_scope_opt (parser,
15370 /*current_scope_valid_p=*/false);
15371 /* Look for the nested-name-specifier. */
15372 if (tag_type == typename_type && !globalscope)
15374 if (!cp_parser_nested_name_specifier (parser,
15375 /*typename_keyword_p=*/true,
15376 /*check_dependency_p=*/true,
15377 /*type_p=*/true,
15378 is_declaration))
15379 return error_mark_node;
15381 else
15382 /* Even though `typename' is not present, the proposed resolution
15383 to Core Issue 180 says that in `class A<T>::B', `B' should be
15384 considered a type-name, even if `A<T>' is dependent. */
15385 cp_parser_nested_name_specifier_opt (parser,
15386 /*typename_keyword_p=*/true,
15387 /*check_dependency_p=*/true,
15388 /*type_p=*/true,
15389 is_declaration);
15390 /* For everything but enumeration types, consider a template-id.
15391 For an enumeration type, consider only a plain identifier. */
15392 if (tag_type != enum_type)
15394 bool template_p = false;
15395 tree decl;
15397 /* Allow the `template' keyword. */
15398 template_p = cp_parser_optional_template_keyword (parser);
15399 /* If we didn't see `template', we don't know if there's a
15400 template-id or not. */
15401 if (!template_p)
15402 cp_parser_parse_tentatively (parser);
15403 /* Parse the template-id. */
15404 token = cp_lexer_peek_token (parser->lexer);
15405 decl = cp_parser_template_id (parser, template_p,
15406 /*check_dependency_p=*/true,
15407 tag_type,
15408 is_declaration);
15409 /* If we didn't find a template-id, look for an ordinary
15410 identifier. */
15411 if (!template_p && !cp_parser_parse_definitely (parser))
15413 /* We can get here when cp_parser_template_id, called by
15414 cp_parser_class_name with tag_type == none_type, succeeds
15415 and caches a BASELINK. Then, when called again here,
15416 instead of failing and returning an error_mark_node
15417 returns it (see template/typename17.C in C++11).
15418 ??? Could we diagnose this earlier? */
15419 else if (tag_type == typename_type && BASELINK_P (decl))
15421 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15422 type = error_mark_node;
15424 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15425 in effect, then we must assume that, upon instantiation, the
15426 template will correspond to a class. */
15427 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15428 && tag_type == typename_type)
15429 type = make_typename_type (parser->scope, decl,
15430 typename_type,
15431 /*complain=*/tf_error);
15432 /* If the `typename' keyword is in effect and DECL is not a type
15433 decl, then type is non existent. */
15434 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15436 else if (TREE_CODE (decl) == TYPE_DECL)
15437 type = check_elaborated_type_specifier (tag_type, decl,
15438 /*allow_template_p=*/true);
15439 else if (decl == error_mark_node)
15440 type = error_mark_node;
15443 if (!type)
15445 token = cp_lexer_peek_token (parser->lexer);
15446 identifier = cp_parser_identifier (parser);
15448 if (identifier == error_mark_node)
15450 parser->scope = NULL_TREE;
15451 return error_mark_node;
15454 /* For a `typename', we needn't call xref_tag. */
15455 if (tag_type == typename_type
15456 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15457 return cp_parser_make_typename_type (parser, identifier,
15458 token->location);
15460 /* Template parameter lists apply only if we are not within a
15461 function parameter list. */
15462 bool template_parm_lists_apply
15463 = parser->num_template_parameter_lists;
15464 if (template_parm_lists_apply)
15465 for (cp_binding_level *s = current_binding_level;
15466 s && s->kind != sk_template_parms;
15467 s = s->level_chain)
15468 if (s->kind == sk_function_parms)
15469 template_parm_lists_apply = false;
15471 /* Look up a qualified name in the usual way. */
15472 if (parser->scope)
15474 tree decl;
15475 tree ambiguous_decls;
15477 decl = cp_parser_lookup_name (parser, identifier,
15478 tag_type,
15479 /*is_template=*/false,
15480 /*is_namespace=*/false,
15481 /*check_dependency=*/true,
15482 &ambiguous_decls,
15483 token->location);
15485 /* If the lookup was ambiguous, an error will already have been
15486 issued. */
15487 if (ambiguous_decls)
15488 return error_mark_node;
15490 /* If we are parsing friend declaration, DECL may be a
15491 TEMPLATE_DECL tree node here. However, we need to check
15492 whether this TEMPLATE_DECL results in valid code. Consider
15493 the following example:
15495 namespace N {
15496 template <class T> class C {};
15498 class X {
15499 template <class T> friend class N::C; // #1, valid code
15501 template <class T> class Y {
15502 friend class N::C; // #2, invalid code
15505 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15506 name lookup of `N::C'. We see that friend declaration must
15507 be template for the code to be valid. Note that
15508 processing_template_decl does not work here since it is
15509 always 1 for the above two cases. */
15511 decl = (cp_parser_maybe_treat_template_as_class
15512 (decl, /*tag_name_p=*/is_friend
15513 && template_parm_lists_apply));
15515 if (TREE_CODE (decl) != TYPE_DECL)
15517 cp_parser_diagnose_invalid_type_name (parser,
15518 identifier,
15519 token->location);
15520 return error_mark_node;
15523 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15525 bool allow_template = (template_parm_lists_apply
15526 || DECL_SELF_REFERENCE_P (decl));
15527 type = check_elaborated_type_specifier (tag_type, decl,
15528 allow_template);
15530 if (type == error_mark_node)
15531 return error_mark_node;
15534 /* Forward declarations of nested types, such as
15536 class C1::C2;
15537 class C1::C2::C3;
15539 are invalid unless all components preceding the final '::'
15540 are complete. If all enclosing types are complete, these
15541 declarations become merely pointless.
15543 Invalid forward declarations of nested types are errors
15544 caught elsewhere in parsing. Those that are pointless arrive
15545 here. */
15547 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15548 && !is_friend && !processing_explicit_instantiation)
15549 warning (0, "declaration %qD does not declare anything", decl);
15551 type = TREE_TYPE (decl);
15553 else
15555 /* An elaborated-type-specifier sometimes introduces a new type and
15556 sometimes names an existing type. Normally, the rule is that it
15557 introduces a new type only if there is not an existing type of
15558 the same name already in scope. For example, given:
15560 struct S {};
15561 void f() { struct S s; }
15563 the `struct S' in the body of `f' is the same `struct S' as in
15564 the global scope; the existing definition is used. However, if
15565 there were no global declaration, this would introduce a new
15566 local class named `S'.
15568 An exception to this rule applies to the following code:
15570 namespace N { struct S; }
15572 Here, the elaborated-type-specifier names a new type
15573 unconditionally; even if there is already an `S' in the
15574 containing scope this declaration names a new type.
15575 This exception only applies if the elaborated-type-specifier
15576 forms the complete declaration:
15578 [class.name]
15580 A declaration consisting solely of `class-key identifier ;' is
15581 either a redeclaration of the name in the current scope or a
15582 forward declaration of the identifier as a class name. It
15583 introduces the name into the current scope.
15585 We are in this situation precisely when the next token is a `;'.
15587 An exception to the exception is that a `friend' declaration does
15588 *not* name a new type; i.e., given:
15590 struct S { friend struct T; };
15592 `T' is not a new type in the scope of `S'.
15594 Also, `new struct S' or `sizeof (struct S)' never results in the
15595 definition of a new type; a new type can only be declared in a
15596 declaration context. */
15598 tag_scope ts;
15599 bool template_p;
15601 if (is_friend)
15602 /* Friends have special name lookup rules. */
15603 ts = ts_within_enclosing_non_class;
15604 else if (is_declaration
15605 && cp_lexer_next_token_is (parser->lexer,
15606 CPP_SEMICOLON))
15607 /* This is a `class-key identifier ;' */
15608 ts = ts_current;
15609 else
15610 ts = ts_global;
15612 template_p =
15613 (template_parm_lists_apply
15614 && (cp_parser_next_token_starts_class_definition_p (parser)
15615 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15616 /* An unqualified name was used to reference this type, so
15617 there were no qualifying templates. */
15618 if (template_parm_lists_apply
15619 && !cp_parser_check_template_parameters (parser,
15620 /*num_templates=*/0,
15621 token->location,
15622 /*declarator=*/NULL))
15623 return error_mark_node;
15624 type = xref_tag (tag_type, identifier, ts, template_p);
15628 if (type == error_mark_node)
15629 return error_mark_node;
15631 /* Allow attributes on forward declarations of classes. */
15632 if (attributes)
15634 if (TREE_CODE (type) == TYPENAME_TYPE)
15635 warning (OPT_Wattributes,
15636 "attributes ignored on uninstantiated type");
15637 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15638 && ! processing_explicit_instantiation)
15639 warning (OPT_Wattributes,
15640 "attributes ignored on template instantiation");
15641 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15642 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15643 else
15644 warning (OPT_Wattributes,
15645 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15648 if (tag_type != enum_type)
15650 /* Indicate whether this class was declared as a `class' or as a
15651 `struct'. */
15652 if (TREE_CODE (type) == RECORD_TYPE)
15653 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15654 cp_parser_check_class_key (tag_type, type);
15657 /* A "<" cannot follow an elaborated type specifier. If that
15658 happens, the user was probably trying to form a template-id. */
15659 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15660 token->location);
15662 return type;
15665 /* Parse an enum-specifier.
15667 enum-specifier:
15668 enum-head { enumerator-list [opt] }
15669 enum-head { enumerator-list , } [C++0x]
15671 enum-head:
15672 enum-key identifier [opt] enum-base [opt]
15673 enum-key nested-name-specifier identifier enum-base [opt]
15675 enum-key:
15676 enum
15677 enum class [C++0x]
15678 enum struct [C++0x]
15680 enum-base: [C++0x]
15681 : type-specifier-seq
15683 opaque-enum-specifier:
15684 enum-key identifier enum-base [opt] ;
15686 GNU Extensions:
15687 enum-key attributes[opt] identifier [opt] enum-base [opt]
15688 { enumerator-list [opt] }attributes[opt]
15689 enum-key attributes[opt] identifier [opt] enum-base [opt]
15690 { enumerator-list, }attributes[opt] [C++0x]
15692 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15693 if the token stream isn't an enum-specifier after all. */
15695 static tree
15696 cp_parser_enum_specifier (cp_parser* parser)
15698 tree identifier;
15699 tree type = NULL_TREE;
15700 tree prev_scope;
15701 tree nested_name_specifier = NULL_TREE;
15702 tree attributes;
15703 bool scoped_enum_p = false;
15704 bool has_underlying_type = false;
15705 bool nested_being_defined = false;
15706 bool new_value_list = false;
15707 bool is_new_type = false;
15708 bool is_anonymous = false;
15709 tree underlying_type = NULL_TREE;
15710 cp_token *type_start_token = NULL;
15711 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15713 parser->colon_corrects_to_scope_p = false;
15715 /* Parse tentatively so that we can back up if we don't find a
15716 enum-specifier. */
15717 cp_parser_parse_tentatively (parser);
15719 /* Caller guarantees that the current token is 'enum', an identifier
15720 possibly follows, and the token after that is an opening brace.
15721 If we don't have an identifier, fabricate an anonymous name for
15722 the enumeration being defined. */
15723 cp_lexer_consume_token (parser->lexer);
15725 /* Parse the "class" or "struct", which indicates a scoped
15726 enumeration type in C++0x. */
15727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15728 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15730 if (cxx_dialect < cxx11)
15731 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15733 /* Consume the `struct' or `class' token. */
15734 cp_lexer_consume_token (parser->lexer);
15736 scoped_enum_p = true;
15739 attributes = cp_parser_attributes_opt (parser);
15741 /* Clear the qualification. */
15742 parser->scope = NULL_TREE;
15743 parser->qualifying_scope = NULL_TREE;
15744 parser->object_scope = NULL_TREE;
15746 /* Figure out in what scope the declaration is being placed. */
15747 prev_scope = current_scope ();
15749 type_start_token = cp_lexer_peek_token (parser->lexer);
15751 push_deferring_access_checks (dk_no_check);
15752 nested_name_specifier
15753 = cp_parser_nested_name_specifier_opt (parser,
15754 /*typename_keyword_p=*/true,
15755 /*check_dependency_p=*/false,
15756 /*type_p=*/false,
15757 /*is_declaration=*/false);
15759 if (nested_name_specifier)
15761 tree name;
15763 identifier = cp_parser_identifier (parser);
15764 name = cp_parser_lookup_name (parser, identifier,
15765 enum_type,
15766 /*is_template=*/false,
15767 /*is_namespace=*/false,
15768 /*check_dependency=*/true,
15769 /*ambiguous_decls=*/NULL,
15770 input_location);
15771 if (name && name != error_mark_node)
15773 type = TREE_TYPE (name);
15774 if (TREE_CODE (type) == TYPENAME_TYPE)
15776 /* Are template enums allowed in ISO? */
15777 if (template_parm_scope_p ())
15778 pedwarn (type_start_token->location, OPT_Wpedantic,
15779 "%qD is an enumeration template", name);
15780 /* ignore a typename reference, for it will be solved by name
15781 in start_enum. */
15782 type = NULL_TREE;
15785 else if (nested_name_specifier == error_mark_node)
15786 /* We already issued an error. */;
15787 else
15788 error_at (type_start_token->location,
15789 "%qD is not an enumerator-name", identifier);
15791 else
15793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15794 identifier = cp_parser_identifier (parser);
15795 else
15797 identifier = make_anon_name ();
15798 is_anonymous = true;
15799 if (scoped_enum_p)
15800 error_at (type_start_token->location,
15801 "anonymous scoped enum is not allowed");
15804 pop_deferring_access_checks ();
15806 /* Check for the `:' that denotes a specified underlying type in C++0x.
15807 Note that a ':' could also indicate a bitfield width, however. */
15808 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15810 cp_decl_specifier_seq type_specifiers;
15812 /* Consume the `:'. */
15813 cp_lexer_consume_token (parser->lexer);
15815 /* Parse the type-specifier-seq. */
15816 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15817 /*is_trailing_return=*/false,
15818 &type_specifiers);
15820 /* At this point this is surely not elaborated type specifier. */
15821 if (!cp_parser_parse_definitely (parser))
15822 return NULL_TREE;
15824 if (cxx_dialect < cxx11)
15825 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15827 has_underlying_type = true;
15829 /* If that didn't work, stop. */
15830 if (type_specifiers.type != error_mark_node)
15832 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15833 /*initialized=*/0, NULL);
15834 if (underlying_type == error_mark_node
15835 || check_for_bare_parameter_packs (underlying_type))
15836 underlying_type = NULL_TREE;
15840 /* Look for the `{' but don't consume it yet. */
15841 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15843 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15845 cp_parser_error (parser, "expected %<{%>");
15846 if (has_underlying_type)
15848 type = NULL_TREE;
15849 goto out;
15852 /* An opaque-enum-specifier must have a ';' here. */
15853 if ((scoped_enum_p || underlying_type)
15854 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15856 cp_parser_error (parser, "expected %<;%> or %<{%>");
15857 if (has_underlying_type)
15859 type = NULL_TREE;
15860 goto out;
15865 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15866 return NULL_TREE;
15868 if (nested_name_specifier)
15870 if (CLASS_TYPE_P (nested_name_specifier))
15872 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15873 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15874 push_scope (nested_name_specifier);
15876 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15878 push_nested_namespace (nested_name_specifier);
15882 /* Issue an error message if type-definitions are forbidden here. */
15883 if (!cp_parser_check_type_definition (parser))
15884 type = error_mark_node;
15885 else
15886 /* Create the new type. We do this before consuming the opening
15887 brace so the enum will be recorded as being on the line of its
15888 tag (or the 'enum' keyword, if there is no tag). */
15889 type = start_enum (identifier, type, underlying_type,
15890 scoped_enum_p, &is_new_type);
15892 /* If the next token is not '{' it is an opaque-enum-specifier or an
15893 elaborated-type-specifier. */
15894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15896 timevar_push (TV_PARSE_ENUM);
15897 if (nested_name_specifier
15898 && nested_name_specifier != error_mark_node)
15900 /* The following catches invalid code such as:
15901 enum class S<int>::E { A, B, C }; */
15902 if (!processing_specialization
15903 && CLASS_TYPE_P (nested_name_specifier)
15904 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15905 error_at (type_start_token->location, "cannot add an enumerator "
15906 "list to a template instantiation");
15908 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15910 error_at (type_start_token->location,
15911 "%<%T::%E%> has not been declared",
15912 TYPE_CONTEXT (nested_name_specifier),
15913 nested_name_specifier);
15914 type = error_mark_node;
15916 /* If that scope does not contain the scope in which the
15917 class was originally declared, the program is invalid. */
15918 else if (prev_scope && !is_ancestor (prev_scope,
15919 nested_name_specifier))
15921 if (at_namespace_scope_p ())
15922 error_at (type_start_token->location,
15923 "declaration of %qD in namespace %qD which does not "
15924 "enclose %qD",
15925 type, prev_scope, nested_name_specifier);
15926 else
15927 error_at (type_start_token->location,
15928 "declaration of %qD in %qD which does not "
15929 "enclose %qD",
15930 type, prev_scope, nested_name_specifier);
15931 type = error_mark_node;
15935 if (scoped_enum_p)
15936 begin_scope (sk_scoped_enum, type);
15938 /* Consume the opening brace. */
15939 cp_lexer_consume_token (parser->lexer);
15941 if (type == error_mark_node)
15942 ; /* Nothing to add */
15943 else if (OPAQUE_ENUM_P (type)
15944 || (cxx_dialect > cxx98 && processing_specialization))
15946 new_value_list = true;
15947 SET_OPAQUE_ENUM_P (type, false);
15948 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15950 else
15952 error_at (type_start_token->location,
15953 "multiple definition of %q#T", type);
15954 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15955 "previous definition here");
15956 type = error_mark_node;
15959 if (type == error_mark_node)
15960 cp_parser_skip_to_end_of_block_or_statement (parser);
15961 /* If the next token is not '}', then there are some enumerators. */
15962 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15964 if (is_anonymous && !scoped_enum_p)
15965 pedwarn (type_start_token->location, OPT_Wpedantic,
15966 "ISO C++ forbids empty anonymous enum");
15968 else
15969 cp_parser_enumerator_list (parser, type);
15971 /* Consume the final '}'. */
15972 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15974 if (scoped_enum_p)
15975 finish_scope ();
15976 timevar_pop (TV_PARSE_ENUM);
15978 else
15980 /* If a ';' follows, then it is an opaque-enum-specifier
15981 and additional restrictions apply. */
15982 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15984 if (is_anonymous)
15985 error_at (type_start_token->location,
15986 "opaque-enum-specifier without name");
15987 else if (nested_name_specifier)
15988 error_at (type_start_token->location,
15989 "opaque-enum-specifier must use a simple identifier");
15993 /* Look for trailing attributes to apply to this enumeration, and
15994 apply them if appropriate. */
15995 if (cp_parser_allow_gnu_extensions_p (parser))
15997 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15998 trailing_attr = chainon (trailing_attr, attributes);
15999 cplus_decl_attributes (&type,
16000 trailing_attr,
16001 (int) ATTR_FLAG_TYPE_IN_PLACE);
16004 /* Finish up the enumeration. */
16005 if (type != error_mark_node)
16007 if (new_value_list)
16008 finish_enum_value_list (type);
16009 if (is_new_type)
16010 finish_enum (type);
16013 if (nested_name_specifier)
16015 if (CLASS_TYPE_P (nested_name_specifier))
16017 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16018 pop_scope (nested_name_specifier);
16020 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16022 pop_nested_namespace (nested_name_specifier);
16025 out:
16026 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16027 return type;
16030 /* Parse an enumerator-list. The enumerators all have the indicated
16031 TYPE.
16033 enumerator-list:
16034 enumerator-definition
16035 enumerator-list , enumerator-definition */
16037 static void
16038 cp_parser_enumerator_list (cp_parser* parser, tree type)
16040 while (true)
16042 /* Parse an enumerator-definition. */
16043 cp_parser_enumerator_definition (parser, type);
16045 /* If the next token is not a ',', we've reached the end of
16046 the list. */
16047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16048 break;
16049 /* Otherwise, consume the `,' and keep going. */
16050 cp_lexer_consume_token (parser->lexer);
16051 /* If the next token is a `}', there is a trailing comma. */
16052 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16054 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16055 pedwarn (input_location, OPT_Wpedantic,
16056 "comma at end of enumerator list");
16057 break;
16062 /* Parse an enumerator-definition. The enumerator has the indicated
16063 TYPE.
16065 enumerator-definition:
16066 enumerator
16067 enumerator = constant-expression
16069 enumerator:
16070 identifier */
16072 static void
16073 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16075 tree identifier;
16076 tree value;
16077 location_t loc;
16079 /* Save the input location because we are interested in the location
16080 of the identifier and not the location of the explicit value. */
16081 loc = cp_lexer_peek_token (parser->lexer)->location;
16083 /* Look for the identifier. */
16084 identifier = cp_parser_identifier (parser);
16085 if (identifier == error_mark_node)
16086 return;
16088 /* If the next token is an '=', then there is an explicit value. */
16089 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16091 /* Consume the `=' token. */
16092 cp_lexer_consume_token (parser->lexer);
16093 /* Parse the value. */
16094 value = cp_parser_constant_expression (parser);
16096 else
16097 value = NULL_TREE;
16099 /* If we are processing a template, make sure the initializer of the
16100 enumerator doesn't contain any bare template parameter pack. */
16101 if (check_for_bare_parameter_packs (value))
16102 value = error_mark_node;
16104 /* Create the enumerator. */
16105 build_enumerator (identifier, value, type, loc);
16108 /* Parse a namespace-name.
16110 namespace-name:
16111 original-namespace-name
16112 namespace-alias
16114 Returns the NAMESPACE_DECL for the namespace. */
16116 static tree
16117 cp_parser_namespace_name (cp_parser* parser)
16119 tree identifier;
16120 tree namespace_decl;
16122 cp_token *token = cp_lexer_peek_token (parser->lexer);
16124 /* Get the name of the namespace. */
16125 identifier = cp_parser_identifier (parser);
16126 if (identifier == error_mark_node)
16127 return error_mark_node;
16129 /* Look up the identifier in the currently active scope. Look only
16130 for namespaces, due to:
16132 [basic.lookup.udir]
16134 When looking up a namespace-name in a using-directive or alias
16135 definition, only namespace names are considered.
16137 And:
16139 [basic.lookup.qual]
16141 During the lookup of a name preceding the :: scope resolution
16142 operator, object, function, and enumerator names are ignored.
16144 (Note that cp_parser_qualifying_entity only calls this
16145 function if the token after the name is the scope resolution
16146 operator.) */
16147 namespace_decl = cp_parser_lookup_name (parser, identifier,
16148 none_type,
16149 /*is_template=*/false,
16150 /*is_namespace=*/true,
16151 /*check_dependency=*/true,
16152 /*ambiguous_decls=*/NULL,
16153 token->location);
16154 /* If it's not a namespace, issue an error. */
16155 if (namespace_decl == error_mark_node
16156 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16158 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16159 error_at (token->location, "%qD is not a namespace-name", identifier);
16160 cp_parser_error (parser, "expected namespace-name");
16161 namespace_decl = error_mark_node;
16164 return namespace_decl;
16167 /* Parse a namespace-definition.
16169 namespace-definition:
16170 named-namespace-definition
16171 unnamed-namespace-definition
16173 named-namespace-definition:
16174 original-namespace-definition
16175 extension-namespace-definition
16177 original-namespace-definition:
16178 namespace identifier { namespace-body }
16180 extension-namespace-definition:
16181 namespace original-namespace-name { namespace-body }
16183 unnamed-namespace-definition:
16184 namespace { namespace-body } */
16186 static void
16187 cp_parser_namespace_definition (cp_parser* parser)
16189 tree identifier, attribs;
16190 bool has_visibility;
16191 bool is_inline;
16193 cp_ensure_no_omp_declare_simd (parser);
16194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16196 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16197 is_inline = true;
16198 cp_lexer_consume_token (parser->lexer);
16200 else
16201 is_inline = false;
16203 /* Look for the `namespace' keyword. */
16204 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16206 /* Get the name of the namespace. We do not attempt to distinguish
16207 between an original-namespace-definition and an
16208 extension-namespace-definition at this point. The semantic
16209 analysis routines are responsible for that. */
16210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16211 identifier = cp_parser_identifier (parser);
16212 else
16213 identifier = NULL_TREE;
16215 /* Parse any specified attributes. */
16216 attribs = cp_parser_attributes_opt (parser);
16218 /* Look for the `{' to start the namespace. */
16219 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16220 /* Start the namespace. */
16221 push_namespace (identifier);
16223 /* "inline namespace" is equivalent to a stub namespace definition
16224 followed by a strong using directive. */
16225 if (is_inline)
16227 tree name_space = current_namespace;
16228 /* Set up namespace association. */
16229 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16230 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16231 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16232 /* Import the contents of the inline namespace. */
16233 pop_namespace ();
16234 do_using_directive (name_space);
16235 push_namespace (identifier);
16238 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16240 /* Parse the body of the namespace. */
16241 cp_parser_namespace_body (parser);
16243 if (has_visibility)
16244 pop_visibility (1);
16246 /* Finish the namespace. */
16247 pop_namespace ();
16248 /* Look for the final `}'. */
16249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16252 /* Parse a namespace-body.
16254 namespace-body:
16255 declaration-seq [opt] */
16257 static void
16258 cp_parser_namespace_body (cp_parser* parser)
16260 cp_parser_declaration_seq_opt (parser);
16263 /* Parse a namespace-alias-definition.
16265 namespace-alias-definition:
16266 namespace identifier = qualified-namespace-specifier ; */
16268 static void
16269 cp_parser_namespace_alias_definition (cp_parser* parser)
16271 tree identifier;
16272 tree namespace_specifier;
16274 cp_token *token = cp_lexer_peek_token (parser->lexer);
16276 /* Look for the `namespace' keyword. */
16277 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16278 /* Look for the identifier. */
16279 identifier = cp_parser_identifier (parser);
16280 if (identifier == error_mark_node)
16281 return;
16282 /* Look for the `=' token. */
16283 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16284 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16286 error_at (token->location, "%<namespace%> definition is not allowed here");
16287 /* Skip the definition. */
16288 cp_lexer_consume_token (parser->lexer);
16289 if (cp_parser_skip_to_closing_brace (parser))
16290 cp_lexer_consume_token (parser->lexer);
16291 return;
16293 cp_parser_require (parser, CPP_EQ, RT_EQ);
16294 /* Look for the qualified-namespace-specifier. */
16295 namespace_specifier
16296 = cp_parser_qualified_namespace_specifier (parser);
16297 /* Look for the `;' token. */
16298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16300 /* Register the alias in the symbol table. */
16301 do_namespace_alias (identifier, namespace_specifier);
16304 /* Parse a qualified-namespace-specifier.
16306 qualified-namespace-specifier:
16307 :: [opt] nested-name-specifier [opt] namespace-name
16309 Returns a NAMESPACE_DECL corresponding to the specified
16310 namespace. */
16312 static tree
16313 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16315 /* Look for the optional `::'. */
16316 cp_parser_global_scope_opt (parser,
16317 /*current_scope_valid_p=*/false);
16319 /* Look for the optional nested-name-specifier. */
16320 cp_parser_nested_name_specifier_opt (parser,
16321 /*typename_keyword_p=*/false,
16322 /*check_dependency_p=*/true,
16323 /*type_p=*/false,
16324 /*is_declaration=*/true);
16326 return cp_parser_namespace_name (parser);
16329 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16330 access declaration.
16332 using-declaration:
16333 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16334 using :: unqualified-id ;
16336 access-declaration:
16337 qualified-id ;
16341 static bool
16342 cp_parser_using_declaration (cp_parser* parser,
16343 bool access_declaration_p)
16345 cp_token *token;
16346 bool typename_p = false;
16347 bool global_scope_p;
16348 tree decl;
16349 tree identifier;
16350 tree qscope;
16351 int oldcount = errorcount;
16352 cp_token *diag_token = NULL;
16354 if (access_declaration_p)
16356 diag_token = cp_lexer_peek_token (parser->lexer);
16357 cp_parser_parse_tentatively (parser);
16359 else
16361 /* Look for the `using' keyword. */
16362 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16364 /* Peek at the next token. */
16365 token = cp_lexer_peek_token (parser->lexer);
16366 /* See if it's `typename'. */
16367 if (token->keyword == RID_TYPENAME)
16369 /* Remember that we've seen it. */
16370 typename_p = true;
16371 /* Consume the `typename' token. */
16372 cp_lexer_consume_token (parser->lexer);
16376 /* Look for the optional global scope qualification. */
16377 global_scope_p
16378 = (cp_parser_global_scope_opt (parser,
16379 /*current_scope_valid_p=*/false)
16380 != NULL_TREE);
16382 /* If we saw `typename', or didn't see `::', then there must be a
16383 nested-name-specifier present. */
16384 if (typename_p || !global_scope_p)
16386 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16387 /*check_dependency_p=*/true,
16388 /*type_p=*/false,
16389 /*is_declaration=*/true);
16390 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16392 cp_parser_skip_to_end_of_block_or_statement (parser);
16393 return false;
16396 /* Otherwise, we could be in either of the two productions. In that
16397 case, treat the nested-name-specifier as optional. */
16398 else
16399 qscope = cp_parser_nested_name_specifier_opt (parser,
16400 /*typename_keyword_p=*/false,
16401 /*check_dependency_p=*/true,
16402 /*type_p=*/false,
16403 /*is_declaration=*/true);
16404 if (!qscope)
16405 qscope = global_namespace;
16406 else if (UNSCOPED_ENUM_P (qscope))
16407 qscope = CP_TYPE_CONTEXT (qscope);
16409 if (access_declaration_p && cp_parser_error_occurred (parser))
16410 /* Something has already gone wrong; there's no need to parse
16411 further. Since an error has occurred, the return value of
16412 cp_parser_parse_definitely will be false, as required. */
16413 return cp_parser_parse_definitely (parser);
16415 token = cp_lexer_peek_token (parser->lexer);
16416 /* Parse the unqualified-id. */
16417 identifier = cp_parser_unqualified_id (parser,
16418 /*template_keyword_p=*/false,
16419 /*check_dependency_p=*/true,
16420 /*declarator_p=*/true,
16421 /*optional_p=*/false);
16423 if (access_declaration_p)
16425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16426 cp_parser_simulate_error (parser);
16427 if (!cp_parser_parse_definitely (parser))
16428 return false;
16431 /* The function we call to handle a using-declaration is different
16432 depending on what scope we are in. */
16433 if (qscope == error_mark_node || identifier == error_mark_node)
16435 else if (!identifier_p (identifier)
16436 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16437 /* [namespace.udecl]
16439 A using declaration shall not name a template-id. */
16440 error_at (token->location,
16441 "a template-id may not appear in a using-declaration");
16442 else
16444 if (at_class_scope_p ())
16446 /* Create the USING_DECL. */
16447 decl = do_class_using_decl (parser->scope, identifier);
16449 if (decl && typename_p)
16450 USING_DECL_TYPENAME_P (decl) = 1;
16452 if (check_for_bare_parameter_packs (decl))
16454 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16455 return false;
16457 else
16458 /* Add it to the list of members in this class. */
16459 finish_member_declaration (decl);
16461 else
16463 decl = cp_parser_lookup_name_simple (parser,
16464 identifier,
16465 token->location);
16466 if (decl == error_mark_node)
16467 cp_parser_name_lookup_error (parser, identifier,
16468 decl, NLE_NULL,
16469 token->location);
16470 else if (check_for_bare_parameter_packs (decl))
16472 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16473 return false;
16475 else if (!at_namespace_scope_p ())
16476 do_local_using_decl (decl, qscope, identifier);
16477 else
16478 do_toplevel_using_decl (decl, qscope, identifier);
16482 /* Look for the final `;'. */
16483 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16485 if (access_declaration_p && errorcount == oldcount)
16486 warning_at (diag_token->location, OPT_Wdeprecated,
16487 "access declarations are deprecated "
16488 "in favour of using-declarations; "
16489 "suggestion: add the %<using%> keyword");
16491 return true;
16494 /* Parse an alias-declaration.
16496 alias-declaration:
16497 using identifier attribute-specifier-seq [opt] = type-id */
16499 static tree
16500 cp_parser_alias_declaration (cp_parser* parser)
16502 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16503 location_t id_location;
16504 cp_declarator *declarator;
16505 cp_decl_specifier_seq decl_specs;
16506 bool member_p;
16507 const char *saved_message = NULL;
16509 /* Look for the `using' keyword. */
16510 cp_token *using_token
16511 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16512 if (using_token == NULL)
16513 return error_mark_node;
16515 id_location = cp_lexer_peek_token (parser->lexer)->location;
16516 id = cp_parser_identifier (parser);
16517 if (id == error_mark_node)
16518 return error_mark_node;
16520 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16521 attributes = cp_parser_attributes_opt (parser);
16522 if (attributes == error_mark_node)
16523 return error_mark_node;
16525 cp_parser_require (parser, CPP_EQ, RT_EQ);
16527 if (cp_parser_error_occurred (parser))
16528 return error_mark_node;
16530 cp_parser_commit_to_tentative_parse (parser);
16532 /* Now we are going to parse the type-id of the declaration. */
16535 [dcl.type]/3 says:
16537 "A type-specifier-seq shall not define a class or enumeration
16538 unless it appears in the type-id of an alias-declaration (7.1.3) that
16539 is not the declaration of a template-declaration."
16541 In other words, if we currently are in an alias template, the
16542 type-id should not define a type.
16544 So let's set parser->type_definition_forbidden_message in that
16545 case; cp_parser_check_type_definition (called by
16546 cp_parser_class_specifier) will then emit an error if a type is
16547 defined in the type-id. */
16548 if (parser->num_template_parameter_lists)
16550 saved_message = parser->type_definition_forbidden_message;
16551 parser->type_definition_forbidden_message =
16552 G_("types may not be defined in alias template declarations");
16555 type = cp_parser_type_id (parser);
16557 /* Restore the error message if need be. */
16558 if (parser->num_template_parameter_lists)
16559 parser->type_definition_forbidden_message = saved_message;
16561 if (type == error_mark_node
16562 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16564 cp_parser_skip_to_end_of_block_or_statement (parser);
16565 return error_mark_node;
16568 /* A typedef-name can also be introduced by an alias-declaration. The
16569 identifier following the using keyword becomes a typedef-name. It has
16570 the same semantics as if it were introduced by the typedef
16571 specifier. In particular, it does not define a new type and it shall
16572 not appear in the type-id. */
16574 clear_decl_specs (&decl_specs);
16575 decl_specs.type = type;
16576 if (attributes != NULL_TREE)
16578 decl_specs.attributes = attributes;
16579 set_and_check_decl_spec_loc (&decl_specs,
16580 ds_attribute,
16581 attrs_token);
16583 set_and_check_decl_spec_loc (&decl_specs,
16584 ds_typedef,
16585 using_token);
16586 set_and_check_decl_spec_loc (&decl_specs,
16587 ds_alias,
16588 using_token);
16590 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16591 declarator->id_loc = id_location;
16593 member_p = at_class_scope_p ();
16594 if (member_p)
16595 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16596 NULL_TREE, attributes);
16597 else
16598 decl = start_decl (declarator, &decl_specs, 0,
16599 attributes, NULL_TREE, &pushed_scope);
16600 if (decl == error_mark_node)
16601 return decl;
16603 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16605 if (pushed_scope)
16606 pop_scope (pushed_scope);
16608 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16609 added into the symbol table; otherwise, return the TYPE_DECL. */
16610 if (DECL_LANG_SPECIFIC (decl)
16611 && DECL_TEMPLATE_INFO (decl)
16612 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16614 decl = DECL_TI_TEMPLATE (decl);
16615 if (member_p)
16616 check_member_template (decl);
16619 return decl;
16622 /* Parse a using-directive.
16624 using-directive:
16625 using namespace :: [opt] nested-name-specifier [opt]
16626 namespace-name ; */
16628 static void
16629 cp_parser_using_directive (cp_parser* parser)
16631 tree namespace_decl;
16632 tree attribs;
16634 /* Look for the `using' keyword. */
16635 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16636 /* And the `namespace' keyword. */
16637 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16638 /* Look for the optional `::' operator. */
16639 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16640 /* And the optional nested-name-specifier. */
16641 cp_parser_nested_name_specifier_opt (parser,
16642 /*typename_keyword_p=*/false,
16643 /*check_dependency_p=*/true,
16644 /*type_p=*/false,
16645 /*is_declaration=*/true);
16646 /* Get the namespace being used. */
16647 namespace_decl = cp_parser_namespace_name (parser);
16648 /* And any specified attributes. */
16649 attribs = cp_parser_attributes_opt (parser);
16650 /* Update the symbol table. */
16651 parse_using_directive (namespace_decl, attribs);
16652 /* Look for the final `;'. */
16653 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16656 /* Parse an asm-definition.
16658 asm-definition:
16659 asm ( string-literal ) ;
16661 GNU Extension:
16663 asm-definition:
16664 asm volatile [opt] ( string-literal ) ;
16665 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16666 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16667 : asm-operand-list [opt] ) ;
16668 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16669 : asm-operand-list [opt]
16670 : asm-clobber-list [opt] ) ;
16671 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16672 : asm-clobber-list [opt]
16673 : asm-goto-list ) ; */
16675 static void
16676 cp_parser_asm_definition (cp_parser* parser)
16678 tree string;
16679 tree outputs = NULL_TREE;
16680 tree inputs = NULL_TREE;
16681 tree clobbers = NULL_TREE;
16682 tree labels = NULL_TREE;
16683 tree asm_stmt;
16684 bool volatile_p = false;
16685 bool extended_p = false;
16686 bool invalid_inputs_p = false;
16687 bool invalid_outputs_p = false;
16688 bool goto_p = false;
16689 required_token missing = RT_NONE;
16691 /* Look for the `asm' keyword. */
16692 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16694 if (parser->in_function_body
16695 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16697 error ("%<asm%> in %<constexpr%> function");
16698 cp_function_chain->invalid_constexpr = true;
16701 /* See if the next token is `volatile'. */
16702 if (cp_parser_allow_gnu_extensions_p (parser)
16703 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16705 /* Remember that we saw the `volatile' keyword. */
16706 volatile_p = true;
16707 /* Consume the token. */
16708 cp_lexer_consume_token (parser->lexer);
16710 if (cp_parser_allow_gnu_extensions_p (parser)
16711 && parser->in_function_body
16712 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16714 /* Remember that we saw the `goto' keyword. */
16715 goto_p = true;
16716 /* Consume the token. */
16717 cp_lexer_consume_token (parser->lexer);
16719 /* Look for the opening `('. */
16720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16721 return;
16722 /* Look for the string. */
16723 string = cp_parser_string_literal (parser, false, false);
16724 if (string == error_mark_node)
16726 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16727 /*consume_paren=*/true);
16728 return;
16731 /* If we're allowing GNU extensions, check for the extended assembly
16732 syntax. Unfortunately, the `:' tokens need not be separated by
16733 a space in C, and so, for compatibility, we tolerate that here
16734 too. Doing that means that we have to treat the `::' operator as
16735 two `:' tokens. */
16736 if (cp_parser_allow_gnu_extensions_p (parser)
16737 && parser->in_function_body
16738 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16739 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16741 bool inputs_p = false;
16742 bool clobbers_p = false;
16743 bool labels_p = false;
16745 /* The extended syntax was used. */
16746 extended_p = true;
16748 /* Look for outputs. */
16749 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16751 /* Consume the `:'. */
16752 cp_lexer_consume_token (parser->lexer);
16753 /* Parse the output-operands. */
16754 if (cp_lexer_next_token_is_not (parser->lexer,
16755 CPP_COLON)
16756 && cp_lexer_next_token_is_not (parser->lexer,
16757 CPP_SCOPE)
16758 && cp_lexer_next_token_is_not (parser->lexer,
16759 CPP_CLOSE_PAREN)
16760 && !goto_p)
16761 outputs = cp_parser_asm_operand_list (parser);
16763 if (outputs == error_mark_node)
16764 invalid_outputs_p = true;
16766 /* If the next token is `::', there are no outputs, and the
16767 next token is the beginning of the inputs. */
16768 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16769 /* The inputs are coming next. */
16770 inputs_p = true;
16772 /* Look for inputs. */
16773 if (inputs_p
16774 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16776 /* Consume the `:' or `::'. */
16777 cp_lexer_consume_token (parser->lexer);
16778 /* Parse the output-operands. */
16779 if (cp_lexer_next_token_is_not (parser->lexer,
16780 CPP_COLON)
16781 && cp_lexer_next_token_is_not (parser->lexer,
16782 CPP_SCOPE)
16783 && cp_lexer_next_token_is_not (parser->lexer,
16784 CPP_CLOSE_PAREN))
16785 inputs = cp_parser_asm_operand_list (parser);
16787 if (inputs == error_mark_node)
16788 invalid_inputs_p = true;
16790 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16791 /* The clobbers are coming next. */
16792 clobbers_p = true;
16794 /* Look for clobbers. */
16795 if (clobbers_p
16796 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16798 clobbers_p = true;
16799 /* Consume the `:' or `::'. */
16800 cp_lexer_consume_token (parser->lexer);
16801 /* Parse the clobbers. */
16802 if (cp_lexer_next_token_is_not (parser->lexer,
16803 CPP_COLON)
16804 && cp_lexer_next_token_is_not (parser->lexer,
16805 CPP_CLOSE_PAREN))
16806 clobbers = cp_parser_asm_clobber_list (parser);
16808 else if (goto_p
16809 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16810 /* The labels are coming next. */
16811 labels_p = true;
16813 /* Look for labels. */
16814 if (labels_p
16815 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16817 labels_p = true;
16818 /* Consume the `:' or `::'. */
16819 cp_lexer_consume_token (parser->lexer);
16820 /* Parse the labels. */
16821 labels = cp_parser_asm_label_list (parser);
16824 if (goto_p && !labels_p)
16825 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16827 else if (goto_p)
16828 missing = RT_COLON_SCOPE;
16830 /* Look for the closing `)'. */
16831 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16832 missing ? missing : RT_CLOSE_PAREN))
16833 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16834 /*consume_paren=*/true);
16835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16837 if (!invalid_inputs_p && !invalid_outputs_p)
16839 /* Create the ASM_EXPR. */
16840 if (parser->in_function_body)
16842 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16843 inputs, clobbers, labels);
16844 /* If the extended syntax was not used, mark the ASM_EXPR. */
16845 if (!extended_p)
16847 tree temp = asm_stmt;
16848 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16849 temp = TREE_OPERAND (temp, 0);
16851 ASM_INPUT_P (temp) = 1;
16854 else
16855 symtab->finalize_toplevel_asm (string);
16859 /* Declarators [gram.dcl.decl] */
16861 /* Parse an init-declarator.
16863 init-declarator:
16864 declarator initializer [opt]
16866 GNU Extension:
16868 init-declarator:
16869 declarator asm-specification [opt] attributes [opt] initializer [opt]
16871 function-definition:
16872 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16873 function-body
16874 decl-specifier-seq [opt] declarator function-try-block
16876 GNU Extension:
16878 function-definition:
16879 __extension__ function-definition
16881 TM Extension:
16883 function-definition:
16884 decl-specifier-seq [opt] declarator function-transaction-block
16886 The DECL_SPECIFIERS apply to this declarator. Returns a
16887 representation of the entity declared. If MEMBER_P is TRUE, then
16888 this declarator appears in a class scope. The new DECL created by
16889 this declarator is returned.
16891 The CHECKS are access checks that should be performed once we know
16892 what entity is being declared (and, therefore, what classes have
16893 befriended it).
16895 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16896 for a function-definition here as well. If the declarator is a
16897 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16898 be TRUE upon return. By that point, the function-definition will
16899 have been completely parsed.
16901 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16902 is FALSE.
16904 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16905 parsed declaration if it is an uninitialized single declarator not followed
16906 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16907 if present, will not be consumed. If returned, this declarator will be
16908 created with SD_INITIALIZED but will not call cp_finish_decl.
16910 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16911 and there is an initializer, the pointed location_t is set to the
16912 location of the '=' or `(', or '{' in C++11 token introducing the
16913 initializer. */
16915 static tree
16916 cp_parser_init_declarator (cp_parser* parser,
16917 cp_decl_specifier_seq *decl_specifiers,
16918 vec<deferred_access_check, va_gc> *checks,
16919 bool function_definition_allowed_p,
16920 bool member_p,
16921 int declares_class_or_enum,
16922 bool* function_definition_p,
16923 tree* maybe_range_for_decl,
16924 location_t* init_loc)
16926 cp_token *token = NULL, *asm_spec_start_token = NULL,
16927 *attributes_start_token = NULL;
16928 cp_declarator *declarator;
16929 tree prefix_attributes;
16930 tree attributes = NULL;
16931 tree asm_specification;
16932 tree initializer;
16933 tree decl = NULL_TREE;
16934 tree scope;
16935 int is_initialized;
16936 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16937 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16938 "(...)". */
16939 enum cpp_ttype initialization_kind;
16940 bool is_direct_init = false;
16941 bool is_non_constant_init;
16942 int ctor_dtor_or_conv_p;
16943 bool friend_p = cp_parser_friend_p (decl_specifiers);
16944 tree pushed_scope = NULL_TREE;
16945 bool range_for_decl_p = false;
16946 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16947 location_t tmp_init_loc = UNKNOWN_LOCATION;
16949 /* Gather the attributes that were provided with the
16950 decl-specifiers. */
16951 prefix_attributes = decl_specifiers->attributes;
16953 /* Assume that this is not the declarator for a function
16954 definition. */
16955 if (function_definition_p)
16956 *function_definition_p = false;
16958 /* Default arguments are only permitted for function parameters. */
16959 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16960 parser->default_arg_ok_p = false;
16962 /* Defer access checks while parsing the declarator; we cannot know
16963 what names are accessible until we know what is being
16964 declared. */
16965 resume_deferring_access_checks ();
16967 /* Parse the declarator. */
16968 token = cp_lexer_peek_token (parser->lexer);
16969 declarator
16970 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16971 &ctor_dtor_or_conv_p,
16972 /*parenthesized_p=*/NULL,
16973 member_p, friend_p);
16974 /* Gather up the deferred checks. */
16975 stop_deferring_access_checks ();
16977 parser->default_arg_ok_p = saved_default_arg_ok_p;
16979 /* If the DECLARATOR was erroneous, there's no need to go
16980 further. */
16981 if (declarator == cp_error_declarator)
16982 return error_mark_node;
16984 /* Check that the number of template-parameter-lists is OK. */
16985 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16986 token->location))
16987 return error_mark_node;
16989 if (declares_class_or_enum & 2)
16990 cp_parser_check_for_definition_in_return_type (declarator,
16991 decl_specifiers->type,
16992 decl_specifiers->locations[ds_type_spec]);
16994 /* Figure out what scope the entity declared by the DECLARATOR is
16995 located in. `grokdeclarator' sometimes changes the scope, so
16996 we compute it now. */
16997 scope = get_scope_of_declarator (declarator);
16999 /* Perform any lookups in the declared type which were thought to be
17000 dependent, but are not in the scope of the declarator. */
17001 decl_specifiers->type
17002 = maybe_update_decl_type (decl_specifiers->type, scope);
17004 /* If we're allowing GNU extensions, look for an
17005 asm-specification. */
17006 if (cp_parser_allow_gnu_extensions_p (parser))
17008 /* Look for an asm-specification. */
17009 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17010 asm_specification = cp_parser_asm_specification_opt (parser);
17012 else
17013 asm_specification = NULL_TREE;
17015 /* Look for attributes. */
17016 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17017 attributes = cp_parser_attributes_opt (parser);
17019 /* Peek at the next token. */
17020 token = cp_lexer_peek_token (parser->lexer);
17022 bool bogus_implicit_tmpl = false;
17024 if (function_declarator_p (declarator))
17026 /* Check to see if the token indicates the start of a
17027 function-definition. */
17028 if (cp_parser_token_starts_function_definition_p (token))
17030 if (!function_definition_allowed_p)
17032 /* If a function-definition should not appear here, issue an
17033 error message. */
17034 cp_parser_error (parser,
17035 "a function-definition is not allowed here");
17036 return error_mark_node;
17039 location_t func_brace_location
17040 = cp_lexer_peek_token (parser->lexer)->location;
17042 /* Neither attributes nor an asm-specification are allowed
17043 on a function-definition. */
17044 if (asm_specification)
17045 error_at (asm_spec_start_token->location,
17046 "an asm-specification is not allowed "
17047 "on a function-definition");
17048 if (attributes)
17049 error_at (attributes_start_token->location,
17050 "attributes are not allowed "
17051 "on a function-definition");
17052 /* This is a function-definition. */
17053 *function_definition_p = true;
17055 /* Parse the function definition. */
17056 if (member_p)
17057 decl = cp_parser_save_member_function_body (parser,
17058 decl_specifiers,
17059 declarator,
17060 prefix_attributes);
17061 else
17062 decl =
17063 (cp_parser_function_definition_from_specifiers_and_declarator
17064 (parser, decl_specifiers, prefix_attributes, declarator));
17066 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17068 /* This is where the prologue starts... */
17069 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17070 = func_brace_location;
17073 return decl;
17076 else if (parser->fully_implicit_function_template_p)
17078 /* A non-template declaration involving a function parameter list
17079 containing an implicit template parameter will be made into a
17080 template. If the resulting declaration is not going to be an
17081 actual function then finish the template scope here to prevent it.
17082 An error message will be issued once we have a decl to talk about.
17084 FIXME probably we should do type deduction rather than create an
17085 implicit template, but the standard currently doesn't allow it. */
17086 bogus_implicit_tmpl = true;
17087 finish_fully_implicit_template (parser, NULL_TREE);
17090 /* [dcl.dcl]
17092 Only in function declarations for constructors, destructors, and
17093 type conversions can the decl-specifier-seq be omitted.
17095 We explicitly postpone this check past the point where we handle
17096 function-definitions because we tolerate function-definitions
17097 that are missing their return types in some modes. */
17098 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17100 cp_parser_error (parser,
17101 "expected constructor, destructor, or type conversion");
17102 return error_mark_node;
17105 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17106 if (token->type == CPP_EQ
17107 || token->type == CPP_OPEN_PAREN
17108 || token->type == CPP_OPEN_BRACE)
17110 is_initialized = SD_INITIALIZED;
17111 initialization_kind = token->type;
17112 if (maybe_range_for_decl)
17113 *maybe_range_for_decl = error_mark_node;
17114 tmp_init_loc = token->location;
17115 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17116 *init_loc = tmp_init_loc;
17118 if (token->type == CPP_EQ
17119 && function_declarator_p (declarator))
17121 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17122 if (t2->keyword == RID_DEFAULT)
17123 is_initialized = SD_DEFAULTED;
17124 else if (t2->keyword == RID_DELETE)
17125 is_initialized = SD_DELETED;
17128 else
17130 /* If the init-declarator isn't initialized and isn't followed by a
17131 `,' or `;', it's not a valid init-declarator. */
17132 if (token->type != CPP_COMMA
17133 && token->type != CPP_SEMICOLON)
17135 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17136 range_for_decl_p = true;
17137 else
17139 if (!maybe_range_for_decl)
17140 cp_parser_error (parser, "expected initializer");
17141 return error_mark_node;
17144 is_initialized = SD_UNINITIALIZED;
17145 initialization_kind = CPP_EOF;
17148 /* Because start_decl has side-effects, we should only call it if we
17149 know we're going ahead. By this point, we know that we cannot
17150 possibly be looking at any other construct. */
17151 cp_parser_commit_to_tentative_parse (parser);
17153 /* Enter the newly declared entry in the symbol table. If we're
17154 processing a declaration in a class-specifier, we wait until
17155 after processing the initializer. */
17156 if (!member_p)
17158 if (parser->in_unbraced_linkage_specification_p)
17159 decl_specifiers->storage_class = sc_extern;
17160 decl = start_decl (declarator, decl_specifiers,
17161 range_for_decl_p? SD_INITIALIZED : is_initialized,
17162 attributes, prefix_attributes, &pushed_scope);
17163 cp_finalize_omp_declare_simd (parser, decl);
17164 /* Adjust location of decl if declarator->id_loc is more appropriate:
17165 set, and decl wasn't merged with another decl, in which case its
17166 location would be different from input_location, and more accurate. */
17167 if (DECL_P (decl)
17168 && declarator->id_loc != UNKNOWN_LOCATION
17169 && DECL_SOURCE_LOCATION (decl) == input_location)
17170 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17172 else if (scope)
17173 /* Enter the SCOPE. That way unqualified names appearing in the
17174 initializer will be looked up in SCOPE. */
17175 pushed_scope = push_scope (scope);
17177 /* Perform deferred access control checks, now that we know in which
17178 SCOPE the declared entity resides. */
17179 if (!member_p && decl)
17181 tree saved_current_function_decl = NULL_TREE;
17183 /* If the entity being declared is a function, pretend that we
17184 are in its scope. If it is a `friend', it may have access to
17185 things that would not otherwise be accessible. */
17186 if (TREE_CODE (decl) == FUNCTION_DECL)
17188 saved_current_function_decl = current_function_decl;
17189 current_function_decl = decl;
17192 /* Perform access checks for template parameters. */
17193 cp_parser_perform_template_parameter_access_checks (checks);
17195 /* Perform the access control checks for the declarator and the
17196 decl-specifiers. */
17197 perform_deferred_access_checks (tf_warning_or_error);
17199 /* Restore the saved value. */
17200 if (TREE_CODE (decl) == FUNCTION_DECL)
17201 current_function_decl = saved_current_function_decl;
17204 /* Parse the initializer. */
17205 initializer = NULL_TREE;
17206 is_direct_init = false;
17207 is_non_constant_init = true;
17208 if (is_initialized)
17210 if (function_declarator_p (declarator))
17212 if (initialization_kind == CPP_EQ)
17213 initializer = cp_parser_pure_specifier (parser);
17214 else
17216 /* If the declaration was erroneous, we don't really
17217 know what the user intended, so just silently
17218 consume the initializer. */
17219 if (decl != error_mark_node)
17220 error_at (tmp_init_loc, "initializer provided for function");
17221 cp_parser_skip_to_closing_parenthesis (parser,
17222 /*recovering=*/true,
17223 /*or_comma=*/false,
17224 /*consume_paren=*/true);
17227 else
17229 /* We want to record the extra mangling scope for in-class
17230 initializers of class members and initializers of static data
17231 member templates. The former involves deferring
17232 parsing of the initializer until end of class as with default
17233 arguments. So right here we only handle the latter. */
17234 if (!member_p && processing_template_decl)
17235 start_lambda_scope (decl);
17236 initializer = cp_parser_initializer (parser,
17237 &is_direct_init,
17238 &is_non_constant_init);
17239 if (!member_p && processing_template_decl)
17240 finish_lambda_scope ();
17241 if (initializer == error_mark_node)
17242 cp_parser_skip_to_end_of_statement (parser);
17246 /* The old parser allows attributes to appear after a parenthesized
17247 initializer. Mark Mitchell proposed removing this functionality
17248 on the GCC mailing lists on 2002-08-13. This parser accepts the
17249 attributes -- but ignores them. */
17250 if (cp_parser_allow_gnu_extensions_p (parser)
17251 && initialization_kind == CPP_OPEN_PAREN)
17252 if (cp_parser_attributes_opt (parser))
17253 warning (OPT_Wattributes,
17254 "attributes after parenthesized initializer ignored");
17256 /* And now complain about a non-function implicit template. */
17257 if (bogus_implicit_tmpl)
17258 error_at (DECL_SOURCE_LOCATION (decl),
17259 "non-function %qD declared as implicit template", decl);
17261 /* For an in-class declaration, use `grokfield' to create the
17262 declaration. */
17263 if (member_p)
17265 if (pushed_scope)
17267 pop_scope (pushed_scope);
17268 pushed_scope = NULL_TREE;
17270 decl = grokfield (declarator, decl_specifiers,
17271 initializer, !is_non_constant_init,
17272 /*asmspec=*/NULL_TREE,
17273 chainon (attributes, prefix_attributes));
17274 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17275 cp_parser_save_default_args (parser, decl);
17276 cp_finalize_omp_declare_simd (parser, decl);
17279 /* Finish processing the declaration. But, skip member
17280 declarations. */
17281 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17283 cp_finish_decl (decl,
17284 initializer, !is_non_constant_init,
17285 asm_specification,
17286 /* If the initializer is in parentheses, then this is
17287 a direct-initialization, which means that an
17288 `explicit' constructor is OK. Otherwise, an
17289 `explicit' constructor cannot be used. */
17290 ((is_direct_init || !is_initialized)
17291 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17293 else if ((cxx_dialect != cxx98) && friend_p
17294 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17295 /* Core issue #226 (C++0x only): A default template-argument
17296 shall not be specified in a friend class template
17297 declaration. */
17298 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17299 /*is_partial=*/false, /*is_friend_decl=*/1);
17301 if (!friend_p && pushed_scope)
17302 pop_scope (pushed_scope);
17304 if (function_declarator_p (declarator)
17305 && parser->fully_implicit_function_template_p)
17307 if (member_p)
17308 decl = finish_fully_implicit_template (parser, decl);
17309 else
17310 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17313 return decl;
17316 /* Parse a declarator.
17318 declarator:
17319 direct-declarator
17320 ptr-operator declarator
17322 abstract-declarator:
17323 ptr-operator abstract-declarator [opt]
17324 direct-abstract-declarator
17326 GNU Extensions:
17328 declarator:
17329 attributes [opt] direct-declarator
17330 attributes [opt] ptr-operator declarator
17332 abstract-declarator:
17333 attributes [opt] ptr-operator abstract-declarator [opt]
17334 attributes [opt] direct-abstract-declarator
17336 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17337 detect constructor, destructor or conversion operators. It is set
17338 to -1 if the declarator is a name, and +1 if it is a
17339 function. Otherwise it is set to zero. Usually you just want to
17340 test for >0, but internally the negative value is used.
17342 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17343 a decl-specifier-seq unless it declares a constructor, destructor,
17344 or conversion. It might seem that we could check this condition in
17345 semantic analysis, rather than parsing, but that makes it difficult
17346 to handle something like `f()'. We want to notice that there are
17347 no decl-specifiers, and therefore realize that this is an
17348 expression, not a declaration.)
17350 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17351 the declarator is a direct-declarator of the form "(...)".
17353 MEMBER_P is true iff this declarator is a member-declarator.
17355 FRIEND_P is true iff this declarator is a friend. */
17357 static cp_declarator *
17358 cp_parser_declarator (cp_parser* parser,
17359 cp_parser_declarator_kind dcl_kind,
17360 int* ctor_dtor_or_conv_p,
17361 bool* parenthesized_p,
17362 bool member_p, bool friend_p)
17364 cp_declarator *declarator;
17365 enum tree_code code;
17366 cp_cv_quals cv_quals;
17367 tree class_type;
17368 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17370 /* Assume this is not a constructor, destructor, or type-conversion
17371 operator. */
17372 if (ctor_dtor_or_conv_p)
17373 *ctor_dtor_or_conv_p = 0;
17375 if (cp_parser_allow_gnu_extensions_p (parser))
17376 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17378 /* Check for the ptr-operator production. */
17379 cp_parser_parse_tentatively (parser);
17380 /* Parse the ptr-operator. */
17381 code = cp_parser_ptr_operator (parser,
17382 &class_type,
17383 &cv_quals,
17384 &std_attributes);
17386 /* If that worked, then we have a ptr-operator. */
17387 if (cp_parser_parse_definitely (parser))
17389 /* If a ptr-operator was found, then this declarator was not
17390 parenthesized. */
17391 if (parenthesized_p)
17392 *parenthesized_p = true;
17393 /* The dependent declarator is optional if we are parsing an
17394 abstract-declarator. */
17395 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17396 cp_parser_parse_tentatively (parser);
17398 /* Parse the dependent declarator. */
17399 declarator = cp_parser_declarator (parser, dcl_kind,
17400 /*ctor_dtor_or_conv_p=*/NULL,
17401 /*parenthesized_p=*/NULL,
17402 /*member_p=*/false,
17403 friend_p);
17405 /* If we are parsing an abstract-declarator, we must handle the
17406 case where the dependent declarator is absent. */
17407 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17408 && !cp_parser_parse_definitely (parser))
17409 declarator = NULL;
17411 declarator = cp_parser_make_indirect_declarator
17412 (code, class_type, cv_quals, declarator, std_attributes);
17414 /* Everything else is a direct-declarator. */
17415 else
17417 if (parenthesized_p)
17418 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17419 CPP_OPEN_PAREN);
17420 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17421 ctor_dtor_or_conv_p,
17422 member_p, friend_p);
17425 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17426 declarator->attributes = gnu_attributes;
17427 return declarator;
17430 /* Parse a direct-declarator or direct-abstract-declarator.
17432 direct-declarator:
17433 declarator-id
17434 direct-declarator ( parameter-declaration-clause )
17435 cv-qualifier-seq [opt]
17436 ref-qualifier [opt]
17437 exception-specification [opt]
17438 direct-declarator [ constant-expression [opt] ]
17439 ( declarator )
17441 direct-abstract-declarator:
17442 direct-abstract-declarator [opt]
17443 ( parameter-declaration-clause )
17444 cv-qualifier-seq [opt]
17445 ref-qualifier [opt]
17446 exception-specification [opt]
17447 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17448 ( abstract-declarator )
17450 Returns a representation of the declarator. DCL_KIND is
17451 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17452 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17453 we are parsing a direct-declarator. It is
17454 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17455 of ambiguity we prefer an abstract declarator, as per
17456 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17457 as for cp_parser_declarator. */
17459 static cp_declarator *
17460 cp_parser_direct_declarator (cp_parser* parser,
17461 cp_parser_declarator_kind dcl_kind,
17462 int* ctor_dtor_or_conv_p,
17463 bool member_p, bool friend_p)
17465 cp_token *token;
17466 cp_declarator *declarator = NULL;
17467 tree scope = NULL_TREE;
17468 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17469 bool saved_in_declarator_p = parser->in_declarator_p;
17470 bool first = true;
17471 tree pushed_scope = NULL_TREE;
17473 while (true)
17475 /* Peek at the next token. */
17476 token = cp_lexer_peek_token (parser->lexer);
17477 if (token->type == CPP_OPEN_PAREN)
17479 /* This is either a parameter-declaration-clause, or a
17480 parenthesized declarator. When we know we are parsing a
17481 named declarator, it must be a parenthesized declarator
17482 if FIRST is true. For instance, `(int)' is a
17483 parameter-declaration-clause, with an omitted
17484 direct-abstract-declarator. But `((*))', is a
17485 parenthesized abstract declarator. Finally, when T is a
17486 template parameter `(T)' is a
17487 parameter-declaration-clause, and not a parenthesized
17488 named declarator.
17490 We first try and parse a parameter-declaration-clause,
17491 and then try a nested declarator (if FIRST is true).
17493 It is not an error for it not to be a
17494 parameter-declaration-clause, even when FIRST is
17495 false. Consider,
17497 int i (int);
17498 int i (3);
17500 The first is the declaration of a function while the
17501 second is the definition of a variable, including its
17502 initializer.
17504 Having seen only the parenthesis, we cannot know which of
17505 these two alternatives should be selected. Even more
17506 complex are examples like:
17508 int i (int (a));
17509 int i (int (3));
17511 The former is a function-declaration; the latter is a
17512 variable initialization.
17514 Thus again, we try a parameter-declaration-clause, and if
17515 that fails, we back out and return. */
17517 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17519 tree params;
17520 bool is_declarator = false;
17522 /* In a member-declarator, the only valid interpretation
17523 of a parenthesis is the start of a
17524 parameter-declaration-clause. (It is invalid to
17525 initialize a static data member with a parenthesized
17526 initializer; only the "=" form of initialization is
17527 permitted.) */
17528 if (!member_p)
17529 cp_parser_parse_tentatively (parser);
17531 /* Consume the `('. */
17532 cp_lexer_consume_token (parser->lexer);
17533 if (first)
17535 /* If this is going to be an abstract declarator, we're
17536 in a declarator and we can't have default args. */
17537 parser->default_arg_ok_p = false;
17538 parser->in_declarator_p = true;
17541 begin_scope (sk_function_parms, NULL_TREE);
17543 /* Parse the parameter-declaration-clause. */
17544 params = cp_parser_parameter_declaration_clause (parser);
17546 /* Consume the `)'. */
17547 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17549 /* If all went well, parse the cv-qualifier-seq,
17550 ref-qualifier and the exception-specification. */
17551 if (member_p || cp_parser_parse_definitely (parser))
17553 cp_cv_quals cv_quals;
17554 cp_virt_specifiers virt_specifiers;
17555 cp_ref_qualifier ref_qual;
17556 tree exception_specification;
17557 tree late_return;
17558 tree attrs;
17559 bool memfn = (member_p || (pushed_scope
17560 && CLASS_TYPE_P (pushed_scope)));
17562 is_declarator = true;
17564 if (ctor_dtor_or_conv_p)
17565 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17566 first = false;
17568 /* Parse the cv-qualifier-seq. */
17569 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17570 /* Parse the ref-qualifier. */
17571 ref_qual = cp_parser_ref_qualifier_opt (parser);
17572 /* And the exception-specification. */
17573 exception_specification
17574 = cp_parser_exception_specification_opt (parser);
17576 attrs = cp_parser_std_attribute_spec_seq (parser);
17578 /* In here, we handle cases where attribute is used after
17579 the function declaration. For example:
17580 void func (int x) __attribute__((vector(..))); */
17581 if (flag_cilkplus
17582 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17584 cp_parser_parse_tentatively (parser);
17585 tree attr = cp_parser_gnu_attributes_opt (parser);
17586 if (cp_lexer_next_token_is_not (parser->lexer,
17587 CPP_SEMICOLON)
17588 && cp_lexer_next_token_is_not (parser->lexer,
17589 CPP_OPEN_BRACE))
17590 cp_parser_abort_tentative_parse (parser);
17591 else if (!cp_parser_parse_definitely (parser))
17593 else
17594 attrs = chainon (attr, attrs);
17596 late_return = (cp_parser_late_return_type_opt
17597 (parser, declarator,
17598 memfn ? cv_quals : -1));
17601 /* Parse the virt-specifier-seq. */
17602 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17604 /* Create the function-declarator. */
17605 declarator = make_call_declarator (declarator,
17606 params,
17607 cv_quals,
17608 virt_specifiers,
17609 ref_qual,
17610 exception_specification,
17611 late_return);
17612 declarator->std_attributes = attrs;
17613 /* Any subsequent parameter lists are to do with
17614 return type, so are not those of the declared
17615 function. */
17616 parser->default_arg_ok_p = false;
17619 /* Remove the function parms from scope. */
17620 pop_bindings_and_leave_scope ();
17622 if (is_declarator)
17623 /* Repeat the main loop. */
17624 continue;
17627 /* If this is the first, we can try a parenthesized
17628 declarator. */
17629 if (first)
17631 bool saved_in_type_id_in_expr_p;
17633 parser->default_arg_ok_p = saved_default_arg_ok_p;
17634 parser->in_declarator_p = saved_in_declarator_p;
17636 /* Consume the `('. */
17637 cp_lexer_consume_token (parser->lexer);
17638 /* Parse the nested declarator. */
17639 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17640 parser->in_type_id_in_expr_p = true;
17641 declarator
17642 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17643 /*parenthesized_p=*/NULL,
17644 member_p, friend_p);
17645 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17646 first = false;
17647 /* Expect a `)'. */
17648 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17649 declarator = cp_error_declarator;
17650 if (declarator == cp_error_declarator)
17651 break;
17653 goto handle_declarator;
17655 /* Otherwise, we must be done. */
17656 else
17657 break;
17659 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17660 && token->type == CPP_OPEN_SQUARE
17661 && !cp_next_tokens_can_be_attribute_p (parser))
17663 /* Parse an array-declarator. */
17664 tree bounds, attrs;
17666 if (ctor_dtor_or_conv_p)
17667 *ctor_dtor_or_conv_p = 0;
17669 first = false;
17670 parser->default_arg_ok_p = false;
17671 parser->in_declarator_p = true;
17672 /* Consume the `['. */
17673 cp_lexer_consume_token (parser->lexer);
17674 /* Peek at the next token. */
17675 token = cp_lexer_peek_token (parser->lexer);
17676 /* If the next token is `]', then there is no
17677 constant-expression. */
17678 if (token->type != CPP_CLOSE_SQUARE)
17680 bool non_constant_p;
17681 bounds
17682 = cp_parser_constant_expression (parser,
17683 /*allow_non_constant=*/true,
17684 &non_constant_p);
17685 if (!non_constant_p)
17686 /* OK */;
17687 else if (error_operand_p (bounds))
17688 /* Already gave an error. */;
17689 else if (!parser->in_function_body
17690 || current_binding_level->kind == sk_function_parms)
17692 /* Normally, the array bound must be an integral constant
17693 expression. However, as an extension, we allow VLAs
17694 in function scopes as long as they aren't part of a
17695 parameter declaration. */
17696 cp_parser_error (parser,
17697 "array bound is not an integer constant");
17698 bounds = error_mark_node;
17700 else if (processing_template_decl
17701 && !type_dependent_expression_p (bounds))
17703 /* Remember this wasn't a constant-expression. */
17704 bounds = build_nop (TREE_TYPE (bounds), bounds);
17705 TREE_SIDE_EFFECTS (bounds) = 1;
17708 else
17709 bounds = NULL_TREE;
17710 /* Look for the closing `]'. */
17711 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17713 declarator = cp_error_declarator;
17714 break;
17717 attrs = cp_parser_std_attribute_spec_seq (parser);
17718 declarator = make_array_declarator (declarator, bounds);
17719 declarator->std_attributes = attrs;
17721 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17724 tree qualifying_scope;
17725 tree unqualified_name;
17726 tree attrs;
17727 special_function_kind sfk;
17728 bool abstract_ok;
17729 bool pack_expansion_p = false;
17730 cp_token *declarator_id_start_token;
17732 /* Parse a declarator-id */
17733 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17734 if (abstract_ok)
17736 cp_parser_parse_tentatively (parser);
17738 /* If we see an ellipsis, we should be looking at a
17739 parameter pack. */
17740 if (token->type == CPP_ELLIPSIS)
17742 /* Consume the `...' */
17743 cp_lexer_consume_token (parser->lexer);
17745 pack_expansion_p = true;
17749 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17750 unqualified_name
17751 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17752 qualifying_scope = parser->scope;
17753 if (abstract_ok)
17755 bool okay = false;
17757 if (!unqualified_name && pack_expansion_p)
17759 /* Check whether an error occurred. */
17760 okay = !cp_parser_error_occurred (parser);
17762 /* We already consumed the ellipsis to mark a
17763 parameter pack, but we have no way to report it,
17764 so abort the tentative parse. We will be exiting
17765 immediately anyway. */
17766 cp_parser_abort_tentative_parse (parser);
17768 else
17769 okay = cp_parser_parse_definitely (parser);
17771 if (!okay)
17772 unqualified_name = error_mark_node;
17773 else if (unqualified_name
17774 && (qualifying_scope
17775 || (!identifier_p (unqualified_name))))
17777 cp_parser_error (parser, "expected unqualified-id");
17778 unqualified_name = error_mark_node;
17782 if (!unqualified_name)
17783 return NULL;
17784 if (unqualified_name == error_mark_node)
17786 declarator = cp_error_declarator;
17787 pack_expansion_p = false;
17788 declarator->parameter_pack_p = false;
17789 break;
17792 attrs = cp_parser_std_attribute_spec_seq (parser);
17794 if (qualifying_scope && at_namespace_scope_p ()
17795 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17797 /* In the declaration of a member of a template class
17798 outside of the class itself, the SCOPE will sometimes
17799 be a TYPENAME_TYPE. For example, given:
17801 template <typename T>
17802 int S<T>::R::i = 3;
17804 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17805 this context, we must resolve S<T>::R to an ordinary
17806 type, rather than a typename type.
17808 The reason we normally avoid resolving TYPENAME_TYPEs
17809 is that a specialization of `S' might render
17810 `S<T>::R' not a type. However, if `S' is
17811 specialized, then this `i' will not be used, so there
17812 is no harm in resolving the types here. */
17813 tree type;
17815 /* Resolve the TYPENAME_TYPE. */
17816 type = resolve_typename_type (qualifying_scope,
17817 /*only_current_p=*/false);
17818 /* If that failed, the declarator is invalid. */
17819 if (TREE_CODE (type) == TYPENAME_TYPE)
17821 if (typedef_variant_p (type))
17822 error_at (declarator_id_start_token->location,
17823 "cannot define member of dependent typedef "
17824 "%qT", type);
17825 else
17826 error_at (declarator_id_start_token->location,
17827 "%<%T::%E%> is not a type",
17828 TYPE_CONTEXT (qualifying_scope),
17829 TYPE_IDENTIFIER (qualifying_scope));
17831 qualifying_scope = type;
17834 sfk = sfk_none;
17836 if (unqualified_name)
17838 tree class_type;
17840 if (qualifying_scope
17841 && CLASS_TYPE_P (qualifying_scope))
17842 class_type = qualifying_scope;
17843 else
17844 class_type = current_class_type;
17846 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17848 tree name_type = TREE_TYPE (unqualified_name);
17849 if (class_type && same_type_p (name_type, class_type))
17851 if (qualifying_scope
17852 && CLASSTYPE_USE_TEMPLATE (name_type))
17854 error_at (declarator_id_start_token->location,
17855 "invalid use of constructor as a template");
17856 inform (declarator_id_start_token->location,
17857 "use %<%T::%D%> instead of %<%T::%D%> to "
17858 "name the constructor in a qualified name",
17859 class_type,
17860 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17861 class_type, name_type);
17862 declarator = cp_error_declarator;
17863 break;
17865 else
17866 unqualified_name = constructor_name (class_type);
17868 else
17870 /* We do not attempt to print the declarator
17871 here because we do not have enough
17872 information about its original syntactic
17873 form. */
17874 cp_parser_error (parser, "invalid declarator");
17875 declarator = cp_error_declarator;
17876 break;
17880 if (class_type)
17882 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17883 sfk = sfk_destructor;
17884 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17885 sfk = sfk_conversion;
17886 else if (/* There's no way to declare a constructor
17887 for an anonymous type, even if the type
17888 got a name for linkage purposes. */
17889 !TYPE_WAS_ANONYMOUS (class_type)
17890 /* Handle correctly (c++/19200):
17892 struct S {
17893 struct T{};
17894 friend void S(T);
17897 and also:
17899 namespace N {
17900 void S();
17903 struct S {
17904 friend void N::S();
17905 }; */
17906 && !(friend_p
17907 && class_type != qualifying_scope)
17908 && constructor_name_p (unqualified_name,
17909 class_type))
17911 unqualified_name = constructor_name (class_type);
17912 sfk = sfk_constructor;
17914 else if (is_overloaded_fn (unqualified_name)
17915 && DECL_CONSTRUCTOR_P (get_first_fn
17916 (unqualified_name)))
17917 sfk = sfk_constructor;
17919 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17920 *ctor_dtor_or_conv_p = -1;
17923 declarator = make_id_declarator (qualifying_scope,
17924 unqualified_name,
17925 sfk);
17926 declarator->std_attributes = attrs;
17927 declarator->id_loc = token->location;
17928 declarator->parameter_pack_p = pack_expansion_p;
17930 if (pack_expansion_p)
17931 maybe_warn_variadic_templates ();
17934 handle_declarator:;
17935 scope = get_scope_of_declarator (declarator);
17936 if (scope)
17938 /* Any names that appear after the declarator-id for a
17939 member are looked up in the containing scope. */
17940 if (at_function_scope_p ())
17942 /* But declarations with qualified-ids can't appear in a
17943 function. */
17944 cp_parser_error (parser, "qualified-id in declaration");
17945 declarator = cp_error_declarator;
17946 break;
17948 pushed_scope = push_scope (scope);
17950 parser->in_declarator_p = true;
17951 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17952 || (declarator && declarator->kind == cdk_id))
17953 /* Default args are only allowed on function
17954 declarations. */
17955 parser->default_arg_ok_p = saved_default_arg_ok_p;
17956 else
17957 parser->default_arg_ok_p = false;
17959 first = false;
17961 /* We're done. */
17962 else
17963 break;
17966 /* For an abstract declarator, we might wind up with nothing at this
17967 point. That's an error; the declarator is not optional. */
17968 if (!declarator)
17969 cp_parser_error (parser, "expected declarator");
17971 /* If we entered a scope, we must exit it now. */
17972 if (pushed_scope)
17973 pop_scope (pushed_scope);
17975 parser->default_arg_ok_p = saved_default_arg_ok_p;
17976 parser->in_declarator_p = saved_in_declarator_p;
17978 return declarator;
17981 /* Parse a ptr-operator.
17983 ptr-operator:
17984 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17985 * cv-qualifier-seq [opt]
17987 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17988 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17990 GNU Extension:
17992 ptr-operator:
17993 & cv-qualifier-seq [opt]
17995 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17996 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17997 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17998 filled in with the TYPE containing the member. *CV_QUALS is
17999 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18000 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18001 Note that the tree codes returned by this function have nothing
18002 to do with the types of trees that will be eventually be created
18003 to represent the pointer or reference type being parsed. They are
18004 just constants with suggestive names. */
18005 static enum tree_code
18006 cp_parser_ptr_operator (cp_parser* parser,
18007 tree* type,
18008 cp_cv_quals *cv_quals,
18009 tree *attributes)
18011 enum tree_code code = ERROR_MARK;
18012 cp_token *token;
18013 tree attrs = NULL_TREE;
18015 /* Assume that it's not a pointer-to-member. */
18016 *type = NULL_TREE;
18017 /* And that there are no cv-qualifiers. */
18018 *cv_quals = TYPE_UNQUALIFIED;
18020 /* Peek at the next token. */
18021 token = cp_lexer_peek_token (parser->lexer);
18023 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18024 if (token->type == CPP_MULT)
18025 code = INDIRECT_REF;
18026 else if (token->type == CPP_AND)
18027 code = ADDR_EXPR;
18028 else if ((cxx_dialect != cxx98) &&
18029 token->type == CPP_AND_AND) /* C++0x only */
18030 code = NON_LVALUE_EXPR;
18032 if (code != ERROR_MARK)
18034 /* Consume the `*', `&' or `&&'. */
18035 cp_lexer_consume_token (parser->lexer);
18037 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18038 `&', if we are allowing GNU extensions. (The only qualifier
18039 that can legally appear after `&' is `restrict', but that is
18040 enforced during semantic analysis. */
18041 if (code == INDIRECT_REF
18042 || cp_parser_allow_gnu_extensions_p (parser))
18043 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18045 attrs = cp_parser_std_attribute_spec_seq (parser);
18046 if (attributes != NULL)
18047 *attributes = attrs;
18049 else
18051 /* Try the pointer-to-member case. */
18052 cp_parser_parse_tentatively (parser);
18053 /* Look for the optional `::' operator. */
18054 cp_parser_global_scope_opt (parser,
18055 /*current_scope_valid_p=*/false);
18056 /* Look for the nested-name specifier. */
18057 token = cp_lexer_peek_token (parser->lexer);
18058 cp_parser_nested_name_specifier (parser,
18059 /*typename_keyword_p=*/false,
18060 /*check_dependency_p=*/true,
18061 /*type_p=*/false,
18062 /*is_declaration=*/false);
18063 /* If we found it, and the next token is a `*', then we are
18064 indeed looking at a pointer-to-member operator. */
18065 if (!cp_parser_error_occurred (parser)
18066 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18068 /* Indicate that the `*' operator was used. */
18069 code = INDIRECT_REF;
18071 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18072 error_at (token->location, "%qD is a namespace", parser->scope);
18073 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18074 error_at (token->location, "cannot form pointer to member of "
18075 "non-class %q#T", parser->scope);
18076 else
18078 /* The type of which the member is a member is given by the
18079 current SCOPE. */
18080 *type = parser->scope;
18081 /* The next name will not be qualified. */
18082 parser->scope = NULL_TREE;
18083 parser->qualifying_scope = NULL_TREE;
18084 parser->object_scope = NULL_TREE;
18085 /* Look for optional c++11 attributes. */
18086 attrs = cp_parser_std_attribute_spec_seq (parser);
18087 if (attributes != NULL)
18088 *attributes = attrs;
18089 /* Look for the optional cv-qualifier-seq. */
18090 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18093 /* If that didn't work we don't have a ptr-operator. */
18094 if (!cp_parser_parse_definitely (parser))
18095 cp_parser_error (parser, "expected ptr-operator");
18098 return code;
18101 /* Parse an (optional) cv-qualifier-seq.
18103 cv-qualifier-seq:
18104 cv-qualifier cv-qualifier-seq [opt]
18106 cv-qualifier:
18107 const
18108 volatile
18110 GNU Extension:
18112 cv-qualifier:
18113 __restrict__
18115 Returns a bitmask representing the cv-qualifiers. */
18117 static cp_cv_quals
18118 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18120 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18122 while (true)
18124 cp_token *token;
18125 cp_cv_quals cv_qualifier;
18127 /* Peek at the next token. */
18128 token = cp_lexer_peek_token (parser->lexer);
18129 /* See if it's a cv-qualifier. */
18130 switch (token->keyword)
18132 case RID_CONST:
18133 cv_qualifier = TYPE_QUAL_CONST;
18134 break;
18136 case RID_VOLATILE:
18137 cv_qualifier = TYPE_QUAL_VOLATILE;
18138 break;
18140 case RID_RESTRICT:
18141 cv_qualifier = TYPE_QUAL_RESTRICT;
18142 break;
18144 default:
18145 cv_qualifier = TYPE_UNQUALIFIED;
18146 break;
18149 if (!cv_qualifier)
18150 break;
18152 if (cv_quals & cv_qualifier)
18154 error_at (token->location, "duplicate cv-qualifier");
18155 cp_lexer_purge_token (parser->lexer);
18157 else
18159 cp_lexer_consume_token (parser->lexer);
18160 cv_quals |= cv_qualifier;
18164 return cv_quals;
18167 /* Parse an (optional) ref-qualifier
18169 ref-qualifier:
18173 Returns cp_ref_qualifier representing ref-qualifier. */
18175 static cp_ref_qualifier
18176 cp_parser_ref_qualifier_opt (cp_parser* parser)
18178 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18180 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18181 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18182 return ref_qual;
18184 while (true)
18186 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18187 cp_token *token = cp_lexer_peek_token (parser->lexer);
18189 switch (token->type)
18191 case CPP_AND:
18192 curr_ref_qual = REF_QUAL_LVALUE;
18193 break;
18195 case CPP_AND_AND:
18196 curr_ref_qual = REF_QUAL_RVALUE;
18197 break;
18199 default:
18200 curr_ref_qual = REF_QUAL_NONE;
18201 break;
18204 if (!curr_ref_qual)
18205 break;
18206 else if (ref_qual)
18208 error_at (token->location, "multiple ref-qualifiers");
18209 cp_lexer_purge_token (parser->lexer);
18211 else
18213 ref_qual = curr_ref_qual;
18214 cp_lexer_consume_token (parser->lexer);
18218 return ref_qual;
18221 /* Parse an (optional) virt-specifier-seq.
18223 virt-specifier-seq:
18224 virt-specifier virt-specifier-seq [opt]
18226 virt-specifier:
18227 override
18228 final
18230 Returns a bitmask representing the virt-specifiers. */
18232 static cp_virt_specifiers
18233 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18235 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18237 while (true)
18239 cp_token *token;
18240 cp_virt_specifiers virt_specifier;
18242 /* Peek at the next token. */
18243 token = cp_lexer_peek_token (parser->lexer);
18244 /* See if it's a virt-specifier-qualifier. */
18245 if (token->type != CPP_NAME)
18246 break;
18247 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18249 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18250 virt_specifier = VIRT_SPEC_OVERRIDE;
18252 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18254 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18255 virt_specifier = VIRT_SPEC_FINAL;
18257 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18259 virt_specifier = VIRT_SPEC_FINAL;
18261 else
18262 break;
18264 if (virt_specifiers & virt_specifier)
18266 error_at (token->location, "duplicate virt-specifier");
18267 cp_lexer_purge_token (parser->lexer);
18269 else
18271 cp_lexer_consume_token (parser->lexer);
18272 virt_specifiers |= virt_specifier;
18275 return virt_specifiers;
18278 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18279 is in scope even though it isn't real. */
18281 void
18282 inject_this_parameter (tree ctype, cp_cv_quals quals)
18284 tree this_parm;
18286 if (current_class_ptr)
18288 /* We don't clear this between NSDMIs. Is it already what we want? */
18289 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18290 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18291 && cp_type_quals (type) == quals)
18292 return;
18295 this_parm = build_this_parm (ctype, quals);
18296 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18297 current_class_ptr = NULL_TREE;
18298 current_class_ref
18299 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18300 current_class_ptr = this_parm;
18303 /* Return true iff our current scope is a non-static data member
18304 initializer. */
18306 bool
18307 parsing_nsdmi (void)
18309 /* We recognize NSDMI context by the context-less 'this' pointer set up
18310 by the function above. */
18311 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18312 return true;
18313 return false;
18316 /* Parse a late-specified return type, if any. This is not a separate
18317 non-terminal, but part of a function declarator, which looks like
18319 -> trailing-type-specifier-seq abstract-declarator(opt)
18321 Returns the type indicated by the type-id.
18323 In addition to this this parses any queued up omp declare simd
18324 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18326 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18327 function. */
18329 static tree
18330 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18331 cp_cv_quals quals)
18333 cp_token *token;
18334 tree type = NULL_TREE;
18335 bool declare_simd_p = (parser->omp_declare_simd
18336 && declarator
18337 && declarator->kind == cdk_id);
18339 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18340 && declarator && declarator->kind == cdk_id);
18342 /* Peek at the next token. */
18343 token = cp_lexer_peek_token (parser->lexer);
18344 /* A late-specified return type is indicated by an initial '->'. */
18345 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18346 return NULL_TREE;
18348 tree save_ccp = current_class_ptr;
18349 tree save_ccr = current_class_ref;
18350 if (quals >= 0)
18352 /* DR 1207: 'this' is in scope in the trailing return type. */
18353 inject_this_parameter (current_class_type, quals);
18356 if (token->type == CPP_DEREF)
18358 /* Consume the ->. */
18359 cp_lexer_consume_token (parser->lexer);
18361 type = cp_parser_trailing_type_id (parser);
18364 if (cilk_simd_fn_vector_p)
18365 declarator->std_attributes
18366 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18367 declarator->std_attributes);
18368 if (declare_simd_p)
18369 declarator->std_attributes
18370 = cp_parser_late_parsing_omp_declare_simd (parser,
18371 declarator->std_attributes);
18373 if (quals >= 0)
18375 current_class_ptr = save_ccp;
18376 current_class_ref = save_ccr;
18379 return type;
18382 /* Parse a declarator-id.
18384 declarator-id:
18385 id-expression
18386 :: [opt] nested-name-specifier [opt] type-name
18388 In the `id-expression' case, the value returned is as for
18389 cp_parser_id_expression if the id-expression was an unqualified-id.
18390 If the id-expression was a qualified-id, then a SCOPE_REF is
18391 returned. The first operand is the scope (either a NAMESPACE_DECL
18392 or TREE_TYPE), but the second is still just a representation of an
18393 unqualified-id. */
18395 static tree
18396 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18398 tree id;
18399 /* The expression must be an id-expression. Assume that qualified
18400 names are the names of types so that:
18402 template <class T>
18403 int S<T>::R::i = 3;
18405 will work; we must treat `S<T>::R' as the name of a type.
18406 Similarly, assume that qualified names are templates, where
18407 required, so that:
18409 template <class T>
18410 int S<T>::R<T>::i = 3;
18412 will work, too. */
18413 id = cp_parser_id_expression (parser,
18414 /*template_keyword_p=*/false,
18415 /*check_dependency_p=*/false,
18416 /*template_p=*/NULL,
18417 /*declarator_p=*/true,
18418 optional_p);
18419 if (id && BASELINK_P (id))
18420 id = BASELINK_FUNCTIONS (id);
18421 return id;
18424 /* Parse a type-id.
18426 type-id:
18427 type-specifier-seq abstract-declarator [opt]
18429 Returns the TYPE specified. */
18431 static tree
18432 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18433 bool is_trailing_return)
18435 cp_decl_specifier_seq type_specifier_seq;
18436 cp_declarator *abstract_declarator;
18438 /* Parse the type-specifier-seq. */
18439 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18440 is_trailing_return,
18441 &type_specifier_seq);
18442 if (type_specifier_seq.type == error_mark_node)
18443 return error_mark_node;
18445 /* There might or might not be an abstract declarator. */
18446 cp_parser_parse_tentatively (parser);
18447 /* Look for the declarator. */
18448 abstract_declarator
18449 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18450 /*parenthesized_p=*/NULL,
18451 /*member_p=*/false,
18452 /*friend_p=*/false);
18453 /* Check to see if there really was a declarator. */
18454 if (!cp_parser_parse_definitely (parser))
18455 abstract_declarator = NULL;
18457 if (type_specifier_seq.type
18458 /* None of the valid uses of 'auto' in C++14 involve the type-id
18459 nonterminal, but it is valid in a trailing-return-type. */
18460 && !(cxx_dialect >= cxx14 && is_trailing_return)
18461 && type_uses_auto (type_specifier_seq.type))
18463 /* A type-id with type 'auto' is only ok if the abstract declarator
18464 is a function declarator with a late-specified return type. */
18465 if (abstract_declarator
18466 && abstract_declarator->kind == cdk_function
18467 && abstract_declarator->u.function.late_return_type)
18468 /* OK */;
18469 else
18471 error ("invalid use of %<auto%>");
18472 return error_mark_node;
18476 return groktypename (&type_specifier_seq, abstract_declarator,
18477 is_template_arg);
18480 static tree cp_parser_type_id (cp_parser *parser)
18482 return cp_parser_type_id_1 (parser, false, false);
18485 static tree cp_parser_template_type_arg (cp_parser *parser)
18487 tree r;
18488 const char *saved_message = parser->type_definition_forbidden_message;
18489 parser->type_definition_forbidden_message
18490 = G_("types may not be defined in template arguments");
18491 r = cp_parser_type_id_1 (parser, true, false);
18492 parser->type_definition_forbidden_message = saved_message;
18493 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18495 error ("invalid use of %<auto%> in template argument");
18496 r = error_mark_node;
18498 return r;
18501 static tree cp_parser_trailing_type_id (cp_parser *parser)
18503 return cp_parser_type_id_1 (parser, false, true);
18506 /* Parse a type-specifier-seq.
18508 type-specifier-seq:
18509 type-specifier type-specifier-seq [opt]
18511 GNU extension:
18513 type-specifier-seq:
18514 attributes type-specifier-seq [opt]
18516 If IS_DECLARATION is true, we are at the start of a "condition" or
18517 exception-declaration, so we might be followed by a declarator-id.
18519 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18520 i.e. we've just seen "->".
18522 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18524 static void
18525 cp_parser_type_specifier_seq (cp_parser* parser,
18526 bool is_declaration,
18527 bool is_trailing_return,
18528 cp_decl_specifier_seq *type_specifier_seq)
18530 bool seen_type_specifier = false;
18531 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18532 cp_token *start_token = NULL;
18534 /* Clear the TYPE_SPECIFIER_SEQ. */
18535 clear_decl_specs (type_specifier_seq);
18537 /* In the context of a trailing return type, enum E { } is an
18538 elaborated-type-specifier followed by a function-body, not an
18539 enum-specifier. */
18540 if (is_trailing_return)
18541 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18543 /* Parse the type-specifiers and attributes. */
18544 while (true)
18546 tree type_specifier;
18547 bool is_cv_qualifier;
18549 /* Check for attributes first. */
18550 if (cp_next_tokens_can_be_attribute_p (parser))
18552 type_specifier_seq->attributes =
18553 chainon (type_specifier_seq->attributes,
18554 cp_parser_attributes_opt (parser));
18555 continue;
18558 /* record the token of the beginning of the type specifier seq,
18559 for error reporting purposes*/
18560 if (!start_token)
18561 start_token = cp_lexer_peek_token (parser->lexer);
18563 /* Look for the type-specifier. */
18564 type_specifier = cp_parser_type_specifier (parser,
18565 flags,
18566 type_specifier_seq,
18567 /*is_declaration=*/false,
18568 NULL,
18569 &is_cv_qualifier);
18570 if (!type_specifier)
18572 /* If the first type-specifier could not be found, this is not a
18573 type-specifier-seq at all. */
18574 if (!seen_type_specifier)
18576 /* Set in_declarator_p to avoid skipping to the semicolon. */
18577 int in_decl = parser->in_declarator_p;
18578 parser->in_declarator_p = true;
18580 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18581 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18582 cp_parser_error (parser, "expected type-specifier");
18584 parser->in_declarator_p = in_decl;
18586 type_specifier_seq->type = error_mark_node;
18587 return;
18589 /* If subsequent type-specifiers could not be found, the
18590 type-specifier-seq is complete. */
18591 break;
18594 seen_type_specifier = true;
18595 /* The standard says that a condition can be:
18597 type-specifier-seq declarator = assignment-expression
18599 However, given:
18601 struct S {};
18602 if (int S = ...)
18604 we should treat the "S" as a declarator, not as a
18605 type-specifier. The standard doesn't say that explicitly for
18606 type-specifier-seq, but it does say that for
18607 decl-specifier-seq in an ordinary declaration. Perhaps it
18608 would be clearer just to allow a decl-specifier-seq here, and
18609 then add a semantic restriction that if any decl-specifiers
18610 that are not type-specifiers appear, the program is invalid. */
18611 if (is_declaration && !is_cv_qualifier)
18612 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18616 /* Return whether the function currently being declared has an associated
18617 template parameter list. */
18619 static bool
18620 function_being_declared_is_template_p (cp_parser* parser)
18622 if (!current_template_parms || processing_template_parmlist)
18623 return false;
18625 if (parser->implicit_template_scope)
18626 return true;
18628 if (at_class_scope_p ()
18629 && TYPE_BEING_DEFINED (current_class_type))
18630 return parser->num_template_parameter_lists != 0;
18632 return ((int) parser->num_template_parameter_lists > template_class_depth
18633 (current_class_type));
18636 /* Parse a parameter-declaration-clause.
18638 parameter-declaration-clause:
18639 parameter-declaration-list [opt] ... [opt]
18640 parameter-declaration-list , ...
18642 Returns a representation for the parameter declarations. A return
18643 value of NULL indicates a parameter-declaration-clause consisting
18644 only of an ellipsis. */
18646 static tree
18647 cp_parser_parameter_declaration_clause (cp_parser* parser)
18649 tree parameters;
18650 cp_token *token;
18651 bool ellipsis_p;
18652 bool is_error;
18654 struct cleanup {
18655 cp_parser* parser;
18656 int auto_is_implicit_function_template_parm_p;
18657 ~cleanup() {
18658 parser->auto_is_implicit_function_template_parm_p
18659 = auto_is_implicit_function_template_parm_p;
18661 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18663 (void) cleanup;
18665 if (!processing_specialization
18666 && !processing_template_parmlist
18667 && !processing_explicit_instantiation)
18668 if (!current_function_decl
18669 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18670 parser->auto_is_implicit_function_template_parm_p = true;
18672 /* Peek at the next token. */
18673 token = cp_lexer_peek_token (parser->lexer);
18674 /* Check for trivial parameter-declaration-clauses. */
18675 if (token->type == CPP_ELLIPSIS)
18677 /* Consume the `...' token. */
18678 cp_lexer_consume_token (parser->lexer);
18679 return NULL_TREE;
18681 else if (token->type == CPP_CLOSE_PAREN)
18682 /* There are no parameters. */
18684 #ifndef NO_IMPLICIT_EXTERN_C
18685 if (in_system_header_at (input_location)
18686 && current_class_type == NULL
18687 && current_lang_name == lang_name_c)
18688 return NULL_TREE;
18689 else
18690 #endif
18691 return void_list_node;
18693 /* Check for `(void)', too, which is a special case. */
18694 else if (token->keyword == RID_VOID
18695 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18696 == CPP_CLOSE_PAREN))
18698 /* Consume the `void' token. */
18699 cp_lexer_consume_token (parser->lexer);
18700 /* There are no parameters. */
18701 return void_list_node;
18704 /* Parse the parameter-declaration-list. */
18705 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18706 /* If a parse error occurred while parsing the
18707 parameter-declaration-list, then the entire
18708 parameter-declaration-clause is erroneous. */
18709 if (is_error)
18710 return NULL;
18712 /* Peek at the next token. */
18713 token = cp_lexer_peek_token (parser->lexer);
18714 /* If it's a `,', the clause should terminate with an ellipsis. */
18715 if (token->type == CPP_COMMA)
18717 /* Consume the `,'. */
18718 cp_lexer_consume_token (parser->lexer);
18719 /* Expect an ellipsis. */
18720 ellipsis_p
18721 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18723 /* It might also be `...' if the optional trailing `,' was
18724 omitted. */
18725 else if (token->type == CPP_ELLIPSIS)
18727 /* Consume the `...' token. */
18728 cp_lexer_consume_token (parser->lexer);
18729 /* And remember that we saw it. */
18730 ellipsis_p = true;
18732 else
18733 ellipsis_p = false;
18735 /* Finish the parameter list. */
18736 if (!ellipsis_p)
18737 parameters = chainon (parameters, void_list_node);
18739 return parameters;
18742 /* Parse a parameter-declaration-list.
18744 parameter-declaration-list:
18745 parameter-declaration
18746 parameter-declaration-list , parameter-declaration
18748 Returns a representation of the parameter-declaration-list, as for
18749 cp_parser_parameter_declaration_clause. However, the
18750 `void_list_node' is never appended to the list. Upon return,
18751 *IS_ERROR will be true iff an error occurred. */
18753 static tree
18754 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18756 tree parameters = NULL_TREE;
18757 tree *tail = &parameters;
18758 bool saved_in_unbraced_linkage_specification_p;
18759 int index = 0;
18761 /* Assume all will go well. */
18762 *is_error = false;
18763 /* The special considerations that apply to a function within an
18764 unbraced linkage specifications do not apply to the parameters
18765 to the function. */
18766 saved_in_unbraced_linkage_specification_p
18767 = parser->in_unbraced_linkage_specification_p;
18768 parser->in_unbraced_linkage_specification_p = false;
18770 /* Look for more parameters. */
18771 while (true)
18773 cp_parameter_declarator *parameter;
18774 tree decl = error_mark_node;
18775 bool parenthesized_p = false;
18776 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18777 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18778 (current_template_parms)) : 0);
18780 /* Parse the parameter. */
18781 parameter
18782 = cp_parser_parameter_declaration (parser,
18783 /*template_parm_p=*/false,
18784 &parenthesized_p);
18786 /* We don't know yet if the enclosing context is deprecated, so wait
18787 and warn in grokparms if appropriate. */
18788 deprecated_state = DEPRECATED_SUPPRESS;
18790 if (parameter)
18792 /* If a function parameter pack was specified and an implicit template
18793 parameter was introduced during cp_parser_parameter_declaration,
18794 change any implicit parameters introduced into packs. */
18795 if (parser->implicit_template_parms
18796 && parameter->declarator
18797 && parameter->declarator->parameter_pack_p)
18799 int latest_template_parm_idx = TREE_VEC_LENGTH
18800 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18802 if (latest_template_parm_idx != template_parm_idx)
18803 parameter->decl_specifiers.type = convert_generic_types_to_packs
18804 (parameter->decl_specifiers.type,
18805 template_parm_idx, latest_template_parm_idx);
18808 decl = grokdeclarator (parameter->declarator,
18809 &parameter->decl_specifiers,
18810 PARM,
18811 parameter->default_argument != NULL_TREE,
18812 &parameter->decl_specifiers.attributes);
18815 deprecated_state = DEPRECATED_NORMAL;
18817 /* If a parse error occurred parsing the parameter declaration,
18818 then the entire parameter-declaration-list is erroneous. */
18819 if (decl == error_mark_node)
18821 *is_error = true;
18822 parameters = error_mark_node;
18823 break;
18826 if (parameter->decl_specifiers.attributes)
18827 cplus_decl_attributes (&decl,
18828 parameter->decl_specifiers.attributes,
18830 if (DECL_NAME (decl))
18831 decl = pushdecl (decl);
18833 if (decl != error_mark_node)
18835 retrofit_lang_decl (decl);
18836 DECL_PARM_INDEX (decl) = ++index;
18837 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18840 /* Add the new parameter to the list. */
18841 *tail = build_tree_list (parameter->default_argument, decl);
18842 tail = &TREE_CHAIN (*tail);
18844 /* Peek at the next token. */
18845 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18846 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18847 /* These are for Objective-C++ */
18848 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18849 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18850 /* The parameter-declaration-list is complete. */
18851 break;
18852 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18854 cp_token *token;
18856 /* Peek at the next token. */
18857 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18858 /* If it's an ellipsis, then the list is complete. */
18859 if (token->type == CPP_ELLIPSIS)
18860 break;
18861 /* Otherwise, there must be more parameters. Consume the
18862 `,'. */
18863 cp_lexer_consume_token (parser->lexer);
18864 /* When parsing something like:
18866 int i(float f, double d)
18868 we can tell after seeing the declaration for "f" that we
18869 are not looking at an initialization of a variable "i",
18870 but rather at the declaration of a function "i".
18872 Due to the fact that the parsing of template arguments
18873 (as specified to a template-id) requires backtracking we
18874 cannot use this technique when inside a template argument
18875 list. */
18876 if (!parser->in_template_argument_list_p
18877 && !parser->in_type_id_in_expr_p
18878 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18879 /* However, a parameter-declaration of the form
18880 "float(f)" (which is a valid declaration of a
18881 parameter "f") can also be interpreted as an
18882 expression (the conversion of "f" to "float"). */
18883 && !parenthesized_p)
18884 cp_parser_commit_to_tentative_parse (parser);
18886 else
18888 cp_parser_error (parser, "expected %<,%> or %<...%>");
18889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18890 cp_parser_skip_to_closing_parenthesis (parser,
18891 /*recovering=*/true,
18892 /*or_comma=*/false,
18893 /*consume_paren=*/false);
18894 break;
18898 parser->in_unbraced_linkage_specification_p
18899 = saved_in_unbraced_linkage_specification_p;
18901 /* Reset implicit_template_scope if we are about to leave the function
18902 parameter list that introduced it. Note that for out-of-line member
18903 definitions, there will be one or more class scopes before we get to
18904 the template parameter scope. */
18906 if (cp_binding_level *its = parser->implicit_template_scope)
18907 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18909 while (maybe_its->kind == sk_class)
18910 maybe_its = maybe_its->level_chain;
18911 if (maybe_its == its)
18913 parser->implicit_template_parms = 0;
18914 parser->implicit_template_scope = 0;
18918 return parameters;
18921 /* Parse a parameter declaration.
18923 parameter-declaration:
18924 decl-specifier-seq ... [opt] declarator
18925 decl-specifier-seq declarator = assignment-expression
18926 decl-specifier-seq ... [opt] abstract-declarator [opt]
18927 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18929 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18930 declares a template parameter. (In that case, a non-nested `>'
18931 token encountered during the parsing of the assignment-expression
18932 is not interpreted as a greater-than operator.)
18934 Returns a representation of the parameter, or NULL if an error
18935 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18936 true iff the declarator is of the form "(p)". */
18938 static cp_parameter_declarator *
18939 cp_parser_parameter_declaration (cp_parser *parser,
18940 bool template_parm_p,
18941 bool *parenthesized_p)
18943 int declares_class_or_enum;
18944 cp_decl_specifier_seq decl_specifiers;
18945 cp_declarator *declarator;
18946 tree default_argument;
18947 cp_token *token = NULL, *declarator_token_start = NULL;
18948 const char *saved_message;
18950 /* In a template parameter, `>' is not an operator.
18952 [temp.param]
18954 When parsing a default template-argument for a non-type
18955 template-parameter, the first non-nested `>' is taken as the end
18956 of the template parameter-list rather than a greater-than
18957 operator. */
18959 /* Type definitions may not appear in parameter types. */
18960 saved_message = parser->type_definition_forbidden_message;
18961 parser->type_definition_forbidden_message
18962 = G_("types may not be defined in parameter types");
18964 /* Parse the declaration-specifiers. */
18965 cp_parser_decl_specifier_seq (parser,
18966 CP_PARSER_FLAGS_NONE,
18967 &decl_specifiers,
18968 &declares_class_or_enum);
18970 /* Complain about missing 'typename' or other invalid type names. */
18971 if (!decl_specifiers.any_type_specifiers_p
18972 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18973 decl_specifiers.type = error_mark_node;
18975 /* If an error occurred, there's no reason to attempt to parse the
18976 rest of the declaration. */
18977 if (cp_parser_error_occurred (parser))
18979 parser->type_definition_forbidden_message = saved_message;
18980 return NULL;
18983 /* Peek at the next token. */
18984 token = cp_lexer_peek_token (parser->lexer);
18986 /* If the next token is a `)', `,', `=', `>', or `...', then there
18987 is no declarator. However, when variadic templates are enabled,
18988 there may be a declarator following `...'. */
18989 if (token->type == CPP_CLOSE_PAREN
18990 || token->type == CPP_COMMA
18991 || token->type == CPP_EQ
18992 || token->type == CPP_GREATER)
18994 declarator = NULL;
18995 if (parenthesized_p)
18996 *parenthesized_p = false;
18998 /* Otherwise, there should be a declarator. */
18999 else
19001 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19002 parser->default_arg_ok_p = false;
19004 /* After seeing a decl-specifier-seq, if the next token is not a
19005 "(", there is no possibility that the code is a valid
19006 expression. Therefore, if parsing tentatively, we commit at
19007 this point. */
19008 if (!parser->in_template_argument_list_p
19009 /* In an expression context, having seen:
19011 (int((char ...
19013 we cannot be sure whether we are looking at a
19014 function-type (taking a "char" as a parameter) or a cast
19015 of some object of type "char" to "int". */
19016 && !parser->in_type_id_in_expr_p
19017 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19018 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19019 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19020 cp_parser_commit_to_tentative_parse (parser);
19021 /* Parse the declarator. */
19022 declarator_token_start = token;
19023 declarator = cp_parser_declarator (parser,
19024 CP_PARSER_DECLARATOR_EITHER,
19025 /*ctor_dtor_or_conv_p=*/NULL,
19026 parenthesized_p,
19027 /*member_p=*/false,
19028 /*friend_p=*/false);
19029 parser->default_arg_ok_p = saved_default_arg_ok_p;
19030 /* After the declarator, allow more attributes. */
19031 decl_specifiers.attributes
19032 = chainon (decl_specifiers.attributes,
19033 cp_parser_attributes_opt (parser));
19036 /* If the next token is an ellipsis, and we have not seen a
19037 declarator name, and the type of the declarator contains parameter
19038 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19039 a parameter pack expansion expression. Otherwise, leave the
19040 ellipsis for a C-style variadic function. */
19041 token = cp_lexer_peek_token (parser->lexer);
19042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19044 tree type = decl_specifiers.type;
19046 if (type && DECL_P (type))
19047 type = TREE_TYPE (type);
19049 if (type
19050 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19051 && declarator_can_be_parameter_pack (declarator)
19052 && (!declarator || !declarator->parameter_pack_p)
19053 && uses_parameter_packs (type))
19055 /* Consume the `...'. */
19056 cp_lexer_consume_token (parser->lexer);
19057 maybe_warn_variadic_templates ();
19059 /* Build a pack expansion type */
19060 if (declarator)
19061 declarator->parameter_pack_p = true;
19062 else
19063 decl_specifiers.type = make_pack_expansion (type);
19067 /* The restriction on defining new types applies only to the type
19068 of the parameter, not to the default argument. */
19069 parser->type_definition_forbidden_message = saved_message;
19071 /* If the next token is `=', then process a default argument. */
19072 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19074 token = cp_lexer_peek_token (parser->lexer);
19075 /* If we are defining a class, then the tokens that make up the
19076 default argument must be saved and processed later. */
19077 if (!template_parm_p && at_class_scope_p ()
19078 && TYPE_BEING_DEFINED (current_class_type)
19079 && !LAMBDA_TYPE_P (current_class_type))
19080 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19081 /* Outside of a class definition, we can just parse the
19082 assignment-expression. */
19083 else
19084 default_argument
19085 = cp_parser_default_argument (parser, template_parm_p);
19087 if (!parser->default_arg_ok_p)
19089 if (flag_permissive)
19090 warning (0, "deprecated use of default argument for parameter of non-function");
19091 else
19093 error_at (token->location,
19094 "default arguments are only "
19095 "permitted for function parameters");
19096 default_argument = NULL_TREE;
19099 else if ((declarator && declarator->parameter_pack_p)
19100 || (decl_specifiers.type
19101 && PACK_EXPANSION_P (decl_specifiers.type)))
19103 /* Find the name of the parameter pack. */
19104 cp_declarator *id_declarator = declarator;
19105 while (id_declarator && id_declarator->kind != cdk_id)
19106 id_declarator = id_declarator->declarator;
19108 if (id_declarator && id_declarator->kind == cdk_id)
19109 error_at (declarator_token_start->location,
19110 template_parm_p
19111 ? G_("template parameter pack %qD "
19112 "cannot have a default argument")
19113 : G_("parameter pack %qD cannot have "
19114 "a default argument"),
19115 id_declarator->u.id.unqualified_name);
19116 else
19117 error_at (declarator_token_start->location,
19118 template_parm_p
19119 ? G_("template parameter pack cannot have "
19120 "a default argument")
19121 : G_("parameter pack cannot have a "
19122 "default argument"));
19124 default_argument = NULL_TREE;
19127 else
19128 default_argument = NULL_TREE;
19130 return make_parameter_declarator (&decl_specifiers,
19131 declarator,
19132 default_argument);
19135 /* Parse a default argument and return it.
19137 TEMPLATE_PARM_P is true if this is a default argument for a
19138 non-type template parameter. */
19139 static tree
19140 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19142 tree default_argument = NULL_TREE;
19143 bool saved_greater_than_is_operator_p;
19144 bool saved_local_variables_forbidden_p;
19145 bool non_constant_p, is_direct_init;
19147 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19148 set correctly. */
19149 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19150 parser->greater_than_is_operator_p = !template_parm_p;
19151 /* Local variable names (and the `this' keyword) may not
19152 appear in a default argument. */
19153 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19154 parser->local_variables_forbidden_p = true;
19155 /* Parse the assignment-expression. */
19156 if (template_parm_p)
19157 push_deferring_access_checks (dk_no_deferred);
19158 tree saved_class_ptr = NULL_TREE;
19159 tree saved_class_ref = NULL_TREE;
19160 /* The "this" pointer is not valid in a default argument. */
19161 if (cfun)
19163 saved_class_ptr = current_class_ptr;
19164 cp_function_chain->x_current_class_ptr = NULL_TREE;
19165 saved_class_ref = current_class_ref;
19166 cp_function_chain->x_current_class_ref = NULL_TREE;
19168 default_argument
19169 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19170 /* Restore the "this" pointer. */
19171 if (cfun)
19173 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19174 cp_function_chain->x_current_class_ref = saved_class_ref;
19176 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19177 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19178 if (template_parm_p)
19179 pop_deferring_access_checks ();
19180 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19181 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19183 return default_argument;
19186 /* Parse a function-body.
19188 function-body:
19189 compound_statement */
19191 static void
19192 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19194 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19197 /* Parse a ctor-initializer-opt followed by a function-body. Return
19198 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19199 is true we are parsing a function-try-block. */
19201 static bool
19202 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19203 bool in_function_try_block)
19205 tree body, list;
19206 bool ctor_initializer_p;
19207 const bool check_body_p =
19208 DECL_CONSTRUCTOR_P (current_function_decl)
19209 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19210 tree last = NULL;
19212 /* Begin the function body. */
19213 body = begin_function_body ();
19214 /* Parse the optional ctor-initializer. */
19215 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19217 /* If we're parsing a constexpr constructor definition, we need
19218 to check that the constructor body is indeed empty. However,
19219 before we get to cp_parser_function_body lot of junk has been
19220 generated, so we can't just check that we have an empty block.
19221 Rather we take a snapshot of the outermost block, and check whether
19222 cp_parser_function_body changed its state. */
19223 if (check_body_p)
19225 list = cur_stmt_list;
19226 if (STATEMENT_LIST_TAIL (list))
19227 last = STATEMENT_LIST_TAIL (list)->stmt;
19229 /* Parse the function-body. */
19230 cp_parser_function_body (parser, in_function_try_block);
19231 if (check_body_p)
19232 check_constexpr_ctor_body (last, list, /*complain=*/true);
19233 /* Finish the function body. */
19234 finish_function_body (body);
19236 return ctor_initializer_p;
19239 /* Parse an initializer.
19241 initializer:
19242 = initializer-clause
19243 ( expression-list )
19245 Returns an expression representing the initializer. If no
19246 initializer is present, NULL_TREE is returned.
19248 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19249 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19250 set to TRUE if there is no initializer present. If there is an
19251 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19252 is set to true; otherwise it is set to false. */
19254 static tree
19255 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19256 bool* non_constant_p)
19258 cp_token *token;
19259 tree init;
19261 /* Peek at the next token. */
19262 token = cp_lexer_peek_token (parser->lexer);
19264 /* Let our caller know whether or not this initializer was
19265 parenthesized. */
19266 *is_direct_init = (token->type != CPP_EQ);
19267 /* Assume that the initializer is constant. */
19268 *non_constant_p = false;
19270 if (token->type == CPP_EQ)
19272 /* Consume the `='. */
19273 cp_lexer_consume_token (parser->lexer);
19274 /* Parse the initializer-clause. */
19275 init = cp_parser_initializer_clause (parser, non_constant_p);
19277 else if (token->type == CPP_OPEN_PAREN)
19279 vec<tree, va_gc> *vec;
19280 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19281 /*cast_p=*/false,
19282 /*allow_expansion_p=*/true,
19283 non_constant_p);
19284 if (vec == NULL)
19285 return error_mark_node;
19286 init = build_tree_list_vec (vec);
19287 release_tree_vector (vec);
19289 else if (token->type == CPP_OPEN_BRACE)
19291 cp_lexer_set_source_position (parser->lexer);
19292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19293 init = cp_parser_braced_list (parser, non_constant_p);
19294 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19296 else
19298 /* Anything else is an error. */
19299 cp_parser_error (parser, "expected initializer");
19300 init = error_mark_node;
19303 return init;
19306 /* Parse an initializer-clause.
19308 initializer-clause:
19309 assignment-expression
19310 braced-init-list
19312 Returns an expression representing the initializer.
19314 If the `assignment-expression' production is used the value
19315 returned is simply a representation for the expression.
19317 Otherwise, calls cp_parser_braced_list. */
19319 static tree
19320 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19322 tree initializer;
19324 /* Assume the expression is constant. */
19325 *non_constant_p = false;
19327 /* If it is not a `{', then we are looking at an
19328 assignment-expression. */
19329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19331 initializer
19332 = cp_parser_constant_expression (parser,
19333 /*allow_non_constant_p=*/true,
19334 non_constant_p);
19336 else
19337 initializer = cp_parser_braced_list (parser, non_constant_p);
19339 return initializer;
19342 /* Parse a brace-enclosed initializer list.
19344 braced-init-list:
19345 { initializer-list , [opt] }
19348 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19349 the elements of the initializer-list (or NULL, if the last
19350 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19351 NULL_TREE. There is no way to detect whether or not the optional
19352 trailing `,' was provided. NON_CONSTANT_P is as for
19353 cp_parser_initializer. */
19355 static tree
19356 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19358 tree initializer;
19360 /* Consume the `{' token. */
19361 cp_lexer_consume_token (parser->lexer);
19362 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19363 initializer = make_node (CONSTRUCTOR);
19364 /* If it's not a `}', then there is a non-trivial initializer. */
19365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19367 /* Parse the initializer list. */
19368 CONSTRUCTOR_ELTS (initializer)
19369 = cp_parser_initializer_list (parser, non_constant_p);
19370 /* A trailing `,' token is allowed. */
19371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19372 cp_lexer_consume_token (parser->lexer);
19374 else
19375 *non_constant_p = false;
19376 /* Now, there should be a trailing `}'. */
19377 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19378 TREE_TYPE (initializer) = init_list_type_node;
19379 return initializer;
19382 /* Consume tokens up to, and including, the next non-nested closing `]'.
19383 Returns true iff we found a closing `]'. */
19385 static bool
19386 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19388 unsigned square_depth = 0;
19390 while (true)
19392 cp_token * token = cp_lexer_peek_token (parser->lexer);
19394 switch (token->type)
19396 case CPP_EOF:
19397 case CPP_PRAGMA_EOL:
19398 /* If we've run out of tokens, then there is no closing `]'. */
19399 return false;
19401 case CPP_OPEN_SQUARE:
19402 ++square_depth;
19403 break;
19405 case CPP_CLOSE_SQUARE:
19406 if (!square_depth--)
19408 cp_lexer_consume_token (parser->lexer);
19409 return true;
19411 break;
19413 default:
19414 break;
19417 /* Consume the token. */
19418 cp_lexer_consume_token (parser->lexer);
19422 /* Return true if we are looking at an array-designator, false otherwise. */
19424 static bool
19425 cp_parser_array_designator_p (cp_parser *parser)
19427 /* Consume the `['. */
19428 cp_lexer_consume_token (parser->lexer);
19430 cp_lexer_save_tokens (parser->lexer);
19432 /* Skip tokens until the next token is a closing square bracket.
19433 If we find the closing `]', and the next token is a `=', then
19434 we are looking at an array designator. */
19435 bool array_designator_p
19436 = (cp_parser_skip_to_closing_square_bracket (parser)
19437 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19439 /* Roll back the tokens we skipped. */
19440 cp_lexer_rollback_tokens (parser->lexer);
19442 return array_designator_p;
19445 /* Parse an initializer-list.
19447 initializer-list:
19448 initializer-clause ... [opt]
19449 initializer-list , initializer-clause ... [opt]
19451 GNU Extension:
19453 initializer-list:
19454 designation initializer-clause ...[opt]
19455 initializer-list , designation initializer-clause ...[opt]
19457 designation:
19458 . identifier =
19459 identifier :
19460 [ constant-expression ] =
19462 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19463 for the initializer. If the INDEX of the elt is non-NULL, it is the
19464 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19465 as for cp_parser_initializer. */
19467 static vec<constructor_elt, va_gc> *
19468 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19470 vec<constructor_elt, va_gc> *v = NULL;
19472 /* Assume all of the expressions are constant. */
19473 *non_constant_p = false;
19475 /* Parse the rest of the list. */
19476 while (true)
19478 cp_token *token;
19479 tree designator;
19480 tree initializer;
19481 bool clause_non_constant_p;
19483 /* If the next token is an identifier and the following one is a
19484 colon, we are looking at the GNU designated-initializer
19485 syntax. */
19486 if (cp_parser_allow_gnu_extensions_p (parser)
19487 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19488 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19490 /* Warn the user that they are using an extension. */
19491 pedwarn (input_location, OPT_Wpedantic,
19492 "ISO C++ does not allow designated initializers");
19493 /* Consume the identifier. */
19494 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19495 /* Consume the `:'. */
19496 cp_lexer_consume_token (parser->lexer);
19498 /* Also handle the C99 syntax, '. id ='. */
19499 else if (cp_parser_allow_gnu_extensions_p (parser)
19500 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19501 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19502 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19504 /* Warn the user that they are using an extension. */
19505 pedwarn (input_location, OPT_Wpedantic,
19506 "ISO C++ does not allow C99 designated initializers");
19507 /* Consume the `.'. */
19508 cp_lexer_consume_token (parser->lexer);
19509 /* Consume the identifier. */
19510 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19511 /* Consume the `='. */
19512 cp_lexer_consume_token (parser->lexer);
19514 /* Also handle C99 array designators, '[ const ] ='. */
19515 else if (cp_parser_allow_gnu_extensions_p (parser)
19516 && !c_dialect_objc ()
19517 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19519 /* In C++11, [ could start a lambda-introducer. */
19520 bool non_const = false;
19522 cp_parser_parse_tentatively (parser);
19524 if (!cp_parser_array_designator_p (parser))
19526 cp_parser_simulate_error (parser);
19527 designator = NULL_TREE;
19529 else
19531 designator = cp_parser_constant_expression (parser, true,
19532 &non_const);
19533 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19534 cp_parser_require (parser, CPP_EQ, RT_EQ);
19537 if (!cp_parser_parse_definitely (parser))
19538 designator = NULL_TREE;
19539 else if (non_const)
19540 require_potential_rvalue_constant_expression (designator);
19542 else
19543 designator = NULL_TREE;
19545 /* Parse the initializer. */
19546 initializer = cp_parser_initializer_clause (parser,
19547 &clause_non_constant_p);
19548 /* If any clause is non-constant, so is the entire initializer. */
19549 if (clause_non_constant_p)
19550 *non_constant_p = true;
19552 /* If we have an ellipsis, this is an initializer pack
19553 expansion. */
19554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19556 /* Consume the `...'. */
19557 cp_lexer_consume_token (parser->lexer);
19559 /* Turn the initializer into an initializer expansion. */
19560 initializer = make_pack_expansion (initializer);
19563 /* Add it to the vector. */
19564 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19566 /* If the next token is not a comma, we have reached the end of
19567 the list. */
19568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19569 break;
19571 /* Peek at the next token. */
19572 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19573 /* If the next token is a `}', then we're still done. An
19574 initializer-clause can have a trailing `,' after the
19575 initializer-list and before the closing `}'. */
19576 if (token->type == CPP_CLOSE_BRACE)
19577 break;
19579 /* Consume the `,' token. */
19580 cp_lexer_consume_token (parser->lexer);
19583 return v;
19586 /* Classes [gram.class] */
19588 /* Parse a class-name.
19590 class-name:
19591 identifier
19592 template-id
19594 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19595 to indicate that names looked up in dependent types should be
19596 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19597 keyword has been used to indicate that the name that appears next
19598 is a template. TAG_TYPE indicates the explicit tag given before
19599 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19600 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19601 is the class being defined in a class-head.
19603 Returns the TYPE_DECL representing the class. */
19605 static tree
19606 cp_parser_class_name (cp_parser *parser,
19607 bool typename_keyword_p,
19608 bool template_keyword_p,
19609 enum tag_types tag_type,
19610 bool check_dependency_p,
19611 bool class_head_p,
19612 bool is_declaration)
19614 tree decl;
19615 tree scope;
19616 bool typename_p;
19617 cp_token *token;
19618 tree identifier = NULL_TREE;
19620 /* All class-names start with an identifier. */
19621 token = cp_lexer_peek_token (parser->lexer);
19622 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19624 cp_parser_error (parser, "expected class-name");
19625 return error_mark_node;
19628 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19629 to a template-id, so we save it here. */
19630 scope = parser->scope;
19631 if (scope == error_mark_node)
19632 return error_mark_node;
19634 /* Any name names a type if we're following the `typename' keyword
19635 in a qualified name where the enclosing scope is type-dependent. */
19636 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19637 && dependent_type_p (scope));
19638 /* Handle the common case (an identifier, but not a template-id)
19639 efficiently. */
19640 if (token->type == CPP_NAME
19641 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19643 cp_token *identifier_token;
19644 bool ambiguous_p;
19646 /* Look for the identifier. */
19647 identifier_token = cp_lexer_peek_token (parser->lexer);
19648 ambiguous_p = identifier_token->error_reported;
19649 identifier = cp_parser_identifier (parser);
19650 /* If the next token isn't an identifier, we are certainly not
19651 looking at a class-name. */
19652 if (identifier == error_mark_node)
19653 decl = error_mark_node;
19654 /* If we know this is a type-name, there's no need to look it
19655 up. */
19656 else if (typename_p)
19657 decl = identifier;
19658 else
19660 tree ambiguous_decls;
19661 /* If we already know that this lookup is ambiguous, then
19662 we've already issued an error message; there's no reason
19663 to check again. */
19664 if (ambiguous_p)
19666 cp_parser_simulate_error (parser);
19667 return error_mark_node;
19669 /* If the next token is a `::', then the name must be a type
19670 name.
19672 [basic.lookup.qual]
19674 During the lookup for a name preceding the :: scope
19675 resolution operator, object, function, and enumerator
19676 names are ignored. */
19677 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19678 tag_type = typename_type;
19679 /* Look up the name. */
19680 decl = cp_parser_lookup_name (parser, identifier,
19681 tag_type,
19682 /*is_template=*/false,
19683 /*is_namespace=*/false,
19684 check_dependency_p,
19685 &ambiguous_decls,
19686 identifier_token->location);
19687 if (ambiguous_decls)
19689 if (cp_parser_parsing_tentatively (parser))
19690 cp_parser_simulate_error (parser);
19691 return error_mark_node;
19695 else
19697 /* Try a template-id. */
19698 decl = cp_parser_template_id (parser, template_keyword_p,
19699 check_dependency_p,
19700 tag_type,
19701 is_declaration);
19702 if (decl == error_mark_node)
19703 return error_mark_node;
19706 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19708 /* If this is a typename, create a TYPENAME_TYPE. */
19709 if (typename_p && decl != error_mark_node)
19711 decl = make_typename_type (scope, decl, typename_type,
19712 /*complain=*/tf_error);
19713 if (decl != error_mark_node)
19714 decl = TYPE_NAME (decl);
19717 decl = strip_using_decl (decl);
19719 /* Check to see that it is really the name of a class. */
19720 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19721 && identifier_p (TREE_OPERAND (decl, 0))
19722 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19723 /* Situations like this:
19725 template <typename T> struct A {
19726 typename T::template X<int>::I i;
19729 are problematic. Is `T::template X<int>' a class-name? The
19730 standard does not seem to be definitive, but there is no other
19731 valid interpretation of the following `::'. Therefore, those
19732 names are considered class-names. */
19734 decl = make_typename_type (scope, decl, tag_type, tf_error);
19735 if (decl != error_mark_node)
19736 decl = TYPE_NAME (decl);
19738 else if (TREE_CODE (decl) != TYPE_DECL
19739 || TREE_TYPE (decl) == error_mark_node
19740 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19741 /* In Objective-C 2.0, a classname followed by '.' starts a
19742 dot-syntax expression, and it's not a type-name. */
19743 || (c_dialect_objc ()
19744 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19745 && objc_is_class_name (decl)))
19746 decl = error_mark_node;
19748 if (decl == error_mark_node)
19749 cp_parser_error (parser, "expected class-name");
19750 else if (identifier && !parser->scope)
19751 maybe_note_name_used_in_class (identifier, decl);
19753 return decl;
19756 /* Parse a class-specifier.
19758 class-specifier:
19759 class-head { member-specification [opt] }
19761 Returns the TREE_TYPE representing the class. */
19763 static tree
19764 cp_parser_class_specifier_1 (cp_parser* parser)
19766 tree type;
19767 tree attributes = NULL_TREE;
19768 bool nested_name_specifier_p;
19769 unsigned saved_num_template_parameter_lists;
19770 bool saved_in_function_body;
19771 unsigned char in_statement;
19772 bool in_switch_statement_p;
19773 bool saved_in_unbraced_linkage_specification_p;
19774 tree old_scope = NULL_TREE;
19775 tree scope = NULL_TREE;
19776 cp_token *closing_brace;
19778 push_deferring_access_checks (dk_no_deferred);
19780 /* Parse the class-head. */
19781 type = cp_parser_class_head (parser,
19782 &nested_name_specifier_p);
19783 /* If the class-head was a semantic disaster, skip the entire body
19784 of the class. */
19785 if (!type)
19787 cp_parser_skip_to_end_of_block_or_statement (parser);
19788 pop_deferring_access_checks ();
19789 return error_mark_node;
19792 /* Look for the `{'. */
19793 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19795 pop_deferring_access_checks ();
19796 return error_mark_node;
19799 cp_ensure_no_omp_declare_simd (parser);
19801 /* Issue an error message if type-definitions are forbidden here. */
19802 cp_parser_check_type_definition (parser);
19803 /* Remember that we are defining one more class. */
19804 ++parser->num_classes_being_defined;
19805 /* Inside the class, surrounding template-parameter-lists do not
19806 apply. */
19807 saved_num_template_parameter_lists
19808 = parser->num_template_parameter_lists;
19809 parser->num_template_parameter_lists = 0;
19810 /* We are not in a function body. */
19811 saved_in_function_body = parser->in_function_body;
19812 parser->in_function_body = false;
19813 /* Or in a loop. */
19814 in_statement = parser->in_statement;
19815 parser->in_statement = 0;
19816 /* Or in a switch. */
19817 in_switch_statement_p = parser->in_switch_statement_p;
19818 parser->in_switch_statement_p = false;
19819 /* We are not immediately inside an extern "lang" block. */
19820 saved_in_unbraced_linkage_specification_p
19821 = parser->in_unbraced_linkage_specification_p;
19822 parser->in_unbraced_linkage_specification_p = false;
19824 /* Start the class. */
19825 if (nested_name_specifier_p)
19827 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19828 old_scope = push_inner_scope (scope);
19830 type = begin_class_definition (type);
19832 if (type == error_mark_node)
19833 /* If the type is erroneous, skip the entire body of the class. */
19834 cp_parser_skip_to_closing_brace (parser);
19835 else
19836 /* Parse the member-specification. */
19837 cp_parser_member_specification_opt (parser);
19839 /* Look for the trailing `}'. */
19840 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19841 /* Look for trailing attributes to apply to this class. */
19842 if (cp_parser_allow_gnu_extensions_p (parser))
19843 attributes = cp_parser_gnu_attributes_opt (parser);
19844 if (type != error_mark_node)
19845 type = finish_struct (type, attributes);
19846 if (nested_name_specifier_p)
19847 pop_inner_scope (old_scope, scope);
19849 /* We've finished a type definition. Check for the common syntax
19850 error of forgetting a semicolon after the definition. We need to
19851 be careful, as we can't just check for not-a-semicolon and be done
19852 with it; the user might have typed:
19854 class X { } c = ...;
19855 class X { } *p = ...;
19857 and so forth. Instead, enumerate all the possible tokens that
19858 might follow this production; if we don't see one of them, then
19859 complain and silently insert the semicolon. */
19861 cp_token *token = cp_lexer_peek_token (parser->lexer);
19862 bool want_semicolon = true;
19864 if (cp_next_tokens_can_be_std_attribute_p (parser))
19865 /* Don't try to parse c++11 attributes here. As per the
19866 grammar, that should be a task for
19867 cp_parser_decl_specifier_seq. */
19868 want_semicolon = false;
19870 switch (token->type)
19872 case CPP_NAME:
19873 case CPP_SEMICOLON:
19874 case CPP_MULT:
19875 case CPP_AND:
19876 case CPP_OPEN_PAREN:
19877 case CPP_CLOSE_PAREN:
19878 case CPP_COMMA:
19879 want_semicolon = false;
19880 break;
19882 /* While it's legal for type qualifiers and storage class
19883 specifiers to follow type definitions in the grammar, only
19884 compiler testsuites contain code like that. Assume that if
19885 we see such code, then what we're really seeing is a case
19886 like:
19888 class X { }
19889 const <type> var = ...;
19893 class Y { }
19894 static <type> func (...) ...
19896 i.e. the qualifier or specifier applies to the next
19897 declaration. To do so, however, we need to look ahead one
19898 more token to see if *that* token is a type specifier.
19900 This code could be improved to handle:
19902 class Z { }
19903 static const <type> var = ...; */
19904 case CPP_KEYWORD:
19905 if (keyword_is_decl_specifier (token->keyword))
19907 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19909 /* Handling user-defined types here would be nice, but very
19910 tricky. */
19911 want_semicolon
19912 = (lookahead->type == CPP_KEYWORD
19913 && keyword_begins_type_specifier (lookahead->keyword));
19915 break;
19916 default:
19917 break;
19920 /* If we don't have a type, then something is very wrong and we
19921 shouldn't try to do anything clever. Likewise for not seeing the
19922 closing brace. */
19923 if (closing_brace && TYPE_P (type) && want_semicolon)
19925 cp_token_position prev
19926 = cp_lexer_previous_token_position (parser->lexer);
19927 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19928 location_t loc = prev_token->location;
19930 if (CLASSTYPE_DECLARED_CLASS (type))
19931 error_at (loc, "expected %<;%> after class definition");
19932 else if (TREE_CODE (type) == RECORD_TYPE)
19933 error_at (loc, "expected %<;%> after struct definition");
19934 else if (TREE_CODE (type) == UNION_TYPE)
19935 error_at (loc, "expected %<;%> after union definition");
19936 else
19937 gcc_unreachable ();
19939 /* Unget one token and smash it to look as though we encountered
19940 a semicolon in the input stream. */
19941 cp_lexer_set_token_position (parser->lexer, prev);
19942 token = cp_lexer_peek_token (parser->lexer);
19943 token->type = CPP_SEMICOLON;
19944 token->keyword = RID_MAX;
19948 /* If this class is not itself within the scope of another class,
19949 then we need to parse the bodies of all of the queued function
19950 definitions. Note that the queued functions defined in a class
19951 are not always processed immediately following the
19952 class-specifier for that class. Consider:
19954 struct A {
19955 struct B { void f() { sizeof (A); } };
19958 If `f' were processed before the processing of `A' were
19959 completed, there would be no way to compute the size of `A'.
19960 Note that the nesting we are interested in here is lexical --
19961 not the semantic nesting given by TYPE_CONTEXT. In particular,
19962 for:
19964 struct A { struct B; };
19965 struct A::B { void f() { } };
19967 there is no need to delay the parsing of `A::B::f'. */
19968 if (--parser->num_classes_being_defined == 0)
19970 tree decl;
19971 tree class_type = NULL_TREE;
19972 tree pushed_scope = NULL_TREE;
19973 unsigned ix;
19974 cp_default_arg_entry *e;
19975 tree save_ccp, save_ccr;
19977 /* In a first pass, parse default arguments to the functions.
19978 Then, in a second pass, parse the bodies of the functions.
19979 This two-phased approach handles cases like:
19981 struct S {
19982 void f() { g(); }
19983 void g(int i = 3);
19987 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19989 decl = e->decl;
19990 /* If there are default arguments that have not yet been processed,
19991 take care of them now. */
19992 if (class_type != e->class_type)
19994 if (pushed_scope)
19995 pop_scope (pushed_scope);
19996 class_type = e->class_type;
19997 pushed_scope = push_scope (class_type);
19999 /* Make sure that any template parameters are in scope. */
20000 maybe_begin_member_template_processing (decl);
20001 /* Parse the default argument expressions. */
20002 cp_parser_late_parsing_default_args (parser, decl);
20003 /* Remove any template parameters from the symbol table. */
20004 maybe_end_member_template_processing ();
20006 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20007 /* Now parse any NSDMIs. */
20008 save_ccp = current_class_ptr;
20009 save_ccr = current_class_ref;
20010 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20012 if (class_type != DECL_CONTEXT (decl))
20014 if (pushed_scope)
20015 pop_scope (pushed_scope);
20016 class_type = DECL_CONTEXT (decl);
20017 pushed_scope = push_scope (class_type);
20019 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20020 cp_parser_late_parsing_nsdmi (parser, decl);
20022 vec_safe_truncate (unparsed_nsdmis, 0);
20023 current_class_ptr = save_ccp;
20024 current_class_ref = save_ccr;
20025 if (pushed_scope)
20026 pop_scope (pushed_scope);
20028 /* Now do some post-NSDMI bookkeeping. */
20029 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20030 after_nsdmi_defaulted_late_checks (class_type);
20031 vec_safe_truncate (unparsed_classes, 0);
20032 after_nsdmi_defaulted_late_checks (type);
20034 /* Now parse the body of the functions. */
20035 if (flag_openmp)
20037 /* OpenMP UDRs need to be parsed before all other functions. */
20038 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20039 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20040 cp_parser_late_parsing_for_member (parser, decl);
20041 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20042 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20043 cp_parser_late_parsing_for_member (parser, decl);
20045 else
20046 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20047 cp_parser_late_parsing_for_member (parser, decl);
20048 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20050 else
20051 vec_safe_push (unparsed_classes, type);
20053 /* Put back any saved access checks. */
20054 pop_deferring_access_checks ();
20056 /* Restore saved state. */
20057 parser->in_switch_statement_p = in_switch_statement_p;
20058 parser->in_statement = in_statement;
20059 parser->in_function_body = saved_in_function_body;
20060 parser->num_template_parameter_lists
20061 = saved_num_template_parameter_lists;
20062 parser->in_unbraced_linkage_specification_p
20063 = saved_in_unbraced_linkage_specification_p;
20065 return type;
20068 static tree
20069 cp_parser_class_specifier (cp_parser* parser)
20071 tree ret;
20072 timevar_push (TV_PARSE_STRUCT);
20073 ret = cp_parser_class_specifier_1 (parser);
20074 timevar_pop (TV_PARSE_STRUCT);
20075 return ret;
20078 /* Parse a class-head.
20080 class-head:
20081 class-key identifier [opt] base-clause [opt]
20082 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20083 class-key nested-name-specifier [opt] template-id
20084 base-clause [opt]
20086 class-virt-specifier:
20087 final
20089 GNU Extensions:
20090 class-key attributes identifier [opt] base-clause [opt]
20091 class-key attributes nested-name-specifier identifier base-clause [opt]
20092 class-key attributes nested-name-specifier [opt] template-id
20093 base-clause [opt]
20095 Upon return BASES is initialized to the list of base classes (or
20096 NULL, if there are none) in the same form returned by
20097 cp_parser_base_clause.
20099 Returns the TYPE of the indicated class. Sets
20100 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20101 involving a nested-name-specifier was used, and FALSE otherwise.
20103 Returns error_mark_node if this is not a class-head.
20105 Returns NULL_TREE if the class-head is syntactically valid, but
20106 semantically invalid in a way that means we should skip the entire
20107 body of the class. */
20109 static tree
20110 cp_parser_class_head (cp_parser* parser,
20111 bool* nested_name_specifier_p)
20113 tree nested_name_specifier;
20114 enum tag_types class_key;
20115 tree id = NULL_TREE;
20116 tree type = NULL_TREE;
20117 tree attributes;
20118 tree bases;
20119 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20120 bool template_id_p = false;
20121 bool qualified_p = false;
20122 bool invalid_nested_name_p = false;
20123 bool invalid_explicit_specialization_p = false;
20124 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20125 tree pushed_scope = NULL_TREE;
20126 unsigned num_templates;
20127 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20128 /* Assume no nested-name-specifier will be present. */
20129 *nested_name_specifier_p = false;
20130 /* Assume no template parameter lists will be used in defining the
20131 type. */
20132 num_templates = 0;
20133 parser->colon_corrects_to_scope_p = false;
20135 /* Look for the class-key. */
20136 class_key = cp_parser_class_key (parser);
20137 if (class_key == none_type)
20138 return error_mark_node;
20140 /* Parse the attributes. */
20141 attributes = cp_parser_attributes_opt (parser);
20143 /* If the next token is `::', that is invalid -- but sometimes
20144 people do try to write:
20146 struct ::S {};
20148 Handle this gracefully by accepting the extra qualifier, and then
20149 issuing an error about it later if this really is a
20150 class-head. If it turns out just to be an elaborated type
20151 specifier, remain silent. */
20152 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20153 qualified_p = true;
20155 push_deferring_access_checks (dk_no_check);
20157 /* Determine the name of the class. Begin by looking for an
20158 optional nested-name-specifier. */
20159 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20160 nested_name_specifier
20161 = cp_parser_nested_name_specifier_opt (parser,
20162 /*typename_keyword_p=*/false,
20163 /*check_dependency_p=*/false,
20164 /*type_p=*/true,
20165 /*is_declaration=*/false);
20166 /* If there was a nested-name-specifier, then there *must* be an
20167 identifier. */
20168 if (nested_name_specifier)
20170 type_start_token = cp_lexer_peek_token (parser->lexer);
20171 /* Although the grammar says `identifier', it really means
20172 `class-name' or `template-name'. You are only allowed to
20173 define a class that has already been declared with this
20174 syntax.
20176 The proposed resolution for Core Issue 180 says that wherever
20177 you see `class T::X' you should treat `X' as a type-name.
20179 It is OK to define an inaccessible class; for example:
20181 class A { class B; };
20182 class A::B {};
20184 We do not know if we will see a class-name, or a
20185 template-name. We look for a class-name first, in case the
20186 class-name is a template-id; if we looked for the
20187 template-name first we would stop after the template-name. */
20188 cp_parser_parse_tentatively (parser);
20189 type = cp_parser_class_name (parser,
20190 /*typename_keyword_p=*/false,
20191 /*template_keyword_p=*/false,
20192 class_type,
20193 /*check_dependency_p=*/false,
20194 /*class_head_p=*/true,
20195 /*is_declaration=*/false);
20196 /* If that didn't work, ignore the nested-name-specifier. */
20197 if (!cp_parser_parse_definitely (parser))
20199 invalid_nested_name_p = true;
20200 type_start_token = cp_lexer_peek_token (parser->lexer);
20201 id = cp_parser_identifier (parser);
20202 if (id == error_mark_node)
20203 id = NULL_TREE;
20205 /* If we could not find a corresponding TYPE, treat this
20206 declaration like an unqualified declaration. */
20207 if (type == error_mark_node)
20208 nested_name_specifier = NULL_TREE;
20209 /* Otherwise, count the number of templates used in TYPE and its
20210 containing scopes. */
20211 else
20213 tree scope;
20215 for (scope = TREE_TYPE (type);
20216 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20217 scope = get_containing_scope (scope))
20218 if (TYPE_P (scope)
20219 && CLASS_TYPE_P (scope)
20220 && CLASSTYPE_TEMPLATE_INFO (scope)
20221 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20222 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20223 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20224 ++num_templates;
20227 /* Otherwise, the identifier is optional. */
20228 else
20230 /* We don't know whether what comes next is a template-id,
20231 an identifier, or nothing at all. */
20232 cp_parser_parse_tentatively (parser);
20233 /* Check for a template-id. */
20234 type_start_token = cp_lexer_peek_token (parser->lexer);
20235 id = cp_parser_template_id (parser,
20236 /*template_keyword_p=*/false,
20237 /*check_dependency_p=*/true,
20238 class_key,
20239 /*is_declaration=*/true);
20240 /* If that didn't work, it could still be an identifier. */
20241 if (!cp_parser_parse_definitely (parser))
20243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20245 type_start_token = cp_lexer_peek_token (parser->lexer);
20246 id = cp_parser_identifier (parser);
20248 else
20249 id = NULL_TREE;
20251 else
20253 template_id_p = true;
20254 ++num_templates;
20258 pop_deferring_access_checks ();
20260 if (id)
20262 cp_parser_check_for_invalid_template_id (parser, id,
20263 class_key,
20264 type_start_token->location);
20266 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20268 /* If it's not a `:' or a `{' then we can't really be looking at a
20269 class-head, since a class-head only appears as part of a
20270 class-specifier. We have to detect this situation before calling
20271 xref_tag, since that has irreversible side-effects. */
20272 if (!cp_parser_next_token_starts_class_definition_p (parser))
20274 cp_parser_error (parser, "expected %<{%> or %<:%>");
20275 type = error_mark_node;
20276 goto out;
20279 /* At this point, we're going ahead with the class-specifier, even
20280 if some other problem occurs. */
20281 cp_parser_commit_to_tentative_parse (parser);
20282 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20284 cp_parser_error (parser,
20285 "cannot specify %<override%> for a class");
20286 type = error_mark_node;
20287 goto out;
20289 /* Issue the error about the overly-qualified name now. */
20290 if (qualified_p)
20292 cp_parser_error (parser,
20293 "global qualification of class name is invalid");
20294 type = error_mark_node;
20295 goto out;
20297 else if (invalid_nested_name_p)
20299 cp_parser_error (parser,
20300 "qualified name does not name a class");
20301 type = error_mark_node;
20302 goto out;
20304 else if (nested_name_specifier)
20306 tree scope;
20308 /* Reject typedef-names in class heads. */
20309 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20311 error_at (type_start_token->location,
20312 "invalid class name in declaration of %qD",
20313 type);
20314 type = NULL_TREE;
20315 goto done;
20318 /* Figure out in what scope the declaration is being placed. */
20319 scope = current_scope ();
20320 /* If that scope does not contain the scope in which the
20321 class was originally declared, the program is invalid. */
20322 if (scope && !is_ancestor (scope, nested_name_specifier))
20324 if (at_namespace_scope_p ())
20325 error_at (type_start_token->location,
20326 "declaration of %qD in namespace %qD which does not "
20327 "enclose %qD",
20328 type, scope, nested_name_specifier);
20329 else
20330 error_at (type_start_token->location,
20331 "declaration of %qD in %qD which does not enclose %qD",
20332 type, scope, nested_name_specifier);
20333 type = NULL_TREE;
20334 goto done;
20336 /* [dcl.meaning]
20338 A declarator-id shall not be qualified except for the
20339 definition of a ... nested class outside of its class
20340 ... [or] the definition or explicit instantiation of a
20341 class member of a namespace outside of its namespace. */
20342 if (scope == nested_name_specifier)
20344 permerror (nested_name_specifier_token_start->location,
20345 "extra qualification not allowed");
20346 nested_name_specifier = NULL_TREE;
20347 num_templates = 0;
20350 /* An explicit-specialization must be preceded by "template <>". If
20351 it is not, try to recover gracefully. */
20352 if (at_namespace_scope_p ()
20353 && parser->num_template_parameter_lists == 0
20354 && template_id_p)
20356 error_at (type_start_token->location,
20357 "an explicit specialization must be preceded by %<template <>%>");
20358 invalid_explicit_specialization_p = true;
20359 /* Take the same action that would have been taken by
20360 cp_parser_explicit_specialization. */
20361 ++parser->num_template_parameter_lists;
20362 begin_specialization ();
20364 /* There must be no "return" statements between this point and the
20365 end of this function; set "type "to the correct return value and
20366 use "goto done;" to return. */
20367 /* Make sure that the right number of template parameters were
20368 present. */
20369 if (!cp_parser_check_template_parameters (parser, num_templates,
20370 type_start_token->location,
20371 /*declarator=*/NULL))
20373 /* If something went wrong, there is no point in even trying to
20374 process the class-definition. */
20375 type = NULL_TREE;
20376 goto done;
20379 /* Look up the type. */
20380 if (template_id_p)
20382 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20383 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20384 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20386 error_at (type_start_token->location,
20387 "function template %qD redeclared as a class template", id);
20388 type = error_mark_node;
20390 else
20392 type = TREE_TYPE (id);
20393 type = maybe_process_partial_specialization (type);
20395 if (nested_name_specifier)
20396 pushed_scope = push_scope (nested_name_specifier);
20398 else if (nested_name_specifier)
20400 tree class_type;
20402 /* Given:
20404 template <typename T> struct S { struct T };
20405 template <typename T> struct S<T>::T { };
20407 we will get a TYPENAME_TYPE when processing the definition of
20408 `S::T'. We need to resolve it to the actual type before we
20409 try to define it. */
20410 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20412 class_type = resolve_typename_type (TREE_TYPE (type),
20413 /*only_current_p=*/false);
20414 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20415 type = TYPE_NAME (class_type);
20416 else
20418 cp_parser_error (parser, "could not resolve typename type");
20419 type = error_mark_node;
20423 if (maybe_process_partial_specialization (TREE_TYPE (type))
20424 == error_mark_node)
20426 type = NULL_TREE;
20427 goto done;
20430 class_type = current_class_type;
20431 /* Enter the scope indicated by the nested-name-specifier. */
20432 pushed_scope = push_scope (nested_name_specifier);
20433 /* Get the canonical version of this type. */
20434 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20435 /* Call push_template_decl if it seems like we should be defining a
20436 template either from the template headers or the type we're
20437 defining, so that we diagnose both extra and missing headers. */
20438 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20439 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20440 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20442 type = push_template_decl (type);
20443 if (type == error_mark_node)
20445 type = NULL_TREE;
20446 goto done;
20450 type = TREE_TYPE (type);
20451 *nested_name_specifier_p = true;
20453 else /* The name is not a nested name. */
20455 /* If the class was unnamed, create a dummy name. */
20456 if (!id)
20457 id = make_anon_name ();
20458 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20459 parser->num_template_parameter_lists);
20462 /* Indicate whether this class was declared as a `class' or as a
20463 `struct'. */
20464 if (TREE_CODE (type) == RECORD_TYPE)
20465 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20466 cp_parser_check_class_key (class_key, type);
20468 /* If this type was already complete, and we see another definition,
20469 that's an error. */
20470 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20472 error_at (type_start_token->location, "redefinition of %q#T",
20473 type);
20474 error_at (type_start_token->location, "previous definition of %q+#T",
20475 type);
20476 type = NULL_TREE;
20477 goto done;
20479 else if (type == error_mark_node)
20480 type = NULL_TREE;
20482 if (type)
20484 /* Apply attributes now, before any use of the class as a template
20485 argument in its base list. */
20486 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20487 fixup_attribute_variants (type);
20490 /* We will have entered the scope containing the class; the names of
20491 base classes should be looked up in that context. For example:
20493 struct A { struct B {}; struct C; };
20494 struct A::C : B {};
20496 is valid. */
20498 /* Get the list of base-classes, if there is one. */
20499 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20501 /* PR59482: enter the class scope so that base-specifiers are looked
20502 up correctly. */
20503 if (type)
20504 pushclass (type);
20505 bases = cp_parser_base_clause (parser);
20506 /* PR59482: get out of the previously pushed class scope so that the
20507 subsequent pops pop the right thing. */
20508 if (type)
20509 popclass ();
20511 else
20512 bases = NULL_TREE;
20514 /* If we're really defining a class, process the base classes.
20515 If they're invalid, fail. */
20516 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20517 && !xref_basetypes (type, bases))
20518 type = NULL_TREE;
20520 done:
20521 /* Leave the scope given by the nested-name-specifier. We will
20522 enter the class scope itself while processing the members. */
20523 if (pushed_scope)
20524 pop_scope (pushed_scope);
20526 if (invalid_explicit_specialization_p)
20528 end_specialization ();
20529 --parser->num_template_parameter_lists;
20532 if (type)
20533 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20534 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20535 CLASSTYPE_FINAL (type) = 1;
20536 out:
20537 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20538 return type;
20541 /* Parse a class-key.
20543 class-key:
20544 class
20545 struct
20546 union
20548 Returns the kind of class-key specified, or none_type to indicate
20549 error. */
20551 static enum tag_types
20552 cp_parser_class_key (cp_parser* parser)
20554 cp_token *token;
20555 enum tag_types tag_type;
20557 /* Look for the class-key. */
20558 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20559 if (!token)
20560 return none_type;
20562 /* Check to see if the TOKEN is a class-key. */
20563 tag_type = cp_parser_token_is_class_key (token);
20564 if (!tag_type)
20565 cp_parser_error (parser, "expected class-key");
20566 return tag_type;
20569 /* Parse a type-parameter-key.
20571 type-parameter-key:
20572 class
20573 typename
20576 static void
20577 cp_parser_type_parameter_key (cp_parser* parser)
20579 /* Look for the type-parameter-key. */
20580 enum tag_types tag_type = none_type;
20581 cp_token *token = cp_lexer_peek_token (parser->lexer);
20582 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20584 cp_lexer_consume_token (parser->lexer);
20585 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20586 /* typename is not allowed in a template template parameter
20587 by the standard until C++1Z. */
20588 pedwarn (token->location, OPT_Wpedantic,
20589 "ISO C++ forbids typename key in template template parameter;"
20590 " use -std=c++1z or -std=gnu++1z");
20592 else
20593 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20595 return;
20598 /* Parse an (optional) member-specification.
20600 member-specification:
20601 member-declaration member-specification [opt]
20602 access-specifier : member-specification [opt] */
20604 static void
20605 cp_parser_member_specification_opt (cp_parser* parser)
20607 while (true)
20609 cp_token *token;
20610 enum rid keyword;
20612 /* Peek at the next token. */
20613 token = cp_lexer_peek_token (parser->lexer);
20614 /* If it's a `}', or EOF then we've seen all the members. */
20615 if (token->type == CPP_CLOSE_BRACE
20616 || token->type == CPP_EOF
20617 || token->type == CPP_PRAGMA_EOL)
20618 break;
20620 /* See if this token is a keyword. */
20621 keyword = token->keyword;
20622 switch (keyword)
20624 case RID_PUBLIC:
20625 case RID_PROTECTED:
20626 case RID_PRIVATE:
20627 /* Consume the access-specifier. */
20628 cp_lexer_consume_token (parser->lexer);
20629 /* Remember which access-specifier is active. */
20630 current_access_specifier = token->u.value;
20631 /* Look for the `:'. */
20632 cp_parser_require (parser, CPP_COLON, RT_COLON);
20633 break;
20635 default:
20636 /* Accept #pragmas at class scope. */
20637 if (token->type == CPP_PRAGMA)
20639 cp_parser_pragma (parser, pragma_member);
20640 break;
20643 /* Otherwise, the next construction must be a
20644 member-declaration. */
20645 cp_parser_member_declaration (parser);
20650 /* Parse a member-declaration.
20652 member-declaration:
20653 decl-specifier-seq [opt] member-declarator-list [opt] ;
20654 function-definition ; [opt]
20655 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20656 using-declaration
20657 template-declaration
20658 alias-declaration
20660 member-declarator-list:
20661 member-declarator
20662 member-declarator-list , member-declarator
20664 member-declarator:
20665 declarator pure-specifier [opt]
20666 declarator constant-initializer [opt]
20667 identifier [opt] : constant-expression
20669 GNU Extensions:
20671 member-declaration:
20672 __extension__ member-declaration
20674 member-declarator:
20675 declarator attributes [opt] pure-specifier [opt]
20676 declarator attributes [opt] constant-initializer [opt]
20677 identifier [opt] attributes [opt] : constant-expression
20679 C++0x Extensions:
20681 member-declaration:
20682 static_assert-declaration */
20684 static void
20685 cp_parser_member_declaration (cp_parser* parser)
20687 cp_decl_specifier_seq decl_specifiers;
20688 tree prefix_attributes;
20689 tree decl;
20690 int declares_class_or_enum;
20691 bool friend_p;
20692 cp_token *token = NULL;
20693 cp_token *decl_spec_token_start = NULL;
20694 cp_token *initializer_token_start = NULL;
20695 int saved_pedantic;
20696 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20698 /* Check for the `__extension__' keyword. */
20699 if (cp_parser_extension_opt (parser, &saved_pedantic))
20701 /* Recurse. */
20702 cp_parser_member_declaration (parser);
20703 /* Restore the old value of the PEDANTIC flag. */
20704 pedantic = saved_pedantic;
20706 return;
20709 /* Check for a template-declaration. */
20710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20712 /* An explicit specialization here is an error condition, and we
20713 expect the specialization handler to detect and report this. */
20714 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20715 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20716 cp_parser_explicit_specialization (parser);
20717 else
20718 cp_parser_template_declaration (parser, /*member_p=*/true);
20720 return;
20723 /* Check for a using-declaration. */
20724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20726 if (cxx_dialect < cxx11)
20728 /* Parse the using-declaration. */
20729 cp_parser_using_declaration (parser,
20730 /*access_declaration_p=*/false);
20731 return;
20733 else
20735 tree decl;
20736 bool alias_decl_expected;
20737 cp_parser_parse_tentatively (parser);
20738 decl = cp_parser_alias_declaration (parser);
20739 /* Note that if we actually see the '=' token after the
20740 identifier, cp_parser_alias_declaration commits the
20741 tentative parse. In that case, we really expects an
20742 alias-declaration. Otherwise, we expect a using
20743 declaration. */
20744 alias_decl_expected =
20745 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20746 cp_parser_parse_definitely (parser);
20748 if (alias_decl_expected)
20749 finish_member_declaration (decl);
20750 else
20751 cp_parser_using_declaration (parser,
20752 /*access_declaration_p=*/false);
20753 return;
20757 /* Check for @defs. */
20758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20760 tree ivar, member;
20761 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20762 ivar = ivar_chains;
20763 while (ivar)
20765 member = ivar;
20766 ivar = TREE_CHAIN (member);
20767 TREE_CHAIN (member) = NULL_TREE;
20768 finish_member_declaration (member);
20770 return;
20773 /* If the next token is `static_assert' we have a static assertion. */
20774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20776 cp_parser_static_assert (parser, /*member_p=*/true);
20777 return;
20780 parser->colon_corrects_to_scope_p = false;
20782 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20783 goto out;
20785 /* Parse the decl-specifier-seq. */
20786 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20787 cp_parser_decl_specifier_seq (parser,
20788 CP_PARSER_FLAGS_OPTIONAL,
20789 &decl_specifiers,
20790 &declares_class_or_enum);
20791 /* Check for an invalid type-name. */
20792 if (!decl_specifiers.any_type_specifiers_p
20793 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20794 goto out;
20795 /* If there is no declarator, then the decl-specifier-seq should
20796 specify a type. */
20797 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20799 /* If there was no decl-specifier-seq, and the next token is a
20800 `;', then we have something like:
20802 struct S { ; };
20804 [class.mem]
20806 Each member-declaration shall declare at least one member
20807 name of the class. */
20808 if (!decl_specifiers.any_specifiers_p)
20810 cp_token *token = cp_lexer_peek_token (parser->lexer);
20811 if (!in_system_header_at (token->location))
20812 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20814 else
20816 tree type;
20818 /* See if this declaration is a friend. */
20819 friend_p = cp_parser_friend_p (&decl_specifiers);
20820 /* If there were decl-specifiers, check to see if there was
20821 a class-declaration. */
20822 type = check_tag_decl (&decl_specifiers,
20823 /*explicit_type_instantiation_p=*/false);
20824 /* Nested classes have already been added to the class, but
20825 a `friend' needs to be explicitly registered. */
20826 if (friend_p)
20828 /* If the `friend' keyword was present, the friend must
20829 be introduced with a class-key. */
20830 if (!declares_class_or_enum && cxx_dialect < cxx11)
20831 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20832 "in C++03 a class-key must be used "
20833 "when declaring a friend");
20834 /* In this case:
20836 template <typename T> struct A {
20837 friend struct A<T>::B;
20840 A<T>::B will be represented by a TYPENAME_TYPE, and
20841 therefore not recognized by check_tag_decl. */
20842 if (!type)
20844 type = decl_specifiers.type;
20845 if (type && TREE_CODE (type) == TYPE_DECL)
20846 type = TREE_TYPE (type);
20848 if (!type || !TYPE_P (type))
20849 error_at (decl_spec_token_start->location,
20850 "friend declaration does not name a class or "
20851 "function");
20852 else
20853 make_friend_class (current_class_type, type,
20854 /*complain=*/true);
20856 /* If there is no TYPE, an error message will already have
20857 been issued. */
20858 else if (!type || type == error_mark_node)
20860 /* An anonymous aggregate has to be handled specially; such
20861 a declaration really declares a data member (with a
20862 particular type), as opposed to a nested class. */
20863 else if (ANON_AGGR_TYPE_P (type))
20865 /* C++11 9.5/6. */
20866 if (decl_specifiers.storage_class != sc_none)
20867 error_at (decl_spec_token_start->location,
20868 "a storage class on an anonymous aggregate "
20869 "in class scope is not allowed");
20871 /* Remove constructors and such from TYPE, now that we
20872 know it is an anonymous aggregate. */
20873 fixup_anonymous_aggr (type);
20874 /* And make the corresponding data member. */
20875 decl = build_decl (decl_spec_token_start->location,
20876 FIELD_DECL, NULL_TREE, type);
20877 /* Add it to the class. */
20878 finish_member_declaration (decl);
20880 else
20881 cp_parser_check_access_in_redeclaration
20882 (TYPE_NAME (type),
20883 decl_spec_token_start->location);
20886 else
20888 bool assume_semicolon = false;
20890 /* Clear attributes from the decl_specifiers but keep them
20891 around as prefix attributes that apply them to the entity
20892 being declared. */
20893 prefix_attributes = decl_specifiers.attributes;
20894 decl_specifiers.attributes = NULL_TREE;
20896 /* See if these declarations will be friends. */
20897 friend_p = cp_parser_friend_p (&decl_specifiers);
20899 /* Keep going until we hit the `;' at the end of the
20900 declaration. */
20901 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20903 tree attributes = NULL_TREE;
20904 tree first_attribute;
20906 /* Peek at the next token. */
20907 token = cp_lexer_peek_token (parser->lexer);
20909 /* Check for a bitfield declaration. */
20910 if (token->type == CPP_COLON
20911 || (token->type == CPP_NAME
20912 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20913 == CPP_COLON))
20915 tree identifier;
20916 tree width;
20918 /* Get the name of the bitfield. Note that we cannot just
20919 check TOKEN here because it may have been invalidated by
20920 the call to cp_lexer_peek_nth_token above. */
20921 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20922 identifier = cp_parser_identifier (parser);
20923 else
20924 identifier = NULL_TREE;
20926 /* Consume the `:' token. */
20927 cp_lexer_consume_token (parser->lexer);
20928 /* Get the width of the bitfield. */
20929 width
20930 = cp_parser_constant_expression (parser);
20932 /* Look for attributes that apply to the bitfield. */
20933 attributes = cp_parser_attributes_opt (parser);
20934 /* Remember which attributes are prefix attributes and
20935 which are not. */
20936 first_attribute = attributes;
20937 /* Combine the attributes. */
20938 attributes = chainon (prefix_attributes, attributes);
20940 /* Create the bitfield declaration. */
20941 decl = grokbitfield (identifier
20942 ? make_id_declarator (NULL_TREE,
20943 identifier,
20944 sfk_none)
20945 : NULL,
20946 &decl_specifiers,
20947 width,
20948 attributes);
20950 else
20952 cp_declarator *declarator;
20953 tree initializer;
20954 tree asm_specification;
20955 int ctor_dtor_or_conv_p;
20957 /* Parse the declarator. */
20958 declarator
20959 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20960 &ctor_dtor_or_conv_p,
20961 /*parenthesized_p=*/NULL,
20962 /*member_p=*/true,
20963 friend_p);
20965 /* If something went wrong parsing the declarator, make sure
20966 that we at least consume some tokens. */
20967 if (declarator == cp_error_declarator)
20969 /* Skip to the end of the statement. */
20970 cp_parser_skip_to_end_of_statement (parser);
20971 /* If the next token is not a semicolon, that is
20972 probably because we just skipped over the body of
20973 a function. So, we consume a semicolon if
20974 present, but do not issue an error message if it
20975 is not present. */
20976 if (cp_lexer_next_token_is (parser->lexer,
20977 CPP_SEMICOLON))
20978 cp_lexer_consume_token (parser->lexer);
20979 goto out;
20982 if (declares_class_or_enum & 2)
20983 cp_parser_check_for_definition_in_return_type
20984 (declarator, decl_specifiers.type,
20985 decl_specifiers.locations[ds_type_spec]);
20987 /* Look for an asm-specification. */
20988 asm_specification = cp_parser_asm_specification_opt (parser);
20989 /* Look for attributes that apply to the declaration. */
20990 attributes = cp_parser_attributes_opt (parser);
20991 /* Remember which attributes are prefix attributes and
20992 which are not. */
20993 first_attribute = attributes;
20994 /* Combine the attributes. */
20995 attributes = chainon (prefix_attributes, attributes);
20997 /* If it's an `=', then we have a constant-initializer or a
20998 pure-specifier. It is not correct to parse the
20999 initializer before registering the member declaration
21000 since the member declaration should be in scope while
21001 its initializer is processed. However, the rest of the
21002 front end does not yet provide an interface that allows
21003 us to handle this correctly. */
21004 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21006 /* In [class.mem]:
21008 A pure-specifier shall be used only in the declaration of
21009 a virtual function.
21011 A member-declarator can contain a constant-initializer
21012 only if it declares a static member of integral or
21013 enumeration type.
21015 Therefore, if the DECLARATOR is for a function, we look
21016 for a pure-specifier; otherwise, we look for a
21017 constant-initializer. When we call `grokfield', it will
21018 perform more stringent semantics checks. */
21019 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21020 if (function_declarator_p (declarator)
21021 || (decl_specifiers.type
21022 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21023 && declarator->kind == cdk_id
21024 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21025 == FUNCTION_TYPE)))
21026 initializer = cp_parser_pure_specifier (parser);
21027 else if (decl_specifiers.storage_class != sc_static)
21028 initializer = cp_parser_save_nsdmi (parser);
21029 else if (cxx_dialect >= cxx11)
21031 bool nonconst;
21032 /* Don't require a constant rvalue in C++11, since we
21033 might want a reference constant. We'll enforce
21034 constancy later. */
21035 cp_lexer_consume_token (parser->lexer);
21036 /* Parse the initializer. */
21037 initializer = cp_parser_initializer_clause (parser,
21038 &nonconst);
21040 else
21041 /* Parse the initializer. */
21042 initializer = cp_parser_constant_initializer (parser);
21044 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21045 && !function_declarator_p (declarator))
21047 bool x;
21048 if (decl_specifiers.storage_class != sc_static)
21049 initializer = cp_parser_save_nsdmi (parser);
21050 else
21051 initializer = cp_parser_initializer (parser, &x, &x);
21053 /* Otherwise, there is no initializer. */
21054 else
21055 initializer = NULL_TREE;
21057 /* See if we are probably looking at a function
21058 definition. We are certainly not looking at a
21059 member-declarator. Calling `grokfield' has
21060 side-effects, so we must not do it unless we are sure
21061 that we are looking at a member-declarator. */
21062 if (cp_parser_token_starts_function_definition_p
21063 (cp_lexer_peek_token (parser->lexer)))
21065 /* The grammar does not allow a pure-specifier to be
21066 used when a member function is defined. (It is
21067 possible that this fact is an oversight in the
21068 standard, since a pure function may be defined
21069 outside of the class-specifier. */
21070 if (initializer && initializer_token_start)
21071 error_at (initializer_token_start->location,
21072 "pure-specifier on function-definition");
21073 decl = cp_parser_save_member_function_body (parser,
21074 &decl_specifiers,
21075 declarator,
21076 attributes);
21077 if (parser->fully_implicit_function_template_p)
21078 decl = finish_fully_implicit_template (parser, decl);
21079 /* If the member was not a friend, declare it here. */
21080 if (!friend_p)
21081 finish_member_declaration (decl);
21082 /* Peek at the next token. */
21083 token = cp_lexer_peek_token (parser->lexer);
21084 /* If the next token is a semicolon, consume it. */
21085 if (token->type == CPP_SEMICOLON)
21086 cp_lexer_consume_token (parser->lexer);
21087 goto out;
21089 else
21090 if (declarator->kind == cdk_function)
21091 declarator->id_loc = token->location;
21092 /* Create the declaration. */
21093 decl = grokfield (declarator, &decl_specifiers,
21094 initializer, /*init_const_expr_p=*/true,
21095 asm_specification, attributes);
21096 if (parser->fully_implicit_function_template_p)
21098 if (friend_p)
21099 finish_fully_implicit_template (parser, 0);
21100 else
21101 decl = finish_fully_implicit_template (parser, decl);
21105 cp_finalize_omp_declare_simd (parser, decl);
21107 /* Reset PREFIX_ATTRIBUTES. */
21108 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21109 attributes = TREE_CHAIN (attributes);
21110 if (attributes)
21111 TREE_CHAIN (attributes) = NULL_TREE;
21113 /* If there is any qualification still in effect, clear it
21114 now; we will be starting fresh with the next declarator. */
21115 parser->scope = NULL_TREE;
21116 parser->qualifying_scope = NULL_TREE;
21117 parser->object_scope = NULL_TREE;
21118 /* If it's a `,', then there are more declarators. */
21119 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21121 cp_lexer_consume_token (parser->lexer);
21122 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21124 cp_token *token = cp_lexer_previous_token (parser->lexer);
21125 error_at (token->location,
21126 "stray %<,%> at end of member declaration");
21129 /* If the next token isn't a `;', then we have a parse error. */
21130 else if (cp_lexer_next_token_is_not (parser->lexer,
21131 CPP_SEMICOLON))
21133 /* The next token might be a ways away from where the
21134 actual semicolon is missing. Find the previous token
21135 and use that for our error position. */
21136 cp_token *token = cp_lexer_previous_token (parser->lexer);
21137 error_at (token->location,
21138 "expected %<;%> at end of member declaration");
21140 /* Assume that the user meant to provide a semicolon. If
21141 we were to cp_parser_skip_to_end_of_statement, we might
21142 skip to a semicolon inside a member function definition
21143 and issue nonsensical error messages. */
21144 assume_semicolon = true;
21147 if (decl)
21149 /* Add DECL to the list of members. */
21150 if (!friend_p
21151 /* Explicitly include, eg, NSDMIs, for better error
21152 recovery (c++/58650). */
21153 || !DECL_DECLARES_FUNCTION_P (decl))
21154 finish_member_declaration (decl);
21156 if (TREE_CODE (decl) == FUNCTION_DECL)
21157 cp_parser_save_default_args (parser, decl);
21158 else if (TREE_CODE (decl) == FIELD_DECL
21159 && !DECL_C_BIT_FIELD (decl)
21160 && DECL_INITIAL (decl))
21161 /* Add DECL to the queue of NSDMI to be parsed later. */
21162 vec_safe_push (unparsed_nsdmis, decl);
21165 if (assume_semicolon)
21166 goto out;
21170 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21171 out:
21172 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21175 /* Parse a pure-specifier.
21177 pure-specifier:
21180 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21181 Otherwise, ERROR_MARK_NODE is returned. */
21183 static tree
21184 cp_parser_pure_specifier (cp_parser* parser)
21186 cp_token *token;
21188 /* Look for the `=' token. */
21189 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21190 return error_mark_node;
21191 /* Look for the `0' token. */
21192 token = cp_lexer_peek_token (parser->lexer);
21194 if (token->type == CPP_EOF
21195 || token->type == CPP_PRAGMA_EOL)
21196 return error_mark_node;
21198 cp_lexer_consume_token (parser->lexer);
21200 /* Accept = default or = delete in c++0x mode. */
21201 if (token->keyword == RID_DEFAULT
21202 || token->keyword == RID_DELETE)
21204 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21205 return token->u.value;
21208 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21209 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21211 cp_parser_error (parser,
21212 "invalid pure specifier (only %<= 0%> is allowed)");
21213 cp_parser_skip_to_end_of_statement (parser);
21214 return error_mark_node;
21216 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21218 error_at (token->location, "templates may not be %<virtual%>");
21219 return error_mark_node;
21222 return integer_zero_node;
21225 /* Parse a constant-initializer.
21227 constant-initializer:
21228 = constant-expression
21230 Returns a representation of the constant-expression. */
21232 static tree
21233 cp_parser_constant_initializer (cp_parser* parser)
21235 /* Look for the `=' token. */
21236 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21237 return error_mark_node;
21239 /* It is invalid to write:
21241 struct S { static const int i = { 7 }; };
21244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21246 cp_parser_error (parser,
21247 "a brace-enclosed initializer is not allowed here");
21248 /* Consume the opening brace. */
21249 cp_lexer_consume_token (parser->lexer);
21250 /* Skip the initializer. */
21251 cp_parser_skip_to_closing_brace (parser);
21252 /* Look for the trailing `}'. */
21253 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21255 return error_mark_node;
21258 return cp_parser_constant_expression (parser);
21261 /* Derived classes [gram.class.derived] */
21263 /* Parse a base-clause.
21265 base-clause:
21266 : base-specifier-list
21268 base-specifier-list:
21269 base-specifier ... [opt]
21270 base-specifier-list , base-specifier ... [opt]
21272 Returns a TREE_LIST representing the base-classes, in the order in
21273 which they were declared. The representation of each node is as
21274 described by cp_parser_base_specifier.
21276 In the case that no bases are specified, this function will return
21277 NULL_TREE, not ERROR_MARK_NODE. */
21279 static tree
21280 cp_parser_base_clause (cp_parser* parser)
21282 tree bases = NULL_TREE;
21284 /* Look for the `:' that begins the list. */
21285 cp_parser_require (parser, CPP_COLON, RT_COLON);
21287 /* Scan the base-specifier-list. */
21288 while (true)
21290 cp_token *token;
21291 tree base;
21292 bool pack_expansion_p = false;
21294 /* Look for the base-specifier. */
21295 base = cp_parser_base_specifier (parser);
21296 /* Look for the (optional) ellipsis. */
21297 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21299 /* Consume the `...'. */
21300 cp_lexer_consume_token (parser->lexer);
21302 pack_expansion_p = true;
21305 /* Add BASE to the front of the list. */
21306 if (base && base != error_mark_node)
21308 if (pack_expansion_p)
21309 /* Make this a pack expansion type. */
21310 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21312 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21314 TREE_CHAIN (base) = bases;
21315 bases = base;
21318 /* Peek at the next token. */
21319 token = cp_lexer_peek_token (parser->lexer);
21320 /* If it's not a comma, then the list is complete. */
21321 if (token->type != CPP_COMMA)
21322 break;
21323 /* Consume the `,'. */
21324 cp_lexer_consume_token (parser->lexer);
21327 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21328 base class had a qualified name. However, the next name that
21329 appears is certainly not qualified. */
21330 parser->scope = NULL_TREE;
21331 parser->qualifying_scope = NULL_TREE;
21332 parser->object_scope = NULL_TREE;
21334 return nreverse (bases);
21337 /* Parse a base-specifier.
21339 base-specifier:
21340 :: [opt] nested-name-specifier [opt] class-name
21341 virtual access-specifier [opt] :: [opt] nested-name-specifier
21342 [opt] class-name
21343 access-specifier virtual [opt] :: [opt] nested-name-specifier
21344 [opt] class-name
21346 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21347 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21348 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21349 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21351 static tree
21352 cp_parser_base_specifier (cp_parser* parser)
21354 cp_token *token;
21355 bool done = false;
21356 bool virtual_p = false;
21357 bool duplicate_virtual_error_issued_p = false;
21358 bool duplicate_access_error_issued_p = false;
21359 bool class_scope_p, template_p;
21360 tree access = access_default_node;
21361 tree type;
21363 /* Process the optional `virtual' and `access-specifier'. */
21364 while (!done)
21366 /* Peek at the next token. */
21367 token = cp_lexer_peek_token (parser->lexer);
21368 /* Process `virtual'. */
21369 switch (token->keyword)
21371 case RID_VIRTUAL:
21372 /* If `virtual' appears more than once, issue an error. */
21373 if (virtual_p && !duplicate_virtual_error_issued_p)
21375 cp_parser_error (parser,
21376 "%<virtual%> specified more than once in base-specified");
21377 duplicate_virtual_error_issued_p = true;
21380 virtual_p = true;
21382 /* Consume the `virtual' token. */
21383 cp_lexer_consume_token (parser->lexer);
21385 break;
21387 case RID_PUBLIC:
21388 case RID_PROTECTED:
21389 case RID_PRIVATE:
21390 /* If more than one access specifier appears, issue an
21391 error. */
21392 if (access != access_default_node
21393 && !duplicate_access_error_issued_p)
21395 cp_parser_error (parser,
21396 "more than one access specifier in base-specified");
21397 duplicate_access_error_issued_p = true;
21400 access = ridpointers[(int) token->keyword];
21402 /* Consume the access-specifier. */
21403 cp_lexer_consume_token (parser->lexer);
21405 break;
21407 default:
21408 done = true;
21409 break;
21412 /* It is not uncommon to see programs mechanically, erroneously, use
21413 the 'typename' keyword to denote (dependent) qualified types
21414 as base classes. */
21415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21417 token = cp_lexer_peek_token (parser->lexer);
21418 if (!processing_template_decl)
21419 error_at (token->location,
21420 "keyword %<typename%> not allowed outside of templates");
21421 else
21422 error_at (token->location,
21423 "keyword %<typename%> not allowed in this context "
21424 "(the base class is implicitly a type)");
21425 cp_lexer_consume_token (parser->lexer);
21428 /* Look for the optional `::' operator. */
21429 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21430 /* Look for the nested-name-specifier. The simplest way to
21431 implement:
21433 [temp.res]
21435 The keyword `typename' is not permitted in a base-specifier or
21436 mem-initializer; in these contexts a qualified name that
21437 depends on a template-parameter is implicitly assumed to be a
21438 type name.
21440 is to pretend that we have seen the `typename' keyword at this
21441 point. */
21442 cp_parser_nested_name_specifier_opt (parser,
21443 /*typename_keyword_p=*/true,
21444 /*check_dependency_p=*/true,
21445 typename_type,
21446 /*is_declaration=*/true);
21447 /* If the base class is given by a qualified name, assume that names
21448 we see are type names or templates, as appropriate. */
21449 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21450 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21452 if (!parser->scope
21453 && cp_lexer_next_token_is_decltype (parser->lexer))
21454 /* DR 950 allows decltype as a base-specifier. */
21455 type = cp_parser_decltype (parser);
21456 else
21458 /* Otherwise, look for the class-name. */
21459 type = cp_parser_class_name (parser,
21460 class_scope_p,
21461 template_p,
21462 typename_type,
21463 /*check_dependency_p=*/true,
21464 /*class_head_p=*/false,
21465 /*is_declaration=*/true);
21466 type = TREE_TYPE (type);
21469 if (type == error_mark_node)
21470 return error_mark_node;
21472 return finish_base_specifier (type, access, virtual_p);
21475 /* Exception handling [gram.exception] */
21477 /* Parse an (optional) noexcept-specification.
21479 noexcept-specification:
21480 noexcept ( constant-expression ) [opt]
21482 If no noexcept-specification is present, returns NULL_TREE.
21483 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21484 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21485 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21486 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21487 in which case a boolean condition is returned instead. */
21489 static tree
21490 cp_parser_noexcept_specification_opt (cp_parser* parser,
21491 bool require_constexpr,
21492 bool* consumed_expr,
21493 bool return_cond)
21495 cp_token *token;
21496 const char *saved_message;
21498 /* Peek at the next token. */
21499 token = cp_lexer_peek_token (parser->lexer);
21501 /* Is it a noexcept-specification? */
21502 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21504 tree expr;
21505 cp_lexer_consume_token (parser->lexer);
21507 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21509 cp_lexer_consume_token (parser->lexer);
21511 if (require_constexpr)
21513 /* Types may not be defined in an exception-specification. */
21514 saved_message = parser->type_definition_forbidden_message;
21515 parser->type_definition_forbidden_message
21516 = G_("types may not be defined in an exception-specification");
21518 expr = cp_parser_constant_expression (parser);
21520 /* Restore the saved message. */
21521 parser->type_definition_forbidden_message = saved_message;
21523 else
21525 expr = cp_parser_expression (parser);
21526 *consumed_expr = true;
21529 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21531 else
21533 expr = boolean_true_node;
21534 if (!require_constexpr)
21535 *consumed_expr = false;
21538 /* We cannot build a noexcept-spec right away because this will check
21539 that expr is a constexpr. */
21540 if (!return_cond)
21541 return build_noexcept_spec (expr, tf_warning_or_error);
21542 else
21543 return expr;
21545 else
21546 return NULL_TREE;
21549 /* Parse an (optional) exception-specification.
21551 exception-specification:
21552 throw ( type-id-list [opt] )
21554 Returns a TREE_LIST representing the exception-specification. The
21555 TREE_VALUE of each node is a type. */
21557 static tree
21558 cp_parser_exception_specification_opt (cp_parser* parser)
21560 cp_token *token;
21561 tree type_id_list;
21562 const char *saved_message;
21564 /* Peek at the next token. */
21565 token = cp_lexer_peek_token (parser->lexer);
21567 /* Is it a noexcept-specification? */
21568 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21569 false);
21570 if (type_id_list != NULL_TREE)
21571 return type_id_list;
21573 /* If it's not `throw', then there's no exception-specification. */
21574 if (!cp_parser_is_keyword (token, RID_THROW))
21575 return NULL_TREE;
21577 #if 0
21578 /* Enable this once a lot of code has transitioned to noexcept? */
21579 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21580 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21581 "deprecated in C++0x; use %<noexcept%> instead");
21582 #endif
21584 /* Consume the `throw'. */
21585 cp_lexer_consume_token (parser->lexer);
21587 /* Look for the `('. */
21588 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21590 /* Peek at the next token. */
21591 token = cp_lexer_peek_token (parser->lexer);
21592 /* If it's not a `)', then there is a type-id-list. */
21593 if (token->type != CPP_CLOSE_PAREN)
21595 /* Types may not be defined in an exception-specification. */
21596 saved_message = parser->type_definition_forbidden_message;
21597 parser->type_definition_forbidden_message
21598 = G_("types may not be defined in an exception-specification");
21599 /* Parse the type-id-list. */
21600 type_id_list = cp_parser_type_id_list (parser);
21601 /* Restore the saved message. */
21602 parser->type_definition_forbidden_message = saved_message;
21604 else
21605 type_id_list = empty_except_spec;
21607 /* Look for the `)'. */
21608 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21610 return type_id_list;
21613 /* Parse an (optional) type-id-list.
21615 type-id-list:
21616 type-id ... [opt]
21617 type-id-list , type-id ... [opt]
21619 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21620 in the order that the types were presented. */
21622 static tree
21623 cp_parser_type_id_list (cp_parser* parser)
21625 tree types = NULL_TREE;
21627 while (true)
21629 cp_token *token;
21630 tree type;
21632 /* Get the next type-id. */
21633 type = cp_parser_type_id (parser);
21634 /* Parse the optional ellipsis. */
21635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21637 /* Consume the `...'. */
21638 cp_lexer_consume_token (parser->lexer);
21640 /* Turn the type into a pack expansion expression. */
21641 type = make_pack_expansion (type);
21643 /* Add it to the list. */
21644 types = add_exception_specifier (types, type, /*complain=*/1);
21645 /* Peek at the next token. */
21646 token = cp_lexer_peek_token (parser->lexer);
21647 /* If it is not a `,', we are done. */
21648 if (token->type != CPP_COMMA)
21649 break;
21650 /* Consume the `,'. */
21651 cp_lexer_consume_token (parser->lexer);
21654 return nreverse (types);
21657 /* Parse a try-block.
21659 try-block:
21660 try compound-statement handler-seq */
21662 static tree
21663 cp_parser_try_block (cp_parser* parser)
21665 tree try_block;
21667 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21668 if (parser->in_function_body
21669 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21670 error ("%<try%> in %<constexpr%> function");
21672 try_block = begin_try_block ();
21673 cp_parser_compound_statement (parser, NULL, true, false);
21674 finish_try_block (try_block);
21675 cp_parser_handler_seq (parser);
21676 finish_handler_sequence (try_block);
21678 return try_block;
21681 /* Parse a function-try-block.
21683 function-try-block:
21684 try ctor-initializer [opt] function-body handler-seq */
21686 static bool
21687 cp_parser_function_try_block (cp_parser* parser)
21689 tree compound_stmt;
21690 tree try_block;
21691 bool ctor_initializer_p;
21693 /* Look for the `try' keyword. */
21694 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21695 return false;
21696 /* Let the rest of the front end know where we are. */
21697 try_block = begin_function_try_block (&compound_stmt);
21698 /* Parse the function-body. */
21699 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21700 (parser, /*in_function_try_block=*/true);
21701 /* We're done with the `try' part. */
21702 finish_function_try_block (try_block);
21703 /* Parse the handlers. */
21704 cp_parser_handler_seq (parser);
21705 /* We're done with the handlers. */
21706 finish_function_handler_sequence (try_block, compound_stmt);
21708 return ctor_initializer_p;
21711 /* Parse a handler-seq.
21713 handler-seq:
21714 handler handler-seq [opt] */
21716 static void
21717 cp_parser_handler_seq (cp_parser* parser)
21719 while (true)
21721 cp_token *token;
21723 /* Parse the handler. */
21724 cp_parser_handler (parser);
21725 /* Peek at the next token. */
21726 token = cp_lexer_peek_token (parser->lexer);
21727 /* If it's not `catch' then there are no more handlers. */
21728 if (!cp_parser_is_keyword (token, RID_CATCH))
21729 break;
21733 /* Parse a handler.
21735 handler:
21736 catch ( exception-declaration ) compound-statement */
21738 static void
21739 cp_parser_handler (cp_parser* parser)
21741 tree handler;
21742 tree declaration;
21744 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21745 handler = begin_handler ();
21746 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21747 declaration = cp_parser_exception_declaration (parser);
21748 finish_handler_parms (declaration, handler);
21749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21750 cp_parser_compound_statement (parser, NULL, false, false);
21751 finish_handler (handler);
21754 /* Parse an exception-declaration.
21756 exception-declaration:
21757 type-specifier-seq declarator
21758 type-specifier-seq abstract-declarator
21759 type-specifier-seq
21762 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21763 ellipsis variant is used. */
21765 static tree
21766 cp_parser_exception_declaration (cp_parser* parser)
21768 cp_decl_specifier_seq type_specifiers;
21769 cp_declarator *declarator;
21770 const char *saved_message;
21772 /* If it's an ellipsis, it's easy to handle. */
21773 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21775 /* Consume the `...' token. */
21776 cp_lexer_consume_token (parser->lexer);
21777 return NULL_TREE;
21780 /* Types may not be defined in exception-declarations. */
21781 saved_message = parser->type_definition_forbidden_message;
21782 parser->type_definition_forbidden_message
21783 = G_("types may not be defined in exception-declarations");
21785 /* Parse the type-specifier-seq. */
21786 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21787 /*is_trailing_return=*/false,
21788 &type_specifiers);
21789 /* If it's a `)', then there is no declarator. */
21790 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21791 declarator = NULL;
21792 else
21793 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21794 /*ctor_dtor_or_conv_p=*/NULL,
21795 /*parenthesized_p=*/NULL,
21796 /*member_p=*/false,
21797 /*friend_p=*/false);
21799 /* Restore the saved message. */
21800 parser->type_definition_forbidden_message = saved_message;
21802 if (!type_specifiers.any_specifiers_p)
21803 return error_mark_node;
21805 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21808 /* Parse a throw-expression.
21810 throw-expression:
21811 throw assignment-expression [opt]
21813 Returns a THROW_EXPR representing the throw-expression. */
21815 static tree
21816 cp_parser_throw_expression (cp_parser* parser)
21818 tree expression;
21819 cp_token* token;
21821 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21822 token = cp_lexer_peek_token (parser->lexer);
21823 /* Figure out whether or not there is an assignment-expression
21824 following the "throw" keyword. */
21825 if (token->type == CPP_COMMA
21826 || token->type == CPP_SEMICOLON
21827 || token->type == CPP_CLOSE_PAREN
21828 || token->type == CPP_CLOSE_SQUARE
21829 || token->type == CPP_CLOSE_BRACE
21830 || token->type == CPP_COLON)
21831 expression = NULL_TREE;
21832 else
21833 expression = cp_parser_assignment_expression (parser);
21835 return build_throw (expression);
21838 /* GNU Extensions */
21840 /* Parse an (optional) asm-specification.
21842 asm-specification:
21843 asm ( string-literal )
21845 If the asm-specification is present, returns a STRING_CST
21846 corresponding to the string-literal. Otherwise, returns
21847 NULL_TREE. */
21849 static tree
21850 cp_parser_asm_specification_opt (cp_parser* parser)
21852 cp_token *token;
21853 tree asm_specification;
21855 /* Peek at the next token. */
21856 token = cp_lexer_peek_token (parser->lexer);
21857 /* If the next token isn't the `asm' keyword, then there's no
21858 asm-specification. */
21859 if (!cp_parser_is_keyword (token, RID_ASM))
21860 return NULL_TREE;
21862 /* Consume the `asm' token. */
21863 cp_lexer_consume_token (parser->lexer);
21864 /* Look for the `('. */
21865 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21867 /* Look for the string-literal. */
21868 asm_specification = cp_parser_string_literal (parser, false, false);
21870 /* Look for the `)'. */
21871 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21873 return asm_specification;
21876 /* Parse an asm-operand-list.
21878 asm-operand-list:
21879 asm-operand
21880 asm-operand-list , asm-operand
21882 asm-operand:
21883 string-literal ( expression )
21884 [ string-literal ] string-literal ( expression )
21886 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21887 each node is the expression. The TREE_PURPOSE is itself a
21888 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21889 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21890 is a STRING_CST for the string literal before the parenthesis. Returns
21891 ERROR_MARK_NODE if any of the operands are invalid. */
21893 static tree
21894 cp_parser_asm_operand_list (cp_parser* parser)
21896 tree asm_operands = NULL_TREE;
21897 bool invalid_operands = false;
21899 while (true)
21901 tree string_literal;
21902 tree expression;
21903 tree name;
21905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21907 /* Consume the `[' token. */
21908 cp_lexer_consume_token (parser->lexer);
21909 /* Read the operand name. */
21910 name = cp_parser_identifier (parser);
21911 if (name != error_mark_node)
21912 name = build_string (IDENTIFIER_LENGTH (name),
21913 IDENTIFIER_POINTER (name));
21914 /* Look for the closing `]'. */
21915 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21917 else
21918 name = NULL_TREE;
21919 /* Look for the string-literal. */
21920 string_literal = cp_parser_string_literal (parser, false, false);
21922 /* Look for the `('. */
21923 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21924 /* Parse the expression. */
21925 expression = cp_parser_expression (parser);
21926 /* Look for the `)'. */
21927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21929 if (name == error_mark_node
21930 || string_literal == error_mark_node
21931 || expression == error_mark_node)
21932 invalid_operands = true;
21934 /* Add this operand to the list. */
21935 asm_operands = tree_cons (build_tree_list (name, string_literal),
21936 expression,
21937 asm_operands);
21938 /* If the next token is not a `,', there are no more
21939 operands. */
21940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21941 break;
21942 /* Consume the `,'. */
21943 cp_lexer_consume_token (parser->lexer);
21946 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21949 /* Parse an asm-clobber-list.
21951 asm-clobber-list:
21952 string-literal
21953 asm-clobber-list , string-literal
21955 Returns a TREE_LIST, indicating the clobbers in the order that they
21956 appeared. The TREE_VALUE of each node is a STRING_CST. */
21958 static tree
21959 cp_parser_asm_clobber_list (cp_parser* parser)
21961 tree clobbers = NULL_TREE;
21963 while (true)
21965 tree string_literal;
21967 /* Look for the string literal. */
21968 string_literal = cp_parser_string_literal (parser, false, false);
21969 /* Add it to the list. */
21970 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21971 /* If the next token is not a `,', then the list is
21972 complete. */
21973 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21974 break;
21975 /* Consume the `,' token. */
21976 cp_lexer_consume_token (parser->lexer);
21979 return clobbers;
21982 /* Parse an asm-label-list.
21984 asm-label-list:
21985 identifier
21986 asm-label-list , identifier
21988 Returns a TREE_LIST, indicating the labels in the order that they
21989 appeared. The TREE_VALUE of each node is a label. */
21991 static tree
21992 cp_parser_asm_label_list (cp_parser* parser)
21994 tree labels = NULL_TREE;
21996 while (true)
21998 tree identifier, label, name;
22000 /* Look for the identifier. */
22001 identifier = cp_parser_identifier (parser);
22002 if (!error_operand_p (identifier))
22004 label = lookup_label (identifier);
22005 if (TREE_CODE (label) == LABEL_DECL)
22007 TREE_USED (label) = 1;
22008 check_goto (label);
22009 name = build_string (IDENTIFIER_LENGTH (identifier),
22010 IDENTIFIER_POINTER (identifier));
22011 labels = tree_cons (name, label, labels);
22014 /* If the next token is not a `,', then the list is
22015 complete. */
22016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22017 break;
22018 /* Consume the `,' token. */
22019 cp_lexer_consume_token (parser->lexer);
22022 return nreverse (labels);
22025 /* Return TRUE iff the next tokens in the stream are possibly the
22026 beginning of a GNU extension attribute. */
22028 static bool
22029 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22031 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22034 /* Return TRUE iff the next tokens in the stream are possibly the
22035 beginning of a standard C++-11 attribute specifier. */
22037 static bool
22038 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22040 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22043 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22044 beginning of a standard C++-11 attribute specifier. */
22046 static bool
22047 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22049 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22051 return (cxx_dialect >= cxx11
22052 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22053 || (token->type == CPP_OPEN_SQUARE
22054 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22055 && token->type == CPP_OPEN_SQUARE)));
22058 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22059 beginning of a GNU extension attribute. */
22061 static bool
22062 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22064 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22066 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22069 /* Return true iff the next tokens can be the beginning of either a
22070 GNU attribute list, or a standard C++11 attribute sequence. */
22072 static bool
22073 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22075 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22076 || cp_next_tokens_can_be_std_attribute_p (parser));
22079 /* Return true iff the next Nth tokens can be the beginning of either
22080 a GNU attribute list, or a standard C++11 attribute sequence. */
22082 static bool
22083 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22085 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22086 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22089 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22090 of GNU attributes, or return NULL. */
22092 static tree
22093 cp_parser_attributes_opt (cp_parser *parser)
22095 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22096 return cp_parser_gnu_attributes_opt (parser);
22097 return cp_parser_std_attribute_spec_seq (parser);
22100 #define CILK_SIMD_FN_CLAUSE_MASK \
22101 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22102 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22103 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22104 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22105 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22107 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22108 vector [(<clauses>)] */
22110 static void
22111 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22113 bool first_p = parser->cilk_simd_fn_info == NULL;
22114 cp_token *token = v_token;
22115 if (first_p)
22117 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22118 parser->cilk_simd_fn_info->error_seen = false;
22119 parser->cilk_simd_fn_info->fndecl_seen = false;
22120 parser->cilk_simd_fn_info->tokens = vNULL;
22122 int paren_scope = 0;
22123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22125 cp_lexer_consume_token (parser->lexer);
22126 v_token = cp_lexer_peek_token (parser->lexer);
22127 paren_scope++;
22129 while (paren_scope > 0)
22131 token = cp_lexer_peek_token (parser->lexer);
22132 if (token->type == CPP_OPEN_PAREN)
22133 paren_scope++;
22134 else if (token->type == CPP_CLOSE_PAREN)
22135 paren_scope--;
22136 /* Do not push the last ')' */
22137 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22138 cp_lexer_consume_token (parser->lexer);
22141 token->type = CPP_PRAGMA_EOL;
22142 parser->lexer->next_token = token;
22143 cp_lexer_consume_token (parser->lexer);
22145 struct cp_token_cache *cp
22146 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22147 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22150 /* Parse an (optional) series of attributes.
22152 attributes:
22153 attributes attribute
22155 attribute:
22156 __attribute__ (( attribute-list [opt] ))
22158 The return value is as for cp_parser_gnu_attribute_list. */
22160 static tree
22161 cp_parser_gnu_attributes_opt (cp_parser* parser)
22163 tree attributes = NULL_TREE;
22165 while (true)
22167 cp_token *token;
22168 tree attribute_list;
22169 bool ok = true;
22171 /* Peek at the next token. */
22172 token = cp_lexer_peek_token (parser->lexer);
22173 /* If it's not `__attribute__', then we're done. */
22174 if (token->keyword != RID_ATTRIBUTE)
22175 break;
22177 /* Consume the `__attribute__' keyword. */
22178 cp_lexer_consume_token (parser->lexer);
22179 /* Look for the two `(' tokens. */
22180 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22181 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22183 /* Peek at the next token. */
22184 token = cp_lexer_peek_token (parser->lexer);
22185 if (token->type != CPP_CLOSE_PAREN)
22186 /* Parse the attribute-list. */
22187 attribute_list = cp_parser_gnu_attribute_list (parser);
22188 else
22189 /* If the next token is a `)', then there is no attribute
22190 list. */
22191 attribute_list = NULL;
22193 /* Look for the two `)' tokens. */
22194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22195 ok = false;
22196 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22197 ok = false;
22198 if (!ok)
22199 cp_parser_skip_to_end_of_statement (parser);
22201 /* Add these new attributes to the list. */
22202 attributes = chainon (attributes, attribute_list);
22205 return attributes;
22208 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22209 "__vector" or "__vector__." */
22211 static inline bool
22212 is_cilkplus_vector_p (tree name)
22214 if (flag_cilkplus && is_attribute_p ("vector", name))
22215 return true;
22216 return false;
22219 /* Parse a GNU attribute-list.
22221 attribute-list:
22222 attribute
22223 attribute-list , attribute
22225 attribute:
22226 identifier
22227 identifier ( identifier )
22228 identifier ( identifier , expression-list )
22229 identifier ( expression-list )
22231 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22232 to an attribute. The TREE_PURPOSE of each node is the identifier
22233 indicating which attribute is in use. The TREE_VALUE represents
22234 the arguments, if any. */
22236 static tree
22237 cp_parser_gnu_attribute_list (cp_parser* parser)
22239 tree attribute_list = NULL_TREE;
22240 bool save_translate_strings_p = parser->translate_strings_p;
22242 parser->translate_strings_p = false;
22243 while (true)
22245 cp_token *token;
22246 tree identifier;
22247 tree attribute;
22249 /* Look for the identifier. We also allow keywords here; for
22250 example `__attribute__ ((const))' is legal. */
22251 token = cp_lexer_peek_token (parser->lexer);
22252 if (token->type == CPP_NAME
22253 || token->type == CPP_KEYWORD)
22255 tree arguments = NULL_TREE;
22257 /* Consume the token, but save it since we need it for the
22258 SIMD enabled function parsing. */
22259 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22261 /* Save away the identifier that indicates which attribute
22262 this is. */
22263 identifier = (token->type == CPP_KEYWORD)
22264 /* For keywords, use the canonical spelling, not the
22265 parsed identifier. */
22266 ? ridpointers[(int) token->keyword]
22267 : id_token->u.value;
22269 attribute = build_tree_list (identifier, NULL_TREE);
22271 /* Peek at the next token. */
22272 token = cp_lexer_peek_token (parser->lexer);
22273 /* If it's an `(', then parse the attribute arguments. */
22274 if (token->type == CPP_OPEN_PAREN)
22276 vec<tree, va_gc> *vec;
22277 int attr_flag = (attribute_takes_identifier_p (identifier)
22278 ? id_attr : normal_attr);
22279 if (is_cilkplus_vector_p (identifier))
22281 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22282 continue;
22284 else
22285 vec = cp_parser_parenthesized_expression_list
22286 (parser, attr_flag, /*cast_p=*/false,
22287 /*allow_expansion_p=*/false,
22288 /*non_constant_p=*/NULL);
22289 if (vec == NULL)
22290 arguments = error_mark_node;
22291 else
22293 arguments = build_tree_list_vec (vec);
22294 release_tree_vector (vec);
22296 /* Save the arguments away. */
22297 TREE_VALUE (attribute) = arguments;
22299 else if (is_cilkplus_vector_p (identifier))
22301 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22302 continue;
22305 if (arguments != error_mark_node)
22307 /* Add this attribute to the list. */
22308 TREE_CHAIN (attribute) = attribute_list;
22309 attribute_list = attribute;
22312 token = cp_lexer_peek_token (parser->lexer);
22314 /* Now, look for more attributes. If the next token isn't a
22315 `,', we're done. */
22316 if (token->type != CPP_COMMA)
22317 break;
22319 /* Consume the comma and keep going. */
22320 cp_lexer_consume_token (parser->lexer);
22322 parser->translate_strings_p = save_translate_strings_p;
22324 /* We built up the list in reverse order. */
22325 return nreverse (attribute_list);
22328 /* Parse a standard C++11 attribute.
22330 The returned representation is a TREE_LIST which TREE_PURPOSE is
22331 the scoped name of the attribute, and the TREE_VALUE is its
22332 arguments list.
22334 Note that the scoped name of the attribute is itself a TREE_LIST
22335 which TREE_PURPOSE is the namespace of the attribute, and
22336 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22337 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22338 and which TREE_PURPOSE is directly the attribute name.
22340 Clients of the attribute code should use get_attribute_namespace
22341 and get_attribute_name to get the actual namespace and name of
22342 attributes, regardless of their being GNU or C++11 attributes.
22344 attribute:
22345 attribute-token attribute-argument-clause [opt]
22347 attribute-token:
22348 identifier
22349 attribute-scoped-token
22351 attribute-scoped-token:
22352 attribute-namespace :: identifier
22354 attribute-namespace:
22355 identifier
22357 attribute-argument-clause:
22358 ( balanced-token-seq )
22360 balanced-token-seq:
22361 balanced-token [opt]
22362 balanced-token-seq balanced-token
22364 balanced-token:
22365 ( balanced-token-seq )
22366 [ balanced-token-seq ]
22367 { balanced-token-seq }. */
22369 static tree
22370 cp_parser_std_attribute (cp_parser *parser)
22372 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22373 cp_token *token;
22375 /* First, parse name of the the attribute, a.k.a
22376 attribute-token. */
22378 token = cp_lexer_peek_token (parser->lexer);
22379 if (token->type == CPP_NAME)
22380 attr_id = token->u.value;
22381 else if (token->type == CPP_KEYWORD)
22382 attr_id = ridpointers[(int) token->keyword];
22383 else if (token->flags & NAMED_OP)
22384 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22386 if (attr_id == NULL_TREE)
22387 return NULL_TREE;
22389 cp_lexer_consume_token (parser->lexer);
22391 token = cp_lexer_peek_token (parser->lexer);
22392 if (token->type == CPP_SCOPE)
22394 /* We are seeing a scoped attribute token. */
22396 cp_lexer_consume_token (parser->lexer);
22397 attr_ns = attr_id;
22399 token = cp_lexer_consume_token (parser->lexer);
22400 if (token->type == CPP_NAME)
22401 attr_id = token->u.value;
22402 else if (token->type == CPP_KEYWORD)
22403 attr_id = ridpointers[(int) token->keyword];
22404 else
22406 error_at (token->location,
22407 "expected an identifier for the attribute name");
22408 return error_mark_node;
22410 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22411 NULL_TREE);
22412 token = cp_lexer_peek_token (parser->lexer);
22414 else
22416 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22417 NULL_TREE);
22418 /* C++11 noreturn attribute is equivalent to GNU's. */
22419 if (is_attribute_p ("noreturn", attr_id))
22420 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22421 /* C++14 deprecated attribute is equivalent to GNU's. */
22422 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22424 if (cxx_dialect == cxx11)
22425 pedwarn (token->location, OPT_Wpedantic,
22426 "%<deprecated%> is a C++14 feature;"
22427 " use %<gnu::deprecated%>");
22428 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22432 /* Now parse the optional argument clause of the attribute. */
22434 if (token->type != CPP_OPEN_PAREN)
22435 return attribute;
22438 vec<tree, va_gc> *vec;
22439 int attr_flag = normal_attr;
22441 if (attr_ns == get_identifier ("gnu")
22442 && attribute_takes_identifier_p (attr_id))
22443 /* A GNU attribute that takes an identifier in parameter. */
22444 attr_flag = id_attr;
22446 vec = cp_parser_parenthesized_expression_list
22447 (parser, attr_flag, /*cast_p=*/false,
22448 /*allow_expansion_p=*/true,
22449 /*non_constant_p=*/NULL);
22450 if (vec == NULL)
22451 arguments = error_mark_node;
22452 else
22454 arguments = build_tree_list_vec (vec);
22455 release_tree_vector (vec);
22458 if (arguments == error_mark_node)
22459 attribute = error_mark_node;
22460 else
22461 TREE_VALUE (attribute) = arguments;
22464 return attribute;
22467 /* Parse a list of standard C++-11 attributes.
22469 attribute-list:
22470 attribute [opt]
22471 attribute-list , attribute[opt]
22472 attribute ...
22473 attribute-list , attribute ...
22476 static tree
22477 cp_parser_std_attribute_list (cp_parser *parser)
22479 tree attributes = NULL_TREE, attribute = NULL_TREE;
22480 cp_token *token = NULL;
22482 while (true)
22484 attribute = cp_parser_std_attribute (parser);
22485 if (attribute == error_mark_node)
22486 break;
22487 if (attribute != NULL_TREE)
22489 TREE_CHAIN (attribute) = attributes;
22490 attributes = attribute;
22492 token = cp_lexer_peek_token (parser->lexer);
22493 if (token->type != CPP_COMMA)
22494 break;
22495 cp_lexer_consume_token (parser->lexer);
22497 attributes = nreverse (attributes);
22498 return attributes;
22501 /* Parse a standard C++-11 attribute specifier.
22503 attribute-specifier:
22504 [ [ attribute-list ] ]
22505 alignment-specifier
22507 alignment-specifier:
22508 alignas ( type-id ... [opt] )
22509 alignas ( alignment-expression ... [opt] ). */
22511 static tree
22512 cp_parser_std_attribute_spec (cp_parser *parser)
22514 tree attributes = NULL_TREE;
22515 cp_token *token = cp_lexer_peek_token (parser->lexer);
22517 if (token->type == CPP_OPEN_SQUARE
22518 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22520 cp_lexer_consume_token (parser->lexer);
22521 cp_lexer_consume_token (parser->lexer);
22523 attributes = cp_parser_std_attribute_list (parser);
22525 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22526 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22527 cp_parser_skip_to_end_of_statement (parser);
22528 else
22529 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22530 when we are sure that we have actually parsed them. */
22531 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22533 else
22535 tree alignas_expr;
22537 /* Look for an alignment-specifier. */
22539 token = cp_lexer_peek_token (parser->lexer);
22541 if (token->type != CPP_KEYWORD
22542 || token->keyword != RID_ALIGNAS)
22543 return NULL_TREE;
22545 cp_lexer_consume_token (parser->lexer);
22546 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22548 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22550 cp_parser_error (parser, "expected %<(%>");
22551 return error_mark_node;
22554 cp_parser_parse_tentatively (parser);
22555 alignas_expr = cp_parser_type_id (parser);
22557 if (!cp_parser_parse_definitely (parser))
22559 gcc_assert (alignas_expr == error_mark_node
22560 || alignas_expr == NULL_TREE);
22562 alignas_expr =
22563 cp_parser_assignment_expression (parser);
22564 if (alignas_expr == error_mark_node)
22565 cp_parser_skip_to_end_of_statement (parser);
22566 if (alignas_expr == NULL_TREE
22567 || alignas_expr == error_mark_node)
22568 return alignas_expr;
22571 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22573 cp_parser_error (parser, "expected %<)%>");
22574 return error_mark_node;
22577 alignas_expr = cxx_alignas_expr (alignas_expr);
22579 /* Build the C++-11 representation of an 'aligned'
22580 attribute. */
22581 attributes =
22582 build_tree_list (build_tree_list (get_identifier ("gnu"),
22583 get_identifier ("aligned")),
22584 build_tree_list (NULL_TREE, alignas_expr));
22587 return attributes;
22590 /* Parse a standard C++-11 attribute-specifier-seq.
22592 attribute-specifier-seq:
22593 attribute-specifier-seq [opt] attribute-specifier
22596 static tree
22597 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22599 tree attr_specs = NULL;
22601 while (true)
22603 tree attr_spec = cp_parser_std_attribute_spec (parser);
22604 if (attr_spec == NULL_TREE)
22605 break;
22606 if (attr_spec == error_mark_node)
22607 return error_mark_node;
22609 TREE_CHAIN (attr_spec) = attr_specs;
22610 attr_specs = attr_spec;
22613 attr_specs = nreverse (attr_specs);
22614 return attr_specs;
22617 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22618 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22619 current value of the PEDANTIC flag, regardless of whether or not
22620 the `__extension__' keyword is present. The caller is responsible
22621 for restoring the value of the PEDANTIC flag. */
22623 static bool
22624 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22626 /* Save the old value of the PEDANTIC flag. */
22627 *saved_pedantic = pedantic;
22629 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22631 /* Consume the `__extension__' token. */
22632 cp_lexer_consume_token (parser->lexer);
22633 /* We're not being pedantic while the `__extension__' keyword is
22634 in effect. */
22635 pedantic = 0;
22637 return true;
22640 return false;
22643 /* Parse a label declaration.
22645 label-declaration:
22646 __label__ label-declarator-seq ;
22648 label-declarator-seq:
22649 identifier , label-declarator-seq
22650 identifier */
22652 static void
22653 cp_parser_label_declaration (cp_parser* parser)
22655 /* Look for the `__label__' keyword. */
22656 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22658 while (true)
22660 tree identifier;
22662 /* Look for an identifier. */
22663 identifier = cp_parser_identifier (parser);
22664 /* If we failed, stop. */
22665 if (identifier == error_mark_node)
22666 break;
22667 /* Declare it as a label. */
22668 finish_label_decl (identifier);
22669 /* If the next token is a `;', stop. */
22670 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22671 break;
22672 /* Look for the `,' separating the label declarations. */
22673 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22676 /* Look for the final `;'. */
22677 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22680 /* Support Functions */
22682 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22683 NAME should have one of the representations used for an
22684 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22685 is returned. If PARSER->SCOPE is a dependent type, then a
22686 SCOPE_REF is returned.
22688 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22689 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22690 was formed. Abstractly, such entities should not be passed to this
22691 function, because they do not need to be looked up, but it is
22692 simpler to check for this special case here, rather than at the
22693 call-sites.
22695 In cases not explicitly covered above, this function returns a
22696 DECL, OVERLOAD, or baselink representing the result of the lookup.
22697 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22698 is returned.
22700 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22701 (e.g., "struct") that was used. In that case bindings that do not
22702 refer to types are ignored.
22704 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22705 ignored.
22707 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22708 are ignored.
22710 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22711 types.
22713 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22714 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22715 NULL_TREE otherwise. */
22717 static tree
22718 cp_parser_lookup_name (cp_parser *parser, tree name,
22719 enum tag_types tag_type,
22720 bool is_template,
22721 bool is_namespace,
22722 bool check_dependency,
22723 tree *ambiguous_decls,
22724 location_t name_location)
22726 tree decl;
22727 tree object_type = parser->context->object_type;
22729 /* Assume that the lookup will be unambiguous. */
22730 if (ambiguous_decls)
22731 *ambiguous_decls = NULL_TREE;
22733 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22734 no longer valid. Note that if we are parsing tentatively, and
22735 the parse fails, OBJECT_TYPE will be automatically restored. */
22736 parser->context->object_type = NULL_TREE;
22738 if (name == error_mark_node)
22739 return error_mark_node;
22741 /* A template-id has already been resolved; there is no lookup to
22742 do. */
22743 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22744 return name;
22745 if (BASELINK_P (name))
22747 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22748 == TEMPLATE_ID_EXPR);
22749 return name;
22752 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22753 it should already have been checked to make sure that the name
22754 used matches the type being destroyed. */
22755 if (TREE_CODE (name) == BIT_NOT_EXPR)
22757 tree type;
22759 /* Figure out to which type this destructor applies. */
22760 if (parser->scope)
22761 type = parser->scope;
22762 else if (object_type)
22763 type = object_type;
22764 else
22765 type = current_class_type;
22766 /* If that's not a class type, there is no destructor. */
22767 if (!type || !CLASS_TYPE_P (type))
22768 return error_mark_node;
22769 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22770 lazily_declare_fn (sfk_destructor, type);
22771 if (!CLASSTYPE_DESTRUCTORS (type))
22772 return error_mark_node;
22773 /* If it was a class type, return the destructor. */
22774 return CLASSTYPE_DESTRUCTORS (type);
22777 /* By this point, the NAME should be an ordinary identifier. If
22778 the id-expression was a qualified name, the qualifying scope is
22779 stored in PARSER->SCOPE at this point. */
22780 gcc_assert (identifier_p (name));
22782 /* Perform the lookup. */
22783 if (parser->scope)
22785 bool dependent_p;
22787 if (parser->scope == error_mark_node)
22788 return error_mark_node;
22790 /* If the SCOPE is dependent, the lookup must be deferred until
22791 the template is instantiated -- unless we are explicitly
22792 looking up names in uninstantiated templates. Even then, we
22793 cannot look up the name if the scope is not a class type; it
22794 might, for example, be a template type parameter. */
22795 dependent_p = (TYPE_P (parser->scope)
22796 && dependent_scope_p (parser->scope));
22797 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22798 && dependent_p)
22799 /* Defer lookup. */
22800 decl = error_mark_node;
22801 else
22803 tree pushed_scope = NULL_TREE;
22805 /* If PARSER->SCOPE is a dependent type, then it must be a
22806 class type, and we must not be checking dependencies;
22807 otherwise, we would have processed this lookup above. So
22808 that PARSER->SCOPE is not considered a dependent base by
22809 lookup_member, we must enter the scope here. */
22810 if (dependent_p)
22811 pushed_scope = push_scope (parser->scope);
22813 /* If the PARSER->SCOPE is a template specialization, it
22814 may be instantiated during name lookup. In that case,
22815 errors may be issued. Even if we rollback the current
22816 tentative parse, those errors are valid. */
22817 decl = lookup_qualified_name (parser->scope, name,
22818 tag_type != none_type,
22819 /*complain=*/true);
22821 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22822 lookup result and the nested-name-specifier nominates a class C:
22823 * if the name specified after the nested-name-specifier, when
22824 looked up in C, is the injected-class-name of C (Clause 9), or
22825 * if the name specified after the nested-name-specifier is the
22826 same as the identifier or the simple-template-id's template-
22827 name in the last component of the nested-name-specifier,
22828 the name is instead considered to name the constructor of
22829 class C. [ Note: for example, the constructor is not an
22830 acceptable lookup result in an elaborated-type-specifier so
22831 the constructor would not be used in place of the
22832 injected-class-name. --end note ] Such a constructor name
22833 shall be used only in the declarator-id of a declaration that
22834 names a constructor or in a using-declaration. */
22835 if (tag_type == none_type
22836 && DECL_SELF_REFERENCE_P (decl)
22837 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22838 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22839 tag_type != none_type,
22840 /*complain=*/true);
22842 /* If we have a single function from a using decl, pull it out. */
22843 if (TREE_CODE (decl) == OVERLOAD
22844 && !really_overloaded_fn (decl))
22845 decl = OVL_FUNCTION (decl);
22847 if (pushed_scope)
22848 pop_scope (pushed_scope);
22851 /* If the scope is a dependent type and either we deferred lookup or
22852 we did lookup but didn't find the name, rememeber the name. */
22853 if (decl == error_mark_node && TYPE_P (parser->scope)
22854 && dependent_type_p (parser->scope))
22856 if (tag_type)
22858 tree type;
22860 /* The resolution to Core Issue 180 says that `struct
22861 A::B' should be considered a type-name, even if `A'
22862 is dependent. */
22863 type = make_typename_type (parser->scope, name, tag_type,
22864 /*complain=*/tf_error);
22865 if (type != error_mark_node)
22866 decl = TYPE_NAME (type);
22868 else if (is_template
22869 && (cp_parser_next_token_ends_template_argument_p (parser)
22870 || cp_lexer_next_token_is (parser->lexer,
22871 CPP_CLOSE_PAREN)))
22872 decl = make_unbound_class_template (parser->scope,
22873 name, NULL_TREE,
22874 /*complain=*/tf_error);
22875 else
22876 decl = build_qualified_name (/*type=*/NULL_TREE,
22877 parser->scope, name,
22878 is_template);
22880 parser->qualifying_scope = parser->scope;
22881 parser->object_scope = NULL_TREE;
22883 else if (object_type)
22885 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22886 OBJECT_TYPE is not a class. */
22887 if (CLASS_TYPE_P (object_type))
22888 /* If the OBJECT_TYPE is a template specialization, it may
22889 be instantiated during name lookup. In that case, errors
22890 may be issued. Even if we rollback the current tentative
22891 parse, those errors are valid. */
22892 decl = lookup_member (object_type,
22893 name,
22894 /*protect=*/0,
22895 tag_type != none_type,
22896 tf_warning_or_error);
22897 else
22898 decl = NULL_TREE;
22900 if (!decl)
22901 /* Look it up in the enclosing context. */
22902 decl = lookup_name_real (name, tag_type != none_type,
22903 /*nonclass=*/0,
22904 /*block_p=*/true, is_namespace, 0);
22905 parser->object_scope = object_type;
22906 parser->qualifying_scope = NULL_TREE;
22908 else
22910 decl = lookup_name_real (name, tag_type != none_type,
22911 /*nonclass=*/0,
22912 /*block_p=*/true, is_namespace, 0);
22913 parser->qualifying_scope = NULL_TREE;
22914 parser->object_scope = NULL_TREE;
22917 /* If the lookup failed, let our caller know. */
22918 if (!decl || decl == error_mark_node)
22919 return error_mark_node;
22921 /* Pull out the template from an injected-class-name (or multiple). */
22922 if (is_template)
22923 decl = maybe_get_template_decl_from_type_decl (decl);
22925 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22926 if (TREE_CODE (decl) == TREE_LIST)
22928 if (ambiguous_decls)
22929 *ambiguous_decls = decl;
22930 /* The error message we have to print is too complicated for
22931 cp_parser_error, so we incorporate its actions directly. */
22932 if (!cp_parser_simulate_error (parser))
22934 error_at (name_location, "reference to %qD is ambiguous",
22935 name);
22936 print_candidates (decl);
22938 return error_mark_node;
22941 gcc_assert (DECL_P (decl)
22942 || TREE_CODE (decl) == OVERLOAD
22943 || TREE_CODE (decl) == SCOPE_REF
22944 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22945 || BASELINK_P (decl));
22947 /* If we have resolved the name of a member declaration, check to
22948 see if the declaration is accessible. When the name resolves to
22949 set of overloaded functions, accessibility is checked when
22950 overload resolution is done.
22952 During an explicit instantiation, access is not checked at all,
22953 as per [temp.explicit]. */
22954 if (DECL_P (decl))
22955 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22957 maybe_record_typedef_use (decl);
22959 return decl;
22962 /* Like cp_parser_lookup_name, but for use in the typical case where
22963 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22964 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22966 static tree
22967 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22969 return cp_parser_lookup_name (parser, name,
22970 none_type,
22971 /*is_template=*/false,
22972 /*is_namespace=*/false,
22973 /*check_dependency=*/true,
22974 /*ambiguous_decls=*/NULL,
22975 location);
22978 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22979 the current context, return the TYPE_DECL. If TAG_NAME_P is
22980 true, the DECL indicates the class being defined in a class-head,
22981 or declared in an elaborated-type-specifier.
22983 Otherwise, return DECL. */
22985 static tree
22986 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22988 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22989 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22991 struct A {
22992 template <typename T> struct B;
22995 template <typename T> struct A::B {};
22997 Similarly, in an elaborated-type-specifier:
22999 namespace N { struct X{}; }
23001 struct A {
23002 template <typename T> friend struct N::X;
23005 However, if the DECL refers to a class type, and we are in
23006 the scope of the class, then the name lookup automatically
23007 finds the TYPE_DECL created by build_self_reference rather
23008 than a TEMPLATE_DECL. For example, in:
23010 template <class T> struct S {
23011 S s;
23014 there is no need to handle such case. */
23016 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23017 return DECL_TEMPLATE_RESULT (decl);
23019 return decl;
23022 /* If too many, or too few, template-parameter lists apply to the
23023 declarator, issue an error message. Returns TRUE if all went well,
23024 and FALSE otherwise. */
23026 static bool
23027 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23028 cp_declarator *declarator,
23029 location_t declarator_location)
23031 switch (declarator->kind)
23033 case cdk_id:
23035 unsigned num_templates = 0;
23036 tree scope = declarator->u.id.qualifying_scope;
23038 if (scope)
23039 num_templates = num_template_headers_for_class (scope);
23040 else if (TREE_CODE (declarator->u.id.unqualified_name)
23041 == TEMPLATE_ID_EXPR)
23042 /* If the DECLARATOR has the form `X<y>' then it uses one
23043 additional level of template parameters. */
23044 ++num_templates;
23046 return cp_parser_check_template_parameters
23047 (parser, num_templates, declarator_location, declarator);
23050 case cdk_function:
23051 case cdk_array:
23052 case cdk_pointer:
23053 case cdk_reference:
23054 case cdk_ptrmem:
23055 return (cp_parser_check_declarator_template_parameters
23056 (parser, declarator->declarator, declarator_location));
23058 case cdk_error:
23059 return true;
23061 default:
23062 gcc_unreachable ();
23064 return false;
23067 /* NUM_TEMPLATES were used in the current declaration. If that is
23068 invalid, return FALSE and issue an error messages. Otherwise,
23069 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23070 declarator and we can print more accurate diagnostics. */
23072 static bool
23073 cp_parser_check_template_parameters (cp_parser* parser,
23074 unsigned num_templates,
23075 location_t location,
23076 cp_declarator *declarator)
23078 /* If there are the same number of template classes and parameter
23079 lists, that's OK. */
23080 if (parser->num_template_parameter_lists == num_templates)
23081 return true;
23082 /* If there are more, but only one more, then we are referring to a
23083 member template. That's OK too. */
23084 if (parser->num_template_parameter_lists == num_templates + 1)
23085 return true;
23086 /* If there are more template classes than parameter lists, we have
23087 something like:
23089 template <class T> void S<T>::R<T>::f (); */
23090 if (parser->num_template_parameter_lists < num_templates)
23092 if (declarator && !current_function_decl)
23093 error_at (location, "specializing member %<%T::%E%> "
23094 "requires %<template<>%> syntax",
23095 declarator->u.id.qualifying_scope,
23096 declarator->u.id.unqualified_name);
23097 else if (declarator)
23098 error_at (location, "invalid declaration of %<%T::%E%>",
23099 declarator->u.id.qualifying_scope,
23100 declarator->u.id.unqualified_name);
23101 else
23102 error_at (location, "too few template-parameter-lists");
23103 return false;
23105 /* Otherwise, there are too many template parameter lists. We have
23106 something like:
23108 template <class T> template <class U> void S::f(); */
23109 error_at (location, "too many template-parameter-lists");
23110 return false;
23113 /* Parse an optional `::' token indicating that the following name is
23114 from the global namespace. If so, PARSER->SCOPE is set to the
23115 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23116 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23117 Returns the new value of PARSER->SCOPE, if the `::' token is
23118 present, and NULL_TREE otherwise. */
23120 static tree
23121 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23123 cp_token *token;
23125 /* Peek at the next token. */
23126 token = cp_lexer_peek_token (parser->lexer);
23127 /* If we're looking at a `::' token then we're starting from the
23128 global namespace, not our current location. */
23129 if (token->type == CPP_SCOPE)
23131 /* Consume the `::' token. */
23132 cp_lexer_consume_token (parser->lexer);
23133 /* Set the SCOPE so that we know where to start the lookup. */
23134 parser->scope = global_namespace;
23135 parser->qualifying_scope = global_namespace;
23136 parser->object_scope = NULL_TREE;
23138 return parser->scope;
23140 else if (!current_scope_valid_p)
23142 parser->scope = NULL_TREE;
23143 parser->qualifying_scope = NULL_TREE;
23144 parser->object_scope = NULL_TREE;
23147 return NULL_TREE;
23150 /* Returns TRUE if the upcoming token sequence is the start of a
23151 constructor declarator. If FRIEND_P is true, the declarator is
23152 preceded by the `friend' specifier. */
23154 static bool
23155 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23157 bool constructor_p;
23158 bool outside_class_specifier_p;
23159 tree nested_name_specifier;
23160 cp_token *next_token;
23162 /* The common case is that this is not a constructor declarator, so
23163 try to avoid doing lots of work if at all possible. It's not
23164 valid declare a constructor at function scope. */
23165 if (parser->in_function_body)
23166 return false;
23167 /* And only certain tokens can begin a constructor declarator. */
23168 next_token = cp_lexer_peek_token (parser->lexer);
23169 if (next_token->type != CPP_NAME
23170 && next_token->type != CPP_SCOPE
23171 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23172 && next_token->type != CPP_TEMPLATE_ID)
23173 return false;
23175 /* Parse tentatively; we are going to roll back all of the tokens
23176 consumed here. */
23177 cp_parser_parse_tentatively (parser);
23178 /* Assume that we are looking at a constructor declarator. */
23179 constructor_p = true;
23181 /* Look for the optional `::' operator. */
23182 cp_parser_global_scope_opt (parser,
23183 /*current_scope_valid_p=*/false);
23184 /* Look for the nested-name-specifier. */
23185 nested_name_specifier
23186 = (cp_parser_nested_name_specifier_opt (parser,
23187 /*typename_keyword_p=*/false,
23188 /*check_dependency_p=*/false,
23189 /*type_p=*/false,
23190 /*is_declaration=*/false));
23192 outside_class_specifier_p = (!at_class_scope_p ()
23193 || !TYPE_BEING_DEFINED (current_class_type)
23194 || friend_p);
23196 /* Outside of a class-specifier, there must be a
23197 nested-name-specifier. */
23198 if (!nested_name_specifier && outside_class_specifier_p)
23199 constructor_p = false;
23200 else if (nested_name_specifier == error_mark_node)
23201 constructor_p = false;
23203 /* If we have a class scope, this is easy; DR 147 says that S::S always
23204 names the constructor, and no other qualified name could. */
23205 if (constructor_p && nested_name_specifier
23206 && CLASS_TYPE_P (nested_name_specifier))
23208 tree id = cp_parser_unqualified_id (parser,
23209 /*template_keyword_p=*/false,
23210 /*check_dependency_p=*/false,
23211 /*declarator_p=*/true,
23212 /*optional_p=*/false);
23213 if (is_overloaded_fn (id))
23214 id = DECL_NAME (get_first_fn (id));
23215 if (!constructor_name_p (id, nested_name_specifier))
23216 constructor_p = false;
23218 /* If we still think that this might be a constructor-declarator,
23219 look for a class-name. */
23220 else if (constructor_p)
23222 /* If we have:
23224 template <typename T> struct S {
23225 S();
23228 we must recognize that the nested `S' names a class. */
23229 tree type_decl;
23230 type_decl = cp_parser_class_name (parser,
23231 /*typename_keyword_p=*/false,
23232 /*template_keyword_p=*/false,
23233 none_type,
23234 /*check_dependency_p=*/false,
23235 /*class_head_p=*/false,
23236 /*is_declaration=*/false);
23237 /* If there was no class-name, then this is not a constructor.
23238 Otherwise, if we are in a class-specifier and we aren't
23239 handling a friend declaration, check that its type matches
23240 current_class_type (c++/38313). Note: error_mark_node
23241 is left alone for error recovery purposes. */
23242 constructor_p = (!cp_parser_error_occurred (parser)
23243 && (outside_class_specifier_p
23244 || type_decl == error_mark_node
23245 || same_type_p (current_class_type,
23246 TREE_TYPE (type_decl))));
23248 /* If we're still considering a constructor, we have to see a `(',
23249 to begin the parameter-declaration-clause, followed by either a
23250 `)', an `...', or a decl-specifier. We need to check for a
23251 type-specifier to avoid being fooled into thinking that:
23253 S (f) (int);
23255 is a constructor. (It is actually a function named `f' that
23256 takes one parameter (of type `int') and returns a value of type
23257 `S'. */
23258 if (constructor_p
23259 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23260 constructor_p = false;
23262 if (constructor_p
23263 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23264 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23265 /* A parameter declaration begins with a decl-specifier,
23266 which is either the "attribute" keyword, a storage class
23267 specifier, or (usually) a type-specifier. */
23268 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23270 tree type;
23271 tree pushed_scope = NULL_TREE;
23272 unsigned saved_num_template_parameter_lists;
23274 /* Names appearing in the type-specifier should be looked up
23275 in the scope of the class. */
23276 if (current_class_type)
23277 type = NULL_TREE;
23278 else
23280 type = TREE_TYPE (type_decl);
23281 if (TREE_CODE (type) == TYPENAME_TYPE)
23283 type = resolve_typename_type (type,
23284 /*only_current_p=*/false);
23285 if (TREE_CODE (type) == TYPENAME_TYPE)
23287 cp_parser_abort_tentative_parse (parser);
23288 return false;
23291 pushed_scope = push_scope (type);
23294 /* Inside the constructor parameter list, surrounding
23295 template-parameter-lists do not apply. */
23296 saved_num_template_parameter_lists
23297 = parser->num_template_parameter_lists;
23298 parser->num_template_parameter_lists = 0;
23300 /* Look for the type-specifier. */
23301 cp_parser_type_specifier (parser,
23302 CP_PARSER_FLAGS_NONE,
23303 /*decl_specs=*/NULL,
23304 /*is_declarator=*/true,
23305 /*declares_class_or_enum=*/NULL,
23306 /*is_cv_qualifier=*/NULL);
23308 parser->num_template_parameter_lists
23309 = saved_num_template_parameter_lists;
23311 /* Leave the scope of the class. */
23312 if (pushed_scope)
23313 pop_scope (pushed_scope);
23315 constructor_p = !cp_parser_error_occurred (parser);
23319 /* We did not really want to consume any tokens. */
23320 cp_parser_abort_tentative_parse (parser);
23322 return constructor_p;
23325 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23326 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23327 they must be performed once we are in the scope of the function.
23329 Returns the function defined. */
23331 static tree
23332 cp_parser_function_definition_from_specifiers_and_declarator
23333 (cp_parser* parser,
23334 cp_decl_specifier_seq *decl_specifiers,
23335 tree attributes,
23336 const cp_declarator *declarator)
23338 tree fn;
23339 bool success_p;
23341 /* Begin the function-definition. */
23342 success_p = start_function (decl_specifiers, declarator, attributes);
23344 /* The things we're about to see are not directly qualified by any
23345 template headers we've seen thus far. */
23346 reset_specialization ();
23348 /* If there were names looked up in the decl-specifier-seq that we
23349 did not check, check them now. We must wait until we are in the
23350 scope of the function to perform the checks, since the function
23351 might be a friend. */
23352 perform_deferred_access_checks (tf_warning_or_error);
23354 if (success_p)
23356 cp_finalize_omp_declare_simd (parser, current_function_decl);
23357 parser->omp_declare_simd = NULL;
23360 if (!success_p)
23362 /* Skip the entire function. */
23363 cp_parser_skip_to_end_of_block_or_statement (parser);
23364 fn = error_mark_node;
23366 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23368 /* Seen already, skip it. An error message has already been output. */
23369 cp_parser_skip_to_end_of_block_or_statement (parser);
23370 fn = current_function_decl;
23371 current_function_decl = NULL_TREE;
23372 /* If this is a function from a class, pop the nested class. */
23373 if (current_class_name)
23374 pop_nested_class ();
23376 else
23378 timevar_id_t tv;
23379 if (DECL_DECLARED_INLINE_P (current_function_decl))
23380 tv = TV_PARSE_INLINE;
23381 else
23382 tv = TV_PARSE_FUNC;
23383 timevar_push (tv);
23384 fn = cp_parser_function_definition_after_declarator (parser,
23385 /*inline_p=*/false);
23386 timevar_pop (tv);
23389 return fn;
23392 /* Parse the part of a function-definition that follows the
23393 declarator. INLINE_P is TRUE iff this function is an inline
23394 function defined within a class-specifier.
23396 Returns the function defined. */
23398 static tree
23399 cp_parser_function_definition_after_declarator (cp_parser* parser,
23400 bool inline_p)
23402 tree fn;
23403 bool ctor_initializer_p = false;
23404 bool saved_in_unbraced_linkage_specification_p;
23405 bool saved_in_function_body;
23406 unsigned saved_num_template_parameter_lists;
23407 cp_token *token;
23408 bool fully_implicit_function_template_p
23409 = parser->fully_implicit_function_template_p;
23410 parser->fully_implicit_function_template_p = false;
23411 tree implicit_template_parms
23412 = parser->implicit_template_parms;
23413 parser->implicit_template_parms = 0;
23414 cp_binding_level* implicit_template_scope
23415 = parser->implicit_template_scope;
23416 parser->implicit_template_scope = 0;
23418 saved_in_function_body = parser->in_function_body;
23419 parser->in_function_body = true;
23420 /* If the next token is `return', then the code may be trying to
23421 make use of the "named return value" extension that G++ used to
23422 support. */
23423 token = cp_lexer_peek_token (parser->lexer);
23424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23426 /* Consume the `return' keyword. */
23427 cp_lexer_consume_token (parser->lexer);
23428 /* Look for the identifier that indicates what value is to be
23429 returned. */
23430 cp_parser_identifier (parser);
23431 /* Issue an error message. */
23432 error_at (token->location,
23433 "named return values are no longer supported");
23434 /* Skip tokens until we reach the start of the function body. */
23435 while (true)
23437 cp_token *token = cp_lexer_peek_token (parser->lexer);
23438 if (token->type == CPP_OPEN_BRACE
23439 || token->type == CPP_EOF
23440 || token->type == CPP_PRAGMA_EOL)
23441 break;
23442 cp_lexer_consume_token (parser->lexer);
23445 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23446 anything declared inside `f'. */
23447 saved_in_unbraced_linkage_specification_p
23448 = parser->in_unbraced_linkage_specification_p;
23449 parser->in_unbraced_linkage_specification_p = false;
23450 /* Inside the function, surrounding template-parameter-lists do not
23451 apply. */
23452 saved_num_template_parameter_lists
23453 = parser->num_template_parameter_lists;
23454 parser->num_template_parameter_lists = 0;
23456 start_lambda_scope (current_function_decl);
23458 /* If the next token is `try', `__transaction_atomic', or
23459 `__transaction_relaxed`, then we are looking at either function-try-block
23460 or function-transaction-block. Note that all of these include the
23461 function-body. */
23462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23463 ctor_initializer_p = cp_parser_function_transaction (parser,
23464 RID_TRANSACTION_ATOMIC);
23465 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23466 RID_TRANSACTION_RELAXED))
23467 ctor_initializer_p = cp_parser_function_transaction (parser,
23468 RID_TRANSACTION_RELAXED);
23469 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23470 ctor_initializer_p = cp_parser_function_try_block (parser);
23471 else
23472 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23473 (parser, /*in_function_try_block=*/false);
23475 finish_lambda_scope ();
23477 /* Finish the function. */
23478 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23479 (inline_p ? 2 : 0));
23480 /* Generate code for it, if necessary. */
23481 expand_or_defer_fn (fn);
23482 /* Restore the saved values. */
23483 parser->in_unbraced_linkage_specification_p
23484 = saved_in_unbraced_linkage_specification_p;
23485 parser->num_template_parameter_lists
23486 = saved_num_template_parameter_lists;
23487 parser->in_function_body = saved_in_function_body;
23489 parser->fully_implicit_function_template_p
23490 = fully_implicit_function_template_p;
23491 parser->implicit_template_parms
23492 = implicit_template_parms;
23493 parser->implicit_template_scope
23494 = implicit_template_scope;
23496 if (parser->fully_implicit_function_template_p)
23497 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23499 return fn;
23502 /* Parse a template-declaration, assuming that the `export' (and
23503 `extern') keywords, if present, has already been scanned. MEMBER_P
23504 is as for cp_parser_template_declaration. */
23506 static void
23507 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23509 tree decl = NULL_TREE;
23510 vec<deferred_access_check, va_gc> *checks;
23511 tree parameter_list;
23512 bool friend_p = false;
23513 bool need_lang_pop;
23514 cp_token *token;
23516 /* Look for the `template' keyword. */
23517 token = cp_lexer_peek_token (parser->lexer);
23518 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23519 return;
23521 /* And the `<'. */
23522 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23523 return;
23524 if (at_class_scope_p () && current_function_decl)
23526 /* 14.5.2.2 [temp.mem]
23528 A local class shall not have member templates. */
23529 error_at (token->location,
23530 "invalid declaration of member template in local class");
23531 cp_parser_skip_to_end_of_block_or_statement (parser);
23532 return;
23534 /* [temp]
23536 A template ... shall not have C linkage. */
23537 if (current_lang_name == lang_name_c)
23539 error_at (token->location, "template with C linkage");
23540 /* Give it C++ linkage to avoid confusing other parts of the
23541 front end. */
23542 push_lang_context (lang_name_cplusplus);
23543 need_lang_pop = true;
23545 else
23546 need_lang_pop = false;
23548 /* We cannot perform access checks on the template parameter
23549 declarations until we know what is being declared, just as we
23550 cannot check the decl-specifier list. */
23551 push_deferring_access_checks (dk_deferred);
23553 /* If the next token is `>', then we have an invalid
23554 specialization. Rather than complain about an invalid template
23555 parameter, issue an error message here. */
23556 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23558 cp_parser_error (parser, "invalid explicit specialization");
23559 begin_specialization ();
23560 parameter_list = NULL_TREE;
23562 else
23564 /* Parse the template parameters. */
23565 parameter_list = cp_parser_template_parameter_list (parser);
23568 /* Get the deferred access checks from the parameter list. These
23569 will be checked once we know what is being declared, as for a
23570 member template the checks must be performed in the scope of the
23571 class containing the member. */
23572 checks = get_deferred_access_checks ();
23574 /* Look for the `>'. */
23575 cp_parser_skip_to_end_of_template_parameter_list (parser);
23576 /* We just processed one more parameter list. */
23577 ++parser->num_template_parameter_lists;
23578 /* If the next token is `template', there are more template
23579 parameters. */
23580 if (cp_lexer_next_token_is_keyword (parser->lexer,
23581 RID_TEMPLATE))
23582 cp_parser_template_declaration_after_export (parser, member_p);
23583 else if (cxx_dialect >= cxx11
23584 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23585 decl = cp_parser_alias_declaration (parser);
23586 else
23588 /* There are no access checks when parsing a template, as we do not
23589 know if a specialization will be a friend. */
23590 push_deferring_access_checks (dk_no_check);
23591 token = cp_lexer_peek_token (parser->lexer);
23592 decl = cp_parser_single_declaration (parser,
23593 checks,
23594 member_p,
23595 /*explicit_specialization_p=*/false,
23596 &friend_p);
23597 pop_deferring_access_checks ();
23599 /* If this is a member template declaration, let the front
23600 end know. */
23601 if (member_p && !friend_p && decl)
23603 if (TREE_CODE (decl) == TYPE_DECL)
23604 cp_parser_check_access_in_redeclaration (decl, token->location);
23606 decl = finish_member_template_decl (decl);
23608 else if (friend_p && decl
23609 && DECL_DECLARES_TYPE_P (decl))
23610 make_friend_class (current_class_type, TREE_TYPE (decl),
23611 /*complain=*/true);
23613 /* We are done with the current parameter list. */
23614 --parser->num_template_parameter_lists;
23616 pop_deferring_access_checks ();
23618 /* Finish up. */
23619 finish_template_decl (parameter_list);
23621 /* Check the template arguments for a literal operator template. */
23622 if (decl
23623 && DECL_DECLARES_FUNCTION_P (decl)
23624 && UDLIT_OPER_P (DECL_NAME (decl)))
23626 bool ok = true;
23627 if (parameter_list == NULL_TREE)
23628 ok = false;
23629 else
23631 int num_parms = TREE_VEC_LENGTH (parameter_list);
23632 if (num_parms == 1)
23634 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23635 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23636 if (TREE_TYPE (parm) != char_type_node
23637 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23638 ok = false;
23640 else if (num_parms == 2 && cxx_dialect >= cxx14)
23642 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23643 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23644 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23645 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23646 if (TREE_TYPE (parm) != TREE_TYPE (type)
23647 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23648 ok = false;
23650 else
23651 ok = false;
23653 if (!ok)
23655 if (cxx_dialect >= cxx14)
23656 error ("literal operator template %qD has invalid parameter list."
23657 " Expected non-type template argument pack <char...>"
23658 " or <typename CharT, CharT...>",
23659 decl);
23660 else
23661 error ("literal operator template %qD has invalid parameter list."
23662 " Expected non-type template argument pack <char...>",
23663 decl);
23666 /* Register member declarations. */
23667 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23668 finish_member_declaration (decl);
23669 /* For the erroneous case of a template with C linkage, we pushed an
23670 implicit C++ linkage scope; exit that scope now. */
23671 if (need_lang_pop)
23672 pop_lang_context ();
23673 /* If DECL is a function template, we must return to parse it later.
23674 (Even though there is no definition, there might be default
23675 arguments that need handling.) */
23676 if (member_p && decl
23677 && DECL_DECLARES_FUNCTION_P (decl))
23678 vec_safe_push (unparsed_funs_with_definitions, decl);
23681 /* Perform the deferred access checks from a template-parameter-list.
23682 CHECKS is a TREE_LIST of access checks, as returned by
23683 get_deferred_access_checks. */
23685 static void
23686 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23688 ++processing_template_parmlist;
23689 perform_access_checks (checks, tf_warning_or_error);
23690 --processing_template_parmlist;
23693 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23694 `function-definition' sequence that follows a template header.
23695 If MEMBER_P is true, this declaration appears in a class scope.
23697 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23698 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23700 static tree
23701 cp_parser_single_declaration (cp_parser* parser,
23702 vec<deferred_access_check, va_gc> *checks,
23703 bool member_p,
23704 bool explicit_specialization_p,
23705 bool* friend_p)
23707 int declares_class_or_enum;
23708 tree decl = NULL_TREE;
23709 cp_decl_specifier_seq decl_specifiers;
23710 bool function_definition_p = false;
23711 cp_token *decl_spec_token_start;
23713 /* This function is only used when processing a template
23714 declaration. */
23715 gcc_assert (innermost_scope_kind () == sk_template_parms
23716 || innermost_scope_kind () == sk_template_spec);
23718 /* Defer access checks until we know what is being declared. */
23719 push_deferring_access_checks (dk_deferred);
23721 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23722 alternative. */
23723 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23724 cp_parser_decl_specifier_seq (parser,
23725 CP_PARSER_FLAGS_OPTIONAL,
23726 &decl_specifiers,
23727 &declares_class_or_enum);
23728 if (friend_p)
23729 *friend_p = cp_parser_friend_p (&decl_specifiers);
23731 /* There are no template typedefs. */
23732 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23734 error_at (decl_spec_token_start->location,
23735 "template declaration of %<typedef%>");
23736 decl = error_mark_node;
23739 /* Gather up the access checks that occurred the
23740 decl-specifier-seq. */
23741 stop_deferring_access_checks ();
23743 /* Check for the declaration of a template class. */
23744 if (declares_class_or_enum)
23746 if (cp_parser_declares_only_class_p (parser))
23748 decl = shadow_tag (&decl_specifiers);
23750 /* In this case:
23752 struct C {
23753 friend template <typename T> struct A<T>::B;
23756 A<T>::B will be represented by a TYPENAME_TYPE, and
23757 therefore not recognized by shadow_tag. */
23758 if (friend_p && *friend_p
23759 && !decl
23760 && decl_specifiers.type
23761 && TYPE_P (decl_specifiers.type))
23762 decl = decl_specifiers.type;
23764 if (decl && decl != error_mark_node)
23765 decl = TYPE_NAME (decl);
23766 else
23767 decl = error_mark_node;
23769 /* Perform access checks for template parameters. */
23770 cp_parser_perform_template_parameter_access_checks (checks);
23774 /* Complain about missing 'typename' or other invalid type names. */
23775 if (!decl_specifiers.any_type_specifiers_p
23776 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23778 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23779 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23780 the rest of this declaration. */
23781 decl = error_mark_node;
23782 goto out;
23785 /* If it's not a template class, try for a template function. If
23786 the next token is a `;', then this declaration does not declare
23787 anything. But, if there were errors in the decl-specifiers, then
23788 the error might well have come from an attempted class-specifier.
23789 In that case, there's no need to warn about a missing declarator. */
23790 if (!decl
23791 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23792 || decl_specifiers.type != error_mark_node))
23794 decl = cp_parser_init_declarator (parser,
23795 &decl_specifiers,
23796 checks,
23797 /*function_definition_allowed_p=*/true,
23798 member_p,
23799 declares_class_or_enum,
23800 &function_definition_p,
23801 NULL, NULL);
23803 /* 7.1.1-1 [dcl.stc]
23805 A storage-class-specifier shall not be specified in an explicit
23806 specialization... */
23807 if (decl
23808 && explicit_specialization_p
23809 && decl_specifiers.storage_class != sc_none)
23811 error_at (decl_spec_token_start->location,
23812 "explicit template specialization cannot have a storage class");
23813 decl = error_mark_node;
23816 if (decl && VAR_P (decl))
23817 check_template_variable (decl);
23820 /* Look for a trailing `;' after the declaration. */
23821 if (!function_definition_p
23822 && (decl == error_mark_node
23823 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23824 cp_parser_skip_to_end_of_block_or_statement (parser);
23826 out:
23827 pop_deferring_access_checks ();
23829 /* Clear any current qualification; whatever comes next is the start
23830 of something new. */
23831 parser->scope = NULL_TREE;
23832 parser->qualifying_scope = NULL_TREE;
23833 parser->object_scope = NULL_TREE;
23835 return decl;
23838 /* Parse a cast-expression that is not the operand of a unary "&". */
23840 static tree
23841 cp_parser_simple_cast_expression (cp_parser *parser)
23843 return cp_parser_cast_expression (parser, /*address_p=*/false,
23844 /*cast_p=*/false, /*decltype*/false, NULL);
23847 /* Parse a functional cast to TYPE. Returns an expression
23848 representing the cast. */
23850 static tree
23851 cp_parser_functional_cast (cp_parser* parser, tree type)
23853 vec<tree, va_gc> *vec;
23854 tree expression_list;
23855 tree cast;
23856 bool nonconst_p;
23858 if (!type)
23859 type = error_mark_node;
23861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23863 cp_lexer_set_source_position (parser->lexer);
23864 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23865 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23866 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23867 if (TREE_CODE (type) == TYPE_DECL)
23868 type = TREE_TYPE (type);
23869 return finish_compound_literal (type, expression_list,
23870 tf_warning_or_error);
23874 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23875 /*cast_p=*/true,
23876 /*allow_expansion_p=*/true,
23877 /*non_constant_p=*/NULL);
23878 if (vec == NULL)
23879 expression_list = error_mark_node;
23880 else
23882 expression_list = build_tree_list_vec (vec);
23883 release_tree_vector (vec);
23886 cast = build_functional_cast (type, expression_list,
23887 tf_warning_or_error);
23888 /* [expr.const]/1: In an integral constant expression "only type
23889 conversions to integral or enumeration type can be used". */
23890 if (TREE_CODE (type) == TYPE_DECL)
23891 type = TREE_TYPE (type);
23892 if (cast != error_mark_node
23893 && !cast_valid_in_integral_constant_expression_p (type)
23894 && cp_parser_non_integral_constant_expression (parser,
23895 NIC_CONSTRUCTOR))
23896 return error_mark_node;
23897 return cast;
23900 /* Save the tokens that make up the body of a member function defined
23901 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23902 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23903 specifiers applied to the declaration. Returns the FUNCTION_DECL
23904 for the member function. */
23906 static tree
23907 cp_parser_save_member_function_body (cp_parser* parser,
23908 cp_decl_specifier_seq *decl_specifiers,
23909 cp_declarator *declarator,
23910 tree attributes)
23912 cp_token *first;
23913 cp_token *last;
23914 tree fn;
23916 /* Create the FUNCTION_DECL. */
23917 fn = grokmethod (decl_specifiers, declarator, attributes);
23918 cp_finalize_omp_declare_simd (parser, fn);
23919 /* If something went badly wrong, bail out now. */
23920 if (fn == error_mark_node)
23922 /* If there's a function-body, skip it. */
23923 if (cp_parser_token_starts_function_definition_p
23924 (cp_lexer_peek_token (parser->lexer)))
23925 cp_parser_skip_to_end_of_block_or_statement (parser);
23926 return error_mark_node;
23929 /* Remember it, if there default args to post process. */
23930 cp_parser_save_default_args (parser, fn);
23932 /* Save away the tokens that make up the body of the
23933 function. */
23934 first = parser->lexer->next_token;
23935 /* Handle function try blocks. */
23936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23937 cp_lexer_consume_token (parser->lexer);
23938 /* We can have braced-init-list mem-initializers before the fn body. */
23939 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23941 cp_lexer_consume_token (parser->lexer);
23942 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23944 /* cache_group will stop after an un-nested { } pair, too. */
23945 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23946 break;
23948 /* variadic mem-inits have ... after the ')'. */
23949 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23950 cp_lexer_consume_token (parser->lexer);
23953 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23954 /* Handle function try blocks. */
23955 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23956 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23957 last = parser->lexer->next_token;
23959 /* Save away the inline definition; we will process it when the
23960 class is complete. */
23961 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23962 DECL_PENDING_INLINE_P (fn) = 1;
23964 /* We need to know that this was defined in the class, so that
23965 friend templates are handled correctly. */
23966 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23968 /* Add FN to the queue of functions to be parsed later. */
23969 vec_safe_push (unparsed_funs_with_definitions, fn);
23971 return fn;
23974 /* Save the tokens that make up the in-class initializer for a non-static
23975 data member. Returns a DEFAULT_ARG. */
23977 static tree
23978 cp_parser_save_nsdmi (cp_parser* parser)
23980 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23983 /* Parse a template-argument-list, as well as the trailing ">" (but
23984 not the opening "<"). See cp_parser_template_argument_list for the
23985 return value. */
23987 static tree
23988 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23990 tree arguments;
23991 tree saved_scope;
23992 tree saved_qualifying_scope;
23993 tree saved_object_scope;
23994 bool saved_greater_than_is_operator_p;
23995 int saved_unevaluated_operand;
23996 int saved_inhibit_evaluation_warnings;
23998 /* [temp.names]
24000 When parsing a template-id, the first non-nested `>' is taken as
24001 the end of the template-argument-list rather than a greater-than
24002 operator. */
24003 saved_greater_than_is_operator_p
24004 = parser->greater_than_is_operator_p;
24005 parser->greater_than_is_operator_p = false;
24006 /* Parsing the argument list may modify SCOPE, so we save it
24007 here. */
24008 saved_scope = parser->scope;
24009 saved_qualifying_scope = parser->qualifying_scope;
24010 saved_object_scope = parser->object_scope;
24011 /* We need to evaluate the template arguments, even though this
24012 template-id may be nested within a "sizeof". */
24013 saved_unevaluated_operand = cp_unevaluated_operand;
24014 cp_unevaluated_operand = 0;
24015 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24016 c_inhibit_evaluation_warnings = 0;
24017 /* Parse the template-argument-list itself. */
24018 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24019 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24020 arguments = NULL_TREE;
24021 else
24022 arguments = cp_parser_template_argument_list (parser);
24023 /* Look for the `>' that ends the template-argument-list. If we find
24024 a '>>' instead, it's probably just a typo. */
24025 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24027 if (cxx_dialect != cxx98)
24029 /* In C++0x, a `>>' in a template argument list or cast
24030 expression is considered to be two separate `>'
24031 tokens. So, change the current token to a `>', but don't
24032 consume it: it will be consumed later when the outer
24033 template argument list (or cast expression) is parsed.
24034 Note that this replacement of `>' for `>>' is necessary
24035 even if we are parsing tentatively: in the tentative
24036 case, after calling
24037 cp_parser_enclosed_template_argument_list we will always
24038 throw away all of the template arguments and the first
24039 closing `>', either because the template argument list
24040 was erroneous or because we are replacing those tokens
24041 with a CPP_TEMPLATE_ID token. The second `>' (which will
24042 not have been thrown away) is needed either to close an
24043 outer template argument list or to complete a new-style
24044 cast. */
24045 cp_token *token = cp_lexer_peek_token (parser->lexer);
24046 token->type = CPP_GREATER;
24048 else if (!saved_greater_than_is_operator_p)
24050 /* If we're in a nested template argument list, the '>>' has
24051 to be a typo for '> >'. We emit the error message, but we
24052 continue parsing and we push a '>' as next token, so that
24053 the argument list will be parsed correctly. Note that the
24054 global source location is still on the token before the
24055 '>>', so we need to say explicitly where we want it. */
24056 cp_token *token = cp_lexer_peek_token (parser->lexer);
24057 error_at (token->location, "%<>>%> should be %<> >%> "
24058 "within a nested template argument list");
24060 token->type = CPP_GREATER;
24062 else
24064 /* If this is not a nested template argument list, the '>>'
24065 is a typo for '>'. Emit an error message and continue.
24066 Same deal about the token location, but here we can get it
24067 right by consuming the '>>' before issuing the diagnostic. */
24068 cp_token *token = cp_lexer_consume_token (parser->lexer);
24069 error_at (token->location,
24070 "spurious %<>>%>, use %<>%> to terminate "
24071 "a template argument list");
24074 else
24075 cp_parser_skip_to_end_of_template_parameter_list (parser);
24076 /* The `>' token might be a greater-than operator again now. */
24077 parser->greater_than_is_operator_p
24078 = saved_greater_than_is_operator_p;
24079 /* Restore the SAVED_SCOPE. */
24080 parser->scope = saved_scope;
24081 parser->qualifying_scope = saved_qualifying_scope;
24082 parser->object_scope = saved_object_scope;
24083 cp_unevaluated_operand = saved_unevaluated_operand;
24084 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24086 return arguments;
24089 /* MEMBER_FUNCTION is a member function, or a friend. If default
24090 arguments, or the body of the function have not yet been parsed,
24091 parse them now. */
24093 static void
24094 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24096 timevar_push (TV_PARSE_INMETH);
24097 /* If this member is a template, get the underlying
24098 FUNCTION_DECL. */
24099 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24100 member_function = DECL_TEMPLATE_RESULT (member_function);
24102 /* There should not be any class definitions in progress at this
24103 point; the bodies of members are only parsed outside of all class
24104 definitions. */
24105 gcc_assert (parser->num_classes_being_defined == 0);
24106 /* While we're parsing the member functions we might encounter more
24107 classes. We want to handle them right away, but we don't want
24108 them getting mixed up with functions that are currently in the
24109 queue. */
24110 push_unparsed_function_queues (parser);
24112 /* Make sure that any template parameters are in scope. */
24113 maybe_begin_member_template_processing (member_function);
24115 /* If the body of the function has not yet been parsed, parse it
24116 now. */
24117 if (DECL_PENDING_INLINE_P (member_function))
24119 tree function_scope;
24120 cp_token_cache *tokens;
24122 /* The function is no longer pending; we are processing it. */
24123 tokens = DECL_PENDING_INLINE_INFO (member_function);
24124 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24125 DECL_PENDING_INLINE_P (member_function) = 0;
24127 /* If this is a local class, enter the scope of the containing
24128 function. */
24129 function_scope = current_function_decl;
24130 if (function_scope)
24131 push_function_context ();
24133 /* Push the body of the function onto the lexer stack. */
24134 cp_parser_push_lexer_for_tokens (parser, tokens);
24136 /* Let the front end know that we going to be defining this
24137 function. */
24138 start_preparsed_function (member_function, NULL_TREE,
24139 SF_PRE_PARSED | SF_INCLASS_INLINE);
24141 /* Don't do access checking if it is a templated function. */
24142 if (processing_template_decl)
24143 push_deferring_access_checks (dk_no_check);
24145 /* #pragma omp declare reduction needs special parsing. */
24146 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24148 parser->lexer->in_pragma = true;
24149 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24150 finish_function (/*inline*/2);
24151 cp_check_omp_declare_reduction (member_function);
24153 else
24154 /* Now, parse the body of the function. */
24155 cp_parser_function_definition_after_declarator (parser,
24156 /*inline_p=*/true);
24158 if (processing_template_decl)
24159 pop_deferring_access_checks ();
24161 /* Leave the scope of the containing function. */
24162 if (function_scope)
24163 pop_function_context ();
24164 cp_parser_pop_lexer (parser);
24167 /* Remove any template parameters from the symbol table. */
24168 maybe_end_member_template_processing ();
24170 /* Restore the queue. */
24171 pop_unparsed_function_queues (parser);
24172 timevar_pop (TV_PARSE_INMETH);
24175 /* If DECL contains any default args, remember it on the unparsed
24176 functions queue. */
24178 static void
24179 cp_parser_save_default_args (cp_parser* parser, tree decl)
24181 tree probe;
24183 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24184 probe;
24185 probe = TREE_CHAIN (probe))
24186 if (TREE_PURPOSE (probe))
24188 cp_default_arg_entry entry = {current_class_type, decl};
24189 vec_safe_push (unparsed_funs_with_default_args, entry);
24190 break;
24194 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24195 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24196 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24197 from the parameter-type-list. */
24199 static tree
24200 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24201 tree default_arg, tree parmtype)
24203 cp_token_cache *tokens;
24204 tree parsed_arg;
24205 bool dummy;
24207 if (default_arg == error_mark_node)
24208 return error_mark_node;
24210 /* Push the saved tokens for the default argument onto the parser's
24211 lexer stack. */
24212 tokens = DEFARG_TOKENS (default_arg);
24213 cp_parser_push_lexer_for_tokens (parser, tokens);
24215 start_lambda_scope (decl);
24217 /* Parse the default argument. */
24218 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24219 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24220 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24222 finish_lambda_scope ();
24224 if (parsed_arg == error_mark_node)
24225 cp_parser_skip_to_end_of_statement (parser);
24227 if (!processing_template_decl)
24229 /* In a non-template class, check conversions now. In a template,
24230 we'll wait and instantiate these as needed. */
24231 if (TREE_CODE (decl) == PARM_DECL)
24232 parsed_arg = check_default_argument (parmtype, parsed_arg,
24233 tf_warning_or_error);
24234 else
24235 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24238 /* If the token stream has not been completely used up, then
24239 there was extra junk after the end of the default
24240 argument. */
24241 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24243 if (TREE_CODE (decl) == PARM_DECL)
24244 cp_parser_error (parser, "expected %<,%>");
24245 else
24246 cp_parser_error (parser, "expected %<;%>");
24249 /* Revert to the main lexer. */
24250 cp_parser_pop_lexer (parser);
24252 return parsed_arg;
24255 /* FIELD is a non-static data member with an initializer which we saved for
24256 later; parse it now. */
24258 static void
24259 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24261 tree def;
24263 maybe_begin_member_template_processing (field);
24265 push_unparsed_function_queues (parser);
24266 def = cp_parser_late_parse_one_default_arg (parser, field,
24267 DECL_INITIAL (field),
24268 NULL_TREE);
24269 pop_unparsed_function_queues (parser);
24271 maybe_end_member_template_processing ();
24273 DECL_INITIAL (field) = def;
24276 /* FN is a FUNCTION_DECL which may contains a parameter with an
24277 unparsed DEFAULT_ARG. Parse the default args now. This function
24278 assumes that the current scope is the scope in which the default
24279 argument should be processed. */
24281 static void
24282 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24284 bool saved_local_variables_forbidden_p;
24285 tree parm, parmdecl;
24287 /* While we're parsing the default args, we might (due to the
24288 statement expression extension) encounter more classes. We want
24289 to handle them right away, but we don't want them getting mixed
24290 up with default args that are currently in the queue. */
24291 push_unparsed_function_queues (parser);
24293 /* Local variable names (and the `this' keyword) may not appear
24294 in a default argument. */
24295 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24296 parser->local_variables_forbidden_p = true;
24298 push_defarg_context (fn);
24300 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24301 parmdecl = DECL_ARGUMENTS (fn);
24302 parm && parm != void_list_node;
24303 parm = TREE_CHAIN (parm),
24304 parmdecl = DECL_CHAIN (parmdecl))
24306 tree default_arg = TREE_PURPOSE (parm);
24307 tree parsed_arg;
24308 vec<tree, va_gc> *insts;
24309 tree copy;
24310 unsigned ix;
24312 if (!default_arg)
24313 continue;
24315 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24316 /* This can happen for a friend declaration for a function
24317 already declared with default arguments. */
24318 continue;
24320 parsed_arg
24321 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24322 default_arg,
24323 TREE_VALUE (parm));
24324 if (parsed_arg == error_mark_node)
24326 continue;
24329 TREE_PURPOSE (parm) = parsed_arg;
24331 /* Update any instantiations we've already created. */
24332 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24333 vec_safe_iterate (insts, ix, &copy); ix++)
24334 TREE_PURPOSE (copy) = parsed_arg;
24337 pop_defarg_context ();
24339 /* Make sure no default arg is missing. */
24340 check_default_args (fn);
24342 /* Restore the state of local_variables_forbidden_p. */
24343 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24345 /* Restore the queue. */
24346 pop_unparsed_function_queues (parser);
24349 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24351 sizeof ... ( identifier )
24353 where the 'sizeof' token has already been consumed. */
24355 static tree
24356 cp_parser_sizeof_pack (cp_parser *parser)
24358 /* Consume the `...'. */
24359 cp_lexer_consume_token (parser->lexer);
24360 maybe_warn_variadic_templates ();
24362 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24363 if (paren)
24364 cp_lexer_consume_token (parser->lexer);
24365 else
24366 permerror (cp_lexer_peek_token (parser->lexer)->location,
24367 "%<sizeof...%> argument must be surrounded by parentheses");
24369 cp_token *token = cp_lexer_peek_token (parser->lexer);
24370 tree name = cp_parser_identifier (parser);
24371 if (name == error_mark_node)
24372 return error_mark_node;
24373 /* The name is not qualified. */
24374 parser->scope = NULL_TREE;
24375 parser->qualifying_scope = NULL_TREE;
24376 parser->object_scope = NULL_TREE;
24377 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24378 if (expr == error_mark_node)
24379 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24380 token->location);
24381 if (TREE_CODE (expr) == TYPE_DECL)
24382 expr = TREE_TYPE (expr);
24383 else if (TREE_CODE (expr) == CONST_DECL)
24384 expr = DECL_INITIAL (expr);
24385 expr = make_pack_expansion (expr);
24387 if (paren)
24388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24390 return expr;
24393 /* Parse the operand of `sizeof' (or a similar operator). Returns
24394 either a TYPE or an expression, depending on the form of the
24395 input. The KEYWORD indicates which kind of expression we have
24396 encountered. */
24398 static tree
24399 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24401 tree expr = NULL_TREE;
24402 const char *saved_message;
24403 char *tmp;
24404 bool saved_integral_constant_expression_p;
24405 bool saved_non_integral_constant_expression_p;
24407 /* If it's a `...', then we are computing the length of a parameter
24408 pack. */
24409 if (keyword == RID_SIZEOF
24410 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24411 return cp_parser_sizeof_pack (parser);
24413 /* Types cannot be defined in a `sizeof' expression. Save away the
24414 old message. */
24415 saved_message = parser->type_definition_forbidden_message;
24416 /* And create the new one. */
24417 tmp = concat ("types may not be defined in %<",
24418 IDENTIFIER_POINTER (ridpointers[keyword]),
24419 "%> expressions", NULL);
24420 parser->type_definition_forbidden_message = tmp;
24422 /* The restrictions on constant-expressions do not apply inside
24423 sizeof expressions. */
24424 saved_integral_constant_expression_p
24425 = parser->integral_constant_expression_p;
24426 saved_non_integral_constant_expression_p
24427 = parser->non_integral_constant_expression_p;
24428 parser->integral_constant_expression_p = false;
24430 /* Do not actually evaluate the expression. */
24431 ++cp_unevaluated_operand;
24432 ++c_inhibit_evaluation_warnings;
24433 /* If it's a `(', then we might be looking at the type-id
24434 construction. */
24435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24437 tree type = NULL_TREE;
24439 /* We can't be sure yet whether we're looking at a type-id or an
24440 expression. */
24441 cp_parser_parse_tentatively (parser);
24442 /* Note: as a GNU Extension, compound literals are considered
24443 postfix-expressions as they are in C99, so they are valid
24444 arguments to sizeof. See comment in cp_parser_cast_expression
24445 for details. */
24446 if (cp_parser_compound_literal_p (parser))
24447 cp_parser_simulate_error (parser);
24448 else
24450 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24451 parser->in_type_id_in_expr_p = true;
24452 /* Look for the type-id. */
24453 type = cp_parser_type_id (parser);
24454 /* Look for the closing `)'. */
24455 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24456 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24459 /* If all went well, then we're done. */
24460 if (cp_parser_parse_definitely (parser))
24462 cp_decl_specifier_seq decl_specs;
24464 /* Build a trivial decl-specifier-seq. */
24465 clear_decl_specs (&decl_specs);
24466 decl_specs.type = type;
24468 /* Call grokdeclarator to figure out what type this is. */
24469 expr = grokdeclarator (NULL,
24470 &decl_specs,
24471 TYPENAME,
24472 /*initialized=*/0,
24473 /*attrlist=*/NULL);
24477 /* If the type-id production did not work out, then we must be
24478 looking at the unary-expression production. */
24479 if (!expr)
24480 expr = cp_parser_unary_expression (parser);
24482 /* Go back to evaluating expressions. */
24483 --cp_unevaluated_operand;
24484 --c_inhibit_evaluation_warnings;
24486 /* Free the message we created. */
24487 free (tmp);
24488 /* And restore the old one. */
24489 parser->type_definition_forbidden_message = saved_message;
24490 parser->integral_constant_expression_p
24491 = saved_integral_constant_expression_p;
24492 parser->non_integral_constant_expression_p
24493 = saved_non_integral_constant_expression_p;
24495 return expr;
24498 /* If the current declaration has no declarator, return true. */
24500 static bool
24501 cp_parser_declares_only_class_p (cp_parser *parser)
24503 /* If the next token is a `;' or a `,' then there is no
24504 declarator. */
24505 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24506 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24509 /* Update the DECL_SPECS to reflect the storage class indicated by
24510 KEYWORD. */
24512 static void
24513 cp_parser_set_storage_class (cp_parser *parser,
24514 cp_decl_specifier_seq *decl_specs,
24515 enum rid keyword,
24516 cp_token *token)
24518 cp_storage_class storage_class;
24520 if (parser->in_unbraced_linkage_specification_p)
24522 error_at (token->location, "invalid use of %qD in linkage specification",
24523 ridpointers[keyword]);
24524 return;
24526 else if (decl_specs->storage_class != sc_none)
24528 decl_specs->conflicting_specifiers_p = true;
24529 return;
24532 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24533 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24534 && decl_specs->gnu_thread_keyword_p)
24536 pedwarn (decl_specs->locations[ds_thread], 0,
24537 "%<__thread%> before %qD", ridpointers[keyword]);
24540 switch (keyword)
24542 case RID_AUTO:
24543 storage_class = sc_auto;
24544 break;
24545 case RID_REGISTER:
24546 storage_class = sc_register;
24547 break;
24548 case RID_STATIC:
24549 storage_class = sc_static;
24550 break;
24551 case RID_EXTERN:
24552 storage_class = sc_extern;
24553 break;
24554 case RID_MUTABLE:
24555 storage_class = sc_mutable;
24556 break;
24557 default:
24558 gcc_unreachable ();
24560 decl_specs->storage_class = storage_class;
24561 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24563 /* A storage class specifier cannot be applied alongside a typedef
24564 specifier. If there is a typedef specifier present then set
24565 conflicting_specifiers_p which will trigger an error later
24566 on in grokdeclarator. */
24567 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24568 decl_specs->conflicting_specifiers_p = true;
24571 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24572 is true, the type is a class or enum definition. */
24574 static void
24575 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24576 tree type_spec,
24577 cp_token *token,
24578 bool type_definition_p)
24580 decl_specs->any_specifiers_p = true;
24582 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24583 (with, for example, in "typedef int wchar_t;") we remember that
24584 this is what happened. In system headers, we ignore these
24585 declarations so that G++ can work with system headers that are not
24586 C++-safe. */
24587 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24588 && !type_definition_p
24589 && (type_spec == boolean_type_node
24590 || type_spec == char16_type_node
24591 || type_spec == char32_type_node
24592 || type_spec == wchar_type_node)
24593 && (decl_specs->type
24594 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24595 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24596 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24597 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24599 decl_specs->redefined_builtin_type = type_spec;
24600 set_and_check_decl_spec_loc (decl_specs,
24601 ds_redefined_builtin_type_spec,
24602 token);
24603 if (!decl_specs->type)
24605 decl_specs->type = type_spec;
24606 decl_specs->type_definition_p = false;
24607 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24610 else if (decl_specs->type)
24611 decl_specs->multiple_types_p = true;
24612 else
24614 decl_specs->type = type_spec;
24615 decl_specs->type_definition_p = type_definition_p;
24616 decl_specs->redefined_builtin_type = NULL_TREE;
24617 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24621 /* True iff TOKEN is the GNU keyword __thread. */
24623 static bool
24624 token_is__thread (cp_token *token)
24626 gcc_assert (token->keyword == RID_THREAD);
24627 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24630 /* Set the location for a declarator specifier and check if it is
24631 duplicated.
24633 DECL_SPECS is the sequence of declarator specifiers onto which to
24634 set the location.
24636 DS is the single declarator specifier to set which location is to
24637 be set onto the existing sequence of declarators.
24639 LOCATION is the location for the declarator specifier to
24640 consider. */
24642 static void
24643 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24644 cp_decl_spec ds, cp_token *token)
24646 gcc_assert (ds < ds_last);
24648 if (decl_specs == NULL)
24649 return;
24651 source_location location = token->location;
24653 if (decl_specs->locations[ds] == 0)
24655 decl_specs->locations[ds] = location;
24656 if (ds == ds_thread)
24657 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24659 else
24661 if (ds == ds_long)
24663 if (decl_specs->locations[ds_long_long] != 0)
24664 error_at (location,
24665 "%<long long long%> is too long for GCC");
24666 else
24668 decl_specs->locations[ds_long_long] = location;
24669 pedwarn_cxx98 (location,
24670 OPT_Wlong_long,
24671 "ISO C++ 1998 does not support %<long long%>");
24674 else if (ds == ds_thread)
24676 bool gnu = token_is__thread (token);
24677 if (gnu != decl_specs->gnu_thread_keyword_p)
24678 error_at (location,
24679 "both %<__thread%> and %<thread_local%> specified");
24680 else
24681 error_at (location, "duplicate %qD", token->u.value);
24683 else
24685 static const char *const decl_spec_names[] = {
24686 "signed",
24687 "unsigned",
24688 "short",
24689 "long",
24690 "const",
24691 "volatile",
24692 "restrict",
24693 "inline",
24694 "virtual",
24695 "explicit",
24696 "friend",
24697 "typedef",
24698 "using",
24699 "constexpr",
24700 "__complex"
24702 error_at (location,
24703 "duplicate %qs", decl_spec_names[ds]);
24708 /* Return true iff the declarator specifier DS is present in the
24709 sequence of declarator specifiers DECL_SPECS. */
24711 bool
24712 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24713 cp_decl_spec ds)
24715 gcc_assert (ds < ds_last);
24717 if (decl_specs == NULL)
24718 return false;
24720 return decl_specs->locations[ds] != 0;
24723 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24724 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24726 static bool
24727 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24729 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24732 /* Issue an error message indicating that TOKEN_DESC was expected.
24733 If KEYWORD is true, it indicated this function is called by
24734 cp_parser_require_keword and the required token can only be
24735 a indicated keyword. */
24737 static void
24738 cp_parser_required_error (cp_parser *parser,
24739 required_token token_desc,
24740 bool keyword)
24742 switch (token_desc)
24744 case RT_NEW:
24745 cp_parser_error (parser, "expected %<new%>");
24746 return;
24747 case RT_DELETE:
24748 cp_parser_error (parser, "expected %<delete%>");
24749 return;
24750 case RT_RETURN:
24751 cp_parser_error (parser, "expected %<return%>");
24752 return;
24753 case RT_WHILE:
24754 cp_parser_error (parser, "expected %<while%>");
24755 return;
24756 case RT_EXTERN:
24757 cp_parser_error (parser, "expected %<extern%>");
24758 return;
24759 case RT_STATIC_ASSERT:
24760 cp_parser_error (parser, "expected %<static_assert%>");
24761 return;
24762 case RT_DECLTYPE:
24763 cp_parser_error (parser, "expected %<decltype%>");
24764 return;
24765 case RT_OPERATOR:
24766 cp_parser_error (parser, "expected %<operator%>");
24767 return;
24768 case RT_CLASS:
24769 cp_parser_error (parser, "expected %<class%>");
24770 return;
24771 case RT_TEMPLATE:
24772 cp_parser_error (parser, "expected %<template%>");
24773 return;
24774 case RT_NAMESPACE:
24775 cp_parser_error (parser, "expected %<namespace%>");
24776 return;
24777 case RT_USING:
24778 cp_parser_error (parser, "expected %<using%>");
24779 return;
24780 case RT_ASM:
24781 cp_parser_error (parser, "expected %<asm%>");
24782 return;
24783 case RT_TRY:
24784 cp_parser_error (parser, "expected %<try%>");
24785 return;
24786 case RT_CATCH:
24787 cp_parser_error (parser, "expected %<catch%>");
24788 return;
24789 case RT_THROW:
24790 cp_parser_error (parser, "expected %<throw%>");
24791 return;
24792 case RT_LABEL:
24793 cp_parser_error (parser, "expected %<__label__%>");
24794 return;
24795 case RT_AT_TRY:
24796 cp_parser_error (parser, "expected %<@try%>");
24797 return;
24798 case RT_AT_SYNCHRONIZED:
24799 cp_parser_error (parser, "expected %<@synchronized%>");
24800 return;
24801 case RT_AT_THROW:
24802 cp_parser_error (parser, "expected %<@throw%>");
24803 return;
24804 case RT_TRANSACTION_ATOMIC:
24805 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24806 return;
24807 case RT_TRANSACTION_RELAXED:
24808 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24809 return;
24810 default:
24811 break;
24813 if (!keyword)
24815 switch (token_desc)
24817 case RT_SEMICOLON:
24818 cp_parser_error (parser, "expected %<;%>");
24819 return;
24820 case RT_OPEN_PAREN:
24821 cp_parser_error (parser, "expected %<(%>");
24822 return;
24823 case RT_CLOSE_BRACE:
24824 cp_parser_error (parser, "expected %<}%>");
24825 return;
24826 case RT_OPEN_BRACE:
24827 cp_parser_error (parser, "expected %<{%>");
24828 return;
24829 case RT_CLOSE_SQUARE:
24830 cp_parser_error (parser, "expected %<]%>");
24831 return;
24832 case RT_OPEN_SQUARE:
24833 cp_parser_error (parser, "expected %<[%>");
24834 return;
24835 case RT_COMMA:
24836 cp_parser_error (parser, "expected %<,%>");
24837 return;
24838 case RT_SCOPE:
24839 cp_parser_error (parser, "expected %<::%>");
24840 return;
24841 case RT_LESS:
24842 cp_parser_error (parser, "expected %<<%>");
24843 return;
24844 case RT_GREATER:
24845 cp_parser_error (parser, "expected %<>%>");
24846 return;
24847 case RT_EQ:
24848 cp_parser_error (parser, "expected %<=%>");
24849 return;
24850 case RT_ELLIPSIS:
24851 cp_parser_error (parser, "expected %<...%>");
24852 return;
24853 case RT_MULT:
24854 cp_parser_error (parser, "expected %<*%>");
24855 return;
24856 case RT_COMPL:
24857 cp_parser_error (parser, "expected %<~%>");
24858 return;
24859 case RT_COLON:
24860 cp_parser_error (parser, "expected %<:%>");
24861 return;
24862 case RT_COLON_SCOPE:
24863 cp_parser_error (parser, "expected %<:%> or %<::%>");
24864 return;
24865 case RT_CLOSE_PAREN:
24866 cp_parser_error (parser, "expected %<)%>");
24867 return;
24868 case RT_COMMA_CLOSE_PAREN:
24869 cp_parser_error (parser, "expected %<,%> or %<)%>");
24870 return;
24871 case RT_PRAGMA_EOL:
24872 cp_parser_error (parser, "expected end of line");
24873 return;
24874 case RT_NAME:
24875 cp_parser_error (parser, "expected identifier");
24876 return;
24877 case RT_SELECT:
24878 cp_parser_error (parser, "expected selection-statement");
24879 return;
24880 case RT_INTERATION:
24881 cp_parser_error (parser, "expected iteration-statement");
24882 return;
24883 case RT_JUMP:
24884 cp_parser_error (parser, "expected jump-statement");
24885 return;
24886 case RT_CLASS_KEY:
24887 cp_parser_error (parser, "expected class-key");
24888 return;
24889 case RT_CLASS_TYPENAME_TEMPLATE:
24890 cp_parser_error (parser,
24891 "expected %<class%>, %<typename%>, or %<template%>");
24892 return;
24893 default:
24894 gcc_unreachable ();
24897 else
24898 gcc_unreachable ();
24903 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24904 issue an error message indicating that TOKEN_DESC was expected.
24906 Returns the token consumed, if the token had the appropriate type.
24907 Otherwise, returns NULL. */
24909 static cp_token *
24910 cp_parser_require (cp_parser* parser,
24911 enum cpp_ttype type,
24912 required_token token_desc)
24914 if (cp_lexer_next_token_is (parser->lexer, type))
24915 return cp_lexer_consume_token (parser->lexer);
24916 else
24918 /* Output the MESSAGE -- unless we're parsing tentatively. */
24919 if (!cp_parser_simulate_error (parser))
24920 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24921 return NULL;
24925 /* An error message is produced if the next token is not '>'.
24926 All further tokens are skipped until the desired token is
24927 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24929 static void
24930 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24932 /* Current level of '< ... >'. */
24933 unsigned level = 0;
24934 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24935 unsigned nesting_depth = 0;
24937 /* Are we ready, yet? If not, issue error message. */
24938 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24939 return;
24941 /* Skip tokens until the desired token is found. */
24942 while (true)
24944 /* Peek at the next token. */
24945 switch (cp_lexer_peek_token (parser->lexer)->type)
24947 case CPP_LESS:
24948 if (!nesting_depth)
24949 ++level;
24950 break;
24952 case CPP_RSHIFT:
24953 if (cxx_dialect == cxx98)
24954 /* C++0x views the `>>' operator as two `>' tokens, but
24955 C++98 does not. */
24956 break;
24957 else if (!nesting_depth && level-- == 0)
24959 /* We've hit a `>>' where the first `>' closes the
24960 template argument list, and the second `>' is
24961 spurious. Just consume the `>>' and stop; we've
24962 already produced at least one error. */
24963 cp_lexer_consume_token (parser->lexer);
24964 return;
24966 /* Fall through for C++0x, so we handle the second `>' in
24967 the `>>'. */
24969 case CPP_GREATER:
24970 if (!nesting_depth && level-- == 0)
24972 /* We've reached the token we want, consume it and stop. */
24973 cp_lexer_consume_token (parser->lexer);
24974 return;
24976 break;
24978 case CPP_OPEN_PAREN:
24979 case CPP_OPEN_SQUARE:
24980 ++nesting_depth;
24981 break;
24983 case CPP_CLOSE_PAREN:
24984 case CPP_CLOSE_SQUARE:
24985 if (nesting_depth-- == 0)
24986 return;
24987 break;
24989 case CPP_EOF:
24990 case CPP_PRAGMA_EOL:
24991 case CPP_SEMICOLON:
24992 case CPP_OPEN_BRACE:
24993 case CPP_CLOSE_BRACE:
24994 /* The '>' was probably forgotten, don't look further. */
24995 return;
24997 default:
24998 break;
25001 /* Consume this token. */
25002 cp_lexer_consume_token (parser->lexer);
25006 /* If the next token is the indicated keyword, consume it. Otherwise,
25007 issue an error message indicating that TOKEN_DESC was expected.
25009 Returns the token consumed, if the token had the appropriate type.
25010 Otherwise, returns NULL. */
25012 static cp_token *
25013 cp_parser_require_keyword (cp_parser* parser,
25014 enum rid keyword,
25015 required_token token_desc)
25017 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25019 if (token && token->keyword != keyword)
25021 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25022 return NULL;
25025 return token;
25028 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25029 function-definition. */
25031 static bool
25032 cp_parser_token_starts_function_definition_p (cp_token* token)
25034 return (/* An ordinary function-body begins with an `{'. */
25035 token->type == CPP_OPEN_BRACE
25036 /* A ctor-initializer begins with a `:'. */
25037 || token->type == CPP_COLON
25038 /* A function-try-block begins with `try'. */
25039 || token->keyword == RID_TRY
25040 /* A function-transaction-block begins with `__transaction_atomic'
25041 or `__transaction_relaxed'. */
25042 || token->keyword == RID_TRANSACTION_ATOMIC
25043 || token->keyword == RID_TRANSACTION_RELAXED
25044 /* The named return value extension begins with `return'. */
25045 || token->keyword == RID_RETURN);
25048 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25049 definition. */
25051 static bool
25052 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25054 cp_token *token;
25056 token = cp_lexer_peek_token (parser->lexer);
25057 return (token->type == CPP_OPEN_BRACE
25058 || (token->type == CPP_COLON
25059 && !parser->colon_doesnt_start_class_def_p));
25062 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25063 C++0x) ending a template-argument. */
25065 static bool
25066 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25068 cp_token *token;
25070 token = cp_lexer_peek_token (parser->lexer);
25071 return (token->type == CPP_COMMA
25072 || token->type == CPP_GREATER
25073 || token->type == CPP_ELLIPSIS
25074 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25077 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25078 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25080 static bool
25081 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25082 size_t n)
25084 cp_token *token;
25086 token = cp_lexer_peek_nth_token (parser->lexer, n);
25087 if (token->type == CPP_LESS)
25088 return true;
25089 /* Check for the sequence `<::' in the original code. It would be lexed as
25090 `[:', where `[' is a digraph, and there is no whitespace before
25091 `:'. */
25092 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25094 cp_token *token2;
25095 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25096 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25097 return true;
25099 return false;
25102 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25103 or none_type otherwise. */
25105 static enum tag_types
25106 cp_parser_token_is_class_key (cp_token* token)
25108 switch (token->keyword)
25110 case RID_CLASS:
25111 return class_type;
25112 case RID_STRUCT:
25113 return record_type;
25114 case RID_UNION:
25115 return union_type;
25117 default:
25118 return none_type;
25122 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25123 or none_type otherwise or if the token is null. */
25125 static enum tag_types
25126 cp_parser_token_is_type_parameter_key (cp_token* token)
25128 if (!token)
25129 return none_type;
25131 switch (token->keyword)
25133 case RID_CLASS:
25134 return class_type;
25135 case RID_TYPENAME:
25136 return typename_type;
25138 default:
25139 return none_type;
25143 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25145 static void
25146 cp_parser_check_class_key (enum tag_types class_key, tree type)
25148 if (type == error_mark_node)
25149 return;
25150 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25152 if (permerror (input_location, "%qs tag used in naming %q#T",
25153 class_key == union_type ? "union"
25154 : class_key == record_type ? "struct" : "class",
25155 type))
25156 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25157 "%q#T was previously declared here", type);
25161 /* Issue an error message if DECL is redeclared with different
25162 access than its original declaration [class.access.spec/3].
25163 This applies to nested classes and nested class templates.
25164 [class.mem/1]. */
25166 static void
25167 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25169 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25170 return;
25172 if ((TREE_PRIVATE (decl)
25173 != (current_access_specifier == access_private_node))
25174 || (TREE_PROTECTED (decl)
25175 != (current_access_specifier == access_protected_node)))
25176 error_at (location, "%qD redeclared with different access", decl);
25179 /* Look for the `template' keyword, as a syntactic disambiguator.
25180 Return TRUE iff it is present, in which case it will be
25181 consumed. */
25183 static bool
25184 cp_parser_optional_template_keyword (cp_parser *parser)
25186 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25188 /* In C++98 the `template' keyword can only be used within templates;
25189 outside templates the parser can always figure out what is a
25190 template and what is not. In C++11, per the resolution of DR 468,
25191 `template' is allowed in cases where it is not strictly necessary. */
25192 if (!processing_template_decl
25193 && pedantic && cxx_dialect == cxx98)
25195 cp_token *token = cp_lexer_peek_token (parser->lexer);
25196 pedwarn (token->location, OPT_Wpedantic,
25197 "in C++98 %<template%> (as a disambiguator) is only "
25198 "allowed within templates");
25199 /* If this part of the token stream is rescanned, the same
25200 error message would be generated. So, we purge the token
25201 from the stream. */
25202 cp_lexer_purge_token (parser->lexer);
25203 return false;
25205 else
25207 /* Consume the `template' keyword. */
25208 cp_lexer_consume_token (parser->lexer);
25209 return true;
25212 return false;
25215 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25216 set PARSER->SCOPE, and perform other related actions. */
25218 static void
25219 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25221 int i;
25222 struct tree_check *check_value;
25223 deferred_access_check *chk;
25224 vec<deferred_access_check, va_gc> *checks;
25226 /* Get the stored value. */
25227 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25228 /* Perform any access checks that were deferred. */
25229 checks = check_value->checks;
25230 if (checks)
25232 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25233 perform_or_defer_access_check (chk->binfo,
25234 chk->decl,
25235 chk->diag_decl, tf_warning_or_error);
25237 /* Set the scope from the stored value. */
25238 parser->scope = check_value->value;
25239 parser->qualifying_scope = check_value->qualifying_scope;
25240 parser->object_scope = NULL_TREE;
25243 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25244 encounter the end of a block before what we were looking for. */
25246 static bool
25247 cp_parser_cache_group (cp_parser *parser,
25248 enum cpp_ttype end,
25249 unsigned depth)
25251 while (true)
25253 cp_token *token = cp_lexer_peek_token (parser->lexer);
25255 /* Abort a parenthesized expression if we encounter a semicolon. */
25256 if ((end == CPP_CLOSE_PAREN || depth == 0)
25257 && token->type == CPP_SEMICOLON)
25258 return true;
25259 /* If we've reached the end of the file, stop. */
25260 if (token->type == CPP_EOF
25261 || (end != CPP_PRAGMA_EOL
25262 && token->type == CPP_PRAGMA_EOL))
25263 return true;
25264 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25265 /* We've hit the end of an enclosing block, so there's been some
25266 kind of syntax error. */
25267 return true;
25269 /* Consume the token. */
25270 cp_lexer_consume_token (parser->lexer);
25271 /* See if it starts a new group. */
25272 if (token->type == CPP_OPEN_BRACE)
25274 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25275 /* In theory this should probably check end == '}', but
25276 cp_parser_save_member_function_body needs it to exit
25277 after either '}' or ')' when called with ')'. */
25278 if (depth == 0)
25279 return false;
25281 else if (token->type == CPP_OPEN_PAREN)
25283 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25284 if (depth == 0 && end == CPP_CLOSE_PAREN)
25285 return false;
25287 else if (token->type == CPP_PRAGMA)
25288 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25289 else if (token->type == end)
25290 return false;
25294 /* Like above, for caching a default argument or NSDMI. Both of these are
25295 terminated by a non-nested comma, but it can be unclear whether or not a
25296 comma is nested in a template argument list unless we do more parsing.
25297 In order to handle this ambiguity, when we encounter a ',' after a '<'
25298 we try to parse what follows as a parameter-declaration-list (in the
25299 case of a default argument) or a member-declarator (in the case of an
25300 NSDMI). If that succeeds, then we stop caching. */
25302 static tree
25303 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25305 unsigned depth = 0;
25306 int maybe_template_id = 0;
25307 cp_token *first_token;
25308 cp_token *token;
25309 tree default_argument;
25311 /* Add tokens until we have processed the entire default
25312 argument. We add the range [first_token, token). */
25313 first_token = cp_lexer_peek_token (parser->lexer);
25314 if (first_token->type == CPP_OPEN_BRACE)
25316 /* For list-initialization, this is straightforward. */
25317 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25318 token = cp_lexer_peek_token (parser->lexer);
25320 else while (true)
25322 bool done = false;
25324 /* Peek at the next token. */
25325 token = cp_lexer_peek_token (parser->lexer);
25326 /* What we do depends on what token we have. */
25327 switch (token->type)
25329 /* In valid code, a default argument must be
25330 immediately followed by a `,' `)', or `...'. */
25331 case CPP_COMMA:
25332 if (depth == 0 && maybe_template_id)
25334 /* If we've seen a '<', we might be in a
25335 template-argument-list. Until Core issue 325 is
25336 resolved, we don't know how this situation ought
25337 to be handled, so try to DTRT. We check whether
25338 what comes after the comma is a valid parameter
25339 declaration list. If it is, then the comma ends
25340 the default argument; otherwise the default
25341 argument continues. */
25342 bool error = false;
25344 /* Set ITALP so cp_parser_parameter_declaration_list
25345 doesn't decide to commit to this parse. */
25346 bool saved_italp = parser->in_template_argument_list_p;
25347 parser->in_template_argument_list_p = true;
25349 cp_parser_parse_tentatively (parser);
25350 cp_lexer_consume_token (parser->lexer);
25352 if (nsdmi)
25354 int ctor_dtor_or_conv_p;
25355 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25356 &ctor_dtor_or_conv_p,
25357 /*parenthesized_p=*/NULL,
25358 /*member_p=*/true,
25359 /*friend_p=*/false);
25361 else
25363 begin_scope (sk_function_parms, NULL_TREE);
25364 cp_parser_parameter_declaration_list (parser, &error);
25365 pop_bindings_and_leave_scope ();
25367 if (!cp_parser_error_occurred (parser) && !error)
25368 done = true;
25369 cp_parser_abort_tentative_parse (parser);
25371 parser->in_template_argument_list_p = saved_italp;
25372 break;
25374 case CPP_CLOSE_PAREN:
25375 case CPP_ELLIPSIS:
25376 /* If we run into a non-nested `;', `}', or `]',
25377 then the code is invalid -- but the default
25378 argument is certainly over. */
25379 case CPP_SEMICOLON:
25380 case CPP_CLOSE_BRACE:
25381 case CPP_CLOSE_SQUARE:
25382 if (depth == 0
25383 /* Handle correctly int n = sizeof ... ( p ); */
25384 && token->type != CPP_ELLIPSIS)
25385 done = true;
25386 /* Update DEPTH, if necessary. */
25387 else if (token->type == CPP_CLOSE_PAREN
25388 || token->type == CPP_CLOSE_BRACE
25389 || token->type == CPP_CLOSE_SQUARE)
25390 --depth;
25391 break;
25393 case CPP_OPEN_PAREN:
25394 case CPP_OPEN_SQUARE:
25395 case CPP_OPEN_BRACE:
25396 ++depth;
25397 break;
25399 case CPP_LESS:
25400 if (depth == 0)
25401 /* This might be the comparison operator, or it might
25402 start a template argument list. */
25403 ++maybe_template_id;
25404 break;
25406 case CPP_RSHIFT:
25407 if (cxx_dialect == cxx98)
25408 break;
25409 /* Fall through for C++0x, which treats the `>>'
25410 operator like two `>' tokens in certain
25411 cases. */
25413 case CPP_GREATER:
25414 if (depth == 0)
25416 /* This might be an operator, or it might close a
25417 template argument list. But if a previous '<'
25418 started a template argument list, this will have
25419 closed it, so we can't be in one anymore. */
25420 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25421 if (maybe_template_id < 0)
25422 maybe_template_id = 0;
25424 break;
25426 /* If we run out of tokens, issue an error message. */
25427 case CPP_EOF:
25428 case CPP_PRAGMA_EOL:
25429 error_at (token->location, "file ends in default argument");
25430 done = true;
25431 break;
25433 case CPP_NAME:
25434 case CPP_SCOPE:
25435 /* In these cases, we should look for template-ids.
25436 For example, if the default argument is
25437 `X<int, double>()', we need to do name lookup to
25438 figure out whether or not `X' is a template; if
25439 so, the `,' does not end the default argument.
25441 That is not yet done. */
25442 break;
25444 default:
25445 break;
25448 /* If we've reached the end, stop. */
25449 if (done)
25450 break;
25452 /* Add the token to the token block. */
25453 token = cp_lexer_consume_token (parser->lexer);
25456 /* Create a DEFAULT_ARG to represent the unparsed default
25457 argument. */
25458 default_argument = make_node (DEFAULT_ARG);
25459 DEFARG_TOKENS (default_argument)
25460 = cp_token_cache_new (first_token, token);
25461 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25463 return default_argument;
25466 /* Begin parsing tentatively. We always save tokens while parsing
25467 tentatively so that if the tentative parsing fails we can restore the
25468 tokens. */
25470 static void
25471 cp_parser_parse_tentatively (cp_parser* parser)
25473 /* Enter a new parsing context. */
25474 parser->context = cp_parser_context_new (parser->context);
25475 /* Begin saving tokens. */
25476 cp_lexer_save_tokens (parser->lexer);
25477 /* In order to avoid repetitive access control error messages,
25478 access checks are queued up until we are no longer parsing
25479 tentatively. */
25480 push_deferring_access_checks (dk_deferred);
25483 /* Commit to the currently active tentative parse. */
25485 static void
25486 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25488 cp_parser_context *context;
25489 cp_lexer *lexer;
25491 /* Mark all of the levels as committed. */
25492 lexer = parser->lexer;
25493 for (context = parser->context; context->next; context = context->next)
25495 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25496 break;
25497 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25498 while (!cp_lexer_saving_tokens (lexer))
25499 lexer = lexer->next;
25500 cp_lexer_commit_tokens (lexer);
25504 /* Commit to the topmost currently active tentative parse.
25506 Note that this function shouldn't be called when there are
25507 irreversible side-effects while in a tentative state. For
25508 example, we shouldn't create a permanent entry in the symbol
25509 table, or issue an error message that might not apply if the
25510 tentative parse is aborted. */
25512 static void
25513 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25515 cp_parser_context *context = parser->context;
25516 cp_lexer *lexer = parser->lexer;
25518 if (context)
25520 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25521 return;
25522 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25524 while (!cp_lexer_saving_tokens (lexer))
25525 lexer = lexer->next;
25526 cp_lexer_commit_tokens (lexer);
25530 /* Abort the currently active tentative parse. All consumed tokens
25531 will be rolled back, and no diagnostics will be issued. */
25533 static void
25534 cp_parser_abort_tentative_parse (cp_parser* parser)
25536 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25537 || errorcount > 0);
25538 cp_parser_simulate_error (parser);
25539 /* Now, pretend that we want to see if the construct was
25540 successfully parsed. */
25541 cp_parser_parse_definitely (parser);
25544 /* Stop parsing tentatively. If a parse error has occurred, restore the
25545 token stream. Otherwise, commit to the tokens we have consumed.
25546 Returns true if no error occurred; false otherwise. */
25548 static bool
25549 cp_parser_parse_definitely (cp_parser* parser)
25551 bool error_occurred;
25552 cp_parser_context *context;
25554 /* Remember whether or not an error occurred, since we are about to
25555 destroy that information. */
25556 error_occurred = cp_parser_error_occurred (parser);
25557 /* Remove the topmost context from the stack. */
25558 context = parser->context;
25559 parser->context = context->next;
25560 /* If no parse errors occurred, commit to the tentative parse. */
25561 if (!error_occurred)
25563 /* Commit to the tokens read tentatively, unless that was
25564 already done. */
25565 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25566 cp_lexer_commit_tokens (parser->lexer);
25568 pop_to_parent_deferring_access_checks ();
25570 /* Otherwise, if errors occurred, roll back our state so that things
25571 are just as they were before we began the tentative parse. */
25572 else
25574 cp_lexer_rollback_tokens (parser->lexer);
25575 pop_deferring_access_checks ();
25577 /* Add the context to the front of the free list. */
25578 context->next = cp_parser_context_free_list;
25579 cp_parser_context_free_list = context;
25581 return !error_occurred;
25584 /* Returns true if we are parsing tentatively and are not committed to
25585 this tentative parse. */
25587 static bool
25588 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25590 return (cp_parser_parsing_tentatively (parser)
25591 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25594 /* Returns nonzero iff an error has occurred during the most recent
25595 tentative parse. */
25597 static bool
25598 cp_parser_error_occurred (cp_parser* parser)
25600 return (cp_parser_parsing_tentatively (parser)
25601 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25604 /* Returns nonzero if GNU extensions are allowed. */
25606 static bool
25607 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25609 return parser->allow_gnu_extensions_p;
25612 /* Objective-C++ Productions */
25615 /* Parse an Objective-C expression, which feeds into a primary-expression
25616 above.
25618 objc-expression:
25619 objc-message-expression
25620 objc-string-literal
25621 objc-encode-expression
25622 objc-protocol-expression
25623 objc-selector-expression
25625 Returns a tree representation of the expression. */
25627 static tree
25628 cp_parser_objc_expression (cp_parser* parser)
25630 /* Try to figure out what kind of declaration is present. */
25631 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25633 switch (kwd->type)
25635 case CPP_OPEN_SQUARE:
25636 return cp_parser_objc_message_expression (parser);
25638 case CPP_OBJC_STRING:
25639 kwd = cp_lexer_consume_token (parser->lexer);
25640 return objc_build_string_object (kwd->u.value);
25642 case CPP_KEYWORD:
25643 switch (kwd->keyword)
25645 case RID_AT_ENCODE:
25646 return cp_parser_objc_encode_expression (parser);
25648 case RID_AT_PROTOCOL:
25649 return cp_parser_objc_protocol_expression (parser);
25651 case RID_AT_SELECTOR:
25652 return cp_parser_objc_selector_expression (parser);
25654 default:
25655 break;
25657 default:
25658 error_at (kwd->location,
25659 "misplaced %<@%D%> Objective-C++ construct",
25660 kwd->u.value);
25661 cp_parser_skip_to_end_of_block_or_statement (parser);
25664 return error_mark_node;
25667 /* Parse an Objective-C message expression.
25669 objc-message-expression:
25670 [ objc-message-receiver objc-message-args ]
25672 Returns a representation of an Objective-C message. */
25674 static tree
25675 cp_parser_objc_message_expression (cp_parser* parser)
25677 tree receiver, messageargs;
25679 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25680 receiver = cp_parser_objc_message_receiver (parser);
25681 messageargs = cp_parser_objc_message_args (parser);
25682 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25684 return objc_build_message_expr (receiver, messageargs);
25687 /* Parse an objc-message-receiver.
25689 objc-message-receiver:
25690 expression
25691 simple-type-specifier
25693 Returns a representation of the type or expression. */
25695 static tree
25696 cp_parser_objc_message_receiver (cp_parser* parser)
25698 tree rcv;
25700 /* An Objective-C message receiver may be either (1) a type
25701 or (2) an expression. */
25702 cp_parser_parse_tentatively (parser);
25703 rcv = cp_parser_expression (parser);
25705 /* If that worked out, fine. */
25706 if (cp_parser_parse_definitely (parser))
25707 return rcv;
25709 cp_parser_parse_tentatively (parser);
25710 rcv = cp_parser_simple_type_specifier (parser,
25711 /*decl_specs=*/NULL,
25712 CP_PARSER_FLAGS_NONE);
25714 if (cp_parser_parse_definitely (parser))
25715 return objc_get_class_reference (rcv);
25717 cp_parser_error (parser, "objective-c++ message receiver expected");
25718 return error_mark_node;
25721 /* Parse the arguments and selectors comprising an Objective-C message.
25723 objc-message-args:
25724 objc-selector
25725 objc-selector-args
25726 objc-selector-args , objc-comma-args
25728 objc-selector-args:
25729 objc-selector [opt] : assignment-expression
25730 objc-selector-args objc-selector [opt] : assignment-expression
25732 objc-comma-args:
25733 assignment-expression
25734 objc-comma-args , assignment-expression
25736 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25737 selector arguments and TREE_VALUE containing a list of comma
25738 arguments. */
25740 static tree
25741 cp_parser_objc_message_args (cp_parser* parser)
25743 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25744 bool maybe_unary_selector_p = true;
25745 cp_token *token = cp_lexer_peek_token (parser->lexer);
25747 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25749 tree selector = NULL_TREE, arg;
25751 if (token->type != CPP_COLON)
25752 selector = cp_parser_objc_selector (parser);
25754 /* Detect if we have a unary selector. */
25755 if (maybe_unary_selector_p
25756 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25757 return build_tree_list (selector, NULL_TREE);
25759 maybe_unary_selector_p = false;
25760 cp_parser_require (parser, CPP_COLON, RT_COLON);
25761 arg = cp_parser_assignment_expression (parser);
25763 sel_args
25764 = chainon (sel_args,
25765 build_tree_list (selector, arg));
25767 token = cp_lexer_peek_token (parser->lexer);
25770 /* Handle non-selector arguments, if any. */
25771 while (token->type == CPP_COMMA)
25773 tree arg;
25775 cp_lexer_consume_token (parser->lexer);
25776 arg = cp_parser_assignment_expression (parser);
25778 addl_args
25779 = chainon (addl_args,
25780 build_tree_list (NULL_TREE, arg));
25782 token = cp_lexer_peek_token (parser->lexer);
25785 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25787 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25788 return build_tree_list (error_mark_node, error_mark_node);
25791 return build_tree_list (sel_args, addl_args);
25794 /* Parse an Objective-C encode expression.
25796 objc-encode-expression:
25797 @encode objc-typename
25799 Returns an encoded representation of the type argument. */
25801 static tree
25802 cp_parser_objc_encode_expression (cp_parser* parser)
25804 tree type;
25805 cp_token *token;
25807 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25808 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25809 token = cp_lexer_peek_token (parser->lexer);
25810 type = complete_type (cp_parser_type_id (parser));
25811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25813 if (!type)
25815 error_at (token->location,
25816 "%<@encode%> must specify a type as an argument");
25817 return error_mark_node;
25820 /* This happens if we find @encode(T) (where T is a template
25821 typename or something dependent on a template typename) when
25822 parsing a template. In that case, we can't compile it
25823 immediately, but we rather create an AT_ENCODE_EXPR which will
25824 need to be instantiated when the template is used.
25826 if (dependent_type_p (type))
25828 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25829 TREE_READONLY (value) = 1;
25830 return value;
25833 return objc_build_encode_expr (type);
25836 /* Parse an Objective-C @defs expression. */
25838 static tree
25839 cp_parser_objc_defs_expression (cp_parser *parser)
25841 tree name;
25843 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25845 name = cp_parser_identifier (parser);
25846 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25848 return objc_get_class_ivars (name);
25851 /* Parse an Objective-C protocol expression.
25853 objc-protocol-expression:
25854 @protocol ( identifier )
25856 Returns a representation of the protocol expression. */
25858 static tree
25859 cp_parser_objc_protocol_expression (cp_parser* parser)
25861 tree proto;
25863 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25865 proto = cp_parser_identifier (parser);
25866 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25868 return objc_build_protocol_expr (proto);
25871 /* Parse an Objective-C selector expression.
25873 objc-selector-expression:
25874 @selector ( objc-method-signature )
25876 objc-method-signature:
25877 objc-selector
25878 objc-selector-seq
25880 objc-selector-seq:
25881 objc-selector :
25882 objc-selector-seq objc-selector :
25884 Returns a representation of the method selector. */
25886 static tree
25887 cp_parser_objc_selector_expression (cp_parser* parser)
25889 tree sel_seq = NULL_TREE;
25890 bool maybe_unary_selector_p = true;
25891 cp_token *token;
25892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25894 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25896 token = cp_lexer_peek_token (parser->lexer);
25898 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25899 || token->type == CPP_SCOPE)
25901 tree selector = NULL_TREE;
25903 if (token->type != CPP_COLON
25904 || token->type == CPP_SCOPE)
25905 selector = cp_parser_objc_selector (parser);
25907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25908 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25910 /* Detect if we have a unary selector. */
25911 if (maybe_unary_selector_p)
25913 sel_seq = selector;
25914 goto finish_selector;
25916 else
25918 cp_parser_error (parser, "expected %<:%>");
25921 maybe_unary_selector_p = false;
25922 token = cp_lexer_consume_token (parser->lexer);
25924 if (token->type == CPP_SCOPE)
25926 sel_seq
25927 = chainon (sel_seq,
25928 build_tree_list (selector, NULL_TREE));
25929 sel_seq
25930 = chainon (sel_seq,
25931 build_tree_list (NULL_TREE, NULL_TREE));
25933 else
25934 sel_seq
25935 = chainon (sel_seq,
25936 build_tree_list (selector, NULL_TREE));
25938 token = cp_lexer_peek_token (parser->lexer);
25941 finish_selector:
25942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25944 return objc_build_selector_expr (loc, sel_seq);
25947 /* Parse a list of identifiers.
25949 objc-identifier-list:
25950 identifier
25951 objc-identifier-list , identifier
25953 Returns a TREE_LIST of identifier nodes. */
25955 static tree
25956 cp_parser_objc_identifier_list (cp_parser* parser)
25958 tree identifier;
25959 tree list;
25960 cp_token *sep;
25962 identifier = cp_parser_identifier (parser);
25963 if (identifier == error_mark_node)
25964 return error_mark_node;
25966 list = build_tree_list (NULL_TREE, identifier);
25967 sep = cp_lexer_peek_token (parser->lexer);
25969 while (sep->type == CPP_COMMA)
25971 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25972 identifier = cp_parser_identifier (parser);
25973 if (identifier == error_mark_node)
25974 return list;
25976 list = chainon (list, build_tree_list (NULL_TREE,
25977 identifier));
25978 sep = cp_lexer_peek_token (parser->lexer);
25981 return list;
25984 /* Parse an Objective-C alias declaration.
25986 objc-alias-declaration:
25987 @compatibility_alias identifier identifier ;
25989 This function registers the alias mapping with the Objective-C front end.
25990 It returns nothing. */
25992 static void
25993 cp_parser_objc_alias_declaration (cp_parser* parser)
25995 tree alias, orig;
25997 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25998 alias = cp_parser_identifier (parser);
25999 orig = cp_parser_identifier (parser);
26000 objc_declare_alias (alias, orig);
26001 cp_parser_consume_semicolon_at_end_of_statement (parser);
26004 /* Parse an Objective-C class forward-declaration.
26006 objc-class-declaration:
26007 @class objc-identifier-list ;
26009 The function registers the forward declarations with the Objective-C
26010 front end. It returns nothing. */
26012 static void
26013 cp_parser_objc_class_declaration (cp_parser* parser)
26015 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26016 while (true)
26018 tree id;
26020 id = cp_parser_identifier (parser);
26021 if (id == error_mark_node)
26022 break;
26024 objc_declare_class (id);
26026 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26027 cp_lexer_consume_token (parser->lexer);
26028 else
26029 break;
26031 cp_parser_consume_semicolon_at_end_of_statement (parser);
26034 /* Parse a list of Objective-C protocol references.
26036 objc-protocol-refs-opt:
26037 objc-protocol-refs [opt]
26039 objc-protocol-refs:
26040 < objc-identifier-list >
26042 Returns a TREE_LIST of identifiers, if any. */
26044 static tree
26045 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26047 tree protorefs = NULL_TREE;
26049 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26051 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26052 protorefs = cp_parser_objc_identifier_list (parser);
26053 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26056 return protorefs;
26059 /* Parse a Objective-C visibility specification. */
26061 static void
26062 cp_parser_objc_visibility_spec (cp_parser* parser)
26064 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26066 switch (vis->keyword)
26068 case RID_AT_PRIVATE:
26069 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26070 break;
26071 case RID_AT_PROTECTED:
26072 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26073 break;
26074 case RID_AT_PUBLIC:
26075 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26076 break;
26077 case RID_AT_PACKAGE:
26078 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26079 break;
26080 default:
26081 return;
26084 /* Eat '@private'/'@protected'/'@public'. */
26085 cp_lexer_consume_token (parser->lexer);
26088 /* Parse an Objective-C method type. Return 'true' if it is a class
26089 (+) method, and 'false' if it is an instance (-) method. */
26091 static inline bool
26092 cp_parser_objc_method_type (cp_parser* parser)
26094 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26095 return true;
26096 else
26097 return false;
26100 /* Parse an Objective-C protocol qualifier. */
26102 static tree
26103 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26105 tree quals = NULL_TREE, node;
26106 cp_token *token = cp_lexer_peek_token (parser->lexer);
26108 node = token->u.value;
26110 while (node && identifier_p (node)
26111 && (node == ridpointers [(int) RID_IN]
26112 || node == ridpointers [(int) RID_OUT]
26113 || node == ridpointers [(int) RID_INOUT]
26114 || node == ridpointers [(int) RID_BYCOPY]
26115 || node == ridpointers [(int) RID_BYREF]
26116 || node == ridpointers [(int) RID_ONEWAY]))
26118 quals = tree_cons (NULL_TREE, node, quals);
26119 cp_lexer_consume_token (parser->lexer);
26120 token = cp_lexer_peek_token (parser->lexer);
26121 node = token->u.value;
26124 return quals;
26127 /* Parse an Objective-C typename. */
26129 static tree
26130 cp_parser_objc_typename (cp_parser* parser)
26132 tree type_name = NULL_TREE;
26134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26136 tree proto_quals, cp_type = NULL_TREE;
26138 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26139 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26141 /* An ObjC type name may consist of just protocol qualifiers, in which
26142 case the type shall default to 'id'. */
26143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26145 cp_type = cp_parser_type_id (parser);
26147 /* If the type could not be parsed, an error has already
26148 been produced. For error recovery, behave as if it had
26149 not been specified, which will use the default type
26150 'id'. */
26151 if (cp_type == error_mark_node)
26153 cp_type = NULL_TREE;
26154 /* We need to skip to the closing parenthesis as
26155 cp_parser_type_id() does not seem to do it for
26156 us. */
26157 cp_parser_skip_to_closing_parenthesis (parser,
26158 /*recovering=*/true,
26159 /*or_comma=*/false,
26160 /*consume_paren=*/false);
26164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26165 type_name = build_tree_list (proto_quals, cp_type);
26168 return type_name;
26171 /* Check to see if TYPE refers to an Objective-C selector name. */
26173 static bool
26174 cp_parser_objc_selector_p (enum cpp_ttype type)
26176 return (type == CPP_NAME || type == CPP_KEYWORD
26177 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26178 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26179 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26180 || type == CPP_XOR || type == CPP_XOR_EQ);
26183 /* Parse an Objective-C selector. */
26185 static tree
26186 cp_parser_objc_selector (cp_parser* parser)
26188 cp_token *token = cp_lexer_consume_token (parser->lexer);
26190 if (!cp_parser_objc_selector_p (token->type))
26192 error_at (token->location, "invalid Objective-C++ selector name");
26193 return error_mark_node;
26196 /* C++ operator names are allowed to appear in ObjC selectors. */
26197 switch (token->type)
26199 case CPP_AND_AND: return get_identifier ("and");
26200 case CPP_AND_EQ: return get_identifier ("and_eq");
26201 case CPP_AND: return get_identifier ("bitand");
26202 case CPP_OR: return get_identifier ("bitor");
26203 case CPP_COMPL: return get_identifier ("compl");
26204 case CPP_NOT: return get_identifier ("not");
26205 case CPP_NOT_EQ: return get_identifier ("not_eq");
26206 case CPP_OR_OR: return get_identifier ("or");
26207 case CPP_OR_EQ: return get_identifier ("or_eq");
26208 case CPP_XOR: return get_identifier ("xor");
26209 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26210 default: return token->u.value;
26214 /* Parse an Objective-C params list. */
26216 static tree
26217 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26219 tree params = NULL_TREE;
26220 bool maybe_unary_selector_p = true;
26221 cp_token *token = cp_lexer_peek_token (parser->lexer);
26223 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26225 tree selector = NULL_TREE, type_name, identifier;
26226 tree parm_attr = NULL_TREE;
26228 if (token->keyword == RID_ATTRIBUTE)
26229 break;
26231 if (token->type != CPP_COLON)
26232 selector = cp_parser_objc_selector (parser);
26234 /* Detect if we have a unary selector. */
26235 if (maybe_unary_selector_p
26236 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26238 params = selector; /* Might be followed by attributes. */
26239 break;
26242 maybe_unary_selector_p = false;
26243 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26245 /* Something went quite wrong. There should be a colon
26246 here, but there is not. Stop parsing parameters. */
26247 break;
26249 type_name = cp_parser_objc_typename (parser);
26250 /* New ObjC allows attributes on parameters too. */
26251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26252 parm_attr = cp_parser_attributes_opt (parser);
26253 identifier = cp_parser_identifier (parser);
26255 params
26256 = chainon (params,
26257 objc_build_keyword_decl (selector,
26258 type_name,
26259 identifier,
26260 parm_attr));
26262 token = cp_lexer_peek_token (parser->lexer);
26265 if (params == NULL_TREE)
26267 cp_parser_error (parser, "objective-c++ method declaration is expected");
26268 return error_mark_node;
26271 /* We allow tail attributes for the method. */
26272 if (token->keyword == RID_ATTRIBUTE)
26274 *attributes = cp_parser_attributes_opt (parser);
26275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26276 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26277 return params;
26278 cp_parser_error (parser,
26279 "method attributes must be specified at the end");
26280 return error_mark_node;
26283 if (params == NULL_TREE)
26285 cp_parser_error (parser, "objective-c++ method declaration is expected");
26286 return error_mark_node;
26288 return params;
26291 /* Parse the non-keyword Objective-C params. */
26293 static tree
26294 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26295 tree* attributes)
26297 tree params = make_node (TREE_LIST);
26298 cp_token *token = cp_lexer_peek_token (parser->lexer);
26299 *ellipsisp = false; /* Initially, assume no ellipsis. */
26301 while (token->type == CPP_COMMA)
26303 cp_parameter_declarator *parmdecl;
26304 tree parm;
26306 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26307 token = cp_lexer_peek_token (parser->lexer);
26309 if (token->type == CPP_ELLIPSIS)
26311 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26312 *ellipsisp = true;
26313 token = cp_lexer_peek_token (parser->lexer);
26314 break;
26317 /* TODO: parse attributes for tail parameters. */
26318 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26319 parm = grokdeclarator (parmdecl->declarator,
26320 &parmdecl->decl_specifiers,
26321 PARM, /*initialized=*/0,
26322 /*attrlist=*/NULL);
26324 chainon (params, build_tree_list (NULL_TREE, parm));
26325 token = cp_lexer_peek_token (parser->lexer);
26328 /* We allow tail attributes for the method. */
26329 if (token->keyword == RID_ATTRIBUTE)
26331 if (*attributes == NULL_TREE)
26333 *attributes = cp_parser_attributes_opt (parser);
26334 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26335 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26336 return params;
26338 else
26339 /* We have an error, but parse the attributes, so that we can
26340 carry on. */
26341 *attributes = cp_parser_attributes_opt (parser);
26343 cp_parser_error (parser,
26344 "method attributes must be specified at the end");
26345 return error_mark_node;
26348 return params;
26351 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26353 static void
26354 cp_parser_objc_interstitial_code (cp_parser* parser)
26356 cp_token *token = cp_lexer_peek_token (parser->lexer);
26358 /* If the next token is `extern' and the following token is a string
26359 literal, then we have a linkage specification. */
26360 if (token->keyword == RID_EXTERN
26361 && cp_parser_is_pure_string_literal
26362 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26363 cp_parser_linkage_specification (parser);
26364 /* Handle #pragma, if any. */
26365 else if (token->type == CPP_PRAGMA)
26366 cp_parser_pragma (parser, pragma_objc_icode);
26367 /* Allow stray semicolons. */
26368 else if (token->type == CPP_SEMICOLON)
26369 cp_lexer_consume_token (parser->lexer);
26370 /* Mark methods as optional or required, when building protocols. */
26371 else if (token->keyword == RID_AT_OPTIONAL)
26373 cp_lexer_consume_token (parser->lexer);
26374 objc_set_method_opt (true);
26376 else if (token->keyword == RID_AT_REQUIRED)
26378 cp_lexer_consume_token (parser->lexer);
26379 objc_set_method_opt (false);
26381 else if (token->keyword == RID_NAMESPACE)
26382 cp_parser_namespace_definition (parser);
26383 /* Other stray characters must generate errors. */
26384 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26386 cp_lexer_consume_token (parser->lexer);
26387 error ("stray %qs between Objective-C++ methods",
26388 token->type == CPP_OPEN_BRACE ? "{" : "}");
26390 /* Finally, try to parse a block-declaration, or a function-definition. */
26391 else
26392 cp_parser_block_declaration (parser, /*statement_p=*/false);
26395 /* Parse a method signature. */
26397 static tree
26398 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26400 tree rettype, kwdparms, optparms;
26401 bool ellipsis = false;
26402 bool is_class_method;
26404 is_class_method = cp_parser_objc_method_type (parser);
26405 rettype = cp_parser_objc_typename (parser);
26406 *attributes = NULL_TREE;
26407 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26408 if (kwdparms == error_mark_node)
26409 return error_mark_node;
26410 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26411 if (optparms == error_mark_node)
26412 return error_mark_node;
26414 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26417 static bool
26418 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26420 tree tattr;
26421 cp_lexer_save_tokens (parser->lexer);
26422 tattr = cp_parser_attributes_opt (parser);
26423 gcc_assert (tattr) ;
26425 /* If the attributes are followed by a method introducer, this is not allowed.
26426 Dump the attributes and flag the situation. */
26427 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26428 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26429 return true;
26431 /* Otherwise, the attributes introduce some interstitial code, possibly so
26432 rewind to allow that check. */
26433 cp_lexer_rollback_tokens (parser->lexer);
26434 return false;
26437 /* Parse an Objective-C method prototype list. */
26439 static void
26440 cp_parser_objc_method_prototype_list (cp_parser* parser)
26442 cp_token *token = cp_lexer_peek_token (parser->lexer);
26444 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26446 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26448 tree attributes, sig;
26449 bool is_class_method;
26450 if (token->type == CPP_PLUS)
26451 is_class_method = true;
26452 else
26453 is_class_method = false;
26454 sig = cp_parser_objc_method_signature (parser, &attributes);
26455 if (sig == error_mark_node)
26457 cp_parser_skip_to_end_of_block_or_statement (parser);
26458 token = cp_lexer_peek_token (parser->lexer);
26459 continue;
26461 objc_add_method_declaration (is_class_method, sig, attributes);
26462 cp_parser_consume_semicolon_at_end_of_statement (parser);
26464 else if (token->keyword == RID_AT_PROPERTY)
26465 cp_parser_objc_at_property_declaration (parser);
26466 else if (token->keyword == RID_ATTRIBUTE
26467 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26468 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26469 OPT_Wattributes,
26470 "prefix attributes are ignored for methods");
26471 else
26472 /* Allow for interspersed non-ObjC++ code. */
26473 cp_parser_objc_interstitial_code (parser);
26475 token = cp_lexer_peek_token (parser->lexer);
26478 if (token->type != CPP_EOF)
26479 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26480 else
26481 cp_parser_error (parser, "expected %<@end%>");
26483 objc_finish_interface ();
26486 /* Parse an Objective-C method definition list. */
26488 static void
26489 cp_parser_objc_method_definition_list (cp_parser* parser)
26491 cp_token *token = cp_lexer_peek_token (parser->lexer);
26493 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26495 tree meth;
26497 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26499 cp_token *ptk;
26500 tree sig, attribute;
26501 bool is_class_method;
26502 if (token->type == CPP_PLUS)
26503 is_class_method = true;
26504 else
26505 is_class_method = false;
26506 push_deferring_access_checks (dk_deferred);
26507 sig = cp_parser_objc_method_signature (parser, &attribute);
26508 if (sig == error_mark_node)
26510 cp_parser_skip_to_end_of_block_or_statement (parser);
26511 token = cp_lexer_peek_token (parser->lexer);
26512 continue;
26514 objc_start_method_definition (is_class_method, sig, attribute,
26515 NULL_TREE);
26517 /* For historical reasons, we accept an optional semicolon. */
26518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26519 cp_lexer_consume_token (parser->lexer);
26521 ptk = cp_lexer_peek_token (parser->lexer);
26522 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26523 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26525 perform_deferred_access_checks (tf_warning_or_error);
26526 stop_deferring_access_checks ();
26527 meth = cp_parser_function_definition_after_declarator (parser,
26528 false);
26529 pop_deferring_access_checks ();
26530 objc_finish_method_definition (meth);
26533 /* The following case will be removed once @synthesize is
26534 completely implemented. */
26535 else if (token->keyword == RID_AT_PROPERTY)
26536 cp_parser_objc_at_property_declaration (parser);
26537 else if (token->keyword == RID_AT_SYNTHESIZE)
26538 cp_parser_objc_at_synthesize_declaration (parser);
26539 else if (token->keyword == RID_AT_DYNAMIC)
26540 cp_parser_objc_at_dynamic_declaration (parser);
26541 else if (token->keyword == RID_ATTRIBUTE
26542 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26543 warning_at (token->location, OPT_Wattributes,
26544 "prefix attributes are ignored for methods");
26545 else
26546 /* Allow for interspersed non-ObjC++ code. */
26547 cp_parser_objc_interstitial_code (parser);
26549 token = cp_lexer_peek_token (parser->lexer);
26552 if (token->type != CPP_EOF)
26553 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26554 else
26555 cp_parser_error (parser, "expected %<@end%>");
26557 objc_finish_implementation ();
26560 /* Parse Objective-C ivars. */
26562 static void
26563 cp_parser_objc_class_ivars (cp_parser* parser)
26565 cp_token *token = cp_lexer_peek_token (parser->lexer);
26567 if (token->type != CPP_OPEN_BRACE)
26568 return; /* No ivars specified. */
26570 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26571 token = cp_lexer_peek_token (parser->lexer);
26573 while (token->type != CPP_CLOSE_BRACE
26574 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26576 cp_decl_specifier_seq declspecs;
26577 int decl_class_or_enum_p;
26578 tree prefix_attributes;
26580 cp_parser_objc_visibility_spec (parser);
26582 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26583 break;
26585 cp_parser_decl_specifier_seq (parser,
26586 CP_PARSER_FLAGS_OPTIONAL,
26587 &declspecs,
26588 &decl_class_or_enum_p);
26590 /* auto, register, static, extern, mutable. */
26591 if (declspecs.storage_class != sc_none)
26593 cp_parser_error (parser, "invalid type for instance variable");
26594 declspecs.storage_class = sc_none;
26597 /* thread_local. */
26598 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26600 cp_parser_error (parser, "invalid type for instance variable");
26601 declspecs.locations[ds_thread] = 0;
26604 /* typedef. */
26605 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26607 cp_parser_error (parser, "invalid type for instance variable");
26608 declspecs.locations[ds_typedef] = 0;
26611 prefix_attributes = declspecs.attributes;
26612 declspecs.attributes = NULL_TREE;
26614 /* Keep going until we hit the `;' at the end of the
26615 declaration. */
26616 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26618 tree width = NULL_TREE, attributes, first_attribute, decl;
26619 cp_declarator *declarator = NULL;
26620 int ctor_dtor_or_conv_p;
26622 /* Check for a (possibly unnamed) bitfield declaration. */
26623 token = cp_lexer_peek_token (parser->lexer);
26624 if (token->type == CPP_COLON)
26625 goto eat_colon;
26627 if (token->type == CPP_NAME
26628 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26629 == CPP_COLON))
26631 /* Get the name of the bitfield. */
26632 declarator = make_id_declarator (NULL_TREE,
26633 cp_parser_identifier (parser),
26634 sfk_none);
26636 eat_colon:
26637 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26638 /* Get the width of the bitfield. */
26639 width
26640 = cp_parser_constant_expression (parser);
26642 else
26644 /* Parse the declarator. */
26645 declarator
26646 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26647 &ctor_dtor_or_conv_p,
26648 /*parenthesized_p=*/NULL,
26649 /*member_p=*/false,
26650 /*friend_p=*/false);
26653 /* Look for attributes that apply to the ivar. */
26654 attributes = cp_parser_attributes_opt (parser);
26655 /* Remember which attributes are prefix attributes and
26656 which are not. */
26657 first_attribute = attributes;
26658 /* Combine the attributes. */
26659 attributes = chainon (prefix_attributes, attributes);
26661 if (width)
26662 /* Create the bitfield declaration. */
26663 decl = grokbitfield (declarator, &declspecs,
26664 width,
26665 attributes);
26666 else
26667 decl = grokfield (declarator, &declspecs,
26668 NULL_TREE, /*init_const_expr_p=*/false,
26669 NULL_TREE, attributes);
26671 /* Add the instance variable. */
26672 if (decl != error_mark_node && decl != NULL_TREE)
26673 objc_add_instance_variable (decl);
26675 /* Reset PREFIX_ATTRIBUTES. */
26676 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26677 attributes = TREE_CHAIN (attributes);
26678 if (attributes)
26679 TREE_CHAIN (attributes) = NULL_TREE;
26681 token = cp_lexer_peek_token (parser->lexer);
26683 if (token->type == CPP_COMMA)
26685 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26686 continue;
26688 break;
26691 cp_parser_consume_semicolon_at_end_of_statement (parser);
26692 token = cp_lexer_peek_token (parser->lexer);
26695 if (token->keyword == RID_AT_END)
26696 cp_parser_error (parser, "expected %<}%>");
26698 /* Do not consume the RID_AT_END, so it will be read again as terminating
26699 the @interface of @implementation. */
26700 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26701 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26703 /* For historical reasons, we accept an optional semicolon. */
26704 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26705 cp_lexer_consume_token (parser->lexer);
26708 /* Parse an Objective-C protocol declaration. */
26710 static void
26711 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26713 tree proto, protorefs;
26714 cp_token *tok;
26716 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26719 tok = cp_lexer_peek_token (parser->lexer);
26720 error_at (tok->location, "identifier expected after %<@protocol%>");
26721 cp_parser_consume_semicolon_at_end_of_statement (parser);
26722 return;
26725 /* See if we have a forward declaration or a definition. */
26726 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26728 /* Try a forward declaration first. */
26729 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26731 while (true)
26733 tree id;
26735 id = cp_parser_identifier (parser);
26736 if (id == error_mark_node)
26737 break;
26739 objc_declare_protocol (id, attributes);
26741 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26742 cp_lexer_consume_token (parser->lexer);
26743 else
26744 break;
26746 cp_parser_consume_semicolon_at_end_of_statement (parser);
26749 /* Ok, we got a full-fledged definition (or at least should). */
26750 else
26752 proto = cp_parser_identifier (parser);
26753 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26754 objc_start_protocol (proto, protorefs, attributes);
26755 cp_parser_objc_method_prototype_list (parser);
26759 /* Parse an Objective-C superclass or category. */
26761 static void
26762 cp_parser_objc_superclass_or_category (cp_parser *parser,
26763 bool iface_p,
26764 tree *super,
26765 tree *categ, bool *is_class_extension)
26767 cp_token *next = cp_lexer_peek_token (parser->lexer);
26769 *super = *categ = NULL_TREE;
26770 *is_class_extension = false;
26771 if (next->type == CPP_COLON)
26773 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26774 *super = cp_parser_identifier (parser);
26776 else if (next->type == CPP_OPEN_PAREN)
26778 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26780 /* If there is no category name, and this is an @interface, we
26781 have a class extension. */
26782 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26784 *categ = NULL_TREE;
26785 *is_class_extension = true;
26787 else
26788 *categ = cp_parser_identifier (parser);
26790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26794 /* Parse an Objective-C class interface. */
26796 static void
26797 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26799 tree name, super, categ, protos;
26800 bool is_class_extension;
26802 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26803 name = cp_parser_identifier (parser);
26804 if (name == error_mark_node)
26806 /* It's hard to recover because even if valid @interface stuff
26807 is to follow, we can't compile it (or validate it) if we
26808 don't even know which class it refers to. Let's assume this
26809 was a stray '@interface' token in the stream and skip it.
26811 return;
26813 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26814 &is_class_extension);
26815 protos = cp_parser_objc_protocol_refs_opt (parser);
26817 /* We have either a class or a category on our hands. */
26818 if (categ || is_class_extension)
26819 objc_start_category_interface (name, categ, protos, attributes);
26820 else
26822 objc_start_class_interface (name, super, protos, attributes);
26823 /* Handle instance variable declarations, if any. */
26824 cp_parser_objc_class_ivars (parser);
26825 objc_continue_interface ();
26828 cp_parser_objc_method_prototype_list (parser);
26831 /* Parse an Objective-C class implementation. */
26833 static void
26834 cp_parser_objc_class_implementation (cp_parser* parser)
26836 tree name, super, categ;
26837 bool is_class_extension;
26839 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26840 name = cp_parser_identifier (parser);
26841 if (name == error_mark_node)
26843 /* It's hard to recover because even if valid @implementation
26844 stuff is to follow, we can't compile it (or validate it) if
26845 we don't even know which class it refers to. Let's assume
26846 this was a stray '@implementation' token in the stream and
26847 skip it.
26849 return;
26851 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26852 &is_class_extension);
26854 /* We have either a class or a category on our hands. */
26855 if (categ)
26856 objc_start_category_implementation (name, categ);
26857 else
26859 objc_start_class_implementation (name, super);
26860 /* Handle instance variable declarations, if any. */
26861 cp_parser_objc_class_ivars (parser);
26862 objc_continue_implementation ();
26865 cp_parser_objc_method_definition_list (parser);
26868 /* Consume the @end token and finish off the implementation. */
26870 static void
26871 cp_parser_objc_end_implementation (cp_parser* parser)
26873 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26874 objc_finish_implementation ();
26877 /* Parse an Objective-C declaration. */
26879 static void
26880 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26882 /* Try to figure out what kind of declaration is present. */
26883 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26885 if (attributes)
26886 switch (kwd->keyword)
26888 case RID_AT_ALIAS:
26889 case RID_AT_CLASS:
26890 case RID_AT_END:
26891 error_at (kwd->location, "attributes may not be specified before"
26892 " the %<@%D%> Objective-C++ keyword",
26893 kwd->u.value);
26894 attributes = NULL;
26895 break;
26896 case RID_AT_IMPLEMENTATION:
26897 warning_at (kwd->location, OPT_Wattributes,
26898 "prefix attributes are ignored before %<@%D%>",
26899 kwd->u.value);
26900 attributes = NULL;
26901 default:
26902 break;
26905 switch (kwd->keyword)
26907 case RID_AT_ALIAS:
26908 cp_parser_objc_alias_declaration (parser);
26909 break;
26910 case RID_AT_CLASS:
26911 cp_parser_objc_class_declaration (parser);
26912 break;
26913 case RID_AT_PROTOCOL:
26914 cp_parser_objc_protocol_declaration (parser, attributes);
26915 break;
26916 case RID_AT_INTERFACE:
26917 cp_parser_objc_class_interface (parser, attributes);
26918 break;
26919 case RID_AT_IMPLEMENTATION:
26920 cp_parser_objc_class_implementation (parser);
26921 break;
26922 case RID_AT_END:
26923 cp_parser_objc_end_implementation (parser);
26924 break;
26925 default:
26926 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26927 kwd->u.value);
26928 cp_parser_skip_to_end_of_block_or_statement (parser);
26932 /* Parse an Objective-C try-catch-finally statement.
26934 objc-try-catch-finally-stmt:
26935 @try compound-statement objc-catch-clause-seq [opt]
26936 objc-finally-clause [opt]
26938 objc-catch-clause-seq:
26939 objc-catch-clause objc-catch-clause-seq [opt]
26941 objc-catch-clause:
26942 @catch ( objc-exception-declaration ) compound-statement
26944 objc-finally-clause:
26945 @finally compound-statement
26947 objc-exception-declaration:
26948 parameter-declaration
26949 '...'
26951 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26953 Returns NULL_TREE.
26955 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26956 for C. Keep them in sync. */
26958 static tree
26959 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26961 location_t location;
26962 tree stmt;
26964 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26965 location = cp_lexer_peek_token (parser->lexer)->location;
26966 objc_maybe_warn_exceptions (location);
26967 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26968 node, lest it get absorbed into the surrounding block. */
26969 stmt = push_stmt_list ();
26970 cp_parser_compound_statement (parser, NULL, false, false);
26971 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26973 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26975 cp_parameter_declarator *parm;
26976 tree parameter_declaration = error_mark_node;
26977 bool seen_open_paren = false;
26979 cp_lexer_consume_token (parser->lexer);
26980 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26981 seen_open_paren = true;
26982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26984 /* We have "@catch (...)" (where the '...' are literally
26985 what is in the code). Skip the '...'.
26986 parameter_declaration is set to NULL_TREE, and
26987 objc_being_catch_clauses() knows that that means
26988 '...'. */
26989 cp_lexer_consume_token (parser->lexer);
26990 parameter_declaration = NULL_TREE;
26992 else
26994 /* We have "@catch (NSException *exception)" or something
26995 like that. Parse the parameter declaration. */
26996 parm = cp_parser_parameter_declaration (parser, false, NULL);
26997 if (parm == NULL)
26998 parameter_declaration = error_mark_node;
26999 else
27000 parameter_declaration = grokdeclarator (parm->declarator,
27001 &parm->decl_specifiers,
27002 PARM, /*initialized=*/0,
27003 /*attrlist=*/NULL);
27005 if (seen_open_paren)
27006 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27007 else
27009 /* If there was no open parenthesis, we are recovering from
27010 an error, and we are trying to figure out what mistake
27011 the user has made. */
27013 /* If there is an immediate closing parenthesis, the user
27014 probably forgot the opening one (ie, they typed "@catch
27015 NSException *e)". Parse the closing parenthesis and keep
27016 going. */
27017 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27018 cp_lexer_consume_token (parser->lexer);
27020 /* If these is no immediate closing parenthesis, the user
27021 probably doesn't know that parenthesis are required at
27022 all (ie, they typed "@catch NSException *e"). So, just
27023 forget about the closing parenthesis and keep going. */
27025 objc_begin_catch_clause (parameter_declaration);
27026 cp_parser_compound_statement (parser, NULL, false, false);
27027 objc_finish_catch_clause ();
27029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27031 cp_lexer_consume_token (parser->lexer);
27032 location = cp_lexer_peek_token (parser->lexer)->location;
27033 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27034 node, lest it get absorbed into the surrounding block. */
27035 stmt = push_stmt_list ();
27036 cp_parser_compound_statement (parser, NULL, false, false);
27037 objc_build_finally_clause (location, pop_stmt_list (stmt));
27040 return objc_finish_try_stmt ();
27043 /* Parse an Objective-C synchronized statement.
27045 objc-synchronized-stmt:
27046 @synchronized ( expression ) compound-statement
27048 Returns NULL_TREE. */
27050 static tree
27051 cp_parser_objc_synchronized_statement (cp_parser *parser)
27053 location_t location;
27054 tree lock, stmt;
27056 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27058 location = cp_lexer_peek_token (parser->lexer)->location;
27059 objc_maybe_warn_exceptions (location);
27060 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27061 lock = cp_parser_expression (parser);
27062 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27064 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27065 node, lest it get absorbed into the surrounding block. */
27066 stmt = push_stmt_list ();
27067 cp_parser_compound_statement (parser, NULL, false, false);
27069 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27072 /* Parse an Objective-C throw statement.
27074 objc-throw-stmt:
27075 @throw assignment-expression [opt] ;
27077 Returns a constructed '@throw' statement. */
27079 static tree
27080 cp_parser_objc_throw_statement (cp_parser *parser)
27082 tree expr = NULL_TREE;
27083 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27085 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27088 expr = cp_parser_expression (parser);
27090 cp_parser_consume_semicolon_at_end_of_statement (parser);
27092 return objc_build_throw_stmt (loc, expr);
27095 /* Parse an Objective-C statement. */
27097 static tree
27098 cp_parser_objc_statement (cp_parser * parser)
27100 /* Try to figure out what kind of declaration is present. */
27101 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27103 switch (kwd->keyword)
27105 case RID_AT_TRY:
27106 return cp_parser_objc_try_catch_finally_statement (parser);
27107 case RID_AT_SYNCHRONIZED:
27108 return cp_parser_objc_synchronized_statement (parser);
27109 case RID_AT_THROW:
27110 return cp_parser_objc_throw_statement (parser);
27111 default:
27112 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27113 kwd->u.value);
27114 cp_parser_skip_to_end_of_block_or_statement (parser);
27117 return error_mark_node;
27120 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27121 look ahead to see if an objc keyword follows the attributes. This
27122 is to detect the use of prefix attributes on ObjC @interface and
27123 @protocol. */
27125 static bool
27126 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27128 cp_lexer_save_tokens (parser->lexer);
27129 *attrib = cp_parser_attributes_opt (parser);
27130 gcc_assert (*attrib);
27131 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27133 cp_lexer_commit_tokens (parser->lexer);
27134 return true;
27136 cp_lexer_rollback_tokens (parser->lexer);
27137 return false;
27140 /* This routine is a minimal replacement for
27141 c_parser_struct_declaration () used when parsing the list of
27142 types/names or ObjC++ properties. For example, when parsing the
27143 code
27145 @property (readonly) int a, b, c;
27147 this function is responsible for parsing "int a, int b, int c" and
27148 returning the declarations as CHAIN of DECLs.
27150 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27151 similar parsing. */
27152 static tree
27153 cp_parser_objc_struct_declaration (cp_parser *parser)
27155 tree decls = NULL_TREE;
27156 cp_decl_specifier_seq declspecs;
27157 int decl_class_or_enum_p;
27158 tree prefix_attributes;
27160 cp_parser_decl_specifier_seq (parser,
27161 CP_PARSER_FLAGS_NONE,
27162 &declspecs,
27163 &decl_class_or_enum_p);
27165 if (declspecs.type == error_mark_node)
27166 return error_mark_node;
27168 /* auto, register, static, extern, mutable. */
27169 if (declspecs.storage_class != sc_none)
27171 cp_parser_error (parser, "invalid type for property");
27172 declspecs.storage_class = sc_none;
27175 /* thread_local. */
27176 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27178 cp_parser_error (parser, "invalid type for property");
27179 declspecs.locations[ds_thread] = 0;
27182 /* typedef. */
27183 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27185 cp_parser_error (parser, "invalid type for property");
27186 declspecs.locations[ds_typedef] = 0;
27189 prefix_attributes = declspecs.attributes;
27190 declspecs.attributes = NULL_TREE;
27192 /* Keep going until we hit the `;' at the end of the declaration. */
27193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27195 tree attributes, first_attribute, decl;
27196 cp_declarator *declarator;
27197 cp_token *token;
27199 /* Parse the declarator. */
27200 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27201 NULL, NULL, false, false);
27203 /* Look for attributes that apply to the ivar. */
27204 attributes = cp_parser_attributes_opt (parser);
27205 /* Remember which attributes are prefix attributes and
27206 which are not. */
27207 first_attribute = attributes;
27208 /* Combine the attributes. */
27209 attributes = chainon (prefix_attributes, attributes);
27211 decl = grokfield (declarator, &declspecs,
27212 NULL_TREE, /*init_const_expr_p=*/false,
27213 NULL_TREE, attributes);
27215 if (decl == error_mark_node || decl == NULL_TREE)
27216 return error_mark_node;
27218 /* Reset PREFIX_ATTRIBUTES. */
27219 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27220 attributes = TREE_CHAIN (attributes);
27221 if (attributes)
27222 TREE_CHAIN (attributes) = NULL_TREE;
27224 DECL_CHAIN (decl) = decls;
27225 decls = decl;
27227 token = cp_lexer_peek_token (parser->lexer);
27228 if (token->type == CPP_COMMA)
27230 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27231 continue;
27233 else
27234 break;
27236 return decls;
27239 /* Parse an Objective-C @property declaration. The syntax is:
27241 objc-property-declaration:
27242 '@property' objc-property-attributes[opt] struct-declaration ;
27244 objc-property-attributes:
27245 '(' objc-property-attribute-list ')'
27247 objc-property-attribute-list:
27248 objc-property-attribute
27249 objc-property-attribute-list, objc-property-attribute
27251 objc-property-attribute
27252 'getter' = identifier
27253 'setter' = identifier
27254 'readonly'
27255 'readwrite'
27256 'assign'
27257 'retain'
27258 'copy'
27259 'nonatomic'
27261 For example:
27262 @property NSString *name;
27263 @property (readonly) id object;
27264 @property (retain, nonatomic, getter=getTheName) id name;
27265 @property int a, b, c;
27267 PS: This function is identical to
27268 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27269 static void
27270 cp_parser_objc_at_property_declaration (cp_parser *parser)
27272 /* The following variables hold the attributes of the properties as
27273 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27274 seen. When we see an attribute, we set them to 'true' (if they
27275 are boolean properties) or to the identifier (if they have an
27276 argument, ie, for getter and setter). Note that here we only
27277 parse the list of attributes, check the syntax and accumulate the
27278 attributes that we find. objc_add_property_declaration() will
27279 then process the information. */
27280 bool property_assign = false;
27281 bool property_copy = false;
27282 tree property_getter_ident = NULL_TREE;
27283 bool property_nonatomic = false;
27284 bool property_readonly = false;
27285 bool property_readwrite = false;
27286 bool property_retain = false;
27287 tree property_setter_ident = NULL_TREE;
27289 /* 'properties' is the list of properties that we read. Usually a
27290 single one, but maybe more (eg, in "@property int a, b, c;" there
27291 are three). */
27292 tree properties;
27293 location_t loc;
27295 loc = cp_lexer_peek_token (parser->lexer)->location;
27297 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27299 /* Parse the optional attribute list... */
27300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27302 /* Eat the '('. */
27303 cp_lexer_consume_token (parser->lexer);
27305 while (true)
27307 bool syntax_error = false;
27308 cp_token *token = cp_lexer_peek_token (parser->lexer);
27309 enum rid keyword;
27311 if (token->type != CPP_NAME)
27313 cp_parser_error (parser, "expected identifier");
27314 break;
27316 keyword = C_RID_CODE (token->u.value);
27317 cp_lexer_consume_token (parser->lexer);
27318 switch (keyword)
27320 case RID_ASSIGN: property_assign = true; break;
27321 case RID_COPY: property_copy = true; break;
27322 case RID_NONATOMIC: property_nonatomic = true; break;
27323 case RID_READONLY: property_readonly = true; break;
27324 case RID_READWRITE: property_readwrite = true; break;
27325 case RID_RETAIN: property_retain = true; break;
27327 case RID_GETTER:
27328 case RID_SETTER:
27329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27331 if (keyword == RID_GETTER)
27332 cp_parser_error (parser,
27333 "missing %<=%> (after %<getter%> attribute)");
27334 else
27335 cp_parser_error (parser,
27336 "missing %<=%> (after %<setter%> attribute)");
27337 syntax_error = true;
27338 break;
27340 cp_lexer_consume_token (parser->lexer); /* eat the = */
27341 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27343 cp_parser_error (parser, "expected identifier");
27344 syntax_error = true;
27345 break;
27347 if (keyword == RID_SETTER)
27349 if (property_setter_ident != NULL_TREE)
27351 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27352 cp_lexer_consume_token (parser->lexer);
27354 else
27355 property_setter_ident = cp_parser_objc_selector (parser);
27356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27357 cp_parser_error (parser, "setter name must terminate with %<:%>");
27358 else
27359 cp_lexer_consume_token (parser->lexer);
27361 else
27363 if (property_getter_ident != NULL_TREE)
27365 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27366 cp_lexer_consume_token (parser->lexer);
27368 else
27369 property_getter_ident = cp_parser_objc_selector (parser);
27371 break;
27372 default:
27373 cp_parser_error (parser, "unknown property attribute");
27374 syntax_error = true;
27375 break;
27378 if (syntax_error)
27379 break;
27381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27382 cp_lexer_consume_token (parser->lexer);
27383 else
27384 break;
27387 /* FIXME: "@property (setter, assign);" will generate a spurious
27388 "error: expected ‘)’ before ‘,’ token". This is because
27389 cp_parser_require, unlike the C counterpart, will produce an
27390 error even if we are in error recovery. */
27391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27393 cp_parser_skip_to_closing_parenthesis (parser,
27394 /*recovering=*/true,
27395 /*or_comma=*/false,
27396 /*consume_paren=*/true);
27400 /* ... and the property declaration(s). */
27401 properties = cp_parser_objc_struct_declaration (parser);
27403 if (properties == error_mark_node)
27405 cp_parser_skip_to_end_of_statement (parser);
27406 /* If the next token is now a `;', consume it. */
27407 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27408 cp_lexer_consume_token (parser->lexer);
27409 return;
27412 if (properties == NULL_TREE)
27413 cp_parser_error (parser, "expected identifier");
27414 else
27416 /* Comma-separated properties are chained together in
27417 reverse order; add them one by one. */
27418 properties = nreverse (properties);
27420 for (; properties; properties = TREE_CHAIN (properties))
27421 objc_add_property_declaration (loc, copy_node (properties),
27422 property_readonly, property_readwrite,
27423 property_assign, property_retain,
27424 property_copy, property_nonatomic,
27425 property_getter_ident, property_setter_ident);
27428 cp_parser_consume_semicolon_at_end_of_statement (parser);
27431 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27433 objc-synthesize-declaration:
27434 @synthesize objc-synthesize-identifier-list ;
27436 objc-synthesize-identifier-list:
27437 objc-synthesize-identifier
27438 objc-synthesize-identifier-list, objc-synthesize-identifier
27440 objc-synthesize-identifier
27441 identifier
27442 identifier = identifier
27444 For example:
27445 @synthesize MyProperty;
27446 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27448 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27449 for C. Keep them in sync.
27451 static void
27452 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27454 tree list = NULL_TREE;
27455 location_t loc;
27456 loc = cp_lexer_peek_token (parser->lexer)->location;
27458 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27459 while (true)
27461 tree property, ivar;
27462 property = cp_parser_identifier (parser);
27463 if (property == error_mark_node)
27465 cp_parser_consume_semicolon_at_end_of_statement (parser);
27466 return;
27468 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27470 cp_lexer_consume_token (parser->lexer);
27471 ivar = cp_parser_identifier (parser);
27472 if (ivar == error_mark_node)
27474 cp_parser_consume_semicolon_at_end_of_statement (parser);
27475 return;
27478 else
27479 ivar = NULL_TREE;
27480 list = chainon (list, build_tree_list (ivar, property));
27481 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27482 cp_lexer_consume_token (parser->lexer);
27483 else
27484 break;
27486 cp_parser_consume_semicolon_at_end_of_statement (parser);
27487 objc_add_synthesize_declaration (loc, list);
27490 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27492 objc-dynamic-declaration:
27493 @dynamic identifier-list ;
27495 For example:
27496 @dynamic MyProperty;
27497 @dynamic MyProperty, AnotherProperty;
27499 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27500 for C. Keep them in sync.
27502 static void
27503 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27505 tree list = NULL_TREE;
27506 location_t loc;
27507 loc = cp_lexer_peek_token (parser->lexer)->location;
27509 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27510 while (true)
27512 tree property;
27513 property = cp_parser_identifier (parser);
27514 if (property == error_mark_node)
27516 cp_parser_consume_semicolon_at_end_of_statement (parser);
27517 return;
27519 list = chainon (list, build_tree_list (NULL, property));
27520 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27521 cp_lexer_consume_token (parser->lexer);
27522 else
27523 break;
27525 cp_parser_consume_semicolon_at_end_of_statement (parser);
27526 objc_add_dynamic_declaration (loc, list);
27530 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27532 /* Returns name of the next clause.
27533 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27534 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27535 returned and the token is consumed. */
27537 static pragma_omp_clause
27538 cp_parser_omp_clause_name (cp_parser *parser)
27540 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27543 result = PRAGMA_OMP_CLAUSE_IF;
27544 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27545 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27546 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27547 result = PRAGMA_OACC_CLAUSE_DELETE;
27548 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27549 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27550 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27551 result = PRAGMA_OMP_CLAUSE_FOR;
27552 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27555 const char *p = IDENTIFIER_POINTER (id);
27557 switch (p[0])
27559 case 'a':
27560 if (!strcmp ("aligned", p))
27561 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27562 else if (!strcmp ("async", p))
27563 result = PRAGMA_OACC_CLAUSE_ASYNC;
27564 break;
27565 case 'c':
27566 if (!strcmp ("collapse", p))
27567 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27568 else if (!strcmp ("copy", p))
27569 result = PRAGMA_OACC_CLAUSE_COPY;
27570 else if (!strcmp ("copyin", p))
27571 result = PRAGMA_OMP_CLAUSE_COPYIN;
27572 else if (!strcmp ("copyout", p))
27573 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27574 else if (!strcmp ("copyprivate", p))
27575 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27576 else if (!strcmp ("create", p))
27577 result = PRAGMA_OACC_CLAUSE_CREATE;
27578 break;
27579 case 'd':
27580 if (!strcmp ("depend", p))
27581 result = PRAGMA_OMP_CLAUSE_DEPEND;
27582 else if (!strcmp ("device", p))
27583 result = PRAGMA_OMP_CLAUSE_DEVICE;
27584 else if (!strcmp ("deviceptr", p))
27585 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27586 else if (!strcmp ("dist_schedule", p))
27587 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27588 break;
27589 case 'f':
27590 if (!strcmp ("final", p))
27591 result = PRAGMA_OMP_CLAUSE_FINAL;
27592 else if (!strcmp ("firstprivate", p))
27593 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27594 else if (!strcmp ("from", p))
27595 result = PRAGMA_OMP_CLAUSE_FROM;
27596 break;
27597 case 'h':
27598 if (!strcmp ("host", p))
27599 result = PRAGMA_OACC_CLAUSE_HOST;
27600 break;
27601 case 'i':
27602 if (!strcmp ("inbranch", p))
27603 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27604 break;
27605 case 'l':
27606 if (!strcmp ("lastprivate", p))
27607 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27608 else if (!strcmp ("linear", p))
27609 result = PRAGMA_OMP_CLAUSE_LINEAR;
27610 break;
27611 case 'm':
27612 if (!strcmp ("map", p))
27613 result = PRAGMA_OMP_CLAUSE_MAP;
27614 else if (!strcmp ("mergeable", p))
27615 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27616 else if (flag_cilkplus && !strcmp ("mask", p))
27617 result = PRAGMA_CILK_CLAUSE_MASK;
27618 break;
27619 case 'n':
27620 if (!strcmp ("notinbranch", p))
27621 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27622 else if (!strcmp ("nowait", p))
27623 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27624 else if (flag_cilkplus && !strcmp ("nomask", p))
27625 result = PRAGMA_CILK_CLAUSE_NOMASK;
27626 else if (!strcmp ("num_gangs", p))
27627 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27628 else if (!strcmp ("num_teams", p))
27629 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27630 else if (!strcmp ("num_threads", p))
27631 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27632 else if (!strcmp ("num_workers", p))
27633 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27634 break;
27635 case 'o':
27636 if (!strcmp ("ordered", p))
27637 result = PRAGMA_OMP_CLAUSE_ORDERED;
27638 break;
27639 case 'p':
27640 if (!strcmp ("parallel", p))
27641 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27642 else if (!strcmp ("present", p))
27643 result = PRAGMA_OACC_CLAUSE_PRESENT;
27644 else if (!strcmp ("present_or_copy", p)
27645 || !strcmp ("pcopy", p))
27646 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27647 else if (!strcmp ("present_or_copyin", p)
27648 || !strcmp ("pcopyin", p))
27649 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27650 else if (!strcmp ("present_or_copyout", p)
27651 || !strcmp ("pcopyout", p))
27652 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27653 else if (!strcmp ("present_or_create", p)
27654 || !strcmp ("pcreate", p))
27655 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27656 else if (!strcmp ("private", p))
27657 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27658 else if (!strcmp ("proc_bind", p))
27659 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27660 break;
27661 case 'r':
27662 if (!strcmp ("reduction", p))
27663 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27664 break;
27665 case 's':
27666 if (!strcmp ("safelen", p))
27667 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27668 else if (!strcmp ("schedule", p))
27669 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27670 else if (!strcmp ("sections", p))
27671 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27672 else if (!strcmp ("self", p))
27673 result = PRAGMA_OACC_CLAUSE_SELF;
27674 else if (!strcmp ("shared", p))
27675 result = PRAGMA_OMP_CLAUSE_SHARED;
27676 else if (!strcmp ("simdlen", p))
27677 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27678 break;
27679 case 't':
27680 if (!strcmp ("taskgroup", p))
27681 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27682 else if (!strcmp ("thread_limit", p))
27683 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27684 else if (!strcmp ("to", p))
27685 result = PRAGMA_OMP_CLAUSE_TO;
27686 break;
27687 case 'u':
27688 if (!strcmp ("uniform", p))
27689 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27690 else if (!strcmp ("untied", p))
27691 result = PRAGMA_OMP_CLAUSE_UNTIED;
27692 break;
27693 case 'v':
27694 if (!strcmp ("vector_length", p))
27695 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27696 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27697 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27698 break;
27699 case 'w':
27700 if (!strcmp ("wait", p))
27701 result = PRAGMA_OACC_CLAUSE_WAIT;
27702 break;
27706 if (result != PRAGMA_OMP_CLAUSE_NONE)
27707 cp_lexer_consume_token (parser->lexer);
27709 return result;
27712 /* Validate that a clause of the given type does not already exist. */
27714 static void
27715 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27716 const char *name, location_t location)
27718 tree c;
27720 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27721 if (OMP_CLAUSE_CODE (c) == code)
27723 error_at (location, "too many %qs clauses", name);
27724 break;
27728 /* OpenMP 2.5:
27729 variable-list:
27730 identifier
27731 variable-list , identifier
27733 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27734 colon). An opening parenthesis will have been consumed by the caller.
27736 If KIND is nonzero, create the appropriate node and install the decl
27737 in OMP_CLAUSE_DECL and add the node to the head of the list.
27739 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27740 return the list created.
27742 COLON can be NULL if only closing parenthesis should end the list,
27743 or pointer to bool which will receive false if the list is terminated
27744 by closing parenthesis or true if the list is terminated by colon. */
27746 static tree
27747 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27748 tree list, bool *colon)
27750 cp_token *token;
27751 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27752 if (colon)
27754 parser->colon_corrects_to_scope_p = false;
27755 *colon = false;
27757 while (1)
27759 tree name, decl;
27761 token = cp_lexer_peek_token (parser->lexer);
27762 name = cp_parser_id_expression (parser, /*template_p=*/false,
27763 /*check_dependency_p=*/true,
27764 /*template_p=*/NULL,
27765 /*declarator_p=*/false,
27766 /*optional_p=*/false);
27767 if (name == error_mark_node)
27768 goto skip_comma;
27770 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27771 if (decl == error_mark_node)
27772 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27773 token->location);
27774 else if (kind != 0)
27776 switch (kind)
27778 case OMP_CLAUSE__CACHE_:
27779 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27781 error_at (token->location, "expected %<[%>");
27782 decl = error_mark_node;
27783 break;
27785 /* FALL THROUGH. */
27786 case OMP_CLAUSE_MAP:
27787 case OMP_CLAUSE_FROM:
27788 case OMP_CLAUSE_TO:
27789 case OMP_CLAUSE_DEPEND:
27790 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27792 tree low_bound = NULL_TREE, length = NULL_TREE;
27794 parser->colon_corrects_to_scope_p = false;
27795 cp_lexer_consume_token (parser->lexer);
27796 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27797 low_bound = cp_parser_expression (parser);
27798 if (!colon)
27799 parser->colon_corrects_to_scope_p
27800 = saved_colon_corrects_to_scope_p;
27801 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27802 length = integer_one_node;
27803 else
27805 /* Look for `:'. */
27806 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27807 goto skip_comma;
27808 if (!cp_lexer_next_token_is (parser->lexer,
27809 CPP_CLOSE_SQUARE))
27810 length = cp_parser_expression (parser);
27812 /* Look for the closing `]'. */
27813 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27814 RT_CLOSE_SQUARE))
27815 goto skip_comma;
27817 if (kind == OMP_CLAUSE__CACHE_)
27819 if (TREE_CODE (low_bound) != INTEGER_CST
27820 && !TREE_READONLY (low_bound))
27822 error_at (token->location,
27823 "%qD is not a constant", low_bound);
27824 decl = error_mark_node;
27827 if (TREE_CODE (length) != INTEGER_CST
27828 && !TREE_READONLY (length))
27830 error_at (token->location,
27831 "%qD is not a constant", length);
27832 decl = error_mark_node;
27836 decl = tree_cons (low_bound, length, decl);
27838 break;
27839 default:
27840 break;
27843 tree u = build_omp_clause (token->location, kind);
27844 OMP_CLAUSE_DECL (u) = decl;
27845 OMP_CLAUSE_CHAIN (u) = list;
27846 list = u;
27848 else
27849 list = tree_cons (decl, NULL_TREE, list);
27851 get_comma:
27852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27853 break;
27854 cp_lexer_consume_token (parser->lexer);
27857 if (colon)
27858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27860 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27862 *colon = true;
27863 cp_parser_require (parser, CPP_COLON, RT_COLON);
27864 return list;
27867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27869 int ending;
27871 /* Try to resync to an unnested comma. Copied from
27872 cp_parser_parenthesized_expression_list. */
27873 skip_comma:
27874 if (colon)
27875 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27876 ending = cp_parser_skip_to_closing_parenthesis (parser,
27877 /*recovering=*/true,
27878 /*or_comma=*/true,
27879 /*consume_paren=*/true);
27880 if (ending < 0)
27881 goto get_comma;
27884 return list;
27887 /* Similarly, but expect leading and trailing parenthesis. This is a very
27888 common case for omp clauses. */
27890 static tree
27891 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27893 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27894 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27895 return list;
27898 /* OpenACC 2.0:
27899 copy ( variable-list )
27900 copyin ( variable-list )
27901 copyout ( variable-list )
27902 create ( variable-list )
27903 delete ( variable-list )
27904 present ( variable-list )
27905 present_or_copy ( variable-list )
27906 pcopy ( variable-list )
27907 present_or_copyin ( variable-list )
27908 pcopyin ( variable-list )
27909 present_or_copyout ( variable-list )
27910 pcopyout ( variable-list )
27911 present_or_create ( variable-list )
27912 pcreate ( variable-list ) */
27914 static tree
27915 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27916 tree list)
27918 enum gomp_map_kind kind;
27919 switch (c_kind)
27921 case PRAGMA_OACC_CLAUSE_COPY:
27922 kind = GOMP_MAP_FORCE_TOFROM;
27923 break;
27924 case PRAGMA_OACC_CLAUSE_COPYIN:
27925 kind = GOMP_MAP_FORCE_TO;
27926 break;
27927 case PRAGMA_OACC_CLAUSE_COPYOUT:
27928 kind = GOMP_MAP_FORCE_FROM;
27929 break;
27930 case PRAGMA_OACC_CLAUSE_CREATE:
27931 kind = GOMP_MAP_FORCE_ALLOC;
27932 break;
27933 case PRAGMA_OACC_CLAUSE_DELETE:
27934 kind = GOMP_MAP_FORCE_DEALLOC;
27935 break;
27936 case PRAGMA_OACC_CLAUSE_DEVICE:
27937 kind = GOMP_MAP_FORCE_TO;
27938 break;
27939 case PRAGMA_OACC_CLAUSE_HOST:
27940 case PRAGMA_OACC_CLAUSE_SELF:
27941 kind = GOMP_MAP_FORCE_FROM;
27942 break;
27943 case PRAGMA_OACC_CLAUSE_PRESENT:
27944 kind = GOMP_MAP_FORCE_PRESENT;
27945 break;
27946 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27947 kind = GOMP_MAP_TOFROM;
27948 break;
27949 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27950 kind = GOMP_MAP_TO;
27951 break;
27952 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27953 kind = GOMP_MAP_FROM;
27954 break;
27955 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27956 kind = GOMP_MAP_ALLOC;
27957 break;
27958 default:
27959 gcc_unreachable ();
27961 tree nl, c;
27962 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27964 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27965 OMP_CLAUSE_MAP_KIND (c) = kind;
27967 return nl;
27970 /* OpenACC 2.0:
27971 deviceptr ( variable-list ) */
27973 static tree
27974 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27977 tree vars, t;
27979 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27980 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27981 variable-list must only allow for pointer variables. */
27982 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27983 for (t = vars; t; t = TREE_CHAIN (t))
27985 tree v = TREE_PURPOSE (t);
27987 /* FIXME diagnostics: Ideally we should keep individual
27988 locations for all the variables in the var list to make the
27989 following errors more precise. Perhaps
27990 c_parser_omp_var_list_parens should construct a list of
27991 locations to go along with the var list. */
27993 if (TREE_CODE (v) != VAR_DECL)
27994 error_at (loc, "%qD is not a variable", v);
27995 else if (TREE_TYPE (v) == error_mark_node)
27997 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27998 error_at (loc, "%qD is not a pointer variable", v);
28000 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28001 OMP_CLAUSE_MAP_KIND (u) = GOMP_MAP_FORCE_DEVICEPTR;
28002 OMP_CLAUSE_DECL (u) = v;
28003 OMP_CLAUSE_CHAIN (u) = list;
28004 list = u;
28007 return list;
28010 /* OpenACC:
28011 vector_length ( expression ) */
28013 static tree
28014 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28016 tree t, c;
28017 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28018 bool error = false;
28020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28021 return list;
28023 t = cp_parser_condition (parser);
28024 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28026 error_at (location, "expected positive integer expression");
28027 error = true;
28030 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28033 /*or_comma=*/false,
28034 /*consume_paren=*/true);
28035 return list;
28038 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28039 location);
28041 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28042 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28043 OMP_CLAUSE_CHAIN (c) = list;
28044 list = c;
28046 return list;
28049 /* OpenACC 2.0
28050 Parse wait clause or directive parameters. */
28052 static tree
28053 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28055 vec<tree, va_gc> *args;
28056 tree t, args_tree;
28058 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28059 /*cast_p=*/false,
28060 /*allow_expansion_p=*/true,
28061 /*non_constant_p=*/NULL);
28063 if (args == NULL || args->length () == 0)
28065 cp_parser_error (parser, "expected integer expression before ')'");
28066 if (args != NULL)
28067 release_tree_vector (args);
28068 return list;
28071 args_tree = build_tree_list_vec (args);
28073 release_tree_vector (args);
28075 for (t = args_tree; t; t = TREE_CHAIN (t))
28077 tree targ = TREE_VALUE (t);
28079 if (targ != error_mark_node)
28081 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28082 error ("%<wait%> expression must be integral");
28083 else
28085 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28087 mark_rvalue_use (targ);
28088 OMP_CLAUSE_DECL (c) = targ;
28089 OMP_CLAUSE_CHAIN (c) = list;
28090 list = c;
28095 return list;
28098 /* OpenACC:
28099 wait ( int-expr-list ) */
28101 static tree
28102 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28104 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28106 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28107 return list;
28109 list = cp_parser_oacc_wait_list (parser, location, list);
28111 return list;
28114 /* OpenMP 3.0:
28115 collapse ( constant-expression ) */
28117 static tree
28118 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28120 tree c, num;
28121 location_t loc;
28122 HOST_WIDE_INT n;
28124 loc = cp_lexer_peek_token (parser->lexer)->location;
28125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28126 return list;
28128 num = cp_parser_constant_expression (parser);
28130 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28132 /*or_comma=*/false,
28133 /*consume_paren=*/true);
28135 if (num == error_mark_node)
28136 return list;
28137 num = fold_non_dependent_expr (num);
28138 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28139 || !tree_fits_shwi_p (num)
28140 || (n = tree_to_shwi (num)) <= 0
28141 || (int) n != n)
28143 error_at (loc, "collapse argument needs positive constant integer expression");
28144 return list;
28147 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28148 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28149 OMP_CLAUSE_CHAIN (c) = list;
28150 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28152 return c;
28155 /* OpenMP 2.5:
28156 default ( shared | none ) */
28158 static tree
28159 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28161 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28162 tree c;
28164 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28165 return list;
28166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28169 const char *p = IDENTIFIER_POINTER (id);
28171 switch (p[0])
28173 case 'n':
28174 if (strcmp ("none", p) != 0)
28175 goto invalid_kind;
28176 kind = OMP_CLAUSE_DEFAULT_NONE;
28177 break;
28179 case 's':
28180 if (strcmp ("shared", p) != 0)
28181 goto invalid_kind;
28182 kind = OMP_CLAUSE_DEFAULT_SHARED;
28183 break;
28185 default:
28186 goto invalid_kind;
28189 cp_lexer_consume_token (parser->lexer);
28191 else
28193 invalid_kind:
28194 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28197 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28198 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28199 /*or_comma=*/false,
28200 /*consume_paren=*/true);
28202 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28203 return list;
28205 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28206 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28207 OMP_CLAUSE_CHAIN (c) = list;
28208 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28210 return c;
28213 /* OpenMP 3.1:
28214 final ( expression ) */
28216 static tree
28217 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28219 tree t, c;
28221 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28222 return list;
28224 t = cp_parser_condition (parser);
28226 if (t == error_mark_node
28227 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28228 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28229 /*or_comma=*/false,
28230 /*consume_paren=*/true);
28232 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28234 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28235 OMP_CLAUSE_FINAL_EXPR (c) = t;
28236 OMP_CLAUSE_CHAIN (c) = list;
28238 return c;
28241 /* OpenMP 2.5:
28242 if ( expression ) */
28244 static tree
28245 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28247 tree t, c;
28249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28250 return list;
28252 t = cp_parser_condition (parser);
28254 if (t == error_mark_node
28255 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28256 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28257 /*or_comma=*/false,
28258 /*consume_paren=*/true);
28260 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28262 c = build_omp_clause (location, OMP_CLAUSE_IF);
28263 OMP_CLAUSE_IF_EXPR (c) = t;
28264 OMP_CLAUSE_CHAIN (c) = list;
28266 return c;
28269 /* OpenMP 3.1:
28270 mergeable */
28272 static tree
28273 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28274 tree list, location_t location)
28276 tree c;
28278 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28279 location);
28281 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28282 OMP_CLAUSE_CHAIN (c) = list;
28283 return c;
28286 /* OpenMP 2.5:
28287 nowait */
28289 static tree
28290 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28291 tree list, location_t location)
28293 tree c;
28295 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28297 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28298 OMP_CLAUSE_CHAIN (c) = list;
28299 return c;
28302 /* OpenACC:
28303 num_gangs ( expression ) */
28305 static tree
28306 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28308 tree t, c;
28309 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28311 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28312 return list;
28314 t = cp_parser_condition (parser);
28316 if (t == error_mark_node
28317 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28318 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28319 /*or_comma=*/false,
28320 /*consume_paren=*/true);
28322 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28324 error_at (location, "expected positive integer expression");
28325 return list;
28328 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28330 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28331 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28332 OMP_CLAUSE_CHAIN (c) = list;
28333 list = c;
28335 return list;
28338 /* OpenMP 2.5:
28339 num_threads ( expression ) */
28341 static tree
28342 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28343 location_t location)
28345 tree t, c;
28347 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28348 return list;
28350 t = cp_parser_expression (parser);
28352 if (t == error_mark_node
28353 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28355 /*or_comma=*/false,
28356 /*consume_paren=*/true);
28358 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28359 "num_threads", location);
28361 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28362 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28363 OMP_CLAUSE_CHAIN (c) = list;
28365 return c;
28368 /* OpenACC:
28369 num_workers ( expression ) */
28371 static tree
28372 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28374 tree t, c;
28375 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28378 return list;
28380 t = cp_parser_condition (parser);
28382 if (t == error_mark_node
28383 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28385 /*or_comma=*/false,
28386 /*consume_paren=*/true);
28388 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28390 error_at (location, "expected positive integer expression");
28391 return list;
28394 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28395 location);
28397 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28398 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28399 OMP_CLAUSE_CHAIN (c) = list;
28400 list = c;
28402 return list;
28405 /* OpenMP 2.5:
28406 ordered */
28408 static tree
28409 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28410 tree list, location_t location)
28412 tree c;
28414 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28415 "ordered", location);
28417 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28418 OMP_CLAUSE_CHAIN (c) = list;
28419 return c;
28422 /* OpenMP 2.5:
28423 reduction ( reduction-operator : variable-list )
28425 reduction-operator:
28426 One of: + * - & ^ | && ||
28428 OpenMP 3.1:
28430 reduction-operator:
28431 One of: + * - & ^ | && || min max
28433 OpenMP 4.0:
28435 reduction-operator:
28436 One of: + * - & ^ | && ||
28437 id-expression */
28439 static tree
28440 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28442 enum tree_code code = ERROR_MARK;
28443 tree nlist, c, id = NULL_TREE;
28445 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28446 return list;
28448 switch (cp_lexer_peek_token (parser->lexer)->type)
28450 case CPP_PLUS: code = PLUS_EXPR; break;
28451 case CPP_MULT: code = MULT_EXPR; break;
28452 case CPP_MINUS: code = MINUS_EXPR; break;
28453 case CPP_AND: code = BIT_AND_EXPR; break;
28454 case CPP_XOR: code = BIT_XOR_EXPR; break;
28455 case CPP_OR: code = BIT_IOR_EXPR; break;
28456 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28457 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28458 default: break;
28461 if (code != ERROR_MARK)
28462 cp_lexer_consume_token (parser->lexer);
28463 else
28465 bool saved_colon_corrects_to_scope_p;
28466 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28467 parser->colon_corrects_to_scope_p = false;
28468 id = cp_parser_id_expression (parser, /*template_p=*/false,
28469 /*check_dependency_p=*/true,
28470 /*template_p=*/NULL,
28471 /*declarator_p=*/false,
28472 /*optional_p=*/false);
28473 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28474 if (identifier_p (id))
28476 const char *p = IDENTIFIER_POINTER (id);
28478 if (strcmp (p, "min") == 0)
28479 code = MIN_EXPR;
28480 else if (strcmp (p, "max") == 0)
28481 code = MAX_EXPR;
28482 else if (id == ansi_opname (PLUS_EXPR))
28483 code = PLUS_EXPR;
28484 else if (id == ansi_opname (MULT_EXPR))
28485 code = MULT_EXPR;
28486 else if (id == ansi_opname (MINUS_EXPR))
28487 code = MINUS_EXPR;
28488 else if (id == ansi_opname (BIT_AND_EXPR))
28489 code = BIT_AND_EXPR;
28490 else if (id == ansi_opname (BIT_IOR_EXPR))
28491 code = BIT_IOR_EXPR;
28492 else if (id == ansi_opname (BIT_XOR_EXPR))
28493 code = BIT_XOR_EXPR;
28494 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28495 code = TRUTH_ANDIF_EXPR;
28496 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28497 code = TRUTH_ORIF_EXPR;
28498 id = omp_reduction_id (code, id, NULL_TREE);
28499 tree scope = parser->scope;
28500 if (scope)
28501 id = build_qualified_name (NULL_TREE, scope, id, false);
28502 parser->scope = NULL_TREE;
28503 parser->qualifying_scope = NULL_TREE;
28504 parser->object_scope = NULL_TREE;
28506 else
28508 error ("invalid reduction-identifier");
28509 resync_fail:
28510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28511 /*or_comma=*/false,
28512 /*consume_paren=*/true);
28513 return list;
28517 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28518 goto resync_fail;
28520 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28521 NULL);
28522 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28524 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28525 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28528 return nlist;
28531 /* OpenMP 2.5:
28532 schedule ( schedule-kind )
28533 schedule ( schedule-kind , expression )
28535 schedule-kind:
28536 static | dynamic | guided | runtime | auto */
28538 static tree
28539 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28541 tree c, t;
28543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28544 return list;
28546 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28551 const char *p = IDENTIFIER_POINTER (id);
28553 switch (p[0])
28555 case 'd':
28556 if (strcmp ("dynamic", p) != 0)
28557 goto invalid_kind;
28558 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28559 break;
28561 case 'g':
28562 if (strcmp ("guided", p) != 0)
28563 goto invalid_kind;
28564 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28565 break;
28567 case 'r':
28568 if (strcmp ("runtime", p) != 0)
28569 goto invalid_kind;
28570 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28571 break;
28573 default:
28574 goto invalid_kind;
28577 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28579 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28580 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28581 else
28582 goto invalid_kind;
28583 cp_lexer_consume_token (parser->lexer);
28585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28587 cp_token *token;
28588 cp_lexer_consume_token (parser->lexer);
28590 token = cp_lexer_peek_token (parser->lexer);
28591 t = cp_parser_assignment_expression (parser);
28593 if (t == error_mark_node)
28594 goto resync_fail;
28595 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28596 error_at (token->location, "schedule %<runtime%> does not take "
28597 "a %<chunk_size%> parameter");
28598 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28599 error_at (token->location, "schedule %<auto%> does not take "
28600 "a %<chunk_size%> parameter");
28601 else
28602 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28604 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28605 goto resync_fail;
28607 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28608 goto resync_fail;
28610 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28611 OMP_CLAUSE_CHAIN (c) = list;
28612 return c;
28614 invalid_kind:
28615 cp_parser_error (parser, "invalid schedule kind");
28616 resync_fail:
28617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28618 /*or_comma=*/false,
28619 /*consume_paren=*/true);
28620 return list;
28623 /* OpenMP 3.0:
28624 untied */
28626 static tree
28627 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28628 tree list, location_t location)
28630 tree c;
28632 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28634 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28635 OMP_CLAUSE_CHAIN (c) = list;
28636 return c;
28639 /* OpenMP 4.0:
28640 inbranch
28641 notinbranch */
28643 static tree
28644 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28645 tree list, location_t location)
28647 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28648 tree c = build_omp_clause (location, code);
28649 OMP_CLAUSE_CHAIN (c) = list;
28650 return c;
28653 /* OpenMP 4.0:
28654 parallel
28656 sections
28657 taskgroup */
28659 static tree
28660 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28661 enum omp_clause_code code,
28662 tree list, location_t location)
28664 tree c = build_omp_clause (location, code);
28665 OMP_CLAUSE_CHAIN (c) = list;
28666 return c;
28669 /* OpenMP 4.0:
28670 num_teams ( expression ) */
28672 static tree
28673 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28674 location_t location)
28676 tree t, c;
28678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28679 return list;
28681 t = cp_parser_expression (parser);
28683 if (t == error_mark_node
28684 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28686 /*or_comma=*/false,
28687 /*consume_paren=*/true);
28689 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28690 "num_teams", location);
28692 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28693 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28694 OMP_CLAUSE_CHAIN (c) = list;
28696 return c;
28699 /* OpenMP 4.0:
28700 thread_limit ( expression ) */
28702 static tree
28703 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28704 location_t location)
28706 tree t, c;
28708 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28709 return list;
28711 t = cp_parser_expression (parser);
28713 if (t == error_mark_node
28714 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28715 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28716 /*or_comma=*/false,
28717 /*consume_paren=*/true);
28719 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28720 "thread_limit", location);
28722 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28723 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28724 OMP_CLAUSE_CHAIN (c) = list;
28726 return c;
28729 /* OpenMP 4.0:
28730 aligned ( variable-list )
28731 aligned ( variable-list : constant-expression ) */
28733 static tree
28734 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28736 tree nlist, c, alignment = NULL_TREE;
28737 bool colon;
28739 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28740 return list;
28742 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28743 &colon);
28745 if (colon)
28747 alignment = cp_parser_constant_expression (parser);
28749 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28751 /*or_comma=*/false,
28752 /*consume_paren=*/true);
28754 if (alignment == error_mark_node)
28755 alignment = NULL_TREE;
28758 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28759 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28761 return nlist;
28764 /* OpenMP 4.0:
28765 linear ( variable-list )
28766 linear ( variable-list : expression ) */
28768 static tree
28769 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28770 bool is_cilk_simd_fn)
28772 tree nlist, c, step = integer_one_node;
28773 bool colon;
28775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28776 return list;
28778 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28779 &colon);
28781 if (colon)
28783 step = cp_parser_expression (parser);
28785 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28787 sorry ("using parameters for %<linear%> step is not supported yet");
28788 step = integer_one_node;
28790 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28792 /*or_comma=*/false,
28793 /*consume_paren=*/true);
28795 if (step == error_mark_node)
28796 return list;
28799 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28800 OMP_CLAUSE_LINEAR_STEP (c) = step;
28802 return nlist;
28805 /* OpenMP 4.0:
28806 safelen ( constant-expression ) */
28808 static tree
28809 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28810 location_t location)
28812 tree t, c;
28814 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28815 return list;
28817 t = cp_parser_constant_expression (parser);
28819 if (t == error_mark_node
28820 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28821 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28822 /*or_comma=*/false,
28823 /*consume_paren=*/true);
28825 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28827 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28828 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28829 OMP_CLAUSE_CHAIN (c) = list;
28831 return c;
28834 /* OpenMP 4.0:
28835 simdlen ( constant-expression ) */
28837 static tree
28838 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28839 location_t location)
28841 tree t, c;
28843 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28844 return list;
28846 t = cp_parser_constant_expression (parser);
28848 if (t == error_mark_node
28849 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28850 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28851 /*or_comma=*/false,
28852 /*consume_paren=*/true);
28854 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28856 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28857 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28858 OMP_CLAUSE_CHAIN (c) = list;
28860 return c;
28863 /* OpenMP 4.0:
28864 depend ( depend-kind : variable-list )
28866 depend-kind:
28867 in | out | inout */
28869 static tree
28870 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28872 tree nlist, c;
28873 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28875 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28876 return list;
28878 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28880 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28881 const char *p = IDENTIFIER_POINTER (id);
28883 if (strcmp ("in", p) == 0)
28884 kind = OMP_CLAUSE_DEPEND_IN;
28885 else if (strcmp ("inout", p) == 0)
28886 kind = OMP_CLAUSE_DEPEND_INOUT;
28887 else if (strcmp ("out", p) == 0)
28888 kind = OMP_CLAUSE_DEPEND_OUT;
28889 else
28890 goto invalid_kind;
28892 else
28893 goto invalid_kind;
28895 cp_lexer_consume_token (parser->lexer);
28896 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28897 goto resync_fail;
28899 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28900 NULL);
28902 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28903 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28905 return nlist;
28907 invalid_kind:
28908 cp_parser_error (parser, "invalid depend kind");
28909 resync_fail:
28910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28911 /*or_comma=*/false,
28912 /*consume_paren=*/true);
28913 return list;
28916 /* OpenMP 4.0:
28917 map ( map-kind : variable-list )
28918 map ( variable-list )
28920 map-kind:
28921 alloc | to | from | tofrom */
28923 static tree
28924 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28926 tree nlist, c;
28927 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28929 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28930 return list;
28932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28933 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28935 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28936 const char *p = IDENTIFIER_POINTER (id);
28938 if (strcmp ("alloc", p) == 0)
28939 kind = GOMP_MAP_ALLOC;
28940 else if (strcmp ("to", p) == 0)
28941 kind = GOMP_MAP_TO;
28942 else if (strcmp ("from", p) == 0)
28943 kind = GOMP_MAP_FROM;
28944 else if (strcmp ("tofrom", p) == 0)
28945 kind = GOMP_MAP_TOFROM;
28946 else
28948 cp_parser_error (parser, "invalid map kind");
28949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28950 /*or_comma=*/false,
28951 /*consume_paren=*/true);
28952 return list;
28954 cp_lexer_consume_token (parser->lexer);
28955 cp_lexer_consume_token (parser->lexer);
28958 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28959 NULL);
28961 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28962 OMP_CLAUSE_MAP_KIND (c) = kind;
28964 return nlist;
28967 /* OpenMP 4.0:
28968 device ( expression ) */
28970 static tree
28971 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28972 location_t location)
28974 tree t, c;
28976 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28977 return list;
28979 t = cp_parser_expression (parser);
28981 if (t == error_mark_node
28982 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28983 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28984 /*or_comma=*/false,
28985 /*consume_paren=*/true);
28987 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28988 "device", location);
28990 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28991 OMP_CLAUSE_DEVICE_ID (c) = t;
28992 OMP_CLAUSE_CHAIN (c) = list;
28994 return c;
28997 /* OpenMP 4.0:
28998 dist_schedule ( static )
28999 dist_schedule ( static , expression ) */
29001 static tree
29002 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29003 location_t location)
29005 tree c, t;
29007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29008 return list;
29010 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29012 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29013 goto invalid_kind;
29014 cp_lexer_consume_token (parser->lexer);
29016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29018 cp_lexer_consume_token (parser->lexer);
29020 t = cp_parser_assignment_expression (parser);
29022 if (t == error_mark_node)
29023 goto resync_fail;
29024 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29027 goto resync_fail;
29029 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29030 goto resync_fail;
29032 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29033 location);
29034 OMP_CLAUSE_CHAIN (c) = list;
29035 return c;
29037 invalid_kind:
29038 cp_parser_error (parser, "invalid dist_schedule kind");
29039 resync_fail:
29040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29041 /*or_comma=*/false,
29042 /*consume_paren=*/true);
29043 return list;
29046 /* OpenMP 4.0:
29047 proc_bind ( proc-bind-kind )
29049 proc-bind-kind:
29050 master | close | spread */
29052 static tree
29053 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29054 location_t location)
29056 tree c;
29057 enum omp_clause_proc_bind_kind kind;
29059 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29060 return list;
29062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29065 const char *p = IDENTIFIER_POINTER (id);
29067 if (strcmp ("master", p) == 0)
29068 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29069 else if (strcmp ("close", p) == 0)
29070 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29071 else if (strcmp ("spread", p) == 0)
29072 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29073 else
29074 goto invalid_kind;
29076 else
29077 goto invalid_kind;
29079 cp_lexer_consume_token (parser->lexer);
29080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29081 goto resync_fail;
29083 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29084 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29085 location);
29086 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29087 OMP_CLAUSE_CHAIN (c) = list;
29088 return c;
29090 invalid_kind:
29091 cp_parser_error (parser, "invalid depend kind");
29092 resync_fail:
29093 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29094 /*or_comma=*/false,
29095 /*consume_paren=*/true);
29096 return list;
29099 /* OpenACC:
29100 async [( int-expr )] */
29102 static tree
29103 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29105 tree c, t;
29106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29108 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29110 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29112 cp_lexer_consume_token (parser->lexer);
29114 t = cp_parser_expression (parser);
29115 if (t == error_mark_node
29116 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29117 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29118 /*or_comma=*/false,
29119 /*consume_paren=*/true);
29122 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29124 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29125 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29126 OMP_CLAUSE_CHAIN (c) = list;
29127 list = c;
29129 return list;
29132 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29133 is a bitmask in MASK. Return the list of clauses found. */
29135 static tree
29136 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29137 const char *where, cp_token *pragma_tok,
29138 bool finish_p = true)
29140 tree clauses = NULL;
29141 bool first = true;
29143 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29145 location_t here;
29146 pragma_omp_clause c_kind;
29147 const char *c_name;
29148 tree prev = clauses;
29150 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29151 cp_lexer_consume_token (parser->lexer);
29153 here = cp_lexer_peek_token (parser->lexer)->location;
29154 c_kind = cp_parser_omp_clause_name (parser);
29156 switch (c_kind)
29158 case PRAGMA_OACC_CLAUSE_ASYNC:
29159 clauses = cp_parser_oacc_clause_async (parser, clauses);
29160 c_name = "async";
29161 break;
29162 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29163 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29164 c_name = "collapse";
29165 break;
29166 case PRAGMA_OACC_CLAUSE_COPY:
29167 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29168 c_name = "copy";
29169 break;
29170 case PRAGMA_OACC_CLAUSE_COPYIN:
29171 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29172 c_name = "copyin";
29173 break;
29174 case PRAGMA_OACC_CLAUSE_COPYOUT:
29175 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29176 c_name = "copyout";
29177 break;
29178 case PRAGMA_OACC_CLAUSE_CREATE:
29179 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29180 c_name = "create";
29181 break;
29182 case PRAGMA_OACC_CLAUSE_DELETE:
29183 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29184 c_name = "delete";
29185 break;
29186 case PRAGMA_OACC_CLAUSE_DEVICE:
29187 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29188 c_name = "device";
29189 break;
29190 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29191 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29192 c_name = "deviceptr";
29193 break;
29194 case PRAGMA_OACC_CLAUSE_HOST:
29195 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29196 c_name = "host";
29197 break;
29198 case PRAGMA_OACC_CLAUSE_IF:
29199 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29200 c_name = "if";
29201 break;
29202 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29203 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29204 c_name = "num_gangs";
29205 break;
29206 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29207 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29208 c_name = "num_workers";
29209 break;
29210 case PRAGMA_OACC_CLAUSE_PRESENT:
29211 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29212 c_name = "present";
29213 break;
29214 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29215 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29216 c_name = "present_or_copy";
29217 break;
29218 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29219 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29220 c_name = "present_or_copyin";
29221 break;
29222 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29223 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29224 c_name = "present_or_copyout";
29225 break;
29226 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29227 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29228 c_name = "present_or_create";
29229 break;
29230 case PRAGMA_OACC_CLAUSE_REDUCTION:
29231 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29232 c_name = "reduction";
29233 break;
29234 case PRAGMA_OACC_CLAUSE_SELF:
29235 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29236 c_name = "self";
29237 break;
29238 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29239 clauses =
29240 cp_parser_oacc_clause_vector_length (parser, clauses);
29241 c_name = "vector_length";
29242 break;
29243 case PRAGMA_OACC_CLAUSE_WAIT:
29244 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29245 c_name = "wait";
29246 break;
29247 default:
29248 cp_parser_error (parser, "expected clause");
29249 goto saw_error;
29252 first = false;
29254 if (((mask >> c_kind) & 1) == 0)
29256 /* Remove the invalid clause(s) from the list to avoid
29257 confusing the rest of the compiler. */
29258 clauses = prev;
29259 error_at (here, "%qs is not valid for %qs", c_name, where);
29263 saw_error:
29264 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29266 if (finish_p)
29267 return finish_omp_clauses (clauses);
29269 return clauses;
29272 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29273 is a bitmask in MASK. Return the list of clauses found; the result
29274 of clause default goes in *pdefault. */
29276 static tree
29277 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29278 const char *where, cp_token *pragma_tok,
29279 bool finish_p = true)
29281 tree clauses = NULL;
29282 bool first = true;
29283 cp_token *token = NULL;
29284 bool cilk_simd_fn = false;
29286 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29288 pragma_omp_clause c_kind;
29289 const char *c_name;
29290 tree prev = clauses;
29292 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29293 cp_lexer_consume_token (parser->lexer);
29295 token = cp_lexer_peek_token (parser->lexer);
29296 c_kind = cp_parser_omp_clause_name (parser);
29298 switch (c_kind)
29300 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29301 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29302 token->location);
29303 c_name = "collapse";
29304 break;
29305 case PRAGMA_OMP_CLAUSE_COPYIN:
29306 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29307 c_name = "copyin";
29308 break;
29309 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29310 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29311 clauses);
29312 c_name = "copyprivate";
29313 break;
29314 case PRAGMA_OMP_CLAUSE_DEFAULT:
29315 clauses = cp_parser_omp_clause_default (parser, clauses,
29316 token->location);
29317 c_name = "default";
29318 break;
29319 case PRAGMA_OMP_CLAUSE_FINAL:
29320 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29321 c_name = "final";
29322 break;
29323 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29325 clauses);
29326 c_name = "firstprivate";
29327 break;
29328 case PRAGMA_OMP_CLAUSE_IF:
29329 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29330 c_name = "if";
29331 break;
29332 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29333 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29334 clauses);
29335 c_name = "lastprivate";
29336 break;
29337 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29338 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29339 token->location);
29340 c_name = "mergeable";
29341 break;
29342 case PRAGMA_OMP_CLAUSE_NOWAIT:
29343 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29344 c_name = "nowait";
29345 break;
29346 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29347 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29348 token->location);
29349 c_name = "num_threads";
29350 break;
29351 case PRAGMA_OMP_CLAUSE_ORDERED:
29352 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29353 token->location);
29354 c_name = "ordered";
29355 break;
29356 case PRAGMA_OMP_CLAUSE_PRIVATE:
29357 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29358 clauses);
29359 c_name = "private";
29360 break;
29361 case PRAGMA_OMP_CLAUSE_REDUCTION:
29362 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29363 c_name = "reduction";
29364 break;
29365 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29366 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29367 token->location);
29368 c_name = "schedule";
29369 break;
29370 case PRAGMA_OMP_CLAUSE_SHARED:
29371 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29372 clauses);
29373 c_name = "shared";
29374 break;
29375 case PRAGMA_OMP_CLAUSE_UNTIED:
29376 clauses = cp_parser_omp_clause_untied (parser, clauses,
29377 token->location);
29378 c_name = "untied";
29379 break;
29380 case PRAGMA_OMP_CLAUSE_INBRANCH:
29381 case PRAGMA_CILK_CLAUSE_MASK:
29382 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29383 clauses, token->location);
29384 c_name = "inbranch";
29385 break;
29386 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29387 case PRAGMA_CILK_CLAUSE_NOMASK:
29388 clauses = cp_parser_omp_clause_branch (parser,
29389 OMP_CLAUSE_NOTINBRANCH,
29390 clauses, token->location);
29391 c_name = "notinbranch";
29392 break;
29393 case PRAGMA_OMP_CLAUSE_PARALLEL:
29394 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29395 clauses, token->location);
29396 c_name = "parallel";
29397 if (!first)
29399 clause_not_first:
29400 error_at (token->location, "%qs must be the first clause of %qs",
29401 c_name, where);
29402 clauses = prev;
29404 break;
29405 case PRAGMA_OMP_CLAUSE_FOR:
29406 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29407 clauses, token->location);
29408 c_name = "for";
29409 if (!first)
29410 goto clause_not_first;
29411 break;
29412 case PRAGMA_OMP_CLAUSE_SECTIONS:
29413 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29414 clauses, token->location);
29415 c_name = "sections";
29416 if (!first)
29417 goto clause_not_first;
29418 break;
29419 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29420 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29421 clauses, token->location);
29422 c_name = "taskgroup";
29423 if (!first)
29424 goto clause_not_first;
29425 break;
29426 case PRAGMA_OMP_CLAUSE_TO:
29427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29428 clauses);
29429 c_name = "to";
29430 break;
29431 case PRAGMA_OMP_CLAUSE_FROM:
29432 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29433 clauses);
29434 c_name = "from";
29435 break;
29436 case PRAGMA_OMP_CLAUSE_UNIFORM:
29437 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29438 clauses);
29439 c_name = "uniform";
29440 break;
29441 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29442 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29443 token->location);
29444 c_name = "num_teams";
29445 break;
29446 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29447 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29448 token->location);
29449 c_name = "thread_limit";
29450 break;
29451 case PRAGMA_OMP_CLAUSE_ALIGNED:
29452 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29453 c_name = "aligned";
29454 break;
29455 case PRAGMA_OMP_CLAUSE_LINEAR:
29456 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29457 cilk_simd_fn = true;
29458 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29459 c_name = "linear";
29460 break;
29461 case PRAGMA_OMP_CLAUSE_DEPEND:
29462 clauses = cp_parser_omp_clause_depend (parser, clauses);
29463 c_name = "depend";
29464 break;
29465 case PRAGMA_OMP_CLAUSE_MAP:
29466 clauses = cp_parser_omp_clause_map (parser, clauses);
29467 c_name = "map";
29468 break;
29469 case PRAGMA_OMP_CLAUSE_DEVICE:
29470 clauses = cp_parser_omp_clause_device (parser, clauses,
29471 token->location);
29472 c_name = "device";
29473 break;
29474 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29475 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29476 token->location);
29477 c_name = "dist_schedule";
29478 break;
29479 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29480 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29481 token->location);
29482 c_name = "proc_bind";
29483 break;
29484 case PRAGMA_OMP_CLAUSE_SAFELEN:
29485 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29486 token->location);
29487 c_name = "safelen";
29488 break;
29489 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29490 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29491 token->location);
29492 c_name = "simdlen";
29493 break;
29494 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29495 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29496 c_name = "simdlen";
29497 break;
29498 default:
29499 cp_parser_error (parser, "expected clause");
29500 goto saw_error;
29503 first = false;
29505 if (((mask >> c_kind) & 1) == 0)
29507 /* Remove the invalid clause(s) from the list to avoid
29508 confusing the rest of the compiler. */
29509 clauses = prev;
29510 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29513 saw_error:
29514 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29515 no reason to skip to the end. */
29516 if (!(flag_cilkplus && pragma_tok == NULL))
29517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29518 if (finish_p)
29519 return finish_omp_clauses (clauses);
29520 return clauses;
29523 /* OpenMP 2.5:
29524 structured-block:
29525 statement
29527 In practice, we're also interested in adding the statement to an
29528 outer node. So it is convenient if we work around the fact that
29529 cp_parser_statement calls add_stmt. */
29531 static unsigned
29532 cp_parser_begin_omp_structured_block (cp_parser *parser)
29534 unsigned save = parser->in_statement;
29536 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29537 This preserves the "not within loop or switch" style error messages
29538 for nonsense cases like
29539 void foo() {
29540 #pragma omp single
29541 break;
29544 if (parser->in_statement)
29545 parser->in_statement = IN_OMP_BLOCK;
29547 return save;
29550 static void
29551 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29553 parser->in_statement = save;
29556 static tree
29557 cp_parser_omp_structured_block (cp_parser *parser)
29559 tree stmt = begin_omp_structured_block ();
29560 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29562 cp_parser_statement (parser, NULL_TREE, false, NULL);
29564 cp_parser_end_omp_structured_block (parser, save);
29565 return finish_omp_structured_block (stmt);
29568 /* OpenMP 2.5:
29569 # pragma omp atomic new-line
29570 expression-stmt
29572 expression-stmt:
29573 x binop= expr | x++ | ++x | x-- | --x
29574 binop:
29575 +, *, -, /, &, ^, |, <<, >>
29577 where x is an lvalue expression with scalar type.
29579 OpenMP 3.1:
29580 # pragma omp atomic new-line
29581 update-stmt
29583 # pragma omp atomic read new-line
29584 read-stmt
29586 # pragma omp atomic write new-line
29587 write-stmt
29589 # pragma omp atomic update new-line
29590 update-stmt
29592 # pragma omp atomic capture new-line
29593 capture-stmt
29595 # pragma omp atomic capture new-line
29596 capture-block
29598 read-stmt:
29599 v = x
29600 write-stmt:
29601 x = expr
29602 update-stmt:
29603 expression-stmt | x = x binop expr
29604 capture-stmt:
29605 v = expression-stmt
29606 capture-block:
29607 { v = x; update-stmt; } | { update-stmt; v = x; }
29609 OpenMP 4.0:
29610 update-stmt:
29611 expression-stmt | x = x binop expr | x = expr binop x
29612 capture-stmt:
29613 v = update-stmt
29614 capture-block:
29615 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29617 where x and v are lvalue expressions with scalar type. */
29619 static void
29620 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29622 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29623 tree rhs1 = NULL_TREE, orig_lhs;
29624 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29625 bool structured_block = false;
29626 bool seq_cst = false;
29628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29631 const char *p = IDENTIFIER_POINTER (id);
29633 if (!strcmp (p, "seq_cst"))
29635 seq_cst = true;
29636 cp_lexer_consume_token (parser->lexer);
29637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29638 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29639 cp_lexer_consume_token (parser->lexer);
29642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29644 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29645 const char *p = IDENTIFIER_POINTER (id);
29647 if (!strcmp (p, "read"))
29648 code = OMP_ATOMIC_READ;
29649 else if (!strcmp (p, "write"))
29650 code = NOP_EXPR;
29651 else if (!strcmp (p, "update"))
29652 code = OMP_ATOMIC;
29653 else if (!strcmp (p, "capture"))
29654 code = OMP_ATOMIC_CAPTURE_NEW;
29655 else
29656 p = NULL;
29657 if (p)
29658 cp_lexer_consume_token (parser->lexer);
29660 if (!seq_cst)
29662 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29663 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29664 cp_lexer_consume_token (parser->lexer);
29666 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29668 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29669 const char *p = IDENTIFIER_POINTER (id);
29671 if (!strcmp (p, "seq_cst"))
29673 seq_cst = true;
29674 cp_lexer_consume_token (parser->lexer);
29678 cp_parser_require_pragma_eol (parser, pragma_tok);
29680 switch (code)
29682 case OMP_ATOMIC_READ:
29683 case NOP_EXPR: /* atomic write */
29684 v = cp_parser_unary_expression (parser);
29685 if (v == error_mark_node)
29686 goto saw_error;
29687 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29688 goto saw_error;
29689 if (code == NOP_EXPR)
29690 lhs = cp_parser_expression (parser);
29691 else
29692 lhs = cp_parser_unary_expression (parser);
29693 if (lhs == error_mark_node)
29694 goto saw_error;
29695 if (code == NOP_EXPR)
29697 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29698 opcode. */
29699 code = OMP_ATOMIC;
29700 rhs = lhs;
29701 lhs = v;
29702 v = NULL_TREE;
29704 goto done;
29705 case OMP_ATOMIC_CAPTURE_NEW:
29706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29708 cp_lexer_consume_token (parser->lexer);
29709 structured_block = true;
29711 else
29713 v = cp_parser_unary_expression (parser);
29714 if (v == error_mark_node)
29715 goto saw_error;
29716 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29717 goto saw_error;
29719 default:
29720 break;
29723 restart:
29724 lhs = cp_parser_unary_expression (parser);
29725 orig_lhs = lhs;
29726 switch (TREE_CODE (lhs))
29728 case ERROR_MARK:
29729 goto saw_error;
29731 case POSTINCREMENT_EXPR:
29732 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29733 code = OMP_ATOMIC_CAPTURE_OLD;
29734 /* FALLTHROUGH */
29735 case PREINCREMENT_EXPR:
29736 lhs = TREE_OPERAND (lhs, 0);
29737 opcode = PLUS_EXPR;
29738 rhs = integer_one_node;
29739 break;
29741 case POSTDECREMENT_EXPR:
29742 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29743 code = OMP_ATOMIC_CAPTURE_OLD;
29744 /* FALLTHROUGH */
29745 case PREDECREMENT_EXPR:
29746 lhs = TREE_OPERAND (lhs, 0);
29747 opcode = MINUS_EXPR;
29748 rhs = integer_one_node;
29749 break;
29751 case COMPOUND_EXPR:
29752 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29753 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29754 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29755 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29756 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29757 (TREE_OPERAND (lhs, 1), 0), 0)))
29758 == BOOLEAN_TYPE)
29759 /* Undo effects of boolean_increment for post {in,de}crement. */
29760 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29761 /* FALLTHRU */
29762 case MODIFY_EXPR:
29763 if (TREE_CODE (lhs) == MODIFY_EXPR
29764 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29766 /* Undo effects of boolean_increment. */
29767 if (integer_onep (TREE_OPERAND (lhs, 1)))
29769 /* This is pre or post increment. */
29770 rhs = TREE_OPERAND (lhs, 1);
29771 lhs = TREE_OPERAND (lhs, 0);
29772 opcode = NOP_EXPR;
29773 if (code == OMP_ATOMIC_CAPTURE_NEW
29774 && !structured_block
29775 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29776 code = OMP_ATOMIC_CAPTURE_OLD;
29777 break;
29780 /* FALLTHRU */
29781 default:
29782 switch (cp_lexer_peek_token (parser->lexer)->type)
29784 case CPP_MULT_EQ:
29785 opcode = MULT_EXPR;
29786 break;
29787 case CPP_DIV_EQ:
29788 opcode = TRUNC_DIV_EXPR;
29789 break;
29790 case CPP_PLUS_EQ:
29791 opcode = PLUS_EXPR;
29792 break;
29793 case CPP_MINUS_EQ:
29794 opcode = MINUS_EXPR;
29795 break;
29796 case CPP_LSHIFT_EQ:
29797 opcode = LSHIFT_EXPR;
29798 break;
29799 case CPP_RSHIFT_EQ:
29800 opcode = RSHIFT_EXPR;
29801 break;
29802 case CPP_AND_EQ:
29803 opcode = BIT_AND_EXPR;
29804 break;
29805 case CPP_OR_EQ:
29806 opcode = BIT_IOR_EXPR;
29807 break;
29808 case CPP_XOR_EQ:
29809 opcode = BIT_XOR_EXPR;
29810 break;
29811 case CPP_EQ:
29812 enum cp_parser_prec oprec;
29813 cp_token *token;
29814 cp_lexer_consume_token (parser->lexer);
29815 cp_parser_parse_tentatively (parser);
29816 rhs1 = cp_parser_simple_cast_expression (parser);
29817 if (rhs1 == error_mark_node)
29819 cp_parser_abort_tentative_parse (parser);
29820 cp_parser_simple_cast_expression (parser);
29821 goto saw_error;
29823 token = cp_lexer_peek_token (parser->lexer);
29824 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29826 cp_parser_abort_tentative_parse (parser);
29827 cp_parser_parse_tentatively (parser);
29828 rhs = cp_parser_binary_expression (parser, false, true,
29829 PREC_NOT_OPERATOR, NULL);
29830 if (rhs == error_mark_node)
29832 cp_parser_abort_tentative_parse (parser);
29833 cp_parser_binary_expression (parser, false, true,
29834 PREC_NOT_OPERATOR, NULL);
29835 goto saw_error;
29837 switch (TREE_CODE (rhs))
29839 case MULT_EXPR:
29840 case TRUNC_DIV_EXPR:
29841 case PLUS_EXPR:
29842 case MINUS_EXPR:
29843 case LSHIFT_EXPR:
29844 case RSHIFT_EXPR:
29845 case BIT_AND_EXPR:
29846 case BIT_IOR_EXPR:
29847 case BIT_XOR_EXPR:
29848 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29850 if (cp_parser_parse_definitely (parser))
29852 opcode = TREE_CODE (rhs);
29853 rhs1 = TREE_OPERAND (rhs, 0);
29854 rhs = TREE_OPERAND (rhs, 1);
29855 goto stmt_done;
29857 else
29858 goto saw_error;
29860 break;
29861 default:
29862 break;
29864 cp_parser_abort_tentative_parse (parser);
29865 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29867 rhs = cp_parser_expression (parser);
29868 if (rhs == error_mark_node)
29869 goto saw_error;
29870 opcode = NOP_EXPR;
29871 rhs1 = NULL_TREE;
29872 goto stmt_done;
29874 cp_parser_error (parser,
29875 "invalid form of %<#pragma omp atomic%>");
29876 goto saw_error;
29878 if (!cp_parser_parse_definitely (parser))
29879 goto saw_error;
29880 switch (token->type)
29882 case CPP_SEMICOLON:
29883 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29885 code = OMP_ATOMIC_CAPTURE_OLD;
29886 v = lhs;
29887 lhs = NULL_TREE;
29888 lhs1 = rhs1;
29889 rhs1 = NULL_TREE;
29890 cp_lexer_consume_token (parser->lexer);
29891 goto restart;
29893 else if (structured_block)
29895 opcode = NOP_EXPR;
29896 rhs = rhs1;
29897 rhs1 = NULL_TREE;
29898 goto stmt_done;
29900 cp_parser_error (parser,
29901 "invalid form of %<#pragma omp atomic%>");
29902 goto saw_error;
29903 case CPP_MULT:
29904 opcode = MULT_EXPR;
29905 break;
29906 case CPP_DIV:
29907 opcode = TRUNC_DIV_EXPR;
29908 break;
29909 case CPP_PLUS:
29910 opcode = PLUS_EXPR;
29911 break;
29912 case CPP_MINUS:
29913 opcode = MINUS_EXPR;
29914 break;
29915 case CPP_LSHIFT:
29916 opcode = LSHIFT_EXPR;
29917 break;
29918 case CPP_RSHIFT:
29919 opcode = RSHIFT_EXPR;
29920 break;
29921 case CPP_AND:
29922 opcode = BIT_AND_EXPR;
29923 break;
29924 case CPP_OR:
29925 opcode = BIT_IOR_EXPR;
29926 break;
29927 case CPP_XOR:
29928 opcode = BIT_XOR_EXPR;
29929 break;
29930 default:
29931 cp_parser_error (parser,
29932 "invalid operator for %<#pragma omp atomic%>");
29933 goto saw_error;
29935 oprec = TOKEN_PRECEDENCE (token);
29936 gcc_assert (oprec != PREC_NOT_OPERATOR);
29937 if (commutative_tree_code (opcode))
29938 oprec = (enum cp_parser_prec) (oprec - 1);
29939 cp_lexer_consume_token (parser->lexer);
29940 rhs = cp_parser_binary_expression (parser, false, false,
29941 oprec, NULL);
29942 if (rhs == error_mark_node)
29943 goto saw_error;
29944 goto stmt_done;
29945 /* FALLTHROUGH */
29946 default:
29947 cp_parser_error (parser,
29948 "invalid operator for %<#pragma omp atomic%>");
29949 goto saw_error;
29951 cp_lexer_consume_token (parser->lexer);
29953 rhs = cp_parser_expression (parser);
29954 if (rhs == error_mark_node)
29955 goto saw_error;
29956 break;
29958 stmt_done:
29959 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29961 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29962 goto saw_error;
29963 v = cp_parser_unary_expression (parser);
29964 if (v == error_mark_node)
29965 goto saw_error;
29966 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29967 goto saw_error;
29968 lhs1 = cp_parser_unary_expression (parser);
29969 if (lhs1 == error_mark_node)
29970 goto saw_error;
29972 if (structured_block)
29974 cp_parser_consume_semicolon_at_end_of_statement (parser);
29975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29977 done:
29978 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29979 if (!structured_block)
29980 cp_parser_consume_semicolon_at_end_of_statement (parser);
29981 return;
29983 saw_error:
29984 cp_parser_skip_to_end_of_block_or_statement (parser);
29985 if (structured_block)
29987 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29988 cp_lexer_consume_token (parser->lexer);
29989 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29991 cp_parser_skip_to_end_of_block_or_statement (parser);
29992 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29993 cp_lexer_consume_token (parser->lexer);
29999 /* OpenMP 2.5:
30000 # pragma omp barrier new-line */
30002 static void
30003 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30005 cp_parser_require_pragma_eol (parser, pragma_tok);
30006 finish_omp_barrier ();
30009 /* OpenMP 2.5:
30010 # pragma omp critical [(name)] new-line
30011 structured-block */
30013 static tree
30014 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30016 tree stmt, name = NULL;
30018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30020 cp_lexer_consume_token (parser->lexer);
30022 name = cp_parser_identifier (parser);
30024 if (name == error_mark_node
30025 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30027 /*or_comma=*/false,
30028 /*consume_paren=*/true);
30029 if (name == error_mark_node)
30030 name = NULL;
30032 cp_parser_require_pragma_eol (parser, pragma_tok);
30034 stmt = cp_parser_omp_structured_block (parser);
30035 return c_finish_omp_critical (input_location, stmt, name);
30038 /* OpenMP 2.5:
30039 # pragma omp flush flush-vars[opt] new-line
30041 flush-vars:
30042 ( variable-list ) */
30044 static void
30045 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30048 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30049 cp_parser_require_pragma_eol (parser, pragma_tok);
30051 finish_omp_flush ();
30054 /* Helper function, to parse omp for increment expression. */
30056 static tree
30057 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30059 tree cond = cp_parser_binary_expression (parser, false, true,
30060 PREC_NOT_OPERATOR, NULL);
30061 if (cond == error_mark_node
30062 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30064 cp_parser_skip_to_end_of_statement (parser);
30065 return error_mark_node;
30068 switch (TREE_CODE (cond))
30070 case GT_EXPR:
30071 case GE_EXPR:
30072 case LT_EXPR:
30073 case LE_EXPR:
30074 break;
30075 case NE_EXPR:
30076 if (code == CILK_SIMD || code == CILK_FOR)
30077 break;
30078 /* Fall through: OpenMP disallows NE_EXPR. */
30079 default:
30080 return error_mark_node;
30083 /* If decl is an iterator, preserve LHS and RHS of the relational
30084 expr until finish_omp_for. */
30085 if (decl
30086 && (type_dependent_expression_p (decl)
30087 || CLASS_TYPE_P (TREE_TYPE (decl))))
30088 return cond;
30090 return build_x_binary_op (input_location, TREE_CODE (cond),
30091 TREE_OPERAND (cond, 0), ERROR_MARK,
30092 TREE_OPERAND (cond, 1), ERROR_MARK,
30093 /*overload=*/NULL, tf_warning_or_error);
30096 /* Helper function, to parse omp for increment expression. */
30098 static tree
30099 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30101 cp_token *token = cp_lexer_peek_token (parser->lexer);
30102 enum tree_code op;
30103 tree lhs, rhs;
30104 cp_id_kind idk;
30105 bool decl_first;
30107 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30109 op = (token->type == CPP_PLUS_PLUS
30110 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30111 cp_lexer_consume_token (parser->lexer);
30112 lhs = cp_parser_simple_cast_expression (parser);
30113 if (lhs != decl)
30114 return error_mark_node;
30115 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30118 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30119 if (lhs != decl)
30120 return error_mark_node;
30122 token = cp_lexer_peek_token (parser->lexer);
30123 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30125 op = (token->type == CPP_PLUS_PLUS
30126 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30127 cp_lexer_consume_token (parser->lexer);
30128 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30131 op = cp_parser_assignment_operator_opt (parser);
30132 if (op == ERROR_MARK)
30133 return error_mark_node;
30135 if (op != NOP_EXPR)
30137 rhs = cp_parser_assignment_expression (parser);
30138 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30139 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30142 lhs = cp_parser_binary_expression (parser, false, false,
30143 PREC_ADDITIVE_EXPRESSION, NULL);
30144 token = cp_lexer_peek_token (parser->lexer);
30145 decl_first = lhs == decl;
30146 if (decl_first)
30147 lhs = NULL_TREE;
30148 if (token->type != CPP_PLUS
30149 && token->type != CPP_MINUS)
30150 return error_mark_node;
30154 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30155 cp_lexer_consume_token (parser->lexer);
30156 rhs = cp_parser_binary_expression (parser, false, false,
30157 PREC_ADDITIVE_EXPRESSION, NULL);
30158 token = cp_lexer_peek_token (parser->lexer);
30159 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30161 if (lhs == NULL_TREE)
30163 if (op == PLUS_EXPR)
30164 lhs = rhs;
30165 else
30166 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30167 tf_warning_or_error);
30169 else
30170 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30171 ERROR_MARK, NULL, tf_warning_or_error);
30174 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30176 if (!decl_first)
30178 if (rhs != decl || op == MINUS_EXPR)
30179 return error_mark_node;
30180 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30182 else
30183 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30185 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30188 /* Parse the initialization statement of either an OpenMP for loop or
30189 a Cilk Plus for loop.
30191 Return true if the resulting construct should have an
30192 OMP_CLAUSE_PRIVATE added to it. */
30194 static bool
30195 cp_parser_omp_for_loop_init (cp_parser *parser,
30196 enum tree_code code,
30197 tree &this_pre_body,
30198 vec<tree, va_gc> *for_block,
30199 tree &init,
30200 tree &decl,
30201 tree &real_decl)
30203 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30204 return false;
30206 bool add_private_clause = false;
30208 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30210 init-expr:
30211 var = lb
30212 integer-type var = lb
30213 random-access-iterator-type var = lb
30214 pointer-type var = lb
30216 cp_decl_specifier_seq type_specifiers;
30218 /* First, try to parse as an initialized declaration. See
30219 cp_parser_condition, from whence the bulk of this is copied. */
30221 cp_parser_parse_tentatively (parser);
30222 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30223 /*is_trailing_return=*/false,
30224 &type_specifiers);
30225 if (cp_parser_parse_definitely (parser))
30227 /* If parsing a type specifier seq succeeded, then this
30228 MUST be a initialized declaration. */
30229 tree asm_specification, attributes;
30230 cp_declarator *declarator;
30232 declarator = cp_parser_declarator (parser,
30233 CP_PARSER_DECLARATOR_NAMED,
30234 /*ctor_dtor_or_conv_p=*/NULL,
30235 /*parenthesized_p=*/NULL,
30236 /*member_p=*/false,
30237 /*friend_p=*/false);
30238 attributes = cp_parser_attributes_opt (parser);
30239 asm_specification = cp_parser_asm_specification_opt (parser);
30241 if (declarator == cp_error_declarator)
30242 cp_parser_skip_to_end_of_statement (parser);
30244 else
30246 tree pushed_scope, auto_node;
30248 decl = start_decl (declarator, &type_specifiers,
30249 SD_INITIALIZED, attributes,
30250 /*prefix_attributes=*/NULL_TREE,
30251 &pushed_scope);
30253 auto_node = type_uses_auto (TREE_TYPE (decl));
30254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30256 if (cp_lexer_next_token_is (parser->lexer,
30257 CPP_OPEN_PAREN))
30259 if (code != CILK_SIMD && code != CILK_FOR)
30260 error ("parenthesized initialization is not allowed in "
30261 "OpenMP %<for%> loop");
30262 else
30263 error ("parenthesized initialization is "
30264 "not allowed in for-loop");
30266 else
30267 /* Trigger an error. */
30268 cp_parser_require (parser, CPP_EQ, RT_EQ);
30270 init = error_mark_node;
30271 cp_parser_skip_to_end_of_statement (parser);
30273 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30274 || type_dependent_expression_p (decl)
30275 || auto_node)
30277 bool is_direct_init, is_non_constant_init;
30279 init = cp_parser_initializer (parser,
30280 &is_direct_init,
30281 &is_non_constant_init);
30283 if (auto_node)
30285 TREE_TYPE (decl)
30286 = do_auto_deduction (TREE_TYPE (decl), init,
30287 auto_node);
30289 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30290 && !type_dependent_expression_p (decl))
30291 goto non_class;
30294 cp_finish_decl (decl, init, !is_non_constant_init,
30295 asm_specification,
30296 LOOKUP_ONLYCONVERTING);
30297 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30299 vec_safe_push (for_block, this_pre_body);
30300 init = NULL_TREE;
30302 else
30303 init = pop_stmt_list (this_pre_body);
30304 this_pre_body = NULL_TREE;
30306 else
30308 /* Consume '='. */
30309 cp_lexer_consume_token (parser->lexer);
30310 init = cp_parser_assignment_expression (parser);
30312 non_class:
30313 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30314 init = error_mark_node;
30315 else
30316 cp_finish_decl (decl, NULL_TREE,
30317 /*init_const_expr_p=*/false,
30318 asm_specification,
30319 LOOKUP_ONLYCONVERTING);
30322 if (pushed_scope)
30323 pop_scope (pushed_scope);
30326 else
30328 cp_id_kind idk;
30329 /* If parsing a type specifier sequence failed, then
30330 this MUST be a simple expression. */
30331 if (code == CILK_FOR)
30332 error ("%<_Cilk_for%> allows expression instead of declaration only "
30333 "in C, not in C++");
30334 cp_parser_parse_tentatively (parser);
30335 decl = cp_parser_primary_expression (parser, false, false,
30336 false, &idk);
30337 if (!cp_parser_error_occurred (parser)
30338 && decl
30339 && DECL_P (decl)
30340 && CLASS_TYPE_P (TREE_TYPE (decl)))
30342 tree rhs;
30344 cp_parser_parse_definitely (parser);
30345 cp_parser_require (parser, CPP_EQ, RT_EQ);
30346 rhs = cp_parser_assignment_expression (parser);
30347 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30348 decl, NOP_EXPR,
30349 rhs,
30350 tf_warning_or_error));
30351 add_private_clause = true;
30353 else
30355 decl = NULL;
30356 cp_parser_abort_tentative_parse (parser);
30357 init = cp_parser_expression (parser);
30358 if (init)
30360 if (TREE_CODE (init) == MODIFY_EXPR
30361 || TREE_CODE (init) == MODOP_EXPR)
30362 real_decl = TREE_OPERAND (init, 0);
30366 return add_private_clause;
30369 /* Parse the restricted form of the for statement allowed by OpenMP. */
30371 static tree
30372 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30373 tree *cclauses)
30375 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30376 tree real_decl, initv, condv, incrv, declv;
30377 tree this_pre_body, cl;
30378 location_t loc_first;
30379 bool collapse_err = false;
30380 int i, collapse = 1, nbraces = 0;
30381 vec<tree, va_gc> *for_block = make_tree_vector ();
30383 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30384 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30385 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30387 gcc_assert (collapse >= 1);
30389 declv = make_tree_vec (collapse);
30390 initv = make_tree_vec (collapse);
30391 condv = make_tree_vec (collapse);
30392 incrv = make_tree_vec (collapse);
30394 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30396 for (i = 0; i < collapse; i++)
30398 int bracecount = 0;
30399 bool add_private_clause = false;
30400 location_t loc;
30402 if (code != CILK_FOR
30403 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30405 cp_parser_error (parser, "for statement expected");
30406 return NULL;
30408 if (code == CILK_FOR
30409 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30411 cp_parser_error (parser, "_Cilk_for statement expected");
30412 return NULL;
30414 loc = cp_lexer_consume_token (parser->lexer)->location;
30416 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30417 return NULL;
30419 init = decl = real_decl = NULL;
30420 this_pre_body = push_stmt_list ();
30422 add_private_clause
30423 |= cp_parser_omp_for_loop_init (parser, code,
30424 this_pre_body, for_block,
30425 init, decl, real_decl);
30427 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30428 if (this_pre_body)
30430 this_pre_body = pop_stmt_list (this_pre_body);
30431 if (pre_body)
30433 tree t = pre_body;
30434 pre_body = push_stmt_list ();
30435 add_stmt (t);
30436 add_stmt (this_pre_body);
30437 pre_body = pop_stmt_list (pre_body);
30439 else
30440 pre_body = this_pre_body;
30443 if (decl)
30444 real_decl = decl;
30445 if (cclauses != NULL
30446 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30447 && real_decl != NULL_TREE)
30449 tree *c;
30450 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30451 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30452 && OMP_CLAUSE_DECL (*c) == real_decl)
30454 error_at (loc, "iteration variable %qD"
30455 " should not be firstprivate", real_decl);
30456 *c = OMP_CLAUSE_CHAIN (*c);
30458 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30459 && OMP_CLAUSE_DECL (*c) == real_decl)
30461 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30462 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30463 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30464 OMP_CLAUSE_DECL (l) = real_decl;
30465 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30466 if (code == OMP_SIMD)
30468 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30469 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30471 else
30473 OMP_CLAUSE_CHAIN (l) = clauses;
30474 clauses = l;
30476 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30477 CP_OMP_CLAUSE_INFO (*c) = NULL;
30478 add_private_clause = false;
30480 else
30482 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30483 && OMP_CLAUSE_DECL (*c) == real_decl)
30484 add_private_clause = false;
30485 c = &OMP_CLAUSE_CHAIN (*c);
30489 if (add_private_clause)
30491 tree c;
30492 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30494 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30495 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30496 && OMP_CLAUSE_DECL (c) == decl)
30497 break;
30498 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30499 && OMP_CLAUSE_DECL (c) == decl)
30500 error_at (loc, "iteration variable %qD "
30501 "should not be firstprivate",
30502 decl);
30503 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30504 && OMP_CLAUSE_DECL (c) == decl)
30505 error_at (loc, "iteration variable %qD should not be reduction",
30506 decl);
30508 if (c == NULL)
30510 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30511 OMP_CLAUSE_DECL (c) = decl;
30512 c = finish_omp_clauses (c);
30513 if (c)
30515 OMP_CLAUSE_CHAIN (c) = clauses;
30516 clauses = c;
30521 cond = NULL;
30522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30523 cond = cp_parser_omp_for_cond (parser, decl, code);
30524 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30526 incr = NULL;
30527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30529 /* If decl is an iterator, preserve the operator on decl
30530 until finish_omp_for. */
30531 if (real_decl
30532 && ((processing_template_decl
30533 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30534 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30535 incr = cp_parser_omp_for_incr (parser, real_decl);
30536 else
30537 incr = cp_parser_expression (parser);
30538 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30539 SET_EXPR_LOCATION (incr, input_location);
30542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30543 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30544 /*or_comma=*/false,
30545 /*consume_paren=*/true);
30547 TREE_VEC_ELT (declv, i) = decl;
30548 TREE_VEC_ELT (initv, i) = init;
30549 TREE_VEC_ELT (condv, i) = cond;
30550 TREE_VEC_ELT (incrv, i) = incr;
30552 if (i == collapse - 1)
30553 break;
30555 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30556 in between the collapsed for loops to be still considered perfectly
30557 nested. Hopefully the final version clarifies this.
30558 For now handle (multiple) {'s and empty statements. */
30559 cp_parser_parse_tentatively (parser);
30562 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30563 break;
30564 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30566 cp_lexer_consume_token (parser->lexer);
30567 bracecount++;
30569 else if (bracecount
30570 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30571 cp_lexer_consume_token (parser->lexer);
30572 else
30574 loc = cp_lexer_peek_token (parser->lexer)->location;
30575 error_at (loc, "not enough collapsed for loops");
30576 collapse_err = true;
30577 cp_parser_abort_tentative_parse (parser);
30578 declv = NULL_TREE;
30579 break;
30582 while (1);
30584 if (declv)
30586 cp_parser_parse_definitely (parser);
30587 nbraces += bracecount;
30591 /* Note that we saved the original contents of this flag when we entered
30592 the structured block, and so we don't need to re-save it here. */
30593 if (code == CILK_SIMD || code == CILK_FOR)
30594 parser->in_statement = IN_CILK_SIMD_FOR;
30595 else
30596 parser->in_statement = IN_OMP_FOR;
30598 /* Note that the grammar doesn't call for a structured block here,
30599 though the loop as a whole is a structured block. */
30600 body = push_stmt_list ();
30601 cp_parser_statement (parser, NULL_TREE, false, NULL);
30602 body = pop_stmt_list (body);
30604 if (declv == NULL_TREE)
30605 ret = NULL_TREE;
30606 else
30607 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30608 pre_body, clauses);
30610 while (nbraces)
30612 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30614 cp_lexer_consume_token (parser->lexer);
30615 nbraces--;
30617 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30618 cp_lexer_consume_token (parser->lexer);
30619 else
30621 if (!collapse_err)
30623 error_at (cp_lexer_peek_token (parser->lexer)->location,
30624 "collapsed loops not perfectly nested");
30626 collapse_err = true;
30627 cp_parser_statement_seq_opt (parser, NULL);
30628 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30629 break;
30633 while (!for_block->is_empty ())
30634 add_stmt (pop_stmt_list (for_block->pop ()));
30635 release_tree_vector (for_block);
30637 return ret;
30640 /* Helper function for OpenMP parsing, split clauses and call
30641 finish_omp_clauses on each of the set of clauses afterwards. */
30643 static void
30644 cp_omp_split_clauses (location_t loc, enum tree_code code,
30645 omp_clause_mask mask, tree clauses, tree *cclauses)
30647 int i;
30648 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30649 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30650 if (cclauses[i])
30651 cclauses[i] = finish_omp_clauses (cclauses[i]);
30654 /* OpenMP 4.0:
30655 #pragma omp simd simd-clause[optseq] new-line
30656 for-loop */
30658 #define OMP_SIMD_CLAUSE_MASK \
30659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30667 static tree
30668 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30669 char *p_name, omp_clause_mask mask, tree *cclauses)
30671 tree clauses, sb, ret;
30672 unsigned int save;
30673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30675 strcat (p_name, " simd");
30676 mask |= OMP_SIMD_CLAUSE_MASK;
30677 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30679 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30680 cclauses == NULL);
30681 if (cclauses)
30683 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30684 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30687 sb = begin_omp_structured_block ();
30688 save = cp_parser_begin_omp_structured_block (parser);
30690 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30692 cp_parser_end_omp_structured_block (parser, save);
30693 add_stmt (finish_omp_structured_block (sb));
30695 return ret;
30698 /* OpenMP 2.5:
30699 #pragma omp for for-clause[optseq] new-line
30700 for-loop
30702 OpenMP 4.0:
30703 #pragma omp for simd for-simd-clause[optseq] new-line
30704 for-loop */
30706 #define OMP_FOR_CLAUSE_MASK \
30707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30716 static tree
30717 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30718 char *p_name, omp_clause_mask mask, tree *cclauses)
30720 tree clauses, sb, ret;
30721 unsigned int save;
30722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30724 strcat (p_name, " for");
30725 mask |= OMP_FOR_CLAUSE_MASK;
30726 if (cclauses)
30727 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30729 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30731 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30732 const char *p = IDENTIFIER_POINTER (id);
30734 if (strcmp (p, "simd") == 0)
30736 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30737 if (cclauses == NULL)
30738 cclauses = cclauses_buf;
30740 cp_lexer_consume_token (parser->lexer);
30741 if (!flag_openmp) /* flag_openmp_simd */
30742 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30743 cclauses);
30744 sb = begin_omp_structured_block ();
30745 save = cp_parser_begin_omp_structured_block (parser);
30746 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30747 cclauses);
30748 cp_parser_end_omp_structured_block (parser, save);
30749 tree body = finish_omp_structured_block (sb);
30750 if (ret == NULL)
30751 return ret;
30752 ret = make_node (OMP_FOR);
30753 TREE_TYPE (ret) = void_type_node;
30754 OMP_FOR_BODY (ret) = body;
30755 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30756 SET_EXPR_LOCATION (ret, loc);
30757 add_stmt (ret);
30758 return ret;
30761 if (!flag_openmp) /* flag_openmp_simd */
30763 cp_parser_require_pragma_eol (parser, pragma_tok);
30764 return NULL_TREE;
30767 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30768 cclauses == NULL);
30769 if (cclauses)
30771 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30772 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30775 sb = begin_omp_structured_block ();
30776 save = cp_parser_begin_omp_structured_block (parser);
30778 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30780 cp_parser_end_omp_structured_block (parser, save);
30781 add_stmt (finish_omp_structured_block (sb));
30783 return ret;
30786 /* OpenMP 2.5:
30787 # pragma omp master new-line
30788 structured-block */
30790 static tree
30791 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30793 cp_parser_require_pragma_eol (parser, pragma_tok);
30794 return c_finish_omp_master (input_location,
30795 cp_parser_omp_structured_block (parser));
30798 /* OpenMP 2.5:
30799 # pragma omp ordered new-line
30800 structured-block */
30802 static tree
30803 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30805 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30806 cp_parser_require_pragma_eol (parser, pragma_tok);
30807 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30810 /* OpenMP 2.5:
30812 section-scope:
30813 { section-sequence }
30815 section-sequence:
30816 section-directive[opt] structured-block
30817 section-sequence section-directive structured-block */
30819 static tree
30820 cp_parser_omp_sections_scope (cp_parser *parser)
30822 tree stmt, substmt;
30823 bool error_suppress = false;
30824 cp_token *tok;
30826 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30827 return NULL_TREE;
30829 stmt = push_stmt_list ();
30831 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30833 substmt = cp_parser_omp_structured_block (parser);
30834 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30835 add_stmt (substmt);
30838 while (1)
30840 tok = cp_lexer_peek_token (parser->lexer);
30841 if (tok->type == CPP_CLOSE_BRACE)
30842 break;
30843 if (tok->type == CPP_EOF)
30844 break;
30846 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30848 cp_lexer_consume_token (parser->lexer);
30849 cp_parser_require_pragma_eol (parser, tok);
30850 error_suppress = false;
30852 else if (!error_suppress)
30854 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30855 error_suppress = true;
30858 substmt = cp_parser_omp_structured_block (parser);
30859 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30860 add_stmt (substmt);
30862 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30864 substmt = pop_stmt_list (stmt);
30866 stmt = make_node (OMP_SECTIONS);
30867 TREE_TYPE (stmt) = void_type_node;
30868 OMP_SECTIONS_BODY (stmt) = substmt;
30870 add_stmt (stmt);
30871 return stmt;
30874 /* OpenMP 2.5:
30875 # pragma omp sections sections-clause[optseq] newline
30876 sections-scope */
30878 #define OMP_SECTIONS_CLAUSE_MASK \
30879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30885 static tree
30886 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30887 char *p_name, omp_clause_mask mask, tree *cclauses)
30889 tree clauses, ret;
30890 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30892 strcat (p_name, " sections");
30893 mask |= OMP_SECTIONS_CLAUSE_MASK;
30894 if (cclauses)
30895 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30897 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30898 cclauses == NULL);
30899 if (cclauses)
30901 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30902 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30905 ret = cp_parser_omp_sections_scope (parser);
30906 if (ret)
30907 OMP_SECTIONS_CLAUSES (ret) = clauses;
30909 return ret;
30912 /* OpenMP 2.5:
30913 # pragma omp parallel parallel-clause[optseq] new-line
30914 structured-block
30915 # pragma omp parallel for parallel-for-clause[optseq] new-line
30916 structured-block
30917 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30918 structured-block
30920 OpenMP 4.0:
30921 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30922 structured-block */
30924 #define OMP_PARALLEL_CLAUSE_MASK \
30925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30935 static tree
30936 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30937 char *p_name, omp_clause_mask mask, tree *cclauses)
30939 tree stmt, clauses, block;
30940 unsigned int save;
30941 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30943 strcat (p_name, " parallel");
30944 mask |= OMP_PARALLEL_CLAUSE_MASK;
30946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30948 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30949 if (cclauses == NULL)
30950 cclauses = cclauses_buf;
30952 cp_lexer_consume_token (parser->lexer);
30953 if (!flag_openmp) /* flag_openmp_simd */
30954 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30955 block = begin_omp_parallel ();
30956 save = cp_parser_begin_omp_structured_block (parser);
30957 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30958 cp_parser_end_omp_structured_block (parser, save);
30959 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30960 block);
30961 if (ret == NULL_TREE)
30962 return ret;
30963 OMP_PARALLEL_COMBINED (stmt) = 1;
30964 return stmt;
30966 else if (cclauses)
30968 error_at (loc, "expected %<for%> after %qs", p_name);
30969 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30970 return NULL_TREE;
30972 else if (!flag_openmp) /* flag_openmp_simd */
30974 cp_parser_require_pragma_eol (parser, pragma_tok);
30975 return NULL_TREE;
30977 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30980 const char *p = IDENTIFIER_POINTER (id);
30981 if (strcmp (p, "sections") == 0)
30983 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30984 cclauses = cclauses_buf;
30986 cp_lexer_consume_token (parser->lexer);
30987 block = begin_omp_parallel ();
30988 save = cp_parser_begin_omp_structured_block (parser);
30989 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30990 cp_parser_end_omp_structured_block (parser, save);
30991 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30992 block);
30993 OMP_PARALLEL_COMBINED (stmt) = 1;
30994 return stmt;
30998 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31000 block = begin_omp_parallel ();
31001 save = cp_parser_begin_omp_structured_block (parser);
31002 cp_parser_statement (parser, NULL_TREE, false, NULL);
31003 cp_parser_end_omp_structured_block (parser, save);
31004 stmt = finish_omp_parallel (clauses, block);
31005 return stmt;
31008 /* OpenMP 2.5:
31009 # pragma omp single single-clause[optseq] new-line
31010 structured-block */
31012 #define OMP_SINGLE_CLAUSE_MASK \
31013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31018 static tree
31019 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31021 tree stmt = make_node (OMP_SINGLE);
31022 TREE_TYPE (stmt) = void_type_node;
31024 OMP_SINGLE_CLAUSES (stmt)
31025 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31026 "#pragma omp single", pragma_tok);
31027 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31029 return add_stmt (stmt);
31032 /* OpenMP 3.0:
31033 # pragma omp task task-clause[optseq] new-line
31034 structured-block */
31036 #define OMP_TASK_CLAUSE_MASK \
31037 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31047 static tree
31048 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31050 tree clauses, block;
31051 unsigned int save;
31053 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31054 "#pragma omp task", pragma_tok);
31055 block = begin_omp_task ();
31056 save = cp_parser_begin_omp_structured_block (parser);
31057 cp_parser_statement (parser, NULL_TREE, false, NULL);
31058 cp_parser_end_omp_structured_block (parser, save);
31059 return finish_omp_task (clauses, block);
31062 /* OpenMP 3.0:
31063 # pragma omp taskwait new-line */
31065 static void
31066 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31068 cp_parser_require_pragma_eol (parser, pragma_tok);
31069 finish_omp_taskwait ();
31072 /* OpenMP 3.1:
31073 # pragma omp taskyield new-line */
31075 static void
31076 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31078 cp_parser_require_pragma_eol (parser, pragma_tok);
31079 finish_omp_taskyield ();
31082 /* OpenMP 4.0:
31083 # pragma omp taskgroup new-line
31084 structured-block */
31086 static tree
31087 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31089 cp_parser_require_pragma_eol (parser, pragma_tok);
31090 return c_finish_omp_taskgroup (input_location,
31091 cp_parser_omp_structured_block (parser));
31095 /* OpenMP 2.5:
31096 # pragma omp threadprivate (variable-list) */
31098 static void
31099 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31101 tree vars;
31103 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31104 cp_parser_require_pragma_eol (parser, pragma_tok);
31106 finish_omp_threadprivate (vars);
31109 /* OpenMP 4.0:
31110 # pragma omp cancel cancel-clause[optseq] new-line */
31112 #define OMP_CANCEL_CLAUSE_MASK \
31113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31119 static void
31120 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31122 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31123 "#pragma omp cancel", pragma_tok);
31124 finish_omp_cancel (clauses);
31127 /* OpenMP 4.0:
31128 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31130 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31136 static void
31137 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31139 tree clauses;
31140 bool point_seen = false;
31142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31145 const char *p = IDENTIFIER_POINTER (id);
31147 if (strcmp (p, "point") == 0)
31149 cp_lexer_consume_token (parser->lexer);
31150 point_seen = true;
31153 if (!point_seen)
31155 cp_parser_error (parser, "expected %<point%>");
31156 cp_parser_require_pragma_eol (parser, pragma_tok);
31157 return;
31160 clauses = cp_parser_omp_all_clauses (parser,
31161 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31162 "#pragma omp cancellation point",
31163 pragma_tok);
31164 finish_omp_cancellation_point (clauses);
31167 /* OpenMP 4.0:
31168 #pragma omp distribute distribute-clause[optseq] new-line
31169 for-loop */
31171 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31177 static tree
31178 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31179 char *p_name, omp_clause_mask mask, tree *cclauses)
31181 tree clauses, sb, ret;
31182 unsigned int save;
31183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31185 strcat (p_name, " distribute");
31186 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31188 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31191 const char *p = IDENTIFIER_POINTER (id);
31192 bool simd = false;
31193 bool parallel = false;
31195 if (strcmp (p, "simd") == 0)
31196 simd = true;
31197 else
31198 parallel = strcmp (p, "parallel") == 0;
31199 if (parallel || simd)
31201 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31202 if (cclauses == NULL)
31203 cclauses = cclauses_buf;
31204 cp_lexer_consume_token (parser->lexer);
31205 if (!flag_openmp) /* flag_openmp_simd */
31207 if (simd)
31208 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31209 cclauses);
31210 else
31211 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31212 cclauses);
31214 sb = begin_omp_structured_block ();
31215 save = cp_parser_begin_omp_structured_block (parser);
31216 if (simd)
31217 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31218 cclauses);
31219 else
31220 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31221 cclauses);
31222 cp_parser_end_omp_structured_block (parser, save);
31223 tree body = finish_omp_structured_block (sb);
31224 if (ret == NULL)
31225 return ret;
31226 ret = make_node (OMP_DISTRIBUTE);
31227 TREE_TYPE (ret) = void_type_node;
31228 OMP_FOR_BODY (ret) = body;
31229 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31230 SET_EXPR_LOCATION (ret, loc);
31231 add_stmt (ret);
31232 return ret;
31235 if (!flag_openmp) /* flag_openmp_simd */
31237 cp_parser_require_pragma_eol (parser, pragma_tok);
31238 return NULL_TREE;
31241 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31242 cclauses == NULL);
31243 if (cclauses)
31245 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31246 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31249 sb = begin_omp_structured_block ();
31250 save = cp_parser_begin_omp_structured_block (parser);
31252 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31254 cp_parser_end_omp_structured_block (parser, save);
31255 add_stmt (finish_omp_structured_block (sb));
31257 return ret;
31260 /* OpenMP 4.0:
31261 # pragma omp teams teams-clause[optseq] new-line
31262 structured-block */
31264 #define OMP_TEAMS_CLAUSE_MASK \
31265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31273 static tree
31274 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31275 char *p_name, omp_clause_mask mask, tree *cclauses)
31277 tree clauses, sb, ret;
31278 unsigned int save;
31279 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31281 strcat (p_name, " teams");
31282 mask |= OMP_TEAMS_CLAUSE_MASK;
31284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31286 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31287 const char *p = IDENTIFIER_POINTER (id);
31288 if (strcmp (p, "distribute") == 0)
31290 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31291 if (cclauses == NULL)
31292 cclauses = cclauses_buf;
31294 cp_lexer_consume_token (parser->lexer);
31295 if (!flag_openmp) /* flag_openmp_simd */
31296 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31297 cclauses);
31298 sb = begin_omp_structured_block ();
31299 save = cp_parser_begin_omp_structured_block (parser);
31300 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31301 cclauses);
31302 cp_parser_end_omp_structured_block (parser, save);
31303 tree body = finish_omp_structured_block (sb);
31304 if (ret == NULL)
31305 return ret;
31306 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31307 ret = make_node (OMP_TEAMS);
31308 TREE_TYPE (ret) = void_type_node;
31309 OMP_TEAMS_CLAUSES (ret) = clauses;
31310 OMP_TEAMS_BODY (ret) = body;
31311 return add_stmt (ret);
31314 if (!flag_openmp) /* flag_openmp_simd */
31316 cp_parser_require_pragma_eol (parser, pragma_tok);
31317 return NULL_TREE;
31320 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31321 cclauses == NULL);
31322 if (cclauses)
31324 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31325 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31328 tree stmt = make_node (OMP_TEAMS);
31329 TREE_TYPE (stmt) = void_type_node;
31330 OMP_TEAMS_CLAUSES (stmt) = clauses;
31331 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31333 return add_stmt (stmt);
31336 /* OpenMP 4.0:
31337 # pragma omp target data target-data-clause[optseq] new-line
31338 structured-block */
31340 #define OMP_TARGET_DATA_CLAUSE_MASK \
31341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31345 static tree
31346 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31348 tree stmt = make_node (OMP_TARGET_DATA);
31349 TREE_TYPE (stmt) = void_type_node;
31351 OMP_TARGET_DATA_CLAUSES (stmt)
31352 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31353 "#pragma omp target data", pragma_tok);
31354 keep_next_level (true);
31355 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31357 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31358 return add_stmt (stmt);
31361 /* OpenMP 4.0:
31362 # pragma omp target update target-update-clause[optseq] new-line */
31364 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31370 static bool
31371 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31372 enum pragma_context context)
31374 if (context == pragma_stmt)
31376 error_at (pragma_tok->location,
31377 "%<#pragma omp target update%> may only be "
31378 "used in compound statements");
31379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31380 return false;
31383 tree clauses
31384 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31385 "#pragma omp target update", pragma_tok);
31386 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31387 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31389 error_at (pragma_tok->location,
31390 "%<#pragma omp target update must contain at least one "
31391 "%<from%> or %<to%> clauses");
31392 return false;
31395 tree stmt = make_node (OMP_TARGET_UPDATE);
31396 TREE_TYPE (stmt) = void_type_node;
31397 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31398 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31399 add_stmt (stmt);
31400 return false;
31403 /* OpenMP 4.0:
31404 # pragma omp target target-clause[optseq] new-line
31405 structured-block */
31407 #define OMP_TARGET_CLAUSE_MASK \
31408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31412 static bool
31413 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31414 enum pragma_context context)
31416 if (context != pragma_stmt && context != pragma_compound)
31418 cp_parser_error (parser, "expected declaration specifiers");
31419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31420 return false;
31423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31425 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31426 const char *p = IDENTIFIER_POINTER (id);
31428 if (strcmp (p, "teams") == 0)
31430 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31431 char p_name[sizeof ("#pragma omp target teams distribute "
31432 "parallel for simd")];
31434 cp_lexer_consume_token (parser->lexer);
31435 strcpy (p_name, "#pragma omp target");
31436 if (!flag_openmp) /* flag_openmp_simd */
31438 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31439 OMP_TARGET_CLAUSE_MASK,
31440 cclauses);
31441 return stmt != NULL_TREE;
31443 keep_next_level (true);
31444 tree sb = begin_omp_structured_block ();
31445 unsigned save = cp_parser_begin_omp_structured_block (parser);
31446 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31447 OMP_TARGET_CLAUSE_MASK, cclauses);
31448 cp_parser_end_omp_structured_block (parser, save);
31449 tree body = finish_omp_structured_block (sb);
31450 if (ret == NULL_TREE)
31451 return false;
31452 tree stmt = make_node (OMP_TARGET);
31453 TREE_TYPE (stmt) = void_type_node;
31454 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31455 OMP_TARGET_BODY (stmt) = body;
31456 add_stmt (stmt);
31457 return true;
31459 else if (!flag_openmp) /* flag_openmp_simd */
31461 cp_parser_require_pragma_eol (parser, pragma_tok);
31462 return false;
31464 else if (strcmp (p, "data") == 0)
31466 cp_lexer_consume_token (parser->lexer);
31467 cp_parser_omp_target_data (parser, pragma_tok);
31468 return true;
31470 else if (strcmp (p, "update") == 0)
31472 cp_lexer_consume_token (parser->lexer);
31473 return cp_parser_omp_target_update (parser, pragma_tok, context);
31477 tree stmt = make_node (OMP_TARGET);
31478 TREE_TYPE (stmt) = void_type_node;
31480 OMP_TARGET_CLAUSES (stmt)
31481 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31482 "#pragma omp target", pragma_tok);
31483 keep_next_level (true);
31484 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31486 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31487 add_stmt (stmt);
31488 return true;
31491 /* OpenACC 2.0:
31492 # pragma acc cache (variable-list) new-line
31495 static tree
31496 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31498 tree stmt, clauses;
31500 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31501 clauses = finish_omp_clauses (clauses);
31503 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31505 stmt = make_node (OACC_CACHE);
31506 TREE_TYPE (stmt) = void_type_node;
31507 OACC_CACHE_CLAUSES (stmt) = clauses;
31508 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31509 add_stmt (stmt);
31511 return stmt;
31514 /* OpenACC 2.0:
31515 # pragma acc data oacc-data-clause[optseq] new-line
31516 structured-block */
31518 #define OACC_DATA_CLAUSE_MASK \
31519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31531 static tree
31532 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31534 tree stmt, clauses, block;
31535 unsigned int save;
31537 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31538 "#pragma acc data", pragma_tok);
31540 block = begin_omp_parallel ();
31541 save = cp_parser_begin_omp_structured_block (parser);
31542 cp_parser_statement (parser, NULL_TREE, false, NULL);
31543 cp_parser_end_omp_structured_block (parser, save);
31544 stmt = finish_oacc_data (clauses, block);
31545 return stmt;
31548 /* OpenACC 2.0:
31549 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31553 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31555 LOC is the location of the #pragma token.
31558 #define OACC_ENTER_DATA_CLAUSE_MASK \
31559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31567 #define OACC_EXIT_DATA_CLAUSE_MASK \
31568 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31574 static tree
31575 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31576 bool enter)
31578 tree stmt, clauses;
31580 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31581 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31583 cp_parser_error (parser, enter
31584 ? "expected %<data%> in %<#pragma acc enter data%>"
31585 : "expected %<data%> in %<#pragma acc exit data%>");
31586 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31587 return NULL_TREE;
31590 const char *p =
31591 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31592 if (strcmp (p, "data") != 0)
31594 cp_parser_error (parser, "invalid pragma");
31595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31596 return NULL_TREE;
31599 cp_lexer_consume_token (parser->lexer);
31601 if (enter)
31602 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31603 "#pragma acc enter data", pragma_tok);
31604 else
31605 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31606 "#pragma acc exit data", pragma_tok);
31608 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31610 error_at (pragma_tok->location,
31611 "%<#pragma acc enter data%> has no data movement clause");
31612 return NULL_TREE;
31615 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31616 TREE_TYPE (stmt) = void_type_node;
31617 if (enter)
31618 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31619 else
31620 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31621 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31622 add_stmt (stmt);
31623 return stmt;
31626 /* OpenACC 2.0:
31627 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31628 structured-block */
31630 #define OACC_KERNELS_CLAUSE_MASK \
31631 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31645 static tree
31646 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31648 tree stmt, clauses, block;
31649 unsigned int save;
31651 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31652 "#pragma acc kernels", pragma_tok);
31654 block = begin_omp_parallel ();
31655 save = cp_parser_begin_omp_structured_block (parser);
31656 cp_parser_statement (parser, NULL_TREE, false, NULL);
31657 cp_parser_end_omp_structured_block (parser, save);
31658 stmt = finish_oacc_kernels (clauses, block);
31659 return stmt;
31662 /* OpenACC 2.0:
31663 # pragma acc loop oacc-loop-clause[optseq] new-line
31664 structured-block */
31666 #define OACC_LOOP_CLAUSE_MASK \
31667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31670 static tree
31671 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31673 tree stmt, clauses, block;
31674 int save;
31676 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31677 "#pragma acc loop", pragma_tok);
31679 block = begin_omp_structured_block ();
31680 save = cp_parser_begin_omp_structured_block (parser);
31681 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31682 cp_parser_end_omp_structured_block (parser, save);
31683 add_stmt (finish_omp_structured_block (block));
31684 return stmt;
31687 /* OpenACC 2.0:
31688 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31689 structured-block */
31691 #define OACC_PARALLEL_CLAUSE_MASK \
31692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31710 static tree
31711 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31713 tree stmt, clauses, block;
31714 unsigned int save;
31716 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31717 "#pragma acc parallel", pragma_tok);
31719 block = begin_omp_parallel ();
31720 save = cp_parser_begin_omp_structured_block (parser);
31721 cp_parser_statement (parser, NULL_TREE, false, NULL);
31722 cp_parser_end_omp_structured_block (parser, save);
31723 stmt = finish_oacc_parallel (clauses, block);
31724 return stmt;
31727 /* OpenACC 2.0:
31728 # pragma acc update oacc-update-clause[optseq] new-line
31731 #define OACC_UPDATE_CLAUSE_MASK \
31732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31739 static tree
31740 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31742 tree stmt, clauses;
31744 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31745 "#pragma acc update", pragma_tok);
31747 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31749 error_at (pragma_tok->location,
31750 "%<#pragma acc update%> must contain at least one "
31751 "%<device%> or %<host/self%> clause");
31752 return NULL_TREE;
31755 stmt = make_node (OACC_UPDATE);
31756 TREE_TYPE (stmt) = void_type_node;
31757 OACC_UPDATE_CLAUSES (stmt) = clauses;
31758 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31759 add_stmt (stmt);
31760 return stmt;
31763 /* OpenACC 2.0:
31764 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31766 LOC is the location of the #pragma token.
31769 #define OACC_WAIT_CLAUSE_MASK \
31770 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31772 static tree
31773 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31775 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31776 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31778 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31779 list = cp_parser_oacc_wait_list (parser, loc, list);
31781 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31782 "#pragma acc wait", pragma_tok);
31784 stmt = c_finish_oacc_wait (loc, list, clauses);
31786 return stmt;
31789 /* OpenMP 4.0:
31790 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31792 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31800 static void
31801 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31802 enum pragma_context context)
31804 bool first_p = parser->omp_declare_simd == NULL;
31805 cp_omp_declare_simd_data data;
31806 if (first_p)
31808 data.error_seen = false;
31809 data.fndecl_seen = false;
31810 data.tokens = vNULL;
31811 parser->omp_declare_simd = &data;
31813 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31814 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31815 cp_lexer_consume_token (parser->lexer);
31816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31817 parser->omp_declare_simd->error_seen = true;
31818 cp_parser_require_pragma_eol (parser, pragma_tok);
31819 struct cp_token_cache *cp
31820 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31821 parser->omp_declare_simd->tokens.safe_push (cp);
31822 if (first_p)
31824 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31825 cp_parser_pragma (parser, context);
31826 switch (context)
31828 case pragma_external:
31829 cp_parser_declaration (parser);
31830 break;
31831 case pragma_member:
31832 cp_parser_member_declaration (parser);
31833 break;
31834 case pragma_objc_icode:
31835 cp_parser_block_declaration (parser, /*statement_p=*/false);
31836 break;
31837 default:
31838 cp_parser_declaration_statement (parser);
31839 break;
31841 if (parser->omp_declare_simd
31842 && !parser->omp_declare_simd->error_seen
31843 && !parser->omp_declare_simd->fndecl_seen)
31844 error_at (pragma_tok->location,
31845 "%<#pragma omp declare simd%> not immediately followed by "
31846 "function declaration or definition");
31847 data.tokens.release ();
31848 parser->omp_declare_simd = NULL;
31852 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31853 This function is modelled similar to the late parsing of omp declare
31854 simd. */
31856 static tree
31857 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31859 struct cp_token_cache *ce;
31860 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31861 int ii = 0;
31863 if (parser->omp_declare_simd != NULL)
31865 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31866 " marked as a Cilk Plus SIMD-enabled function");
31867 XDELETE (parser->cilk_simd_fn_info);
31868 parser->cilk_simd_fn_info = NULL;
31869 return attrs;
31871 if (!info->error_seen && info->fndecl_seen)
31873 error ("vector attribute not immediately followed by a single function"
31874 " declaration or definition");
31875 info->error_seen = true;
31877 if (info->error_seen)
31878 return attrs;
31880 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31882 tree c, cl;
31884 cp_parser_push_lexer_for_tokens (parser, ce);
31885 parser->lexer->in_pragma = true;
31886 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31887 "SIMD-enabled functions attribute",
31888 NULL);
31889 cp_parser_pop_lexer (parser);
31890 if (cl)
31891 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31893 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31894 TREE_CHAIN (c) = attrs;
31895 attrs = c;
31897 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31898 TREE_CHAIN (c) = attrs;
31899 if (processing_template_decl)
31900 ATTR_IS_DEPENDENT (c) = 1;
31901 attrs = c;
31903 info->fndecl_seen = true;
31904 XDELETE (parser->cilk_simd_fn_info);
31905 parser->cilk_simd_fn_info = NULL;
31906 return attrs;
31909 /* Finalize #pragma omp declare simd clauses after direct declarator has
31910 been parsed, and put that into "omp declare simd" attribute. */
31912 static tree
31913 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31915 struct cp_token_cache *ce;
31916 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31917 int i;
31919 if (!data->error_seen && data->fndecl_seen)
31921 error ("%<#pragma omp declare simd%> not immediately followed by "
31922 "a single function declaration or definition");
31923 data->error_seen = true;
31924 return attrs;
31926 if (data->error_seen)
31927 return attrs;
31929 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31931 tree c, cl;
31933 cp_parser_push_lexer_for_tokens (parser, ce);
31934 parser->lexer->in_pragma = true;
31935 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31936 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31937 cp_lexer_consume_token (parser->lexer);
31938 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31939 "#pragma omp declare simd", pragma_tok);
31940 cp_parser_pop_lexer (parser);
31941 if (cl)
31942 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31943 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31944 TREE_CHAIN (c) = attrs;
31945 if (processing_template_decl)
31946 ATTR_IS_DEPENDENT (c) = 1;
31947 attrs = c;
31950 data->fndecl_seen = true;
31951 return attrs;
31955 /* OpenMP 4.0:
31956 # pragma omp declare target new-line
31957 declarations and definitions
31958 # pragma omp end declare target new-line */
31960 static void
31961 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31964 scope_chain->omp_declare_target_attribute++;
31967 static void
31968 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31970 const char *p = "";
31971 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31973 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31974 p = IDENTIFIER_POINTER (id);
31976 if (strcmp (p, "declare") == 0)
31978 cp_lexer_consume_token (parser->lexer);
31979 p = "";
31980 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31983 p = IDENTIFIER_POINTER (id);
31985 if (strcmp (p, "target") == 0)
31986 cp_lexer_consume_token (parser->lexer);
31987 else
31989 cp_parser_error (parser, "expected %<target%>");
31990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31991 return;
31994 else
31996 cp_parser_error (parser, "expected %<declare%>");
31997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31998 return;
32000 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32001 if (!scope_chain->omp_declare_target_attribute)
32002 error_at (pragma_tok->location,
32003 "%<#pragma omp end declare target%> without corresponding "
32004 "%<#pragma omp declare target%>");
32005 else
32006 scope_chain->omp_declare_target_attribute--;
32009 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32010 expression and optional initializer clause of
32011 #pragma omp declare reduction. We store the expression(s) as
32012 either 3, 6 or 7 special statements inside of the artificial function's
32013 body. The first two statements are DECL_EXPRs for the artificial
32014 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32015 expression that uses those variables.
32016 If there was any INITIALIZER clause, this is followed by further statements,
32017 the fourth and fifth statements are DECL_EXPRs for the artificial
32018 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32019 constructor variant (first token after open paren is not omp_priv),
32020 then the sixth statement is a statement with the function call expression
32021 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32022 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32023 to initialize the OMP_PRIV artificial variable and there is seventh
32024 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32026 static bool
32027 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32029 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32030 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32031 type = TREE_TYPE (type);
32032 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32033 DECL_ARTIFICIAL (omp_out) = 1;
32034 pushdecl (omp_out);
32035 add_decl_expr (omp_out);
32036 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32037 DECL_ARTIFICIAL (omp_in) = 1;
32038 pushdecl (omp_in);
32039 add_decl_expr (omp_in);
32040 tree combiner;
32041 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32043 keep_next_level (true);
32044 tree block = begin_omp_structured_block ();
32045 combiner = cp_parser_expression (parser);
32046 finish_expr_stmt (combiner);
32047 block = finish_omp_structured_block (block);
32048 add_stmt (block);
32050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32051 return false;
32053 const char *p = "";
32054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32057 p = IDENTIFIER_POINTER (id);
32060 if (strcmp (p, "initializer") == 0)
32062 cp_lexer_consume_token (parser->lexer);
32063 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32064 return false;
32066 p = "";
32067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32069 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32070 p = IDENTIFIER_POINTER (id);
32073 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32074 DECL_ARTIFICIAL (omp_priv) = 1;
32075 pushdecl (omp_priv);
32076 add_decl_expr (omp_priv);
32077 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32078 DECL_ARTIFICIAL (omp_orig) = 1;
32079 pushdecl (omp_orig);
32080 add_decl_expr (omp_orig);
32082 keep_next_level (true);
32083 block = begin_omp_structured_block ();
32085 bool ctor = false;
32086 if (strcmp (p, "omp_priv") == 0)
32088 bool is_direct_init, is_non_constant_init;
32089 ctor = true;
32090 cp_lexer_consume_token (parser->lexer);
32091 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32092 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32093 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32094 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32095 == CPP_CLOSE_PAREN
32096 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32097 == CPP_CLOSE_PAREN))
32099 finish_omp_structured_block (block);
32100 error ("invalid initializer clause");
32101 return false;
32103 initializer = cp_parser_initializer (parser, &is_direct_init,
32104 &is_non_constant_init);
32105 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32106 NULL_TREE, LOOKUP_ONLYCONVERTING);
32108 else
32110 cp_parser_parse_tentatively (parser);
32111 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32112 /*check_dependency_p=*/true,
32113 /*template_p=*/NULL,
32114 /*declarator_p=*/false,
32115 /*optional_p=*/false);
32116 vec<tree, va_gc> *args;
32117 if (fn_name == error_mark_node
32118 || cp_parser_error_occurred (parser)
32119 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32120 || ((args = cp_parser_parenthesized_expression_list
32121 (parser, non_attr, /*cast_p=*/false,
32122 /*allow_expansion_p=*/true,
32123 /*non_constant_p=*/NULL)),
32124 cp_parser_error_occurred (parser)))
32126 finish_omp_structured_block (block);
32127 cp_parser_abort_tentative_parse (parser);
32128 cp_parser_error (parser, "expected id-expression (arguments)");
32129 return false;
32131 unsigned int i;
32132 tree arg;
32133 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32134 if (arg == omp_priv
32135 || (TREE_CODE (arg) == ADDR_EXPR
32136 && TREE_OPERAND (arg, 0) == omp_priv))
32137 break;
32138 cp_parser_abort_tentative_parse (parser);
32139 if (arg == NULL_TREE)
32140 error ("one of the initializer call arguments should be %<omp_priv%>"
32141 " or %<&omp_priv%>");
32142 initializer = cp_parser_postfix_expression (parser, false, false, false,
32143 false, NULL);
32144 finish_expr_stmt (initializer);
32147 block = finish_omp_structured_block (block);
32148 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32149 add_stmt (block);
32151 if (ctor)
32152 add_decl_expr (omp_orig);
32154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32155 return false;
32158 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32159 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32161 return true;
32164 /* OpenMP 4.0
32165 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32166 initializer-clause[opt] new-line
32168 initializer-clause:
32169 initializer (omp_priv initializer)
32170 initializer (function-name (argument-list)) */
32172 static void
32173 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32174 enum pragma_context)
32176 auto_vec<tree> types;
32177 enum tree_code reduc_code = ERROR_MARK;
32178 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32179 unsigned int i;
32180 cp_token *first_token;
32181 cp_token_cache *cp;
32182 int errs;
32183 void *p;
32185 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32186 p = obstack_alloc (&declarator_obstack, 0);
32188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32189 goto fail;
32191 switch (cp_lexer_peek_token (parser->lexer)->type)
32193 case CPP_PLUS:
32194 reduc_code = PLUS_EXPR;
32195 break;
32196 case CPP_MULT:
32197 reduc_code = MULT_EXPR;
32198 break;
32199 case CPP_MINUS:
32200 reduc_code = MINUS_EXPR;
32201 break;
32202 case CPP_AND:
32203 reduc_code = BIT_AND_EXPR;
32204 break;
32205 case CPP_XOR:
32206 reduc_code = BIT_XOR_EXPR;
32207 break;
32208 case CPP_OR:
32209 reduc_code = BIT_IOR_EXPR;
32210 break;
32211 case CPP_AND_AND:
32212 reduc_code = TRUTH_ANDIF_EXPR;
32213 break;
32214 case CPP_OR_OR:
32215 reduc_code = TRUTH_ORIF_EXPR;
32216 break;
32217 case CPP_NAME:
32218 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32219 break;
32220 default:
32221 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32222 "%<|%>, %<&&%>, %<||%> or identifier");
32223 goto fail;
32226 if (reduc_code != ERROR_MARK)
32227 cp_lexer_consume_token (parser->lexer);
32229 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32230 if (reduc_id == error_mark_node)
32231 goto fail;
32233 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32234 goto fail;
32236 /* Types may not be defined in declare reduction type list. */
32237 const char *saved_message;
32238 saved_message = parser->type_definition_forbidden_message;
32239 parser->type_definition_forbidden_message
32240 = G_("types may not be defined in declare reduction type list");
32241 bool saved_colon_corrects_to_scope_p;
32242 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32243 parser->colon_corrects_to_scope_p = false;
32244 bool saved_colon_doesnt_start_class_def_p;
32245 saved_colon_doesnt_start_class_def_p
32246 = parser->colon_doesnt_start_class_def_p;
32247 parser->colon_doesnt_start_class_def_p = true;
32249 while (true)
32251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32252 type = cp_parser_type_id (parser);
32253 if (type == error_mark_node)
32255 else if (ARITHMETIC_TYPE_P (type)
32256 && (orig_reduc_id == NULL_TREE
32257 || (TREE_CODE (type) != COMPLEX_TYPE
32258 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32259 "min") == 0
32260 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32261 "max") == 0))))
32262 error_at (loc, "predeclared arithmetic type %qT in "
32263 "%<#pragma omp declare reduction%>", type);
32264 else if (TREE_CODE (type) == FUNCTION_TYPE
32265 || TREE_CODE (type) == METHOD_TYPE
32266 || TREE_CODE (type) == ARRAY_TYPE)
32267 error_at (loc, "function or array type %qT in "
32268 "%<#pragma omp declare reduction%>", type);
32269 else if (TREE_CODE (type) == REFERENCE_TYPE)
32270 error_at (loc, "reference type %qT in "
32271 "%<#pragma omp declare reduction%>", type);
32272 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32273 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32274 "%<#pragma omp declare reduction%>", type);
32275 else
32276 types.safe_push (type);
32278 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32279 cp_lexer_consume_token (parser->lexer);
32280 else
32281 break;
32284 /* Restore the saved message. */
32285 parser->type_definition_forbidden_message = saved_message;
32286 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32287 parser->colon_doesnt_start_class_def_p
32288 = saved_colon_doesnt_start_class_def_p;
32290 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32291 || types.is_empty ())
32293 fail:
32294 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32295 goto done;
32298 first_token = cp_lexer_peek_token (parser->lexer);
32299 cp = NULL;
32300 errs = errorcount;
32301 FOR_EACH_VEC_ELT (types, i, type)
32303 tree fntype
32304 = build_function_type_list (void_type_node,
32305 cp_build_reference_type (type, false),
32306 NULL_TREE);
32307 tree this_reduc_id = reduc_id;
32308 if (!dependent_type_p (type))
32309 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32310 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32311 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32312 DECL_ARTIFICIAL (fndecl) = 1;
32313 DECL_EXTERNAL (fndecl) = 1;
32314 DECL_DECLARED_INLINE_P (fndecl) = 1;
32315 DECL_IGNORED_P (fndecl) = 1;
32316 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32317 DECL_ATTRIBUTES (fndecl)
32318 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32319 DECL_ATTRIBUTES (fndecl));
32320 if (processing_template_decl)
32321 fndecl = push_template_decl (fndecl);
32322 bool block_scope = false;
32323 tree block = NULL_TREE;
32324 if (current_function_decl)
32326 block_scope = true;
32327 DECL_CONTEXT (fndecl) = global_namespace;
32328 if (!processing_template_decl)
32329 pushdecl (fndecl);
32331 else if (current_class_type)
32333 if (cp == NULL)
32335 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32336 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32337 cp_lexer_consume_token (parser->lexer);
32338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32339 goto fail;
32340 cp = cp_token_cache_new (first_token,
32341 cp_lexer_peek_nth_token (parser->lexer,
32342 2));
32344 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32345 finish_member_declaration (fndecl);
32346 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32347 DECL_PENDING_INLINE_P (fndecl) = 1;
32348 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32349 continue;
32351 else
32353 DECL_CONTEXT (fndecl) = current_namespace;
32354 pushdecl (fndecl);
32356 if (!block_scope)
32357 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32358 else
32359 block = begin_omp_structured_block ();
32360 if (cp)
32362 cp_parser_push_lexer_for_tokens (parser, cp);
32363 parser->lexer->in_pragma = true;
32365 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32367 if (!block_scope)
32368 finish_function (0);
32369 else
32370 DECL_CONTEXT (fndecl) = current_function_decl;
32371 if (cp)
32372 cp_parser_pop_lexer (parser);
32373 goto fail;
32375 if (cp)
32376 cp_parser_pop_lexer (parser);
32377 if (!block_scope)
32378 finish_function (0);
32379 else
32381 DECL_CONTEXT (fndecl) = current_function_decl;
32382 block = finish_omp_structured_block (block);
32383 if (TREE_CODE (block) == BIND_EXPR)
32384 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32385 else if (TREE_CODE (block) == STATEMENT_LIST)
32386 DECL_SAVED_TREE (fndecl) = block;
32387 if (processing_template_decl)
32388 add_decl_expr (fndecl);
32390 cp_check_omp_declare_reduction (fndecl);
32391 if (cp == NULL && types.length () > 1)
32392 cp = cp_token_cache_new (first_token,
32393 cp_lexer_peek_nth_token (parser->lexer, 2));
32394 if (errs != errorcount)
32395 break;
32398 cp_parser_require_pragma_eol (parser, pragma_tok);
32400 done:
32401 /* Free any declarators allocated. */
32402 obstack_free (&declarator_obstack, p);
32405 /* OpenMP 4.0
32406 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32407 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32408 initializer-clause[opt] new-line
32409 #pragma omp declare target new-line */
32411 static void
32412 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32413 enum pragma_context context)
32415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32417 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32418 const char *p = IDENTIFIER_POINTER (id);
32420 if (strcmp (p, "simd") == 0)
32422 cp_lexer_consume_token (parser->lexer);
32423 cp_parser_omp_declare_simd (parser, pragma_tok,
32424 context);
32425 return;
32427 cp_ensure_no_omp_declare_simd (parser);
32428 if (strcmp (p, "reduction") == 0)
32430 cp_lexer_consume_token (parser->lexer);
32431 cp_parser_omp_declare_reduction (parser, pragma_tok,
32432 context);
32433 return;
32435 if (!flag_openmp) /* flag_openmp_simd */
32437 cp_parser_require_pragma_eol (parser, pragma_tok);
32438 return;
32440 if (strcmp (p, "target") == 0)
32442 cp_lexer_consume_token (parser->lexer);
32443 cp_parser_omp_declare_target (parser, pragma_tok);
32444 return;
32447 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32448 "or %<target%>");
32449 cp_parser_require_pragma_eol (parser, pragma_tok);
32452 /* Main entry point to OpenMP statement pragmas. */
32454 static void
32455 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32457 tree stmt;
32458 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32459 omp_clause_mask mask (0);
32461 switch (pragma_tok->pragma_kind)
32463 case PRAGMA_OACC_CACHE:
32464 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32465 break;
32466 case PRAGMA_OACC_DATA:
32467 stmt = cp_parser_oacc_data (parser, pragma_tok);
32468 break;
32469 case PRAGMA_OACC_ENTER_DATA:
32470 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32471 break;
32472 case PRAGMA_OACC_EXIT_DATA:
32473 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32474 break;
32475 case PRAGMA_OACC_KERNELS:
32476 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32477 break;
32478 case PRAGMA_OACC_LOOP:
32479 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32480 break;
32481 case PRAGMA_OACC_PARALLEL:
32482 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32483 break;
32484 case PRAGMA_OACC_UPDATE:
32485 stmt = cp_parser_oacc_update (parser, pragma_tok);
32486 break;
32487 case PRAGMA_OACC_WAIT:
32488 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32489 break;
32490 case PRAGMA_OMP_ATOMIC:
32491 cp_parser_omp_atomic (parser, pragma_tok);
32492 return;
32493 case PRAGMA_OMP_CRITICAL:
32494 stmt = cp_parser_omp_critical (parser, pragma_tok);
32495 break;
32496 case PRAGMA_OMP_DISTRIBUTE:
32497 strcpy (p_name, "#pragma omp");
32498 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32499 break;
32500 case PRAGMA_OMP_FOR:
32501 strcpy (p_name, "#pragma omp");
32502 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32503 break;
32504 case PRAGMA_OMP_MASTER:
32505 stmt = cp_parser_omp_master (parser, pragma_tok);
32506 break;
32507 case PRAGMA_OMP_ORDERED:
32508 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32509 break;
32510 case PRAGMA_OMP_PARALLEL:
32511 strcpy (p_name, "#pragma omp");
32512 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32513 break;
32514 case PRAGMA_OMP_SECTIONS:
32515 strcpy (p_name, "#pragma omp");
32516 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32517 break;
32518 case PRAGMA_OMP_SIMD:
32519 strcpy (p_name, "#pragma omp");
32520 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32521 break;
32522 case PRAGMA_OMP_SINGLE:
32523 stmt = cp_parser_omp_single (parser, pragma_tok);
32524 break;
32525 case PRAGMA_OMP_TASK:
32526 stmt = cp_parser_omp_task (parser, pragma_tok);
32527 break;
32528 case PRAGMA_OMP_TASKGROUP:
32529 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32530 break;
32531 case PRAGMA_OMP_TEAMS:
32532 strcpy (p_name, "#pragma omp");
32533 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32534 break;
32535 default:
32536 gcc_unreachable ();
32539 if (stmt)
32540 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32543 /* Transactional Memory parsing routines. */
32545 /* Parse a transaction attribute.
32547 txn-attribute:
32548 attribute
32549 [ [ identifier ] ]
32551 ??? Simplify this when C++0x bracket attributes are
32552 implemented properly. */
32554 static tree
32555 cp_parser_txn_attribute_opt (cp_parser *parser)
32557 cp_token *token;
32558 tree attr_name, attr = NULL;
32560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32561 return cp_parser_attributes_opt (parser);
32563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32564 return NULL_TREE;
32565 cp_lexer_consume_token (parser->lexer);
32566 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32567 goto error1;
32569 token = cp_lexer_peek_token (parser->lexer);
32570 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32572 token = cp_lexer_consume_token (parser->lexer);
32574 attr_name = (token->type == CPP_KEYWORD
32575 /* For keywords, use the canonical spelling,
32576 not the parsed identifier. */
32577 ? ridpointers[(int) token->keyword]
32578 : token->u.value);
32579 attr = build_tree_list (attr_name, NULL_TREE);
32581 else
32582 cp_parser_error (parser, "expected identifier");
32584 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32585 error1:
32586 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32587 return attr;
32590 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32592 transaction-statement:
32593 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32594 compound-statement
32595 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32598 static tree
32599 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32601 unsigned char old_in = parser->in_transaction;
32602 unsigned char this_in = 1, new_in;
32603 cp_token *token;
32604 tree stmt, attrs, noex;
32606 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32607 || keyword == RID_TRANSACTION_RELAXED);
32608 token = cp_parser_require_keyword (parser, keyword,
32609 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32610 : RT_TRANSACTION_RELAXED));
32611 gcc_assert (token != NULL);
32613 if (keyword == RID_TRANSACTION_RELAXED)
32614 this_in |= TM_STMT_ATTR_RELAXED;
32615 else
32617 attrs = cp_parser_txn_attribute_opt (parser);
32618 if (attrs)
32619 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32622 /* Parse a noexcept specification. */
32623 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32625 /* Keep track if we're in the lexical scope of an outer transaction. */
32626 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32628 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32630 parser->in_transaction = new_in;
32631 cp_parser_compound_statement (parser, NULL, false, false);
32632 parser->in_transaction = old_in;
32634 finish_transaction_stmt (stmt, NULL, this_in, noex);
32636 return stmt;
32639 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32641 transaction-expression:
32642 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32643 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32646 static tree
32647 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32649 unsigned char old_in = parser->in_transaction;
32650 unsigned char this_in = 1;
32651 cp_token *token;
32652 tree expr, noex;
32653 bool noex_expr;
32655 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32656 || keyword == RID_TRANSACTION_RELAXED);
32658 if (!flag_tm)
32659 error (keyword == RID_TRANSACTION_RELAXED
32660 ? G_("%<__transaction_relaxed%> without transactional memory "
32661 "support enabled")
32662 : G_("%<__transaction_atomic%> without transactional memory "
32663 "support enabled"));
32665 token = cp_parser_require_keyword (parser, keyword,
32666 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32667 : RT_TRANSACTION_RELAXED));
32668 gcc_assert (token != NULL);
32670 if (keyword == RID_TRANSACTION_RELAXED)
32671 this_in |= TM_STMT_ATTR_RELAXED;
32673 /* Set this early. This might mean that we allow transaction_cancel in
32674 an expression that we find out later actually has to be a constexpr.
32675 However, we expect that cxx_constant_value will be able to deal with
32676 this; also, if the noexcept has no constexpr, then what we parse next
32677 really is a transaction's body. */
32678 parser->in_transaction = this_in;
32680 /* Parse a noexcept specification. */
32681 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32682 true);
32684 if (!noex || !noex_expr
32685 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32687 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32689 expr = cp_parser_expression (parser);
32690 expr = finish_parenthesized_expr (expr);
32692 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32694 else
32696 /* The only expression that is available got parsed for the noexcept
32697 already. noexcept is true then. */
32698 expr = noex;
32699 noex = boolean_true_node;
32702 expr = build_transaction_expr (token->location, expr, this_in, noex);
32703 parser->in_transaction = old_in;
32705 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32706 return error_mark_node;
32708 return (flag_tm ? expr : error_mark_node);
32711 /* Parse a function-transaction-block.
32713 function-transaction-block:
32714 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32715 function-body
32716 __transaction_atomic txn-attribute[opt] function-try-block
32717 __transaction_relaxed ctor-initializer[opt] function-body
32718 __transaction_relaxed function-try-block
32721 static bool
32722 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32724 unsigned char old_in = parser->in_transaction;
32725 unsigned char new_in = 1;
32726 tree compound_stmt, stmt, attrs;
32727 bool ctor_initializer_p;
32728 cp_token *token;
32730 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32731 || keyword == RID_TRANSACTION_RELAXED);
32732 token = cp_parser_require_keyword (parser, keyword,
32733 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32734 : RT_TRANSACTION_RELAXED));
32735 gcc_assert (token != NULL);
32737 if (keyword == RID_TRANSACTION_RELAXED)
32738 new_in |= TM_STMT_ATTR_RELAXED;
32739 else
32741 attrs = cp_parser_txn_attribute_opt (parser);
32742 if (attrs)
32743 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32746 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32748 parser->in_transaction = new_in;
32750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32751 ctor_initializer_p = cp_parser_function_try_block (parser);
32752 else
32753 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32754 (parser, /*in_function_try_block=*/false);
32756 parser->in_transaction = old_in;
32758 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32760 return ctor_initializer_p;
32763 /* Parse a __transaction_cancel statement.
32765 cancel-statement:
32766 __transaction_cancel txn-attribute[opt] ;
32767 __transaction_cancel txn-attribute[opt] throw-expression ;
32769 ??? Cancel and throw is not yet implemented. */
32771 static tree
32772 cp_parser_transaction_cancel (cp_parser *parser)
32774 cp_token *token;
32775 bool is_outer = false;
32776 tree stmt, attrs;
32778 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32779 RT_TRANSACTION_CANCEL);
32780 gcc_assert (token != NULL);
32782 attrs = cp_parser_txn_attribute_opt (parser);
32783 if (attrs)
32784 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32786 /* ??? Parse cancel-and-throw here. */
32788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32790 if (!flag_tm)
32792 error_at (token->location, "%<__transaction_cancel%> without "
32793 "transactional memory support enabled");
32794 return error_mark_node;
32796 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32798 error_at (token->location, "%<__transaction_cancel%> within a "
32799 "%<__transaction_relaxed%>");
32800 return error_mark_node;
32802 else if (is_outer)
32804 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32805 && !is_tm_may_cancel_outer (current_function_decl))
32807 error_at (token->location, "outer %<__transaction_cancel%> not "
32808 "within outer %<__transaction_atomic%>");
32809 error_at (token->location,
32810 " or a %<transaction_may_cancel_outer%> function");
32811 return error_mark_node;
32814 else if (parser->in_transaction == 0)
32816 error_at (token->location, "%<__transaction_cancel%> not within "
32817 "%<__transaction_atomic%>");
32818 return error_mark_node;
32821 stmt = build_tm_abort_call (token->location, is_outer);
32822 add_stmt (stmt);
32824 return stmt;
32827 /* The parser. */
32829 static GTY (()) cp_parser *the_parser;
32832 /* Special handling for the first token or line in the file. The first
32833 thing in the file might be #pragma GCC pch_preprocess, which loads a
32834 PCH file, which is a GC collection point. So we need to handle this
32835 first pragma without benefit of an existing lexer structure.
32837 Always returns one token to the caller in *FIRST_TOKEN. This is
32838 either the true first token of the file, or the first token after
32839 the initial pragma. */
32841 static void
32842 cp_parser_initial_pragma (cp_token *first_token)
32844 tree name = NULL;
32846 cp_lexer_get_preprocessor_token (NULL, first_token);
32847 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32848 return;
32850 cp_lexer_get_preprocessor_token (NULL, first_token);
32851 if (first_token->type == CPP_STRING)
32853 name = first_token->u.value;
32855 cp_lexer_get_preprocessor_token (NULL, first_token);
32856 if (first_token->type != CPP_PRAGMA_EOL)
32857 error_at (first_token->location,
32858 "junk at end of %<#pragma GCC pch_preprocess%>");
32860 else
32861 error_at (first_token->location, "expected string literal");
32863 /* Skip to the end of the pragma. */
32864 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32865 cp_lexer_get_preprocessor_token (NULL, first_token);
32867 /* Now actually load the PCH file. */
32868 if (name)
32869 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32871 /* Read one more token to return to our caller. We have to do this
32872 after reading the PCH file in, since its pointers have to be
32873 live. */
32874 cp_lexer_get_preprocessor_token (NULL, first_token);
32877 /* Parses the grainsize pragma for the _Cilk_for statement.
32878 Syntax:
32879 #pragma cilk grainsize = <VALUE>. */
32881 static void
32882 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32884 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32886 tree exp = cp_parser_binary_expression (parser, false, false,
32887 PREC_NOT_OPERATOR, NULL);
32888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32889 if (!exp || exp == error_mark_node)
32891 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32892 return;
32895 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32897 cp_parser_cilk_for (parser, exp);
32898 else
32899 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32900 "%<#pragma cilk grainsize%> is not followed by "
32901 "%<_Cilk_for%>");
32902 return;
32904 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32907 /* Normal parsing of a pragma token. Here we can (and must) use the
32908 regular lexer. */
32910 static bool
32911 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32913 cp_token *pragma_tok;
32914 unsigned int id;
32916 pragma_tok = cp_lexer_consume_token (parser->lexer);
32917 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32918 parser->lexer->in_pragma = true;
32920 id = pragma_tok->pragma_kind;
32921 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32922 cp_ensure_no_omp_declare_simd (parser);
32923 switch (id)
32925 case PRAGMA_GCC_PCH_PREPROCESS:
32926 error_at (pragma_tok->location,
32927 "%<#pragma GCC pch_preprocess%> must be first");
32928 break;
32930 case PRAGMA_OMP_BARRIER:
32931 switch (context)
32933 case pragma_compound:
32934 cp_parser_omp_barrier (parser, pragma_tok);
32935 return false;
32936 case pragma_stmt:
32937 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32938 "used in compound statements");
32939 break;
32940 default:
32941 goto bad_stmt;
32943 break;
32945 case PRAGMA_OMP_FLUSH:
32946 switch (context)
32948 case pragma_compound:
32949 cp_parser_omp_flush (parser, pragma_tok);
32950 return false;
32951 case pragma_stmt:
32952 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32953 "used in compound statements");
32954 break;
32955 default:
32956 goto bad_stmt;
32958 break;
32960 case PRAGMA_OMP_TASKWAIT:
32961 switch (context)
32963 case pragma_compound:
32964 cp_parser_omp_taskwait (parser, pragma_tok);
32965 return false;
32966 case pragma_stmt:
32967 error_at (pragma_tok->location,
32968 "%<#pragma omp taskwait%> may only be "
32969 "used in compound statements");
32970 break;
32971 default:
32972 goto bad_stmt;
32974 break;
32976 case PRAGMA_OMP_TASKYIELD:
32977 switch (context)
32979 case pragma_compound:
32980 cp_parser_omp_taskyield (parser, pragma_tok);
32981 return false;
32982 case pragma_stmt:
32983 error_at (pragma_tok->location,
32984 "%<#pragma omp taskyield%> may only be "
32985 "used in compound statements");
32986 break;
32987 default:
32988 goto bad_stmt;
32990 break;
32992 case PRAGMA_OMP_CANCEL:
32993 switch (context)
32995 case pragma_compound:
32996 cp_parser_omp_cancel (parser, pragma_tok);
32997 return false;
32998 case pragma_stmt:
32999 error_at (pragma_tok->location,
33000 "%<#pragma omp cancel%> may only be "
33001 "used in compound statements");
33002 break;
33003 default:
33004 goto bad_stmt;
33006 break;
33008 case PRAGMA_OMP_CANCELLATION_POINT:
33009 switch (context)
33011 case pragma_compound:
33012 cp_parser_omp_cancellation_point (parser, pragma_tok);
33013 return false;
33014 case pragma_stmt:
33015 error_at (pragma_tok->location,
33016 "%<#pragma omp cancellation point%> may only be "
33017 "used in compound statements");
33018 break;
33019 default:
33020 goto bad_stmt;
33022 break;
33024 case PRAGMA_OMP_THREADPRIVATE:
33025 cp_parser_omp_threadprivate (parser, pragma_tok);
33026 return false;
33028 case PRAGMA_OMP_DECLARE_REDUCTION:
33029 cp_parser_omp_declare (parser, pragma_tok, context);
33030 return false;
33032 case PRAGMA_OACC_CACHE:
33033 case PRAGMA_OACC_DATA:
33034 case PRAGMA_OACC_ENTER_DATA:
33035 case PRAGMA_OACC_EXIT_DATA:
33036 case PRAGMA_OACC_KERNELS:
33037 case PRAGMA_OACC_PARALLEL:
33038 case PRAGMA_OACC_LOOP:
33039 case PRAGMA_OACC_UPDATE:
33040 case PRAGMA_OACC_WAIT:
33041 case PRAGMA_OMP_ATOMIC:
33042 case PRAGMA_OMP_CRITICAL:
33043 case PRAGMA_OMP_DISTRIBUTE:
33044 case PRAGMA_OMP_FOR:
33045 case PRAGMA_OMP_MASTER:
33046 case PRAGMA_OMP_ORDERED:
33047 case PRAGMA_OMP_PARALLEL:
33048 case PRAGMA_OMP_SECTIONS:
33049 case PRAGMA_OMP_SIMD:
33050 case PRAGMA_OMP_SINGLE:
33051 case PRAGMA_OMP_TASK:
33052 case PRAGMA_OMP_TASKGROUP:
33053 case PRAGMA_OMP_TEAMS:
33054 if (context != pragma_stmt && context != pragma_compound)
33055 goto bad_stmt;
33056 cp_parser_omp_construct (parser, pragma_tok);
33057 return true;
33059 case PRAGMA_OMP_TARGET:
33060 return cp_parser_omp_target (parser, pragma_tok, context);
33062 case PRAGMA_OMP_END_DECLARE_TARGET:
33063 cp_parser_omp_end_declare_target (parser, pragma_tok);
33064 return false;
33066 case PRAGMA_OMP_SECTION:
33067 error_at (pragma_tok->location,
33068 "%<#pragma omp section%> may only be used in "
33069 "%<#pragma omp sections%> construct");
33070 break;
33072 case PRAGMA_IVDEP:
33074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33075 cp_token *tok;
33076 tok = cp_lexer_peek_token (the_parser->lexer);
33077 if (tok->type != CPP_KEYWORD
33078 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33079 && tok->keyword != RID_DO))
33081 cp_parser_error (parser, "for, while or do statement expected");
33082 return false;
33084 cp_parser_iteration_statement (parser, true);
33085 return true;
33088 case PRAGMA_CILK_SIMD:
33089 if (context == pragma_external)
33091 error_at (pragma_tok->location,
33092 "%<#pragma simd%> must be inside a function");
33093 break;
33095 cp_parser_cilk_simd (parser, pragma_tok);
33096 return true;
33098 case PRAGMA_CILK_GRAINSIZE:
33099 if (context == pragma_external)
33101 error_at (pragma_tok->location,
33102 "%<#pragma cilk grainsize%> must be inside a function");
33103 break;
33106 /* Ignore the pragma if Cilk Plus is not enabled. */
33107 if (flag_cilkplus)
33109 cp_parser_cilk_grainsize (parser, pragma_tok);
33110 return true;
33112 else
33114 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33115 "%<#pragma cilk grainsize%>");
33116 break;
33119 default:
33120 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33121 c_invoke_pragma_handler (id);
33122 break;
33124 bad_stmt:
33125 cp_parser_error (parser, "expected declaration specifiers");
33126 break;
33129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33130 return false;
33133 /* The interface the pragma parsers have to the lexer. */
33135 enum cpp_ttype
33136 pragma_lex (tree *value)
33138 cp_token *tok;
33139 enum cpp_ttype ret;
33141 tok = cp_lexer_peek_token (the_parser->lexer);
33143 ret = tok->type;
33144 *value = tok->u.value;
33146 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33147 ret = CPP_EOF;
33148 else if (ret == CPP_STRING)
33149 *value = cp_parser_string_literal (the_parser, false, false);
33150 else
33152 cp_lexer_consume_token (the_parser->lexer);
33153 if (ret == CPP_KEYWORD)
33154 ret = CPP_NAME;
33157 return ret;
33161 /* External interface. */
33163 /* Parse one entire translation unit. */
33165 void
33166 c_parse_file (void)
33168 static bool already_called = false;
33170 if (already_called)
33171 fatal_error ("inter-module optimizations not implemented for C++");
33172 already_called = true;
33174 the_parser = cp_parser_new ();
33175 push_deferring_access_checks (flag_access_control
33176 ? dk_no_deferred : dk_no_check);
33177 cp_parser_translation_unit (the_parser);
33178 the_parser = NULL;
33181 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33182 vectorlength clause:
33183 Syntax:
33184 vectorlength ( constant-expression ) */
33186 static tree
33187 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33188 bool is_simd_fn)
33190 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33191 tree expr;
33192 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33193 safelen clause. Thus, vectorlength is represented as OMP 4.0
33194 safelen. For SIMD-enabled function it is represented by OMP 4.0
33195 simdlen. */
33196 if (!is_simd_fn)
33197 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33198 loc);
33199 else
33200 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33201 loc);
33203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33204 return error_mark_node;
33206 expr = cp_parser_constant_expression (parser);
33207 expr = maybe_constant_value (expr);
33209 /* If expr == error_mark_node, then don't emit any errors nor
33210 create a clause. if any of the above functions returns
33211 error mark node then they would have emitted an error message. */
33212 if (expr == error_mark_node)
33214 else if (!TREE_TYPE (expr)
33215 || !TREE_CONSTANT (expr)
33216 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33217 error_at (loc, "vectorlength must be an integer constant");
33218 else if (TREE_CONSTANT (expr)
33219 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33220 error_at (loc, "vectorlength must be a power of 2");
33221 else
33223 tree c;
33224 if (!is_simd_fn)
33226 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33227 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33228 OMP_CLAUSE_CHAIN (c) = clauses;
33229 clauses = c;
33231 else
33233 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33234 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33235 OMP_CLAUSE_CHAIN (c) = clauses;
33236 clauses = c;
33240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33241 return error_mark_node;
33242 return clauses;
33245 /* Handles the Cilk Plus #pragma simd linear clause.
33246 Syntax:
33247 linear ( simd-linear-variable-list )
33249 simd-linear-variable-list:
33250 simd-linear-variable
33251 simd-linear-variable-list , simd-linear-variable
33253 simd-linear-variable:
33254 id-expression
33255 id-expression : simd-linear-step
33257 simd-linear-step:
33258 conditional-expression */
33260 static tree
33261 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33263 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33266 return clauses;
33267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33269 cp_parser_error (parser, "expected identifier");
33270 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33271 return error_mark_node;
33274 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33275 parser->colon_corrects_to_scope_p = false;
33276 while (1)
33278 cp_token *token = cp_lexer_peek_token (parser->lexer);
33279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33281 cp_parser_error (parser, "expected variable-name");
33282 clauses = error_mark_node;
33283 break;
33286 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33287 false, false);
33288 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33289 token->location);
33290 if (decl == error_mark_node)
33292 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33293 token->location);
33294 clauses = error_mark_node;
33296 else
33298 tree e = NULL_TREE;
33299 tree step_size = integer_one_node;
33301 /* If present, parse the linear step. Otherwise, assume the default
33302 value of 1. */
33303 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33305 cp_lexer_consume_token (parser->lexer);
33307 e = cp_parser_assignment_expression (parser);
33308 e = maybe_constant_value (e);
33310 if (e == error_mark_node)
33312 /* If an error has occurred, then the whole pragma is
33313 considered ill-formed. Thus, no reason to keep
33314 parsing. */
33315 clauses = error_mark_node;
33316 break;
33318 else if (type_dependent_expression_p (e)
33319 || value_dependent_expression_p (e)
33320 || (TREE_TYPE (e)
33321 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33322 && (TREE_CONSTANT (e)
33323 || DECL_P (e))))
33324 step_size = e;
33325 else
33326 cp_parser_error (parser,
33327 "step size must be an integer constant "
33328 "expression or an integer variable");
33331 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33332 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33333 OMP_CLAUSE_DECL (l) = decl;
33334 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33335 OMP_CLAUSE_CHAIN (l) = clauses;
33336 clauses = l;
33338 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33339 cp_lexer_consume_token (parser->lexer);
33340 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33341 break;
33342 else
33344 error_at (cp_lexer_peek_token (parser->lexer)->location,
33345 "expected %<,%> or %<)%> after %qE", decl);
33346 clauses = error_mark_node;
33347 break;
33350 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33351 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33352 return clauses;
33355 /* Returns the name of the next clause. If the clause is not
33356 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33357 token is not consumed. Otherwise, the appropriate enum from the
33358 pragma_simd_clause is returned and the token is consumed. */
33360 static pragma_omp_clause
33361 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33363 pragma_omp_clause clause_type;
33364 cp_token *token = cp_lexer_peek_token (parser->lexer);
33366 if (token->keyword == RID_PRIVATE)
33367 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33368 else if (!token->u.value || token->type != CPP_NAME)
33369 return PRAGMA_CILK_CLAUSE_NONE;
33370 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33371 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33372 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33373 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33374 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33375 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33376 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33377 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33378 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33379 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33380 else
33381 return PRAGMA_CILK_CLAUSE_NONE;
33383 cp_lexer_consume_token (parser->lexer);
33384 return clause_type;
33387 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33389 static tree
33390 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33392 tree clauses = NULL_TREE;
33394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33395 && clauses != error_mark_node)
33397 pragma_omp_clause c_kind;
33398 c_kind = cp_parser_cilk_simd_clause_name (parser);
33399 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33400 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33401 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33402 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33403 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33404 /* Use the OpenMP 4.0 equivalent function. */
33405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33406 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33407 /* Use the OpenMP 4.0 equivalent function. */
33408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33409 clauses);
33410 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33411 /* Use the OMP 4.0 equivalent function. */
33412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33413 clauses);
33414 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33415 /* Use the OMP 4.0 equivalent function. */
33416 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33417 else
33419 clauses = error_mark_node;
33420 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33421 break;
33425 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33427 if (clauses == error_mark_node)
33428 return error_mark_node;
33429 else
33430 return c_finish_cilk_clauses (clauses);
33433 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33435 static void
33436 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33438 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33440 if (clauses == error_mark_node)
33441 return;
33443 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33445 error_at (cp_lexer_peek_token (parser->lexer)->location,
33446 "for statement expected");
33447 return;
33450 tree sb = begin_omp_structured_block ();
33451 int save = cp_parser_begin_omp_structured_block (parser);
33452 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33453 if (ret)
33454 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33455 cp_parser_end_omp_structured_block (parser, save);
33456 add_stmt (finish_omp_structured_block (sb));
33459 /* Main entry-point for parsing Cilk Plus _Cilk_for
33460 loops. The return value is error_mark_node
33461 when errors happen and CILK_FOR tree on success. */
33463 static tree
33464 cp_parser_cilk_for (cp_parser *parser, tree grain)
33466 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33467 gcc_unreachable ();
33469 tree sb = begin_omp_structured_block ();
33470 int save = cp_parser_begin_omp_structured_block (parser);
33472 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33473 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33474 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33475 clauses = finish_omp_clauses (clauses);
33477 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33478 if (ret)
33479 cpp_validate_cilk_plus_loop (ret);
33480 else
33481 ret = error_mark_node;
33483 cp_parser_end_omp_structured_block (parser, save);
33484 add_stmt (finish_omp_structured_block (sb));
33485 return ret;
33488 /* Create an identifier for a generic parameter type (a synthesized
33489 template parameter implied by `auto' or a concept identifier). */
33491 static GTY(()) int generic_parm_count;
33492 static tree
33493 make_generic_type_name ()
33495 char buf[32];
33496 sprintf (buf, "auto:%d", ++generic_parm_count);
33497 return get_identifier (buf);
33500 /* Predicate that behaves as is_auto_or_concept but matches the parent
33501 node of the generic type rather than the generic type itself. This
33502 allows for type transformation in add_implicit_template_parms. */
33504 static inline bool
33505 tree_type_is_auto_or_concept (const_tree t)
33507 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33510 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33511 (creating a new template parameter list if necessary). Returns the newly
33512 created template type parm. */
33514 tree
33515 synthesize_implicit_template_parm (cp_parser *parser)
33517 gcc_assert (current_binding_level->kind == sk_function_parms);
33519 /* We are either continuing a function template that already contains implicit
33520 template parameters, creating a new fully-implicit function template, or
33521 extending an existing explicit function template with implicit template
33522 parameters. */
33524 cp_binding_level *const entry_scope = current_binding_level;
33526 bool become_template = false;
33527 cp_binding_level *parent_scope = 0;
33529 if (parser->implicit_template_scope)
33531 gcc_assert (parser->implicit_template_parms);
33533 current_binding_level = parser->implicit_template_scope;
33535 else
33537 /* Roll back to the existing template parameter scope (in the case of
33538 extending an explicit function template) or introduce a new template
33539 parameter scope ahead of the function parameter scope (or class scope
33540 in the case of out-of-line member definitions). The function scope is
33541 added back after template parameter synthesis below. */
33543 cp_binding_level *scope = entry_scope;
33545 while (scope->kind == sk_function_parms)
33547 parent_scope = scope;
33548 scope = scope->level_chain;
33550 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33552 /* If not defining a class, then any class scope is a scope level in
33553 an out-of-line member definition. In this case simply wind back
33554 beyond the first such scope to inject the template parameter list.
33555 Otherwise wind back to the class being defined. The latter can
33556 occur in class member friend declarations such as:
33558 class A {
33559 void foo (auto);
33561 class B {
33562 friend void A::foo (auto);
33565 The template parameter list synthesized for the friend declaration
33566 must be injected in the scope of 'B'. This can also occur in
33567 erroneous cases such as:
33569 struct A {
33570 struct B {
33571 void foo (auto);
33573 void B::foo (auto) {}
33576 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33577 but, nevertheless, the template parameter list synthesized for the
33578 declarator should be injected into the scope of 'A' as if the
33579 ill-formed template was specified explicitly. */
33581 while (scope->kind == sk_class && !scope->defining_class_p)
33583 parent_scope = scope;
33584 scope = scope->level_chain;
33588 current_binding_level = scope;
33590 if (scope->kind != sk_template_parms
33591 || !function_being_declared_is_template_p (parser))
33593 /* Introduce a new template parameter list for implicit template
33594 parameters. */
33596 become_template = true;
33598 parser->implicit_template_scope
33599 = begin_scope (sk_template_parms, NULL);
33601 ++processing_template_decl;
33603 parser->fully_implicit_function_template_p = true;
33604 ++parser->num_template_parameter_lists;
33606 else
33608 /* Synthesize implicit template parameters at the end of the explicit
33609 template parameter list. */
33611 gcc_assert (current_template_parms);
33613 parser->implicit_template_scope = scope;
33615 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33616 parser->implicit_template_parms
33617 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33621 /* Synthesize a new template parameter and track the current template
33622 parameter chain with implicit_template_parms. */
33624 tree synth_id = make_generic_type_name ();
33625 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33626 synth_id);
33627 tree new_parm
33628 = process_template_parm (parser->implicit_template_parms,
33629 input_location,
33630 build_tree_list (NULL_TREE, synth_tmpl_parm),
33631 /*non_type=*/false,
33632 /*param_pack=*/false);
33635 if (parser->implicit_template_parms)
33636 parser->implicit_template_parms
33637 = TREE_CHAIN (parser->implicit_template_parms);
33638 else
33639 parser->implicit_template_parms = new_parm;
33641 tree new_type = TREE_TYPE (getdecls ());
33643 /* If creating a fully implicit function template, start the new implicit
33644 template parameter list with this synthesized type, otherwise grow the
33645 current template parameter list. */
33647 if (become_template)
33649 parent_scope->level_chain = current_binding_level;
33651 tree new_parms = make_tree_vec (1);
33652 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33653 current_template_parms = tree_cons (size_int (processing_template_decl),
33654 new_parms, current_template_parms);
33656 else
33658 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33659 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33660 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33661 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33664 current_binding_level = entry_scope;
33666 return new_type;
33669 /* Finish the declaration of a fully implicit function template. Such a
33670 template has no explicit template parameter list so has not been through the
33671 normal template head and tail processing. synthesize_implicit_template_parm
33672 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33673 provided if the declaration is a class member such that its template
33674 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33675 form is returned. Otherwise NULL_TREE is returned. */
33677 tree
33678 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33680 gcc_assert (parser->fully_implicit_function_template_p);
33682 if (member_decl_opt && member_decl_opt != error_mark_node
33683 && DECL_VIRTUAL_P (member_decl_opt))
33685 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33686 "implicit templates may not be %<virtual%>");
33687 DECL_VIRTUAL_P (member_decl_opt) = false;
33690 if (member_decl_opt)
33691 member_decl_opt = finish_member_template_decl (member_decl_opt);
33692 end_template_decl ();
33694 parser->fully_implicit_function_template_p = false;
33695 --parser->num_template_parameter_lists;
33697 return member_decl_opt;
33700 #include "gt-cp-parser.h"