Merged from trunk from revision r222717 up to r222905.
[official-gcc.git] / gcc / cp / parser.c
blob987a903c194c3620803345f84d388b3196c8661c
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, bool = false);
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 (DECL_TYPE_TEMPLATE_P (decl))
2962 error_at (location,
2963 "invalid use of template-name %qE without an argument list",
2964 decl);
2965 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2967 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2968 error_at (location, "invalid use of destructor %qD as a type", id);
2969 else if (TREE_CODE (decl) == TYPE_DECL)
2970 /* Something like 'unsigned A a;' */
2971 error_at (location, "invalid combination of multiple type-specifiers");
2972 else if (!parser->scope)
2974 /* Issue an error message. */
2975 error_at (location, "%qE does not name a type", id);
2976 /* If we're in a template class, it's possible that the user was
2977 referring to a type from a base class. For example:
2979 template <typename T> struct A { typedef T X; };
2980 template <typename T> struct B : public A<T> { X x; };
2982 The user should have said "typename A<T>::X". */
2983 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2984 inform (location, "C++11 %<constexpr%> only available with "
2985 "-std=c++11 or -std=gnu++11");
2986 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2987 inform (location, "C++11 %<noexcept%> only available with "
2988 "-std=c++11 or -std=gnu++11");
2989 else if (cxx_dialect < cxx11
2990 && TREE_CODE (id) == IDENTIFIER_NODE
2991 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2992 inform (location, "C++11 %<thread_local%> only available with "
2993 "-std=c++11 or -std=gnu++11");
2994 else if (processing_template_decl && current_class_type
2995 && TYPE_BINFO (current_class_type))
2997 tree b;
2999 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3001 b = TREE_CHAIN (b))
3003 tree base_type = BINFO_TYPE (b);
3004 if (CLASS_TYPE_P (base_type)
3005 && dependent_type_p (base_type))
3007 tree field;
3008 /* Go from a particular instantiation of the
3009 template (which will have an empty TYPE_FIELDs),
3010 to the main version. */
3011 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3012 for (field = TYPE_FIELDS (base_type);
3013 field;
3014 field = DECL_CHAIN (field))
3015 if (TREE_CODE (field) == TYPE_DECL
3016 && DECL_NAME (field) == id)
3018 inform (location,
3019 "(perhaps %<typename %T::%E%> was intended)",
3020 BINFO_TYPE (b), id);
3021 break;
3023 if (field)
3024 break;
3029 /* Here we diagnose qualified-ids where the scope is actually correct,
3030 but the identifier does not resolve to a valid type name. */
3031 else if (parser->scope != error_mark_node)
3033 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3035 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3036 error_at (location_of (id),
3037 "%qE in namespace %qE does not name a template type",
3038 id, parser->scope);
3039 else
3040 error_at (location_of (id),
3041 "%qE in namespace %qE does not name a type",
3042 id, parser->scope);
3043 if (DECL_P (decl))
3044 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3046 else if (CLASS_TYPE_P (parser->scope)
3047 && constructor_name_p (id, parser->scope))
3049 /* A<T>::A<T>() */
3050 error_at (location, "%<%T::%E%> names the constructor, not"
3051 " the type", parser->scope, id);
3052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3053 error_at (location, "and %qT has no template constructors",
3054 parser->scope);
3056 else if (TYPE_P (parser->scope)
3057 && dependent_scope_p (parser->scope))
3058 error_at (location, "need %<typename%> before %<%T::%E%> because "
3059 "%qT is a dependent scope",
3060 parser->scope, id, parser->scope);
3061 else if (TYPE_P (parser->scope))
3063 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3064 error_at (location_of (id),
3065 "%qE in %q#T does not name a template type",
3066 id, parser->scope);
3067 else
3068 error_at (location_of (id),
3069 "%qE in %q#T does not name a type",
3070 id, parser->scope);
3071 if (DECL_P (decl))
3072 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3074 else
3075 gcc_unreachable ();
3079 /* Check for a common situation where a type-name should be present,
3080 but is not, and issue a sensible error message. Returns true if an
3081 invalid type-name was detected.
3083 The situation handled by this function are variable declarations of the
3084 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3085 Usually, `ID' should name a type, but if we got here it means that it
3086 does not. We try to emit the best possible error message depending on
3087 how exactly the id-expression looks like. */
3089 static bool
3090 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3092 tree id;
3093 cp_token *token = cp_lexer_peek_token (parser->lexer);
3095 /* Avoid duplicate error about ambiguous lookup. */
3096 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3098 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3099 if (next->type == CPP_NAME && next->error_reported)
3100 goto out;
3103 cp_parser_parse_tentatively (parser);
3104 id = cp_parser_id_expression (parser,
3105 /*template_keyword_p=*/false,
3106 /*check_dependency_p=*/true,
3107 /*template_p=*/NULL,
3108 /*declarator_p=*/true,
3109 /*optional_p=*/false);
3110 /* If the next token is a (, this is a function with no explicit return
3111 type, i.e. constructor, destructor or conversion op. */
3112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3113 || TREE_CODE (id) == TYPE_DECL)
3115 cp_parser_abort_tentative_parse (parser);
3116 return false;
3118 if (!cp_parser_parse_definitely (parser))
3119 return false;
3121 /* Emit a diagnostic for the invalid type. */
3122 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3123 out:
3124 /* If we aren't in the middle of a declarator (i.e. in a
3125 parameter-declaration-clause), skip to the end of the declaration;
3126 there's no point in trying to process it. */
3127 if (!parser->in_declarator_p)
3128 cp_parser_skip_to_end_of_block_or_statement (parser);
3129 return true;
3132 /* Consume tokens up to, and including, the next non-nested closing `)'.
3133 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3134 are doing error recovery. Returns -1 if OR_COMMA is true and we
3135 found an unnested comma. */
3137 static int
3138 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3139 bool recovering,
3140 bool or_comma,
3141 bool consume_paren)
3143 unsigned paren_depth = 0;
3144 unsigned brace_depth = 0;
3145 unsigned square_depth = 0;
3147 if (recovering && !or_comma
3148 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3149 return 0;
3151 while (true)
3153 cp_token * token = cp_lexer_peek_token (parser->lexer);
3155 switch (token->type)
3157 case CPP_EOF:
3158 case CPP_PRAGMA_EOL:
3159 /* If we've run out of tokens, then there is no closing `)'. */
3160 return 0;
3162 /* This is good for lambda expression capture-lists. */
3163 case CPP_OPEN_SQUARE:
3164 ++square_depth;
3165 break;
3166 case CPP_CLOSE_SQUARE:
3167 if (!square_depth--)
3168 return 0;
3169 break;
3171 case CPP_SEMICOLON:
3172 /* This matches the processing in skip_to_end_of_statement. */
3173 if (!brace_depth)
3174 return 0;
3175 break;
3177 case CPP_OPEN_BRACE:
3178 ++brace_depth;
3179 break;
3180 case CPP_CLOSE_BRACE:
3181 if (!brace_depth--)
3182 return 0;
3183 break;
3185 case CPP_COMMA:
3186 if (recovering && or_comma && !brace_depth && !paren_depth
3187 && !square_depth)
3188 return -1;
3189 break;
3191 case CPP_OPEN_PAREN:
3192 if (!brace_depth)
3193 ++paren_depth;
3194 break;
3196 case CPP_CLOSE_PAREN:
3197 if (!brace_depth && !paren_depth--)
3199 if (consume_paren)
3200 cp_lexer_consume_token (parser->lexer);
3201 return 1;
3203 break;
3205 default:
3206 break;
3209 /* Consume the token. */
3210 cp_lexer_consume_token (parser->lexer);
3214 /* Consume tokens until we reach the end of the current statement.
3215 Normally, that will be just before consuming a `;'. However, if a
3216 non-nested `}' comes first, then we stop before consuming that. */
3218 static void
3219 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3221 unsigned nesting_depth = 0;
3223 /* Unwind generic function template scope if necessary. */
3224 if (parser->fully_implicit_function_template_p)
3225 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3227 while (true)
3229 cp_token *token = cp_lexer_peek_token (parser->lexer);
3231 switch (token->type)
3233 case CPP_EOF:
3234 case CPP_PRAGMA_EOL:
3235 /* If we've run out of tokens, stop. */
3236 return;
3238 case CPP_SEMICOLON:
3239 /* If the next token is a `;', we have reached the end of the
3240 statement. */
3241 if (!nesting_depth)
3242 return;
3243 break;
3245 case CPP_CLOSE_BRACE:
3246 /* If this is a non-nested '}', stop before consuming it.
3247 That way, when confronted with something like:
3249 { 3 + }
3251 we stop before consuming the closing '}', even though we
3252 have not yet reached a `;'. */
3253 if (nesting_depth == 0)
3254 return;
3256 /* If it is the closing '}' for a block that we have
3257 scanned, stop -- but only after consuming the token.
3258 That way given:
3260 void f g () { ... }
3261 typedef int I;
3263 we will stop after the body of the erroneously declared
3264 function, but before consuming the following `typedef'
3265 declaration. */
3266 if (--nesting_depth == 0)
3268 cp_lexer_consume_token (parser->lexer);
3269 return;
3272 case CPP_OPEN_BRACE:
3273 ++nesting_depth;
3274 break;
3276 default:
3277 break;
3280 /* Consume the token. */
3281 cp_lexer_consume_token (parser->lexer);
3285 /* This function is called at the end of a statement or declaration.
3286 If the next token is a semicolon, it is consumed; otherwise, error
3287 recovery is attempted. */
3289 static void
3290 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3292 /* Look for the trailing `;'. */
3293 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3295 /* If there is additional (erroneous) input, skip to the end of
3296 the statement. */
3297 cp_parser_skip_to_end_of_statement (parser);
3298 /* If the next token is now a `;', consume it. */
3299 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3300 cp_lexer_consume_token (parser->lexer);
3304 /* Skip tokens until we have consumed an entire block, or until we
3305 have consumed a non-nested `;'. */
3307 static void
3308 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3310 int nesting_depth = 0;
3312 /* Unwind generic function template scope if necessary. */
3313 if (parser->fully_implicit_function_template_p)
3314 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3316 while (nesting_depth >= 0)
3318 cp_token *token = cp_lexer_peek_token (parser->lexer);
3320 switch (token->type)
3322 case CPP_EOF:
3323 case CPP_PRAGMA_EOL:
3324 /* If we've run out of tokens, stop. */
3325 return;
3327 case CPP_SEMICOLON:
3328 /* Stop if this is an unnested ';'. */
3329 if (!nesting_depth)
3330 nesting_depth = -1;
3331 break;
3333 case CPP_CLOSE_BRACE:
3334 /* Stop if this is an unnested '}', or closes the outermost
3335 nesting level. */
3336 nesting_depth--;
3337 if (nesting_depth < 0)
3338 return;
3339 if (!nesting_depth)
3340 nesting_depth = -1;
3341 break;
3343 case CPP_OPEN_BRACE:
3344 /* Nest. */
3345 nesting_depth++;
3346 break;
3348 default:
3349 break;
3352 /* Consume the token. */
3353 cp_lexer_consume_token (parser->lexer);
3357 /* Skip tokens until a non-nested closing curly brace is the next
3358 token, or there are no more tokens. Return true in the first case,
3359 false otherwise. */
3361 static bool
3362 cp_parser_skip_to_closing_brace (cp_parser *parser)
3364 unsigned nesting_depth = 0;
3366 while (true)
3368 cp_token *token = cp_lexer_peek_token (parser->lexer);
3370 switch (token->type)
3372 case CPP_EOF:
3373 case CPP_PRAGMA_EOL:
3374 /* If we've run out of tokens, stop. */
3375 return false;
3377 case CPP_CLOSE_BRACE:
3378 /* If the next token is a non-nested `}', then we have reached
3379 the end of the current block. */
3380 if (nesting_depth-- == 0)
3381 return true;
3382 break;
3384 case CPP_OPEN_BRACE:
3385 /* If it the next token is a `{', then we are entering a new
3386 block. Consume the entire block. */
3387 ++nesting_depth;
3388 break;
3390 default:
3391 break;
3394 /* Consume the token. */
3395 cp_lexer_consume_token (parser->lexer);
3399 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3400 parameter is the PRAGMA token, allowing us to purge the entire pragma
3401 sequence. */
3403 static void
3404 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3406 cp_token *token;
3408 parser->lexer->in_pragma = false;
3411 token = cp_lexer_consume_token (parser->lexer);
3412 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3414 /* Ensure that the pragma is not parsed again. */
3415 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3418 /* Require pragma end of line, resyncing with it as necessary. The
3419 arguments are as for cp_parser_skip_to_pragma_eol. */
3421 static void
3422 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3424 parser->lexer->in_pragma = false;
3425 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3429 /* This is a simple wrapper around make_typename_type. When the id is
3430 an unresolved identifier node, we can provide a superior diagnostic
3431 using cp_parser_diagnose_invalid_type_name. */
3433 static tree
3434 cp_parser_make_typename_type (cp_parser *parser, tree id,
3435 location_t id_location)
3437 tree result;
3438 if (identifier_p (id))
3440 result = make_typename_type (parser->scope, id, typename_type,
3441 /*complain=*/tf_none);
3442 if (result == error_mark_node)
3443 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3444 return result;
3446 return make_typename_type (parser->scope, id, typename_type, tf_error);
3449 /* This is a wrapper around the
3450 make_{pointer,ptrmem,reference}_declarator functions that decides
3451 which one to call based on the CODE and CLASS_TYPE arguments. The
3452 CODE argument should be one of the values returned by
3453 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3454 appertain to the pointer or reference. */
3456 static cp_declarator *
3457 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3458 cp_cv_quals cv_qualifiers,
3459 cp_declarator *target,
3460 tree attributes)
3462 if (code == ERROR_MARK)
3463 return cp_error_declarator;
3465 if (code == INDIRECT_REF)
3466 if (class_type == NULL_TREE)
3467 return make_pointer_declarator (cv_qualifiers, target, attributes);
3468 else
3469 return make_ptrmem_declarator (cv_qualifiers, class_type,
3470 target, attributes);
3471 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3472 return make_reference_declarator (cv_qualifiers, target,
3473 false, attributes);
3474 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3475 return make_reference_declarator (cv_qualifiers, target,
3476 true, attributes);
3477 gcc_unreachable ();
3480 /* Create a new C++ parser. */
3482 static cp_parser *
3483 cp_parser_new (void)
3485 cp_parser *parser;
3486 cp_lexer *lexer;
3487 unsigned i;
3489 /* cp_lexer_new_main is called before doing GC allocation because
3490 cp_lexer_new_main might load a PCH file. */
3491 lexer = cp_lexer_new_main ();
3493 /* Initialize the binops_by_token so that we can get the tree
3494 directly from the token. */
3495 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3496 binops_by_token[binops[i].token_type] = binops[i];
3498 parser = ggc_cleared_alloc<cp_parser> ();
3499 parser->lexer = lexer;
3500 parser->context = cp_parser_context_new (NULL);
3502 /* For now, we always accept GNU extensions. */
3503 parser->allow_gnu_extensions_p = 1;
3505 /* The `>' token is a greater-than operator, not the end of a
3506 template-id. */
3507 parser->greater_than_is_operator_p = true;
3509 parser->default_arg_ok_p = true;
3511 /* We are not parsing a constant-expression. */
3512 parser->integral_constant_expression_p = false;
3513 parser->allow_non_integral_constant_expression_p = false;
3514 parser->non_integral_constant_expression_p = false;
3516 /* Local variable names are not forbidden. */
3517 parser->local_variables_forbidden_p = false;
3519 /* We are not processing an `extern "C"' declaration. */
3520 parser->in_unbraced_linkage_specification_p = false;
3522 /* We are not processing a declarator. */
3523 parser->in_declarator_p = false;
3525 /* We are not processing a template-argument-list. */
3526 parser->in_template_argument_list_p = false;
3528 /* We are not in an iteration statement. */
3529 parser->in_statement = 0;
3531 /* We are not in a switch statement. */
3532 parser->in_switch_statement_p = false;
3534 /* We are not parsing a type-id inside an expression. */
3535 parser->in_type_id_in_expr_p = false;
3537 /* Declarations aren't implicitly extern "C". */
3538 parser->implicit_extern_c = false;
3540 /* String literals should be translated to the execution character set. */
3541 parser->translate_strings_p = true;
3543 /* We are not parsing a function body. */
3544 parser->in_function_body = false;
3546 /* We can correct until told otherwise. */
3547 parser->colon_corrects_to_scope_p = true;
3549 /* The unparsed function queue is empty. */
3550 push_unparsed_function_queues (parser);
3552 /* There are no classes being defined. */
3553 parser->num_classes_being_defined = 0;
3555 /* No template parameters apply. */
3556 parser->num_template_parameter_lists = 0;
3558 /* Not declaring an implicit function template. */
3559 parser->auto_is_implicit_function_template_parm_p = false;
3560 parser->fully_implicit_function_template_p = false;
3561 parser->implicit_template_parms = 0;
3562 parser->implicit_template_scope = 0;
3564 return parser;
3567 /* Create a cp_lexer structure which will emit the tokens in CACHE
3568 and push it onto the parser's lexer stack. This is used for delayed
3569 parsing of in-class method bodies and default arguments, and should
3570 not be confused with tentative parsing. */
3571 static void
3572 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3574 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3575 lexer->next = parser->lexer;
3576 parser->lexer = lexer;
3578 /* Move the current source position to that of the first token in the
3579 new lexer. */
3580 cp_lexer_set_source_position_from_token (lexer->next_token);
3583 /* Pop the top lexer off the parser stack. This is never used for the
3584 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3585 static void
3586 cp_parser_pop_lexer (cp_parser *parser)
3588 cp_lexer *lexer = parser->lexer;
3589 parser->lexer = lexer->next;
3590 cp_lexer_destroy (lexer);
3592 /* Put the current source position back where it was before this
3593 lexer was pushed. */
3594 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3597 /* Lexical conventions [gram.lex] */
3599 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3600 identifier. */
3602 static tree
3603 cp_parser_identifier (cp_parser* parser)
3605 cp_token *token;
3607 /* Look for the identifier. */
3608 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3609 /* Return the value. */
3610 return token ? token->u.value : error_mark_node;
3613 /* Parse a sequence of adjacent string constants. Returns a
3614 TREE_STRING representing the combined, nul-terminated string
3615 constant. If TRANSLATE is true, translate the string to the
3616 execution character set. If WIDE_OK is true, a wide string is
3617 invalid here.
3619 C++98 [lex.string] says that if a narrow string literal token is
3620 adjacent to a wide string literal token, the behavior is undefined.
3621 However, C99 6.4.5p4 says that this results in a wide string literal.
3622 We follow C99 here, for consistency with the C front end.
3624 This code is largely lifted from lex_string() in c-lex.c.
3626 FUTURE: ObjC++ will need to handle @-strings here. */
3627 static tree
3628 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3629 bool lookup_udlit = true)
3631 tree value;
3632 size_t count;
3633 struct obstack str_ob;
3634 cpp_string str, istr, *strs;
3635 cp_token *tok;
3636 enum cpp_ttype type, curr_type;
3637 int have_suffix_p = 0;
3638 tree string_tree;
3639 tree suffix_id = NULL_TREE;
3640 bool curr_tok_is_userdef_p = false;
3642 tok = cp_lexer_peek_token (parser->lexer);
3643 if (!cp_parser_is_string_literal (tok))
3645 cp_parser_error (parser, "expected string-literal");
3646 return error_mark_node;
3649 if (cpp_userdef_string_p (tok->type))
3651 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3652 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 curr_tok_is_userdef_p = true;
3655 else
3657 string_tree = tok->u.value;
3658 curr_type = tok->type;
3660 type = curr_type;
3662 /* Try to avoid the overhead of creating and destroying an obstack
3663 for the common case of just one string. */
3664 if (!cp_parser_is_string_literal
3665 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3667 cp_lexer_consume_token (parser->lexer);
3669 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3670 str.len = TREE_STRING_LENGTH (string_tree);
3671 count = 1;
3673 if (curr_tok_is_userdef_p)
3675 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3676 have_suffix_p = 1;
3677 curr_type = cpp_userdef_string_remove_type (tok->type);
3679 else
3680 curr_type = tok->type;
3682 strs = &str;
3684 else
3686 gcc_obstack_init (&str_ob);
3687 count = 0;
3691 cp_lexer_consume_token (parser->lexer);
3692 count++;
3693 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3694 str.len = TREE_STRING_LENGTH (string_tree);
3696 if (curr_tok_is_userdef_p)
3698 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3699 if (have_suffix_p == 0)
3701 suffix_id = curr_suffix_id;
3702 have_suffix_p = 1;
3704 else if (have_suffix_p == 1
3705 && curr_suffix_id != suffix_id)
3707 error ("inconsistent user-defined literal suffixes"
3708 " %qD and %qD in string literal",
3709 suffix_id, curr_suffix_id);
3710 have_suffix_p = -1;
3712 curr_type = cpp_userdef_string_remove_type (tok->type);
3714 else
3715 curr_type = tok->type;
3717 if (type != curr_type)
3719 if (type == CPP_STRING)
3720 type = curr_type;
3721 else if (curr_type != CPP_STRING)
3722 error_at (tok->location,
3723 "unsupported non-standard concatenation "
3724 "of string literals");
3727 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3729 tok = cp_lexer_peek_token (parser->lexer);
3730 if (cpp_userdef_string_p (tok->type))
3732 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3733 curr_type = cpp_userdef_string_remove_type (tok->type);
3734 curr_tok_is_userdef_p = true;
3736 else
3738 string_tree = tok->u.value;
3739 curr_type = tok->type;
3740 curr_tok_is_userdef_p = false;
3743 while (cp_parser_is_string_literal (tok));
3745 strs = (cpp_string *) obstack_finish (&str_ob);
3748 if (type != CPP_STRING && !wide_ok)
3750 cp_parser_error (parser, "a wide string is invalid in this context");
3751 type = CPP_STRING;
3754 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3755 (parse_in, strs, count, &istr, type))
3757 value = build_string (istr.len, (const char *)istr.text);
3758 free (CONST_CAST (unsigned char *, istr.text));
3760 switch (type)
3762 default:
3763 case CPP_STRING:
3764 case CPP_UTF8STRING:
3765 TREE_TYPE (value) = char_array_type_node;
3766 break;
3767 case CPP_STRING16:
3768 TREE_TYPE (value) = char16_array_type_node;
3769 break;
3770 case CPP_STRING32:
3771 TREE_TYPE (value) = char32_array_type_node;
3772 break;
3773 case CPP_WSTRING:
3774 TREE_TYPE (value) = wchar_array_type_node;
3775 break;
3778 value = fix_string_type (value);
3780 if (have_suffix_p)
3782 tree literal = build_userdef_literal (suffix_id, value,
3783 OT_NONE, NULL_TREE);
3784 if (lookup_udlit)
3785 value = cp_parser_userdef_string_literal (literal);
3786 else
3787 value = literal;
3790 else
3791 /* cpp_interpret_string has issued an error. */
3792 value = error_mark_node;
3794 if (count > 1)
3795 obstack_free (&str_ob, 0);
3797 return value;
3800 /* Look up a literal operator with the name and the exact arguments. */
3802 static tree
3803 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3805 tree decl, fns;
3806 decl = lookup_name (name);
3807 if (!decl || !is_overloaded_fn (decl))
3808 return error_mark_node;
3810 for (fns = decl; fns; fns = OVL_NEXT (fns))
3812 unsigned int ix;
3813 bool found = true;
3814 tree fn = OVL_CURRENT (fns);
3815 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3816 if (parmtypes != NULL_TREE)
3818 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3819 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3821 tree tparm = TREE_VALUE (parmtypes);
3822 tree targ = TREE_TYPE ((*args)[ix]);
3823 bool ptr = TYPE_PTR_P (tparm);
3824 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3825 if ((ptr || arr || !same_type_p (tparm, targ))
3826 && (!ptr || !arr
3827 || !same_type_p (TREE_TYPE (tparm),
3828 TREE_TYPE (targ))))
3829 found = false;
3831 if (found
3832 && ix == vec_safe_length (args)
3833 /* May be this should be sufficient_parms_p instead,
3834 depending on how exactly should user-defined literals
3835 work in presence of default arguments on the literal
3836 operator parameters. */
3837 && parmtypes == void_list_node)
3838 return decl;
3842 return error_mark_node;
3845 /* Parse a user-defined char constant. Returns a call to a user-defined
3846 literal operator taking the character as an argument. */
3848 static tree
3849 cp_parser_userdef_char_literal (cp_parser *parser)
3851 cp_token *token = cp_lexer_consume_token (parser->lexer);
3852 tree literal = token->u.value;
3853 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3854 tree value = USERDEF_LITERAL_VALUE (literal);
3855 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3856 tree decl, result;
3858 /* Build up a call to the user-defined operator */
3859 /* Lookup the name we got back from the id-expression. */
3860 vec<tree, va_gc> *args = make_tree_vector ();
3861 vec_safe_push (args, value);
3862 decl = lookup_literal_operator (name, args);
3863 if (!decl || decl == error_mark_node)
3865 error ("unable to find character literal operator %qD with %qT argument",
3866 name, TREE_TYPE (value));
3867 release_tree_vector (args);
3868 return error_mark_node;
3870 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3871 release_tree_vector (args);
3872 return result;
3875 /* A subroutine of cp_parser_userdef_numeric_literal to
3876 create a char... template parameter pack from a string node. */
3878 static tree
3879 make_char_string_pack (tree value)
3881 tree charvec;
3882 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3883 const char *str = TREE_STRING_POINTER (value);
3884 int i, len = TREE_STRING_LENGTH (value) - 1;
3885 tree argvec = make_tree_vec (1);
3887 /* Fill in CHARVEC with all of the parameters. */
3888 charvec = make_tree_vec (len);
3889 for (i = 0; i < len; ++i)
3890 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3892 /* Build the argument packs. */
3893 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3894 TREE_TYPE (argpack) = char_type_node;
3896 TREE_VEC_ELT (argvec, 0) = argpack;
3898 return argvec;
3901 /* A subroutine of cp_parser_userdef_numeric_literal to
3902 create a char... template parameter pack from a string node. */
3904 static tree
3905 make_string_pack (tree value)
3907 tree charvec;
3908 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3909 const unsigned char *str
3910 = (const unsigned char *) TREE_STRING_POINTER (value);
3911 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3912 int len = TREE_STRING_LENGTH (value) / sz - 1;
3913 tree argvec = make_tree_vec (2);
3915 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3916 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3918 /* First template parm is character type. */
3919 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3921 /* Fill in CHARVEC with all of the parameters. */
3922 charvec = make_tree_vec (len);
3923 for (int i = 0; i < len; ++i)
3924 TREE_VEC_ELT (charvec, i)
3925 = double_int_to_tree (str_char_type_node,
3926 double_int::from_buffer (str + i * sz, sz));
3928 /* Build the argument packs. */
3929 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3930 TREE_TYPE (argpack) = str_char_type_node;
3932 TREE_VEC_ELT (argvec, 1) = argpack;
3934 return argvec;
3937 /* Parse a user-defined numeric constant. returns a call to a user-defined
3938 literal operator. */
3940 static tree
3941 cp_parser_userdef_numeric_literal (cp_parser *parser)
3943 cp_token *token = cp_lexer_consume_token (parser->lexer);
3944 tree literal = token->u.value;
3945 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3946 tree value = USERDEF_LITERAL_VALUE (literal);
3947 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3948 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3949 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3950 tree decl, result;
3951 vec<tree, va_gc> *args;
3953 /* Look for a literal operator taking the exact type of numeric argument
3954 as the literal value. */
3955 args = make_tree_vector ();
3956 vec_safe_push (args, value);
3957 decl = lookup_literal_operator (name, args);
3958 if (decl && decl != error_mark_node)
3960 result = finish_call_expr (decl, &args, false, true,
3961 tf_warning_or_error);
3963 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3965 warning_at (token->location, OPT_Woverflow,
3966 "integer literal exceeds range of %qT type",
3967 long_long_unsigned_type_node);
3969 else
3971 if (overflow > 0)
3972 warning_at (token->location, OPT_Woverflow,
3973 "floating literal exceeds range of %qT type",
3974 long_double_type_node);
3975 else if (overflow < 0)
3976 warning_at (token->location, OPT_Woverflow,
3977 "floating literal truncated to zero");
3980 release_tree_vector (args);
3981 return result;
3983 release_tree_vector (args);
3985 /* If the numeric argument didn't work, look for a raw literal
3986 operator taking a const char* argument consisting of the number
3987 in string format. */
3988 args = make_tree_vector ();
3989 vec_safe_push (args, num_string);
3990 decl = lookup_literal_operator (name, args);
3991 if (decl && decl != error_mark_node)
3993 result = finish_call_expr (decl, &args, false, true,
3994 tf_warning_or_error);
3995 release_tree_vector (args);
3996 return result;
3998 release_tree_vector (args);
4000 /* If the raw literal didn't work, look for a non-type template
4001 function with parameter pack char.... Call the function with
4002 template parameter characters representing the number. */
4003 args = make_tree_vector ();
4004 decl = lookup_literal_operator (name, args);
4005 if (decl && decl != error_mark_node)
4007 tree tmpl_args = make_char_string_pack (num_string);
4008 decl = lookup_template_function (decl, tmpl_args);
4009 result = finish_call_expr (decl, &args, false, true,
4010 tf_warning_or_error);
4011 release_tree_vector (args);
4012 return result;
4015 release_tree_vector (args);
4017 error ("unable to find numeric literal operator %qD", name);
4018 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4019 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4020 "to enable more built-in suffixes");
4021 return error_mark_node;
4024 /* Parse a user-defined string constant. Returns a call to a user-defined
4025 literal operator taking a character pointer and the length of the string
4026 as arguments. */
4028 static tree
4029 cp_parser_userdef_string_literal (tree literal)
4031 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4032 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4033 tree value = USERDEF_LITERAL_VALUE (literal);
4034 int len = TREE_STRING_LENGTH (value)
4035 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4036 tree decl, result;
4037 vec<tree, va_gc> *args;
4039 /* Build up a call to the user-defined operator. */
4040 /* Lookup the name we got back from the id-expression. */
4041 args = make_tree_vector ();
4042 vec_safe_push (args, value);
4043 vec_safe_push (args, build_int_cst (size_type_node, len));
4044 decl = lookup_literal_operator (name, args);
4046 if (decl && decl != error_mark_node)
4048 result = finish_call_expr (decl, &args, false, true,
4049 tf_warning_or_error);
4050 release_tree_vector (args);
4051 return result;
4053 release_tree_vector (args);
4055 /* Look for a template function with typename parameter CharT
4056 and parameter pack CharT... Call the function with
4057 template parameter characters representing the string. */
4058 args = make_tree_vector ();
4059 decl = lookup_literal_operator (name, args);
4060 if (decl && decl != error_mark_node)
4062 tree tmpl_args = make_string_pack (value);
4063 decl = lookup_template_function (decl, tmpl_args);
4064 result = finish_call_expr (decl, &args, false, true,
4065 tf_warning_or_error);
4066 release_tree_vector (args);
4067 return result;
4069 release_tree_vector (args);
4071 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4072 name, TREE_TYPE (value), size_type_node);
4073 return error_mark_node;
4077 /* Basic concepts [gram.basic] */
4079 /* Parse a translation-unit.
4081 translation-unit:
4082 declaration-seq [opt]
4084 Returns TRUE if all went well. */
4086 static bool
4087 cp_parser_translation_unit (cp_parser* parser)
4089 /* The address of the first non-permanent object on the declarator
4090 obstack. */
4091 static void *declarator_obstack_base;
4093 bool success;
4095 /* Create the declarator obstack, if necessary. */
4096 if (!cp_error_declarator)
4098 gcc_obstack_init (&declarator_obstack);
4099 /* Create the error declarator. */
4100 cp_error_declarator = make_declarator (cdk_error);
4101 /* Create the empty parameter list. */
4102 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4103 /* Remember where the base of the declarator obstack lies. */
4104 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4107 cp_parser_declaration_seq_opt (parser);
4109 /* If there are no tokens left then all went well. */
4110 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4112 /* Get rid of the token array; we don't need it any more. */
4113 cp_lexer_destroy (parser->lexer);
4114 parser->lexer = NULL;
4116 /* This file might have been a context that's implicitly extern
4117 "C". If so, pop the lang context. (Only relevant for PCH.) */
4118 if (parser->implicit_extern_c)
4120 pop_lang_context ();
4121 parser->implicit_extern_c = false;
4124 /* Finish up. */
4125 finish_translation_unit ();
4127 success = true;
4129 else
4131 cp_parser_error (parser, "expected declaration");
4132 success = false;
4135 /* Make sure the declarator obstack was fully cleaned up. */
4136 gcc_assert (obstack_next_free (&declarator_obstack)
4137 == declarator_obstack_base);
4139 /* All went well. */
4140 return success;
4143 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4144 decltype context. */
4146 static inline tsubst_flags_t
4147 complain_flags (bool decltype_p)
4149 tsubst_flags_t complain = tf_warning_or_error;
4150 if (decltype_p)
4151 complain |= tf_decltype;
4152 return complain;
4155 /* We're about to parse a collection of statements. If we're currently
4156 parsing tentatively, set up a firewall so that any nested
4157 cp_parser_commit_to_tentative_parse won't affect the current context. */
4159 static cp_token_position
4160 cp_parser_start_tentative_firewall (cp_parser *parser)
4162 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4163 return 0;
4165 cp_parser_parse_tentatively (parser);
4166 cp_parser_commit_to_topmost_tentative_parse (parser);
4167 return cp_lexer_token_position (parser->lexer, false);
4170 /* We've finished parsing the collection of statements. Wrap up the
4171 firewall and replace the relevant tokens with the parsed form. */
4173 static void
4174 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4175 tree expr)
4177 if (!start)
4178 return;
4180 /* Finish the firewall level. */
4181 cp_parser_parse_definitely (parser);
4182 /* And remember the result of the parse for when we try again. */
4183 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4184 token->type = CPP_PREPARSED_EXPR;
4185 token->u.value = expr;
4186 token->keyword = RID_MAX;
4187 cp_lexer_purge_tokens_after (parser->lexer, start);
4190 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4191 enclosing parentheses. */
4193 static tree
4194 cp_parser_statement_expr (cp_parser *parser)
4196 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4198 /* Consume the '('. */
4199 cp_lexer_consume_token (parser->lexer);
4200 /* Start the statement-expression. */
4201 tree expr = begin_stmt_expr ();
4202 /* Parse the compound-statement. */
4203 cp_parser_compound_statement (parser, expr, false, false);
4204 /* Finish up. */
4205 expr = finish_stmt_expr (expr, false);
4206 /* Consume the ')'. */
4207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4208 cp_parser_skip_to_end_of_statement (parser);
4210 cp_parser_end_tentative_firewall (parser, start, expr);
4211 return expr;
4214 /* Expressions [gram.expr] */
4216 /* Parse a primary-expression.
4218 primary-expression:
4219 literal
4220 this
4221 ( expression )
4222 id-expression
4223 lambda-expression (C++11)
4225 GNU Extensions:
4227 primary-expression:
4228 ( compound-statement )
4229 __builtin_va_arg ( assignment-expression , type-id )
4230 __builtin_offsetof ( type-id , offsetof-expression )
4232 C++ Extensions:
4233 __has_nothrow_assign ( type-id )
4234 __has_nothrow_constructor ( type-id )
4235 __has_nothrow_copy ( type-id )
4236 __has_trivial_assign ( type-id )
4237 __has_trivial_constructor ( type-id )
4238 __has_trivial_copy ( type-id )
4239 __has_trivial_destructor ( type-id )
4240 __has_virtual_destructor ( type-id )
4241 __is_abstract ( type-id )
4242 __is_base_of ( type-id , type-id )
4243 __is_class ( type-id )
4244 __is_empty ( type-id )
4245 __is_enum ( type-id )
4246 __is_final ( type-id )
4247 __is_literal_type ( type-id )
4248 __is_pod ( type-id )
4249 __is_polymorphic ( type-id )
4250 __is_std_layout ( type-id )
4251 __is_trivial ( type-id )
4252 __is_union ( type-id )
4254 Objective-C++ Extension:
4256 primary-expression:
4257 objc-expression
4259 literal:
4260 __null
4262 ADDRESS_P is true iff this expression was immediately preceded by
4263 "&" and therefore might denote a pointer-to-member. CAST_P is true
4264 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4265 true iff this expression is a template argument.
4267 Returns a representation of the expression. Upon return, *IDK
4268 indicates what kind of id-expression (if any) was present. */
4270 static tree
4271 cp_parser_primary_expression (cp_parser *parser,
4272 bool address_p,
4273 bool cast_p,
4274 bool template_arg_p,
4275 bool decltype_p,
4276 cp_id_kind *idk)
4278 cp_token *token = NULL;
4280 /* Assume the primary expression is not an id-expression. */
4281 *idk = CP_ID_KIND_NONE;
4283 /* Peek at the next token. */
4284 token = cp_lexer_peek_token (parser->lexer);
4285 switch ((int) token->type)
4287 /* literal:
4288 integer-literal
4289 character-literal
4290 floating-literal
4291 string-literal
4292 boolean-literal
4293 pointer-literal
4294 user-defined-literal */
4295 case CPP_CHAR:
4296 case CPP_CHAR16:
4297 case CPP_CHAR32:
4298 case CPP_WCHAR:
4299 case CPP_NUMBER:
4300 case CPP_PREPARSED_EXPR:
4301 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4302 return cp_parser_userdef_numeric_literal (parser);
4303 token = cp_lexer_consume_token (parser->lexer);
4304 if (TREE_CODE (token->u.value) == FIXED_CST)
4306 error_at (token->location,
4307 "fixed-point types not supported in C++");
4308 return error_mark_node;
4310 /* Floating-point literals are only allowed in an integral
4311 constant expression if they are cast to an integral or
4312 enumeration type. */
4313 if (TREE_CODE (token->u.value) == REAL_CST
4314 && parser->integral_constant_expression_p
4315 && pedantic)
4317 /* CAST_P will be set even in invalid code like "int(2.7 +
4318 ...)". Therefore, we have to check that the next token
4319 is sure to end the cast. */
4320 if (cast_p)
4322 cp_token *next_token;
4324 next_token = cp_lexer_peek_token (parser->lexer);
4325 if (/* The comma at the end of an
4326 enumerator-definition. */
4327 next_token->type != CPP_COMMA
4328 /* The curly brace at the end of an enum-specifier. */
4329 && next_token->type != CPP_CLOSE_BRACE
4330 /* The end of a statement. */
4331 && next_token->type != CPP_SEMICOLON
4332 /* The end of the cast-expression. */
4333 && next_token->type != CPP_CLOSE_PAREN
4334 /* The end of an array bound. */
4335 && next_token->type != CPP_CLOSE_SQUARE
4336 /* The closing ">" in a template-argument-list. */
4337 && (next_token->type != CPP_GREATER
4338 || parser->greater_than_is_operator_p)
4339 /* C++0x only: A ">>" treated like two ">" tokens,
4340 in a template-argument-list. */
4341 && (next_token->type != CPP_RSHIFT
4342 || (cxx_dialect == cxx98)
4343 || parser->greater_than_is_operator_p))
4344 cast_p = false;
4347 /* If we are within a cast, then the constraint that the
4348 cast is to an integral or enumeration type will be
4349 checked at that point. If we are not within a cast, then
4350 this code is invalid. */
4351 if (!cast_p)
4352 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4354 return token->u.value;
4356 case CPP_CHAR_USERDEF:
4357 case CPP_CHAR16_USERDEF:
4358 case CPP_CHAR32_USERDEF:
4359 case CPP_WCHAR_USERDEF:
4360 return cp_parser_userdef_char_literal (parser);
4362 case CPP_STRING:
4363 case CPP_STRING16:
4364 case CPP_STRING32:
4365 case CPP_WSTRING:
4366 case CPP_UTF8STRING:
4367 case CPP_STRING_USERDEF:
4368 case CPP_STRING16_USERDEF:
4369 case CPP_STRING32_USERDEF:
4370 case CPP_WSTRING_USERDEF:
4371 case CPP_UTF8STRING_USERDEF:
4372 /* ??? Should wide strings be allowed when parser->translate_strings_p
4373 is false (i.e. in attributes)? If not, we can kill the third
4374 argument to cp_parser_string_literal. */
4375 return cp_parser_string_literal (parser,
4376 parser->translate_strings_p,
4377 true);
4379 case CPP_OPEN_PAREN:
4380 /* If we see `( { ' then we are looking at the beginning of
4381 a GNU statement-expression. */
4382 if (cp_parser_allow_gnu_extensions_p (parser)
4383 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4385 /* Statement-expressions are not allowed by the standard. */
4386 pedwarn (token->location, OPT_Wpedantic,
4387 "ISO C++ forbids braced-groups within expressions");
4389 /* And they're not allowed outside of a function-body; you
4390 cannot, for example, write:
4392 int i = ({ int j = 3; j + 1; });
4394 at class or namespace scope. */
4395 if (!parser->in_function_body
4396 || parser->in_template_argument_list_p)
4398 error_at (token->location,
4399 "statement-expressions are not allowed outside "
4400 "functions nor in template-argument lists");
4401 cp_parser_skip_to_end_of_block_or_statement (parser);
4402 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4403 cp_lexer_consume_token (parser->lexer);
4404 return error_mark_node;
4406 else
4407 return cp_parser_statement_expr (parser);
4409 /* Otherwise it's a normal parenthesized expression. */
4411 tree expr;
4412 bool saved_greater_than_is_operator_p;
4414 /* Consume the `('. */
4415 cp_lexer_consume_token (parser->lexer);
4416 /* Within a parenthesized expression, a `>' token is always
4417 the greater-than operator. */
4418 saved_greater_than_is_operator_p
4419 = parser->greater_than_is_operator_p;
4420 parser->greater_than_is_operator_p = true;
4422 /* Parse the parenthesized expression. */
4423 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4424 /* Let the front end know that this expression was
4425 enclosed in parentheses. This matters in case, for
4426 example, the expression is of the form `A::B', since
4427 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4428 not. */
4429 expr = finish_parenthesized_expr (expr);
4430 /* DR 705: Wrapping an unqualified name in parentheses
4431 suppresses arg-dependent lookup. We want to pass back
4432 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4433 (c++/37862), but none of the others. */
4434 if (*idk != CP_ID_KIND_QUALIFIED)
4435 *idk = CP_ID_KIND_NONE;
4437 /* The `>' token might be the end of a template-id or
4438 template-parameter-list now. */
4439 parser->greater_than_is_operator_p
4440 = saved_greater_than_is_operator_p;
4441 /* Consume the `)'. */
4442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4443 cp_parser_skip_to_end_of_statement (parser);
4445 return expr;
4448 case CPP_OPEN_SQUARE:
4450 if (c_dialect_objc ())
4452 /* We might have an Objective-C++ message. */
4453 cp_parser_parse_tentatively (parser);
4454 tree msg = cp_parser_objc_message_expression (parser);
4455 /* If that works out, we're done ... */
4456 if (cp_parser_parse_definitely (parser))
4457 return msg;
4458 /* ... else, fall though to see if it's a lambda. */
4460 tree lam = cp_parser_lambda_expression (parser);
4461 /* Don't warn about a failed tentative parse. */
4462 if (cp_parser_error_occurred (parser))
4463 return error_mark_node;
4464 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4465 return lam;
4468 case CPP_OBJC_STRING:
4469 if (c_dialect_objc ())
4470 /* We have an Objective-C++ string literal. */
4471 return cp_parser_objc_expression (parser);
4472 cp_parser_error (parser, "expected primary-expression");
4473 return error_mark_node;
4475 case CPP_KEYWORD:
4476 switch (token->keyword)
4478 /* These two are the boolean literals. */
4479 case RID_TRUE:
4480 cp_lexer_consume_token (parser->lexer);
4481 return boolean_true_node;
4482 case RID_FALSE:
4483 cp_lexer_consume_token (parser->lexer);
4484 return boolean_false_node;
4486 /* The `__null' literal. */
4487 case RID_NULL:
4488 cp_lexer_consume_token (parser->lexer);
4489 return null_node;
4491 /* The `nullptr' literal. */
4492 case RID_NULLPTR:
4493 cp_lexer_consume_token (parser->lexer);
4494 return nullptr_node;
4496 /* Recognize the `this' keyword. */
4497 case RID_THIS:
4498 cp_lexer_consume_token (parser->lexer);
4499 if (parser->local_variables_forbidden_p)
4501 error_at (token->location,
4502 "%<this%> may not be used in this context");
4503 return error_mark_node;
4505 /* Pointers cannot appear in constant-expressions. */
4506 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4507 return error_mark_node;
4508 return finish_this_expr ();
4510 /* The `operator' keyword can be the beginning of an
4511 id-expression. */
4512 case RID_OPERATOR:
4513 goto id_expression;
4515 case RID_FUNCTION_NAME:
4516 case RID_PRETTY_FUNCTION_NAME:
4517 case RID_C99_FUNCTION_NAME:
4519 non_integral_constant name;
4521 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4522 __func__ are the names of variables -- but they are
4523 treated specially. Therefore, they are handled here,
4524 rather than relying on the generic id-expression logic
4525 below. Grammatically, these names are id-expressions.
4527 Consume the token. */
4528 token = cp_lexer_consume_token (parser->lexer);
4530 switch (token->keyword)
4532 case RID_FUNCTION_NAME:
4533 name = NIC_FUNC_NAME;
4534 break;
4535 case RID_PRETTY_FUNCTION_NAME:
4536 name = NIC_PRETTY_FUNC;
4537 break;
4538 case RID_C99_FUNCTION_NAME:
4539 name = NIC_C99_FUNC;
4540 break;
4541 default:
4542 gcc_unreachable ();
4545 if (cp_parser_non_integral_constant_expression (parser, name))
4546 return error_mark_node;
4548 /* Look up the name. */
4549 return finish_fname (token->u.value);
4552 case RID_VA_ARG:
4554 tree expression;
4555 tree type;
4556 source_location type_location;
4558 /* The `__builtin_va_arg' construct is used to handle
4559 `va_arg'. Consume the `__builtin_va_arg' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Look for the opening `('. */
4562 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4563 /* Now, parse the assignment-expression. */
4564 expression = cp_parser_assignment_expression (parser);
4565 /* Look for the `,'. */
4566 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4567 type_location = cp_lexer_peek_token (parser->lexer)->location;
4568 /* Parse the type-id. */
4569 type = cp_parser_type_id (parser);
4570 /* Look for the closing `)'. */
4571 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4572 /* Using `va_arg' in a constant-expression is not
4573 allowed. */
4574 if (cp_parser_non_integral_constant_expression (parser,
4575 NIC_VA_ARG))
4576 return error_mark_node;
4577 return build_x_va_arg (type_location, expression, type);
4580 case RID_OFFSETOF:
4581 return cp_parser_builtin_offsetof (parser);
4583 case RID_HAS_NOTHROW_ASSIGN:
4584 case RID_HAS_NOTHROW_CONSTRUCTOR:
4585 case RID_HAS_NOTHROW_COPY:
4586 case RID_HAS_TRIVIAL_ASSIGN:
4587 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4588 case RID_HAS_TRIVIAL_COPY:
4589 case RID_HAS_TRIVIAL_DESTRUCTOR:
4590 case RID_HAS_VIRTUAL_DESTRUCTOR:
4591 case RID_IS_ABSTRACT:
4592 case RID_IS_BASE_OF:
4593 case RID_IS_CLASS:
4594 case RID_IS_EMPTY:
4595 case RID_IS_ENUM:
4596 case RID_IS_FINAL:
4597 case RID_IS_LITERAL_TYPE:
4598 case RID_IS_POD:
4599 case RID_IS_POLYMORPHIC:
4600 case RID_IS_STD_LAYOUT:
4601 case RID_IS_TRIVIAL:
4602 case RID_IS_TRIVIALLY_ASSIGNABLE:
4603 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4604 case RID_IS_TRIVIALLY_COPYABLE:
4605 case RID_IS_UNION:
4606 return cp_parser_trait_expr (parser, token->keyword);
4608 /* Objective-C++ expressions. */
4609 case RID_AT_ENCODE:
4610 case RID_AT_PROTOCOL:
4611 case RID_AT_SELECTOR:
4612 return cp_parser_objc_expression (parser);
4614 case RID_TEMPLATE:
4615 if (parser->in_function_body
4616 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4617 == CPP_LESS))
4619 error_at (token->location,
4620 "a template declaration cannot appear at block scope");
4621 cp_parser_skip_to_end_of_block_or_statement (parser);
4622 return error_mark_node;
4624 default:
4625 cp_parser_error (parser, "expected primary-expression");
4626 return error_mark_node;
4629 /* An id-expression can start with either an identifier, a
4630 `::' as the beginning of a qualified-id, or the "operator"
4631 keyword. */
4632 case CPP_NAME:
4633 case CPP_SCOPE:
4634 case CPP_TEMPLATE_ID:
4635 case CPP_NESTED_NAME_SPECIFIER:
4637 tree id_expression;
4638 tree decl;
4639 const char *error_msg;
4640 bool template_p;
4641 bool done;
4642 cp_token *id_expr_token;
4644 id_expression:
4645 /* Parse the id-expression. */
4646 id_expression
4647 = cp_parser_id_expression (parser,
4648 /*template_keyword_p=*/false,
4649 /*check_dependency_p=*/true,
4650 &template_p,
4651 /*declarator_p=*/false,
4652 /*optional_p=*/false);
4653 if (id_expression == error_mark_node)
4654 return error_mark_node;
4655 id_expr_token = token;
4656 token = cp_lexer_peek_token (parser->lexer);
4657 done = (token->type != CPP_OPEN_SQUARE
4658 && token->type != CPP_OPEN_PAREN
4659 && token->type != CPP_DOT
4660 && token->type != CPP_DEREF
4661 && token->type != CPP_PLUS_PLUS
4662 && token->type != CPP_MINUS_MINUS);
4663 /* If we have a template-id, then no further lookup is
4664 required. If the template-id was for a template-class, we
4665 will sometimes have a TYPE_DECL at this point. */
4666 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4667 || TREE_CODE (id_expression) == TYPE_DECL)
4668 decl = id_expression;
4669 /* Look up the name. */
4670 else
4672 tree ambiguous_decls;
4674 /* If we already know that this lookup is ambiguous, then
4675 we've already issued an error message; there's no reason
4676 to check again. */
4677 if (id_expr_token->type == CPP_NAME
4678 && id_expr_token->error_reported)
4680 cp_parser_simulate_error (parser);
4681 return error_mark_node;
4684 decl = cp_parser_lookup_name (parser, id_expression,
4685 none_type,
4686 template_p,
4687 /*is_namespace=*/false,
4688 /*check_dependency=*/true,
4689 &ambiguous_decls,
4690 id_expr_token->location);
4691 /* If the lookup was ambiguous, an error will already have
4692 been issued. */
4693 if (ambiguous_decls)
4694 return error_mark_node;
4696 /* In Objective-C++, we may have an Objective-C 2.0
4697 dot-syntax for classes here. */
4698 if (c_dialect_objc ()
4699 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4700 && TREE_CODE (decl) == TYPE_DECL
4701 && objc_is_class_name (decl))
4703 tree component;
4704 cp_lexer_consume_token (parser->lexer);
4705 component = cp_parser_identifier (parser);
4706 if (component == error_mark_node)
4707 return error_mark_node;
4709 return objc_build_class_component_ref (id_expression, component);
4712 /* In Objective-C++, an instance variable (ivar) may be preferred
4713 to whatever cp_parser_lookup_name() found. */
4714 decl = objc_lookup_ivar (decl, id_expression);
4716 /* If name lookup gives us a SCOPE_REF, then the
4717 qualifying scope was dependent. */
4718 if (TREE_CODE (decl) == SCOPE_REF)
4720 /* At this point, we do not know if DECL is a valid
4721 integral constant expression. We assume that it is
4722 in fact such an expression, so that code like:
4724 template <int N> struct A {
4725 int a[B<N>::i];
4728 is accepted. At template-instantiation time, we
4729 will check that B<N>::i is actually a constant. */
4730 return decl;
4732 /* Check to see if DECL is a local variable in a context
4733 where that is forbidden. */
4734 if (parser->local_variables_forbidden_p
4735 && local_variable_p (decl))
4737 /* It might be that we only found DECL because we are
4738 trying to be generous with pre-ISO scoping rules.
4739 For example, consider:
4741 int i;
4742 void g() {
4743 for (int i = 0; i < 10; ++i) {}
4744 extern void f(int j = i);
4747 Here, name look up will originally find the out
4748 of scope `i'. We need to issue a warning message,
4749 but then use the global `i'. */
4750 decl = check_for_out_of_scope_variable (decl);
4751 if (local_variable_p (decl))
4753 error_at (id_expr_token->location,
4754 "local variable %qD may not appear in this context",
4755 decl);
4756 return error_mark_node;
4761 decl = (finish_id_expression
4762 (id_expression, decl, parser->scope,
4763 idk,
4764 parser->integral_constant_expression_p,
4765 parser->allow_non_integral_constant_expression_p,
4766 &parser->non_integral_constant_expression_p,
4767 template_p, done, address_p,
4768 template_arg_p,
4769 &error_msg,
4770 id_expr_token->location));
4771 if (error_msg)
4772 cp_parser_error (parser, error_msg);
4773 return decl;
4776 /* Anything else is an error. */
4777 default:
4778 cp_parser_error (parser, "expected primary-expression");
4779 return error_mark_node;
4783 static inline tree
4784 cp_parser_primary_expression (cp_parser *parser,
4785 bool address_p,
4786 bool cast_p,
4787 bool template_arg_p,
4788 cp_id_kind *idk)
4790 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4791 /*decltype*/false, idk);
4794 /* Parse an id-expression.
4796 id-expression:
4797 unqualified-id
4798 qualified-id
4800 qualified-id:
4801 :: [opt] nested-name-specifier template [opt] unqualified-id
4802 :: identifier
4803 :: operator-function-id
4804 :: template-id
4806 Return a representation of the unqualified portion of the
4807 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4808 a `::' or nested-name-specifier.
4810 Often, if the id-expression was a qualified-id, the caller will
4811 want to make a SCOPE_REF to represent the qualified-id. This
4812 function does not do this in order to avoid wastefully creating
4813 SCOPE_REFs when they are not required.
4815 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4816 `template' keyword.
4818 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4819 uninstantiated templates.
4821 If *TEMPLATE_P is non-NULL, it is set to true iff the
4822 `template' keyword is used to explicitly indicate that the entity
4823 named is a template.
4825 If DECLARATOR_P is true, the id-expression is appearing as part of
4826 a declarator, rather than as part of an expression. */
4828 static tree
4829 cp_parser_id_expression (cp_parser *parser,
4830 bool template_keyword_p,
4831 bool check_dependency_p,
4832 bool *template_p,
4833 bool declarator_p,
4834 bool optional_p)
4836 bool global_scope_p;
4837 bool nested_name_specifier_p;
4839 /* Assume the `template' keyword was not used. */
4840 if (template_p)
4841 *template_p = template_keyword_p;
4843 /* Look for the optional `::' operator. */
4844 global_scope_p
4845 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4846 != NULL_TREE);
4847 /* Look for the optional nested-name-specifier. */
4848 nested_name_specifier_p
4849 = (cp_parser_nested_name_specifier_opt (parser,
4850 /*typename_keyword_p=*/false,
4851 check_dependency_p,
4852 /*type_p=*/false,
4853 declarator_p)
4854 != NULL_TREE);
4855 /* If there is a nested-name-specifier, then we are looking at
4856 the first qualified-id production. */
4857 if (nested_name_specifier_p)
4859 tree saved_scope;
4860 tree saved_object_scope;
4861 tree saved_qualifying_scope;
4862 tree unqualified_id;
4863 bool is_template;
4865 /* See if the next token is the `template' keyword. */
4866 if (!template_p)
4867 template_p = &is_template;
4868 *template_p = cp_parser_optional_template_keyword (parser);
4869 /* Name lookup we do during the processing of the
4870 unqualified-id might obliterate SCOPE. */
4871 saved_scope = parser->scope;
4872 saved_object_scope = parser->object_scope;
4873 saved_qualifying_scope = parser->qualifying_scope;
4874 /* Process the final unqualified-id. */
4875 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4876 check_dependency_p,
4877 declarator_p,
4878 /*optional_p=*/false);
4879 /* Restore the SAVED_SCOPE for our caller. */
4880 parser->scope = saved_scope;
4881 parser->object_scope = saved_object_scope;
4882 parser->qualifying_scope = saved_qualifying_scope;
4884 return unqualified_id;
4886 /* Otherwise, if we are in global scope, then we are looking at one
4887 of the other qualified-id productions. */
4888 else if (global_scope_p)
4890 cp_token *token;
4891 tree id;
4893 /* Peek at the next token. */
4894 token = cp_lexer_peek_token (parser->lexer);
4896 /* If it's an identifier, and the next token is not a "<", then
4897 we can avoid the template-id case. This is an optimization
4898 for this common case. */
4899 if (token->type == CPP_NAME
4900 && !cp_parser_nth_token_starts_template_argument_list_p
4901 (parser, 2))
4902 return cp_parser_identifier (parser);
4904 cp_parser_parse_tentatively (parser);
4905 /* Try a template-id. */
4906 id = cp_parser_template_id (parser,
4907 /*template_keyword_p=*/false,
4908 /*check_dependency_p=*/true,
4909 none_type,
4910 declarator_p);
4911 /* If that worked, we're done. */
4912 if (cp_parser_parse_definitely (parser))
4913 return id;
4915 /* Peek at the next token. (Changes in the token buffer may
4916 have invalidated the pointer obtained above.) */
4917 token = cp_lexer_peek_token (parser->lexer);
4919 switch (token->type)
4921 case CPP_NAME:
4922 return cp_parser_identifier (parser);
4924 case CPP_KEYWORD:
4925 if (token->keyword == RID_OPERATOR)
4926 return cp_parser_operator_function_id (parser);
4927 /* Fall through. */
4929 default:
4930 cp_parser_error (parser, "expected id-expression");
4931 return error_mark_node;
4934 else
4935 return cp_parser_unqualified_id (parser, template_keyword_p,
4936 /*check_dependency_p=*/true,
4937 declarator_p,
4938 optional_p);
4941 /* Parse an unqualified-id.
4943 unqualified-id:
4944 identifier
4945 operator-function-id
4946 conversion-function-id
4947 ~ class-name
4948 template-id
4950 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4951 keyword, in a construct like `A::template ...'.
4953 Returns a representation of unqualified-id. For the `identifier'
4954 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4955 production a BIT_NOT_EXPR is returned; the operand of the
4956 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4957 other productions, see the documentation accompanying the
4958 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4959 names are looked up in uninstantiated templates. If DECLARATOR_P
4960 is true, the unqualified-id is appearing as part of a declarator,
4961 rather than as part of an expression. */
4963 static tree
4964 cp_parser_unqualified_id (cp_parser* parser,
4965 bool template_keyword_p,
4966 bool check_dependency_p,
4967 bool declarator_p,
4968 bool optional_p)
4970 cp_token *token;
4972 /* Peek at the next token. */
4973 token = cp_lexer_peek_token (parser->lexer);
4975 switch ((int) token->type)
4977 case CPP_NAME:
4979 tree id;
4981 /* We don't know yet whether or not this will be a
4982 template-id. */
4983 cp_parser_parse_tentatively (parser);
4984 /* Try a template-id. */
4985 id = cp_parser_template_id (parser, template_keyword_p,
4986 check_dependency_p,
4987 none_type,
4988 declarator_p);
4989 /* If it worked, we're done. */
4990 if (cp_parser_parse_definitely (parser))
4991 return id;
4992 /* Otherwise, it's an ordinary identifier. */
4993 return cp_parser_identifier (parser);
4996 case CPP_TEMPLATE_ID:
4997 return cp_parser_template_id (parser, template_keyword_p,
4998 check_dependency_p,
4999 none_type,
5000 declarator_p);
5002 case CPP_COMPL:
5004 tree type_decl;
5005 tree qualifying_scope;
5006 tree object_scope;
5007 tree scope;
5008 bool done;
5010 /* Consume the `~' token. */
5011 cp_lexer_consume_token (parser->lexer);
5012 /* Parse the class-name. The standard, as written, seems to
5013 say that:
5015 template <typename T> struct S { ~S (); };
5016 template <typename T> S<T>::~S() {}
5018 is invalid, since `~' must be followed by a class-name, but
5019 `S<T>' is dependent, and so not known to be a class.
5020 That's not right; we need to look in uninstantiated
5021 templates. A further complication arises from:
5023 template <typename T> void f(T t) {
5024 t.T::~T();
5027 Here, it is not possible to look up `T' in the scope of `T'
5028 itself. We must look in both the current scope, and the
5029 scope of the containing complete expression.
5031 Yet another issue is:
5033 struct S {
5034 int S;
5035 ~S();
5038 S::~S() {}
5040 The standard does not seem to say that the `S' in `~S'
5041 should refer to the type `S' and not the data member
5042 `S::S'. */
5044 /* DR 244 says that we look up the name after the "~" in the
5045 same scope as we looked up the qualifying name. That idea
5046 isn't fully worked out; it's more complicated than that. */
5047 scope = parser->scope;
5048 object_scope = parser->object_scope;
5049 qualifying_scope = parser->qualifying_scope;
5051 /* Check for invalid scopes. */
5052 if (scope == error_mark_node)
5054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5055 cp_lexer_consume_token (parser->lexer);
5056 return error_mark_node;
5058 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5060 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5061 error_at (token->location,
5062 "scope %qT before %<~%> is not a class-name",
5063 scope);
5064 cp_parser_simulate_error (parser);
5065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5066 cp_lexer_consume_token (parser->lexer);
5067 return error_mark_node;
5069 gcc_assert (!scope || TYPE_P (scope));
5071 /* If the name is of the form "X::~X" it's OK even if X is a
5072 typedef. */
5073 token = cp_lexer_peek_token (parser->lexer);
5074 if (scope
5075 && token->type == CPP_NAME
5076 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5077 != CPP_LESS)
5078 && (token->u.value == TYPE_IDENTIFIER (scope)
5079 || (CLASS_TYPE_P (scope)
5080 && constructor_name_p (token->u.value, scope))))
5082 cp_lexer_consume_token (parser->lexer);
5083 return build_nt (BIT_NOT_EXPR, scope);
5086 /* ~auto means the destructor of whatever the object is. */
5087 if (cp_parser_is_keyword (token, RID_AUTO))
5089 if (cxx_dialect < cxx14)
5090 pedwarn (input_location, 0,
5091 "%<~auto%> only available with "
5092 "-std=c++14 or -std=gnu++14");
5093 cp_lexer_consume_token (parser->lexer);
5094 return build_nt (BIT_NOT_EXPR, make_auto ());
5097 /* If there was an explicit qualification (S::~T), first look
5098 in the scope given by the qualification (i.e., S).
5100 Note: in the calls to cp_parser_class_name below we pass
5101 typename_type so that lookup finds the injected-class-name
5102 rather than the constructor. */
5103 done = false;
5104 type_decl = NULL_TREE;
5105 if (scope)
5107 cp_parser_parse_tentatively (parser);
5108 type_decl = cp_parser_class_name (parser,
5109 /*typename_keyword_p=*/false,
5110 /*template_keyword_p=*/false,
5111 typename_type,
5112 /*check_dependency=*/false,
5113 /*class_head_p=*/false,
5114 declarator_p);
5115 if (cp_parser_parse_definitely (parser))
5116 done = true;
5118 /* In "N::S::~S", look in "N" as well. */
5119 if (!done && scope && qualifying_scope)
5121 cp_parser_parse_tentatively (parser);
5122 parser->scope = qualifying_scope;
5123 parser->object_scope = NULL_TREE;
5124 parser->qualifying_scope = NULL_TREE;
5125 type_decl
5126 = cp_parser_class_name (parser,
5127 /*typename_keyword_p=*/false,
5128 /*template_keyword_p=*/false,
5129 typename_type,
5130 /*check_dependency=*/false,
5131 /*class_head_p=*/false,
5132 declarator_p);
5133 if (cp_parser_parse_definitely (parser))
5134 done = true;
5136 /* In "p->S::~T", look in the scope given by "*p" as well. */
5137 else if (!done && object_scope)
5139 cp_parser_parse_tentatively (parser);
5140 parser->scope = object_scope;
5141 parser->object_scope = NULL_TREE;
5142 parser->qualifying_scope = NULL_TREE;
5143 type_decl
5144 = cp_parser_class_name (parser,
5145 /*typename_keyword_p=*/false,
5146 /*template_keyword_p=*/false,
5147 typename_type,
5148 /*check_dependency=*/false,
5149 /*class_head_p=*/false,
5150 declarator_p);
5151 if (cp_parser_parse_definitely (parser))
5152 done = true;
5154 /* Look in the surrounding context. */
5155 if (!done)
5157 parser->scope = NULL_TREE;
5158 parser->object_scope = NULL_TREE;
5159 parser->qualifying_scope = NULL_TREE;
5160 if (processing_template_decl)
5161 cp_parser_parse_tentatively (parser);
5162 type_decl
5163 = cp_parser_class_name (parser,
5164 /*typename_keyword_p=*/false,
5165 /*template_keyword_p=*/false,
5166 typename_type,
5167 /*check_dependency=*/false,
5168 /*class_head_p=*/false,
5169 declarator_p);
5170 if (processing_template_decl
5171 && ! cp_parser_parse_definitely (parser))
5173 /* We couldn't find a type with this name, so just accept
5174 it and check for a match at instantiation time. */
5175 type_decl = cp_parser_identifier (parser);
5176 if (type_decl != error_mark_node)
5177 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5178 return type_decl;
5181 /* If an error occurred, assume that the name of the
5182 destructor is the same as the name of the qualifying
5183 class. That allows us to keep parsing after running
5184 into ill-formed destructor names. */
5185 if (type_decl == error_mark_node && scope)
5186 return build_nt (BIT_NOT_EXPR, scope);
5187 else if (type_decl == error_mark_node)
5188 return error_mark_node;
5190 /* Check that destructor name and scope match. */
5191 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5193 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5194 error_at (token->location,
5195 "declaration of %<~%T%> as member of %qT",
5196 type_decl, scope);
5197 cp_parser_simulate_error (parser);
5198 return error_mark_node;
5201 /* [class.dtor]
5203 A typedef-name that names a class shall not be used as the
5204 identifier in the declarator for a destructor declaration. */
5205 if (declarator_p
5206 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5207 && !DECL_SELF_REFERENCE_P (type_decl)
5208 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5209 error_at (token->location,
5210 "typedef-name %qD used as destructor declarator",
5211 type_decl);
5213 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5216 case CPP_KEYWORD:
5217 if (token->keyword == RID_OPERATOR)
5219 tree id;
5221 /* This could be a template-id, so we try that first. */
5222 cp_parser_parse_tentatively (parser);
5223 /* Try a template-id. */
5224 id = cp_parser_template_id (parser, template_keyword_p,
5225 /*check_dependency_p=*/true,
5226 none_type,
5227 declarator_p);
5228 /* If that worked, we're done. */
5229 if (cp_parser_parse_definitely (parser))
5230 return id;
5231 /* We still don't know whether we're looking at an
5232 operator-function-id or a conversion-function-id. */
5233 cp_parser_parse_tentatively (parser);
5234 /* Try an operator-function-id. */
5235 id = cp_parser_operator_function_id (parser);
5236 /* If that didn't work, try a conversion-function-id. */
5237 if (!cp_parser_parse_definitely (parser))
5238 id = cp_parser_conversion_function_id (parser);
5239 else if (UDLIT_OPER_P (id))
5241 /* 17.6.3.3.5 */
5242 const char *name = UDLIT_OP_SUFFIX (id);
5243 if (name[0] != '_' && !in_system_header_at (input_location)
5244 && declarator_p)
5245 warning (0, "literal operator suffixes not preceded by %<_%>"
5246 " are reserved for future standardization");
5249 return id;
5251 /* Fall through. */
5253 default:
5254 if (optional_p)
5255 return NULL_TREE;
5256 cp_parser_error (parser, "expected unqualified-id");
5257 return error_mark_node;
5261 /* Parse an (optional) nested-name-specifier.
5263 nested-name-specifier: [C++98]
5264 class-or-namespace-name :: nested-name-specifier [opt]
5265 class-or-namespace-name :: template nested-name-specifier [opt]
5267 nested-name-specifier: [C++0x]
5268 type-name ::
5269 namespace-name ::
5270 nested-name-specifier identifier ::
5271 nested-name-specifier template [opt] simple-template-id ::
5273 PARSER->SCOPE should be set appropriately before this function is
5274 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5275 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5276 in name lookups.
5278 Sets PARSER->SCOPE to the class (TYPE) or namespace
5279 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5280 it unchanged if there is no nested-name-specifier. Returns the new
5281 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5283 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5284 part of a declaration and/or decl-specifier. */
5286 static tree
5287 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5288 bool typename_keyword_p,
5289 bool check_dependency_p,
5290 bool type_p,
5291 bool is_declaration)
5293 bool success = false;
5294 cp_token_position start = 0;
5295 cp_token *token;
5297 /* Remember where the nested-name-specifier starts. */
5298 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5300 start = cp_lexer_token_position (parser->lexer, false);
5301 push_deferring_access_checks (dk_deferred);
5304 while (true)
5306 tree new_scope;
5307 tree old_scope;
5308 tree saved_qualifying_scope;
5309 bool template_keyword_p;
5311 /* Spot cases that cannot be the beginning of a
5312 nested-name-specifier. */
5313 token = cp_lexer_peek_token (parser->lexer);
5315 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5316 the already parsed nested-name-specifier. */
5317 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5319 /* Grab the nested-name-specifier and continue the loop. */
5320 cp_parser_pre_parsed_nested_name_specifier (parser);
5321 /* If we originally encountered this nested-name-specifier
5322 with IS_DECLARATION set to false, we will not have
5323 resolved TYPENAME_TYPEs, so we must do so here. */
5324 if (is_declaration
5325 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5327 new_scope = resolve_typename_type (parser->scope,
5328 /*only_current_p=*/false);
5329 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5330 parser->scope = new_scope;
5332 success = true;
5333 continue;
5336 /* Spot cases that cannot be the beginning of a
5337 nested-name-specifier. On the second and subsequent times
5338 through the loop, we look for the `template' keyword. */
5339 if (success && token->keyword == RID_TEMPLATE)
5341 /* A template-id can start a nested-name-specifier. */
5342 else if (token->type == CPP_TEMPLATE_ID)
5344 /* DR 743: decltype can be used in a nested-name-specifier. */
5345 else if (token_is_decltype (token))
5347 else
5349 /* If the next token is not an identifier, then it is
5350 definitely not a type-name or namespace-name. */
5351 if (token->type != CPP_NAME)
5352 break;
5353 /* If the following token is neither a `<' (to begin a
5354 template-id), nor a `::', then we are not looking at a
5355 nested-name-specifier. */
5356 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5358 if (token->type == CPP_COLON
5359 && parser->colon_corrects_to_scope_p
5360 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5362 error_at (token->location,
5363 "found %<:%> in nested-name-specifier, expected %<::%>");
5364 token->type = CPP_SCOPE;
5367 if (token->type != CPP_SCOPE
5368 && !cp_parser_nth_token_starts_template_argument_list_p
5369 (parser, 2))
5370 break;
5373 /* The nested-name-specifier is optional, so we parse
5374 tentatively. */
5375 cp_parser_parse_tentatively (parser);
5377 /* Look for the optional `template' keyword, if this isn't the
5378 first time through the loop. */
5379 if (success)
5380 template_keyword_p = cp_parser_optional_template_keyword (parser);
5381 else
5382 template_keyword_p = false;
5384 /* Save the old scope since the name lookup we are about to do
5385 might destroy it. */
5386 old_scope = parser->scope;
5387 saved_qualifying_scope = parser->qualifying_scope;
5388 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5389 look up names in "X<T>::I" in order to determine that "Y" is
5390 a template. So, if we have a typename at this point, we make
5391 an effort to look through it. */
5392 if (is_declaration
5393 && !typename_keyword_p
5394 && parser->scope
5395 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5396 parser->scope = resolve_typename_type (parser->scope,
5397 /*only_current_p=*/false);
5398 /* Parse the qualifying entity. */
5399 new_scope
5400 = cp_parser_qualifying_entity (parser,
5401 typename_keyword_p,
5402 template_keyword_p,
5403 check_dependency_p,
5404 type_p,
5405 is_declaration);
5406 /* Look for the `::' token. */
5407 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5409 /* If we found what we wanted, we keep going; otherwise, we're
5410 done. */
5411 if (!cp_parser_parse_definitely (parser))
5413 bool error_p = false;
5415 /* Restore the OLD_SCOPE since it was valid before the
5416 failed attempt at finding the last
5417 class-or-namespace-name. */
5418 parser->scope = old_scope;
5419 parser->qualifying_scope = saved_qualifying_scope;
5421 /* If the next token is a decltype, and the one after that is a
5422 `::', then the decltype has failed to resolve to a class or
5423 enumeration type. Give this error even when parsing
5424 tentatively since it can't possibly be valid--and we're going
5425 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5426 won't get another chance.*/
5427 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5428 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5429 == CPP_SCOPE))
5431 token = cp_lexer_consume_token (parser->lexer);
5432 error_at (token->location, "decltype evaluates to %qT, "
5433 "which is not a class or enumeration type",
5434 token->u.value);
5435 parser->scope = error_mark_node;
5436 error_p = true;
5437 /* As below. */
5438 success = true;
5439 cp_lexer_consume_token (parser->lexer);
5442 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5443 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5445 /* If we have a non-type template-id followed by ::, it can't
5446 possibly be valid. */
5447 token = cp_lexer_peek_token (parser->lexer);
5448 tree tid = token->u.tree_check_value->value;
5449 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5450 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5452 tree tmpl = NULL_TREE;
5453 if (is_overloaded_fn (tid))
5455 tree fns = get_fns (tid);
5456 if (!OVL_CHAIN (fns))
5457 tmpl = OVL_CURRENT (fns);
5458 error_at (token->location, "function template-id %qD "
5459 "in nested-name-specifier", tid);
5461 else
5463 /* Variable template. */
5464 tmpl = TREE_OPERAND (tid, 0);
5465 gcc_assert (variable_template_p (tmpl));
5466 error_at (token->location, "variable template-id %qD "
5467 "in nested-name-specifier", tid);
5469 if (tmpl)
5470 inform (DECL_SOURCE_LOCATION (tmpl),
5471 "%qD declared here", tmpl);
5473 parser->scope = error_mark_node;
5474 error_p = true;
5475 /* As below. */
5476 success = true;
5477 cp_lexer_consume_token (parser->lexer);
5478 cp_lexer_consume_token (parser->lexer);
5482 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5483 break;
5484 /* If the next token is an identifier, and the one after
5485 that is a `::', then any valid interpretation would have
5486 found a class-or-namespace-name. */
5487 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5488 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5489 == CPP_SCOPE)
5490 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5491 != CPP_COMPL))
5493 token = cp_lexer_consume_token (parser->lexer);
5494 if (!error_p)
5496 if (!token->error_reported)
5498 tree decl;
5499 tree ambiguous_decls;
5501 decl = cp_parser_lookup_name (parser, token->u.value,
5502 none_type,
5503 /*is_template=*/false,
5504 /*is_namespace=*/false,
5505 /*check_dependency=*/true,
5506 &ambiguous_decls,
5507 token->location);
5508 if (TREE_CODE (decl) == TEMPLATE_DECL)
5509 error_at (token->location,
5510 "%qD used without template parameters",
5511 decl);
5512 else if (ambiguous_decls)
5514 // cp_parser_lookup_name has the same diagnostic,
5515 // thus make sure to emit it at most once.
5516 if (cp_parser_uncommitted_to_tentative_parse_p
5517 (parser))
5519 error_at (token->location,
5520 "reference to %qD is ambiguous",
5521 token->u.value);
5522 print_candidates (ambiguous_decls);
5524 decl = error_mark_node;
5526 else
5528 if (cxx_dialect != cxx98)
5529 cp_parser_name_lookup_error
5530 (parser, token->u.value, decl, NLE_NOT_CXX98,
5531 token->location);
5532 else
5533 cp_parser_name_lookup_error
5534 (parser, token->u.value, decl, NLE_CXX98,
5535 token->location);
5538 parser->scope = error_mark_node;
5539 error_p = true;
5540 /* Treat this as a successful nested-name-specifier
5541 due to:
5543 [basic.lookup.qual]
5545 If the name found is not a class-name (clause
5546 _class_) or namespace-name (_namespace.def_), the
5547 program is ill-formed. */
5548 success = true;
5550 cp_lexer_consume_token (parser->lexer);
5552 break;
5554 /* We've found one valid nested-name-specifier. */
5555 success = true;
5556 /* Name lookup always gives us a DECL. */
5557 if (TREE_CODE (new_scope) == TYPE_DECL)
5558 new_scope = TREE_TYPE (new_scope);
5559 /* Uses of "template" must be followed by actual templates. */
5560 if (template_keyword_p
5561 && !(CLASS_TYPE_P (new_scope)
5562 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5563 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5564 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5565 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5566 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5567 == TEMPLATE_ID_EXPR)))
5568 permerror (input_location, TYPE_P (new_scope)
5569 ? G_("%qT is not a template")
5570 : G_("%qD is not a template"),
5571 new_scope);
5572 /* If it is a class scope, try to complete it; we are about to
5573 be looking up names inside the class. */
5574 if (TYPE_P (new_scope)
5575 /* Since checking types for dependency can be expensive,
5576 avoid doing it if the type is already complete. */
5577 && !COMPLETE_TYPE_P (new_scope)
5578 /* Do not try to complete dependent types. */
5579 && !dependent_type_p (new_scope))
5581 new_scope = complete_type (new_scope);
5582 /* If it is a typedef to current class, use the current
5583 class instead, as the typedef won't have any names inside
5584 it yet. */
5585 if (!COMPLETE_TYPE_P (new_scope)
5586 && currently_open_class (new_scope))
5587 new_scope = TYPE_MAIN_VARIANT (new_scope);
5589 /* Make sure we look in the right scope the next time through
5590 the loop. */
5591 parser->scope = new_scope;
5594 /* If parsing tentatively, replace the sequence of tokens that makes
5595 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5596 token. That way, should we re-parse the token stream, we will
5597 not have to repeat the effort required to do the parse, nor will
5598 we issue duplicate error messages. */
5599 if (success && start)
5601 cp_token *token;
5603 token = cp_lexer_token_at (parser->lexer, start);
5604 /* Reset the contents of the START token. */
5605 token->type = CPP_NESTED_NAME_SPECIFIER;
5606 /* Retrieve any deferred checks. Do not pop this access checks yet
5607 so the memory will not be reclaimed during token replacing below. */
5608 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5609 token->u.tree_check_value->value = parser->scope;
5610 token->u.tree_check_value->checks = get_deferred_access_checks ();
5611 token->u.tree_check_value->qualifying_scope =
5612 parser->qualifying_scope;
5613 token->keyword = RID_MAX;
5615 /* Purge all subsequent tokens. */
5616 cp_lexer_purge_tokens_after (parser->lexer, start);
5619 if (start)
5620 pop_to_parent_deferring_access_checks ();
5622 return success ? parser->scope : NULL_TREE;
5625 /* Parse a nested-name-specifier. See
5626 cp_parser_nested_name_specifier_opt for details. This function
5627 behaves identically, except that it will an issue an error if no
5628 nested-name-specifier is present. */
5630 static tree
5631 cp_parser_nested_name_specifier (cp_parser *parser,
5632 bool typename_keyword_p,
5633 bool check_dependency_p,
5634 bool type_p,
5635 bool is_declaration)
5637 tree scope;
5639 /* Look for the nested-name-specifier. */
5640 scope = cp_parser_nested_name_specifier_opt (parser,
5641 typename_keyword_p,
5642 check_dependency_p,
5643 type_p,
5644 is_declaration);
5645 /* If it was not present, issue an error message. */
5646 if (!scope)
5648 cp_parser_error (parser, "expected nested-name-specifier");
5649 parser->scope = NULL_TREE;
5652 return scope;
5655 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5656 this is either a class-name or a namespace-name (which corresponds
5657 to the class-or-namespace-name production in the grammar). For
5658 C++0x, it can also be a type-name that refers to an enumeration
5659 type or a simple-template-id.
5661 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5662 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5663 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5664 TYPE_P is TRUE iff the next name should be taken as a class-name,
5665 even the same name is declared to be another entity in the same
5666 scope.
5668 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5669 specified by the class-or-namespace-name. If neither is found the
5670 ERROR_MARK_NODE is returned. */
5672 static tree
5673 cp_parser_qualifying_entity (cp_parser *parser,
5674 bool typename_keyword_p,
5675 bool template_keyword_p,
5676 bool check_dependency_p,
5677 bool type_p,
5678 bool is_declaration)
5680 tree saved_scope;
5681 tree saved_qualifying_scope;
5682 tree saved_object_scope;
5683 tree scope;
5684 bool only_class_p;
5685 bool successful_parse_p;
5687 /* DR 743: decltype can appear in a nested-name-specifier. */
5688 if (cp_lexer_next_token_is_decltype (parser->lexer))
5690 scope = cp_parser_decltype (parser);
5691 if (TREE_CODE (scope) != ENUMERAL_TYPE
5692 && !MAYBE_CLASS_TYPE_P (scope))
5694 cp_parser_simulate_error (parser);
5695 return error_mark_node;
5697 if (TYPE_NAME (scope))
5698 scope = TYPE_NAME (scope);
5699 return scope;
5702 /* Before we try to parse the class-name, we must save away the
5703 current PARSER->SCOPE since cp_parser_class_name will destroy
5704 it. */
5705 saved_scope = parser->scope;
5706 saved_qualifying_scope = parser->qualifying_scope;
5707 saved_object_scope = parser->object_scope;
5708 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5709 there is no need to look for a namespace-name. */
5710 only_class_p = template_keyword_p
5711 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5712 if (!only_class_p)
5713 cp_parser_parse_tentatively (parser);
5714 scope = cp_parser_class_name (parser,
5715 typename_keyword_p,
5716 template_keyword_p,
5717 type_p ? class_type : none_type,
5718 check_dependency_p,
5719 /*class_head_p=*/false,
5720 is_declaration,
5721 /*enum_ok=*/cxx_dialect > cxx98);
5722 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5723 /* If that didn't work, try for a namespace-name. */
5724 if (!only_class_p && !successful_parse_p)
5726 /* Restore the saved scope. */
5727 parser->scope = saved_scope;
5728 parser->qualifying_scope = saved_qualifying_scope;
5729 parser->object_scope = saved_object_scope;
5730 /* If we are not looking at an identifier followed by the scope
5731 resolution operator, then this is not part of a
5732 nested-name-specifier. (Note that this function is only used
5733 to parse the components of a nested-name-specifier.) */
5734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5735 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5736 return error_mark_node;
5737 scope = cp_parser_namespace_name (parser);
5740 return scope;
5743 /* Return true if we are looking at a compound-literal, false otherwise. */
5745 static bool
5746 cp_parser_compound_literal_p (cp_parser *parser)
5748 /* Consume the `('. */
5749 cp_lexer_consume_token (parser->lexer);
5751 cp_lexer_save_tokens (parser->lexer);
5753 /* Skip tokens until the next token is a closing parenthesis.
5754 If we find the closing `)', and the next token is a `{', then
5755 we are looking at a compound-literal. */
5756 bool compound_literal_p
5757 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5758 /*consume_paren=*/true)
5759 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5761 /* Roll back the tokens we skipped. */
5762 cp_lexer_rollback_tokens (parser->lexer);
5764 return compound_literal_p;
5767 /* Parse a postfix-expression.
5769 postfix-expression:
5770 primary-expression
5771 postfix-expression [ expression ]
5772 postfix-expression ( expression-list [opt] )
5773 simple-type-specifier ( expression-list [opt] )
5774 typename :: [opt] nested-name-specifier identifier
5775 ( expression-list [opt] )
5776 typename :: [opt] nested-name-specifier template [opt] template-id
5777 ( expression-list [opt] )
5778 postfix-expression . template [opt] id-expression
5779 postfix-expression -> template [opt] id-expression
5780 postfix-expression . pseudo-destructor-name
5781 postfix-expression -> pseudo-destructor-name
5782 postfix-expression ++
5783 postfix-expression --
5784 dynamic_cast < type-id > ( expression )
5785 static_cast < type-id > ( expression )
5786 reinterpret_cast < type-id > ( expression )
5787 const_cast < type-id > ( expression )
5788 typeid ( expression )
5789 typeid ( type-id )
5791 GNU Extension:
5793 postfix-expression:
5794 ( type-id ) { initializer-list , [opt] }
5796 This extension is a GNU version of the C99 compound-literal
5797 construct. (The C99 grammar uses `type-name' instead of `type-id',
5798 but they are essentially the same concept.)
5800 If ADDRESS_P is true, the postfix expression is the operand of the
5801 `&' operator. CAST_P is true if this expression is the target of a
5802 cast.
5804 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5805 class member access expressions [expr.ref].
5807 Returns a representation of the expression. */
5809 static tree
5810 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5811 bool member_access_only_p, bool decltype_p,
5812 cp_id_kind * pidk_return)
5814 cp_token *token;
5815 location_t loc;
5816 enum rid keyword;
5817 cp_id_kind idk = CP_ID_KIND_NONE;
5818 tree postfix_expression = NULL_TREE;
5819 bool is_member_access = false;
5820 int saved_in_statement = -1;
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5824 loc = token->location;
5825 /* Some of the productions are determined by keywords. */
5826 keyword = token->keyword;
5827 switch (keyword)
5829 case RID_DYNCAST:
5830 case RID_STATCAST:
5831 case RID_REINTCAST:
5832 case RID_CONSTCAST:
5834 tree type;
5835 tree expression;
5836 const char *saved_message;
5837 bool saved_in_type_id_in_expr_p;
5839 /* All of these can be handled in the same way from the point
5840 of view of parsing. Begin by consuming the token
5841 identifying the cast. */
5842 cp_lexer_consume_token (parser->lexer);
5844 /* New types cannot be defined in the cast. */
5845 saved_message = parser->type_definition_forbidden_message;
5846 parser->type_definition_forbidden_message
5847 = G_("types may not be defined in casts");
5849 /* Look for the opening `<'. */
5850 cp_parser_require (parser, CPP_LESS, RT_LESS);
5851 /* Parse the type to which we are casting. */
5852 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5853 parser->in_type_id_in_expr_p = true;
5854 type = cp_parser_type_id (parser);
5855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5856 /* Look for the closing `>'. */
5857 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5858 /* Restore the old message. */
5859 parser->type_definition_forbidden_message = saved_message;
5861 bool saved_greater_than_is_operator_p
5862 = parser->greater_than_is_operator_p;
5863 parser->greater_than_is_operator_p = true;
5865 /* And the expression which is being cast. */
5866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5867 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5868 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5870 parser->greater_than_is_operator_p
5871 = saved_greater_than_is_operator_p;
5873 /* Only type conversions to integral or enumeration types
5874 can be used in constant-expressions. */
5875 if (!cast_valid_in_integral_constant_expression_p (type)
5876 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5877 return error_mark_node;
5879 switch (keyword)
5881 case RID_DYNCAST:
5882 postfix_expression
5883 = build_dynamic_cast (type, expression, tf_warning_or_error);
5884 break;
5885 case RID_STATCAST:
5886 postfix_expression
5887 = build_static_cast (type, expression, tf_warning_or_error);
5888 break;
5889 case RID_REINTCAST:
5890 postfix_expression
5891 = build_reinterpret_cast (type, expression,
5892 tf_warning_or_error);
5893 break;
5894 case RID_CONSTCAST:
5895 postfix_expression
5896 = build_const_cast (type, expression, tf_warning_or_error);
5897 break;
5898 default:
5899 gcc_unreachable ();
5902 break;
5904 case RID_TYPEID:
5906 tree type;
5907 const char *saved_message;
5908 bool saved_in_type_id_in_expr_p;
5910 /* Consume the `typeid' token. */
5911 cp_lexer_consume_token (parser->lexer);
5912 /* Look for the `(' token. */
5913 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5914 /* Types cannot be defined in a `typeid' expression. */
5915 saved_message = parser->type_definition_forbidden_message;
5916 parser->type_definition_forbidden_message
5917 = G_("types may not be defined in a %<typeid%> expression");
5918 /* We can't be sure yet whether we're looking at a type-id or an
5919 expression. */
5920 cp_parser_parse_tentatively (parser);
5921 /* Try a type-id first. */
5922 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5923 parser->in_type_id_in_expr_p = true;
5924 type = cp_parser_type_id (parser);
5925 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5926 /* Look for the `)' token. Otherwise, we can't be sure that
5927 we're not looking at an expression: consider `typeid (int
5928 (3))', for example. */
5929 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5930 /* If all went well, simply lookup the type-id. */
5931 if (cp_parser_parse_definitely (parser))
5932 postfix_expression = get_typeid (type, tf_warning_or_error);
5933 /* Otherwise, fall back to the expression variant. */
5934 else
5936 tree expression;
5938 /* Look for an expression. */
5939 expression = cp_parser_expression (parser, & idk);
5940 /* Compute its typeid. */
5941 postfix_expression = build_typeid (expression, tf_warning_or_error);
5942 /* Look for the `)' token. */
5943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5945 /* Restore the saved message. */
5946 parser->type_definition_forbidden_message = saved_message;
5947 /* `typeid' may not appear in an integral constant expression. */
5948 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5949 return error_mark_node;
5951 break;
5953 case RID_TYPENAME:
5955 tree type;
5956 /* The syntax permitted here is the same permitted for an
5957 elaborated-type-specifier. */
5958 type = cp_parser_elaborated_type_specifier (parser,
5959 /*is_friend=*/false,
5960 /*is_declaration=*/false);
5961 postfix_expression = cp_parser_functional_cast (parser, type);
5963 break;
5965 case RID_CILK_SPAWN:
5967 cp_lexer_consume_token (parser->lexer);
5968 token = cp_lexer_peek_token (parser->lexer);
5969 if (token->type == CPP_SEMICOLON)
5971 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5972 "an expression");
5973 postfix_expression = error_mark_node;
5974 break;
5976 else if (!current_function_decl)
5978 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5979 "inside a function");
5980 postfix_expression = error_mark_node;
5981 break;
5983 else
5985 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5986 saved_in_statement = parser->in_statement;
5987 parser->in_statement |= IN_CILK_SPAWN;
5989 cfun->calls_cilk_spawn = 1;
5990 postfix_expression =
5991 cp_parser_postfix_expression (parser, false, false,
5992 false, false, &idk);
5993 if (!flag_cilkplus)
5995 error_at (token->location, "-fcilkplus must be enabled to use"
5996 " %<_Cilk_spawn%>");
5997 cfun->calls_cilk_spawn = 0;
5999 else if (saved_in_statement & IN_CILK_SPAWN)
6001 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6002 "are not permitted");
6003 postfix_expression = error_mark_node;
6004 cfun->calls_cilk_spawn = 0;
6006 else
6008 postfix_expression = build_cilk_spawn (token->location,
6009 postfix_expression);
6010 if (postfix_expression != error_mark_node)
6011 SET_EXPR_LOCATION (postfix_expression, input_location);
6012 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6014 break;
6017 case RID_BUILTIN_SHUFFLE:
6019 vec<tree, va_gc> *vec;
6020 unsigned int i;
6021 tree p;
6023 cp_lexer_consume_token (parser->lexer);
6024 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6025 /*cast_p=*/false, /*allow_expansion_p=*/true,
6026 /*non_constant_p=*/NULL);
6027 if (vec == NULL)
6028 return error_mark_node;
6030 FOR_EACH_VEC_ELT (*vec, i, p)
6031 mark_exp_read (p);
6033 if (vec->length () == 2)
6034 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6035 tf_warning_or_error);
6036 else if (vec->length () == 3)
6037 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6038 tf_warning_or_error);
6039 else
6041 error_at (loc, "wrong number of arguments to "
6042 "%<__builtin_shuffle%>");
6043 return error_mark_node;
6045 break;
6048 default:
6050 tree type;
6052 /* If the next thing is a simple-type-specifier, we may be
6053 looking at a functional cast. We could also be looking at
6054 an id-expression. So, we try the functional cast, and if
6055 that doesn't work we fall back to the primary-expression. */
6056 cp_parser_parse_tentatively (parser);
6057 /* Look for the simple-type-specifier. */
6058 type = cp_parser_simple_type_specifier (parser,
6059 /*decl_specs=*/NULL,
6060 CP_PARSER_FLAGS_NONE);
6061 /* Parse the cast itself. */
6062 if (!cp_parser_error_occurred (parser))
6063 postfix_expression
6064 = cp_parser_functional_cast (parser, type);
6065 /* If that worked, we're done. */
6066 if (cp_parser_parse_definitely (parser))
6067 break;
6069 /* If the functional-cast didn't work out, try a
6070 compound-literal. */
6071 if (cp_parser_allow_gnu_extensions_p (parser)
6072 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6074 tree initializer = NULL_TREE;
6076 cp_parser_parse_tentatively (parser);
6078 /* Avoid calling cp_parser_type_id pointlessly, see comment
6079 in cp_parser_cast_expression about c++/29234. */
6080 if (!cp_parser_compound_literal_p (parser))
6081 cp_parser_simulate_error (parser);
6082 else
6084 /* Parse the type. */
6085 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6086 parser->in_type_id_in_expr_p = true;
6087 type = cp_parser_type_id (parser);
6088 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6089 /* Look for the `)'. */
6090 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6093 /* If things aren't going well, there's no need to
6094 keep going. */
6095 if (!cp_parser_error_occurred (parser))
6097 bool non_constant_p;
6098 /* Parse the brace-enclosed initializer list. */
6099 initializer = cp_parser_braced_list (parser,
6100 &non_constant_p);
6102 /* If that worked, we're definitely looking at a
6103 compound-literal expression. */
6104 if (cp_parser_parse_definitely (parser))
6106 /* Warn the user that a compound literal is not
6107 allowed in standard C++. */
6108 pedwarn (input_location, OPT_Wpedantic,
6109 "ISO C++ forbids compound-literals");
6110 /* For simplicity, we disallow compound literals in
6111 constant-expressions. We could
6112 allow compound literals of integer type, whose
6113 initializer was a constant, in constant
6114 expressions. Permitting that usage, as a further
6115 extension, would not change the meaning of any
6116 currently accepted programs. (Of course, as
6117 compound literals are not part of ISO C++, the
6118 standard has nothing to say.) */
6119 if (cp_parser_non_integral_constant_expression (parser,
6120 NIC_NCC))
6122 postfix_expression = error_mark_node;
6123 break;
6125 /* Form the representation of the compound-literal. */
6126 postfix_expression
6127 = finish_compound_literal (type, initializer,
6128 tf_warning_or_error);
6129 break;
6133 /* It must be a primary-expression. */
6134 postfix_expression
6135 = cp_parser_primary_expression (parser, address_p, cast_p,
6136 /*template_arg_p=*/false,
6137 decltype_p,
6138 &idk);
6140 break;
6143 /* Note that we don't need to worry about calling build_cplus_new on a
6144 class-valued CALL_EXPR in decltype when it isn't the end of the
6145 postfix-expression; unary_complex_lvalue will take care of that for
6146 all these cases. */
6148 /* Keep looping until the postfix-expression is complete. */
6149 while (true)
6151 if (idk == CP_ID_KIND_UNQUALIFIED
6152 && identifier_p (postfix_expression)
6153 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6154 /* It is not a Koenig lookup function call. */
6155 postfix_expression
6156 = unqualified_name_lookup_error (postfix_expression);
6158 /* Peek at the next token. */
6159 token = cp_lexer_peek_token (parser->lexer);
6161 switch (token->type)
6163 case CPP_OPEN_SQUARE:
6164 if (cp_next_tokens_can_be_std_attribute_p (parser))
6166 cp_parser_error (parser,
6167 "two consecutive %<[%> shall "
6168 "only introduce an attribute");
6169 return error_mark_node;
6171 postfix_expression
6172 = cp_parser_postfix_open_square_expression (parser,
6173 postfix_expression,
6174 false,
6175 decltype_p);
6176 idk = CP_ID_KIND_NONE;
6177 is_member_access = false;
6178 break;
6180 case CPP_OPEN_PAREN:
6181 /* postfix-expression ( expression-list [opt] ) */
6183 bool koenig_p;
6184 bool is_builtin_constant_p;
6185 bool saved_integral_constant_expression_p = false;
6186 bool saved_non_integral_constant_expression_p = false;
6187 tsubst_flags_t complain = complain_flags (decltype_p);
6188 vec<tree, va_gc> *args;
6190 is_member_access = false;
6192 is_builtin_constant_p
6193 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6194 if (is_builtin_constant_p)
6196 /* The whole point of __builtin_constant_p is to allow
6197 non-constant expressions to appear as arguments. */
6198 saved_integral_constant_expression_p
6199 = parser->integral_constant_expression_p;
6200 saved_non_integral_constant_expression_p
6201 = parser->non_integral_constant_expression_p;
6202 parser->integral_constant_expression_p = false;
6204 args = (cp_parser_parenthesized_expression_list
6205 (parser, non_attr,
6206 /*cast_p=*/false, /*allow_expansion_p=*/true,
6207 /*non_constant_p=*/NULL,
6208 /*want_literal_zero_p=*/warn_memset_transposed_args));
6209 if (is_builtin_constant_p)
6211 parser->integral_constant_expression_p
6212 = saved_integral_constant_expression_p;
6213 parser->non_integral_constant_expression_p
6214 = saved_non_integral_constant_expression_p;
6217 if (args == NULL)
6219 postfix_expression = error_mark_node;
6220 break;
6223 /* Function calls are not permitted in
6224 constant-expressions. */
6225 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6226 && cp_parser_non_integral_constant_expression (parser,
6227 NIC_FUNC_CALL))
6229 postfix_expression = error_mark_node;
6230 release_tree_vector (args);
6231 break;
6234 koenig_p = false;
6235 if (idk == CP_ID_KIND_UNQUALIFIED
6236 || idk == CP_ID_KIND_TEMPLATE_ID)
6238 if (identifier_p (postfix_expression))
6240 if (!args->is_empty ())
6242 koenig_p = true;
6243 if (!any_type_dependent_arguments_p (args))
6244 postfix_expression
6245 = perform_koenig_lookup (postfix_expression, args,
6246 complain);
6248 else
6249 postfix_expression
6250 = unqualified_fn_lookup_error (postfix_expression);
6252 /* We do not perform argument-dependent lookup if
6253 normal lookup finds a non-function, in accordance
6254 with the expected resolution of DR 218. */
6255 else if (!args->is_empty ()
6256 && is_overloaded_fn (postfix_expression))
6258 tree fn = get_first_fn (postfix_expression);
6259 fn = STRIP_TEMPLATE (fn);
6261 /* Do not do argument dependent lookup if regular
6262 lookup finds a member function or a block-scope
6263 function declaration. [basic.lookup.argdep]/3 */
6264 if (!DECL_FUNCTION_MEMBER_P (fn)
6265 && !DECL_LOCAL_FUNCTION_P (fn))
6267 koenig_p = true;
6268 if (!any_type_dependent_arguments_p (args))
6269 postfix_expression
6270 = perform_koenig_lookup (postfix_expression, args,
6271 complain);
6276 if (warn_memset_transposed_args)
6278 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6279 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6280 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6281 && vec_safe_length (args) == 3
6282 && integer_zerop ((*args)[2])
6283 && LITERAL_ZERO_P ((*args)[2])
6284 && !(integer_zerop ((*args)[1])
6285 && LITERAL_ZERO_P ((*args)[1])))
6286 warning (OPT_Wmemset_transposed_args,
6287 "%<memset%> used with constant zero length "
6288 "parameter; this could be due to transposed "
6289 "parameters");
6291 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6292 to avoid leaking those into folder and middle-end. */
6293 unsigned int i;
6294 tree arg;
6295 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6296 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6297 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6300 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6302 tree instance = TREE_OPERAND (postfix_expression, 0);
6303 tree fn = TREE_OPERAND (postfix_expression, 1);
6305 if (processing_template_decl
6306 && (type_dependent_expression_p (instance)
6307 || (!BASELINK_P (fn)
6308 && TREE_CODE (fn) != FIELD_DECL)
6309 || type_dependent_expression_p (fn)
6310 || any_type_dependent_arguments_p (args)))
6312 postfix_expression
6313 = build_nt_call_vec (postfix_expression, args);
6314 release_tree_vector (args);
6315 break;
6318 if (BASELINK_P (fn))
6320 postfix_expression
6321 = (build_new_method_call
6322 (instance, fn, &args, NULL_TREE,
6323 (idk == CP_ID_KIND_QUALIFIED
6324 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6325 : LOOKUP_NORMAL),
6326 /*fn_p=*/NULL,
6327 complain));
6329 else
6330 postfix_expression
6331 = finish_call_expr (postfix_expression, &args,
6332 /*disallow_virtual=*/false,
6333 /*koenig_p=*/false,
6334 complain);
6336 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6337 || TREE_CODE (postfix_expression) == MEMBER_REF
6338 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6339 postfix_expression = (build_offset_ref_call_from_tree
6340 (postfix_expression, &args,
6341 complain));
6342 else if (idk == CP_ID_KIND_QUALIFIED)
6343 /* A call to a static class member, or a namespace-scope
6344 function. */
6345 postfix_expression
6346 = finish_call_expr (postfix_expression, &args,
6347 /*disallow_virtual=*/true,
6348 koenig_p,
6349 complain);
6350 else
6351 /* All other function calls. */
6352 postfix_expression
6353 = finish_call_expr (postfix_expression, &args,
6354 /*disallow_virtual=*/false,
6355 koenig_p,
6356 complain);
6358 protected_set_expr_location (postfix_expression, token->location);
6360 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6361 idk = CP_ID_KIND_NONE;
6363 release_tree_vector (args);
6365 break;
6367 case CPP_DOT:
6368 case CPP_DEREF:
6369 /* postfix-expression . template [opt] id-expression
6370 postfix-expression . pseudo-destructor-name
6371 postfix-expression -> template [opt] id-expression
6372 postfix-expression -> pseudo-destructor-name */
6374 /* Consume the `.' or `->' operator. */
6375 cp_lexer_consume_token (parser->lexer);
6377 postfix_expression
6378 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6379 postfix_expression,
6380 false, &idk, loc);
6382 is_member_access = true;
6383 break;
6385 case CPP_PLUS_PLUS:
6386 /* postfix-expression ++ */
6387 /* Consume the `++' token. */
6388 cp_lexer_consume_token (parser->lexer);
6389 /* Generate a representation for the complete expression. */
6390 postfix_expression
6391 = finish_increment_expr (postfix_expression,
6392 POSTINCREMENT_EXPR);
6393 /* Increments may not appear in constant-expressions. */
6394 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6395 postfix_expression = error_mark_node;
6396 idk = CP_ID_KIND_NONE;
6397 is_member_access = false;
6398 break;
6400 case CPP_MINUS_MINUS:
6401 /* postfix-expression -- */
6402 /* Consume the `--' token. */
6403 cp_lexer_consume_token (parser->lexer);
6404 /* Generate a representation for the complete expression. */
6405 postfix_expression
6406 = finish_increment_expr (postfix_expression,
6407 POSTDECREMENT_EXPR);
6408 /* Decrements may not appear in constant-expressions. */
6409 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6410 postfix_expression = error_mark_node;
6411 idk = CP_ID_KIND_NONE;
6412 is_member_access = false;
6413 break;
6415 default:
6416 if (pidk_return != NULL)
6417 * pidk_return = idk;
6418 if (member_access_only_p)
6419 return is_member_access? postfix_expression : error_mark_node;
6420 else
6421 return postfix_expression;
6425 /* We should never get here. */
6426 gcc_unreachable ();
6427 return error_mark_node;
6430 /* This function parses Cilk Plus array notations. If a normal array expr. is
6431 parsed then the array index is passed back to the caller through *INIT_INDEX
6432 and the function returns a NULL_TREE. If array notation expr. is parsed,
6433 then *INIT_INDEX is ignored by the caller and the function returns
6434 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6435 error_mark_node. */
6437 static tree
6438 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6439 tree array_value)
6441 cp_token *token = NULL;
6442 tree length_index, stride = NULL_TREE, value_tree, array_type;
6443 if (!array_value || array_value == error_mark_node)
6445 cp_parser_skip_to_end_of_statement (parser);
6446 return error_mark_node;
6449 array_type = TREE_TYPE (array_value);
6451 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6452 parser->colon_corrects_to_scope_p = false;
6453 token = cp_lexer_peek_token (parser->lexer);
6455 if (!token)
6457 cp_parser_error (parser, "expected %<:%> or numeral");
6458 return error_mark_node;
6460 else if (token->type == CPP_COLON)
6462 /* Consume the ':'. */
6463 cp_lexer_consume_token (parser->lexer);
6465 /* If we are here, then we have a case like this A[:]. */
6466 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6468 cp_parser_error (parser, "expected %<]%>");
6469 cp_parser_skip_to_end_of_statement (parser);
6470 return error_mark_node;
6472 *init_index = NULL_TREE;
6473 stride = NULL_TREE;
6474 length_index = NULL_TREE;
6476 else
6478 /* If we are here, then there are three valid possibilities:
6479 1. ARRAY [ EXP ]
6480 2. ARRAY [ EXP : EXP ]
6481 3. ARRAY [ EXP : EXP : EXP ] */
6483 *init_index = cp_parser_expression (parser);
6484 *init_index = maybe_constant_value (*init_index);
6485 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6487 /* This indicates that we have a normal array expression. */
6488 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6489 return NULL_TREE;
6492 /* Consume the ':'. */
6493 cp_lexer_consume_token (parser->lexer);
6494 length_index = cp_parser_expression (parser);
6495 length_index = maybe_constant_value (length_index);
6496 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6498 cp_lexer_consume_token (parser->lexer);
6499 stride = cp_parser_expression (parser);
6500 stride = maybe_constant_value (stride);
6503 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6505 if (*init_index == error_mark_node || length_index == error_mark_node
6506 || stride == error_mark_node || array_type == error_mark_node)
6508 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6509 cp_lexer_consume_token (parser->lexer);
6510 return error_mark_node;
6512 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6514 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6515 length_index, stride, array_type);
6516 return value_tree;
6519 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6520 by cp_parser_builtin_offsetof. We're looking for
6522 postfix-expression [ expression ]
6523 postfix-expression [ braced-init-list ] (C++11)
6525 FOR_OFFSETOF is set if we're being called in that context, which
6526 changes how we deal with integer constant expressions. */
6528 static tree
6529 cp_parser_postfix_open_square_expression (cp_parser *parser,
6530 tree postfix_expression,
6531 bool for_offsetof,
6532 bool decltype_p)
6534 tree index = NULL_TREE;
6535 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6536 bool saved_greater_than_is_operator_p;
6538 /* Consume the `[' token. */
6539 cp_lexer_consume_token (parser->lexer);
6541 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6542 parser->greater_than_is_operator_p = true;
6544 /* Parse the index expression. */
6545 /* ??? For offsetof, there is a question of what to allow here. If
6546 offsetof is not being used in an integral constant expression context,
6547 then we *could* get the right answer by computing the value at runtime.
6548 If we are in an integral constant expression context, then we might
6549 could accept any constant expression; hard to say without analysis.
6550 Rather than open the barn door too wide right away, allow only integer
6551 constant expressions here. */
6552 if (for_offsetof)
6553 index = cp_parser_constant_expression (parser);
6554 else
6556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6558 bool expr_nonconst_p;
6559 cp_lexer_set_source_position (parser->lexer);
6560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6561 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6562 if (flag_cilkplus
6563 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6565 error_at (cp_lexer_peek_token (parser->lexer)->location,
6566 "braced list index is not allowed with array "
6567 "notation");
6568 cp_parser_skip_to_end_of_statement (parser);
6569 return error_mark_node;
6572 else if (flag_cilkplus)
6574 /* Here are have these two options:
6575 ARRAY[EXP : EXP] - Array notation expr with default
6576 stride of 1.
6577 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6578 stride. */
6579 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6580 postfix_expression);
6581 if (an_exp)
6582 return an_exp;
6584 else
6585 index = cp_parser_expression (parser);
6588 /* For offsetof and declaration of types we need
6589 constant integeral values.
6590 Also we meed to fold for negative constants so that diagnostic in
6591 c-family/c-common.c doesn't fail for array-bounds. */
6592 if (for_offsetof || decltype_p
6593 || (TREE_CODE (index) == NEGATE_EXPR && TREE_CODE (TREE_OPERAND (index, 0)) == INTEGER_CST))
6594 index = maybe_constant_value (index);
6595 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6597 /* Look for the closing `]'. */
6598 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6600 /* Build the ARRAY_REF. */
6601 postfix_expression = grok_array_decl (loc, postfix_expression,
6602 index, decltype_p);
6604 /* When not doing offsetof, array references are not permitted in
6605 constant-expressions. */
6606 if (!for_offsetof
6607 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6608 postfix_expression = error_mark_node;
6610 return postfix_expression;
6613 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6614 by cp_parser_builtin_offsetof. We're looking for
6616 postfix-expression . template [opt] id-expression
6617 postfix-expression . pseudo-destructor-name
6618 postfix-expression -> template [opt] id-expression
6619 postfix-expression -> pseudo-destructor-name
6621 FOR_OFFSETOF is set if we're being called in that context. That sorta
6622 limits what of the above we'll actually accept, but nevermind.
6623 TOKEN_TYPE is the "." or "->" token, which will already have been
6624 removed from the stream. */
6626 static tree
6627 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6628 enum cpp_ttype token_type,
6629 tree postfix_expression,
6630 bool for_offsetof, cp_id_kind *idk,
6631 location_t location)
6633 tree name;
6634 bool dependent_p;
6635 bool pseudo_destructor_p;
6636 tree scope = NULL_TREE;
6638 /* If this is a `->' operator, dereference the pointer. */
6639 if (token_type == CPP_DEREF)
6640 postfix_expression = build_x_arrow (location, postfix_expression,
6641 tf_warning_or_error);
6642 /* Check to see whether or not the expression is type-dependent. */
6643 dependent_p = type_dependent_expression_p (postfix_expression);
6644 /* The identifier following the `->' or `.' is not qualified. */
6645 parser->scope = NULL_TREE;
6646 parser->qualifying_scope = NULL_TREE;
6647 parser->object_scope = NULL_TREE;
6648 *idk = CP_ID_KIND_NONE;
6650 /* Enter the scope corresponding to the type of the object
6651 given by the POSTFIX_EXPRESSION. */
6652 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6654 scope = TREE_TYPE (postfix_expression);
6655 /* According to the standard, no expression should ever have
6656 reference type. Unfortunately, we do not currently match
6657 the standard in this respect in that our internal representation
6658 of an expression may have reference type even when the standard
6659 says it does not. Therefore, we have to manually obtain the
6660 underlying type here. */
6661 scope = non_reference (scope);
6662 /* The type of the POSTFIX_EXPRESSION must be complete. */
6663 if (scope == unknown_type_node)
6665 error_at (location, "%qE does not have class type",
6666 postfix_expression);
6667 scope = NULL_TREE;
6669 /* Unlike the object expression in other contexts, *this is not
6670 required to be of complete type for purposes of class member
6671 access (5.2.5) outside the member function body. */
6672 else if (postfix_expression != current_class_ref
6673 && !(processing_template_decl && scope == current_class_type))
6674 scope = complete_type_or_else (scope, NULL_TREE);
6675 /* Let the name lookup machinery know that we are processing a
6676 class member access expression. */
6677 parser->context->object_type = scope;
6678 /* If something went wrong, we want to be able to discern that case,
6679 as opposed to the case where there was no SCOPE due to the type
6680 of expression being dependent. */
6681 if (!scope)
6682 scope = error_mark_node;
6683 /* If the SCOPE was erroneous, make the various semantic analysis
6684 functions exit quickly -- and without issuing additional error
6685 messages. */
6686 if (scope == error_mark_node)
6687 postfix_expression = error_mark_node;
6690 /* Assume this expression is not a pseudo-destructor access. */
6691 pseudo_destructor_p = false;
6693 /* If the SCOPE is a scalar type, then, if this is a valid program,
6694 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6695 is type dependent, it can be pseudo-destructor-name or something else.
6696 Try to parse it as pseudo-destructor-name first. */
6697 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6699 tree s;
6700 tree type;
6702 cp_parser_parse_tentatively (parser);
6703 /* Parse the pseudo-destructor-name. */
6704 s = NULL_TREE;
6705 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6706 &s, &type);
6707 if (dependent_p
6708 && (cp_parser_error_occurred (parser)
6709 || !SCALAR_TYPE_P (type)))
6710 cp_parser_abort_tentative_parse (parser);
6711 else if (cp_parser_parse_definitely (parser))
6713 pseudo_destructor_p = true;
6714 postfix_expression
6715 = finish_pseudo_destructor_expr (postfix_expression,
6716 s, type, location);
6720 if (!pseudo_destructor_p)
6722 /* If the SCOPE is not a scalar type, we are looking at an
6723 ordinary class member access expression, rather than a
6724 pseudo-destructor-name. */
6725 bool template_p;
6726 cp_token *token = cp_lexer_peek_token (parser->lexer);
6727 /* Parse the id-expression. */
6728 name = (cp_parser_id_expression
6729 (parser,
6730 cp_parser_optional_template_keyword (parser),
6731 /*check_dependency_p=*/true,
6732 &template_p,
6733 /*declarator_p=*/false,
6734 /*optional_p=*/false));
6735 /* In general, build a SCOPE_REF if the member name is qualified.
6736 However, if the name was not dependent and has already been
6737 resolved; there is no need to build the SCOPE_REF. For example;
6739 struct X { void f(); };
6740 template <typename T> void f(T* t) { t->X::f(); }
6742 Even though "t" is dependent, "X::f" is not and has been resolved
6743 to a BASELINK; there is no need to include scope information. */
6745 /* But we do need to remember that there was an explicit scope for
6746 virtual function calls. */
6747 if (parser->scope)
6748 *idk = CP_ID_KIND_QUALIFIED;
6750 /* If the name is a template-id that names a type, we will get a
6751 TYPE_DECL here. That is invalid code. */
6752 if (TREE_CODE (name) == TYPE_DECL)
6754 error_at (token->location, "invalid use of %qD", name);
6755 postfix_expression = error_mark_node;
6757 else
6759 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6761 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6763 error_at (token->location, "%<%D::%D%> is not a class member",
6764 parser->scope, name);
6765 postfix_expression = error_mark_node;
6767 else
6768 name = build_qualified_name (/*type=*/NULL_TREE,
6769 parser->scope,
6770 name,
6771 template_p);
6772 parser->scope = NULL_TREE;
6773 parser->qualifying_scope = NULL_TREE;
6774 parser->object_scope = NULL_TREE;
6776 if (parser->scope && name && BASELINK_P (name))
6777 adjust_result_of_qualified_name_lookup
6778 (name, parser->scope, scope);
6779 postfix_expression
6780 = finish_class_member_access_expr (postfix_expression, name,
6781 template_p,
6782 tf_warning_or_error);
6786 /* We no longer need to look up names in the scope of the object on
6787 the left-hand side of the `.' or `->' operator. */
6788 parser->context->object_type = NULL_TREE;
6790 /* Outside of offsetof, these operators may not appear in
6791 constant-expressions. */
6792 if (!for_offsetof
6793 && (cp_parser_non_integral_constant_expression
6794 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6795 postfix_expression = error_mark_node;
6797 return postfix_expression;
6800 /* Cache of LITERAL_ZERO_P constants. */
6802 static GTY(()) tree literal_zeros[itk_none];
6804 /* Parse a parenthesized expression-list.
6806 expression-list:
6807 assignment-expression
6808 expression-list, assignment-expression
6810 attribute-list:
6811 expression-list
6812 identifier
6813 identifier, expression-list
6815 CAST_P is true if this expression is the target of a cast.
6817 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6818 argument pack.
6820 Returns a vector of trees. Each element is a representation of an
6821 assignment-expression. NULL is returned if the ( and or ) are
6822 missing. An empty, but allocated, vector is returned on no
6823 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6824 if we are parsing an attribute list for an attribute that wants a
6825 plain identifier argument, normal_attr for an attribute that wants
6826 an expression, or non_attr if we aren't parsing an attribute list. If
6827 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6828 not all of the expressions in the list were constant.
6829 WANT_LITERAL_ZERO_P is true if the caller is interested in
6830 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6831 immediately, this can be removed. */
6833 static vec<tree, va_gc> *
6834 cp_parser_parenthesized_expression_list (cp_parser* parser,
6835 int is_attribute_list,
6836 bool cast_p,
6837 bool allow_expansion_p,
6838 bool *non_constant_p,
6839 bool want_literal_zero_p)
6841 vec<tree, va_gc> *expression_list;
6842 bool fold_expr_p = is_attribute_list != non_attr;
6843 tree identifier = NULL_TREE;
6844 bool saved_greater_than_is_operator_p;
6846 /* Assume all the expressions will be constant. */
6847 if (non_constant_p)
6848 *non_constant_p = false;
6850 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6851 return NULL;
6853 expression_list = make_tree_vector ();
6855 /* Within a parenthesized expression, a `>' token is always
6856 the greater-than operator. */
6857 saved_greater_than_is_operator_p
6858 = parser->greater_than_is_operator_p;
6859 parser->greater_than_is_operator_p = true;
6861 /* Consume expressions until there are no more. */
6862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6863 while (true)
6865 tree expr;
6867 /* At the beginning of attribute lists, check to see if the
6868 next token is an identifier. */
6869 if (is_attribute_list == id_attr
6870 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6872 cp_token *token;
6874 /* Consume the identifier. */
6875 token = cp_lexer_consume_token (parser->lexer);
6876 /* Save the identifier. */
6877 identifier = token->u.value;
6879 else
6881 bool expr_non_constant_p;
6883 /* Parse the next assignment-expression. */
6884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6886 /* A braced-init-list. */
6887 cp_lexer_set_source_position (parser->lexer);
6888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6889 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6890 if (non_constant_p && expr_non_constant_p)
6891 *non_constant_p = true;
6893 else if (non_constant_p)
6895 expr = (cp_parser_constant_expression
6896 (parser, /*allow_non_constant_p=*/true,
6897 &expr_non_constant_p));
6898 if (expr_non_constant_p)
6899 *non_constant_p = true;
6901 else
6903 expr = NULL_TREE;
6904 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6905 switch (tok->type)
6907 case CPP_NUMBER:
6908 case CPP_CHAR:
6909 case CPP_WCHAR:
6910 case CPP_CHAR16:
6911 case CPP_CHAR32:
6912 /* If a parameter is literal zero alone, remember it
6913 for -Wmemset-transposed-args warning. */
6914 if (integer_zerop (tok->u.value)
6915 && !TREE_OVERFLOW (tok->u.value)
6916 && want_literal_zero_p
6917 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6918 == CPP_COMMA
6919 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6920 == CPP_CLOSE_PAREN))
6922 unsigned int i;
6923 for (i = 0; i < itk_none; ++i)
6924 if (TREE_TYPE (tok->u.value) == integer_types[i])
6925 break;
6926 if (i < itk_none && literal_zeros[i])
6927 expr = literal_zeros[i];
6928 else
6930 expr = copy_node (tok->u.value);
6931 LITERAL_ZERO_P (expr) = 1;
6932 if (i < itk_none)
6933 literal_zeros[i] = expr;
6935 /* Consume the 0 token (or '\0', 0LL etc.). */
6936 cp_lexer_consume_token (parser->lexer);
6938 break;
6939 default:
6940 break;
6942 if (expr == NULL_TREE)
6943 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6944 cast_p);
6947 if (fold_expr_p)
6948 expr = instantiate_non_dependent_expr (expr);
6950 /* If we have an ellipsis, then this is an expression
6951 expansion. */
6952 if (allow_expansion_p
6953 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6955 /* Consume the `...'. */
6956 cp_lexer_consume_token (parser->lexer);
6958 /* Build the argument pack. */
6959 expr = make_pack_expansion (expr);
6962 /* Add it to the list. We add error_mark_node
6963 expressions to the list, so that we can still tell if
6964 the correct form for a parenthesized expression-list
6965 is found. That gives better errors. */
6966 vec_safe_push (expression_list, expr);
6968 if (expr == error_mark_node)
6969 goto skip_comma;
6972 /* After the first item, attribute lists look the same as
6973 expression lists. */
6974 is_attribute_list = non_attr;
6976 get_comma:;
6977 /* If the next token isn't a `,', then we are done. */
6978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6979 break;
6981 /* Otherwise, consume the `,' and keep going. */
6982 cp_lexer_consume_token (parser->lexer);
6985 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6987 int ending;
6989 skip_comma:;
6990 /* We try and resync to an unnested comma, as that will give the
6991 user better diagnostics. */
6992 ending = cp_parser_skip_to_closing_parenthesis (parser,
6993 /*recovering=*/true,
6994 /*or_comma=*/true,
6995 /*consume_paren=*/true);
6996 if (ending < 0)
6997 goto get_comma;
6998 if (!ending)
7000 parser->greater_than_is_operator_p
7001 = saved_greater_than_is_operator_p;
7002 return NULL;
7006 parser->greater_than_is_operator_p
7007 = saved_greater_than_is_operator_p;
7009 if (identifier)
7010 vec_safe_insert (expression_list, 0, identifier);
7012 return expression_list;
7015 /* Parse a pseudo-destructor-name.
7017 pseudo-destructor-name:
7018 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7019 :: [opt] nested-name-specifier template template-id :: ~ type-name
7020 :: [opt] nested-name-specifier [opt] ~ type-name
7022 If either of the first two productions is used, sets *SCOPE to the
7023 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7024 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7025 or ERROR_MARK_NODE if the parse fails. */
7027 static void
7028 cp_parser_pseudo_destructor_name (cp_parser* parser,
7029 tree object,
7030 tree* scope,
7031 tree* type)
7033 bool nested_name_specifier_p;
7035 /* Handle ~auto. */
7036 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7037 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7038 && !type_dependent_expression_p (object))
7040 if (cxx_dialect < cxx14)
7041 pedwarn (input_location, 0,
7042 "%<~auto%> only available with "
7043 "-std=c++14 or -std=gnu++14");
7044 cp_lexer_consume_token (parser->lexer);
7045 cp_lexer_consume_token (parser->lexer);
7046 *scope = NULL_TREE;
7047 *type = TREE_TYPE (object);
7048 return;
7051 /* Assume that things will not work out. */
7052 *type = error_mark_node;
7054 /* Look for the optional `::' operator. */
7055 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7056 /* Look for the optional nested-name-specifier. */
7057 nested_name_specifier_p
7058 = (cp_parser_nested_name_specifier_opt (parser,
7059 /*typename_keyword_p=*/false,
7060 /*check_dependency_p=*/true,
7061 /*type_p=*/false,
7062 /*is_declaration=*/false)
7063 != NULL_TREE);
7064 /* Now, if we saw a nested-name-specifier, we might be doing the
7065 second production. */
7066 if (nested_name_specifier_p
7067 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7069 /* Consume the `template' keyword. */
7070 cp_lexer_consume_token (parser->lexer);
7071 /* Parse the template-id. */
7072 cp_parser_template_id (parser,
7073 /*template_keyword_p=*/true,
7074 /*check_dependency_p=*/false,
7075 class_type,
7076 /*is_declaration=*/true);
7077 /* Look for the `::' token. */
7078 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7080 /* If the next token is not a `~', then there might be some
7081 additional qualification. */
7082 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7084 /* At this point, we're looking for "type-name :: ~". The type-name
7085 must not be a class-name, since this is a pseudo-destructor. So,
7086 it must be either an enum-name, or a typedef-name -- both of which
7087 are just identifiers. So, we peek ahead to check that the "::"
7088 and "~" tokens are present; if they are not, then we can avoid
7089 calling type_name. */
7090 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7091 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7092 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7094 cp_parser_error (parser, "non-scalar type");
7095 return;
7098 /* Look for the type-name. */
7099 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7100 if (*scope == error_mark_node)
7101 return;
7103 /* Look for the `::' token. */
7104 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7106 else
7107 *scope = NULL_TREE;
7109 /* Look for the `~'. */
7110 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7112 /* Once we see the ~, this has to be a pseudo-destructor. */
7113 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7114 cp_parser_commit_to_topmost_tentative_parse (parser);
7116 /* Look for the type-name again. We are not responsible for
7117 checking that it matches the first type-name. */
7118 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7121 /* Parse a unary-expression.
7123 unary-expression:
7124 postfix-expression
7125 ++ cast-expression
7126 -- cast-expression
7127 unary-operator cast-expression
7128 sizeof unary-expression
7129 sizeof ( type-id )
7130 alignof ( type-id ) [C++0x]
7131 new-expression
7132 delete-expression
7134 GNU Extensions:
7136 unary-expression:
7137 __extension__ cast-expression
7138 __alignof__ unary-expression
7139 __alignof__ ( type-id )
7140 alignof unary-expression [C++0x]
7141 __real__ cast-expression
7142 __imag__ cast-expression
7143 && identifier
7144 sizeof ( type-id ) { initializer-list , [opt] }
7145 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7146 __alignof__ ( type-id ) { initializer-list , [opt] }
7148 ADDRESS_P is true iff the unary-expression is appearing as the
7149 operand of the `&' operator. CAST_P is true if this expression is
7150 the target of a cast.
7152 Returns a representation of the expression. */
7154 static tree
7155 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7156 bool address_p, bool cast_p, bool decltype_p)
7158 cp_token *token;
7159 enum tree_code unary_operator;
7161 /* Peek at the next token. */
7162 token = cp_lexer_peek_token (parser->lexer);
7163 /* Some keywords give away the kind of expression. */
7164 if (token->type == CPP_KEYWORD)
7166 enum rid keyword = token->keyword;
7168 switch (keyword)
7170 case RID_ALIGNOF:
7171 case RID_SIZEOF:
7173 tree operand, ret;
7174 enum tree_code op;
7175 location_t first_loc;
7177 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7178 /* Consume the token. */
7179 cp_lexer_consume_token (parser->lexer);
7180 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7181 /* Parse the operand. */
7182 operand = cp_parser_sizeof_operand (parser, keyword);
7184 if (TYPE_P (operand))
7185 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7186 else
7188 /* ISO C++ defines alignof only with types, not with
7189 expressions. So pedwarn if alignof is used with a non-
7190 type expression. However, __alignof__ is ok. */
7191 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7192 pedwarn (token->location, OPT_Wpedantic,
7193 "ISO C++ does not allow %<alignof%> "
7194 "with a non-type");
7196 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7198 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7199 SIZEOF_EXPR with the original operand. */
7200 if (op == SIZEOF_EXPR && ret != error_mark_node)
7202 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7204 if (!processing_template_decl && TYPE_P (operand))
7206 ret = build_min (SIZEOF_EXPR, size_type_node,
7207 build1 (NOP_EXPR, operand,
7208 error_mark_node));
7209 SIZEOF_EXPR_TYPE_P (ret) = 1;
7211 else
7212 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7213 TREE_SIDE_EFFECTS (ret) = 0;
7214 TREE_READONLY (ret) = 1;
7216 SET_EXPR_LOCATION (ret, first_loc);
7218 return ret;
7221 case RID_NEW:
7222 return cp_parser_new_expression (parser);
7224 case RID_DELETE:
7225 return cp_parser_delete_expression (parser);
7227 case RID_EXTENSION:
7229 /* The saved value of the PEDANTIC flag. */
7230 int saved_pedantic;
7231 tree expr;
7233 /* Save away the PEDANTIC flag. */
7234 cp_parser_extension_opt (parser, &saved_pedantic);
7235 /* Parse the cast-expression. */
7236 expr = cp_parser_simple_cast_expression (parser);
7237 /* Restore the PEDANTIC flag. */
7238 pedantic = saved_pedantic;
7240 return expr;
7243 case RID_REALPART:
7244 case RID_IMAGPART:
7246 tree expression;
7248 /* Consume the `__real__' or `__imag__' token. */
7249 cp_lexer_consume_token (parser->lexer);
7250 /* Parse the cast-expression. */
7251 expression = cp_parser_simple_cast_expression (parser);
7252 /* Create the complete representation. */
7253 return build_x_unary_op (token->location,
7254 (keyword == RID_REALPART
7255 ? REALPART_EXPR : IMAGPART_EXPR),
7256 expression,
7257 tf_warning_or_error);
7259 break;
7261 case RID_TRANSACTION_ATOMIC:
7262 case RID_TRANSACTION_RELAXED:
7263 return cp_parser_transaction_expression (parser, keyword);
7265 case RID_NOEXCEPT:
7267 tree expr;
7268 const char *saved_message;
7269 bool saved_integral_constant_expression_p;
7270 bool saved_non_integral_constant_expression_p;
7271 bool saved_greater_than_is_operator_p;
7273 cp_lexer_consume_token (parser->lexer);
7274 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7276 saved_message = parser->type_definition_forbidden_message;
7277 parser->type_definition_forbidden_message
7278 = G_("types may not be defined in %<noexcept%> expressions");
7280 saved_integral_constant_expression_p
7281 = parser->integral_constant_expression_p;
7282 saved_non_integral_constant_expression_p
7283 = parser->non_integral_constant_expression_p;
7284 parser->integral_constant_expression_p = false;
7286 saved_greater_than_is_operator_p
7287 = parser->greater_than_is_operator_p;
7288 parser->greater_than_is_operator_p = true;
7290 ++cp_unevaluated_operand;
7291 ++c_inhibit_evaluation_warnings;
7292 ++cp_noexcept_operand;
7293 expr = cp_parser_expression (parser);
7294 --cp_noexcept_operand;
7295 --c_inhibit_evaluation_warnings;
7296 --cp_unevaluated_operand;
7298 parser->greater_than_is_operator_p
7299 = saved_greater_than_is_operator_p;
7301 parser->integral_constant_expression_p
7302 = saved_integral_constant_expression_p;
7303 parser->non_integral_constant_expression_p
7304 = saved_non_integral_constant_expression_p;
7306 parser->type_definition_forbidden_message = saved_message;
7308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7309 return finish_noexcept_expr (expr, tf_warning_or_error);
7312 default:
7313 break;
7317 /* Look for the `:: new' and `:: delete', which also signal the
7318 beginning of a new-expression, or delete-expression,
7319 respectively. If the next token is `::', then it might be one of
7320 these. */
7321 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7323 enum rid keyword;
7325 /* See if the token after the `::' is one of the keywords in
7326 which we're interested. */
7327 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7328 /* If it's `new', we have a new-expression. */
7329 if (keyword == RID_NEW)
7330 return cp_parser_new_expression (parser);
7331 /* Similarly, for `delete'. */
7332 else if (keyword == RID_DELETE)
7333 return cp_parser_delete_expression (parser);
7336 /* Look for a unary operator. */
7337 unary_operator = cp_parser_unary_operator (token);
7338 /* The `++' and `--' operators can be handled similarly, even though
7339 they are not technically unary-operators in the grammar. */
7340 if (unary_operator == ERROR_MARK)
7342 if (token->type == CPP_PLUS_PLUS)
7343 unary_operator = PREINCREMENT_EXPR;
7344 else if (token->type == CPP_MINUS_MINUS)
7345 unary_operator = PREDECREMENT_EXPR;
7346 /* Handle the GNU address-of-label extension. */
7347 else if (cp_parser_allow_gnu_extensions_p (parser)
7348 && token->type == CPP_AND_AND)
7350 tree identifier;
7351 tree expression;
7352 location_t loc = token->location;
7354 /* Consume the '&&' token. */
7355 cp_lexer_consume_token (parser->lexer);
7356 /* Look for the identifier. */
7357 identifier = cp_parser_identifier (parser);
7358 /* Create an expression representing the address. */
7359 expression = finish_label_address_expr (identifier, loc);
7360 if (cp_parser_non_integral_constant_expression (parser,
7361 NIC_ADDR_LABEL))
7362 expression = error_mark_node;
7363 return expression;
7366 if (unary_operator != ERROR_MARK)
7368 tree cast_expression;
7369 tree expression = error_mark_node;
7370 non_integral_constant non_constant_p = NIC_NONE;
7371 location_t loc = token->location;
7372 tsubst_flags_t complain = complain_flags (decltype_p);
7374 /* Consume the operator token. */
7375 token = cp_lexer_consume_token (parser->lexer);
7376 /* Parse the cast-expression. */
7377 cast_expression
7378 = cp_parser_cast_expression (parser,
7379 unary_operator == ADDR_EXPR,
7380 /*cast_p=*/false,
7381 /*decltype*/false,
7382 pidk);
7383 /* Now, build an appropriate representation. */
7384 switch (unary_operator)
7386 case INDIRECT_REF:
7387 non_constant_p = NIC_STAR;
7388 expression = build_x_indirect_ref (loc, cast_expression,
7389 RO_UNARY_STAR,
7390 complain);
7391 break;
7393 case ADDR_EXPR:
7394 non_constant_p = NIC_ADDR;
7395 /* Fall through. */
7396 case BIT_NOT_EXPR:
7397 expression = build_x_unary_op (loc, unary_operator,
7398 cast_expression,
7399 complain);
7400 break;
7402 case PREINCREMENT_EXPR:
7403 case PREDECREMENT_EXPR:
7404 non_constant_p = unary_operator == PREINCREMENT_EXPR
7405 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7406 /* Fall through. */
7407 case UNARY_PLUS_EXPR:
7408 case NEGATE_EXPR:
7409 case TRUTH_NOT_EXPR:
7410 expression = finish_unary_op_expr (loc, unary_operator,
7411 cast_expression, complain);
7412 break;
7414 default:
7415 gcc_unreachable ();
7418 if (non_constant_p != NIC_NONE
7419 && cp_parser_non_integral_constant_expression (parser,
7420 non_constant_p))
7421 expression = error_mark_node;
7423 return expression;
7426 return cp_parser_postfix_expression (parser, address_p, cast_p,
7427 /*member_access_only_p=*/false,
7428 decltype_p,
7429 pidk);
7432 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7433 unary-operator, the corresponding tree code is returned. */
7435 static enum tree_code
7436 cp_parser_unary_operator (cp_token* token)
7438 switch (token->type)
7440 case CPP_MULT:
7441 return INDIRECT_REF;
7443 case CPP_AND:
7444 return ADDR_EXPR;
7446 case CPP_PLUS:
7447 return UNARY_PLUS_EXPR;
7449 case CPP_MINUS:
7450 return NEGATE_EXPR;
7452 case CPP_NOT:
7453 return TRUTH_NOT_EXPR;
7455 case CPP_COMPL:
7456 return BIT_NOT_EXPR;
7458 default:
7459 return ERROR_MARK;
7463 /* Parse a new-expression.
7465 new-expression:
7466 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7467 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7469 Returns a representation of the expression. */
7471 static tree
7472 cp_parser_new_expression (cp_parser* parser)
7474 bool global_scope_p;
7475 vec<tree, va_gc> *placement;
7476 tree type;
7477 vec<tree, va_gc> *initializer;
7478 tree nelts = NULL_TREE;
7479 tree ret;
7481 /* Look for the optional `::' operator. */
7482 global_scope_p
7483 = (cp_parser_global_scope_opt (parser,
7484 /*current_scope_valid_p=*/false)
7485 != NULL_TREE);
7486 /* Look for the `new' operator. */
7487 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7488 /* There's no easy way to tell a new-placement from the
7489 `( type-id )' construct. */
7490 cp_parser_parse_tentatively (parser);
7491 /* Look for a new-placement. */
7492 placement = cp_parser_new_placement (parser);
7493 /* If that didn't work out, there's no new-placement. */
7494 if (!cp_parser_parse_definitely (parser))
7496 if (placement != NULL)
7497 release_tree_vector (placement);
7498 placement = NULL;
7501 /* If the next token is a `(', then we have a parenthesized
7502 type-id. */
7503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7505 cp_token *token;
7506 const char *saved_message = parser->type_definition_forbidden_message;
7508 /* Consume the `('. */
7509 cp_lexer_consume_token (parser->lexer);
7511 /* Parse the type-id. */
7512 parser->type_definition_forbidden_message
7513 = G_("types may not be defined in a new-expression");
7514 type = cp_parser_type_id (parser);
7515 parser->type_definition_forbidden_message = saved_message;
7517 /* Look for the closing `)'. */
7518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7519 token = cp_lexer_peek_token (parser->lexer);
7520 /* There should not be a direct-new-declarator in this production,
7521 but GCC used to allowed this, so we check and emit a sensible error
7522 message for this case. */
7523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7525 error_at (token->location,
7526 "array bound forbidden after parenthesized type-id");
7527 inform (token->location,
7528 "try removing the parentheses around the type-id");
7529 cp_parser_direct_new_declarator (parser);
7532 /* Otherwise, there must be a new-type-id. */
7533 else
7534 type = cp_parser_new_type_id (parser, &nelts);
7536 /* If the next token is a `(' or '{', then we have a new-initializer. */
7537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7538 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7539 initializer = cp_parser_new_initializer (parser);
7540 else
7541 initializer = NULL;
7543 /* A new-expression may not appear in an integral constant
7544 expression. */
7545 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7546 ret = error_mark_node;
7547 else
7549 /* Create a representation of the new-expression. */
7550 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7551 tf_warning_or_error);
7554 if (placement != NULL)
7555 release_tree_vector (placement);
7556 if (initializer != NULL)
7557 release_tree_vector (initializer);
7559 return ret;
7562 /* Parse a new-placement.
7564 new-placement:
7565 ( expression-list )
7567 Returns the same representation as for an expression-list. */
7569 static vec<tree, va_gc> *
7570 cp_parser_new_placement (cp_parser* parser)
7572 vec<tree, va_gc> *expression_list;
7574 /* Parse the expression-list. */
7575 expression_list = (cp_parser_parenthesized_expression_list
7576 (parser, non_attr, /*cast_p=*/false,
7577 /*allow_expansion_p=*/true,
7578 /*non_constant_p=*/NULL));
7580 return expression_list;
7583 /* Parse a new-type-id.
7585 new-type-id:
7586 type-specifier-seq new-declarator [opt]
7588 Returns the TYPE allocated. If the new-type-id indicates an array
7589 type, *NELTS is set to the number of elements in the last array
7590 bound; the TYPE will not include the last array bound. */
7592 static tree
7593 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7595 cp_decl_specifier_seq type_specifier_seq;
7596 cp_declarator *new_declarator;
7597 cp_declarator *declarator;
7598 cp_declarator *outer_declarator;
7599 const char *saved_message;
7601 /* The type-specifier sequence must not contain type definitions.
7602 (It cannot contain declarations of new types either, but if they
7603 are not definitions we will catch that because they are not
7604 complete.) */
7605 saved_message = parser->type_definition_forbidden_message;
7606 parser->type_definition_forbidden_message
7607 = G_("types may not be defined in a new-type-id");
7608 /* Parse the type-specifier-seq. */
7609 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7610 /*is_trailing_return=*/false,
7611 &type_specifier_seq);
7612 /* Restore the old message. */
7613 parser->type_definition_forbidden_message = saved_message;
7615 if (type_specifier_seq.type == error_mark_node)
7616 return error_mark_node;
7618 /* Parse the new-declarator. */
7619 new_declarator = cp_parser_new_declarator_opt (parser);
7621 /* Determine the number of elements in the last array dimension, if
7622 any. */
7623 *nelts = NULL_TREE;
7624 /* Skip down to the last array dimension. */
7625 declarator = new_declarator;
7626 outer_declarator = NULL;
7627 while (declarator && (declarator->kind == cdk_pointer
7628 || declarator->kind == cdk_ptrmem))
7630 outer_declarator = declarator;
7631 declarator = declarator->declarator;
7633 while (declarator
7634 && declarator->kind == cdk_array
7635 && declarator->declarator
7636 && declarator->declarator->kind == cdk_array)
7638 outer_declarator = declarator;
7639 declarator = declarator->declarator;
7642 if (declarator && declarator->kind == cdk_array)
7644 *nelts = declarator->u.array.bounds;
7645 if (*nelts == error_mark_node)
7646 *nelts = integer_one_node;
7648 if (outer_declarator)
7649 outer_declarator->declarator = declarator->declarator;
7650 else
7651 new_declarator = NULL;
7654 return groktypename (&type_specifier_seq, new_declarator, false);
7657 /* Parse an (optional) new-declarator.
7659 new-declarator:
7660 ptr-operator new-declarator [opt]
7661 direct-new-declarator
7663 Returns the declarator. */
7665 static cp_declarator *
7666 cp_parser_new_declarator_opt (cp_parser* parser)
7668 enum tree_code code;
7669 tree type, std_attributes = NULL_TREE;
7670 cp_cv_quals cv_quals;
7672 /* We don't know if there's a ptr-operator next, or not. */
7673 cp_parser_parse_tentatively (parser);
7674 /* Look for a ptr-operator. */
7675 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7676 /* If that worked, look for more new-declarators. */
7677 if (cp_parser_parse_definitely (parser))
7679 cp_declarator *declarator;
7681 /* Parse another optional declarator. */
7682 declarator = cp_parser_new_declarator_opt (parser);
7684 declarator = cp_parser_make_indirect_declarator
7685 (code, type, cv_quals, declarator, std_attributes);
7687 return declarator;
7690 /* If the next token is a `[', there is a direct-new-declarator. */
7691 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7692 return cp_parser_direct_new_declarator (parser);
7694 return NULL;
7697 /* Parse a direct-new-declarator.
7699 direct-new-declarator:
7700 [ expression ]
7701 direct-new-declarator [constant-expression]
7705 static cp_declarator *
7706 cp_parser_direct_new_declarator (cp_parser* parser)
7708 cp_declarator *declarator = NULL;
7710 while (true)
7712 tree expression;
7713 cp_token *token;
7715 /* Look for the opening `['. */
7716 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7718 token = cp_lexer_peek_token (parser->lexer);
7719 expression = cp_parser_expression (parser);
7720 /* The standard requires that the expression have integral
7721 type. DR 74 adds enumeration types. We believe that the
7722 real intent is that these expressions be handled like the
7723 expression in a `switch' condition, which also allows
7724 classes with a single conversion to integral or
7725 enumeration type. */
7726 if (!processing_template_decl)
7728 expression
7729 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7730 expression,
7731 /*complain=*/true);
7732 if (!expression)
7734 error_at (token->location,
7735 "expression in new-declarator must have integral "
7736 "or enumeration type");
7737 expression = error_mark_node;
7741 /* Look for the closing `]'. */
7742 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7744 /* Add this bound to the declarator. */
7745 declarator = make_array_declarator (declarator, expression);
7747 /* If the next token is not a `[', then there are no more
7748 bounds. */
7749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7750 break;
7753 return declarator;
7756 /* Parse a new-initializer.
7758 new-initializer:
7759 ( expression-list [opt] )
7760 braced-init-list
7762 Returns a representation of the expression-list. */
7764 static vec<tree, va_gc> *
7765 cp_parser_new_initializer (cp_parser* parser)
7767 vec<tree, va_gc> *expression_list;
7769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7771 tree t;
7772 bool expr_non_constant_p;
7773 cp_lexer_set_source_position (parser->lexer);
7774 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7775 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7776 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7777 expression_list = make_tree_vector_single (t);
7779 else
7780 expression_list = (cp_parser_parenthesized_expression_list
7781 (parser, non_attr, /*cast_p=*/false,
7782 /*allow_expansion_p=*/true,
7783 /*non_constant_p=*/NULL));
7785 return expression_list;
7788 /* Parse a delete-expression.
7790 delete-expression:
7791 :: [opt] delete cast-expression
7792 :: [opt] delete [ ] cast-expression
7794 Returns a representation of the expression. */
7796 static tree
7797 cp_parser_delete_expression (cp_parser* parser)
7799 bool global_scope_p;
7800 bool array_p;
7801 tree expression;
7803 /* Look for the optional `::' operator. */
7804 global_scope_p
7805 = (cp_parser_global_scope_opt (parser,
7806 /*current_scope_valid_p=*/false)
7807 != NULL_TREE);
7808 /* Look for the `delete' keyword. */
7809 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7810 /* See if the array syntax is in use. */
7811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7813 /* Consume the `[' token. */
7814 cp_lexer_consume_token (parser->lexer);
7815 /* Look for the `]' token. */
7816 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7817 /* Remember that this is the `[]' construct. */
7818 array_p = true;
7820 else
7821 array_p = false;
7823 /* Parse the cast-expression. */
7824 expression = cp_parser_simple_cast_expression (parser);
7826 /* A delete-expression may not appear in an integral constant
7827 expression. */
7828 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7829 return error_mark_node;
7831 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7832 tf_warning_or_error);
7835 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7836 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7837 0 otherwise. */
7839 static int
7840 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7842 cp_token *token = cp_lexer_peek_token (parser->lexer);
7843 switch (token->type)
7845 case CPP_COMMA:
7846 case CPP_SEMICOLON:
7847 case CPP_QUERY:
7848 case CPP_COLON:
7849 case CPP_CLOSE_SQUARE:
7850 case CPP_CLOSE_PAREN:
7851 case CPP_CLOSE_BRACE:
7852 case CPP_OPEN_BRACE:
7853 case CPP_DOT:
7854 case CPP_DOT_STAR:
7855 case CPP_DEREF:
7856 case CPP_DEREF_STAR:
7857 case CPP_DIV:
7858 case CPP_MOD:
7859 case CPP_LSHIFT:
7860 case CPP_RSHIFT:
7861 case CPP_LESS:
7862 case CPP_GREATER:
7863 case CPP_LESS_EQ:
7864 case CPP_GREATER_EQ:
7865 case CPP_EQ_EQ:
7866 case CPP_NOT_EQ:
7867 case CPP_EQ:
7868 case CPP_MULT_EQ:
7869 case CPP_DIV_EQ:
7870 case CPP_MOD_EQ:
7871 case CPP_PLUS_EQ:
7872 case CPP_MINUS_EQ:
7873 case CPP_RSHIFT_EQ:
7874 case CPP_LSHIFT_EQ:
7875 case CPP_AND_EQ:
7876 case CPP_XOR_EQ:
7877 case CPP_OR_EQ:
7878 case CPP_XOR:
7879 case CPP_OR:
7880 case CPP_OR_OR:
7881 case CPP_EOF:
7882 case CPP_ELLIPSIS:
7883 return 0;
7885 case CPP_OPEN_PAREN:
7886 /* In ((type ()) () the last () isn't a valid cast-expression,
7887 so the whole must be parsed as postfix-expression. */
7888 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7889 != CPP_CLOSE_PAREN;
7891 case CPP_OPEN_SQUARE:
7892 /* '[' may start a primary-expression in obj-c++ and in C++11,
7893 as a lambda-expression, eg, '(void)[]{}'. */
7894 if (cxx_dialect >= cxx11)
7895 return -1;
7896 return c_dialect_objc ();
7898 case CPP_PLUS_PLUS:
7899 case CPP_MINUS_MINUS:
7900 /* '++' and '--' may or may not start a cast-expression:
7902 struct T { void operator++(int); };
7903 void f() { (T())++; }
7907 int a;
7908 (int)++a; */
7909 return -1;
7911 default:
7912 return 1;
7916 /* Parse a cast-expression.
7918 cast-expression:
7919 unary-expression
7920 ( type-id ) cast-expression
7922 ADDRESS_P is true iff the unary-expression is appearing as the
7923 operand of the `&' operator. CAST_P is true if this expression is
7924 the target of a cast.
7926 Returns a representation of the expression. */
7928 static tree
7929 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7930 bool decltype_p, cp_id_kind * pidk)
7932 /* If it's a `(', then we might be looking at a cast. */
7933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7935 tree type = NULL_TREE;
7936 tree expr = NULL_TREE;
7937 int cast_expression = 0;
7938 const char *saved_message;
7940 /* There's no way to know yet whether or not this is a cast.
7941 For example, `(int (3))' is a unary-expression, while `(int)
7942 3' is a cast. So, we resort to parsing tentatively. */
7943 cp_parser_parse_tentatively (parser);
7944 /* Types may not be defined in a cast. */
7945 saved_message = parser->type_definition_forbidden_message;
7946 parser->type_definition_forbidden_message
7947 = G_("types may not be defined in casts");
7948 /* Consume the `('. */
7949 cp_lexer_consume_token (parser->lexer);
7950 /* A very tricky bit is that `(struct S) { 3 }' is a
7951 compound-literal (which we permit in C++ as an extension).
7952 But, that construct is not a cast-expression -- it is a
7953 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7954 is legal; if the compound-literal were a cast-expression,
7955 you'd need an extra set of parentheses.) But, if we parse
7956 the type-id, and it happens to be a class-specifier, then we
7957 will commit to the parse at that point, because we cannot
7958 undo the action that is done when creating a new class. So,
7959 then we cannot back up and do a postfix-expression.
7961 Another tricky case is the following (c++/29234):
7963 struct S { void operator () (); };
7965 void foo ()
7967 ( S()() );
7970 As a type-id we parse the parenthesized S()() as a function
7971 returning a function, groktypename complains and we cannot
7972 back up in this case either.
7974 Therefore, we scan ahead to the closing `)', and check to see
7975 if the tokens after the `)' can start a cast-expression. Otherwise
7976 we are dealing with an unary-expression, a postfix-expression
7977 or something else.
7979 Yet another tricky case, in C++11, is the following (c++/54891):
7981 (void)[]{};
7983 The issue is that usually, besides the case of lambda-expressions,
7984 the parenthesized type-id cannot be followed by '[', and, eg, we
7985 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7986 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7987 we don't commit, we try a cast-expression, then an unary-expression.
7989 Save tokens so that we can put them back. */
7990 cp_lexer_save_tokens (parser->lexer);
7992 /* We may be looking at a cast-expression. */
7993 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7994 /*consume_paren=*/true))
7995 cast_expression
7996 = cp_parser_tokens_start_cast_expression (parser);
7998 /* Roll back the tokens we skipped. */
7999 cp_lexer_rollback_tokens (parser->lexer);
8000 /* If we aren't looking at a cast-expression, simulate an error so
8001 that the call to cp_parser_error_occurred below returns true. */
8002 if (!cast_expression)
8003 cp_parser_simulate_error (parser);
8004 else
8006 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8007 parser->in_type_id_in_expr_p = true;
8008 /* Look for the type-id. */
8009 type = cp_parser_type_id (parser);
8010 /* Look for the closing `)'. */
8011 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8012 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8015 /* Restore the saved message. */
8016 parser->type_definition_forbidden_message = saved_message;
8018 /* At this point this can only be either a cast or a
8019 parenthesized ctor such as `(T ())' that looks like a cast to
8020 function returning T. */
8021 if (!cp_parser_error_occurred (parser))
8023 /* Only commit if the cast-expression doesn't start with
8024 '++', '--', or '[' in C++11. */
8025 if (cast_expression > 0)
8026 cp_parser_commit_to_topmost_tentative_parse (parser);
8028 expr = cp_parser_cast_expression (parser,
8029 /*address_p=*/false,
8030 /*cast_p=*/true,
8031 /*decltype_p=*/false,
8032 pidk);
8034 if (cp_parser_parse_definitely (parser))
8036 /* Warn about old-style casts, if so requested. */
8037 if (warn_old_style_cast
8038 && !in_system_header_at (input_location)
8039 && !VOID_TYPE_P (type)
8040 && current_lang_name != lang_name_c)
8041 warning (OPT_Wold_style_cast, "use of old-style cast");
8043 /* Only type conversions to integral or enumeration types
8044 can be used in constant-expressions. */
8045 if (!cast_valid_in_integral_constant_expression_p (type)
8046 && cp_parser_non_integral_constant_expression (parser,
8047 NIC_CAST))
8048 return error_mark_node;
8050 /* Perform the cast. */
8051 /* We don't want to resolve cast too early. Therefore we don't
8052 be able to use build_c_cast. */
8053 expr = cp_build_c_cast (type, expr, tf_warning_or_error);
8054 return expr;
8057 else
8058 cp_parser_abort_tentative_parse (parser);
8061 /* If we get here, then it's not a cast, so it must be a
8062 unary-expression. */
8063 return cp_parser_unary_expression (parser, pidk, address_p,
8064 cast_p, decltype_p);
8067 /* Parse a binary expression of the general form:
8069 pm-expression:
8070 cast-expression
8071 pm-expression .* cast-expression
8072 pm-expression ->* cast-expression
8074 multiplicative-expression:
8075 pm-expression
8076 multiplicative-expression * pm-expression
8077 multiplicative-expression / pm-expression
8078 multiplicative-expression % pm-expression
8080 additive-expression:
8081 multiplicative-expression
8082 additive-expression + multiplicative-expression
8083 additive-expression - multiplicative-expression
8085 shift-expression:
8086 additive-expression
8087 shift-expression << additive-expression
8088 shift-expression >> additive-expression
8090 relational-expression:
8091 shift-expression
8092 relational-expression < shift-expression
8093 relational-expression > shift-expression
8094 relational-expression <= shift-expression
8095 relational-expression >= shift-expression
8097 GNU Extension:
8099 relational-expression:
8100 relational-expression <? shift-expression
8101 relational-expression >? shift-expression
8103 equality-expression:
8104 relational-expression
8105 equality-expression == relational-expression
8106 equality-expression != relational-expression
8108 and-expression:
8109 equality-expression
8110 and-expression & equality-expression
8112 exclusive-or-expression:
8113 and-expression
8114 exclusive-or-expression ^ and-expression
8116 inclusive-or-expression:
8117 exclusive-or-expression
8118 inclusive-or-expression | exclusive-or-expression
8120 logical-and-expression:
8121 inclusive-or-expression
8122 logical-and-expression && inclusive-or-expression
8124 logical-or-expression:
8125 logical-and-expression
8126 logical-or-expression || logical-and-expression
8128 All these are implemented with a single function like:
8130 binary-expression:
8131 simple-cast-expression
8132 binary-expression <token> binary-expression
8134 CAST_P is true if this expression is the target of a cast.
8136 The binops_by_token map is used to get the tree codes for each <token> type.
8137 binary-expressions are associated according to a precedence table. */
8139 #define TOKEN_PRECEDENCE(token) \
8140 (((token->type == CPP_GREATER \
8141 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8142 && !parser->greater_than_is_operator_p) \
8143 ? PREC_NOT_OPERATOR \
8144 : binops_by_token[token->type].prec)
8146 static tree
8147 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8148 bool no_toplevel_fold_p,
8149 bool decltype_p,
8150 enum cp_parser_prec prec,
8151 cp_id_kind * pidk)
8153 cp_parser_expression_stack stack;
8154 cp_parser_expression_stack_entry *sp = &stack[0];
8155 cp_parser_expression_stack_entry current;
8156 tree rhs;
8157 cp_token *token;
8158 enum tree_code rhs_type;
8159 enum cp_parser_prec new_prec, lookahead_prec;
8160 tree overload;
8162 /* Parse the first expression. */
8163 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8164 ? TRUTH_NOT_EXPR : ERROR_MARK);
8165 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8166 cast_p, decltype_p, pidk);
8167 current.prec = prec;
8169 if (cp_parser_error_occurred (parser))
8170 return error_mark_node;
8172 for (;;)
8174 /* Get an operator token. */
8175 token = cp_lexer_peek_token (parser->lexer);
8177 if (warn_cxx0x_compat
8178 && token->type == CPP_RSHIFT
8179 && !parser->greater_than_is_operator_p)
8181 if (warning_at (token->location, OPT_Wc__0x_compat,
8182 "%<>>%> operator is treated"
8183 " as two right angle brackets in C++11"))
8184 inform (token->location,
8185 "suggest parentheses around %<>>%> expression");
8188 new_prec = TOKEN_PRECEDENCE (token);
8190 /* Popping an entry off the stack means we completed a subexpression:
8191 - either we found a token which is not an operator (`>' where it is not
8192 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8193 will happen repeatedly;
8194 - or, we found an operator which has lower priority. This is the case
8195 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8196 parsing `3 * 4'. */
8197 if (new_prec <= current.prec)
8199 if (sp == stack)
8200 break;
8201 else
8202 goto pop;
8205 get_rhs:
8206 current.tree_type = binops_by_token[token->type].tree_type;
8207 current.loc = token->location;
8209 /* We used the operator token. */
8210 cp_lexer_consume_token (parser->lexer);
8212 /* For "false && x" or "true || x", x will never be executed;
8213 disable warnings while evaluating it. */
8214 if (current.tree_type == TRUTH_ANDIF_EXPR)
8215 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8216 else if (current.tree_type == TRUTH_ORIF_EXPR)
8217 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8219 /* Extract another operand. It may be the RHS of this expression
8220 or the LHS of a new, higher priority expression. */
8221 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8222 ? TRUTH_NOT_EXPR : ERROR_MARK);
8223 rhs = cp_parser_simple_cast_expression (parser);
8225 /* Get another operator token. Look up its precedence to avoid
8226 building a useless (immediately popped) stack entry for common
8227 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8228 token = cp_lexer_peek_token (parser->lexer);
8229 lookahead_prec = TOKEN_PRECEDENCE (token);
8230 if (lookahead_prec > new_prec)
8232 /* ... and prepare to parse the RHS of the new, higher priority
8233 expression. Since precedence levels on the stack are
8234 monotonically increasing, we do not have to care about
8235 stack overflows. */
8236 *sp = current;
8237 ++sp;
8238 current.lhs = rhs;
8239 current.lhs_type = rhs_type;
8240 current.prec = new_prec;
8241 new_prec = lookahead_prec;
8242 goto get_rhs;
8244 pop:
8245 lookahead_prec = new_prec;
8246 /* If the stack is not empty, we have parsed into LHS the right side
8247 (`4' in the example above) of an expression we had suspended.
8248 We can use the information on the stack to recover the LHS (`3')
8249 from the stack together with the tree code (`MULT_EXPR'), and
8250 the precedence of the higher level subexpression
8251 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8252 which will be used to actually build the additive expression. */
8253 rhs = current.lhs;
8254 rhs_type = current.lhs_type;
8255 --sp;
8256 current = *sp;
8259 /* Undo the disabling of warnings done above. */
8260 if (current.tree_type == TRUTH_ANDIF_EXPR)
8261 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8262 else if (current.tree_type == TRUTH_ORIF_EXPR)
8263 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8265 if (warn_logical_not_paren
8266 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8267 && current.lhs_type == TRUTH_NOT_EXPR
8268 /* Avoid warning for !!x == y. */
8269 && (TREE_CODE (current.lhs) != NE_EXPR
8270 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8271 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8272 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8273 /* Avoid warning for !b == y where b is boolean. */
8274 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8275 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8276 != BOOLEAN_TYPE))))
8277 /* Avoid warning for !!b == y where b is boolean. */
8278 && (!DECL_P (current.lhs)
8279 || TREE_TYPE (current.lhs) == NULL_TREE
8280 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8281 warn_logical_not_parentheses (current.loc, current.tree_type,
8282 maybe_constant_value (rhs));
8284 overload = NULL;
8285 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8286 ERROR_MARK for everything that is not a binary expression.
8287 This makes warn_about_parentheses miss some warnings that
8288 involve unary operators. For unary expressions we should
8289 pass the correct tree_code unless the unary expression was
8290 surrounded by parentheses.
8292 if (no_toplevel_fold_p
8293 && lookahead_prec <= current.prec
8294 && sp == stack)
8295 current.lhs = build2 (current.tree_type,
8296 TREE_CODE_CLASS (current.tree_type)
8297 == tcc_comparison
8298 ? boolean_type_node : TREE_TYPE (current.lhs),
8299 current.lhs, rhs);
8300 else
8301 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8302 current.lhs, current.lhs_type,
8303 rhs, rhs_type, &overload,
8304 complain_flags (decltype_p));
8305 current.lhs_type = current.tree_type;
8306 if (EXPR_P (current.lhs))
8307 SET_EXPR_LOCATION (current.lhs, current.loc);
8309 /* If the binary operator required the use of an overloaded operator,
8310 then this expression cannot be an integral constant-expression.
8311 An overloaded operator can be used even if both operands are
8312 otherwise permissible in an integral constant-expression if at
8313 least one of the operands is of enumeration type. */
8315 if (overload
8316 && cp_parser_non_integral_constant_expression (parser,
8317 NIC_OVERLOADED))
8318 return error_mark_node;
8321 return current.lhs;
8324 static tree
8325 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8326 bool no_toplevel_fold_p,
8327 enum cp_parser_prec prec,
8328 cp_id_kind * pidk)
8330 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8331 /*decltype*/false, prec, pidk);
8334 /* Parse the `? expression : assignment-expression' part of a
8335 conditional-expression. The LOGICAL_OR_EXPR is the
8336 logical-or-expression that started the conditional-expression.
8337 Returns a representation of the entire conditional-expression.
8339 This routine is used by cp_parser_assignment_expression.
8341 ? expression : assignment-expression
8343 GNU Extensions:
8345 ? : assignment-expression */
8347 static tree
8348 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8350 tree expr;
8351 tree assignment_expr;
8352 struct cp_token *token;
8353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8355 /* Consume the `?' token. */
8356 cp_lexer_consume_token (parser->lexer);
8357 token = cp_lexer_peek_token (parser->lexer);
8358 if (cp_parser_allow_gnu_extensions_p (parser)
8359 && token->type == CPP_COLON)
8361 pedwarn (token->location, OPT_Wpedantic,
8362 "ISO C++ does not allow ?: with omitted middle operand");
8363 /* Implicit true clause. */
8364 expr = NULL_TREE;
8365 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8366 warn_for_omitted_condop (token->location, logical_or_expr);
8368 else
8370 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8371 parser->colon_corrects_to_scope_p = false;
8372 /* Parse the expression. */
8373 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8374 expr = cp_parser_expression (parser);
8375 c_inhibit_evaluation_warnings +=
8376 ((logical_or_expr == truthvalue_true_node)
8377 - (logical_or_expr == truthvalue_false_node));
8378 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8381 /* The next token should be a `:'. */
8382 cp_parser_require (parser, CPP_COLON, RT_COLON);
8383 /* Parse the assignment-expression. */
8384 assignment_expr = cp_parser_assignment_expression (parser);
8385 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8387 /* Build the conditional-expression. */
8388 return build_x_conditional_expr (loc, logical_or_expr,
8389 expr,
8390 assignment_expr,
8391 tf_warning_or_error);
8394 /* Parse an assignment-expression.
8396 assignment-expression:
8397 conditional-expression
8398 logical-or-expression assignment-operator assignment_expression
8399 throw-expression
8401 CAST_P is true if this expression is the target of a cast.
8402 DECLTYPE_P is true if this expression is the operand of decltype.
8404 Returns a representation for the expression. */
8406 static tree
8407 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8408 bool cast_p, bool decltype_p)
8410 tree expr;
8412 /* If the next token is the `throw' keyword, then we're looking at
8413 a throw-expression. */
8414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8415 expr = cp_parser_throw_expression (parser);
8416 /* Otherwise, it must be that we are looking at a
8417 logical-or-expression. */
8418 else
8420 /* Parse the binary expressions (logical-or-expression). */
8421 expr = cp_parser_binary_expression (parser, cast_p, false,
8422 decltype_p,
8423 PREC_NOT_OPERATOR, pidk);
8424 /* If the next token is a `?' then we're actually looking at a
8425 conditional-expression. */
8426 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8427 return cp_parser_question_colon_clause (parser, expr);
8428 else
8430 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8432 /* If it's an assignment-operator, we're using the second
8433 production. */
8434 enum tree_code assignment_operator
8435 = cp_parser_assignment_operator_opt (parser);
8436 if (assignment_operator != ERROR_MARK)
8438 bool non_constant_p;
8439 location_t saved_input_location;
8441 /* Parse the right-hand side of the assignment. */
8442 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8444 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8447 /* An assignment may not appear in a
8448 constant-expression. */
8449 if (cp_parser_non_integral_constant_expression (parser,
8450 NIC_ASSIGNMENT))
8451 return error_mark_node;
8452 /* Build the assignment expression. Its default
8453 location is the location of the '=' token. */
8454 saved_input_location = input_location;
8455 input_location = loc;
8456 expr = build_x_modify_expr (loc, expr,
8457 assignment_operator,
8458 rhs,
8459 complain_flags (decltype_p));
8460 input_location = saved_input_location;
8465 return expr;
8468 /* Parse an (optional) assignment-operator.
8470 assignment-operator: one of
8471 = *= /= %= += -= >>= <<= &= ^= |=
8473 GNU Extension:
8475 assignment-operator: one of
8476 <?= >?=
8478 If the next token is an assignment operator, the corresponding tree
8479 code is returned, and the token is consumed. For example, for
8480 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8481 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8482 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8483 operator, ERROR_MARK is returned. */
8485 static enum tree_code
8486 cp_parser_assignment_operator_opt (cp_parser* parser)
8488 enum tree_code op;
8489 cp_token *token;
8491 /* Peek at the next token. */
8492 token = cp_lexer_peek_token (parser->lexer);
8494 switch (token->type)
8496 case CPP_EQ:
8497 op = NOP_EXPR;
8498 break;
8500 case CPP_MULT_EQ:
8501 op = MULT_EXPR;
8502 break;
8504 case CPP_DIV_EQ:
8505 op = TRUNC_DIV_EXPR;
8506 break;
8508 case CPP_MOD_EQ:
8509 op = TRUNC_MOD_EXPR;
8510 break;
8512 case CPP_PLUS_EQ:
8513 op = PLUS_EXPR;
8514 break;
8516 case CPP_MINUS_EQ:
8517 op = MINUS_EXPR;
8518 break;
8520 case CPP_RSHIFT_EQ:
8521 op = RSHIFT_EXPR;
8522 break;
8524 case CPP_LSHIFT_EQ:
8525 op = LSHIFT_EXPR;
8526 break;
8528 case CPP_AND_EQ:
8529 op = BIT_AND_EXPR;
8530 break;
8532 case CPP_XOR_EQ:
8533 op = BIT_XOR_EXPR;
8534 break;
8536 case CPP_OR_EQ:
8537 op = BIT_IOR_EXPR;
8538 break;
8540 default:
8541 /* Nothing else is an assignment operator. */
8542 op = ERROR_MARK;
8545 /* If it was an assignment operator, consume it. */
8546 if (op != ERROR_MARK)
8547 cp_lexer_consume_token (parser->lexer);
8549 return op;
8552 /* Parse an expression.
8554 expression:
8555 assignment-expression
8556 expression , assignment-expression
8558 CAST_P is true if this expression is the target of a cast.
8559 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8560 except possibly parenthesized or on the RHS of a comma (N3276).
8562 Returns a representation of the expression. */
8564 static tree
8565 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8566 bool cast_p, bool decltype_p)
8568 tree expression = NULL_TREE;
8569 location_t loc = UNKNOWN_LOCATION;
8571 while (true)
8573 tree assignment_expression;
8575 /* Parse the next assignment-expression. */
8576 assignment_expression
8577 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8579 /* We don't create a temporary for a call that is the immediate operand
8580 of decltype or on the RHS of a comma. But when we see a comma, we
8581 need to create a temporary for a call on the LHS. */
8582 if (decltype_p && !processing_template_decl
8583 && TREE_CODE (assignment_expression) == CALL_EXPR
8584 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8585 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8586 assignment_expression
8587 = build_cplus_new (TREE_TYPE (assignment_expression),
8588 assignment_expression, tf_warning_or_error);
8590 /* If this is the first assignment-expression, we can just
8591 save it away. */
8592 if (!expression)
8593 expression = assignment_expression;
8594 else
8595 expression = build_x_compound_expr (loc, expression,
8596 assignment_expression,
8597 complain_flags (decltype_p));
8598 /* If the next token is not a comma, then we are done with the
8599 expression. */
8600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8601 break;
8602 /* Consume the `,'. */
8603 loc = cp_lexer_peek_token (parser->lexer)->location;
8604 cp_lexer_consume_token (parser->lexer);
8605 /* A comma operator cannot appear in a constant-expression. */
8606 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8607 expression = error_mark_node;
8610 return expression;
8613 /* Parse a constant-expression.
8615 constant-expression:
8616 conditional-expression
8618 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8619 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8620 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8621 is false, NON_CONSTANT_P should be NULL. */
8623 static tree
8624 cp_parser_constant_expression (cp_parser* parser,
8625 bool allow_non_constant_p,
8626 bool *non_constant_p)
8628 bool saved_integral_constant_expression_p;
8629 bool saved_allow_non_integral_constant_expression_p;
8630 bool saved_non_integral_constant_expression_p;
8631 tree expression;
8633 /* It might seem that we could simply parse the
8634 conditional-expression, and then check to see if it were
8635 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8636 one that the compiler can figure out is constant, possibly after
8637 doing some simplifications or optimizations. The standard has a
8638 precise definition of constant-expression, and we must honor
8639 that, even though it is somewhat more restrictive.
8641 For example:
8643 int i[(2, 3)];
8645 is not a legal declaration, because `(2, 3)' is not a
8646 constant-expression. The `,' operator is forbidden in a
8647 constant-expression. However, GCC's constant-folding machinery
8648 will fold this operation to an INTEGER_CST for `3'. */
8650 /* Save the old settings. */
8651 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8652 saved_allow_non_integral_constant_expression_p
8653 = parser->allow_non_integral_constant_expression_p;
8654 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8655 /* We are now parsing a constant-expression. */
8656 parser->integral_constant_expression_p = true;
8657 parser->allow_non_integral_constant_expression_p
8658 = (allow_non_constant_p || cxx_dialect >= cxx11);
8659 parser->non_integral_constant_expression_p = false;
8660 /* Although the grammar says "conditional-expression", we parse an
8661 "assignment-expression", which also permits "throw-expression"
8662 and the use of assignment operators. In the case that
8663 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8664 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8665 actually essential that we look for an assignment-expression.
8666 For example, cp_parser_initializer_clauses uses this function to
8667 determine whether a particular assignment-expression is in fact
8668 constant. */
8669 expression = cp_parser_assignment_expression (parser);
8670 /* Restore the old settings. */
8671 parser->integral_constant_expression_p
8672 = saved_integral_constant_expression_p;
8673 parser->allow_non_integral_constant_expression_p
8674 = saved_allow_non_integral_constant_expression_p;
8675 if (cxx_dialect >= cxx11)
8677 /* Require an rvalue constant expression here; that's what our
8678 callers expect. Reference constant expressions are handled
8679 separately in e.g. cp_parser_template_argument. */
8680 bool is_const = potential_rvalue_constant_expression (expression);
8681 parser->non_integral_constant_expression_p = !is_const;
8682 if (!is_const && !allow_non_constant_p)
8683 require_potential_rvalue_constant_expression (expression);
8685 if (allow_non_constant_p)
8686 *non_constant_p = parser->non_integral_constant_expression_p;
8687 parser->non_integral_constant_expression_p
8688 = saved_non_integral_constant_expression_p;
8690 return expression;
8693 /* Parse __builtin_offsetof.
8695 offsetof-expression:
8696 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8698 offsetof-member-designator:
8699 id-expression
8700 | offsetof-member-designator "." id-expression
8701 | offsetof-member-designator "[" expression "]"
8702 | offsetof-member-designator "->" id-expression */
8704 static tree
8705 cp_parser_builtin_offsetof (cp_parser *parser)
8707 int save_ice_p, save_non_ice_p;
8708 tree type, expr;
8709 cp_id_kind dummy;
8710 cp_token *token;
8712 /* We're about to accept non-integral-constant things, but will
8713 definitely yield an integral constant expression. Save and
8714 restore these values around our local parsing. */
8715 save_ice_p = parser->integral_constant_expression_p;
8716 save_non_ice_p = parser->non_integral_constant_expression_p;
8718 /* Consume the "__builtin_offsetof" token. */
8719 cp_lexer_consume_token (parser->lexer);
8720 /* Consume the opening `('. */
8721 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8722 /* Parse the type-id. */
8723 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8724 type = cp_parser_type_id (parser);
8725 /* Look for the `,'. */
8726 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8727 token = cp_lexer_peek_token (parser->lexer);
8729 /* Build the (type *)null that begins the traditional offsetof macro. */
8730 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8731 tf_warning_or_error);
8733 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8734 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8735 true, &dummy, token->location);
8736 while (true)
8738 token = cp_lexer_peek_token (parser->lexer);
8739 switch (token->type)
8741 case CPP_OPEN_SQUARE:
8742 /* offsetof-member-designator "[" expression "]" */
8743 expr = cp_parser_postfix_open_square_expression (parser, expr,
8744 true, false);
8745 break;
8747 case CPP_DEREF:
8748 /* offsetof-member-designator "->" identifier */
8749 expr = grok_array_decl (token->location, expr,
8750 integer_zero_node, false);
8751 /* FALLTHRU */
8753 case CPP_DOT:
8754 /* offsetof-member-designator "." identifier */
8755 cp_lexer_consume_token (parser->lexer);
8756 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8757 expr, true, &dummy,
8758 token->location);
8759 break;
8761 case CPP_CLOSE_PAREN:
8762 /* Consume the ")" token. */
8763 cp_lexer_consume_token (parser->lexer);
8764 goto success;
8766 default:
8767 /* Error. We know the following require will fail, but
8768 that gives the proper error message. */
8769 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8770 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8771 expr = error_mark_node;
8772 goto failure;
8776 success:
8777 expr = finish_offsetof (expr, loc);
8779 failure:
8780 parser->integral_constant_expression_p = save_ice_p;
8781 parser->non_integral_constant_expression_p = save_non_ice_p;
8783 return expr;
8786 /* Parse a trait expression.
8788 Returns a representation of the expression, the underlying type
8789 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8791 static tree
8792 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8794 cp_trait_kind kind;
8795 tree type1, type2 = NULL_TREE;
8796 bool binary = false;
8797 bool variadic = false;
8799 switch (keyword)
8801 case RID_HAS_NOTHROW_ASSIGN:
8802 kind = CPTK_HAS_NOTHROW_ASSIGN;
8803 break;
8804 case RID_HAS_NOTHROW_CONSTRUCTOR:
8805 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8806 break;
8807 case RID_HAS_NOTHROW_COPY:
8808 kind = CPTK_HAS_NOTHROW_COPY;
8809 break;
8810 case RID_HAS_TRIVIAL_ASSIGN:
8811 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8812 break;
8813 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8814 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8815 break;
8816 case RID_HAS_TRIVIAL_COPY:
8817 kind = CPTK_HAS_TRIVIAL_COPY;
8818 break;
8819 case RID_HAS_TRIVIAL_DESTRUCTOR:
8820 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8821 break;
8822 case RID_HAS_VIRTUAL_DESTRUCTOR:
8823 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8824 break;
8825 case RID_IS_ABSTRACT:
8826 kind = CPTK_IS_ABSTRACT;
8827 break;
8828 case RID_IS_BASE_OF:
8829 kind = CPTK_IS_BASE_OF;
8830 binary = true;
8831 break;
8832 case RID_IS_CLASS:
8833 kind = CPTK_IS_CLASS;
8834 break;
8835 case RID_IS_EMPTY:
8836 kind = CPTK_IS_EMPTY;
8837 break;
8838 case RID_IS_ENUM:
8839 kind = CPTK_IS_ENUM;
8840 break;
8841 case RID_IS_FINAL:
8842 kind = CPTK_IS_FINAL;
8843 break;
8844 case RID_IS_LITERAL_TYPE:
8845 kind = CPTK_IS_LITERAL_TYPE;
8846 break;
8847 case RID_IS_POD:
8848 kind = CPTK_IS_POD;
8849 break;
8850 case RID_IS_POLYMORPHIC:
8851 kind = CPTK_IS_POLYMORPHIC;
8852 break;
8853 case RID_IS_STD_LAYOUT:
8854 kind = CPTK_IS_STD_LAYOUT;
8855 break;
8856 case RID_IS_TRIVIAL:
8857 kind = CPTK_IS_TRIVIAL;
8858 break;
8859 case RID_IS_TRIVIALLY_ASSIGNABLE:
8860 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8861 binary = true;
8862 break;
8863 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8864 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8865 variadic = true;
8866 break;
8867 case RID_IS_TRIVIALLY_COPYABLE:
8868 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8869 break;
8870 case RID_IS_UNION:
8871 kind = CPTK_IS_UNION;
8872 break;
8873 case RID_UNDERLYING_TYPE:
8874 kind = CPTK_UNDERLYING_TYPE;
8875 break;
8876 case RID_BASES:
8877 kind = CPTK_BASES;
8878 break;
8879 case RID_DIRECT_BASES:
8880 kind = CPTK_DIRECT_BASES;
8881 break;
8882 default:
8883 gcc_unreachable ();
8886 /* Consume the token. */
8887 cp_lexer_consume_token (parser->lexer);
8889 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8891 type1 = cp_parser_type_id (parser);
8893 if (type1 == error_mark_node)
8894 return error_mark_node;
8896 if (binary)
8898 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8900 type2 = cp_parser_type_id (parser);
8902 if (type2 == error_mark_node)
8903 return error_mark_node;
8905 else if (variadic)
8907 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8909 cp_lexer_consume_token (parser->lexer);
8910 tree elt = cp_parser_type_id (parser);
8911 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8913 cp_lexer_consume_token (parser->lexer);
8914 elt = make_pack_expansion (elt);
8916 if (elt == error_mark_node)
8917 return error_mark_node;
8918 type2 = tree_cons (NULL_TREE, elt, type2);
8922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8924 /* Complete the trait expression, which may mean either processing
8925 the trait expr now or saving it for template instantiation. */
8926 switch(kind)
8928 case CPTK_UNDERLYING_TYPE:
8929 return finish_underlying_type (type1);
8930 case CPTK_BASES:
8931 return finish_bases (type1, false);
8932 case CPTK_DIRECT_BASES:
8933 return finish_bases (type1, true);
8934 default:
8935 return finish_trait_expr (kind, type1, type2);
8939 /* Lambdas that appear in variable initializer or default argument scope
8940 get that in their mangling, so we need to record it. We might as well
8941 use the count for function and namespace scopes as well. */
8942 static GTY(()) tree lambda_scope;
8943 static GTY(()) int lambda_count;
8944 typedef struct GTY(()) tree_int
8946 tree t;
8947 int i;
8948 } tree_int;
8949 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8951 static void
8952 start_lambda_scope (tree decl)
8954 tree_int ti;
8955 gcc_assert (decl);
8956 /* Once we're inside a function, we ignore other scopes and just push
8957 the function again so that popping works properly. */
8958 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8959 decl = current_function_decl;
8960 ti.t = lambda_scope;
8961 ti.i = lambda_count;
8962 vec_safe_push (lambda_scope_stack, ti);
8963 if (lambda_scope != decl)
8965 /* Don't reset the count if we're still in the same function. */
8966 lambda_scope = decl;
8967 lambda_count = 0;
8971 static void
8972 record_lambda_scope (tree lambda)
8974 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8975 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8978 static void
8979 finish_lambda_scope (void)
8981 tree_int *p = &lambda_scope_stack->last ();
8982 if (lambda_scope != p->t)
8984 lambda_scope = p->t;
8985 lambda_count = p->i;
8987 lambda_scope_stack->pop ();
8990 /* Parse a lambda expression.
8992 lambda-expression:
8993 lambda-introducer lambda-declarator [opt] compound-statement
8995 Returns a representation of the expression. */
8997 static tree
8998 cp_parser_lambda_expression (cp_parser* parser)
9000 tree lambda_expr = build_lambda_expr ();
9001 tree type;
9002 bool ok = true;
9003 cp_token *token = cp_lexer_peek_token (parser->lexer);
9004 cp_token_position start = 0;
9006 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9008 if (cp_unevaluated_operand)
9010 if (!token->error_reported)
9012 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9013 "lambda-expression in unevaluated context");
9014 token->error_reported = true;
9016 ok = false;
9018 else if (parser->in_template_argument_list_p)
9020 if (!token->error_reported)
9022 error_at (token->location, "lambda-expression in template-argument");
9023 token->error_reported = true;
9025 ok = false;
9028 /* We may be in the middle of deferred access check. Disable
9029 it now. */
9030 push_deferring_access_checks (dk_no_deferred);
9032 cp_parser_lambda_introducer (parser, lambda_expr);
9034 type = begin_lambda_type (lambda_expr);
9035 if (type == error_mark_node)
9036 return error_mark_node;
9038 record_lambda_scope (lambda_expr);
9040 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9041 determine_visibility (TYPE_NAME (type));
9043 /* Now that we've started the type, add the capture fields for any
9044 explicit captures. */
9045 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9048 /* Inside the class, surrounding template-parameter-lists do not apply. */
9049 unsigned int saved_num_template_parameter_lists
9050 = parser->num_template_parameter_lists;
9051 unsigned char in_statement = parser->in_statement;
9052 bool in_switch_statement_p = parser->in_switch_statement_p;
9053 bool fully_implicit_function_template_p
9054 = parser->fully_implicit_function_template_p;
9055 tree implicit_template_parms = parser->implicit_template_parms;
9056 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9057 bool auto_is_implicit_function_template_parm_p
9058 = parser->auto_is_implicit_function_template_parm_p;
9060 parser->num_template_parameter_lists = 0;
9061 parser->in_statement = 0;
9062 parser->in_switch_statement_p = false;
9063 parser->fully_implicit_function_template_p = false;
9064 parser->implicit_template_parms = 0;
9065 parser->implicit_template_scope = 0;
9066 parser->auto_is_implicit_function_template_parm_p = false;
9068 /* By virtue of defining a local class, a lambda expression has access to
9069 the private variables of enclosing classes. */
9071 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9073 if (ok)
9075 if (!cp_parser_error_occurred (parser)
9076 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9077 && cp_parser_start_tentative_firewall (parser))
9078 start = token;
9079 cp_parser_lambda_body (parser, lambda_expr);
9081 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9083 if (cp_parser_skip_to_closing_brace (parser))
9084 cp_lexer_consume_token (parser->lexer);
9087 /* The capture list was built up in reverse order; fix that now. */
9088 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9089 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9091 if (ok)
9092 maybe_add_lambda_conv_op (type);
9094 type = finish_struct (type, /*attributes=*/NULL_TREE);
9096 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9097 parser->in_statement = in_statement;
9098 parser->in_switch_statement_p = in_switch_statement_p;
9099 parser->fully_implicit_function_template_p
9100 = fully_implicit_function_template_p;
9101 parser->implicit_template_parms = implicit_template_parms;
9102 parser->implicit_template_scope = implicit_template_scope;
9103 parser->auto_is_implicit_function_template_parm_p
9104 = auto_is_implicit_function_template_parm_p;
9107 pop_deferring_access_checks ();
9109 /* This field is only used during parsing of the lambda. */
9110 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9112 /* This lambda shouldn't have any proxies left at this point. */
9113 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9114 /* And now that we're done, push proxies for an enclosing lambda. */
9115 insert_pending_capture_proxies ();
9117 if (ok)
9118 lambda_expr = build_lambda_object (lambda_expr);
9119 else
9120 lambda_expr = error_mark_node;
9122 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9124 return lambda_expr;
9127 /* Parse the beginning of a lambda expression.
9129 lambda-introducer:
9130 [ lambda-capture [opt] ]
9132 LAMBDA_EXPR is the current representation of the lambda expression. */
9134 static void
9135 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9137 /* Need commas after the first capture. */
9138 bool first = true;
9140 /* Eat the leading `['. */
9141 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9143 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9144 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9145 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9146 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9147 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9148 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9150 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9152 cp_lexer_consume_token (parser->lexer);
9153 first = false;
9156 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9158 cp_token* capture_token;
9159 tree capture_id;
9160 tree capture_init_expr;
9161 cp_id_kind idk = CP_ID_KIND_NONE;
9162 bool explicit_init_p = false;
9164 enum capture_kind_type
9166 BY_COPY,
9167 BY_REFERENCE
9169 enum capture_kind_type capture_kind = BY_COPY;
9171 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9173 error ("expected end of capture-list");
9174 return;
9177 if (first)
9178 first = false;
9179 else
9180 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9182 /* Possibly capture `this'. */
9183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9186 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9187 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9188 "with by-copy capture default");
9189 cp_lexer_consume_token (parser->lexer);
9190 add_capture (lambda_expr,
9191 /*id=*/this_identifier,
9192 /*initializer=*/finish_this_expr(),
9193 /*by_reference_p=*/false,
9194 explicit_init_p);
9195 continue;
9198 /* Remember whether we want to capture as a reference or not. */
9199 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9201 capture_kind = BY_REFERENCE;
9202 cp_lexer_consume_token (parser->lexer);
9205 /* Get the identifier. */
9206 capture_token = cp_lexer_peek_token (parser->lexer);
9207 capture_id = cp_parser_identifier (parser);
9209 if (capture_id == error_mark_node)
9210 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9211 delimiters, but I modified this to stop on unnested ']' as well. It
9212 was already changed to stop on unnested '}', so the
9213 "closing_parenthesis" name is no more misleading with my change. */
9215 cp_parser_skip_to_closing_parenthesis (parser,
9216 /*recovering=*/true,
9217 /*or_comma=*/true,
9218 /*consume_paren=*/true);
9219 break;
9222 /* Find the initializer for this capture. */
9223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9224 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9225 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9227 bool direct, non_constant;
9228 /* An explicit initializer exists. */
9229 if (cxx_dialect < cxx14)
9230 pedwarn (input_location, 0,
9231 "lambda capture initializers "
9232 "only available with -std=c++14 or -std=gnu++14");
9233 capture_init_expr = cp_parser_initializer (parser, &direct,
9234 &non_constant);
9235 explicit_init_p = true;
9236 if (capture_init_expr == NULL_TREE)
9238 error ("empty initializer for lambda init-capture");
9239 capture_init_expr = error_mark_node;
9242 else
9244 const char* error_msg;
9246 /* Turn the identifier into an id-expression. */
9247 capture_init_expr
9248 = cp_parser_lookup_name_simple (parser, capture_id,
9249 capture_token->location);
9251 if (capture_init_expr == error_mark_node)
9253 unqualified_name_lookup_error (capture_id);
9254 continue;
9256 else if (DECL_P (capture_init_expr)
9257 && (!VAR_P (capture_init_expr)
9258 && TREE_CODE (capture_init_expr) != PARM_DECL))
9260 error_at (capture_token->location,
9261 "capture of non-variable %qD ",
9262 capture_init_expr);
9263 inform (0, "%q+#D declared here", capture_init_expr);
9264 continue;
9266 if (VAR_P (capture_init_expr)
9267 && decl_storage_duration (capture_init_expr) != dk_auto)
9269 if (pedwarn (capture_token->location, 0, "capture of variable "
9270 "%qD with non-automatic storage duration",
9271 capture_init_expr))
9272 inform (0, "%q+#D declared here", capture_init_expr);
9273 continue;
9276 capture_init_expr
9277 = finish_id_expression
9278 (capture_id,
9279 capture_init_expr,
9280 parser->scope,
9281 &idk,
9282 /*integral_constant_expression_p=*/false,
9283 /*allow_non_integral_constant_expression_p=*/false,
9284 /*non_integral_constant_expression_p=*/NULL,
9285 /*template_p=*/false,
9286 /*done=*/true,
9287 /*address_p=*/false,
9288 /*template_arg_p=*/false,
9289 &error_msg,
9290 capture_token->location);
9292 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9294 cp_lexer_consume_token (parser->lexer);
9295 capture_init_expr = make_pack_expansion (capture_init_expr);
9297 else
9298 check_for_bare_parameter_packs (capture_init_expr);
9301 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9302 && !explicit_init_p)
9304 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9305 && capture_kind == BY_COPY)
9306 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9307 "of %qD redundant with by-copy capture default",
9308 capture_id);
9309 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9310 && capture_kind == BY_REFERENCE)
9311 pedwarn (capture_token->location, 0, "explicit by-reference "
9312 "capture of %qD redundant with by-reference capture "
9313 "default", capture_id);
9316 add_capture (lambda_expr,
9317 capture_id,
9318 capture_init_expr,
9319 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9320 explicit_init_p);
9323 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9326 /* Parse the (optional) middle of a lambda expression.
9328 lambda-declarator:
9329 < template-parameter-list [opt] >
9330 ( parameter-declaration-clause [opt] )
9331 attribute-specifier [opt]
9332 mutable [opt]
9333 exception-specification [opt]
9334 lambda-return-type-clause [opt]
9336 LAMBDA_EXPR is the current representation of the lambda expression. */
9338 static bool
9339 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9341 /* 5.1.1.4 of the standard says:
9342 If a lambda-expression does not include a lambda-declarator, it is as if
9343 the lambda-declarator were ().
9344 This means an empty parameter list, no attributes, and no exception
9345 specification. */
9346 tree param_list = void_list_node;
9347 tree attributes = NULL_TREE;
9348 tree exception_spec = NULL_TREE;
9349 tree template_param_list = NULL_TREE;
9351 /* The template-parameter-list is optional, but must begin with
9352 an opening angle if present. */
9353 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9355 if (cxx_dialect < cxx14)
9356 pedwarn (parser->lexer->next_token->location, 0,
9357 "lambda templates are only available with "
9358 "-std=c++14 or -std=gnu++14");
9360 cp_lexer_consume_token (parser->lexer);
9362 template_param_list = cp_parser_template_parameter_list (parser);
9364 cp_parser_skip_to_end_of_template_parameter_list (parser);
9366 /* We just processed one more parameter list. */
9367 ++parser->num_template_parameter_lists;
9370 /* The parameter-declaration-clause is optional (unless
9371 template-parameter-list was given), but must begin with an
9372 opening parenthesis if present. */
9373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9375 cp_lexer_consume_token (parser->lexer);
9377 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9379 /* Parse parameters. */
9380 param_list = cp_parser_parameter_declaration_clause (parser);
9382 /* Default arguments shall not be specified in the
9383 parameter-declaration-clause of a lambda-declarator. */
9384 for (tree t = param_list; t; t = TREE_CHAIN (t))
9385 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9386 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9387 "default argument specified for lambda parameter");
9389 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9391 attributes = cp_parser_attributes_opt (parser);
9393 /* Parse optional `mutable' keyword. */
9394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9396 cp_lexer_consume_token (parser->lexer);
9397 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9400 /* Parse optional exception specification. */
9401 exception_spec = cp_parser_exception_specification_opt (parser);
9403 /* Parse optional trailing return type. */
9404 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9406 cp_lexer_consume_token (parser->lexer);
9407 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9408 = cp_parser_trailing_type_id (parser);
9411 /* The function parameters must be in scope all the way until after the
9412 trailing-return-type in case of decltype. */
9413 pop_bindings_and_leave_scope ();
9415 else if (template_param_list != NULL_TREE) // generate diagnostic
9416 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9418 /* Create the function call operator.
9420 Messing with declarators like this is no uglier than building up the
9421 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9422 other code. */
9424 cp_decl_specifier_seq return_type_specs;
9425 cp_declarator* declarator;
9426 tree fco;
9427 int quals;
9428 void *p;
9430 clear_decl_specs (&return_type_specs);
9431 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9432 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9433 else
9434 /* Maybe we will deduce the return type later. */
9435 return_type_specs.type = make_auto ();
9437 p = obstack_alloc (&declarator_obstack, 0);
9439 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9440 sfk_none);
9442 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9443 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9444 declarator = make_call_declarator (declarator, param_list, quals,
9445 VIRT_SPEC_UNSPECIFIED,
9446 REF_QUAL_NONE,
9447 exception_spec,
9448 /*late_return_type=*/NULL_TREE);
9449 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9451 fco = grokmethod (&return_type_specs,
9452 declarator,
9453 attributes);
9454 if (fco != error_mark_node)
9456 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9457 DECL_ARTIFICIAL (fco) = 1;
9458 /* Give the object parameter a different name. */
9459 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9460 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9461 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9463 if (template_param_list)
9465 fco = finish_member_template_decl (fco);
9466 finish_template_decl (template_param_list);
9467 --parser->num_template_parameter_lists;
9469 else if (parser->fully_implicit_function_template_p)
9470 fco = finish_fully_implicit_template (parser, fco);
9472 finish_member_declaration (fco);
9474 obstack_free (&declarator_obstack, p);
9476 return (fco != error_mark_node);
9480 /* Parse the body of a lambda expression, which is simply
9482 compound-statement
9484 but which requires special handling.
9485 LAMBDA_EXPR is the current representation of the lambda expression. */
9487 static void
9488 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9490 bool nested = (current_function_decl != NULL_TREE);
9491 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9492 if (nested)
9493 push_function_context ();
9494 else
9495 /* Still increment function_depth so that we don't GC in the
9496 middle of an expression. */
9497 ++function_depth;
9498 /* Clear this in case we're in the middle of a default argument. */
9499 parser->local_variables_forbidden_p = false;
9501 /* Finish the function call operator
9502 - class_specifier
9503 + late_parsing_for_member
9504 + function_definition_after_declarator
9505 + ctor_initializer_opt_and_function_body */
9507 tree fco = lambda_function (lambda_expr);
9508 tree body;
9509 bool done = false;
9510 tree compound_stmt;
9511 tree cap;
9513 /* Let the front end know that we are going to be defining this
9514 function. */
9515 start_preparsed_function (fco,
9516 NULL_TREE,
9517 SF_PRE_PARSED | SF_INCLASS_INLINE);
9519 start_lambda_scope (fco);
9520 body = begin_function_body ();
9522 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9523 goto out;
9525 /* Push the proxies for any explicit captures. */
9526 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9527 cap = TREE_CHAIN (cap))
9528 build_capture_proxy (TREE_PURPOSE (cap));
9530 compound_stmt = begin_compound_stmt (0);
9532 /* 5.1.1.4 of the standard says:
9533 If a lambda-expression does not include a trailing-return-type, it
9534 is as if the trailing-return-type denotes the following type:
9535 * if the compound-statement is of the form
9536 { return attribute-specifier [opt] expression ; }
9537 the type of the returned expression after lvalue-to-rvalue
9538 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9539 (_conv.array_ 4.2), and function-to-pointer conversion
9540 (_conv.func_ 4.3);
9541 * otherwise, void. */
9543 /* In a lambda that has neither a lambda-return-type-clause
9544 nor a deducible form, errors should be reported for return statements
9545 in the body. Since we used void as the placeholder return type, parsing
9546 the body as usual will give such desired behavior. */
9547 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9548 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9549 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9551 tree expr = NULL_TREE;
9552 cp_id_kind idk = CP_ID_KIND_NONE;
9554 /* Parse tentatively in case there's more after the initial return
9555 statement. */
9556 cp_parser_parse_tentatively (parser);
9558 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9560 expr = cp_parser_expression (parser, &idk);
9562 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9563 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9565 if (cp_parser_parse_definitely (parser))
9567 if (!processing_template_decl)
9568 apply_deduced_return_type (fco, lambda_return_type (expr));
9570 /* Will get error here if type not deduced yet. */
9571 finish_return_stmt (expr);
9573 done = true;
9577 if (!done)
9579 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9580 cp_parser_label_declaration (parser);
9581 cp_parser_statement_seq_opt (parser, NULL_TREE);
9582 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9585 finish_compound_stmt (compound_stmt);
9587 out:
9588 finish_function_body (body);
9589 finish_lambda_scope ();
9591 /* Finish the function and generate code for it if necessary. */
9592 tree fn = finish_function (/*inline*/2);
9594 /* Only expand if the call op is not a template. */
9595 if (!DECL_TEMPLATE_INFO (fco))
9596 expand_or_defer_fn (fn);
9599 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9600 if (nested)
9601 pop_function_context();
9602 else
9603 --function_depth;
9606 /* Statements [gram.stmt.stmt] */
9608 /* Parse a statement.
9610 statement:
9611 labeled-statement
9612 expression-statement
9613 compound-statement
9614 selection-statement
9615 iteration-statement
9616 jump-statement
9617 declaration-statement
9618 try-block
9620 C++11:
9622 statement:
9623 labeled-statement
9624 attribute-specifier-seq (opt) expression-statement
9625 attribute-specifier-seq (opt) compound-statement
9626 attribute-specifier-seq (opt) selection-statement
9627 attribute-specifier-seq (opt) iteration-statement
9628 attribute-specifier-seq (opt) jump-statement
9629 declaration-statement
9630 attribute-specifier-seq (opt) try-block
9632 TM Extension:
9634 statement:
9635 atomic-statement
9637 IN_COMPOUND is true when the statement is nested inside a
9638 cp_parser_compound_statement; this matters for certain pragmas.
9640 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9641 is a (possibly labeled) if statement which is not enclosed in braces
9642 and has an else clause. This is used to implement -Wparentheses. */
9644 static void
9645 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9646 bool in_compound, bool *if_p)
9648 tree statement, std_attrs = NULL_TREE;
9649 cp_token *token;
9650 location_t statement_location, attrs_location;
9652 restart:
9653 if (if_p != NULL)
9654 *if_p = false;
9655 /* There is no statement yet. */
9656 statement = NULL_TREE;
9658 saved_token_sentinel saved_tokens (parser->lexer);
9659 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9660 if (c_dialect_objc ())
9661 /* In obj-c++, seeing '[[' might be the either the beginning of
9662 c++11 attributes, or a nested objc-message-expression. So
9663 let's parse the c++11 attributes tentatively. */
9664 cp_parser_parse_tentatively (parser);
9665 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9666 if (c_dialect_objc ())
9668 if (!cp_parser_parse_definitely (parser))
9669 std_attrs = NULL_TREE;
9672 /* Peek at the next token. */
9673 token = cp_lexer_peek_token (parser->lexer);
9674 /* Remember the location of the first token in the statement. */
9675 statement_location = token->location;
9676 /* If this is a keyword, then that will often determine what kind of
9677 statement we have. */
9678 if (token->type == CPP_KEYWORD)
9680 enum rid keyword = token->keyword;
9682 switch (keyword)
9684 case RID_CASE:
9685 case RID_DEFAULT:
9686 /* Looks like a labeled-statement with a case label.
9687 Parse the label, and then use tail recursion to parse
9688 the statement. */
9689 cp_parser_label_for_labeled_statement (parser, std_attrs);
9690 goto restart;
9692 case RID_IF:
9693 case RID_SWITCH:
9694 statement = cp_parser_selection_statement (parser, if_p);
9695 break;
9697 case RID_WHILE:
9698 case RID_DO:
9699 case RID_FOR:
9700 statement = cp_parser_iteration_statement (parser, false);
9701 break;
9703 case RID_CILK_FOR:
9704 if (!flag_cilkplus)
9706 error_at (cp_lexer_peek_token (parser->lexer)->location,
9707 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9708 cp_lexer_consume_token (parser->lexer);
9709 statement = error_mark_node;
9711 else
9712 statement = cp_parser_cilk_for (parser, integer_zero_node);
9713 break;
9715 case RID_BREAK:
9716 case RID_CONTINUE:
9717 case RID_RETURN:
9718 case RID_GOTO:
9719 statement = cp_parser_jump_statement (parser);
9720 break;
9722 case RID_CILK_SYNC:
9723 cp_lexer_consume_token (parser->lexer);
9724 if (flag_cilkplus)
9726 tree sync_expr = build_cilk_sync ();
9727 SET_EXPR_LOCATION (sync_expr,
9728 token->location);
9729 statement = finish_expr_stmt (sync_expr);
9731 else
9733 error_at (token->location, "-fcilkplus must be enabled to use"
9734 " %<_Cilk_sync%>");
9735 statement = error_mark_node;
9737 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9738 break;
9740 /* Objective-C++ exception-handling constructs. */
9741 case RID_AT_TRY:
9742 case RID_AT_CATCH:
9743 case RID_AT_FINALLY:
9744 case RID_AT_SYNCHRONIZED:
9745 case RID_AT_THROW:
9746 statement = cp_parser_objc_statement (parser);
9747 break;
9749 case RID_TRY:
9750 statement = cp_parser_try_block (parser);
9751 break;
9753 case RID_NAMESPACE:
9754 /* This must be a namespace alias definition. */
9755 cp_parser_declaration_statement (parser);
9756 return;
9758 case RID_TRANSACTION_ATOMIC:
9759 case RID_TRANSACTION_RELAXED:
9760 statement = cp_parser_transaction (parser, keyword);
9761 break;
9762 case RID_TRANSACTION_CANCEL:
9763 statement = cp_parser_transaction_cancel (parser);
9764 break;
9766 default:
9767 /* It might be a keyword like `int' that can start a
9768 declaration-statement. */
9769 break;
9772 else if (token->type == CPP_NAME)
9774 /* If the next token is a `:', then we are looking at a
9775 labeled-statement. */
9776 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9777 if (token->type == CPP_COLON)
9779 /* Looks like a labeled-statement with an ordinary label.
9780 Parse the label, and then use tail recursion to parse
9781 the statement. */
9783 cp_parser_label_for_labeled_statement (parser, std_attrs);
9784 goto restart;
9787 /* Anything that starts with a `{' must be a compound-statement. */
9788 else if (token->type == CPP_OPEN_BRACE)
9789 statement = cp_parser_compound_statement (parser, NULL, false, false);
9790 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9791 a statement all its own. */
9792 else if (token->type == CPP_PRAGMA)
9794 /* Only certain OpenMP pragmas are attached to statements, and thus
9795 are considered statements themselves. All others are not. In
9796 the context of a compound, accept the pragma as a "statement" and
9797 return so that we can check for a close brace. Otherwise we
9798 require a real statement and must go back and read one. */
9799 if (in_compound)
9800 cp_parser_pragma (parser, pragma_compound);
9801 else if (!cp_parser_pragma (parser, pragma_stmt))
9802 goto restart;
9803 return;
9805 else if (token->type == CPP_EOF)
9807 cp_parser_error (parser, "expected statement");
9808 return;
9811 /* Everything else must be a declaration-statement or an
9812 expression-statement. Try for the declaration-statement
9813 first, unless we are looking at a `;', in which case we know that
9814 we have an expression-statement. */
9815 if (!statement)
9817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9819 if (std_attrs != NULL_TREE)
9821 /* Attributes should be parsed as part of the the
9822 declaration, so let's un-parse them. */
9823 saved_tokens.rollback();
9824 std_attrs = NULL_TREE;
9827 cp_parser_parse_tentatively (parser);
9828 /* Try to parse the declaration-statement. */
9829 cp_parser_declaration_statement (parser);
9830 /* If that worked, we're done. */
9831 if (cp_parser_parse_definitely (parser))
9832 return;
9834 /* Look for an expression-statement instead. */
9835 statement = cp_parser_expression_statement (parser, in_statement_expr);
9838 /* Set the line number for the statement. */
9839 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9840 SET_EXPR_LOCATION (statement, statement_location);
9842 /* Note that for now, we don't do anything with c++11 statements
9843 parsed at this level. */
9844 if (std_attrs != NULL_TREE)
9845 warning_at (attrs_location,
9846 OPT_Wattributes,
9847 "attributes at the beginning of statement are ignored");
9850 /* Parse the label for a labeled-statement, i.e.
9852 identifier :
9853 case constant-expression :
9854 default :
9856 GNU Extension:
9857 case constant-expression ... constant-expression : statement
9859 When a label is parsed without errors, the label is added to the
9860 parse tree by the finish_* functions, so this function doesn't
9861 have to return the label. */
9863 static void
9864 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9866 cp_token *token;
9867 tree label = NULL_TREE;
9868 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9870 /* The next token should be an identifier. */
9871 token = cp_lexer_peek_token (parser->lexer);
9872 if (token->type != CPP_NAME
9873 && token->type != CPP_KEYWORD)
9875 cp_parser_error (parser, "expected labeled-statement");
9876 return;
9879 parser->colon_corrects_to_scope_p = false;
9880 switch (token->keyword)
9882 case RID_CASE:
9884 tree expr, expr_hi;
9885 cp_token *ellipsis;
9887 /* Consume the `case' token. */
9888 cp_lexer_consume_token (parser->lexer);
9889 /* Parse the constant-expression. */
9890 expr = cp_parser_constant_expression (parser);
9891 expr = maybe_constant_value (expr);
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 expr_hi = maybe_constant_value (expr_hi);
9902 if (check_for_bare_parameter_packs (expr_hi))
9903 expr_hi = error_mark_node;
9905 /* We don't need to emit warnings here, as the common code
9906 will do this for us. */
9908 else
9909 expr_hi = NULL_TREE;
9911 if (parser->in_switch_statement_p)
9912 finish_case_label (token->location, expr, expr_hi);
9913 else
9914 error_at (token->location,
9915 "case label %qE not within a switch statement",
9916 expr);
9918 break;
9920 case RID_DEFAULT:
9921 /* Consume the `default' token. */
9922 cp_lexer_consume_token (parser->lexer);
9924 if (parser->in_switch_statement_p)
9925 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9926 else
9927 error_at (token->location, "case label not within a switch statement");
9928 break;
9930 default:
9931 /* Anything else must be an ordinary label. */
9932 label = finish_label_stmt (cp_parser_identifier (parser));
9933 break;
9936 /* Require the `:' token. */
9937 cp_parser_require (parser, CPP_COLON, RT_COLON);
9939 /* An ordinary label may optionally be followed by attributes.
9940 However, this is only permitted if the attributes are then
9941 followed by a semicolon. This is because, for backward
9942 compatibility, when parsing
9943 lab: __attribute__ ((unused)) int i;
9944 we want the attribute to attach to "i", not "lab". */
9945 if (label != NULL_TREE
9946 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9948 tree attrs;
9949 cp_parser_parse_tentatively (parser);
9950 attrs = cp_parser_gnu_attributes_opt (parser);
9951 if (attrs == NULL_TREE
9952 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9953 cp_parser_abort_tentative_parse (parser);
9954 else if (!cp_parser_parse_definitely (parser))
9956 else
9957 attributes = chainon (attributes, attrs);
9960 if (attributes != NULL_TREE)
9961 cplus_decl_attributes (&label, attributes, 0);
9963 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9966 /* Parse an expression-statement.
9968 expression-statement:
9969 expression [opt] ;
9971 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9972 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9973 indicates whether this expression-statement is part of an
9974 expression statement. */
9976 static tree
9977 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9979 tree statement = NULL_TREE;
9980 cp_token *token = cp_lexer_peek_token (parser->lexer);
9982 /* If the next token is a ';', then there is no expression
9983 statement. */
9984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9986 statement = cp_parser_expression (parser);
9987 if (statement == error_mark_node
9988 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9990 cp_parser_skip_to_end_of_block_or_statement (parser);
9991 return error_mark_node;
9995 /* Give a helpful message for "A<T>::type t;" and the like. */
9996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9997 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9999 if (TREE_CODE (statement) == SCOPE_REF)
10000 error_at (token->location, "need %<typename%> before %qE because "
10001 "%qT is a dependent scope",
10002 statement, TREE_OPERAND (statement, 0));
10003 else if (is_overloaded_fn (statement)
10004 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10006 /* A::A a; */
10007 tree fn = get_first_fn (statement);
10008 error_at (token->location,
10009 "%<%T::%D%> names the constructor, not the type",
10010 DECL_CONTEXT (fn), DECL_NAME (fn));
10014 /* Consume the final `;'. */
10015 cp_parser_consume_semicolon_at_end_of_statement (parser);
10017 if (in_statement_expr
10018 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10019 /* This is the final expression statement of a statement
10020 expression. */
10021 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10022 else if (statement)
10023 statement = finish_expr_stmt (statement);
10025 return statement;
10028 /* Parse a compound-statement.
10030 compound-statement:
10031 { statement-seq [opt] }
10033 GNU extension:
10035 compound-statement:
10036 { label-declaration-seq [opt] statement-seq [opt] }
10038 label-declaration-seq:
10039 label-declaration
10040 label-declaration-seq label-declaration
10042 Returns a tree representing the statement. */
10044 static tree
10045 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10046 bool in_try, bool function_body)
10048 tree compound_stmt;
10050 /* Consume the `{'. */
10051 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10052 return error_mark_node;
10053 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10054 && !function_body && cxx_dialect < cxx14)
10055 pedwarn (input_location, OPT_Wpedantic,
10056 "compound-statement in constexpr function");
10057 /* Begin the compound-statement. */
10058 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10059 /* If the next keyword is `__label__' we have a label declaration. */
10060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10061 cp_parser_label_declaration (parser);
10062 /* Parse an (optional) statement-seq. */
10063 cp_parser_statement_seq_opt (parser, in_statement_expr);
10064 /* Finish the compound-statement. */
10065 finish_compound_stmt (compound_stmt);
10066 /* Consume the `}'. */
10067 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10069 return compound_stmt;
10072 /* Parse an (optional) statement-seq.
10074 statement-seq:
10075 statement
10076 statement-seq [opt] statement */
10078 static void
10079 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10081 /* Scan statements until there aren't any more. */
10082 while (true)
10084 cp_token *token = cp_lexer_peek_token (parser->lexer);
10086 /* If we are looking at a `}', then we have run out of
10087 statements; the same is true if we have reached the end
10088 of file, or have stumbled upon a stray '@end'. */
10089 if (token->type == CPP_CLOSE_BRACE
10090 || token->type == CPP_EOF
10091 || token->type == CPP_PRAGMA_EOL
10092 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10093 break;
10095 /* If we are in a compound statement and find 'else' then
10096 something went wrong. */
10097 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10099 if (parser->in_statement & IN_IF_STMT)
10100 break;
10101 else
10103 token = cp_lexer_consume_token (parser->lexer);
10104 error_at (token->location, "%<else%> without a previous %<if%>");
10108 /* Parse the statement. */
10109 cp_parser_statement (parser, in_statement_expr, true, NULL);
10113 /* Parse a selection-statement.
10115 selection-statement:
10116 if ( condition ) statement
10117 if ( condition ) statement else statement
10118 switch ( condition ) statement
10120 Returns the new IF_STMT or SWITCH_STMT.
10122 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10123 is a (possibly labeled) if statement which is not enclosed in
10124 braces and has an else clause. This is used to implement
10125 -Wparentheses. */
10127 static tree
10128 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10130 cp_token *token;
10131 enum rid keyword;
10133 if (if_p != NULL)
10134 *if_p = false;
10136 /* Peek at the next token. */
10137 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10139 /* See what kind of keyword it is. */
10140 keyword = token->keyword;
10141 switch (keyword)
10143 case RID_IF:
10144 case RID_SWITCH:
10146 tree statement;
10147 tree condition;
10149 /* Look for the `('. */
10150 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10152 cp_parser_skip_to_end_of_statement (parser);
10153 return error_mark_node;
10156 /* Begin the selection-statement. */
10157 if (keyword == RID_IF)
10158 statement = begin_if_stmt ();
10159 else
10160 statement = begin_switch_stmt ();
10162 /* Parse the condition. */
10163 condition = cp_parser_condition (parser);
10164 /* Look for the `)'. */
10165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10166 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10167 /*consume_paren=*/true);
10169 if (keyword == RID_IF)
10171 bool nested_if;
10172 unsigned char in_statement;
10174 /* Add the condition. */
10175 finish_if_stmt_cond (condition, statement);
10177 /* Parse the then-clause. */
10178 in_statement = parser->in_statement;
10179 parser->in_statement |= IN_IF_STMT;
10180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10182 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10183 add_stmt (build_empty_stmt (loc));
10184 cp_lexer_consume_token (parser->lexer);
10185 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10186 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10187 "empty body in an %<if%> statement");
10188 nested_if = false;
10190 else
10191 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10192 parser->in_statement = in_statement;
10194 finish_then_clause (statement);
10196 /* If the next token is `else', parse the else-clause. */
10197 if (cp_lexer_next_token_is_keyword (parser->lexer,
10198 RID_ELSE))
10200 /* Consume the `else' keyword. */
10201 cp_lexer_consume_token (parser->lexer);
10202 begin_else_clause (statement);
10203 /* Parse the else-clause. */
10204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10206 location_t loc;
10207 loc = cp_lexer_peek_token (parser->lexer)->location;
10208 warning_at (loc,
10209 OPT_Wempty_body, "suggest braces around "
10210 "empty body in an %<else%> statement");
10211 add_stmt (build_empty_stmt (loc));
10212 cp_lexer_consume_token (parser->lexer);
10214 else
10215 cp_parser_implicitly_scoped_statement (parser, NULL);
10217 finish_else_clause (statement);
10219 /* If we are currently parsing a then-clause, then
10220 IF_P will not be NULL. We set it to true to
10221 indicate that this if statement has an else clause.
10222 This may trigger the Wparentheses warning below
10223 when we get back up to the parent if statement. */
10224 if (if_p != NULL)
10225 *if_p = true;
10227 else
10229 /* This if statement does not have an else clause. If
10230 NESTED_IF is true, then the then-clause is an if
10231 statement which does have an else clause. We warn
10232 about the potential ambiguity. */
10233 if (nested_if)
10234 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10235 "suggest explicit braces to avoid ambiguous"
10236 " %<else%>");
10239 /* Now we're all done with the if-statement. */
10240 finish_if_stmt (statement);
10242 else
10244 bool in_switch_statement_p;
10245 unsigned char in_statement;
10247 /* Add the condition. */
10248 finish_switch_cond (condition, statement);
10250 /* Parse the body of the switch-statement. */
10251 in_switch_statement_p = parser->in_switch_statement_p;
10252 in_statement = parser->in_statement;
10253 parser->in_switch_statement_p = true;
10254 parser->in_statement |= IN_SWITCH_STMT;
10255 cp_parser_implicitly_scoped_statement (parser, NULL);
10256 parser->in_switch_statement_p = in_switch_statement_p;
10257 parser->in_statement = in_statement;
10259 /* Now we're all done with the switch-statement. */
10260 finish_switch_stmt (statement);
10263 return statement;
10265 break;
10267 default:
10268 cp_parser_error (parser, "expected selection-statement");
10269 return error_mark_node;
10273 /* Parse a condition.
10275 condition:
10276 expression
10277 type-specifier-seq declarator = initializer-clause
10278 type-specifier-seq declarator braced-init-list
10280 GNU Extension:
10282 condition:
10283 type-specifier-seq declarator asm-specification [opt]
10284 attributes [opt] = assignment-expression
10286 Returns the expression that should be tested. */
10288 static tree
10289 cp_parser_condition (cp_parser* parser)
10291 cp_decl_specifier_seq type_specifiers;
10292 const char *saved_message;
10293 int declares_class_or_enum;
10295 /* Try the declaration first. */
10296 cp_parser_parse_tentatively (parser);
10297 /* New types are not allowed in the type-specifier-seq for a
10298 condition. */
10299 saved_message = parser->type_definition_forbidden_message;
10300 parser->type_definition_forbidden_message
10301 = G_("types may not be defined in conditions");
10302 /* Parse the type-specifier-seq. */
10303 cp_parser_decl_specifier_seq (parser,
10304 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10305 &type_specifiers,
10306 &declares_class_or_enum);
10307 /* Restore the saved message. */
10308 parser->type_definition_forbidden_message = saved_message;
10309 /* If all is well, we might be looking at a declaration. */
10310 if (!cp_parser_error_occurred (parser))
10312 tree decl;
10313 tree asm_specification;
10314 tree attributes;
10315 cp_declarator *declarator;
10316 tree initializer = NULL_TREE;
10318 /* Parse the declarator. */
10319 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10320 /*ctor_dtor_or_conv_p=*/NULL,
10321 /*parenthesized_p=*/NULL,
10322 /*member_p=*/false,
10323 /*friend_p=*/false);
10324 /* Parse the attributes. */
10325 attributes = cp_parser_attributes_opt (parser);
10326 /* Parse the asm-specification. */
10327 asm_specification = cp_parser_asm_specification_opt (parser);
10328 /* If the next token is not an `=' or '{', then we might still be
10329 looking at an expression. For example:
10331 if (A(a).x)
10333 looks like a decl-specifier-seq and a declarator -- but then
10334 there is no `=', so this is an expression. */
10335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10336 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10337 cp_parser_simulate_error (parser);
10339 /* If we did see an `=' or '{', then we are looking at a declaration
10340 for sure. */
10341 if (cp_parser_parse_definitely (parser))
10343 tree pushed_scope;
10344 bool non_constant_p;
10345 bool flags = LOOKUP_ONLYCONVERTING;
10347 /* Create the declaration. */
10348 decl = start_decl (declarator, &type_specifiers,
10349 /*initialized_p=*/true,
10350 attributes, /*prefix_attributes=*/NULL_TREE,
10351 &pushed_scope);
10353 /* Parse the initializer. */
10354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10356 initializer = cp_parser_braced_list (parser, &non_constant_p);
10357 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10358 flags = 0;
10360 else
10362 /* Consume the `='. */
10363 cp_parser_require (parser, CPP_EQ, RT_EQ);
10364 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10366 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10369 /* Process the initializer. */
10370 cp_finish_decl (decl,
10371 initializer, !non_constant_p,
10372 asm_specification,
10373 flags);
10375 if (pushed_scope)
10376 pop_scope (pushed_scope);
10378 return convert_from_reference (decl);
10381 /* If we didn't even get past the declarator successfully, we are
10382 definitely not looking at a declaration. */
10383 else
10384 cp_parser_abort_tentative_parse (parser);
10386 /* Otherwise, we are looking at an expression. */
10387 return cp_parser_expression (parser);
10390 /* Parses a for-statement or range-for-statement until the closing ')',
10391 not included. */
10393 static tree
10394 cp_parser_for (cp_parser *parser, bool ivdep)
10396 tree init, scope, decl;
10397 bool is_range_for;
10399 /* Begin the for-statement. */
10400 scope = begin_for_scope (&init);
10402 /* Parse the initialization. */
10403 is_range_for = cp_parser_for_init_statement (parser, &decl);
10405 if (is_range_for)
10406 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10407 else
10408 return cp_parser_c_for (parser, scope, init, ivdep);
10411 static tree
10412 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10414 /* Normal for loop */
10415 tree condition = NULL_TREE;
10416 tree expression = NULL_TREE;
10417 tree stmt;
10419 stmt = begin_for_stmt (scope, init);
10420 /* The for-init-statement has already been parsed in
10421 cp_parser_for_init_statement, so no work is needed here. */
10422 finish_for_init_stmt (stmt);
10424 /* If there's a condition, process it. */
10425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10426 condition = cp_parser_condition (parser);
10427 else if (ivdep)
10429 cp_parser_error (parser, "missing loop condition in loop with "
10430 "%<GCC ivdep%> pragma");
10431 condition = error_mark_node;
10433 finish_for_cond (condition, stmt, ivdep);
10434 /* Look for the `;'. */
10435 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10437 /* If there's an expression, process it. */
10438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10439 expression = cp_parser_expression (parser);
10440 finish_for_expr (expression, stmt);
10442 return stmt;
10445 /* Tries to parse a range-based for-statement:
10447 range-based-for:
10448 decl-specifier-seq declarator : expression
10450 The decl-specifier-seq declarator and the `:' are already parsed by
10451 cp_parser_for_init_statement. If processing_template_decl it returns a
10452 newly created RANGE_FOR_STMT; if not, it is converted to a
10453 regular FOR_STMT. */
10455 static tree
10456 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10457 bool ivdep)
10459 tree stmt, range_expr;
10461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10463 bool expr_non_constant_p;
10464 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10466 else
10467 range_expr = cp_parser_expression (parser);
10469 /* If in template, STMT is converted to a normal for-statement
10470 at instantiation. If not, it is done just ahead. */
10471 if (processing_template_decl)
10473 if (check_for_bare_parameter_packs (range_expr))
10474 range_expr = error_mark_node;
10475 stmt = begin_range_for_stmt (scope, init);
10476 if (ivdep)
10477 RANGE_FOR_IVDEP (stmt) = 1;
10478 finish_range_for_decl (stmt, range_decl, range_expr);
10479 if (!type_dependent_expression_p (range_expr)
10480 /* do_auto_deduction doesn't mess with template init-lists. */
10481 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10482 do_range_for_auto_deduction (range_decl, range_expr);
10484 else
10486 stmt = begin_for_stmt (scope, init);
10487 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10489 return stmt;
10492 /* Subroutine of cp_convert_range_for: given the initializer expression,
10493 builds up the range temporary. */
10495 static tree
10496 build_range_temp (tree range_expr)
10498 tree range_type, range_temp;
10500 /* Find out the type deduced by the declaration
10501 `auto &&__range = range_expr'. */
10502 range_type = cp_build_reference_type (make_auto (), true);
10503 range_type = do_auto_deduction (range_type, range_expr,
10504 type_uses_auto (range_type));
10506 /* Create the __range variable. */
10507 range_temp = build_decl (input_location, VAR_DECL,
10508 get_identifier ("__for_range"), range_type);
10509 TREE_USED (range_temp) = 1;
10510 DECL_ARTIFICIAL (range_temp) = 1;
10512 return range_temp;
10515 /* Used by cp_parser_range_for in template context: we aren't going to
10516 do a full conversion yet, but we still need to resolve auto in the
10517 type of the for-range-declaration if present. This is basically
10518 a shortcut version of cp_convert_range_for. */
10520 static void
10521 do_range_for_auto_deduction (tree decl, tree range_expr)
10523 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10524 if (auto_node)
10526 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10527 range_temp = convert_from_reference (build_range_temp (range_expr));
10528 iter_type = (cp_parser_perform_range_for_lookup
10529 (range_temp, &begin_dummy, &end_dummy));
10530 if (iter_type)
10532 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10533 iter_type);
10534 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10535 tf_warning_or_error);
10536 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10537 iter_decl, auto_node);
10542 /* Converts a range-based for-statement into a normal
10543 for-statement, as per the definition.
10545 for (RANGE_DECL : RANGE_EXPR)
10546 BLOCK
10548 should be equivalent to:
10551 auto &&__range = RANGE_EXPR;
10552 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10553 __begin != __end;
10554 ++__begin)
10556 RANGE_DECL = *__begin;
10557 BLOCK
10561 If RANGE_EXPR is an array:
10562 BEGIN_EXPR = __range
10563 END_EXPR = __range + ARRAY_SIZE(__range)
10564 Else if RANGE_EXPR has a member 'begin' or 'end':
10565 BEGIN_EXPR = __range.begin()
10566 END_EXPR = __range.end()
10567 Else:
10568 BEGIN_EXPR = begin(__range)
10569 END_EXPR = end(__range);
10571 If __range has a member 'begin' but not 'end', or vice versa, we must
10572 still use the second alternative (it will surely fail, however).
10573 When calling begin()/end() in the third alternative we must use
10574 argument dependent lookup, but always considering 'std' as an associated
10575 namespace. */
10577 tree
10578 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10579 bool ivdep)
10581 tree begin, end;
10582 tree iter_type, begin_expr, end_expr;
10583 tree condition, expression;
10585 if (range_decl == error_mark_node || range_expr == error_mark_node)
10586 /* If an error happened previously do nothing or else a lot of
10587 unhelpful errors would be issued. */
10588 begin_expr = end_expr = iter_type = error_mark_node;
10589 else
10591 tree range_temp;
10593 if (TREE_CODE (range_expr) == VAR_DECL
10594 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10595 /* Can't bind a reference to an array of runtime bound. */
10596 range_temp = range_expr;
10597 else
10599 range_temp = build_range_temp (range_expr);
10600 pushdecl (range_temp);
10601 cp_finish_decl (range_temp, range_expr,
10602 /*is_constant_init*/false, NULL_TREE,
10603 LOOKUP_ONLYCONVERTING);
10604 range_temp = convert_from_reference (range_temp);
10606 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10607 &begin_expr, &end_expr);
10610 /* The new for initialization statement. */
10611 begin = build_decl (input_location, VAR_DECL,
10612 get_identifier ("__for_begin"), iter_type);
10613 TREE_USED (begin) = 1;
10614 DECL_ARTIFICIAL (begin) = 1;
10615 pushdecl (begin);
10616 cp_finish_decl (begin, begin_expr,
10617 /*is_constant_init*/false, NULL_TREE,
10618 LOOKUP_ONLYCONVERTING);
10620 end = build_decl (input_location, VAR_DECL,
10621 get_identifier ("__for_end"), iter_type);
10622 TREE_USED (end) = 1;
10623 DECL_ARTIFICIAL (end) = 1;
10624 pushdecl (end);
10625 cp_finish_decl (end, end_expr,
10626 /*is_constant_init*/false, NULL_TREE,
10627 LOOKUP_ONLYCONVERTING);
10629 finish_for_init_stmt (statement);
10631 /* The new for condition. */
10632 condition = build_x_binary_op (input_location, NE_EXPR,
10633 begin, ERROR_MARK,
10634 end, ERROR_MARK,
10635 NULL, tf_warning_or_error);
10636 finish_for_cond (condition, statement, ivdep);
10638 /* The new increment expression. */
10639 expression = finish_unary_op_expr (input_location,
10640 PREINCREMENT_EXPR, begin,
10641 tf_warning_or_error);
10642 finish_for_expr (expression, statement);
10644 /* The declaration is initialized with *__begin inside the loop body. */
10645 cp_finish_decl (range_decl,
10646 build_x_indirect_ref (input_location, begin, RO_NULL,
10647 tf_warning_or_error),
10648 /*is_constant_init*/false, NULL_TREE,
10649 LOOKUP_ONLYCONVERTING);
10651 return statement;
10654 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10655 We need to solve both at the same time because the method used
10656 depends on the existence of members begin or end.
10657 Returns the type deduced for the iterator expression. */
10659 static tree
10660 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10662 if (error_operand_p (range))
10664 *begin = *end = error_mark_node;
10665 return error_mark_node;
10668 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10670 error ("range-based %<for%> expression of type %qT "
10671 "has incomplete type", TREE_TYPE (range));
10672 *begin = *end = error_mark_node;
10673 return error_mark_node;
10675 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10677 /* If RANGE is an array, we will use pointer arithmetic. */
10678 *begin = range;
10679 *end = build_binary_op (input_location, PLUS_EXPR,
10680 range,
10681 array_type_nelts_top (TREE_TYPE (range)),
10683 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10685 else
10687 /* If it is not an array, we must do a bit of magic. */
10688 tree id_begin, id_end;
10689 tree member_begin, member_end;
10691 *begin = *end = error_mark_node;
10693 id_begin = get_identifier ("begin");
10694 id_end = get_identifier ("end");
10695 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10696 /*protect=*/2, /*want_type=*/false,
10697 tf_warning_or_error);
10698 member_end = lookup_member (TREE_TYPE (range), id_end,
10699 /*protect=*/2, /*want_type=*/false,
10700 tf_warning_or_error);
10702 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10704 /* Use the member functions. */
10705 if (member_begin != NULL_TREE)
10706 *begin = cp_parser_range_for_member_function (range, id_begin);
10707 else
10708 error ("range-based %<for%> expression of type %qT has an "
10709 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10711 if (member_end != NULL_TREE)
10712 *end = cp_parser_range_for_member_function (range, id_end);
10713 else
10714 error ("range-based %<for%> expression of type %qT has a "
10715 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10717 else
10719 /* Use global functions with ADL. */
10720 vec<tree, va_gc> *vec;
10721 vec = make_tree_vector ();
10723 vec_safe_push (vec, range);
10725 member_begin = perform_koenig_lookup (id_begin, vec,
10726 tf_warning_or_error);
10727 *begin = finish_call_expr (member_begin, &vec, false, true,
10728 tf_warning_or_error);
10729 member_end = perform_koenig_lookup (id_end, vec,
10730 tf_warning_or_error);
10731 *end = finish_call_expr (member_end, &vec, false, true,
10732 tf_warning_or_error);
10734 release_tree_vector (vec);
10737 /* Last common checks. */
10738 if (*begin == error_mark_node || *end == error_mark_node)
10740 /* If one of the expressions is an error do no more checks. */
10741 *begin = *end = error_mark_node;
10742 return error_mark_node;
10744 else if (type_dependent_expression_p (*begin)
10745 || type_dependent_expression_p (*end))
10746 /* Can happen, when, eg, in a template context, Koenig lookup
10747 can't resolve begin/end (c++/58503). */
10748 return NULL_TREE;
10749 else
10751 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10752 /* The unqualified type of the __begin and __end temporaries should
10753 be the same, as required by the multiple auto declaration. */
10754 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10755 error ("inconsistent begin/end types in range-based %<for%> "
10756 "statement: %qT and %qT",
10757 TREE_TYPE (*begin), TREE_TYPE (*end));
10758 return iter_type;
10763 /* Helper function for cp_parser_perform_range_for_lookup.
10764 Builds a tree for RANGE.IDENTIFIER(). */
10766 static tree
10767 cp_parser_range_for_member_function (tree range, tree identifier)
10769 tree member, res;
10770 vec<tree, va_gc> *vec;
10772 member = finish_class_member_access_expr (range, identifier,
10773 false, tf_warning_or_error);
10774 if (member == error_mark_node)
10775 return error_mark_node;
10777 vec = make_tree_vector ();
10778 res = finish_call_expr (member, &vec,
10779 /*disallow_virtual=*/false,
10780 /*koenig_p=*/false,
10781 tf_warning_or_error);
10782 release_tree_vector (vec);
10783 return res;
10786 /* Parse an iteration-statement.
10788 iteration-statement:
10789 while ( condition ) statement
10790 do statement while ( expression ) ;
10791 for ( for-init-statement condition [opt] ; expression [opt] )
10792 statement
10794 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10796 static tree
10797 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10799 cp_token *token;
10800 enum rid keyword;
10801 tree statement;
10802 unsigned char in_statement;
10804 /* Peek at the next token. */
10805 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10806 if (!token)
10807 return error_mark_node;
10809 /* Remember whether or not we are already within an iteration
10810 statement. */
10811 in_statement = parser->in_statement;
10813 /* See what kind of keyword it is. */
10814 keyword = token->keyword;
10815 switch (keyword)
10817 case RID_WHILE:
10819 tree condition;
10821 /* Begin the while-statement. */
10822 statement = begin_while_stmt ();
10823 /* Look for the `('. */
10824 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10825 /* Parse the condition. */
10826 condition = cp_parser_condition (parser);
10827 finish_while_stmt_cond (condition, statement, ivdep);
10828 /* Look for the `)'. */
10829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10830 /* Parse the dependent statement. */
10831 parser->in_statement = IN_ITERATION_STMT;
10832 cp_parser_already_scoped_statement (parser);
10833 parser->in_statement = in_statement;
10834 /* We're done with the while-statement. */
10835 finish_while_stmt (statement);
10837 break;
10839 case RID_DO:
10841 tree expression;
10843 /* Begin the do-statement. */
10844 statement = begin_do_stmt ();
10845 /* Parse the body of the do-statement. */
10846 parser->in_statement = IN_ITERATION_STMT;
10847 cp_parser_implicitly_scoped_statement (parser, NULL);
10848 parser->in_statement = in_statement;
10849 finish_do_body (statement);
10850 /* Look for the `while' keyword. */
10851 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10852 /* Look for the `('. */
10853 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10854 /* Parse the expression. */
10855 expression = cp_parser_expression (parser);
10856 /* We're done with the do-statement. */
10857 finish_do_stmt (expression, statement, ivdep);
10858 /* Look for the `)'. */
10859 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10860 /* Look for the `;'. */
10861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10863 break;
10865 case RID_FOR:
10867 /* Look for the `('. */
10868 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10870 statement = cp_parser_for (parser, ivdep);
10872 /* Look for the `)'. */
10873 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10875 /* Parse the body of the for-statement. */
10876 parser->in_statement = IN_ITERATION_STMT;
10877 cp_parser_already_scoped_statement (parser);
10878 parser->in_statement = in_statement;
10880 /* We're done with the for-statement. */
10881 finish_for_stmt (statement);
10883 break;
10885 default:
10886 cp_parser_error (parser, "expected iteration-statement");
10887 statement = error_mark_node;
10888 break;
10891 return statement;
10894 /* Parse a for-init-statement or the declarator of a range-based-for.
10895 Returns true if a range-based-for declaration is seen.
10897 for-init-statement:
10898 expression-statement
10899 simple-declaration */
10901 static bool
10902 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10904 /* If the next token is a `;', then we have an empty
10905 expression-statement. Grammatically, this is also a
10906 simple-declaration, but an invalid one, because it does not
10907 declare anything. Therefore, if we did not handle this case
10908 specially, we would issue an error message about an invalid
10909 declaration. */
10910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10912 bool is_range_for = false;
10913 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10916 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10918 /* N3994 -- for (id : init) ... */
10919 if (cxx_dialect < cxx1z)
10920 pedwarn (input_location, 0, "range-based for loop without a "
10921 "type-specifier only available with "
10922 "-std=c++1z or -std=gnu++1z");
10923 tree name = cp_parser_identifier (parser);
10924 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10925 *decl = build_decl (input_location, VAR_DECL, name, type);
10926 pushdecl (*decl);
10927 cp_lexer_consume_token (parser->lexer);
10928 return true;
10931 /* A colon is used in range-based for. */
10932 parser->colon_corrects_to_scope_p = false;
10934 /* We're going to speculatively look for a declaration, falling back
10935 to an expression, if necessary. */
10936 cp_parser_parse_tentatively (parser);
10937 /* Parse the declaration. */
10938 cp_parser_simple_declaration (parser,
10939 /*function_definition_allowed_p=*/false,
10940 decl);
10941 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10942 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10944 /* It is a range-for, consume the ':' */
10945 cp_lexer_consume_token (parser->lexer);
10946 is_range_for = true;
10947 if (cxx_dialect < cxx11)
10949 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10950 "range-based %<for%> loops only available with "
10951 "-std=c++11 or -std=gnu++11");
10952 *decl = error_mark_node;
10955 else
10956 /* The ';' is not consumed yet because we told
10957 cp_parser_simple_declaration not to. */
10958 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10960 if (cp_parser_parse_definitely (parser))
10961 return is_range_for;
10962 /* If the tentative parse failed, then we shall need to look for an
10963 expression-statement. */
10965 /* If we are here, it is an expression-statement. */
10966 cp_parser_expression_statement (parser, NULL_TREE);
10967 return false;
10970 /* Parse a jump-statement.
10972 jump-statement:
10973 break ;
10974 continue ;
10975 return expression [opt] ;
10976 return braced-init-list ;
10977 goto identifier ;
10979 GNU extension:
10981 jump-statement:
10982 goto * expression ;
10984 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10986 static tree
10987 cp_parser_jump_statement (cp_parser* parser)
10989 tree statement = error_mark_node;
10990 cp_token *token;
10991 enum rid keyword;
10992 unsigned char in_statement;
10994 /* Peek at the next token. */
10995 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10996 if (!token)
10997 return error_mark_node;
10999 /* See what kind of keyword it is. */
11000 keyword = token->keyword;
11001 switch (keyword)
11003 case RID_BREAK:
11004 in_statement = parser->in_statement & ~IN_IF_STMT;
11005 switch (in_statement)
11007 case 0:
11008 error_at (token->location, "break statement not within loop or switch");
11009 break;
11010 default:
11011 gcc_assert ((in_statement & IN_SWITCH_STMT)
11012 || in_statement == IN_ITERATION_STMT);
11013 statement = finish_break_stmt ();
11014 if (in_statement == IN_ITERATION_STMT)
11015 break_maybe_infinite_loop ();
11016 break;
11017 case IN_OMP_BLOCK:
11018 error_at (token->location, "invalid exit from OpenMP structured block");
11019 break;
11020 case IN_OMP_FOR:
11021 error_at (token->location, "break statement used with OpenMP for loop");
11022 break;
11023 case IN_CILK_SIMD_FOR:
11024 error_at (token->location, "break statement used with Cilk Plus for loop");
11025 break;
11027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11028 break;
11030 case RID_CONTINUE:
11031 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11033 case 0:
11034 error_at (token->location, "continue statement not within a loop");
11035 break;
11036 case IN_CILK_SIMD_FOR:
11037 error_at (token->location,
11038 "continue statement within %<#pragma simd%> loop body");
11039 /* Fall through. */
11040 case IN_ITERATION_STMT:
11041 case IN_OMP_FOR:
11042 statement = finish_continue_stmt ();
11043 break;
11044 case IN_OMP_BLOCK:
11045 error_at (token->location, "invalid exit from OpenMP structured block");
11046 break;
11047 default:
11048 gcc_unreachable ();
11050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11051 break;
11053 case RID_RETURN:
11055 tree expr;
11056 bool expr_non_constant_p;
11058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11060 cp_lexer_set_source_position (parser->lexer);
11061 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11062 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11064 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11065 expr = cp_parser_expression (parser);
11066 else
11067 /* If the next token is a `;', then there is no
11068 expression. */
11069 expr = NULL_TREE;
11070 /* Build the return-statement. */
11071 statement = finish_return_stmt (expr);
11072 /* Look for the final `;'. */
11073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11075 break;
11077 case RID_GOTO:
11078 if (parser->in_function_body
11079 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11081 error ("%<goto%> in %<constexpr%> function");
11082 cp_function_chain->invalid_constexpr = true;
11085 /* Create the goto-statement. */
11086 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11088 /* Issue a warning about this use of a GNU extension. */
11089 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11090 /* Consume the '*' token. */
11091 cp_lexer_consume_token (parser->lexer);
11092 /* Parse the dependent expression. */
11093 finish_goto_stmt (cp_parser_expression (parser));
11095 else
11096 finish_goto_stmt (cp_parser_identifier (parser));
11097 /* Look for the final `;'. */
11098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11099 break;
11101 default:
11102 cp_parser_error (parser, "expected jump-statement");
11103 break;
11106 return statement;
11109 /* Parse a declaration-statement.
11111 declaration-statement:
11112 block-declaration */
11114 static void
11115 cp_parser_declaration_statement (cp_parser* parser)
11117 void *p;
11119 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11120 p = obstack_alloc (&declarator_obstack, 0);
11122 /* Parse the block-declaration. */
11123 cp_parser_block_declaration (parser, /*statement_p=*/true);
11125 /* Free any declarators allocated. */
11126 obstack_free (&declarator_obstack, p);
11129 /* Some dependent statements (like `if (cond) statement'), are
11130 implicitly in their own scope. In other words, if the statement is
11131 a single statement (as opposed to a compound-statement), it is
11132 none-the-less treated as if it were enclosed in braces. Any
11133 declarations appearing in the dependent statement are out of scope
11134 after control passes that point. This function parses a statement,
11135 but ensures that is in its own scope, even if it is not a
11136 compound-statement.
11138 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11139 is a (possibly labeled) if statement which is not enclosed in
11140 braces and has an else clause. This is used to implement
11141 -Wparentheses.
11143 Returns the new statement. */
11145 static tree
11146 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11148 tree statement;
11150 if (if_p != NULL)
11151 *if_p = false;
11153 /* Mark if () ; with a special NOP_EXPR. */
11154 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11156 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11157 cp_lexer_consume_token (parser->lexer);
11158 statement = add_stmt (build_empty_stmt (loc));
11160 /* if a compound is opened, we simply parse the statement directly. */
11161 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11162 statement = cp_parser_compound_statement (parser, NULL, false, false);
11163 /* If the token is not a `{', then we must take special action. */
11164 else
11166 /* Create a compound-statement. */
11167 statement = begin_compound_stmt (0);
11168 /* Parse the dependent-statement. */
11169 cp_parser_statement (parser, NULL_TREE, false, if_p);
11170 /* Finish the dummy compound-statement. */
11171 finish_compound_stmt (statement);
11174 /* Return the statement. */
11175 return statement;
11178 /* For some dependent statements (like `while (cond) statement'), we
11179 have already created a scope. Therefore, even if the dependent
11180 statement is a compound-statement, we do not want to create another
11181 scope. */
11183 static void
11184 cp_parser_already_scoped_statement (cp_parser* parser)
11186 /* If the token is a `{', then we must take special action. */
11187 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11188 cp_parser_statement (parser, NULL_TREE, false, NULL);
11189 else
11191 /* Avoid calling cp_parser_compound_statement, so that we
11192 don't create a new scope. Do everything else by hand. */
11193 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11194 /* If the next keyword is `__label__' we have a label declaration. */
11195 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11196 cp_parser_label_declaration (parser);
11197 /* Parse an (optional) statement-seq. */
11198 cp_parser_statement_seq_opt (parser, NULL_TREE);
11199 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11203 /* Declarations [gram.dcl.dcl] */
11205 /* Parse an optional declaration-sequence.
11207 declaration-seq:
11208 declaration
11209 declaration-seq declaration */
11211 static void
11212 cp_parser_declaration_seq_opt (cp_parser* parser)
11214 while (true)
11216 cp_token *token;
11218 token = cp_lexer_peek_token (parser->lexer);
11220 if (token->type == CPP_CLOSE_BRACE
11221 || token->type == CPP_EOF
11222 || token->type == CPP_PRAGMA_EOL)
11223 break;
11225 if (token->type == CPP_SEMICOLON)
11227 /* A declaration consisting of a single semicolon is
11228 invalid. Allow it unless we're being pedantic. */
11229 cp_lexer_consume_token (parser->lexer);
11230 if (!in_system_header_at (input_location))
11231 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11232 continue;
11235 /* If we're entering or exiting a region that's implicitly
11236 extern "C", modify the lang context appropriately. */
11237 if (!parser->implicit_extern_c && token->implicit_extern_c)
11239 push_lang_context (lang_name_c);
11240 parser->implicit_extern_c = true;
11242 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11244 pop_lang_context ();
11245 parser->implicit_extern_c = false;
11248 if (token->type == CPP_PRAGMA)
11250 /* A top-level declaration can consist solely of a #pragma.
11251 A nested declaration cannot, so this is done here and not
11252 in cp_parser_declaration. (A #pragma at block scope is
11253 handled in cp_parser_statement.) */
11254 cp_parser_pragma (parser, pragma_external);
11255 continue;
11258 /* Parse the declaration itself. */
11259 cp_parser_declaration (parser);
11263 /* Parse a declaration.
11265 declaration:
11266 block-declaration
11267 function-definition
11268 template-declaration
11269 explicit-instantiation
11270 explicit-specialization
11271 linkage-specification
11272 namespace-definition
11274 GNU extension:
11276 declaration:
11277 __extension__ declaration */
11279 static void
11280 cp_parser_declaration (cp_parser* parser)
11282 cp_token token1;
11283 cp_token token2;
11284 int saved_pedantic;
11285 void *p;
11286 tree attributes = NULL_TREE;
11288 /* Check for the `__extension__' keyword. */
11289 if (cp_parser_extension_opt (parser, &saved_pedantic))
11291 /* Parse the qualified declaration. */
11292 cp_parser_declaration (parser);
11293 /* Restore the PEDANTIC flag. */
11294 pedantic = saved_pedantic;
11296 return;
11299 /* Try to figure out what kind of declaration is present. */
11300 token1 = *cp_lexer_peek_token (parser->lexer);
11302 if (token1.type != CPP_EOF)
11303 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11304 else
11306 token2.type = CPP_EOF;
11307 token2.keyword = RID_MAX;
11310 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11311 p = obstack_alloc (&declarator_obstack, 0);
11313 /* If the next token is `extern' and the following token is a string
11314 literal, then we have a linkage specification. */
11315 if (token1.keyword == RID_EXTERN
11316 && cp_parser_is_pure_string_literal (&token2))
11317 cp_parser_linkage_specification (parser);
11318 /* If the next token is `template', then we have either a template
11319 declaration, an explicit instantiation, or an explicit
11320 specialization. */
11321 else if (token1.keyword == RID_TEMPLATE)
11323 /* `template <>' indicates a template specialization. */
11324 if (token2.type == CPP_LESS
11325 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11326 cp_parser_explicit_specialization (parser);
11327 /* `template <' indicates a template declaration. */
11328 else if (token2.type == CPP_LESS)
11329 cp_parser_template_declaration (parser, /*member_p=*/false);
11330 /* Anything else must be an explicit instantiation. */
11331 else
11332 cp_parser_explicit_instantiation (parser);
11334 /* If the next token is `export', then we have a template
11335 declaration. */
11336 else if (token1.keyword == RID_EXPORT)
11337 cp_parser_template_declaration (parser, /*member_p=*/false);
11338 /* If the next token is `extern', 'static' or 'inline' and the one
11339 after that is `template', we have a GNU extended explicit
11340 instantiation directive. */
11341 else if (cp_parser_allow_gnu_extensions_p (parser)
11342 && (token1.keyword == RID_EXTERN
11343 || token1.keyword == RID_STATIC
11344 || token1.keyword == RID_INLINE)
11345 && token2.keyword == RID_TEMPLATE)
11346 cp_parser_explicit_instantiation (parser);
11347 /* If the next token is `namespace', check for a named or unnamed
11348 namespace definition. */
11349 else if (token1.keyword == RID_NAMESPACE
11350 && (/* A named namespace definition. */
11351 (token2.type == CPP_NAME
11352 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11353 != CPP_EQ))
11354 /* An unnamed namespace definition. */
11355 || token2.type == CPP_OPEN_BRACE
11356 || token2.keyword == RID_ATTRIBUTE))
11357 cp_parser_namespace_definition (parser);
11358 /* An inline (associated) namespace definition. */
11359 else if (token1.keyword == RID_INLINE
11360 && token2.keyword == RID_NAMESPACE)
11361 cp_parser_namespace_definition (parser);
11362 /* Objective-C++ declaration/definition. */
11363 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11364 cp_parser_objc_declaration (parser, NULL_TREE);
11365 else if (c_dialect_objc ()
11366 && token1.keyword == RID_ATTRIBUTE
11367 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11368 cp_parser_objc_declaration (parser, attributes);
11369 /* We must have either a block declaration or a function
11370 definition. */
11371 else
11372 /* Try to parse a block-declaration, or a function-definition. */
11373 cp_parser_block_declaration (parser, /*statement_p=*/false);
11375 /* Free any declarators allocated. */
11376 obstack_free (&declarator_obstack, p);
11379 /* Parse a block-declaration.
11381 block-declaration:
11382 simple-declaration
11383 asm-definition
11384 namespace-alias-definition
11385 using-declaration
11386 using-directive
11388 GNU Extension:
11390 block-declaration:
11391 __extension__ block-declaration
11393 C++0x Extension:
11395 block-declaration:
11396 static_assert-declaration
11398 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11399 part of a declaration-statement. */
11401 static void
11402 cp_parser_block_declaration (cp_parser *parser,
11403 bool statement_p)
11405 cp_token *token1;
11406 int saved_pedantic;
11408 /* Check for the `__extension__' keyword. */
11409 if (cp_parser_extension_opt (parser, &saved_pedantic))
11411 /* Parse the qualified declaration. */
11412 cp_parser_block_declaration (parser, statement_p);
11413 /* Restore the PEDANTIC flag. */
11414 pedantic = saved_pedantic;
11416 return;
11419 /* Peek at the next token to figure out which kind of declaration is
11420 present. */
11421 token1 = cp_lexer_peek_token (parser->lexer);
11423 /* If the next keyword is `asm', we have an asm-definition. */
11424 if (token1->keyword == RID_ASM)
11426 if (statement_p)
11427 cp_parser_commit_to_tentative_parse (parser);
11428 cp_parser_asm_definition (parser);
11430 /* If the next keyword is `namespace', we have a
11431 namespace-alias-definition. */
11432 else if (token1->keyword == RID_NAMESPACE)
11433 cp_parser_namespace_alias_definition (parser);
11434 /* If the next keyword is `using', we have a
11435 using-declaration, a using-directive, or an alias-declaration. */
11436 else if (token1->keyword == RID_USING)
11438 cp_token *token2;
11440 if (statement_p)
11441 cp_parser_commit_to_tentative_parse (parser);
11442 /* If the token after `using' is `namespace', then we have a
11443 using-directive. */
11444 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11445 if (token2->keyword == RID_NAMESPACE)
11446 cp_parser_using_directive (parser);
11447 /* If the second token after 'using' is '=', then we have an
11448 alias-declaration. */
11449 else if (cxx_dialect >= cxx11
11450 && token2->type == CPP_NAME
11451 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11452 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11453 cp_parser_alias_declaration (parser);
11454 /* Otherwise, it's a using-declaration. */
11455 else
11456 cp_parser_using_declaration (parser,
11457 /*access_declaration_p=*/false);
11459 /* If the next keyword is `__label__' we have a misplaced label
11460 declaration. */
11461 else if (token1->keyword == RID_LABEL)
11463 cp_lexer_consume_token (parser->lexer);
11464 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11465 cp_parser_skip_to_end_of_statement (parser);
11466 /* If the next token is now a `;', consume it. */
11467 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11468 cp_lexer_consume_token (parser->lexer);
11470 /* If the next token is `static_assert' we have a static assertion. */
11471 else if (token1->keyword == RID_STATIC_ASSERT)
11472 cp_parser_static_assert (parser, /*member_p=*/false);
11473 /* Anything else must be a simple-declaration. */
11474 else
11475 cp_parser_simple_declaration (parser, !statement_p,
11476 /*maybe_range_for_decl*/NULL);
11479 /* Parse a simple-declaration.
11481 simple-declaration:
11482 decl-specifier-seq [opt] init-declarator-list [opt] ;
11484 init-declarator-list:
11485 init-declarator
11486 init-declarator-list , init-declarator
11488 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11489 function-definition as a simple-declaration.
11491 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11492 parsed declaration if it is an uninitialized single declarator not followed
11493 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11494 if present, will not be consumed. */
11496 static void
11497 cp_parser_simple_declaration (cp_parser* parser,
11498 bool function_definition_allowed_p,
11499 tree *maybe_range_for_decl)
11501 cp_decl_specifier_seq decl_specifiers;
11502 int declares_class_or_enum;
11503 bool saw_declarator;
11504 location_t comma_loc = UNKNOWN_LOCATION;
11505 location_t init_loc = UNKNOWN_LOCATION;
11507 if (maybe_range_for_decl)
11508 *maybe_range_for_decl = NULL_TREE;
11510 /* Defer access checks until we know what is being declared; the
11511 checks for names appearing in the decl-specifier-seq should be
11512 done as if we were in the scope of the thing being declared. */
11513 push_deferring_access_checks (dk_deferred);
11515 /* Parse the decl-specifier-seq. We have to keep track of whether
11516 or not the decl-specifier-seq declares a named class or
11517 enumeration type, since that is the only case in which the
11518 init-declarator-list is allowed to be empty.
11520 [dcl.dcl]
11522 In a simple-declaration, the optional init-declarator-list can be
11523 omitted only when declaring a class or enumeration, that is when
11524 the decl-specifier-seq contains either a class-specifier, an
11525 elaborated-type-specifier, or an enum-specifier. */
11526 cp_parser_decl_specifier_seq (parser,
11527 CP_PARSER_FLAGS_OPTIONAL,
11528 &decl_specifiers,
11529 &declares_class_or_enum);
11530 /* We no longer need to defer access checks. */
11531 stop_deferring_access_checks ();
11533 /* In a block scope, a valid declaration must always have a
11534 decl-specifier-seq. By not trying to parse declarators, we can
11535 resolve the declaration/expression ambiguity more quickly. */
11536 if (!function_definition_allowed_p
11537 && !decl_specifiers.any_specifiers_p)
11539 cp_parser_error (parser, "expected declaration");
11540 goto done;
11543 /* If the next two tokens are both identifiers, the code is
11544 erroneous. The usual cause of this situation is code like:
11546 T t;
11548 where "T" should name a type -- but does not. */
11549 if (!decl_specifiers.any_type_specifiers_p
11550 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11552 /* If parsing tentatively, we should commit; we really are
11553 looking at a declaration. */
11554 cp_parser_commit_to_tentative_parse (parser);
11555 /* Give up. */
11556 goto done;
11559 /* If we have seen at least one decl-specifier, and the next token
11560 is not a parenthesis, then we must be looking at a declaration.
11561 (After "int (" we might be looking at a functional cast.) */
11562 if (decl_specifiers.any_specifiers_p
11563 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11564 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11565 && !cp_parser_error_occurred (parser))
11566 cp_parser_commit_to_tentative_parse (parser);
11568 /* Keep going until we hit the `;' at the end of the simple
11569 declaration. */
11570 saw_declarator = false;
11571 while (cp_lexer_next_token_is_not (parser->lexer,
11572 CPP_SEMICOLON))
11574 cp_token *token;
11575 bool function_definition_p;
11576 tree decl;
11578 if (saw_declarator)
11580 /* If we are processing next declarator, comma is expected */
11581 token = cp_lexer_peek_token (parser->lexer);
11582 gcc_assert (token->type == CPP_COMMA);
11583 cp_lexer_consume_token (parser->lexer);
11584 if (maybe_range_for_decl)
11586 *maybe_range_for_decl = error_mark_node;
11587 if (comma_loc == UNKNOWN_LOCATION)
11588 comma_loc = token->location;
11591 else
11592 saw_declarator = true;
11594 /* Parse the init-declarator. */
11595 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11596 /*checks=*/NULL,
11597 function_definition_allowed_p,
11598 /*member_p=*/false,
11599 declares_class_or_enum,
11600 &function_definition_p,
11601 maybe_range_for_decl,
11602 &init_loc);
11603 /* If an error occurred while parsing tentatively, exit quickly.
11604 (That usually happens when in the body of a function; each
11605 statement is treated as a declaration-statement until proven
11606 otherwise.) */
11607 if (cp_parser_error_occurred (parser))
11608 goto done;
11609 /* Handle function definitions specially. */
11610 if (function_definition_p)
11612 /* If the next token is a `,', then we are probably
11613 processing something like:
11615 void f() {}, *p;
11617 which is erroneous. */
11618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11620 cp_token *token = cp_lexer_peek_token (parser->lexer);
11621 error_at (token->location,
11622 "mixing"
11623 " declarations and function-definitions is forbidden");
11625 /* Otherwise, we're done with the list of declarators. */
11626 else
11628 pop_deferring_access_checks ();
11629 return;
11632 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11633 *maybe_range_for_decl = decl;
11634 /* The next token should be either a `,' or a `;'. */
11635 token = cp_lexer_peek_token (parser->lexer);
11636 /* If it's a `,', there are more declarators to come. */
11637 if (token->type == CPP_COMMA)
11638 /* will be consumed next time around */;
11639 /* If it's a `;', we are done. */
11640 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11641 break;
11642 /* Anything else is an error. */
11643 else
11645 /* If we have already issued an error message we don't need
11646 to issue another one. */
11647 if (decl != error_mark_node
11648 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11649 cp_parser_error (parser, "expected %<,%> or %<;%>");
11650 /* Skip tokens until we reach the end of the statement. */
11651 cp_parser_skip_to_end_of_statement (parser);
11652 /* If the next token is now a `;', consume it. */
11653 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11654 cp_lexer_consume_token (parser->lexer);
11655 goto done;
11657 /* After the first time around, a function-definition is not
11658 allowed -- even if it was OK at first. For example:
11660 int i, f() {}
11662 is not valid. */
11663 function_definition_allowed_p = false;
11666 /* Issue an error message if no declarators are present, and the
11667 decl-specifier-seq does not itself declare a class or
11668 enumeration: [dcl.dcl]/3. */
11669 if (!saw_declarator)
11671 if (cp_parser_declares_only_class_p (parser))
11673 if (!declares_class_or_enum
11674 && decl_specifiers.type
11675 && OVERLOAD_TYPE_P (decl_specifiers.type))
11676 /* Ensure an error is issued anyway when finish_decltype_type,
11677 called via cp_parser_decl_specifier_seq, returns a class or
11678 an enumeration (c++/51786). */
11679 decl_specifiers.type = NULL_TREE;
11680 shadow_tag (&decl_specifiers);
11682 /* Perform any deferred access checks. */
11683 perform_deferred_access_checks (tf_warning_or_error);
11686 /* Consume the `;'. */
11687 if (!maybe_range_for_decl)
11688 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11689 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11691 if (init_loc != UNKNOWN_LOCATION)
11692 error_at (init_loc, "initializer in range-based %<for%> loop");
11693 if (comma_loc != UNKNOWN_LOCATION)
11694 error_at (comma_loc,
11695 "multiple declarations in range-based %<for%> loop");
11698 done:
11699 pop_deferring_access_checks ();
11702 /* Parse a decl-specifier-seq.
11704 decl-specifier-seq:
11705 decl-specifier-seq [opt] decl-specifier
11706 decl-specifier attribute-specifier-seq [opt] (C++11)
11708 decl-specifier:
11709 storage-class-specifier
11710 type-specifier
11711 function-specifier
11712 friend
11713 typedef
11715 GNU Extension:
11717 decl-specifier:
11718 attributes
11720 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11722 The parser flags FLAGS is used to control type-specifier parsing.
11724 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11725 flags:
11727 1: one of the decl-specifiers is an elaborated-type-specifier
11728 (i.e., a type declaration)
11729 2: one of the decl-specifiers is an enum-specifier or a
11730 class-specifier (i.e., a type definition)
11734 static void
11735 cp_parser_decl_specifier_seq (cp_parser* parser,
11736 cp_parser_flags flags,
11737 cp_decl_specifier_seq *decl_specs,
11738 int* declares_class_or_enum)
11740 bool constructor_possible_p = !parser->in_declarator_p;
11741 bool found_decl_spec = false;
11742 cp_token *start_token = NULL;
11743 cp_decl_spec ds;
11745 /* Clear DECL_SPECS. */
11746 clear_decl_specs (decl_specs);
11748 /* Assume no class or enumeration type is declared. */
11749 *declares_class_or_enum = 0;
11751 /* Keep reading specifiers until there are no more to read. */
11752 while (true)
11754 bool constructor_p;
11755 cp_token *token;
11756 ds = ds_last;
11758 /* Peek at the next token. */
11759 token = cp_lexer_peek_token (parser->lexer);
11761 /* Save the first token of the decl spec list for error
11762 reporting. */
11763 if (!start_token)
11764 start_token = token;
11765 /* Handle attributes. */
11766 if (cp_next_tokens_can_be_attribute_p (parser))
11768 /* Parse the attributes. */
11769 tree attrs = cp_parser_attributes_opt (parser);
11771 /* In a sequence of declaration specifiers, c++11 attributes
11772 appertain to the type that precede them. In that case
11773 [dcl.spec]/1 says:
11775 The attribute-specifier-seq affects the type only for
11776 the declaration it appears in, not other declarations
11777 involving the same type.
11779 But for now let's force the user to position the
11780 attribute either at the beginning of the declaration or
11781 after the declarator-id, which would clearly mean that it
11782 applies to the declarator. */
11783 if (cxx11_attribute_p (attrs))
11785 if (!found_decl_spec)
11786 /* The c++11 attribute is at the beginning of the
11787 declaration. It appertains to the entity being
11788 declared. */;
11789 else
11791 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11793 /* This is an attribute following a
11794 class-specifier. */
11795 if (decl_specs->type_definition_p)
11796 warn_misplaced_attr_for_class_type (token->location,
11797 decl_specs->type);
11798 attrs = NULL_TREE;
11800 else
11802 decl_specs->std_attributes
11803 = chainon (decl_specs->std_attributes,
11804 attrs);
11805 if (decl_specs->locations[ds_std_attribute] == 0)
11806 decl_specs->locations[ds_std_attribute] = token->location;
11808 continue;
11812 decl_specs->attributes
11813 = chainon (decl_specs->attributes,
11814 attrs);
11815 if (decl_specs->locations[ds_attribute] == 0)
11816 decl_specs->locations[ds_attribute] = token->location;
11817 continue;
11819 /* Assume we will find a decl-specifier keyword. */
11820 found_decl_spec = true;
11821 /* If the next token is an appropriate keyword, we can simply
11822 add it to the list. */
11823 switch (token->keyword)
11825 /* decl-specifier:
11826 friend
11827 constexpr */
11828 case RID_FRIEND:
11829 if (!at_class_scope_p ())
11831 error_at (token->location, "%<friend%> used outside of class");
11832 cp_lexer_purge_token (parser->lexer);
11834 else
11836 ds = ds_friend;
11837 /* Consume the token. */
11838 cp_lexer_consume_token (parser->lexer);
11840 break;
11842 case RID_CONSTEXPR:
11843 ds = ds_constexpr;
11844 cp_lexer_consume_token (parser->lexer);
11845 break;
11847 /* function-specifier:
11848 inline
11849 virtual
11850 explicit */
11851 case RID_INLINE:
11852 case RID_VIRTUAL:
11853 case RID_EXPLICIT:
11854 cp_parser_function_specifier_opt (parser, decl_specs);
11855 break;
11857 /* decl-specifier:
11858 typedef */
11859 case RID_TYPEDEF:
11860 ds = ds_typedef;
11861 /* Consume the token. */
11862 cp_lexer_consume_token (parser->lexer);
11863 /* A constructor declarator cannot appear in a typedef. */
11864 constructor_possible_p = false;
11865 /* The "typedef" keyword can only occur in a declaration; we
11866 may as well commit at this point. */
11867 cp_parser_commit_to_tentative_parse (parser);
11869 if (decl_specs->storage_class != sc_none)
11870 decl_specs->conflicting_specifiers_p = true;
11871 break;
11873 /* storage-class-specifier:
11874 auto
11875 register
11876 static
11877 extern
11878 mutable
11880 GNU Extension:
11881 thread */
11882 case RID_AUTO:
11883 if (cxx_dialect == cxx98)
11885 /* Consume the token. */
11886 cp_lexer_consume_token (parser->lexer);
11888 /* Complain about `auto' as a storage specifier, if
11889 we're complaining about C++0x compatibility. */
11890 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11891 " changes meaning in C++11; please remove it");
11893 /* Set the storage class anyway. */
11894 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11895 token);
11897 else
11898 /* C++0x auto type-specifier. */
11899 found_decl_spec = false;
11900 break;
11902 case RID_REGISTER:
11903 case RID_STATIC:
11904 case RID_EXTERN:
11905 case RID_MUTABLE:
11906 /* Consume the token. */
11907 cp_lexer_consume_token (parser->lexer);
11908 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11909 token);
11910 break;
11911 case RID_THREAD:
11912 /* Consume the token. */
11913 ds = ds_thread;
11914 cp_lexer_consume_token (parser->lexer);
11915 break;
11917 default:
11918 /* We did not yet find a decl-specifier yet. */
11919 found_decl_spec = false;
11920 break;
11923 if (found_decl_spec
11924 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11925 && token->keyword != RID_CONSTEXPR)
11926 error ("decl-specifier invalid in condition");
11928 if (ds != ds_last)
11929 set_and_check_decl_spec_loc (decl_specs, ds, token);
11931 /* Constructors are a special case. The `S' in `S()' is not a
11932 decl-specifier; it is the beginning of the declarator. */
11933 constructor_p
11934 = (!found_decl_spec
11935 && constructor_possible_p
11936 && (cp_parser_constructor_declarator_p
11937 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11939 /* If we don't have a DECL_SPEC yet, then we must be looking at
11940 a type-specifier. */
11941 if (!found_decl_spec && !constructor_p)
11943 int decl_spec_declares_class_or_enum;
11944 bool is_cv_qualifier;
11945 tree type_spec;
11947 type_spec
11948 = cp_parser_type_specifier (parser, flags,
11949 decl_specs,
11950 /*is_declaration=*/true,
11951 &decl_spec_declares_class_or_enum,
11952 &is_cv_qualifier);
11953 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11955 /* If this type-specifier referenced a user-defined type
11956 (a typedef, class-name, etc.), then we can't allow any
11957 more such type-specifiers henceforth.
11959 [dcl.spec]
11961 The longest sequence of decl-specifiers that could
11962 possibly be a type name is taken as the
11963 decl-specifier-seq of a declaration. The sequence shall
11964 be self-consistent as described below.
11966 [dcl.type]
11968 As a general rule, at most one type-specifier is allowed
11969 in the complete decl-specifier-seq of a declaration. The
11970 only exceptions are the following:
11972 -- const or volatile can be combined with any other
11973 type-specifier.
11975 -- signed or unsigned can be combined with char, long,
11976 short, or int.
11978 -- ..
11980 Example:
11982 typedef char* Pc;
11983 void g (const int Pc);
11985 Here, Pc is *not* part of the decl-specifier seq; it's
11986 the declarator. Therefore, once we see a type-specifier
11987 (other than a cv-qualifier), we forbid any additional
11988 user-defined types. We *do* still allow things like `int
11989 int' to be considered a decl-specifier-seq, and issue the
11990 error message later. */
11991 if (type_spec && !is_cv_qualifier)
11992 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11993 /* A constructor declarator cannot follow a type-specifier. */
11994 if (type_spec)
11996 constructor_possible_p = false;
11997 found_decl_spec = true;
11998 if (!is_cv_qualifier)
11999 decl_specs->any_type_specifiers_p = true;
12003 /* If we still do not have a DECL_SPEC, then there are no more
12004 decl-specifiers. */
12005 if (!found_decl_spec)
12006 break;
12008 decl_specs->any_specifiers_p = true;
12009 /* After we see one decl-specifier, further decl-specifiers are
12010 always optional. */
12011 flags |= CP_PARSER_FLAGS_OPTIONAL;
12014 /* Don't allow a friend specifier with a class definition. */
12015 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12016 && (*declares_class_or_enum & 2))
12017 error_at (decl_specs->locations[ds_friend],
12018 "class definition may not be declared a friend");
12021 /* Parse an (optional) storage-class-specifier.
12023 storage-class-specifier:
12024 auto
12025 register
12026 static
12027 extern
12028 mutable
12030 GNU Extension:
12032 storage-class-specifier:
12033 thread
12035 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12037 static tree
12038 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12040 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12042 case RID_AUTO:
12043 if (cxx_dialect != cxx98)
12044 return NULL_TREE;
12045 /* Fall through for C++98. */
12047 case RID_REGISTER:
12048 case RID_STATIC:
12049 case RID_EXTERN:
12050 case RID_MUTABLE:
12051 case RID_THREAD:
12052 /* Consume the token. */
12053 return cp_lexer_consume_token (parser->lexer)->u.value;
12055 default:
12056 return NULL_TREE;
12060 /* Parse an (optional) function-specifier.
12062 function-specifier:
12063 inline
12064 virtual
12065 explicit
12067 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12068 Updates DECL_SPECS, if it is non-NULL. */
12070 static tree
12071 cp_parser_function_specifier_opt (cp_parser* parser,
12072 cp_decl_specifier_seq *decl_specs)
12074 cp_token *token = cp_lexer_peek_token (parser->lexer);
12075 switch (token->keyword)
12077 case RID_INLINE:
12078 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12079 break;
12081 case RID_VIRTUAL:
12082 /* 14.5.2.3 [temp.mem]
12084 A member function template shall not be virtual. */
12085 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12086 error_at (token->location, "templates may not be %<virtual%>");
12087 else
12088 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12089 break;
12091 case RID_EXPLICIT:
12092 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12093 break;
12095 default:
12096 return NULL_TREE;
12099 /* Consume the token. */
12100 return cp_lexer_consume_token (parser->lexer)->u.value;
12103 /* Parse a linkage-specification.
12105 linkage-specification:
12106 extern string-literal { declaration-seq [opt] }
12107 extern string-literal declaration */
12109 static void
12110 cp_parser_linkage_specification (cp_parser* parser)
12112 tree linkage;
12114 /* Look for the `extern' keyword. */
12115 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12117 /* Look for the string-literal. */
12118 linkage = cp_parser_string_literal (parser, false, false);
12120 /* Transform the literal into an identifier. If the literal is a
12121 wide-character string, or contains embedded NULs, then we can't
12122 handle it as the user wants. */
12123 if (strlen (TREE_STRING_POINTER (linkage))
12124 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12126 cp_parser_error (parser, "invalid linkage-specification");
12127 /* Assume C++ linkage. */
12128 linkage = lang_name_cplusplus;
12130 else
12131 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12133 /* We're now using the new linkage. */
12134 push_lang_context (linkage);
12136 /* If the next token is a `{', then we're using the first
12137 production. */
12138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12140 cp_ensure_no_omp_declare_simd (parser);
12142 /* Consume the `{' token. */
12143 cp_lexer_consume_token (parser->lexer);
12144 /* Parse the declarations. */
12145 cp_parser_declaration_seq_opt (parser);
12146 /* Look for the closing `}'. */
12147 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12149 /* Otherwise, there's just one declaration. */
12150 else
12152 bool saved_in_unbraced_linkage_specification_p;
12154 saved_in_unbraced_linkage_specification_p
12155 = parser->in_unbraced_linkage_specification_p;
12156 parser->in_unbraced_linkage_specification_p = true;
12157 cp_parser_declaration (parser);
12158 parser->in_unbraced_linkage_specification_p
12159 = saved_in_unbraced_linkage_specification_p;
12162 /* We're done with the linkage-specification. */
12163 pop_lang_context ();
12166 /* Parse a static_assert-declaration.
12168 static_assert-declaration:
12169 static_assert ( constant-expression , string-literal ) ;
12171 If MEMBER_P, this static_assert is a class member. */
12173 static void
12174 cp_parser_static_assert(cp_parser *parser, bool member_p)
12176 tree condition;
12177 tree message;
12178 cp_token *token;
12179 location_t saved_loc;
12180 bool dummy;
12182 /* Peek at the `static_assert' token so we can keep track of exactly
12183 where the static assertion started. */
12184 token = cp_lexer_peek_token (parser->lexer);
12185 saved_loc = token->location;
12187 /* Look for the `static_assert' keyword. */
12188 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12189 RT_STATIC_ASSERT))
12190 return;
12192 /* We know we are in a static assertion; commit to any tentative
12193 parse. */
12194 if (cp_parser_parsing_tentatively (parser))
12195 cp_parser_commit_to_tentative_parse (parser);
12197 /* Parse the `(' starting the static assertion condition. */
12198 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12200 /* Parse the constant-expression. Allow a non-constant expression
12201 here in order to give better diagnostics in finish_static_assert. */
12202 condition =
12203 cp_parser_constant_expression (parser,
12204 /*allow_non_constant_p=*/true,
12205 /*non_constant_p=*/&dummy);
12207 /* Make sure we folded it completely before doing trying to get
12208 constant value. */
12209 condition = fold_non_dependent_expr (condition);
12211 /* Parse the separating `,'. */
12212 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12214 /* Parse the string-literal message. */
12215 message = cp_parser_string_literal (parser,
12216 /*translate=*/false,
12217 /*wide_ok=*/true);
12219 /* A `)' completes the static assertion. */
12220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12221 cp_parser_skip_to_closing_parenthesis (parser,
12222 /*recovering=*/true,
12223 /*or_comma=*/false,
12224 /*consume_paren=*/true);
12226 /* A semicolon terminates the declaration. */
12227 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12229 /* Complete the static assertion, which may mean either processing
12230 the static assert now or saving it for template instantiation. */
12231 finish_static_assert (condition, message, saved_loc, member_p);
12234 /* Parse the expression in decltype ( expression ). */
12236 static tree
12237 cp_parser_decltype_expr (cp_parser *parser,
12238 bool &id_expression_or_member_access_p)
12240 cp_token *id_expr_start_token;
12241 tree expr;
12243 /* First, try parsing an id-expression. */
12244 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12245 cp_parser_parse_tentatively (parser);
12246 expr = cp_parser_id_expression (parser,
12247 /*template_keyword_p=*/false,
12248 /*check_dependency_p=*/true,
12249 /*template_p=*/NULL,
12250 /*declarator_p=*/false,
12251 /*optional_p=*/false);
12253 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12255 bool non_integral_constant_expression_p = false;
12256 tree id_expression = expr;
12257 cp_id_kind idk;
12258 const char *error_msg;
12260 if (identifier_p (expr))
12261 /* Lookup the name we got back from the id-expression. */
12262 expr = cp_parser_lookup_name_simple (parser, expr,
12263 id_expr_start_token->location);
12265 if (expr
12266 && expr != error_mark_node
12267 && TREE_CODE (expr) != TYPE_DECL
12268 && (TREE_CODE (expr) != BIT_NOT_EXPR
12269 || !TYPE_P (TREE_OPERAND (expr, 0)))
12270 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12272 /* Complete lookup of the id-expression. */
12273 expr = (finish_id_expression
12274 (id_expression, expr, parser->scope, &idk,
12275 /*integral_constant_expression_p=*/false,
12276 /*allow_non_integral_constant_expression_p=*/true,
12277 &non_integral_constant_expression_p,
12278 /*template_p=*/false,
12279 /*done=*/true,
12280 /*address_p=*/false,
12281 /*template_arg_p=*/false,
12282 &error_msg,
12283 id_expr_start_token->location));
12285 if (expr == error_mark_node)
12286 /* We found an id-expression, but it was something that we
12287 should not have found. This is an error, not something
12288 we can recover from, so note that we found an
12289 id-expression and we'll recover as gracefully as
12290 possible. */
12291 id_expression_or_member_access_p = true;
12294 if (expr
12295 && expr != error_mark_node
12296 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12297 /* We have an id-expression. */
12298 id_expression_or_member_access_p = true;
12301 if (!id_expression_or_member_access_p)
12303 /* Abort the id-expression parse. */
12304 cp_parser_abort_tentative_parse (parser);
12306 /* Parsing tentatively, again. */
12307 cp_parser_parse_tentatively (parser);
12309 /* Parse a class member access. */
12310 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12311 /*cast_p=*/false, /*decltype*/true,
12312 /*member_access_only_p=*/true, NULL);
12314 if (expr
12315 && expr != error_mark_node
12316 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12317 /* We have an id-expression. */
12318 id_expression_or_member_access_p = true;
12321 if (id_expression_or_member_access_p)
12322 /* We have parsed the complete id-expression or member access. */
12323 cp_parser_parse_definitely (parser);
12324 else
12326 /* Abort our attempt to parse an id-expression or member access
12327 expression. */
12328 cp_parser_abort_tentative_parse (parser);
12330 /* Parse a full expression. */
12331 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12332 /*decltype_p=*/true);
12335 return expr;
12338 /* Parse a `decltype' type. Returns the type.
12340 simple-type-specifier:
12341 decltype ( expression )
12342 C++14 proposal:
12343 decltype ( auto ) */
12345 static tree
12346 cp_parser_decltype (cp_parser *parser)
12348 tree expr;
12349 bool id_expression_or_member_access_p = false;
12350 const char *saved_message;
12351 bool saved_integral_constant_expression_p;
12352 bool saved_non_integral_constant_expression_p;
12353 bool saved_greater_than_is_operator_p;
12354 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12356 if (start_token->type == CPP_DECLTYPE)
12358 /* Already parsed. */
12359 cp_lexer_consume_token (parser->lexer);
12360 return start_token->u.value;
12363 /* Look for the `decltype' token. */
12364 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12365 return error_mark_node;
12367 /* Parse the opening `('. */
12368 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12369 return error_mark_node;
12371 /* decltype (auto) */
12372 if (cxx_dialect >= cxx14
12373 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12375 cp_lexer_consume_token (parser->lexer);
12376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12377 return error_mark_node;
12378 expr = make_decltype_auto ();
12379 AUTO_IS_DECLTYPE (expr) = true;
12380 goto rewrite;
12383 /* Types cannot be defined in a `decltype' expression. Save away the
12384 old message. */
12385 saved_message = parser->type_definition_forbidden_message;
12387 /* And create the new one. */
12388 parser->type_definition_forbidden_message
12389 = G_("types may not be defined in %<decltype%> expressions");
12391 /* The restrictions on constant-expressions do not apply inside
12392 decltype expressions. */
12393 saved_integral_constant_expression_p
12394 = parser->integral_constant_expression_p;
12395 saved_non_integral_constant_expression_p
12396 = parser->non_integral_constant_expression_p;
12397 parser->integral_constant_expression_p = false;
12399 /* Within a parenthesized expression, a `>' token is always
12400 the greater-than operator. */
12401 saved_greater_than_is_operator_p
12402 = parser->greater_than_is_operator_p;
12403 parser->greater_than_is_operator_p = true;
12405 /* Do not actually evaluate the expression. */
12406 ++cp_unevaluated_operand;
12408 /* Do not warn about problems with the expression. */
12409 ++c_inhibit_evaluation_warnings;
12411 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12413 /* Go back to evaluating expressions. */
12414 --cp_unevaluated_operand;
12415 --c_inhibit_evaluation_warnings;
12417 /* The `>' token might be the end of a template-id or
12418 template-parameter-list now. */
12419 parser->greater_than_is_operator_p
12420 = saved_greater_than_is_operator_p;
12422 /* Restore the old message and the integral constant expression
12423 flags. */
12424 parser->type_definition_forbidden_message = saved_message;
12425 parser->integral_constant_expression_p
12426 = saved_integral_constant_expression_p;
12427 parser->non_integral_constant_expression_p
12428 = saved_non_integral_constant_expression_p;
12430 /* Parse to the closing `)'. */
12431 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12433 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12434 /*consume_paren=*/true);
12435 return error_mark_node;
12438 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12439 tf_warning_or_error);
12441 rewrite:
12442 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12443 it again. */
12444 start_token->type = CPP_DECLTYPE;
12445 start_token->u.value = expr;
12446 start_token->keyword = RID_MAX;
12447 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12449 return expr;
12452 /* Special member functions [gram.special] */
12454 /* Parse a conversion-function-id.
12456 conversion-function-id:
12457 operator conversion-type-id
12459 Returns an IDENTIFIER_NODE representing the operator. */
12461 static tree
12462 cp_parser_conversion_function_id (cp_parser* parser)
12464 tree type;
12465 tree saved_scope;
12466 tree saved_qualifying_scope;
12467 tree saved_object_scope;
12468 tree pushed_scope = NULL_TREE;
12470 /* Look for the `operator' token. */
12471 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12472 return error_mark_node;
12473 /* When we parse the conversion-type-id, the current scope will be
12474 reset. However, we need that information in able to look up the
12475 conversion function later, so we save it here. */
12476 saved_scope = parser->scope;
12477 saved_qualifying_scope = parser->qualifying_scope;
12478 saved_object_scope = parser->object_scope;
12479 /* We must enter the scope of the class so that the names of
12480 entities declared within the class are available in the
12481 conversion-type-id. For example, consider:
12483 struct S {
12484 typedef int I;
12485 operator I();
12488 S::operator I() { ... }
12490 In order to see that `I' is a type-name in the definition, we
12491 must be in the scope of `S'. */
12492 if (saved_scope)
12493 pushed_scope = push_scope (saved_scope);
12494 /* Parse the conversion-type-id. */
12495 type = cp_parser_conversion_type_id (parser);
12496 /* Leave the scope of the class, if any. */
12497 if (pushed_scope)
12498 pop_scope (pushed_scope);
12499 /* Restore the saved scope. */
12500 parser->scope = saved_scope;
12501 parser->qualifying_scope = saved_qualifying_scope;
12502 parser->object_scope = saved_object_scope;
12503 /* If the TYPE is invalid, indicate failure. */
12504 if (type == error_mark_node)
12505 return error_mark_node;
12506 return mangle_conv_op_name_for_type (type);
12509 /* Parse a conversion-type-id:
12511 conversion-type-id:
12512 type-specifier-seq conversion-declarator [opt]
12514 Returns the TYPE specified. */
12516 static tree
12517 cp_parser_conversion_type_id (cp_parser* parser)
12519 tree attributes;
12520 cp_decl_specifier_seq type_specifiers;
12521 cp_declarator *declarator;
12522 tree type_specified;
12523 const char *saved_message;
12525 /* Parse the attributes. */
12526 attributes = cp_parser_attributes_opt (parser);
12528 saved_message = parser->type_definition_forbidden_message;
12529 parser->type_definition_forbidden_message
12530 = G_("types may not be defined in a conversion-type-id");
12532 /* Parse the type-specifiers. */
12533 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12534 /*is_trailing_return=*/false,
12535 &type_specifiers);
12537 parser->type_definition_forbidden_message = saved_message;
12539 /* If that didn't work, stop. */
12540 if (type_specifiers.type == error_mark_node)
12541 return error_mark_node;
12542 /* Parse the conversion-declarator. */
12543 declarator = cp_parser_conversion_declarator_opt (parser);
12545 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12546 /*initialized=*/0, &attributes);
12547 if (attributes)
12548 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12550 /* Don't give this error when parsing tentatively. This happens to
12551 work because we always parse this definitively once. */
12552 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12553 && type_uses_auto (type_specified))
12555 if (cxx_dialect < cxx14)
12557 error ("invalid use of %<auto%> in conversion operator");
12558 return error_mark_node;
12560 else if (template_parm_scope_p ())
12561 warning (0, "use of %<auto%> in member template "
12562 "conversion operator can never be deduced");
12565 return type_specified;
12568 /* Parse an (optional) conversion-declarator.
12570 conversion-declarator:
12571 ptr-operator conversion-declarator [opt]
12575 static cp_declarator *
12576 cp_parser_conversion_declarator_opt (cp_parser* parser)
12578 enum tree_code code;
12579 tree class_type, std_attributes = NULL_TREE;
12580 cp_cv_quals cv_quals;
12582 /* We don't know if there's a ptr-operator next, or not. */
12583 cp_parser_parse_tentatively (parser);
12584 /* Try the ptr-operator. */
12585 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12586 &std_attributes);
12587 /* If it worked, look for more conversion-declarators. */
12588 if (cp_parser_parse_definitely (parser))
12590 cp_declarator *declarator;
12592 /* Parse another optional declarator. */
12593 declarator = cp_parser_conversion_declarator_opt (parser);
12595 declarator = cp_parser_make_indirect_declarator
12596 (code, class_type, cv_quals, declarator, std_attributes);
12598 return declarator;
12601 return NULL;
12604 /* Parse an (optional) ctor-initializer.
12606 ctor-initializer:
12607 : mem-initializer-list
12609 Returns TRUE iff the ctor-initializer was actually present. */
12611 static bool
12612 cp_parser_ctor_initializer_opt (cp_parser* parser)
12614 /* If the next token is not a `:', then there is no
12615 ctor-initializer. */
12616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12618 /* Do default initialization of any bases and members. */
12619 if (DECL_CONSTRUCTOR_P (current_function_decl))
12620 finish_mem_initializers (NULL_TREE);
12622 return false;
12625 /* Consume the `:' token. */
12626 cp_lexer_consume_token (parser->lexer);
12627 /* And the mem-initializer-list. */
12628 cp_parser_mem_initializer_list (parser);
12630 return true;
12633 /* Parse a mem-initializer-list.
12635 mem-initializer-list:
12636 mem-initializer ... [opt]
12637 mem-initializer ... [opt] , mem-initializer-list */
12639 static void
12640 cp_parser_mem_initializer_list (cp_parser* parser)
12642 tree mem_initializer_list = NULL_TREE;
12643 tree target_ctor = error_mark_node;
12644 cp_token *token = cp_lexer_peek_token (parser->lexer);
12646 /* Let the semantic analysis code know that we are starting the
12647 mem-initializer-list. */
12648 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12649 error_at (token->location,
12650 "only constructors take member initializers");
12652 /* Loop through the list. */
12653 while (true)
12655 tree mem_initializer;
12657 token = cp_lexer_peek_token (parser->lexer);
12658 /* Parse the mem-initializer. */
12659 mem_initializer = cp_parser_mem_initializer (parser);
12660 /* If the next token is a `...', we're expanding member initializers. */
12661 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12663 /* Consume the `...'. */
12664 cp_lexer_consume_token (parser->lexer);
12666 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12667 can be expanded but members cannot. */
12668 if (mem_initializer != error_mark_node
12669 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12671 error_at (token->location,
12672 "cannot expand initializer for member %<%D%>",
12673 TREE_PURPOSE (mem_initializer));
12674 mem_initializer = error_mark_node;
12677 /* Construct the pack expansion type. */
12678 if (mem_initializer != error_mark_node)
12679 mem_initializer = make_pack_expansion (mem_initializer);
12681 if (target_ctor != error_mark_node
12682 && mem_initializer != error_mark_node)
12684 error ("mem-initializer for %qD follows constructor delegation",
12685 TREE_PURPOSE (mem_initializer));
12686 mem_initializer = error_mark_node;
12688 /* Look for a target constructor. */
12689 if (mem_initializer != error_mark_node
12690 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12691 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12693 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12694 if (mem_initializer_list)
12696 error ("constructor delegation follows mem-initializer for %qD",
12697 TREE_PURPOSE (mem_initializer_list));
12698 mem_initializer = error_mark_node;
12700 target_ctor = mem_initializer;
12702 /* Add it to the list, unless it was erroneous. */
12703 if (mem_initializer != error_mark_node)
12705 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12706 mem_initializer_list = mem_initializer;
12708 /* If the next token is not a `,', we're done. */
12709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12710 break;
12711 /* Consume the `,' token. */
12712 cp_lexer_consume_token (parser->lexer);
12715 /* Perform semantic analysis. */
12716 if (DECL_CONSTRUCTOR_P (current_function_decl))
12717 finish_mem_initializers (mem_initializer_list);
12720 /* Parse a mem-initializer.
12722 mem-initializer:
12723 mem-initializer-id ( expression-list [opt] )
12724 mem-initializer-id braced-init-list
12726 GNU extension:
12728 mem-initializer:
12729 ( expression-list [opt] )
12731 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12732 class) or FIELD_DECL (for a non-static data member) to initialize;
12733 the TREE_VALUE is the expression-list. An empty initialization
12734 list is represented by void_list_node. */
12736 static tree
12737 cp_parser_mem_initializer (cp_parser* parser)
12739 tree mem_initializer_id;
12740 tree expression_list;
12741 tree member;
12742 cp_token *token = cp_lexer_peek_token (parser->lexer);
12744 /* Find out what is being initialized. */
12745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12747 permerror (token->location,
12748 "anachronistic old-style base class initializer");
12749 mem_initializer_id = NULL_TREE;
12751 else
12753 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12754 if (mem_initializer_id == error_mark_node)
12755 return mem_initializer_id;
12757 member = expand_member_init (mem_initializer_id);
12758 if (member && !DECL_P (member))
12759 in_base_initializer = 1;
12761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12763 bool expr_non_constant_p;
12764 cp_lexer_set_source_position (parser->lexer);
12765 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12766 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12767 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12768 expression_list = build_tree_list (NULL_TREE, expression_list);
12770 else
12772 vec<tree, va_gc> *vec;
12773 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12774 /*cast_p=*/false,
12775 /*allow_expansion_p=*/true,
12776 /*non_constant_p=*/NULL);
12777 if (vec == NULL)
12778 return error_mark_node;
12779 expression_list = build_tree_list_vec (vec);
12780 release_tree_vector (vec);
12783 if (expression_list == error_mark_node)
12784 return error_mark_node;
12785 if (!expression_list)
12786 expression_list = void_type_node;
12788 in_base_initializer = 0;
12790 return member ? build_tree_list (member, expression_list) : error_mark_node;
12793 /* Parse a mem-initializer-id.
12795 mem-initializer-id:
12796 :: [opt] nested-name-specifier [opt] class-name
12797 identifier
12799 Returns a TYPE indicating the class to be initializer for the first
12800 production. Returns an IDENTIFIER_NODE indicating the data member
12801 to be initialized for the second production. */
12803 static tree
12804 cp_parser_mem_initializer_id (cp_parser* parser)
12806 bool global_scope_p;
12807 bool nested_name_specifier_p;
12808 bool template_p = false;
12809 tree id;
12811 cp_token *token = cp_lexer_peek_token (parser->lexer);
12813 /* `typename' is not allowed in this context ([temp.res]). */
12814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12816 error_at (token->location,
12817 "keyword %<typename%> not allowed in this context (a qualified "
12818 "member initializer is implicitly a type)");
12819 cp_lexer_consume_token (parser->lexer);
12821 /* Look for the optional `::' operator. */
12822 global_scope_p
12823 = (cp_parser_global_scope_opt (parser,
12824 /*current_scope_valid_p=*/false)
12825 != NULL_TREE);
12826 /* Look for the optional nested-name-specifier. The simplest way to
12827 implement:
12829 [temp.res]
12831 The keyword `typename' is not permitted in a base-specifier or
12832 mem-initializer; in these contexts a qualified name that
12833 depends on a template-parameter is implicitly assumed to be a
12834 type name.
12836 is to assume that we have seen the `typename' keyword at this
12837 point. */
12838 nested_name_specifier_p
12839 = (cp_parser_nested_name_specifier_opt (parser,
12840 /*typename_keyword_p=*/true,
12841 /*check_dependency_p=*/true,
12842 /*type_p=*/true,
12843 /*is_declaration=*/true)
12844 != NULL_TREE);
12845 if (nested_name_specifier_p)
12846 template_p = cp_parser_optional_template_keyword (parser);
12847 /* If there is a `::' operator or a nested-name-specifier, then we
12848 are definitely looking for a class-name. */
12849 if (global_scope_p || nested_name_specifier_p)
12850 return cp_parser_class_name (parser,
12851 /*typename_keyword_p=*/true,
12852 /*template_keyword_p=*/template_p,
12853 typename_type,
12854 /*check_dependency_p=*/true,
12855 /*class_head_p=*/false,
12856 /*is_declaration=*/true);
12857 /* Otherwise, we could also be looking for an ordinary identifier. */
12858 cp_parser_parse_tentatively (parser);
12859 /* Try a class-name. */
12860 id = cp_parser_class_name (parser,
12861 /*typename_keyword_p=*/true,
12862 /*template_keyword_p=*/false,
12863 none_type,
12864 /*check_dependency_p=*/true,
12865 /*class_head_p=*/false,
12866 /*is_declaration=*/true);
12867 /* If we found one, we're done. */
12868 if (cp_parser_parse_definitely (parser))
12869 return id;
12870 /* Otherwise, look for an ordinary identifier. */
12871 return cp_parser_identifier (parser);
12874 /* Overloading [gram.over] */
12876 /* Parse an operator-function-id.
12878 operator-function-id:
12879 operator operator
12881 Returns an IDENTIFIER_NODE for the operator which is a
12882 human-readable spelling of the identifier, e.g., `operator +'. */
12884 static tree
12885 cp_parser_operator_function_id (cp_parser* parser)
12887 /* Look for the `operator' keyword. */
12888 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12889 return error_mark_node;
12890 /* And then the name of the operator itself. */
12891 return cp_parser_operator (parser);
12894 /* Return an identifier node for a user-defined literal operator.
12895 The suffix identifier is chained to the operator name identifier. */
12897 static tree
12898 cp_literal_operator_id (const char* name)
12900 tree identifier;
12901 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12902 + strlen (name) + 10);
12903 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12904 identifier = get_identifier (buffer);
12906 return identifier;
12909 /* Parse an operator.
12911 operator:
12912 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12913 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12914 || ++ -- , ->* -> () []
12916 GNU Extensions:
12918 operator:
12919 <? >? <?= >?=
12921 Returns an IDENTIFIER_NODE for the operator which is a
12922 human-readable spelling of the identifier, e.g., `operator +'. */
12924 static tree
12925 cp_parser_operator (cp_parser* parser)
12927 tree id = NULL_TREE;
12928 cp_token *token;
12929 bool utf8 = false;
12931 /* Peek at the next token. */
12932 token = cp_lexer_peek_token (parser->lexer);
12933 /* Figure out which operator we have. */
12934 switch (token->type)
12936 case CPP_KEYWORD:
12938 enum tree_code op;
12940 /* The keyword should be either `new' or `delete'. */
12941 if (token->keyword == RID_NEW)
12942 op = NEW_EXPR;
12943 else if (token->keyword == RID_DELETE)
12944 op = DELETE_EXPR;
12945 else
12946 break;
12948 /* Consume the `new' or `delete' token. */
12949 cp_lexer_consume_token (parser->lexer);
12951 /* Peek at the next token. */
12952 token = cp_lexer_peek_token (parser->lexer);
12953 /* If it's a `[' token then this is the array variant of the
12954 operator. */
12955 if (token->type == CPP_OPEN_SQUARE)
12957 /* Consume the `[' token. */
12958 cp_lexer_consume_token (parser->lexer);
12959 /* Look for the `]' token. */
12960 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12961 id = ansi_opname (op == NEW_EXPR
12962 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12964 /* Otherwise, we have the non-array variant. */
12965 else
12966 id = ansi_opname (op);
12968 return id;
12971 case CPP_PLUS:
12972 id = ansi_opname (PLUS_EXPR);
12973 break;
12975 case CPP_MINUS:
12976 id = ansi_opname (MINUS_EXPR);
12977 break;
12979 case CPP_MULT:
12980 id = ansi_opname (MULT_EXPR);
12981 break;
12983 case CPP_DIV:
12984 id = ansi_opname (TRUNC_DIV_EXPR);
12985 break;
12987 case CPP_MOD:
12988 id = ansi_opname (TRUNC_MOD_EXPR);
12989 break;
12991 case CPP_XOR:
12992 id = ansi_opname (BIT_XOR_EXPR);
12993 break;
12995 case CPP_AND:
12996 id = ansi_opname (BIT_AND_EXPR);
12997 break;
12999 case CPP_OR:
13000 id = ansi_opname (BIT_IOR_EXPR);
13001 break;
13003 case CPP_COMPL:
13004 id = ansi_opname (BIT_NOT_EXPR);
13005 break;
13007 case CPP_NOT:
13008 id = ansi_opname (TRUTH_NOT_EXPR);
13009 break;
13011 case CPP_EQ:
13012 id = ansi_assopname (NOP_EXPR);
13013 break;
13015 case CPP_LESS:
13016 id = ansi_opname (LT_EXPR);
13017 break;
13019 case CPP_GREATER:
13020 id = ansi_opname (GT_EXPR);
13021 break;
13023 case CPP_PLUS_EQ:
13024 id = ansi_assopname (PLUS_EXPR);
13025 break;
13027 case CPP_MINUS_EQ:
13028 id = ansi_assopname (MINUS_EXPR);
13029 break;
13031 case CPP_MULT_EQ:
13032 id = ansi_assopname (MULT_EXPR);
13033 break;
13035 case CPP_DIV_EQ:
13036 id = ansi_assopname (TRUNC_DIV_EXPR);
13037 break;
13039 case CPP_MOD_EQ:
13040 id = ansi_assopname (TRUNC_MOD_EXPR);
13041 break;
13043 case CPP_XOR_EQ:
13044 id = ansi_assopname (BIT_XOR_EXPR);
13045 break;
13047 case CPP_AND_EQ:
13048 id = ansi_assopname (BIT_AND_EXPR);
13049 break;
13051 case CPP_OR_EQ:
13052 id = ansi_assopname (BIT_IOR_EXPR);
13053 break;
13055 case CPP_LSHIFT:
13056 id = ansi_opname (LSHIFT_EXPR);
13057 break;
13059 case CPP_RSHIFT:
13060 id = ansi_opname (RSHIFT_EXPR);
13061 break;
13063 case CPP_LSHIFT_EQ:
13064 id = ansi_assopname (LSHIFT_EXPR);
13065 break;
13067 case CPP_RSHIFT_EQ:
13068 id = ansi_assopname (RSHIFT_EXPR);
13069 break;
13071 case CPP_EQ_EQ:
13072 id = ansi_opname (EQ_EXPR);
13073 break;
13075 case CPP_NOT_EQ:
13076 id = ansi_opname (NE_EXPR);
13077 break;
13079 case CPP_LESS_EQ:
13080 id = ansi_opname (LE_EXPR);
13081 break;
13083 case CPP_GREATER_EQ:
13084 id = ansi_opname (GE_EXPR);
13085 break;
13087 case CPP_AND_AND:
13088 id = ansi_opname (TRUTH_ANDIF_EXPR);
13089 break;
13091 case CPP_OR_OR:
13092 id = ansi_opname (TRUTH_ORIF_EXPR);
13093 break;
13095 case CPP_PLUS_PLUS:
13096 id = ansi_opname (POSTINCREMENT_EXPR);
13097 break;
13099 case CPP_MINUS_MINUS:
13100 id = ansi_opname (PREDECREMENT_EXPR);
13101 break;
13103 case CPP_COMMA:
13104 id = ansi_opname (COMPOUND_EXPR);
13105 break;
13107 case CPP_DEREF_STAR:
13108 id = ansi_opname (MEMBER_REF);
13109 break;
13111 case CPP_DEREF:
13112 id = ansi_opname (COMPONENT_REF);
13113 break;
13115 case CPP_OPEN_PAREN:
13116 /* Consume the `('. */
13117 cp_lexer_consume_token (parser->lexer);
13118 /* Look for the matching `)'. */
13119 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13120 return ansi_opname (CALL_EXPR);
13122 case CPP_OPEN_SQUARE:
13123 /* Consume the `['. */
13124 cp_lexer_consume_token (parser->lexer);
13125 /* Look for the matching `]'. */
13126 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13127 return ansi_opname (ARRAY_REF);
13129 case CPP_UTF8STRING:
13130 case CPP_UTF8STRING_USERDEF:
13131 utf8 = true;
13132 case CPP_STRING:
13133 case CPP_WSTRING:
13134 case CPP_STRING16:
13135 case CPP_STRING32:
13136 case CPP_STRING_USERDEF:
13137 case CPP_WSTRING_USERDEF:
13138 case CPP_STRING16_USERDEF:
13139 case CPP_STRING32_USERDEF:
13141 tree str, string_tree;
13142 int sz, len;
13144 if (cxx_dialect == cxx98)
13145 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13147 /* Consume the string. */
13148 str = cp_parser_string_literal (parser, /*translate=*/true,
13149 /*wide_ok=*/true, /*lookup_udlit=*/false);
13150 if (str == error_mark_node)
13151 return error_mark_node;
13152 else if (TREE_CODE (str) == USERDEF_LITERAL)
13154 string_tree = USERDEF_LITERAL_VALUE (str);
13155 id = USERDEF_LITERAL_SUFFIX_ID (str);
13157 else
13159 string_tree = str;
13160 /* Look for the suffix identifier. */
13161 token = cp_lexer_peek_token (parser->lexer);
13162 if (token->type == CPP_NAME)
13163 id = cp_parser_identifier (parser);
13164 else if (token->type == CPP_KEYWORD)
13166 error ("unexpected keyword;"
13167 " remove space between quotes and suffix identifier");
13168 return error_mark_node;
13170 else
13172 error ("expected suffix identifier");
13173 return error_mark_node;
13176 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13177 (TREE_TYPE (TREE_TYPE (string_tree))));
13178 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13179 if (len != 0)
13181 error ("expected empty string after %<operator%> keyword");
13182 return error_mark_node;
13184 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13185 != char_type_node)
13187 error ("invalid encoding prefix in literal operator");
13188 return error_mark_node;
13190 if (id != error_mark_node)
13192 const char *name = IDENTIFIER_POINTER (id);
13193 id = cp_literal_operator_id (name);
13195 return id;
13198 default:
13199 /* Anything else is an error. */
13200 break;
13203 /* If we have selected an identifier, we need to consume the
13204 operator token. */
13205 if (id)
13206 cp_lexer_consume_token (parser->lexer);
13207 /* Otherwise, no valid operator name was present. */
13208 else
13210 cp_parser_error (parser, "expected operator");
13211 id = error_mark_node;
13214 return id;
13217 /* Parse a template-declaration.
13219 template-declaration:
13220 export [opt] template < template-parameter-list > declaration
13222 If MEMBER_P is TRUE, this template-declaration occurs within a
13223 class-specifier.
13225 The grammar rule given by the standard isn't correct. What
13226 is really meant is:
13228 template-declaration:
13229 export [opt] template-parameter-list-seq
13230 decl-specifier-seq [opt] init-declarator [opt] ;
13231 export [opt] template-parameter-list-seq
13232 function-definition
13234 template-parameter-list-seq:
13235 template-parameter-list-seq [opt]
13236 template < template-parameter-list > */
13238 static void
13239 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13241 /* Check for `export'. */
13242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13244 /* Consume the `export' token. */
13245 cp_lexer_consume_token (parser->lexer);
13246 /* Warn that we do not support `export'. */
13247 warning (0, "keyword %<export%> not implemented, and will be ignored");
13250 cp_parser_template_declaration_after_export (parser, member_p);
13253 /* Parse a template-parameter-list.
13255 template-parameter-list:
13256 template-parameter
13257 template-parameter-list , template-parameter
13259 Returns a TREE_LIST. Each node represents a template parameter.
13260 The nodes are connected via their TREE_CHAINs. */
13262 static tree
13263 cp_parser_template_parameter_list (cp_parser* parser)
13265 tree parameter_list = NULL_TREE;
13267 begin_template_parm_list ();
13269 /* The loop below parses the template parms. We first need to know
13270 the total number of template parms to be able to compute proper
13271 canonical types of each dependent type. So after the loop, when
13272 we know the total number of template parms,
13273 end_template_parm_list computes the proper canonical types and
13274 fixes up the dependent types accordingly. */
13275 while (true)
13277 tree parameter;
13278 bool is_non_type;
13279 bool is_parameter_pack;
13280 location_t parm_loc;
13282 /* Parse the template-parameter. */
13283 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13284 parameter = cp_parser_template_parameter (parser,
13285 &is_non_type,
13286 &is_parameter_pack);
13287 /* Add it to the list. */
13288 if (parameter != error_mark_node)
13289 parameter_list = process_template_parm (parameter_list,
13290 parm_loc,
13291 parameter,
13292 is_non_type,
13293 is_parameter_pack);
13294 else
13296 tree err_parm = build_tree_list (parameter, parameter);
13297 parameter_list = chainon (parameter_list, err_parm);
13300 /* If the next token is not a `,', we're done. */
13301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13302 break;
13303 /* Otherwise, consume the `,' token. */
13304 cp_lexer_consume_token (parser->lexer);
13307 return end_template_parm_list (parameter_list);
13310 /* Parse a template-parameter.
13312 template-parameter:
13313 type-parameter
13314 parameter-declaration
13316 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13317 the parameter. The TREE_PURPOSE is the default value, if any.
13318 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13319 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13320 set to true iff this parameter is a parameter pack. */
13322 static tree
13323 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13324 bool *is_parameter_pack)
13326 cp_token *token;
13327 cp_parameter_declarator *parameter_declarator;
13328 cp_declarator *id_declarator;
13329 tree parm;
13331 /* Assume it is a type parameter or a template parameter. */
13332 *is_non_type = false;
13333 /* Assume it not a parameter pack. */
13334 *is_parameter_pack = false;
13335 /* Peek at the next token. */
13336 token = cp_lexer_peek_token (parser->lexer);
13337 /* If it is `class' or `template', we have a type-parameter. */
13338 if (token->keyword == RID_TEMPLATE)
13339 return cp_parser_type_parameter (parser, is_parameter_pack);
13340 /* If it is `class' or `typename' we do not know yet whether it is a
13341 type parameter or a non-type parameter. Consider:
13343 template <typename T, typename T::X X> ...
13347 template <class C, class D*> ...
13349 Here, the first parameter is a type parameter, and the second is
13350 a non-type parameter. We can tell by looking at the token after
13351 the identifier -- if it is a `,', `=', or `>' then we have a type
13352 parameter. */
13353 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13355 /* Peek at the token after `class' or `typename'. */
13356 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13357 /* If it's an ellipsis, we have a template type parameter
13358 pack. */
13359 if (token->type == CPP_ELLIPSIS)
13360 return cp_parser_type_parameter (parser, is_parameter_pack);
13361 /* If it's an identifier, skip it. */
13362 if (token->type == CPP_NAME)
13363 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13364 /* Now, see if the token looks like the end of a template
13365 parameter. */
13366 if (token->type == CPP_COMMA
13367 || token->type == CPP_EQ
13368 || token->type == CPP_GREATER)
13369 return cp_parser_type_parameter (parser, is_parameter_pack);
13372 /* Otherwise, it is a non-type parameter.
13374 [temp.param]
13376 When parsing a default template-argument for a non-type
13377 template-parameter, the first non-nested `>' is taken as the end
13378 of the template parameter-list rather than a greater-than
13379 operator. */
13380 *is_non_type = true;
13381 parameter_declarator
13382 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13383 /*parenthesized_p=*/NULL);
13385 if (!parameter_declarator)
13386 return error_mark_node;
13388 /* If the parameter declaration is marked as a parameter pack, set
13389 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13390 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13391 grokdeclarator. */
13392 if (parameter_declarator->declarator
13393 && parameter_declarator->declarator->parameter_pack_p)
13395 *is_parameter_pack = true;
13396 parameter_declarator->declarator->parameter_pack_p = false;
13399 if (parameter_declarator->default_argument)
13401 /* Can happen in some cases of erroneous input (c++/34892). */
13402 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13403 /* Consume the `...' for better error recovery. */
13404 cp_lexer_consume_token (parser->lexer);
13406 /* If the next token is an ellipsis, and we don't already have it
13407 marked as a parameter pack, then we have a parameter pack (that
13408 has no declarator). */
13409 else if (!*is_parameter_pack
13410 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13411 && (declarator_can_be_parameter_pack
13412 (parameter_declarator->declarator)))
13414 /* Consume the `...'. */
13415 cp_lexer_consume_token (parser->lexer);
13416 maybe_warn_variadic_templates ();
13418 *is_parameter_pack = true;
13420 /* We might end up with a pack expansion as the type of the non-type
13421 template parameter, in which case this is a non-type template
13422 parameter pack. */
13423 else if (parameter_declarator->decl_specifiers.type
13424 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13426 *is_parameter_pack = true;
13427 parameter_declarator->decl_specifiers.type =
13428 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13431 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13433 /* Parameter packs cannot have default arguments. However, a
13434 user may try to do so, so we'll parse them and give an
13435 appropriate diagnostic here. */
13437 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13439 /* Find the name of the parameter pack. */
13440 id_declarator = parameter_declarator->declarator;
13441 while (id_declarator && id_declarator->kind != cdk_id)
13442 id_declarator = id_declarator->declarator;
13444 if (id_declarator && id_declarator->kind == cdk_id)
13445 error_at (start_token->location,
13446 "template parameter pack %qD cannot have a default argument",
13447 id_declarator->u.id.unqualified_name);
13448 else
13449 error_at (start_token->location,
13450 "template parameter pack cannot have a default argument");
13452 /* Parse the default argument, but throw away the result. */
13453 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13456 parm = grokdeclarator (parameter_declarator->declarator,
13457 &parameter_declarator->decl_specifiers,
13458 TPARM, /*initialized=*/0,
13459 /*attrlist=*/NULL);
13460 if (parm == error_mark_node)
13461 return error_mark_node;
13463 return build_tree_list (parameter_declarator->default_argument, parm);
13466 /* Parse a type-parameter.
13468 type-parameter:
13469 class identifier [opt]
13470 class identifier [opt] = type-id
13471 typename identifier [opt]
13472 typename identifier [opt] = type-id
13473 template < template-parameter-list > class identifier [opt]
13474 template < template-parameter-list > class identifier [opt]
13475 = id-expression
13477 GNU Extension (variadic templates):
13479 type-parameter:
13480 class ... identifier [opt]
13481 typename ... identifier [opt]
13483 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13484 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13485 the declaration of the parameter.
13487 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13489 static tree
13490 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13492 cp_token *token;
13493 tree parameter;
13495 /* Look for a keyword to tell us what kind of parameter this is. */
13496 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13497 if (!token)
13498 return error_mark_node;
13500 switch (token->keyword)
13502 case RID_CLASS:
13503 case RID_TYPENAME:
13505 tree identifier;
13506 tree default_argument;
13508 /* If the next token is an ellipsis, we have a template
13509 argument pack. */
13510 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13512 /* Consume the `...' token. */
13513 cp_lexer_consume_token (parser->lexer);
13514 maybe_warn_variadic_templates ();
13516 *is_parameter_pack = true;
13519 /* If the next token is an identifier, then it names the
13520 parameter. */
13521 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13522 identifier = cp_parser_identifier (parser);
13523 else
13524 identifier = NULL_TREE;
13526 /* Create the parameter. */
13527 parameter = finish_template_type_parm (class_type_node, identifier);
13529 /* If the next token is an `=', we have a default argument. */
13530 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13532 /* Consume the `=' token. */
13533 cp_lexer_consume_token (parser->lexer);
13534 /* Parse the default-argument. */
13535 push_deferring_access_checks (dk_no_deferred);
13536 default_argument = cp_parser_type_id (parser);
13538 /* Template parameter packs cannot have default
13539 arguments. */
13540 if (*is_parameter_pack)
13542 if (identifier)
13543 error_at (token->location,
13544 "template parameter pack %qD cannot have a "
13545 "default argument", identifier);
13546 else
13547 error_at (token->location,
13548 "template parameter packs cannot have "
13549 "default arguments");
13550 default_argument = NULL_TREE;
13552 else if (check_for_bare_parameter_packs (default_argument))
13553 default_argument = error_mark_node;
13554 pop_deferring_access_checks ();
13556 else
13557 default_argument = NULL_TREE;
13559 /* Create the combined representation of the parameter and the
13560 default argument. */
13561 parameter = build_tree_list (default_argument, parameter);
13563 break;
13565 case RID_TEMPLATE:
13567 tree identifier;
13568 tree default_argument;
13570 /* Look for the `<'. */
13571 cp_parser_require (parser, CPP_LESS, RT_LESS);
13572 /* Parse the template-parameter-list. */
13573 cp_parser_template_parameter_list (parser);
13574 /* Look for the `>'. */
13575 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13576 /* Look for the `class' or 'typename' keywords. */
13577 cp_parser_type_parameter_key (parser);
13578 /* If the next token is an ellipsis, we have a template
13579 argument pack. */
13580 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13582 /* Consume the `...' token. */
13583 cp_lexer_consume_token (parser->lexer);
13584 maybe_warn_variadic_templates ();
13586 *is_parameter_pack = true;
13588 /* If the next token is an `=', then there is a
13589 default-argument. If the next token is a `>', we are at
13590 the end of the parameter-list. If the next token is a `,',
13591 then we are at the end of this parameter. */
13592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13593 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13594 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13596 identifier = cp_parser_identifier (parser);
13597 /* Treat invalid names as if the parameter were nameless. */
13598 if (identifier == error_mark_node)
13599 identifier = NULL_TREE;
13601 else
13602 identifier = NULL_TREE;
13604 /* Create the template parameter. */
13605 parameter = finish_template_template_parm (class_type_node,
13606 identifier);
13608 /* If the next token is an `=', then there is a
13609 default-argument. */
13610 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13612 bool is_template;
13614 /* Consume the `='. */
13615 cp_lexer_consume_token (parser->lexer);
13616 /* Parse the id-expression. */
13617 push_deferring_access_checks (dk_no_deferred);
13618 /* save token before parsing the id-expression, for error
13619 reporting */
13620 token = cp_lexer_peek_token (parser->lexer);
13621 default_argument
13622 = cp_parser_id_expression (parser,
13623 /*template_keyword_p=*/false,
13624 /*check_dependency_p=*/true,
13625 /*template_p=*/&is_template,
13626 /*declarator_p=*/false,
13627 /*optional_p=*/false);
13628 if (TREE_CODE (default_argument) == TYPE_DECL)
13629 /* If the id-expression was a template-id that refers to
13630 a template-class, we already have the declaration here,
13631 so no further lookup is needed. */
13633 else
13634 /* Look up the name. */
13635 default_argument
13636 = cp_parser_lookup_name (parser, default_argument,
13637 none_type,
13638 /*is_template=*/is_template,
13639 /*is_namespace=*/false,
13640 /*check_dependency=*/true,
13641 /*ambiguous_decls=*/NULL,
13642 token->location);
13643 /* See if the default argument is valid. */
13644 default_argument
13645 = check_template_template_default_arg (default_argument);
13647 /* Template parameter packs cannot have default
13648 arguments. */
13649 if (*is_parameter_pack)
13651 if (identifier)
13652 error_at (token->location,
13653 "template parameter pack %qD cannot "
13654 "have a default argument",
13655 identifier);
13656 else
13657 error_at (token->location, "template parameter packs cannot "
13658 "have default arguments");
13659 default_argument = NULL_TREE;
13661 pop_deferring_access_checks ();
13663 else
13664 default_argument = NULL_TREE;
13666 /* Create the combined representation of the parameter and the
13667 default argument. */
13668 parameter = build_tree_list (default_argument, parameter);
13670 break;
13672 default:
13673 gcc_unreachable ();
13674 break;
13677 return parameter;
13680 /* Parse a template-id.
13682 template-id:
13683 template-name < template-argument-list [opt] >
13685 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13686 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13687 returned. Otherwise, if the template-name names a function, or set
13688 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13689 names a class, returns a TYPE_DECL for the specialization.
13691 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13692 uninstantiated templates. */
13694 static tree
13695 cp_parser_template_id (cp_parser *parser,
13696 bool template_keyword_p,
13697 bool check_dependency_p,
13698 enum tag_types tag_type,
13699 bool is_declaration)
13701 int i;
13702 tree templ;
13703 tree arguments;
13704 tree template_id;
13705 cp_token_position start_of_id = 0;
13706 deferred_access_check *chk;
13707 vec<deferred_access_check, va_gc> *access_check;
13708 cp_token *next_token = NULL, *next_token_2 = NULL;
13709 bool is_identifier;
13711 /* If the next token corresponds to a template-id, there is no need
13712 to reparse it. */
13713 next_token = cp_lexer_peek_token (parser->lexer);
13714 if (next_token->type == CPP_TEMPLATE_ID)
13716 struct tree_check *check_value;
13718 /* Get the stored value. */
13719 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13720 /* Perform any access checks that were deferred. */
13721 access_check = check_value->checks;
13722 if (access_check)
13724 FOR_EACH_VEC_ELT (*access_check, i, chk)
13725 perform_or_defer_access_check (chk->binfo,
13726 chk->decl,
13727 chk->diag_decl,
13728 tf_warning_or_error);
13730 /* Return the stored value. */
13731 return check_value->value;
13734 /* Avoid performing name lookup if there is no possibility of
13735 finding a template-id. */
13736 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13737 || (next_token->type == CPP_NAME
13738 && !cp_parser_nth_token_starts_template_argument_list_p
13739 (parser, 2)))
13741 cp_parser_error (parser, "expected template-id");
13742 return error_mark_node;
13745 /* Remember where the template-id starts. */
13746 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13747 start_of_id = cp_lexer_token_position (parser->lexer, false);
13749 push_deferring_access_checks (dk_deferred);
13751 /* Parse the template-name. */
13752 is_identifier = false;
13753 templ = cp_parser_template_name (parser, template_keyword_p,
13754 check_dependency_p,
13755 is_declaration,
13756 tag_type,
13757 &is_identifier);
13758 if (templ == error_mark_node || is_identifier)
13760 pop_deferring_access_checks ();
13761 return templ;
13764 /* If we find the sequence `[:' after a template-name, it's probably
13765 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13766 parse correctly the argument list. */
13767 next_token = cp_lexer_peek_token (parser->lexer);
13768 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13769 if (next_token->type == CPP_OPEN_SQUARE
13770 && next_token->flags & DIGRAPH
13771 && next_token_2->type == CPP_COLON
13772 && !(next_token_2->flags & PREV_WHITE))
13774 cp_parser_parse_tentatively (parser);
13775 /* Change `:' into `::'. */
13776 next_token_2->type = CPP_SCOPE;
13777 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13778 CPP_LESS. */
13779 cp_lexer_consume_token (parser->lexer);
13781 /* Parse the arguments. */
13782 arguments = cp_parser_enclosed_template_argument_list (parser);
13783 if (!cp_parser_parse_definitely (parser))
13785 /* If we couldn't parse an argument list, then we revert our changes
13786 and return simply an error. Maybe this is not a template-id
13787 after all. */
13788 next_token_2->type = CPP_COLON;
13789 cp_parser_error (parser, "expected %<<%>");
13790 pop_deferring_access_checks ();
13791 return error_mark_node;
13793 /* Otherwise, emit an error about the invalid digraph, but continue
13794 parsing because we got our argument list. */
13795 if (permerror (next_token->location,
13796 "%<<::%> cannot begin a template-argument list"))
13798 static bool hint = false;
13799 inform (next_token->location,
13800 "%<<:%> is an alternate spelling for %<[%>."
13801 " Insert whitespace between %<<%> and %<::%>");
13802 if (!hint && !flag_permissive)
13804 inform (next_token->location, "(if you use %<-fpermissive%> "
13805 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13806 "accept your code)");
13807 hint = true;
13811 else
13813 /* Look for the `<' that starts the template-argument-list. */
13814 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13816 pop_deferring_access_checks ();
13817 return error_mark_node;
13819 /* Parse the arguments. */
13820 arguments = cp_parser_enclosed_template_argument_list (parser);
13823 /* Build a representation of the specialization. */
13824 if (identifier_p (templ))
13825 template_id = build_min_nt_loc (next_token->location,
13826 TEMPLATE_ID_EXPR,
13827 templ, arguments);
13828 else if (DECL_TYPE_TEMPLATE_P (templ)
13829 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13831 bool entering_scope;
13832 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13833 template (rather than some instantiation thereof) only if
13834 is not nested within some other construct. For example, in
13835 "template <typename T> void f(T) { A<T>::", A<T> is just an
13836 instantiation of A. */
13837 entering_scope = (template_parm_scope_p ()
13838 && cp_lexer_next_token_is (parser->lexer,
13839 CPP_SCOPE));
13840 template_id
13841 = finish_template_type (templ, arguments, entering_scope);
13843 else if (variable_template_p (templ))
13845 template_id = lookup_template_variable (templ, arguments);
13847 else
13849 /* If it's not a class-template or a template-template, it should be
13850 a function-template. */
13851 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13852 || TREE_CODE (templ) == OVERLOAD
13853 || BASELINK_P (templ)));
13855 template_id = lookup_template_function (templ, arguments);
13858 /* If parsing tentatively, replace the sequence of tokens that makes
13859 up the template-id with a CPP_TEMPLATE_ID token. That way,
13860 should we re-parse the token stream, we will not have to repeat
13861 the effort required to do the parse, nor will we issue duplicate
13862 error messages about problems during instantiation of the
13863 template. */
13864 if (start_of_id
13865 /* Don't do this if we had a parse error in a declarator; re-parsing
13866 might succeed if a name changes meaning (60361). */
13867 && !(cp_parser_error_occurred (parser)
13868 && cp_parser_parsing_tentatively (parser)
13869 && parser->in_declarator_p))
13871 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13873 /* Reset the contents of the START_OF_ID token. */
13874 token->type = CPP_TEMPLATE_ID;
13875 /* Retrieve any deferred checks. Do not pop this access checks yet
13876 so the memory will not be reclaimed during token replacing below. */
13877 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13878 token->u.tree_check_value->value = template_id;
13879 token->u.tree_check_value->checks = get_deferred_access_checks ();
13880 token->keyword = RID_MAX;
13882 /* Purge all subsequent tokens. */
13883 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13885 /* ??? Can we actually assume that, if template_id ==
13886 error_mark_node, we will have issued a diagnostic to the
13887 user, as opposed to simply marking the tentative parse as
13888 failed? */
13889 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13890 error_at (token->location, "parse error in template argument list");
13893 pop_to_parent_deferring_access_checks ();
13894 return template_id;
13897 /* Parse a template-name.
13899 template-name:
13900 identifier
13902 The standard should actually say:
13904 template-name:
13905 identifier
13906 operator-function-id
13908 A defect report has been filed about this issue.
13910 A conversion-function-id cannot be a template name because they cannot
13911 be part of a template-id. In fact, looking at this code:
13913 a.operator K<int>()
13915 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13916 It is impossible to call a templated conversion-function-id with an
13917 explicit argument list, since the only allowed template parameter is
13918 the type to which it is converting.
13920 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13921 `template' keyword, in a construction like:
13923 T::template f<3>()
13925 In that case `f' is taken to be a template-name, even though there
13926 is no way of knowing for sure.
13928 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13929 name refers to a set of overloaded functions, at least one of which
13930 is a template, or an IDENTIFIER_NODE with the name of the template,
13931 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13932 names are looked up inside uninstantiated templates. */
13934 static tree
13935 cp_parser_template_name (cp_parser* parser,
13936 bool template_keyword_p,
13937 bool check_dependency_p,
13938 bool is_declaration,
13939 enum tag_types tag_type,
13940 bool *is_identifier)
13942 tree identifier;
13943 tree decl;
13944 tree fns;
13945 cp_token *token = cp_lexer_peek_token (parser->lexer);
13947 /* If the next token is `operator', then we have either an
13948 operator-function-id or a conversion-function-id. */
13949 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13951 /* We don't know whether we're looking at an
13952 operator-function-id or a conversion-function-id. */
13953 cp_parser_parse_tentatively (parser);
13954 /* Try an operator-function-id. */
13955 identifier = cp_parser_operator_function_id (parser);
13956 /* If that didn't work, try a conversion-function-id. */
13957 if (!cp_parser_parse_definitely (parser))
13959 cp_parser_error (parser, "expected template-name");
13960 return error_mark_node;
13963 /* Look for the identifier. */
13964 else
13965 identifier = cp_parser_identifier (parser);
13967 /* If we didn't find an identifier, we don't have a template-id. */
13968 if (identifier == error_mark_node)
13969 return error_mark_node;
13971 /* If the name immediately followed the `template' keyword, then it
13972 is a template-name. However, if the next token is not `<', then
13973 we do not treat it as a template-name, since it is not being used
13974 as part of a template-id. This enables us to handle constructs
13975 like:
13977 template <typename T> struct S { S(); };
13978 template <typename T> S<T>::S();
13980 correctly. We would treat `S' as a template -- if it were `S<T>'
13981 -- but we do not if there is no `<'. */
13983 if (processing_template_decl
13984 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13986 /* In a declaration, in a dependent context, we pretend that the
13987 "template" keyword was present in order to improve error
13988 recovery. For example, given:
13990 template <typename T> void f(T::X<int>);
13992 we want to treat "X<int>" as a template-id. */
13993 if (is_declaration
13994 && !template_keyword_p
13995 && parser->scope && TYPE_P (parser->scope)
13996 && check_dependency_p
13997 && dependent_scope_p (parser->scope)
13998 /* Do not do this for dtors (or ctors), since they never
13999 need the template keyword before their name. */
14000 && !constructor_name_p (identifier, parser->scope))
14002 cp_token_position start = 0;
14004 /* Explain what went wrong. */
14005 error_at (token->location, "non-template %qD used as template",
14006 identifier);
14007 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14008 parser->scope, identifier);
14009 /* If parsing tentatively, find the location of the "<" token. */
14010 if (cp_parser_simulate_error (parser))
14011 start = cp_lexer_token_position (parser->lexer, true);
14012 /* Parse the template arguments so that we can issue error
14013 messages about them. */
14014 cp_lexer_consume_token (parser->lexer);
14015 cp_parser_enclosed_template_argument_list (parser);
14016 /* Skip tokens until we find a good place from which to
14017 continue parsing. */
14018 cp_parser_skip_to_closing_parenthesis (parser,
14019 /*recovering=*/true,
14020 /*or_comma=*/true,
14021 /*consume_paren=*/false);
14022 /* If parsing tentatively, permanently remove the
14023 template argument list. That will prevent duplicate
14024 error messages from being issued about the missing
14025 "template" keyword. */
14026 if (start)
14027 cp_lexer_purge_tokens_after (parser->lexer, start);
14028 if (is_identifier)
14029 *is_identifier = true;
14030 return identifier;
14033 /* If the "template" keyword is present, then there is generally
14034 no point in doing name-lookup, so we just return IDENTIFIER.
14035 But, if the qualifying scope is non-dependent then we can
14036 (and must) do name-lookup normally. */
14037 if (template_keyword_p
14038 && (!parser->scope
14039 || (TYPE_P (parser->scope)
14040 && dependent_type_p (parser->scope))))
14041 return identifier;
14044 /* Look up the name. */
14045 decl = cp_parser_lookup_name (parser, identifier,
14046 tag_type,
14047 /*is_template=*/true,
14048 /*is_namespace=*/false,
14049 check_dependency_p,
14050 /*ambiguous_decls=*/NULL,
14051 token->location);
14053 decl = strip_using_decl (decl);
14055 /* If DECL is a template, then the name was a template-name. */
14056 if (TREE_CODE (decl) == TEMPLATE_DECL)
14058 if (TREE_DEPRECATED (decl)
14059 && deprecated_state != DEPRECATED_SUPPRESS)
14060 warn_deprecated_use (decl, NULL_TREE);
14062 else
14064 tree fn = NULL_TREE;
14066 /* The standard does not explicitly indicate whether a name that
14067 names a set of overloaded declarations, some of which are
14068 templates, is a template-name. However, such a name should
14069 be a template-name; otherwise, there is no way to form a
14070 template-id for the overloaded templates. */
14071 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14072 if (TREE_CODE (fns) == OVERLOAD)
14073 for (fn = fns; fn; fn = OVL_NEXT (fn))
14074 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14075 break;
14077 if (!fn)
14079 /* The name does not name a template. */
14080 cp_parser_error (parser, "expected template-name");
14081 return error_mark_node;
14085 /* If DECL is dependent, and refers to a function, then just return
14086 its name; we will look it up again during template instantiation. */
14087 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14089 tree scope = ovl_scope (decl);
14090 if (TYPE_P (scope) && dependent_type_p (scope))
14091 return identifier;
14094 return decl;
14097 /* Parse a template-argument-list.
14099 template-argument-list:
14100 template-argument ... [opt]
14101 template-argument-list , template-argument ... [opt]
14103 Returns a TREE_VEC containing the arguments. */
14105 static tree
14106 cp_parser_template_argument_list (cp_parser* parser)
14108 tree fixed_args[10];
14109 unsigned n_args = 0;
14110 unsigned alloced = 10;
14111 tree *arg_ary = fixed_args;
14112 tree vec;
14113 bool saved_in_template_argument_list_p;
14114 bool saved_ice_p;
14115 bool saved_non_ice_p;
14117 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14118 parser->in_template_argument_list_p = true;
14119 /* Even if the template-id appears in an integral
14120 constant-expression, the contents of the argument list do
14121 not. */
14122 saved_ice_p = parser->integral_constant_expression_p;
14123 parser->integral_constant_expression_p = false;
14124 saved_non_ice_p = parser->non_integral_constant_expression_p;
14125 parser->non_integral_constant_expression_p = false;
14127 /* Parse the arguments. */
14130 tree argument;
14132 if (n_args)
14133 /* Consume the comma. */
14134 cp_lexer_consume_token (parser->lexer);
14136 /* Parse the template-argument. */
14137 argument = cp_parser_template_argument (parser);
14139 /* If the next token is an ellipsis, we're expanding a template
14140 argument pack. */
14141 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14143 if (argument == error_mark_node)
14145 cp_token *token = cp_lexer_peek_token (parser->lexer);
14146 error_at (token->location,
14147 "expected parameter pack before %<...%>");
14149 /* Consume the `...' token. */
14150 cp_lexer_consume_token (parser->lexer);
14152 /* Make the argument into a TYPE_PACK_EXPANSION or
14153 EXPR_PACK_EXPANSION. */
14154 argument = make_pack_expansion (argument);
14157 if (n_args == alloced)
14159 alloced *= 2;
14161 if (arg_ary == fixed_args)
14163 arg_ary = XNEWVEC (tree, alloced);
14164 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14166 else
14167 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14169 arg_ary[n_args++] = argument;
14171 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14173 vec = make_tree_vec (n_args);
14175 while (n_args--)
14176 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14178 if (arg_ary != fixed_args)
14179 free (arg_ary);
14180 parser->non_integral_constant_expression_p = saved_non_ice_p;
14181 parser->integral_constant_expression_p = saved_ice_p;
14182 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14183 #ifdef ENABLE_CHECKING
14184 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14185 #endif
14186 return vec;
14189 /* Parse a template-argument.
14191 template-argument:
14192 assignment-expression
14193 type-id
14194 id-expression
14196 The representation is that of an assignment-expression, type-id, or
14197 id-expression -- except that the qualified id-expression is
14198 evaluated, so that the value returned is either a DECL or an
14199 OVERLOAD.
14201 Although the standard says "assignment-expression", it forbids
14202 throw-expressions or assignments in the template argument.
14203 Therefore, we use "conditional-expression" instead. */
14205 static tree
14206 cp_parser_template_argument (cp_parser* parser)
14208 tree argument;
14209 bool template_p;
14210 bool address_p;
14211 bool maybe_type_id = false;
14212 cp_token *token = NULL, *argument_start_token = NULL;
14213 location_t loc = 0;
14214 cp_id_kind idk;
14216 /* There's really no way to know what we're looking at, so we just
14217 try each alternative in order.
14219 [temp.arg]
14221 In a template-argument, an ambiguity between a type-id and an
14222 expression is resolved to a type-id, regardless of the form of
14223 the corresponding template-parameter.
14225 Therefore, we try a type-id first. */
14226 cp_parser_parse_tentatively (parser);
14227 argument = cp_parser_template_type_arg (parser);
14228 /* If there was no error parsing the type-id but the next token is a
14229 '>>', our behavior depends on which dialect of C++ we're
14230 parsing. In C++98, we probably found a typo for '> >'. But there
14231 are type-id which are also valid expressions. For instance:
14233 struct X { int operator >> (int); };
14234 template <int V> struct Foo {};
14235 Foo<X () >> 5> r;
14237 Here 'X()' is a valid type-id of a function type, but the user just
14238 wanted to write the expression "X() >> 5". Thus, we remember that we
14239 found a valid type-id, but we still try to parse the argument as an
14240 expression to see what happens.
14242 In C++0x, the '>>' will be considered two separate '>'
14243 tokens. */
14244 if (!cp_parser_error_occurred (parser)
14245 && cxx_dialect == cxx98
14246 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14248 maybe_type_id = true;
14249 cp_parser_abort_tentative_parse (parser);
14251 else
14253 /* If the next token isn't a `,' or a `>', then this argument wasn't
14254 really finished. This means that the argument is not a valid
14255 type-id. */
14256 if (!cp_parser_next_token_ends_template_argument_p (parser))
14257 cp_parser_error (parser, "expected template-argument");
14258 /* If that worked, we're done. */
14259 if (cp_parser_parse_definitely (parser))
14260 return argument;
14262 /* We're still not sure what the argument will be. */
14263 cp_parser_parse_tentatively (parser);
14264 /* Try a template. */
14265 argument_start_token = cp_lexer_peek_token (parser->lexer);
14266 argument = cp_parser_id_expression (parser,
14267 /*template_keyword_p=*/false,
14268 /*check_dependency_p=*/true,
14269 &template_p,
14270 /*declarator_p=*/false,
14271 /*optional_p=*/false);
14272 /* If the next token isn't a `,' or a `>', then this argument wasn't
14273 really finished. */
14274 if (!cp_parser_next_token_ends_template_argument_p (parser))
14275 cp_parser_error (parser, "expected template-argument");
14276 if (!cp_parser_error_occurred (parser))
14278 /* Figure out what is being referred to. If the id-expression
14279 was for a class template specialization, then we will have a
14280 TYPE_DECL at this point. There is no need to do name lookup
14281 at this point in that case. */
14282 if (TREE_CODE (argument) != TYPE_DECL)
14283 argument = cp_parser_lookup_name (parser, argument,
14284 none_type,
14285 /*is_template=*/template_p,
14286 /*is_namespace=*/false,
14287 /*check_dependency=*/true,
14288 /*ambiguous_decls=*/NULL,
14289 argument_start_token->location);
14290 if (TREE_CODE (argument) != TEMPLATE_DECL
14291 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14292 cp_parser_error (parser, "expected template-name");
14294 if (cp_parser_parse_definitely (parser))
14296 if (TREE_DEPRECATED (argument))
14297 warn_deprecated_use (argument, NULL_TREE);
14298 return argument;
14300 /* It must be a non-type argument. There permitted cases are given
14301 in [temp.arg.nontype]:
14303 -- an integral constant-expression of integral or enumeration
14304 type; or
14306 -- the name of a non-type template-parameter; or
14308 -- the name of an object or function with external linkage...
14310 -- the address of an object or function with external linkage...
14312 -- a pointer to member... */
14313 /* Look for a non-type template parameter. */
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14316 cp_parser_parse_tentatively (parser);
14317 argument = cp_parser_primary_expression (parser,
14318 /*address_p=*/false,
14319 /*cast_p=*/false,
14320 /*template_arg_p=*/true,
14321 &idk);
14322 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14323 || !cp_parser_next_token_ends_template_argument_p (parser))
14324 cp_parser_simulate_error (parser);
14325 if (cp_parser_parse_definitely (parser))
14326 return argument;
14329 /* If the next token is "&", the argument must be the address of an
14330 object or function with external linkage. */
14331 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14332 if (address_p)
14334 loc = cp_lexer_peek_token (parser->lexer)->location;
14335 cp_lexer_consume_token (parser->lexer);
14337 /* See if we might have an id-expression. */
14338 token = cp_lexer_peek_token (parser->lexer);
14339 if (token->type == CPP_NAME
14340 || token->keyword == RID_OPERATOR
14341 || token->type == CPP_SCOPE
14342 || token->type == CPP_TEMPLATE_ID
14343 || token->type == CPP_NESTED_NAME_SPECIFIER)
14345 cp_parser_parse_tentatively (parser);
14346 argument = cp_parser_primary_expression (parser,
14347 address_p,
14348 /*cast_p=*/false,
14349 /*template_arg_p=*/true,
14350 &idk);
14351 if (cp_parser_error_occurred (parser)
14352 || !cp_parser_next_token_ends_template_argument_p (parser))
14353 cp_parser_abort_tentative_parse (parser);
14354 else
14356 tree probe;
14358 if (INDIRECT_REF_P (argument))
14360 /* Strip the dereference temporarily. */
14361 gcc_assert (REFERENCE_REF_P (argument));
14362 argument = TREE_OPERAND (argument, 0);
14365 /* If we're in a template, we represent a qualified-id referring
14366 to a static data member as a SCOPE_REF even if the scope isn't
14367 dependent so that we can check access control later. */
14368 probe = argument;
14369 if (TREE_CODE (probe) == SCOPE_REF)
14370 probe = TREE_OPERAND (probe, 1);
14371 if (VAR_P (probe))
14373 /* A variable without external linkage might still be a
14374 valid constant-expression, so no error is issued here
14375 if the external-linkage check fails. */
14376 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14377 cp_parser_simulate_error (parser);
14379 else if (is_overloaded_fn (argument))
14380 /* All overloaded functions are allowed; if the external
14381 linkage test does not pass, an error will be issued
14382 later. */
14384 else if (address_p
14385 && (TREE_CODE (argument) == OFFSET_REF
14386 || TREE_CODE (argument) == SCOPE_REF))
14387 /* A pointer-to-member. */
14389 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14391 else
14392 cp_parser_simulate_error (parser);
14394 if (cp_parser_parse_definitely (parser))
14396 if (address_p)
14397 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14398 tf_warning_or_error);
14399 else
14400 argument = convert_from_reference (argument);
14401 return argument;
14405 /* If the argument started with "&", there are no other valid
14406 alternatives at this point. */
14407 if (address_p)
14409 cp_parser_error (parser, "invalid non-type template argument");
14410 return error_mark_node;
14413 /* If the argument wasn't successfully parsed as a type-id followed
14414 by '>>', the argument can only be a constant expression now.
14415 Otherwise, we try parsing the constant-expression tentatively,
14416 because the argument could really be a type-id. */
14417 if (maybe_type_id)
14418 cp_parser_parse_tentatively (parser);
14419 argument = cp_parser_constant_expression (parser);
14421 if (!maybe_type_id)
14422 return argument;
14423 if (!cp_parser_next_token_ends_template_argument_p (parser))
14424 cp_parser_error (parser, "expected template-argument");
14425 if (cp_parser_parse_definitely (parser))
14426 return argument;
14427 /* We did our best to parse the argument as a non type-id, but that
14428 was the only alternative that matched (albeit with a '>' after
14429 it). We can assume it's just a typo from the user, and a
14430 diagnostic will then be issued. */
14431 return cp_parser_template_type_arg (parser);
14434 /* Parse an explicit-instantiation.
14436 explicit-instantiation:
14437 template declaration
14439 Although the standard says `declaration', what it really means is:
14441 explicit-instantiation:
14442 template decl-specifier-seq [opt] declarator [opt] ;
14444 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14445 supposed to be allowed. A defect report has been filed about this
14446 issue.
14448 GNU Extension:
14450 explicit-instantiation:
14451 storage-class-specifier template
14452 decl-specifier-seq [opt] declarator [opt] ;
14453 function-specifier template
14454 decl-specifier-seq [opt] declarator [opt] ; */
14456 static void
14457 cp_parser_explicit_instantiation (cp_parser* parser)
14459 int declares_class_or_enum;
14460 cp_decl_specifier_seq decl_specifiers;
14461 tree extension_specifier = NULL_TREE;
14463 timevar_push (TV_TEMPLATE_INST);
14465 /* Look for an (optional) storage-class-specifier or
14466 function-specifier. */
14467 if (cp_parser_allow_gnu_extensions_p (parser))
14469 extension_specifier
14470 = cp_parser_storage_class_specifier_opt (parser);
14471 if (!extension_specifier)
14472 extension_specifier
14473 = cp_parser_function_specifier_opt (parser,
14474 /*decl_specs=*/NULL);
14477 /* Look for the `template' keyword. */
14478 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14479 /* Let the front end know that we are processing an explicit
14480 instantiation. */
14481 begin_explicit_instantiation ();
14482 /* [temp.explicit] says that we are supposed to ignore access
14483 control while processing explicit instantiation directives. */
14484 push_deferring_access_checks (dk_no_check);
14485 /* Parse a decl-specifier-seq. */
14486 cp_parser_decl_specifier_seq (parser,
14487 CP_PARSER_FLAGS_OPTIONAL,
14488 &decl_specifiers,
14489 &declares_class_or_enum);
14490 /* If there was exactly one decl-specifier, and it declared a class,
14491 and there's no declarator, then we have an explicit type
14492 instantiation. */
14493 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14495 tree type;
14497 type = check_tag_decl (&decl_specifiers,
14498 /*explicit_type_instantiation_p=*/true);
14499 /* Turn access control back on for names used during
14500 template instantiation. */
14501 pop_deferring_access_checks ();
14502 if (type)
14503 do_type_instantiation (type, extension_specifier,
14504 /*complain=*/tf_error);
14506 else
14508 cp_declarator *declarator;
14509 tree decl;
14511 /* Parse the declarator. */
14512 declarator
14513 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14514 /*ctor_dtor_or_conv_p=*/NULL,
14515 /*parenthesized_p=*/NULL,
14516 /*member_p=*/false,
14517 /*friend_p=*/false);
14518 if (declares_class_or_enum & 2)
14519 cp_parser_check_for_definition_in_return_type (declarator,
14520 decl_specifiers.type,
14521 decl_specifiers.locations[ds_type_spec]);
14522 if (declarator != cp_error_declarator)
14524 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14525 permerror (decl_specifiers.locations[ds_inline],
14526 "explicit instantiation shall not use"
14527 " %<inline%> specifier");
14528 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14529 permerror (decl_specifiers.locations[ds_constexpr],
14530 "explicit instantiation shall not use"
14531 " %<constexpr%> specifier");
14533 decl = grokdeclarator (declarator, &decl_specifiers,
14534 NORMAL, 0, &decl_specifiers.attributes);
14535 /* Turn access control back on for names used during
14536 template instantiation. */
14537 pop_deferring_access_checks ();
14538 /* Do the explicit instantiation. */
14539 do_decl_instantiation (decl, extension_specifier);
14541 else
14543 pop_deferring_access_checks ();
14544 /* Skip the body of the explicit instantiation. */
14545 cp_parser_skip_to_end_of_statement (parser);
14548 /* We're done with the instantiation. */
14549 end_explicit_instantiation ();
14551 cp_parser_consume_semicolon_at_end_of_statement (parser);
14553 timevar_pop (TV_TEMPLATE_INST);
14556 /* Parse an explicit-specialization.
14558 explicit-specialization:
14559 template < > declaration
14561 Although the standard says `declaration', what it really means is:
14563 explicit-specialization:
14564 template <> decl-specifier [opt] init-declarator [opt] ;
14565 template <> function-definition
14566 template <> explicit-specialization
14567 template <> template-declaration */
14569 static void
14570 cp_parser_explicit_specialization (cp_parser* parser)
14572 bool need_lang_pop;
14573 cp_token *token = cp_lexer_peek_token (parser->lexer);
14575 /* Look for the `template' keyword. */
14576 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14577 /* Look for the `<'. */
14578 cp_parser_require (parser, CPP_LESS, RT_LESS);
14579 /* Look for the `>'. */
14580 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14581 /* We have processed another parameter list. */
14582 ++parser->num_template_parameter_lists;
14583 /* [temp]
14585 A template ... explicit specialization ... shall not have C
14586 linkage. */
14587 if (current_lang_name == lang_name_c)
14589 error_at (token->location, "template specialization with C linkage");
14590 /* Give it C++ linkage to avoid confusing other parts of the
14591 front end. */
14592 push_lang_context (lang_name_cplusplus);
14593 need_lang_pop = true;
14595 else
14596 need_lang_pop = false;
14597 /* Let the front end know that we are beginning a specialization. */
14598 if (!begin_specialization ())
14600 end_specialization ();
14601 return;
14604 /* If the next keyword is `template', we need to figure out whether
14605 or not we're looking a template-declaration. */
14606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14608 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14609 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14610 cp_parser_template_declaration_after_export (parser,
14611 /*member_p=*/false);
14612 else
14613 cp_parser_explicit_specialization (parser);
14615 else
14616 /* Parse the dependent declaration. */
14617 cp_parser_single_declaration (parser,
14618 /*checks=*/NULL,
14619 /*member_p=*/false,
14620 /*explicit_specialization_p=*/true,
14621 /*friend_p=*/NULL);
14622 /* We're done with the specialization. */
14623 end_specialization ();
14624 /* For the erroneous case of a template with C linkage, we pushed an
14625 implicit C++ linkage scope; exit that scope now. */
14626 if (need_lang_pop)
14627 pop_lang_context ();
14628 /* We're done with this parameter list. */
14629 --parser->num_template_parameter_lists;
14632 /* Parse a type-specifier.
14634 type-specifier:
14635 simple-type-specifier
14636 class-specifier
14637 enum-specifier
14638 elaborated-type-specifier
14639 cv-qualifier
14641 GNU Extension:
14643 type-specifier:
14644 __complex__
14646 Returns a representation of the type-specifier. For a
14647 class-specifier, enum-specifier, or elaborated-type-specifier, a
14648 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14650 The parser flags FLAGS is used to control type-specifier parsing.
14652 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14653 in a decl-specifier-seq.
14655 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14656 class-specifier, enum-specifier, or elaborated-type-specifier, then
14657 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14658 if a type is declared; 2 if it is defined. Otherwise, it is set to
14659 zero.
14661 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14662 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14663 is set to FALSE. */
14665 static tree
14666 cp_parser_type_specifier (cp_parser* parser,
14667 cp_parser_flags flags,
14668 cp_decl_specifier_seq *decl_specs,
14669 bool is_declaration,
14670 int* declares_class_or_enum,
14671 bool* is_cv_qualifier)
14673 tree type_spec = NULL_TREE;
14674 cp_token *token;
14675 enum rid keyword;
14676 cp_decl_spec ds = ds_last;
14678 /* Assume this type-specifier does not declare a new type. */
14679 if (declares_class_or_enum)
14680 *declares_class_or_enum = 0;
14681 /* And that it does not specify a cv-qualifier. */
14682 if (is_cv_qualifier)
14683 *is_cv_qualifier = false;
14684 /* Peek at the next token. */
14685 token = cp_lexer_peek_token (parser->lexer);
14687 /* If we're looking at a keyword, we can use that to guide the
14688 production we choose. */
14689 keyword = token->keyword;
14690 switch (keyword)
14692 case RID_ENUM:
14693 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14694 goto elaborated_type_specifier;
14696 /* Look for the enum-specifier. */
14697 type_spec = cp_parser_enum_specifier (parser);
14698 /* If that worked, we're done. */
14699 if (type_spec)
14701 if (declares_class_or_enum)
14702 *declares_class_or_enum = 2;
14703 if (decl_specs)
14704 cp_parser_set_decl_spec_type (decl_specs,
14705 type_spec,
14706 token,
14707 /*type_definition_p=*/true);
14708 return type_spec;
14710 else
14711 goto elaborated_type_specifier;
14713 /* Any of these indicate either a class-specifier, or an
14714 elaborated-type-specifier. */
14715 case RID_CLASS:
14716 case RID_STRUCT:
14717 case RID_UNION:
14718 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14719 goto elaborated_type_specifier;
14721 /* Parse tentatively so that we can back up if we don't find a
14722 class-specifier. */
14723 cp_parser_parse_tentatively (parser);
14724 /* Look for the class-specifier. */
14725 type_spec = cp_parser_class_specifier (parser);
14726 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14727 /* If that worked, we're done. */
14728 if (cp_parser_parse_definitely (parser))
14730 if (declares_class_or_enum)
14731 *declares_class_or_enum = 2;
14732 if (decl_specs)
14733 cp_parser_set_decl_spec_type (decl_specs,
14734 type_spec,
14735 token,
14736 /*type_definition_p=*/true);
14737 return type_spec;
14740 /* Fall through. */
14741 elaborated_type_specifier:
14742 /* We're declaring (not defining) a class or enum. */
14743 if (declares_class_or_enum)
14744 *declares_class_or_enum = 1;
14746 /* Fall through. */
14747 case RID_TYPENAME:
14748 /* Look for an elaborated-type-specifier. */
14749 type_spec
14750 = (cp_parser_elaborated_type_specifier
14751 (parser,
14752 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14753 is_declaration));
14754 if (decl_specs)
14755 cp_parser_set_decl_spec_type (decl_specs,
14756 type_spec,
14757 token,
14758 /*type_definition_p=*/false);
14759 return type_spec;
14761 case RID_CONST:
14762 ds = ds_const;
14763 if (is_cv_qualifier)
14764 *is_cv_qualifier = true;
14765 break;
14767 case RID_VOLATILE:
14768 ds = ds_volatile;
14769 if (is_cv_qualifier)
14770 *is_cv_qualifier = true;
14771 break;
14773 case RID_RESTRICT:
14774 ds = ds_restrict;
14775 if (is_cv_qualifier)
14776 *is_cv_qualifier = true;
14777 break;
14779 case RID_COMPLEX:
14780 /* The `__complex__' keyword is a GNU extension. */
14781 ds = ds_complex;
14782 break;
14784 default:
14785 break;
14788 /* Handle simple keywords. */
14789 if (ds != ds_last)
14791 if (decl_specs)
14793 set_and_check_decl_spec_loc (decl_specs, ds, token);
14794 decl_specs->any_specifiers_p = true;
14796 return cp_lexer_consume_token (parser->lexer)->u.value;
14799 /* If we do not already have a type-specifier, assume we are looking
14800 at a simple-type-specifier. */
14801 type_spec = cp_parser_simple_type_specifier (parser,
14802 decl_specs,
14803 flags);
14805 /* If we didn't find a type-specifier, and a type-specifier was not
14806 optional in this context, issue an error message. */
14807 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14809 cp_parser_error (parser, "expected type specifier");
14810 return error_mark_node;
14813 return type_spec;
14816 /* Parse a simple-type-specifier.
14818 simple-type-specifier:
14819 :: [opt] nested-name-specifier [opt] type-name
14820 :: [opt] nested-name-specifier template template-id
14821 char
14822 wchar_t
14823 bool
14824 short
14826 long
14827 signed
14828 unsigned
14829 float
14830 double
14831 void
14833 C++0x Extension:
14835 simple-type-specifier:
14836 auto
14837 decltype ( expression )
14838 char16_t
14839 char32_t
14840 __underlying_type ( type-id )
14842 GNU Extension:
14844 simple-type-specifier:
14845 __int128
14846 __typeof__ unary-expression
14847 __typeof__ ( type-id )
14848 __typeof__ ( type-id ) { initializer-list , [opt] }
14850 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14851 appropriately updated. */
14853 static tree
14854 cp_parser_simple_type_specifier (cp_parser* parser,
14855 cp_decl_specifier_seq *decl_specs,
14856 cp_parser_flags flags)
14858 tree type = NULL_TREE;
14859 cp_token *token;
14860 int idx;
14862 /* Peek at the next token. */
14863 token = cp_lexer_peek_token (parser->lexer);
14865 /* If we're looking at a keyword, things are easy. */
14866 switch (token->keyword)
14868 case RID_CHAR:
14869 if (decl_specs)
14870 decl_specs->explicit_char_p = true;
14871 type = char_type_node;
14872 break;
14873 case RID_CHAR16:
14874 type = char16_type_node;
14875 break;
14876 case RID_CHAR32:
14877 type = char32_type_node;
14878 break;
14879 case RID_WCHAR:
14880 type = wchar_type_node;
14881 break;
14882 case RID_BOOL:
14883 type = boolean_type_node;
14884 break;
14885 case RID_SHORT:
14886 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14887 type = short_integer_type_node;
14888 break;
14889 case RID_INT:
14890 if (decl_specs)
14891 decl_specs->explicit_int_p = true;
14892 type = integer_type_node;
14893 break;
14894 case RID_INT_N_0:
14895 case RID_INT_N_1:
14896 case RID_INT_N_2:
14897 case RID_INT_N_3:
14898 idx = token->keyword - RID_INT_N_0;
14899 if (! int_n_enabled_p [idx])
14900 break;
14901 if (decl_specs)
14903 decl_specs->explicit_intN_p = true;
14904 decl_specs->int_n_idx = idx;
14906 type = int_n_trees [idx].signed_type;
14907 break;
14908 case RID_LONG:
14909 if (decl_specs)
14910 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14911 type = long_integer_type_node;
14912 break;
14913 case RID_SIGNED:
14914 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14915 type = integer_type_node;
14916 break;
14917 case RID_UNSIGNED:
14918 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14919 type = unsigned_type_node;
14920 break;
14921 case RID_FLOAT:
14922 type = float_type_node;
14923 break;
14924 case RID_DOUBLE:
14925 type = double_type_node;
14926 break;
14927 case RID_VOID:
14928 type = void_type_node;
14929 break;
14931 case RID_AUTO:
14932 maybe_warn_cpp0x (CPP0X_AUTO);
14933 if (parser->auto_is_implicit_function_template_parm_p)
14935 if (cxx_dialect >= cxx14)
14936 type = synthesize_implicit_template_parm (parser);
14937 else
14938 type = error_mark_node;
14940 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14942 if (cxx_dialect < cxx14)
14943 error_at (token->location,
14944 "use of %<auto%> in lambda parameter declaration "
14945 "only available with "
14946 "-std=c++14 or -std=gnu++14");
14948 else if (cxx_dialect < cxx14)
14949 error_at (token->location,
14950 "use of %<auto%> in parameter declaration "
14951 "only available with "
14952 "-std=c++14 or -std=gnu++14");
14953 else
14954 pedwarn (token->location, OPT_Wpedantic,
14955 "ISO C++ forbids use of %<auto%> in parameter "
14956 "declaration");
14958 else
14959 type = make_auto ();
14960 break;
14962 case RID_DECLTYPE:
14963 /* Since DR 743, decltype can either be a simple-type-specifier by
14964 itself or begin a nested-name-specifier. Parsing it will replace
14965 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14966 handling below decide what to do. */
14967 cp_parser_decltype (parser);
14968 cp_lexer_set_token_position (parser->lexer, token);
14969 break;
14971 case RID_TYPEOF:
14972 /* Consume the `typeof' token. */
14973 cp_lexer_consume_token (parser->lexer);
14974 /* Parse the operand to `typeof'. */
14975 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14976 /* If it is not already a TYPE, take its type. */
14977 if (!TYPE_P (type))
14978 type = finish_typeof (type);
14980 if (decl_specs)
14981 cp_parser_set_decl_spec_type (decl_specs, type,
14982 token,
14983 /*type_definition_p=*/false);
14985 return type;
14987 case RID_UNDERLYING_TYPE:
14988 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14989 if (decl_specs)
14990 cp_parser_set_decl_spec_type (decl_specs, type,
14991 token,
14992 /*type_definition_p=*/false);
14994 return type;
14996 case RID_BASES:
14997 case RID_DIRECT_BASES:
14998 type = cp_parser_trait_expr (parser, token->keyword);
14999 if (decl_specs)
15000 cp_parser_set_decl_spec_type (decl_specs, type,
15001 token,
15002 /*type_definition_p=*/false);
15003 return type;
15004 default:
15005 break;
15008 /* If token is an already-parsed decltype not followed by ::,
15009 it's a simple-type-specifier. */
15010 if (token->type == CPP_DECLTYPE
15011 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15013 type = token->u.value;
15014 if (decl_specs)
15016 cp_parser_set_decl_spec_type (decl_specs, type,
15017 token,
15018 /*type_definition_p=*/false);
15019 /* Remember that we are handling a decltype in order to
15020 implement the resolution of DR 1510 when the argument
15021 isn't instantiation dependent. */
15022 decl_specs->decltype_p = true;
15024 cp_lexer_consume_token (parser->lexer);
15025 return type;
15028 /* If the type-specifier was for a built-in type, we're done. */
15029 if (type)
15031 /* Record the type. */
15032 if (decl_specs
15033 && (token->keyword != RID_SIGNED
15034 && token->keyword != RID_UNSIGNED
15035 && token->keyword != RID_SHORT
15036 && token->keyword != RID_LONG))
15037 cp_parser_set_decl_spec_type (decl_specs,
15038 type,
15039 token,
15040 /*type_definition_p=*/false);
15041 if (decl_specs)
15042 decl_specs->any_specifiers_p = true;
15044 /* Consume the token. */
15045 cp_lexer_consume_token (parser->lexer);
15047 if (type == error_mark_node)
15048 return error_mark_node;
15050 /* There is no valid C++ program where a non-template type is
15051 followed by a "<". That usually indicates that the user thought
15052 that the type was a template. */
15053 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15054 token->location);
15056 return TYPE_NAME (type);
15059 /* The type-specifier must be a user-defined type. */
15060 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15062 bool qualified_p;
15063 bool global_p;
15065 /* Don't gobble tokens or issue error messages if this is an
15066 optional type-specifier. */
15067 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15068 cp_parser_parse_tentatively (parser);
15070 /* Look for the optional `::' operator. */
15071 global_p
15072 = (cp_parser_global_scope_opt (parser,
15073 /*current_scope_valid_p=*/false)
15074 != NULL_TREE);
15075 /* Look for the nested-name specifier. */
15076 qualified_p
15077 = (cp_parser_nested_name_specifier_opt (parser,
15078 /*typename_keyword_p=*/false,
15079 /*check_dependency_p=*/true,
15080 /*type_p=*/false,
15081 /*is_declaration=*/false)
15082 != NULL_TREE);
15083 token = cp_lexer_peek_token (parser->lexer);
15084 /* If we have seen a nested-name-specifier, and the next token
15085 is `template', then we are using the template-id production. */
15086 if (parser->scope
15087 && cp_parser_optional_template_keyword (parser))
15089 /* Look for the template-id. */
15090 type = cp_parser_template_id (parser,
15091 /*template_keyword_p=*/true,
15092 /*check_dependency_p=*/true,
15093 none_type,
15094 /*is_declaration=*/false);
15095 /* If the template-id did not name a type, we are out of
15096 luck. */
15097 if (TREE_CODE (type) != TYPE_DECL)
15099 cp_parser_error (parser, "expected template-id for type");
15100 type = NULL_TREE;
15103 /* Otherwise, look for a type-name. */
15104 else
15105 type = cp_parser_type_name (parser);
15106 /* Keep track of all name-lookups performed in class scopes. */
15107 if (type
15108 && !global_p
15109 && !qualified_p
15110 && TREE_CODE (type) == TYPE_DECL
15111 && identifier_p (DECL_NAME (type)))
15112 maybe_note_name_used_in_class (DECL_NAME (type), type);
15113 /* If it didn't work out, we don't have a TYPE. */
15114 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15115 && !cp_parser_parse_definitely (parser))
15116 type = NULL_TREE;
15117 if (type && decl_specs)
15118 cp_parser_set_decl_spec_type (decl_specs, type,
15119 token,
15120 /*type_definition_p=*/false);
15123 /* If we didn't get a type-name, issue an error message. */
15124 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15126 cp_parser_error (parser, "expected type-name");
15127 return error_mark_node;
15130 if (type && type != error_mark_node)
15132 /* See if TYPE is an Objective-C type, and if so, parse and
15133 accept any protocol references following it. Do this before
15134 the cp_parser_check_for_invalid_template_id() call, because
15135 Objective-C types can be followed by '<...>' which would
15136 enclose protocol names rather than template arguments, and so
15137 everything is fine. */
15138 if (c_dialect_objc () && !parser->scope
15139 && (objc_is_id (type) || objc_is_class_name (type)))
15141 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15142 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15144 /* Clobber the "unqualified" type previously entered into
15145 DECL_SPECS with the new, improved protocol-qualified version. */
15146 if (decl_specs)
15147 decl_specs->type = qual_type;
15149 return qual_type;
15152 /* There is no valid C++ program where a non-template type is
15153 followed by a "<". That usually indicates that the user
15154 thought that the type was a template. */
15155 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15156 none_type,
15157 token->location);
15160 return type;
15163 /* Parse a type-name.
15165 type-name:
15166 class-name
15167 enum-name
15168 typedef-name
15169 simple-template-id [in c++0x]
15171 enum-name:
15172 identifier
15174 typedef-name:
15175 identifier
15177 Returns a TYPE_DECL for the type. */
15179 static tree
15180 cp_parser_type_name (cp_parser* parser)
15182 tree type_decl;
15184 /* We can't know yet whether it is a class-name or not. */
15185 cp_parser_parse_tentatively (parser);
15186 /* Try a class-name. */
15187 type_decl = cp_parser_class_name (parser,
15188 /*typename_keyword_p=*/false,
15189 /*template_keyword_p=*/false,
15190 none_type,
15191 /*check_dependency_p=*/true,
15192 /*class_head_p=*/false,
15193 /*is_declaration=*/false);
15194 /* If it's not a class-name, keep looking. */
15195 if (!cp_parser_parse_definitely (parser))
15197 if (cxx_dialect < cxx11)
15198 /* It must be a typedef-name or an enum-name. */
15199 return cp_parser_nonclass_name (parser);
15201 cp_parser_parse_tentatively (parser);
15202 /* It is either a simple-template-id representing an
15203 instantiation of an alias template... */
15204 type_decl = cp_parser_template_id (parser,
15205 /*template_keyword_p=*/false,
15206 /*check_dependency_p=*/true,
15207 none_type,
15208 /*is_declaration=*/false);
15209 /* Note that this must be an instantiation of an alias template
15210 because [temp.names]/6 says:
15212 A template-id that names an alias template specialization
15213 is a type-name.
15215 Whereas [temp.names]/7 says:
15217 A simple-template-id that names a class template
15218 specialization is a class-name. */
15219 if (type_decl != NULL_TREE
15220 && TREE_CODE (type_decl) == TYPE_DECL
15221 && TYPE_DECL_ALIAS_P (type_decl))
15222 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15223 else
15224 cp_parser_simulate_error (parser);
15226 if (!cp_parser_parse_definitely (parser))
15227 /* ... Or a typedef-name or an enum-name. */
15228 return cp_parser_nonclass_name (parser);
15231 return type_decl;
15234 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15236 enum-name:
15237 identifier
15239 typedef-name:
15240 identifier
15242 Returns a TYPE_DECL for the type. */
15244 static tree
15245 cp_parser_nonclass_name (cp_parser* parser)
15247 tree type_decl;
15248 tree identifier;
15250 cp_token *token = cp_lexer_peek_token (parser->lexer);
15251 identifier = cp_parser_identifier (parser);
15252 if (identifier == error_mark_node)
15253 return error_mark_node;
15255 /* Look up the type-name. */
15256 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15258 type_decl = strip_using_decl (type_decl);
15260 if (TREE_CODE (type_decl) != TYPE_DECL
15261 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15263 /* See if this is an Objective-C type. */
15264 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15265 tree type = objc_get_protocol_qualified_type (identifier, protos);
15266 if (type)
15267 type_decl = TYPE_NAME (type);
15270 /* Issue an error if we did not find a type-name. */
15271 if (TREE_CODE (type_decl) != TYPE_DECL
15272 /* In Objective-C, we have the complication that class names are
15273 normally type names and start declarations (eg, the
15274 "NSObject" in "NSObject *object;"), but can be used in an
15275 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15276 is an expression. So, a classname followed by a dot is not a
15277 valid type-name. */
15278 || (objc_is_class_name (TREE_TYPE (type_decl))
15279 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15281 if (!cp_parser_simulate_error (parser))
15282 cp_parser_name_lookup_error (parser, identifier, type_decl,
15283 NLE_TYPE, token->location);
15284 return error_mark_node;
15286 /* Remember that the name was used in the definition of the
15287 current class so that we can check later to see if the
15288 meaning would have been different after the class was
15289 entirely defined. */
15290 else if (type_decl != error_mark_node
15291 && !parser->scope)
15292 maybe_note_name_used_in_class (identifier, type_decl);
15294 return type_decl;
15297 /* Parse an elaborated-type-specifier. Note that the grammar given
15298 here incorporates the resolution to DR68.
15300 elaborated-type-specifier:
15301 class-key :: [opt] nested-name-specifier [opt] identifier
15302 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15303 enum-key :: [opt] nested-name-specifier [opt] identifier
15304 typename :: [opt] nested-name-specifier identifier
15305 typename :: [opt] nested-name-specifier template [opt]
15306 template-id
15308 GNU extension:
15310 elaborated-type-specifier:
15311 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15312 class-key attributes :: [opt] nested-name-specifier [opt]
15313 template [opt] template-id
15314 enum attributes :: [opt] nested-name-specifier [opt] identifier
15316 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15317 declared `friend'. If IS_DECLARATION is TRUE, then this
15318 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15319 something is being declared.
15321 Returns the TYPE specified. */
15323 static tree
15324 cp_parser_elaborated_type_specifier (cp_parser* parser,
15325 bool is_friend,
15326 bool is_declaration)
15328 enum tag_types tag_type;
15329 tree identifier;
15330 tree type = NULL_TREE;
15331 tree attributes = NULL_TREE;
15332 tree globalscope;
15333 cp_token *token = NULL;
15335 /* See if we're looking at the `enum' keyword. */
15336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15338 /* Consume the `enum' token. */
15339 cp_lexer_consume_token (parser->lexer);
15340 /* Remember that it's an enumeration type. */
15341 tag_type = enum_type;
15342 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15343 enums) is used here. */
15344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15345 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15347 pedwarn (input_location, 0, "elaborated-type-specifier "
15348 "for a scoped enum must not use the %<%D%> keyword",
15349 cp_lexer_peek_token (parser->lexer)->u.value);
15350 /* Consume the `struct' or `class' and parse it anyway. */
15351 cp_lexer_consume_token (parser->lexer);
15353 /* Parse the attributes. */
15354 attributes = cp_parser_attributes_opt (parser);
15356 /* Or, it might be `typename'. */
15357 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15358 RID_TYPENAME))
15360 /* Consume the `typename' token. */
15361 cp_lexer_consume_token (parser->lexer);
15362 /* Remember that it's a `typename' type. */
15363 tag_type = typename_type;
15365 /* Otherwise it must be a class-key. */
15366 else
15368 tag_type = cp_parser_class_key (parser);
15369 if (tag_type == none_type)
15370 return error_mark_node;
15371 /* Parse the attributes. */
15372 attributes = cp_parser_attributes_opt (parser);
15375 /* Look for the `::' operator. */
15376 globalscope = cp_parser_global_scope_opt (parser,
15377 /*current_scope_valid_p=*/false);
15378 /* Look for the nested-name-specifier. */
15379 if (tag_type == typename_type && !globalscope)
15381 if (!cp_parser_nested_name_specifier (parser,
15382 /*typename_keyword_p=*/true,
15383 /*check_dependency_p=*/true,
15384 /*type_p=*/true,
15385 is_declaration))
15386 return error_mark_node;
15388 else
15389 /* Even though `typename' is not present, the proposed resolution
15390 to Core Issue 180 says that in `class A<T>::B', `B' should be
15391 considered a type-name, even if `A<T>' is dependent. */
15392 cp_parser_nested_name_specifier_opt (parser,
15393 /*typename_keyword_p=*/true,
15394 /*check_dependency_p=*/true,
15395 /*type_p=*/true,
15396 is_declaration);
15397 /* For everything but enumeration types, consider a template-id.
15398 For an enumeration type, consider only a plain identifier. */
15399 if (tag_type != enum_type)
15401 bool template_p = false;
15402 tree decl;
15404 /* Allow the `template' keyword. */
15405 template_p = cp_parser_optional_template_keyword (parser);
15406 /* If we didn't see `template', we don't know if there's a
15407 template-id or not. */
15408 if (!template_p)
15409 cp_parser_parse_tentatively (parser);
15410 /* Parse the template-id. */
15411 token = cp_lexer_peek_token (parser->lexer);
15412 decl = cp_parser_template_id (parser, template_p,
15413 /*check_dependency_p=*/true,
15414 tag_type,
15415 is_declaration);
15416 /* If we didn't find a template-id, look for an ordinary
15417 identifier. */
15418 if (!template_p && !cp_parser_parse_definitely (parser))
15420 /* We can get here when cp_parser_template_id, called by
15421 cp_parser_class_name with tag_type == none_type, succeeds
15422 and caches a BASELINK. Then, when called again here,
15423 instead of failing and returning an error_mark_node
15424 returns it (see template/typename17.C in C++11).
15425 ??? Could we diagnose this earlier? */
15426 else if (tag_type == typename_type && BASELINK_P (decl))
15428 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15429 type = error_mark_node;
15431 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15432 in effect, then we must assume that, upon instantiation, the
15433 template will correspond to a class. */
15434 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15435 && tag_type == typename_type)
15436 type = make_typename_type (parser->scope, decl,
15437 typename_type,
15438 /*complain=*/tf_error);
15439 /* If the `typename' keyword is in effect and DECL is not a type
15440 decl, then type is non existent. */
15441 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15443 else if (TREE_CODE (decl) == TYPE_DECL)
15444 type = check_elaborated_type_specifier (tag_type, decl,
15445 /*allow_template_p=*/true);
15446 else if (decl == error_mark_node)
15447 type = error_mark_node;
15450 if (!type)
15452 token = cp_lexer_peek_token (parser->lexer);
15453 identifier = cp_parser_identifier (parser);
15455 if (identifier == error_mark_node)
15457 parser->scope = NULL_TREE;
15458 return error_mark_node;
15461 /* For a `typename', we needn't call xref_tag. */
15462 if (tag_type == typename_type
15463 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15464 return cp_parser_make_typename_type (parser, identifier,
15465 token->location);
15467 /* Template parameter lists apply only if we are not within a
15468 function parameter list. */
15469 bool template_parm_lists_apply
15470 = parser->num_template_parameter_lists;
15471 if (template_parm_lists_apply)
15472 for (cp_binding_level *s = current_binding_level;
15473 s && s->kind != sk_template_parms;
15474 s = s->level_chain)
15475 if (s->kind == sk_function_parms)
15476 template_parm_lists_apply = false;
15478 /* Look up a qualified name in the usual way. */
15479 if (parser->scope)
15481 tree decl;
15482 tree ambiguous_decls;
15484 decl = cp_parser_lookup_name (parser, identifier,
15485 tag_type,
15486 /*is_template=*/false,
15487 /*is_namespace=*/false,
15488 /*check_dependency=*/true,
15489 &ambiguous_decls,
15490 token->location);
15492 /* If the lookup was ambiguous, an error will already have been
15493 issued. */
15494 if (ambiguous_decls)
15495 return error_mark_node;
15497 /* If we are parsing friend declaration, DECL may be a
15498 TEMPLATE_DECL tree node here. However, we need to check
15499 whether this TEMPLATE_DECL results in valid code. Consider
15500 the following example:
15502 namespace N {
15503 template <class T> class C {};
15505 class X {
15506 template <class T> friend class N::C; // #1, valid code
15508 template <class T> class Y {
15509 friend class N::C; // #2, invalid code
15512 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15513 name lookup of `N::C'. We see that friend declaration must
15514 be template for the code to be valid. Note that
15515 processing_template_decl does not work here since it is
15516 always 1 for the above two cases. */
15518 decl = (cp_parser_maybe_treat_template_as_class
15519 (decl, /*tag_name_p=*/is_friend
15520 && template_parm_lists_apply));
15522 if (TREE_CODE (decl) != TYPE_DECL)
15524 cp_parser_diagnose_invalid_type_name (parser,
15525 identifier,
15526 token->location);
15527 return error_mark_node;
15530 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15532 bool allow_template = (template_parm_lists_apply
15533 || DECL_SELF_REFERENCE_P (decl));
15534 type = check_elaborated_type_specifier (tag_type, decl,
15535 allow_template);
15537 if (type == error_mark_node)
15538 return error_mark_node;
15541 /* Forward declarations of nested types, such as
15543 class C1::C2;
15544 class C1::C2::C3;
15546 are invalid unless all components preceding the final '::'
15547 are complete. If all enclosing types are complete, these
15548 declarations become merely pointless.
15550 Invalid forward declarations of nested types are errors
15551 caught elsewhere in parsing. Those that are pointless arrive
15552 here. */
15554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15555 && !is_friend && !processing_explicit_instantiation)
15556 warning (0, "declaration %qD does not declare anything", decl);
15558 type = TREE_TYPE (decl);
15560 else
15562 /* An elaborated-type-specifier sometimes introduces a new type and
15563 sometimes names an existing type. Normally, the rule is that it
15564 introduces a new type only if there is not an existing type of
15565 the same name already in scope. For example, given:
15567 struct S {};
15568 void f() { struct S s; }
15570 the `struct S' in the body of `f' is the same `struct S' as in
15571 the global scope; the existing definition is used. However, if
15572 there were no global declaration, this would introduce a new
15573 local class named `S'.
15575 An exception to this rule applies to the following code:
15577 namespace N { struct S; }
15579 Here, the elaborated-type-specifier names a new type
15580 unconditionally; even if there is already an `S' in the
15581 containing scope this declaration names a new type.
15582 This exception only applies if the elaborated-type-specifier
15583 forms the complete declaration:
15585 [class.name]
15587 A declaration consisting solely of `class-key identifier ;' is
15588 either a redeclaration of the name in the current scope or a
15589 forward declaration of the identifier as a class name. It
15590 introduces the name into the current scope.
15592 We are in this situation precisely when the next token is a `;'.
15594 An exception to the exception is that a `friend' declaration does
15595 *not* name a new type; i.e., given:
15597 struct S { friend struct T; };
15599 `T' is not a new type in the scope of `S'.
15601 Also, `new struct S' or `sizeof (struct S)' never results in the
15602 definition of a new type; a new type can only be declared in a
15603 declaration context. */
15605 tag_scope ts;
15606 bool template_p;
15608 if (is_friend)
15609 /* Friends have special name lookup rules. */
15610 ts = ts_within_enclosing_non_class;
15611 else if (is_declaration
15612 && cp_lexer_next_token_is (parser->lexer,
15613 CPP_SEMICOLON))
15614 /* This is a `class-key identifier ;' */
15615 ts = ts_current;
15616 else
15617 ts = ts_global;
15619 template_p =
15620 (template_parm_lists_apply
15621 && (cp_parser_next_token_starts_class_definition_p (parser)
15622 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15623 /* An unqualified name was used to reference this type, so
15624 there were no qualifying templates. */
15625 if (template_parm_lists_apply
15626 && !cp_parser_check_template_parameters (parser,
15627 /*num_templates=*/0,
15628 token->location,
15629 /*declarator=*/NULL))
15630 return error_mark_node;
15631 type = xref_tag (tag_type, identifier, ts, template_p);
15635 if (type == error_mark_node)
15636 return error_mark_node;
15638 /* Allow attributes on forward declarations of classes. */
15639 if (attributes)
15641 if (TREE_CODE (type) == TYPENAME_TYPE)
15642 warning (OPT_Wattributes,
15643 "attributes ignored on uninstantiated type");
15644 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15645 && ! processing_explicit_instantiation)
15646 warning (OPT_Wattributes,
15647 "attributes ignored on template instantiation");
15648 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15649 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15650 else
15651 warning (OPT_Wattributes,
15652 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15655 if (tag_type != enum_type)
15657 /* Indicate whether this class was declared as a `class' or as a
15658 `struct'. */
15659 if (TREE_CODE (type) == RECORD_TYPE)
15660 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15661 cp_parser_check_class_key (tag_type, type);
15664 /* A "<" cannot follow an elaborated type specifier. If that
15665 happens, the user was probably trying to form a template-id. */
15666 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15667 token->location);
15669 return type;
15672 /* Parse an enum-specifier.
15674 enum-specifier:
15675 enum-head { enumerator-list [opt] }
15676 enum-head { enumerator-list , } [C++0x]
15678 enum-head:
15679 enum-key identifier [opt] enum-base [opt]
15680 enum-key nested-name-specifier identifier enum-base [opt]
15682 enum-key:
15683 enum
15684 enum class [C++0x]
15685 enum struct [C++0x]
15687 enum-base: [C++0x]
15688 : type-specifier-seq
15690 opaque-enum-specifier:
15691 enum-key identifier enum-base [opt] ;
15693 GNU Extensions:
15694 enum-key attributes[opt] identifier [opt] enum-base [opt]
15695 { enumerator-list [opt] }attributes[opt]
15696 enum-key attributes[opt] identifier [opt] enum-base [opt]
15697 { enumerator-list, }attributes[opt] [C++0x]
15699 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15700 if the token stream isn't an enum-specifier after all. */
15702 static tree
15703 cp_parser_enum_specifier (cp_parser* parser)
15705 tree identifier;
15706 tree type = NULL_TREE;
15707 tree prev_scope;
15708 tree nested_name_specifier = NULL_TREE;
15709 tree attributes;
15710 bool scoped_enum_p = false;
15711 bool has_underlying_type = false;
15712 bool nested_being_defined = false;
15713 bool new_value_list = false;
15714 bool is_new_type = false;
15715 bool is_anonymous = false;
15716 tree underlying_type = NULL_TREE;
15717 cp_token *type_start_token = NULL;
15718 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15720 parser->colon_corrects_to_scope_p = false;
15722 /* Parse tentatively so that we can back up if we don't find a
15723 enum-specifier. */
15724 cp_parser_parse_tentatively (parser);
15726 /* Caller guarantees that the current token is 'enum', an identifier
15727 possibly follows, and the token after that is an opening brace.
15728 If we don't have an identifier, fabricate an anonymous name for
15729 the enumeration being defined. */
15730 cp_lexer_consume_token (parser->lexer);
15732 /* Parse the "class" or "struct", which indicates a scoped
15733 enumeration type in C++0x. */
15734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15735 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15737 if (cxx_dialect < cxx11)
15738 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15740 /* Consume the `struct' or `class' token. */
15741 cp_lexer_consume_token (parser->lexer);
15743 scoped_enum_p = true;
15746 attributes = cp_parser_attributes_opt (parser);
15748 /* Clear the qualification. */
15749 parser->scope = NULL_TREE;
15750 parser->qualifying_scope = NULL_TREE;
15751 parser->object_scope = NULL_TREE;
15753 /* Figure out in what scope the declaration is being placed. */
15754 prev_scope = current_scope ();
15756 type_start_token = cp_lexer_peek_token (parser->lexer);
15758 push_deferring_access_checks (dk_no_check);
15759 nested_name_specifier
15760 = cp_parser_nested_name_specifier_opt (parser,
15761 /*typename_keyword_p=*/true,
15762 /*check_dependency_p=*/false,
15763 /*type_p=*/false,
15764 /*is_declaration=*/false);
15766 if (nested_name_specifier)
15768 tree name;
15770 identifier = cp_parser_identifier (parser);
15771 name = cp_parser_lookup_name (parser, identifier,
15772 enum_type,
15773 /*is_template=*/false,
15774 /*is_namespace=*/false,
15775 /*check_dependency=*/true,
15776 /*ambiguous_decls=*/NULL,
15777 input_location);
15778 if (name && name != error_mark_node)
15780 type = TREE_TYPE (name);
15781 if (TREE_CODE (type) == TYPENAME_TYPE)
15783 /* Are template enums allowed in ISO? */
15784 if (template_parm_scope_p ())
15785 pedwarn (type_start_token->location, OPT_Wpedantic,
15786 "%qD is an enumeration template", name);
15787 /* ignore a typename reference, for it will be solved by name
15788 in start_enum. */
15789 type = NULL_TREE;
15792 else if (nested_name_specifier == error_mark_node)
15793 /* We already issued an error. */;
15794 else
15795 error_at (type_start_token->location,
15796 "%qD is not an enumerator-name", identifier);
15798 else
15800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15801 identifier = cp_parser_identifier (parser);
15802 else
15804 identifier = make_anon_name ();
15805 is_anonymous = true;
15806 if (scoped_enum_p)
15807 error_at (type_start_token->location,
15808 "anonymous scoped enum is not allowed");
15811 pop_deferring_access_checks ();
15813 /* Check for the `:' that denotes a specified underlying type in C++0x.
15814 Note that a ':' could also indicate a bitfield width, however. */
15815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15817 cp_decl_specifier_seq type_specifiers;
15819 /* Consume the `:'. */
15820 cp_lexer_consume_token (parser->lexer);
15822 /* Parse the type-specifier-seq. */
15823 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15824 /*is_trailing_return=*/false,
15825 &type_specifiers);
15827 /* At this point this is surely not elaborated type specifier. */
15828 if (!cp_parser_parse_definitely (parser))
15829 return NULL_TREE;
15831 if (cxx_dialect < cxx11)
15832 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15834 has_underlying_type = true;
15836 /* If that didn't work, stop. */
15837 if (type_specifiers.type != error_mark_node)
15839 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15840 /*initialized=*/0, NULL);
15841 if (underlying_type == error_mark_node
15842 || check_for_bare_parameter_packs (underlying_type))
15843 underlying_type = NULL_TREE;
15847 /* Look for the `{' but don't consume it yet. */
15848 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15850 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15852 cp_parser_error (parser, "expected %<{%>");
15853 if (has_underlying_type)
15855 type = NULL_TREE;
15856 goto out;
15859 /* An opaque-enum-specifier must have a ';' here. */
15860 if ((scoped_enum_p || underlying_type)
15861 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15863 cp_parser_error (parser, "expected %<;%> or %<{%>");
15864 if (has_underlying_type)
15866 type = NULL_TREE;
15867 goto out;
15872 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15873 return NULL_TREE;
15875 if (nested_name_specifier)
15877 if (CLASS_TYPE_P (nested_name_specifier))
15879 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15880 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15881 push_scope (nested_name_specifier);
15883 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15885 push_nested_namespace (nested_name_specifier);
15889 /* Issue an error message if type-definitions are forbidden here. */
15890 if (!cp_parser_check_type_definition (parser))
15891 type = error_mark_node;
15892 else
15893 /* Create the new type. We do this before consuming the opening
15894 brace so the enum will be recorded as being on the line of its
15895 tag (or the 'enum' keyword, if there is no tag). */
15896 type = start_enum (identifier, type, underlying_type,
15897 scoped_enum_p, &is_new_type);
15899 /* If the next token is not '{' it is an opaque-enum-specifier or an
15900 elaborated-type-specifier. */
15901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15903 timevar_push (TV_PARSE_ENUM);
15904 if (nested_name_specifier
15905 && nested_name_specifier != error_mark_node)
15907 /* The following catches invalid code such as:
15908 enum class S<int>::E { A, B, C }; */
15909 if (!processing_specialization
15910 && CLASS_TYPE_P (nested_name_specifier)
15911 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15912 error_at (type_start_token->location, "cannot add an enumerator "
15913 "list to a template instantiation");
15915 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15917 error_at (type_start_token->location,
15918 "%<%T::%E%> has not been declared",
15919 TYPE_CONTEXT (nested_name_specifier),
15920 nested_name_specifier);
15921 type = error_mark_node;
15923 /* If that scope does not contain the scope in which the
15924 class was originally declared, the program is invalid. */
15925 else if (prev_scope && !is_ancestor (prev_scope,
15926 nested_name_specifier))
15928 if (at_namespace_scope_p ())
15929 error_at (type_start_token->location,
15930 "declaration of %qD in namespace %qD which does not "
15931 "enclose %qD",
15932 type, prev_scope, nested_name_specifier);
15933 else
15934 error_at (type_start_token->location,
15935 "declaration of %qD in %qD which does not "
15936 "enclose %qD",
15937 type, prev_scope, nested_name_specifier);
15938 type = error_mark_node;
15942 if (scoped_enum_p)
15943 begin_scope (sk_scoped_enum, type);
15945 /* Consume the opening brace. */
15946 cp_lexer_consume_token (parser->lexer);
15948 if (type == error_mark_node)
15949 ; /* Nothing to add */
15950 else if (OPAQUE_ENUM_P (type)
15951 || (cxx_dialect > cxx98 && processing_specialization))
15953 new_value_list = true;
15954 SET_OPAQUE_ENUM_P (type, false);
15955 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15957 else
15959 error_at (type_start_token->location,
15960 "multiple definition of %q#T", type);
15961 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15962 "previous definition here");
15963 type = error_mark_node;
15966 if (type == error_mark_node)
15967 cp_parser_skip_to_end_of_block_or_statement (parser);
15968 /* If the next token is not '}', then there are some enumerators. */
15969 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15971 if (is_anonymous && !scoped_enum_p)
15972 pedwarn (type_start_token->location, OPT_Wpedantic,
15973 "ISO C++ forbids empty anonymous enum");
15975 else
15976 cp_parser_enumerator_list (parser, type);
15978 /* Consume the final '}'. */
15979 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15981 if (scoped_enum_p)
15982 finish_scope ();
15983 timevar_pop (TV_PARSE_ENUM);
15985 else
15987 /* If a ';' follows, then it is an opaque-enum-specifier
15988 and additional restrictions apply. */
15989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15991 if (is_anonymous)
15992 error_at (type_start_token->location,
15993 "opaque-enum-specifier without name");
15994 else if (nested_name_specifier)
15995 error_at (type_start_token->location,
15996 "opaque-enum-specifier must use a simple identifier");
16000 /* Look for trailing attributes to apply to this enumeration, and
16001 apply them if appropriate. */
16002 if (cp_parser_allow_gnu_extensions_p (parser))
16004 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16005 trailing_attr = chainon (trailing_attr, attributes);
16006 cplus_decl_attributes (&type,
16007 trailing_attr,
16008 (int) ATTR_FLAG_TYPE_IN_PLACE);
16011 /* Finish up the enumeration. */
16012 if (type != error_mark_node)
16014 if (new_value_list)
16015 finish_enum_value_list (type);
16016 if (is_new_type)
16017 finish_enum (type);
16020 if (nested_name_specifier)
16022 if (CLASS_TYPE_P (nested_name_specifier))
16024 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16025 pop_scope (nested_name_specifier);
16027 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16029 pop_nested_namespace (nested_name_specifier);
16032 out:
16033 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16034 return type;
16037 /* Parse an enumerator-list. The enumerators all have the indicated
16038 TYPE.
16040 enumerator-list:
16041 enumerator-definition
16042 enumerator-list , enumerator-definition */
16044 static void
16045 cp_parser_enumerator_list (cp_parser* parser, tree type)
16047 while (true)
16049 /* Parse an enumerator-definition. */
16050 cp_parser_enumerator_definition (parser, type);
16052 /* If the next token is not a ',', we've reached the end of
16053 the list. */
16054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16055 break;
16056 /* Otherwise, consume the `,' and keep going. */
16057 cp_lexer_consume_token (parser->lexer);
16058 /* If the next token is a `}', there is a trailing comma. */
16059 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16061 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16062 pedwarn (input_location, OPT_Wpedantic,
16063 "comma at end of enumerator list");
16064 break;
16069 /* Parse an enumerator-definition. The enumerator has the indicated
16070 TYPE.
16072 enumerator-definition:
16073 enumerator
16074 enumerator = constant-expression
16076 enumerator:
16077 identifier */
16079 static void
16080 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16082 tree identifier;
16083 tree value;
16084 location_t loc;
16086 /* Save the input location because we are interested in the location
16087 of the identifier and not the location of the explicit value. */
16088 loc = cp_lexer_peek_token (parser->lexer)->location;
16090 /* Look for the identifier. */
16091 identifier = cp_parser_identifier (parser);
16092 if (identifier == error_mark_node)
16093 return;
16095 /* If the next token is an '=', then there is an explicit value. */
16096 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16098 /* Consume the `=' token. */
16099 cp_lexer_consume_token (parser->lexer);
16100 /* Parse the value. */
16101 value = cp_parser_constant_expression (parser);
16102 value = maybe_constant_value (value);
16104 else
16105 value = NULL_TREE;
16107 /* If we are processing a template, make sure the initializer of the
16108 enumerator doesn't contain any bare template parameter pack. */
16109 if (check_for_bare_parameter_packs (value))
16110 value = error_mark_node;
16112 /* Create the enumerator. */
16113 build_enumerator (identifier, value, type, loc);
16116 /* Parse a namespace-name.
16118 namespace-name:
16119 original-namespace-name
16120 namespace-alias
16122 Returns the NAMESPACE_DECL for the namespace. */
16124 static tree
16125 cp_parser_namespace_name (cp_parser* parser)
16127 tree identifier;
16128 tree namespace_decl;
16130 cp_token *token = cp_lexer_peek_token (parser->lexer);
16132 /* Get the name of the namespace. */
16133 identifier = cp_parser_identifier (parser);
16134 if (identifier == error_mark_node)
16135 return error_mark_node;
16137 /* Look up the identifier in the currently active scope. Look only
16138 for namespaces, due to:
16140 [basic.lookup.udir]
16142 When looking up a namespace-name in a using-directive or alias
16143 definition, only namespace names are considered.
16145 And:
16147 [basic.lookup.qual]
16149 During the lookup of a name preceding the :: scope resolution
16150 operator, object, function, and enumerator names are ignored.
16152 (Note that cp_parser_qualifying_entity only calls this
16153 function if the token after the name is the scope resolution
16154 operator.) */
16155 namespace_decl = cp_parser_lookup_name (parser, identifier,
16156 none_type,
16157 /*is_template=*/false,
16158 /*is_namespace=*/true,
16159 /*check_dependency=*/true,
16160 /*ambiguous_decls=*/NULL,
16161 token->location);
16162 /* If it's not a namespace, issue an error. */
16163 if (namespace_decl == error_mark_node
16164 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16166 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16167 error_at (token->location, "%qD is not a namespace-name", identifier);
16168 cp_parser_error (parser, "expected namespace-name");
16169 namespace_decl = error_mark_node;
16172 return namespace_decl;
16175 /* Parse a namespace-definition.
16177 namespace-definition:
16178 named-namespace-definition
16179 unnamed-namespace-definition
16181 named-namespace-definition:
16182 original-namespace-definition
16183 extension-namespace-definition
16185 original-namespace-definition:
16186 namespace identifier { namespace-body }
16188 extension-namespace-definition:
16189 namespace original-namespace-name { namespace-body }
16191 unnamed-namespace-definition:
16192 namespace { namespace-body } */
16194 static void
16195 cp_parser_namespace_definition (cp_parser* parser)
16197 tree identifier, attribs;
16198 bool has_visibility;
16199 bool is_inline;
16201 cp_ensure_no_omp_declare_simd (parser);
16202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16204 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16205 is_inline = true;
16206 cp_lexer_consume_token (parser->lexer);
16208 else
16209 is_inline = false;
16211 /* Look for the `namespace' keyword. */
16212 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16214 /* Get the name of the namespace. We do not attempt to distinguish
16215 between an original-namespace-definition and an
16216 extension-namespace-definition at this point. The semantic
16217 analysis routines are responsible for that. */
16218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16219 identifier = cp_parser_identifier (parser);
16220 else
16221 identifier = NULL_TREE;
16223 /* Parse any specified attributes. */
16224 attribs = cp_parser_attributes_opt (parser);
16226 /* Look for the `{' to start the namespace. */
16227 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16228 /* Start the namespace. */
16229 push_namespace (identifier);
16231 /* "inline namespace" is equivalent to a stub namespace definition
16232 followed by a strong using directive. */
16233 if (is_inline)
16235 tree name_space = current_namespace;
16236 /* Set up namespace association. */
16237 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16238 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16239 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16240 /* Import the contents of the inline namespace. */
16241 pop_namespace ();
16242 do_using_directive (name_space);
16243 push_namespace (identifier);
16246 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16248 /* Parse the body of the namespace. */
16249 cp_parser_namespace_body (parser);
16251 if (has_visibility)
16252 pop_visibility (1);
16254 /* Finish the namespace. */
16255 pop_namespace ();
16256 /* Look for the final `}'. */
16257 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16260 /* Parse a namespace-body.
16262 namespace-body:
16263 declaration-seq [opt] */
16265 static void
16266 cp_parser_namespace_body (cp_parser* parser)
16268 cp_parser_declaration_seq_opt (parser);
16271 /* Parse a namespace-alias-definition.
16273 namespace-alias-definition:
16274 namespace identifier = qualified-namespace-specifier ; */
16276 static void
16277 cp_parser_namespace_alias_definition (cp_parser* parser)
16279 tree identifier;
16280 tree namespace_specifier;
16282 cp_token *token = cp_lexer_peek_token (parser->lexer);
16284 /* Look for the `namespace' keyword. */
16285 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16286 /* Look for the identifier. */
16287 identifier = cp_parser_identifier (parser);
16288 if (identifier == error_mark_node)
16289 return;
16290 /* Look for the `=' token. */
16291 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16292 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16294 error_at (token->location, "%<namespace%> definition is not allowed here");
16295 /* Skip the definition. */
16296 cp_lexer_consume_token (parser->lexer);
16297 if (cp_parser_skip_to_closing_brace (parser))
16298 cp_lexer_consume_token (parser->lexer);
16299 return;
16301 cp_parser_require (parser, CPP_EQ, RT_EQ);
16302 /* Look for the qualified-namespace-specifier. */
16303 namespace_specifier
16304 = cp_parser_qualified_namespace_specifier (parser);
16305 /* Look for the `;' token. */
16306 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16308 /* Register the alias in the symbol table. */
16309 do_namespace_alias (identifier, namespace_specifier);
16312 /* Parse a qualified-namespace-specifier.
16314 qualified-namespace-specifier:
16315 :: [opt] nested-name-specifier [opt] namespace-name
16317 Returns a NAMESPACE_DECL corresponding to the specified
16318 namespace. */
16320 static tree
16321 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16323 /* Look for the optional `::'. */
16324 cp_parser_global_scope_opt (parser,
16325 /*current_scope_valid_p=*/false);
16327 /* Look for the optional nested-name-specifier. */
16328 cp_parser_nested_name_specifier_opt (parser,
16329 /*typename_keyword_p=*/false,
16330 /*check_dependency_p=*/true,
16331 /*type_p=*/false,
16332 /*is_declaration=*/true);
16334 return cp_parser_namespace_name (parser);
16337 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16338 access declaration.
16340 using-declaration:
16341 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16342 using :: unqualified-id ;
16344 access-declaration:
16345 qualified-id ;
16349 static bool
16350 cp_parser_using_declaration (cp_parser* parser,
16351 bool access_declaration_p)
16353 cp_token *token;
16354 bool typename_p = false;
16355 bool global_scope_p;
16356 tree decl;
16357 tree identifier;
16358 tree qscope;
16359 int oldcount = errorcount;
16360 cp_token *diag_token = NULL;
16362 if (access_declaration_p)
16364 diag_token = cp_lexer_peek_token (parser->lexer);
16365 cp_parser_parse_tentatively (parser);
16367 else
16369 /* Look for the `using' keyword. */
16370 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16372 /* Peek at the next token. */
16373 token = cp_lexer_peek_token (parser->lexer);
16374 /* See if it's `typename'. */
16375 if (token->keyword == RID_TYPENAME)
16377 /* Remember that we've seen it. */
16378 typename_p = true;
16379 /* Consume the `typename' token. */
16380 cp_lexer_consume_token (parser->lexer);
16384 /* Look for the optional global scope qualification. */
16385 global_scope_p
16386 = (cp_parser_global_scope_opt (parser,
16387 /*current_scope_valid_p=*/false)
16388 != NULL_TREE);
16390 /* If we saw `typename', or didn't see `::', then there must be a
16391 nested-name-specifier present. */
16392 if (typename_p || !global_scope_p)
16394 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16395 /*check_dependency_p=*/true,
16396 /*type_p=*/false,
16397 /*is_declaration=*/true);
16398 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16400 cp_parser_skip_to_end_of_block_or_statement (parser);
16401 return false;
16404 /* Otherwise, we could be in either of the two productions. In that
16405 case, treat the nested-name-specifier as optional. */
16406 else
16407 qscope = cp_parser_nested_name_specifier_opt (parser,
16408 /*typename_keyword_p=*/false,
16409 /*check_dependency_p=*/true,
16410 /*type_p=*/false,
16411 /*is_declaration=*/true);
16412 if (!qscope)
16413 qscope = global_namespace;
16414 else if (UNSCOPED_ENUM_P (qscope))
16415 qscope = CP_TYPE_CONTEXT (qscope);
16417 if (access_declaration_p && cp_parser_error_occurred (parser))
16418 /* Something has already gone wrong; there's no need to parse
16419 further. Since an error has occurred, the return value of
16420 cp_parser_parse_definitely will be false, as required. */
16421 return cp_parser_parse_definitely (parser);
16423 token = cp_lexer_peek_token (parser->lexer);
16424 /* Parse the unqualified-id. */
16425 identifier = cp_parser_unqualified_id (parser,
16426 /*template_keyword_p=*/false,
16427 /*check_dependency_p=*/true,
16428 /*declarator_p=*/true,
16429 /*optional_p=*/false);
16431 if (access_declaration_p)
16433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16434 cp_parser_simulate_error (parser);
16435 if (!cp_parser_parse_definitely (parser))
16436 return false;
16439 /* The function we call to handle a using-declaration is different
16440 depending on what scope we are in. */
16441 if (qscope == error_mark_node || identifier == error_mark_node)
16443 else if (!identifier_p (identifier)
16444 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16445 /* [namespace.udecl]
16447 A using declaration shall not name a template-id. */
16448 error_at (token->location,
16449 "a template-id may not appear in a using-declaration");
16450 else
16452 if (at_class_scope_p ())
16454 /* Create the USING_DECL. */
16455 decl = do_class_using_decl (parser->scope, identifier);
16457 if (decl && typename_p)
16458 USING_DECL_TYPENAME_P (decl) = 1;
16460 if (check_for_bare_parameter_packs (decl))
16462 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16463 return false;
16465 else
16466 /* Add it to the list of members in this class. */
16467 finish_member_declaration (decl);
16469 else
16471 decl = cp_parser_lookup_name_simple (parser,
16472 identifier,
16473 token->location);
16474 if (decl == error_mark_node)
16475 cp_parser_name_lookup_error (parser, identifier,
16476 decl, NLE_NULL,
16477 token->location);
16478 else if (check_for_bare_parameter_packs (decl))
16480 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16481 return false;
16483 else if (!at_namespace_scope_p ())
16484 do_local_using_decl (decl, qscope, identifier);
16485 else
16486 do_toplevel_using_decl (decl, qscope, identifier);
16490 /* Look for the final `;'. */
16491 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16493 if (access_declaration_p && errorcount == oldcount)
16494 warning_at (diag_token->location, OPT_Wdeprecated,
16495 "access declarations are deprecated "
16496 "in favour of using-declarations; "
16497 "suggestion: add the %<using%> keyword");
16499 return true;
16502 /* Parse an alias-declaration.
16504 alias-declaration:
16505 using identifier attribute-specifier-seq [opt] = type-id */
16507 static tree
16508 cp_parser_alias_declaration (cp_parser* parser)
16510 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16511 location_t id_location;
16512 cp_declarator *declarator;
16513 cp_decl_specifier_seq decl_specs;
16514 bool member_p;
16515 const char *saved_message = NULL;
16517 /* Look for the `using' keyword. */
16518 cp_token *using_token
16519 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16520 if (using_token == NULL)
16521 return error_mark_node;
16523 id_location = cp_lexer_peek_token (parser->lexer)->location;
16524 id = cp_parser_identifier (parser);
16525 if (id == error_mark_node)
16526 return error_mark_node;
16528 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16529 attributes = cp_parser_attributes_opt (parser);
16530 if (attributes == error_mark_node)
16531 return error_mark_node;
16533 cp_parser_require (parser, CPP_EQ, RT_EQ);
16535 if (cp_parser_error_occurred (parser))
16536 return error_mark_node;
16538 cp_parser_commit_to_tentative_parse (parser);
16540 /* Now we are going to parse the type-id of the declaration. */
16543 [dcl.type]/3 says:
16545 "A type-specifier-seq shall not define a class or enumeration
16546 unless it appears in the type-id of an alias-declaration (7.1.3) that
16547 is not the declaration of a template-declaration."
16549 In other words, if we currently are in an alias template, the
16550 type-id should not define a type.
16552 So let's set parser->type_definition_forbidden_message in that
16553 case; cp_parser_check_type_definition (called by
16554 cp_parser_class_specifier) will then emit an error if a type is
16555 defined in the type-id. */
16556 if (parser->num_template_parameter_lists)
16558 saved_message = parser->type_definition_forbidden_message;
16559 parser->type_definition_forbidden_message =
16560 G_("types may not be defined in alias template declarations");
16563 type = cp_parser_type_id (parser);
16565 /* Restore the error message if need be. */
16566 if (parser->num_template_parameter_lists)
16567 parser->type_definition_forbidden_message = saved_message;
16569 if (type == error_mark_node
16570 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16572 cp_parser_skip_to_end_of_block_or_statement (parser);
16573 return error_mark_node;
16576 /* A typedef-name can also be introduced by an alias-declaration. The
16577 identifier following the using keyword becomes a typedef-name. It has
16578 the same semantics as if it were introduced by the typedef
16579 specifier. In particular, it does not define a new type and it shall
16580 not appear in the type-id. */
16582 clear_decl_specs (&decl_specs);
16583 decl_specs.type = type;
16584 if (attributes != NULL_TREE)
16586 decl_specs.attributes = attributes;
16587 set_and_check_decl_spec_loc (&decl_specs,
16588 ds_attribute,
16589 attrs_token);
16591 set_and_check_decl_spec_loc (&decl_specs,
16592 ds_typedef,
16593 using_token);
16594 set_and_check_decl_spec_loc (&decl_specs,
16595 ds_alias,
16596 using_token);
16598 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16599 declarator->id_loc = id_location;
16601 member_p = at_class_scope_p ();
16602 if (member_p)
16603 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16604 NULL_TREE, attributes);
16605 else
16606 decl = start_decl (declarator, &decl_specs, 0,
16607 attributes, NULL_TREE, &pushed_scope);
16608 if (decl == error_mark_node)
16609 return decl;
16611 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16613 if (pushed_scope)
16614 pop_scope (pushed_scope);
16616 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16617 added into the symbol table; otherwise, return the TYPE_DECL. */
16618 if (DECL_LANG_SPECIFIC (decl)
16619 && DECL_TEMPLATE_INFO (decl)
16620 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16622 decl = DECL_TI_TEMPLATE (decl);
16623 if (member_p)
16624 check_member_template (decl);
16627 return decl;
16630 /* Parse a using-directive.
16632 using-directive:
16633 using namespace :: [opt] nested-name-specifier [opt]
16634 namespace-name ; */
16636 static void
16637 cp_parser_using_directive (cp_parser* parser)
16639 tree namespace_decl;
16640 tree attribs;
16642 /* Look for the `using' keyword. */
16643 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16644 /* And the `namespace' keyword. */
16645 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16646 /* Look for the optional `::' operator. */
16647 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16648 /* And the optional nested-name-specifier. */
16649 cp_parser_nested_name_specifier_opt (parser,
16650 /*typename_keyword_p=*/false,
16651 /*check_dependency_p=*/true,
16652 /*type_p=*/false,
16653 /*is_declaration=*/true);
16654 /* Get the namespace being used. */
16655 namespace_decl = cp_parser_namespace_name (parser);
16656 /* And any specified attributes. */
16657 attribs = cp_parser_attributes_opt (parser);
16658 /* Update the symbol table. */
16659 parse_using_directive (namespace_decl, attribs);
16660 /* Look for the final `;'. */
16661 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16664 /* Parse an asm-definition.
16666 asm-definition:
16667 asm ( string-literal ) ;
16669 GNU Extension:
16671 asm-definition:
16672 asm volatile [opt] ( string-literal ) ;
16673 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16674 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16675 : asm-operand-list [opt] ) ;
16676 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16677 : asm-operand-list [opt]
16678 : asm-clobber-list [opt] ) ;
16679 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16680 : asm-clobber-list [opt]
16681 : asm-goto-list ) ; */
16683 static void
16684 cp_parser_asm_definition (cp_parser* parser)
16686 tree string;
16687 tree outputs = NULL_TREE;
16688 tree inputs = NULL_TREE;
16689 tree clobbers = NULL_TREE;
16690 tree labels = NULL_TREE;
16691 tree asm_stmt;
16692 bool volatile_p = false;
16693 bool extended_p = false;
16694 bool invalid_inputs_p = false;
16695 bool invalid_outputs_p = false;
16696 bool goto_p = false;
16697 required_token missing = RT_NONE;
16699 /* Look for the `asm' keyword. */
16700 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16702 if (parser->in_function_body
16703 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16705 error ("%<asm%> in %<constexpr%> function");
16706 cp_function_chain->invalid_constexpr = true;
16709 /* See if the next token is `volatile'. */
16710 if (cp_parser_allow_gnu_extensions_p (parser)
16711 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16713 /* Remember that we saw the `volatile' keyword. */
16714 volatile_p = true;
16715 /* Consume the token. */
16716 cp_lexer_consume_token (parser->lexer);
16718 if (cp_parser_allow_gnu_extensions_p (parser)
16719 && parser->in_function_body
16720 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16722 /* Remember that we saw the `goto' keyword. */
16723 goto_p = true;
16724 /* Consume the token. */
16725 cp_lexer_consume_token (parser->lexer);
16727 /* Look for the opening `('. */
16728 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16729 return;
16730 /* Look for the string. */
16731 string = cp_parser_string_literal (parser, false, false);
16732 if (string == error_mark_node)
16734 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16735 /*consume_paren=*/true);
16736 return;
16739 /* If we're allowing GNU extensions, check for the extended assembly
16740 syntax. Unfortunately, the `:' tokens need not be separated by
16741 a space in C, and so, for compatibility, we tolerate that here
16742 too. Doing that means that we have to treat the `::' operator as
16743 two `:' tokens. */
16744 if (cp_parser_allow_gnu_extensions_p (parser)
16745 && parser->in_function_body
16746 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16747 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16749 bool inputs_p = false;
16750 bool clobbers_p = false;
16751 bool labels_p = false;
16753 /* The extended syntax was used. */
16754 extended_p = true;
16756 /* Look for outputs. */
16757 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16759 /* Consume the `:'. */
16760 cp_lexer_consume_token (parser->lexer);
16761 /* Parse the output-operands. */
16762 if (cp_lexer_next_token_is_not (parser->lexer,
16763 CPP_COLON)
16764 && cp_lexer_next_token_is_not (parser->lexer,
16765 CPP_SCOPE)
16766 && cp_lexer_next_token_is_not (parser->lexer,
16767 CPP_CLOSE_PAREN)
16768 && !goto_p)
16770 outputs = cp_parser_asm_operand_list (parser);
16771 if (outputs == error_mark_node)
16772 invalid_outputs_p = true;
16775 /* If the next token is `::', there are no outputs, and the
16776 next token is the beginning of the inputs. */
16777 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16778 /* The inputs are coming next. */
16779 inputs_p = true;
16781 /* Look for inputs. */
16782 if (inputs_p
16783 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16785 /* Consume the `:' or `::'. */
16786 cp_lexer_consume_token (parser->lexer);
16787 /* Parse the output-operands. */
16788 if (cp_lexer_next_token_is_not (parser->lexer,
16789 CPP_COLON)
16790 && cp_lexer_next_token_is_not (parser->lexer,
16791 CPP_SCOPE)
16792 && cp_lexer_next_token_is_not (parser->lexer,
16793 CPP_CLOSE_PAREN))
16795 inputs = cp_parser_asm_operand_list (parser);
16796 if (inputs == error_mark_node)
16797 invalid_inputs_p = true;
16800 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16801 /* The clobbers are coming next. */
16802 clobbers_p = true;
16804 /* Look for clobbers. */
16805 if (clobbers_p
16806 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16808 clobbers_p = true;
16809 /* Consume the `:' or `::'. */
16810 cp_lexer_consume_token (parser->lexer);
16811 /* Parse the clobbers. */
16812 if (cp_lexer_next_token_is_not (parser->lexer,
16813 CPP_COLON)
16814 && cp_lexer_next_token_is_not (parser->lexer,
16815 CPP_CLOSE_PAREN))
16816 clobbers = cp_parser_asm_clobber_list (parser);
16818 else if (goto_p
16819 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16820 /* The labels are coming next. */
16821 labels_p = true;
16823 /* Look for labels. */
16824 if (labels_p
16825 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16827 labels_p = true;
16828 /* Consume the `:' or `::'. */
16829 cp_lexer_consume_token (parser->lexer);
16830 /* Parse the labels. */
16831 labels = cp_parser_asm_label_list (parser);
16834 if (goto_p && !labels_p)
16835 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16837 else if (goto_p)
16838 missing = RT_COLON_SCOPE;
16840 /* Look for the closing `)'. */
16841 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16842 missing ? missing : RT_CLOSE_PAREN))
16843 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16844 /*consume_paren=*/true);
16845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16847 if (!invalid_inputs_p && !invalid_outputs_p)
16849 /* Create the ASM_EXPR. */
16850 if (parser->in_function_body)
16852 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16853 inputs, clobbers, labels);
16854 /* If the extended syntax was not used, mark the ASM_EXPR. */
16855 if (!extended_p)
16857 tree temp = asm_stmt;
16858 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16859 temp = TREE_OPERAND (temp, 0);
16861 ASM_INPUT_P (temp) = 1;
16864 else
16865 symtab->finalize_toplevel_asm (string);
16869 /* Declarators [gram.dcl.decl] */
16871 /* Parse an init-declarator.
16873 init-declarator:
16874 declarator initializer [opt]
16876 GNU Extension:
16878 init-declarator:
16879 declarator asm-specification [opt] attributes [opt] initializer [opt]
16881 function-definition:
16882 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16883 function-body
16884 decl-specifier-seq [opt] declarator function-try-block
16886 GNU Extension:
16888 function-definition:
16889 __extension__ function-definition
16891 TM Extension:
16893 function-definition:
16894 decl-specifier-seq [opt] declarator function-transaction-block
16896 The DECL_SPECIFIERS apply to this declarator. Returns a
16897 representation of the entity declared. If MEMBER_P is TRUE, then
16898 this declarator appears in a class scope. The new DECL created by
16899 this declarator is returned.
16901 The CHECKS are access checks that should be performed once we know
16902 what entity is being declared (and, therefore, what classes have
16903 befriended it).
16905 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16906 for a function-definition here as well. If the declarator is a
16907 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16908 be TRUE upon return. By that point, the function-definition will
16909 have been completely parsed.
16911 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16912 is FALSE.
16914 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16915 parsed declaration if it is an uninitialized single declarator not followed
16916 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16917 if present, will not be consumed. If returned, this declarator will be
16918 created with SD_INITIALIZED but will not call cp_finish_decl.
16920 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16921 and there is an initializer, the pointed location_t is set to the
16922 location of the '=' or `(', or '{' in C++11 token introducing the
16923 initializer. */
16925 static tree
16926 cp_parser_init_declarator (cp_parser* parser,
16927 cp_decl_specifier_seq *decl_specifiers,
16928 vec<deferred_access_check, va_gc> *checks,
16929 bool function_definition_allowed_p,
16930 bool member_p,
16931 int declares_class_or_enum,
16932 bool* function_definition_p,
16933 tree* maybe_range_for_decl,
16934 location_t* init_loc)
16936 cp_token *token = NULL, *asm_spec_start_token = NULL,
16937 *attributes_start_token = NULL;
16938 cp_declarator *declarator;
16939 tree prefix_attributes;
16940 tree attributes = NULL;
16941 tree asm_specification;
16942 tree initializer;
16943 tree decl = NULL_TREE;
16944 tree scope;
16945 int is_initialized;
16946 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16947 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16948 "(...)". */
16949 enum cpp_ttype initialization_kind;
16950 bool is_direct_init = false;
16951 bool is_non_constant_init;
16952 int ctor_dtor_or_conv_p;
16953 bool friend_p = cp_parser_friend_p (decl_specifiers);
16954 tree pushed_scope = NULL_TREE;
16955 bool range_for_decl_p = false;
16956 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16957 location_t tmp_init_loc = UNKNOWN_LOCATION;
16959 /* Gather the attributes that were provided with the
16960 decl-specifiers. */
16961 prefix_attributes = decl_specifiers->attributes;
16963 /* Assume that this is not the declarator for a function
16964 definition. */
16965 if (function_definition_p)
16966 *function_definition_p = false;
16968 /* Default arguments are only permitted for function parameters. */
16969 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16970 parser->default_arg_ok_p = false;
16972 /* Defer access checks while parsing the declarator; we cannot know
16973 what names are accessible until we know what is being
16974 declared. */
16975 resume_deferring_access_checks ();
16977 /* Parse the declarator. */
16978 token = cp_lexer_peek_token (parser->lexer);
16979 declarator
16980 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16981 &ctor_dtor_or_conv_p,
16982 /*parenthesized_p=*/NULL,
16983 member_p, friend_p);
16984 /* Gather up the deferred checks. */
16985 stop_deferring_access_checks ();
16987 parser->default_arg_ok_p = saved_default_arg_ok_p;
16989 /* If the DECLARATOR was erroneous, there's no need to go
16990 further. */
16991 if (declarator == cp_error_declarator)
16992 return error_mark_node;
16994 /* Check that the number of template-parameter-lists is OK. */
16995 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16996 token->location))
16997 return error_mark_node;
16999 if (declares_class_or_enum & 2)
17000 cp_parser_check_for_definition_in_return_type (declarator,
17001 decl_specifiers->type,
17002 decl_specifiers->locations[ds_type_spec]);
17004 /* Figure out what scope the entity declared by the DECLARATOR is
17005 located in. `grokdeclarator' sometimes changes the scope, so
17006 we compute it now. */
17007 scope = get_scope_of_declarator (declarator);
17009 /* Perform any lookups in the declared type which were thought to be
17010 dependent, but are not in the scope of the declarator. */
17011 decl_specifiers->type
17012 = maybe_update_decl_type (decl_specifiers->type, scope);
17014 /* If we're allowing GNU extensions, look for an
17015 asm-specification. */
17016 if (cp_parser_allow_gnu_extensions_p (parser))
17018 /* Look for an asm-specification. */
17019 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17020 asm_specification = cp_parser_asm_specification_opt (parser);
17022 else
17023 asm_specification = NULL_TREE;
17025 /* Look for attributes. */
17026 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17027 attributes = cp_parser_attributes_opt (parser);
17029 /* Peek at the next token. */
17030 token = cp_lexer_peek_token (parser->lexer);
17032 bool bogus_implicit_tmpl = false;
17034 if (function_declarator_p (declarator))
17036 /* Check to see if the token indicates the start of a
17037 function-definition. */
17038 if (cp_parser_token_starts_function_definition_p (token))
17040 if (!function_definition_allowed_p)
17042 /* If a function-definition should not appear here, issue an
17043 error message. */
17044 cp_parser_error (parser,
17045 "a function-definition is not allowed here");
17046 return error_mark_node;
17049 location_t func_brace_location
17050 = cp_lexer_peek_token (parser->lexer)->location;
17052 /* Neither attributes nor an asm-specification are allowed
17053 on a function-definition. */
17054 if (asm_specification)
17055 error_at (asm_spec_start_token->location,
17056 "an asm-specification is not allowed "
17057 "on a function-definition");
17058 if (attributes)
17059 error_at (attributes_start_token->location,
17060 "attributes are not allowed "
17061 "on a function-definition");
17062 /* This is a function-definition. */
17063 *function_definition_p = true;
17065 /* Parse the function definition. */
17066 if (member_p)
17067 decl = cp_parser_save_member_function_body (parser,
17068 decl_specifiers,
17069 declarator,
17070 prefix_attributes);
17071 else
17072 decl =
17073 (cp_parser_function_definition_from_specifiers_and_declarator
17074 (parser, decl_specifiers, prefix_attributes, declarator));
17076 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17078 /* This is where the prologue starts... */
17079 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17080 = func_brace_location;
17083 return decl;
17086 else if (parser->fully_implicit_function_template_p)
17088 /* A non-template declaration involving a function parameter list
17089 containing an implicit template parameter will be made into a
17090 template. If the resulting declaration is not going to be an
17091 actual function then finish the template scope here to prevent it.
17092 An error message will be issued once we have a decl to talk about.
17094 FIXME probably we should do type deduction rather than create an
17095 implicit template, but the standard currently doesn't allow it. */
17096 bogus_implicit_tmpl = true;
17097 finish_fully_implicit_template (parser, NULL_TREE);
17100 /* [dcl.dcl]
17102 Only in function declarations for constructors, destructors, and
17103 type conversions can the decl-specifier-seq be omitted.
17105 We explicitly postpone this check past the point where we handle
17106 function-definitions because we tolerate function-definitions
17107 that are missing their return types in some modes. */
17108 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17110 cp_parser_error (parser,
17111 "expected constructor, destructor, or type conversion");
17112 return error_mark_node;
17115 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17116 if (token->type == CPP_EQ
17117 || token->type == CPP_OPEN_PAREN
17118 || token->type == CPP_OPEN_BRACE)
17120 is_initialized = SD_INITIALIZED;
17121 initialization_kind = token->type;
17122 if (maybe_range_for_decl)
17123 *maybe_range_for_decl = error_mark_node;
17124 tmp_init_loc = token->location;
17125 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17126 *init_loc = tmp_init_loc;
17128 if (token->type == CPP_EQ
17129 && function_declarator_p (declarator))
17131 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17132 if (t2->keyword == RID_DEFAULT)
17133 is_initialized = SD_DEFAULTED;
17134 else if (t2->keyword == RID_DELETE)
17135 is_initialized = SD_DELETED;
17138 else
17140 /* If the init-declarator isn't initialized and isn't followed by a
17141 `,' or `;', it's not a valid init-declarator. */
17142 if (token->type != CPP_COMMA
17143 && token->type != CPP_SEMICOLON)
17145 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17146 range_for_decl_p = true;
17147 else
17149 if (!maybe_range_for_decl)
17150 cp_parser_error (parser, "expected initializer");
17151 return error_mark_node;
17154 is_initialized = SD_UNINITIALIZED;
17155 initialization_kind = CPP_EOF;
17158 /* Because start_decl has side-effects, we should only call it if we
17159 know we're going ahead. By this point, we know that we cannot
17160 possibly be looking at any other construct. */
17161 cp_parser_commit_to_tentative_parse (parser);
17163 /* Enter the newly declared entry in the symbol table. If we're
17164 processing a declaration in a class-specifier, we wait until
17165 after processing the initializer. */
17166 if (!member_p)
17168 if (parser->in_unbraced_linkage_specification_p)
17169 decl_specifiers->storage_class = sc_extern;
17170 decl = start_decl (declarator, decl_specifiers,
17171 range_for_decl_p? SD_INITIALIZED : is_initialized,
17172 attributes, prefix_attributes, &pushed_scope);
17173 cp_finalize_omp_declare_simd (parser, decl);
17174 /* Adjust location of decl if declarator->id_loc is more appropriate:
17175 set, and decl wasn't merged with another decl, in which case its
17176 location would be different from input_location, and more accurate. */
17177 if (DECL_P (decl)
17178 && declarator->id_loc != UNKNOWN_LOCATION
17179 && DECL_SOURCE_LOCATION (decl) == input_location)
17180 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17182 else if (scope)
17183 /* Enter the SCOPE. That way unqualified names appearing in the
17184 initializer will be looked up in SCOPE. */
17185 pushed_scope = push_scope (scope);
17187 /* Perform deferred access control checks, now that we know in which
17188 SCOPE the declared entity resides. */
17189 if (!member_p && decl)
17191 tree saved_current_function_decl = NULL_TREE;
17193 /* If the entity being declared is a function, pretend that we
17194 are in its scope. If it is a `friend', it may have access to
17195 things that would not otherwise be accessible. */
17196 if (TREE_CODE (decl) == FUNCTION_DECL)
17198 saved_current_function_decl = current_function_decl;
17199 current_function_decl = decl;
17202 /* Perform access checks for template parameters. */
17203 cp_parser_perform_template_parameter_access_checks (checks);
17205 /* Perform the access control checks for the declarator and the
17206 decl-specifiers. */
17207 perform_deferred_access_checks (tf_warning_or_error);
17209 /* Restore the saved value. */
17210 if (TREE_CODE (decl) == FUNCTION_DECL)
17211 current_function_decl = saved_current_function_decl;
17214 /* Parse the initializer. */
17215 initializer = NULL_TREE;
17216 is_direct_init = false;
17217 is_non_constant_init = true;
17218 if (is_initialized)
17220 if (function_declarator_p (declarator))
17222 if (initialization_kind == CPP_EQ)
17223 initializer = cp_parser_pure_specifier (parser);
17224 else
17226 /* If the declaration was erroneous, we don't really
17227 know what the user intended, so just silently
17228 consume the initializer. */
17229 if (decl != error_mark_node)
17230 error_at (tmp_init_loc, "initializer provided for function");
17231 cp_parser_skip_to_closing_parenthesis (parser,
17232 /*recovering=*/true,
17233 /*or_comma=*/false,
17234 /*consume_paren=*/true);
17237 else
17239 /* We want to record the extra mangling scope for in-class
17240 initializers of class members and initializers of static data
17241 member templates. The former involves deferring
17242 parsing of the initializer until end of class as with default
17243 arguments. So right here we only handle the latter. */
17244 if (!member_p && processing_template_decl)
17245 start_lambda_scope (decl);
17246 initializer = cp_parser_initializer (parser,
17247 &is_direct_init,
17248 &is_non_constant_init);
17249 if (!member_p && processing_template_decl)
17250 finish_lambda_scope ();
17251 if (initializer == error_mark_node)
17252 cp_parser_skip_to_end_of_statement (parser);
17256 /* The old parser allows attributes to appear after a parenthesized
17257 initializer. Mark Mitchell proposed removing this functionality
17258 on the GCC mailing lists on 2002-08-13. This parser accepts the
17259 attributes -- but ignores them. */
17260 if (cp_parser_allow_gnu_extensions_p (parser)
17261 && initialization_kind == CPP_OPEN_PAREN)
17262 if (cp_parser_attributes_opt (parser))
17263 warning (OPT_Wattributes,
17264 "attributes after parenthesized initializer ignored");
17266 /* And now complain about a non-function implicit template. */
17267 if (bogus_implicit_tmpl)
17268 error_at (DECL_SOURCE_LOCATION (decl),
17269 "non-function %qD declared as implicit template", decl);
17271 /* For an in-class declaration, use `grokfield' to create the
17272 declaration. */
17273 if (member_p)
17275 if (pushed_scope)
17277 pop_scope (pushed_scope);
17278 pushed_scope = NULL_TREE;
17280 decl = grokfield (declarator, decl_specifiers,
17281 initializer, !is_non_constant_init,
17282 /*asmspec=*/NULL_TREE,
17283 chainon (attributes, prefix_attributes));
17284 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17285 cp_parser_save_default_args (parser, decl);
17286 cp_finalize_omp_declare_simd (parser, decl);
17289 /* Finish processing the declaration. But, skip member
17290 declarations. */
17291 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17293 cp_finish_decl (decl,
17294 initializer, !is_non_constant_init,
17295 asm_specification,
17296 /* If the initializer is in parentheses, then this is
17297 a direct-initialization, which means that an
17298 `explicit' constructor is OK. Otherwise, an
17299 `explicit' constructor cannot be used. */
17300 ((is_direct_init || !is_initialized)
17301 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17303 else if ((cxx_dialect != cxx98) && friend_p
17304 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17305 /* Core issue #226 (C++0x only): A default template-argument
17306 shall not be specified in a friend class template
17307 declaration. */
17308 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17309 /*is_partial=*/false, /*is_friend_decl=*/1);
17311 if (!friend_p && pushed_scope)
17312 pop_scope (pushed_scope);
17314 if (function_declarator_p (declarator)
17315 && parser->fully_implicit_function_template_p)
17317 if (member_p)
17318 decl = finish_fully_implicit_template (parser, decl);
17319 else
17320 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17323 return decl;
17326 /* Parse a declarator.
17328 declarator:
17329 direct-declarator
17330 ptr-operator declarator
17332 abstract-declarator:
17333 ptr-operator abstract-declarator [opt]
17334 direct-abstract-declarator
17336 GNU Extensions:
17338 declarator:
17339 attributes [opt] direct-declarator
17340 attributes [opt] ptr-operator declarator
17342 abstract-declarator:
17343 attributes [opt] ptr-operator abstract-declarator [opt]
17344 attributes [opt] direct-abstract-declarator
17346 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17347 detect constructor, destructor or conversion operators. It is set
17348 to -1 if the declarator is a name, and +1 if it is a
17349 function. Otherwise it is set to zero. Usually you just want to
17350 test for >0, but internally the negative value is used.
17352 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17353 a decl-specifier-seq unless it declares a constructor, destructor,
17354 or conversion. It might seem that we could check this condition in
17355 semantic analysis, rather than parsing, but that makes it difficult
17356 to handle something like `f()'. We want to notice that there are
17357 no decl-specifiers, and therefore realize that this is an
17358 expression, not a declaration.)
17360 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17361 the declarator is a direct-declarator of the form "(...)".
17363 MEMBER_P is true iff this declarator is a member-declarator.
17365 FRIEND_P is true iff this declarator is a friend. */
17367 static cp_declarator *
17368 cp_parser_declarator (cp_parser* parser,
17369 cp_parser_declarator_kind dcl_kind,
17370 int* ctor_dtor_or_conv_p,
17371 bool* parenthesized_p,
17372 bool member_p, bool friend_p)
17374 cp_declarator *declarator;
17375 enum tree_code code;
17376 cp_cv_quals cv_quals;
17377 tree class_type;
17378 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17380 /* Assume this is not a constructor, destructor, or type-conversion
17381 operator. */
17382 if (ctor_dtor_or_conv_p)
17383 *ctor_dtor_or_conv_p = 0;
17385 if (cp_parser_allow_gnu_extensions_p (parser))
17386 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17388 /* Check for the ptr-operator production. */
17389 cp_parser_parse_tentatively (parser);
17390 /* Parse the ptr-operator. */
17391 code = cp_parser_ptr_operator (parser,
17392 &class_type,
17393 &cv_quals,
17394 &std_attributes);
17396 /* If that worked, then we have a ptr-operator. */
17397 if (cp_parser_parse_definitely (parser))
17399 /* If a ptr-operator was found, then this declarator was not
17400 parenthesized. */
17401 if (parenthesized_p)
17402 *parenthesized_p = true;
17403 /* The dependent declarator is optional if we are parsing an
17404 abstract-declarator. */
17405 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17406 cp_parser_parse_tentatively (parser);
17408 /* Parse the dependent declarator. */
17409 declarator = cp_parser_declarator (parser, dcl_kind,
17410 /*ctor_dtor_or_conv_p=*/NULL,
17411 /*parenthesized_p=*/NULL,
17412 /*member_p=*/false,
17413 friend_p);
17415 /* If we are parsing an abstract-declarator, we must handle the
17416 case where the dependent declarator is absent. */
17417 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17418 && !cp_parser_parse_definitely (parser))
17419 declarator = NULL;
17421 declarator = cp_parser_make_indirect_declarator
17422 (code, class_type, cv_quals, declarator, std_attributes);
17424 /* Everything else is a direct-declarator. */
17425 else
17427 if (parenthesized_p)
17428 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17429 CPP_OPEN_PAREN);
17430 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17431 ctor_dtor_or_conv_p,
17432 member_p, friend_p);
17435 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17436 declarator->attributes = gnu_attributes;
17437 return declarator;
17440 /* Parse a direct-declarator or direct-abstract-declarator.
17442 direct-declarator:
17443 declarator-id
17444 direct-declarator ( parameter-declaration-clause )
17445 cv-qualifier-seq [opt]
17446 ref-qualifier [opt]
17447 exception-specification [opt]
17448 direct-declarator [ constant-expression [opt] ]
17449 ( declarator )
17451 direct-abstract-declarator:
17452 direct-abstract-declarator [opt]
17453 ( parameter-declaration-clause )
17454 cv-qualifier-seq [opt]
17455 ref-qualifier [opt]
17456 exception-specification [opt]
17457 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17458 ( abstract-declarator )
17460 Returns a representation of the declarator. DCL_KIND is
17461 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17462 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17463 we are parsing a direct-declarator. It is
17464 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17465 of ambiguity we prefer an abstract declarator, as per
17466 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17467 as for cp_parser_declarator. */
17469 static cp_declarator *
17470 cp_parser_direct_declarator (cp_parser* parser,
17471 cp_parser_declarator_kind dcl_kind,
17472 int* ctor_dtor_or_conv_p,
17473 bool member_p, bool friend_p)
17475 cp_token *token;
17476 cp_declarator *declarator = NULL;
17477 tree scope = NULL_TREE;
17478 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17479 bool saved_in_declarator_p = parser->in_declarator_p;
17480 bool first = true;
17481 tree pushed_scope = NULL_TREE;
17483 while (true)
17485 /* Peek at the next token. */
17486 token = cp_lexer_peek_token (parser->lexer);
17487 if (token->type == CPP_OPEN_PAREN)
17489 /* This is either a parameter-declaration-clause, or a
17490 parenthesized declarator. When we know we are parsing a
17491 named declarator, it must be a parenthesized declarator
17492 if FIRST is true. For instance, `(int)' is a
17493 parameter-declaration-clause, with an omitted
17494 direct-abstract-declarator. But `((*))', is a
17495 parenthesized abstract declarator. Finally, when T is a
17496 template parameter `(T)' is a
17497 parameter-declaration-clause, and not a parenthesized
17498 named declarator.
17500 We first try and parse a parameter-declaration-clause,
17501 and then try a nested declarator (if FIRST is true).
17503 It is not an error for it not to be a
17504 parameter-declaration-clause, even when FIRST is
17505 false. Consider,
17507 int i (int);
17508 int i (3);
17510 The first is the declaration of a function while the
17511 second is the definition of a variable, including its
17512 initializer.
17514 Having seen only the parenthesis, we cannot know which of
17515 these two alternatives should be selected. Even more
17516 complex are examples like:
17518 int i (int (a));
17519 int i (int (3));
17521 The former is a function-declaration; the latter is a
17522 variable initialization.
17524 Thus again, we try a parameter-declaration-clause, and if
17525 that fails, we back out and return. */
17527 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17529 tree params;
17530 bool is_declarator = false;
17532 /* In a member-declarator, the only valid interpretation
17533 of a parenthesis is the start of a
17534 parameter-declaration-clause. (It is invalid to
17535 initialize a static data member with a parenthesized
17536 initializer; only the "=" form of initialization is
17537 permitted.) */
17538 if (!member_p)
17539 cp_parser_parse_tentatively (parser);
17541 /* Consume the `('. */
17542 cp_lexer_consume_token (parser->lexer);
17543 if (first)
17545 /* If this is going to be an abstract declarator, we're
17546 in a declarator and we can't have default args. */
17547 parser->default_arg_ok_p = false;
17548 parser->in_declarator_p = true;
17551 begin_scope (sk_function_parms, NULL_TREE);
17553 /* Parse the parameter-declaration-clause. */
17554 params = cp_parser_parameter_declaration_clause (parser);
17556 /* Consume the `)'. */
17557 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17559 /* If all went well, parse the cv-qualifier-seq,
17560 ref-qualifier and the exception-specification. */
17561 if (member_p || cp_parser_parse_definitely (parser))
17563 cp_cv_quals cv_quals;
17564 cp_virt_specifiers virt_specifiers;
17565 cp_ref_qualifier ref_qual;
17566 tree exception_specification;
17567 tree late_return;
17568 tree attrs;
17569 bool memfn = (member_p || (pushed_scope
17570 && CLASS_TYPE_P (pushed_scope)));
17572 is_declarator = true;
17574 if (ctor_dtor_or_conv_p)
17575 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17576 first = false;
17578 /* Parse the cv-qualifier-seq. */
17579 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17580 /* Parse the ref-qualifier. */
17581 ref_qual = cp_parser_ref_qualifier_opt (parser);
17582 /* And the exception-specification. */
17583 exception_specification
17584 = cp_parser_exception_specification_opt (parser);
17586 attrs = cp_parser_std_attribute_spec_seq (parser);
17588 /* In here, we handle cases where attribute is used after
17589 the function declaration. For example:
17590 void func (int x) __attribute__((vector(..))); */
17591 if (flag_cilkplus
17592 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17594 cp_parser_parse_tentatively (parser);
17595 tree attr = cp_parser_gnu_attributes_opt (parser);
17596 if (cp_lexer_next_token_is_not (parser->lexer,
17597 CPP_SEMICOLON)
17598 && cp_lexer_next_token_is_not (parser->lexer,
17599 CPP_OPEN_BRACE))
17600 cp_parser_abort_tentative_parse (parser);
17601 else if (!cp_parser_parse_definitely (parser))
17603 else
17604 attrs = chainon (attr, attrs);
17606 late_return = (cp_parser_late_return_type_opt
17607 (parser, declarator,
17608 memfn ? cv_quals : -1));
17611 /* Parse the virt-specifier-seq. */
17612 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17614 /* Create the function-declarator. */
17615 declarator = make_call_declarator (declarator,
17616 params,
17617 cv_quals,
17618 virt_specifiers,
17619 ref_qual,
17620 exception_specification,
17621 late_return);
17622 declarator->std_attributes = attrs;
17623 /* Any subsequent parameter lists are to do with
17624 return type, so are not those of the declared
17625 function. */
17626 parser->default_arg_ok_p = false;
17629 /* Remove the function parms from scope. */
17630 pop_bindings_and_leave_scope ();
17632 if (is_declarator)
17633 /* Repeat the main loop. */
17634 continue;
17637 /* If this is the first, we can try a parenthesized
17638 declarator. */
17639 if (first)
17641 bool saved_in_type_id_in_expr_p;
17643 parser->default_arg_ok_p = saved_default_arg_ok_p;
17644 parser->in_declarator_p = saved_in_declarator_p;
17646 /* Consume the `('. */
17647 cp_lexer_consume_token (parser->lexer);
17648 /* Parse the nested declarator. */
17649 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17650 parser->in_type_id_in_expr_p = true;
17651 declarator
17652 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17653 /*parenthesized_p=*/NULL,
17654 member_p, friend_p);
17655 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17656 first = false;
17657 /* Expect a `)'. */
17658 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17659 declarator = cp_error_declarator;
17660 if (declarator == cp_error_declarator)
17661 break;
17663 goto handle_declarator;
17665 /* Otherwise, we must be done. */
17666 else
17667 break;
17669 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17670 && token->type == CPP_OPEN_SQUARE
17671 && !cp_next_tokens_can_be_attribute_p (parser))
17673 /* Parse an array-declarator. */
17674 tree bounds, attrs;
17676 if (ctor_dtor_or_conv_p)
17677 *ctor_dtor_or_conv_p = 0;
17679 first = false;
17680 parser->default_arg_ok_p = false;
17681 parser->in_declarator_p = true;
17682 /* Consume the `['. */
17683 cp_lexer_consume_token (parser->lexer);
17684 /* Peek at the next token. */
17685 token = cp_lexer_peek_token (parser->lexer);
17686 /* If the next token is `]', then there is no
17687 constant-expression. */
17688 if (token->type != CPP_CLOSE_SQUARE)
17690 bool non_constant_p = false;
17692 bounds
17693 = cp_parser_constant_expression (parser,
17694 /*allow_non_constant=*/true,
17695 &non_constant_p);
17696 if (!non_constant_p)
17697 /* OK */;
17698 else if (error_operand_p (bounds))
17699 /* Already gave an error. */;
17700 else if (!parser->in_function_body
17701 || current_binding_level->kind == sk_function_parms)
17703 /* Normally, the array bound must be an integral constant
17704 expression. However, as an extension, we allow VLAs
17705 in function scopes as long as they aren't part of a
17706 parameter declaration. */
17707 cp_parser_error (parser,
17708 "array bound is not an integer constant");
17709 bounds = error_mark_node;
17711 else if (processing_template_decl
17712 && !type_dependent_expression_p (bounds))
17714 /* Remember this wasn't a constant-expression. */
17715 bounds = build_nop (TREE_TYPE (bounds), bounds);
17716 TREE_SIDE_EFFECTS (bounds) = 1;
17719 else
17720 bounds = NULL_TREE;
17721 /* Look for the closing `]'. */
17722 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17724 declarator = cp_error_declarator;
17725 break;
17728 attrs = cp_parser_std_attribute_spec_seq (parser);
17729 declarator = make_array_declarator (declarator, bounds);
17730 declarator->std_attributes = attrs;
17732 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17735 tree qualifying_scope;
17736 tree unqualified_name;
17737 tree attrs;
17738 special_function_kind sfk;
17739 bool abstract_ok;
17740 bool pack_expansion_p = false;
17741 cp_token *declarator_id_start_token;
17743 /* Parse a declarator-id */
17744 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17745 if (abstract_ok)
17747 cp_parser_parse_tentatively (parser);
17749 /* If we see an ellipsis, we should be looking at a
17750 parameter pack. */
17751 if (token->type == CPP_ELLIPSIS)
17753 /* Consume the `...' */
17754 cp_lexer_consume_token (parser->lexer);
17756 pack_expansion_p = true;
17760 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17761 unqualified_name
17762 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17763 qualifying_scope = parser->scope;
17764 if (abstract_ok)
17766 bool okay = false;
17768 if (!unqualified_name && pack_expansion_p)
17770 /* Check whether an error occurred. */
17771 okay = !cp_parser_error_occurred (parser);
17773 /* We already consumed the ellipsis to mark a
17774 parameter pack, but we have no way to report it,
17775 so abort the tentative parse. We will be exiting
17776 immediately anyway. */
17777 cp_parser_abort_tentative_parse (parser);
17779 else
17780 okay = cp_parser_parse_definitely (parser);
17782 if (!okay)
17783 unqualified_name = error_mark_node;
17784 else if (unqualified_name
17785 && (qualifying_scope
17786 || (!identifier_p (unqualified_name))))
17788 cp_parser_error (parser, "expected unqualified-id");
17789 unqualified_name = error_mark_node;
17793 if (!unqualified_name)
17794 return NULL;
17795 if (unqualified_name == error_mark_node)
17797 declarator = cp_error_declarator;
17798 pack_expansion_p = false;
17799 declarator->parameter_pack_p = false;
17800 break;
17803 attrs = cp_parser_std_attribute_spec_seq (parser);
17805 if (qualifying_scope && at_namespace_scope_p ()
17806 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17808 /* In the declaration of a member of a template class
17809 outside of the class itself, the SCOPE will sometimes
17810 be a TYPENAME_TYPE. For example, given:
17812 template <typename T>
17813 int S<T>::R::i = 3;
17815 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17816 this context, we must resolve S<T>::R to an ordinary
17817 type, rather than a typename type.
17819 The reason we normally avoid resolving TYPENAME_TYPEs
17820 is that a specialization of `S' might render
17821 `S<T>::R' not a type. However, if `S' is
17822 specialized, then this `i' will not be used, so there
17823 is no harm in resolving the types here. */
17824 tree type;
17826 /* Resolve the TYPENAME_TYPE. */
17827 type = resolve_typename_type (qualifying_scope,
17828 /*only_current_p=*/false);
17829 /* If that failed, the declarator is invalid. */
17830 if (TREE_CODE (type) == TYPENAME_TYPE)
17832 if (typedef_variant_p (type))
17833 error_at (declarator_id_start_token->location,
17834 "cannot define member of dependent typedef "
17835 "%qT", type);
17836 else
17837 error_at (declarator_id_start_token->location,
17838 "%<%T::%E%> is not a type",
17839 TYPE_CONTEXT (qualifying_scope),
17840 TYPE_IDENTIFIER (qualifying_scope));
17842 qualifying_scope = type;
17845 sfk = sfk_none;
17847 if (unqualified_name)
17849 tree class_type;
17851 if (qualifying_scope
17852 && CLASS_TYPE_P (qualifying_scope))
17853 class_type = qualifying_scope;
17854 else
17855 class_type = current_class_type;
17857 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17859 tree name_type = TREE_TYPE (unqualified_name);
17860 if (class_type && same_type_p (name_type, class_type))
17862 if (qualifying_scope
17863 && CLASSTYPE_USE_TEMPLATE (name_type))
17865 error_at (declarator_id_start_token->location,
17866 "invalid use of constructor as a template");
17867 inform (declarator_id_start_token->location,
17868 "use %<%T::%D%> instead of %<%T::%D%> to "
17869 "name the constructor in a qualified name",
17870 class_type,
17871 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17872 class_type, name_type);
17873 declarator = cp_error_declarator;
17874 break;
17876 else
17877 unqualified_name = constructor_name (class_type);
17879 else
17881 /* We do not attempt to print the declarator
17882 here because we do not have enough
17883 information about its original syntactic
17884 form. */
17885 cp_parser_error (parser, "invalid declarator");
17886 declarator = cp_error_declarator;
17887 break;
17891 if (class_type)
17893 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17894 sfk = sfk_destructor;
17895 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17896 sfk = sfk_conversion;
17897 else if (/* There's no way to declare a constructor
17898 for an anonymous type, even if the type
17899 got a name for linkage purposes. */
17900 !TYPE_WAS_ANONYMOUS (class_type)
17901 /* Handle correctly (c++/19200):
17903 struct S {
17904 struct T{};
17905 friend void S(T);
17908 and also:
17910 namespace N {
17911 void S();
17914 struct S {
17915 friend void N::S();
17916 }; */
17917 && !(friend_p
17918 && class_type != qualifying_scope)
17919 && constructor_name_p (unqualified_name,
17920 class_type))
17922 unqualified_name = constructor_name (class_type);
17923 sfk = sfk_constructor;
17925 else if (is_overloaded_fn (unqualified_name)
17926 && DECL_CONSTRUCTOR_P (get_first_fn
17927 (unqualified_name)))
17928 sfk = sfk_constructor;
17930 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17931 *ctor_dtor_or_conv_p = -1;
17934 declarator = make_id_declarator (qualifying_scope,
17935 unqualified_name,
17936 sfk);
17937 declarator->std_attributes = attrs;
17938 declarator->id_loc = token->location;
17939 declarator->parameter_pack_p = pack_expansion_p;
17941 if (pack_expansion_p)
17942 maybe_warn_variadic_templates ();
17945 handle_declarator:;
17946 scope = get_scope_of_declarator (declarator);
17947 if (scope)
17949 /* Any names that appear after the declarator-id for a
17950 member are looked up in the containing scope. */
17951 if (at_function_scope_p ())
17953 /* But declarations with qualified-ids can't appear in a
17954 function. */
17955 cp_parser_error (parser, "qualified-id in declaration");
17956 declarator = cp_error_declarator;
17957 break;
17959 pushed_scope = push_scope (scope);
17961 parser->in_declarator_p = true;
17962 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17963 || (declarator && declarator->kind == cdk_id))
17964 /* Default args are only allowed on function
17965 declarations. */
17966 parser->default_arg_ok_p = saved_default_arg_ok_p;
17967 else
17968 parser->default_arg_ok_p = false;
17970 first = false;
17972 /* We're done. */
17973 else
17974 break;
17977 /* For an abstract declarator, we might wind up with nothing at this
17978 point. That's an error; the declarator is not optional. */
17979 if (!declarator)
17980 cp_parser_error (parser, "expected declarator");
17982 /* If we entered a scope, we must exit it now. */
17983 if (pushed_scope)
17984 pop_scope (pushed_scope);
17986 parser->default_arg_ok_p = saved_default_arg_ok_p;
17987 parser->in_declarator_p = saved_in_declarator_p;
17989 return declarator;
17992 /* Parse a ptr-operator.
17994 ptr-operator:
17995 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17996 * cv-qualifier-seq [opt]
17998 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17999 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18001 GNU Extension:
18003 ptr-operator:
18004 & cv-qualifier-seq [opt]
18006 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18007 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18008 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18009 filled in with the TYPE containing the member. *CV_QUALS is
18010 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18011 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18012 Note that the tree codes returned by this function have nothing
18013 to do with the types of trees that will be eventually be created
18014 to represent the pointer or reference type being parsed. They are
18015 just constants with suggestive names. */
18016 static enum tree_code
18017 cp_parser_ptr_operator (cp_parser* parser,
18018 tree* type,
18019 cp_cv_quals *cv_quals,
18020 tree *attributes)
18022 enum tree_code code = ERROR_MARK;
18023 cp_token *token;
18024 tree attrs = NULL_TREE;
18026 /* Assume that it's not a pointer-to-member. */
18027 *type = NULL_TREE;
18028 /* And that there are no cv-qualifiers. */
18029 *cv_quals = TYPE_UNQUALIFIED;
18031 /* Peek at the next token. */
18032 token = cp_lexer_peek_token (parser->lexer);
18034 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18035 if (token->type == CPP_MULT)
18036 code = INDIRECT_REF;
18037 else if (token->type == CPP_AND)
18038 code = ADDR_EXPR;
18039 else if ((cxx_dialect != cxx98) &&
18040 token->type == CPP_AND_AND) /* C++0x only */
18041 code = NON_LVALUE_EXPR;
18043 if (code != ERROR_MARK)
18045 /* Consume the `*', `&' or `&&'. */
18046 cp_lexer_consume_token (parser->lexer);
18048 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18049 `&', if we are allowing GNU extensions. (The only qualifier
18050 that can legally appear after `&' is `restrict', but that is
18051 enforced during semantic analysis. */
18052 if (code == INDIRECT_REF
18053 || cp_parser_allow_gnu_extensions_p (parser))
18054 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18056 attrs = cp_parser_std_attribute_spec_seq (parser);
18057 if (attributes != NULL)
18058 *attributes = attrs;
18060 else
18062 /* Try the pointer-to-member case. */
18063 cp_parser_parse_tentatively (parser);
18064 /* Look for the optional `::' operator. */
18065 cp_parser_global_scope_opt (parser,
18066 /*current_scope_valid_p=*/false);
18067 /* Look for the nested-name specifier. */
18068 token = cp_lexer_peek_token (parser->lexer);
18069 cp_parser_nested_name_specifier (parser,
18070 /*typename_keyword_p=*/false,
18071 /*check_dependency_p=*/true,
18072 /*type_p=*/false,
18073 /*is_declaration=*/false);
18074 /* If we found it, and the next token is a `*', then we are
18075 indeed looking at a pointer-to-member operator. */
18076 if (!cp_parser_error_occurred (parser)
18077 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18079 /* Indicate that the `*' operator was used. */
18080 code = INDIRECT_REF;
18082 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18083 error_at (token->location, "%qD is a namespace", parser->scope);
18084 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18085 error_at (token->location, "cannot form pointer to member of "
18086 "non-class %q#T", parser->scope);
18087 else
18089 /* The type of which the member is a member is given by the
18090 current SCOPE. */
18091 *type = parser->scope;
18092 /* The next name will not be qualified. */
18093 parser->scope = NULL_TREE;
18094 parser->qualifying_scope = NULL_TREE;
18095 parser->object_scope = NULL_TREE;
18096 /* Look for optional c++11 attributes. */
18097 attrs = cp_parser_std_attribute_spec_seq (parser);
18098 if (attributes != NULL)
18099 *attributes = attrs;
18100 /* Look for the optional cv-qualifier-seq. */
18101 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18104 /* If that didn't work we don't have a ptr-operator. */
18105 if (!cp_parser_parse_definitely (parser))
18106 cp_parser_error (parser, "expected ptr-operator");
18109 return code;
18112 /* Parse an (optional) cv-qualifier-seq.
18114 cv-qualifier-seq:
18115 cv-qualifier cv-qualifier-seq [opt]
18117 cv-qualifier:
18118 const
18119 volatile
18121 GNU Extension:
18123 cv-qualifier:
18124 __restrict__
18126 Returns a bitmask representing the cv-qualifiers. */
18128 static cp_cv_quals
18129 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18131 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18133 while (true)
18135 cp_token *token;
18136 cp_cv_quals cv_qualifier;
18138 /* Peek at the next token. */
18139 token = cp_lexer_peek_token (parser->lexer);
18140 /* See if it's a cv-qualifier. */
18141 switch (token->keyword)
18143 case RID_CONST:
18144 cv_qualifier = TYPE_QUAL_CONST;
18145 break;
18147 case RID_VOLATILE:
18148 cv_qualifier = TYPE_QUAL_VOLATILE;
18149 break;
18151 case RID_RESTRICT:
18152 cv_qualifier = TYPE_QUAL_RESTRICT;
18153 break;
18155 default:
18156 cv_qualifier = TYPE_UNQUALIFIED;
18157 break;
18160 if (!cv_qualifier)
18161 break;
18163 if (cv_quals & cv_qualifier)
18165 error_at (token->location, "duplicate cv-qualifier");
18166 cp_lexer_purge_token (parser->lexer);
18168 else
18170 cp_lexer_consume_token (parser->lexer);
18171 cv_quals |= cv_qualifier;
18175 return cv_quals;
18178 /* Parse an (optional) ref-qualifier
18180 ref-qualifier:
18184 Returns cp_ref_qualifier representing ref-qualifier. */
18186 static cp_ref_qualifier
18187 cp_parser_ref_qualifier_opt (cp_parser* parser)
18189 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18191 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18192 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18193 return ref_qual;
18195 while (true)
18197 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18198 cp_token *token = cp_lexer_peek_token (parser->lexer);
18200 switch (token->type)
18202 case CPP_AND:
18203 curr_ref_qual = REF_QUAL_LVALUE;
18204 break;
18206 case CPP_AND_AND:
18207 curr_ref_qual = REF_QUAL_RVALUE;
18208 break;
18210 default:
18211 curr_ref_qual = REF_QUAL_NONE;
18212 break;
18215 if (!curr_ref_qual)
18216 break;
18217 else if (ref_qual)
18219 error_at (token->location, "multiple ref-qualifiers");
18220 cp_lexer_purge_token (parser->lexer);
18222 else
18224 ref_qual = curr_ref_qual;
18225 cp_lexer_consume_token (parser->lexer);
18229 return ref_qual;
18232 /* Parse an (optional) virt-specifier-seq.
18234 virt-specifier-seq:
18235 virt-specifier virt-specifier-seq [opt]
18237 virt-specifier:
18238 override
18239 final
18241 Returns a bitmask representing the virt-specifiers. */
18243 static cp_virt_specifiers
18244 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18246 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18248 while (true)
18250 cp_token *token;
18251 cp_virt_specifiers virt_specifier;
18253 /* Peek at the next token. */
18254 token = cp_lexer_peek_token (parser->lexer);
18255 /* See if it's a virt-specifier-qualifier. */
18256 if (token->type != CPP_NAME)
18257 break;
18258 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18260 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18261 virt_specifier = VIRT_SPEC_OVERRIDE;
18263 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18265 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18266 virt_specifier = VIRT_SPEC_FINAL;
18268 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18270 virt_specifier = VIRT_SPEC_FINAL;
18272 else
18273 break;
18275 if (virt_specifiers & virt_specifier)
18277 error_at (token->location, "duplicate virt-specifier");
18278 cp_lexer_purge_token (parser->lexer);
18280 else
18282 cp_lexer_consume_token (parser->lexer);
18283 virt_specifiers |= virt_specifier;
18286 return virt_specifiers;
18289 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18290 is in scope even though it isn't real. */
18292 void
18293 inject_this_parameter (tree ctype, cp_cv_quals quals)
18295 tree this_parm;
18297 if (current_class_ptr)
18299 /* We don't clear this between NSDMIs. Is it already what we want? */
18300 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18301 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18302 && cp_type_quals (type) == quals)
18303 return;
18306 this_parm = build_this_parm (ctype, quals);
18307 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18308 current_class_ptr = NULL_TREE;
18309 current_class_ref
18310 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18311 current_class_ptr = this_parm;
18314 /* Return true iff our current scope is a non-static data member
18315 initializer. */
18317 bool
18318 parsing_nsdmi (void)
18320 /* We recognize NSDMI context by the context-less 'this' pointer set up
18321 by the function above. */
18322 if (current_class_ptr
18323 && TREE_CODE (current_class_ptr) == PARM_DECL
18324 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18325 return true;
18326 return false;
18329 /* Parse a late-specified return type, if any. This is not a separate
18330 non-terminal, but part of a function declarator, which looks like
18332 -> trailing-type-specifier-seq abstract-declarator(opt)
18334 Returns the type indicated by the type-id.
18336 In addition to this this parses any queued up omp declare simd
18337 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18339 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18340 function. */
18342 static tree
18343 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18344 cp_cv_quals quals)
18346 cp_token *token;
18347 tree type = NULL_TREE;
18348 bool declare_simd_p = (parser->omp_declare_simd
18349 && declarator
18350 && declarator->kind == cdk_id);
18352 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18353 && declarator && declarator->kind == cdk_id);
18355 /* Peek at the next token. */
18356 token = cp_lexer_peek_token (parser->lexer);
18357 /* A late-specified return type is indicated by an initial '->'. */
18358 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18359 return NULL_TREE;
18361 tree save_ccp = current_class_ptr;
18362 tree save_ccr = current_class_ref;
18363 if (quals >= 0)
18365 /* DR 1207: 'this' is in scope in the trailing return type. */
18366 inject_this_parameter (current_class_type, quals);
18369 if (token->type == CPP_DEREF)
18371 /* Consume the ->. */
18372 cp_lexer_consume_token (parser->lexer);
18374 type = cp_parser_trailing_type_id (parser);
18377 if (cilk_simd_fn_vector_p)
18378 declarator->std_attributes
18379 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18380 declarator->std_attributes);
18381 if (declare_simd_p)
18382 declarator->std_attributes
18383 = cp_parser_late_parsing_omp_declare_simd (parser,
18384 declarator->std_attributes);
18386 if (quals >= 0)
18388 current_class_ptr = save_ccp;
18389 current_class_ref = save_ccr;
18392 return type;
18395 /* Parse a declarator-id.
18397 declarator-id:
18398 id-expression
18399 :: [opt] nested-name-specifier [opt] type-name
18401 In the `id-expression' case, the value returned is as for
18402 cp_parser_id_expression if the id-expression was an unqualified-id.
18403 If the id-expression was a qualified-id, then a SCOPE_REF is
18404 returned. The first operand is the scope (either a NAMESPACE_DECL
18405 or TREE_TYPE), but the second is still just a representation of an
18406 unqualified-id. */
18408 static tree
18409 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18411 tree id;
18412 /* The expression must be an id-expression. Assume that qualified
18413 names are the names of types so that:
18415 template <class T>
18416 int S<T>::R::i = 3;
18418 will work; we must treat `S<T>::R' as the name of a type.
18419 Similarly, assume that qualified names are templates, where
18420 required, so that:
18422 template <class T>
18423 int S<T>::R<T>::i = 3;
18425 will work, too. */
18426 id = cp_parser_id_expression (parser,
18427 /*template_keyword_p=*/false,
18428 /*check_dependency_p=*/false,
18429 /*template_p=*/NULL,
18430 /*declarator_p=*/true,
18431 optional_p);
18432 if (id && BASELINK_P (id))
18433 id = BASELINK_FUNCTIONS (id);
18434 return id;
18437 /* Parse a type-id.
18439 type-id:
18440 type-specifier-seq abstract-declarator [opt]
18442 Returns the TYPE specified. */
18444 static tree
18445 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18446 bool is_trailing_return)
18448 cp_decl_specifier_seq type_specifier_seq;
18449 cp_declarator *abstract_declarator;
18451 /* Parse the type-specifier-seq. */
18452 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18453 is_trailing_return,
18454 &type_specifier_seq);
18455 if (type_specifier_seq.type == error_mark_node)
18456 return error_mark_node;
18458 /* There might or might not be an abstract declarator. */
18459 cp_parser_parse_tentatively (parser);
18460 /* Look for the declarator. */
18461 abstract_declarator
18462 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18463 /*parenthesized_p=*/NULL,
18464 /*member_p=*/false,
18465 /*friend_p=*/false);
18466 /* Check to see if there really was a declarator. */
18467 if (!cp_parser_parse_definitely (parser))
18468 abstract_declarator = NULL;
18470 if (type_specifier_seq.type
18471 /* None of the valid uses of 'auto' in C++14 involve the type-id
18472 nonterminal, but it is valid in a trailing-return-type. */
18473 && !(cxx_dialect >= cxx14 && is_trailing_return)
18474 && type_uses_auto (type_specifier_seq.type))
18476 /* A type-id with type 'auto' is only ok if the abstract declarator
18477 is a function declarator with a late-specified return type. */
18478 if (abstract_declarator
18479 && abstract_declarator->kind == cdk_function
18480 && abstract_declarator->u.function.late_return_type)
18481 /* OK */;
18482 else
18484 error ("invalid use of %<auto%>");
18485 return error_mark_node;
18489 return groktypename (&type_specifier_seq, abstract_declarator,
18490 is_template_arg);
18493 static tree cp_parser_type_id (cp_parser *parser)
18495 return cp_parser_type_id_1 (parser, false, false);
18498 static tree cp_parser_template_type_arg (cp_parser *parser)
18500 tree r;
18501 const char *saved_message = parser->type_definition_forbidden_message;
18502 parser->type_definition_forbidden_message
18503 = G_("types may not be defined in template arguments");
18504 r = cp_parser_type_id_1 (parser, true, false);
18505 parser->type_definition_forbidden_message = saved_message;
18506 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18508 error ("invalid use of %<auto%> in template argument");
18509 r = error_mark_node;
18511 return r;
18514 static tree cp_parser_trailing_type_id (cp_parser *parser)
18516 return cp_parser_type_id_1 (parser, false, true);
18519 /* Parse a type-specifier-seq.
18521 type-specifier-seq:
18522 type-specifier type-specifier-seq [opt]
18524 GNU extension:
18526 type-specifier-seq:
18527 attributes type-specifier-seq [opt]
18529 If IS_DECLARATION is true, we are at the start of a "condition" or
18530 exception-declaration, so we might be followed by a declarator-id.
18532 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18533 i.e. we've just seen "->".
18535 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18537 static void
18538 cp_parser_type_specifier_seq (cp_parser* parser,
18539 bool is_declaration,
18540 bool is_trailing_return,
18541 cp_decl_specifier_seq *type_specifier_seq)
18543 bool seen_type_specifier = false;
18544 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18545 cp_token *start_token = NULL;
18547 /* Clear the TYPE_SPECIFIER_SEQ. */
18548 clear_decl_specs (type_specifier_seq);
18550 /* In the context of a trailing return type, enum E { } is an
18551 elaborated-type-specifier followed by a function-body, not an
18552 enum-specifier. */
18553 if (is_trailing_return)
18554 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18556 /* Parse the type-specifiers and attributes. */
18557 while (true)
18559 tree type_specifier;
18560 bool is_cv_qualifier;
18562 /* Check for attributes first. */
18563 if (cp_next_tokens_can_be_attribute_p (parser))
18565 type_specifier_seq->attributes =
18566 chainon (type_specifier_seq->attributes,
18567 cp_parser_attributes_opt (parser));
18568 continue;
18571 /* record the token of the beginning of the type specifier seq,
18572 for error reporting purposes*/
18573 if (!start_token)
18574 start_token = cp_lexer_peek_token (parser->lexer);
18576 /* Look for the type-specifier. */
18577 type_specifier = cp_parser_type_specifier (parser,
18578 flags,
18579 type_specifier_seq,
18580 /*is_declaration=*/false,
18581 NULL,
18582 &is_cv_qualifier);
18583 if (!type_specifier)
18585 /* If the first type-specifier could not be found, this is not a
18586 type-specifier-seq at all. */
18587 if (!seen_type_specifier)
18589 /* Set in_declarator_p to avoid skipping to the semicolon. */
18590 int in_decl = parser->in_declarator_p;
18591 parser->in_declarator_p = true;
18593 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18594 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18595 cp_parser_error (parser, "expected type-specifier");
18597 parser->in_declarator_p = in_decl;
18599 type_specifier_seq->type = error_mark_node;
18600 return;
18602 /* If subsequent type-specifiers could not be found, the
18603 type-specifier-seq is complete. */
18604 break;
18607 seen_type_specifier = true;
18608 /* The standard says that a condition can be:
18610 type-specifier-seq declarator = assignment-expression
18612 However, given:
18614 struct S {};
18615 if (int S = ...)
18617 we should treat the "S" as a declarator, not as a
18618 type-specifier. The standard doesn't say that explicitly for
18619 type-specifier-seq, but it does say that for
18620 decl-specifier-seq in an ordinary declaration. Perhaps it
18621 would be clearer just to allow a decl-specifier-seq here, and
18622 then add a semantic restriction that if any decl-specifiers
18623 that are not type-specifiers appear, the program is invalid. */
18624 if (is_declaration && !is_cv_qualifier)
18625 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18629 /* Return whether the function currently being declared has an associated
18630 template parameter list. */
18632 static bool
18633 function_being_declared_is_template_p (cp_parser* parser)
18635 if (!current_template_parms || processing_template_parmlist)
18636 return false;
18638 if (parser->implicit_template_scope)
18639 return true;
18641 if (at_class_scope_p ()
18642 && TYPE_BEING_DEFINED (current_class_type))
18643 return parser->num_template_parameter_lists != 0;
18645 return ((int) parser->num_template_parameter_lists > template_class_depth
18646 (current_class_type));
18649 /* Parse a parameter-declaration-clause.
18651 parameter-declaration-clause:
18652 parameter-declaration-list [opt] ... [opt]
18653 parameter-declaration-list , ...
18655 Returns a representation for the parameter declarations. A return
18656 value of NULL indicates a parameter-declaration-clause consisting
18657 only of an ellipsis. */
18659 static tree
18660 cp_parser_parameter_declaration_clause (cp_parser* parser)
18662 tree parameters;
18663 cp_token *token;
18664 bool ellipsis_p;
18665 bool is_error;
18667 struct cleanup {
18668 cp_parser* parser;
18669 int auto_is_implicit_function_template_parm_p;
18670 ~cleanup() {
18671 parser->auto_is_implicit_function_template_parm_p
18672 = auto_is_implicit_function_template_parm_p;
18674 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18676 (void) cleanup;
18678 if (!processing_specialization
18679 && !processing_template_parmlist
18680 && !processing_explicit_instantiation)
18681 if (!current_function_decl
18682 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18683 parser->auto_is_implicit_function_template_parm_p = true;
18685 /* Peek at the next token. */
18686 token = cp_lexer_peek_token (parser->lexer);
18687 /* Check for trivial parameter-declaration-clauses. */
18688 if (token->type == CPP_ELLIPSIS)
18690 /* Consume the `...' token. */
18691 cp_lexer_consume_token (parser->lexer);
18692 return NULL_TREE;
18694 else if (token->type == CPP_CLOSE_PAREN)
18695 /* There are no parameters. */
18697 #ifndef NO_IMPLICIT_EXTERN_C
18698 if (in_system_header_at (input_location)
18699 && current_class_type == NULL
18700 && current_lang_name == lang_name_c)
18701 return NULL_TREE;
18702 else
18703 #endif
18704 return void_list_node;
18706 /* Check for `(void)', too, which is a special case. */
18707 else if (token->keyword == RID_VOID
18708 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18709 == CPP_CLOSE_PAREN))
18711 /* Consume the `void' token. */
18712 cp_lexer_consume_token (parser->lexer);
18713 /* There are no parameters. */
18714 return void_list_node;
18717 /* Parse the parameter-declaration-list. */
18718 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18719 /* If a parse error occurred while parsing the
18720 parameter-declaration-list, then the entire
18721 parameter-declaration-clause is erroneous. */
18722 if (is_error)
18723 return NULL;
18725 /* Peek at the next token. */
18726 token = cp_lexer_peek_token (parser->lexer);
18727 /* If it's a `,', the clause should terminate with an ellipsis. */
18728 if (token->type == CPP_COMMA)
18730 /* Consume the `,'. */
18731 cp_lexer_consume_token (parser->lexer);
18732 /* Expect an ellipsis. */
18733 ellipsis_p
18734 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18736 /* It might also be `...' if the optional trailing `,' was
18737 omitted. */
18738 else if (token->type == CPP_ELLIPSIS)
18740 /* Consume the `...' token. */
18741 cp_lexer_consume_token (parser->lexer);
18742 /* And remember that we saw it. */
18743 ellipsis_p = true;
18745 else
18746 ellipsis_p = false;
18748 /* Finish the parameter list. */
18749 if (!ellipsis_p)
18750 parameters = chainon (parameters, void_list_node);
18752 return parameters;
18755 /* Parse a parameter-declaration-list.
18757 parameter-declaration-list:
18758 parameter-declaration
18759 parameter-declaration-list , parameter-declaration
18761 Returns a representation of the parameter-declaration-list, as for
18762 cp_parser_parameter_declaration_clause. However, the
18763 `void_list_node' is never appended to the list. Upon return,
18764 *IS_ERROR will be true iff an error occurred. */
18766 static tree
18767 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18769 tree parameters = NULL_TREE;
18770 tree *tail = &parameters;
18771 bool saved_in_unbraced_linkage_specification_p;
18772 int index = 0;
18774 /* Assume all will go well. */
18775 *is_error = false;
18776 /* The special considerations that apply to a function within an
18777 unbraced linkage specifications do not apply to the parameters
18778 to the function. */
18779 saved_in_unbraced_linkage_specification_p
18780 = parser->in_unbraced_linkage_specification_p;
18781 parser->in_unbraced_linkage_specification_p = false;
18783 /* Look for more parameters. */
18784 while (true)
18786 cp_parameter_declarator *parameter;
18787 tree decl = error_mark_node;
18788 bool parenthesized_p = false;
18789 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18790 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18791 (current_template_parms)) : 0);
18793 /* Parse the parameter. */
18794 parameter
18795 = cp_parser_parameter_declaration (parser,
18796 /*template_parm_p=*/false,
18797 &parenthesized_p);
18799 /* We don't know yet if the enclosing context is deprecated, so wait
18800 and warn in grokparms if appropriate. */
18801 deprecated_state = DEPRECATED_SUPPRESS;
18803 if (parameter)
18805 /* If a function parameter pack was specified and an implicit template
18806 parameter was introduced during cp_parser_parameter_declaration,
18807 change any implicit parameters introduced into packs. */
18808 if (parser->implicit_template_parms
18809 && parameter->declarator
18810 && parameter->declarator->parameter_pack_p)
18812 int latest_template_parm_idx = TREE_VEC_LENGTH
18813 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18815 if (latest_template_parm_idx != template_parm_idx)
18816 parameter->decl_specifiers.type = convert_generic_types_to_packs
18817 (parameter->decl_specifiers.type,
18818 template_parm_idx, latest_template_parm_idx);
18821 decl = grokdeclarator (parameter->declarator,
18822 &parameter->decl_specifiers,
18823 PARM,
18824 parameter->default_argument != NULL_TREE,
18825 &parameter->decl_specifiers.attributes);
18828 deprecated_state = DEPRECATED_NORMAL;
18830 /* If a parse error occurred parsing the parameter declaration,
18831 then the entire parameter-declaration-list is erroneous. */
18832 if (decl == error_mark_node)
18834 *is_error = true;
18835 parameters = error_mark_node;
18836 break;
18839 if (parameter->decl_specifiers.attributes)
18840 cplus_decl_attributes (&decl,
18841 parameter->decl_specifiers.attributes,
18843 if (DECL_NAME (decl))
18844 decl = pushdecl (decl);
18846 if (decl != error_mark_node)
18848 retrofit_lang_decl (decl);
18849 DECL_PARM_INDEX (decl) = ++index;
18850 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18853 /* Add the new parameter to the list. */
18854 *tail = build_tree_list (parameter->default_argument, decl);
18855 tail = &TREE_CHAIN (*tail);
18857 /* Peek at the next token. */
18858 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18859 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18860 /* These are for Objective-C++ */
18861 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18862 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18863 /* The parameter-declaration-list is complete. */
18864 break;
18865 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18867 cp_token *token;
18869 /* Peek at the next token. */
18870 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18871 /* If it's an ellipsis, then the list is complete. */
18872 if (token->type == CPP_ELLIPSIS)
18873 break;
18874 /* Otherwise, there must be more parameters. Consume the
18875 `,'. */
18876 cp_lexer_consume_token (parser->lexer);
18877 /* When parsing something like:
18879 int i(float f, double d)
18881 we can tell after seeing the declaration for "f" that we
18882 are not looking at an initialization of a variable "i",
18883 but rather at the declaration of a function "i".
18885 Due to the fact that the parsing of template arguments
18886 (as specified to a template-id) requires backtracking we
18887 cannot use this technique when inside a template argument
18888 list. */
18889 if (!parser->in_template_argument_list_p
18890 && !parser->in_type_id_in_expr_p
18891 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18892 /* However, a parameter-declaration of the form
18893 "float(f)" (which is a valid declaration of a
18894 parameter "f") can also be interpreted as an
18895 expression (the conversion of "f" to "float"). */
18896 && !parenthesized_p)
18897 cp_parser_commit_to_tentative_parse (parser);
18899 else
18901 cp_parser_error (parser, "expected %<,%> or %<...%>");
18902 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18903 cp_parser_skip_to_closing_parenthesis (parser,
18904 /*recovering=*/true,
18905 /*or_comma=*/false,
18906 /*consume_paren=*/false);
18907 break;
18911 parser->in_unbraced_linkage_specification_p
18912 = saved_in_unbraced_linkage_specification_p;
18914 /* Reset implicit_template_scope if we are about to leave the function
18915 parameter list that introduced it. Note that for out-of-line member
18916 definitions, there will be one or more class scopes before we get to
18917 the template parameter scope. */
18919 if (cp_binding_level *its = parser->implicit_template_scope)
18920 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18922 while (maybe_its->kind == sk_class)
18923 maybe_its = maybe_its->level_chain;
18924 if (maybe_its == its)
18926 parser->implicit_template_parms = 0;
18927 parser->implicit_template_scope = 0;
18931 return parameters;
18934 /* Parse a parameter declaration.
18936 parameter-declaration:
18937 decl-specifier-seq ... [opt] declarator
18938 decl-specifier-seq declarator = assignment-expression
18939 decl-specifier-seq ... [opt] abstract-declarator [opt]
18940 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18942 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18943 declares a template parameter. (In that case, a non-nested `>'
18944 token encountered during the parsing of the assignment-expression
18945 is not interpreted as a greater-than operator.)
18947 Returns a representation of the parameter, or NULL if an error
18948 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18949 true iff the declarator is of the form "(p)". */
18951 static cp_parameter_declarator *
18952 cp_parser_parameter_declaration (cp_parser *parser,
18953 bool template_parm_p,
18954 bool *parenthesized_p)
18956 int declares_class_or_enum;
18957 cp_decl_specifier_seq decl_specifiers;
18958 cp_declarator *declarator;
18959 tree default_argument;
18960 cp_token *token = NULL, *declarator_token_start = NULL;
18961 const char *saved_message;
18963 /* In a template parameter, `>' is not an operator.
18965 [temp.param]
18967 When parsing a default template-argument for a non-type
18968 template-parameter, the first non-nested `>' is taken as the end
18969 of the template parameter-list rather than a greater-than
18970 operator. */
18972 /* Type definitions may not appear in parameter types. */
18973 saved_message = parser->type_definition_forbidden_message;
18974 parser->type_definition_forbidden_message
18975 = G_("types may not be defined in parameter types");
18977 /* Parse the declaration-specifiers. */
18978 cp_parser_decl_specifier_seq (parser,
18979 CP_PARSER_FLAGS_NONE,
18980 &decl_specifiers,
18981 &declares_class_or_enum);
18983 /* Complain about missing 'typename' or other invalid type names. */
18984 if (!decl_specifiers.any_type_specifiers_p
18985 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18986 decl_specifiers.type = error_mark_node;
18988 /* If an error occurred, there's no reason to attempt to parse the
18989 rest of the declaration. */
18990 if (cp_parser_error_occurred (parser))
18992 parser->type_definition_forbidden_message = saved_message;
18993 return NULL;
18996 /* Peek at the next token. */
18997 token = cp_lexer_peek_token (parser->lexer);
18999 /* If the next token is a `)', `,', `=', `>', or `...', then there
19000 is no declarator. However, when variadic templates are enabled,
19001 there may be a declarator following `...'. */
19002 if (token->type == CPP_CLOSE_PAREN
19003 || token->type == CPP_COMMA
19004 || token->type == CPP_EQ
19005 || token->type == CPP_GREATER)
19007 declarator = NULL;
19008 if (parenthesized_p)
19009 *parenthesized_p = false;
19011 /* Otherwise, there should be a declarator. */
19012 else
19014 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19015 parser->default_arg_ok_p = false;
19017 /* After seeing a decl-specifier-seq, if the next token is not a
19018 "(", there is no possibility that the code is a valid
19019 expression. Therefore, if parsing tentatively, we commit at
19020 this point. */
19021 if (!parser->in_template_argument_list_p
19022 /* In an expression context, having seen:
19024 (int((char ...
19026 we cannot be sure whether we are looking at a
19027 function-type (taking a "char" as a parameter) or a cast
19028 of some object of type "char" to "int". */
19029 && !parser->in_type_id_in_expr_p
19030 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19031 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19033 cp_parser_commit_to_tentative_parse (parser);
19034 /* Parse the declarator. */
19035 declarator_token_start = token;
19036 declarator = cp_parser_declarator (parser,
19037 CP_PARSER_DECLARATOR_EITHER,
19038 /*ctor_dtor_or_conv_p=*/NULL,
19039 parenthesized_p,
19040 /*member_p=*/false,
19041 /*friend_p=*/false);
19042 parser->default_arg_ok_p = saved_default_arg_ok_p;
19043 /* After the declarator, allow more attributes. */
19044 decl_specifiers.attributes
19045 = chainon (decl_specifiers.attributes,
19046 cp_parser_attributes_opt (parser));
19049 /* If the next token is an ellipsis, and we have not seen a
19050 declarator name, and the type of the declarator contains parameter
19051 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19052 a parameter pack expansion expression. Otherwise, leave the
19053 ellipsis for a C-style variadic function. */
19054 token = cp_lexer_peek_token (parser->lexer);
19055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19057 tree type = decl_specifiers.type;
19059 if (type && DECL_P (type))
19060 type = TREE_TYPE (type);
19062 if (type
19063 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19064 && declarator_can_be_parameter_pack (declarator)
19065 && (!declarator || !declarator->parameter_pack_p)
19066 && uses_parameter_packs (type))
19068 /* Consume the `...'. */
19069 cp_lexer_consume_token (parser->lexer);
19070 maybe_warn_variadic_templates ();
19072 /* Build a pack expansion type */
19073 if (declarator)
19074 declarator->parameter_pack_p = true;
19075 else
19076 decl_specifiers.type = make_pack_expansion (type);
19080 /* The restriction on defining new types applies only to the type
19081 of the parameter, not to the default argument. */
19082 parser->type_definition_forbidden_message = saved_message;
19084 /* If the next token is `=', then process a default argument. */
19085 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19087 token = cp_lexer_peek_token (parser->lexer);
19088 /* If we are defining a class, then the tokens that make up the
19089 default argument must be saved and processed later. */
19090 if (!template_parm_p && at_class_scope_p ()
19091 && TYPE_BEING_DEFINED (current_class_type)
19092 && !LAMBDA_TYPE_P (current_class_type))
19093 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19094 /* Outside of a class definition, we can just parse the
19095 assignment-expression. */
19096 else
19097 default_argument
19098 = cp_parser_default_argument (parser, template_parm_p);
19100 if (!parser->default_arg_ok_p)
19102 if (flag_permissive)
19103 warning (0, "deprecated use of default argument for parameter of non-function");
19104 else
19106 error_at (token->location,
19107 "default arguments are only "
19108 "permitted for function parameters");
19109 default_argument = NULL_TREE;
19112 else if ((declarator && declarator->parameter_pack_p)
19113 || (decl_specifiers.type
19114 && PACK_EXPANSION_P (decl_specifiers.type)))
19116 /* Find the name of the parameter pack. */
19117 cp_declarator *id_declarator = declarator;
19118 while (id_declarator && id_declarator->kind != cdk_id)
19119 id_declarator = id_declarator->declarator;
19121 if (id_declarator && id_declarator->kind == cdk_id)
19122 error_at (declarator_token_start->location,
19123 template_parm_p
19124 ? G_("template parameter pack %qD "
19125 "cannot have a default argument")
19126 : G_("parameter pack %qD cannot have "
19127 "a default argument"),
19128 id_declarator->u.id.unqualified_name);
19129 else
19130 error_at (declarator_token_start->location,
19131 template_parm_p
19132 ? G_("template parameter pack cannot have "
19133 "a default argument")
19134 : G_("parameter pack cannot have a "
19135 "default argument"));
19137 default_argument = NULL_TREE;
19140 else
19141 default_argument = NULL_TREE;
19143 return make_parameter_declarator (&decl_specifiers,
19144 declarator,
19145 default_argument);
19148 /* Parse a default argument and return it.
19150 TEMPLATE_PARM_P is true if this is a default argument for a
19151 non-type template parameter. */
19152 static tree
19153 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19155 tree default_argument = NULL_TREE;
19156 bool saved_greater_than_is_operator_p;
19157 bool saved_local_variables_forbidden_p;
19158 bool non_constant_p, is_direct_init;
19160 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19161 set correctly. */
19162 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19163 parser->greater_than_is_operator_p = !template_parm_p;
19164 /* Local variable names (and the `this' keyword) may not
19165 appear in a default argument. */
19166 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19167 parser->local_variables_forbidden_p = true;
19168 /* Parse the assignment-expression. */
19169 if (template_parm_p)
19170 push_deferring_access_checks (dk_no_deferred);
19171 tree saved_class_ptr = NULL_TREE;
19172 tree saved_class_ref = NULL_TREE;
19173 /* The "this" pointer is not valid in a default argument. */
19174 if (cfun)
19176 saved_class_ptr = current_class_ptr;
19177 cp_function_chain->x_current_class_ptr = NULL_TREE;
19178 saved_class_ref = current_class_ref;
19179 cp_function_chain->x_current_class_ref = NULL_TREE;
19181 default_argument
19182 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19183 /* Restore the "this" pointer. */
19184 if (cfun)
19186 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19187 cp_function_chain->x_current_class_ref = saved_class_ref;
19189 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19190 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19191 if (template_parm_p)
19192 pop_deferring_access_checks ();
19193 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19194 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19196 return default_argument;
19199 /* Parse a function-body.
19201 function-body:
19202 compound_statement */
19204 static void
19205 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19207 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19210 /* Parse a ctor-initializer-opt followed by a function-body. Return
19211 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19212 is true we are parsing a function-try-block. */
19214 static bool
19215 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19216 bool in_function_try_block)
19218 tree body, list;
19219 bool ctor_initializer_p;
19220 const bool check_body_p =
19221 DECL_CONSTRUCTOR_P (current_function_decl)
19222 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19223 tree last = NULL;
19225 /* Begin the function body. */
19226 body = begin_function_body ();
19227 /* Parse the optional ctor-initializer. */
19228 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19230 /* If we're parsing a constexpr constructor definition, we need
19231 to check that the constructor body is indeed empty. However,
19232 before we get to cp_parser_function_body lot of junk has been
19233 generated, so we can't just check that we have an empty block.
19234 Rather we take a snapshot of the outermost block, and check whether
19235 cp_parser_function_body changed its state. */
19236 if (check_body_p)
19238 list = cur_stmt_list;
19239 if (STATEMENT_LIST_TAIL (list))
19240 last = STATEMENT_LIST_TAIL (list)->stmt;
19242 /* Parse the function-body. */
19243 cp_parser_function_body (parser, in_function_try_block);
19244 if (check_body_p)
19245 check_constexpr_ctor_body (last, list, /*complain=*/true);
19246 /* Finish the function body. */
19247 finish_function_body (body);
19249 return ctor_initializer_p;
19252 /* Parse an initializer.
19254 initializer:
19255 = initializer-clause
19256 ( expression-list )
19258 Returns an expression representing the initializer. If no
19259 initializer is present, NULL_TREE is returned.
19261 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19262 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19263 set to TRUE if there is no initializer present. If there is an
19264 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19265 is set to true; otherwise it is set to false. */
19267 static tree
19268 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19269 bool* non_constant_p)
19271 cp_token *token;
19272 tree init;
19274 /* Peek at the next token. */
19275 token = cp_lexer_peek_token (parser->lexer);
19277 /* Let our caller know whether or not this initializer was
19278 parenthesized. */
19279 *is_direct_init = (token->type != CPP_EQ);
19280 /* Assume that the initializer is constant. */
19281 *non_constant_p = false;
19283 if (token->type == CPP_EQ)
19285 /* Consume the `='. */
19286 cp_lexer_consume_token (parser->lexer);
19287 /* Parse the initializer-clause. */
19288 init = cp_parser_initializer_clause (parser, non_constant_p);
19290 else if (token->type == CPP_OPEN_PAREN)
19292 vec<tree, va_gc> *vec;
19293 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19294 /*cast_p=*/false,
19295 /*allow_expansion_p=*/true,
19296 non_constant_p);
19297 if (vec == NULL)
19298 return error_mark_node;
19299 init = build_tree_list_vec (vec);
19300 release_tree_vector (vec);
19302 else if (token->type == CPP_OPEN_BRACE)
19304 cp_lexer_set_source_position (parser->lexer);
19305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19306 init = cp_parser_braced_list (parser, non_constant_p);
19307 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19309 else
19311 /* Anything else is an error. */
19312 cp_parser_error (parser, "expected initializer");
19313 init = error_mark_node;
19316 return init;
19319 /* Parse an initializer-clause.
19321 initializer-clause:
19322 assignment-expression
19323 braced-init-list
19325 Returns an expression representing the initializer.
19327 If the `assignment-expression' production is used the value
19328 returned is simply a representation for the expression.
19330 Otherwise, calls cp_parser_braced_list. */
19332 static tree
19333 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19335 tree initializer;
19337 /* Assume the expression is constant. */
19338 *non_constant_p = false;
19340 /* If it is not a `{', then we are looking at an
19341 assignment-expression. */
19342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19343 initializer
19344 = cp_parser_constant_expression (parser,
19345 /*allow_non_constant_p=*/true,
19346 non_constant_p);
19347 else
19348 initializer = cp_parser_braced_list (parser, non_constant_p);
19350 return initializer;
19353 /* Parse a brace-enclosed initializer list.
19355 braced-init-list:
19356 { initializer-list , [opt] }
19359 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19360 the elements of the initializer-list (or NULL, if the last
19361 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19362 NULL_TREE. There is no way to detect whether or not the optional
19363 trailing `,' was provided. NON_CONSTANT_P is as for
19364 cp_parser_initializer. */
19366 static tree
19367 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19369 tree initializer;
19371 /* Consume the `{' token. */
19372 cp_lexer_consume_token (parser->lexer);
19373 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19374 initializer = make_node (CONSTRUCTOR);
19375 /* If it's not a `}', then there is a non-trivial initializer. */
19376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19378 /* Parse the initializer list. */
19379 CONSTRUCTOR_ELTS (initializer)
19380 = cp_parser_initializer_list (parser, non_constant_p);
19381 /* A trailing `,' token is allowed. */
19382 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19383 cp_lexer_consume_token (parser->lexer);
19385 else
19386 *non_constant_p = false;
19387 /* Now, there should be a trailing `}'. */
19388 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19389 TREE_TYPE (initializer) = init_list_type_node;
19390 return initializer;
19393 /* Consume tokens up to, and including, the next non-nested closing `]'.
19394 Returns true iff we found a closing `]'. */
19396 static bool
19397 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19399 unsigned square_depth = 0;
19401 while (true)
19403 cp_token * token = cp_lexer_peek_token (parser->lexer);
19405 switch (token->type)
19407 case CPP_EOF:
19408 case CPP_PRAGMA_EOL:
19409 /* If we've run out of tokens, then there is no closing `]'. */
19410 return false;
19412 case CPP_OPEN_SQUARE:
19413 ++square_depth;
19414 break;
19416 case CPP_CLOSE_SQUARE:
19417 if (!square_depth--)
19419 cp_lexer_consume_token (parser->lexer);
19420 return true;
19422 break;
19424 default:
19425 break;
19428 /* Consume the token. */
19429 cp_lexer_consume_token (parser->lexer);
19433 /* Return true if we are looking at an array-designator, false otherwise. */
19435 static bool
19436 cp_parser_array_designator_p (cp_parser *parser)
19438 /* Consume the `['. */
19439 cp_lexer_consume_token (parser->lexer);
19441 cp_lexer_save_tokens (parser->lexer);
19443 /* Skip tokens until the next token is a closing square bracket.
19444 If we find the closing `]', and the next token is a `=', then
19445 we are looking at an array designator. */
19446 bool array_designator_p
19447 = (cp_parser_skip_to_closing_square_bracket (parser)
19448 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19450 /* Roll back the tokens we skipped. */
19451 cp_lexer_rollback_tokens (parser->lexer);
19453 return array_designator_p;
19456 /* Parse an initializer-list.
19458 initializer-list:
19459 initializer-clause ... [opt]
19460 initializer-list , initializer-clause ... [opt]
19462 GNU Extension:
19464 initializer-list:
19465 designation initializer-clause ...[opt]
19466 initializer-list , designation initializer-clause ...[opt]
19468 designation:
19469 . identifier =
19470 identifier :
19471 [ constant-expression ] =
19473 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19474 for the initializer. If the INDEX of the elt is non-NULL, it is the
19475 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19476 as for cp_parser_initializer. */
19478 static vec<constructor_elt, va_gc> *
19479 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19481 vec<constructor_elt, va_gc> *v = NULL;
19483 /* Assume all of the expressions are constant. */
19484 *non_constant_p = false;
19486 /* Parse the rest of the list. */
19487 while (true)
19489 cp_token *token;
19490 tree designator;
19491 tree initializer;
19492 bool clause_non_constant_p;
19494 /* If the next token is an identifier and the following one is a
19495 colon, we are looking at the GNU designated-initializer
19496 syntax. */
19497 if (cp_parser_allow_gnu_extensions_p (parser)
19498 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19499 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19501 /* Warn the user that they are using an extension. */
19502 pedwarn (input_location, OPT_Wpedantic,
19503 "ISO C++ does not allow designated initializers");
19504 /* Consume the identifier. */
19505 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19506 /* Consume the `:'. */
19507 cp_lexer_consume_token (parser->lexer);
19509 /* Also handle the C99 syntax, '. id ='. */
19510 else if (cp_parser_allow_gnu_extensions_p (parser)
19511 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19513 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19515 /* Warn the user that they are using an extension. */
19516 pedwarn (input_location, OPT_Wpedantic,
19517 "ISO C++ does not allow C99 designated initializers");
19518 /* Consume the `.'. */
19519 cp_lexer_consume_token (parser->lexer);
19520 /* Consume the identifier. */
19521 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19522 /* Consume the `='. */
19523 cp_lexer_consume_token (parser->lexer);
19525 /* Also handle C99 array designators, '[ const ] ='. */
19526 else if (cp_parser_allow_gnu_extensions_p (parser)
19527 && !c_dialect_objc ()
19528 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19530 /* In C++11, [ could start a lambda-introducer. */
19531 bool non_const = false;
19533 cp_parser_parse_tentatively (parser);
19535 if (!cp_parser_array_designator_p (parser))
19537 cp_parser_simulate_error (parser);
19538 designator = NULL_TREE;
19540 else
19542 designator = cp_parser_constant_expression (parser, true,
19543 &non_const);
19544 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19545 cp_parser_require (parser, CPP_EQ, RT_EQ);
19548 if (!cp_parser_parse_definitely (parser))
19549 designator = NULL_TREE;
19550 else if (non_const)
19551 require_potential_rvalue_constant_expression (designator);
19553 else
19554 designator = NULL_TREE;
19556 /* Parse the initializer. */
19557 initializer = cp_parser_initializer_clause (parser,
19558 &clause_non_constant_p);
19559 /* If any clause is non-constant, so is the entire initializer. */
19560 if (clause_non_constant_p)
19561 *non_constant_p = true;
19563 /* If we have an ellipsis, this is an initializer pack
19564 expansion. */
19565 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19567 /* Consume the `...'. */
19568 cp_lexer_consume_token (parser->lexer);
19570 /* Turn the initializer into an initializer expansion. */
19571 initializer = make_pack_expansion (initializer);
19574 /* Add it to the vector. */
19575 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19577 /* If the next token is not a comma, we have reached the end of
19578 the list. */
19579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19580 break;
19582 /* Peek at the next token. */
19583 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19584 /* If the next token is a `}', then we're still done. An
19585 initializer-clause can have a trailing `,' after the
19586 initializer-list and before the closing `}'. */
19587 if (token->type == CPP_CLOSE_BRACE)
19588 break;
19590 /* Consume the `,' token. */
19591 cp_lexer_consume_token (parser->lexer);
19594 return v;
19597 /* Classes [gram.class] */
19599 /* Parse a class-name.
19601 class-name:
19602 identifier
19603 template-id
19605 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19606 to indicate that names looked up in dependent types should be
19607 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19608 keyword has been used to indicate that the name that appears next
19609 is a template. TAG_TYPE indicates the explicit tag given before
19610 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19611 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19612 is the class being defined in a class-head. If ENUM_OK is TRUE,
19613 enum-names are also accepted.
19615 Returns the TYPE_DECL representing the class. */
19617 static tree
19618 cp_parser_class_name (cp_parser *parser,
19619 bool typename_keyword_p,
19620 bool template_keyword_p,
19621 enum tag_types tag_type,
19622 bool check_dependency_p,
19623 bool class_head_p,
19624 bool is_declaration,
19625 bool enum_ok)
19627 tree decl;
19628 tree scope;
19629 bool typename_p;
19630 cp_token *token;
19631 tree identifier = NULL_TREE;
19633 /* All class-names start with an identifier. */
19634 token = cp_lexer_peek_token (parser->lexer);
19635 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19637 cp_parser_error (parser, "expected class-name");
19638 return error_mark_node;
19641 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19642 to a template-id, so we save it here. */
19643 scope = parser->scope;
19644 if (scope == error_mark_node)
19645 return error_mark_node;
19647 /* Any name names a type if we're following the `typename' keyword
19648 in a qualified name where the enclosing scope is type-dependent. */
19649 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19650 && dependent_type_p (scope));
19651 /* Handle the common case (an identifier, but not a template-id)
19652 efficiently. */
19653 if (token->type == CPP_NAME
19654 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19656 cp_token *identifier_token;
19657 bool ambiguous_p;
19659 /* Look for the identifier. */
19660 identifier_token = cp_lexer_peek_token (parser->lexer);
19661 ambiguous_p = identifier_token->error_reported;
19662 identifier = cp_parser_identifier (parser);
19663 /* If the next token isn't an identifier, we are certainly not
19664 looking at a class-name. */
19665 if (identifier == error_mark_node)
19666 decl = error_mark_node;
19667 /* If we know this is a type-name, there's no need to look it
19668 up. */
19669 else if (typename_p)
19670 decl = identifier;
19671 else
19673 tree ambiguous_decls;
19674 /* If we already know that this lookup is ambiguous, then
19675 we've already issued an error message; there's no reason
19676 to check again. */
19677 if (ambiguous_p)
19679 cp_parser_simulate_error (parser);
19680 return error_mark_node;
19682 /* If the next token is a `::', then the name must be a type
19683 name.
19685 [basic.lookup.qual]
19687 During the lookup for a name preceding the :: scope
19688 resolution operator, object, function, and enumerator
19689 names are ignored. */
19690 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19691 tag_type = typename_type;
19692 /* Look up the name. */
19693 decl = cp_parser_lookup_name (parser, identifier,
19694 tag_type,
19695 /*is_template=*/false,
19696 /*is_namespace=*/false,
19697 check_dependency_p,
19698 &ambiguous_decls,
19699 identifier_token->location);
19700 if (ambiguous_decls)
19702 if (cp_parser_parsing_tentatively (parser))
19703 cp_parser_simulate_error (parser);
19704 return error_mark_node;
19708 else
19710 /* Try a template-id. */
19711 decl = cp_parser_template_id (parser, template_keyword_p,
19712 check_dependency_p,
19713 tag_type,
19714 is_declaration);
19715 if (decl == error_mark_node)
19716 return error_mark_node;
19719 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19721 /* If this is a typename, create a TYPENAME_TYPE. */
19722 if (typename_p && decl != error_mark_node)
19724 decl = make_typename_type (scope, decl, typename_type,
19725 /*complain=*/tf_error);
19726 if (decl != error_mark_node)
19727 decl = TYPE_NAME (decl);
19730 decl = strip_using_decl (decl);
19732 /* Check to see that it is really the name of a class. */
19733 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19734 && identifier_p (TREE_OPERAND (decl, 0))
19735 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19736 /* Situations like this:
19738 template <typename T> struct A {
19739 typename T::template X<int>::I i;
19742 are problematic. Is `T::template X<int>' a class-name? The
19743 standard does not seem to be definitive, but there is no other
19744 valid interpretation of the following `::'. Therefore, those
19745 names are considered class-names. */
19747 decl = make_typename_type (scope, decl, tag_type, tf_error);
19748 if (decl != error_mark_node)
19749 decl = TYPE_NAME (decl);
19751 else if (TREE_CODE (decl) != TYPE_DECL
19752 || TREE_TYPE (decl) == error_mark_node
19753 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19754 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19755 /* In Objective-C 2.0, a classname followed by '.' starts a
19756 dot-syntax expression, and it's not a type-name. */
19757 || (c_dialect_objc ()
19758 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19759 && objc_is_class_name (decl)))
19760 decl = error_mark_node;
19762 if (decl == error_mark_node)
19763 cp_parser_error (parser, "expected class-name");
19764 else if (identifier && !parser->scope)
19765 maybe_note_name_used_in_class (identifier, decl);
19767 return decl;
19770 /* Parse a class-specifier.
19772 class-specifier:
19773 class-head { member-specification [opt] }
19775 Returns the TREE_TYPE representing the class. */
19777 static tree
19778 cp_parser_class_specifier_1 (cp_parser* parser)
19780 tree type;
19781 tree attributes = NULL_TREE;
19782 bool nested_name_specifier_p;
19783 unsigned saved_num_template_parameter_lists;
19784 bool saved_in_function_body;
19785 unsigned char in_statement;
19786 bool in_switch_statement_p;
19787 bool saved_in_unbraced_linkage_specification_p;
19788 tree old_scope = NULL_TREE;
19789 tree scope = NULL_TREE;
19790 cp_token *closing_brace;
19792 push_deferring_access_checks (dk_no_deferred);
19794 /* Parse the class-head. */
19795 type = cp_parser_class_head (parser,
19796 &nested_name_specifier_p);
19797 /* If the class-head was a semantic disaster, skip the entire body
19798 of the class. */
19799 if (!type)
19801 cp_parser_skip_to_end_of_block_or_statement (parser);
19802 pop_deferring_access_checks ();
19803 return error_mark_node;
19806 /* Look for the `{'. */
19807 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19809 pop_deferring_access_checks ();
19810 return error_mark_node;
19813 cp_ensure_no_omp_declare_simd (parser);
19815 /* Issue an error message if type-definitions are forbidden here. */
19816 cp_parser_check_type_definition (parser);
19817 /* Remember that we are defining one more class. */
19818 ++parser->num_classes_being_defined;
19819 /* Inside the class, surrounding template-parameter-lists do not
19820 apply. */
19821 saved_num_template_parameter_lists
19822 = parser->num_template_parameter_lists;
19823 parser->num_template_parameter_lists = 0;
19824 /* We are not in a function body. */
19825 saved_in_function_body = parser->in_function_body;
19826 parser->in_function_body = false;
19827 /* Or in a loop. */
19828 in_statement = parser->in_statement;
19829 parser->in_statement = 0;
19830 /* Or in a switch. */
19831 in_switch_statement_p = parser->in_switch_statement_p;
19832 parser->in_switch_statement_p = false;
19833 /* We are not immediately inside an extern "lang" block. */
19834 saved_in_unbraced_linkage_specification_p
19835 = parser->in_unbraced_linkage_specification_p;
19836 parser->in_unbraced_linkage_specification_p = false;
19838 /* Start the class. */
19839 if (nested_name_specifier_p)
19841 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19842 old_scope = push_inner_scope (scope);
19844 type = begin_class_definition (type);
19846 if (type == error_mark_node)
19847 /* If the type is erroneous, skip the entire body of the class. */
19848 cp_parser_skip_to_closing_brace (parser);
19849 else
19850 /* Parse the member-specification. */
19851 cp_parser_member_specification_opt (parser);
19853 /* Look for the trailing `}'. */
19854 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19855 /* Look for trailing attributes to apply to this class. */
19856 if (cp_parser_allow_gnu_extensions_p (parser))
19857 attributes = cp_parser_gnu_attributes_opt (parser);
19858 if (type != error_mark_node)
19859 type = finish_struct (type, attributes);
19860 if (nested_name_specifier_p)
19861 pop_inner_scope (old_scope, scope);
19863 /* We've finished a type definition. Check for the common syntax
19864 error of forgetting a semicolon after the definition. We need to
19865 be careful, as we can't just check for not-a-semicolon and be done
19866 with it; the user might have typed:
19868 class X { } c = ...;
19869 class X { } *p = ...;
19871 and so forth. Instead, enumerate all the possible tokens that
19872 might follow this production; if we don't see one of them, then
19873 complain and silently insert the semicolon. */
19875 cp_token *token = cp_lexer_peek_token (parser->lexer);
19876 bool want_semicolon = true;
19878 if (cp_next_tokens_can_be_std_attribute_p (parser))
19879 /* Don't try to parse c++11 attributes here. As per the
19880 grammar, that should be a task for
19881 cp_parser_decl_specifier_seq. */
19882 want_semicolon = false;
19884 switch (token->type)
19886 case CPP_NAME:
19887 case CPP_SEMICOLON:
19888 case CPP_MULT:
19889 case CPP_AND:
19890 case CPP_OPEN_PAREN:
19891 case CPP_CLOSE_PAREN:
19892 case CPP_COMMA:
19893 want_semicolon = false;
19894 break;
19896 /* While it's legal for type qualifiers and storage class
19897 specifiers to follow type definitions in the grammar, only
19898 compiler testsuites contain code like that. Assume that if
19899 we see such code, then what we're really seeing is a case
19900 like:
19902 class X { }
19903 const <type> var = ...;
19907 class Y { }
19908 static <type> func (...) ...
19910 i.e. the qualifier or specifier applies to the next
19911 declaration. To do so, however, we need to look ahead one
19912 more token to see if *that* token is a type specifier.
19914 This code could be improved to handle:
19916 class Z { }
19917 static const <type> var = ...; */
19918 case CPP_KEYWORD:
19919 if (keyword_is_decl_specifier (token->keyword))
19921 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19923 /* Handling user-defined types here would be nice, but very
19924 tricky. */
19925 want_semicolon
19926 = (lookahead->type == CPP_KEYWORD
19927 && keyword_begins_type_specifier (lookahead->keyword));
19929 break;
19930 default:
19931 break;
19934 /* If we don't have a type, then something is very wrong and we
19935 shouldn't try to do anything clever. Likewise for not seeing the
19936 closing brace. */
19937 if (closing_brace && TYPE_P (type) && want_semicolon)
19939 cp_token_position prev
19940 = cp_lexer_previous_token_position (parser->lexer);
19941 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19942 location_t loc = prev_token->location;
19944 if (CLASSTYPE_DECLARED_CLASS (type))
19945 error_at (loc, "expected %<;%> after class definition");
19946 else if (TREE_CODE (type) == RECORD_TYPE)
19947 error_at (loc, "expected %<;%> after struct definition");
19948 else if (TREE_CODE (type) == UNION_TYPE)
19949 error_at (loc, "expected %<;%> after union definition");
19950 else
19951 gcc_unreachable ();
19953 /* Unget one token and smash it to look as though we encountered
19954 a semicolon in the input stream. */
19955 cp_lexer_set_token_position (parser->lexer, prev);
19956 token = cp_lexer_peek_token (parser->lexer);
19957 token->type = CPP_SEMICOLON;
19958 token->keyword = RID_MAX;
19962 /* If this class is not itself within the scope of another class,
19963 then we need to parse the bodies of all of the queued function
19964 definitions. Note that the queued functions defined in a class
19965 are not always processed immediately following the
19966 class-specifier for that class. Consider:
19968 struct A {
19969 struct B { void f() { sizeof (A); } };
19972 If `f' were processed before the processing of `A' were
19973 completed, there would be no way to compute the size of `A'.
19974 Note that the nesting we are interested in here is lexical --
19975 not the semantic nesting given by TYPE_CONTEXT. In particular,
19976 for:
19978 struct A { struct B; };
19979 struct A::B { void f() { } };
19981 there is no need to delay the parsing of `A::B::f'. */
19982 if (--parser->num_classes_being_defined == 0)
19984 tree decl;
19985 tree class_type = NULL_TREE;
19986 tree pushed_scope = NULL_TREE;
19987 unsigned ix;
19988 cp_default_arg_entry *e;
19989 tree save_ccp, save_ccr;
19991 /* In a first pass, parse default arguments to the functions.
19992 Then, in a second pass, parse the bodies of the functions.
19993 This two-phased approach handles cases like:
19995 struct S {
19996 void f() { g(); }
19997 void g(int i = 3);
20001 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20003 decl = e->decl;
20004 /* If there are default arguments that have not yet been processed,
20005 take care of them now. */
20006 if (class_type != e->class_type)
20008 if (pushed_scope)
20009 pop_scope (pushed_scope);
20010 class_type = e->class_type;
20011 pushed_scope = push_scope (class_type);
20013 /* Make sure that any template parameters are in scope. */
20014 maybe_begin_member_template_processing (decl);
20015 /* Parse the default argument expressions. */
20016 cp_parser_late_parsing_default_args (parser, decl);
20017 /* Remove any template parameters from the symbol table. */
20018 maybe_end_member_template_processing ();
20020 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20021 /* Now parse any NSDMIs. */
20022 save_ccp = current_class_ptr;
20023 save_ccr = current_class_ref;
20024 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20026 if (class_type != DECL_CONTEXT (decl))
20028 if (pushed_scope)
20029 pop_scope (pushed_scope);
20030 class_type = DECL_CONTEXT (decl);
20031 pushed_scope = push_scope (class_type);
20033 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20034 cp_parser_late_parsing_nsdmi (parser, decl);
20036 vec_safe_truncate (unparsed_nsdmis, 0);
20037 current_class_ptr = save_ccp;
20038 current_class_ref = save_ccr;
20039 if (pushed_scope)
20040 pop_scope (pushed_scope);
20042 /* Now do some post-NSDMI bookkeeping. */
20043 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20044 after_nsdmi_defaulted_late_checks (class_type);
20045 vec_safe_truncate (unparsed_classes, 0);
20046 after_nsdmi_defaulted_late_checks (type);
20048 /* Now parse the body of the functions. */
20049 if (flag_openmp)
20051 /* OpenMP UDRs need to be parsed before all other functions. */
20052 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20053 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20054 cp_parser_late_parsing_for_member (parser, decl);
20055 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20056 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20057 cp_parser_late_parsing_for_member (parser, decl);
20059 else
20060 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20061 cp_parser_late_parsing_for_member (parser, decl);
20062 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20064 else
20065 vec_safe_push (unparsed_classes, type);
20067 /* Put back any saved access checks. */
20068 pop_deferring_access_checks ();
20070 /* Restore saved state. */
20071 parser->in_switch_statement_p = in_switch_statement_p;
20072 parser->in_statement = in_statement;
20073 parser->in_function_body = saved_in_function_body;
20074 parser->num_template_parameter_lists
20075 = saved_num_template_parameter_lists;
20076 parser->in_unbraced_linkage_specification_p
20077 = saved_in_unbraced_linkage_specification_p;
20079 return type;
20082 static tree
20083 cp_parser_class_specifier (cp_parser* parser)
20085 tree ret;
20086 timevar_push (TV_PARSE_STRUCT);
20087 ret = cp_parser_class_specifier_1 (parser);
20088 timevar_pop (TV_PARSE_STRUCT);
20089 return ret;
20092 /* Parse a class-head.
20094 class-head:
20095 class-key identifier [opt] base-clause [opt]
20096 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20097 class-key nested-name-specifier [opt] template-id
20098 base-clause [opt]
20100 class-virt-specifier:
20101 final
20103 GNU Extensions:
20104 class-key attributes identifier [opt] base-clause [opt]
20105 class-key attributes nested-name-specifier identifier base-clause [opt]
20106 class-key attributes nested-name-specifier [opt] template-id
20107 base-clause [opt]
20109 Upon return BASES is initialized to the list of base classes (or
20110 NULL, if there are none) in the same form returned by
20111 cp_parser_base_clause.
20113 Returns the TYPE of the indicated class. Sets
20114 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20115 involving a nested-name-specifier was used, and FALSE otherwise.
20117 Returns error_mark_node if this is not a class-head.
20119 Returns NULL_TREE if the class-head is syntactically valid, but
20120 semantically invalid in a way that means we should skip the entire
20121 body of the class. */
20123 static tree
20124 cp_parser_class_head (cp_parser* parser,
20125 bool* nested_name_specifier_p)
20127 tree nested_name_specifier;
20128 enum tag_types class_key;
20129 tree id = NULL_TREE;
20130 tree type = NULL_TREE;
20131 tree attributes;
20132 tree bases;
20133 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20134 bool template_id_p = false;
20135 bool qualified_p = false;
20136 bool invalid_nested_name_p = false;
20137 bool invalid_explicit_specialization_p = false;
20138 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20139 tree pushed_scope = NULL_TREE;
20140 unsigned num_templates;
20141 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20142 /* Assume no nested-name-specifier will be present. */
20143 *nested_name_specifier_p = false;
20144 /* Assume no template parameter lists will be used in defining the
20145 type. */
20146 num_templates = 0;
20147 parser->colon_corrects_to_scope_p = false;
20149 /* Look for the class-key. */
20150 class_key = cp_parser_class_key (parser);
20151 if (class_key == none_type)
20152 return error_mark_node;
20154 /* Parse the attributes. */
20155 attributes = cp_parser_attributes_opt (parser);
20157 /* If the next token is `::', that is invalid -- but sometimes
20158 people do try to write:
20160 struct ::S {};
20162 Handle this gracefully by accepting the extra qualifier, and then
20163 issuing an error about it later if this really is a
20164 class-head. If it turns out just to be an elaborated type
20165 specifier, remain silent. */
20166 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20167 qualified_p = true;
20169 push_deferring_access_checks (dk_no_check);
20171 /* Determine the name of the class. Begin by looking for an
20172 optional nested-name-specifier. */
20173 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20174 nested_name_specifier
20175 = cp_parser_nested_name_specifier_opt (parser,
20176 /*typename_keyword_p=*/false,
20177 /*check_dependency_p=*/false,
20178 /*type_p=*/true,
20179 /*is_declaration=*/false);
20180 /* If there was a nested-name-specifier, then there *must* be an
20181 identifier. */
20182 if (nested_name_specifier)
20184 type_start_token = cp_lexer_peek_token (parser->lexer);
20185 /* Although the grammar says `identifier', it really means
20186 `class-name' or `template-name'. You are only allowed to
20187 define a class that has already been declared with this
20188 syntax.
20190 The proposed resolution for Core Issue 180 says that wherever
20191 you see `class T::X' you should treat `X' as a type-name.
20193 It is OK to define an inaccessible class; for example:
20195 class A { class B; };
20196 class A::B {};
20198 We do not know if we will see a class-name, or a
20199 template-name. We look for a class-name first, in case the
20200 class-name is a template-id; if we looked for the
20201 template-name first we would stop after the template-name. */
20202 cp_parser_parse_tentatively (parser);
20203 type = cp_parser_class_name (parser,
20204 /*typename_keyword_p=*/false,
20205 /*template_keyword_p=*/false,
20206 class_type,
20207 /*check_dependency_p=*/false,
20208 /*class_head_p=*/true,
20209 /*is_declaration=*/false);
20210 /* If that didn't work, ignore the nested-name-specifier. */
20211 if (!cp_parser_parse_definitely (parser))
20213 invalid_nested_name_p = true;
20214 type_start_token = cp_lexer_peek_token (parser->lexer);
20215 id = cp_parser_identifier (parser);
20216 if (id == error_mark_node)
20217 id = NULL_TREE;
20219 /* If we could not find a corresponding TYPE, treat this
20220 declaration like an unqualified declaration. */
20221 if (type == error_mark_node)
20222 nested_name_specifier = NULL_TREE;
20223 /* Otherwise, count the number of templates used in TYPE and its
20224 containing scopes. */
20225 else
20227 tree scope;
20229 for (scope = TREE_TYPE (type);
20230 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20231 scope = get_containing_scope (scope))
20232 if (TYPE_P (scope)
20233 && CLASS_TYPE_P (scope)
20234 && CLASSTYPE_TEMPLATE_INFO (scope)
20235 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20236 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20237 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20238 ++num_templates;
20241 /* Otherwise, the identifier is optional. */
20242 else
20244 /* We don't know whether what comes next is a template-id,
20245 an identifier, or nothing at all. */
20246 cp_parser_parse_tentatively (parser);
20247 /* Check for a template-id. */
20248 type_start_token = cp_lexer_peek_token (parser->lexer);
20249 id = cp_parser_template_id (parser,
20250 /*template_keyword_p=*/false,
20251 /*check_dependency_p=*/true,
20252 class_key,
20253 /*is_declaration=*/true);
20254 /* If that didn't work, it could still be an identifier. */
20255 if (!cp_parser_parse_definitely (parser))
20257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20259 type_start_token = cp_lexer_peek_token (parser->lexer);
20260 id = cp_parser_identifier (parser);
20262 else
20263 id = NULL_TREE;
20265 else
20267 template_id_p = true;
20268 ++num_templates;
20272 pop_deferring_access_checks ();
20274 if (id)
20276 cp_parser_check_for_invalid_template_id (parser, id,
20277 class_key,
20278 type_start_token->location);
20280 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20282 /* If it's not a `:' or a `{' then we can't really be looking at a
20283 class-head, since a class-head only appears as part of a
20284 class-specifier. We have to detect this situation before calling
20285 xref_tag, since that has irreversible side-effects. */
20286 if (!cp_parser_next_token_starts_class_definition_p (parser))
20288 cp_parser_error (parser, "expected %<{%> or %<:%>");
20289 type = error_mark_node;
20290 goto out;
20293 /* At this point, we're going ahead with the class-specifier, even
20294 if some other problem occurs. */
20295 cp_parser_commit_to_tentative_parse (parser);
20296 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20298 cp_parser_error (parser,
20299 "cannot specify %<override%> for a class");
20300 type = error_mark_node;
20301 goto out;
20303 /* Issue the error about the overly-qualified name now. */
20304 if (qualified_p)
20306 cp_parser_error (parser,
20307 "global qualification of class name is invalid");
20308 type = error_mark_node;
20309 goto out;
20311 else if (invalid_nested_name_p)
20313 cp_parser_error (parser,
20314 "qualified name does not name a class");
20315 type = error_mark_node;
20316 goto out;
20318 else if (nested_name_specifier)
20320 tree scope;
20322 /* Reject typedef-names in class heads. */
20323 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20325 error_at (type_start_token->location,
20326 "invalid class name in declaration of %qD",
20327 type);
20328 type = NULL_TREE;
20329 goto done;
20332 /* Figure out in what scope the declaration is being placed. */
20333 scope = current_scope ();
20334 /* If that scope does not contain the scope in which the
20335 class was originally declared, the program is invalid. */
20336 if (scope && !is_ancestor (scope, nested_name_specifier))
20338 if (at_namespace_scope_p ())
20339 error_at (type_start_token->location,
20340 "declaration of %qD in namespace %qD which does not "
20341 "enclose %qD",
20342 type, scope, nested_name_specifier);
20343 else
20344 error_at (type_start_token->location,
20345 "declaration of %qD in %qD which does not enclose %qD",
20346 type, scope, nested_name_specifier);
20347 type = NULL_TREE;
20348 goto done;
20350 /* [dcl.meaning]
20352 A declarator-id shall not be qualified except for the
20353 definition of a ... nested class outside of its class
20354 ... [or] the definition or explicit instantiation of a
20355 class member of a namespace outside of its namespace. */
20356 if (scope == nested_name_specifier)
20358 permerror (nested_name_specifier_token_start->location,
20359 "extra qualification not allowed");
20360 nested_name_specifier = NULL_TREE;
20361 num_templates = 0;
20364 /* An explicit-specialization must be preceded by "template <>". If
20365 it is not, try to recover gracefully. */
20366 if (at_namespace_scope_p ()
20367 && parser->num_template_parameter_lists == 0
20368 && template_id_p)
20370 error_at (type_start_token->location,
20371 "an explicit specialization must be preceded by %<template <>%>");
20372 invalid_explicit_specialization_p = true;
20373 /* Take the same action that would have been taken by
20374 cp_parser_explicit_specialization. */
20375 ++parser->num_template_parameter_lists;
20376 begin_specialization ();
20378 /* There must be no "return" statements between this point and the
20379 end of this function; set "type "to the correct return value and
20380 use "goto done;" to return. */
20381 /* Make sure that the right number of template parameters were
20382 present. */
20383 if (!cp_parser_check_template_parameters (parser, num_templates,
20384 type_start_token->location,
20385 /*declarator=*/NULL))
20387 /* If something went wrong, there is no point in even trying to
20388 process the class-definition. */
20389 type = NULL_TREE;
20390 goto done;
20393 /* Look up the type. */
20394 if (template_id_p)
20396 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20397 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20398 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20400 error_at (type_start_token->location,
20401 "function template %qD redeclared as a class template", id);
20402 type = error_mark_node;
20404 else
20406 type = TREE_TYPE (id);
20407 type = maybe_process_partial_specialization (type);
20409 if (nested_name_specifier)
20410 pushed_scope = push_scope (nested_name_specifier);
20412 else if (nested_name_specifier)
20414 tree class_type;
20416 /* Given:
20418 template <typename T> struct S { struct T };
20419 template <typename T> struct S<T>::T { };
20421 we will get a TYPENAME_TYPE when processing the definition of
20422 `S::T'. We need to resolve it to the actual type before we
20423 try to define it. */
20424 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20426 class_type = resolve_typename_type (TREE_TYPE (type),
20427 /*only_current_p=*/false);
20428 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20429 type = TYPE_NAME (class_type);
20430 else
20432 cp_parser_error (parser, "could not resolve typename type");
20433 type = error_mark_node;
20437 if (maybe_process_partial_specialization (TREE_TYPE (type))
20438 == error_mark_node)
20440 type = NULL_TREE;
20441 goto done;
20444 class_type = current_class_type;
20445 /* Enter the scope indicated by the nested-name-specifier. */
20446 pushed_scope = push_scope (nested_name_specifier);
20447 /* Get the canonical version of this type. */
20448 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20449 /* Call push_template_decl if it seems like we should be defining a
20450 template either from the template headers or the type we're
20451 defining, so that we diagnose both extra and missing headers. */
20452 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20453 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20454 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20456 type = push_template_decl (type);
20457 if (type == error_mark_node)
20459 type = NULL_TREE;
20460 goto done;
20464 type = TREE_TYPE (type);
20465 *nested_name_specifier_p = true;
20467 else /* The name is not a nested name. */
20469 /* If the class was unnamed, create a dummy name. */
20470 if (!id)
20471 id = make_anon_name ();
20472 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20473 parser->num_template_parameter_lists);
20476 /* Indicate whether this class was declared as a `class' or as a
20477 `struct'. */
20478 if (TREE_CODE (type) == RECORD_TYPE)
20479 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20480 cp_parser_check_class_key (class_key, type);
20482 /* If this type was already complete, and we see another definition,
20483 that's an error. */
20484 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20486 error_at (type_start_token->location, "redefinition of %q#T",
20487 type);
20488 error_at (type_start_token->location, "previous definition of %q+#T",
20489 type);
20490 type = NULL_TREE;
20491 goto done;
20493 else if (type == error_mark_node)
20494 type = NULL_TREE;
20496 if (type)
20498 /* Apply attributes now, before any use of the class as a template
20499 argument in its base list. */
20500 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20501 fixup_attribute_variants (type);
20504 /* We will have entered the scope containing the class; the names of
20505 base classes should be looked up in that context. For example:
20507 struct A { struct B {}; struct C; };
20508 struct A::C : B {};
20510 is valid. */
20512 /* Get the list of base-classes, if there is one. */
20513 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20515 /* PR59482: enter the class scope so that base-specifiers are looked
20516 up correctly. */
20517 if (type)
20518 pushclass (type);
20519 bases = cp_parser_base_clause (parser);
20520 /* PR59482: get out of the previously pushed class scope so that the
20521 subsequent pops pop the right thing. */
20522 if (type)
20523 popclass ();
20525 else
20526 bases = NULL_TREE;
20528 /* If we're really defining a class, process the base classes.
20529 If they're invalid, fail. */
20530 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20531 && !xref_basetypes (type, bases))
20532 type = NULL_TREE;
20534 done:
20535 /* Leave the scope given by the nested-name-specifier. We will
20536 enter the class scope itself while processing the members. */
20537 if (pushed_scope)
20538 pop_scope (pushed_scope);
20540 if (invalid_explicit_specialization_p)
20542 end_specialization ();
20543 --parser->num_template_parameter_lists;
20546 if (type)
20547 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20548 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20549 CLASSTYPE_FINAL (type) = 1;
20550 out:
20551 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20552 return type;
20555 /* Parse a class-key.
20557 class-key:
20558 class
20559 struct
20560 union
20562 Returns the kind of class-key specified, or none_type to indicate
20563 error. */
20565 static enum tag_types
20566 cp_parser_class_key (cp_parser* parser)
20568 cp_token *token;
20569 enum tag_types tag_type;
20571 /* Look for the class-key. */
20572 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20573 if (!token)
20574 return none_type;
20576 /* Check to see if the TOKEN is a class-key. */
20577 tag_type = cp_parser_token_is_class_key (token);
20578 if (!tag_type)
20579 cp_parser_error (parser, "expected class-key");
20580 return tag_type;
20583 /* Parse a type-parameter-key.
20585 type-parameter-key:
20586 class
20587 typename
20590 static void
20591 cp_parser_type_parameter_key (cp_parser* parser)
20593 /* Look for the type-parameter-key. */
20594 enum tag_types tag_type = none_type;
20595 cp_token *token = cp_lexer_peek_token (parser->lexer);
20596 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20598 cp_lexer_consume_token (parser->lexer);
20599 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20600 /* typename is not allowed in a template template parameter
20601 by the standard until C++1Z. */
20602 pedwarn (token->location, OPT_Wpedantic,
20603 "ISO C++ forbids typename key in template template parameter;"
20604 " use -std=c++1z or -std=gnu++1z");
20606 else
20607 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20609 return;
20612 /* Parse an (optional) member-specification.
20614 member-specification:
20615 member-declaration member-specification [opt]
20616 access-specifier : member-specification [opt] */
20618 static void
20619 cp_parser_member_specification_opt (cp_parser* parser)
20621 while (true)
20623 cp_token *token;
20624 enum rid keyword;
20626 /* Peek at the next token. */
20627 token = cp_lexer_peek_token (parser->lexer);
20628 /* If it's a `}', or EOF then we've seen all the members. */
20629 if (token->type == CPP_CLOSE_BRACE
20630 || token->type == CPP_EOF
20631 || token->type == CPP_PRAGMA_EOL)
20632 break;
20634 /* See if this token is a keyword. */
20635 keyword = token->keyword;
20636 switch (keyword)
20638 case RID_PUBLIC:
20639 case RID_PROTECTED:
20640 case RID_PRIVATE:
20641 /* Consume the access-specifier. */
20642 cp_lexer_consume_token (parser->lexer);
20643 /* Remember which access-specifier is active. */
20644 current_access_specifier = token->u.value;
20645 /* Look for the `:'. */
20646 cp_parser_require (parser, CPP_COLON, RT_COLON);
20647 break;
20649 default:
20650 /* Accept #pragmas at class scope. */
20651 if (token->type == CPP_PRAGMA)
20653 cp_parser_pragma (parser, pragma_member);
20654 break;
20657 /* Otherwise, the next construction must be a
20658 member-declaration. */
20659 cp_parser_member_declaration (parser);
20664 /* Parse a member-declaration.
20666 member-declaration:
20667 decl-specifier-seq [opt] member-declarator-list [opt] ;
20668 function-definition ; [opt]
20669 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20670 using-declaration
20671 template-declaration
20672 alias-declaration
20674 member-declarator-list:
20675 member-declarator
20676 member-declarator-list , member-declarator
20678 member-declarator:
20679 declarator pure-specifier [opt]
20680 declarator constant-initializer [opt]
20681 identifier [opt] : constant-expression
20683 GNU Extensions:
20685 member-declaration:
20686 __extension__ member-declaration
20688 member-declarator:
20689 declarator attributes [opt] pure-specifier [opt]
20690 declarator attributes [opt] constant-initializer [opt]
20691 identifier [opt] attributes [opt] : constant-expression
20693 C++0x Extensions:
20695 member-declaration:
20696 static_assert-declaration */
20698 static void
20699 cp_parser_member_declaration (cp_parser* parser)
20701 cp_decl_specifier_seq decl_specifiers;
20702 tree prefix_attributes;
20703 tree decl;
20704 int declares_class_or_enum;
20705 bool friend_p;
20706 cp_token *token = NULL;
20707 cp_token *decl_spec_token_start = NULL;
20708 cp_token *initializer_token_start = NULL;
20709 int saved_pedantic;
20710 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20712 /* Check for the `__extension__' keyword. */
20713 if (cp_parser_extension_opt (parser, &saved_pedantic))
20715 /* Recurse. */
20716 cp_parser_member_declaration (parser);
20717 /* Restore the old value of the PEDANTIC flag. */
20718 pedantic = saved_pedantic;
20720 return;
20723 /* Check for a template-declaration. */
20724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20726 /* An explicit specialization here is an error condition, and we
20727 expect the specialization handler to detect and report this. */
20728 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20729 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20730 cp_parser_explicit_specialization (parser);
20731 else
20732 cp_parser_template_declaration (parser, /*member_p=*/true);
20734 return;
20737 /* Check for a using-declaration. */
20738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20740 if (cxx_dialect < cxx11)
20742 /* Parse the using-declaration. */
20743 cp_parser_using_declaration (parser,
20744 /*access_declaration_p=*/false);
20745 return;
20747 else
20749 tree decl;
20750 bool alias_decl_expected;
20751 cp_parser_parse_tentatively (parser);
20752 decl = cp_parser_alias_declaration (parser);
20753 /* Note that if we actually see the '=' token after the
20754 identifier, cp_parser_alias_declaration commits the
20755 tentative parse. In that case, we really expects an
20756 alias-declaration. Otherwise, we expect a using
20757 declaration. */
20758 alias_decl_expected =
20759 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20760 cp_parser_parse_definitely (parser);
20762 if (alias_decl_expected)
20763 finish_member_declaration (decl);
20764 else
20765 cp_parser_using_declaration (parser,
20766 /*access_declaration_p=*/false);
20767 return;
20771 /* Check for @defs. */
20772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20774 tree ivar, member;
20775 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20776 ivar = ivar_chains;
20777 while (ivar)
20779 member = ivar;
20780 ivar = TREE_CHAIN (member);
20781 TREE_CHAIN (member) = NULL_TREE;
20782 finish_member_declaration (member);
20784 return;
20787 /* If the next token is `static_assert' we have a static assertion. */
20788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20790 cp_parser_static_assert (parser, /*member_p=*/true);
20791 return;
20794 parser->colon_corrects_to_scope_p = false;
20796 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20797 goto out;
20799 /* Parse the decl-specifier-seq. */
20800 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20801 cp_parser_decl_specifier_seq (parser,
20802 CP_PARSER_FLAGS_OPTIONAL,
20803 &decl_specifiers,
20804 &declares_class_or_enum);
20805 /* Check for an invalid type-name. */
20806 if (!decl_specifiers.any_type_specifiers_p
20807 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20808 goto out;
20809 /* If there is no declarator, then the decl-specifier-seq should
20810 specify a type. */
20811 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20813 /* If there was no decl-specifier-seq, and the next token is a
20814 `;', then we have something like:
20816 struct S { ; };
20818 [class.mem]
20820 Each member-declaration shall declare at least one member
20821 name of the class. */
20822 if (!decl_specifiers.any_specifiers_p)
20824 cp_token *token = cp_lexer_peek_token (parser->lexer);
20825 if (!in_system_header_at (token->location))
20826 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20828 else
20830 tree type;
20832 /* See if this declaration is a friend. */
20833 friend_p = cp_parser_friend_p (&decl_specifiers);
20834 /* If there were decl-specifiers, check to see if there was
20835 a class-declaration. */
20836 type = check_tag_decl (&decl_specifiers,
20837 /*explicit_type_instantiation_p=*/false);
20838 /* Nested classes have already been added to the class, but
20839 a `friend' needs to be explicitly registered. */
20840 if (friend_p)
20842 /* If the `friend' keyword was present, the friend must
20843 be introduced with a class-key. */
20844 if (!declares_class_or_enum && cxx_dialect < cxx11)
20845 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20846 "in C++03 a class-key must be used "
20847 "when declaring a friend");
20848 /* In this case:
20850 template <typename T> struct A {
20851 friend struct A<T>::B;
20854 A<T>::B will be represented by a TYPENAME_TYPE, and
20855 therefore not recognized by check_tag_decl. */
20856 if (!type)
20858 type = decl_specifiers.type;
20859 if (type && TREE_CODE (type) == TYPE_DECL)
20860 type = TREE_TYPE (type);
20862 if (!type || !TYPE_P (type))
20863 error_at (decl_spec_token_start->location,
20864 "friend declaration does not name a class or "
20865 "function");
20866 else
20867 make_friend_class (current_class_type, type,
20868 /*complain=*/true);
20870 /* If there is no TYPE, an error message will already have
20871 been issued. */
20872 else if (!type || type == error_mark_node)
20874 /* An anonymous aggregate has to be handled specially; such
20875 a declaration really declares a data member (with a
20876 particular type), as opposed to a nested class. */
20877 else if (ANON_AGGR_TYPE_P (type))
20879 /* C++11 9.5/6. */
20880 if (decl_specifiers.storage_class != sc_none)
20881 error_at (decl_spec_token_start->location,
20882 "a storage class on an anonymous aggregate "
20883 "in class scope is not allowed");
20885 /* Remove constructors and such from TYPE, now that we
20886 know it is an anonymous aggregate. */
20887 fixup_anonymous_aggr (type);
20888 /* And make the corresponding data member. */
20889 decl = build_decl (decl_spec_token_start->location,
20890 FIELD_DECL, NULL_TREE, type);
20891 /* Add it to the class. */
20892 finish_member_declaration (decl);
20894 else
20895 cp_parser_check_access_in_redeclaration
20896 (TYPE_NAME (type),
20897 decl_spec_token_start->location);
20900 else
20902 bool assume_semicolon = false;
20904 /* Clear attributes from the decl_specifiers but keep them
20905 around as prefix attributes that apply them to the entity
20906 being declared. */
20907 prefix_attributes = decl_specifiers.attributes;
20908 decl_specifiers.attributes = NULL_TREE;
20910 /* See if these declarations will be friends. */
20911 friend_p = cp_parser_friend_p (&decl_specifiers);
20913 /* Keep going until we hit the `;' at the end of the
20914 declaration. */
20915 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20917 tree attributes = NULL_TREE;
20918 tree first_attribute;
20920 /* Peek at the next token. */
20921 token = cp_lexer_peek_token (parser->lexer);
20923 /* Check for a bitfield declaration. */
20924 if (token->type == CPP_COLON
20925 || (token->type == CPP_NAME
20926 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20927 == CPP_COLON))
20929 tree identifier;
20930 tree width;
20932 /* Get the name of the bitfield. Note that we cannot just
20933 check TOKEN here because it may have been invalidated by
20934 the call to cp_lexer_peek_nth_token above. */
20935 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20936 identifier = cp_parser_identifier (parser);
20937 else
20938 identifier = NULL_TREE;
20940 /* Consume the `:' token. */
20941 cp_lexer_consume_token (parser->lexer);
20943 /* Get the width of the bitfield. */
20944 width
20945 = cp_parser_constant_expression (parser);
20946 width = maybe_constant_value (width);
20948 /* Look for attributes that apply to the bitfield. */
20949 attributes = cp_parser_attributes_opt (parser);
20950 /* Remember which attributes are prefix attributes and
20951 which are not. */
20952 first_attribute = attributes;
20953 /* Combine the attributes. */
20954 attributes = chainon (prefix_attributes, attributes);
20956 /* Create the bitfield declaration. */
20957 decl = grokbitfield (identifier
20958 ? make_id_declarator (NULL_TREE,
20959 identifier,
20960 sfk_none)
20961 : NULL,
20962 &decl_specifiers,
20963 width,
20964 attributes);
20966 else
20968 cp_declarator *declarator;
20969 tree initializer;
20970 tree asm_specification;
20971 int ctor_dtor_or_conv_p;
20973 /* Parse the declarator. */
20974 declarator
20975 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20976 &ctor_dtor_or_conv_p,
20977 /*parenthesized_p=*/NULL,
20978 /*member_p=*/true,
20979 friend_p);
20981 /* If something went wrong parsing the declarator, make sure
20982 that we at least consume some tokens. */
20983 if (declarator == cp_error_declarator)
20985 /* Skip to the end of the statement. */
20986 cp_parser_skip_to_end_of_statement (parser);
20987 /* If the next token is not a semicolon, that is
20988 probably because we just skipped over the body of
20989 a function. So, we consume a semicolon if
20990 present, but do not issue an error message if it
20991 is not present. */
20992 if (cp_lexer_next_token_is (parser->lexer,
20993 CPP_SEMICOLON))
20994 cp_lexer_consume_token (parser->lexer);
20995 goto out;
20998 if (declares_class_or_enum & 2)
20999 cp_parser_check_for_definition_in_return_type
21000 (declarator, decl_specifiers.type,
21001 decl_specifiers.locations[ds_type_spec]);
21003 /* Look for an asm-specification. */
21004 asm_specification = cp_parser_asm_specification_opt (parser);
21005 /* Look for attributes that apply to the declaration. */
21006 attributes = cp_parser_attributes_opt (parser);
21007 /* Remember which attributes are prefix attributes and
21008 which are not. */
21009 first_attribute = attributes;
21010 /* Combine the attributes. */
21011 attributes = chainon (prefix_attributes, attributes);
21013 /* If it's an `=', then we have a constant-initializer or a
21014 pure-specifier. It is not correct to parse the
21015 initializer before registering the member declaration
21016 since the member declaration should be in scope while
21017 its initializer is processed. However, the rest of the
21018 front end does not yet provide an interface that allows
21019 us to handle this correctly. */
21020 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21022 /* In [class.mem]:
21024 A pure-specifier shall be used only in the declaration of
21025 a virtual function.
21027 A member-declarator can contain a constant-initializer
21028 only if it declares a static member of integral or
21029 enumeration type.
21031 Therefore, if the DECLARATOR is for a function, we look
21032 for a pure-specifier; otherwise, we look for a
21033 constant-initializer. When we call `grokfield', it will
21034 perform more stringent semantics checks. */
21035 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21036 if (function_declarator_p (declarator)
21037 || (decl_specifiers.type
21038 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21039 && declarator->kind == cdk_id
21040 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21041 == FUNCTION_TYPE)))
21042 initializer = cp_parser_pure_specifier (parser);
21043 else if (decl_specifiers.storage_class != sc_static)
21044 initializer = cp_parser_save_nsdmi (parser);
21045 else if (cxx_dialect >= cxx11)
21047 bool nonconst;
21048 /* Don't require a constant rvalue in C++11, since we
21049 might want a reference constant. We'll enforce
21050 constancy later. */
21051 cp_lexer_consume_token (parser->lexer);
21052 /* Parse the initializer. */
21053 initializer = cp_parser_initializer_clause (parser,
21054 &nonconst);
21056 else
21057 /* Parse the initializer. */
21058 initializer = cp_parser_constant_initializer (parser);
21060 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21061 && !function_declarator_p (declarator))
21063 bool x;
21064 if (decl_specifiers.storage_class != sc_static)
21065 initializer = cp_parser_save_nsdmi (parser);
21066 else
21067 initializer = cp_parser_initializer (parser, &x, &x);
21069 /* Otherwise, there is no initializer. */
21070 else
21071 initializer = NULL_TREE;
21073 /* See if we are probably looking at a function
21074 definition. We are certainly not looking at a
21075 member-declarator. Calling `grokfield' has
21076 side-effects, so we must not do it unless we are sure
21077 that we are looking at a member-declarator. */
21078 if (cp_parser_token_starts_function_definition_p
21079 (cp_lexer_peek_token (parser->lexer)))
21081 /* The grammar does not allow a pure-specifier to be
21082 used when a member function is defined. (It is
21083 possible that this fact is an oversight in the
21084 standard, since a pure function may be defined
21085 outside of the class-specifier. */
21086 if (initializer && initializer_token_start)
21087 error_at (initializer_token_start->location,
21088 "pure-specifier on function-definition");
21089 decl = cp_parser_save_member_function_body (parser,
21090 &decl_specifiers,
21091 declarator,
21092 attributes);
21093 if (parser->fully_implicit_function_template_p)
21094 decl = finish_fully_implicit_template (parser, decl);
21095 /* If the member was not a friend, declare it here. */
21096 if (!friend_p)
21097 finish_member_declaration (decl);
21098 /* Peek at the next token. */
21099 token = cp_lexer_peek_token (parser->lexer);
21100 /* If the next token is a semicolon, consume it. */
21101 if (token->type == CPP_SEMICOLON)
21102 cp_lexer_consume_token (parser->lexer);
21103 goto out;
21105 else
21106 if (declarator->kind == cdk_function)
21107 declarator->id_loc = token->location;
21108 /* Create the declaration. */
21109 decl = grokfield (declarator, &decl_specifiers,
21110 initializer, /*init_const_expr_p=*/true,
21111 asm_specification, attributes);
21112 if (parser->fully_implicit_function_template_p)
21114 if (friend_p)
21115 finish_fully_implicit_template (parser, 0);
21116 else
21117 decl = finish_fully_implicit_template (parser, decl);
21121 cp_finalize_omp_declare_simd (parser, decl);
21123 /* Reset PREFIX_ATTRIBUTES. */
21124 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21125 attributes = TREE_CHAIN (attributes);
21126 if (attributes)
21127 TREE_CHAIN (attributes) = NULL_TREE;
21129 /* If there is any qualification still in effect, clear it
21130 now; we will be starting fresh with the next declarator. */
21131 parser->scope = NULL_TREE;
21132 parser->qualifying_scope = NULL_TREE;
21133 parser->object_scope = NULL_TREE;
21134 /* If it's a `,', then there are more declarators. */
21135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21137 cp_lexer_consume_token (parser->lexer);
21138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21140 cp_token *token = cp_lexer_previous_token (parser->lexer);
21141 error_at (token->location,
21142 "stray %<,%> at end of member declaration");
21145 /* If the next token isn't a `;', then we have a parse error. */
21146 else if (cp_lexer_next_token_is_not (parser->lexer,
21147 CPP_SEMICOLON))
21149 /* The next token might be a ways away from where the
21150 actual semicolon is missing. Find the previous token
21151 and use that for our error position. */
21152 cp_token *token = cp_lexer_previous_token (parser->lexer);
21153 error_at (token->location,
21154 "expected %<;%> at end of member declaration");
21156 /* Assume that the user meant to provide a semicolon. If
21157 we were to cp_parser_skip_to_end_of_statement, we might
21158 skip to a semicolon inside a member function definition
21159 and issue nonsensical error messages. */
21160 assume_semicolon = true;
21163 if (decl)
21165 /* Add DECL to the list of members. */
21166 if (!friend_p
21167 /* Explicitly include, eg, NSDMIs, for better error
21168 recovery (c++/58650). */
21169 || !DECL_DECLARES_FUNCTION_P (decl))
21170 finish_member_declaration (decl);
21172 if (TREE_CODE (decl) == FUNCTION_DECL)
21173 cp_parser_save_default_args (parser, decl);
21174 else if (TREE_CODE (decl) == FIELD_DECL
21175 && !DECL_C_BIT_FIELD (decl)
21176 && DECL_INITIAL (decl))
21177 /* Add DECL to the queue of NSDMI to be parsed later. */
21178 vec_safe_push (unparsed_nsdmis, decl);
21181 if (assume_semicolon)
21182 goto out;
21186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21187 out:
21188 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21191 /* Parse a pure-specifier.
21193 pure-specifier:
21196 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21197 Otherwise, ERROR_MARK_NODE is returned. */
21199 static tree
21200 cp_parser_pure_specifier (cp_parser* parser)
21202 cp_token *token;
21204 /* Look for the `=' token. */
21205 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21206 return error_mark_node;
21207 /* Look for the `0' token. */
21208 token = cp_lexer_peek_token (parser->lexer);
21210 if (token->type == CPP_EOF
21211 || token->type == CPP_PRAGMA_EOL)
21212 return error_mark_node;
21214 cp_lexer_consume_token (parser->lexer);
21216 /* Accept = default or = delete in c++0x mode. */
21217 if (token->keyword == RID_DEFAULT
21218 || token->keyword == RID_DELETE)
21220 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21221 return token->u.value;
21224 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21225 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21227 cp_parser_error (parser,
21228 "invalid pure specifier (only %<= 0%> is allowed)");
21229 cp_parser_skip_to_end_of_statement (parser);
21230 return error_mark_node;
21232 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21234 error_at (token->location, "templates may not be %<virtual%>");
21235 return error_mark_node;
21238 return integer_zero_node;
21241 /* Parse a constant-initializer.
21243 constant-initializer:
21244 = constant-expression
21246 Returns a representation of the constant-expression. */
21248 static tree
21249 cp_parser_constant_initializer (cp_parser* parser)
21251 /* Look for the `=' token. */
21252 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21253 return error_mark_node;
21255 /* It is invalid to write:
21257 struct S { static const int i = { 7 }; };
21260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21262 cp_parser_error (parser,
21263 "a brace-enclosed initializer is not allowed here");
21264 /* Consume the opening brace. */
21265 cp_lexer_consume_token (parser->lexer);
21266 /* Skip the initializer. */
21267 cp_parser_skip_to_closing_brace (parser);
21268 /* Look for the trailing `}'. */
21269 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21271 return error_mark_node;
21274 return cp_parser_constant_expression (parser);
21277 /* Derived classes [gram.class.derived] */
21279 /* Parse a base-clause.
21281 base-clause:
21282 : base-specifier-list
21284 base-specifier-list:
21285 base-specifier ... [opt]
21286 base-specifier-list , base-specifier ... [opt]
21288 Returns a TREE_LIST representing the base-classes, in the order in
21289 which they were declared. The representation of each node is as
21290 described by cp_parser_base_specifier.
21292 In the case that no bases are specified, this function will return
21293 NULL_TREE, not ERROR_MARK_NODE. */
21295 static tree
21296 cp_parser_base_clause (cp_parser* parser)
21298 tree bases = NULL_TREE;
21300 /* Look for the `:' that begins the list. */
21301 cp_parser_require (parser, CPP_COLON, RT_COLON);
21303 /* Scan the base-specifier-list. */
21304 while (true)
21306 cp_token *token;
21307 tree base;
21308 bool pack_expansion_p = false;
21310 /* Look for the base-specifier. */
21311 base = cp_parser_base_specifier (parser);
21312 /* Look for the (optional) ellipsis. */
21313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21315 /* Consume the `...'. */
21316 cp_lexer_consume_token (parser->lexer);
21318 pack_expansion_p = true;
21321 /* Add BASE to the front of the list. */
21322 if (base && base != error_mark_node)
21324 if (pack_expansion_p)
21325 /* Make this a pack expansion type. */
21326 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21328 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21330 TREE_CHAIN (base) = bases;
21331 bases = base;
21334 /* Peek at the next token. */
21335 token = cp_lexer_peek_token (parser->lexer);
21336 /* If it's not a comma, then the list is complete. */
21337 if (token->type != CPP_COMMA)
21338 break;
21339 /* Consume the `,'. */
21340 cp_lexer_consume_token (parser->lexer);
21343 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21344 base class had a qualified name. However, the next name that
21345 appears is certainly not qualified. */
21346 parser->scope = NULL_TREE;
21347 parser->qualifying_scope = NULL_TREE;
21348 parser->object_scope = NULL_TREE;
21350 return nreverse (bases);
21353 /* Parse a base-specifier.
21355 base-specifier:
21356 :: [opt] nested-name-specifier [opt] class-name
21357 virtual access-specifier [opt] :: [opt] nested-name-specifier
21358 [opt] class-name
21359 access-specifier virtual [opt] :: [opt] nested-name-specifier
21360 [opt] class-name
21362 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21363 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21364 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21365 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21367 static tree
21368 cp_parser_base_specifier (cp_parser* parser)
21370 cp_token *token;
21371 bool done = false;
21372 bool virtual_p = false;
21373 bool duplicate_virtual_error_issued_p = false;
21374 bool duplicate_access_error_issued_p = false;
21375 bool class_scope_p, template_p;
21376 tree access = access_default_node;
21377 tree type;
21379 /* Process the optional `virtual' and `access-specifier'. */
21380 while (!done)
21382 /* Peek at the next token. */
21383 token = cp_lexer_peek_token (parser->lexer);
21384 /* Process `virtual'. */
21385 switch (token->keyword)
21387 case RID_VIRTUAL:
21388 /* If `virtual' appears more than once, issue an error. */
21389 if (virtual_p && !duplicate_virtual_error_issued_p)
21391 cp_parser_error (parser,
21392 "%<virtual%> specified more than once in base-specified");
21393 duplicate_virtual_error_issued_p = true;
21396 virtual_p = true;
21398 /* Consume the `virtual' token. */
21399 cp_lexer_consume_token (parser->lexer);
21401 break;
21403 case RID_PUBLIC:
21404 case RID_PROTECTED:
21405 case RID_PRIVATE:
21406 /* If more than one access specifier appears, issue an
21407 error. */
21408 if (access != access_default_node
21409 && !duplicate_access_error_issued_p)
21411 cp_parser_error (parser,
21412 "more than one access specifier in base-specified");
21413 duplicate_access_error_issued_p = true;
21416 access = ridpointers[(int) token->keyword];
21418 /* Consume the access-specifier. */
21419 cp_lexer_consume_token (parser->lexer);
21421 break;
21423 default:
21424 done = true;
21425 break;
21428 /* It is not uncommon to see programs mechanically, erroneously, use
21429 the 'typename' keyword to denote (dependent) qualified types
21430 as base classes. */
21431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21433 token = cp_lexer_peek_token (parser->lexer);
21434 if (!processing_template_decl)
21435 error_at (token->location,
21436 "keyword %<typename%> not allowed outside of templates");
21437 else
21438 error_at (token->location,
21439 "keyword %<typename%> not allowed in this context "
21440 "(the base class is implicitly a type)");
21441 cp_lexer_consume_token (parser->lexer);
21444 /* Look for the optional `::' operator. */
21445 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21446 /* Look for the nested-name-specifier. The simplest way to
21447 implement:
21449 [temp.res]
21451 The keyword `typename' is not permitted in a base-specifier or
21452 mem-initializer; in these contexts a qualified name that
21453 depends on a template-parameter is implicitly assumed to be a
21454 type name.
21456 is to pretend that we have seen the `typename' keyword at this
21457 point. */
21458 cp_parser_nested_name_specifier_opt (parser,
21459 /*typename_keyword_p=*/true,
21460 /*check_dependency_p=*/true,
21461 typename_type,
21462 /*is_declaration=*/true);
21463 /* If the base class is given by a qualified name, assume that names
21464 we see are type names or templates, as appropriate. */
21465 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21466 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21468 if (!parser->scope
21469 && cp_lexer_next_token_is_decltype (parser->lexer))
21470 /* DR 950 allows decltype as a base-specifier. */
21471 type = cp_parser_decltype (parser);
21472 else
21474 /* Otherwise, look for the class-name. */
21475 type = cp_parser_class_name (parser,
21476 class_scope_p,
21477 template_p,
21478 typename_type,
21479 /*check_dependency_p=*/true,
21480 /*class_head_p=*/false,
21481 /*is_declaration=*/true);
21482 type = TREE_TYPE (type);
21485 if (type == error_mark_node)
21486 return error_mark_node;
21488 return finish_base_specifier (type, access, virtual_p);
21491 /* Exception handling [gram.exception] */
21493 /* Parse an (optional) noexcept-specification.
21495 noexcept-specification:
21496 noexcept ( constant-expression ) [opt]
21498 If no noexcept-specification is present, returns NULL_TREE.
21499 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21500 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21501 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21502 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21503 in which case a boolean condition is returned instead. */
21505 static tree
21506 cp_parser_noexcept_specification_opt (cp_parser* parser,
21507 bool require_constexpr,
21508 bool* consumed_expr,
21509 bool return_cond)
21511 cp_token *token;
21512 const char *saved_message;
21514 /* Peek at the next token. */
21515 token = cp_lexer_peek_token (parser->lexer);
21517 /* Is it a noexcept-specification? */
21518 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21520 tree expr;
21521 cp_lexer_consume_token (parser->lexer);
21523 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21525 cp_lexer_consume_token (parser->lexer);
21527 if (require_constexpr)
21529 /* Types may not be defined in an exception-specification. */
21530 saved_message = parser->type_definition_forbidden_message;
21531 parser->type_definition_forbidden_message
21532 = G_("types may not be defined in an exception-specification");
21534 expr = cp_parser_constant_expression (parser);
21535 /* Restore the saved message. */
21536 parser->type_definition_forbidden_message = saved_message;
21538 else
21540 expr = cp_parser_expression (parser);
21541 *consumed_expr = true;
21544 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21546 else
21548 expr = boolean_true_node;
21549 if (!require_constexpr)
21550 *consumed_expr = false;
21553 /* We cannot build a noexcept-spec right away because this will check
21554 that expr is a constexpr. */
21555 if (!return_cond)
21556 return build_noexcept_spec (expr, tf_warning_or_error);
21557 else
21558 return expr;
21560 else
21561 return NULL_TREE;
21564 /* Parse an (optional) exception-specification.
21566 exception-specification:
21567 throw ( type-id-list [opt] )
21569 Returns a TREE_LIST representing the exception-specification. The
21570 TREE_VALUE of each node is a type. */
21572 static tree
21573 cp_parser_exception_specification_opt (cp_parser* parser)
21575 cp_token *token;
21576 tree type_id_list;
21577 const char *saved_message;
21579 /* Peek at the next token. */
21580 token = cp_lexer_peek_token (parser->lexer);
21582 /* Is it a noexcept-specification? */
21583 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21584 false);
21585 if (type_id_list != NULL_TREE)
21586 return type_id_list;
21588 /* If it's not `throw', then there's no exception-specification. */
21589 if (!cp_parser_is_keyword (token, RID_THROW))
21590 return NULL_TREE;
21592 #if 0
21593 /* Enable this once a lot of code has transitioned to noexcept? */
21594 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21595 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21596 "deprecated in C++0x; use %<noexcept%> instead");
21597 #endif
21599 /* Consume the `throw'. */
21600 cp_lexer_consume_token (parser->lexer);
21602 /* Look for the `('. */
21603 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21605 /* Peek at the next token. */
21606 token = cp_lexer_peek_token (parser->lexer);
21607 /* If it's not a `)', then there is a type-id-list. */
21608 if (token->type != CPP_CLOSE_PAREN)
21610 /* Types may not be defined in an exception-specification. */
21611 saved_message = parser->type_definition_forbidden_message;
21612 parser->type_definition_forbidden_message
21613 = G_("types may not be defined in an exception-specification");
21614 /* Parse the type-id-list. */
21615 type_id_list = cp_parser_type_id_list (parser);
21616 /* Restore the saved message. */
21617 parser->type_definition_forbidden_message = saved_message;
21619 else
21620 type_id_list = empty_except_spec;
21622 /* Look for the `)'. */
21623 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21625 return type_id_list;
21628 /* Parse an (optional) type-id-list.
21630 type-id-list:
21631 type-id ... [opt]
21632 type-id-list , type-id ... [opt]
21634 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21635 in the order that the types were presented. */
21637 static tree
21638 cp_parser_type_id_list (cp_parser* parser)
21640 tree types = NULL_TREE;
21642 while (true)
21644 cp_token *token;
21645 tree type;
21647 /* Get the next type-id. */
21648 type = cp_parser_type_id (parser);
21649 /* Parse the optional ellipsis. */
21650 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21652 /* Consume the `...'. */
21653 cp_lexer_consume_token (parser->lexer);
21655 /* Turn the type into a pack expansion expression. */
21656 type = make_pack_expansion (type);
21658 /* Add it to the list. */
21659 types = add_exception_specifier (types, type, /*complain=*/1);
21660 /* Peek at the next token. */
21661 token = cp_lexer_peek_token (parser->lexer);
21662 /* If it is not a `,', we are done. */
21663 if (token->type != CPP_COMMA)
21664 break;
21665 /* Consume the `,'. */
21666 cp_lexer_consume_token (parser->lexer);
21669 return nreverse (types);
21672 /* Parse a try-block.
21674 try-block:
21675 try compound-statement handler-seq */
21677 static tree
21678 cp_parser_try_block (cp_parser* parser)
21680 tree try_block;
21682 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21683 if (parser->in_function_body
21684 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21685 error ("%<try%> in %<constexpr%> function");
21687 try_block = begin_try_block ();
21688 cp_parser_compound_statement (parser, NULL, true, false);
21689 finish_try_block (try_block);
21690 cp_parser_handler_seq (parser);
21691 finish_handler_sequence (try_block);
21693 return try_block;
21696 /* Parse a function-try-block.
21698 function-try-block:
21699 try ctor-initializer [opt] function-body handler-seq */
21701 static bool
21702 cp_parser_function_try_block (cp_parser* parser)
21704 tree compound_stmt;
21705 tree try_block;
21706 bool ctor_initializer_p;
21708 /* Look for the `try' keyword. */
21709 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21710 return false;
21711 /* Let the rest of the front end know where we are. */
21712 try_block = begin_function_try_block (&compound_stmt);
21713 /* Parse the function-body. */
21714 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21715 (parser, /*in_function_try_block=*/true);
21716 /* We're done with the `try' part. */
21717 finish_function_try_block (try_block);
21718 /* Parse the handlers. */
21719 cp_parser_handler_seq (parser);
21720 /* We're done with the handlers. */
21721 finish_function_handler_sequence (try_block, compound_stmt);
21723 return ctor_initializer_p;
21726 /* Parse a handler-seq.
21728 handler-seq:
21729 handler handler-seq [opt] */
21731 static void
21732 cp_parser_handler_seq (cp_parser* parser)
21734 while (true)
21736 cp_token *token;
21738 /* Parse the handler. */
21739 cp_parser_handler (parser);
21740 /* Peek at the next token. */
21741 token = cp_lexer_peek_token (parser->lexer);
21742 /* If it's not `catch' then there are no more handlers. */
21743 if (!cp_parser_is_keyword (token, RID_CATCH))
21744 break;
21748 /* Parse a handler.
21750 handler:
21751 catch ( exception-declaration ) compound-statement */
21753 static void
21754 cp_parser_handler (cp_parser* parser)
21756 tree handler;
21757 tree declaration;
21759 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21760 handler = begin_handler ();
21761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21762 declaration = cp_parser_exception_declaration (parser);
21763 finish_handler_parms (declaration, handler);
21764 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21765 cp_parser_compound_statement (parser, NULL, false, false);
21766 finish_handler (handler);
21769 /* Parse an exception-declaration.
21771 exception-declaration:
21772 type-specifier-seq declarator
21773 type-specifier-seq abstract-declarator
21774 type-specifier-seq
21777 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21778 ellipsis variant is used. */
21780 static tree
21781 cp_parser_exception_declaration (cp_parser* parser)
21783 cp_decl_specifier_seq type_specifiers;
21784 cp_declarator *declarator;
21785 const char *saved_message;
21787 /* If it's an ellipsis, it's easy to handle. */
21788 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21790 /* Consume the `...' token. */
21791 cp_lexer_consume_token (parser->lexer);
21792 return NULL_TREE;
21795 /* Types may not be defined in exception-declarations. */
21796 saved_message = parser->type_definition_forbidden_message;
21797 parser->type_definition_forbidden_message
21798 = G_("types may not be defined in exception-declarations");
21800 /* Parse the type-specifier-seq. */
21801 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21802 /*is_trailing_return=*/false,
21803 &type_specifiers);
21804 /* If it's a `)', then there is no declarator. */
21805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21806 declarator = NULL;
21807 else
21808 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21809 /*ctor_dtor_or_conv_p=*/NULL,
21810 /*parenthesized_p=*/NULL,
21811 /*member_p=*/false,
21812 /*friend_p=*/false);
21814 /* Restore the saved message. */
21815 parser->type_definition_forbidden_message = saved_message;
21817 if (!type_specifiers.any_specifiers_p)
21818 return error_mark_node;
21820 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21823 /* Parse a throw-expression.
21825 throw-expression:
21826 throw assignment-expression [opt]
21828 Returns a THROW_EXPR representing the throw-expression. */
21830 static tree
21831 cp_parser_throw_expression (cp_parser* parser)
21833 tree expression;
21834 cp_token* token;
21836 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21837 token = cp_lexer_peek_token (parser->lexer);
21838 /* Figure out whether or not there is an assignment-expression
21839 following the "throw" keyword. */
21840 if (token->type == CPP_COMMA
21841 || token->type == CPP_SEMICOLON
21842 || token->type == CPP_CLOSE_PAREN
21843 || token->type == CPP_CLOSE_SQUARE
21844 || token->type == CPP_CLOSE_BRACE
21845 || token->type == CPP_COLON)
21846 expression = NULL_TREE;
21847 else
21848 expression = cp_parser_assignment_expression (parser);
21850 return build_throw (expression);
21853 /* GNU Extensions */
21855 /* Parse an (optional) asm-specification.
21857 asm-specification:
21858 asm ( string-literal )
21860 If the asm-specification is present, returns a STRING_CST
21861 corresponding to the string-literal. Otherwise, returns
21862 NULL_TREE. */
21864 static tree
21865 cp_parser_asm_specification_opt (cp_parser* parser)
21867 cp_token *token;
21868 tree asm_specification;
21870 /* Peek at the next token. */
21871 token = cp_lexer_peek_token (parser->lexer);
21872 /* If the next token isn't the `asm' keyword, then there's no
21873 asm-specification. */
21874 if (!cp_parser_is_keyword (token, RID_ASM))
21875 return NULL_TREE;
21877 /* Consume the `asm' token. */
21878 cp_lexer_consume_token (parser->lexer);
21879 /* Look for the `('. */
21880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21882 /* Look for the string-literal. */
21883 asm_specification = cp_parser_string_literal (parser, false, false);
21885 /* Look for the `)'. */
21886 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21888 return asm_specification;
21891 /* Parse an asm-operand-list.
21893 asm-operand-list:
21894 asm-operand
21895 asm-operand-list , asm-operand
21897 asm-operand:
21898 string-literal ( expression )
21899 [ string-literal ] string-literal ( expression )
21901 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21902 each node is the expression. The TREE_PURPOSE is itself a
21903 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21904 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21905 is a STRING_CST for the string literal before the parenthesis. Returns
21906 ERROR_MARK_NODE if any of the operands are invalid. */
21908 static tree
21909 cp_parser_asm_operand_list (cp_parser* parser)
21911 tree asm_operands = NULL_TREE;
21912 bool invalid_operands = false;
21914 while (true)
21916 tree string_literal;
21917 tree expression;
21918 tree name;
21920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21922 /* Consume the `[' token. */
21923 cp_lexer_consume_token (parser->lexer);
21924 /* Read the operand name. */
21925 name = cp_parser_identifier (parser);
21926 if (name != error_mark_node)
21927 name = build_string (IDENTIFIER_LENGTH (name),
21928 IDENTIFIER_POINTER (name));
21929 /* Look for the closing `]'. */
21930 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21932 else
21933 name = NULL_TREE;
21934 /* Look for the string-literal. */
21935 string_literal = cp_parser_string_literal (parser, false, false);
21937 /* Look for the `('. */
21938 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21939 /* Parse the expression. */
21940 expression = cp_parser_expression (parser);
21941 /* Look for the `)'. */
21942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21944 if (name == error_mark_node
21945 || string_literal == error_mark_node
21946 || expression == error_mark_node)
21947 invalid_operands = true;
21949 /* Add this operand to the list. */
21950 asm_operands = tree_cons (build_tree_list (name, string_literal),
21951 expression,
21952 asm_operands);
21953 /* If the next token is not a `,', there are no more
21954 operands. */
21955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21956 break;
21957 /* Consume the `,'. */
21958 cp_lexer_consume_token (parser->lexer);
21961 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21964 /* Parse an asm-clobber-list.
21966 asm-clobber-list:
21967 string-literal
21968 asm-clobber-list , string-literal
21970 Returns a TREE_LIST, indicating the clobbers in the order that they
21971 appeared. The TREE_VALUE of each node is a STRING_CST. */
21973 static tree
21974 cp_parser_asm_clobber_list (cp_parser* parser)
21976 tree clobbers = NULL_TREE;
21978 while (true)
21980 tree string_literal;
21982 /* Look for the string literal. */
21983 string_literal = cp_parser_string_literal (parser, false, false);
21984 /* Add it to the list. */
21985 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21986 /* If the next token is not a `,', then the list is
21987 complete. */
21988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21989 break;
21990 /* Consume the `,' token. */
21991 cp_lexer_consume_token (parser->lexer);
21994 return clobbers;
21997 /* Parse an asm-label-list.
21999 asm-label-list:
22000 identifier
22001 asm-label-list , identifier
22003 Returns a TREE_LIST, indicating the labels in the order that they
22004 appeared. The TREE_VALUE of each node is a label. */
22006 static tree
22007 cp_parser_asm_label_list (cp_parser* parser)
22009 tree labels = NULL_TREE;
22011 while (true)
22013 tree identifier, label, name;
22015 /* Look for the identifier. */
22016 identifier = cp_parser_identifier (parser);
22017 if (!error_operand_p (identifier))
22019 label = lookup_label (identifier);
22020 if (TREE_CODE (label) == LABEL_DECL)
22022 TREE_USED (label) = 1;
22023 check_goto (label);
22024 name = build_string (IDENTIFIER_LENGTH (identifier),
22025 IDENTIFIER_POINTER (identifier));
22026 labels = tree_cons (name, label, labels);
22029 /* If the next token is not a `,', then the list is
22030 complete. */
22031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22032 break;
22033 /* Consume the `,' token. */
22034 cp_lexer_consume_token (parser->lexer);
22037 return nreverse (labels);
22040 /* Return TRUE iff the next tokens in the stream are possibly the
22041 beginning of a GNU extension attribute. */
22043 static bool
22044 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22046 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22049 /* Return TRUE iff the next tokens in the stream are possibly the
22050 beginning of a standard C++-11 attribute specifier. */
22052 static bool
22053 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22055 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22058 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22059 beginning of a standard C++-11 attribute specifier. */
22061 static bool
22062 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22064 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22066 return (cxx_dialect >= cxx11
22067 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22068 || (token->type == CPP_OPEN_SQUARE
22069 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22070 && token->type == CPP_OPEN_SQUARE)));
22073 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22074 beginning of a GNU extension attribute. */
22076 static bool
22077 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22079 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22081 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22084 /* Return true iff the next tokens can be the beginning of either a
22085 GNU attribute list, or a standard C++11 attribute sequence. */
22087 static bool
22088 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22090 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22091 || cp_next_tokens_can_be_std_attribute_p (parser));
22094 /* Return true iff the next Nth tokens can be the beginning of either
22095 a GNU attribute list, or a standard C++11 attribute sequence. */
22097 static bool
22098 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22100 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22101 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22104 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22105 of GNU attributes, or return NULL. */
22107 static tree
22108 cp_parser_attributes_opt (cp_parser *parser)
22110 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22111 return cp_parser_gnu_attributes_opt (parser);
22112 return cp_parser_std_attribute_spec_seq (parser);
22115 #define CILK_SIMD_FN_CLAUSE_MASK \
22116 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22117 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22118 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22119 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22120 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22122 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22123 vector [(<clauses>)] */
22125 static void
22126 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22128 bool first_p = parser->cilk_simd_fn_info == NULL;
22129 cp_token *token = v_token;
22130 if (first_p)
22132 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22133 parser->cilk_simd_fn_info->error_seen = false;
22134 parser->cilk_simd_fn_info->fndecl_seen = false;
22135 parser->cilk_simd_fn_info->tokens = vNULL;
22137 int paren_scope = 0;
22138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22140 cp_lexer_consume_token (parser->lexer);
22141 v_token = cp_lexer_peek_token (parser->lexer);
22142 paren_scope++;
22144 while (paren_scope > 0)
22146 token = cp_lexer_peek_token (parser->lexer);
22147 if (token->type == CPP_OPEN_PAREN)
22148 paren_scope++;
22149 else if (token->type == CPP_CLOSE_PAREN)
22150 paren_scope--;
22151 /* Do not push the last ')' */
22152 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22153 cp_lexer_consume_token (parser->lexer);
22156 token->type = CPP_PRAGMA_EOL;
22157 parser->lexer->next_token = token;
22158 cp_lexer_consume_token (parser->lexer);
22160 struct cp_token_cache *cp
22161 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22162 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22165 /* Parse an (optional) series of attributes.
22167 attributes:
22168 attributes attribute
22170 attribute:
22171 __attribute__ (( attribute-list [opt] ))
22173 The return value is as for cp_parser_gnu_attribute_list. */
22175 static tree
22176 cp_parser_gnu_attributes_opt (cp_parser* parser)
22178 tree attributes = NULL_TREE;
22180 while (true)
22182 cp_token *token;
22183 tree attribute_list;
22184 bool ok = true;
22186 /* Peek at the next token. */
22187 token = cp_lexer_peek_token (parser->lexer);
22188 /* If it's not `__attribute__', then we're done. */
22189 if (token->keyword != RID_ATTRIBUTE)
22190 break;
22192 /* Consume the `__attribute__' keyword. */
22193 cp_lexer_consume_token (parser->lexer);
22194 /* Look for the two `(' tokens. */
22195 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22196 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22198 /* Peek at the next token. */
22199 token = cp_lexer_peek_token (parser->lexer);
22200 if (token->type != CPP_CLOSE_PAREN)
22201 /* Parse the attribute-list. */
22202 attribute_list = cp_parser_gnu_attribute_list (parser);
22203 else
22204 /* If the next token is a `)', then there is no attribute
22205 list. */
22206 attribute_list = NULL;
22208 /* Look for the two `)' tokens. */
22209 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22210 ok = false;
22211 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22212 ok = false;
22213 if (!ok)
22214 cp_parser_skip_to_end_of_statement (parser);
22216 /* Add these new attributes to the list. */
22217 attributes = chainon (attributes, attribute_list);
22220 return attributes;
22223 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22224 "__vector" or "__vector__." */
22226 static inline bool
22227 is_cilkplus_vector_p (tree name)
22229 if (flag_cilkplus && is_attribute_p ("vector", name))
22230 return true;
22231 return false;
22234 /* Parse a GNU attribute-list.
22236 attribute-list:
22237 attribute
22238 attribute-list , attribute
22240 attribute:
22241 identifier
22242 identifier ( identifier )
22243 identifier ( identifier , expression-list )
22244 identifier ( expression-list )
22246 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22247 to an attribute. The TREE_PURPOSE of each node is the identifier
22248 indicating which attribute is in use. The TREE_VALUE represents
22249 the arguments, if any. */
22251 static tree
22252 cp_parser_gnu_attribute_list (cp_parser* parser)
22254 tree attribute_list = NULL_TREE;
22255 bool save_translate_strings_p = parser->translate_strings_p;
22257 parser->translate_strings_p = false;
22258 while (true)
22260 cp_token *token;
22261 tree identifier;
22262 tree attribute;
22264 /* Look for the identifier. We also allow keywords here; for
22265 example `__attribute__ ((const))' is legal. */
22266 token = cp_lexer_peek_token (parser->lexer);
22267 if (token->type == CPP_NAME
22268 || token->type == CPP_KEYWORD)
22270 tree arguments = NULL_TREE;
22272 /* Consume the token, but save it since we need it for the
22273 SIMD enabled function parsing. */
22274 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22276 /* Save away the identifier that indicates which attribute
22277 this is. */
22278 identifier = (token->type == CPP_KEYWORD)
22279 /* For keywords, use the canonical spelling, not the
22280 parsed identifier. */
22281 ? ridpointers[(int) token->keyword]
22282 : id_token->u.value;
22284 attribute = build_tree_list (identifier, NULL_TREE);
22286 /* Peek at the next token. */
22287 token = cp_lexer_peek_token (parser->lexer);
22288 /* If it's an `(', then parse the attribute arguments. */
22289 if (token->type == CPP_OPEN_PAREN)
22291 vec<tree, va_gc> *vec;
22292 int attr_flag = (attribute_takes_identifier_p (identifier)
22293 ? id_attr : normal_attr);
22294 if (is_cilkplus_vector_p (identifier))
22296 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22297 continue;
22299 else
22300 vec = cp_parser_parenthesized_expression_list
22301 (parser, attr_flag, /*cast_p=*/false,
22302 /*allow_expansion_p=*/false,
22303 /*non_constant_p=*/NULL);
22304 if (vec == NULL)
22305 arguments = error_mark_node;
22306 else
22308 arguments = build_tree_list_vec (vec);
22309 release_tree_vector (vec);
22311 /* Save the arguments away. */
22312 TREE_VALUE (attribute) = arguments;
22314 else if (is_cilkplus_vector_p (identifier))
22316 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22317 continue;
22320 if (arguments != error_mark_node)
22322 /* Add this attribute to the list. */
22323 TREE_CHAIN (attribute) = attribute_list;
22324 attribute_list = attribute;
22327 token = cp_lexer_peek_token (parser->lexer);
22329 /* Now, look for more attributes. If the next token isn't a
22330 `,', we're done. */
22331 if (token->type != CPP_COMMA)
22332 break;
22334 /* Consume the comma and keep going. */
22335 cp_lexer_consume_token (parser->lexer);
22337 parser->translate_strings_p = save_translate_strings_p;
22339 /* We built up the list in reverse order. */
22340 return nreverse (attribute_list);
22343 /* Parse a standard C++11 attribute.
22345 The returned representation is a TREE_LIST which TREE_PURPOSE is
22346 the scoped name of the attribute, and the TREE_VALUE is its
22347 arguments list.
22349 Note that the scoped name of the attribute is itself a TREE_LIST
22350 which TREE_PURPOSE is the namespace of the attribute, and
22351 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22352 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22353 and which TREE_PURPOSE is directly the attribute name.
22355 Clients of the attribute code should use get_attribute_namespace
22356 and get_attribute_name to get the actual namespace and name of
22357 attributes, regardless of their being GNU or C++11 attributes.
22359 attribute:
22360 attribute-token attribute-argument-clause [opt]
22362 attribute-token:
22363 identifier
22364 attribute-scoped-token
22366 attribute-scoped-token:
22367 attribute-namespace :: identifier
22369 attribute-namespace:
22370 identifier
22372 attribute-argument-clause:
22373 ( balanced-token-seq )
22375 balanced-token-seq:
22376 balanced-token [opt]
22377 balanced-token-seq balanced-token
22379 balanced-token:
22380 ( balanced-token-seq )
22381 [ balanced-token-seq ]
22382 { balanced-token-seq }. */
22384 static tree
22385 cp_parser_std_attribute (cp_parser *parser)
22387 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22388 cp_token *token;
22390 /* First, parse name of the the attribute, a.k.a
22391 attribute-token. */
22393 token = cp_lexer_peek_token (parser->lexer);
22394 if (token->type == CPP_NAME)
22395 attr_id = token->u.value;
22396 else if (token->type == CPP_KEYWORD)
22397 attr_id = ridpointers[(int) token->keyword];
22398 else if (token->flags & NAMED_OP)
22399 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22401 if (attr_id == NULL_TREE)
22402 return NULL_TREE;
22404 cp_lexer_consume_token (parser->lexer);
22406 token = cp_lexer_peek_token (parser->lexer);
22407 if (token->type == CPP_SCOPE)
22409 /* We are seeing a scoped attribute token. */
22411 cp_lexer_consume_token (parser->lexer);
22412 attr_ns = attr_id;
22414 token = cp_lexer_consume_token (parser->lexer);
22415 if (token->type == CPP_NAME)
22416 attr_id = token->u.value;
22417 else if (token->type == CPP_KEYWORD)
22418 attr_id = ridpointers[(int) token->keyword];
22419 else
22421 error_at (token->location,
22422 "expected an identifier for the attribute name");
22423 return error_mark_node;
22425 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22426 NULL_TREE);
22427 token = cp_lexer_peek_token (parser->lexer);
22429 else
22431 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22432 NULL_TREE);
22433 /* C++11 noreturn attribute is equivalent to GNU's. */
22434 if (is_attribute_p ("noreturn", attr_id))
22435 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22436 /* C++14 deprecated attribute is equivalent to GNU's. */
22437 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22439 if (cxx_dialect == cxx11)
22440 pedwarn (token->location, OPT_Wpedantic,
22441 "%<deprecated%> is a C++14 feature;"
22442 " use %<gnu::deprecated%>");
22443 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22447 /* Now parse the optional argument clause of the attribute. */
22449 if (token->type != CPP_OPEN_PAREN)
22450 return attribute;
22453 vec<tree, va_gc> *vec;
22454 int attr_flag = normal_attr;
22456 if (attr_ns == get_identifier ("gnu")
22457 && attribute_takes_identifier_p (attr_id))
22458 /* A GNU attribute that takes an identifier in parameter. */
22459 attr_flag = id_attr;
22461 vec = cp_parser_parenthesized_expression_list
22462 (parser, attr_flag, /*cast_p=*/false,
22463 /*allow_expansion_p=*/true,
22464 /*non_constant_p=*/NULL);
22465 if (vec == NULL)
22466 arguments = error_mark_node;
22467 else
22469 arguments = build_tree_list_vec (vec);
22470 release_tree_vector (vec);
22473 if (arguments == error_mark_node)
22474 attribute = error_mark_node;
22475 else
22476 TREE_VALUE (attribute) = arguments;
22479 return attribute;
22482 /* Parse a list of standard C++-11 attributes.
22484 attribute-list:
22485 attribute [opt]
22486 attribute-list , attribute[opt]
22487 attribute ...
22488 attribute-list , attribute ...
22491 static tree
22492 cp_parser_std_attribute_list (cp_parser *parser)
22494 tree attributes = NULL_TREE, attribute = NULL_TREE;
22495 cp_token *token = NULL;
22497 while (true)
22499 attribute = cp_parser_std_attribute (parser);
22500 if (attribute == error_mark_node)
22501 break;
22502 if (attribute != NULL_TREE)
22504 TREE_CHAIN (attribute) = attributes;
22505 attributes = attribute;
22507 token = cp_lexer_peek_token (parser->lexer);
22508 if (token->type == CPP_ELLIPSIS)
22510 cp_lexer_consume_token (parser->lexer);
22511 TREE_VALUE (attribute)
22512 = make_pack_expansion (TREE_VALUE (attribute));
22513 token = cp_lexer_peek_token (parser->lexer);
22515 if (token->type != CPP_COMMA)
22516 break;
22517 cp_lexer_consume_token (parser->lexer);
22519 attributes = nreverse (attributes);
22520 return attributes;
22523 /* Parse a standard C++-11 attribute specifier.
22525 attribute-specifier:
22526 [ [ attribute-list ] ]
22527 alignment-specifier
22529 alignment-specifier:
22530 alignas ( type-id ... [opt] )
22531 alignas ( alignment-expression ... [opt] ). */
22533 static tree
22534 cp_parser_std_attribute_spec (cp_parser *parser)
22536 tree attributes = NULL_TREE;
22537 cp_token *token = cp_lexer_peek_token (parser->lexer);
22539 if (token->type == CPP_OPEN_SQUARE
22540 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22542 cp_lexer_consume_token (parser->lexer);
22543 cp_lexer_consume_token (parser->lexer);
22545 attributes = cp_parser_std_attribute_list (parser);
22547 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22548 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22549 cp_parser_skip_to_end_of_statement (parser);
22550 else
22551 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22552 when we are sure that we have actually parsed them. */
22553 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22555 else
22557 tree alignas_expr;
22559 /* Look for an alignment-specifier. */
22561 token = cp_lexer_peek_token (parser->lexer);
22563 if (token->type != CPP_KEYWORD
22564 || token->keyword != RID_ALIGNAS)
22565 return NULL_TREE;
22567 cp_lexer_consume_token (parser->lexer);
22568 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22570 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22572 cp_parser_error (parser, "expected %<(%>");
22573 return error_mark_node;
22576 cp_parser_parse_tentatively (parser);
22577 alignas_expr = cp_parser_type_id (parser);
22579 if (!cp_parser_parse_definitely (parser))
22581 gcc_assert (alignas_expr == error_mark_node
22582 || alignas_expr == NULL_TREE);
22584 alignas_expr =
22585 cp_parser_assignment_expression (parser);
22586 if (alignas_expr == error_mark_node)
22587 cp_parser_skip_to_end_of_statement (parser);
22588 if (alignas_expr == NULL_TREE
22589 || alignas_expr == error_mark_node)
22590 return alignas_expr;
22593 alignas_expr = cxx_alignas_expr (alignas_expr);
22594 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22598 cp_lexer_consume_token (parser->lexer);
22599 alignas_expr = make_pack_expansion (alignas_expr);
22602 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22604 cp_parser_error (parser, "expected %<)%>");
22605 return error_mark_node;
22608 /* Build the C++-11 representation of an 'aligned'
22609 attribute. */
22610 attributes =
22611 build_tree_list (build_tree_list (get_identifier ("gnu"),
22612 get_identifier ("aligned")),
22613 alignas_expr);
22616 return attributes;
22619 /* Parse a standard C++-11 attribute-specifier-seq.
22621 attribute-specifier-seq:
22622 attribute-specifier-seq [opt] attribute-specifier
22625 static tree
22626 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22628 tree attr_specs = NULL;
22630 while (true)
22632 tree attr_spec = cp_parser_std_attribute_spec (parser);
22633 if (attr_spec == NULL_TREE)
22634 break;
22635 if (attr_spec == error_mark_node)
22636 return error_mark_node;
22638 TREE_CHAIN (attr_spec) = attr_specs;
22639 attr_specs = attr_spec;
22642 attr_specs = nreverse (attr_specs);
22643 return attr_specs;
22646 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22647 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22648 current value of the PEDANTIC flag, regardless of whether or not
22649 the `__extension__' keyword is present. The caller is responsible
22650 for restoring the value of the PEDANTIC flag. */
22652 static bool
22653 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22655 /* Save the old value of the PEDANTIC flag. */
22656 *saved_pedantic = pedantic;
22658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22660 /* Consume the `__extension__' token. */
22661 cp_lexer_consume_token (parser->lexer);
22662 /* We're not being pedantic while the `__extension__' keyword is
22663 in effect. */
22664 pedantic = 0;
22666 return true;
22669 return false;
22672 /* Parse a label declaration.
22674 label-declaration:
22675 __label__ label-declarator-seq ;
22677 label-declarator-seq:
22678 identifier , label-declarator-seq
22679 identifier */
22681 static void
22682 cp_parser_label_declaration (cp_parser* parser)
22684 /* Look for the `__label__' keyword. */
22685 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22687 while (true)
22689 tree identifier;
22691 /* Look for an identifier. */
22692 identifier = cp_parser_identifier (parser);
22693 /* If we failed, stop. */
22694 if (identifier == error_mark_node)
22695 break;
22696 /* Declare it as a label. */
22697 finish_label_decl (identifier);
22698 /* If the next token is a `;', stop. */
22699 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22700 break;
22701 /* Look for the `,' separating the label declarations. */
22702 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22705 /* Look for the final `;'. */
22706 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22709 /* Support Functions */
22711 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22712 NAME should have one of the representations used for an
22713 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22714 is returned. If PARSER->SCOPE is a dependent type, then a
22715 SCOPE_REF is returned.
22717 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22718 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22719 was formed. Abstractly, such entities should not be passed to this
22720 function, because they do not need to be looked up, but it is
22721 simpler to check for this special case here, rather than at the
22722 call-sites.
22724 In cases not explicitly covered above, this function returns a
22725 DECL, OVERLOAD, or baselink representing the result of the lookup.
22726 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22727 is returned.
22729 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22730 (e.g., "struct") that was used. In that case bindings that do not
22731 refer to types are ignored.
22733 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22734 ignored.
22736 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22737 are ignored.
22739 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22740 types.
22742 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22743 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22744 NULL_TREE otherwise. */
22746 static tree
22747 cp_parser_lookup_name (cp_parser *parser, tree name,
22748 enum tag_types tag_type,
22749 bool is_template,
22750 bool is_namespace,
22751 bool check_dependency,
22752 tree *ambiguous_decls,
22753 location_t name_location)
22755 tree decl;
22756 tree object_type = parser->context->object_type;
22758 /* Assume that the lookup will be unambiguous. */
22759 if (ambiguous_decls)
22760 *ambiguous_decls = NULL_TREE;
22762 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22763 no longer valid. Note that if we are parsing tentatively, and
22764 the parse fails, OBJECT_TYPE will be automatically restored. */
22765 parser->context->object_type = NULL_TREE;
22767 if (name == error_mark_node)
22768 return error_mark_node;
22770 /* A template-id has already been resolved; there is no lookup to
22771 do. */
22772 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22773 return name;
22774 if (BASELINK_P (name))
22776 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22777 == TEMPLATE_ID_EXPR);
22778 return name;
22781 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22782 it should already have been checked to make sure that the name
22783 used matches the type being destroyed. */
22784 if (TREE_CODE (name) == BIT_NOT_EXPR)
22786 tree type;
22788 /* Figure out to which type this destructor applies. */
22789 if (parser->scope)
22790 type = parser->scope;
22791 else if (object_type)
22792 type = object_type;
22793 else
22794 type = current_class_type;
22795 /* If that's not a class type, there is no destructor. */
22796 if (!type || !CLASS_TYPE_P (type))
22797 return error_mark_node;
22798 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22799 lazily_declare_fn (sfk_destructor, type);
22800 if (!CLASSTYPE_DESTRUCTORS (type))
22801 return error_mark_node;
22802 /* If it was a class type, return the destructor. */
22803 return CLASSTYPE_DESTRUCTORS (type);
22806 /* By this point, the NAME should be an ordinary identifier. If
22807 the id-expression was a qualified name, the qualifying scope is
22808 stored in PARSER->SCOPE at this point. */
22809 gcc_assert (identifier_p (name));
22811 /* Perform the lookup. */
22812 if (parser->scope)
22814 bool dependent_p;
22816 if (parser->scope == error_mark_node)
22817 return error_mark_node;
22819 /* If the SCOPE is dependent, the lookup must be deferred until
22820 the template is instantiated -- unless we are explicitly
22821 looking up names in uninstantiated templates. Even then, we
22822 cannot look up the name if the scope is not a class type; it
22823 might, for example, be a template type parameter. */
22824 dependent_p = (TYPE_P (parser->scope)
22825 && dependent_scope_p (parser->scope));
22826 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22827 && dependent_p)
22828 /* Defer lookup. */
22829 decl = error_mark_node;
22830 else
22832 tree pushed_scope = NULL_TREE;
22834 /* If PARSER->SCOPE is a dependent type, then it must be a
22835 class type, and we must not be checking dependencies;
22836 otherwise, we would have processed this lookup above. So
22837 that PARSER->SCOPE is not considered a dependent base by
22838 lookup_member, we must enter the scope here. */
22839 if (dependent_p)
22840 pushed_scope = push_scope (parser->scope);
22842 /* If the PARSER->SCOPE is a template specialization, it
22843 may be instantiated during name lookup. In that case,
22844 errors may be issued. Even if we rollback the current
22845 tentative parse, those errors are valid. */
22846 decl = lookup_qualified_name (parser->scope, name,
22847 tag_type != none_type,
22848 /*complain=*/true);
22850 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22851 lookup result and the nested-name-specifier nominates a class C:
22852 * if the name specified after the nested-name-specifier, when
22853 looked up in C, is the injected-class-name of C (Clause 9), or
22854 * if the name specified after the nested-name-specifier is the
22855 same as the identifier or the simple-template-id's template-
22856 name in the last component of the nested-name-specifier,
22857 the name is instead considered to name the constructor of
22858 class C. [ Note: for example, the constructor is not an
22859 acceptable lookup result in an elaborated-type-specifier so
22860 the constructor would not be used in place of the
22861 injected-class-name. --end note ] Such a constructor name
22862 shall be used only in the declarator-id of a declaration that
22863 names a constructor or in a using-declaration. */
22864 if (tag_type == none_type
22865 && DECL_SELF_REFERENCE_P (decl)
22866 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22867 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22868 tag_type != none_type,
22869 /*complain=*/true);
22871 /* If we have a single function from a using decl, pull it out. */
22872 if (TREE_CODE (decl) == OVERLOAD
22873 && !really_overloaded_fn (decl))
22874 decl = OVL_FUNCTION (decl);
22876 if (pushed_scope)
22877 pop_scope (pushed_scope);
22880 /* If the scope is a dependent type and either we deferred lookup or
22881 we did lookup but didn't find the name, rememeber the name. */
22882 if (decl == error_mark_node && TYPE_P (parser->scope)
22883 && dependent_type_p (parser->scope))
22885 if (tag_type)
22887 tree type;
22889 /* The resolution to Core Issue 180 says that `struct
22890 A::B' should be considered a type-name, even if `A'
22891 is dependent. */
22892 type = make_typename_type (parser->scope, name, tag_type,
22893 /*complain=*/tf_error);
22894 if (type != error_mark_node)
22895 decl = TYPE_NAME (type);
22897 else if (is_template
22898 && (cp_parser_next_token_ends_template_argument_p (parser)
22899 || cp_lexer_next_token_is (parser->lexer,
22900 CPP_CLOSE_PAREN)))
22901 decl = make_unbound_class_template (parser->scope,
22902 name, NULL_TREE,
22903 /*complain=*/tf_error);
22904 else
22905 decl = build_qualified_name (/*type=*/NULL_TREE,
22906 parser->scope, name,
22907 is_template);
22909 parser->qualifying_scope = parser->scope;
22910 parser->object_scope = NULL_TREE;
22912 else if (object_type)
22914 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22915 OBJECT_TYPE is not a class. */
22916 if (CLASS_TYPE_P (object_type))
22917 /* If the OBJECT_TYPE is a template specialization, it may
22918 be instantiated during name lookup. In that case, errors
22919 may be issued. Even if we rollback the current tentative
22920 parse, those errors are valid. */
22921 decl = lookup_member (object_type,
22922 name,
22923 /*protect=*/0,
22924 tag_type != none_type,
22925 tf_warning_or_error);
22926 else
22927 decl = NULL_TREE;
22929 if (!decl)
22930 /* Look it up in the enclosing context. */
22931 decl = lookup_name_real (name, tag_type != none_type,
22932 /*nonclass=*/0,
22933 /*block_p=*/true, is_namespace, 0);
22934 parser->object_scope = object_type;
22935 parser->qualifying_scope = NULL_TREE;
22937 else
22939 decl = lookup_name_real (name, tag_type != none_type,
22940 /*nonclass=*/0,
22941 /*block_p=*/true, is_namespace, 0);
22942 parser->qualifying_scope = NULL_TREE;
22943 parser->object_scope = NULL_TREE;
22946 /* If the lookup failed, let our caller know. */
22947 if (!decl || decl == error_mark_node)
22948 return error_mark_node;
22950 /* Pull out the template from an injected-class-name (or multiple). */
22951 if (is_template)
22952 decl = maybe_get_template_decl_from_type_decl (decl);
22954 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22955 if (TREE_CODE (decl) == TREE_LIST)
22957 if (ambiguous_decls)
22958 *ambiguous_decls = decl;
22959 /* The error message we have to print is too complicated for
22960 cp_parser_error, so we incorporate its actions directly. */
22961 if (!cp_parser_simulate_error (parser))
22963 error_at (name_location, "reference to %qD is ambiguous",
22964 name);
22965 print_candidates (decl);
22967 return error_mark_node;
22970 gcc_assert (DECL_P (decl)
22971 || TREE_CODE (decl) == OVERLOAD
22972 || TREE_CODE (decl) == SCOPE_REF
22973 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22974 || BASELINK_P (decl));
22976 /* If we have resolved the name of a member declaration, check to
22977 see if the declaration is accessible. When the name resolves to
22978 set of overloaded functions, accessibility is checked when
22979 overload resolution is done.
22981 During an explicit instantiation, access is not checked at all,
22982 as per [temp.explicit]. */
22983 if (DECL_P (decl))
22984 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22986 maybe_record_typedef_use (decl);
22988 return decl;
22991 /* Like cp_parser_lookup_name, but for use in the typical case where
22992 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22993 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22995 static tree
22996 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22998 return cp_parser_lookup_name (parser, name,
22999 none_type,
23000 /*is_template=*/false,
23001 /*is_namespace=*/false,
23002 /*check_dependency=*/true,
23003 /*ambiguous_decls=*/NULL,
23004 location);
23007 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23008 the current context, return the TYPE_DECL. If TAG_NAME_P is
23009 true, the DECL indicates the class being defined in a class-head,
23010 or declared in an elaborated-type-specifier.
23012 Otherwise, return DECL. */
23014 static tree
23015 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23017 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23018 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23020 struct A {
23021 template <typename T> struct B;
23024 template <typename T> struct A::B {};
23026 Similarly, in an elaborated-type-specifier:
23028 namespace N { struct X{}; }
23030 struct A {
23031 template <typename T> friend struct N::X;
23034 However, if the DECL refers to a class type, and we are in
23035 the scope of the class, then the name lookup automatically
23036 finds the TYPE_DECL created by build_self_reference rather
23037 than a TEMPLATE_DECL. For example, in:
23039 template <class T> struct S {
23040 S s;
23043 there is no need to handle such case. */
23045 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23046 return DECL_TEMPLATE_RESULT (decl);
23048 return decl;
23051 /* If too many, or too few, template-parameter lists apply to the
23052 declarator, issue an error message. Returns TRUE if all went well,
23053 and FALSE otherwise. */
23055 static bool
23056 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23057 cp_declarator *declarator,
23058 location_t declarator_location)
23060 switch (declarator->kind)
23062 case cdk_id:
23064 unsigned num_templates = 0;
23065 tree scope = declarator->u.id.qualifying_scope;
23067 if (scope)
23068 num_templates = num_template_headers_for_class (scope);
23069 else if (TREE_CODE (declarator->u.id.unqualified_name)
23070 == TEMPLATE_ID_EXPR)
23071 /* If the DECLARATOR has the form `X<y>' then it uses one
23072 additional level of template parameters. */
23073 ++num_templates;
23075 return cp_parser_check_template_parameters
23076 (parser, num_templates, declarator_location, declarator);
23079 case cdk_function:
23080 case cdk_array:
23081 case cdk_pointer:
23082 case cdk_reference:
23083 case cdk_ptrmem:
23084 return (cp_parser_check_declarator_template_parameters
23085 (parser, declarator->declarator, declarator_location));
23087 case cdk_error:
23088 return true;
23090 default:
23091 gcc_unreachable ();
23093 return false;
23096 /* NUM_TEMPLATES were used in the current declaration. If that is
23097 invalid, return FALSE and issue an error messages. Otherwise,
23098 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23099 declarator and we can print more accurate diagnostics. */
23101 static bool
23102 cp_parser_check_template_parameters (cp_parser* parser,
23103 unsigned num_templates,
23104 location_t location,
23105 cp_declarator *declarator)
23107 /* If there are the same number of template classes and parameter
23108 lists, that's OK. */
23109 if (parser->num_template_parameter_lists == num_templates)
23110 return true;
23111 /* If there are more, but only one more, then we are referring to a
23112 member template. That's OK too. */
23113 if (parser->num_template_parameter_lists == num_templates + 1)
23114 return true;
23115 /* If there are more template classes than parameter lists, we have
23116 something like:
23118 template <class T> void S<T>::R<T>::f (); */
23119 if (parser->num_template_parameter_lists < num_templates)
23121 if (declarator && !current_function_decl)
23122 error_at (location, "specializing member %<%T::%E%> "
23123 "requires %<template<>%> syntax",
23124 declarator->u.id.qualifying_scope,
23125 declarator->u.id.unqualified_name);
23126 else if (declarator)
23127 error_at (location, "invalid declaration of %<%T::%E%>",
23128 declarator->u.id.qualifying_scope,
23129 declarator->u.id.unqualified_name);
23130 else
23131 error_at (location, "too few template-parameter-lists");
23132 return false;
23134 /* Otherwise, there are too many template parameter lists. We have
23135 something like:
23137 template <class T> template <class U> void S::f(); */
23138 error_at (location, "too many template-parameter-lists");
23139 return false;
23142 /* Parse an optional `::' token indicating that the following name is
23143 from the global namespace. If so, PARSER->SCOPE is set to the
23144 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23145 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23146 Returns the new value of PARSER->SCOPE, if the `::' token is
23147 present, and NULL_TREE otherwise. */
23149 static tree
23150 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23152 cp_token *token;
23154 /* Peek at the next token. */
23155 token = cp_lexer_peek_token (parser->lexer);
23156 /* If we're looking at a `::' token then we're starting from the
23157 global namespace, not our current location. */
23158 if (token->type == CPP_SCOPE)
23160 /* Consume the `::' token. */
23161 cp_lexer_consume_token (parser->lexer);
23162 /* Set the SCOPE so that we know where to start the lookup. */
23163 parser->scope = global_namespace;
23164 parser->qualifying_scope = global_namespace;
23165 parser->object_scope = NULL_TREE;
23167 return parser->scope;
23169 else if (!current_scope_valid_p)
23171 parser->scope = NULL_TREE;
23172 parser->qualifying_scope = NULL_TREE;
23173 parser->object_scope = NULL_TREE;
23176 return NULL_TREE;
23179 /* Returns TRUE if the upcoming token sequence is the start of a
23180 constructor declarator. If FRIEND_P is true, the declarator is
23181 preceded by the `friend' specifier. */
23183 static bool
23184 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23186 bool constructor_p;
23187 bool outside_class_specifier_p;
23188 tree nested_name_specifier;
23189 cp_token *next_token;
23191 /* The common case is that this is not a constructor declarator, so
23192 try to avoid doing lots of work if at all possible. It's not
23193 valid declare a constructor at function scope. */
23194 if (parser->in_function_body)
23195 return false;
23196 /* And only certain tokens can begin a constructor declarator. */
23197 next_token = cp_lexer_peek_token (parser->lexer);
23198 if (next_token->type != CPP_NAME
23199 && next_token->type != CPP_SCOPE
23200 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23201 && next_token->type != CPP_TEMPLATE_ID)
23202 return false;
23204 /* Parse tentatively; we are going to roll back all of the tokens
23205 consumed here. */
23206 cp_parser_parse_tentatively (parser);
23207 /* Assume that we are looking at a constructor declarator. */
23208 constructor_p = true;
23210 /* Look for the optional `::' operator. */
23211 cp_parser_global_scope_opt (parser,
23212 /*current_scope_valid_p=*/false);
23213 /* Look for the nested-name-specifier. */
23214 nested_name_specifier
23215 = (cp_parser_nested_name_specifier_opt (parser,
23216 /*typename_keyword_p=*/false,
23217 /*check_dependency_p=*/false,
23218 /*type_p=*/false,
23219 /*is_declaration=*/false));
23221 outside_class_specifier_p = (!at_class_scope_p ()
23222 || !TYPE_BEING_DEFINED (current_class_type)
23223 || friend_p);
23225 /* Outside of a class-specifier, there must be a
23226 nested-name-specifier. */
23227 if (!nested_name_specifier && outside_class_specifier_p)
23228 constructor_p = false;
23229 else if (nested_name_specifier == error_mark_node)
23230 constructor_p = false;
23232 /* If we have a class scope, this is easy; DR 147 says that S::S always
23233 names the constructor, and no other qualified name could. */
23234 if (constructor_p && nested_name_specifier
23235 && CLASS_TYPE_P (nested_name_specifier))
23237 tree id = cp_parser_unqualified_id (parser,
23238 /*template_keyword_p=*/false,
23239 /*check_dependency_p=*/false,
23240 /*declarator_p=*/true,
23241 /*optional_p=*/false);
23242 if (is_overloaded_fn (id))
23243 id = DECL_NAME (get_first_fn (id));
23244 if (!constructor_name_p (id, nested_name_specifier))
23245 constructor_p = false;
23247 /* If we still think that this might be a constructor-declarator,
23248 look for a class-name. */
23249 else if (constructor_p)
23251 /* If we have:
23253 template <typename T> struct S {
23254 S();
23257 we must recognize that the nested `S' names a class. */
23258 tree type_decl;
23259 type_decl = cp_parser_class_name (parser,
23260 /*typename_keyword_p=*/false,
23261 /*template_keyword_p=*/false,
23262 none_type,
23263 /*check_dependency_p=*/false,
23264 /*class_head_p=*/false,
23265 /*is_declaration=*/false);
23266 /* If there was no class-name, then this is not a constructor.
23267 Otherwise, if we are in a class-specifier and we aren't
23268 handling a friend declaration, check that its type matches
23269 current_class_type (c++/38313). Note: error_mark_node
23270 is left alone for error recovery purposes. */
23271 constructor_p = (!cp_parser_error_occurred (parser)
23272 && (outside_class_specifier_p
23273 || type_decl == error_mark_node
23274 || same_type_p (current_class_type,
23275 TREE_TYPE (type_decl))));
23277 /* If we're still considering a constructor, we have to see a `(',
23278 to begin the parameter-declaration-clause, followed by either a
23279 `)', an `...', or a decl-specifier. We need to check for a
23280 type-specifier to avoid being fooled into thinking that:
23282 S (f) (int);
23284 is a constructor. (It is actually a function named `f' that
23285 takes one parameter (of type `int') and returns a value of type
23286 `S'. */
23287 if (constructor_p
23288 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23289 constructor_p = false;
23291 if (constructor_p
23292 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23294 /* A parameter declaration begins with a decl-specifier,
23295 which is either the "attribute" keyword, a storage class
23296 specifier, or (usually) a type-specifier. */
23297 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23299 tree type;
23300 tree pushed_scope = NULL_TREE;
23301 unsigned saved_num_template_parameter_lists;
23303 /* Names appearing in the type-specifier should be looked up
23304 in the scope of the class. */
23305 if (current_class_type)
23306 type = NULL_TREE;
23307 else
23309 type = TREE_TYPE (type_decl);
23310 if (TREE_CODE (type) == TYPENAME_TYPE)
23312 type = resolve_typename_type (type,
23313 /*only_current_p=*/false);
23314 if (TREE_CODE (type) == TYPENAME_TYPE)
23316 cp_parser_abort_tentative_parse (parser);
23317 return false;
23320 pushed_scope = push_scope (type);
23323 /* Inside the constructor parameter list, surrounding
23324 template-parameter-lists do not apply. */
23325 saved_num_template_parameter_lists
23326 = parser->num_template_parameter_lists;
23327 parser->num_template_parameter_lists = 0;
23329 /* Look for the type-specifier. */
23330 cp_parser_type_specifier (parser,
23331 CP_PARSER_FLAGS_NONE,
23332 /*decl_specs=*/NULL,
23333 /*is_declarator=*/true,
23334 /*declares_class_or_enum=*/NULL,
23335 /*is_cv_qualifier=*/NULL);
23337 parser->num_template_parameter_lists
23338 = saved_num_template_parameter_lists;
23340 /* Leave the scope of the class. */
23341 if (pushed_scope)
23342 pop_scope (pushed_scope);
23344 constructor_p = !cp_parser_error_occurred (parser);
23348 /* We did not really want to consume any tokens. */
23349 cp_parser_abort_tentative_parse (parser);
23351 return constructor_p;
23354 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23355 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23356 they must be performed once we are in the scope of the function.
23358 Returns the function defined. */
23360 static tree
23361 cp_parser_function_definition_from_specifiers_and_declarator
23362 (cp_parser* parser,
23363 cp_decl_specifier_seq *decl_specifiers,
23364 tree attributes,
23365 const cp_declarator *declarator)
23367 tree fn;
23368 bool success_p;
23370 /* Begin the function-definition. */
23371 success_p = start_function (decl_specifiers, declarator, attributes);
23373 /* The things we're about to see are not directly qualified by any
23374 template headers we've seen thus far. */
23375 reset_specialization ();
23377 /* If there were names looked up in the decl-specifier-seq that we
23378 did not check, check them now. We must wait until we are in the
23379 scope of the function to perform the checks, since the function
23380 might be a friend. */
23381 perform_deferred_access_checks (tf_warning_or_error);
23383 if (success_p)
23385 cp_finalize_omp_declare_simd (parser, current_function_decl);
23386 parser->omp_declare_simd = NULL;
23389 if (!success_p)
23391 /* Skip the entire function. */
23392 cp_parser_skip_to_end_of_block_or_statement (parser);
23393 fn = error_mark_node;
23395 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23397 /* Seen already, skip it. An error message has already been output. */
23398 cp_parser_skip_to_end_of_block_or_statement (parser);
23399 fn = current_function_decl;
23400 current_function_decl = NULL_TREE;
23401 /* If this is a function from a class, pop the nested class. */
23402 if (current_class_name)
23403 pop_nested_class ();
23405 else
23407 timevar_id_t tv;
23408 if (DECL_DECLARED_INLINE_P (current_function_decl))
23409 tv = TV_PARSE_INLINE;
23410 else
23411 tv = TV_PARSE_FUNC;
23412 timevar_push (tv);
23413 fn = cp_parser_function_definition_after_declarator (parser,
23414 /*inline_p=*/false);
23415 timevar_pop (tv);
23418 return fn;
23421 /* Parse the part of a function-definition that follows the
23422 declarator. INLINE_P is TRUE iff this function is an inline
23423 function defined within a class-specifier.
23425 Returns the function defined. */
23427 static tree
23428 cp_parser_function_definition_after_declarator (cp_parser* parser,
23429 bool inline_p)
23431 tree fn;
23432 bool ctor_initializer_p = false;
23433 bool saved_in_unbraced_linkage_specification_p;
23434 bool saved_in_function_body;
23435 unsigned saved_num_template_parameter_lists;
23436 cp_token *token;
23437 bool fully_implicit_function_template_p
23438 = parser->fully_implicit_function_template_p;
23439 parser->fully_implicit_function_template_p = false;
23440 tree implicit_template_parms
23441 = parser->implicit_template_parms;
23442 parser->implicit_template_parms = 0;
23443 cp_binding_level* implicit_template_scope
23444 = parser->implicit_template_scope;
23445 parser->implicit_template_scope = 0;
23447 saved_in_function_body = parser->in_function_body;
23448 parser->in_function_body = true;
23449 /* If the next token is `return', then the code may be trying to
23450 make use of the "named return value" extension that G++ used to
23451 support. */
23452 token = cp_lexer_peek_token (parser->lexer);
23453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23455 /* Consume the `return' keyword. */
23456 cp_lexer_consume_token (parser->lexer);
23457 /* Look for the identifier that indicates what value is to be
23458 returned. */
23459 cp_parser_identifier (parser);
23460 /* Issue an error message. */
23461 error_at (token->location,
23462 "named return values are no longer supported");
23463 /* Skip tokens until we reach the start of the function body. */
23464 while (true)
23466 cp_token *token = cp_lexer_peek_token (parser->lexer);
23467 if (token->type == CPP_OPEN_BRACE
23468 || token->type == CPP_EOF
23469 || token->type == CPP_PRAGMA_EOL)
23470 break;
23471 cp_lexer_consume_token (parser->lexer);
23474 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23475 anything declared inside `f'. */
23476 saved_in_unbraced_linkage_specification_p
23477 = parser->in_unbraced_linkage_specification_p;
23478 parser->in_unbraced_linkage_specification_p = false;
23479 /* Inside the function, surrounding template-parameter-lists do not
23480 apply. */
23481 saved_num_template_parameter_lists
23482 = parser->num_template_parameter_lists;
23483 parser->num_template_parameter_lists = 0;
23485 start_lambda_scope (current_function_decl);
23487 /* If the next token is `try', `__transaction_atomic', or
23488 `__transaction_relaxed`, then we are looking at either function-try-block
23489 or function-transaction-block. Note that all of these include the
23490 function-body. */
23491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23492 ctor_initializer_p = cp_parser_function_transaction (parser,
23493 RID_TRANSACTION_ATOMIC);
23494 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23495 RID_TRANSACTION_RELAXED))
23496 ctor_initializer_p = cp_parser_function_transaction (parser,
23497 RID_TRANSACTION_RELAXED);
23498 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23499 ctor_initializer_p = cp_parser_function_try_block (parser);
23500 else
23501 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23502 (parser, /*in_function_try_block=*/false);
23504 finish_lambda_scope ();
23506 /* Finish the function. */
23507 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23508 (inline_p ? 2 : 0));
23509 /* Generate code for it, if necessary. */
23510 expand_or_defer_fn (fn);
23511 /* Restore the saved values. */
23512 parser->in_unbraced_linkage_specification_p
23513 = saved_in_unbraced_linkage_specification_p;
23514 parser->num_template_parameter_lists
23515 = saved_num_template_parameter_lists;
23516 parser->in_function_body = saved_in_function_body;
23518 parser->fully_implicit_function_template_p
23519 = fully_implicit_function_template_p;
23520 parser->implicit_template_parms
23521 = implicit_template_parms;
23522 parser->implicit_template_scope
23523 = implicit_template_scope;
23525 if (parser->fully_implicit_function_template_p)
23526 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23528 return fn;
23531 /* Parse a template-declaration, assuming that the `export' (and
23532 `extern') keywords, if present, has already been scanned. MEMBER_P
23533 is as for cp_parser_template_declaration. */
23535 static void
23536 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23538 tree decl = NULL_TREE;
23539 vec<deferred_access_check, va_gc> *checks;
23540 tree parameter_list;
23541 bool friend_p = false;
23542 bool need_lang_pop;
23543 cp_token *token;
23545 /* Look for the `template' keyword. */
23546 token = cp_lexer_peek_token (parser->lexer);
23547 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23548 return;
23550 /* And the `<'. */
23551 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23552 return;
23553 if (at_class_scope_p () && current_function_decl)
23555 /* 14.5.2.2 [temp.mem]
23557 A local class shall not have member templates. */
23558 error_at (token->location,
23559 "invalid declaration of member template in local class");
23560 cp_parser_skip_to_end_of_block_or_statement (parser);
23561 return;
23563 /* [temp]
23565 A template ... shall not have C linkage. */
23566 if (current_lang_name == lang_name_c)
23568 error_at (token->location, "template with C linkage");
23569 /* Give it C++ linkage to avoid confusing other parts of the
23570 front end. */
23571 push_lang_context (lang_name_cplusplus);
23572 need_lang_pop = true;
23574 else
23575 need_lang_pop = false;
23577 /* We cannot perform access checks on the template parameter
23578 declarations until we know what is being declared, just as we
23579 cannot check the decl-specifier list. */
23580 push_deferring_access_checks (dk_deferred);
23582 /* If the next token is `>', then we have an invalid
23583 specialization. Rather than complain about an invalid template
23584 parameter, issue an error message here. */
23585 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23587 cp_parser_error (parser, "invalid explicit specialization");
23588 begin_specialization ();
23589 parameter_list = NULL_TREE;
23591 else
23593 /* Parse the template parameters. */
23594 parameter_list = cp_parser_template_parameter_list (parser);
23597 /* Get the deferred access checks from the parameter list. These
23598 will be checked once we know what is being declared, as for a
23599 member template the checks must be performed in the scope of the
23600 class containing the member. */
23601 checks = get_deferred_access_checks ();
23603 /* Look for the `>'. */
23604 cp_parser_skip_to_end_of_template_parameter_list (parser);
23605 /* We just processed one more parameter list. */
23606 ++parser->num_template_parameter_lists;
23607 /* If the next token is `template', there are more template
23608 parameters. */
23609 if (cp_lexer_next_token_is_keyword (parser->lexer,
23610 RID_TEMPLATE))
23611 cp_parser_template_declaration_after_export (parser, member_p);
23612 else if (cxx_dialect >= cxx11
23613 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23614 decl = cp_parser_alias_declaration (parser);
23615 else
23617 /* There are no access checks when parsing a template, as we do not
23618 know if a specialization will be a friend. */
23619 push_deferring_access_checks (dk_no_check);
23620 token = cp_lexer_peek_token (parser->lexer);
23621 decl = cp_parser_single_declaration (parser,
23622 checks,
23623 member_p,
23624 /*explicit_specialization_p=*/false,
23625 &friend_p);
23626 pop_deferring_access_checks ();
23628 /* If this is a member template declaration, let the front
23629 end know. */
23630 if (member_p && !friend_p && decl)
23632 if (TREE_CODE (decl) == TYPE_DECL)
23633 cp_parser_check_access_in_redeclaration (decl, token->location);
23635 decl = finish_member_template_decl (decl);
23637 else if (friend_p && decl
23638 && DECL_DECLARES_TYPE_P (decl))
23639 make_friend_class (current_class_type, TREE_TYPE (decl),
23640 /*complain=*/true);
23642 /* We are done with the current parameter list. */
23643 --parser->num_template_parameter_lists;
23645 pop_deferring_access_checks ();
23647 /* Finish up. */
23648 finish_template_decl (parameter_list);
23650 /* Check the template arguments for a literal operator template. */
23651 if (decl
23652 && DECL_DECLARES_FUNCTION_P (decl)
23653 && UDLIT_OPER_P (DECL_NAME (decl)))
23655 bool ok = true;
23656 if (parameter_list == NULL_TREE)
23657 ok = false;
23658 else
23660 int num_parms = TREE_VEC_LENGTH (parameter_list);
23661 if (num_parms == 1)
23663 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23664 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23665 if (TREE_TYPE (parm) != char_type_node
23666 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23667 ok = false;
23669 else if (num_parms == 2 && cxx_dialect >= cxx14)
23671 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23672 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23673 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23674 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23675 if (TREE_TYPE (parm) != TREE_TYPE (type)
23676 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23677 ok = false;
23679 else
23680 ok = false;
23682 if (!ok)
23684 if (cxx_dialect >= cxx14)
23685 error ("literal operator template %qD has invalid parameter list."
23686 " Expected non-type template argument pack <char...>"
23687 " or <typename CharT, CharT...>",
23688 decl);
23689 else
23690 error ("literal operator template %qD has invalid parameter list."
23691 " Expected non-type template argument pack <char...>",
23692 decl);
23695 /* Register member declarations. */
23696 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23697 finish_member_declaration (decl);
23698 /* For the erroneous case of a template with C linkage, we pushed an
23699 implicit C++ linkage scope; exit that scope now. */
23700 if (need_lang_pop)
23701 pop_lang_context ();
23702 /* If DECL is a function template, we must return to parse it later.
23703 (Even though there is no definition, there might be default
23704 arguments that need handling.) */
23705 if (member_p && decl
23706 && DECL_DECLARES_FUNCTION_P (decl))
23707 vec_safe_push (unparsed_funs_with_definitions, decl);
23710 /* Perform the deferred access checks from a template-parameter-list.
23711 CHECKS is a TREE_LIST of access checks, as returned by
23712 get_deferred_access_checks. */
23714 static void
23715 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23717 ++processing_template_parmlist;
23718 perform_access_checks (checks, tf_warning_or_error);
23719 --processing_template_parmlist;
23722 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23723 `function-definition' sequence that follows a template header.
23724 If MEMBER_P is true, this declaration appears in a class scope.
23726 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23727 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23729 static tree
23730 cp_parser_single_declaration (cp_parser* parser,
23731 vec<deferred_access_check, va_gc> *checks,
23732 bool member_p,
23733 bool explicit_specialization_p,
23734 bool* friend_p)
23736 int declares_class_or_enum;
23737 tree decl = NULL_TREE;
23738 cp_decl_specifier_seq decl_specifiers;
23739 bool function_definition_p = false;
23740 cp_token *decl_spec_token_start;
23742 /* This function is only used when processing a template
23743 declaration. */
23744 gcc_assert (innermost_scope_kind () == sk_template_parms
23745 || innermost_scope_kind () == sk_template_spec);
23747 /* Defer access checks until we know what is being declared. */
23748 push_deferring_access_checks (dk_deferred);
23750 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23751 alternative. */
23752 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23753 cp_parser_decl_specifier_seq (parser,
23754 CP_PARSER_FLAGS_OPTIONAL,
23755 &decl_specifiers,
23756 &declares_class_or_enum);
23757 if (friend_p)
23758 *friend_p = cp_parser_friend_p (&decl_specifiers);
23760 /* There are no template typedefs. */
23761 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23763 error_at (decl_spec_token_start->location,
23764 "template declaration of %<typedef%>");
23765 decl = error_mark_node;
23768 /* Gather up the access checks that occurred the
23769 decl-specifier-seq. */
23770 stop_deferring_access_checks ();
23772 /* Check for the declaration of a template class. */
23773 if (declares_class_or_enum)
23775 if (cp_parser_declares_only_class_p (parser))
23777 decl = shadow_tag (&decl_specifiers);
23779 /* In this case:
23781 struct C {
23782 friend template <typename T> struct A<T>::B;
23785 A<T>::B will be represented by a TYPENAME_TYPE, and
23786 therefore not recognized by shadow_tag. */
23787 if (friend_p && *friend_p
23788 && !decl
23789 && decl_specifiers.type
23790 && TYPE_P (decl_specifiers.type))
23791 decl = decl_specifiers.type;
23793 if (decl && decl != error_mark_node)
23794 decl = TYPE_NAME (decl);
23795 else
23796 decl = error_mark_node;
23798 /* Perform access checks for template parameters. */
23799 cp_parser_perform_template_parameter_access_checks (checks);
23803 /* Complain about missing 'typename' or other invalid type names. */
23804 if (!decl_specifiers.any_type_specifiers_p
23805 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23807 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23808 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23809 the rest of this declaration. */
23810 decl = error_mark_node;
23811 goto out;
23814 /* If it's not a template class, try for a template function. If
23815 the next token is a `;', then this declaration does not declare
23816 anything. But, if there were errors in the decl-specifiers, then
23817 the error might well have come from an attempted class-specifier.
23818 In that case, there's no need to warn about a missing declarator. */
23819 if (!decl
23820 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23821 || decl_specifiers.type != error_mark_node))
23823 decl = cp_parser_init_declarator (parser,
23824 &decl_specifiers,
23825 checks,
23826 /*function_definition_allowed_p=*/true,
23827 member_p,
23828 declares_class_or_enum,
23829 &function_definition_p,
23830 NULL, NULL);
23832 /* 7.1.1-1 [dcl.stc]
23834 A storage-class-specifier shall not be specified in an explicit
23835 specialization... */
23836 if (decl
23837 && explicit_specialization_p
23838 && decl_specifiers.storage_class != sc_none)
23840 error_at (decl_spec_token_start->location,
23841 "explicit template specialization cannot have a storage class");
23842 decl = error_mark_node;
23845 if (decl && VAR_P (decl))
23846 check_template_variable (decl);
23849 /* Look for a trailing `;' after the declaration. */
23850 if (!function_definition_p
23851 && (decl == error_mark_node
23852 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23853 cp_parser_skip_to_end_of_block_or_statement (parser);
23855 out:
23856 pop_deferring_access_checks ();
23858 /* Clear any current qualification; whatever comes next is the start
23859 of something new. */
23860 parser->scope = NULL_TREE;
23861 parser->qualifying_scope = NULL_TREE;
23862 parser->object_scope = NULL_TREE;
23864 return decl;
23867 /* Parse a cast-expression that is not the operand of a unary "&". */
23869 static tree
23870 cp_parser_simple_cast_expression (cp_parser *parser)
23872 return cp_parser_cast_expression (parser, /*address_p=*/false,
23873 /*cast_p=*/false, /*decltype*/false, NULL);
23876 /* Parse a functional cast to TYPE. Returns an expression
23877 representing the cast. */
23879 static tree
23880 cp_parser_functional_cast (cp_parser* parser, tree type)
23882 vec<tree, va_gc> *vec;
23883 tree expression_list;
23884 tree cast;
23885 bool nonconst_p;
23887 if (!type)
23888 type = error_mark_node;
23890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23892 cp_lexer_set_source_position (parser->lexer);
23893 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23894 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23895 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23896 if (TREE_CODE (type) == TYPE_DECL)
23897 type = TREE_TYPE (type);
23898 return finish_compound_literal (type, expression_list,
23899 tf_warning_or_error);
23903 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23904 /*cast_p=*/true,
23905 /*allow_expansion_p=*/true,
23906 /*non_constant_p=*/NULL);
23907 if (vec == NULL)
23908 expression_list = error_mark_node;
23909 else
23911 expression_list = build_tree_list_vec (vec);
23912 release_tree_vector (vec);
23915 cast = build_functional_cast (type, expression_list,
23916 tf_warning_or_error);
23917 /* [expr.const]/1: In an integral constant expression "only type
23918 conversions to integral or enumeration type can be used". */
23919 if (TREE_CODE (type) == TYPE_DECL)
23920 type = TREE_TYPE (type);
23921 if (cast != error_mark_node
23922 && !cast_valid_in_integral_constant_expression_p (type)
23923 && cp_parser_non_integral_constant_expression (parser,
23924 NIC_CONSTRUCTOR))
23925 return error_mark_node;
23926 return cast;
23929 /* Save the tokens that make up the body of a member function defined
23930 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23931 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23932 specifiers applied to the declaration. Returns the FUNCTION_DECL
23933 for the member function. */
23935 static tree
23936 cp_parser_save_member_function_body (cp_parser* parser,
23937 cp_decl_specifier_seq *decl_specifiers,
23938 cp_declarator *declarator,
23939 tree attributes)
23941 cp_token *first;
23942 cp_token *last;
23943 tree fn;
23945 /* Create the FUNCTION_DECL. */
23946 fn = grokmethod (decl_specifiers, declarator, attributes);
23947 cp_finalize_omp_declare_simd (parser, fn);
23948 /* If something went badly wrong, bail out now. */
23949 if (fn == error_mark_node)
23951 /* If there's a function-body, skip it. */
23952 if (cp_parser_token_starts_function_definition_p
23953 (cp_lexer_peek_token (parser->lexer)))
23954 cp_parser_skip_to_end_of_block_or_statement (parser);
23955 return error_mark_node;
23958 /* Remember it, if there default args to post process. */
23959 cp_parser_save_default_args (parser, fn);
23961 /* Save away the tokens that make up the body of the
23962 function. */
23963 first = parser->lexer->next_token;
23964 /* Handle function try blocks. */
23965 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23966 cp_lexer_consume_token (parser->lexer);
23967 /* We can have braced-init-list mem-initializers before the fn body. */
23968 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23970 cp_lexer_consume_token (parser->lexer);
23971 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23973 /* cache_group will stop after an un-nested { } pair, too. */
23974 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23975 break;
23977 /* variadic mem-inits have ... after the ')'. */
23978 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23979 cp_lexer_consume_token (parser->lexer);
23982 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23983 /* Handle function try blocks. */
23984 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23985 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23986 last = parser->lexer->next_token;
23988 /* Save away the inline definition; we will process it when the
23989 class is complete. */
23990 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23991 DECL_PENDING_INLINE_P (fn) = 1;
23993 /* We need to know that this was defined in the class, so that
23994 friend templates are handled correctly. */
23995 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23997 /* Add FN to the queue of functions to be parsed later. */
23998 vec_safe_push (unparsed_funs_with_definitions, fn);
24000 return fn;
24003 /* Save the tokens that make up the in-class initializer for a non-static
24004 data member. Returns a DEFAULT_ARG. */
24006 static tree
24007 cp_parser_save_nsdmi (cp_parser* parser)
24009 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24012 /* Parse a template-argument-list, as well as the trailing ">" (but
24013 not the opening "<"). See cp_parser_template_argument_list for the
24014 return value. */
24016 static tree
24017 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24019 tree arguments;
24020 tree saved_scope;
24021 tree saved_qualifying_scope;
24022 tree saved_object_scope;
24023 bool saved_greater_than_is_operator_p;
24024 int saved_unevaluated_operand;
24025 int saved_inhibit_evaluation_warnings;
24027 /* [temp.names]
24029 When parsing a template-id, the first non-nested `>' is taken as
24030 the end of the template-argument-list rather than a greater-than
24031 operator. */
24032 saved_greater_than_is_operator_p
24033 = parser->greater_than_is_operator_p;
24034 parser->greater_than_is_operator_p = false;
24035 /* Parsing the argument list may modify SCOPE, so we save it
24036 here. */
24037 saved_scope = parser->scope;
24038 saved_qualifying_scope = parser->qualifying_scope;
24039 saved_object_scope = parser->object_scope;
24040 /* We need to evaluate the template arguments, even though this
24041 template-id may be nested within a "sizeof". */
24042 saved_unevaluated_operand = cp_unevaluated_operand;
24043 cp_unevaluated_operand = 0;
24044 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24045 c_inhibit_evaluation_warnings = 0;
24046 /* Parse the template-argument-list itself. */
24047 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24048 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24049 arguments = NULL_TREE;
24050 else
24051 arguments = cp_parser_template_argument_list (parser);
24052 /* Look for the `>' that ends the template-argument-list. If we find
24053 a '>>' instead, it's probably just a typo. */
24054 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24056 if (cxx_dialect != cxx98)
24058 /* In C++0x, a `>>' in a template argument list or cast
24059 expression is considered to be two separate `>'
24060 tokens. So, change the current token to a `>', but don't
24061 consume it: it will be consumed later when the outer
24062 template argument list (or cast expression) is parsed.
24063 Note that this replacement of `>' for `>>' is necessary
24064 even if we are parsing tentatively: in the tentative
24065 case, after calling
24066 cp_parser_enclosed_template_argument_list we will always
24067 throw away all of the template arguments and the first
24068 closing `>', either because the template argument list
24069 was erroneous or because we are replacing those tokens
24070 with a CPP_TEMPLATE_ID token. The second `>' (which will
24071 not have been thrown away) is needed either to close an
24072 outer template argument list or to complete a new-style
24073 cast. */
24074 cp_token *token = cp_lexer_peek_token (parser->lexer);
24075 token->type = CPP_GREATER;
24077 else if (!saved_greater_than_is_operator_p)
24079 /* If we're in a nested template argument list, the '>>' has
24080 to be a typo for '> >'. We emit the error message, but we
24081 continue parsing and we push a '>' as next token, so that
24082 the argument list will be parsed correctly. Note that the
24083 global source location is still on the token before the
24084 '>>', so we need to say explicitly where we want it. */
24085 cp_token *token = cp_lexer_peek_token (parser->lexer);
24086 error_at (token->location, "%<>>%> should be %<> >%> "
24087 "within a nested template argument list");
24089 token->type = CPP_GREATER;
24091 else
24093 /* If this is not a nested template argument list, the '>>'
24094 is a typo for '>'. Emit an error message and continue.
24095 Same deal about the token location, but here we can get it
24096 right by consuming the '>>' before issuing the diagnostic. */
24097 cp_token *token = cp_lexer_consume_token (parser->lexer);
24098 error_at (token->location,
24099 "spurious %<>>%>, use %<>%> to terminate "
24100 "a template argument list");
24103 else
24104 cp_parser_skip_to_end_of_template_parameter_list (parser);
24105 /* The `>' token might be a greater-than operator again now. */
24106 parser->greater_than_is_operator_p
24107 = saved_greater_than_is_operator_p;
24108 /* Restore the SAVED_SCOPE. */
24109 parser->scope = saved_scope;
24110 parser->qualifying_scope = saved_qualifying_scope;
24111 parser->object_scope = saved_object_scope;
24112 cp_unevaluated_operand = saved_unevaluated_operand;
24113 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24115 return arguments;
24118 /* MEMBER_FUNCTION is a member function, or a friend. If default
24119 arguments, or the body of the function have not yet been parsed,
24120 parse them now. */
24122 static void
24123 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24125 timevar_push (TV_PARSE_INMETH);
24126 /* If this member is a template, get the underlying
24127 FUNCTION_DECL. */
24128 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24129 member_function = DECL_TEMPLATE_RESULT (member_function);
24131 /* There should not be any class definitions in progress at this
24132 point; the bodies of members are only parsed outside of all class
24133 definitions. */
24134 gcc_assert (parser->num_classes_being_defined == 0);
24135 /* While we're parsing the member functions we might encounter more
24136 classes. We want to handle them right away, but we don't want
24137 them getting mixed up with functions that are currently in the
24138 queue. */
24139 push_unparsed_function_queues (parser);
24141 /* Make sure that any template parameters are in scope. */
24142 maybe_begin_member_template_processing (member_function);
24144 /* If the body of the function has not yet been parsed, parse it
24145 now. */
24146 if (DECL_PENDING_INLINE_P (member_function))
24148 tree function_scope;
24149 cp_token_cache *tokens;
24151 /* The function is no longer pending; we are processing it. */
24152 tokens = DECL_PENDING_INLINE_INFO (member_function);
24153 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24154 DECL_PENDING_INLINE_P (member_function) = 0;
24156 /* If this is a local class, enter the scope of the containing
24157 function. */
24158 function_scope = current_function_decl;
24159 if (function_scope)
24160 push_function_context ();
24162 /* Push the body of the function onto the lexer stack. */
24163 cp_parser_push_lexer_for_tokens (parser, tokens);
24165 /* Let the front end know that we going to be defining this
24166 function. */
24167 start_preparsed_function (member_function, NULL_TREE,
24168 SF_PRE_PARSED | SF_INCLASS_INLINE);
24170 /* Don't do access checking if it is a templated function. */
24171 if (processing_template_decl)
24172 push_deferring_access_checks (dk_no_check);
24174 /* #pragma omp declare reduction needs special parsing. */
24175 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24177 parser->lexer->in_pragma = true;
24178 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24179 finish_function (/*inline*/2);
24180 cp_check_omp_declare_reduction (member_function);
24182 else
24183 /* Now, parse the body of the function. */
24184 cp_parser_function_definition_after_declarator (parser,
24185 /*inline_p=*/true);
24187 if (processing_template_decl)
24188 pop_deferring_access_checks ();
24190 /* Leave the scope of the containing function. */
24191 if (function_scope)
24192 pop_function_context ();
24193 cp_parser_pop_lexer (parser);
24196 /* Remove any template parameters from the symbol table. */
24197 maybe_end_member_template_processing ();
24199 /* Restore the queue. */
24200 pop_unparsed_function_queues (parser);
24201 timevar_pop (TV_PARSE_INMETH);
24204 /* If DECL contains any default args, remember it on the unparsed
24205 functions queue. */
24207 static void
24208 cp_parser_save_default_args (cp_parser* parser, tree decl)
24210 tree probe;
24212 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24213 probe;
24214 probe = TREE_CHAIN (probe))
24215 if (TREE_PURPOSE (probe))
24217 cp_default_arg_entry entry = {current_class_type, decl};
24218 vec_safe_push (unparsed_funs_with_default_args, entry);
24219 break;
24223 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24224 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24225 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24226 from the parameter-type-list. */
24228 static tree
24229 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24230 tree default_arg, tree parmtype)
24232 cp_token_cache *tokens;
24233 tree parsed_arg;
24234 bool dummy;
24236 if (default_arg == error_mark_node)
24237 return error_mark_node;
24239 /* Push the saved tokens for the default argument onto the parser's
24240 lexer stack. */
24241 tokens = DEFARG_TOKENS (default_arg);
24242 cp_parser_push_lexer_for_tokens (parser, tokens);
24244 start_lambda_scope (decl);
24246 /* Parse the default argument. */
24247 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24248 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24249 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24251 finish_lambda_scope ();
24253 if (parsed_arg == error_mark_node)
24254 cp_parser_skip_to_end_of_statement (parser);
24256 if (!processing_template_decl)
24258 /* In a non-template class, check conversions now. In a template,
24259 we'll wait and instantiate these as needed. */
24260 if (TREE_CODE (decl) == PARM_DECL)
24261 parsed_arg = check_default_argument (parmtype, parsed_arg,
24262 tf_warning_or_error);
24263 else
24264 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24267 /* If the token stream has not been completely used up, then
24268 there was extra junk after the end of the default
24269 argument. */
24270 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24272 if (TREE_CODE (decl) == PARM_DECL)
24273 cp_parser_error (parser, "expected %<,%>");
24274 else
24275 cp_parser_error (parser, "expected %<;%>");
24278 /* Revert to the main lexer. */
24279 cp_parser_pop_lexer (parser);
24281 return parsed_arg;
24284 /* FIELD is a non-static data member with an initializer which we saved for
24285 later; parse it now. */
24287 static void
24288 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24290 tree def;
24292 maybe_begin_member_template_processing (field);
24294 push_unparsed_function_queues (parser);
24295 def = cp_parser_late_parse_one_default_arg (parser, field,
24296 DECL_INITIAL (field),
24297 NULL_TREE);
24298 pop_unparsed_function_queues (parser);
24300 maybe_end_member_template_processing ();
24302 DECL_INITIAL (field) = def;
24305 /* FN is a FUNCTION_DECL which may contains a parameter with an
24306 unparsed DEFAULT_ARG. Parse the default args now. This function
24307 assumes that the current scope is the scope in which the default
24308 argument should be processed. */
24310 static void
24311 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24313 bool saved_local_variables_forbidden_p;
24314 tree parm, parmdecl;
24316 /* While we're parsing the default args, we might (due to the
24317 statement expression extension) encounter more classes. We want
24318 to handle them right away, but we don't want them getting mixed
24319 up with default args that are currently in the queue. */
24320 push_unparsed_function_queues (parser);
24322 /* Local variable names (and the `this' keyword) may not appear
24323 in a default argument. */
24324 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24325 parser->local_variables_forbidden_p = true;
24327 push_defarg_context (fn);
24329 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24330 parmdecl = DECL_ARGUMENTS (fn);
24331 parm && parm != void_list_node;
24332 parm = TREE_CHAIN (parm),
24333 parmdecl = DECL_CHAIN (parmdecl))
24335 tree default_arg = TREE_PURPOSE (parm);
24336 tree parsed_arg;
24337 vec<tree, va_gc> *insts;
24338 tree copy;
24339 unsigned ix;
24341 if (!default_arg)
24342 continue;
24344 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24345 /* This can happen for a friend declaration for a function
24346 already declared with default arguments. */
24347 continue;
24349 parsed_arg
24350 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24351 default_arg,
24352 TREE_VALUE (parm));
24353 if (parsed_arg == error_mark_node)
24355 continue;
24358 TREE_PURPOSE (parm) = parsed_arg;
24360 /* Update any instantiations we've already created. */
24361 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24362 vec_safe_iterate (insts, ix, &copy); ix++)
24363 TREE_PURPOSE (copy) = parsed_arg;
24366 pop_defarg_context ();
24368 /* Make sure no default arg is missing. */
24369 check_default_args (fn);
24371 /* Restore the state of local_variables_forbidden_p. */
24372 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24374 /* Restore the queue. */
24375 pop_unparsed_function_queues (parser);
24378 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24380 sizeof ... ( identifier )
24382 where the 'sizeof' token has already been consumed. */
24384 static tree
24385 cp_parser_sizeof_pack (cp_parser *parser)
24387 /* Consume the `...'. */
24388 cp_lexer_consume_token (parser->lexer);
24389 maybe_warn_variadic_templates ();
24391 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24392 if (paren)
24393 cp_lexer_consume_token (parser->lexer);
24394 else
24395 permerror (cp_lexer_peek_token (parser->lexer)->location,
24396 "%<sizeof...%> argument must be surrounded by parentheses");
24398 cp_token *token = cp_lexer_peek_token (parser->lexer);
24399 tree name = cp_parser_identifier (parser);
24400 if (name == error_mark_node)
24401 return error_mark_node;
24402 /* The name is not qualified. */
24403 parser->scope = NULL_TREE;
24404 parser->qualifying_scope = NULL_TREE;
24405 parser->object_scope = NULL_TREE;
24406 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24407 if (expr == error_mark_node)
24408 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24409 token->location);
24410 if (TREE_CODE (expr) == TYPE_DECL)
24411 expr = TREE_TYPE (expr);
24412 else if (TREE_CODE (expr) == CONST_DECL)
24413 expr = DECL_INITIAL (expr);
24414 expr = make_pack_expansion (expr);
24416 if (paren)
24417 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24419 return expr;
24422 /* Parse the operand of `sizeof' (or a similar operator). Returns
24423 either a TYPE or an expression, depending on the form of the
24424 input. The KEYWORD indicates which kind of expression we have
24425 encountered. */
24427 static tree
24428 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24430 tree expr = NULL_TREE;
24431 const char *saved_message;
24432 char *tmp;
24433 bool saved_integral_constant_expression_p;
24434 bool saved_non_integral_constant_expression_p;
24436 /* If it's a `...', then we are computing the length of a parameter
24437 pack. */
24438 if (keyword == RID_SIZEOF
24439 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24440 return cp_parser_sizeof_pack (parser);
24442 /* Types cannot be defined in a `sizeof' expression. Save away the
24443 old message. */
24444 saved_message = parser->type_definition_forbidden_message;
24445 /* And create the new one. */
24446 tmp = concat ("types may not be defined in %<",
24447 IDENTIFIER_POINTER (ridpointers[keyword]),
24448 "%> expressions", NULL);
24449 parser->type_definition_forbidden_message = tmp;
24451 /* The restrictions on constant-expressions do not apply inside
24452 sizeof expressions. */
24453 saved_integral_constant_expression_p
24454 = parser->integral_constant_expression_p;
24455 saved_non_integral_constant_expression_p
24456 = parser->non_integral_constant_expression_p;
24457 parser->integral_constant_expression_p = false;
24459 /* Do not actually evaluate the expression. */
24460 ++cp_unevaluated_operand;
24461 ++c_inhibit_evaluation_warnings;
24462 /* If it's a `(', then we might be looking at the type-id
24463 construction. */
24464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24466 tree type = NULL_TREE;
24468 /* We can't be sure yet whether we're looking at a type-id or an
24469 expression. */
24470 cp_parser_parse_tentatively (parser);
24471 /* Note: as a GNU Extension, compound literals are considered
24472 postfix-expressions as they are in C99, so they are valid
24473 arguments to sizeof. See comment in cp_parser_cast_expression
24474 for details. */
24475 if (cp_parser_compound_literal_p (parser))
24476 cp_parser_simulate_error (parser);
24477 else
24479 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24480 parser->in_type_id_in_expr_p = true;
24481 /* Look for the type-id. */
24482 type = cp_parser_type_id (parser);
24483 /* Look for the closing `)'. */
24484 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24485 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24488 /* If all went well, then we're done. */
24489 if (cp_parser_parse_definitely (parser))
24491 cp_decl_specifier_seq decl_specs;
24493 /* Build a trivial decl-specifier-seq. */
24494 clear_decl_specs (&decl_specs);
24495 decl_specs.type = type;
24497 /* Call grokdeclarator to figure out what type this is. */
24498 expr = grokdeclarator (NULL,
24499 &decl_specs,
24500 TYPENAME,
24501 /*initialized=*/0,
24502 /*attrlist=*/NULL);
24506 /* If the type-id production did not work out, then we must be
24507 looking at the unary-expression production. */
24508 if (!expr)
24509 expr = cp_parser_unary_expression (parser);
24511 /* Go back to evaluating expressions. */
24512 --cp_unevaluated_operand;
24513 --c_inhibit_evaluation_warnings;
24515 /* Free the message we created. */
24516 free (tmp);
24517 /* And restore the old one. */
24518 parser->type_definition_forbidden_message = saved_message;
24519 parser->integral_constant_expression_p
24520 = saved_integral_constant_expression_p;
24521 parser->non_integral_constant_expression_p
24522 = saved_non_integral_constant_expression_p;
24524 return expr;
24527 /* If the current declaration has no declarator, return true. */
24529 static bool
24530 cp_parser_declares_only_class_p (cp_parser *parser)
24532 /* If the next token is a `;' or a `,' then there is no
24533 declarator. */
24534 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24535 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24538 /* Update the DECL_SPECS to reflect the storage class indicated by
24539 KEYWORD. */
24541 static void
24542 cp_parser_set_storage_class (cp_parser *parser,
24543 cp_decl_specifier_seq *decl_specs,
24544 enum rid keyword,
24545 cp_token *token)
24547 cp_storage_class storage_class;
24549 if (parser->in_unbraced_linkage_specification_p)
24551 error_at (token->location, "invalid use of %qD in linkage specification",
24552 ridpointers[keyword]);
24553 return;
24555 else if (decl_specs->storage_class != sc_none)
24557 decl_specs->conflicting_specifiers_p = true;
24558 return;
24561 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24562 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24563 && decl_specs->gnu_thread_keyword_p)
24565 pedwarn (decl_specs->locations[ds_thread], 0,
24566 "%<__thread%> before %qD", ridpointers[keyword]);
24569 switch (keyword)
24571 case RID_AUTO:
24572 storage_class = sc_auto;
24573 break;
24574 case RID_REGISTER:
24575 storage_class = sc_register;
24576 break;
24577 case RID_STATIC:
24578 storage_class = sc_static;
24579 break;
24580 case RID_EXTERN:
24581 storage_class = sc_extern;
24582 break;
24583 case RID_MUTABLE:
24584 storage_class = sc_mutable;
24585 break;
24586 default:
24587 gcc_unreachable ();
24589 decl_specs->storage_class = storage_class;
24590 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24592 /* A storage class specifier cannot be applied alongside a typedef
24593 specifier. If there is a typedef specifier present then set
24594 conflicting_specifiers_p which will trigger an error later
24595 on in grokdeclarator. */
24596 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24597 decl_specs->conflicting_specifiers_p = true;
24600 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24601 is true, the type is a class or enum definition. */
24603 static void
24604 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24605 tree type_spec,
24606 cp_token *token,
24607 bool type_definition_p)
24609 decl_specs->any_specifiers_p = true;
24611 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24612 (with, for example, in "typedef int wchar_t;") we remember that
24613 this is what happened. In system headers, we ignore these
24614 declarations so that G++ can work with system headers that are not
24615 C++-safe. */
24616 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24617 && !type_definition_p
24618 && (type_spec == boolean_type_node
24619 || type_spec == char16_type_node
24620 || type_spec == char32_type_node
24621 || type_spec == wchar_type_node)
24622 && (decl_specs->type
24623 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24624 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24625 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24626 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24628 decl_specs->redefined_builtin_type = type_spec;
24629 set_and_check_decl_spec_loc (decl_specs,
24630 ds_redefined_builtin_type_spec,
24631 token);
24632 if (!decl_specs->type)
24634 decl_specs->type = type_spec;
24635 decl_specs->type_definition_p = false;
24636 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24639 else if (decl_specs->type)
24640 decl_specs->multiple_types_p = true;
24641 else
24643 decl_specs->type = type_spec;
24644 decl_specs->type_definition_p = type_definition_p;
24645 decl_specs->redefined_builtin_type = NULL_TREE;
24646 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24650 /* True iff TOKEN is the GNU keyword __thread. */
24652 static bool
24653 token_is__thread (cp_token *token)
24655 gcc_assert (token->keyword == RID_THREAD);
24656 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24659 /* Set the location for a declarator specifier and check if it is
24660 duplicated.
24662 DECL_SPECS is the sequence of declarator specifiers onto which to
24663 set the location.
24665 DS is the single declarator specifier to set which location is to
24666 be set onto the existing sequence of declarators.
24668 LOCATION is the location for the declarator specifier to
24669 consider. */
24671 static void
24672 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24673 cp_decl_spec ds, cp_token *token)
24675 gcc_assert (ds < ds_last);
24677 if (decl_specs == NULL)
24678 return;
24680 source_location location = token->location;
24682 if (decl_specs->locations[ds] == 0)
24684 decl_specs->locations[ds] = location;
24685 if (ds == ds_thread)
24686 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24688 else
24690 if (ds == ds_long)
24692 if (decl_specs->locations[ds_long_long] != 0)
24693 error_at (location,
24694 "%<long long long%> is too long for GCC");
24695 else
24697 decl_specs->locations[ds_long_long] = location;
24698 pedwarn_cxx98 (location,
24699 OPT_Wlong_long,
24700 "ISO C++ 1998 does not support %<long long%>");
24703 else if (ds == ds_thread)
24705 bool gnu = token_is__thread (token);
24706 if (gnu != decl_specs->gnu_thread_keyword_p)
24707 error_at (location,
24708 "both %<__thread%> and %<thread_local%> specified");
24709 else
24710 error_at (location, "duplicate %qD", token->u.value);
24712 else
24714 static const char *const decl_spec_names[] = {
24715 "signed",
24716 "unsigned",
24717 "short",
24718 "long",
24719 "const",
24720 "volatile",
24721 "restrict",
24722 "inline",
24723 "virtual",
24724 "explicit",
24725 "friend",
24726 "typedef",
24727 "using",
24728 "constexpr",
24729 "__complex"
24731 error_at (location,
24732 "duplicate %qs", decl_spec_names[ds]);
24737 /* Return true iff the declarator specifier DS is present in the
24738 sequence of declarator specifiers DECL_SPECS. */
24740 bool
24741 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24742 cp_decl_spec ds)
24744 gcc_assert (ds < ds_last);
24746 if (decl_specs == NULL)
24747 return false;
24749 return decl_specs->locations[ds] != 0;
24752 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24753 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24755 static bool
24756 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24758 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24761 /* Issue an error message indicating that TOKEN_DESC was expected.
24762 If KEYWORD is true, it indicated this function is called by
24763 cp_parser_require_keword and the required token can only be
24764 a indicated keyword. */
24766 static void
24767 cp_parser_required_error (cp_parser *parser,
24768 required_token token_desc,
24769 bool keyword)
24771 switch (token_desc)
24773 case RT_NEW:
24774 cp_parser_error (parser, "expected %<new%>");
24775 return;
24776 case RT_DELETE:
24777 cp_parser_error (parser, "expected %<delete%>");
24778 return;
24779 case RT_RETURN:
24780 cp_parser_error (parser, "expected %<return%>");
24781 return;
24782 case RT_WHILE:
24783 cp_parser_error (parser, "expected %<while%>");
24784 return;
24785 case RT_EXTERN:
24786 cp_parser_error (parser, "expected %<extern%>");
24787 return;
24788 case RT_STATIC_ASSERT:
24789 cp_parser_error (parser, "expected %<static_assert%>");
24790 return;
24791 case RT_DECLTYPE:
24792 cp_parser_error (parser, "expected %<decltype%>");
24793 return;
24794 case RT_OPERATOR:
24795 cp_parser_error (parser, "expected %<operator%>");
24796 return;
24797 case RT_CLASS:
24798 cp_parser_error (parser, "expected %<class%>");
24799 return;
24800 case RT_TEMPLATE:
24801 cp_parser_error (parser, "expected %<template%>");
24802 return;
24803 case RT_NAMESPACE:
24804 cp_parser_error (parser, "expected %<namespace%>");
24805 return;
24806 case RT_USING:
24807 cp_parser_error (parser, "expected %<using%>");
24808 return;
24809 case RT_ASM:
24810 cp_parser_error (parser, "expected %<asm%>");
24811 return;
24812 case RT_TRY:
24813 cp_parser_error (parser, "expected %<try%>");
24814 return;
24815 case RT_CATCH:
24816 cp_parser_error (parser, "expected %<catch%>");
24817 return;
24818 case RT_THROW:
24819 cp_parser_error (parser, "expected %<throw%>");
24820 return;
24821 case RT_LABEL:
24822 cp_parser_error (parser, "expected %<__label__%>");
24823 return;
24824 case RT_AT_TRY:
24825 cp_parser_error (parser, "expected %<@try%>");
24826 return;
24827 case RT_AT_SYNCHRONIZED:
24828 cp_parser_error (parser, "expected %<@synchronized%>");
24829 return;
24830 case RT_AT_THROW:
24831 cp_parser_error (parser, "expected %<@throw%>");
24832 return;
24833 case RT_TRANSACTION_ATOMIC:
24834 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24835 return;
24836 case RT_TRANSACTION_RELAXED:
24837 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24838 return;
24839 default:
24840 break;
24842 if (!keyword)
24844 switch (token_desc)
24846 case RT_SEMICOLON:
24847 cp_parser_error (parser, "expected %<;%>");
24848 return;
24849 case RT_OPEN_PAREN:
24850 cp_parser_error (parser, "expected %<(%>");
24851 return;
24852 case RT_CLOSE_BRACE:
24853 cp_parser_error (parser, "expected %<}%>");
24854 return;
24855 case RT_OPEN_BRACE:
24856 cp_parser_error (parser, "expected %<{%>");
24857 return;
24858 case RT_CLOSE_SQUARE:
24859 cp_parser_error (parser, "expected %<]%>");
24860 return;
24861 case RT_OPEN_SQUARE:
24862 cp_parser_error (parser, "expected %<[%>");
24863 return;
24864 case RT_COMMA:
24865 cp_parser_error (parser, "expected %<,%>");
24866 return;
24867 case RT_SCOPE:
24868 cp_parser_error (parser, "expected %<::%>");
24869 return;
24870 case RT_LESS:
24871 cp_parser_error (parser, "expected %<<%>");
24872 return;
24873 case RT_GREATER:
24874 cp_parser_error (parser, "expected %<>%>");
24875 return;
24876 case RT_EQ:
24877 cp_parser_error (parser, "expected %<=%>");
24878 return;
24879 case RT_ELLIPSIS:
24880 cp_parser_error (parser, "expected %<...%>");
24881 return;
24882 case RT_MULT:
24883 cp_parser_error (parser, "expected %<*%>");
24884 return;
24885 case RT_COMPL:
24886 cp_parser_error (parser, "expected %<~%>");
24887 return;
24888 case RT_COLON:
24889 cp_parser_error (parser, "expected %<:%>");
24890 return;
24891 case RT_COLON_SCOPE:
24892 cp_parser_error (parser, "expected %<:%> or %<::%>");
24893 return;
24894 case RT_CLOSE_PAREN:
24895 cp_parser_error (parser, "expected %<)%>");
24896 return;
24897 case RT_COMMA_CLOSE_PAREN:
24898 cp_parser_error (parser, "expected %<,%> or %<)%>");
24899 return;
24900 case RT_PRAGMA_EOL:
24901 cp_parser_error (parser, "expected end of line");
24902 return;
24903 case RT_NAME:
24904 cp_parser_error (parser, "expected identifier");
24905 return;
24906 case RT_SELECT:
24907 cp_parser_error (parser, "expected selection-statement");
24908 return;
24909 case RT_INTERATION:
24910 cp_parser_error (parser, "expected iteration-statement");
24911 return;
24912 case RT_JUMP:
24913 cp_parser_error (parser, "expected jump-statement");
24914 return;
24915 case RT_CLASS_KEY:
24916 cp_parser_error (parser, "expected class-key");
24917 return;
24918 case RT_CLASS_TYPENAME_TEMPLATE:
24919 cp_parser_error (parser,
24920 "expected %<class%>, %<typename%>, or %<template%>");
24921 return;
24922 default:
24923 gcc_unreachable ();
24926 else
24927 gcc_unreachable ();
24932 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24933 issue an error message indicating that TOKEN_DESC was expected.
24935 Returns the token consumed, if the token had the appropriate type.
24936 Otherwise, returns NULL. */
24938 static cp_token *
24939 cp_parser_require (cp_parser* parser,
24940 enum cpp_ttype type,
24941 required_token token_desc)
24943 if (cp_lexer_next_token_is (parser->lexer, type))
24944 return cp_lexer_consume_token (parser->lexer);
24945 else
24947 /* Output the MESSAGE -- unless we're parsing tentatively. */
24948 if (!cp_parser_simulate_error (parser))
24949 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24950 return NULL;
24954 /* An error message is produced if the next token is not '>'.
24955 All further tokens are skipped until the desired token is
24956 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24958 static void
24959 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24961 /* Current level of '< ... >'. */
24962 unsigned level = 0;
24963 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24964 unsigned nesting_depth = 0;
24966 /* Are we ready, yet? If not, issue error message. */
24967 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24968 return;
24970 /* Skip tokens until the desired token is found. */
24971 while (true)
24973 /* Peek at the next token. */
24974 switch (cp_lexer_peek_token (parser->lexer)->type)
24976 case CPP_LESS:
24977 if (!nesting_depth)
24978 ++level;
24979 break;
24981 case CPP_RSHIFT:
24982 if (cxx_dialect == cxx98)
24983 /* C++0x views the `>>' operator as two `>' tokens, but
24984 C++98 does not. */
24985 break;
24986 else if (!nesting_depth && level-- == 0)
24988 /* We've hit a `>>' where the first `>' closes the
24989 template argument list, and the second `>' is
24990 spurious. Just consume the `>>' and stop; we've
24991 already produced at least one error. */
24992 cp_lexer_consume_token (parser->lexer);
24993 return;
24995 /* Fall through for C++0x, so we handle the second `>' in
24996 the `>>'. */
24998 case CPP_GREATER:
24999 if (!nesting_depth && level-- == 0)
25001 /* We've reached the token we want, consume it and stop. */
25002 cp_lexer_consume_token (parser->lexer);
25003 return;
25005 break;
25007 case CPP_OPEN_PAREN:
25008 case CPP_OPEN_SQUARE:
25009 ++nesting_depth;
25010 break;
25012 case CPP_CLOSE_PAREN:
25013 case CPP_CLOSE_SQUARE:
25014 if (nesting_depth-- == 0)
25015 return;
25016 break;
25018 case CPP_EOF:
25019 case CPP_PRAGMA_EOL:
25020 case CPP_SEMICOLON:
25021 case CPP_OPEN_BRACE:
25022 case CPP_CLOSE_BRACE:
25023 /* The '>' was probably forgotten, don't look further. */
25024 return;
25026 default:
25027 break;
25030 /* Consume this token. */
25031 cp_lexer_consume_token (parser->lexer);
25035 /* If the next token is the indicated keyword, consume it. Otherwise,
25036 issue an error message indicating that TOKEN_DESC was expected.
25038 Returns the token consumed, if the token had the appropriate type.
25039 Otherwise, returns NULL. */
25041 static cp_token *
25042 cp_parser_require_keyword (cp_parser* parser,
25043 enum rid keyword,
25044 required_token token_desc)
25046 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25048 if (token && token->keyword != keyword)
25050 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25051 return NULL;
25054 return token;
25057 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25058 function-definition. */
25060 static bool
25061 cp_parser_token_starts_function_definition_p (cp_token* token)
25063 return (/* An ordinary function-body begins with an `{'. */
25064 token->type == CPP_OPEN_BRACE
25065 /* A ctor-initializer begins with a `:'. */
25066 || token->type == CPP_COLON
25067 /* A function-try-block begins with `try'. */
25068 || token->keyword == RID_TRY
25069 /* A function-transaction-block begins with `__transaction_atomic'
25070 or `__transaction_relaxed'. */
25071 || token->keyword == RID_TRANSACTION_ATOMIC
25072 || token->keyword == RID_TRANSACTION_RELAXED
25073 /* The named return value extension begins with `return'. */
25074 || token->keyword == RID_RETURN);
25077 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25078 definition. */
25080 static bool
25081 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25083 cp_token *token;
25085 token = cp_lexer_peek_token (parser->lexer);
25086 return (token->type == CPP_OPEN_BRACE
25087 || (token->type == CPP_COLON
25088 && !parser->colon_doesnt_start_class_def_p));
25091 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25092 C++0x) ending a template-argument. */
25094 static bool
25095 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25097 cp_token *token;
25099 token = cp_lexer_peek_token (parser->lexer);
25100 return (token->type == CPP_COMMA
25101 || token->type == CPP_GREATER
25102 || token->type == CPP_ELLIPSIS
25103 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25106 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25107 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25109 static bool
25110 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25111 size_t n)
25113 cp_token *token;
25115 token = cp_lexer_peek_nth_token (parser->lexer, n);
25116 if (token->type == CPP_LESS)
25117 return true;
25118 /* Check for the sequence `<::' in the original code. It would be lexed as
25119 `[:', where `[' is a digraph, and there is no whitespace before
25120 `:'. */
25121 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25123 cp_token *token2;
25124 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25125 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25126 return true;
25128 return false;
25131 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25132 or none_type otherwise. */
25134 static enum tag_types
25135 cp_parser_token_is_class_key (cp_token* token)
25137 switch (token->keyword)
25139 case RID_CLASS:
25140 return class_type;
25141 case RID_STRUCT:
25142 return record_type;
25143 case RID_UNION:
25144 return union_type;
25146 default:
25147 return none_type;
25151 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25152 or none_type otherwise or if the token is null. */
25154 static enum tag_types
25155 cp_parser_token_is_type_parameter_key (cp_token* token)
25157 if (!token)
25158 return none_type;
25160 switch (token->keyword)
25162 case RID_CLASS:
25163 return class_type;
25164 case RID_TYPENAME:
25165 return typename_type;
25167 default:
25168 return none_type;
25172 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25174 static void
25175 cp_parser_check_class_key (enum tag_types class_key, tree type)
25177 if (type == error_mark_node)
25178 return;
25179 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25181 if (permerror (input_location, "%qs tag used in naming %q#T",
25182 class_key == union_type ? "union"
25183 : class_key == record_type ? "struct" : "class",
25184 type))
25185 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25186 "%q#T was previously declared here", type);
25190 /* Issue an error message if DECL is redeclared with different
25191 access than its original declaration [class.access.spec/3].
25192 This applies to nested classes and nested class templates.
25193 [class.mem/1]. */
25195 static void
25196 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25198 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25199 return;
25201 if ((TREE_PRIVATE (decl)
25202 != (current_access_specifier == access_private_node))
25203 || (TREE_PROTECTED (decl)
25204 != (current_access_specifier == access_protected_node)))
25205 error_at (location, "%qD redeclared with different access", decl);
25208 /* Look for the `template' keyword, as a syntactic disambiguator.
25209 Return TRUE iff it is present, in which case it will be
25210 consumed. */
25212 static bool
25213 cp_parser_optional_template_keyword (cp_parser *parser)
25215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25217 /* In C++98 the `template' keyword can only be used within templates;
25218 outside templates the parser can always figure out what is a
25219 template and what is not. In C++11, per the resolution of DR 468,
25220 `template' is allowed in cases where it is not strictly necessary. */
25221 if (!processing_template_decl
25222 && pedantic && cxx_dialect == cxx98)
25224 cp_token *token = cp_lexer_peek_token (parser->lexer);
25225 pedwarn (token->location, OPT_Wpedantic,
25226 "in C++98 %<template%> (as a disambiguator) is only "
25227 "allowed within templates");
25228 /* If this part of the token stream is rescanned, the same
25229 error message would be generated. So, we purge the token
25230 from the stream. */
25231 cp_lexer_purge_token (parser->lexer);
25232 return false;
25234 else
25236 /* Consume the `template' keyword. */
25237 cp_lexer_consume_token (parser->lexer);
25238 return true;
25241 return false;
25244 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25245 set PARSER->SCOPE, and perform other related actions. */
25247 static void
25248 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25250 int i;
25251 struct tree_check *check_value;
25252 deferred_access_check *chk;
25253 vec<deferred_access_check, va_gc> *checks;
25255 /* Get the stored value. */
25256 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25257 /* Perform any access checks that were deferred. */
25258 checks = check_value->checks;
25259 if (checks)
25261 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25262 perform_or_defer_access_check (chk->binfo,
25263 chk->decl,
25264 chk->diag_decl, tf_warning_or_error);
25266 /* Set the scope from the stored value. */
25267 parser->scope = check_value->value;
25268 parser->qualifying_scope = check_value->qualifying_scope;
25269 parser->object_scope = NULL_TREE;
25272 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25273 encounter the end of a block before what we were looking for. */
25275 static bool
25276 cp_parser_cache_group (cp_parser *parser,
25277 enum cpp_ttype end,
25278 unsigned depth)
25280 while (true)
25282 cp_token *token = cp_lexer_peek_token (parser->lexer);
25284 /* Abort a parenthesized expression if we encounter a semicolon. */
25285 if ((end == CPP_CLOSE_PAREN || depth == 0)
25286 && token->type == CPP_SEMICOLON)
25287 return true;
25288 /* If we've reached the end of the file, stop. */
25289 if (token->type == CPP_EOF
25290 || (end != CPP_PRAGMA_EOL
25291 && token->type == CPP_PRAGMA_EOL))
25292 return true;
25293 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25294 /* We've hit the end of an enclosing block, so there's been some
25295 kind of syntax error. */
25296 return true;
25298 /* Consume the token. */
25299 cp_lexer_consume_token (parser->lexer);
25300 /* See if it starts a new group. */
25301 if (token->type == CPP_OPEN_BRACE)
25303 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25304 /* In theory this should probably check end == '}', but
25305 cp_parser_save_member_function_body needs it to exit
25306 after either '}' or ')' when called with ')'. */
25307 if (depth == 0)
25308 return false;
25310 else if (token->type == CPP_OPEN_PAREN)
25312 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25313 if (depth == 0 && end == CPP_CLOSE_PAREN)
25314 return false;
25316 else if (token->type == CPP_PRAGMA)
25317 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25318 else if (token->type == end)
25319 return false;
25323 /* Like above, for caching a default argument or NSDMI. Both of these are
25324 terminated by a non-nested comma, but it can be unclear whether or not a
25325 comma is nested in a template argument list unless we do more parsing.
25326 In order to handle this ambiguity, when we encounter a ',' after a '<'
25327 we try to parse what follows as a parameter-declaration-list (in the
25328 case of a default argument) or a member-declarator (in the case of an
25329 NSDMI). If that succeeds, then we stop caching. */
25331 static tree
25332 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25334 unsigned depth = 0;
25335 int maybe_template_id = 0;
25336 cp_token *first_token;
25337 cp_token *token;
25338 tree default_argument;
25340 /* Add tokens until we have processed the entire default
25341 argument. We add the range [first_token, token). */
25342 first_token = cp_lexer_peek_token (parser->lexer);
25343 if (first_token->type == CPP_OPEN_BRACE)
25345 /* For list-initialization, this is straightforward. */
25346 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25347 token = cp_lexer_peek_token (parser->lexer);
25349 else while (true)
25351 bool done = false;
25353 /* Peek at the next token. */
25354 token = cp_lexer_peek_token (parser->lexer);
25355 /* What we do depends on what token we have. */
25356 switch (token->type)
25358 /* In valid code, a default argument must be
25359 immediately followed by a `,' `)', or `...'. */
25360 case CPP_COMMA:
25361 if (depth == 0 && maybe_template_id)
25363 /* If we've seen a '<', we might be in a
25364 template-argument-list. Until Core issue 325 is
25365 resolved, we don't know how this situation ought
25366 to be handled, so try to DTRT. We check whether
25367 what comes after the comma is a valid parameter
25368 declaration list. If it is, then the comma ends
25369 the default argument; otherwise the default
25370 argument continues. */
25371 bool error = false;
25373 /* Set ITALP so cp_parser_parameter_declaration_list
25374 doesn't decide to commit to this parse. */
25375 bool saved_italp = parser->in_template_argument_list_p;
25376 parser->in_template_argument_list_p = true;
25378 cp_parser_parse_tentatively (parser);
25379 cp_lexer_consume_token (parser->lexer);
25381 if (nsdmi)
25383 int ctor_dtor_or_conv_p;
25384 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25385 &ctor_dtor_or_conv_p,
25386 /*parenthesized_p=*/NULL,
25387 /*member_p=*/true,
25388 /*friend_p=*/false);
25390 else
25392 begin_scope (sk_function_parms, NULL_TREE);
25393 cp_parser_parameter_declaration_list (parser, &error);
25394 pop_bindings_and_leave_scope ();
25396 if (!cp_parser_error_occurred (parser) && !error)
25397 done = true;
25398 cp_parser_abort_tentative_parse (parser);
25400 parser->in_template_argument_list_p = saved_italp;
25401 break;
25403 case CPP_CLOSE_PAREN:
25404 case CPP_ELLIPSIS:
25405 /* If we run into a non-nested `;', `}', or `]',
25406 then the code is invalid -- but the default
25407 argument is certainly over. */
25408 case CPP_SEMICOLON:
25409 case CPP_CLOSE_BRACE:
25410 case CPP_CLOSE_SQUARE:
25411 if (depth == 0
25412 /* Handle correctly int n = sizeof ... ( p ); */
25413 && token->type != CPP_ELLIPSIS)
25414 done = true;
25415 /* Update DEPTH, if necessary. */
25416 else if (token->type == CPP_CLOSE_PAREN
25417 || token->type == CPP_CLOSE_BRACE
25418 || token->type == CPP_CLOSE_SQUARE)
25419 --depth;
25420 break;
25422 case CPP_OPEN_PAREN:
25423 case CPP_OPEN_SQUARE:
25424 case CPP_OPEN_BRACE:
25425 ++depth;
25426 break;
25428 case CPP_LESS:
25429 if (depth == 0)
25430 /* This might be the comparison operator, or it might
25431 start a template argument list. */
25432 ++maybe_template_id;
25433 break;
25435 case CPP_RSHIFT:
25436 if (cxx_dialect == cxx98)
25437 break;
25438 /* Fall through for C++0x, which treats the `>>'
25439 operator like two `>' tokens in certain
25440 cases. */
25442 case CPP_GREATER:
25443 if (depth == 0)
25445 /* This might be an operator, or it might close a
25446 template argument list. But if a previous '<'
25447 started a template argument list, this will have
25448 closed it, so we can't be in one anymore. */
25449 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25450 if (maybe_template_id < 0)
25451 maybe_template_id = 0;
25453 break;
25455 /* If we run out of tokens, issue an error message. */
25456 case CPP_EOF:
25457 case CPP_PRAGMA_EOL:
25458 error_at (token->location, "file ends in default argument");
25459 done = true;
25460 break;
25462 case CPP_NAME:
25463 case CPP_SCOPE:
25464 /* In these cases, we should look for template-ids.
25465 For example, if the default argument is
25466 `X<int, double>()', we need to do name lookup to
25467 figure out whether or not `X' is a template; if
25468 so, the `,' does not end the default argument.
25470 That is not yet done. */
25471 break;
25473 default:
25474 break;
25477 /* If we've reached the end, stop. */
25478 if (done)
25479 break;
25481 /* Add the token to the token block. */
25482 token = cp_lexer_consume_token (parser->lexer);
25485 /* Create a DEFAULT_ARG to represent the unparsed default
25486 argument. */
25487 default_argument = make_node (DEFAULT_ARG);
25488 DEFARG_TOKENS (default_argument)
25489 = cp_token_cache_new (first_token, token);
25490 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25492 return default_argument;
25495 /* Begin parsing tentatively. We always save tokens while parsing
25496 tentatively so that if the tentative parsing fails we can restore the
25497 tokens. */
25499 static void
25500 cp_parser_parse_tentatively (cp_parser* parser)
25502 /* Enter a new parsing context. */
25503 parser->context = cp_parser_context_new (parser->context);
25504 /* Begin saving tokens. */
25505 cp_lexer_save_tokens (parser->lexer);
25506 /* In order to avoid repetitive access control error messages,
25507 access checks are queued up until we are no longer parsing
25508 tentatively. */
25509 push_deferring_access_checks (dk_deferred);
25512 /* Commit to the currently active tentative parse. */
25514 static void
25515 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25517 cp_parser_context *context;
25518 cp_lexer *lexer;
25520 /* Mark all of the levels as committed. */
25521 lexer = parser->lexer;
25522 for (context = parser->context; context->next; context = context->next)
25524 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25525 break;
25526 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25527 while (!cp_lexer_saving_tokens (lexer))
25528 lexer = lexer->next;
25529 cp_lexer_commit_tokens (lexer);
25533 /* Commit to the topmost currently active tentative parse.
25535 Note that this function shouldn't be called when there are
25536 irreversible side-effects while in a tentative state. For
25537 example, we shouldn't create a permanent entry in the symbol
25538 table, or issue an error message that might not apply if the
25539 tentative parse is aborted. */
25541 static void
25542 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25544 cp_parser_context *context = parser->context;
25545 cp_lexer *lexer = parser->lexer;
25547 if (context)
25549 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25550 return;
25551 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25553 while (!cp_lexer_saving_tokens (lexer))
25554 lexer = lexer->next;
25555 cp_lexer_commit_tokens (lexer);
25559 /* Abort the currently active tentative parse. All consumed tokens
25560 will be rolled back, and no diagnostics will be issued. */
25562 static void
25563 cp_parser_abort_tentative_parse (cp_parser* parser)
25565 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25566 || errorcount > 0);
25567 cp_parser_simulate_error (parser);
25568 /* Now, pretend that we want to see if the construct was
25569 successfully parsed. */
25570 cp_parser_parse_definitely (parser);
25573 /* Stop parsing tentatively. If a parse error has occurred, restore the
25574 token stream. Otherwise, commit to the tokens we have consumed.
25575 Returns true if no error occurred; false otherwise. */
25577 static bool
25578 cp_parser_parse_definitely (cp_parser* parser)
25580 bool error_occurred;
25581 cp_parser_context *context;
25583 /* Remember whether or not an error occurred, since we are about to
25584 destroy that information. */
25585 error_occurred = cp_parser_error_occurred (parser);
25586 /* Remove the topmost context from the stack. */
25587 context = parser->context;
25588 parser->context = context->next;
25589 /* If no parse errors occurred, commit to the tentative parse. */
25590 if (!error_occurred)
25592 /* Commit to the tokens read tentatively, unless that was
25593 already done. */
25594 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25595 cp_lexer_commit_tokens (parser->lexer);
25597 pop_to_parent_deferring_access_checks ();
25599 /* Otherwise, if errors occurred, roll back our state so that things
25600 are just as they were before we began the tentative parse. */
25601 else
25603 cp_lexer_rollback_tokens (parser->lexer);
25604 pop_deferring_access_checks ();
25606 /* Add the context to the front of the free list. */
25607 context->next = cp_parser_context_free_list;
25608 cp_parser_context_free_list = context;
25610 return !error_occurred;
25613 /* Returns true if we are parsing tentatively and are not committed to
25614 this tentative parse. */
25616 static bool
25617 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25619 return (cp_parser_parsing_tentatively (parser)
25620 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25623 /* Returns nonzero iff an error has occurred during the most recent
25624 tentative parse. */
25626 static bool
25627 cp_parser_error_occurred (cp_parser* parser)
25629 return (cp_parser_parsing_tentatively (parser)
25630 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25633 /* Returns nonzero if GNU extensions are allowed. */
25635 static bool
25636 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25638 return parser->allow_gnu_extensions_p;
25641 /* Objective-C++ Productions */
25644 /* Parse an Objective-C expression, which feeds into a primary-expression
25645 above.
25647 objc-expression:
25648 objc-message-expression
25649 objc-string-literal
25650 objc-encode-expression
25651 objc-protocol-expression
25652 objc-selector-expression
25654 Returns a tree representation of the expression. */
25656 static tree
25657 cp_parser_objc_expression (cp_parser* parser)
25659 /* Try to figure out what kind of declaration is present. */
25660 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25662 switch (kwd->type)
25664 case CPP_OPEN_SQUARE:
25665 return cp_parser_objc_message_expression (parser);
25667 case CPP_OBJC_STRING:
25668 kwd = cp_lexer_consume_token (parser->lexer);
25669 return objc_build_string_object (kwd->u.value);
25671 case CPP_KEYWORD:
25672 switch (kwd->keyword)
25674 case RID_AT_ENCODE:
25675 return cp_parser_objc_encode_expression (parser);
25677 case RID_AT_PROTOCOL:
25678 return cp_parser_objc_protocol_expression (parser);
25680 case RID_AT_SELECTOR:
25681 return cp_parser_objc_selector_expression (parser);
25683 default:
25684 break;
25686 default:
25687 error_at (kwd->location,
25688 "misplaced %<@%D%> Objective-C++ construct",
25689 kwd->u.value);
25690 cp_parser_skip_to_end_of_block_or_statement (parser);
25693 return error_mark_node;
25696 /* Parse an Objective-C message expression.
25698 objc-message-expression:
25699 [ objc-message-receiver objc-message-args ]
25701 Returns a representation of an Objective-C message. */
25703 static tree
25704 cp_parser_objc_message_expression (cp_parser* parser)
25706 tree receiver, messageargs;
25708 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25709 receiver = cp_parser_objc_message_receiver (parser);
25710 messageargs = cp_parser_objc_message_args (parser);
25711 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25713 return objc_build_message_expr (receiver, messageargs);
25716 /* Parse an objc-message-receiver.
25718 objc-message-receiver:
25719 expression
25720 simple-type-specifier
25722 Returns a representation of the type or expression. */
25724 static tree
25725 cp_parser_objc_message_receiver (cp_parser* parser)
25727 tree rcv;
25729 /* An Objective-C message receiver may be either (1) a type
25730 or (2) an expression. */
25731 cp_parser_parse_tentatively (parser);
25732 rcv = cp_parser_expression (parser);
25734 /* If that worked out, fine. */
25735 if (cp_parser_parse_definitely (parser))
25736 return rcv;
25738 cp_parser_parse_tentatively (parser);
25739 rcv = cp_parser_simple_type_specifier (parser,
25740 /*decl_specs=*/NULL,
25741 CP_PARSER_FLAGS_NONE);
25743 if (cp_parser_parse_definitely (parser))
25744 return objc_get_class_reference (rcv);
25746 cp_parser_error (parser, "objective-c++ message receiver expected");
25747 return error_mark_node;
25750 /* Parse the arguments and selectors comprising an Objective-C message.
25752 objc-message-args:
25753 objc-selector
25754 objc-selector-args
25755 objc-selector-args , objc-comma-args
25757 objc-selector-args:
25758 objc-selector [opt] : assignment-expression
25759 objc-selector-args objc-selector [opt] : assignment-expression
25761 objc-comma-args:
25762 assignment-expression
25763 objc-comma-args , assignment-expression
25765 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25766 selector arguments and TREE_VALUE containing a list of comma
25767 arguments. */
25769 static tree
25770 cp_parser_objc_message_args (cp_parser* parser)
25772 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25773 bool maybe_unary_selector_p = true;
25774 cp_token *token = cp_lexer_peek_token (parser->lexer);
25776 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25778 tree selector = NULL_TREE, arg;
25780 if (token->type != CPP_COLON)
25781 selector = cp_parser_objc_selector (parser);
25783 /* Detect if we have a unary selector. */
25784 if (maybe_unary_selector_p
25785 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25786 return build_tree_list (selector, NULL_TREE);
25788 maybe_unary_selector_p = false;
25789 cp_parser_require (parser, CPP_COLON, RT_COLON);
25790 arg = cp_parser_assignment_expression (parser);
25792 sel_args
25793 = chainon (sel_args,
25794 build_tree_list (selector, arg));
25796 token = cp_lexer_peek_token (parser->lexer);
25799 /* Handle non-selector arguments, if any. */
25800 while (token->type == CPP_COMMA)
25802 tree arg;
25804 cp_lexer_consume_token (parser->lexer);
25805 arg = cp_parser_assignment_expression (parser);
25807 addl_args
25808 = chainon (addl_args,
25809 build_tree_list (NULL_TREE, arg));
25811 token = cp_lexer_peek_token (parser->lexer);
25814 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25816 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25817 return build_tree_list (error_mark_node, error_mark_node);
25820 return build_tree_list (sel_args, addl_args);
25823 /* Parse an Objective-C encode expression.
25825 objc-encode-expression:
25826 @encode objc-typename
25828 Returns an encoded representation of the type argument. */
25830 static tree
25831 cp_parser_objc_encode_expression (cp_parser* parser)
25833 tree type;
25834 cp_token *token;
25836 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25838 token = cp_lexer_peek_token (parser->lexer);
25839 type = complete_type (cp_parser_type_id (parser));
25840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25842 if (!type)
25844 error_at (token->location,
25845 "%<@encode%> must specify a type as an argument");
25846 return error_mark_node;
25849 /* This happens if we find @encode(T) (where T is a template
25850 typename or something dependent on a template typename) when
25851 parsing a template. In that case, we can't compile it
25852 immediately, but we rather create an AT_ENCODE_EXPR which will
25853 need to be instantiated when the template is used.
25855 if (dependent_type_p (type))
25857 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25858 TREE_READONLY (value) = 1;
25859 return value;
25862 return objc_build_encode_expr (type);
25865 /* Parse an Objective-C @defs expression. */
25867 static tree
25868 cp_parser_objc_defs_expression (cp_parser *parser)
25870 tree name;
25872 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25873 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25874 name = cp_parser_identifier (parser);
25875 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25877 return objc_get_class_ivars (name);
25880 /* Parse an Objective-C protocol expression.
25882 objc-protocol-expression:
25883 @protocol ( identifier )
25885 Returns a representation of the protocol expression. */
25887 static tree
25888 cp_parser_objc_protocol_expression (cp_parser* parser)
25890 tree proto;
25892 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25893 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25894 proto = cp_parser_identifier (parser);
25895 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25897 return objc_build_protocol_expr (proto);
25900 /* Parse an Objective-C selector expression.
25902 objc-selector-expression:
25903 @selector ( objc-method-signature )
25905 objc-method-signature:
25906 objc-selector
25907 objc-selector-seq
25909 objc-selector-seq:
25910 objc-selector :
25911 objc-selector-seq objc-selector :
25913 Returns a representation of the method selector. */
25915 static tree
25916 cp_parser_objc_selector_expression (cp_parser* parser)
25918 tree sel_seq = NULL_TREE;
25919 bool maybe_unary_selector_p = true;
25920 cp_token *token;
25921 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25923 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25924 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25925 token = cp_lexer_peek_token (parser->lexer);
25927 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25928 || token->type == CPP_SCOPE)
25930 tree selector = NULL_TREE;
25932 if (token->type != CPP_COLON
25933 || token->type == CPP_SCOPE)
25934 selector = cp_parser_objc_selector (parser);
25936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25937 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25939 /* Detect if we have a unary selector. */
25940 if (maybe_unary_selector_p)
25942 sel_seq = selector;
25943 goto finish_selector;
25945 else
25947 cp_parser_error (parser, "expected %<:%>");
25950 maybe_unary_selector_p = false;
25951 token = cp_lexer_consume_token (parser->lexer);
25953 if (token->type == CPP_SCOPE)
25955 sel_seq
25956 = chainon (sel_seq,
25957 build_tree_list (selector, NULL_TREE));
25958 sel_seq
25959 = chainon (sel_seq,
25960 build_tree_list (NULL_TREE, NULL_TREE));
25962 else
25963 sel_seq
25964 = chainon (sel_seq,
25965 build_tree_list (selector, NULL_TREE));
25967 token = cp_lexer_peek_token (parser->lexer);
25970 finish_selector:
25971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25973 return objc_build_selector_expr (loc, sel_seq);
25976 /* Parse a list of identifiers.
25978 objc-identifier-list:
25979 identifier
25980 objc-identifier-list , identifier
25982 Returns a TREE_LIST of identifier nodes. */
25984 static tree
25985 cp_parser_objc_identifier_list (cp_parser* parser)
25987 tree identifier;
25988 tree list;
25989 cp_token *sep;
25991 identifier = cp_parser_identifier (parser);
25992 if (identifier == error_mark_node)
25993 return error_mark_node;
25995 list = build_tree_list (NULL_TREE, identifier);
25996 sep = cp_lexer_peek_token (parser->lexer);
25998 while (sep->type == CPP_COMMA)
26000 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26001 identifier = cp_parser_identifier (parser);
26002 if (identifier == error_mark_node)
26003 return list;
26005 list = chainon (list, build_tree_list (NULL_TREE,
26006 identifier));
26007 sep = cp_lexer_peek_token (parser->lexer);
26010 return list;
26013 /* Parse an Objective-C alias declaration.
26015 objc-alias-declaration:
26016 @compatibility_alias identifier identifier ;
26018 This function registers the alias mapping with the Objective-C front end.
26019 It returns nothing. */
26021 static void
26022 cp_parser_objc_alias_declaration (cp_parser* parser)
26024 tree alias, orig;
26026 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26027 alias = cp_parser_identifier (parser);
26028 orig = cp_parser_identifier (parser);
26029 objc_declare_alias (alias, orig);
26030 cp_parser_consume_semicolon_at_end_of_statement (parser);
26033 /* Parse an Objective-C class forward-declaration.
26035 objc-class-declaration:
26036 @class objc-identifier-list ;
26038 The function registers the forward declarations with the Objective-C
26039 front end. It returns nothing. */
26041 static void
26042 cp_parser_objc_class_declaration (cp_parser* parser)
26044 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26045 while (true)
26047 tree id;
26049 id = cp_parser_identifier (parser);
26050 if (id == error_mark_node)
26051 break;
26053 objc_declare_class (id);
26055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26056 cp_lexer_consume_token (parser->lexer);
26057 else
26058 break;
26060 cp_parser_consume_semicolon_at_end_of_statement (parser);
26063 /* Parse a list of Objective-C protocol references.
26065 objc-protocol-refs-opt:
26066 objc-protocol-refs [opt]
26068 objc-protocol-refs:
26069 < objc-identifier-list >
26071 Returns a TREE_LIST of identifiers, if any. */
26073 static tree
26074 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26076 tree protorefs = NULL_TREE;
26078 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26080 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26081 protorefs = cp_parser_objc_identifier_list (parser);
26082 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26085 return protorefs;
26088 /* Parse a Objective-C visibility specification. */
26090 static void
26091 cp_parser_objc_visibility_spec (cp_parser* parser)
26093 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26095 switch (vis->keyword)
26097 case RID_AT_PRIVATE:
26098 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26099 break;
26100 case RID_AT_PROTECTED:
26101 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26102 break;
26103 case RID_AT_PUBLIC:
26104 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26105 break;
26106 case RID_AT_PACKAGE:
26107 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26108 break;
26109 default:
26110 return;
26113 /* Eat '@private'/'@protected'/'@public'. */
26114 cp_lexer_consume_token (parser->lexer);
26117 /* Parse an Objective-C method type. Return 'true' if it is a class
26118 (+) method, and 'false' if it is an instance (-) method. */
26120 static inline bool
26121 cp_parser_objc_method_type (cp_parser* parser)
26123 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26124 return true;
26125 else
26126 return false;
26129 /* Parse an Objective-C protocol qualifier. */
26131 static tree
26132 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26134 tree quals = NULL_TREE, node;
26135 cp_token *token = cp_lexer_peek_token (parser->lexer);
26137 node = token->u.value;
26139 while (node && identifier_p (node)
26140 && (node == ridpointers [(int) RID_IN]
26141 || node == ridpointers [(int) RID_OUT]
26142 || node == ridpointers [(int) RID_INOUT]
26143 || node == ridpointers [(int) RID_BYCOPY]
26144 || node == ridpointers [(int) RID_BYREF]
26145 || node == ridpointers [(int) RID_ONEWAY]))
26147 quals = tree_cons (NULL_TREE, node, quals);
26148 cp_lexer_consume_token (parser->lexer);
26149 token = cp_lexer_peek_token (parser->lexer);
26150 node = token->u.value;
26153 return quals;
26156 /* Parse an Objective-C typename. */
26158 static tree
26159 cp_parser_objc_typename (cp_parser* parser)
26161 tree type_name = NULL_TREE;
26163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26165 tree proto_quals, cp_type = NULL_TREE;
26167 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26168 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26170 /* An ObjC type name may consist of just protocol qualifiers, in which
26171 case the type shall default to 'id'. */
26172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26174 cp_type = cp_parser_type_id (parser);
26176 /* If the type could not be parsed, an error has already
26177 been produced. For error recovery, behave as if it had
26178 not been specified, which will use the default type
26179 'id'. */
26180 if (cp_type == error_mark_node)
26182 cp_type = NULL_TREE;
26183 /* We need to skip to the closing parenthesis as
26184 cp_parser_type_id() does not seem to do it for
26185 us. */
26186 cp_parser_skip_to_closing_parenthesis (parser,
26187 /*recovering=*/true,
26188 /*or_comma=*/false,
26189 /*consume_paren=*/false);
26193 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26194 type_name = build_tree_list (proto_quals, cp_type);
26197 return type_name;
26200 /* Check to see if TYPE refers to an Objective-C selector name. */
26202 static bool
26203 cp_parser_objc_selector_p (enum cpp_ttype type)
26205 return (type == CPP_NAME || type == CPP_KEYWORD
26206 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26207 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26208 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26209 || type == CPP_XOR || type == CPP_XOR_EQ);
26212 /* Parse an Objective-C selector. */
26214 static tree
26215 cp_parser_objc_selector (cp_parser* parser)
26217 cp_token *token = cp_lexer_consume_token (parser->lexer);
26219 if (!cp_parser_objc_selector_p (token->type))
26221 error_at (token->location, "invalid Objective-C++ selector name");
26222 return error_mark_node;
26225 /* C++ operator names are allowed to appear in ObjC selectors. */
26226 switch (token->type)
26228 case CPP_AND_AND: return get_identifier ("and");
26229 case CPP_AND_EQ: return get_identifier ("and_eq");
26230 case CPP_AND: return get_identifier ("bitand");
26231 case CPP_OR: return get_identifier ("bitor");
26232 case CPP_COMPL: return get_identifier ("compl");
26233 case CPP_NOT: return get_identifier ("not");
26234 case CPP_NOT_EQ: return get_identifier ("not_eq");
26235 case CPP_OR_OR: return get_identifier ("or");
26236 case CPP_OR_EQ: return get_identifier ("or_eq");
26237 case CPP_XOR: return get_identifier ("xor");
26238 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26239 default: return token->u.value;
26243 /* Parse an Objective-C params list. */
26245 static tree
26246 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26248 tree params = NULL_TREE;
26249 bool maybe_unary_selector_p = true;
26250 cp_token *token = cp_lexer_peek_token (parser->lexer);
26252 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26254 tree selector = NULL_TREE, type_name, identifier;
26255 tree parm_attr = NULL_TREE;
26257 if (token->keyword == RID_ATTRIBUTE)
26258 break;
26260 if (token->type != CPP_COLON)
26261 selector = cp_parser_objc_selector (parser);
26263 /* Detect if we have a unary selector. */
26264 if (maybe_unary_selector_p
26265 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26267 params = selector; /* Might be followed by attributes. */
26268 break;
26271 maybe_unary_selector_p = false;
26272 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26274 /* Something went quite wrong. There should be a colon
26275 here, but there is not. Stop parsing parameters. */
26276 break;
26278 type_name = cp_parser_objc_typename (parser);
26279 /* New ObjC allows attributes on parameters too. */
26280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26281 parm_attr = cp_parser_attributes_opt (parser);
26282 identifier = cp_parser_identifier (parser);
26284 params
26285 = chainon (params,
26286 objc_build_keyword_decl (selector,
26287 type_name,
26288 identifier,
26289 parm_attr));
26291 token = cp_lexer_peek_token (parser->lexer);
26294 if (params == NULL_TREE)
26296 cp_parser_error (parser, "objective-c++ method declaration is expected");
26297 return error_mark_node;
26300 /* We allow tail attributes for the method. */
26301 if (token->keyword == RID_ATTRIBUTE)
26303 *attributes = cp_parser_attributes_opt (parser);
26304 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26305 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26306 return params;
26307 cp_parser_error (parser,
26308 "method attributes must be specified at the end");
26309 return error_mark_node;
26312 if (params == NULL_TREE)
26314 cp_parser_error (parser, "objective-c++ method declaration is expected");
26315 return error_mark_node;
26317 return params;
26320 /* Parse the non-keyword Objective-C params. */
26322 static tree
26323 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26324 tree* attributes)
26326 tree params = make_node (TREE_LIST);
26327 cp_token *token = cp_lexer_peek_token (parser->lexer);
26328 *ellipsisp = false; /* Initially, assume no ellipsis. */
26330 while (token->type == CPP_COMMA)
26332 cp_parameter_declarator *parmdecl;
26333 tree parm;
26335 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26336 token = cp_lexer_peek_token (parser->lexer);
26338 if (token->type == CPP_ELLIPSIS)
26340 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26341 *ellipsisp = true;
26342 token = cp_lexer_peek_token (parser->lexer);
26343 break;
26346 /* TODO: parse attributes for tail parameters. */
26347 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26348 parm = grokdeclarator (parmdecl->declarator,
26349 &parmdecl->decl_specifiers,
26350 PARM, /*initialized=*/0,
26351 /*attrlist=*/NULL);
26353 chainon (params, build_tree_list (NULL_TREE, parm));
26354 token = cp_lexer_peek_token (parser->lexer);
26357 /* We allow tail attributes for the method. */
26358 if (token->keyword == RID_ATTRIBUTE)
26360 if (*attributes == NULL_TREE)
26362 *attributes = cp_parser_attributes_opt (parser);
26363 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26364 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26365 return params;
26367 else
26368 /* We have an error, but parse the attributes, so that we can
26369 carry on. */
26370 *attributes = cp_parser_attributes_opt (parser);
26372 cp_parser_error (parser,
26373 "method attributes must be specified at the end");
26374 return error_mark_node;
26377 return params;
26380 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26382 static void
26383 cp_parser_objc_interstitial_code (cp_parser* parser)
26385 cp_token *token = cp_lexer_peek_token (parser->lexer);
26387 /* If the next token is `extern' and the following token is a string
26388 literal, then we have a linkage specification. */
26389 if (token->keyword == RID_EXTERN
26390 && cp_parser_is_pure_string_literal
26391 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26392 cp_parser_linkage_specification (parser);
26393 /* Handle #pragma, if any. */
26394 else if (token->type == CPP_PRAGMA)
26395 cp_parser_pragma (parser, pragma_objc_icode);
26396 /* Allow stray semicolons. */
26397 else if (token->type == CPP_SEMICOLON)
26398 cp_lexer_consume_token (parser->lexer);
26399 /* Mark methods as optional or required, when building protocols. */
26400 else if (token->keyword == RID_AT_OPTIONAL)
26402 cp_lexer_consume_token (parser->lexer);
26403 objc_set_method_opt (true);
26405 else if (token->keyword == RID_AT_REQUIRED)
26407 cp_lexer_consume_token (parser->lexer);
26408 objc_set_method_opt (false);
26410 else if (token->keyword == RID_NAMESPACE)
26411 cp_parser_namespace_definition (parser);
26412 /* Other stray characters must generate errors. */
26413 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26415 cp_lexer_consume_token (parser->lexer);
26416 error ("stray %qs between Objective-C++ methods",
26417 token->type == CPP_OPEN_BRACE ? "{" : "}");
26419 /* Finally, try to parse a block-declaration, or a function-definition. */
26420 else
26421 cp_parser_block_declaration (parser, /*statement_p=*/false);
26424 /* Parse a method signature. */
26426 static tree
26427 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26429 tree rettype, kwdparms, optparms;
26430 bool ellipsis = false;
26431 bool is_class_method;
26433 is_class_method = cp_parser_objc_method_type (parser);
26434 rettype = cp_parser_objc_typename (parser);
26435 *attributes = NULL_TREE;
26436 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26437 if (kwdparms == error_mark_node)
26438 return error_mark_node;
26439 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26440 if (optparms == error_mark_node)
26441 return error_mark_node;
26443 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26446 static bool
26447 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26449 tree tattr;
26450 cp_lexer_save_tokens (parser->lexer);
26451 tattr = cp_parser_attributes_opt (parser);
26452 gcc_assert (tattr) ;
26454 /* If the attributes are followed by a method introducer, this is not allowed.
26455 Dump the attributes and flag the situation. */
26456 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26457 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26458 return true;
26460 /* Otherwise, the attributes introduce some interstitial code, possibly so
26461 rewind to allow that check. */
26462 cp_lexer_rollback_tokens (parser->lexer);
26463 return false;
26466 /* Parse an Objective-C method prototype list. */
26468 static void
26469 cp_parser_objc_method_prototype_list (cp_parser* parser)
26471 cp_token *token = cp_lexer_peek_token (parser->lexer);
26473 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26475 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26477 tree attributes, sig;
26478 bool is_class_method;
26479 if (token->type == CPP_PLUS)
26480 is_class_method = true;
26481 else
26482 is_class_method = false;
26483 sig = cp_parser_objc_method_signature (parser, &attributes);
26484 if (sig == error_mark_node)
26486 cp_parser_skip_to_end_of_block_or_statement (parser);
26487 token = cp_lexer_peek_token (parser->lexer);
26488 continue;
26490 objc_add_method_declaration (is_class_method, sig, attributes);
26491 cp_parser_consume_semicolon_at_end_of_statement (parser);
26493 else if (token->keyword == RID_AT_PROPERTY)
26494 cp_parser_objc_at_property_declaration (parser);
26495 else if (token->keyword == RID_ATTRIBUTE
26496 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26497 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26498 OPT_Wattributes,
26499 "prefix attributes are ignored for methods");
26500 else
26501 /* Allow for interspersed non-ObjC++ code. */
26502 cp_parser_objc_interstitial_code (parser);
26504 token = cp_lexer_peek_token (parser->lexer);
26507 if (token->type != CPP_EOF)
26508 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26509 else
26510 cp_parser_error (parser, "expected %<@end%>");
26512 objc_finish_interface ();
26515 /* Parse an Objective-C method definition list. */
26517 static void
26518 cp_parser_objc_method_definition_list (cp_parser* parser)
26520 cp_token *token = cp_lexer_peek_token (parser->lexer);
26522 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26524 tree meth;
26526 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26528 cp_token *ptk;
26529 tree sig, attribute;
26530 bool is_class_method;
26531 if (token->type == CPP_PLUS)
26532 is_class_method = true;
26533 else
26534 is_class_method = false;
26535 push_deferring_access_checks (dk_deferred);
26536 sig = cp_parser_objc_method_signature (parser, &attribute);
26537 if (sig == error_mark_node)
26539 cp_parser_skip_to_end_of_block_or_statement (parser);
26540 token = cp_lexer_peek_token (parser->lexer);
26541 continue;
26543 objc_start_method_definition (is_class_method, sig, attribute,
26544 NULL_TREE);
26546 /* For historical reasons, we accept an optional semicolon. */
26547 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26548 cp_lexer_consume_token (parser->lexer);
26550 ptk = cp_lexer_peek_token (parser->lexer);
26551 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26552 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26554 perform_deferred_access_checks (tf_warning_or_error);
26555 stop_deferring_access_checks ();
26556 meth = cp_parser_function_definition_after_declarator (parser,
26557 false);
26558 pop_deferring_access_checks ();
26559 objc_finish_method_definition (meth);
26562 /* The following case will be removed once @synthesize is
26563 completely implemented. */
26564 else if (token->keyword == RID_AT_PROPERTY)
26565 cp_parser_objc_at_property_declaration (parser);
26566 else if (token->keyword == RID_AT_SYNTHESIZE)
26567 cp_parser_objc_at_synthesize_declaration (parser);
26568 else if (token->keyword == RID_AT_DYNAMIC)
26569 cp_parser_objc_at_dynamic_declaration (parser);
26570 else if (token->keyword == RID_ATTRIBUTE
26571 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26572 warning_at (token->location, OPT_Wattributes,
26573 "prefix attributes are ignored for methods");
26574 else
26575 /* Allow for interspersed non-ObjC++ code. */
26576 cp_parser_objc_interstitial_code (parser);
26578 token = cp_lexer_peek_token (parser->lexer);
26581 if (token->type != CPP_EOF)
26582 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26583 else
26584 cp_parser_error (parser, "expected %<@end%>");
26586 objc_finish_implementation ();
26589 /* Parse Objective-C ivars. */
26591 static void
26592 cp_parser_objc_class_ivars (cp_parser* parser)
26594 cp_token *token = cp_lexer_peek_token (parser->lexer);
26596 if (token->type != CPP_OPEN_BRACE)
26597 return; /* No ivars specified. */
26599 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26600 token = cp_lexer_peek_token (parser->lexer);
26602 while (token->type != CPP_CLOSE_BRACE
26603 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26605 cp_decl_specifier_seq declspecs;
26606 int decl_class_or_enum_p;
26607 tree prefix_attributes;
26609 cp_parser_objc_visibility_spec (parser);
26611 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26612 break;
26614 cp_parser_decl_specifier_seq (parser,
26615 CP_PARSER_FLAGS_OPTIONAL,
26616 &declspecs,
26617 &decl_class_or_enum_p);
26619 /* auto, register, static, extern, mutable. */
26620 if (declspecs.storage_class != sc_none)
26622 cp_parser_error (parser, "invalid type for instance variable");
26623 declspecs.storage_class = sc_none;
26626 /* thread_local. */
26627 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26629 cp_parser_error (parser, "invalid type for instance variable");
26630 declspecs.locations[ds_thread] = 0;
26633 /* typedef. */
26634 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26636 cp_parser_error (parser, "invalid type for instance variable");
26637 declspecs.locations[ds_typedef] = 0;
26640 prefix_attributes = declspecs.attributes;
26641 declspecs.attributes = NULL_TREE;
26643 /* Keep going until we hit the `;' at the end of the
26644 declaration. */
26645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26647 tree width = NULL_TREE, attributes, first_attribute, decl;
26648 cp_declarator *declarator = NULL;
26649 int ctor_dtor_or_conv_p;
26651 /* Check for a (possibly unnamed) bitfield declaration. */
26652 token = cp_lexer_peek_token (parser->lexer);
26653 if (token->type == CPP_COLON)
26654 goto eat_colon;
26656 if (token->type == CPP_NAME
26657 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26658 == CPP_COLON))
26660 /* Get the name of the bitfield. */
26661 declarator = make_id_declarator (NULL_TREE,
26662 cp_parser_identifier (parser),
26663 sfk_none);
26665 eat_colon:
26666 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26667 /* Get the width of the bitfield. */
26668 width
26669 = cp_parser_constant_expression (parser);
26670 width = maybe_constant_value (width);
26672 else
26674 /* Parse the declarator. */
26675 declarator
26676 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26677 &ctor_dtor_or_conv_p,
26678 /*parenthesized_p=*/NULL,
26679 /*member_p=*/false,
26680 /*friend_p=*/false);
26683 /* Look for attributes that apply to the ivar. */
26684 attributes = cp_parser_attributes_opt (parser);
26685 /* Remember which attributes are prefix attributes and
26686 which are not. */
26687 first_attribute = attributes;
26688 /* Combine the attributes. */
26689 attributes = chainon (prefix_attributes, attributes);
26691 if (width)
26692 /* Create the bitfield declaration. */
26693 decl = grokbitfield (declarator, &declspecs,
26694 width,
26695 attributes);
26696 else
26697 decl = grokfield (declarator, &declspecs,
26698 NULL_TREE, /*init_const_expr_p=*/false,
26699 NULL_TREE, attributes);
26701 /* Add the instance variable. */
26702 if (decl != error_mark_node && decl != NULL_TREE)
26703 objc_add_instance_variable (decl);
26705 /* Reset PREFIX_ATTRIBUTES. */
26706 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26707 attributes = TREE_CHAIN (attributes);
26708 if (attributes)
26709 TREE_CHAIN (attributes) = NULL_TREE;
26711 token = cp_lexer_peek_token (parser->lexer);
26713 if (token->type == CPP_COMMA)
26715 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26716 continue;
26718 break;
26721 cp_parser_consume_semicolon_at_end_of_statement (parser);
26722 token = cp_lexer_peek_token (parser->lexer);
26725 if (token->keyword == RID_AT_END)
26726 cp_parser_error (parser, "expected %<}%>");
26728 /* Do not consume the RID_AT_END, so it will be read again as terminating
26729 the @interface of @implementation. */
26730 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26731 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26733 /* For historical reasons, we accept an optional semicolon. */
26734 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26735 cp_lexer_consume_token (parser->lexer);
26738 /* Parse an Objective-C protocol declaration. */
26740 static void
26741 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26743 tree proto, protorefs;
26744 cp_token *tok;
26746 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26749 tok = cp_lexer_peek_token (parser->lexer);
26750 error_at (tok->location, "identifier expected after %<@protocol%>");
26751 cp_parser_consume_semicolon_at_end_of_statement (parser);
26752 return;
26755 /* See if we have a forward declaration or a definition. */
26756 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26758 /* Try a forward declaration first. */
26759 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26761 while (true)
26763 tree id;
26765 id = cp_parser_identifier (parser);
26766 if (id == error_mark_node)
26767 break;
26769 objc_declare_protocol (id, attributes);
26771 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26772 cp_lexer_consume_token (parser->lexer);
26773 else
26774 break;
26776 cp_parser_consume_semicolon_at_end_of_statement (parser);
26779 /* Ok, we got a full-fledged definition (or at least should). */
26780 else
26782 proto = cp_parser_identifier (parser);
26783 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26784 objc_start_protocol (proto, protorefs, attributes);
26785 cp_parser_objc_method_prototype_list (parser);
26789 /* Parse an Objective-C superclass or category. */
26791 static void
26792 cp_parser_objc_superclass_or_category (cp_parser *parser,
26793 bool iface_p,
26794 tree *super,
26795 tree *categ, bool *is_class_extension)
26797 cp_token *next = cp_lexer_peek_token (parser->lexer);
26799 *super = *categ = NULL_TREE;
26800 *is_class_extension = false;
26801 if (next->type == CPP_COLON)
26803 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26804 *super = cp_parser_identifier (parser);
26806 else if (next->type == CPP_OPEN_PAREN)
26808 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26810 /* If there is no category name, and this is an @interface, we
26811 have a class extension. */
26812 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26814 *categ = NULL_TREE;
26815 *is_class_extension = true;
26817 else
26818 *categ = cp_parser_identifier (parser);
26820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26824 /* Parse an Objective-C class interface. */
26826 static void
26827 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26829 tree name, super, categ, protos;
26830 bool is_class_extension;
26832 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26833 name = cp_parser_identifier (parser);
26834 if (name == error_mark_node)
26836 /* It's hard to recover because even if valid @interface stuff
26837 is to follow, we can't compile it (or validate it) if we
26838 don't even know which class it refers to. Let's assume this
26839 was a stray '@interface' token in the stream and skip it.
26841 return;
26843 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26844 &is_class_extension);
26845 protos = cp_parser_objc_protocol_refs_opt (parser);
26847 /* We have either a class or a category on our hands. */
26848 if (categ || is_class_extension)
26849 objc_start_category_interface (name, categ, protos, attributes);
26850 else
26852 objc_start_class_interface (name, super, protos, attributes);
26853 /* Handle instance variable declarations, if any. */
26854 cp_parser_objc_class_ivars (parser);
26855 objc_continue_interface ();
26858 cp_parser_objc_method_prototype_list (parser);
26861 /* Parse an Objective-C class implementation. */
26863 static void
26864 cp_parser_objc_class_implementation (cp_parser* parser)
26866 tree name, super, categ;
26867 bool is_class_extension;
26869 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26870 name = cp_parser_identifier (parser);
26871 if (name == error_mark_node)
26873 /* It's hard to recover because even if valid @implementation
26874 stuff is to follow, we can't compile it (or validate it) if
26875 we don't even know which class it refers to. Let's assume
26876 this was a stray '@implementation' token in the stream and
26877 skip it.
26879 return;
26881 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26882 &is_class_extension);
26884 /* We have either a class or a category on our hands. */
26885 if (categ)
26886 objc_start_category_implementation (name, categ);
26887 else
26889 objc_start_class_implementation (name, super);
26890 /* Handle instance variable declarations, if any. */
26891 cp_parser_objc_class_ivars (parser);
26892 objc_continue_implementation ();
26895 cp_parser_objc_method_definition_list (parser);
26898 /* Consume the @end token and finish off the implementation. */
26900 static void
26901 cp_parser_objc_end_implementation (cp_parser* parser)
26903 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26904 objc_finish_implementation ();
26907 /* Parse an Objective-C declaration. */
26909 static void
26910 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26912 /* Try to figure out what kind of declaration is present. */
26913 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26915 if (attributes)
26916 switch (kwd->keyword)
26918 case RID_AT_ALIAS:
26919 case RID_AT_CLASS:
26920 case RID_AT_END:
26921 error_at (kwd->location, "attributes may not be specified before"
26922 " the %<@%D%> Objective-C++ keyword",
26923 kwd->u.value);
26924 attributes = NULL;
26925 break;
26926 case RID_AT_IMPLEMENTATION:
26927 warning_at (kwd->location, OPT_Wattributes,
26928 "prefix attributes are ignored before %<@%D%>",
26929 kwd->u.value);
26930 attributes = NULL;
26931 default:
26932 break;
26935 switch (kwd->keyword)
26937 case RID_AT_ALIAS:
26938 cp_parser_objc_alias_declaration (parser);
26939 break;
26940 case RID_AT_CLASS:
26941 cp_parser_objc_class_declaration (parser);
26942 break;
26943 case RID_AT_PROTOCOL:
26944 cp_parser_objc_protocol_declaration (parser, attributes);
26945 break;
26946 case RID_AT_INTERFACE:
26947 cp_parser_objc_class_interface (parser, attributes);
26948 break;
26949 case RID_AT_IMPLEMENTATION:
26950 cp_parser_objc_class_implementation (parser);
26951 break;
26952 case RID_AT_END:
26953 cp_parser_objc_end_implementation (parser);
26954 break;
26955 default:
26956 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26957 kwd->u.value);
26958 cp_parser_skip_to_end_of_block_or_statement (parser);
26962 /* Parse an Objective-C try-catch-finally statement.
26964 objc-try-catch-finally-stmt:
26965 @try compound-statement objc-catch-clause-seq [opt]
26966 objc-finally-clause [opt]
26968 objc-catch-clause-seq:
26969 objc-catch-clause objc-catch-clause-seq [opt]
26971 objc-catch-clause:
26972 @catch ( objc-exception-declaration ) compound-statement
26974 objc-finally-clause:
26975 @finally compound-statement
26977 objc-exception-declaration:
26978 parameter-declaration
26979 '...'
26981 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26983 Returns NULL_TREE.
26985 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26986 for C. Keep them in sync. */
26988 static tree
26989 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26991 location_t location;
26992 tree stmt;
26994 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26995 location = cp_lexer_peek_token (parser->lexer)->location;
26996 objc_maybe_warn_exceptions (location);
26997 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26998 node, lest it get absorbed into the surrounding block. */
26999 stmt = push_stmt_list ();
27000 cp_parser_compound_statement (parser, NULL, false, false);
27001 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27003 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27005 cp_parameter_declarator *parm;
27006 tree parameter_declaration = error_mark_node;
27007 bool seen_open_paren = false;
27009 cp_lexer_consume_token (parser->lexer);
27010 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27011 seen_open_paren = true;
27012 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27014 /* We have "@catch (...)" (where the '...' are literally
27015 what is in the code). Skip the '...'.
27016 parameter_declaration is set to NULL_TREE, and
27017 objc_being_catch_clauses() knows that that means
27018 '...'. */
27019 cp_lexer_consume_token (parser->lexer);
27020 parameter_declaration = NULL_TREE;
27022 else
27024 /* We have "@catch (NSException *exception)" or something
27025 like that. Parse the parameter declaration. */
27026 parm = cp_parser_parameter_declaration (parser, false, NULL);
27027 if (parm == NULL)
27028 parameter_declaration = error_mark_node;
27029 else
27030 parameter_declaration = grokdeclarator (parm->declarator,
27031 &parm->decl_specifiers,
27032 PARM, /*initialized=*/0,
27033 /*attrlist=*/NULL);
27035 if (seen_open_paren)
27036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27037 else
27039 /* If there was no open parenthesis, we are recovering from
27040 an error, and we are trying to figure out what mistake
27041 the user has made. */
27043 /* If there is an immediate closing parenthesis, the user
27044 probably forgot the opening one (ie, they typed "@catch
27045 NSException *e)". Parse the closing parenthesis and keep
27046 going. */
27047 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27048 cp_lexer_consume_token (parser->lexer);
27050 /* If these is no immediate closing parenthesis, the user
27051 probably doesn't know that parenthesis are required at
27052 all (ie, they typed "@catch NSException *e"). So, just
27053 forget about the closing parenthesis and keep going. */
27055 objc_begin_catch_clause (parameter_declaration);
27056 cp_parser_compound_statement (parser, NULL, false, false);
27057 objc_finish_catch_clause ();
27059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27061 cp_lexer_consume_token (parser->lexer);
27062 location = cp_lexer_peek_token (parser->lexer)->location;
27063 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27064 node, lest it get absorbed into the surrounding block. */
27065 stmt = push_stmt_list ();
27066 cp_parser_compound_statement (parser, NULL, false, false);
27067 objc_build_finally_clause (location, pop_stmt_list (stmt));
27070 return objc_finish_try_stmt ();
27073 /* Parse an Objective-C synchronized statement.
27075 objc-synchronized-stmt:
27076 @synchronized ( expression ) compound-statement
27078 Returns NULL_TREE. */
27080 static tree
27081 cp_parser_objc_synchronized_statement (cp_parser *parser)
27083 location_t location;
27084 tree lock, stmt;
27086 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27088 location = cp_lexer_peek_token (parser->lexer)->location;
27089 objc_maybe_warn_exceptions (location);
27090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27091 lock = cp_parser_expression (parser);
27092 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27094 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27095 node, lest it get absorbed into the surrounding block. */
27096 stmt = push_stmt_list ();
27097 cp_parser_compound_statement (parser, NULL, false, false);
27099 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27102 /* Parse an Objective-C throw statement.
27104 objc-throw-stmt:
27105 @throw assignment-expression [opt] ;
27107 Returns a constructed '@throw' statement. */
27109 static tree
27110 cp_parser_objc_throw_statement (cp_parser *parser)
27112 tree expr = NULL_TREE;
27113 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27115 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27118 expr = cp_parser_expression (parser);
27120 cp_parser_consume_semicolon_at_end_of_statement (parser);
27122 return objc_build_throw_stmt (loc, expr);
27125 /* Parse an Objective-C statement. */
27127 static tree
27128 cp_parser_objc_statement (cp_parser * parser)
27130 /* Try to figure out what kind of declaration is present. */
27131 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27133 switch (kwd->keyword)
27135 case RID_AT_TRY:
27136 return cp_parser_objc_try_catch_finally_statement (parser);
27137 case RID_AT_SYNCHRONIZED:
27138 return cp_parser_objc_synchronized_statement (parser);
27139 case RID_AT_THROW:
27140 return cp_parser_objc_throw_statement (parser);
27141 default:
27142 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27143 kwd->u.value);
27144 cp_parser_skip_to_end_of_block_or_statement (parser);
27147 return error_mark_node;
27150 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27151 look ahead to see if an objc keyword follows the attributes. This
27152 is to detect the use of prefix attributes on ObjC @interface and
27153 @protocol. */
27155 static bool
27156 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27158 cp_lexer_save_tokens (parser->lexer);
27159 *attrib = cp_parser_attributes_opt (parser);
27160 gcc_assert (*attrib);
27161 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27163 cp_lexer_commit_tokens (parser->lexer);
27164 return true;
27166 cp_lexer_rollback_tokens (parser->lexer);
27167 return false;
27170 /* This routine is a minimal replacement for
27171 c_parser_struct_declaration () used when parsing the list of
27172 types/names or ObjC++ properties. For example, when parsing the
27173 code
27175 @property (readonly) int a, b, c;
27177 this function is responsible for parsing "int a, int b, int c" and
27178 returning the declarations as CHAIN of DECLs.
27180 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27181 similar parsing. */
27182 static tree
27183 cp_parser_objc_struct_declaration (cp_parser *parser)
27185 tree decls = NULL_TREE;
27186 cp_decl_specifier_seq declspecs;
27187 int decl_class_or_enum_p;
27188 tree prefix_attributes;
27190 cp_parser_decl_specifier_seq (parser,
27191 CP_PARSER_FLAGS_NONE,
27192 &declspecs,
27193 &decl_class_or_enum_p);
27195 if (declspecs.type == error_mark_node)
27196 return error_mark_node;
27198 /* auto, register, static, extern, mutable. */
27199 if (declspecs.storage_class != sc_none)
27201 cp_parser_error (parser, "invalid type for property");
27202 declspecs.storage_class = sc_none;
27205 /* thread_local. */
27206 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27208 cp_parser_error (parser, "invalid type for property");
27209 declspecs.locations[ds_thread] = 0;
27212 /* typedef. */
27213 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27215 cp_parser_error (parser, "invalid type for property");
27216 declspecs.locations[ds_typedef] = 0;
27219 prefix_attributes = declspecs.attributes;
27220 declspecs.attributes = NULL_TREE;
27222 /* Keep going until we hit the `;' at the end of the declaration. */
27223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27225 tree attributes, first_attribute, decl;
27226 cp_declarator *declarator;
27227 cp_token *token;
27229 /* Parse the declarator. */
27230 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27231 NULL, NULL, false, false);
27233 /* Look for attributes that apply to the ivar. */
27234 attributes = cp_parser_attributes_opt (parser);
27235 /* Remember which attributes are prefix attributes and
27236 which are not. */
27237 first_attribute = attributes;
27238 /* Combine the attributes. */
27239 attributes = chainon (prefix_attributes, attributes);
27241 decl = grokfield (declarator, &declspecs,
27242 NULL_TREE, /*init_const_expr_p=*/false,
27243 NULL_TREE, attributes);
27245 if (decl == error_mark_node || decl == NULL_TREE)
27246 return error_mark_node;
27248 /* Reset PREFIX_ATTRIBUTES. */
27249 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27250 attributes = TREE_CHAIN (attributes);
27251 if (attributes)
27252 TREE_CHAIN (attributes) = NULL_TREE;
27254 DECL_CHAIN (decl) = decls;
27255 decls = decl;
27257 token = cp_lexer_peek_token (parser->lexer);
27258 if (token->type == CPP_COMMA)
27260 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27261 continue;
27263 else
27264 break;
27266 return decls;
27269 /* Parse an Objective-C @property declaration. The syntax is:
27271 objc-property-declaration:
27272 '@property' objc-property-attributes[opt] struct-declaration ;
27274 objc-property-attributes:
27275 '(' objc-property-attribute-list ')'
27277 objc-property-attribute-list:
27278 objc-property-attribute
27279 objc-property-attribute-list, objc-property-attribute
27281 objc-property-attribute
27282 'getter' = identifier
27283 'setter' = identifier
27284 'readonly'
27285 'readwrite'
27286 'assign'
27287 'retain'
27288 'copy'
27289 'nonatomic'
27291 For example:
27292 @property NSString *name;
27293 @property (readonly) id object;
27294 @property (retain, nonatomic, getter=getTheName) id name;
27295 @property int a, b, c;
27297 PS: This function is identical to
27298 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27299 static void
27300 cp_parser_objc_at_property_declaration (cp_parser *parser)
27302 /* The following variables hold the attributes of the properties as
27303 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27304 seen. When we see an attribute, we set them to 'true' (if they
27305 are boolean properties) or to the identifier (if they have an
27306 argument, ie, for getter and setter). Note that here we only
27307 parse the list of attributes, check the syntax and accumulate the
27308 attributes that we find. objc_add_property_declaration() will
27309 then process the information. */
27310 bool property_assign = false;
27311 bool property_copy = false;
27312 tree property_getter_ident = NULL_TREE;
27313 bool property_nonatomic = false;
27314 bool property_readonly = false;
27315 bool property_readwrite = false;
27316 bool property_retain = false;
27317 tree property_setter_ident = NULL_TREE;
27319 /* 'properties' is the list of properties that we read. Usually a
27320 single one, but maybe more (eg, in "@property int a, b, c;" there
27321 are three). */
27322 tree properties;
27323 location_t loc;
27325 loc = cp_lexer_peek_token (parser->lexer)->location;
27327 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27329 /* Parse the optional attribute list... */
27330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27332 /* Eat the '('. */
27333 cp_lexer_consume_token (parser->lexer);
27335 while (true)
27337 bool syntax_error = false;
27338 cp_token *token = cp_lexer_peek_token (parser->lexer);
27339 enum rid keyword;
27341 if (token->type != CPP_NAME)
27343 cp_parser_error (parser, "expected identifier");
27344 break;
27346 keyword = C_RID_CODE (token->u.value);
27347 cp_lexer_consume_token (parser->lexer);
27348 switch (keyword)
27350 case RID_ASSIGN: property_assign = true; break;
27351 case RID_COPY: property_copy = true; break;
27352 case RID_NONATOMIC: property_nonatomic = true; break;
27353 case RID_READONLY: property_readonly = true; break;
27354 case RID_READWRITE: property_readwrite = true; break;
27355 case RID_RETAIN: property_retain = true; break;
27357 case RID_GETTER:
27358 case RID_SETTER:
27359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27361 if (keyword == RID_GETTER)
27362 cp_parser_error (parser,
27363 "missing %<=%> (after %<getter%> attribute)");
27364 else
27365 cp_parser_error (parser,
27366 "missing %<=%> (after %<setter%> attribute)");
27367 syntax_error = true;
27368 break;
27370 cp_lexer_consume_token (parser->lexer); /* eat the = */
27371 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27373 cp_parser_error (parser, "expected identifier");
27374 syntax_error = true;
27375 break;
27377 if (keyword == RID_SETTER)
27379 if (property_setter_ident != NULL_TREE)
27381 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27382 cp_lexer_consume_token (parser->lexer);
27384 else
27385 property_setter_ident = cp_parser_objc_selector (parser);
27386 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27387 cp_parser_error (parser, "setter name must terminate with %<:%>");
27388 else
27389 cp_lexer_consume_token (parser->lexer);
27391 else
27393 if (property_getter_ident != NULL_TREE)
27395 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27396 cp_lexer_consume_token (parser->lexer);
27398 else
27399 property_getter_ident = cp_parser_objc_selector (parser);
27401 break;
27402 default:
27403 cp_parser_error (parser, "unknown property attribute");
27404 syntax_error = true;
27405 break;
27408 if (syntax_error)
27409 break;
27411 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27412 cp_lexer_consume_token (parser->lexer);
27413 else
27414 break;
27417 /* FIXME: "@property (setter, assign);" will generate a spurious
27418 "error: expected ‘)’ before ‘,’ token". This is because
27419 cp_parser_require, unlike the C counterpart, will produce an
27420 error even if we are in error recovery. */
27421 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27423 cp_parser_skip_to_closing_parenthesis (parser,
27424 /*recovering=*/true,
27425 /*or_comma=*/false,
27426 /*consume_paren=*/true);
27430 /* ... and the property declaration(s). */
27431 properties = cp_parser_objc_struct_declaration (parser);
27433 if (properties == error_mark_node)
27435 cp_parser_skip_to_end_of_statement (parser);
27436 /* If the next token is now a `;', consume it. */
27437 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27438 cp_lexer_consume_token (parser->lexer);
27439 return;
27442 if (properties == NULL_TREE)
27443 cp_parser_error (parser, "expected identifier");
27444 else
27446 /* Comma-separated properties are chained together in
27447 reverse order; add them one by one. */
27448 properties = nreverse (properties);
27450 for (; properties; properties = TREE_CHAIN (properties))
27451 objc_add_property_declaration (loc, copy_node (properties),
27452 property_readonly, property_readwrite,
27453 property_assign, property_retain,
27454 property_copy, property_nonatomic,
27455 property_getter_ident, property_setter_ident);
27458 cp_parser_consume_semicolon_at_end_of_statement (parser);
27461 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27463 objc-synthesize-declaration:
27464 @synthesize objc-synthesize-identifier-list ;
27466 objc-synthesize-identifier-list:
27467 objc-synthesize-identifier
27468 objc-synthesize-identifier-list, objc-synthesize-identifier
27470 objc-synthesize-identifier
27471 identifier
27472 identifier = identifier
27474 For example:
27475 @synthesize MyProperty;
27476 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27478 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27479 for C. Keep them in sync.
27481 static void
27482 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27484 tree list = NULL_TREE;
27485 location_t loc;
27486 loc = cp_lexer_peek_token (parser->lexer)->location;
27488 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27489 while (true)
27491 tree property, ivar;
27492 property = cp_parser_identifier (parser);
27493 if (property == error_mark_node)
27495 cp_parser_consume_semicolon_at_end_of_statement (parser);
27496 return;
27498 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27500 cp_lexer_consume_token (parser->lexer);
27501 ivar = cp_parser_identifier (parser);
27502 if (ivar == error_mark_node)
27504 cp_parser_consume_semicolon_at_end_of_statement (parser);
27505 return;
27508 else
27509 ivar = NULL_TREE;
27510 list = chainon (list, build_tree_list (ivar, property));
27511 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27512 cp_lexer_consume_token (parser->lexer);
27513 else
27514 break;
27516 cp_parser_consume_semicolon_at_end_of_statement (parser);
27517 objc_add_synthesize_declaration (loc, list);
27520 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27522 objc-dynamic-declaration:
27523 @dynamic identifier-list ;
27525 For example:
27526 @dynamic MyProperty;
27527 @dynamic MyProperty, AnotherProperty;
27529 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27530 for C. Keep them in sync.
27532 static void
27533 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27535 tree list = NULL_TREE;
27536 location_t loc;
27537 loc = cp_lexer_peek_token (parser->lexer)->location;
27539 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27540 while (true)
27542 tree property;
27543 property = cp_parser_identifier (parser);
27544 if (property == error_mark_node)
27546 cp_parser_consume_semicolon_at_end_of_statement (parser);
27547 return;
27549 list = chainon (list, build_tree_list (NULL, property));
27550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27551 cp_lexer_consume_token (parser->lexer);
27552 else
27553 break;
27555 cp_parser_consume_semicolon_at_end_of_statement (parser);
27556 objc_add_dynamic_declaration (loc, list);
27560 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27562 /* Returns name of the next clause.
27563 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27564 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27565 returned and the token is consumed. */
27567 static pragma_omp_clause
27568 cp_parser_omp_clause_name (cp_parser *parser)
27570 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27573 result = PRAGMA_OMP_CLAUSE_IF;
27574 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27575 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27576 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27577 result = PRAGMA_OACC_CLAUSE_DELETE;
27578 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27579 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27580 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27581 result = PRAGMA_OMP_CLAUSE_FOR;
27582 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27585 const char *p = IDENTIFIER_POINTER (id);
27587 switch (p[0])
27589 case 'a':
27590 if (!strcmp ("aligned", p))
27591 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27592 else if (!strcmp ("async", p))
27593 result = PRAGMA_OACC_CLAUSE_ASYNC;
27594 break;
27595 case 'c':
27596 if (!strcmp ("collapse", p))
27597 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27598 else if (!strcmp ("copy", p))
27599 result = PRAGMA_OACC_CLAUSE_COPY;
27600 else if (!strcmp ("copyin", p))
27601 result = PRAGMA_OMP_CLAUSE_COPYIN;
27602 else if (!strcmp ("copyout", p))
27603 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27604 else if (!strcmp ("copyprivate", p))
27605 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27606 else if (!strcmp ("create", p))
27607 result = PRAGMA_OACC_CLAUSE_CREATE;
27608 break;
27609 case 'd':
27610 if (!strcmp ("depend", p))
27611 result = PRAGMA_OMP_CLAUSE_DEPEND;
27612 else if (!strcmp ("device", p))
27613 result = PRAGMA_OMP_CLAUSE_DEVICE;
27614 else if (!strcmp ("deviceptr", p))
27615 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27616 else if (!strcmp ("dist_schedule", p))
27617 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27618 break;
27619 case 'f':
27620 if (!strcmp ("final", p))
27621 result = PRAGMA_OMP_CLAUSE_FINAL;
27622 else if (!strcmp ("firstprivate", p))
27623 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27624 else if (!strcmp ("from", p))
27625 result = PRAGMA_OMP_CLAUSE_FROM;
27626 break;
27627 case 'h':
27628 if (!strcmp ("host", p))
27629 result = PRAGMA_OACC_CLAUSE_HOST;
27630 break;
27631 case 'i':
27632 if (!strcmp ("inbranch", p))
27633 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27634 break;
27635 case 'l':
27636 if (!strcmp ("lastprivate", p))
27637 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27638 else if (!strcmp ("linear", p))
27639 result = PRAGMA_OMP_CLAUSE_LINEAR;
27640 break;
27641 case 'm':
27642 if (!strcmp ("map", p))
27643 result = PRAGMA_OMP_CLAUSE_MAP;
27644 else if (!strcmp ("mergeable", p))
27645 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27646 else if (flag_cilkplus && !strcmp ("mask", p))
27647 result = PRAGMA_CILK_CLAUSE_MASK;
27648 break;
27649 case 'n':
27650 if (!strcmp ("notinbranch", p))
27651 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27652 else if (!strcmp ("nowait", p))
27653 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27654 else if (flag_cilkplus && !strcmp ("nomask", p))
27655 result = PRAGMA_CILK_CLAUSE_NOMASK;
27656 else if (!strcmp ("num_gangs", p))
27657 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27658 else if (!strcmp ("num_teams", p))
27659 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27660 else if (!strcmp ("num_threads", p))
27661 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27662 else if (!strcmp ("num_workers", p))
27663 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27664 break;
27665 case 'o':
27666 if (!strcmp ("ordered", p))
27667 result = PRAGMA_OMP_CLAUSE_ORDERED;
27668 break;
27669 case 'p':
27670 if (!strcmp ("parallel", p))
27671 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27672 else if (!strcmp ("present", p))
27673 result = PRAGMA_OACC_CLAUSE_PRESENT;
27674 else if (!strcmp ("present_or_copy", p)
27675 || !strcmp ("pcopy", p))
27676 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27677 else if (!strcmp ("present_or_copyin", p)
27678 || !strcmp ("pcopyin", p))
27679 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27680 else if (!strcmp ("present_or_copyout", p)
27681 || !strcmp ("pcopyout", p))
27682 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27683 else if (!strcmp ("present_or_create", p)
27684 || !strcmp ("pcreate", p))
27685 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27686 else if (!strcmp ("proc_bind", p))
27687 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27688 break;
27689 case 'r':
27690 if (!strcmp ("reduction", p))
27691 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27692 break;
27693 case 's':
27694 if (!strcmp ("safelen", p))
27695 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27696 else if (!strcmp ("schedule", p))
27697 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27698 else if (!strcmp ("sections", p))
27699 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27700 else if (!strcmp ("self", p))
27701 result = PRAGMA_OACC_CLAUSE_SELF;
27702 else if (!strcmp ("shared", p))
27703 result = PRAGMA_OMP_CLAUSE_SHARED;
27704 else if (!strcmp ("simdlen", p))
27705 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27706 break;
27707 case 't':
27708 if (!strcmp ("taskgroup", p))
27709 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27710 else if (!strcmp ("thread_limit", p))
27711 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27712 else if (!strcmp ("to", p))
27713 result = PRAGMA_OMP_CLAUSE_TO;
27714 break;
27715 case 'u':
27716 if (!strcmp ("uniform", p))
27717 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27718 else if (!strcmp ("untied", p))
27719 result = PRAGMA_OMP_CLAUSE_UNTIED;
27720 break;
27721 case 'v':
27722 if (!strcmp ("vector_length", p))
27723 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27724 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27725 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27726 break;
27727 case 'w':
27728 if (!strcmp ("wait", p))
27729 result = PRAGMA_OACC_CLAUSE_WAIT;
27730 break;
27734 if (result != PRAGMA_OMP_CLAUSE_NONE)
27735 cp_lexer_consume_token (parser->lexer);
27737 return result;
27740 /* Validate that a clause of the given type does not already exist. */
27742 static void
27743 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27744 const char *name, location_t location)
27746 tree c;
27748 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27749 if (OMP_CLAUSE_CODE (c) == code)
27751 error_at (location, "too many %qs clauses", name);
27752 break;
27756 /* OpenMP 2.5:
27757 variable-list:
27758 identifier
27759 variable-list , identifier
27761 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27762 colon). An opening parenthesis will have been consumed by the caller.
27764 If KIND is nonzero, create the appropriate node and install the decl
27765 in OMP_CLAUSE_DECL and add the node to the head of the list.
27767 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27768 return the list created.
27770 COLON can be NULL if only closing parenthesis should end the list,
27771 or pointer to bool which will receive false if the list is terminated
27772 by closing parenthesis or true if the list is terminated by colon. */
27774 static tree
27775 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27776 tree list, bool *colon)
27778 cp_token *token;
27779 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27780 if (colon)
27782 parser->colon_corrects_to_scope_p = false;
27783 *colon = false;
27785 while (1)
27787 tree name, decl;
27789 token = cp_lexer_peek_token (parser->lexer);
27790 name = cp_parser_id_expression (parser, /*template_p=*/false,
27791 /*check_dependency_p=*/true,
27792 /*template_p=*/NULL,
27793 /*declarator_p=*/false,
27794 /*optional_p=*/false);
27795 if (name == error_mark_node)
27796 goto skip_comma;
27798 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27799 if (decl == error_mark_node)
27800 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27801 token->location);
27802 else if (kind != 0)
27804 switch (kind)
27806 case OMP_CLAUSE__CACHE_:
27807 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27809 error_at (token->location, "expected %<[%>");
27810 decl = error_mark_node;
27811 break;
27813 /* FALL THROUGH. */
27814 case OMP_CLAUSE_MAP:
27815 case OMP_CLAUSE_FROM:
27816 case OMP_CLAUSE_TO:
27817 case OMP_CLAUSE_DEPEND:
27818 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27820 tree low_bound = NULL_TREE, length = NULL_TREE;
27822 parser->colon_corrects_to_scope_p = false;
27823 cp_lexer_consume_token (parser->lexer);
27824 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27825 low_bound = cp_parser_expression (parser);
27826 if (!colon)
27827 parser->colon_corrects_to_scope_p
27828 = saved_colon_corrects_to_scope_p;
27829 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27830 length = integer_one_node;
27831 else
27833 /* Look for `:'. */
27834 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27835 goto skip_comma;
27836 if (!cp_lexer_next_token_is (parser->lexer,
27837 CPP_CLOSE_SQUARE))
27838 length = cp_parser_expression (parser);
27840 /* So we need here fully folded values. */
27841 if (length)
27842 length = maybe_constant_value (fold (length));
27843 if (low_bound)
27844 low_bound = maybe_constant_value (fold (low_bound));
27845 /* Look for the closing `]'. */
27846 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27847 RT_CLOSE_SQUARE))
27848 goto skip_comma;
27850 if (kind == OMP_CLAUSE__CACHE_)
27852 if (TREE_CODE (low_bound) != INTEGER_CST
27853 && !TREE_READONLY (low_bound))
27855 error_at (token->location,
27856 "%qD is not a constant", low_bound);
27857 decl = error_mark_node;
27860 if (TREE_CODE (length) != INTEGER_CST
27861 && !TREE_READONLY (length))
27863 error_at (token->location,
27864 "%qD is not a constant", length);
27865 decl = error_mark_node;
27869 decl = tree_cons (low_bound, length, decl);
27871 break;
27872 default:
27873 break;
27876 tree u = build_omp_clause (token->location, kind);
27877 OMP_CLAUSE_DECL (u) = decl;
27878 OMP_CLAUSE_CHAIN (u) = list;
27879 list = u;
27881 else
27882 list = tree_cons (decl, NULL_TREE, list);
27884 get_comma:
27885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27886 break;
27887 cp_lexer_consume_token (parser->lexer);
27890 if (colon)
27891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27893 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27895 *colon = true;
27896 cp_parser_require (parser, CPP_COLON, RT_COLON);
27897 return list;
27900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27902 int ending;
27904 /* Try to resync to an unnested comma. Copied from
27905 cp_parser_parenthesized_expression_list. */
27906 skip_comma:
27907 if (colon)
27908 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27909 ending = cp_parser_skip_to_closing_parenthesis (parser,
27910 /*recovering=*/true,
27911 /*or_comma=*/true,
27912 /*consume_paren=*/true);
27913 if (ending < 0)
27914 goto get_comma;
27917 return list;
27920 /* Similarly, but expect leading and trailing parenthesis. This is a very
27921 common case for omp clauses. */
27923 static tree
27924 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27926 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27927 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27928 return list;
27931 /* OpenACC 2.0:
27932 copy ( variable-list )
27933 copyin ( variable-list )
27934 copyout ( variable-list )
27935 create ( variable-list )
27936 delete ( variable-list )
27937 present ( variable-list )
27938 present_or_copy ( variable-list )
27939 pcopy ( variable-list )
27940 present_or_copyin ( variable-list )
27941 pcopyin ( variable-list )
27942 present_or_copyout ( variable-list )
27943 pcopyout ( variable-list )
27944 present_or_create ( variable-list )
27945 pcreate ( variable-list ) */
27947 static tree
27948 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27949 tree list)
27951 enum gomp_map_kind kind;
27952 switch (c_kind)
27954 case PRAGMA_OACC_CLAUSE_COPY:
27955 kind = GOMP_MAP_FORCE_TOFROM;
27956 break;
27957 case PRAGMA_OACC_CLAUSE_COPYIN:
27958 kind = GOMP_MAP_FORCE_TO;
27959 break;
27960 case PRAGMA_OACC_CLAUSE_COPYOUT:
27961 kind = GOMP_MAP_FORCE_FROM;
27962 break;
27963 case PRAGMA_OACC_CLAUSE_CREATE:
27964 kind = GOMP_MAP_FORCE_ALLOC;
27965 break;
27966 case PRAGMA_OACC_CLAUSE_DELETE:
27967 kind = GOMP_MAP_FORCE_DEALLOC;
27968 break;
27969 case PRAGMA_OACC_CLAUSE_DEVICE:
27970 kind = GOMP_MAP_FORCE_TO;
27971 break;
27972 case PRAGMA_OACC_CLAUSE_HOST:
27973 case PRAGMA_OACC_CLAUSE_SELF:
27974 kind = GOMP_MAP_FORCE_FROM;
27975 break;
27976 case PRAGMA_OACC_CLAUSE_PRESENT:
27977 kind = GOMP_MAP_FORCE_PRESENT;
27978 break;
27979 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27980 kind = GOMP_MAP_TOFROM;
27981 break;
27982 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27983 kind = GOMP_MAP_TO;
27984 break;
27985 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27986 kind = GOMP_MAP_FROM;
27987 break;
27988 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27989 kind = GOMP_MAP_ALLOC;
27990 break;
27991 default:
27992 gcc_unreachable ();
27994 tree nl, c;
27995 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27997 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27998 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28000 return nl;
28003 /* OpenACC 2.0:
28004 deviceptr ( variable-list ) */
28006 static tree
28007 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28009 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28010 tree vars, t;
28012 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28013 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28014 variable-list must only allow for pointer variables. */
28015 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28016 for (t = vars; t; t = TREE_CHAIN (t))
28018 tree v = TREE_PURPOSE (t);
28020 /* FIXME diagnostics: Ideally we should keep individual
28021 locations for all the variables in the var list to make the
28022 following errors more precise. Perhaps
28023 c_parser_omp_var_list_parens should construct a list of
28024 locations to go along with the var list. */
28026 if (TREE_CODE (v) != VAR_DECL)
28027 error_at (loc, "%qD is not a variable", v);
28028 else if (TREE_TYPE (v) == error_mark_node)
28030 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28031 error_at (loc, "%qD is not a pointer variable", v);
28033 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28034 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28035 OMP_CLAUSE_DECL (u) = v;
28036 OMP_CLAUSE_CHAIN (u) = list;
28037 list = u;
28040 return list;
28043 /* OpenACC:
28044 vector_length ( expression ) */
28046 static tree
28047 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28049 tree t, c;
28050 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28051 bool error = false;
28053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28054 return list;
28056 t = cp_parser_condition (parser);
28057 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28059 error_at (location, "expected positive integer expression");
28060 error = true;
28063 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28065 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28066 /*or_comma=*/false,
28067 /*consume_paren=*/true);
28068 return list;
28071 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28072 location);
28074 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28075 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28076 OMP_CLAUSE_CHAIN (c) = list;
28077 list = c;
28079 return list;
28082 /* OpenACC 2.0
28083 Parse wait clause or directive parameters. */
28085 static tree
28086 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28088 vec<tree, va_gc> *args;
28089 tree t, args_tree;
28091 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28092 /*cast_p=*/false,
28093 /*allow_expansion_p=*/true,
28094 /*non_constant_p=*/NULL);
28096 if (args == NULL || args->length () == 0)
28098 cp_parser_error (parser, "expected integer expression before ')'");
28099 if (args != NULL)
28100 release_tree_vector (args);
28101 return list;
28104 args_tree = build_tree_list_vec (args);
28106 release_tree_vector (args);
28108 for (t = args_tree; t; t = TREE_CHAIN (t))
28110 tree targ = TREE_VALUE (t);
28112 if (targ != error_mark_node)
28114 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28115 error ("%<wait%> expression must be integral");
28116 else
28118 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28120 mark_rvalue_use (targ);
28121 OMP_CLAUSE_DECL (c) = targ;
28122 OMP_CLAUSE_CHAIN (c) = list;
28123 list = c;
28128 return list;
28131 /* OpenACC:
28132 wait ( int-expr-list ) */
28134 static tree
28135 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28137 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28139 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28140 return list;
28142 list = cp_parser_oacc_wait_list (parser, location, list);
28144 return list;
28147 /* OpenMP 3.0:
28148 collapse ( constant-expression ) */
28150 static tree
28151 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28153 tree c, num;
28154 location_t loc;
28155 HOST_WIDE_INT n;
28157 loc = cp_lexer_peek_token (parser->lexer)->location;
28158 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28159 return list;
28161 num = cp_parser_constant_expression (parser);
28162 num = maybe_constant_value (num);
28164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28165 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28166 /*or_comma=*/false,
28167 /*consume_paren=*/true);
28169 if (num == error_mark_node)
28170 return list;
28171 num = fold_non_dependent_expr (num);
28172 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28173 || !tree_fits_shwi_p (num)
28174 || (n = tree_to_shwi (num)) <= 0
28175 || (int) n != n)
28177 error_at (loc, "collapse argument needs positive constant integer expression");
28178 return list;
28181 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28182 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28183 OMP_CLAUSE_CHAIN (c) = list;
28184 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28186 return c;
28189 /* OpenMP 2.5:
28190 default ( shared | none ) */
28192 static tree
28193 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28195 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28196 tree c;
28198 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28199 return list;
28200 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28202 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28203 const char *p = IDENTIFIER_POINTER (id);
28205 switch (p[0])
28207 case 'n':
28208 if (strcmp ("none", p) != 0)
28209 goto invalid_kind;
28210 kind = OMP_CLAUSE_DEFAULT_NONE;
28211 break;
28213 case 's':
28214 if (strcmp ("shared", p) != 0)
28215 goto invalid_kind;
28216 kind = OMP_CLAUSE_DEFAULT_SHARED;
28217 break;
28219 default:
28220 goto invalid_kind;
28223 cp_lexer_consume_token (parser->lexer);
28225 else
28227 invalid_kind:
28228 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28231 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28232 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28233 /*or_comma=*/false,
28234 /*consume_paren=*/true);
28236 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28237 return list;
28239 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28240 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28241 OMP_CLAUSE_CHAIN (c) = list;
28242 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28244 return c;
28247 /* OpenMP 3.1:
28248 final ( expression ) */
28250 static tree
28251 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28253 tree t, c;
28255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28256 return list;
28258 t = cp_parser_condition (parser);
28260 if (t == error_mark_node
28261 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28263 /*or_comma=*/false,
28264 /*consume_paren=*/true);
28266 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28268 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28269 OMP_CLAUSE_FINAL_EXPR (c) = t;
28270 OMP_CLAUSE_CHAIN (c) = list;
28272 return c;
28275 /* OpenMP 2.5:
28276 if ( expression ) */
28278 static tree
28279 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28281 tree t, c;
28283 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28284 return list;
28286 t = cp_parser_condition (parser);
28288 if (t == error_mark_node
28289 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28290 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28291 /*or_comma=*/false,
28292 /*consume_paren=*/true);
28294 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28296 c = build_omp_clause (location, OMP_CLAUSE_IF);
28297 OMP_CLAUSE_IF_EXPR (c) = t;
28298 OMP_CLAUSE_CHAIN (c) = list;
28300 return c;
28303 /* OpenMP 3.1:
28304 mergeable */
28306 static tree
28307 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28308 tree list, location_t location)
28310 tree c;
28312 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28313 location);
28315 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28316 OMP_CLAUSE_CHAIN (c) = list;
28317 return c;
28320 /* OpenMP 2.5:
28321 nowait */
28323 static tree
28324 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28325 tree list, location_t location)
28327 tree c;
28329 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28331 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28332 OMP_CLAUSE_CHAIN (c) = list;
28333 return c;
28336 /* OpenACC:
28337 num_gangs ( expression ) */
28339 static tree
28340 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28342 tree t, c;
28343 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28346 return list;
28348 t = cp_parser_condition (parser);
28350 if (t == error_mark_node
28351 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28353 /*or_comma=*/false,
28354 /*consume_paren=*/true);
28356 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28358 error_at (location, "expected positive integer expression");
28359 return list;
28362 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28364 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28365 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28366 OMP_CLAUSE_CHAIN (c) = list;
28367 list = c;
28369 return list;
28372 /* OpenMP 2.5:
28373 num_threads ( expression ) */
28375 static tree
28376 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28377 location_t location)
28379 tree t, c;
28381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28382 return list;
28384 t = cp_parser_expression (parser);
28386 if (t == error_mark_node
28387 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28389 /*or_comma=*/false,
28390 /*consume_paren=*/true);
28392 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28393 "num_threads", location);
28395 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28396 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28397 OMP_CLAUSE_CHAIN (c) = list;
28399 return c;
28402 /* OpenACC:
28403 num_workers ( expression ) */
28405 static tree
28406 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28408 tree t, c;
28409 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28411 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28412 return list;
28414 t = cp_parser_condition (parser);
28416 if (t == error_mark_node
28417 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28418 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28419 /*or_comma=*/false,
28420 /*consume_paren=*/true);
28422 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28424 error_at (location, "expected positive integer expression");
28425 return list;
28428 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28429 location);
28431 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28432 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28433 OMP_CLAUSE_CHAIN (c) = list;
28434 list = c;
28436 return list;
28439 /* OpenMP 2.5:
28440 ordered */
28442 static tree
28443 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28444 tree list, location_t location)
28446 tree c;
28448 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28449 "ordered", location);
28451 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28452 OMP_CLAUSE_CHAIN (c) = list;
28453 return c;
28456 /* OpenMP 2.5:
28457 reduction ( reduction-operator : variable-list )
28459 reduction-operator:
28460 One of: + * - & ^ | && ||
28462 OpenMP 3.1:
28464 reduction-operator:
28465 One of: + * - & ^ | && || min max
28467 OpenMP 4.0:
28469 reduction-operator:
28470 One of: + * - & ^ | && ||
28471 id-expression */
28473 static tree
28474 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28476 enum tree_code code = ERROR_MARK;
28477 tree nlist, c, id = NULL_TREE;
28479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28480 return list;
28482 switch (cp_lexer_peek_token (parser->lexer)->type)
28484 case CPP_PLUS: code = PLUS_EXPR; break;
28485 case CPP_MULT: code = MULT_EXPR; break;
28486 case CPP_MINUS: code = MINUS_EXPR; break;
28487 case CPP_AND: code = BIT_AND_EXPR; break;
28488 case CPP_XOR: code = BIT_XOR_EXPR; break;
28489 case CPP_OR: code = BIT_IOR_EXPR; break;
28490 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28491 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28492 default: break;
28495 if (code != ERROR_MARK)
28496 cp_lexer_consume_token (parser->lexer);
28497 else
28499 bool saved_colon_corrects_to_scope_p;
28500 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28501 parser->colon_corrects_to_scope_p = false;
28502 id = cp_parser_id_expression (parser, /*template_p=*/false,
28503 /*check_dependency_p=*/true,
28504 /*template_p=*/NULL,
28505 /*declarator_p=*/false,
28506 /*optional_p=*/false);
28507 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28508 if (identifier_p (id))
28510 const char *p = IDENTIFIER_POINTER (id);
28512 if (strcmp (p, "min") == 0)
28513 code = MIN_EXPR;
28514 else if (strcmp (p, "max") == 0)
28515 code = MAX_EXPR;
28516 else if (id == ansi_opname (PLUS_EXPR))
28517 code = PLUS_EXPR;
28518 else if (id == ansi_opname (MULT_EXPR))
28519 code = MULT_EXPR;
28520 else if (id == ansi_opname (MINUS_EXPR))
28521 code = MINUS_EXPR;
28522 else if (id == ansi_opname (BIT_AND_EXPR))
28523 code = BIT_AND_EXPR;
28524 else if (id == ansi_opname (BIT_IOR_EXPR))
28525 code = BIT_IOR_EXPR;
28526 else if (id == ansi_opname (BIT_XOR_EXPR))
28527 code = BIT_XOR_EXPR;
28528 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28529 code = TRUTH_ANDIF_EXPR;
28530 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28531 code = TRUTH_ORIF_EXPR;
28532 id = omp_reduction_id (code, id, NULL_TREE);
28533 tree scope = parser->scope;
28534 if (scope)
28535 id = build_qualified_name (NULL_TREE, scope, id, false);
28536 parser->scope = NULL_TREE;
28537 parser->qualifying_scope = NULL_TREE;
28538 parser->object_scope = NULL_TREE;
28540 else
28542 error ("invalid reduction-identifier");
28543 resync_fail:
28544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28545 /*or_comma=*/false,
28546 /*consume_paren=*/true);
28547 return list;
28551 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28552 goto resync_fail;
28554 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28555 NULL);
28556 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28558 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28559 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28562 return nlist;
28565 /* OpenMP 2.5:
28566 schedule ( schedule-kind )
28567 schedule ( schedule-kind , expression )
28569 schedule-kind:
28570 static | dynamic | guided | runtime | auto */
28572 static tree
28573 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28575 tree c, t;
28577 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28578 return list;
28580 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28585 const char *p = IDENTIFIER_POINTER (id);
28587 switch (p[0])
28589 case 'd':
28590 if (strcmp ("dynamic", p) != 0)
28591 goto invalid_kind;
28592 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28593 break;
28595 case 'g':
28596 if (strcmp ("guided", p) != 0)
28597 goto invalid_kind;
28598 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28599 break;
28601 case 'r':
28602 if (strcmp ("runtime", p) != 0)
28603 goto invalid_kind;
28604 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28605 break;
28607 default:
28608 goto invalid_kind;
28611 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28612 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28613 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28614 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28615 else
28616 goto invalid_kind;
28617 cp_lexer_consume_token (parser->lexer);
28619 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28621 cp_token *token;
28622 cp_lexer_consume_token (parser->lexer);
28624 token = cp_lexer_peek_token (parser->lexer);
28625 t = cp_parser_assignment_expression (parser);
28627 if (t == error_mark_node)
28628 goto resync_fail;
28629 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28630 error_at (token->location, "schedule %<runtime%> does not take "
28631 "a %<chunk_size%> parameter");
28632 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28633 error_at (token->location, "schedule %<auto%> does not take "
28634 "a %<chunk_size%> parameter");
28635 else
28636 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28638 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28639 goto resync_fail;
28641 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28642 goto resync_fail;
28644 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28645 OMP_CLAUSE_CHAIN (c) = list;
28646 return c;
28648 invalid_kind:
28649 cp_parser_error (parser, "invalid schedule kind");
28650 resync_fail:
28651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28652 /*or_comma=*/false,
28653 /*consume_paren=*/true);
28654 return list;
28657 /* OpenMP 3.0:
28658 untied */
28660 static tree
28661 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28662 tree list, location_t location)
28664 tree c;
28666 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28668 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28669 OMP_CLAUSE_CHAIN (c) = list;
28670 return c;
28673 /* OpenMP 4.0:
28674 inbranch
28675 notinbranch */
28677 static tree
28678 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28679 tree list, location_t location)
28681 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28682 tree c = build_omp_clause (location, code);
28683 OMP_CLAUSE_CHAIN (c) = list;
28684 return c;
28687 /* OpenMP 4.0:
28688 parallel
28690 sections
28691 taskgroup */
28693 static tree
28694 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28695 enum omp_clause_code code,
28696 tree list, location_t location)
28698 tree c = build_omp_clause (location, code);
28699 OMP_CLAUSE_CHAIN (c) = list;
28700 return c;
28703 /* OpenMP 4.0:
28704 num_teams ( expression ) */
28706 static tree
28707 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28708 location_t location)
28710 tree t, c;
28712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28713 return list;
28715 t = cp_parser_expression (parser);
28717 if (t == error_mark_node
28718 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28720 /*or_comma=*/false,
28721 /*consume_paren=*/true);
28723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28724 "num_teams", location);
28726 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28727 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28728 OMP_CLAUSE_CHAIN (c) = list;
28730 return c;
28733 /* OpenMP 4.0:
28734 thread_limit ( expression ) */
28736 static tree
28737 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28738 location_t location)
28740 tree t, c;
28742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28743 return list;
28745 t = cp_parser_expression (parser);
28747 if (t == error_mark_node
28748 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28749 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28750 /*or_comma=*/false,
28751 /*consume_paren=*/true);
28753 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28754 "thread_limit", location);
28756 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28757 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28758 OMP_CLAUSE_CHAIN (c) = list;
28760 return c;
28763 /* OpenMP 4.0:
28764 aligned ( variable-list )
28765 aligned ( variable-list : constant-expression ) */
28767 static tree
28768 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28770 tree nlist, c, alignment = NULL_TREE;
28771 bool colon;
28773 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28774 return list;
28776 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28777 &colon);
28779 if (colon)
28781 alignment = cp_parser_constant_expression (parser);
28782 alignment = maybe_constant_value (alignment);
28784 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28785 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28786 /*or_comma=*/false,
28787 /*consume_paren=*/true);
28789 if (alignment == error_mark_node)
28790 alignment = NULL_TREE;
28793 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28794 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28796 return nlist;
28799 /* OpenMP 4.0:
28800 linear ( variable-list )
28801 linear ( variable-list : expression ) */
28803 static tree
28804 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28805 bool is_cilk_simd_fn)
28807 tree nlist, c, step = integer_one_node;
28808 bool colon;
28810 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28811 return list;
28813 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28814 &colon);
28816 if (colon)
28818 step = cp_parser_expression (parser);
28820 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28822 sorry ("using parameters for %<linear%> step is not supported yet");
28823 step = integer_one_node;
28825 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28827 /*or_comma=*/false,
28828 /*consume_paren=*/true);
28830 if (step == error_mark_node)
28831 return list;
28834 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28835 OMP_CLAUSE_LINEAR_STEP (c) = step;
28837 return nlist;
28840 /* OpenMP 4.0:
28841 safelen ( constant-expression ) */
28843 static tree
28844 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28845 location_t location)
28847 tree t, c;
28849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28850 return list;
28852 t = cp_parser_constant_expression (parser);
28853 t = maybe_constant_value (t);
28855 if (t == error_mark_node
28856 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28858 /*or_comma=*/false,
28859 /*consume_paren=*/true);
28861 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28863 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28864 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28865 OMP_CLAUSE_CHAIN (c) = list;
28867 return c;
28870 /* OpenMP 4.0:
28871 simdlen ( constant-expression ) */
28873 static tree
28874 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28875 location_t location)
28877 tree t, c;
28879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28880 return list;
28882 t = cp_parser_constant_expression (parser);
28883 t = maybe_constant_value (t);
28885 if (t == error_mark_node
28886 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28888 /*or_comma=*/false,
28889 /*consume_paren=*/true);
28891 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28893 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28894 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28895 OMP_CLAUSE_CHAIN (c) = list;
28897 return c;
28900 /* OpenMP 4.0:
28901 depend ( depend-kind : variable-list )
28903 depend-kind:
28904 in | out | inout */
28906 static tree
28907 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28909 tree nlist, c;
28910 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28912 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28913 return list;
28915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28917 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28918 const char *p = IDENTIFIER_POINTER (id);
28920 if (strcmp ("in", p) == 0)
28921 kind = OMP_CLAUSE_DEPEND_IN;
28922 else if (strcmp ("inout", p) == 0)
28923 kind = OMP_CLAUSE_DEPEND_INOUT;
28924 else if (strcmp ("out", p) == 0)
28925 kind = OMP_CLAUSE_DEPEND_OUT;
28926 else
28927 goto invalid_kind;
28929 else
28930 goto invalid_kind;
28932 cp_lexer_consume_token (parser->lexer);
28933 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28934 goto resync_fail;
28936 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28937 NULL);
28939 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28940 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28942 return nlist;
28944 invalid_kind:
28945 cp_parser_error (parser, "invalid depend kind");
28946 resync_fail:
28947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28948 /*or_comma=*/false,
28949 /*consume_paren=*/true);
28950 return list;
28953 /* OpenMP 4.0:
28954 map ( map-kind : variable-list )
28955 map ( variable-list )
28957 map-kind:
28958 alloc | to | from | tofrom */
28960 static tree
28961 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28963 tree nlist, c;
28964 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28967 return list;
28969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28970 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28972 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28973 const char *p = IDENTIFIER_POINTER (id);
28975 if (strcmp ("alloc", p) == 0)
28976 kind = GOMP_MAP_ALLOC;
28977 else if (strcmp ("to", p) == 0)
28978 kind = GOMP_MAP_TO;
28979 else if (strcmp ("from", p) == 0)
28980 kind = GOMP_MAP_FROM;
28981 else if (strcmp ("tofrom", p) == 0)
28982 kind = GOMP_MAP_TOFROM;
28983 else
28985 cp_parser_error (parser, "invalid map kind");
28986 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28987 /*or_comma=*/false,
28988 /*consume_paren=*/true);
28989 return list;
28991 cp_lexer_consume_token (parser->lexer);
28992 cp_lexer_consume_token (parser->lexer);
28995 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28996 NULL);
28998 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28999 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29001 return nlist;
29004 /* OpenMP 4.0:
29005 device ( expression ) */
29007 static tree
29008 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29009 location_t location)
29011 tree t, c;
29013 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29014 return list;
29016 t = cp_parser_expression (parser);
29018 if (t == error_mark_node
29019 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29021 /*or_comma=*/false,
29022 /*consume_paren=*/true);
29024 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29025 "device", location);
29027 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29028 OMP_CLAUSE_DEVICE_ID (c) = t;
29029 OMP_CLAUSE_CHAIN (c) = list;
29031 return c;
29034 /* OpenMP 4.0:
29035 dist_schedule ( static )
29036 dist_schedule ( static , expression ) */
29038 static tree
29039 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29040 location_t location)
29042 tree c, t;
29044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29045 return list;
29047 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29049 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29050 goto invalid_kind;
29051 cp_lexer_consume_token (parser->lexer);
29053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29055 cp_lexer_consume_token (parser->lexer);
29057 t = cp_parser_assignment_expression (parser);
29059 if (t == error_mark_node)
29060 goto resync_fail;
29061 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29064 goto resync_fail;
29066 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29067 goto resync_fail;
29069 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29070 location);
29071 OMP_CLAUSE_CHAIN (c) = list;
29072 return c;
29074 invalid_kind:
29075 cp_parser_error (parser, "invalid dist_schedule kind");
29076 resync_fail:
29077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29078 /*or_comma=*/false,
29079 /*consume_paren=*/true);
29080 return list;
29083 /* OpenMP 4.0:
29084 proc_bind ( proc-bind-kind )
29086 proc-bind-kind:
29087 master | close | spread */
29089 static tree
29090 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29091 location_t location)
29093 tree c;
29094 enum omp_clause_proc_bind_kind kind;
29096 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29097 return list;
29099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29101 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29102 const char *p = IDENTIFIER_POINTER (id);
29104 if (strcmp ("master", p) == 0)
29105 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29106 else if (strcmp ("close", p) == 0)
29107 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29108 else if (strcmp ("spread", p) == 0)
29109 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29110 else
29111 goto invalid_kind;
29113 else
29114 goto invalid_kind;
29116 cp_lexer_consume_token (parser->lexer);
29117 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29118 goto resync_fail;
29120 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29121 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29122 location);
29123 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29124 OMP_CLAUSE_CHAIN (c) = list;
29125 return c;
29127 invalid_kind:
29128 cp_parser_error (parser, "invalid depend kind");
29129 resync_fail:
29130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29131 /*or_comma=*/false,
29132 /*consume_paren=*/true);
29133 return list;
29136 /* OpenACC:
29137 async [( int-expr )] */
29139 static tree
29140 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29142 tree c, t;
29143 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29145 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29147 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29149 cp_lexer_consume_token (parser->lexer);
29151 t = cp_parser_expression (parser);
29152 if (t == error_mark_node
29153 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29155 /*or_comma=*/false,
29156 /*consume_paren=*/true);
29159 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29161 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29162 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29163 OMP_CLAUSE_CHAIN (c) = list;
29164 list = c;
29166 return list;
29169 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29170 is a bitmask in MASK. Return the list of clauses found. */
29172 static tree
29173 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29174 const char *where, cp_token *pragma_tok,
29175 bool finish_p = true)
29177 tree clauses = NULL;
29178 bool first = true;
29180 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29182 location_t here;
29183 pragma_omp_clause c_kind;
29184 const char *c_name;
29185 tree prev = clauses;
29187 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29188 cp_lexer_consume_token (parser->lexer);
29190 here = cp_lexer_peek_token (parser->lexer)->location;
29191 c_kind = cp_parser_omp_clause_name (parser);
29193 switch (c_kind)
29195 case PRAGMA_OACC_CLAUSE_ASYNC:
29196 clauses = cp_parser_oacc_clause_async (parser, clauses);
29197 c_name = "async";
29198 break;
29199 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29200 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29201 c_name = "collapse";
29202 break;
29203 case PRAGMA_OACC_CLAUSE_COPY:
29204 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29205 c_name = "copy";
29206 break;
29207 case PRAGMA_OACC_CLAUSE_COPYIN:
29208 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29209 c_name = "copyin";
29210 break;
29211 case PRAGMA_OACC_CLAUSE_COPYOUT:
29212 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29213 c_name = "copyout";
29214 break;
29215 case PRAGMA_OACC_CLAUSE_CREATE:
29216 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29217 c_name = "create";
29218 break;
29219 case PRAGMA_OACC_CLAUSE_DELETE:
29220 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29221 c_name = "delete";
29222 break;
29223 case PRAGMA_OACC_CLAUSE_DEVICE:
29224 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29225 c_name = "device";
29226 break;
29227 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29228 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29229 c_name = "deviceptr";
29230 break;
29231 case PRAGMA_OACC_CLAUSE_HOST:
29232 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29233 c_name = "host";
29234 break;
29235 case PRAGMA_OACC_CLAUSE_IF:
29236 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29237 c_name = "if";
29238 break;
29239 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29240 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29241 c_name = "num_gangs";
29242 break;
29243 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29244 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29245 c_name = "num_workers";
29246 break;
29247 case PRAGMA_OACC_CLAUSE_PRESENT:
29248 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29249 c_name = "present";
29250 break;
29251 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29252 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29253 c_name = "present_or_copy";
29254 break;
29255 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29256 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29257 c_name = "present_or_copyin";
29258 break;
29259 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29260 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29261 c_name = "present_or_copyout";
29262 break;
29263 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29264 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29265 c_name = "present_or_create";
29266 break;
29267 case PRAGMA_OACC_CLAUSE_REDUCTION:
29268 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29269 c_name = "reduction";
29270 break;
29271 case PRAGMA_OACC_CLAUSE_SELF:
29272 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29273 c_name = "self";
29274 break;
29275 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29276 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29277 c_name = "vector_length";
29278 break;
29279 case PRAGMA_OACC_CLAUSE_WAIT:
29280 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29281 c_name = "wait";
29282 break;
29283 default:
29284 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29285 goto saw_error;
29288 first = false;
29290 if (((mask >> c_kind) & 1) == 0)
29292 /* Remove the invalid clause(s) from the list to avoid
29293 confusing the rest of the compiler. */
29294 clauses = prev;
29295 error_at (here, "%qs is not valid for %qs", c_name, where);
29299 saw_error:
29300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29302 if (finish_p)
29303 return finish_omp_clauses (clauses);
29305 return clauses;
29308 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29309 is a bitmask in MASK. Return the list of clauses found; the result
29310 of clause default goes in *pdefault. */
29312 static tree
29313 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29314 const char *where, cp_token *pragma_tok,
29315 bool finish_p = true)
29317 tree clauses = NULL;
29318 bool first = true;
29319 cp_token *token = NULL;
29320 bool cilk_simd_fn = false;
29322 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29324 pragma_omp_clause c_kind;
29325 const char *c_name;
29326 tree prev = clauses;
29328 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29329 cp_lexer_consume_token (parser->lexer);
29331 token = cp_lexer_peek_token (parser->lexer);
29332 c_kind = cp_parser_omp_clause_name (parser);
29334 switch (c_kind)
29336 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29337 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29338 token->location);
29339 c_name = "collapse";
29340 break;
29341 case PRAGMA_OMP_CLAUSE_COPYIN:
29342 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29343 c_name = "copyin";
29344 break;
29345 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29346 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29347 clauses);
29348 c_name = "copyprivate";
29349 break;
29350 case PRAGMA_OMP_CLAUSE_DEFAULT:
29351 clauses = cp_parser_omp_clause_default (parser, clauses,
29352 token->location);
29353 c_name = "default";
29354 break;
29355 case PRAGMA_OMP_CLAUSE_FINAL:
29356 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29357 c_name = "final";
29358 break;
29359 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29360 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29361 clauses);
29362 c_name = "firstprivate";
29363 break;
29364 case PRAGMA_OMP_CLAUSE_IF:
29365 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29366 c_name = "if";
29367 break;
29368 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29369 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29370 clauses);
29371 c_name = "lastprivate";
29372 break;
29373 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29374 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29375 token->location);
29376 c_name = "mergeable";
29377 break;
29378 case PRAGMA_OMP_CLAUSE_NOWAIT:
29379 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29380 c_name = "nowait";
29381 break;
29382 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29383 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29384 token->location);
29385 c_name = "num_threads";
29386 break;
29387 case PRAGMA_OMP_CLAUSE_ORDERED:
29388 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29389 token->location);
29390 c_name = "ordered";
29391 break;
29392 case PRAGMA_OMP_CLAUSE_PRIVATE:
29393 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29394 clauses);
29395 c_name = "private";
29396 break;
29397 case PRAGMA_OMP_CLAUSE_REDUCTION:
29398 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29399 c_name = "reduction";
29400 break;
29401 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29402 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29403 token->location);
29404 c_name = "schedule";
29405 break;
29406 case PRAGMA_OMP_CLAUSE_SHARED:
29407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29408 clauses);
29409 c_name = "shared";
29410 break;
29411 case PRAGMA_OMP_CLAUSE_UNTIED:
29412 clauses = cp_parser_omp_clause_untied (parser, clauses,
29413 token->location);
29414 c_name = "untied";
29415 break;
29416 case PRAGMA_OMP_CLAUSE_INBRANCH:
29417 case PRAGMA_CILK_CLAUSE_MASK:
29418 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29419 clauses, token->location);
29420 c_name = "inbranch";
29421 break;
29422 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29423 case PRAGMA_CILK_CLAUSE_NOMASK:
29424 clauses = cp_parser_omp_clause_branch (parser,
29425 OMP_CLAUSE_NOTINBRANCH,
29426 clauses, token->location);
29427 c_name = "notinbranch";
29428 break;
29429 case PRAGMA_OMP_CLAUSE_PARALLEL:
29430 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29431 clauses, token->location);
29432 c_name = "parallel";
29433 if (!first)
29435 clause_not_first:
29436 error_at (token->location, "%qs must be the first clause of %qs",
29437 c_name, where);
29438 clauses = prev;
29440 break;
29441 case PRAGMA_OMP_CLAUSE_FOR:
29442 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29443 clauses, token->location);
29444 c_name = "for";
29445 if (!first)
29446 goto clause_not_first;
29447 break;
29448 case PRAGMA_OMP_CLAUSE_SECTIONS:
29449 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29450 clauses, token->location);
29451 c_name = "sections";
29452 if (!first)
29453 goto clause_not_first;
29454 break;
29455 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29456 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29457 clauses, token->location);
29458 c_name = "taskgroup";
29459 if (!first)
29460 goto clause_not_first;
29461 break;
29462 case PRAGMA_OMP_CLAUSE_TO:
29463 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29464 clauses);
29465 c_name = "to";
29466 break;
29467 case PRAGMA_OMP_CLAUSE_FROM:
29468 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29469 clauses);
29470 c_name = "from";
29471 break;
29472 case PRAGMA_OMP_CLAUSE_UNIFORM:
29473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29474 clauses);
29475 c_name = "uniform";
29476 break;
29477 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29478 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29479 token->location);
29480 c_name = "num_teams";
29481 break;
29482 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29483 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29484 token->location);
29485 c_name = "thread_limit";
29486 break;
29487 case PRAGMA_OMP_CLAUSE_ALIGNED:
29488 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29489 c_name = "aligned";
29490 break;
29491 case PRAGMA_OMP_CLAUSE_LINEAR:
29492 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29493 cilk_simd_fn = true;
29494 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29495 c_name = "linear";
29496 break;
29497 case PRAGMA_OMP_CLAUSE_DEPEND:
29498 clauses = cp_parser_omp_clause_depend (parser, clauses);
29499 c_name = "depend";
29500 break;
29501 case PRAGMA_OMP_CLAUSE_MAP:
29502 clauses = cp_parser_omp_clause_map (parser, clauses);
29503 c_name = "map";
29504 break;
29505 case PRAGMA_OMP_CLAUSE_DEVICE:
29506 clauses = cp_parser_omp_clause_device (parser, clauses,
29507 token->location);
29508 c_name = "device";
29509 break;
29510 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29511 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29512 token->location);
29513 c_name = "dist_schedule";
29514 break;
29515 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29516 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29517 token->location);
29518 c_name = "proc_bind";
29519 break;
29520 case PRAGMA_OMP_CLAUSE_SAFELEN:
29521 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29522 token->location);
29523 c_name = "safelen";
29524 break;
29525 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29526 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29527 token->location);
29528 c_name = "simdlen";
29529 break;
29530 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29531 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29532 c_name = "simdlen";
29533 break;
29534 default:
29535 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29536 goto saw_error;
29539 first = false;
29541 if (((mask >> c_kind) & 1) == 0)
29543 /* Remove the invalid clause(s) from the list to avoid
29544 confusing the rest of the compiler. */
29545 clauses = prev;
29546 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29549 saw_error:
29550 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29551 no reason to skip to the end. */
29552 if (!(flag_cilkplus && pragma_tok == NULL))
29553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29554 if (finish_p)
29555 return finish_omp_clauses (clauses);
29556 return clauses;
29559 /* OpenMP 2.5:
29560 structured-block:
29561 statement
29563 In practice, we're also interested in adding the statement to an
29564 outer node. So it is convenient if we work around the fact that
29565 cp_parser_statement calls add_stmt. */
29567 static unsigned
29568 cp_parser_begin_omp_structured_block (cp_parser *parser)
29570 unsigned save = parser->in_statement;
29572 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29573 This preserves the "not within loop or switch" style error messages
29574 for nonsense cases like
29575 void foo() {
29576 #pragma omp single
29577 break;
29580 if (parser->in_statement)
29581 parser->in_statement = IN_OMP_BLOCK;
29583 return save;
29586 static void
29587 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29589 parser->in_statement = save;
29592 static tree
29593 cp_parser_omp_structured_block (cp_parser *parser)
29595 tree stmt = begin_omp_structured_block ();
29596 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29598 cp_parser_statement (parser, NULL_TREE, false, NULL);
29600 cp_parser_end_omp_structured_block (parser, save);
29601 return finish_omp_structured_block (stmt);
29604 /* OpenMP 2.5:
29605 # pragma omp atomic new-line
29606 expression-stmt
29608 expression-stmt:
29609 x binop= expr | x++ | ++x | x-- | --x
29610 binop:
29611 +, *, -, /, &, ^, |, <<, >>
29613 where x is an lvalue expression with scalar type.
29615 OpenMP 3.1:
29616 # pragma omp atomic new-line
29617 update-stmt
29619 # pragma omp atomic read new-line
29620 read-stmt
29622 # pragma omp atomic write new-line
29623 write-stmt
29625 # pragma omp atomic update new-line
29626 update-stmt
29628 # pragma omp atomic capture new-line
29629 capture-stmt
29631 # pragma omp atomic capture new-line
29632 capture-block
29634 read-stmt:
29635 v = x
29636 write-stmt:
29637 x = expr
29638 update-stmt:
29639 expression-stmt | x = x binop expr
29640 capture-stmt:
29641 v = expression-stmt
29642 capture-block:
29643 { v = x; update-stmt; } | { update-stmt; v = x; }
29645 OpenMP 4.0:
29646 update-stmt:
29647 expression-stmt | x = x binop expr | x = expr binop x
29648 capture-stmt:
29649 v = update-stmt
29650 capture-block:
29651 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29653 where x and v are lvalue expressions with scalar type. */
29655 static void
29656 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29658 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29659 tree rhs1 = NULL_TREE, orig_lhs;
29660 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29661 bool structured_block = false;
29662 bool seq_cst = false;
29664 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29666 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29667 const char *p = IDENTIFIER_POINTER (id);
29669 if (!strcmp (p, "seq_cst"))
29671 seq_cst = true;
29672 cp_lexer_consume_token (parser->lexer);
29673 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29674 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29675 cp_lexer_consume_token (parser->lexer);
29678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29681 const char *p = IDENTIFIER_POINTER (id);
29683 if (!strcmp (p, "read"))
29684 code = OMP_ATOMIC_READ;
29685 else if (!strcmp (p, "write"))
29686 code = NOP_EXPR;
29687 else if (!strcmp (p, "update"))
29688 code = OMP_ATOMIC;
29689 else if (!strcmp (p, "capture"))
29690 code = OMP_ATOMIC_CAPTURE_NEW;
29691 else
29692 p = NULL;
29693 if (p)
29694 cp_lexer_consume_token (parser->lexer);
29696 if (!seq_cst)
29698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29699 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29700 cp_lexer_consume_token (parser->lexer);
29702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29704 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29705 const char *p = IDENTIFIER_POINTER (id);
29707 if (!strcmp (p, "seq_cst"))
29709 seq_cst = true;
29710 cp_lexer_consume_token (parser->lexer);
29714 cp_parser_require_pragma_eol (parser, pragma_tok);
29716 switch (code)
29718 case OMP_ATOMIC_READ:
29719 case NOP_EXPR: /* atomic write */
29720 v = cp_parser_unary_expression (parser);
29721 if (v == error_mark_node)
29722 goto saw_error;
29723 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29724 goto saw_error;
29725 if (code == NOP_EXPR)
29726 lhs = cp_parser_expression (parser);
29727 else
29728 lhs = cp_parser_unary_expression (parser);
29729 if (lhs == error_mark_node)
29730 goto saw_error;
29731 if (code == NOP_EXPR)
29733 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29734 opcode. */
29735 code = OMP_ATOMIC;
29736 rhs = lhs;
29737 lhs = v;
29738 v = NULL_TREE;
29740 goto done;
29741 case OMP_ATOMIC_CAPTURE_NEW:
29742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29744 cp_lexer_consume_token (parser->lexer);
29745 structured_block = true;
29747 else
29749 v = cp_parser_unary_expression (parser);
29750 if (v == error_mark_node)
29751 goto saw_error;
29752 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29753 goto saw_error;
29755 default:
29756 break;
29759 restart:
29760 lhs = cp_parser_unary_expression (parser);
29761 orig_lhs = lhs;
29762 switch (TREE_CODE (lhs))
29764 case ERROR_MARK:
29765 goto saw_error;
29767 case POSTINCREMENT_EXPR:
29768 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29769 code = OMP_ATOMIC_CAPTURE_OLD;
29770 /* FALLTHROUGH */
29771 case PREINCREMENT_EXPR:
29772 lhs = TREE_OPERAND (lhs, 0);
29773 opcode = PLUS_EXPR;
29774 rhs = integer_one_node;
29775 break;
29777 case POSTDECREMENT_EXPR:
29778 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29779 code = OMP_ATOMIC_CAPTURE_OLD;
29780 /* FALLTHROUGH */
29781 case PREDECREMENT_EXPR:
29782 lhs = TREE_OPERAND (lhs, 0);
29783 opcode = MINUS_EXPR;
29784 rhs = integer_one_node;
29785 break;
29787 case COMPOUND_EXPR:
29788 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29789 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29790 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29791 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29792 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29793 (TREE_OPERAND (lhs, 1), 0), 0)))
29794 == BOOLEAN_TYPE)
29795 /* Undo effects of boolean_increment for post {in,de}crement. */
29796 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29797 /* FALLTHRU */
29798 case MODIFY_EXPR:
29799 if (TREE_CODE (lhs) == MODIFY_EXPR
29800 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29802 /* Undo effects of boolean_increment. */
29803 if (integer_onep (TREE_OPERAND (lhs, 1)))
29805 /* This is pre or post increment. */
29806 rhs = TREE_OPERAND (lhs, 1);
29807 lhs = TREE_OPERAND (lhs, 0);
29808 opcode = NOP_EXPR;
29809 if (code == OMP_ATOMIC_CAPTURE_NEW
29810 && !structured_block
29811 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29812 code = OMP_ATOMIC_CAPTURE_OLD;
29813 break;
29816 /* FALLTHRU */
29817 default:
29818 switch (cp_lexer_peek_token (parser->lexer)->type)
29820 case CPP_MULT_EQ:
29821 opcode = MULT_EXPR;
29822 break;
29823 case CPP_DIV_EQ:
29824 opcode = TRUNC_DIV_EXPR;
29825 break;
29826 case CPP_PLUS_EQ:
29827 opcode = PLUS_EXPR;
29828 break;
29829 case CPP_MINUS_EQ:
29830 opcode = MINUS_EXPR;
29831 break;
29832 case CPP_LSHIFT_EQ:
29833 opcode = LSHIFT_EXPR;
29834 break;
29835 case CPP_RSHIFT_EQ:
29836 opcode = RSHIFT_EXPR;
29837 break;
29838 case CPP_AND_EQ:
29839 opcode = BIT_AND_EXPR;
29840 break;
29841 case CPP_OR_EQ:
29842 opcode = BIT_IOR_EXPR;
29843 break;
29844 case CPP_XOR_EQ:
29845 opcode = BIT_XOR_EXPR;
29846 break;
29847 case CPP_EQ:
29848 enum cp_parser_prec oprec;
29849 cp_token *token;
29850 cp_lexer_consume_token (parser->lexer);
29851 cp_parser_parse_tentatively (parser);
29852 rhs1 = cp_parser_simple_cast_expression (parser);
29853 if (rhs1 == error_mark_node)
29855 cp_parser_abort_tentative_parse (parser);
29856 cp_parser_simple_cast_expression (parser);
29857 goto saw_error;
29859 token = cp_lexer_peek_token (parser->lexer);
29860 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29862 cp_parser_abort_tentative_parse (parser);
29863 cp_parser_parse_tentatively (parser);
29864 rhs = cp_parser_binary_expression (parser, false, true,
29865 PREC_NOT_OPERATOR, NULL);
29866 if (rhs == error_mark_node)
29868 cp_parser_abort_tentative_parse (parser);
29869 cp_parser_binary_expression (parser, false, true,
29870 PREC_NOT_OPERATOR, NULL);
29871 goto saw_error;
29873 switch (TREE_CODE (rhs))
29875 case MULT_EXPR:
29876 case TRUNC_DIV_EXPR:
29877 case RDIV_EXPR:
29878 case PLUS_EXPR:
29879 case MINUS_EXPR:
29880 case LSHIFT_EXPR:
29881 case RSHIFT_EXPR:
29882 case BIT_AND_EXPR:
29883 case BIT_IOR_EXPR:
29884 case BIT_XOR_EXPR:
29885 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29887 if (cp_parser_parse_definitely (parser))
29889 opcode = TREE_CODE (rhs);
29890 rhs1 = TREE_OPERAND (rhs, 0);
29891 rhs = TREE_OPERAND (rhs, 1);
29892 goto stmt_done;
29894 else
29895 goto saw_error;
29897 break;
29898 default:
29899 break;
29901 cp_parser_abort_tentative_parse (parser);
29902 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29904 rhs = cp_parser_expression (parser);
29905 if (rhs == error_mark_node)
29906 goto saw_error;
29907 opcode = NOP_EXPR;
29908 rhs1 = NULL_TREE;
29909 goto stmt_done;
29911 cp_parser_error (parser,
29912 "invalid form of %<#pragma omp atomic%>");
29913 goto saw_error;
29915 if (!cp_parser_parse_definitely (parser))
29916 goto saw_error;
29917 switch (token->type)
29919 case CPP_SEMICOLON:
29920 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29922 code = OMP_ATOMIC_CAPTURE_OLD;
29923 v = lhs;
29924 lhs = NULL_TREE;
29925 lhs1 = rhs1;
29926 rhs1 = NULL_TREE;
29927 cp_lexer_consume_token (parser->lexer);
29928 goto restart;
29930 else if (structured_block)
29932 opcode = NOP_EXPR;
29933 rhs = rhs1;
29934 rhs1 = NULL_TREE;
29935 goto stmt_done;
29937 cp_parser_error (parser,
29938 "invalid form of %<#pragma omp atomic%>");
29939 goto saw_error;
29940 case CPP_MULT:
29941 opcode = MULT_EXPR;
29942 break;
29943 case CPP_DIV:
29944 opcode = TRUNC_DIV_EXPR;
29945 break;
29946 case CPP_PLUS:
29947 opcode = PLUS_EXPR;
29948 break;
29949 case CPP_MINUS:
29950 opcode = MINUS_EXPR;
29951 break;
29952 case CPP_LSHIFT:
29953 opcode = LSHIFT_EXPR;
29954 break;
29955 case CPP_RSHIFT:
29956 opcode = RSHIFT_EXPR;
29957 break;
29958 case CPP_AND:
29959 opcode = BIT_AND_EXPR;
29960 break;
29961 case CPP_OR:
29962 opcode = BIT_IOR_EXPR;
29963 break;
29964 case CPP_XOR:
29965 opcode = BIT_XOR_EXPR;
29966 break;
29967 default:
29968 cp_parser_error (parser,
29969 "invalid operator for %<#pragma omp atomic%>");
29970 goto saw_error;
29972 oprec = TOKEN_PRECEDENCE (token);
29973 gcc_assert (oprec != PREC_NOT_OPERATOR);
29974 if (commutative_tree_code (opcode))
29975 oprec = (enum cp_parser_prec) (oprec - 1);
29976 cp_lexer_consume_token (parser->lexer);
29977 rhs = cp_parser_binary_expression (parser, false, false,
29978 oprec, NULL);
29979 if (rhs == error_mark_node)
29980 goto saw_error;
29981 goto stmt_done;
29982 /* FALLTHROUGH */
29983 default:
29984 cp_parser_error (parser,
29985 "invalid operator for %<#pragma omp atomic%>");
29986 goto saw_error;
29988 cp_lexer_consume_token (parser->lexer);
29990 rhs = cp_parser_expression (parser);
29991 if (rhs == error_mark_node)
29992 goto saw_error;
29993 break;
29995 stmt_done:
29996 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29998 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29999 goto saw_error;
30000 v = cp_parser_unary_expression (parser);
30001 if (v == error_mark_node)
30002 goto saw_error;
30003 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30004 goto saw_error;
30005 lhs1 = cp_parser_unary_expression (parser);
30006 if (lhs1 == error_mark_node)
30007 goto saw_error;
30009 if (structured_block)
30011 cp_parser_consume_semicolon_at_end_of_statement (parser);
30012 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30014 done:
30015 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30016 if (!structured_block)
30017 cp_parser_consume_semicolon_at_end_of_statement (parser);
30018 return;
30020 saw_error:
30021 cp_parser_skip_to_end_of_block_or_statement (parser);
30022 if (structured_block)
30024 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30025 cp_lexer_consume_token (parser->lexer);
30026 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30028 cp_parser_skip_to_end_of_block_or_statement (parser);
30029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30030 cp_lexer_consume_token (parser->lexer);
30036 /* OpenMP 2.5:
30037 # pragma omp barrier new-line */
30039 static void
30040 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30042 cp_parser_require_pragma_eol (parser, pragma_tok);
30043 finish_omp_barrier ();
30046 /* OpenMP 2.5:
30047 # pragma omp critical [(name)] new-line
30048 structured-block */
30050 static tree
30051 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30053 tree stmt, name = NULL;
30055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30057 cp_lexer_consume_token (parser->lexer);
30059 name = cp_parser_identifier (parser);
30061 if (name == error_mark_node
30062 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30063 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30064 /*or_comma=*/false,
30065 /*consume_paren=*/true);
30066 if (name == error_mark_node)
30067 name = NULL;
30069 cp_parser_require_pragma_eol (parser, pragma_tok);
30071 stmt = cp_parser_omp_structured_block (parser);
30072 return c_finish_omp_critical (input_location, stmt, name);
30075 /* OpenMP 2.5:
30076 # pragma omp flush flush-vars[opt] new-line
30078 flush-vars:
30079 ( variable-list ) */
30081 static void
30082 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30085 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30086 cp_parser_require_pragma_eol (parser, pragma_tok);
30088 finish_omp_flush ();
30091 /* Helper function, to parse omp for increment expression. */
30093 static tree
30094 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30096 tree cond = cp_parser_binary_expression (parser, false, true,
30097 PREC_NOT_OPERATOR, NULL);
30098 if (cond == error_mark_node
30099 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30101 cp_parser_skip_to_end_of_statement (parser);
30102 return error_mark_node;
30105 switch (TREE_CODE (cond))
30107 case GT_EXPR:
30108 case GE_EXPR:
30109 case LT_EXPR:
30110 case LE_EXPR:
30111 break;
30112 case NE_EXPR:
30113 if (code == CILK_SIMD || code == CILK_FOR)
30114 break;
30115 /* Fall through: OpenMP disallows NE_EXPR. */
30116 default:
30117 return error_mark_node;
30120 /* If decl is an iterator, preserve LHS and RHS of the relational
30121 expr until finish_omp_for. */
30122 if (decl
30123 && (type_dependent_expression_p (decl)
30124 || CLASS_TYPE_P (TREE_TYPE (decl))))
30125 return cond;
30127 return build_x_binary_op (input_location, TREE_CODE (cond),
30128 TREE_OPERAND (cond, 0), ERROR_MARK,
30129 TREE_OPERAND (cond, 1), ERROR_MARK,
30130 /*overload=*/NULL, tf_warning_or_error);
30133 /* Helper function, to parse omp for increment expression. */
30135 static tree
30136 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30138 cp_token *token = cp_lexer_peek_token (parser->lexer);
30139 enum tree_code op;
30140 tree lhs, rhs;
30141 cp_id_kind idk;
30142 bool decl_first;
30144 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30146 op = (token->type == CPP_PLUS_PLUS
30147 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30148 cp_lexer_consume_token (parser->lexer);
30149 lhs = cp_parser_simple_cast_expression (parser);
30150 if (lhs != decl)
30151 return error_mark_node;
30152 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30155 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30156 if (lhs != decl)
30157 return error_mark_node;
30159 token = cp_lexer_peek_token (parser->lexer);
30160 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30162 op = (token->type == CPP_PLUS_PLUS
30163 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30164 cp_lexer_consume_token (parser->lexer);
30165 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30168 op = cp_parser_assignment_operator_opt (parser);
30169 if (op == ERROR_MARK)
30170 return error_mark_node;
30172 if (op != NOP_EXPR)
30174 rhs = cp_parser_assignment_expression (parser);
30175 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30176 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30179 lhs = cp_parser_binary_expression (parser, false, false,
30180 PREC_ADDITIVE_EXPRESSION, NULL);
30181 token = cp_lexer_peek_token (parser->lexer);
30182 decl_first = lhs == decl;
30183 if (decl_first)
30184 lhs = NULL_TREE;
30185 if (token->type != CPP_PLUS
30186 && token->type != CPP_MINUS)
30187 return error_mark_node;
30191 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30192 cp_lexer_consume_token (parser->lexer);
30193 rhs = cp_parser_binary_expression (parser, false, false,
30194 PREC_ADDITIVE_EXPRESSION, NULL);
30195 token = cp_lexer_peek_token (parser->lexer);
30196 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30198 if (lhs == NULL_TREE)
30200 if (op == PLUS_EXPR)
30201 lhs = rhs;
30202 else
30203 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30204 tf_warning_or_error);
30205 if (op != PLUS_EXPR && CONSTANT_CLASS_P (rhs))
30206 lhs = fold (lhs);
30208 else
30209 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30210 ERROR_MARK, NULL, tf_warning_or_error);
30213 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30215 if (!decl_first)
30217 if (rhs != decl || op == MINUS_EXPR)
30218 return error_mark_node;
30219 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30221 else
30222 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30224 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30227 /* Parse the initialization statement of either an OpenMP for loop or
30228 a Cilk Plus for loop.
30230 Return true if the resulting construct should have an
30231 OMP_CLAUSE_PRIVATE added to it. */
30233 static bool
30234 cp_parser_omp_for_loop_init (cp_parser *parser,
30235 enum tree_code code,
30236 tree &this_pre_body,
30237 vec<tree, va_gc> *for_block,
30238 tree &init,
30239 tree &decl,
30240 tree &real_decl)
30242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30243 return false;
30245 bool add_private_clause = false;
30247 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30249 init-expr:
30250 var = lb
30251 integer-type var = lb
30252 random-access-iterator-type var = lb
30253 pointer-type var = lb
30255 cp_decl_specifier_seq type_specifiers;
30257 /* First, try to parse as an initialized declaration. See
30258 cp_parser_condition, from whence the bulk of this is copied. */
30260 cp_parser_parse_tentatively (parser);
30261 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30262 /*is_trailing_return=*/false,
30263 &type_specifiers);
30264 if (cp_parser_parse_definitely (parser))
30266 /* If parsing a type specifier seq succeeded, then this
30267 MUST be a initialized declaration. */
30268 tree asm_specification, attributes;
30269 cp_declarator *declarator;
30271 declarator = cp_parser_declarator (parser,
30272 CP_PARSER_DECLARATOR_NAMED,
30273 /*ctor_dtor_or_conv_p=*/NULL,
30274 /*parenthesized_p=*/NULL,
30275 /*member_p=*/false,
30276 /*friend_p=*/false);
30277 attributes = cp_parser_attributes_opt (parser);
30278 asm_specification = cp_parser_asm_specification_opt (parser);
30280 if (declarator == cp_error_declarator)
30281 cp_parser_skip_to_end_of_statement (parser);
30283 else
30285 tree pushed_scope, auto_node;
30287 decl = start_decl (declarator, &type_specifiers,
30288 SD_INITIALIZED, attributes,
30289 /*prefix_attributes=*/NULL_TREE,
30290 &pushed_scope);
30292 auto_node = type_uses_auto (TREE_TYPE (decl));
30293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30295 if (cp_lexer_next_token_is (parser->lexer,
30296 CPP_OPEN_PAREN))
30298 if (code != CILK_SIMD && code != CILK_FOR)
30299 error ("parenthesized initialization is not allowed in "
30300 "OpenMP %<for%> loop");
30301 else
30302 error ("parenthesized initialization is "
30303 "not allowed in for-loop");
30305 else
30306 /* Trigger an error. */
30307 cp_parser_require (parser, CPP_EQ, RT_EQ);
30309 init = error_mark_node;
30310 cp_parser_skip_to_end_of_statement (parser);
30312 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30313 || type_dependent_expression_p (decl)
30314 || auto_node)
30316 bool is_direct_init, is_non_constant_init;
30318 init = cp_parser_initializer (parser,
30319 &is_direct_init,
30320 &is_non_constant_init);
30322 if (auto_node)
30324 TREE_TYPE (decl)
30325 = do_auto_deduction (TREE_TYPE (decl), init,
30326 auto_node);
30328 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30329 && !type_dependent_expression_p (decl))
30330 goto non_class;
30333 cp_finish_decl (decl, init, !is_non_constant_init,
30334 asm_specification,
30335 LOOKUP_ONLYCONVERTING);
30336 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30338 vec_safe_push (for_block, this_pre_body);
30339 init = NULL_TREE;
30341 else
30342 init = pop_stmt_list (this_pre_body);
30343 this_pre_body = NULL_TREE;
30345 else
30347 /* Consume '='. */
30348 cp_lexer_consume_token (parser->lexer);
30349 init = cp_parser_assignment_expression (parser);
30351 non_class:
30352 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30353 init = error_mark_node;
30354 else
30355 cp_finish_decl (decl, NULL_TREE,
30356 /*init_const_expr_p=*/false,
30357 asm_specification,
30358 LOOKUP_ONLYCONVERTING);
30361 if (pushed_scope)
30362 pop_scope (pushed_scope);
30365 else
30367 cp_id_kind idk;
30368 /* If parsing a type specifier sequence failed, then
30369 this MUST be a simple expression. */
30370 if (code == CILK_FOR)
30371 error ("%<_Cilk_for%> allows expression instead of declaration only "
30372 "in C, not in C++");
30373 cp_parser_parse_tentatively (parser);
30374 decl = cp_parser_primary_expression (parser, false, false,
30375 false, &idk);
30376 if (!cp_parser_error_occurred (parser)
30377 && decl
30378 && DECL_P (decl)
30379 && CLASS_TYPE_P (TREE_TYPE (decl)))
30381 tree rhs;
30383 cp_parser_parse_definitely (parser);
30384 cp_parser_require (parser, CPP_EQ, RT_EQ);
30385 rhs = cp_parser_assignment_expression (parser);
30386 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30387 decl, NOP_EXPR,
30388 rhs,
30389 tf_warning_or_error));
30390 add_private_clause = true;
30392 else
30394 decl = NULL;
30395 cp_parser_abort_tentative_parse (parser);
30396 init = cp_parser_expression (parser);
30397 if (init)
30399 if (TREE_CODE (init) == MODIFY_EXPR
30400 || TREE_CODE (init) == MODOP_EXPR)
30401 real_decl = TREE_OPERAND (init, 0);
30405 return add_private_clause;
30408 /* Parse the restricted form of the for statement allowed by OpenMP. */
30410 static tree
30411 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30412 tree *cclauses)
30414 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30415 tree real_decl, initv, condv, incrv, declv;
30416 tree this_pre_body, cl;
30417 location_t loc_first;
30418 bool collapse_err = false;
30419 int i, collapse = 1, nbraces = 0;
30420 vec<tree, va_gc> *for_block = make_tree_vector ();
30422 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30423 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30424 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30426 gcc_assert (collapse >= 1);
30428 declv = make_tree_vec (collapse);
30429 initv = make_tree_vec (collapse);
30430 condv = make_tree_vec (collapse);
30431 incrv = make_tree_vec (collapse);
30433 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30435 for (i = 0; i < collapse; i++)
30437 int bracecount = 0;
30438 bool add_private_clause = false;
30439 location_t loc;
30441 if (code != CILK_FOR
30442 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30444 cp_parser_error (parser, "for statement expected");
30445 return NULL;
30447 if (code == CILK_FOR
30448 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30450 cp_parser_error (parser, "_Cilk_for statement expected");
30451 return NULL;
30453 loc = cp_lexer_consume_token (parser->lexer)->location;
30455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30456 return NULL;
30458 init = decl = real_decl = NULL;
30459 this_pre_body = push_stmt_list ();
30461 add_private_clause
30462 |= cp_parser_omp_for_loop_init (parser, code,
30463 this_pre_body, for_block,
30464 init, decl, real_decl);
30466 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30467 if (this_pre_body)
30469 this_pre_body = pop_stmt_list (this_pre_body);
30470 if (pre_body)
30472 tree t = pre_body;
30473 pre_body = push_stmt_list ();
30474 add_stmt (t);
30475 add_stmt (this_pre_body);
30476 pre_body = pop_stmt_list (pre_body);
30478 else
30479 pre_body = this_pre_body;
30482 if (decl)
30483 real_decl = decl;
30484 if (cclauses != NULL
30485 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30486 && real_decl != NULL_TREE)
30488 tree *c;
30489 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30490 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30491 && OMP_CLAUSE_DECL (*c) == real_decl)
30493 error_at (loc, "iteration variable %qD"
30494 " should not be firstprivate", real_decl);
30495 *c = OMP_CLAUSE_CHAIN (*c);
30497 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30498 && OMP_CLAUSE_DECL (*c) == real_decl)
30500 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30501 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30502 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30503 OMP_CLAUSE_DECL (l) = real_decl;
30504 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30505 if (code == OMP_SIMD)
30507 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30508 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30510 else
30512 OMP_CLAUSE_CHAIN (l) = clauses;
30513 clauses = l;
30515 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30516 CP_OMP_CLAUSE_INFO (*c) = NULL;
30517 add_private_clause = false;
30519 else
30521 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30522 && OMP_CLAUSE_DECL (*c) == real_decl)
30523 add_private_clause = false;
30524 c = &OMP_CLAUSE_CHAIN (*c);
30528 if (add_private_clause)
30530 tree c;
30531 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30533 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30534 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30535 && OMP_CLAUSE_DECL (c) == decl)
30536 break;
30537 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30538 && OMP_CLAUSE_DECL (c) == decl)
30539 error_at (loc, "iteration variable %qD "
30540 "should not be firstprivate",
30541 decl);
30542 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30543 && OMP_CLAUSE_DECL (c) == decl)
30544 error_at (loc, "iteration variable %qD should not be reduction",
30545 decl);
30547 if (c == NULL)
30549 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30550 OMP_CLAUSE_DECL (c) = decl;
30551 c = finish_omp_clauses (c);
30552 if (c)
30554 OMP_CLAUSE_CHAIN (c) = clauses;
30555 clauses = c;
30560 cond = NULL;
30561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30562 cond = cp_parser_omp_for_cond (parser, decl, code);
30563 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30565 incr = NULL;
30566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30568 /* If decl is an iterator, preserve the operator on decl
30569 until finish_omp_for. */
30570 if (real_decl
30571 && ((processing_template_decl
30572 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30573 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30574 incr = cp_parser_omp_for_incr (parser, real_decl);
30575 else
30576 incr = cp_parser_expression (parser);
30577 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30578 SET_EXPR_LOCATION (incr, input_location);
30581 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30583 /*or_comma=*/false,
30584 /*consume_paren=*/true);
30586 TREE_VEC_ELT (declv, i) = decl;
30587 TREE_VEC_ELT (initv, i) = init;
30588 TREE_VEC_ELT (condv, i) = cond;
30589 TREE_VEC_ELT (incrv, i) = incr;
30591 if (i == collapse - 1)
30592 break;
30594 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30595 in between the collapsed for loops to be still considered perfectly
30596 nested. Hopefully the final version clarifies this.
30597 For now handle (multiple) {'s and empty statements. */
30598 cp_parser_parse_tentatively (parser);
30601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30602 break;
30603 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30605 cp_lexer_consume_token (parser->lexer);
30606 bracecount++;
30608 else if (bracecount
30609 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30610 cp_lexer_consume_token (parser->lexer);
30611 else
30613 loc = cp_lexer_peek_token (parser->lexer)->location;
30614 error_at (loc, "not enough collapsed for loops");
30615 collapse_err = true;
30616 cp_parser_abort_tentative_parse (parser);
30617 declv = NULL_TREE;
30618 break;
30621 while (1);
30623 if (declv)
30625 cp_parser_parse_definitely (parser);
30626 nbraces += bracecount;
30630 /* Note that we saved the original contents of this flag when we entered
30631 the structured block, and so we don't need to re-save it here. */
30632 if (code == CILK_SIMD || code == CILK_FOR)
30633 parser->in_statement = IN_CILK_SIMD_FOR;
30634 else
30635 parser->in_statement = IN_OMP_FOR;
30637 /* Note that the grammar doesn't call for a structured block here,
30638 though the loop as a whole is a structured block. */
30639 body = push_stmt_list ();
30640 cp_parser_statement (parser, NULL_TREE, false, NULL);
30641 body = pop_stmt_list (body);
30643 if (declv == NULL_TREE)
30644 ret = NULL_TREE;
30645 else
30646 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30647 pre_body, clauses);
30649 while (nbraces)
30651 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30653 cp_lexer_consume_token (parser->lexer);
30654 nbraces--;
30656 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30657 cp_lexer_consume_token (parser->lexer);
30658 else
30660 if (!collapse_err)
30662 error_at (cp_lexer_peek_token (parser->lexer)->location,
30663 "collapsed loops not perfectly nested");
30665 collapse_err = true;
30666 cp_parser_statement_seq_opt (parser, NULL);
30667 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30668 break;
30672 while (!for_block->is_empty ())
30673 add_stmt (pop_stmt_list (for_block->pop ()));
30674 release_tree_vector (for_block);
30676 return ret;
30679 /* Helper function for OpenMP parsing, split clauses and call
30680 finish_omp_clauses on each of the set of clauses afterwards. */
30682 static void
30683 cp_omp_split_clauses (location_t loc, enum tree_code code,
30684 omp_clause_mask mask, tree clauses, tree *cclauses)
30686 int i;
30687 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30688 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30689 if (cclauses[i])
30690 cclauses[i] = finish_omp_clauses (cclauses[i]);
30693 /* OpenMP 4.0:
30694 #pragma omp simd simd-clause[optseq] new-line
30695 for-loop */
30697 #define OMP_SIMD_CLAUSE_MASK \
30698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30706 static tree
30707 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30708 char *p_name, omp_clause_mask mask, tree *cclauses)
30710 tree clauses, sb, ret;
30711 unsigned int save;
30712 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30714 strcat (p_name, " simd");
30715 mask |= OMP_SIMD_CLAUSE_MASK;
30716 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30718 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30719 cclauses == NULL);
30720 if (cclauses)
30722 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30723 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30726 sb = begin_omp_structured_block ();
30727 save = cp_parser_begin_omp_structured_block (parser);
30729 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30731 cp_parser_end_omp_structured_block (parser, save);
30732 add_stmt (finish_omp_structured_block (sb));
30734 return ret;
30737 /* OpenMP 2.5:
30738 #pragma omp for for-clause[optseq] new-line
30739 for-loop
30741 OpenMP 4.0:
30742 #pragma omp for simd for-simd-clause[optseq] new-line
30743 for-loop */
30745 #define OMP_FOR_CLAUSE_MASK \
30746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30755 static tree
30756 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30757 char *p_name, omp_clause_mask mask, tree *cclauses)
30759 tree clauses, sb, ret;
30760 unsigned int save;
30761 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30763 strcat (p_name, " for");
30764 mask |= OMP_FOR_CLAUSE_MASK;
30765 if (cclauses)
30766 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30771 const char *p = IDENTIFIER_POINTER (id);
30773 if (strcmp (p, "simd") == 0)
30775 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30776 if (cclauses == NULL)
30777 cclauses = cclauses_buf;
30779 cp_lexer_consume_token (parser->lexer);
30780 if (!flag_openmp) /* flag_openmp_simd */
30781 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30782 cclauses);
30783 sb = begin_omp_structured_block ();
30784 save = cp_parser_begin_omp_structured_block (parser);
30785 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30786 cclauses);
30787 cp_parser_end_omp_structured_block (parser, save);
30788 tree body = finish_omp_structured_block (sb);
30789 if (ret == NULL)
30790 return ret;
30791 ret = make_node (OMP_FOR);
30792 TREE_TYPE (ret) = void_type_node;
30793 OMP_FOR_BODY (ret) = body;
30794 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30795 SET_EXPR_LOCATION (ret, loc);
30796 add_stmt (ret);
30797 return ret;
30800 if (!flag_openmp) /* flag_openmp_simd */
30802 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30803 return NULL_TREE;
30806 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30807 cclauses == NULL);
30808 if (cclauses)
30810 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30811 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30814 sb = begin_omp_structured_block ();
30815 save = cp_parser_begin_omp_structured_block (parser);
30817 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30819 cp_parser_end_omp_structured_block (parser, save);
30820 add_stmt (finish_omp_structured_block (sb));
30822 return ret;
30825 /* OpenMP 2.5:
30826 # pragma omp master new-line
30827 structured-block */
30829 static tree
30830 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30832 cp_parser_require_pragma_eol (parser, pragma_tok);
30833 return c_finish_omp_master (input_location,
30834 cp_parser_omp_structured_block (parser));
30837 /* OpenMP 2.5:
30838 # pragma omp ordered new-line
30839 structured-block */
30841 static tree
30842 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30844 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30845 cp_parser_require_pragma_eol (parser, pragma_tok);
30846 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30849 /* OpenMP 2.5:
30851 section-scope:
30852 { section-sequence }
30854 section-sequence:
30855 section-directive[opt] structured-block
30856 section-sequence section-directive structured-block */
30858 static tree
30859 cp_parser_omp_sections_scope (cp_parser *parser)
30861 tree stmt, substmt;
30862 bool error_suppress = false;
30863 cp_token *tok;
30865 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30866 return NULL_TREE;
30868 stmt = push_stmt_list ();
30870 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30872 substmt = cp_parser_omp_structured_block (parser);
30873 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30874 add_stmt (substmt);
30877 while (1)
30879 tok = cp_lexer_peek_token (parser->lexer);
30880 if (tok->type == CPP_CLOSE_BRACE)
30881 break;
30882 if (tok->type == CPP_EOF)
30883 break;
30885 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30887 cp_lexer_consume_token (parser->lexer);
30888 cp_parser_require_pragma_eol (parser, tok);
30889 error_suppress = false;
30891 else if (!error_suppress)
30893 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30894 error_suppress = true;
30897 substmt = cp_parser_omp_structured_block (parser);
30898 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30899 add_stmt (substmt);
30901 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30903 substmt = pop_stmt_list (stmt);
30905 stmt = make_node (OMP_SECTIONS);
30906 TREE_TYPE (stmt) = void_type_node;
30907 OMP_SECTIONS_BODY (stmt) = substmt;
30909 add_stmt (stmt);
30910 return stmt;
30913 /* OpenMP 2.5:
30914 # pragma omp sections sections-clause[optseq] newline
30915 sections-scope */
30917 #define OMP_SECTIONS_CLAUSE_MASK \
30918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30924 static tree
30925 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30926 char *p_name, omp_clause_mask mask, tree *cclauses)
30928 tree clauses, ret;
30929 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30931 strcat (p_name, " sections");
30932 mask |= OMP_SECTIONS_CLAUSE_MASK;
30933 if (cclauses)
30934 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30936 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30937 cclauses == NULL);
30938 if (cclauses)
30940 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30941 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30944 ret = cp_parser_omp_sections_scope (parser);
30945 if (ret)
30946 OMP_SECTIONS_CLAUSES (ret) = clauses;
30948 return ret;
30951 /* OpenMP 2.5:
30952 # pragma omp parallel parallel-clause[optseq] new-line
30953 structured-block
30954 # pragma omp parallel for parallel-for-clause[optseq] new-line
30955 structured-block
30956 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30957 structured-block
30959 OpenMP 4.0:
30960 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30961 structured-block */
30963 #define OMP_PARALLEL_CLAUSE_MASK \
30964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30974 static tree
30975 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30976 char *p_name, omp_clause_mask mask, tree *cclauses)
30978 tree stmt, clauses, block;
30979 unsigned int save;
30980 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30982 strcat (p_name, " parallel");
30983 mask |= OMP_PARALLEL_CLAUSE_MASK;
30985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30987 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30988 if (cclauses == NULL)
30989 cclauses = cclauses_buf;
30991 cp_lexer_consume_token (parser->lexer);
30992 if (!flag_openmp) /* flag_openmp_simd */
30993 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30994 block = begin_omp_parallel ();
30995 save = cp_parser_begin_omp_structured_block (parser);
30996 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30997 cp_parser_end_omp_structured_block (parser, save);
30998 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30999 block);
31000 if (ret == NULL_TREE)
31001 return ret;
31002 OMP_PARALLEL_COMBINED (stmt) = 1;
31003 return stmt;
31005 else if (cclauses)
31007 error_at (loc, "expected %<for%> after %qs", p_name);
31008 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31009 return NULL_TREE;
31011 else if (!flag_openmp) /* flag_openmp_simd */
31013 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31014 return NULL_TREE;
31016 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31018 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31019 const char *p = IDENTIFIER_POINTER (id);
31020 if (strcmp (p, "sections") == 0)
31022 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31023 cclauses = cclauses_buf;
31025 cp_lexer_consume_token (parser->lexer);
31026 block = begin_omp_parallel ();
31027 save = cp_parser_begin_omp_structured_block (parser);
31028 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31029 cp_parser_end_omp_structured_block (parser, save);
31030 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31031 block);
31032 OMP_PARALLEL_COMBINED (stmt) = 1;
31033 return stmt;
31037 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31039 block = begin_omp_parallel ();
31040 save = cp_parser_begin_omp_structured_block (parser);
31041 cp_parser_statement (parser, NULL_TREE, false, NULL);
31042 cp_parser_end_omp_structured_block (parser, save);
31043 stmt = finish_omp_parallel (clauses, block);
31044 return stmt;
31047 /* OpenMP 2.5:
31048 # pragma omp single single-clause[optseq] new-line
31049 structured-block */
31051 #define OMP_SINGLE_CLAUSE_MASK \
31052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31057 static tree
31058 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31060 tree stmt = make_node (OMP_SINGLE);
31061 TREE_TYPE (stmt) = void_type_node;
31063 OMP_SINGLE_CLAUSES (stmt)
31064 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31065 "#pragma omp single", pragma_tok);
31066 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31068 return add_stmt (stmt);
31071 /* OpenMP 3.0:
31072 # pragma omp task task-clause[optseq] new-line
31073 structured-block */
31075 #define OMP_TASK_CLAUSE_MASK \
31076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31086 static tree
31087 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31089 tree clauses, block;
31090 unsigned int save;
31092 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31093 "#pragma omp task", pragma_tok);
31094 block = begin_omp_task ();
31095 save = cp_parser_begin_omp_structured_block (parser);
31096 cp_parser_statement (parser, NULL_TREE, false, NULL);
31097 cp_parser_end_omp_structured_block (parser, save);
31098 return finish_omp_task (clauses, block);
31101 /* OpenMP 3.0:
31102 # pragma omp taskwait new-line */
31104 static void
31105 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31107 cp_parser_require_pragma_eol (parser, pragma_tok);
31108 finish_omp_taskwait ();
31111 /* OpenMP 3.1:
31112 # pragma omp taskyield new-line */
31114 static void
31115 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31117 cp_parser_require_pragma_eol (parser, pragma_tok);
31118 finish_omp_taskyield ();
31121 /* OpenMP 4.0:
31122 # pragma omp taskgroup new-line
31123 structured-block */
31125 static tree
31126 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31128 cp_parser_require_pragma_eol (parser, pragma_tok);
31129 return c_finish_omp_taskgroup (input_location,
31130 cp_parser_omp_structured_block (parser));
31134 /* OpenMP 2.5:
31135 # pragma omp threadprivate (variable-list) */
31137 static void
31138 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31140 tree vars;
31142 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31143 cp_parser_require_pragma_eol (parser, pragma_tok);
31145 finish_omp_threadprivate (vars);
31148 /* OpenMP 4.0:
31149 # pragma omp cancel cancel-clause[optseq] new-line */
31151 #define OMP_CANCEL_CLAUSE_MASK \
31152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31158 static void
31159 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31161 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31162 "#pragma omp cancel", pragma_tok);
31163 finish_omp_cancel (clauses);
31166 /* OpenMP 4.0:
31167 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31169 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31175 static void
31176 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31178 tree clauses;
31179 bool point_seen = false;
31181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31183 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31184 const char *p = IDENTIFIER_POINTER (id);
31186 if (strcmp (p, "point") == 0)
31188 cp_lexer_consume_token (parser->lexer);
31189 point_seen = true;
31192 if (!point_seen)
31194 cp_parser_error (parser, "expected %<point%>");
31195 cp_parser_require_pragma_eol (parser, pragma_tok);
31196 return;
31199 clauses = cp_parser_omp_all_clauses (parser,
31200 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31201 "#pragma omp cancellation point",
31202 pragma_tok);
31203 finish_omp_cancellation_point (clauses);
31206 /* OpenMP 4.0:
31207 #pragma omp distribute distribute-clause[optseq] new-line
31208 for-loop */
31210 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31216 static tree
31217 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31218 char *p_name, omp_clause_mask mask, tree *cclauses)
31220 tree clauses, sb, ret;
31221 unsigned int save;
31222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31224 strcat (p_name, " distribute");
31225 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31230 const char *p = IDENTIFIER_POINTER (id);
31231 bool simd = false;
31232 bool parallel = false;
31234 if (strcmp (p, "simd") == 0)
31235 simd = true;
31236 else
31237 parallel = strcmp (p, "parallel") == 0;
31238 if (parallel || simd)
31240 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31241 if (cclauses == NULL)
31242 cclauses = cclauses_buf;
31243 cp_lexer_consume_token (parser->lexer);
31244 if (!flag_openmp) /* flag_openmp_simd */
31246 if (simd)
31247 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31248 cclauses);
31249 else
31250 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31251 cclauses);
31253 sb = begin_omp_structured_block ();
31254 save = cp_parser_begin_omp_structured_block (parser);
31255 if (simd)
31256 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31257 cclauses);
31258 else
31259 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31260 cclauses);
31261 cp_parser_end_omp_structured_block (parser, save);
31262 tree body = finish_omp_structured_block (sb);
31263 if (ret == NULL)
31264 return ret;
31265 ret = make_node (OMP_DISTRIBUTE);
31266 TREE_TYPE (ret) = void_type_node;
31267 OMP_FOR_BODY (ret) = body;
31268 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31269 SET_EXPR_LOCATION (ret, loc);
31270 add_stmt (ret);
31271 return ret;
31274 if (!flag_openmp) /* flag_openmp_simd */
31276 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31277 return NULL_TREE;
31280 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31281 cclauses == NULL);
31282 if (cclauses)
31284 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31285 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31288 sb = begin_omp_structured_block ();
31289 save = cp_parser_begin_omp_structured_block (parser);
31291 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31293 cp_parser_end_omp_structured_block (parser, save);
31294 add_stmt (finish_omp_structured_block (sb));
31296 return ret;
31299 /* OpenMP 4.0:
31300 # pragma omp teams teams-clause[optseq] new-line
31301 structured-block */
31303 #define OMP_TEAMS_CLAUSE_MASK \
31304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31312 static tree
31313 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31314 char *p_name, omp_clause_mask mask, tree *cclauses)
31316 tree clauses, sb, ret;
31317 unsigned int save;
31318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31320 strcat (p_name, " teams");
31321 mask |= OMP_TEAMS_CLAUSE_MASK;
31323 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31326 const char *p = IDENTIFIER_POINTER (id);
31327 if (strcmp (p, "distribute") == 0)
31329 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31330 if (cclauses == NULL)
31331 cclauses = cclauses_buf;
31333 cp_lexer_consume_token (parser->lexer);
31334 if (!flag_openmp) /* flag_openmp_simd */
31335 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31336 cclauses);
31337 sb = begin_omp_structured_block ();
31338 save = cp_parser_begin_omp_structured_block (parser);
31339 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31340 cclauses);
31341 cp_parser_end_omp_structured_block (parser, save);
31342 tree body = finish_omp_structured_block (sb);
31343 if (ret == NULL)
31344 return ret;
31345 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31346 ret = make_node (OMP_TEAMS);
31347 TREE_TYPE (ret) = void_type_node;
31348 OMP_TEAMS_CLAUSES (ret) = clauses;
31349 OMP_TEAMS_BODY (ret) = body;
31350 return add_stmt (ret);
31353 if (!flag_openmp) /* flag_openmp_simd */
31355 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31356 return NULL_TREE;
31359 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31360 cclauses == NULL);
31361 if (cclauses)
31363 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31364 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31367 tree stmt = make_node (OMP_TEAMS);
31368 TREE_TYPE (stmt) = void_type_node;
31369 OMP_TEAMS_CLAUSES (stmt) = clauses;
31370 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31372 return add_stmt (stmt);
31375 /* OpenMP 4.0:
31376 # pragma omp target data target-data-clause[optseq] new-line
31377 structured-block */
31379 #define OMP_TARGET_DATA_CLAUSE_MASK \
31380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31384 static tree
31385 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31387 tree stmt = make_node (OMP_TARGET_DATA);
31388 TREE_TYPE (stmt) = void_type_node;
31390 OMP_TARGET_DATA_CLAUSES (stmt)
31391 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31392 "#pragma omp target data", pragma_tok);
31393 keep_next_level (true);
31394 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31396 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31397 return add_stmt (stmt);
31400 /* OpenMP 4.0:
31401 # pragma omp target update target-update-clause[optseq] new-line */
31403 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31409 static bool
31410 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31411 enum pragma_context context)
31413 if (context == pragma_stmt)
31415 error_at (pragma_tok->location,
31416 "%<#pragma omp target update%> may only be "
31417 "used in compound statements");
31418 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31419 return false;
31422 tree clauses
31423 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31424 "#pragma omp target update", pragma_tok);
31425 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31426 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31428 error_at (pragma_tok->location,
31429 "%<#pragma omp target update%> must contain at least one "
31430 "%<from%> or %<to%> clauses");
31431 return false;
31434 tree stmt = make_node (OMP_TARGET_UPDATE);
31435 TREE_TYPE (stmt) = void_type_node;
31436 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31437 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31438 add_stmt (stmt);
31439 return false;
31442 /* OpenMP 4.0:
31443 # pragma omp target target-clause[optseq] new-line
31444 structured-block */
31446 #define OMP_TARGET_CLAUSE_MASK \
31447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31451 static bool
31452 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31453 enum pragma_context context)
31455 if (context != pragma_stmt && context != pragma_compound)
31457 cp_parser_error (parser, "expected declaration specifiers");
31458 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31459 return false;
31462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31465 const char *p = IDENTIFIER_POINTER (id);
31467 if (strcmp (p, "teams") == 0)
31469 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31470 char p_name[sizeof ("#pragma omp target teams distribute "
31471 "parallel for simd")];
31473 cp_lexer_consume_token (parser->lexer);
31474 strcpy (p_name, "#pragma omp target");
31475 if (!flag_openmp) /* flag_openmp_simd */
31477 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31478 OMP_TARGET_CLAUSE_MASK,
31479 cclauses);
31480 return stmt != NULL_TREE;
31482 keep_next_level (true);
31483 tree sb = begin_omp_structured_block ();
31484 unsigned save = cp_parser_begin_omp_structured_block (parser);
31485 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31486 OMP_TARGET_CLAUSE_MASK, cclauses);
31487 cp_parser_end_omp_structured_block (parser, save);
31488 tree body = finish_omp_structured_block (sb);
31489 if (ret == NULL_TREE)
31490 return false;
31491 tree stmt = make_node (OMP_TARGET);
31492 TREE_TYPE (stmt) = void_type_node;
31493 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31494 OMP_TARGET_BODY (stmt) = body;
31495 add_stmt (stmt);
31496 return true;
31498 else if (!flag_openmp) /* flag_openmp_simd */
31500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31501 return false;
31503 else if (strcmp (p, "data") == 0)
31505 cp_lexer_consume_token (parser->lexer);
31506 cp_parser_omp_target_data (parser, pragma_tok);
31507 return true;
31509 else if (strcmp (p, "update") == 0)
31511 cp_lexer_consume_token (parser->lexer);
31512 return cp_parser_omp_target_update (parser, pragma_tok, context);
31516 tree stmt = make_node (OMP_TARGET);
31517 TREE_TYPE (stmt) = void_type_node;
31519 OMP_TARGET_CLAUSES (stmt)
31520 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31521 "#pragma omp target", pragma_tok);
31522 keep_next_level (true);
31523 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31525 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31526 add_stmt (stmt);
31527 return true;
31530 /* OpenACC 2.0:
31531 # pragma acc cache (variable-list) new-line
31534 static tree
31535 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31537 tree stmt, clauses;
31539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31540 clauses = finish_omp_clauses (clauses);
31542 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31544 stmt = make_node (OACC_CACHE);
31545 TREE_TYPE (stmt) = void_type_node;
31546 OACC_CACHE_CLAUSES (stmt) = clauses;
31547 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31548 add_stmt (stmt);
31550 return stmt;
31553 /* OpenACC 2.0:
31554 # pragma acc data oacc-data-clause[optseq] new-line
31555 structured-block */
31557 #define OACC_DATA_CLAUSE_MASK \
31558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31570 static tree
31571 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31573 tree stmt, clauses, block;
31574 unsigned int save;
31576 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31577 "#pragma acc data", pragma_tok);
31579 block = begin_omp_parallel ();
31580 save = cp_parser_begin_omp_structured_block (parser);
31581 cp_parser_statement (parser, NULL_TREE, false, NULL);
31582 cp_parser_end_omp_structured_block (parser, save);
31583 stmt = finish_oacc_data (clauses, block);
31584 return stmt;
31587 /* OpenACC 2.0:
31588 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31592 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31594 LOC is the location of the #pragma token.
31597 #define OACC_ENTER_DATA_CLAUSE_MASK \
31598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31606 #define OACC_EXIT_DATA_CLAUSE_MASK \
31607 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31613 static tree
31614 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31615 bool enter)
31617 tree stmt, clauses;
31619 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31620 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31622 cp_parser_error (parser, enter
31623 ? "expected %<data%> in %<#pragma acc enter data%>"
31624 : "expected %<data%> in %<#pragma acc exit data%>");
31625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31626 return NULL_TREE;
31629 const char *p =
31630 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31631 if (strcmp (p, "data") != 0)
31633 cp_parser_error (parser, "invalid pragma");
31634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31635 return NULL_TREE;
31638 cp_lexer_consume_token (parser->lexer);
31640 if (enter)
31641 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31642 "#pragma acc enter data", pragma_tok);
31643 else
31644 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31645 "#pragma acc exit data", pragma_tok);
31647 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31649 error_at (pragma_tok->location,
31650 "%<#pragma acc enter data%> has no data movement clause");
31651 return NULL_TREE;
31654 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31655 TREE_TYPE (stmt) = void_type_node;
31656 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31657 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31658 add_stmt (stmt);
31659 return stmt;
31662 /* OpenACC 2.0:
31663 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31664 structured-block */
31666 #define OACC_KERNELS_CLAUSE_MASK \
31667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31681 static tree
31682 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31684 tree stmt, clauses, block;
31685 unsigned int save;
31687 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31688 "#pragma acc kernels", pragma_tok);
31690 block = begin_omp_parallel ();
31691 save = cp_parser_begin_omp_structured_block (parser);
31692 cp_parser_statement (parser, NULL_TREE, false, NULL);
31693 cp_parser_end_omp_structured_block (parser, save);
31694 stmt = finish_oacc_kernels (clauses, block);
31695 return stmt;
31698 /* OpenACC 2.0:
31699 # pragma acc loop oacc-loop-clause[optseq] new-line
31700 structured-block */
31702 #define OACC_LOOP_CLAUSE_MASK \
31703 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31706 static tree
31707 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31709 tree stmt, clauses, block;
31710 int save;
31712 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31713 "#pragma acc loop", pragma_tok);
31715 block = begin_omp_structured_block ();
31716 save = cp_parser_begin_omp_structured_block (parser);
31717 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31718 cp_parser_end_omp_structured_block (parser, save);
31719 add_stmt (finish_omp_structured_block (block));
31720 return stmt;
31723 /* OpenACC 2.0:
31724 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31725 structured-block */
31727 #define OACC_PARALLEL_CLAUSE_MASK \
31728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31746 static tree
31747 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31749 tree stmt, clauses, block;
31750 unsigned int save;
31752 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31753 "#pragma acc parallel", pragma_tok);
31755 block = begin_omp_parallel ();
31756 save = cp_parser_begin_omp_structured_block (parser);
31757 cp_parser_statement (parser, NULL_TREE, false, NULL);
31758 cp_parser_end_omp_structured_block (parser, save);
31759 stmt = finish_oacc_parallel (clauses, block);
31760 return stmt;
31763 /* OpenACC 2.0:
31764 # pragma acc update oacc-update-clause[optseq] new-line
31767 #define OACC_UPDATE_CLAUSE_MASK \
31768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31775 static tree
31776 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31778 tree stmt, clauses;
31780 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31781 "#pragma acc update", pragma_tok);
31783 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31785 error_at (pragma_tok->location,
31786 "%<#pragma acc update%> must contain at least one "
31787 "%<device%> or %<host/self%> clause");
31788 return NULL_TREE;
31791 stmt = make_node (OACC_UPDATE);
31792 TREE_TYPE (stmt) = void_type_node;
31793 OACC_UPDATE_CLAUSES (stmt) = clauses;
31794 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31795 add_stmt (stmt);
31796 return stmt;
31799 /* OpenACC 2.0:
31800 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31802 LOC is the location of the #pragma token.
31805 #define OACC_WAIT_CLAUSE_MASK \
31806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31808 static tree
31809 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31811 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31812 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31814 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31815 list = cp_parser_oacc_wait_list (parser, loc, list);
31817 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31818 "#pragma acc wait", pragma_tok);
31820 stmt = c_finish_oacc_wait (loc, list, clauses);
31822 return stmt;
31825 /* OpenMP 4.0:
31826 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31828 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31836 static void
31837 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31838 enum pragma_context context)
31840 bool first_p = parser->omp_declare_simd == NULL;
31841 cp_omp_declare_simd_data data;
31842 if (first_p)
31844 data.error_seen = false;
31845 data.fndecl_seen = false;
31846 data.tokens = vNULL;
31847 parser->omp_declare_simd = &data;
31849 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31850 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31851 cp_lexer_consume_token (parser->lexer);
31852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31853 parser->omp_declare_simd->error_seen = true;
31854 cp_parser_require_pragma_eol (parser, pragma_tok);
31855 struct cp_token_cache *cp
31856 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31857 parser->omp_declare_simd->tokens.safe_push (cp);
31858 if (first_p)
31860 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31861 cp_parser_pragma (parser, context);
31862 switch (context)
31864 case pragma_external:
31865 cp_parser_declaration (parser);
31866 break;
31867 case pragma_member:
31868 cp_parser_member_declaration (parser);
31869 break;
31870 case pragma_objc_icode:
31871 cp_parser_block_declaration (parser, /*statement_p=*/false);
31872 break;
31873 default:
31874 cp_parser_declaration_statement (parser);
31875 break;
31877 if (parser->omp_declare_simd
31878 && !parser->omp_declare_simd->error_seen
31879 && !parser->omp_declare_simd->fndecl_seen)
31880 error_at (pragma_tok->location,
31881 "%<#pragma omp declare simd%> not immediately followed by "
31882 "function declaration or definition");
31883 data.tokens.release ();
31884 parser->omp_declare_simd = NULL;
31888 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31889 This function is modelled similar to the late parsing of omp declare
31890 simd. */
31892 static tree
31893 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31895 struct cp_token_cache *ce;
31896 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31897 int ii = 0;
31899 if (parser->omp_declare_simd != NULL)
31901 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31902 " marked as a Cilk Plus SIMD-enabled function");
31903 XDELETE (parser->cilk_simd_fn_info);
31904 parser->cilk_simd_fn_info = NULL;
31905 return attrs;
31907 if (!info->error_seen && info->fndecl_seen)
31909 error ("vector attribute not immediately followed by a single function"
31910 " declaration or definition");
31911 info->error_seen = true;
31913 if (info->error_seen)
31914 return attrs;
31916 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31918 tree c, cl;
31920 cp_parser_push_lexer_for_tokens (parser, ce);
31921 parser->lexer->in_pragma = true;
31922 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31923 "SIMD-enabled functions attribute",
31924 NULL);
31925 cp_parser_pop_lexer (parser);
31926 if (cl)
31927 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31929 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31930 TREE_CHAIN (c) = attrs;
31931 attrs = c;
31933 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31934 TREE_CHAIN (c) = attrs;
31935 if (processing_template_decl)
31936 ATTR_IS_DEPENDENT (c) = 1;
31937 attrs = c;
31939 info->fndecl_seen = true;
31940 XDELETE (parser->cilk_simd_fn_info);
31941 parser->cilk_simd_fn_info = NULL;
31942 return attrs;
31945 /* Finalize #pragma omp declare simd clauses after direct declarator has
31946 been parsed, and put that into "omp declare simd" attribute. */
31948 static tree
31949 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31951 struct cp_token_cache *ce;
31952 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31953 int i;
31955 if (!data->error_seen && data->fndecl_seen)
31957 error ("%<#pragma omp declare simd%> not immediately followed by "
31958 "a single function declaration or definition");
31959 data->error_seen = true;
31960 return attrs;
31962 if (data->error_seen)
31963 return attrs;
31965 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31967 tree c, cl;
31969 cp_parser_push_lexer_for_tokens (parser, ce);
31970 parser->lexer->in_pragma = true;
31971 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31972 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31973 cp_lexer_consume_token (parser->lexer);
31974 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31975 "#pragma omp declare simd", pragma_tok);
31976 cp_parser_pop_lexer (parser);
31977 if (cl)
31978 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31979 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31980 TREE_CHAIN (c) = attrs;
31981 if (processing_template_decl)
31982 ATTR_IS_DEPENDENT (c) = 1;
31983 attrs = c;
31986 data->fndecl_seen = true;
31987 return attrs;
31991 /* OpenMP 4.0:
31992 # pragma omp declare target new-line
31993 declarations and definitions
31994 # pragma omp end declare target new-line */
31996 static void
31997 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32000 scope_chain->omp_declare_target_attribute++;
32003 static void
32004 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32006 const char *p = "";
32007 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32009 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32010 p = IDENTIFIER_POINTER (id);
32012 if (strcmp (p, "declare") == 0)
32014 cp_lexer_consume_token (parser->lexer);
32015 p = "";
32016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32018 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32019 p = IDENTIFIER_POINTER (id);
32021 if (strcmp (p, "target") == 0)
32022 cp_lexer_consume_token (parser->lexer);
32023 else
32025 cp_parser_error (parser, "expected %<target%>");
32026 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32027 return;
32030 else
32032 cp_parser_error (parser, "expected %<declare%>");
32033 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32034 return;
32036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32037 if (!scope_chain->omp_declare_target_attribute)
32038 error_at (pragma_tok->location,
32039 "%<#pragma omp end declare target%> without corresponding "
32040 "%<#pragma omp declare target%>");
32041 else
32042 scope_chain->omp_declare_target_attribute--;
32045 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32046 expression and optional initializer clause of
32047 #pragma omp declare reduction. We store the expression(s) as
32048 either 3, 6 or 7 special statements inside of the artificial function's
32049 body. The first two statements are DECL_EXPRs for the artificial
32050 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32051 expression that uses those variables.
32052 If there was any INITIALIZER clause, this is followed by further statements,
32053 the fourth and fifth statements are DECL_EXPRs for the artificial
32054 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32055 constructor variant (first token after open paren is not omp_priv),
32056 then the sixth statement is a statement with the function call expression
32057 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32058 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32059 to initialize the OMP_PRIV artificial variable and there is seventh
32060 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32062 static bool
32063 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32065 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32066 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32067 type = TREE_TYPE (type);
32068 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32069 DECL_ARTIFICIAL (omp_out) = 1;
32070 pushdecl (omp_out);
32071 add_decl_expr (omp_out);
32072 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32073 DECL_ARTIFICIAL (omp_in) = 1;
32074 pushdecl (omp_in);
32075 add_decl_expr (omp_in);
32076 tree combiner;
32077 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32079 keep_next_level (true);
32080 tree block = begin_omp_structured_block ();
32081 combiner = cp_parser_expression (parser);
32082 finish_expr_stmt (combiner);
32083 block = finish_omp_structured_block (block);
32084 add_stmt (block);
32086 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32087 return false;
32089 const char *p = "";
32090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32093 p = IDENTIFIER_POINTER (id);
32096 if (strcmp (p, "initializer") == 0)
32098 cp_lexer_consume_token (parser->lexer);
32099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32100 return false;
32102 p = "";
32103 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32105 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32106 p = IDENTIFIER_POINTER (id);
32109 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32110 DECL_ARTIFICIAL (omp_priv) = 1;
32111 pushdecl (omp_priv);
32112 add_decl_expr (omp_priv);
32113 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32114 DECL_ARTIFICIAL (omp_orig) = 1;
32115 pushdecl (omp_orig);
32116 add_decl_expr (omp_orig);
32118 keep_next_level (true);
32119 block = begin_omp_structured_block ();
32121 bool ctor = false;
32122 if (strcmp (p, "omp_priv") == 0)
32124 bool is_direct_init, is_non_constant_init;
32125 ctor = true;
32126 cp_lexer_consume_token (parser->lexer);
32127 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32128 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32129 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32130 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32131 == CPP_CLOSE_PAREN
32132 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32133 == CPP_CLOSE_PAREN))
32135 finish_omp_structured_block (block);
32136 error ("invalid initializer clause");
32137 return false;
32139 initializer = cp_parser_initializer (parser, &is_direct_init,
32140 &is_non_constant_init);
32141 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32142 NULL_TREE, LOOKUP_ONLYCONVERTING);
32144 else
32146 cp_parser_parse_tentatively (parser);
32147 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32148 /*check_dependency_p=*/true,
32149 /*template_p=*/NULL,
32150 /*declarator_p=*/false,
32151 /*optional_p=*/false);
32152 vec<tree, va_gc> *args;
32153 if (fn_name == error_mark_node
32154 || cp_parser_error_occurred (parser)
32155 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32156 || ((args = cp_parser_parenthesized_expression_list
32157 (parser, non_attr, /*cast_p=*/false,
32158 /*allow_expansion_p=*/true,
32159 /*non_constant_p=*/NULL)),
32160 cp_parser_error_occurred (parser)))
32162 finish_omp_structured_block (block);
32163 cp_parser_abort_tentative_parse (parser);
32164 cp_parser_error (parser, "expected id-expression (arguments)");
32165 return false;
32167 unsigned int i;
32168 tree arg;
32169 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32170 if (arg == omp_priv
32171 || (TREE_CODE (arg) == ADDR_EXPR
32172 && TREE_OPERAND (arg, 0) == omp_priv))
32173 break;
32174 cp_parser_abort_tentative_parse (parser);
32175 if (arg == NULL_TREE)
32176 error ("one of the initializer call arguments should be %<omp_priv%>"
32177 " or %<&omp_priv%>");
32178 initializer = cp_parser_postfix_expression (parser, false, false, false,
32179 false, NULL);
32180 finish_expr_stmt (initializer);
32183 block = finish_omp_structured_block (block);
32184 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32185 add_stmt (block);
32187 if (ctor)
32188 add_decl_expr (omp_orig);
32190 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32191 return false;
32194 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32195 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32197 return true;
32200 /* OpenMP 4.0
32201 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32202 initializer-clause[opt] new-line
32204 initializer-clause:
32205 initializer (omp_priv initializer)
32206 initializer (function-name (argument-list)) */
32208 static void
32209 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32210 enum pragma_context)
32212 auto_vec<tree> types;
32213 enum tree_code reduc_code = ERROR_MARK;
32214 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32215 unsigned int i;
32216 cp_token *first_token;
32217 cp_token_cache *cp;
32218 int errs;
32219 void *p;
32221 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32222 p = obstack_alloc (&declarator_obstack, 0);
32224 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32225 goto fail;
32227 switch (cp_lexer_peek_token (parser->lexer)->type)
32229 case CPP_PLUS:
32230 reduc_code = PLUS_EXPR;
32231 break;
32232 case CPP_MULT:
32233 reduc_code = MULT_EXPR;
32234 break;
32235 case CPP_MINUS:
32236 reduc_code = MINUS_EXPR;
32237 break;
32238 case CPP_AND:
32239 reduc_code = BIT_AND_EXPR;
32240 break;
32241 case CPP_XOR:
32242 reduc_code = BIT_XOR_EXPR;
32243 break;
32244 case CPP_OR:
32245 reduc_code = BIT_IOR_EXPR;
32246 break;
32247 case CPP_AND_AND:
32248 reduc_code = TRUTH_ANDIF_EXPR;
32249 break;
32250 case CPP_OR_OR:
32251 reduc_code = TRUTH_ORIF_EXPR;
32252 break;
32253 case CPP_NAME:
32254 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32255 break;
32256 default:
32257 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32258 "%<|%>, %<&&%>, %<||%> or identifier");
32259 goto fail;
32262 if (reduc_code != ERROR_MARK)
32263 cp_lexer_consume_token (parser->lexer);
32265 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32266 if (reduc_id == error_mark_node)
32267 goto fail;
32269 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32270 goto fail;
32272 /* Types may not be defined in declare reduction type list. */
32273 const char *saved_message;
32274 saved_message = parser->type_definition_forbidden_message;
32275 parser->type_definition_forbidden_message
32276 = G_("types may not be defined in declare reduction type list");
32277 bool saved_colon_corrects_to_scope_p;
32278 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32279 parser->colon_corrects_to_scope_p = false;
32280 bool saved_colon_doesnt_start_class_def_p;
32281 saved_colon_doesnt_start_class_def_p
32282 = parser->colon_doesnt_start_class_def_p;
32283 parser->colon_doesnt_start_class_def_p = true;
32285 while (true)
32287 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32288 type = cp_parser_type_id (parser);
32289 if (type == error_mark_node)
32291 else if (ARITHMETIC_TYPE_P (type)
32292 && (orig_reduc_id == NULL_TREE
32293 || (TREE_CODE (type) != COMPLEX_TYPE
32294 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32295 "min") == 0
32296 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32297 "max") == 0))))
32298 error_at (loc, "predeclared arithmetic type %qT in "
32299 "%<#pragma omp declare reduction%>", type);
32300 else if (TREE_CODE (type) == FUNCTION_TYPE
32301 || TREE_CODE (type) == METHOD_TYPE
32302 || TREE_CODE (type) == ARRAY_TYPE)
32303 error_at (loc, "function or array type %qT in "
32304 "%<#pragma omp declare reduction%>", type);
32305 else if (TREE_CODE (type) == REFERENCE_TYPE)
32306 error_at (loc, "reference type %qT in "
32307 "%<#pragma omp declare reduction%>", type);
32308 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32309 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32310 "%<#pragma omp declare reduction%>", type);
32311 else
32312 types.safe_push (type);
32314 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32315 cp_lexer_consume_token (parser->lexer);
32316 else
32317 break;
32320 /* Restore the saved message. */
32321 parser->type_definition_forbidden_message = saved_message;
32322 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32323 parser->colon_doesnt_start_class_def_p
32324 = saved_colon_doesnt_start_class_def_p;
32326 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32327 || types.is_empty ())
32329 fail:
32330 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32331 goto done;
32334 first_token = cp_lexer_peek_token (parser->lexer);
32335 cp = NULL;
32336 errs = errorcount;
32337 FOR_EACH_VEC_ELT (types, i, type)
32339 tree fntype
32340 = build_function_type_list (void_type_node,
32341 cp_build_reference_type (type, false),
32342 NULL_TREE);
32343 tree this_reduc_id = reduc_id;
32344 if (!dependent_type_p (type))
32345 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32346 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32347 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32348 DECL_ARTIFICIAL (fndecl) = 1;
32349 DECL_EXTERNAL (fndecl) = 1;
32350 DECL_DECLARED_INLINE_P (fndecl) = 1;
32351 DECL_IGNORED_P (fndecl) = 1;
32352 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32353 DECL_ATTRIBUTES (fndecl)
32354 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32355 DECL_ATTRIBUTES (fndecl));
32356 if (processing_template_decl)
32357 fndecl = push_template_decl (fndecl);
32358 bool block_scope = false;
32359 tree block = NULL_TREE;
32360 if (current_function_decl)
32362 block_scope = true;
32363 DECL_CONTEXT (fndecl) = global_namespace;
32364 if (!processing_template_decl)
32365 pushdecl (fndecl);
32367 else if (current_class_type)
32369 if (cp == NULL)
32371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32372 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32373 cp_lexer_consume_token (parser->lexer);
32374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32375 goto fail;
32376 cp = cp_token_cache_new (first_token,
32377 cp_lexer_peek_nth_token (parser->lexer,
32378 2));
32380 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32381 finish_member_declaration (fndecl);
32382 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32383 DECL_PENDING_INLINE_P (fndecl) = 1;
32384 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32385 continue;
32387 else
32389 DECL_CONTEXT (fndecl) = current_namespace;
32390 pushdecl (fndecl);
32392 if (!block_scope)
32393 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32394 else
32395 block = begin_omp_structured_block ();
32396 if (cp)
32398 cp_parser_push_lexer_for_tokens (parser, cp);
32399 parser->lexer->in_pragma = true;
32401 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32403 if (!block_scope)
32404 finish_function (0);
32405 else
32406 DECL_CONTEXT (fndecl) = current_function_decl;
32407 if (cp)
32408 cp_parser_pop_lexer (parser);
32409 goto fail;
32411 if (cp)
32412 cp_parser_pop_lexer (parser);
32413 if (!block_scope)
32414 finish_function (0);
32415 else
32417 DECL_CONTEXT (fndecl) = current_function_decl;
32418 block = finish_omp_structured_block (block);
32419 if (TREE_CODE (block) == BIND_EXPR)
32420 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32421 else if (TREE_CODE (block) == STATEMENT_LIST)
32422 DECL_SAVED_TREE (fndecl) = block;
32423 if (processing_template_decl)
32424 add_decl_expr (fndecl);
32426 cp_check_omp_declare_reduction (fndecl);
32427 if (cp == NULL && types.length () > 1)
32428 cp = cp_token_cache_new (first_token,
32429 cp_lexer_peek_nth_token (parser->lexer, 2));
32430 if (errs != errorcount)
32431 break;
32434 cp_parser_require_pragma_eol (parser, pragma_tok);
32436 done:
32437 /* Free any declarators allocated. */
32438 obstack_free (&declarator_obstack, p);
32441 /* OpenMP 4.0
32442 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32443 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32444 initializer-clause[opt] new-line
32445 #pragma omp declare target new-line */
32447 static void
32448 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32449 enum pragma_context context)
32451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32454 const char *p = IDENTIFIER_POINTER (id);
32456 if (strcmp (p, "simd") == 0)
32458 cp_lexer_consume_token (parser->lexer);
32459 cp_parser_omp_declare_simd (parser, pragma_tok,
32460 context);
32461 return;
32463 cp_ensure_no_omp_declare_simd (parser);
32464 if (strcmp (p, "reduction") == 0)
32466 cp_lexer_consume_token (parser->lexer);
32467 cp_parser_omp_declare_reduction (parser, pragma_tok,
32468 context);
32469 return;
32471 if (!flag_openmp) /* flag_openmp_simd */
32473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32474 return;
32476 if (strcmp (p, "target") == 0)
32478 cp_lexer_consume_token (parser->lexer);
32479 cp_parser_omp_declare_target (parser, pragma_tok);
32480 return;
32483 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32484 "or %<target%>");
32485 cp_parser_require_pragma_eol (parser, pragma_tok);
32488 /* Main entry point to OpenMP statement pragmas. */
32490 static void
32491 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32493 tree stmt;
32494 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32495 omp_clause_mask mask (0);
32497 switch (pragma_tok->pragma_kind)
32499 case PRAGMA_OACC_CACHE:
32500 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32501 break;
32502 case PRAGMA_OACC_DATA:
32503 stmt = cp_parser_oacc_data (parser, pragma_tok);
32504 break;
32505 case PRAGMA_OACC_ENTER_DATA:
32506 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32507 break;
32508 case PRAGMA_OACC_EXIT_DATA:
32509 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32510 break;
32511 case PRAGMA_OACC_KERNELS:
32512 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32513 break;
32514 case PRAGMA_OACC_LOOP:
32515 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32516 break;
32517 case PRAGMA_OACC_PARALLEL:
32518 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32519 break;
32520 case PRAGMA_OACC_UPDATE:
32521 stmt = cp_parser_oacc_update (parser, pragma_tok);
32522 break;
32523 case PRAGMA_OACC_WAIT:
32524 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32525 break;
32526 case PRAGMA_OMP_ATOMIC:
32527 cp_parser_omp_atomic (parser, pragma_tok);
32528 return;
32529 case PRAGMA_OMP_CRITICAL:
32530 stmt = cp_parser_omp_critical (parser, pragma_tok);
32531 break;
32532 case PRAGMA_OMP_DISTRIBUTE:
32533 strcpy (p_name, "#pragma omp");
32534 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32535 break;
32536 case PRAGMA_OMP_FOR:
32537 strcpy (p_name, "#pragma omp");
32538 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32539 break;
32540 case PRAGMA_OMP_MASTER:
32541 stmt = cp_parser_omp_master (parser, pragma_tok);
32542 break;
32543 case PRAGMA_OMP_ORDERED:
32544 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32545 break;
32546 case PRAGMA_OMP_PARALLEL:
32547 strcpy (p_name, "#pragma omp");
32548 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32549 break;
32550 case PRAGMA_OMP_SECTIONS:
32551 strcpy (p_name, "#pragma omp");
32552 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32553 break;
32554 case PRAGMA_OMP_SIMD:
32555 strcpy (p_name, "#pragma omp");
32556 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32557 break;
32558 case PRAGMA_OMP_SINGLE:
32559 stmt = cp_parser_omp_single (parser, pragma_tok);
32560 break;
32561 case PRAGMA_OMP_TASK:
32562 stmt = cp_parser_omp_task (parser, pragma_tok);
32563 break;
32564 case PRAGMA_OMP_TASKGROUP:
32565 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32566 break;
32567 case PRAGMA_OMP_TEAMS:
32568 strcpy (p_name, "#pragma omp");
32569 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32570 break;
32571 default:
32572 gcc_unreachable ();
32575 if (stmt)
32576 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32579 /* Transactional Memory parsing routines. */
32581 /* Parse a transaction attribute.
32583 txn-attribute:
32584 attribute
32585 [ [ identifier ] ]
32587 ??? Simplify this when C++0x bracket attributes are
32588 implemented properly. */
32590 static tree
32591 cp_parser_txn_attribute_opt (cp_parser *parser)
32593 cp_token *token;
32594 tree attr_name, attr = NULL;
32596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32597 return cp_parser_attributes_opt (parser);
32599 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32600 return NULL_TREE;
32601 cp_lexer_consume_token (parser->lexer);
32602 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32603 goto error1;
32605 token = cp_lexer_peek_token (parser->lexer);
32606 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32608 token = cp_lexer_consume_token (parser->lexer);
32610 attr_name = (token->type == CPP_KEYWORD
32611 /* For keywords, use the canonical spelling,
32612 not the parsed identifier. */
32613 ? ridpointers[(int) token->keyword]
32614 : token->u.value);
32615 attr = build_tree_list (attr_name, NULL_TREE);
32617 else
32618 cp_parser_error (parser, "expected identifier");
32620 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32621 error1:
32622 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32623 return attr;
32626 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32628 transaction-statement:
32629 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32630 compound-statement
32631 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32634 static tree
32635 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32637 unsigned char old_in = parser->in_transaction;
32638 unsigned char this_in = 1, new_in;
32639 cp_token *token;
32640 tree stmt, attrs, noex;
32642 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32643 || keyword == RID_TRANSACTION_RELAXED);
32644 token = cp_parser_require_keyword (parser, keyword,
32645 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32646 : RT_TRANSACTION_RELAXED));
32647 gcc_assert (token != NULL);
32649 if (keyword == RID_TRANSACTION_RELAXED)
32650 this_in |= TM_STMT_ATTR_RELAXED;
32651 else
32653 attrs = cp_parser_txn_attribute_opt (parser);
32654 if (attrs)
32655 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32658 /* Parse a noexcept specification. */
32659 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32661 /* Keep track if we're in the lexical scope of an outer transaction. */
32662 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32664 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32666 parser->in_transaction = new_in;
32667 cp_parser_compound_statement (parser, NULL, false, false);
32668 parser->in_transaction = old_in;
32670 finish_transaction_stmt (stmt, NULL, this_in, noex);
32672 return stmt;
32675 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32677 transaction-expression:
32678 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32679 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32682 static tree
32683 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32685 unsigned char old_in = parser->in_transaction;
32686 unsigned char this_in = 1;
32687 cp_token *token;
32688 tree expr, noex;
32689 bool noex_expr;
32691 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32692 || keyword == RID_TRANSACTION_RELAXED);
32694 if (!flag_tm)
32695 error (keyword == RID_TRANSACTION_RELAXED
32696 ? G_("%<__transaction_relaxed%> without transactional memory "
32697 "support enabled")
32698 : G_("%<__transaction_atomic%> without transactional memory "
32699 "support enabled"));
32701 token = cp_parser_require_keyword (parser, keyword,
32702 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32703 : RT_TRANSACTION_RELAXED));
32704 gcc_assert (token != NULL);
32706 if (keyword == RID_TRANSACTION_RELAXED)
32707 this_in |= TM_STMT_ATTR_RELAXED;
32709 /* Set this early. This might mean that we allow transaction_cancel in
32710 an expression that we find out later actually has to be a constexpr.
32711 However, we expect that cxx_constant_value will be able to deal with
32712 this; also, if the noexcept has no constexpr, then what we parse next
32713 really is a transaction's body. */
32714 parser->in_transaction = this_in;
32716 /* Parse a noexcept specification. */
32717 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32718 true);
32720 if (!noex || !noex_expr
32721 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32723 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32725 expr = cp_parser_expression (parser);
32726 expr = finish_parenthesized_expr (expr);
32728 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32730 else
32732 /* The only expression that is available got parsed for the noexcept
32733 already. noexcept is true then. */
32734 expr = noex;
32735 noex = boolean_true_node;
32738 expr = build_transaction_expr (token->location, expr, this_in, noex);
32739 parser->in_transaction = old_in;
32741 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32742 return error_mark_node;
32744 return (flag_tm ? expr : error_mark_node);
32747 /* Parse a function-transaction-block.
32749 function-transaction-block:
32750 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32751 function-body
32752 __transaction_atomic txn-attribute[opt] function-try-block
32753 __transaction_relaxed ctor-initializer[opt] function-body
32754 __transaction_relaxed function-try-block
32757 static bool
32758 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32760 unsigned char old_in = parser->in_transaction;
32761 unsigned char new_in = 1;
32762 tree compound_stmt, stmt, attrs;
32763 bool ctor_initializer_p;
32764 cp_token *token;
32766 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32767 || keyword == RID_TRANSACTION_RELAXED);
32768 token = cp_parser_require_keyword (parser, keyword,
32769 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32770 : RT_TRANSACTION_RELAXED));
32771 gcc_assert (token != NULL);
32773 if (keyword == RID_TRANSACTION_RELAXED)
32774 new_in |= TM_STMT_ATTR_RELAXED;
32775 else
32777 attrs = cp_parser_txn_attribute_opt (parser);
32778 if (attrs)
32779 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32782 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32784 parser->in_transaction = new_in;
32786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32787 ctor_initializer_p = cp_parser_function_try_block (parser);
32788 else
32789 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32790 (parser, /*in_function_try_block=*/false);
32792 parser->in_transaction = old_in;
32794 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32796 return ctor_initializer_p;
32799 /* Parse a __transaction_cancel statement.
32801 cancel-statement:
32802 __transaction_cancel txn-attribute[opt] ;
32803 __transaction_cancel txn-attribute[opt] throw-expression ;
32805 ??? Cancel and throw is not yet implemented. */
32807 static tree
32808 cp_parser_transaction_cancel (cp_parser *parser)
32810 cp_token *token;
32811 bool is_outer = false;
32812 tree stmt, attrs;
32814 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32815 RT_TRANSACTION_CANCEL);
32816 gcc_assert (token != NULL);
32818 attrs = cp_parser_txn_attribute_opt (parser);
32819 if (attrs)
32820 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32822 /* ??? Parse cancel-and-throw here. */
32824 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32826 if (!flag_tm)
32828 error_at (token->location, "%<__transaction_cancel%> without "
32829 "transactional memory support enabled");
32830 return error_mark_node;
32832 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32834 error_at (token->location, "%<__transaction_cancel%> within a "
32835 "%<__transaction_relaxed%>");
32836 return error_mark_node;
32838 else if (is_outer)
32840 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32841 && !is_tm_may_cancel_outer (current_function_decl))
32843 error_at (token->location, "outer %<__transaction_cancel%> not "
32844 "within outer %<__transaction_atomic%>");
32845 error_at (token->location,
32846 " or a %<transaction_may_cancel_outer%> function");
32847 return error_mark_node;
32850 else if (parser->in_transaction == 0)
32852 error_at (token->location, "%<__transaction_cancel%> not within "
32853 "%<__transaction_atomic%>");
32854 return error_mark_node;
32857 stmt = build_tm_abort_call (token->location, is_outer);
32858 add_stmt (stmt);
32860 return stmt;
32863 /* The parser. */
32865 static GTY (()) cp_parser *the_parser;
32868 /* Special handling for the first token or line in the file. The first
32869 thing in the file might be #pragma GCC pch_preprocess, which loads a
32870 PCH file, which is a GC collection point. So we need to handle this
32871 first pragma without benefit of an existing lexer structure.
32873 Always returns one token to the caller in *FIRST_TOKEN. This is
32874 either the true first token of the file, or the first token after
32875 the initial pragma. */
32877 static void
32878 cp_parser_initial_pragma (cp_token *first_token)
32880 tree name = NULL;
32882 cp_lexer_get_preprocessor_token (NULL, first_token);
32883 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32884 return;
32886 cp_lexer_get_preprocessor_token (NULL, first_token);
32887 if (first_token->type == CPP_STRING)
32889 name = first_token->u.value;
32891 cp_lexer_get_preprocessor_token (NULL, first_token);
32892 if (first_token->type != CPP_PRAGMA_EOL)
32893 error_at (first_token->location,
32894 "junk at end of %<#pragma GCC pch_preprocess%>");
32896 else
32897 error_at (first_token->location, "expected string literal");
32899 /* Skip to the end of the pragma. */
32900 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32901 cp_lexer_get_preprocessor_token (NULL, first_token);
32903 /* Now actually load the PCH file. */
32904 if (name)
32905 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32907 /* Read one more token to return to our caller. We have to do this
32908 after reading the PCH file in, since its pointers have to be
32909 live. */
32910 cp_lexer_get_preprocessor_token (NULL, first_token);
32913 /* Parses the grainsize pragma for the _Cilk_for statement.
32914 Syntax:
32915 #pragma cilk grainsize = <VALUE>. */
32917 static void
32918 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32920 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32922 tree exp = cp_parser_binary_expression (parser, false, false,
32923 PREC_NOT_OPERATOR, NULL);
32924 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32925 if (!exp || exp == error_mark_node)
32927 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32928 return;
32931 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32933 cp_parser_cilk_for (parser, maybe_constant_value (exp));
32934 else
32935 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32936 "%<#pragma cilk grainsize%> is not followed by "
32937 "%<_Cilk_for%>");
32938 return;
32940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32943 /* Normal parsing of a pragma token. Here we can (and must) use the
32944 regular lexer. */
32946 static bool
32947 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32949 cp_token *pragma_tok;
32950 unsigned int id;
32952 pragma_tok = cp_lexer_consume_token (parser->lexer);
32953 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32954 parser->lexer->in_pragma = true;
32956 id = pragma_tok->pragma_kind;
32957 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32958 cp_ensure_no_omp_declare_simd (parser);
32959 switch (id)
32961 case PRAGMA_GCC_PCH_PREPROCESS:
32962 error_at (pragma_tok->location,
32963 "%<#pragma GCC pch_preprocess%> must be first");
32964 break;
32966 case PRAGMA_OMP_BARRIER:
32967 switch (context)
32969 case pragma_compound:
32970 cp_parser_omp_barrier (parser, pragma_tok);
32971 return false;
32972 case pragma_stmt:
32973 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32974 "used in compound statements");
32975 break;
32976 default:
32977 goto bad_stmt;
32979 break;
32981 case PRAGMA_OMP_FLUSH:
32982 switch (context)
32984 case pragma_compound:
32985 cp_parser_omp_flush (parser, pragma_tok);
32986 return false;
32987 case pragma_stmt:
32988 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32989 "used in compound statements");
32990 break;
32991 default:
32992 goto bad_stmt;
32994 break;
32996 case PRAGMA_OMP_TASKWAIT:
32997 switch (context)
32999 case pragma_compound:
33000 cp_parser_omp_taskwait (parser, pragma_tok);
33001 return false;
33002 case pragma_stmt:
33003 error_at (pragma_tok->location,
33004 "%<#pragma omp taskwait%> may only be "
33005 "used in compound statements");
33006 break;
33007 default:
33008 goto bad_stmt;
33010 break;
33012 case PRAGMA_OMP_TASKYIELD:
33013 switch (context)
33015 case pragma_compound:
33016 cp_parser_omp_taskyield (parser, pragma_tok);
33017 return false;
33018 case pragma_stmt:
33019 error_at (pragma_tok->location,
33020 "%<#pragma omp taskyield%> may only be "
33021 "used in compound statements");
33022 break;
33023 default:
33024 goto bad_stmt;
33026 break;
33028 case PRAGMA_OMP_CANCEL:
33029 switch (context)
33031 case pragma_compound:
33032 cp_parser_omp_cancel (parser, pragma_tok);
33033 return false;
33034 case pragma_stmt:
33035 error_at (pragma_tok->location,
33036 "%<#pragma omp cancel%> may only be "
33037 "used in compound statements");
33038 break;
33039 default:
33040 goto bad_stmt;
33042 break;
33044 case PRAGMA_OMP_CANCELLATION_POINT:
33045 switch (context)
33047 case pragma_compound:
33048 cp_parser_omp_cancellation_point (parser, pragma_tok);
33049 return false;
33050 case pragma_stmt:
33051 error_at (pragma_tok->location,
33052 "%<#pragma omp cancellation point%> may only be "
33053 "used in compound statements");
33054 break;
33055 default:
33056 goto bad_stmt;
33058 break;
33060 case PRAGMA_OMP_THREADPRIVATE:
33061 cp_parser_omp_threadprivate (parser, pragma_tok);
33062 return false;
33064 case PRAGMA_OMP_DECLARE_REDUCTION:
33065 cp_parser_omp_declare (parser, pragma_tok, context);
33066 return false;
33068 case PRAGMA_OACC_CACHE:
33069 case PRAGMA_OACC_DATA:
33070 case PRAGMA_OACC_ENTER_DATA:
33071 case PRAGMA_OACC_EXIT_DATA:
33072 case PRAGMA_OACC_KERNELS:
33073 case PRAGMA_OACC_PARALLEL:
33074 case PRAGMA_OACC_LOOP:
33075 case PRAGMA_OACC_UPDATE:
33076 case PRAGMA_OACC_WAIT:
33077 case PRAGMA_OMP_ATOMIC:
33078 case PRAGMA_OMP_CRITICAL:
33079 case PRAGMA_OMP_DISTRIBUTE:
33080 case PRAGMA_OMP_FOR:
33081 case PRAGMA_OMP_MASTER:
33082 case PRAGMA_OMP_ORDERED:
33083 case PRAGMA_OMP_PARALLEL:
33084 case PRAGMA_OMP_SECTIONS:
33085 case PRAGMA_OMP_SIMD:
33086 case PRAGMA_OMP_SINGLE:
33087 case PRAGMA_OMP_TASK:
33088 case PRAGMA_OMP_TASKGROUP:
33089 case PRAGMA_OMP_TEAMS:
33090 if (context != pragma_stmt && context != pragma_compound)
33091 goto bad_stmt;
33092 cp_parser_omp_construct (parser, pragma_tok);
33093 return true;
33095 case PRAGMA_OMP_TARGET:
33096 return cp_parser_omp_target (parser, pragma_tok, context);
33098 case PRAGMA_OMP_END_DECLARE_TARGET:
33099 cp_parser_omp_end_declare_target (parser, pragma_tok);
33100 return false;
33102 case PRAGMA_OMP_SECTION:
33103 error_at (pragma_tok->location,
33104 "%<#pragma omp section%> may only be used in "
33105 "%<#pragma omp sections%> construct");
33106 break;
33108 case PRAGMA_IVDEP:
33110 if (context == pragma_external)
33112 error_at (pragma_tok->location,
33113 "%<#pragma GCC ivdep%> must be inside a function");
33114 break;
33116 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33117 cp_token *tok;
33118 tok = cp_lexer_peek_token (the_parser->lexer);
33119 if (tok->type != CPP_KEYWORD
33120 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33121 && tok->keyword != RID_DO))
33123 cp_parser_error (parser, "for, while or do statement expected");
33124 return false;
33126 cp_parser_iteration_statement (parser, true);
33127 return true;
33130 case PRAGMA_CILK_SIMD:
33131 if (context == pragma_external)
33133 error_at (pragma_tok->location,
33134 "%<#pragma simd%> must be inside a function");
33135 break;
33137 cp_parser_cilk_simd (parser, pragma_tok);
33138 return true;
33140 case PRAGMA_CILK_GRAINSIZE:
33141 if (context == pragma_external)
33143 error_at (pragma_tok->location,
33144 "%<#pragma cilk grainsize%> must be inside a function");
33145 break;
33148 /* Ignore the pragma if Cilk Plus is not enabled. */
33149 if (flag_cilkplus)
33151 cp_parser_cilk_grainsize (parser, pragma_tok);
33152 return true;
33154 else
33156 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33157 "%<#pragma cilk grainsize%>");
33158 break;
33161 default:
33162 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33163 c_invoke_pragma_handler (id);
33164 break;
33166 bad_stmt:
33167 cp_parser_error (parser, "expected declaration specifiers");
33168 break;
33171 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33172 return false;
33175 /* The interface the pragma parsers have to the lexer. */
33177 enum cpp_ttype
33178 pragma_lex (tree *value)
33180 cp_token *tok;
33181 enum cpp_ttype ret;
33183 tok = cp_lexer_peek_token (the_parser->lexer);
33185 ret = tok->type;
33186 *value = tok->u.value;
33188 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33189 ret = CPP_EOF;
33190 else if (ret == CPP_STRING)
33191 *value = cp_parser_string_literal (the_parser, false, false);
33192 else
33194 cp_lexer_consume_token (the_parser->lexer);
33195 if (ret == CPP_KEYWORD)
33196 ret = CPP_NAME;
33199 return ret;
33203 /* External interface. */
33205 /* Parse one entire translation unit. */
33207 void
33208 c_parse_file (void)
33210 static bool already_called = false;
33212 if (already_called)
33213 fatal_error (input_location,
33214 "inter-module optimizations not implemented for C++");
33215 already_called = true;
33217 the_parser = cp_parser_new ();
33218 push_deferring_access_checks (flag_access_control
33219 ? dk_no_deferred : dk_no_check);
33220 cp_parser_translation_unit (the_parser);
33221 the_parser = NULL;
33224 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33225 vectorlength clause:
33226 Syntax:
33227 vectorlength ( constant-expression ) */
33229 static tree
33230 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33231 bool is_simd_fn)
33233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33234 tree expr;
33235 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33236 safelen clause. Thus, vectorlength is represented as OMP 4.0
33237 safelen. For SIMD-enabled function it is represented by OMP 4.0
33238 simdlen. */
33239 if (!is_simd_fn)
33240 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33241 loc);
33242 else
33243 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33244 loc);
33246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33247 return error_mark_node;
33249 expr = cp_parser_constant_expression (parser);
33250 expr = maybe_constant_value (expr);
33252 /* If expr == error_mark_node, then don't emit any errors nor
33253 create a clause. if any of the above functions returns
33254 error mark node then they would have emitted an error message. */
33255 if (expr == error_mark_node)
33257 else if (!TREE_TYPE (expr)
33258 || !TREE_CONSTANT (expr)
33259 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33260 error_at (loc, "vectorlength must be an integer constant");
33261 else if (TREE_CONSTANT (expr)
33262 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33263 error_at (loc, "vectorlength must be a power of 2");
33264 else
33266 tree c;
33267 if (!is_simd_fn)
33269 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33270 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33271 OMP_CLAUSE_CHAIN (c) = clauses;
33272 clauses = c;
33274 else
33276 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33277 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33278 OMP_CLAUSE_CHAIN (c) = clauses;
33279 clauses = c;
33283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33284 return error_mark_node;
33285 return clauses;
33288 /* Handles the Cilk Plus #pragma simd linear clause.
33289 Syntax:
33290 linear ( simd-linear-variable-list )
33292 simd-linear-variable-list:
33293 simd-linear-variable
33294 simd-linear-variable-list , simd-linear-variable
33296 simd-linear-variable:
33297 id-expression
33298 id-expression : simd-linear-step
33300 simd-linear-step:
33301 conditional-expression */
33303 static tree
33304 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33306 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33309 return clauses;
33310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33312 cp_parser_error (parser, "expected identifier");
33313 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33314 return error_mark_node;
33317 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33318 parser->colon_corrects_to_scope_p = false;
33319 while (1)
33321 cp_token *token = cp_lexer_peek_token (parser->lexer);
33322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33324 cp_parser_error (parser, "expected variable-name");
33325 clauses = error_mark_node;
33326 break;
33329 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33330 false, false);
33331 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33332 token->location);
33333 if (decl == error_mark_node)
33335 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33336 token->location);
33337 clauses = error_mark_node;
33339 else
33341 tree e = NULL_TREE;
33342 tree step_size = integer_one_node;
33344 /* If present, parse the linear step. Otherwise, assume the default
33345 value of 1. */
33346 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33348 cp_lexer_consume_token (parser->lexer);
33350 e = cp_parser_assignment_expression (parser);
33351 e = maybe_constant_value (e);
33353 if (e == error_mark_node)
33355 /* If an error has occurred, then the whole pragma is
33356 considered ill-formed. Thus, no reason to keep
33357 parsing. */
33358 clauses = error_mark_node;
33359 break;
33361 else if (type_dependent_expression_p (e)
33362 || value_dependent_expression_p (e)
33363 || (TREE_TYPE (e)
33364 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33365 && (TREE_CONSTANT (e)
33366 || DECL_P (e))))
33367 step_size = e;
33368 else
33369 cp_parser_error (parser,
33370 "step size must be an integer constant "
33371 "expression or an integer variable");
33374 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33375 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33376 OMP_CLAUSE_DECL (l) = decl;
33377 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33378 OMP_CLAUSE_CHAIN (l) = clauses;
33379 clauses = l;
33381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33382 cp_lexer_consume_token (parser->lexer);
33383 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33384 break;
33385 else
33387 error_at (cp_lexer_peek_token (parser->lexer)->location,
33388 "expected %<,%> or %<)%> after %qE", decl);
33389 clauses = error_mark_node;
33390 break;
33393 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33394 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33395 return clauses;
33398 /* Returns the name of the next clause. If the clause is not
33399 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33400 token is not consumed. Otherwise, the appropriate enum from the
33401 pragma_simd_clause is returned and the token is consumed. */
33403 static pragma_omp_clause
33404 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33406 pragma_omp_clause clause_type;
33407 cp_token *token = cp_lexer_peek_token (parser->lexer);
33409 if (token->keyword == RID_PRIVATE)
33410 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33411 else if (!token->u.value || token->type != CPP_NAME)
33412 return PRAGMA_CILK_CLAUSE_NONE;
33413 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33414 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33415 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33416 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33417 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33418 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33419 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33420 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33421 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33422 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33423 else
33424 return PRAGMA_CILK_CLAUSE_NONE;
33426 cp_lexer_consume_token (parser->lexer);
33427 return clause_type;
33430 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33432 static tree
33433 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33435 tree clauses = NULL_TREE;
33437 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33438 && clauses != error_mark_node)
33440 pragma_omp_clause c_kind;
33441 c_kind = cp_parser_cilk_simd_clause_name (parser);
33442 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33443 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33444 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33445 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33446 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33447 /* Use the OpenMP 4.0 equivalent function. */
33448 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33449 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33450 /* Use the OpenMP 4.0 equivalent function. */
33451 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33452 clauses);
33453 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33454 /* Use the OMP 4.0 equivalent function. */
33455 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33456 clauses);
33457 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33458 /* Use the OMP 4.0 equivalent function. */
33459 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33460 else
33462 clauses = error_mark_node;
33463 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33464 break;
33468 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33470 if (clauses == error_mark_node)
33471 return error_mark_node;
33472 else
33473 return c_finish_cilk_clauses (clauses);
33476 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33478 static void
33479 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33481 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33483 if (clauses == error_mark_node)
33484 return;
33486 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33488 error_at (cp_lexer_peek_token (parser->lexer)->location,
33489 "for statement expected");
33490 return;
33493 tree sb = begin_omp_structured_block ();
33494 int save = cp_parser_begin_omp_structured_block (parser);
33495 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33496 if (ret)
33497 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33498 cp_parser_end_omp_structured_block (parser, save);
33499 add_stmt (finish_omp_structured_block (sb));
33502 /* Main entry-point for parsing Cilk Plus _Cilk_for
33503 loops. The return value is error_mark_node
33504 when errors happen and CILK_FOR tree on success. */
33506 static tree
33507 cp_parser_cilk_for (cp_parser *parser, tree grain)
33509 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33510 gcc_unreachable ();
33512 tree sb = begin_omp_structured_block ();
33513 int save = cp_parser_begin_omp_structured_block (parser);
33515 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33516 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33517 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33518 clauses = finish_omp_clauses (clauses);
33520 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33521 if (ret)
33522 cpp_validate_cilk_plus_loop (ret);
33523 else
33524 ret = error_mark_node;
33526 cp_parser_end_omp_structured_block (parser, save);
33527 add_stmt (finish_omp_structured_block (sb));
33528 return ret;
33531 /* Create an identifier for a generic parameter type (a synthesized
33532 template parameter implied by `auto' or a concept identifier). */
33534 static GTY(()) int generic_parm_count;
33535 static tree
33536 make_generic_type_name ()
33538 char buf[32];
33539 sprintf (buf, "auto:%d", ++generic_parm_count);
33540 return get_identifier (buf);
33543 /* Predicate that behaves as is_auto_or_concept but matches the parent
33544 node of the generic type rather than the generic type itself. This
33545 allows for type transformation in add_implicit_template_parms. */
33547 static inline bool
33548 tree_type_is_auto_or_concept (const_tree t)
33550 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33553 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33554 (creating a new template parameter list if necessary). Returns the newly
33555 created template type parm. */
33557 tree
33558 synthesize_implicit_template_parm (cp_parser *parser)
33560 gcc_assert (current_binding_level->kind == sk_function_parms);
33562 /* We are either continuing a function template that already contains implicit
33563 template parameters, creating a new fully-implicit function template, or
33564 extending an existing explicit function template with implicit template
33565 parameters. */
33567 cp_binding_level *const entry_scope = current_binding_level;
33569 bool become_template = false;
33570 cp_binding_level *parent_scope = 0;
33572 if (parser->implicit_template_scope)
33574 gcc_assert (parser->implicit_template_parms);
33576 current_binding_level = parser->implicit_template_scope;
33578 else
33580 /* Roll back to the existing template parameter scope (in the case of
33581 extending an explicit function template) or introduce a new template
33582 parameter scope ahead of the function parameter scope (or class scope
33583 in the case of out-of-line member definitions). The function scope is
33584 added back after template parameter synthesis below. */
33586 cp_binding_level *scope = entry_scope;
33588 while (scope->kind == sk_function_parms)
33590 parent_scope = scope;
33591 scope = scope->level_chain;
33593 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33595 /* If not defining a class, then any class scope is a scope level in
33596 an out-of-line member definition. In this case simply wind back
33597 beyond the first such scope to inject the template parameter list.
33598 Otherwise wind back to the class being defined. The latter can
33599 occur in class member friend declarations such as:
33601 class A {
33602 void foo (auto);
33604 class B {
33605 friend void A::foo (auto);
33608 The template parameter list synthesized for the friend declaration
33609 must be injected in the scope of 'B'. This can also occur in
33610 erroneous cases such as:
33612 struct A {
33613 struct B {
33614 void foo (auto);
33616 void B::foo (auto) {}
33619 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33620 but, nevertheless, the template parameter list synthesized for the
33621 declarator should be injected into the scope of 'A' as if the
33622 ill-formed template was specified explicitly. */
33624 while (scope->kind == sk_class && !scope->defining_class_p)
33626 parent_scope = scope;
33627 scope = scope->level_chain;
33631 current_binding_level = scope;
33633 if (scope->kind != sk_template_parms
33634 || !function_being_declared_is_template_p (parser))
33636 /* Introduce a new template parameter list for implicit template
33637 parameters. */
33639 become_template = true;
33641 parser->implicit_template_scope
33642 = begin_scope (sk_template_parms, NULL);
33644 ++processing_template_decl;
33646 parser->fully_implicit_function_template_p = true;
33647 ++parser->num_template_parameter_lists;
33649 else
33651 /* Synthesize implicit template parameters at the end of the explicit
33652 template parameter list. */
33654 gcc_assert (current_template_parms);
33656 parser->implicit_template_scope = scope;
33658 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33659 parser->implicit_template_parms
33660 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33664 /* Synthesize a new template parameter and track the current template
33665 parameter chain with implicit_template_parms. */
33667 tree synth_id = make_generic_type_name ();
33668 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33669 synth_id);
33670 tree new_parm
33671 = process_template_parm (parser->implicit_template_parms,
33672 input_location,
33673 build_tree_list (NULL_TREE, synth_tmpl_parm),
33674 /*non_type=*/false,
33675 /*param_pack=*/false);
33678 if (parser->implicit_template_parms)
33679 parser->implicit_template_parms
33680 = TREE_CHAIN (parser->implicit_template_parms);
33681 else
33682 parser->implicit_template_parms = new_parm;
33684 tree new_type = TREE_TYPE (getdecls ());
33686 /* If creating a fully implicit function template, start the new implicit
33687 template parameter list with this synthesized type, otherwise grow the
33688 current template parameter list. */
33690 if (become_template)
33692 parent_scope->level_chain = current_binding_level;
33694 tree new_parms = make_tree_vec (1);
33695 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33696 current_template_parms = tree_cons (size_int (processing_template_decl),
33697 new_parms, current_template_parms);
33699 else
33701 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33702 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33703 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33704 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33707 current_binding_level = entry_scope;
33709 return new_type;
33712 /* Finish the declaration of a fully implicit function template. Such a
33713 template has no explicit template parameter list so has not been through the
33714 normal template head and tail processing. synthesize_implicit_template_parm
33715 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33716 provided if the declaration is a class member such that its template
33717 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33718 form is returned. Otherwise NULL_TREE is returned. */
33720 tree
33721 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33723 gcc_assert (parser->fully_implicit_function_template_p);
33725 if (member_decl_opt && member_decl_opt != error_mark_node
33726 && DECL_VIRTUAL_P (member_decl_opt))
33728 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33729 "implicit templates may not be %<virtual%>");
33730 DECL_VIRTUAL_P (member_decl_opt) = false;
33733 if (member_decl_opt)
33734 member_decl_opt = finish_member_template_decl (member_decl_opt);
33735 end_template_decl ();
33737 parser->fully_implicit_function_template_p = false;
33738 --parser->num_template_parameter_lists;
33740 return member_decl_opt;
33743 #include "gt-cp-parser.h"