* config/i386/i386.h (TARGET_SUPPORTS_WIDE_INT): New define.
[official-gcc.git] / gcc / cp / parser.c
blobcfb512be8e745cff6c0b670d5f5edb5f64c36a52
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 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6486 /* This indicates that we have a normal array expression. */
6487 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6488 return NULL_TREE;
6491 /* Consume the ':'. */
6492 cp_lexer_consume_token (parser->lexer);
6493 length_index = cp_parser_expression (parser);
6494 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6496 cp_lexer_consume_token (parser->lexer);
6497 stride = cp_parser_expression (parser);
6500 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6502 if (*init_index == error_mark_node || length_index == error_mark_node
6503 || stride == error_mark_node || array_type == error_mark_node)
6505 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6506 cp_lexer_consume_token (parser->lexer);
6507 return error_mark_node;
6509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6511 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6512 length_index, stride, array_type);
6513 return value_tree;
6516 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6517 by cp_parser_builtin_offsetof. We're looking for
6519 postfix-expression [ expression ]
6520 postfix-expression [ braced-init-list ] (C++11)
6522 FOR_OFFSETOF is set if we're being called in that context, which
6523 changes how we deal with integer constant expressions. */
6525 static tree
6526 cp_parser_postfix_open_square_expression (cp_parser *parser,
6527 tree postfix_expression,
6528 bool for_offsetof,
6529 bool decltype_p)
6531 tree index = NULL_TREE;
6532 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6533 bool saved_greater_than_is_operator_p;
6535 /* Consume the `[' token. */
6536 cp_lexer_consume_token (parser->lexer);
6538 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6539 parser->greater_than_is_operator_p = true;
6541 /* Parse the index expression. */
6542 /* ??? For offsetof, there is a question of what to allow here. If
6543 offsetof is not being used in an integral constant expression context,
6544 then we *could* get the right answer by computing the value at runtime.
6545 If we are in an integral constant expression context, then we might
6546 could accept any constant expression; hard to say without analysis.
6547 Rather than open the barn door too wide right away, allow only integer
6548 constant expressions here. */
6549 if (for_offsetof)
6550 index = cp_parser_constant_expression (parser);
6551 else
6553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6555 bool expr_nonconst_p;
6556 cp_lexer_set_source_position (parser->lexer);
6557 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6558 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6559 if (flag_cilkplus
6560 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6562 error_at (cp_lexer_peek_token (parser->lexer)->location,
6563 "braced list index is not allowed with array "
6564 "notation");
6565 cp_parser_skip_to_end_of_statement (parser);
6566 return error_mark_node;
6569 else if (flag_cilkplus)
6571 /* Here are have these two options:
6572 ARRAY[EXP : EXP] - Array notation expr with default
6573 stride of 1.
6574 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6575 stride. */
6576 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6577 postfix_expression);
6578 if (an_exp)
6579 return an_exp;
6581 else
6582 index = cp_parser_expression (parser);
6585 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6587 /* Look for the closing `]'. */
6588 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6590 /* Build the ARRAY_REF. */
6591 postfix_expression = grok_array_decl (loc, postfix_expression,
6592 index, decltype_p);
6594 /* When not doing offsetof, array references are not permitted in
6595 constant-expressions. */
6596 if (!for_offsetof
6597 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6598 postfix_expression = error_mark_node;
6600 return postfix_expression;
6603 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6604 by cp_parser_builtin_offsetof. We're looking for
6606 postfix-expression . template [opt] id-expression
6607 postfix-expression . pseudo-destructor-name
6608 postfix-expression -> template [opt] id-expression
6609 postfix-expression -> pseudo-destructor-name
6611 FOR_OFFSETOF is set if we're being called in that context. That sorta
6612 limits what of the above we'll actually accept, but nevermind.
6613 TOKEN_TYPE is the "." or "->" token, which will already have been
6614 removed from the stream. */
6616 static tree
6617 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6618 enum cpp_ttype token_type,
6619 tree postfix_expression,
6620 bool for_offsetof, cp_id_kind *idk,
6621 location_t location)
6623 tree name;
6624 bool dependent_p;
6625 bool pseudo_destructor_p;
6626 tree scope = NULL_TREE;
6628 /* If this is a `->' operator, dereference the pointer. */
6629 if (token_type == CPP_DEREF)
6630 postfix_expression = build_x_arrow (location, postfix_expression,
6631 tf_warning_or_error);
6632 /* Check to see whether or not the expression is type-dependent. */
6633 dependent_p = type_dependent_expression_p (postfix_expression);
6634 /* The identifier following the `->' or `.' is not qualified. */
6635 parser->scope = NULL_TREE;
6636 parser->qualifying_scope = NULL_TREE;
6637 parser->object_scope = NULL_TREE;
6638 *idk = CP_ID_KIND_NONE;
6640 /* Enter the scope corresponding to the type of the object
6641 given by the POSTFIX_EXPRESSION. */
6642 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6644 scope = TREE_TYPE (postfix_expression);
6645 /* According to the standard, no expression should ever have
6646 reference type. Unfortunately, we do not currently match
6647 the standard in this respect in that our internal representation
6648 of an expression may have reference type even when the standard
6649 says it does not. Therefore, we have to manually obtain the
6650 underlying type here. */
6651 scope = non_reference (scope);
6652 /* The type of the POSTFIX_EXPRESSION must be complete. */
6653 if (scope == unknown_type_node)
6655 error_at (location, "%qE does not have class type",
6656 postfix_expression);
6657 scope = NULL_TREE;
6659 /* Unlike the object expression in other contexts, *this is not
6660 required to be of complete type for purposes of class member
6661 access (5.2.5) outside the member function body. */
6662 else if (postfix_expression != current_class_ref
6663 && !(processing_template_decl && scope == current_class_type))
6664 scope = complete_type_or_else (scope, NULL_TREE);
6665 /* Let the name lookup machinery know that we are processing a
6666 class member access expression. */
6667 parser->context->object_type = scope;
6668 /* If something went wrong, we want to be able to discern that case,
6669 as opposed to the case where there was no SCOPE due to the type
6670 of expression being dependent. */
6671 if (!scope)
6672 scope = error_mark_node;
6673 /* If the SCOPE was erroneous, make the various semantic analysis
6674 functions exit quickly -- and without issuing additional error
6675 messages. */
6676 if (scope == error_mark_node)
6677 postfix_expression = error_mark_node;
6680 /* Assume this expression is not a pseudo-destructor access. */
6681 pseudo_destructor_p = false;
6683 /* If the SCOPE is a scalar type, then, if this is a valid program,
6684 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6685 is type dependent, it can be pseudo-destructor-name or something else.
6686 Try to parse it as pseudo-destructor-name first. */
6687 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6689 tree s;
6690 tree type;
6692 cp_parser_parse_tentatively (parser);
6693 /* Parse the pseudo-destructor-name. */
6694 s = NULL_TREE;
6695 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6696 &s, &type);
6697 if (dependent_p
6698 && (cp_parser_error_occurred (parser)
6699 || !SCALAR_TYPE_P (type)))
6700 cp_parser_abort_tentative_parse (parser);
6701 else if (cp_parser_parse_definitely (parser))
6703 pseudo_destructor_p = true;
6704 postfix_expression
6705 = finish_pseudo_destructor_expr (postfix_expression,
6706 s, type, location);
6710 if (!pseudo_destructor_p)
6712 /* If the SCOPE is not a scalar type, we are looking at an
6713 ordinary class member access expression, rather than a
6714 pseudo-destructor-name. */
6715 bool template_p;
6716 cp_token *token = cp_lexer_peek_token (parser->lexer);
6717 /* Parse the id-expression. */
6718 name = (cp_parser_id_expression
6719 (parser,
6720 cp_parser_optional_template_keyword (parser),
6721 /*check_dependency_p=*/true,
6722 &template_p,
6723 /*declarator_p=*/false,
6724 /*optional_p=*/false));
6725 /* In general, build a SCOPE_REF if the member name is qualified.
6726 However, if the name was not dependent and has already been
6727 resolved; there is no need to build the SCOPE_REF. For example;
6729 struct X { void f(); };
6730 template <typename T> void f(T* t) { t->X::f(); }
6732 Even though "t" is dependent, "X::f" is not and has been resolved
6733 to a BASELINK; there is no need to include scope information. */
6735 /* But we do need to remember that there was an explicit scope for
6736 virtual function calls. */
6737 if (parser->scope)
6738 *idk = CP_ID_KIND_QUALIFIED;
6740 /* If the name is a template-id that names a type, we will get a
6741 TYPE_DECL here. That is invalid code. */
6742 if (TREE_CODE (name) == TYPE_DECL)
6744 error_at (token->location, "invalid use of %qD", name);
6745 postfix_expression = error_mark_node;
6747 else
6749 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6751 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6753 error_at (token->location, "%<%D::%D%> is not a class member",
6754 parser->scope, name);
6755 postfix_expression = error_mark_node;
6757 else
6758 name = build_qualified_name (/*type=*/NULL_TREE,
6759 parser->scope,
6760 name,
6761 template_p);
6762 parser->scope = NULL_TREE;
6763 parser->qualifying_scope = NULL_TREE;
6764 parser->object_scope = NULL_TREE;
6766 if (parser->scope && name && BASELINK_P (name))
6767 adjust_result_of_qualified_name_lookup
6768 (name, parser->scope, scope);
6769 postfix_expression
6770 = finish_class_member_access_expr (postfix_expression, name,
6771 template_p,
6772 tf_warning_or_error);
6776 /* We no longer need to look up names in the scope of the object on
6777 the left-hand side of the `.' or `->' operator. */
6778 parser->context->object_type = NULL_TREE;
6780 /* Outside of offsetof, these operators may not appear in
6781 constant-expressions. */
6782 if (!for_offsetof
6783 && (cp_parser_non_integral_constant_expression
6784 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6785 postfix_expression = error_mark_node;
6787 return postfix_expression;
6790 /* Cache of LITERAL_ZERO_P constants. */
6792 static GTY(()) tree literal_zeros[itk_none];
6794 /* Parse a parenthesized expression-list.
6796 expression-list:
6797 assignment-expression
6798 expression-list, assignment-expression
6800 attribute-list:
6801 expression-list
6802 identifier
6803 identifier, expression-list
6805 CAST_P is true if this expression is the target of a cast.
6807 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6808 argument pack.
6810 Returns a vector of trees. Each element is a representation of an
6811 assignment-expression. NULL is returned if the ( and or ) are
6812 missing. An empty, but allocated, vector is returned on no
6813 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6814 if we are parsing an attribute list for an attribute that wants a
6815 plain identifier argument, normal_attr for an attribute that wants
6816 an expression, or non_attr if we aren't parsing an attribute list. If
6817 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6818 not all of the expressions in the list were constant.
6819 WANT_LITERAL_ZERO_P is true if the caller is interested in
6820 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6821 immediately, this can be removed. */
6823 static vec<tree, va_gc> *
6824 cp_parser_parenthesized_expression_list (cp_parser* parser,
6825 int is_attribute_list,
6826 bool cast_p,
6827 bool allow_expansion_p,
6828 bool *non_constant_p,
6829 bool want_literal_zero_p)
6831 vec<tree, va_gc> *expression_list;
6832 bool fold_expr_p = is_attribute_list != non_attr;
6833 tree identifier = NULL_TREE;
6834 bool saved_greater_than_is_operator_p;
6836 /* Assume all the expressions will be constant. */
6837 if (non_constant_p)
6838 *non_constant_p = false;
6840 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6841 return NULL;
6843 expression_list = make_tree_vector ();
6845 /* Within a parenthesized expression, a `>' token is always
6846 the greater-than operator. */
6847 saved_greater_than_is_operator_p
6848 = parser->greater_than_is_operator_p;
6849 parser->greater_than_is_operator_p = true;
6851 /* Consume expressions until there are no more. */
6852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6853 while (true)
6855 tree expr;
6857 /* At the beginning of attribute lists, check to see if the
6858 next token is an identifier. */
6859 if (is_attribute_list == id_attr
6860 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6862 cp_token *token;
6864 /* Consume the identifier. */
6865 token = cp_lexer_consume_token (parser->lexer);
6866 /* Save the identifier. */
6867 identifier = token->u.value;
6869 else
6871 bool expr_non_constant_p;
6873 /* Parse the next assignment-expression. */
6874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6876 /* A braced-init-list. */
6877 cp_lexer_set_source_position (parser->lexer);
6878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6879 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6880 if (non_constant_p && expr_non_constant_p)
6881 *non_constant_p = true;
6883 else if (non_constant_p)
6885 expr = (cp_parser_constant_expression
6886 (parser, /*allow_non_constant_p=*/true,
6887 &expr_non_constant_p));
6888 if (expr_non_constant_p)
6889 *non_constant_p = true;
6891 else
6893 expr = NULL_TREE;
6894 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6895 switch (tok->type)
6897 case CPP_NUMBER:
6898 case CPP_CHAR:
6899 case CPP_WCHAR:
6900 case CPP_CHAR16:
6901 case CPP_CHAR32:
6902 /* If a parameter is literal zero alone, remember it
6903 for -Wmemset-transposed-args warning. */
6904 if (integer_zerop (tok->u.value)
6905 && !TREE_OVERFLOW (tok->u.value)
6906 && want_literal_zero_p
6907 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6908 == CPP_COMMA
6909 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6910 == CPP_CLOSE_PAREN))
6912 unsigned int i;
6913 for (i = 0; i < itk_none; ++i)
6914 if (TREE_TYPE (tok->u.value) == integer_types[i])
6915 break;
6916 if (i < itk_none && literal_zeros[i])
6917 expr = literal_zeros[i];
6918 else
6920 expr = copy_node (tok->u.value);
6921 LITERAL_ZERO_P (expr) = 1;
6922 if (i < itk_none)
6923 literal_zeros[i] = expr;
6925 /* Consume the 0 token (or '\0', 0LL etc.). */
6926 cp_lexer_consume_token (parser->lexer);
6928 break;
6929 default:
6930 break;
6932 if (expr == NULL_TREE)
6933 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6934 cast_p);
6937 if (fold_expr_p)
6938 expr = instantiate_non_dependent_expr (expr);
6940 /* If we have an ellipsis, then this is an expression
6941 expansion. */
6942 if (allow_expansion_p
6943 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6945 /* Consume the `...'. */
6946 cp_lexer_consume_token (parser->lexer);
6948 /* Build the argument pack. */
6949 expr = make_pack_expansion (expr);
6952 /* Add it to the list. We add error_mark_node
6953 expressions to the list, so that we can still tell if
6954 the correct form for a parenthesized expression-list
6955 is found. That gives better errors. */
6956 vec_safe_push (expression_list, expr);
6958 if (expr == error_mark_node)
6959 goto skip_comma;
6962 /* After the first item, attribute lists look the same as
6963 expression lists. */
6964 is_attribute_list = non_attr;
6966 get_comma:;
6967 /* If the next token isn't a `,', then we are done. */
6968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6969 break;
6971 /* Otherwise, consume the `,' and keep going. */
6972 cp_lexer_consume_token (parser->lexer);
6975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6977 int ending;
6979 skip_comma:;
6980 /* We try and resync to an unnested comma, as that will give the
6981 user better diagnostics. */
6982 ending = cp_parser_skip_to_closing_parenthesis (parser,
6983 /*recovering=*/true,
6984 /*or_comma=*/true,
6985 /*consume_paren=*/true);
6986 if (ending < 0)
6987 goto get_comma;
6988 if (!ending)
6990 parser->greater_than_is_operator_p
6991 = saved_greater_than_is_operator_p;
6992 return NULL;
6996 parser->greater_than_is_operator_p
6997 = saved_greater_than_is_operator_p;
6999 if (identifier)
7000 vec_safe_insert (expression_list, 0, identifier);
7002 return expression_list;
7005 /* Parse a pseudo-destructor-name.
7007 pseudo-destructor-name:
7008 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7009 :: [opt] nested-name-specifier template template-id :: ~ type-name
7010 :: [opt] nested-name-specifier [opt] ~ type-name
7012 If either of the first two productions is used, sets *SCOPE to the
7013 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7014 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7015 or ERROR_MARK_NODE if the parse fails. */
7017 static void
7018 cp_parser_pseudo_destructor_name (cp_parser* parser,
7019 tree object,
7020 tree* scope,
7021 tree* type)
7023 bool nested_name_specifier_p;
7025 /* Handle ~auto. */
7026 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7027 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7028 && !type_dependent_expression_p (object))
7030 if (cxx_dialect < cxx14)
7031 pedwarn (input_location, 0,
7032 "%<~auto%> only available with "
7033 "-std=c++14 or -std=gnu++14");
7034 cp_lexer_consume_token (parser->lexer);
7035 cp_lexer_consume_token (parser->lexer);
7036 *scope = NULL_TREE;
7037 *type = TREE_TYPE (object);
7038 return;
7041 /* Assume that things will not work out. */
7042 *type = error_mark_node;
7044 /* Look for the optional `::' operator. */
7045 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7046 /* Look for the optional nested-name-specifier. */
7047 nested_name_specifier_p
7048 = (cp_parser_nested_name_specifier_opt (parser,
7049 /*typename_keyword_p=*/false,
7050 /*check_dependency_p=*/true,
7051 /*type_p=*/false,
7052 /*is_declaration=*/false)
7053 != NULL_TREE);
7054 /* Now, if we saw a nested-name-specifier, we might be doing the
7055 second production. */
7056 if (nested_name_specifier_p
7057 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7059 /* Consume the `template' keyword. */
7060 cp_lexer_consume_token (parser->lexer);
7061 /* Parse the template-id. */
7062 cp_parser_template_id (parser,
7063 /*template_keyword_p=*/true,
7064 /*check_dependency_p=*/false,
7065 class_type,
7066 /*is_declaration=*/true);
7067 /* Look for the `::' token. */
7068 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7070 /* If the next token is not a `~', then there might be some
7071 additional qualification. */
7072 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7074 /* At this point, we're looking for "type-name :: ~". The type-name
7075 must not be a class-name, since this is a pseudo-destructor. So,
7076 it must be either an enum-name, or a typedef-name -- both of which
7077 are just identifiers. So, we peek ahead to check that the "::"
7078 and "~" tokens are present; if they are not, then we can avoid
7079 calling type_name. */
7080 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7081 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7082 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7084 cp_parser_error (parser, "non-scalar type");
7085 return;
7088 /* Look for the type-name. */
7089 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7090 if (*scope == error_mark_node)
7091 return;
7093 /* Look for the `::' token. */
7094 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7096 else
7097 *scope = NULL_TREE;
7099 /* Look for the `~'. */
7100 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7102 /* Once we see the ~, this has to be a pseudo-destructor. */
7103 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7104 cp_parser_commit_to_topmost_tentative_parse (parser);
7106 /* Look for the type-name again. We are not responsible for
7107 checking that it matches the first type-name. */
7108 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7111 /* Parse a unary-expression.
7113 unary-expression:
7114 postfix-expression
7115 ++ cast-expression
7116 -- cast-expression
7117 unary-operator cast-expression
7118 sizeof unary-expression
7119 sizeof ( type-id )
7120 alignof ( type-id ) [C++0x]
7121 new-expression
7122 delete-expression
7124 GNU Extensions:
7126 unary-expression:
7127 __extension__ cast-expression
7128 __alignof__ unary-expression
7129 __alignof__ ( type-id )
7130 alignof unary-expression [C++0x]
7131 __real__ cast-expression
7132 __imag__ cast-expression
7133 && identifier
7134 sizeof ( type-id ) { initializer-list , [opt] }
7135 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7136 __alignof__ ( type-id ) { initializer-list , [opt] }
7138 ADDRESS_P is true iff the unary-expression is appearing as the
7139 operand of the `&' operator. CAST_P is true if this expression is
7140 the target of a cast.
7142 Returns a representation of the expression. */
7144 static tree
7145 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7146 bool address_p, bool cast_p, bool decltype_p)
7148 cp_token *token;
7149 enum tree_code unary_operator;
7151 /* Peek at the next token. */
7152 token = cp_lexer_peek_token (parser->lexer);
7153 /* Some keywords give away the kind of expression. */
7154 if (token->type == CPP_KEYWORD)
7156 enum rid keyword = token->keyword;
7158 switch (keyword)
7160 case RID_ALIGNOF:
7161 case RID_SIZEOF:
7163 tree operand, ret;
7164 enum tree_code op;
7165 location_t first_loc;
7167 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7168 /* Consume the token. */
7169 cp_lexer_consume_token (parser->lexer);
7170 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7171 /* Parse the operand. */
7172 operand = cp_parser_sizeof_operand (parser, keyword);
7174 if (TYPE_P (operand))
7175 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7176 else
7178 /* ISO C++ defines alignof only with types, not with
7179 expressions. So pedwarn if alignof is used with a non-
7180 type expression. However, __alignof__ is ok. */
7181 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7182 pedwarn (token->location, OPT_Wpedantic,
7183 "ISO C++ does not allow %<alignof%> "
7184 "with a non-type");
7186 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7188 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7189 SIZEOF_EXPR with the original operand. */
7190 if (op == SIZEOF_EXPR && ret != error_mark_node)
7192 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7194 if (!processing_template_decl && TYPE_P (operand))
7196 ret = build_min (SIZEOF_EXPR, size_type_node,
7197 build1 (NOP_EXPR, operand,
7198 error_mark_node));
7199 SIZEOF_EXPR_TYPE_P (ret) = 1;
7201 else
7202 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7203 TREE_SIDE_EFFECTS (ret) = 0;
7204 TREE_READONLY (ret) = 1;
7206 SET_EXPR_LOCATION (ret, first_loc);
7208 return ret;
7211 case RID_NEW:
7212 return cp_parser_new_expression (parser);
7214 case RID_DELETE:
7215 return cp_parser_delete_expression (parser);
7217 case RID_EXTENSION:
7219 /* The saved value of the PEDANTIC flag. */
7220 int saved_pedantic;
7221 tree expr;
7223 /* Save away the PEDANTIC flag. */
7224 cp_parser_extension_opt (parser, &saved_pedantic);
7225 /* Parse the cast-expression. */
7226 expr = cp_parser_simple_cast_expression (parser);
7227 /* Restore the PEDANTIC flag. */
7228 pedantic = saved_pedantic;
7230 return expr;
7233 case RID_REALPART:
7234 case RID_IMAGPART:
7236 tree expression;
7238 /* Consume the `__real__' or `__imag__' token. */
7239 cp_lexer_consume_token (parser->lexer);
7240 /* Parse the cast-expression. */
7241 expression = cp_parser_simple_cast_expression (parser);
7242 /* Create the complete representation. */
7243 return build_x_unary_op (token->location,
7244 (keyword == RID_REALPART
7245 ? REALPART_EXPR : IMAGPART_EXPR),
7246 expression,
7247 tf_warning_or_error);
7249 break;
7251 case RID_TRANSACTION_ATOMIC:
7252 case RID_TRANSACTION_RELAXED:
7253 return cp_parser_transaction_expression (parser, keyword);
7255 case RID_NOEXCEPT:
7257 tree expr;
7258 const char *saved_message;
7259 bool saved_integral_constant_expression_p;
7260 bool saved_non_integral_constant_expression_p;
7261 bool saved_greater_than_is_operator_p;
7263 cp_lexer_consume_token (parser->lexer);
7264 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7266 saved_message = parser->type_definition_forbidden_message;
7267 parser->type_definition_forbidden_message
7268 = G_("types may not be defined in %<noexcept%> expressions");
7270 saved_integral_constant_expression_p
7271 = parser->integral_constant_expression_p;
7272 saved_non_integral_constant_expression_p
7273 = parser->non_integral_constant_expression_p;
7274 parser->integral_constant_expression_p = false;
7276 saved_greater_than_is_operator_p
7277 = parser->greater_than_is_operator_p;
7278 parser->greater_than_is_operator_p = true;
7280 ++cp_unevaluated_operand;
7281 ++c_inhibit_evaluation_warnings;
7282 ++cp_noexcept_operand;
7283 expr = cp_parser_expression (parser);
7284 --cp_noexcept_operand;
7285 --c_inhibit_evaluation_warnings;
7286 --cp_unevaluated_operand;
7288 parser->greater_than_is_operator_p
7289 = saved_greater_than_is_operator_p;
7291 parser->integral_constant_expression_p
7292 = saved_integral_constant_expression_p;
7293 parser->non_integral_constant_expression_p
7294 = saved_non_integral_constant_expression_p;
7296 parser->type_definition_forbidden_message = saved_message;
7298 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7299 return finish_noexcept_expr (expr, tf_warning_or_error);
7302 default:
7303 break;
7307 /* Look for the `:: new' and `:: delete', which also signal the
7308 beginning of a new-expression, or delete-expression,
7309 respectively. If the next token is `::', then it might be one of
7310 these. */
7311 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7313 enum rid keyword;
7315 /* See if the token after the `::' is one of the keywords in
7316 which we're interested. */
7317 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7318 /* If it's `new', we have a new-expression. */
7319 if (keyword == RID_NEW)
7320 return cp_parser_new_expression (parser);
7321 /* Similarly, for `delete'. */
7322 else if (keyword == RID_DELETE)
7323 return cp_parser_delete_expression (parser);
7326 /* Look for a unary operator. */
7327 unary_operator = cp_parser_unary_operator (token);
7328 /* The `++' and `--' operators can be handled similarly, even though
7329 they are not technically unary-operators in the grammar. */
7330 if (unary_operator == ERROR_MARK)
7332 if (token->type == CPP_PLUS_PLUS)
7333 unary_operator = PREINCREMENT_EXPR;
7334 else if (token->type == CPP_MINUS_MINUS)
7335 unary_operator = PREDECREMENT_EXPR;
7336 /* Handle the GNU address-of-label extension. */
7337 else if (cp_parser_allow_gnu_extensions_p (parser)
7338 && token->type == CPP_AND_AND)
7340 tree identifier;
7341 tree expression;
7342 location_t loc = token->location;
7344 /* Consume the '&&' token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 /* Look for the identifier. */
7347 identifier = cp_parser_identifier (parser);
7348 /* Create an expression representing the address. */
7349 expression = finish_label_address_expr (identifier, loc);
7350 if (cp_parser_non_integral_constant_expression (parser,
7351 NIC_ADDR_LABEL))
7352 expression = error_mark_node;
7353 return expression;
7356 if (unary_operator != ERROR_MARK)
7358 tree cast_expression;
7359 tree expression = error_mark_node;
7360 non_integral_constant non_constant_p = NIC_NONE;
7361 location_t loc = token->location;
7362 tsubst_flags_t complain = complain_flags (decltype_p);
7364 /* Consume the operator token. */
7365 token = cp_lexer_consume_token (parser->lexer);
7366 /* Parse the cast-expression. */
7367 cast_expression
7368 = cp_parser_cast_expression (parser,
7369 unary_operator == ADDR_EXPR,
7370 /*cast_p=*/false,
7371 /*decltype*/false,
7372 pidk);
7373 /* Now, build an appropriate representation. */
7374 switch (unary_operator)
7376 case INDIRECT_REF:
7377 non_constant_p = NIC_STAR;
7378 expression = build_x_indirect_ref (loc, cast_expression,
7379 RO_UNARY_STAR,
7380 complain);
7381 break;
7383 case ADDR_EXPR:
7384 non_constant_p = NIC_ADDR;
7385 /* Fall through. */
7386 case BIT_NOT_EXPR:
7387 expression = build_x_unary_op (loc, unary_operator,
7388 cast_expression,
7389 complain);
7390 break;
7392 case PREINCREMENT_EXPR:
7393 case PREDECREMENT_EXPR:
7394 non_constant_p = unary_operator == PREINCREMENT_EXPR
7395 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7396 /* Fall through. */
7397 case UNARY_PLUS_EXPR:
7398 case NEGATE_EXPR:
7399 case TRUTH_NOT_EXPR:
7400 expression = finish_unary_op_expr (loc, unary_operator,
7401 cast_expression, complain);
7402 break;
7404 default:
7405 gcc_unreachable ();
7408 if (non_constant_p != NIC_NONE
7409 && cp_parser_non_integral_constant_expression (parser,
7410 non_constant_p))
7411 expression = error_mark_node;
7413 return expression;
7416 return cp_parser_postfix_expression (parser, address_p, cast_p,
7417 /*member_access_only_p=*/false,
7418 decltype_p,
7419 pidk);
7422 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7423 unary-operator, the corresponding tree code is returned. */
7425 static enum tree_code
7426 cp_parser_unary_operator (cp_token* token)
7428 switch (token->type)
7430 case CPP_MULT:
7431 return INDIRECT_REF;
7433 case CPP_AND:
7434 return ADDR_EXPR;
7436 case CPP_PLUS:
7437 return UNARY_PLUS_EXPR;
7439 case CPP_MINUS:
7440 return NEGATE_EXPR;
7442 case CPP_NOT:
7443 return TRUTH_NOT_EXPR;
7445 case CPP_COMPL:
7446 return BIT_NOT_EXPR;
7448 default:
7449 return ERROR_MARK;
7453 /* Parse a new-expression.
7455 new-expression:
7456 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7457 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7459 Returns a representation of the expression. */
7461 static tree
7462 cp_parser_new_expression (cp_parser* parser)
7464 bool global_scope_p;
7465 vec<tree, va_gc> *placement;
7466 tree type;
7467 vec<tree, va_gc> *initializer;
7468 tree nelts = NULL_TREE;
7469 tree ret;
7471 /* Look for the optional `::' operator. */
7472 global_scope_p
7473 = (cp_parser_global_scope_opt (parser,
7474 /*current_scope_valid_p=*/false)
7475 != NULL_TREE);
7476 /* Look for the `new' operator. */
7477 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7478 /* There's no easy way to tell a new-placement from the
7479 `( type-id )' construct. */
7480 cp_parser_parse_tentatively (parser);
7481 /* Look for a new-placement. */
7482 placement = cp_parser_new_placement (parser);
7483 /* If that didn't work out, there's no new-placement. */
7484 if (!cp_parser_parse_definitely (parser))
7486 if (placement != NULL)
7487 release_tree_vector (placement);
7488 placement = NULL;
7491 /* If the next token is a `(', then we have a parenthesized
7492 type-id. */
7493 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7495 cp_token *token;
7496 const char *saved_message = parser->type_definition_forbidden_message;
7498 /* Consume the `('. */
7499 cp_lexer_consume_token (parser->lexer);
7501 /* Parse the type-id. */
7502 parser->type_definition_forbidden_message
7503 = G_("types may not be defined in a new-expression");
7504 type = cp_parser_type_id (parser);
7505 parser->type_definition_forbidden_message = saved_message;
7507 /* Look for the closing `)'. */
7508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7509 token = cp_lexer_peek_token (parser->lexer);
7510 /* There should not be a direct-new-declarator in this production,
7511 but GCC used to allowed this, so we check and emit a sensible error
7512 message for this case. */
7513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7515 error_at (token->location,
7516 "array bound forbidden after parenthesized type-id");
7517 inform (token->location,
7518 "try removing the parentheses around the type-id");
7519 cp_parser_direct_new_declarator (parser);
7522 /* Otherwise, there must be a new-type-id. */
7523 else
7524 type = cp_parser_new_type_id (parser, &nelts);
7526 /* If the next token is a `(' or '{', then we have a new-initializer. */
7527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7528 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7529 initializer = cp_parser_new_initializer (parser);
7530 else
7531 initializer = NULL;
7533 /* A new-expression may not appear in an integral constant
7534 expression. */
7535 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7536 ret = error_mark_node;
7537 else
7539 /* Create a representation of the new-expression. */
7540 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7541 tf_warning_or_error);
7544 if (placement != NULL)
7545 release_tree_vector (placement);
7546 if (initializer != NULL)
7547 release_tree_vector (initializer);
7549 return ret;
7552 /* Parse a new-placement.
7554 new-placement:
7555 ( expression-list )
7557 Returns the same representation as for an expression-list. */
7559 static vec<tree, va_gc> *
7560 cp_parser_new_placement (cp_parser* parser)
7562 vec<tree, va_gc> *expression_list;
7564 /* Parse the expression-list. */
7565 expression_list = (cp_parser_parenthesized_expression_list
7566 (parser, non_attr, /*cast_p=*/false,
7567 /*allow_expansion_p=*/true,
7568 /*non_constant_p=*/NULL));
7570 return expression_list;
7573 /* Parse a new-type-id.
7575 new-type-id:
7576 type-specifier-seq new-declarator [opt]
7578 Returns the TYPE allocated. If the new-type-id indicates an array
7579 type, *NELTS is set to the number of elements in the last array
7580 bound; the TYPE will not include the last array bound. */
7582 static tree
7583 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7585 cp_decl_specifier_seq type_specifier_seq;
7586 cp_declarator *new_declarator;
7587 cp_declarator *declarator;
7588 cp_declarator *outer_declarator;
7589 const char *saved_message;
7591 /* The type-specifier sequence must not contain type definitions.
7592 (It cannot contain declarations of new types either, but if they
7593 are not definitions we will catch that because they are not
7594 complete.) */
7595 saved_message = parser->type_definition_forbidden_message;
7596 parser->type_definition_forbidden_message
7597 = G_("types may not be defined in a new-type-id");
7598 /* Parse the type-specifier-seq. */
7599 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7600 /*is_trailing_return=*/false,
7601 &type_specifier_seq);
7602 /* Restore the old message. */
7603 parser->type_definition_forbidden_message = saved_message;
7605 if (type_specifier_seq.type == error_mark_node)
7606 return error_mark_node;
7608 /* Parse the new-declarator. */
7609 new_declarator = cp_parser_new_declarator_opt (parser);
7611 /* Determine the number of elements in the last array dimension, if
7612 any. */
7613 *nelts = NULL_TREE;
7614 /* Skip down to the last array dimension. */
7615 declarator = new_declarator;
7616 outer_declarator = NULL;
7617 while (declarator && (declarator->kind == cdk_pointer
7618 || declarator->kind == cdk_ptrmem))
7620 outer_declarator = declarator;
7621 declarator = declarator->declarator;
7623 while (declarator
7624 && declarator->kind == cdk_array
7625 && declarator->declarator
7626 && declarator->declarator->kind == cdk_array)
7628 outer_declarator = declarator;
7629 declarator = declarator->declarator;
7632 if (declarator && declarator->kind == cdk_array)
7634 *nelts = declarator->u.array.bounds;
7635 if (*nelts == error_mark_node)
7636 *nelts = integer_one_node;
7638 if (outer_declarator)
7639 outer_declarator->declarator = declarator->declarator;
7640 else
7641 new_declarator = NULL;
7644 return groktypename (&type_specifier_seq, new_declarator, false);
7647 /* Parse an (optional) new-declarator.
7649 new-declarator:
7650 ptr-operator new-declarator [opt]
7651 direct-new-declarator
7653 Returns the declarator. */
7655 static cp_declarator *
7656 cp_parser_new_declarator_opt (cp_parser* parser)
7658 enum tree_code code;
7659 tree type, std_attributes = NULL_TREE;
7660 cp_cv_quals cv_quals;
7662 /* We don't know if there's a ptr-operator next, or not. */
7663 cp_parser_parse_tentatively (parser);
7664 /* Look for a ptr-operator. */
7665 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7666 /* If that worked, look for more new-declarators. */
7667 if (cp_parser_parse_definitely (parser))
7669 cp_declarator *declarator;
7671 /* Parse another optional declarator. */
7672 declarator = cp_parser_new_declarator_opt (parser);
7674 declarator = cp_parser_make_indirect_declarator
7675 (code, type, cv_quals, declarator, std_attributes);
7677 return declarator;
7680 /* If the next token is a `[', there is a direct-new-declarator. */
7681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7682 return cp_parser_direct_new_declarator (parser);
7684 return NULL;
7687 /* Parse a direct-new-declarator.
7689 direct-new-declarator:
7690 [ expression ]
7691 direct-new-declarator [constant-expression]
7695 static cp_declarator *
7696 cp_parser_direct_new_declarator (cp_parser* parser)
7698 cp_declarator *declarator = NULL;
7700 while (true)
7702 tree expression;
7703 cp_token *token;
7705 /* Look for the opening `['. */
7706 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7708 token = cp_lexer_peek_token (parser->lexer);
7709 expression = cp_parser_expression (parser);
7710 /* The standard requires that the expression have integral
7711 type. DR 74 adds enumeration types. We believe that the
7712 real intent is that these expressions be handled like the
7713 expression in a `switch' condition, which also allows
7714 classes with a single conversion to integral or
7715 enumeration type. */
7716 if (!processing_template_decl)
7718 expression
7719 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7720 expression,
7721 /*complain=*/true);
7722 if (!expression)
7724 error_at (token->location,
7725 "expression in new-declarator must have integral "
7726 "or enumeration type");
7727 expression = error_mark_node;
7731 /* Look for the closing `]'. */
7732 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7734 /* Add this bound to the declarator. */
7735 declarator = make_array_declarator (declarator, expression);
7737 /* If the next token is not a `[', then there are no more
7738 bounds. */
7739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7740 break;
7743 return declarator;
7746 /* Parse a new-initializer.
7748 new-initializer:
7749 ( expression-list [opt] )
7750 braced-init-list
7752 Returns a representation of the expression-list. */
7754 static vec<tree, va_gc> *
7755 cp_parser_new_initializer (cp_parser* parser)
7757 vec<tree, va_gc> *expression_list;
7759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7761 tree t;
7762 bool expr_non_constant_p;
7763 cp_lexer_set_source_position (parser->lexer);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7765 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7766 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7767 expression_list = make_tree_vector_single (t);
7769 else
7770 expression_list = (cp_parser_parenthesized_expression_list
7771 (parser, non_attr, /*cast_p=*/false,
7772 /*allow_expansion_p=*/true,
7773 /*non_constant_p=*/NULL));
7775 return expression_list;
7778 /* Parse a delete-expression.
7780 delete-expression:
7781 :: [opt] delete cast-expression
7782 :: [opt] delete [ ] cast-expression
7784 Returns a representation of the expression. */
7786 static tree
7787 cp_parser_delete_expression (cp_parser* parser)
7789 bool global_scope_p;
7790 bool array_p;
7791 tree expression;
7793 /* Look for the optional `::' operator. */
7794 global_scope_p
7795 = (cp_parser_global_scope_opt (parser,
7796 /*current_scope_valid_p=*/false)
7797 != NULL_TREE);
7798 /* Look for the `delete' keyword. */
7799 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7800 /* See if the array syntax is in use. */
7801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7803 /* Consume the `[' token. */
7804 cp_lexer_consume_token (parser->lexer);
7805 /* Look for the `]' token. */
7806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7807 /* Remember that this is the `[]' construct. */
7808 array_p = true;
7810 else
7811 array_p = false;
7813 /* Parse the cast-expression. */
7814 expression = cp_parser_simple_cast_expression (parser);
7816 /* A delete-expression may not appear in an integral constant
7817 expression. */
7818 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7819 return error_mark_node;
7821 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7822 tf_warning_or_error);
7825 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7826 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7827 0 otherwise. */
7829 static int
7830 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7832 cp_token *token = cp_lexer_peek_token (parser->lexer);
7833 switch (token->type)
7835 case CPP_COMMA:
7836 case CPP_SEMICOLON:
7837 case CPP_QUERY:
7838 case CPP_COLON:
7839 case CPP_CLOSE_SQUARE:
7840 case CPP_CLOSE_PAREN:
7841 case CPP_CLOSE_BRACE:
7842 case CPP_OPEN_BRACE:
7843 case CPP_DOT:
7844 case CPP_DOT_STAR:
7845 case CPP_DEREF:
7846 case CPP_DEREF_STAR:
7847 case CPP_DIV:
7848 case CPP_MOD:
7849 case CPP_LSHIFT:
7850 case CPP_RSHIFT:
7851 case CPP_LESS:
7852 case CPP_GREATER:
7853 case CPP_LESS_EQ:
7854 case CPP_GREATER_EQ:
7855 case CPP_EQ_EQ:
7856 case CPP_NOT_EQ:
7857 case CPP_EQ:
7858 case CPP_MULT_EQ:
7859 case CPP_DIV_EQ:
7860 case CPP_MOD_EQ:
7861 case CPP_PLUS_EQ:
7862 case CPP_MINUS_EQ:
7863 case CPP_RSHIFT_EQ:
7864 case CPP_LSHIFT_EQ:
7865 case CPP_AND_EQ:
7866 case CPP_XOR_EQ:
7867 case CPP_OR_EQ:
7868 case CPP_XOR:
7869 case CPP_OR:
7870 case CPP_OR_OR:
7871 case CPP_EOF:
7872 case CPP_ELLIPSIS:
7873 return 0;
7875 case CPP_OPEN_PAREN:
7876 /* In ((type ()) () the last () isn't a valid cast-expression,
7877 so the whole must be parsed as postfix-expression. */
7878 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7879 != CPP_CLOSE_PAREN;
7881 case CPP_OPEN_SQUARE:
7882 /* '[' may start a primary-expression in obj-c++ and in C++11,
7883 as a lambda-expression, eg, '(void)[]{}'. */
7884 if (cxx_dialect >= cxx11)
7885 return -1;
7886 return c_dialect_objc ();
7888 case CPP_PLUS_PLUS:
7889 case CPP_MINUS_MINUS:
7890 /* '++' and '--' may or may not start a cast-expression:
7892 struct T { void operator++(int); };
7893 void f() { (T())++; }
7897 int a;
7898 (int)++a; */
7899 return -1;
7901 default:
7902 return 1;
7906 /* Parse a cast-expression.
7908 cast-expression:
7909 unary-expression
7910 ( type-id ) cast-expression
7912 ADDRESS_P is true iff the unary-expression is appearing as the
7913 operand of the `&' operator. CAST_P is true if this expression is
7914 the target of a cast.
7916 Returns a representation of the expression. */
7918 static tree
7919 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7920 bool decltype_p, cp_id_kind * pidk)
7922 /* If it's a `(', then we might be looking at a cast. */
7923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7925 tree type = NULL_TREE;
7926 tree expr = NULL_TREE;
7927 int cast_expression = 0;
7928 const char *saved_message;
7930 /* There's no way to know yet whether or not this is a cast.
7931 For example, `(int (3))' is a unary-expression, while `(int)
7932 3' is a cast. So, we resort to parsing tentatively. */
7933 cp_parser_parse_tentatively (parser);
7934 /* Types may not be defined in a cast. */
7935 saved_message = parser->type_definition_forbidden_message;
7936 parser->type_definition_forbidden_message
7937 = G_("types may not be defined in casts");
7938 /* Consume the `('. */
7939 cp_lexer_consume_token (parser->lexer);
7940 /* A very tricky bit is that `(struct S) { 3 }' is a
7941 compound-literal (which we permit in C++ as an extension).
7942 But, that construct is not a cast-expression -- it is a
7943 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7944 is legal; if the compound-literal were a cast-expression,
7945 you'd need an extra set of parentheses.) But, if we parse
7946 the type-id, and it happens to be a class-specifier, then we
7947 will commit to the parse at that point, because we cannot
7948 undo the action that is done when creating a new class. So,
7949 then we cannot back up and do a postfix-expression.
7951 Another tricky case is the following (c++/29234):
7953 struct S { void operator () (); };
7955 void foo ()
7957 ( S()() );
7960 As a type-id we parse the parenthesized S()() as a function
7961 returning a function, groktypename complains and we cannot
7962 back up in this case either.
7964 Therefore, we scan ahead to the closing `)', and check to see
7965 if the tokens after the `)' can start a cast-expression. Otherwise
7966 we are dealing with an unary-expression, a postfix-expression
7967 or something else.
7969 Yet another tricky case, in C++11, is the following (c++/54891):
7971 (void)[]{};
7973 The issue is that usually, besides the case of lambda-expressions,
7974 the parenthesized type-id cannot be followed by '[', and, eg, we
7975 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7976 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7977 we don't commit, we try a cast-expression, then an unary-expression.
7979 Save tokens so that we can put them back. */
7980 cp_lexer_save_tokens (parser->lexer);
7982 /* We may be looking at a cast-expression. */
7983 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7984 /*consume_paren=*/true))
7985 cast_expression
7986 = cp_parser_tokens_start_cast_expression (parser);
7988 /* Roll back the tokens we skipped. */
7989 cp_lexer_rollback_tokens (parser->lexer);
7990 /* If we aren't looking at a cast-expression, simulate an error so
7991 that the call to cp_parser_error_occurred below returns true. */
7992 if (!cast_expression)
7993 cp_parser_simulate_error (parser);
7994 else
7996 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7997 parser->in_type_id_in_expr_p = true;
7998 /* Look for the type-id. */
7999 type = cp_parser_type_id (parser);
8000 /* Look for the closing `)'. */
8001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8002 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8005 /* Restore the saved message. */
8006 parser->type_definition_forbidden_message = saved_message;
8008 /* At this point this can only be either a cast or a
8009 parenthesized ctor such as `(T ())' that looks like a cast to
8010 function returning T. */
8011 if (!cp_parser_error_occurred (parser))
8013 /* Only commit if the cast-expression doesn't start with
8014 '++', '--', or '[' in C++11. */
8015 if (cast_expression > 0)
8016 cp_parser_commit_to_topmost_tentative_parse (parser);
8018 expr = cp_parser_cast_expression (parser,
8019 /*address_p=*/false,
8020 /*cast_p=*/true,
8021 /*decltype_p=*/false,
8022 pidk);
8024 if (cp_parser_parse_definitely (parser))
8026 /* Warn about old-style casts, if so requested. */
8027 if (warn_old_style_cast
8028 && !in_system_header_at (input_location)
8029 && !VOID_TYPE_P (type)
8030 && current_lang_name != lang_name_c)
8031 warning (OPT_Wold_style_cast, "use of old-style cast");
8033 /* Only type conversions to integral or enumeration types
8034 can be used in constant-expressions. */
8035 if (!cast_valid_in_integral_constant_expression_p (type)
8036 && cp_parser_non_integral_constant_expression (parser,
8037 NIC_CAST))
8038 return error_mark_node;
8040 /* Perform the cast. */
8041 expr = build_c_cast (input_location, type, expr);
8042 return expr;
8045 else
8046 cp_parser_abort_tentative_parse (parser);
8049 /* If we get here, then it's not a cast, so it must be a
8050 unary-expression. */
8051 return cp_parser_unary_expression (parser, pidk, address_p,
8052 cast_p, decltype_p);
8055 /* Parse a binary expression of the general form:
8057 pm-expression:
8058 cast-expression
8059 pm-expression .* cast-expression
8060 pm-expression ->* cast-expression
8062 multiplicative-expression:
8063 pm-expression
8064 multiplicative-expression * pm-expression
8065 multiplicative-expression / pm-expression
8066 multiplicative-expression % pm-expression
8068 additive-expression:
8069 multiplicative-expression
8070 additive-expression + multiplicative-expression
8071 additive-expression - multiplicative-expression
8073 shift-expression:
8074 additive-expression
8075 shift-expression << additive-expression
8076 shift-expression >> additive-expression
8078 relational-expression:
8079 shift-expression
8080 relational-expression < shift-expression
8081 relational-expression > shift-expression
8082 relational-expression <= shift-expression
8083 relational-expression >= shift-expression
8085 GNU Extension:
8087 relational-expression:
8088 relational-expression <? shift-expression
8089 relational-expression >? shift-expression
8091 equality-expression:
8092 relational-expression
8093 equality-expression == relational-expression
8094 equality-expression != relational-expression
8096 and-expression:
8097 equality-expression
8098 and-expression & equality-expression
8100 exclusive-or-expression:
8101 and-expression
8102 exclusive-or-expression ^ and-expression
8104 inclusive-or-expression:
8105 exclusive-or-expression
8106 inclusive-or-expression | exclusive-or-expression
8108 logical-and-expression:
8109 inclusive-or-expression
8110 logical-and-expression && inclusive-or-expression
8112 logical-or-expression:
8113 logical-and-expression
8114 logical-or-expression || logical-and-expression
8116 All these are implemented with a single function like:
8118 binary-expression:
8119 simple-cast-expression
8120 binary-expression <token> binary-expression
8122 CAST_P is true if this expression is the target of a cast.
8124 The binops_by_token map is used to get the tree codes for each <token> type.
8125 binary-expressions are associated according to a precedence table. */
8127 #define TOKEN_PRECEDENCE(token) \
8128 (((token->type == CPP_GREATER \
8129 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8130 && !parser->greater_than_is_operator_p) \
8131 ? PREC_NOT_OPERATOR \
8132 : binops_by_token[token->type].prec)
8134 static tree
8135 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8136 bool no_toplevel_fold_p,
8137 bool decltype_p,
8138 enum cp_parser_prec prec,
8139 cp_id_kind * pidk)
8141 cp_parser_expression_stack stack;
8142 cp_parser_expression_stack_entry *sp = &stack[0];
8143 cp_parser_expression_stack_entry current;
8144 tree rhs;
8145 cp_token *token;
8146 enum tree_code rhs_type;
8147 enum cp_parser_prec new_prec, lookahead_prec;
8148 tree overload;
8150 /* Parse the first expression. */
8151 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8152 ? TRUTH_NOT_EXPR : ERROR_MARK);
8153 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8154 cast_p, decltype_p, pidk);
8155 current.prec = prec;
8157 if (cp_parser_error_occurred (parser))
8158 return error_mark_node;
8160 for (;;)
8162 /* Get an operator token. */
8163 token = cp_lexer_peek_token (parser->lexer);
8165 if (warn_cxx0x_compat
8166 && token->type == CPP_RSHIFT
8167 && !parser->greater_than_is_operator_p)
8169 if (warning_at (token->location, OPT_Wc__0x_compat,
8170 "%<>>%> operator is treated"
8171 " as two right angle brackets in C++11"))
8172 inform (token->location,
8173 "suggest parentheses around %<>>%> expression");
8176 new_prec = TOKEN_PRECEDENCE (token);
8178 /* Popping an entry off the stack means we completed a subexpression:
8179 - either we found a token which is not an operator (`>' where it is not
8180 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8181 will happen repeatedly;
8182 - or, we found an operator which has lower priority. This is the case
8183 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8184 parsing `3 * 4'. */
8185 if (new_prec <= current.prec)
8187 if (sp == stack)
8188 break;
8189 else
8190 goto pop;
8193 get_rhs:
8194 current.tree_type = binops_by_token[token->type].tree_type;
8195 current.loc = token->location;
8197 /* We used the operator token. */
8198 cp_lexer_consume_token (parser->lexer);
8200 /* For "false && x" or "true || x", x will never be executed;
8201 disable warnings while evaluating it. */
8202 if (current.tree_type == TRUTH_ANDIF_EXPR)
8203 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8204 else if (current.tree_type == TRUTH_ORIF_EXPR)
8205 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8207 /* Extract another operand. It may be the RHS of this expression
8208 or the LHS of a new, higher priority expression. */
8209 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8210 ? TRUTH_NOT_EXPR : ERROR_MARK);
8211 rhs = cp_parser_simple_cast_expression (parser);
8213 /* Get another operator token. Look up its precedence to avoid
8214 building a useless (immediately popped) stack entry for common
8215 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8216 token = cp_lexer_peek_token (parser->lexer);
8217 lookahead_prec = TOKEN_PRECEDENCE (token);
8218 if (lookahead_prec > new_prec)
8220 /* ... and prepare to parse the RHS of the new, higher priority
8221 expression. Since precedence levels on the stack are
8222 monotonically increasing, we do not have to care about
8223 stack overflows. */
8224 *sp = current;
8225 ++sp;
8226 current.lhs = rhs;
8227 current.lhs_type = rhs_type;
8228 current.prec = new_prec;
8229 new_prec = lookahead_prec;
8230 goto get_rhs;
8232 pop:
8233 lookahead_prec = new_prec;
8234 /* If the stack is not empty, we have parsed into LHS the right side
8235 (`4' in the example above) of an expression we had suspended.
8236 We can use the information on the stack to recover the LHS (`3')
8237 from the stack together with the tree code (`MULT_EXPR'), and
8238 the precedence of the higher level subexpression
8239 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8240 which will be used to actually build the additive expression. */
8241 rhs = current.lhs;
8242 rhs_type = current.lhs_type;
8243 --sp;
8244 current = *sp;
8247 /* Undo the disabling of warnings done above. */
8248 if (current.tree_type == TRUTH_ANDIF_EXPR)
8249 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8250 else if (current.tree_type == TRUTH_ORIF_EXPR)
8251 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8253 if (warn_logical_not_paren
8254 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8255 && current.lhs_type == TRUTH_NOT_EXPR
8256 /* Avoid warning for !!x == y. */
8257 && (TREE_CODE (current.lhs) != NE_EXPR
8258 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8259 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8260 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8261 /* Avoid warning for !b == y where b is boolean. */
8262 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8263 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8264 != BOOLEAN_TYPE))))
8265 /* Avoid warning for !!b == y where b is boolean. */
8266 && (!DECL_P (current.lhs)
8267 || TREE_TYPE (current.lhs) == NULL_TREE
8268 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8269 warn_logical_not_parentheses (current.loc, current.tree_type,
8270 maybe_constant_value (rhs));
8272 overload = NULL;
8273 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8274 ERROR_MARK for everything that is not a binary expression.
8275 This makes warn_about_parentheses miss some warnings that
8276 involve unary operators. For unary expressions we should
8277 pass the correct tree_code unless the unary expression was
8278 surrounded by parentheses.
8280 if (no_toplevel_fold_p
8281 && lookahead_prec <= current.prec
8282 && sp == stack)
8283 current.lhs = build2 (current.tree_type,
8284 TREE_CODE_CLASS (current.tree_type)
8285 == tcc_comparison
8286 ? boolean_type_node : TREE_TYPE (current.lhs),
8287 current.lhs, rhs);
8288 else
8289 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8290 current.lhs, current.lhs_type,
8291 rhs, rhs_type, &overload,
8292 complain_flags (decltype_p));
8293 current.lhs_type = current.tree_type;
8294 if (EXPR_P (current.lhs))
8295 SET_EXPR_LOCATION (current.lhs, current.loc);
8297 /* If the binary operator required the use of an overloaded operator,
8298 then this expression cannot be an integral constant-expression.
8299 An overloaded operator can be used even if both operands are
8300 otherwise permissible in an integral constant-expression if at
8301 least one of the operands is of enumeration type. */
8303 if (overload
8304 && cp_parser_non_integral_constant_expression (parser,
8305 NIC_OVERLOADED))
8306 return error_mark_node;
8309 return current.lhs;
8312 static tree
8313 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8314 bool no_toplevel_fold_p,
8315 enum cp_parser_prec prec,
8316 cp_id_kind * pidk)
8318 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8319 /*decltype*/false, prec, pidk);
8322 /* Parse the `? expression : assignment-expression' part of a
8323 conditional-expression. The LOGICAL_OR_EXPR is the
8324 logical-or-expression that started the conditional-expression.
8325 Returns a representation of the entire conditional-expression.
8327 This routine is used by cp_parser_assignment_expression.
8329 ? expression : assignment-expression
8331 GNU Extensions:
8333 ? : assignment-expression */
8335 static tree
8336 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8338 tree expr;
8339 tree assignment_expr;
8340 struct cp_token *token;
8341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8343 /* Consume the `?' token. */
8344 cp_lexer_consume_token (parser->lexer);
8345 token = cp_lexer_peek_token (parser->lexer);
8346 if (cp_parser_allow_gnu_extensions_p (parser)
8347 && token->type == CPP_COLON)
8349 pedwarn (token->location, OPT_Wpedantic,
8350 "ISO C++ does not allow ?: with omitted middle operand");
8351 /* Implicit true clause. */
8352 expr = NULL_TREE;
8353 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8354 warn_for_omitted_condop (token->location, logical_or_expr);
8356 else
8358 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8359 parser->colon_corrects_to_scope_p = false;
8360 /* Parse the expression. */
8361 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8362 expr = cp_parser_expression (parser);
8363 c_inhibit_evaluation_warnings +=
8364 ((logical_or_expr == truthvalue_true_node)
8365 - (logical_or_expr == truthvalue_false_node));
8366 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8369 /* The next token should be a `:'. */
8370 cp_parser_require (parser, CPP_COLON, RT_COLON);
8371 /* Parse the assignment-expression. */
8372 assignment_expr = cp_parser_assignment_expression (parser);
8373 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8375 /* Build the conditional-expression. */
8376 return build_x_conditional_expr (loc, logical_or_expr,
8377 expr,
8378 assignment_expr,
8379 tf_warning_or_error);
8382 /* Parse an assignment-expression.
8384 assignment-expression:
8385 conditional-expression
8386 logical-or-expression assignment-operator assignment_expression
8387 throw-expression
8389 CAST_P is true if this expression is the target of a cast.
8390 DECLTYPE_P is true if this expression is the operand of decltype.
8392 Returns a representation for the expression. */
8394 static tree
8395 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8396 bool cast_p, bool decltype_p)
8398 tree expr;
8400 /* If the next token is the `throw' keyword, then we're looking at
8401 a throw-expression. */
8402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8403 expr = cp_parser_throw_expression (parser);
8404 /* Otherwise, it must be that we are looking at a
8405 logical-or-expression. */
8406 else
8408 /* Parse the binary expressions (logical-or-expression). */
8409 expr = cp_parser_binary_expression (parser, cast_p, false,
8410 decltype_p,
8411 PREC_NOT_OPERATOR, pidk);
8412 /* If the next token is a `?' then we're actually looking at a
8413 conditional-expression. */
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8415 return cp_parser_question_colon_clause (parser, expr);
8416 else
8418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8420 /* If it's an assignment-operator, we're using the second
8421 production. */
8422 enum tree_code assignment_operator
8423 = cp_parser_assignment_operator_opt (parser);
8424 if (assignment_operator != ERROR_MARK)
8426 bool non_constant_p;
8427 location_t saved_input_location;
8429 /* Parse the right-hand side of the assignment. */
8430 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8432 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8433 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8435 /* An assignment may not appear in a
8436 constant-expression. */
8437 if (cp_parser_non_integral_constant_expression (parser,
8438 NIC_ASSIGNMENT))
8439 return error_mark_node;
8440 /* Build the assignment expression. Its default
8441 location is the location of the '=' token. */
8442 saved_input_location = input_location;
8443 input_location = loc;
8444 expr = build_x_modify_expr (loc, expr,
8445 assignment_operator,
8446 rhs,
8447 complain_flags (decltype_p));
8448 input_location = saved_input_location;
8453 return expr;
8456 /* Parse an (optional) assignment-operator.
8458 assignment-operator: one of
8459 = *= /= %= += -= >>= <<= &= ^= |=
8461 GNU Extension:
8463 assignment-operator: one of
8464 <?= >?=
8466 If the next token is an assignment operator, the corresponding tree
8467 code is returned, and the token is consumed. For example, for
8468 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8469 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8470 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8471 operator, ERROR_MARK is returned. */
8473 static enum tree_code
8474 cp_parser_assignment_operator_opt (cp_parser* parser)
8476 enum tree_code op;
8477 cp_token *token;
8479 /* Peek at the next token. */
8480 token = cp_lexer_peek_token (parser->lexer);
8482 switch (token->type)
8484 case CPP_EQ:
8485 op = NOP_EXPR;
8486 break;
8488 case CPP_MULT_EQ:
8489 op = MULT_EXPR;
8490 break;
8492 case CPP_DIV_EQ:
8493 op = TRUNC_DIV_EXPR;
8494 break;
8496 case CPP_MOD_EQ:
8497 op = TRUNC_MOD_EXPR;
8498 break;
8500 case CPP_PLUS_EQ:
8501 op = PLUS_EXPR;
8502 break;
8504 case CPP_MINUS_EQ:
8505 op = MINUS_EXPR;
8506 break;
8508 case CPP_RSHIFT_EQ:
8509 op = RSHIFT_EXPR;
8510 break;
8512 case CPP_LSHIFT_EQ:
8513 op = LSHIFT_EXPR;
8514 break;
8516 case CPP_AND_EQ:
8517 op = BIT_AND_EXPR;
8518 break;
8520 case CPP_XOR_EQ:
8521 op = BIT_XOR_EXPR;
8522 break;
8524 case CPP_OR_EQ:
8525 op = BIT_IOR_EXPR;
8526 break;
8528 default:
8529 /* Nothing else is an assignment operator. */
8530 op = ERROR_MARK;
8533 /* If it was an assignment operator, consume it. */
8534 if (op != ERROR_MARK)
8535 cp_lexer_consume_token (parser->lexer);
8537 return op;
8540 /* Parse an expression.
8542 expression:
8543 assignment-expression
8544 expression , assignment-expression
8546 CAST_P is true if this expression is the target of a cast.
8547 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8548 except possibly parenthesized or on the RHS of a comma (N3276).
8550 Returns a representation of the expression. */
8552 static tree
8553 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8554 bool cast_p, bool decltype_p)
8556 tree expression = NULL_TREE;
8557 location_t loc = UNKNOWN_LOCATION;
8559 while (true)
8561 tree assignment_expression;
8563 /* Parse the next assignment-expression. */
8564 assignment_expression
8565 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8567 /* We don't create a temporary for a call that is the immediate operand
8568 of decltype or on the RHS of a comma. But when we see a comma, we
8569 need to create a temporary for a call on the LHS. */
8570 if (decltype_p && !processing_template_decl
8571 && TREE_CODE (assignment_expression) == CALL_EXPR
8572 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8573 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8574 assignment_expression
8575 = build_cplus_new (TREE_TYPE (assignment_expression),
8576 assignment_expression, tf_warning_or_error);
8578 /* If this is the first assignment-expression, we can just
8579 save it away. */
8580 if (!expression)
8581 expression = assignment_expression;
8582 else
8583 expression = build_x_compound_expr (loc, expression,
8584 assignment_expression,
8585 complain_flags (decltype_p));
8586 /* If the next token is not a comma, then we are done with the
8587 expression. */
8588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8589 break;
8590 /* Consume the `,'. */
8591 loc = cp_lexer_peek_token (parser->lexer)->location;
8592 cp_lexer_consume_token (parser->lexer);
8593 /* A comma operator cannot appear in a constant-expression. */
8594 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8595 expression = error_mark_node;
8598 return expression;
8601 /* Parse a constant-expression.
8603 constant-expression:
8604 conditional-expression
8606 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8607 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8608 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8609 is false, NON_CONSTANT_P should be NULL. */
8611 static tree
8612 cp_parser_constant_expression (cp_parser* parser,
8613 bool allow_non_constant_p,
8614 bool *non_constant_p)
8616 bool saved_integral_constant_expression_p;
8617 bool saved_allow_non_integral_constant_expression_p;
8618 bool saved_non_integral_constant_expression_p;
8619 tree expression;
8621 /* It might seem that we could simply parse the
8622 conditional-expression, and then check to see if it were
8623 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8624 one that the compiler can figure out is constant, possibly after
8625 doing some simplifications or optimizations. The standard has a
8626 precise definition of constant-expression, and we must honor
8627 that, even though it is somewhat more restrictive.
8629 For example:
8631 int i[(2, 3)];
8633 is not a legal declaration, because `(2, 3)' is not a
8634 constant-expression. The `,' operator is forbidden in a
8635 constant-expression. However, GCC's constant-folding machinery
8636 will fold this operation to an INTEGER_CST for `3'. */
8638 /* Save the old settings. */
8639 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8640 saved_allow_non_integral_constant_expression_p
8641 = parser->allow_non_integral_constant_expression_p;
8642 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8643 /* We are now parsing a constant-expression. */
8644 parser->integral_constant_expression_p = true;
8645 parser->allow_non_integral_constant_expression_p
8646 = (allow_non_constant_p || cxx_dialect >= cxx11);
8647 parser->non_integral_constant_expression_p = false;
8648 /* Although the grammar says "conditional-expression", we parse an
8649 "assignment-expression", which also permits "throw-expression"
8650 and the use of assignment operators. In the case that
8651 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8652 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8653 actually essential that we look for an assignment-expression.
8654 For example, cp_parser_initializer_clauses uses this function to
8655 determine whether a particular assignment-expression is in fact
8656 constant. */
8657 expression = cp_parser_assignment_expression (parser);
8658 /* Restore the old settings. */
8659 parser->integral_constant_expression_p
8660 = saved_integral_constant_expression_p;
8661 parser->allow_non_integral_constant_expression_p
8662 = saved_allow_non_integral_constant_expression_p;
8663 if (cxx_dialect >= cxx11)
8665 /* Require an rvalue constant expression here; that's what our
8666 callers expect. Reference constant expressions are handled
8667 separately in e.g. cp_parser_template_argument. */
8668 bool is_const = potential_rvalue_constant_expression (expression);
8669 parser->non_integral_constant_expression_p = !is_const;
8670 if (!is_const && !allow_non_constant_p)
8671 require_potential_rvalue_constant_expression (expression);
8673 if (allow_non_constant_p)
8674 *non_constant_p = parser->non_integral_constant_expression_p;
8675 parser->non_integral_constant_expression_p
8676 = saved_non_integral_constant_expression_p;
8678 return expression;
8681 /* Parse __builtin_offsetof.
8683 offsetof-expression:
8684 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8686 offsetof-member-designator:
8687 id-expression
8688 | offsetof-member-designator "." id-expression
8689 | offsetof-member-designator "[" expression "]"
8690 | offsetof-member-designator "->" id-expression */
8692 static tree
8693 cp_parser_builtin_offsetof (cp_parser *parser)
8695 int save_ice_p, save_non_ice_p;
8696 tree type, expr;
8697 cp_id_kind dummy;
8698 cp_token *token;
8700 /* We're about to accept non-integral-constant things, but will
8701 definitely yield an integral constant expression. Save and
8702 restore these values around our local parsing. */
8703 save_ice_p = parser->integral_constant_expression_p;
8704 save_non_ice_p = parser->non_integral_constant_expression_p;
8706 /* Consume the "__builtin_offsetof" token. */
8707 cp_lexer_consume_token (parser->lexer);
8708 /* Consume the opening `('. */
8709 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8710 /* Parse the type-id. */
8711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8712 type = cp_parser_type_id (parser);
8713 /* Look for the `,'. */
8714 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8715 token = cp_lexer_peek_token (parser->lexer);
8717 /* Build the (type *)null that begins the traditional offsetof macro. */
8718 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8719 tf_warning_or_error);
8721 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8722 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8723 true, &dummy, token->location);
8724 while (true)
8726 token = cp_lexer_peek_token (parser->lexer);
8727 switch (token->type)
8729 case CPP_OPEN_SQUARE:
8730 /* offsetof-member-designator "[" expression "]" */
8731 expr = cp_parser_postfix_open_square_expression (parser, expr,
8732 true, false);
8733 break;
8735 case CPP_DEREF:
8736 /* offsetof-member-designator "->" identifier */
8737 expr = grok_array_decl (token->location, expr,
8738 integer_zero_node, false);
8739 /* FALLTHRU */
8741 case CPP_DOT:
8742 /* offsetof-member-designator "." identifier */
8743 cp_lexer_consume_token (parser->lexer);
8744 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8745 expr, true, &dummy,
8746 token->location);
8747 break;
8749 case CPP_CLOSE_PAREN:
8750 /* Consume the ")" token. */
8751 cp_lexer_consume_token (parser->lexer);
8752 goto success;
8754 default:
8755 /* Error. We know the following require will fail, but
8756 that gives the proper error message. */
8757 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8758 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8759 expr = error_mark_node;
8760 goto failure;
8764 success:
8765 expr = finish_offsetof (expr, loc);
8767 failure:
8768 parser->integral_constant_expression_p = save_ice_p;
8769 parser->non_integral_constant_expression_p = save_non_ice_p;
8771 return expr;
8774 /* Parse a trait expression.
8776 Returns a representation of the expression, the underlying type
8777 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8779 static tree
8780 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8782 cp_trait_kind kind;
8783 tree type1, type2 = NULL_TREE;
8784 bool binary = false;
8785 bool variadic = false;
8787 switch (keyword)
8789 case RID_HAS_NOTHROW_ASSIGN:
8790 kind = CPTK_HAS_NOTHROW_ASSIGN;
8791 break;
8792 case RID_HAS_NOTHROW_CONSTRUCTOR:
8793 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8794 break;
8795 case RID_HAS_NOTHROW_COPY:
8796 kind = CPTK_HAS_NOTHROW_COPY;
8797 break;
8798 case RID_HAS_TRIVIAL_ASSIGN:
8799 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8800 break;
8801 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8802 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8803 break;
8804 case RID_HAS_TRIVIAL_COPY:
8805 kind = CPTK_HAS_TRIVIAL_COPY;
8806 break;
8807 case RID_HAS_TRIVIAL_DESTRUCTOR:
8808 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8809 break;
8810 case RID_HAS_VIRTUAL_DESTRUCTOR:
8811 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8812 break;
8813 case RID_IS_ABSTRACT:
8814 kind = CPTK_IS_ABSTRACT;
8815 break;
8816 case RID_IS_BASE_OF:
8817 kind = CPTK_IS_BASE_OF;
8818 binary = true;
8819 break;
8820 case RID_IS_CLASS:
8821 kind = CPTK_IS_CLASS;
8822 break;
8823 case RID_IS_EMPTY:
8824 kind = CPTK_IS_EMPTY;
8825 break;
8826 case RID_IS_ENUM:
8827 kind = CPTK_IS_ENUM;
8828 break;
8829 case RID_IS_FINAL:
8830 kind = CPTK_IS_FINAL;
8831 break;
8832 case RID_IS_LITERAL_TYPE:
8833 kind = CPTK_IS_LITERAL_TYPE;
8834 break;
8835 case RID_IS_POD:
8836 kind = CPTK_IS_POD;
8837 break;
8838 case RID_IS_POLYMORPHIC:
8839 kind = CPTK_IS_POLYMORPHIC;
8840 break;
8841 case RID_IS_STD_LAYOUT:
8842 kind = CPTK_IS_STD_LAYOUT;
8843 break;
8844 case RID_IS_TRIVIAL:
8845 kind = CPTK_IS_TRIVIAL;
8846 break;
8847 case RID_IS_TRIVIALLY_ASSIGNABLE:
8848 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8849 binary = true;
8850 break;
8851 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8852 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8853 variadic = true;
8854 break;
8855 case RID_IS_TRIVIALLY_COPYABLE:
8856 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8857 break;
8858 case RID_IS_UNION:
8859 kind = CPTK_IS_UNION;
8860 break;
8861 case RID_UNDERLYING_TYPE:
8862 kind = CPTK_UNDERLYING_TYPE;
8863 break;
8864 case RID_BASES:
8865 kind = CPTK_BASES;
8866 break;
8867 case RID_DIRECT_BASES:
8868 kind = CPTK_DIRECT_BASES;
8869 break;
8870 default:
8871 gcc_unreachable ();
8874 /* Consume the token. */
8875 cp_lexer_consume_token (parser->lexer);
8877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8879 type1 = cp_parser_type_id (parser);
8881 if (type1 == error_mark_node)
8882 return error_mark_node;
8884 if (binary)
8886 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8888 type2 = cp_parser_type_id (parser);
8890 if (type2 == error_mark_node)
8891 return error_mark_node;
8893 else if (variadic)
8895 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8897 cp_lexer_consume_token (parser->lexer);
8898 tree elt = cp_parser_type_id (parser);
8899 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8901 cp_lexer_consume_token (parser->lexer);
8902 elt = make_pack_expansion (elt);
8904 if (elt == error_mark_node)
8905 return error_mark_node;
8906 type2 = tree_cons (NULL_TREE, elt, type2);
8910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8912 /* Complete the trait expression, which may mean either processing
8913 the trait expr now or saving it for template instantiation. */
8914 switch(kind)
8916 case CPTK_UNDERLYING_TYPE:
8917 return finish_underlying_type (type1);
8918 case CPTK_BASES:
8919 return finish_bases (type1, false);
8920 case CPTK_DIRECT_BASES:
8921 return finish_bases (type1, true);
8922 default:
8923 return finish_trait_expr (kind, type1, type2);
8927 /* Lambdas that appear in variable initializer or default argument scope
8928 get that in their mangling, so we need to record it. We might as well
8929 use the count for function and namespace scopes as well. */
8930 static GTY(()) tree lambda_scope;
8931 static GTY(()) int lambda_count;
8932 typedef struct GTY(()) tree_int
8934 tree t;
8935 int i;
8936 } tree_int;
8937 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8939 static void
8940 start_lambda_scope (tree decl)
8942 tree_int ti;
8943 gcc_assert (decl);
8944 /* Once we're inside a function, we ignore other scopes and just push
8945 the function again so that popping works properly. */
8946 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8947 decl = current_function_decl;
8948 ti.t = lambda_scope;
8949 ti.i = lambda_count;
8950 vec_safe_push (lambda_scope_stack, ti);
8951 if (lambda_scope != decl)
8953 /* Don't reset the count if we're still in the same function. */
8954 lambda_scope = decl;
8955 lambda_count = 0;
8959 static void
8960 record_lambda_scope (tree lambda)
8962 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8963 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8966 static void
8967 finish_lambda_scope (void)
8969 tree_int *p = &lambda_scope_stack->last ();
8970 if (lambda_scope != p->t)
8972 lambda_scope = p->t;
8973 lambda_count = p->i;
8975 lambda_scope_stack->pop ();
8978 /* Parse a lambda expression.
8980 lambda-expression:
8981 lambda-introducer lambda-declarator [opt] compound-statement
8983 Returns a representation of the expression. */
8985 static tree
8986 cp_parser_lambda_expression (cp_parser* parser)
8988 tree lambda_expr = build_lambda_expr ();
8989 tree type;
8990 bool ok = true;
8991 cp_token *token = cp_lexer_peek_token (parser->lexer);
8992 cp_token_position start = 0;
8994 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8996 if (cp_unevaluated_operand)
8998 if (!token->error_reported)
9000 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9001 "lambda-expression in unevaluated context");
9002 token->error_reported = true;
9004 ok = false;
9006 else if (parser->in_template_argument_list_p)
9008 if (!token->error_reported)
9010 error_at (token->location, "lambda-expression in template-argument");
9011 token->error_reported = true;
9013 ok = false;
9016 /* We may be in the middle of deferred access check. Disable
9017 it now. */
9018 push_deferring_access_checks (dk_no_deferred);
9020 cp_parser_lambda_introducer (parser, lambda_expr);
9022 type = begin_lambda_type (lambda_expr);
9023 if (type == error_mark_node)
9024 return error_mark_node;
9026 record_lambda_scope (lambda_expr);
9028 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9029 determine_visibility (TYPE_NAME (type));
9031 /* Now that we've started the type, add the capture fields for any
9032 explicit captures. */
9033 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9036 /* Inside the class, surrounding template-parameter-lists do not apply. */
9037 unsigned int saved_num_template_parameter_lists
9038 = parser->num_template_parameter_lists;
9039 unsigned char in_statement = parser->in_statement;
9040 bool in_switch_statement_p = parser->in_switch_statement_p;
9041 bool fully_implicit_function_template_p
9042 = parser->fully_implicit_function_template_p;
9043 tree implicit_template_parms = parser->implicit_template_parms;
9044 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9045 bool auto_is_implicit_function_template_parm_p
9046 = parser->auto_is_implicit_function_template_parm_p;
9048 parser->num_template_parameter_lists = 0;
9049 parser->in_statement = 0;
9050 parser->in_switch_statement_p = false;
9051 parser->fully_implicit_function_template_p = false;
9052 parser->implicit_template_parms = 0;
9053 parser->implicit_template_scope = 0;
9054 parser->auto_is_implicit_function_template_parm_p = false;
9056 /* By virtue of defining a local class, a lambda expression has access to
9057 the private variables of enclosing classes. */
9059 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9061 if (ok)
9063 if (!cp_parser_error_occurred (parser)
9064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9065 && cp_parser_start_tentative_firewall (parser))
9066 start = token;
9067 cp_parser_lambda_body (parser, lambda_expr);
9069 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9071 if (cp_parser_skip_to_closing_brace (parser))
9072 cp_lexer_consume_token (parser->lexer);
9075 /* The capture list was built up in reverse order; fix that now. */
9076 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9077 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9079 if (ok)
9080 maybe_add_lambda_conv_op (type);
9082 type = finish_struct (type, /*attributes=*/NULL_TREE);
9084 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9085 parser->in_statement = in_statement;
9086 parser->in_switch_statement_p = in_switch_statement_p;
9087 parser->fully_implicit_function_template_p
9088 = fully_implicit_function_template_p;
9089 parser->implicit_template_parms = implicit_template_parms;
9090 parser->implicit_template_scope = implicit_template_scope;
9091 parser->auto_is_implicit_function_template_parm_p
9092 = auto_is_implicit_function_template_parm_p;
9095 pop_deferring_access_checks ();
9097 /* This field is only used during parsing of the lambda. */
9098 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9100 /* This lambda shouldn't have any proxies left at this point. */
9101 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9102 /* And now that we're done, push proxies for an enclosing lambda. */
9103 insert_pending_capture_proxies ();
9105 if (ok)
9106 lambda_expr = build_lambda_object (lambda_expr);
9107 else
9108 lambda_expr = error_mark_node;
9110 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9112 return lambda_expr;
9115 /* Parse the beginning of a lambda expression.
9117 lambda-introducer:
9118 [ lambda-capture [opt] ]
9120 LAMBDA_EXPR is the current representation of the lambda expression. */
9122 static void
9123 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9125 /* Need commas after the first capture. */
9126 bool first = true;
9128 /* Eat the leading `['. */
9129 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9131 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9132 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9133 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9134 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9135 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9136 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9138 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9140 cp_lexer_consume_token (parser->lexer);
9141 first = false;
9144 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9146 cp_token* capture_token;
9147 tree capture_id;
9148 tree capture_init_expr;
9149 cp_id_kind idk = CP_ID_KIND_NONE;
9150 bool explicit_init_p = false;
9152 enum capture_kind_type
9154 BY_COPY,
9155 BY_REFERENCE
9157 enum capture_kind_type capture_kind = BY_COPY;
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9161 error ("expected end of capture-list");
9162 return;
9165 if (first)
9166 first = false;
9167 else
9168 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9170 /* Possibly capture `this'. */
9171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9173 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9174 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9175 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9176 "with by-copy capture default");
9177 cp_lexer_consume_token (parser->lexer);
9178 add_capture (lambda_expr,
9179 /*id=*/this_identifier,
9180 /*initializer=*/finish_this_expr(),
9181 /*by_reference_p=*/false,
9182 explicit_init_p);
9183 continue;
9186 /* Remember whether we want to capture as a reference or not. */
9187 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9189 capture_kind = BY_REFERENCE;
9190 cp_lexer_consume_token (parser->lexer);
9193 /* Get the identifier. */
9194 capture_token = cp_lexer_peek_token (parser->lexer);
9195 capture_id = cp_parser_identifier (parser);
9197 if (capture_id == error_mark_node)
9198 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9199 delimiters, but I modified this to stop on unnested ']' as well. It
9200 was already changed to stop on unnested '}', so the
9201 "closing_parenthesis" name is no more misleading with my change. */
9203 cp_parser_skip_to_closing_parenthesis (parser,
9204 /*recovering=*/true,
9205 /*or_comma=*/true,
9206 /*consume_paren=*/true);
9207 break;
9210 /* Find the initializer for this capture. */
9211 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9212 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9213 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9215 bool direct, non_constant;
9216 /* An explicit initializer exists. */
9217 if (cxx_dialect < cxx14)
9218 pedwarn (input_location, 0,
9219 "lambda capture initializers "
9220 "only available with -std=c++14 or -std=gnu++14");
9221 capture_init_expr = cp_parser_initializer (parser, &direct,
9222 &non_constant);
9223 explicit_init_p = true;
9224 if (capture_init_expr == NULL_TREE)
9226 error ("empty initializer for lambda init-capture");
9227 capture_init_expr = error_mark_node;
9230 else
9232 const char* error_msg;
9234 /* Turn the identifier into an id-expression. */
9235 capture_init_expr
9236 = cp_parser_lookup_name_simple (parser, capture_id,
9237 capture_token->location);
9239 if (capture_init_expr == error_mark_node)
9241 unqualified_name_lookup_error (capture_id);
9242 continue;
9244 else if (DECL_P (capture_init_expr)
9245 && (!VAR_P (capture_init_expr)
9246 && TREE_CODE (capture_init_expr) != PARM_DECL))
9248 error_at (capture_token->location,
9249 "capture of non-variable %qD ",
9250 capture_init_expr);
9251 inform (0, "%q+#D declared here", capture_init_expr);
9252 continue;
9254 if (VAR_P (capture_init_expr)
9255 && decl_storage_duration (capture_init_expr) != dk_auto)
9257 if (pedwarn (capture_token->location, 0, "capture of variable "
9258 "%qD with non-automatic storage duration",
9259 capture_init_expr))
9260 inform (0, "%q+#D declared here", capture_init_expr);
9261 continue;
9264 capture_init_expr
9265 = finish_id_expression
9266 (capture_id,
9267 capture_init_expr,
9268 parser->scope,
9269 &idk,
9270 /*integral_constant_expression_p=*/false,
9271 /*allow_non_integral_constant_expression_p=*/false,
9272 /*non_integral_constant_expression_p=*/NULL,
9273 /*template_p=*/false,
9274 /*done=*/true,
9275 /*address_p=*/false,
9276 /*template_arg_p=*/false,
9277 &error_msg,
9278 capture_token->location);
9280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9282 cp_lexer_consume_token (parser->lexer);
9283 capture_init_expr = make_pack_expansion (capture_init_expr);
9285 else
9286 check_for_bare_parameter_packs (capture_init_expr);
9289 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9290 && !explicit_init_p)
9292 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9293 && capture_kind == BY_COPY)
9294 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9295 "of %qD redundant with by-copy capture default",
9296 capture_id);
9297 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9298 && capture_kind == BY_REFERENCE)
9299 pedwarn (capture_token->location, 0, "explicit by-reference "
9300 "capture of %qD redundant with by-reference capture "
9301 "default", capture_id);
9304 add_capture (lambda_expr,
9305 capture_id,
9306 capture_init_expr,
9307 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9308 explicit_init_p);
9311 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9314 /* Parse the (optional) middle of a lambda expression.
9316 lambda-declarator:
9317 < template-parameter-list [opt] >
9318 ( parameter-declaration-clause [opt] )
9319 attribute-specifier [opt]
9320 mutable [opt]
9321 exception-specification [opt]
9322 lambda-return-type-clause [opt]
9324 LAMBDA_EXPR is the current representation of the lambda expression. */
9326 static bool
9327 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9329 /* 5.1.1.4 of the standard says:
9330 If a lambda-expression does not include a lambda-declarator, it is as if
9331 the lambda-declarator were ().
9332 This means an empty parameter list, no attributes, and no exception
9333 specification. */
9334 tree param_list = void_list_node;
9335 tree attributes = NULL_TREE;
9336 tree exception_spec = NULL_TREE;
9337 tree template_param_list = NULL_TREE;
9339 /* The template-parameter-list is optional, but must begin with
9340 an opening angle if present. */
9341 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9343 if (cxx_dialect < cxx14)
9344 pedwarn (parser->lexer->next_token->location, 0,
9345 "lambda templates are only available with "
9346 "-std=c++14 or -std=gnu++14");
9348 cp_lexer_consume_token (parser->lexer);
9350 template_param_list = cp_parser_template_parameter_list (parser);
9352 cp_parser_skip_to_end_of_template_parameter_list (parser);
9354 /* We just processed one more parameter list. */
9355 ++parser->num_template_parameter_lists;
9358 /* The parameter-declaration-clause is optional (unless
9359 template-parameter-list was given), but must begin with an
9360 opening parenthesis if present. */
9361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9363 cp_lexer_consume_token (parser->lexer);
9365 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9367 /* Parse parameters. */
9368 param_list = cp_parser_parameter_declaration_clause (parser);
9370 /* Default arguments shall not be specified in the
9371 parameter-declaration-clause of a lambda-declarator. */
9372 for (tree t = param_list; t; t = TREE_CHAIN (t))
9373 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9374 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9375 "default argument specified for lambda parameter");
9377 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9379 attributes = cp_parser_attributes_opt (parser);
9381 /* Parse optional `mutable' keyword. */
9382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9384 cp_lexer_consume_token (parser->lexer);
9385 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9388 /* Parse optional exception specification. */
9389 exception_spec = cp_parser_exception_specification_opt (parser);
9391 /* Parse optional trailing return type. */
9392 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9394 cp_lexer_consume_token (parser->lexer);
9395 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9396 = cp_parser_trailing_type_id (parser);
9399 /* The function parameters must be in scope all the way until after the
9400 trailing-return-type in case of decltype. */
9401 pop_bindings_and_leave_scope ();
9403 else if (template_param_list != NULL_TREE) // generate diagnostic
9404 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9406 /* Create the function call operator.
9408 Messing with declarators like this is no uglier than building up the
9409 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9410 other code. */
9412 cp_decl_specifier_seq return_type_specs;
9413 cp_declarator* declarator;
9414 tree fco;
9415 int quals;
9416 void *p;
9418 clear_decl_specs (&return_type_specs);
9419 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9420 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9421 else
9422 /* Maybe we will deduce the return type later. */
9423 return_type_specs.type = make_auto ();
9425 p = obstack_alloc (&declarator_obstack, 0);
9427 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9428 sfk_none);
9430 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9431 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9432 declarator = make_call_declarator (declarator, param_list, quals,
9433 VIRT_SPEC_UNSPECIFIED,
9434 REF_QUAL_NONE,
9435 exception_spec,
9436 /*late_return_type=*/NULL_TREE);
9437 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9439 fco = grokmethod (&return_type_specs,
9440 declarator,
9441 attributes);
9442 if (fco != error_mark_node)
9444 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9445 DECL_ARTIFICIAL (fco) = 1;
9446 /* Give the object parameter a different name. */
9447 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9448 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9449 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9451 if (template_param_list)
9453 fco = finish_member_template_decl (fco);
9454 finish_template_decl (template_param_list);
9455 --parser->num_template_parameter_lists;
9457 else if (parser->fully_implicit_function_template_p)
9458 fco = finish_fully_implicit_template (parser, fco);
9460 finish_member_declaration (fco);
9462 obstack_free (&declarator_obstack, p);
9464 return (fco != error_mark_node);
9468 /* Parse the body of a lambda expression, which is simply
9470 compound-statement
9472 but which requires special handling.
9473 LAMBDA_EXPR is the current representation of the lambda expression. */
9475 static void
9476 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9478 bool nested = (current_function_decl != NULL_TREE);
9479 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9480 if (nested)
9481 push_function_context ();
9482 else
9483 /* Still increment function_depth so that we don't GC in the
9484 middle of an expression. */
9485 ++function_depth;
9486 /* Clear this in case we're in the middle of a default argument. */
9487 parser->local_variables_forbidden_p = false;
9489 /* Finish the function call operator
9490 - class_specifier
9491 + late_parsing_for_member
9492 + function_definition_after_declarator
9493 + ctor_initializer_opt_and_function_body */
9495 tree fco = lambda_function (lambda_expr);
9496 tree body;
9497 bool done = false;
9498 tree compound_stmt;
9499 tree cap;
9501 /* Let the front end know that we are going to be defining this
9502 function. */
9503 start_preparsed_function (fco,
9504 NULL_TREE,
9505 SF_PRE_PARSED | SF_INCLASS_INLINE);
9507 start_lambda_scope (fco);
9508 body = begin_function_body ();
9510 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9511 goto out;
9513 /* Push the proxies for any explicit captures. */
9514 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9515 cap = TREE_CHAIN (cap))
9516 build_capture_proxy (TREE_PURPOSE (cap));
9518 compound_stmt = begin_compound_stmt (0);
9520 /* 5.1.1.4 of the standard says:
9521 If a lambda-expression does not include a trailing-return-type, it
9522 is as if the trailing-return-type denotes the following type:
9523 * if the compound-statement is of the form
9524 { return attribute-specifier [opt] expression ; }
9525 the type of the returned expression after lvalue-to-rvalue
9526 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9527 (_conv.array_ 4.2), and function-to-pointer conversion
9528 (_conv.func_ 4.3);
9529 * otherwise, void. */
9531 /* In a lambda that has neither a lambda-return-type-clause
9532 nor a deducible form, errors should be reported for return statements
9533 in the body. Since we used void as the placeholder return type, parsing
9534 the body as usual will give such desired behavior. */
9535 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9536 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9537 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9539 tree expr = NULL_TREE;
9540 cp_id_kind idk = CP_ID_KIND_NONE;
9542 /* Parse tentatively in case there's more after the initial return
9543 statement. */
9544 cp_parser_parse_tentatively (parser);
9546 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9548 expr = cp_parser_expression (parser, &idk);
9550 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9551 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9553 if (cp_parser_parse_definitely (parser))
9555 if (!processing_template_decl)
9556 apply_deduced_return_type (fco, lambda_return_type (expr));
9558 /* Will get error here if type not deduced yet. */
9559 finish_return_stmt (expr);
9561 done = true;
9565 if (!done)
9567 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9568 cp_parser_label_declaration (parser);
9569 cp_parser_statement_seq_opt (parser, NULL_TREE);
9570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9573 finish_compound_stmt (compound_stmt);
9575 out:
9576 finish_function_body (body);
9577 finish_lambda_scope ();
9579 /* Finish the function and generate code for it if necessary. */
9580 tree fn = finish_function (/*inline*/2);
9582 /* Only expand if the call op is not a template. */
9583 if (!DECL_TEMPLATE_INFO (fco))
9584 expand_or_defer_fn (fn);
9587 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9588 if (nested)
9589 pop_function_context();
9590 else
9591 --function_depth;
9594 /* Statements [gram.stmt.stmt] */
9596 /* Parse a statement.
9598 statement:
9599 labeled-statement
9600 expression-statement
9601 compound-statement
9602 selection-statement
9603 iteration-statement
9604 jump-statement
9605 declaration-statement
9606 try-block
9608 C++11:
9610 statement:
9611 labeled-statement
9612 attribute-specifier-seq (opt) expression-statement
9613 attribute-specifier-seq (opt) compound-statement
9614 attribute-specifier-seq (opt) selection-statement
9615 attribute-specifier-seq (opt) iteration-statement
9616 attribute-specifier-seq (opt) jump-statement
9617 declaration-statement
9618 attribute-specifier-seq (opt) try-block
9620 TM Extension:
9622 statement:
9623 atomic-statement
9625 IN_COMPOUND is true when the statement is nested inside a
9626 cp_parser_compound_statement; this matters for certain pragmas.
9628 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9629 is a (possibly labeled) if statement which is not enclosed in braces
9630 and has an else clause. This is used to implement -Wparentheses. */
9632 static void
9633 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9634 bool in_compound, bool *if_p)
9636 tree statement, std_attrs = NULL_TREE;
9637 cp_token *token;
9638 location_t statement_location, attrs_location;
9640 restart:
9641 if (if_p != NULL)
9642 *if_p = false;
9643 /* There is no statement yet. */
9644 statement = NULL_TREE;
9646 saved_token_sentinel saved_tokens (parser->lexer);
9647 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9648 if (c_dialect_objc ())
9649 /* In obj-c++, seeing '[[' might be the either the beginning of
9650 c++11 attributes, or a nested objc-message-expression. So
9651 let's parse the c++11 attributes tentatively. */
9652 cp_parser_parse_tentatively (parser);
9653 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9654 if (c_dialect_objc ())
9656 if (!cp_parser_parse_definitely (parser))
9657 std_attrs = NULL_TREE;
9660 /* Peek at the next token. */
9661 token = cp_lexer_peek_token (parser->lexer);
9662 /* Remember the location of the first token in the statement. */
9663 statement_location = token->location;
9664 /* If this is a keyword, then that will often determine what kind of
9665 statement we have. */
9666 if (token->type == CPP_KEYWORD)
9668 enum rid keyword = token->keyword;
9670 switch (keyword)
9672 case RID_CASE:
9673 case RID_DEFAULT:
9674 /* Looks like a labeled-statement with a case label.
9675 Parse the label, and then use tail recursion to parse
9676 the statement. */
9677 cp_parser_label_for_labeled_statement (parser, std_attrs);
9678 goto restart;
9680 case RID_IF:
9681 case RID_SWITCH:
9682 statement = cp_parser_selection_statement (parser, if_p);
9683 break;
9685 case RID_WHILE:
9686 case RID_DO:
9687 case RID_FOR:
9688 statement = cp_parser_iteration_statement (parser, false);
9689 break;
9691 case RID_CILK_FOR:
9692 if (!flag_cilkplus)
9694 error_at (cp_lexer_peek_token (parser->lexer)->location,
9695 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9696 cp_lexer_consume_token (parser->lexer);
9697 statement = error_mark_node;
9699 else
9700 statement = cp_parser_cilk_for (parser, integer_zero_node);
9701 break;
9703 case RID_BREAK:
9704 case RID_CONTINUE:
9705 case RID_RETURN:
9706 case RID_GOTO:
9707 statement = cp_parser_jump_statement (parser);
9708 break;
9710 case RID_CILK_SYNC:
9711 cp_lexer_consume_token (parser->lexer);
9712 if (flag_cilkplus)
9714 tree sync_expr = build_cilk_sync ();
9715 SET_EXPR_LOCATION (sync_expr,
9716 token->location);
9717 statement = finish_expr_stmt (sync_expr);
9719 else
9721 error_at (token->location, "-fcilkplus must be enabled to use"
9722 " %<_Cilk_sync%>");
9723 statement = error_mark_node;
9725 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9726 break;
9728 /* Objective-C++ exception-handling constructs. */
9729 case RID_AT_TRY:
9730 case RID_AT_CATCH:
9731 case RID_AT_FINALLY:
9732 case RID_AT_SYNCHRONIZED:
9733 case RID_AT_THROW:
9734 statement = cp_parser_objc_statement (parser);
9735 break;
9737 case RID_TRY:
9738 statement = cp_parser_try_block (parser);
9739 break;
9741 case RID_NAMESPACE:
9742 /* This must be a namespace alias definition. */
9743 cp_parser_declaration_statement (parser);
9744 return;
9746 case RID_TRANSACTION_ATOMIC:
9747 case RID_TRANSACTION_RELAXED:
9748 statement = cp_parser_transaction (parser, keyword);
9749 break;
9750 case RID_TRANSACTION_CANCEL:
9751 statement = cp_parser_transaction_cancel (parser);
9752 break;
9754 default:
9755 /* It might be a keyword like `int' that can start a
9756 declaration-statement. */
9757 break;
9760 else if (token->type == CPP_NAME)
9762 /* If the next token is a `:', then we are looking at a
9763 labeled-statement. */
9764 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9765 if (token->type == CPP_COLON)
9767 /* Looks like a labeled-statement with an ordinary label.
9768 Parse the label, and then use tail recursion to parse
9769 the statement. */
9771 cp_parser_label_for_labeled_statement (parser, std_attrs);
9772 goto restart;
9775 /* Anything that starts with a `{' must be a compound-statement. */
9776 else if (token->type == CPP_OPEN_BRACE)
9777 statement = cp_parser_compound_statement (parser, NULL, false, false);
9778 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9779 a statement all its own. */
9780 else if (token->type == CPP_PRAGMA)
9782 /* Only certain OpenMP pragmas are attached to statements, and thus
9783 are considered statements themselves. All others are not. In
9784 the context of a compound, accept the pragma as a "statement" and
9785 return so that we can check for a close brace. Otherwise we
9786 require a real statement and must go back and read one. */
9787 if (in_compound)
9788 cp_parser_pragma (parser, pragma_compound);
9789 else if (!cp_parser_pragma (parser, pragma_stmt))
9790 goto restart;
9791 return;
9793 else if (token->type == CPP_EOF)
9795 cp_parser_error (parser, "expected statement");
9796 return;
9799 /* Everything else must be a declaration-statement or an
9800 expression-statement. Try for the declaration-statement
9801 first, unless we are looking at a `;', in which case we know that
9802 we have an expression-statement. */
9803 if (!statement)
9805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9807 if (std_attrs != NULL_TREE)
9809 /* Attributes should be parsed as part of the the
9810 declaration, so let's un-parse them. */
9811 saved_tokens.rollback();
9812 std_attrs = NULL_TREE;
9815 cp_parser_parse_tentatively (parser);
9816 /* Try to parse the declaration-statement. */
9817 cp_parser_declaration_statement (parser);
9818 /* If that worked, we're done. */
9819 if (cp_parser_parse_definitely (parser))
9820 return;
9822 /* Look for an expression-statement instead. */
9823 statement = cp_parser_expression_statement (parser, in_statement_expr);
9826 /* Set the line number for the statement. */
9827 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9828 SET_EXPR_LOCATION (statement, statement_location);
9830 /* Note that for now, we don't do anything with c++11 statements
9831 parsed at this level. */
9832 if (std_attrs != NULL_TREE)
9833 warning_at (attrs_location,
9834 OPT_Wattributes,
9835 "attributes at the beginning of statement are ignored");
9838 /* Parse the label for a labeled-statement, i.e.
9840 identifier :
9841 case constant-expression :
9842 default :
9844 GNU Extension:
9845 case constant-expression ... constant-expression : statement
9847 When a label is parsed without errors, the label is added to the
9848 parse tree by the finish_* functions, so this function doesn't
9849 have to return the label. */
9851 static void
9852 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9854 cp_token *token;
9855 tree label = NULL_TREE;
9856 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9858 /* The next token should be an identifier. */
9859 token = cp_lexer_peek_token (parser->lexer);
9860 if (token->type != CPP_NAME
9861 && token->type != CPP_KEYWORD)
9863 cp_parser_error (parser, "expected labeled-statement");
9864 return;
9867 parser->colon_corrects_to_scope_p = false;
9868 switch (token->keyword)
9870 case RID_CASE:
9872 tree expr, expr_hi;
9873 cp_token *ellipsis;
9875 /* Consume the `case' token. */
9876 cp_lexer_consume_token (parser->lexer);
9877 /* Parse the constant-expression. */
9878 expr = cp_parser_constant_expression (parser);
9879 if (check_for_bare_parameter_packs (expr))
9880 expr = error_mark_node;
9882 ellipsis = cp_lexer_peek_token (parser->lexer);
9883 if (ellipsis->type == CPP_ELLIPSIS)
9885 /* Consume the `...' token. */
9886 cp_lexer_consume_token (parser->lexer);
9887 expr_hi = cp_parser_constant_expression (parser);
9888 if (check_for_bare_parameter_packs (expr_hi))
9889 expr_hi = error_mark_node;
9891 /* We don't need to emit warnings here, as the common code
9892 will do this for us. */
9894 else
9895 expr_hi = NULL_TREE;
9897 if (parser->in_switch_statement_p)
9898 finish_case_label (token->location, expr, expr_hi);
9899 else
9900 error_at (token->location,
9901 "case label %qE not within a switch statement",
9902 expr);
9904 break;
9906 case RID_DEFAULT:
9907 /* Consume the `default' token. */
9908 cp_lexer_consume_token (parser->lexer);
9910 if (parser->in_switch_statement_p)
9911 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9912 else
9913 error_at (token->location, "case label not within a switch statement");
9914 break;
9916 default:
9917 /* Anything else must be an ordinary label. */
9918 label = finish_label_stmt (cp_parser_identifier (parser));
9919 break;
9922 /* Require the `:' token. */
9923 cp_parser_require (parser, CPP_COLON, RT_COLON);
9925 /* An ordinary label may optionally be followed by attributes.
9926 However, this is only permitted if the attributes are then
9927 followed by a semicolon. This is because, for backward
9928 compatibility, when parsing
9929 lab: __attribute__ ((unused)) int i;
9930 we want the attribute to attach to "i", not "lab". */
9931 if (label != NULL_TREE
9932 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9934 tree attrs;
9935 cp_parser_parse_tentatively (parser);
9936 attrs = cp_parser_gnu_attributes_opt (parser);
9937 if (attrs == NULL_TREE
9938 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9939 cp_parser_abort_tentative_parse (parser);
9940 else if (!cp_parser_parse_definitely (parser))
9942 else
9943 attributes = chainon (attributes, attrs);
9946 if (attributes != NULL_TREE)
9947 cplus_decl_attributes (&label, attributes, 0);
9949 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9952 /* Parse an expression-statement.
9954 expression-statement:
9955 expression [opt] ;
9957 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9958 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9959 indicates whether this expression-statement is part of an
9960 expression statement. */
9962 static tree
9963 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9965 tree statement = NULL_TREE;
9966 cp_token *token = cp_lexer_peek_token (parser->lexer);
9968 /* If the next token is a ';', then there is no expression
9969 statement. */
9970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9972 statement = cp_parser_expression (parser);
9973 if (statement == error_mark_node
9974 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9976 cp_parser_skip_to_end_of_block_or_statement (parser);
9977 return error_mark_node;
9981 /* Give a helpful message for "A<T>::type t;" and the like. */
9982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9983 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9985 if (TREE_CODE (statement) == SCOPE_REF)
9986 error_at (token->location, "need %<typename%> before %qE because "
9987 "%qT is a dependent scope",
9988 statement, TREE_OPERAND (statement, 0));
9989 else if (is_overloaded_fn (statement)
9990 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9992 /* A::A a; */
9993 tree fn = get_first_fn (statement);
9994 error_at (token->location,
9995 "%<%T::%D%> names the constructor, not the type",
9996 DECL_CONTEXT (fn), DECL_NAME (fn));
10000 /* Consume the final `;'. */
10001 cp_parser_consume_semicolon_at_end_of_statement (parser);
10003 if (in_statement_expr
10004 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10005 /* This is the final expression statement of a statement
10006 expression. */
10007 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10008 else if (statement)
10009 statement = finish_expr_stmt (statement);
10011 return statement;
10014 /* Parse a compound-statement.
10016 compound-statement:
10017 { statement-seq [opt] }
10019 GNU extension:
10021 compound-statement:
10022 { label-declaration-seq [opt] statement-seq [opt] }
10024 label-declaration-seq:
10025 label-declaration
10026 label-declaration-seq label-declaration
10028 Returns a tree representing the statement. */
10030 static tree
10031 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10032 bool in_try, bool function_body)
10034 tree compound_stmt;
10036 /* Consume the `{'. */
10037 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10038 return error_mark_node;
10039 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10040 && !function_body && cxx_dialect < cxx14)
10041 pedwarn (input_location, OPT_Wpedantic,
10042 "compound-statement in constexpr function");
10043 /* Begin the compound-statement. */
10044 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10045 /* If the next keyword is `__label__' we have a label declaration. */
10046 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10047 cp_parser_label_declaration (parser);
10048 /* Parse an (optional) statement-seq. */
10049 cp_parser_statement_seq_opt (parser, in_statement_expr);
10050 /* Finish the compound-statement. */
10051 finish_compound_stmt (compound_stmt);
10052 /* Consume the `}'. */
10053 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10055 return compound_stmt;
10058 /* Parse an (optional) statement-seq.
10060 statement-seq:
10061 statement
10062 statement-seq [opt] statement */
10064 static void
10065 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10067 /* Scan statements until there aren't any more. */
10068 while (true)
10070 cp_token *token = cp_lexer_peek_token (parser->lexer);
10072 /* If we are looking at a `}', then we have run out of
10073 statements; the same is true if we have reached the end
10074 of file, or have stumbled upon a stray '@end'. */
10075 if (token->type == CPP_CLOSE_BRACE
10076 || token->type == CPP_EOF
10077 || token->type == CPP_PRAGMA_EOL
10078 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10079 break;
10081 /* If we are in a compound statement and find 'else' then
10082 something went wrong. */
10083 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10085 if (parser->in_statement & IN_IF_STMT)
10086 break;
10087 else
10089 token = cp_lexer_consume_token (parser->lexer);
10090 error_at (token->location, "%<else%> without a previous %<if%>");
10094 /* Parse the statement. */
10095 cp_parser_statement (parser, in_statement_expr, true, NULL);
10099 /* Parse a selection-statement.
10101 selection-statement:
10102 if ( condition ) statement
10103 if ( condition ) statement else statement
10104 switch ( condition ) statement
10106 Returns the new IF_STMT or SWITCH_STMT.
10108 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10109 is a (possibly labeled) if statement which is not enclosed in
10110 braces and has an else clause. This is used to implement
10111 -Wparentheses. */
10113 static tree
10114 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10116 cp_token *token;
10117 enum rid keyword;
10119 if (if_p != NULL)
10120 *if_p = false;
10122 /* Peek at the next token. */
10123 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10125 /* See what kind of keyword it is. */
10126 keyword = token->keyword;
10127 switch (keyword)
10129 case RID_IF:
10130 case RID_SWITCH:
10132 tree statement;
10133 tree condition;
10135 /* Look for the `('. */
10136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10138 cp_parser_skip_to_end_of_statement (parser);
10139 return error_mark_node;
10142 /* Begin the selection-statement. */
10143 if (keyword == RID_IF)
10144 statement = begin_if_stmt ();
10145 else
10146 statement = begin_switch_stmt ();
10148 /* Parse the condition. */
10149 condition = cp_parser_condition (parser);
10150 /* Look for the `)'. */
10151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10152 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10153 /*consume_paren=*/true);
10155 if (keyword == RID_IF)
10157 bool nested_if;
10158 unsigned char in_statement;
10160 /* Add the condition. */
10161 finish_if_stmt_cond (condition, statement);
10163 /* Parse the then-clause. */
10164 in_statement = parser->in_statement;
10165 parser->in_statement |= IN_IF_STMT;
10166 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10169 add_stmt (build_empty_stmt (loc));
10170 cp_lexer_consume_token (parser->lexer);
10171 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10172 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10173 "empty body in an %<if%> statement");
10174 nested_if = false;
10176 else
10177 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10178 parser->in_statement = in_statement;
10180 finish_then_clause (statement);
10182 /* If the next token is `else', parse the else-clause. */
10183 if (cp_lexer_next_token_is_keyword (parser->lexer,
10184 RID_ELSE))
10186 /* Consume the `else' keyword. */
10187 cp_lexer_consume_token (parser->lexer);
10188 begin_else_clause (statement);
10189 /* Parse the else-clause. */
10190 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10192 location_t loc;
10193 loc = cp_lexer_peek_token (parser->lexer)->location;
10194 warning_at (loc,
10195 OPT_Wempty_body, "suggest braces around "
10196 "empty body in an %<else%> statement");
10197 add_stmt (build_empty_stmt (loc));
10198 cp_lexer_consume_token (parser->lexer);
10200 else
10201 cp_parser_implicitly_scoped_statement (parser, NULL);
10203 finish_else_clause (statement);
10205 /* If we are currently parsing a then-clause, then
10206 IF_P will not be NULL. We set it to true to
10207 indicate that this if statement has an else clause.
10208 This may trigger the Wparentheses warning below
10209 when we get back up to the parent if statement. */
10210 if (if_p != NULL)
10211 *if_p = true;
10213 else
10215 /* This if statement does not have an else clause. If
10216 NESTED_IF is true, then the then-clause is an if
10217 statement which does have an else clause. We warn
10218 about the potential ambiguity. */
10219 if (nested_if)
10220 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10221 "suggest explicit braces to avoid ambiguous"
10222 " %<else%>");
10225 /* Now we're all done with the if-statement. */
10226 finish_if_stmt (statement);
10228 else
10230 bool in_switch_statement_p;
10231 unsigned char in_statement;
10233 /* Add the condition. */
10234 finish_switch_cond (condition, statement);
10236 /* Parse the body of the switch-statement. */
10237 in_switch_statement_p = parser->in_switch_statement_p;
10238 in_statement = parser->in_statement;
10239 parser->in_switch_statement_p = true;
10240 parser->in_statement |= IN_SWITCH_STMT;
10241 cp_parser_implicitly_scoped_statement (parser, NULL);
10242 parser->in_switch_statement_p = in_switch_statement_p;
10243 parser->in_statement = in_statement;
10245 /* Now we're all done with the switch-statement. */
10246 finish_switch_stmt (statement);
10249 return statement;
10251 break;
10253 default:
10254 cp_parser_error (parser, "expected selection-statement");
10255 return error_mark_node;
10259 /* Parse a condition.
10261 condition:
10262 expression
10263 type-specifier-seq declarator = initializer-clause
10264 type-specifier-seq declarator braced-init-list
10266 GNU Extension:
10268 condition:
10269 type-specifier-seq declarator asm-specification [opt]
10270 attributes [opt] = assignment-expression
10272 Returns the expression that should be tested. */
10274 static tree
10275 cp_parser_condition (cp_parser* parser)
10277 cp_decl_specifier_seq type_specifiers;
10278 const char *saved_message;
10279 int declares_class_or_enum;
10281 /* Try the declaration first. */
10282 cp_parser_parse_tentatively (parser);
10283 /* New types are not allowed in the type-specifier-seq for a
10284 condition. */
10285 saved_message = parser->type_definition_forbidden_message;
10286 parser->type_definition_forbidden_message
10287 = G_("types may not be defined in conditions");
10288 /* Parse the type-specifier-seq. */
10289 cp_parser_decl_specifier_seq (parser,
10290 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10291 &type_specifiers,
10292 &declares_class_or_enum);
10293 /* Restore the saved message. */
10294 parser->type_definition_forbidden_message = saved_message;
10295 /* If all is well, we might be looking at a declaration. */
10296 if (!cp_parser_error_occurred (parser))
10298 tree decl;
10299 tree asm_specification;
10300 tree attributes;
10301 cp_declarator *declarator;
10302 tree initializer = NULL_TREE;
10304 /* Parse the declarator. */
10305 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10306 /*ctor_dtor_or_conv_p=*/NULL,
10307 /*parenthesized_p=*/NULL,
10308 /*member_p=*/false,
10309 /*friend_p=*/false);
10310 /* Parse the attributes. */
10311 attributes = cp_parser_attributes_opt (parser);
10312 /* Parse the asm-specification. */
10313 asm_specification = cp_parser_asm_specification_opt (parser);
10314 /* If the next token is not an `=' or '{', then we might still be
10315 looking at an expression. For example:
10317 if (A(a).x)
10319 looks like a decl-specifier-seq and a declarator -- but then
10320 there is no `=', so this is an expression. */
10321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10322 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10323 cp_parser_simulate_error (parser);
10325 /* If we did see an `=' or '{', then we are looking at a declaration
10326 for sure. */
10327 if (cp_parser_parse_definitely (parser))
10329 tree pushed_scope;
10330 bool non_constant_p;
10331 bool flags = LOOKUP_ONLYCONVERTING;
10333 /* Create the declaration. */
10334 decl = start_decl (declarator, &type_specifiers,
10335 /*initialized_p=*/true,
10336 attributes, /*prefix_attributes=*/NULL_TREE,
10337 &pushed_scope);
10339 /* Parse the initializer. */
10340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10342 initializer = cp_parser_braced_list (parser, &non_constant_p);
10343 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10344 flags = 0;
10346 else
10348 /* Consume the `='. */
10349 cp_parser_require (parser, CPP_EQ, RT_EQ);
10350 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10352 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10353 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10355 /* Process the initializer. */
10356 cp_finish_decl (decl,
10357 initializer, !non_constant_p,
10358 asm_specification,
10359 flags);
10361 if (pushed_scope)
10362 pop_scope (pushed_scope);
10364 return convert_from_reference (decl);
10367 /* If we didn't even get past the declarator successfully, we are
10368 definitely not looking at a declaration. */
10369 else
10370 cp_parser_abort_tentative_parse (parser);
10372 /* Otherwise, we are looking at an expression. */
10373 return cp_parser_expression (parser);
10376 /* Parses a for-statement or range-for-statement until the closing ')',
10377 not included. */
10379 static tree
10380 cp_parser_for (cp_parser *parser, bool ivdep)
10382 tree init, scope, decl;
10383 bool is_range_for;
10385 /* Begin the for-statement. */
10386 scope = begin_for_scope (&init);
10388 /* Parse the initialization. */
10389 is_range_for = cp_parser_for_init_statement (parser, &decl);
10391 if (is_range_for)
10392 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10393 else
10394 return cp_parser_c_for (parser, scope, init, ivdep);
10397 static tree
10398 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10400 /* Normal for loop */
10401 tree condition = NULL_TREE;
10402 tree expression = NULL_TREE;
10403 tree stmt;
10405 stmt = begin_for_stmt (scope, init);
10406 /* The for-init-statement has already been parsed in
10407 cp_parser_for_init_statement, so no work is needed here. */
10408 finish_for_init_stmt (stmt);
10410 /* If there's a condition, process it. */
10411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10412 condition = cp_parser_condition (parser);
10413 else if (ivdep)
10415 cp_parser_error (parser, "missing loop condition in loop with "
10416 "%<GCC ivdep%> pragma");
10417 condition = error_mark_node;
10419 finish_for_cond (condition, stmt, ivdep);
10420 /* Look for the `;'. */
10421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10423 /* If there's an expression, process it. */
10424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10425 expression = cp_parser_expression (parser);
10426 finish_for_expr (expression, stmt);
10428 return stmt;
10431 /* Tries to parse a range-based for-statement:
10433 range-based-for:
10434 decl-specifier-seq declarator : expression
10436 The decl-specifier-seq declarator and the `:' are already parsed by
10437 cp_parser_for_init_statement. If processing_template_decl it returns a
10438 newly created RANGE_FOR_STMT; if not, it is converted to a
10439 regular FOR_STMT. */
10441 static tree
10442 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10443 bool ivdep)
10445 tree stmt, range_expr;
10447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10449 bool expr_non_constant_p;
10450 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10452 else
10453 range_expr = cp_parser_expression (parser);
10455 /* If in template, STMT is converted to a normal for-statement
10456 at instantiation. If not, it is done just ahead. */
10457 if (processing_template_decl)
10459 if (check_for_bare_parameter_packs (range_expr))
10460 range_expr = error_mark_node;
10461 stmt = begin_range_for_stmt (scope, init);
10462 if (ivdep)
10463 RANGE_FOR_IVDEP (stmt) = 1;
10464 finish_range_for_decl (stmt, range_decl, range_expr);
10465 if (!type_dependent_expression_p (range_expr)
10466 /* do_auto_deduction doesn't mess with template init-lists. */
10467 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10468 do_range_for_auto_deduction (range_decl, range_expr);
10470 else
10472 stmt = begin_for_stmt (scope, init);
10473 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10475 return stmt;
10478 /* Subroutine of cp_convert_range_for: given the initializer expression,
10479 builds up the range temporary. */
10481 static tree
10482 build_range_temp (tree range_expr)
10484 tree range_type, range_temp;
10486 /* Find out the type deduced by the declaration
10487 `auto &&__range = range_expr'. */
10488 range_type = cp_build_reference_type (make_auto (), true);
10489 range_type = do_auto_deduction (range_type, range_expr,
10490 type_uses_auto (range_type));
10492 /* Create the __range variable. */
10493 range_temp = build_decl (input_location, VAR_DECL,
10494 get_identifier ("__for_range"), range_type);
10495 TREE_USED (range_temp) = 1;
10496 DECL_ARTIFICIAL (range_temp) = 1;
10498 return range_temp;
10501 /* Used by cp_parser_range_for in template context: we aren't going to
10502 do a full conversion yet, but we still need to resolve auto in the
10503 type of the for-range-declaration if present. This is basically
10504 a shortcut version of cp_convert_range_for. */
10506 static void
10507 do_range_for_auto_deduction (tree decl, tree range_expr)
10509 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10510 if (auto_node)
10512 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10513 range_temp = convert_from_reference (build_range_temp (range_expr));
10514 iter_type = (cp_parser_perform_range_for_lookup
10515 (range_temp, &begin_dummy, &end_dummy));
10516 if (iter_type)
10518 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10519 iter_type);
10520 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10521 tf_warning_or_error);
10522 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10523 iter_decl, auto_node);
10528 /* Converts a range-based for-statement into a normal
10529 for-statement, as per the definition.
10531 for (RANGE_DECL : RANGE_EXPR)
10532 BLOCK
10534 should be equivalent to:
10537 auto &&__range = RANGE_EXPR;
10538 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10539 __begin != __end;
10540 ++__begin)
10542 RANGE_DECL = *__begin;
10543 BLOCK
10547 If RANGE_EXPR is an array:
10548 BEGIN_EXPR = __range
10549 END_EXPR = __range + ARRAY_SIZE(__range)
10550 Else if RANGE_EXPR has a member 'begin' or 'end':
10551 BEGIN_EXPR = __range.begin()
10552 END_EXPR = __range.end()
10553 Else:
10554 BEGIN_EXPR = begin(__range)
10555 END_EXPR = end(__range);
10557 If __range has a member 'begin' but not 'end', or vice versa, we must
10558 still use the second alternative (it will surely fail, however).
10559 When calling begin()/end() in the third alternative we must use
10560 argument dependent lookup, but always considering 'std' as an associated
10561 namespace. */
10563 tree
10564 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10565 bool ivdep)
10567 tree begin, end;
10568 tree iter_type, begin_expr, end_expr;
10569 tree condition, expression;
10571 if (range_decl == error_mark_node || range_expr == error_mark_node)
10572 /* If an error happened previously do nothing or else a lot of
10573 unhelpful errors would be issued. */
10574 begin_expr = end_expr = iter_type = error_mark_node;
10575 else
10577 tree range_temp;
10579 if (TREE_CODE (range_expr) == VAR_DECL
10580 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10581 /* Can't bind a reference to an array of runtime bound. */
10582 range_temp = range_expr;
10583 else
10585 range_temp = build_range_temp (range_expr);
10586 pushdecl (range_temp);
10587 cp_finish_decl (range_temp, range_expr,
10588 /*is_constant_init*/false, NULL_TREE,
10589 LOOKUP_ONLYCONVERTING);
10590 range_temp = convert_from_reference (range_temp);
10592 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10593 &begin_expr, &end_expr);
10596 /* The new for initialization statement. */
10597 begin = build_decl (input_location, VAR_DECL,
10598 get_identifier ("__for_begin"), iter_type);
10599 TREE_USED (begin) = 1;
10600 DECL_ARTIFICIAL (begin) = 1;
10601 pushdecl (begin);
10602 cp_finish_decl (begin, begin_expr,
10603 /*is_constant_init*/false, NULL_TREE,
10604 LOOKUP_ONLYCONVERTING);
10606 end = build_decl (input_location, VAR_DECL,
10607 get_identifier ("__for_end"), iter_type);
10608 TREE_USED (end) = 1;
10609 DECL_ARTIFICIAL (end) = 1;
10610 pushdecl (end);
10611 cp_finish_decl (end, end_expr,
10612 /*is_constant_init*/false, NULL_TREE,
10613 LOOKUP_ONLYCONVERTING);
10615 finish_for_init_stmt (statement);
10617 /* The new for condition. */
10618 condition = build_x_binary_op (input_location, NE_EXPR,
10619 begin, ERROR_MARK,
10620 end, ERROR_MARK,
10621 NULL, tf_warning_or_error);
10622 finish_for_cond (condition, statement, ivdep);
10624 /* The new increment expression. */
10625 expression = finish_unary_op_expr (input_location,
10626 PREINCREMENT_EXPR, begin,
10627 tf_warning_or_error);
10628 finish_for_expr (expression, statement);
10630 /* The declaration is initialized with *__begin inside the loop body. */
10631 cp_finish_decl (range_decl,
10632 build_x_indirect_ref (input_location, begin, RO_NULL,
10633 tf_warning_or_error),
10634 /*is_constant_init*/false, NULL_TREE,
10635 LOOKUP_ONLYCONVERTING);
10637 return statement;
10640 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10641 We need to solve both at the same time because the method used
10642 depends on the existence of members begin or end.
10643 Returns the type deduced for the iterator expression. */
10645 static tree
10646 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10648 if (error_operand_p (range))
10650 *begin = *end = error_mark_node;
10651 return error_mark_node;
10654 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10656 error ("range-based %<for%> expression of type %qT "
10657 "has incomplete type", TREE_TYPE (range));
10658 *begin = *end = error_mark_node;
10659 return error_mark_node;
10661 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10663 /* If RANGE is an array, we will use pointer arithmetic. */
10664 *begin = range;
10665 *end = build_binary_op (input_location, PLUS_EXPR,
10666 range,
10667 array_type_nelts_top (TREE_TYPE (range)),
10669 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10671 else
10673 /* If it is not an array, we must do a bit of magic. */
10674 tree id_begin, id_end;
10675 tree member_begin, member_end;
10677 *begin = *end = error_mark_node;
10679 id_begin = get_identifier ("begin");
10680 id_end = get_identifier ("end");
10681 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10682 /*protect=*/2, /*want_type=*/false,
10683 tf_warning_or_error);
10684 member_end = lookup_member (TREE_TYPE (range), id_end,
10685 /*protect=*/2, /*want_type=*/false,
10686 tf_warning_or_error);
10688 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10690 /* Use the member functions. */
10691 if (member_begin != NULL_TREE)
10692 *begin = cp_parser_range_for_member_function (range, id_begin);
10693 else
10694 error ("range-based %<for%> expression of type %qT has an "
10695 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10697 if (member_end != NULL_TREE)
10698 *end = cp_parser_range_for_member_function (range, id_end);
10699 else
10700 error ("range-based %<for%> expression of type %qT has a "
10701 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10703 else
10705 /* Use global functions with ADL. */
10706 vec<tree, va_gc> *vec;
10707 vec = make_tree_vector ();
10709 vec_safe_push (vec, range);
10711 member_begin = perform_koenig_lookup (id_begin, vec,
10712 tf_warning_or_error);
10713 *begin = finish_call_expr (member_begin, &vec, false, true,
10714 tf_warning_or_error);
10715 member_end = perform_koenig_lookup (id_end, vec,
10716 tf_warning_or_error);
10717 *end = finish_call_expr (member_end, &vec, false, true,
10718 tf_warning_or_error);
10720 release_tree_vector (vec);
10723 /* Last common checks. */
10724 if (*begin == error_mark_node || *end == error_mark_node)
10726 /* If one of the expressions is an error do no more checks. */
10727 *begin = *end = error_mark_node;
10728 return error_mark_node;
10730 else if (type_dependent_expression_p (*begin)
10731 || type_dependent_expression_p (*end))
10732 /* Can happen, when, eg, in a template context, Koenig lookup
10733 can't resolve begin/end (c++/58503). */
10734 return NULL_TREE;
10735 else
10737 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10738 /* The unqualified type of the __begin and __end temporaries should
10739 be the same, as required by the multiple auto declaration. */
10740 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10741 error ("inconsistent begin/end types in range-based %<for%> "
10742 "statement: %qT and %qT",
10743 TREE_TYPE (*begin), TREE_TYPE (*end));
10744 return iter_type;
10749 /* Helper function for cp_parser_perform_range_for_lookup.
10750 Builds a tree for RANGE.IDENTIFIER(). */
10752 static tree
10753 cp_parser_range_for_member_function (tree range, tree identifier)
10755 tree member, res;
10756 vec<tree, va_gc> *vec;
10758 member = finish_class_member_access_expr (range, identifier,
10759 false, tf_warning_or_error);
10760 if (member == error_mark_node)
10761 return error_mark_node;
10763 vec = make_tree_vector ();
10764 res = finish_call_expr (member, &vec,
10765 /*disallow_virtual=*/false,
10766 /*koenig_p=*/false,
10767 tf_warning_or_error);
10768 release_tree_vector (vec);
10769 return res;
10772 /* Parse an iteration-statement.
10774 iteration-statement:
10775 while ( condition ) statement
10776 do statement while ( expression ) ;
10777 for ( for-init-statement condition [opt] ; expression [opt] )
10778 statement
10780 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10782 static tree
10783 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10785 cp_token *token;
10786 enum rid keyword;
10787 tree statement;
10788 unsigned char in_statement;
10790 /* Peek at the next token. */
10791 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10792 if (!token)
10793 return error_mark_node;
10795 /* Remember whether or not we are already within an iteration
10796 statement. */
10797 in_statement = parser->in_statement;
10799 /* See what kind of keyword it is. */
10800 keyword = token->keyword;
10801 switch (keyword)
10803 case RID_WHILE:
10805 tree condition;
10807 /* Begin the while-statement. */
10808 statement = begin_while_stmt ();
10809 /* Look for the `('. */
10810 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10811 /* Parse the condition. */
10812 condition = cp_parser_condition (parser);
10813 finish_while_stmt_cond (condition, statement, ivdep);
10814 /* Look for the `)'. */
10815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10816 /* Parse the dependent statement. */
10817 parser->in_statement = IN_ITERATION_STMT;
10818 cp_parser_already_scoped_statement (parser);
10819 parser->in_statement = in_statement;
10820 /* We're done with the while-statement. */
10821 finish_while_stmt (statement);
10823 break;
10825 case RID_DO:
10827 tree expression;
10829 /* Begin the do-statement. */
10830 statement = begin_do_stmt ();
10831 /* Parse the body of the do-statement. */
10832 parser->in_statement = IN_ITERATION_STMT;
10833 cp_parser_implicitly_scoped_statement (parser, NULL);
10834 parser->in_statement = in_statement;
10835 finish_do_body (statement);
10836 /* Look for the `while' keyword. */
10837 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10838 /* Look for the `('. */
10839 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10840 /* Parse the expression. */
10841 expression = cp_parser_expression (parser);
10842 /* We're done with the do-statement. */
10843 finish_do_stmt (expression, statement, ivdep);
10844 /* Look for the `)'. */
10845 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10846 /* Look for the `;'. */
10847 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10849 break;
10851 case RID_FOR:
10853 /* Look for the `('. */
10854 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10856 statement = cp_parser_for (parser, ivdep);
10858 /* Look for the `)'. */
10859 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10861 /* Parse the body of the for-statement. */
10862 parser->in_statement = IN_ITERATION_STMT;
10863 cp_parser_already_scoped_statement (parser);
10864 parser->in_statement = in_statement;
10866 /* We're done with the for-statement. */
10867 finish_for_stmt (statement);
10869 break;
10871 default:
10872 cp_parser_error (parser, "expected iteration-statement");
10873 statement = error_mark_node;
10874 break;
10877 return statement;
10880 /* Parse a for-init-statement or the declarator of a range-based-for.
10881 Returns true if a range-based-for declaration is seen.
10883 for-init-statement:
10884 expression-statement
10885 simple-declaration */
10887 static bool
10888 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10890 /* If the next token is a `;', then we have an empty
10891 expression-statement. Grammatically, this is also a
10892 simple-declaration, but an invalid one, because it does not
10893 declare anything. Therefore, if we did not handle this case
10894 specially, we would issue an error message about an invalid
10895 declaration. */
10896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10898 bool is_range_for = false;
10899 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10902 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10904 /* N3994 -- for (id : init) ... */
10905 if (cxx_dialect < cxx1z)
10906 pedwarn (input_location, 0, "range-based for loop without a "
10907 "type-specifier only available with "
10908 "-std=c++1z or -std=gnu++1z");
10909 tree name = cp_parser_identifier (parser);
10910 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10911 *decl = build_decl (input_location, VAR_DECL, name, type);
10912 pushdecl (*decl);
10913 cp_lexer_consume_token (parser->lexer);
10914 return true;
10917 /* A colon is used in range-based for. */
10918 parser->colon_corrects_to_scope_p = false;
10920 /* We're going to speculatively look for a declaration, falling back
10921 to an expression, if necessary. */
10922 cp_parser_parse_tentatively (parser);
10923 /* Parse the declaration. */
10924 cp_parser_simple_declaration (parser,
10925 /*function_definition_allowed_p=*/false,
10926 decl);
10927 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10928 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10930 /* It is a range-for, consume the ':' */
10931 cp_lexer_consume_token (parser->lexer);
10932 is_range_for = true;
10933 if (cxx_dialect < cxx11)
10935 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10936 "range-based %<for%> loops only available with "
10937 "-std=c++11 or -std=gnu++11");
10938 *decl = error_mark_node;
10941 else
10942 /* The ';' is not consumed yet because we told
10943 cp_parser_simple_declaration not to. */
10944 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10946 if (cp_parser_parse_definitely (parser))
10947 return is_range_for;
10948 /* If the tentative parse failed, then we shall need to look for an
10949 expression-statement. */
10951 /* If we are here, it is an expression-statement. */
10952 cp_parser_expression_statement (parser, NULL_TREE);
10953 return false;
10956 /* Parse a jump-statement.
10958 jump-statement:
10959 break ;
10960 continue ;
10961 return expression [opt] ;
10962 return braced-init-list ;
10963 goto identifier ;
10965 GNU extension:
10967 jump-statement:
10968 goto * expression ;
10970 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10972 static tree
10973 cp_parser_jump_statement (cp_parser* parser)
10975 tree statement = error_mark_node;
10976 cp_token *token;
10977 enum rid keyword;
10978 unsigned char in_statement;
10980 /* Peek at the next token. */
10981 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10982 if (!token)
10983 return error_mark_node;
10985 /* See what kind of keyword it is. */
10986 keyword = token->keyword;
10987 switch (keyword)
10989 case RID_BREAK:
10990 in_statement = parser->in_statement & ~IN_IF_STMT;
10991 switch (in_statement)
10993 case 0:
10994 error_at (token->location, "break statement not within loop or switch");
10995 break;
10996 default:
10997 gcc_assert ((in_statement & IN_SWITCH_STMT)
10998 || in_statement == IN_ITERATION_STMT);
10999 statement = finish_break_stmt ();
11000 if (in_statement == IN_ITERATION_STMT)
11001 break_maybe_infinite_loop ();
11002 break;
11003 case IN_OMP_BLOCK:
11004 error_at (token->location, "invalid exit from OpenMP structured block");
11005 break;
11006 case IN_OMP_FOR:
11007 error_at (token->location, "break statement used with OpenMP for loop");
11008 break;
11009 case IN_CILK_SIMD_FOR:
11010 error_at (token->location, "break statement used with Cilk Plus for loop");
11011 break;
11013 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11014 break;
11016 case RID_CONTINUE:
11017 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11019 case 0:
11020 error_at (token->location, "continue statement not within a loop");
11021 break;
11022 case IN_CILK_SIMD_FOR:
11023 error_at (token->location,
11024 "continue statement within %<#pragma simd%> loop body");
11025 /* Fall through. */
11026 case IN_ITERATION_STMT:
11027 case IN_OMP_FOR:
11028 statement = finish_continue_stmt ();
11029 break;
11030 case IN_OMP_BLOCK:
11031 error_at (token->location, "invalid exit from OpenMP structured block");
11032 break;
11033 default:
11034 gcc_unreachable ();
11036 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11037 break;
11039 case RID_RETURN:
11041 tree expr;
11042 bool expr_non_constant_p;
11044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11046 cp_lexer_set_source_position (parser->lexer);
11047 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11048 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11050 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11051 expr = cp_parser_expression (parser);
11052 else
11053 /* If the next token is a `;', then there is no
11054 expression. */
11055 expr = NULL_TREE;
11056 /* Build the return-statement. */
11057 statement = finish_return_stmt (expr);
11058 /* Look for the final `;'. */
11059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11061 break;
11063 case RID_GOTO:
11064 if (parser->in_function_body
11065 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11067 error ("%<goto%> in %<constexpr%> function");
11068 cp_function_chain->invalid_constexpr = true;
11071 /* Create the goto-statement. */
11072 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11074 /* Issue a warning about this use of a GNU extension. */
11075 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11076 /* Consume the '*' token. */
11077 cp_lexer_consume_token (parser->lexer);
11078 /* Parse the dependent expression. */
11079 finish_goto_stmt (cp_parser_expression (parser));
11081 else
11082 finish_goto_stmt (cp_parser_identifier (parser));
11083 /* Look for the final `;'. */
11084 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11085 break;
11087 default:
11088 cp_parser_error (parser, "expected jump-statement");
11089 break;
11092 return statement;
11095 /* Parse a declaration-statement.
11097 declaration-statement:
11098 block-declaration */
11100 static void
11101 cp_parser_declaration_statement (cp_parser* parser)
11103 void *p;
11105 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11106 p = obstack_alloc (&declarator_obstack, 0);
11108 /* Parse the block-declaration. */
11109 cp_parser_block_declaration (parser, /*statement_p=*/true);
11111 /* Free any declarators allocated. */
11112 obstack_free (&declarator_obstack, p);
11115 /* Some dependent statements (like `if (cond) statement'), are
11116 implicitly in their own scope. In other words, if the statement is
11117 a single statement (as opposed to a compound-statement), it is
11118 none-the-less treated as if it were enclosed in braces. Any
11119 declarations appearing in the dependent statement are out of scope
11120 after control passes that point. This function parses a statement,
11121 but ensures that is in its own scope, even if it is not a
11122 compound-statement.
11124 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11125 is a (possibly labeled) if statement which is not enclosed in
11126 braces and has an else clause. This is used to implement
11127 -Wparentheses.
11129 Returns the new statement. */
11131 static tree
11132 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11134 tree statement;
11136 if (if_p != NULL)
11137 *if_p = false;
11139 /* Mark if () ; with a special NOP_EXPR. */
11140 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11142 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11143 cp_lexer_consume_token (parser->lexer);
11144 statement = add_stmt (build_empty_stmt (loc));
11146 /* if a compound is opened, we simply parse the statement directly. */
11147 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11148 statement = cp_parser_compound_statement (parser, NULL, false, false);
11149 /* If the token is not a `{', then we must take special action. */
11150 else
11152 /* Create a compound-statement. */
11153 statement = begin_compound_stmt (0);
11154 /* Parse the dependent-statement. */
11155 cp_parser_statement (parser, NULL_TREE, false, if_p);
11156 /* Finish the dummy compound-statement. */
11157 finish_compound_stmt (statement);
11160 /* Return the statement. */
11161 return statement;
11164 /* For some dependent statements (like `while (cond) statement'), we
11165 have already created a scope. Therefore, even if the dependent
11166 statement is a compound-statement, we do not want to create another
11167 scope. */
11169 static void
11170 cp_parser_already_scoped_statement (cp_parser* parser)
11172 /* If the token is a `{', then we must take special action. */
11173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11174 cp_parser_statement (parser, NULL_TREE, false, NULL);
11175 else
11177 /* Avoid calling cp_parser_compound_statement, so that we
11178 don't create a new scope. Do everything else by hand. */
11179 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11180 /* If the next keyword is `__label__' we have a label declaration. */
11181 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11182 cp_parser_label_declaration (parser);
11183 /* Parse an (optional) statement-seq. */
11184 cp_parser_statement_seq_opt (parser, NULL_TREE);
11185 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11189 /* Declarations [gram.dcl.dcl] */
11191 /* Parse an optional declaration-sequence.
11193 declaration-seq:
11194 declaration
11195 declaration-seq declaration */
11197 static void
11198 cp_parser_declaration_seq_opt (cp_parser* parser)
11200 while (true)
11202 cp_token *token;
11204 token = cp_lexer_peek_token (parser->lexer);
11206 if (token->type == CPP_CLOSE_BRACE
11207 || token->type == CPP_EOF
11208 || token->type == CPP_PRAGMA_EOL)
11209 break;
11211 if (token->type == CPP_SEMICOLON)
11213 /* A declaration consisting of a single semicolon is
11214 invalid. Allow it unless we're being pedantic. */
11215 cp_lexer_consume_token (parser->lexer);
11216 if (!in_system_header_at (input_location))
11217 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11218 continue;
11221 /* If we're entering or exiting a region that's implicitly
11222 extern "C", modify the lang context appropriately. */
11223 if (!parser->implicit_extern_c && token->implicit_extern_c)
11225 push_lang_context (lang_name_c);
11226 parser->implicit_extern_c = true;
11228 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11230 pop_lang_context ();
11231 parser->implicit_extern_c = false;
11234 if (token->type == CPP_PRAGMA)
11236 /* A top-level declaration can consist solely of a #pragma.
11237 A nested declaration cannot, so this is done here and not
11238 in cp_parser_declaration. (A #pragma at block scope is
11239 handled in cp_parser_statement.) */
11240 cp_parser_pragma (parser, pragma_external);
11241 continue;
11244 /* Parse the declaration itself. */
11245 cp_parser_declaration (parser);
11249 /* Parse a declaration.
11251 declaration:
11252 block-declaration
11253 function-definition
11254 template-declaration
11255 explicit-instantiation
11256 explicit-specialization
11257 linkage-specification
11258 namespace-definition
11260 GNU extension:
11262 declaration:
11263 __extension__ declaration */
11265 static void
11266 cp_parser_declaration (cp_parser* parser)
11268 cp_token token1;
11269 cp_token token2;
11270 int saved_pedantic;
11271 void *p;
11272 tree attributes = NULL_TREE;
11274 /* Check for the `__extension__' keyword. */
11275 if (cp_parser_extension_opt (parser, &saved_pedantic))
11277 /* Parse the qualified declaration. */
11278 cp_parser_declaration (parser);
11279 /* Restore the PEDANTIC flag. */
11280 pedantic = saved_pedantic;
11282 return;
11285 /* Try to figure out what kind of declaration is present. */
11286 token1 = *cp_lexer_peek_token (parser->lexer);
11288 if (token1.type != CPP_EOF)
11289 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11290 else
11292 token2.type = CPP_EOF;
11293 token2.keyword = RID_MAX;
11296 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11297 p = obstack_alloc (&declarator_obstack, 0);
11299 /* If the next token is `extern' and the following token is a string
11300 literal, then we have a linkage specification. */
11301 if (token1.keyword == RID_EXTERN
11302 && cp_parser_is_pure_string_literal (&token2))
11303 cp_parser_linkage_specification (parser);
11304 /* If the next token is `template', then we have either a template
11305 declaration, an explicit instantiation, or an explicit
11306 specialization. */
11307 else if (token1.keyword == RID_TEMPLATE)
11309 /* `template <>' indicates a template specialization. */
11310 if (token2.type == CPP_LESS
11311 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11312 cp_parser_explicit_specialization (parser);
11313 /* `template <' indicates a template declaration. */
11314 else if (token2.type == CPP_LESS)
11315 cp_parser_template_declaration (parser, /*member_p=*/false);
11316 /* Anything else must be an explicit instantiation. */
11317 else
11318 cp_parser_explicit_instantiation (parser);
11320 /* If the next token is `export', then we have a template
11321 declaration. */
11322 else if (token1.keyword == RID_EXPORT)
11323 cp_parser_template_declaration (parser, /*member_p=*/false);
11324 /* If the next token is `extern', 'static' or 'inline' and the one
11325 after that is `template', we have a GNU extended explicit
11326 instantiation directive. */
11327 else if (cp_parser_allow_gnu_extensions_p (parser)
11328 && (token1.keyword == RID_EXTERN
11329 || token1.keyword == RID_STATIC
11330 || token1.keyword == RID_INLINE)
11331 && token2.keyword == RID_TEMPLATE)
11332 cp_parser_explicit_instantiation (parser);
11333 /* If the next token is `namespace', check for a named or unnamed
11334 namespace definition. */
11335 else if (token1.keyword == RID_NAMESPACE
11336 && (/* A named namespace definition. */
11337 (token2.type == CPP_NAME
11338 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11339 != CPP_EQ))
11340 /* An unnamed namespace definition. */
11341 || token2.type == CPP_OPEN_BRACE
11342 || token2.keyword == RID_ATTRIBUTE))
11343 cp_parser_namespace_definition (parser);
11344 /* An inline (associated) namespace definition. */
11345 else if (token1.keyword == RID_INLINE
11346 && token2.keyword == RID_NAMESPACE)
11347 cp_parser_namespace_definition (parser);
11348 /* Objective-C++ declaration/definition. */
11349 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11350 cp_parser_objc_declaration (parser, NULL_TREE);
11351 else if (c_dialect_objc ()
11352 && token1.keyword == RID_ATTRIBUTE
11353 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11354 cp_parser_objc_declaration (parser, attributes);
11355 /* We must have either a block declaration or a function
11356 definition. */
11357 else
11358 /* Try to parse a block-declaration, or a function-definition. */
11359 cp_parser_block_declaration (parser, /*statement_p=*/false);
11361 /* Free any declarators allocated. */
11362 obstack_free (&declarator_obstack, p);
11365 /* Parse a block-declaration.
11367 block-declaration:
11368 simple-declaration
11369 asm-definition
11370 namespace-alias-definition
11371 using-declaration
11372 using-directive
11374 GNU Extension:
11376 block-declaration:
11377 __extension__ block-declaration
11379 C++0x Extension:
11381 block-declaration:
11382 static_assert-declaration
11384 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11385 part of a declaration-statement. */
11387 static void
11388 cp_parser_block_declaration (cp_parser *parser,
11389 bool statement_p)
11391 cp_token *token1;
11392 int saved_pedantic;
11394 /* Check for the `__extension__' keyword. */
11395 if (cp_parser_extension_opt (parser, &saved_pedantic))
11397 /* Parse the qualified declaration. */
11398 cp_parser_block_declaration (parser, statement_p);
11399 /* Restore the PEDANTIC flag. */
11400 pedantic = saved_pedantic;
11402 return;
11405 /* Peek at the next token to figure out which kind of declaration is
11406 present. */
11407 token1 = cp_lexer_peek_token (parser->lexer);
11409 /* If the next keyword is `asm', we have an asm-definition. */
11410 if (token1->keyword == RID_ASM)
11412 if (statement_p)
11413 cp_parser_commit_to_tentative_parse (parser);
11414 cp_parser_asm_definition (parser);
11416 /* If the next keyword is `namespace', we have a
11417 namespace-alias-definition. */
11418 else if (token1->keyword == RID_NAMESPACE)
11419 cp_parser_namespace_alias_definition (parser);
11420 /* If the next keyword is `using', we have a
11421 using-declaration, a using-directive, or an alias-declaration. */
11422 else if (token1->keyword == RID_USING)
11424 cp_token *token2;
11426 if (statement_p)
11427 cp_parser_commit_to_tentative_parse (parser);
11428 /* If the token after `using' is `namespace', then we have a
11429 using-directive. */
11430 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11431 if (token2->keyword == RID_NAMESPACE)
11432 cp_parser_using_directive (parser);
11433 /* If the second token after 'using' is '=', then we have an
11434 alias-declaration. */
11435 else if (cxx_dialect >= cxx11
11436 && token2->type == CPP_NAME
11437 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11438 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11439 cp_parser_alias_declaration (parser);
11440 /* Otherwise, it's a using-declaration. */
11441 else
11442 cp_parser_using_declaration (parser,
11443 /*access_declaration_p=*/false);
11445 /* If the next keyword is `__label__' we have a misplaced label
11446 declaration. */
11447 else if (token1->keyword == RID_LABEL)
11449 cp_lexer_consume_token (parser->lexer);
11450 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11451 cp_parser_skip_to_end_of_statement (parser);
11452 /* If the next token is now a `;', consume it. */
11453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11454 cp_lexer_consume_token (parser->lexer);
11456 /* If the next token is `static_assert' we have a static assertion. */
11457 else if (token1->keyword == RID_STATIC_ASSERT)
11458 cp_parser_static_assert (parser, /*member_p=*/false);
11459 /* Anything else must be a simple-declaration. */
11460 else
11461 cp_parser_simple_declaration (parser, !statement_p,
11462 /*maybe_range_for_decl*/NULL);
11465 /* Parse a simple-declaration.
11467 simple-declaration:
11468 decl-specifier-seq [opt] init-declarator-list [opt] ;
11470 init-declarator-list:
11471 init-declarator
11472 init-declarator-list , init-declarator
11474 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11475 function-definition as a simple-declaration.
11477 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11478 parsed declaration if it is an uninitialized single declarator not followed
11479 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11480 if present, will not be consumed. */
11482 static void
11483 cp_parser_simple_declaration (cp_parser* parser,
11484 bool function_definition_allowed_p,
11485 tree *maybe_range_for_decl)
11487 cp_decl_specifier_seq decl_specifiers;
11488 int declares_class_or_enum;
11489 bool saw_declarator;
11490 location_t comma_loc = UNKNOWN_LOCATION;
11491 location_t init_loc = UNKNOWN_LOCATION;
11493 if (maybe_range_for_decl)
11494 *maybe_range_for_decl = NULL_TREE;
11496 /* Defer access checks until we know what is being declared; the
11497 checks for names appearing in the decl-specifier-seq should be
11498 done as if we were in the scope of the thing being declared. */
11499 push_deferring_access_checks (dk_deferred);
11501 /* Parse the decl-specifier-seq. We have to keep track of whether
11502 or not the decl-specifier-seq declares a named class or
11503 enumeration type, since that is the only case in which the
11504 init-declarator-list is allowed to be empty.
11506 [dcl.dcl]
11508 In a simple-declaration, the optional init-declarator-list can be
11509 omitted only when declaring a class or enumeration, that is when
11510 the decl-specifier-seq contains either a class-specifier, an
11511 elaborated-type-specifier, or an enum-specifier. */
11512 cp_parser_decl_specifier_seq (parser,
11513 CP_PARSER_FLAGS_OPTIONAL,
11514 &decl_specifiers,
11515 &declares_class_or_enum);
11516 /* We no longer need to defer access checks. */
11517 stop_deferring_access_checks ();
11519 /* In a block scope, a valid declaration must always have a
11520 decl-specifier-seq. By not trying to parse declarators, we can
11521 resolve the declaration/expression ambiguity more quickly. */
11522 if (!function_definition_allowed_p
11523 && !decl_specifiers.any_specifiers_p)
11525 cp_parser_error (parser, "expected declaration");
11526 goto done;
11529 /* If the next two tokens are both identifiers, the code is
11530 erroneous. The usual cause of this situation is code like:
11532 T t;
11534 where "T" should name a type -- but does not. */
11535 if (!decl_specifiers.any_type_specifiers_p
11536 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11538 /* If parsing tentatively, we should commit; we really are
11539 looking at a declaration. */
11540 cp_parser_commit_to_tentative_parse (parser);
11541 /* Give up. */
11542 goto done;
11545 /* If we have seen at least one decl-specifier, and the next token
11546 is not a parenthesis, then we must be looking at a declaration.
11547 (After "int (" we might be looking at a functional cast.) */
11548 if (decl_specifiers.any_specifiers_p
11549 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11550 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11551 && !cp_parser_error_occurred (parser))
11552 cp_parser_commit_to_tentative_parse (parser);
11554 /* Keep going until we hit the `;' at the end of the simple
11555 declaration. */
11556 saw_declarator = false;
11557 while (cp_lexer_next_token_is_not (parser->lexer,
11558 CPP_SEMICOLON))
11560 cp_token *token;
11561 bool function_definition_p;
11562 tree decl;
11564 if (saw_declarator)
11566 /* If we are processing next declarator, comma is expected */
11567 token = cp_lexer_peek_token (parser->lexer);
11568 gcc_assert (token->type == CPP_COMMA);
11569 cp_lexer_consume_token (parser->lexer);
11570 if (maybe_range_for_decl)
11572 *maybe_range_for_decl = error_mark_node;
11573 if (comma_loc == UNKNOWN_LOCATION)
11574 comma_loc = token->location;
11577 else
11578 saw_declarator = true;
11580 /* Parse the init-declarator. */
11581 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11582 /*checks=*/NULL,
11583 function_definition_allowed_p,
11584 /*member_p=*/false,
11585 declares_class_or_enum,
11586 &function_definition_p,
11587 maybe_range_for_decl,
11588 &init_loc);
11589 /* If an error occurred while parsing tentatively, exit quickly.
11590 (That usually happens when in the body of a function; each
11591 statement is treated as a declaration-statement until proven
11592 otherwise.) */
11593 if (cp_parser_error_occurred (parser))
11594 goto done;
11595 /* Handle function definitions specially. */
11596 if (function_definition_p)
11598 /* If the next token is a `,', then we are probably
11599 processing something like:
11601 void f() {}, *p;
11603 which is erroneous. */
11604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11606 cp_token *token = cp_lexer_peek_token (parser->lexer);
11607 error_at (token->location,
11608 "mixing"
11609 " declarations and function-definitions is forbidden");
11611 /* Otherwise, we're done with the list of declarators. */
11612 else
11614 pop_deferring_access_checks ();
11615 return;
11618 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11619 *maybe_range_for_decl = decl;
11620 /* The next token should be either a `,' or a `;'. */
11621 token = cp_lexer_peek_token (parser->lexer);
11622 /* If it's a `,', there are more declarators to come. */
11623 if (token->type == CPP_COMMA)
11624 /* will be consumed next time around */;
11625 /* If it's a `;', we are done. */
11626 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11627 break;
11628 /* Anything else is an error. */
11629 else
11631 /* If we have already issued an error message we don't need
11632 to issue another one. */
11633 if (decl != error_mark_node
11634 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11635 cp_parser_error (parser, "expected %<,%> or %<;%>");
11636 /* Skip tokens until we reach the end of the statement. */
11637 cp_parser_skip_to_end_of_statement (parser);
11638 /* If the next token is now a `;', consume it. */
11639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11640 cp_lexer_consume_token (parser->lexer);
11641 goto done;
11643 /* After the first time around, a function-definition is not
11644 allowed -- even if it was OK at first. For example:
11646 int i, f() {}
11648 is not valid. */
11649 function_definition_allowed_p = false;
11652 /* Issue an error message if no declarators are present, and the
11653 decl-specifier-seq does not itself declare a class or
11654 enumeration: [dcl.dcl]/3. */
11655 if (!saw_declarator)
11657 if (cp_parser_declares_only_class_p (parser))
11659 if (!declares_class_or_enum
11660 && decl_specifiers.type
11661 && OVERLOAD_TYPE_P (decl_specifiers.type))
11662 /* Ensure an error is issued anyway when finish_decltype_type,
11663 called via cp_parser_decl_specifier_seq, returns a class or
11664 an enumeration (c++/51786). */
11665 decl_specifiers.type = NULL_TREE;
11666 shadow_tag (&decl_specifiers);
11668 /* Perform any deferred access checks. */
11669 perform_deferred_access_checks (tf_warning_or_error);
11672 /* Consume the `;'. */
11673 if (!maybe_range_for_decl)
11674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11675 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11677 if (init_loc != UNKNOWN_LOCATION)
11678 error_at (init_loc, "initializer in range-based %<for%> loop");
11679 if (comma_loc != UNKNOWN_LOCATION)
11680 error_at (comma_loc,
11681 "multiple declarations in range-based %<for%> loop");
11684 done:
11685 pop_deferring_access_checks ();
11688 /* Parse a decl-specifier-seq.
11690 decl-specifier-seq:
11691 decl-specifier-seq [opt] decl-specifier
11692 decl-specifier attribute-specifier-seq [opt] (C++11)
11694 decl-specifier:
11695 storage-class-specifier
11696 type-specifier
11697 function-specifier
11698 friend
11699 typedef
11701 GNU Extension:
11703 decl-specifier:
11704 attributes
11706 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11708 The parser flags FLAGS is used to control type-specifier parsing.
11710 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11711 flags:
11713 1: one of the decl-specifiers is an elaborated-type-specifier
11714 (i.e., a type declaration)
11715 2: one of the decl-specifiers is an enum-specifier or a
11716 class-specifier (i.e., a type definition)
11720 static void
11721 cp_parser_decl_specifier_seq (cp_parser* parser,
11722 cp_parser_flags flags,
11723 cp_decl_specifier_seq *decl_specs,
11724 int* declares_class_or_enum)
11726 bool constructor_possible_p = !parser->in_declarator_p;
11727 bool found_decl_spec = false;
11728 cp_token *start_token = NULL;
11729 cp_decl_spec ds;
11731 /* Clear DECL_SPECS. */
11732 clear_decl_specs (decl_specs);
11734 /* Assume no class or enumeration type is declared. */
11735 *declares_class_or_enum = 0;
11737 /* Keep reading specifiers until there are no more to read. */
11738 while (true)
11740 bool constructor_p;
11741 cp_token *token;
11742 ds = ds_last;
11744 /* Peek at the next token. */
11745 token = cp_lexer_peek_token (parser->lexer);
11747 /* Save the first token of the decl spec list for error
11748 reporting. */
11749 if (!start_token)
11750 start_token = token;
11751 /* Handle attributes. */
11752 if (cp_next_tokens_can_be_attribute_p (parser))
11754 /* Parse the attributes. */
11755 tree attrs = cp_parser_attributes_opt (parser);
11757 /* In a sequence of declaration specifiers, c++11 attributes
11758 appertain to the type that precede them. In that case
11759 [dcl.spec]/1 says:
11761 The attribute-specifier-seq affects the type only for
11762 the declaration it appears in, not other declarations
11763 involving the same type.
11765 But for now let's force the user to position the
11766 attribute either at the beginning of the declaration or
11767 after the declarator-id, which would clearly mean that it
11768 applies to the declarator. */
11769 if (cxx11_attribute_p (attrs))
11771 if (!found_decl_spec)
11772 /* The c++11 attribute is at the beginning of the
11773 declaration. It appertains to the entity being
11774 declared. */;
11775 else
11777 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11779 /* This is an attribute following a
11780 class-specifier. */
11781 if (decl_specs->type_definition_p)
11782 warn_misplaced_attr_for_class_type (token->location,
11783 decl_specs->type);
11784 attrs = NULL_TREE;
11786 else
11788 decl_specs->std_attributes
11789 = chainon (decl_specs->std_attributes,
11790 attrs);
11791 if (decl_specs->locations[ds_std_attribute] == 0)
11792 decl_specs->locations[ds_std_attribute] = token->location;
11794 continue;
11798 decl_specs->attributes
11799 = chainon (decl_specs->attributes,
11800 attrs);
11801 if (decl_specs->locations[ds_attribute] == 0)
11802 decl_specs->locations[ds_attribute] = token->location;
11803 continue;
11805 /* Assume we will find a decl-specifier keyword. */
11806 found_decl_spec = true;
11807 /* If the next token is an appropriate keyword, we can simply
11808 add it to the list. */
11809 switch (token->keyword)
11811 /* decl-specifier:
11812 friend
11813 constexpr */
11814 case RID_FRIEND:
11815 if (!at_class_scope_p ())
11817 error_at (token->location, "%<friend%> used outside of class");
11818 cp_lexer_purge_token (parser->lexer);
11820 else
11822 ds = ds_friend;
11823 /* Consume the token. */
11824 cp_lexer_consume_token (parser->lexer);
11826 break;
11828 case RID_CONSTEXPR:
11829 ds = ds_constexpr;
11830 cp_lexer_consume_token (parser->lexer);
11831 break;
11833 /* function-specifier:
11834 inline
11835 virtual
11836 explicit */
11837 case RID_INLINE:
11838 case RID_VIRTUAL:
11839 case RID_EXPLICIT:
11840 cp_parser_function_specifier_opt (parser, decl_specs);
11841 break;
11843 /* decl-specifier:
11844 typedef */
11845 case RID_TYPEDEF:
11846 ds = ds_typedef;
11847 /* Consume the token. */
11848 cp_lexer_consume_token (parser->lexer);
11849 /* A constructor declarator cannot appear in a typedef. */
11850 constructor_possible_p = false;
11851 /* The "typedef" keyword can only occur in a declaration; we
11852 may as well commit at this point. */
11853 cp_parser_commit_to_tentative_parse (parser);
11855 if (decl_specs->storage_class != sc_none)
11856 decl_specs->conflicting_specifiers_p = true;
11857 break;
11859 /* storage-class-specifier:
11860 auto
11861 register
11862 static
11863 extern
11864 mutable
11866 GNU Extension:
11867 thread */
11868 case RID_AUTO:
11869 if (cxx_dialect == cxx98)
11871 /* Consume the token. */
11872 cp_lexer_consume_token (parser->lexer);
11874 /* Complain about `auto' as a storage specifier, if
11875 we're complaining about C++0x compatibility. */
11876 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11877 " changes meaning in C++11; please remove it");
11879 /* Set the storage class anyway. */
11880 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11881 token);
11883 else
11884 /* C++0x auto type-specifier. */
11885 found_decl_spec = false;
11886 break;
11888 case RID_REGISTER:
11889 case RID_STATIC:
11890 case RID_EXTERN:
11891 case RID_MUTABLE:
11892 /* Consume the token. */
11893 cp_lexer_consume_token (parser->lexer);
11894 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11895 token);
11896 break;
11897 case RID_THREAD:
11898 /* Consume the token. */
11899 ds = ds_thread;
11900 cp_lexer_consume_token (parser->lexer);
11901 break;
11903 default:
11904 /* We did not yet find a decl-specifier yet. */
11905 found_decl_spec = false;
11906 break;
11909 if (found_decl_spec
11910 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11911 && token->keyword != RID_CONSTEXPR)
11912 error ("decl-specifier invalid in condition");
11914 if (ds != ds_last)
11915 set_and_check_decl_spec_loc (decl_specs, ds, token);
11917 /* Constructors are a special case. The `S' in `S()' is not a
11918 decl-specifier; it is the beginning of the declarator. */
11919 constructor_p
11920 = (!found_decl_spec
11921 && constructor_possible_p
11922 && (cp_parser_constructor_declarator_p
11923 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11925 /* If we don't have a DECL_SPEC yet, then we must be looking at
11926 a type-specifier. */
11927 if (!found_decl_spec && !constructor_p)
11929 int decl_spec_declares_class_or_enum;
11930 bool is_cv_qualifier;
11931 tree type_spec;
11933 type_spec
11934 = cp_parser_type_specifier (parser, flags,
11935 decl_specs,
11936 /*is_declaration=*/true,
11937 &decl_spec_declares_class_or_enum,
11938 &is_cv_qualifier);
11939 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11941 /* If this type-specifier referenced a user-defined type
11942 (a typedef, class-name, etc.), then we can't allow any
11943 more such type-specifiers henceforth.
11945 [dcl.spec]
11947 The longest sequence of decl-specifiers that could
11948 possibly be a type name is taken as the
11949 decl-specifier-seq of a declaration. The sequence shall
11950 be self-consistent as described below.
11952 [dcl.type]
11954 As a general rule, at most one type-specifier is allowed
11955 in the complete decl-specifier-seq of a declaration. The
11956 only exceptions are the following:
11958 -- const or volatile can be combined with any other
11959 type-specifier.
11961 -- signed or unsigned can be combined with char, long,
11962 short, or int.
11964 -- ..
11966 Example:
11968 typedef char* Pc;
11969 void g (const int Pc);
11971 Here, Pc is *not* part of the decl-specifier seq; it's
11972 the declarator. Therefore, once we see a type-specifier
11973 (other than a cv-qualifier), we forbid any additional
11974 user-defined types. We *do* still allow things like `int
11975 int' to be considered a decl-specifier-seq, and issue the
11976 error message later. */
11977 if (type_spec && !is_cv_qualifier)
11978 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11979 /* A constructor declarator cannot follow a type-specifier. */
11980 if (type_spec)
11982 constructor_possible_p = false;
11983 found_decl_spec = true;
11984 if (!is_cv_qualifier)
11985 decl_specs->any_type_specifiers_p = true;
11989 /* If we still do not have a DECL_SPEC, then there are no more
11990 decl-specifiers. */
11991 if (!found_decl_spec)
11992 break;
11994 decl_specs->any_specifiers_p = true;
11995 /* After we see one decl-specifier, further decl-specifiers are
11996 always optional. */
11997 flags |= CP_PARSER_FLAGS_OPTIONAL;
12000 /* Don't allow a friend specifier with a class definition. */
12001 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12002 && (*declares_class_or_enum & 2))
12003 error_at (decl_specs->locations[ds_friend],
12004 "class definition may not be declared a friend");
12007 /* Parse an (optional) storage-class-specifier.
12009 storage-class-specifier:
12010 auto
12011 register
12012 static
12013 extern
12014 mutable
12016 GNU Extension:
12018 storage-class-specifier:
12019 thread
12021 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12023 static tree
12024 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12026 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12028 case RID_AUTO:
12029 if (cxx_dialect != cxx98)
12030 return NULL_TREE;
12031 /* Fall through for C++98. */
12033 case RID_REGISTER:
12034 case RID_STATIC:
12035 case RID_EXTERN:
12036 case RID_MUTABLE:
12037 case RID_THREAD:
12038 /* Consume the token. */
12039 return cp_lexer_consume_token (parser->lexer)->u.value;
12041 default:
12042 return NULL_TREE;
12046 /* Parse an (optional) function-specifier.
12048 function-specifier:
12049 inline
12050 virtual
12051 explicit
12053 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12054 Updates DECL_SPECS, if it is non-NULL. */
12056 static tree
12057 cp_parser_function_specifier_opt (cp_parser* parser,
12058 cp_decl_specifier_seq *decl_specs)
12060 cp_token *token = cp_lexer_peek_token (parser->lexer);
12061 switch (token->keyword)
12063 case RID_INLINE:
12064 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12065 break;
12067 case RID_VIRTUAL:
12068 /* 14.5.2.3 [temp.mem]
12070 A member function template shall not be virtual. */
12071 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12072 error_at (token->location, "templates may not be %<virtual%>");
12073 else
12074 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12075 break;
12077 case RID_EXPLICIT:
12078 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12079 break;
12081 default:
12082 return NULL_TREE;
12085 /* Consume the token. */
12086 return cp_lexer_consume_token (parser->lexer)->u.value;
12089 /* Parse a linkage-specification.
12091 linkage-specification:
12092 extern string-literal { declaration-seq [opt] }
12093 extern string-literal declaration */
12095 static void
12096 cp_parser_linkage_specification (cp_parser* parser)
12098 tree linkage;
12100 /* Look for the `extern' keyword. */
12101 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12103 /* Look for the string-literal. */
12104 linkage = cp_parser_string_literal (parser, false, false);
12106 /* Transform the literal into an identifier. If the literal is a
12107 wide-character string, or contains embedded NULs, then we can't
12108 handle it as the user wants. */
12109 if (strlen (TREE_STRING_POINTER (linkage))
12110 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12112 cp_parser_error (parser, "invalid linkage-specification");
12113 /* Assume C++ linkage. */
12114 linkage = lang_name_cplusplus;
12116 else
12117 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12119 /* We're now using the new linkage. */
12120 push_lang_context (linkage);
12122 /* If the next token is a `{', then we're using the first
12123 production. */
12124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12126 cp_ensure_no_omp_declare_simd (parser);
12128 /* Consume the `{' token. */
12129 cp_lexer_consume_token (parser->lexer);
12130 /* Parse the declarations. */
12131 cp_parser_declaration_seq_opt (parser);
12132 /* Look for the closing `}'. */
12133 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12135 /* Otherwise, there's just one declaration. */
12136 else
12138 bool saved_in_unbraced_linkage_specification_p;
12140 saved_in_unbraced_linkage_specification_p
12141 = parser->in_unbraced_linkage_specification_p;
12142 parser->in_unbraced_linkage_specification_p = true;
12143 cp_parser_declaration (parser);
12144 parser->in_unbraced_linkage_specification_p
12145 = saved_in_unbraced_linkage_specification_p;
12148 /* We're done with the linkage-specification. */
12149 pop_lang_context ();
12152 /* Parse a static_assert-declaration.
12154 static_assert-declaration:
12155 static_assert ( constant-expression , string-literal ) ;
12157 If MEMBER_P, this static_assert is a class member. */
12159 static void
12160 cp_parser_static_assert(cp_parser *parser, bool member_p)
12162 tree condition;
12163 tree message;
12164 cp_token *token;
12165 location_t saved_loc;
12166 bool dummy;
12168 /* Peek at the `static_assert' token so we can keep track of exactly
12169 where the static assertion started. */
12170 token = cp_lexer_peek_token (parser->lexer);
12171 saved_loc = token->location;
12173 /* Look for the `static_assert' keyword. */
12174 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12175 RT_STATIC_ASSERT))
12176 return;
12178 /* We know we are in a static assertion; commit to any tentative
12179 parse. */
12180 if (cp_parser_parsing_tentatively (parser))
12181 cp_parser_commit_to_tentative_parse (parser);
12183 /* Parse the `(' starting the static assertion condition. */
12184 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12186 /* Parse the constant-expression. Allow a non-constant expression
12187 here in order to give better diagnostics in finish_static_assert. */
12188 condition =
12189 cp_parser_constant_expression (parser,
12190 /*allow_non_constant_p=*/true,
12191 /*non_constant_p=*/&dummy);
12193 /* Parse the separating `,'. */
12194 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12196 /* Parse the string-literal message. */
12197 message = cp_parser_string_literal (parser,
12198 /*translate=*/false,
12199 /*wide_ok=*/true);
12201 /* A `)' completes the static assertion. */
12202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12203 cp_parser_skip_to_closing_parenthesis (parser,
12204 /*recovering=*/true,
12205 /*or_comma=*/false,
12206 /*consume_paren=*/true);
12208 /* A semicolon terminates the declaration. */
12209 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12211 /* Complete the static assertion, which may mean either processing
12212 the static assert now or saving it for template instantiation. */
12213 finish_static_assert (condition, message, saved_loc, member_p);
12216 /* Parse the expression in decltype ( expression ). */
12218 static tree
12219 cp_parser_decltype_expr (cp_parser *parser,
12220 bool &id_expression_or_member_access_p)
12222 cp_token *id_expr_start_token;
12223 tree expr;
12225 /* First, try parsing an id-expression. */
12226 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12227 cp_parser_parse_tentatively (parser);
12228 expr = cp_parser_id_expression (parser,
12229 /*template_keyword_p=*/false,
12230 /*check_dependency_p=*/true,
12231 /*template_p=*/NULL,
12232 /*declarator_p=*/false,
12233 /*optional_p=*/false);
12235 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12237 bool non_integral_constant_expression_p = false;
12238 tree id_expression = expr;
12239 cp_id_kind idk;
12240 const char *error_msg;
12242 if (identifier_p (expr))
12243 /* Lookup the name we got back from the id-expression. */
12244 expr = cp_parser_lookup_name_simple (parser, expr,
12245 id_expr_start_token->location);
12247 if (expr
12248 && expr != error_mark_node
12249 && TREE_CODE (expr) != TYPE_DECL
12250 && (TREE_CODE (expr) != BIT_NOT_EXPR
12251 || !TYPE_P (TREE_OPERAND (expr, 0)))
12252 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12254 /* Complete lookup of the id-expression. */
12255 expr = (finish_id_expression
12256 (id_expression, expr, parser->scope, &idk,
12257 /*integral_constant_expression_p=*/false,
12258 /*allow_non_integral_constant_expression_p=*/true,
12259 &non_integral_constant_expression_p,
12260 /*template_p=*/false,
12261 /*done=*/true,
12262 /*address_p=*/false,
12263 /*template_arg_p=*/false,
12264 &error_msg,
12265 id_expr_start_token->location));
12267 if (expr == error_mark_node)
12268 /* We found an id-expression, but it was something that we
12269 should not have found. This is an error, not something
12270 we can recover from, so note that we found an
12271 id-expression and we'll recover as gracefully as
12272 possible. */
12273 id_expression_or_member_access_p = true;
12276 if (expr
12277 && expr != error_mark_node
12278 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12279 /* We have an id-expression. */
12280 id_expression_or_member_access_p = true;
12283 if (!id_expression_or_member_access_p)
12285 /* Abort the id-expression parse. */
12286 cp_parser_abort_tentative_parse (parser);
12288 /* Parsing tentatively, again. */
12289 cp_parser_parse_tentatively (parser);
12291 /* Parse a class member access. */
12292 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12293 /*cast_p=*/false, /*decltype*/true,
12294 /*member_access_only_p=*/true, NULL);
12296 if (expr
12297 && expr != error_mark_node
12298 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12299 /* We have an id-expression. */
12300 id_expression_or_member_access_p = true;
12303 if (id_expression_or_member_access_p)
12304 /* We have parsed the complete id-expression or member access. */
12305 cp_parser_parse_definitely (parser);
12306 else
12308 /* Abort our attempt to parse an id-expression or member access
12309 expression. */
12310 cp_parser_abort_tentative_parse (parser);
12312 /* Parse a full expression. */
12313 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12314 /*decltype_p=*/true);
12317 return expr;
12320 /* Parse a `decltype' type. Returns the type.
12322 simple-type-specifier:
12323 decltype ( expression )
12324 C++14 proposal:
12325 decltype ( auto ) */
12327 static tree
12328 cp_parser_decltype (cp_parser *parser)
12330 tree expr;
12331 bool id_expression_or_member_access_p = false;
12332 const char *saved_message;
12333 bool saved_integral_constant_expression_p;
12334 bool saved_non_integral_constant_expression_p;
12335 bool saved_greater_than_is_operator_p;
12336 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12338 if (start_token->type == CPP_DECLTYPE)
12340 /* Already parsed. */
12341 cp_lexer_consume_token (parser->lexer);
12342 return start_token->u.value;
12345 /* Look for the `decltype' token. */
12346 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12347 return error_mark_node;
12349 /* Parse the opening `('. */
12350 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12351 return error_mark_node;
12353 /* decltype (auto) */
12354 if (cxx_dialect >= cxx14
12355 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12357 cp_lexer_consume_token (parser->lexer);
12358 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12359 return error_mark_node;
12360 expr = make_decltype_auto ();
12361 AUTO_IS_DECLTYPE (expr) = true;
12362 goto rewrite;
12365 /* Types cannot be defined in a `decltype' expression. Save away the
12366 old message. */
12367 saved_message = parser->type_definition_forbidden_message;
12369 /* And create the new one. */
12370 parser->type_definition_forbidden_message
12371 = G_("types may not be defined in %<decltype%> expressions");
12373 /* The restrictions on constant-expressions do not apply inside
12374 decltype expressions. */
12375 saved_integral_constant_expression_p
12376 = parser->integral_constant_expression_p;
12377 saved_non_integral_constant_expression_p
12378 = parser->non_integral_constant_expression_p;
12379 parser->integral_constant_expression_p = false;
12381 /* Within a parenthesized expression, a `>' token is always
12382 the greater-than operator. */
12383 saved_greater_than_is_operator_p
12384 = parser->greater_than_is_operator_p;
12385 parser->greater_than_is_operator_p = true;
12387 /* Do not actually evaluate the expression. */
12388 ++cp_unevaluated_operand;
12390 /* Do not warn about problems with the expression. */
12391 ++c_inhibit_evaluation_warnings;
12393 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12395 /* Go back to evaluating expressions. */
12396 --cp_unevaluated_operand;
12397 --c_inhibit_evaluation_warnings;
12399 /* The `>' token might be the end of a template-id or
12400 template-parameter-list now. */
12401 parser->greater_than_is_operator_p
12402 = saved_greater_than_is_operator_p;
12404 /* Restore the old message and the integral constant expression
12405 flags. */
12406 parser->type_definition_forbidden_message = saved_message;
12407 parser->integral_constant_expression_p
12408 = saved_integral_constant_expression_p;
12409 parser->non_integral_constant_expression_p
12410 = saved_non_integral_constant_expression_p;
12412 /* Parse to the closing `)'. */
12413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12415 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12416 /*consume_paren=*/true);
12417 return error_mark_node;
12420 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12421 tf_warning_or_error);
12423 rewrite:
12424 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12425 it again. */
12426 start_token->type = CPP_DECLTYPE;
12427 start_token->u.value = expr;
12428 start_token->keyword = RID_MAX;
12429 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12431 return expr;
12434 /* Special member functions [gram.special] */
12436 /* Parse a conversion-function-id.
12438 conversion-function-id:
12439 operator conversion-type-id
12441 Returns an IDENTIFIER_NODE representing the operator. */
12443 static tree
12444 cp_parser_conversion_function_id (cp_parser* parser)
12446 tree type;
12447 tree saved_scope;
12448 tree saved_qualifying_scope;
12449 tree saved_object_scope;
12450 tree pushed_scope = NULL_TREE;
12452 /* Look for the `operator' token. */
12453 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12454 return error_mark_node;
12455 /* When we parse the conversion-type-id, the current scope will be
12456 reset. However, we need that information in able to look up the
12457 conversion function later, so we save it here. */
12458 saved_scope = parser->scope;
12459 saved_qualifying_scope = parser->qualifying_scope;
12460 saved_object_scope = parser->object_scope;
12461 /* We must enter the scope of the class so that the names of
12462 entities declared within the class are available in the
12463 conversion-type-id. For example, consider:
12465 struct S {
12466 typedef int I;
12467 operator I();
12470 S::operator I() { ... }
12472 In order to see that `I' is a type-name in the definition, we
12473 must be in the scope of `S'. */
12474 if (saved_scope)
12475 pushed_scope = push_scope (saved_scope);
12476 /* Parse the conversion-type-id. */
12477 type = cp_parser_conversion_type_id (parser);
12478 /* Leave the scope of the class, if any. */
12479 if (pushed_scope)
12480 pop_scope (pushed_scope);
12481 /* Restore the saved scope. */
12482 parser->scope = saved_scope;
12483 parser->qualifying_scope = saved_qualifying_scope;
12484 parser->object_scope = saved_object_scope;
12485 /* If the TYPE is invalid, indicate failure. */
12486 if (type == error_mark_node)
12487 return error_mark_node;
12488 return mangle_conv_op_name_for_type (type);
12491 /* Parse a conversion-type-id:
12493 conversion-type-id:
12494 type-specifier-seq conversion-declarator [opt]
12496 Returns the TYPE specified. */
12498 static tree
12499 cp_parser_conversion_type_id (cp_parser* parser)
12501 tree attributes;
12502 cp_decl_specifier_seq type_specifiers;
12503 cp_declarator *declarator;
12504 tree type_specified;
12505 const char *saved_message;
12507 /* Parse the attributes. */
12508 attributes = cp_parser_attributes_opt (parser);
12510 saved_message = parser->type_definition_forbidden_message;
12511 parser->type_definition_forbidden_message
12512 = G_("types may not be defined in a conversion-type-id");
12514 /* Parse the type-specifiers. */
12515 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12516 /*is_trailing_return=*/false,
12517 &type_specifiers);
12519 parser->type_definition_forbidden_message = saved_message;
12521 /* If that didn't work, stop. */
12522 if (type_specifiers.type == error_mark_node)
12523 return error_mark_node;
12524 /* Parse the conversion-declarator. */
12525 declarator = cp_parser_conversion_declarator_opt (parser);
12527 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12528 /*initialized=*/0, &attributes);
12529 if (attributes)
12530 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12532 /* Don't give this error when parsing tentatively. This happens to
12533 work because we always parse this definitively once. */
12534 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12535 && type_uses_auto (type_specified))
12537 if (cxx_dialect < cxx14)
12539 error ("invalid use of %<auto%> in conversion operator");
12540 return error_mark_node;
12542 else if (template_parm_scope_p ())
12543 warning (0, "use of %<auto%> in member template "
12544 "conversion operator can never be deduced");
12547 return type_specified;
12550 /* Parse an (optional) conversion-declarator.
12552 conversion-declarator:
12553 ptr-operator conversion-declarator [opt]
12557 static cp_declarator *
12558 cp_parser_conversion_declarator_opt (cp_parser* parser)
12560 enum tree_code code;
12561 tree class_type, std_attributes = NULL_TREE;
12562 cp_cv_quals cv_quals;
12564 /* We don't know if there's a ptr-operator next, or not. */
12565 cp_parser_parse_tentatively (parser);
12566 /* Try the ptr-operator. */
12567 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12568 &std_attributes);
12569 /* If it worked, look for more conversion-declarators. */
12570 if (cp_parser_parse_definitely (parser))
12572 cp_declarator *declarator;
12574 /* Parse another optional declarator. */
12575 declarator = cp_parser_conversion_declarator_opt (parser);
12577 declarator = cp_parser_make_indirect_declarator
12578 (code, class_type, cv_quals, declarator, std_attributes);
12580 return declarator;
12583 return NULL;
12586 /* Parse an (optional) ctor-initializer.
12588 ctor-initializer:
12589 : mem-initializer-list
12591 Returns TRUE iff the ctor-initializer was actually present. */
12593 static bool
12594 cp_parser_ctor_initializer_opt (cp_parser* parser)
12596 /* If the next token is not a `:', then there is no
12597 ctor-initializer. */
12598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12600 /* Do default initialization of any bases and members. */
12601 if (DECL_CONSTRUCTOR_P (current_function_decl))
12602 finish_mem_initializers (NULL_TREE);
12604 return false;
12607 /* Consume the `:' token. */
12608 cp_lexer_consume_token (parser->lexer);
12609 /* And the mem-initializer-list. */
12610 cp_parser_mem_initializer_list (parser);
12612 return true;
12615 /* Parse a mem-initializer-list.
12617 mem-initializer-list:
12618 mem-initializer ... [opt]
12619 mem-initializer ... [opt] , mem-initializer-list */
12621 static void
12622 cp_parser_mem_initializer_list (cp_parser* parser)
12624 tree mem_initializer_list = NULL_TREE;
12625 tree target_ctor = error_mark_node;
12626 cp_token *token = cp_lexer_peek_token (parser->lexer);
12628 /* Let the semantic analysis code know that we are starting the
12629 mem-initializer-list. */
12630 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12631 error_at (token->location,
12632 "only constructors take member initializers");
12634 /* Loop through the list. */
12635 while (true)
12637 tree mem_initializer;
12639 token = cp_lexer_peek_token (parser->lexer);
12640 /* Parse the mem-initializer. */
12641 mem_initializer = cp_parser_mem_initializer (parser);
12642 /* If the next token is a `...', we're expanding member initializers. */
12643 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12645 /* Consume the `...'. */
12646 cp_lexer_consume_token (parser->lexer);
12648 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12649 can be expanded but members cannot. */
12650 if (mem_initializer != error_mark_node
12651 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12653 error_at (token->location,
12654 "cannot expand initializer for member %<%D%>",
12655 TREE_PURPOSE (mem_initializer));
12656 mem_initializer = error_mark_node;
12659 /* Construct the pack expansion type. */
12660 if (mem_initializer != error_mark_node)
12661 mem_initializer = make_pack_expansion (mem_initializer);
12663 if (target_ctor != error_mark_node
12664 && mem_initializer != error_mark_node)
12666 error ("mem-initializer for %qD follows constructor delegation",
12667 TREE_PURPOSE (mem_initializer));
12668 mem_initializer = error_mark_node;
12670 /* Look for a target constructor. */
12671 if (mem_initializer != error_mark_node
12672 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12673 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12675 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12676 if (mem_initializer_list)
12678 error ("constructor delegation follows mem-initializer for %qD",
12679 TREE_PURPOSE (mem_initializer_list));
12680 mem_initializer = error_mark_node;
12682 target_ctor = mem_initializer;
12684 /* Add it to the list, unless it was erroneous. */
12685 if (mem_initializer != error_mark_node)
12687 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12688 mem_initializer_list = mem_initializer;
12690 /* If the next token is not a `,', we're done. */
12691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12692 break;
12693 /* Consume the `,' token. */
12694 cp_lexer_consume_token (parser->lexer);
12697 /* Perform semantic analysis. */
12698 if (DECL_CONSTRUCTOR_P (current_function_decl))
12699 finish_mem_initializers (mem_initializer_list);
12702 /* Parse a mem-initializer.
12704 mem-initializer:
12705 mem-initializer-id ( expression-list [opt] )
12706 mem-initializer-id braced-init-list
12708 GNU extension:
12710 mem-initializer:
12711 ( expression-list [opt] )
12713 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12714 class) or FIELD_DECL (for a non-static data member) to initialize;
12715 the TREE_VALUE is the expression-list. An empty initialization
12716 list is represented by void_list_node. */
12718 static tree
12719 cp_parser_mem_initializer (cp_parser* parser)
12721 tree mem_initializer_id;
12722 tree expression_list;
12723 tree member;
12724 cp_token *token = cp_lexer_peek_token (parser->lexer);
12726 /* Find out what is being initialized. */
12727 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12729 permerror (token->location,
12730 "anachronistic old-style base class initializer");
12731 mem_initializer_id = NULL_TREE;
12733 else
12735 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12736 if (mem_initializer_id == error_mark_node)
12737 return mem_initializer_id;
12739 member = expand_member_init (mem_initializer_id);
12740 if (member && !DECL_P (member))
12741 in_base_initializer = 1;
12743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12745 bool expr_non_constant_p;
12746 cp_lexer_set_source_position (parser->lexer);
12747 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12748 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12749 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12750 expression_list = build_tree_list (NULL_TREE, expression_list);
12752 else
12754 vec<tree, va_gc> *vec;
12755 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12756 /*cast_p=*/false,
12757 /*allow_expansion_p=*/true,
12758 /*non_constant_p=*/NULL);
12759 if (vec == NULL)
12760 return error_mark_node;
12761 expression_list = build_tree_list_vec (vec);
12762 release_tree_vector (vec);
12765 if (expression_list == error_mark_node)
12766 return error_mark_node;
12767 if (!expression_list)
12768 expression_list = void_type_node;
12770 in_base_initializer = 0;
12772 return member ? build_tree_list (member, expression_list) : error_mark_node;
12775 /* Parse a mem-initializer-id.
12777 mem-initializer-id:
12778 :: [opt] nested-name-specifier [opt] class-name
12779 identifier
12781 Returns a TYPE indicating the class to be initializer for the first
12782 production. Returns an IDENTIFIER_NODE indicating the data member
12783 to be initialized for the second production. */
12785 static tree
12786 cp_parser_mem_initializer_id (cp_parser* parser)
12788 bool global_scope_p;
12789 bool nested_name_specifier_p;
12790 bool template_p = false;
12791 tree id;
12793 cp_token *token = cp_lexer_peek_token (parser->lexer);
12795 /* `typename' is not allowed in this context ([temp.res]). */
12796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12798 error_at (token->location,
12799 "keyword %<typename%> not allowed in this context (a qualified "
12800 "member initializer is implicitly a type)");
12801 cp_lexer_consume_token (parser->lexer);
12803 /* Look for the optional `::' operator. */
12804 global_scope_p
12805 = (cp_parser_global_scope_opt (parser,
12806 /*current_scope_valid_p=*/false)
12807 != NULL_TREE);
12808 /* Look for the optional nested-name-specifier. The simplest way to
12809 implement:
12811 [temp.res]
12813 The keyword `typename' is not permitted in a base-specifier or
12814 mem-initializer; in these contexts a qualified name that
12815 depends on a template-parameter is implicitly assumed to be a
12816 type name.
12818 is to assume that we have seen the `typename' keyword at this
12819 point. */
12820 nested_name_specifier_p
12821 = (cp_parser_nested_name_specifier_opt (parser,
12822 /*typename_keyword_p=*/true,
12823 /*check_dependency_p=*/true,
12824 /*type_p=*/true,
12825 /*is_declaration=*/true)
12826 != NULL_TREE);
12827 if (nested_name_specifier_p)
12828 template_p = cp_parser_optional_template_keyword (parser);
12829 /* If there is a `::' operator or a nested-name-specifier, then we
12830 are definitely looking for a class-name. */
12831 if (global_scope_p || nested_name_specifier_p)
12832 return cp_parser_class_name (parser,
12833 /*typename_keyword_p=*/true,
12834 /*template_keyword_p=*/template_p,
12835 typename_type,
12836 /*check_dependency_p=*/true,
12837 /*class_head_p=*/false,
12838 /*is_declaration=*/true);
12839 /* Otherwise, we could also be looking for an ordinary identifier. */
12840 cp_parser_parse_tentatively (parser);
12841 /* Try a class-name. */
12842 id = cp_parser_class_name (parser,
12843 /*typename_keyword_p=*/true,
12844 /*template_keyword_p=*/false,
12845 none_type,
12846 /*check_dependency_p=*/true,
12847 /*class_head_p=*/false,
12848 /*is_declaration=*/true);
12849 /* If we found one, we're done. */
12850 if (cp_parser_parse_definitely (parser))
12851 return id;
12852 /* Otherwise, look for an ordinary identifier. */
12853 return cp_parser_identifier (parser);
12856 /* Overloading [gram.over] */
12858 /* Parse an operator-function-id.
12860 operator-function-id:
12861 operator operator
12863 Returns an IDENTIFIER_NODE for the operator which is a
12864 human-readable spelling of the identifier, e.g., `operator +'. */
12866 static tree
12867 cp_parser_operator_function_id (cp_parser* parser)
12869 /* Look for the `operator' keyword. */
12870 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12871 return error_mark_node;
12872 /* And then the name of the operator itself. */
12873 return cp_parser_operator (parser);
12876 /* Return an identifier node for a user-defined literal operator.
12877 The suffix identifier is chained to the operator name identifier. */
12879 static tree
12880 cp_literal_operator_id (const char* name)
12882 tree identifier;
12883 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12884 + strlen (name) + 10);
12885 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12886 identifier = get_identifier (buffer);
12888 return identifier;
12891 /* Parse an operator.
12893 operator:
12894 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12895 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12896 || ++ -- , ->* -> () []
12898 GNU Extensions:
12900 operator:
12901 <? >? <?= >?=
12903 Returns an IDENTIFIER_NODE for the operator which is a
12904 human-readable spelling of the identifier, e.g., `operator +'. */
12906 static tree
12907 cp_parser_operator (cp_parser* parser)
12909 tree id = NULL_TREE;
12910 cp_token *token;
12911 bool utf8 = false;
12913 /* Peek at the next token. */
12914 token = cp_lexer_peek_token (parser->lexer);
12915 /* Figure out which operator we have. */
12916 switch (token->type)
12918 case CPP_KEYWORD:
12920 enum tree_code op;
12922 /* The keyword should be either `new' or `delete'. */
12923 if (token->keyword == RID_NEW)
12924 op = NEW_EXPR;
12925 else if (token->keyword == RID_DELETE)
12926 op = DELETE_EXPR;
12927 else
12928 break;
12930 /* Consume the `new' or `delete' token. */
12931 cp_lexer_consume_token (parser->lexer);
12933 /* Peek at the next token. */
12934 token = cp_lexer_peek_token (parser->lexer);
12935 /* If it's a `[' token then this is the array variant of the
12936 operator. */
12937 if (token->type == CPP_OPEN_SQUARE)
12939 /* Consume the `[' token. */
12940 cp_lexer_consume_token (parser->lexer);
12941 /* Look for the `]' token. */
12942 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12943 id = ansi_opname (op == NEW_EXPR
12944 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12946 /* Otherwise, we have the non-array variant. */
12947 else
12948 id = ansi_opname (op);
12950 return id;
12953 case CPP_PLUS:
12954 id = ansi_opname (PLUS_EXPR);
12955 break;
12957 case CPP_MINUS:
12958 id = ansi_opname (MINUS_EXPR);
12959 break;
12961 case CPP_MULT:
12962 id = ansi_opname (MULT_EXPR);
12963 break;
12965 case CPP_DIV:
12966 id = ansi_opname (TRUNC_DIV_EXPR);
12967 break;
12969 case CPP_MOD:
12970 id = ansi_opname (TRUNC_MOD_EXPR);
12971 break;
12973 case CPP_XOR:
12974 id = ansi_opname (BIT_XOR_EXPR);
12975 break;
12977 case CPP_AND:
12978 id = ansi_opname (BIT_AND_EXPR);
12979 break;
12981 case CPP_OR:
12982 id = ansi_opname (BIT_IOR_EXPR);
12983 break;
12985 case CPP_COMPL:
12986 id = ansi_opname (BIT_NOT_EXPR);
12987 break;
12989 case CPP_NOT:
12990 id = ansi_opname (TRUTH_NOT_EXPR);
12991 break;
12993 case CPP_EQ:
12994 id = ansi_assopname (NOP_EXPR);
12995 break;
12997 case CPP_LESS:
12998 id = ansi_opname (LT_EXPR);
12999 break;
13001 case CPP_GREATER:
13002 id = ansi_opname (GT_EXPR);
13003 break;
13005 case CPP_PLUS_EQ:
13006 id = ansi_assopname (PLUS_EXPR);
13007 break;
13009 case CPP_MINUS_EQ:
13010 id = ansi_assopname (MINUS_EXPR);
13011 break;
13013 case CPP_MULT_EQ:
13014 id = ansi_assopname (MULT_EXPR);
13015 break;
13017 case CPP_DIV_EQ:
13018 id = ansi_assopname (TRUNC_DIV_EXPR);
13019 break;
13021 case CPP_MOD_EQ:
13022 id = ansi_assopname (TRUNC_MOD_EXPR);
13023 break;
13025 case CPP_XOR_EQ:
13026 id = ansi_assopname (BIT_XOR_EXPR);
13027 break;
13029 case CPP_AND_EQ:
13030 id = ansi_assopname (BIT_AND_EXPR);
13031 break;
13033 case CPP_OR_EQ:
13034 id = ansi_assopname (BIT_IOR_EXPR);
13035 break;
13037 case CPP_LSHIFT:
13038 id = ansi_opname (LSHIFT_EXPR);
13039 break;
13041 case CPP_RSHIFT:
13042 id = ansi_opname (RSHIFT_EXPR);
13043 break;
13045 case CPP_LSHIFT_EQ:
13046 id = ansi_assopname (LSHIFT_EXPR);
13047 break;
13049 case CPP_RSHIFT_EQ:
13050 id = ansi_assopname (RSHIFT_EXPR);
13051 break;
13053 case CPP_EQ_EQ:
13054 id = ansi_opname (EQ_EXPR);
13055 break;
13057 case CPP_NOT_EQ:
13058 id = ansi_opname (NE_EXPR);
13059 break;
13061 case CPP_LESS_EQ:
13062 id = ansi_opname (LE_EXPR);
13063 break;
13065 case CPP_GREATER_EQ:
13066 id = ansi_opname (GE_EXPR);
13067 break;
13069 case CPP_AND_AND:
13070 id = ansi_opname (TRUTH_ANDIF_EXPR);
13071 break;
13073 case CPP_OR_OR:
13074 id = ansi_opname (TRUTH_ORIF_EXPR);
13075 break;
13077 case CPP_PLUS_PLUS:
13078 id = ansi_opname (POSTINCREMENT_EXPR);
13079 break;
13081 case CPP_MINUS_MINUS:
13082 id = ansi_opname (PREDECREMENT_EXPR);
13083 break;
13085 case CPP_COMMA:
13086 id = ansi_opname (COMPOUND_EXPR);
13087 break;
13089 case CPP_DEREF_STAR:
13090 id = ansi_opname (MEMBER_REF);
13091 break;
13093 case CPP_DEREF:
13094 id = ansi_opname (COMPONENT_REF);
13095 break;
13097 case CPP_OPEN_PAREN:
13098 /* Consume the `('. */
13099 cp_lexer_consume_token (parser->lexer);
13100 /* Look for the matching `)'. */
13101 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13102 return ansi_opname (CALL_EXPR);
13104 case CPP_OPEN_SQUARE:
13105 /* Consume the `['. */
13106 cp_lexer_consume_token (parser->lexer);
13107 /* Look for the matching `]'. */
13108 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13109 return ansi_opname (ARRAY_REF);
13111 case CPP_UTF8STRING:
13112 case CPP_UTF8STRING_USERDEF:
13113 utf8 = true;
13114 case CPP_STRING:
13115 case CPP_WSTRING:
13116 case CPP_STRING16:
13117 case CPP_STRING32:
13118 case CPP_STRING_USERDEF:
13119 case CPP_WSTRING_USERDEF:
13120 case CPP_STRING16_USERDEF:
13121 case CPP_STRING32_USERDEF:
13123 tree str, string_tree;
13124 int sz, len;
13126 if (cxx_dialect == cxx98)
13127 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13129 /* Consume the string. */
13130 str = cp_parser_string_literal (parser, /*translate=*/true,
13131 /*wide_ok=*/true, /*lookup_udlit=*/false);
13132 if (str == error_mark_node)
13133 return error_mark_node;
13134 else if (TREE_CODE (str) == USERDEF_LITERAL)
13136 string_tree = USERDEF_LITERAL_VALUE (str);
13137 id = USERDEF_LITERAL_SUFFIX_ID (str);
13139 else
13141 string_tree = str;
13142 /* Look for the suffix identifier. */
13143 token = cp_lexer_peek_token (parser->lexer);
13144 if (token->type == CPP_NAME)
13145 id = cp_parser_identifier (parser);
13146 else if (token->type == CPP_KEYWORD)
13148 error ("unexpected keyword;"
13149 " remove space between quotes and suffix identifier");
13150 return error_mark_node;
13152 else
13154 error ("expected suffix identifier");
13155 return error_mark_node;
13158 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13159 (TREE_TYPE (TREE_TYPE (string_tree))));
13160 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13161 if (len != 0)
13163 error ("expected empty string after %<operator%> keyword");
13164 return error_mark_node;
13166 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13167 != char_type_node)
13169 error ("invalid encoding prefix in literal operator");
13170 return error_mark_node;
13172 if (id != error_mark_node)
13174 const char *name = IDENTIFIER_POINTER (id);
13175 id = cp_literal_operator_id (name);
13177 return id;
13180 default:
13181 /* Anything else is an error. */
13182 break;
13185 /* If we have selected an identifier, we need to consume the
13186 operator token. */
13187 if (id)
13188 cp_lexer_consume_token (parser->lexer);
13189 /* Otherwise, no valid operator name was present. */
13190 else
13192 cp_parser_error (parser, "expected operator");
13193 id = error_mark_node;
13196 return id;
13199 /* Parse a template-declaration.
13201 template-declaration:
13202 export [opt] template < template-parameter-list > declaration
13204 If MEMBER_P is TRUE, this template-declaration occurs within a
13205 class-specifier.
13207 The grammar rule given by the standard isn't correct. What
13208 is really meant is:
13210 template-declaration:
13211 export [opt] template-parameter-list-seq
13212 decl-specifier-seq [opt] init-declarator [opt] ;
13213 export [opt] template-parameter-list-seq
13214 function-definition
13216 template-parameter-list-seq:
13217 template-parameter-list-seq [opt]
13218 template < template-parameter-list > */
13220 static void
13221 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13223 /* Check for `export'. */
13224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13226 /* Consume the `export' token. */
13227 cp_lexer_consume_token (parser->lexer);
13228 /* Warn that we do not support `export'. */
13229 warning (0, "keyword %<export%> not implemented, and will be ignored");
13232 cp_parser_template_declaration_after_export (parser, member_p);
13235 /* Parse a template-parameter-list.
13237 template-parameter-list:
13238 template-parameter
13239 template-parameter-list , template-parameter
13241 Returns a TREE_LIST. Each node represents a template parameter.
13242 The nodes are connected via their TREE_CHAINs. */
13244 static tree
13245 cp_parser_template_parameter_list (cp_parser* parser)
13247 tree parameter_list = NULL_TREE;
13249 begin_template_parm_list ();
13251 /* The loop below parses the template parms. We first need to know
13252 the total number of template parms to be able to compute proper
13253 canonical types of each dependent type. So after the loop, when
13254 we know the total number of template parms,
13255 end_template_parm_list computes the proper canonical types and
13256 fixes up the dependent types accordingly. */
13257 while (true)
13259 tree parameter;
13260 bool is_non_type;
13261 bool is_parameter_pack;
13262 location_t parm_loc;
13264 /* Parse the template-parameter. */
13265 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13266 parameter = cp_parser_template_parameter (parser,
13267 &is_non_type,
13268 &is_parameter_pack);
13269 /* Add it to the list. */
13270 if (parameter != error_mark_node)
13271 parameter_list = process_template_parm (parameter_list,
13272 parm_loc,
13273 parameter,
13274 is_non_type,
13275 is_parameter_pack);
13276 else
13278 tree err_parm = build_tree_list (parameter, parameter);
13279 parameter_list = chainon (parameter_list, err_parm);
13282 /* If the next token is not a `,', we're done. */
13283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13284 break;
13285 /* Otherwise, consume the `,' token. */
13286 cp_lexer_consume_token (parser->lexer);
13289 return end_template_parm_list (parameter_list);
13292 /* Parse a template-parameter.
13294 template-parameter:
13295 type-parameter
13296 parameter-declaration
13298 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13299 the parameter. The TREE_PURPOSE is the default value, if any.
13300 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13301 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13302 set to true iff this parameter is a parameter pack. */
13304 static tree
13305 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13306 bool *is_parameter_pack)
13308 cp_token *token;
13309 cp_parameter_declarator *parameter_declarator;
13310 cp_declarator *id_declarator;
13311 tree parm;
13313 /* Assume it is a type parameter or a template parameter. */
13314 *is_non_type = false;
13315 /* Assume it not a parameter pack. */
13316 *is_parameter_pack = false;
13317 /* Peek at the next token. */
13318 token = cp_lexer_peek_token (parser->lexer);
13319 /* If it is `class' or `template', we have a type-parameter. */
13320 if (token->keyword == RID_TEMPLATE)
13321 return cp_parser_type_parameter (parser, is_parameter_pack);
13322 /* If it is `class' or `typename' we do not know yet whether it is a
13323 type parameter or a non-type parameter. Consider:
13325 template <typename T, typename T::X X> ...
13329 template <class C, class D*> ...
13331 Here, the first parameter is a type parameter, and the second is
13332 a non-type parameter. We can tell by looking at the token after
13333 the identifier -- if it is a `,', `=', or `>' then we have a type
13334 parameter. */
13335 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13337 /* Peek at the token after `class' or `typename'. */
13338 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13339 /* If it's an ellipsis, we have a template type parameter
13340 pack. */
13341 if (token->type == CPP_ELLIPSIS)
13342 return cp_parser_type_parameter (parser, is_parameter_pack);
13343 /* If it's an identifier, skip it. */
13344 if (token->type == CPP_NAME)
13345 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13346 /* Now, see if the token looks like the end of a template
13347 parameter. */
13348 if (token->type == CPP_COMMA
13349 || token->type == CPP_EQ
13350 || token->type == CPP_GREATER)
13351 return cp_parser_type_parameter (parser, is_parameter_pack);
13354 /* Otherwise, it is a non-type parameter.
13356 [temp.param]
13358 When parsing a default template-argument for a non-type
13359 template-parameter, the first non-nested `>' is taken as the end
13360 of the template parameter-list rather than a greater-than
13361 operator. */
13362 *is_non_type = true;
13363 parameter_declarator
13364 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13365 /*parenthesized_p=*/NULL);
13367 if (!parameter_declarator)
13368 return error_mark_node;
13370 /* If the parameter declaration is marked as a parameter pack, set
13371 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13372 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13373 grokdeclarator. */
13374 if (parameter_declarator->declarator
13375 && parameter_declarator->declarator->parameter_pack_p)
13377 *is_parameter_pack = true;
13378 parameter_declarator->declarator->parameter_pack_p = false;
13381 if (parameter_declarator->default_argument)
13383 /* Can happen in some cases of erroneous input (c++/34892). */
13384 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13385 /* Consume the `...' for better error recovery. */
13386 cp_lexer_consume_token (parser->lexer);
13388 /* If the next token is an ellipsis, and we don't already have it
13389 marked as a parameter pack, then we have a parameter pack (that
13390 has no declarator). */
13391 else if (!*is_parameter_pack
13392 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13393 && (declarator_can_be_parameter_pack
13394 (parameter_declarator->declarator)))
13396 /* Consume the `...'. */
13397 cp_lexer_consume_token (parser->lexer);
13398 maybe_warn_variadic_templates ();
13400 *is_parameter_pack = true;
13402 /* We might end up with a pack expansion as the type of the non-type
13403 template parameter, in which case this is a non-type template
13404 parameter pack. */
13405 else if (parameter_declarator->decl_specifiers.type
13406 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13408 *is_parameter_pack = true;
13409 parameter_declarator->decl_specifiers.type =
13410 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13413 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13415 /* Parameter packs cannot have default arguments. However, a
13416 user may try to do so, so we'll parse them and give an
13417 appropriate diagnostic here. */
13419 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13421 /* Find the name of the parameter pack. */
13422 id_declarator = parameter_declarator->declarator;
13423 while (id_declarator && id_declarator->kind != cdk_id)
13424 id_declarator = id_declarator->declarator;
13426 if (id_declarator && id_declarator->kind == cdk_id)
13427 error_at (start_token->location,
13428 "template parameter pack %qD cannot have a default argument",
13429 id_declarator->u.id.unqualified_name);
13430 else
13431 error_at (start_token->location,
13432 "template parameter pack cannot have a default argument");
13434 /* Parse the default argument, but throw away the result. */
13435 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13438 parm = grokdeclarator (parameter_declarator->declarator,
13439 &parameter_declarator->decl_specifiers,
13440 TPARM, /*initialized=*/0,
13441 /*attrlist=*/NULL);
13442 if (parm == error_mark_node)
13443 return error_mark_node;
13445 return build_tree_list (parameter_declarator->default_argument, parm);
13448 /* Parse a type-parameter.
13450 type-parameter:
13451 class identifier [opt]
13452 class identifier [opt] = type-id
13453 typename identifier [opt]
13454 typename identifier [opt] = type-id
13455 template < template-parameter-list > class identifier [opt]
13456 template < template-parameter-list > class identifier [opt]
13457 = id-expression
13459 GNU Extension (variadic templates):
13461 type-parameter:
13462 class ... identifier [opt]
13463 typename ... identifier [opt]
13465 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13466 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13467 the declaration of the parameter.
13469 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13471 static tree
13472 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13474 cp_token *token;
13475 tree parameter;
13477 /* Look for a keyword to tell us what kind of parameter this is. */
13478 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13479 if (!token)
13480 return error_mark_node;
13482 switch (token->keyword)
13484 case RID_CLASS:
13485 case RID_TYPENAME:
13487 tree identifier;
13488 tree default_argument;
13490 /* If the next token is an ellipsis, we have a template
13491 argument pack. */
13492 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13494 /* Consume the `...' token. */
13495 cp_lexer_consume_token (parser->lexer);
13496 maybe_warn_variadic_templates ();
13498 *is_parameter_pack = true;
13501 /* If the next token is an identifier, then it names the
13502 parameter. */
13503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13504 identifier = cp_parser_identifier (parser);
13505 else
13506 identifier = NULL_TREE;
13508 /* Create the parameter. */
13509 parameter = finish_template_type_parm (class_type_node, identifier);
13511 /* If the next token is an `=', we have a default argument. */
13512 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13514 /* Consume the `=' token. */
13515 cp_lexer_consume_token (parser->lexer);
13516 /* Parse the default-argument. */
13517 push_deferring_access_checks (dk_no_deferred);
13518 default_argument = cp_parser_type_id (parser);
13520 /* Template parameter packs cannot have default
13521 arguments. */
13522 if (*is_parameter_pack)
13524 if (identifier)
13525 error_at (token->location,
13526 "template parameter pack %qD cannot have a "
13527 "default argument", identifier);
13528 else
13529 error_at (token->location,
13530 "template parameter packs cannot have "
13531 "default arguments");
13532 default_argument = NULL_TREE;
13534 else if (check_for_bare_parameter_packs (default_argument))
13535 default_argument = error_mark_node;
13536 pop_deferring_access_checks ();
13538 else
13539 default_argument = NULL_TREE;
13541 /* Create the combined representation of the parameter and the
13542 default argument. */
13543 parameter = build_tree_list (default_argument, parameter);
13545 break;
13547 case RID_TEMPLATE:
13549 tree identifier;
13550 tree default_argument;
13552 /* Look for the `<'. */
13553 cp_parser_require (parser, CPP_LESS, RT_LESS);
13554 /* Parse the template-parameter-list. */
13555 cp_parser_template_parameter_list (parser);
13556 /* Look for the `>'. */
13557 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13558 /* Look for the `class' or 'typename' keywords. */
13559 cp_parser_type_parameter_key (parser);
13560 /* If the next token is an ellipsis, we have a template
13561 argument pack. */
13562 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13564 /* Consume the `...' token. */
13565 cp_lexer_consume_token (parser->lexer);
13566 maybe_warn_variadic_templates ();
13568 *is_parameter_pack = true;
13570 /* If the next token is an `=', then there is a
13571 default-argument. If the next token is a `>', we are at
13572 the end of the parameter-list. If the next token is a `,',
13573 then we are at the end of this parameter. */
13574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13575 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13576 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13578 identifier = cp_parser_identifier (parser);
13579 /* Treat invalid names as if the parameter were nameless. */
13580 if (identifier == error_mark_node)
13581 identifier = NULL_TREE;
13583 else
13584 identifier = NULL_TREE;
13586 /* Create the template parameter. */
13587 parameter = finish_template_template_parm (class_type_node,
13588 identifier);
13590 /* If the next token is an `=', then there is a
13591 default-argument. */
13592 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13594 bool is_template;
13596 /* Consume the `='. */
13597 cp_lexer_consume_token (parser->lexer);
13598 /* Parse the id-expression. */
13599 push_deferring_access_checks (dk_no_deferred);
13600 /* save token before parsing the id-expression, for error
13601 reporting */
13602 token = cp_lexer_peek_token (parser->lexer);
13603 default_argument
13604 = cp_parser_id_expression (parser,
13605 /*template_keyword_p=*/false,
13606 /*check_dependency_p=*/true,
13607 /*template_p=*/&is_template,
13608 /*declarator_p=*/false,
13609 /*optional_p=*/false);
13610 if (TREE_CODE (default_argument) == TYPE_DECL)
13611 /* If the id-expression was a template-id that refers to
13612 a template-class, we already have the declaration here,
13613 so no further lookup is needed. */
13615 else
13616 /* Look up the name. */
13617 default_argument
13618 = cp_parser_lookup_name (parser, default_argument,
13619 none_type,
13620 /*is_template=*/is_template,
13621 /*is_namespace=*/false,
13622 /*check_dependency=*/true,
13623 /*ambiguous_decls=*/NULL,
13624 token->location);
13625 /* See if the default argument is valid. */
13626 default_argument
13627 = check_template_template_default_arg (default_argument);
13629 /* Template parameter packs cannot have default
13630 arguments. */
13631 if (*is_parameter_pack)
13633 if (identifier)
13634 error_at (token->location,
13635 "template parameter pack %qD cannot "
13636 "have a default argument",
13637 identifier);
13638 else
13639 error_at (token->location, "template parameter packs cannot "
13640 "have default arguments");
13641 default_argument = NULL_TREE;
13643 pop_deferring_access_checks ();
13645 else
13646 default_argument = NULL_TREE;
13648 /* Create the combined representation of the parameter and the
13649 default argument. */
13650 parameter = build_tree_list (default_argument, parameter);
13652 break;
13654 default:
13655 gcc_unreachable ();
13656 break;
13659 return parameter;
13662 /* Parse a template-id.
13664 template-id:
13665 template-name < template-argument-list [opt] >
13667 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13668 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13669 returned. Otherwise, if the template-name names a function, or set
13670 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13671 names a class, returns a TYPE_DECL for the specialization.
13673 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13674 uninstantiated templates. */
13676 static tree
13677 cp_parser_template_id (cp_parser *parser,
13678 bool template_keyword_p,
13679 bool check_dependency_p,
13680 enum tag_types tag_type,
13681 bool is_declaration)
13683 int i;
13684 tree templ;
13685 tree arguments;
13686 tree template_id;
13687 cp_token_position start_of_id = 0;
13688 deferred_access_check *chk;
13689 vec<deferred_access_check, va_gc> *access_check;
13690 cp_token *next_token = NULL, *next_token_2 = NULL;
13691 bool is_identifier;
13693 /* If the next token corresponds to a template-id, there is no need
13694 to reparse it. */
13695 next_token = cp_lexer_peek_token (parser->lexer);
13696 if (next_token->type == CPP_TEMPLATE_ID)
13698 struct tree_check *check_value;
13700 /* Get the stored value. */
13701 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13702 /* Perform any access checks that were deferred. */
13703 access_check = check_value->checks;
13704 if (access_check)
13706 FOR_EACH_VEC_ELT (*access_check, i, chk)
13707 perform_or_defer_access_check (chk->binfo,
13708 chk->decl,
13709 chk->diag_decl,
13710 tf_warning_or_error);
13712 /* Return the stored value. */
13713 return check_value->value;
13716 /* Avoid performing name lookup if there is no possibility of
13717 finding a template-id. */
13718 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13719 || (next_token->type == CPP_NAME
13720 && !cp_parser_nth_token_starts_template_argument_list_p
13721 (parser, 2)))
13723 cp_parser_error (parser, "expected template-id");
13724 return error_mark_node;
13727 /* Remember where the template-id starts. */
13728 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13729 start_of_id = cp_lexer_token_position (parser->lexer, false);
13731 push_deferring_access_checks (dk_deferred);
13733 /* Parse the template-name. */
13734 is_identifier = false;
13735 templ = cp_parser_template_name (parser, template_keyword_p,
13736 check_dependency_p,
13737 is_declaration,
13738 tag_type,
13739 &is_identifier);
13740 if (templ == error_mark_node || is_identifier)
13742 pop_deferring_access_checks ();
13743 return templ;
13746 /* If we find the sequence `[:' after a template-name, it's probably
13747 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13748 parse correctly the argument list. */
13749 next_token = cp_lexer_peek_token (parser->lexer);
13750 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13751 if (next_token->type == CPP_OPEN_SQUARE
13752 && next_token->flags & DIGRAPH
13753 && next_token_2->type == CPP_COLON
13754 && !(next_token_2->flags & PREV_WHITE))
13756 cp_parser_parse_tentatively (parser);
13757 /* Change `:' into `::'. */
13758 next_token_2->type = CPP_SCOPE;
13759 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13760 CPP_LESS. */
13761 cp_lexer_consume_token (parser->lexer);
13763 /* Parse the arguments. */
13764 arguments = cp_parser_enclosed_template_argument_list (parser);
13765 if (!cp_parser_parse_definitely (parser))
13767 /* If we couldn't parse an argument list, then we revert our changes
13768 and return simply an error. Maybe this is not a template-id
13769 after all. */
13770 next_token_2->type = CPP_COLON;
13771 cp_parser_error (parser, "expected %<<%>");
13772 pop_deferring_access_checks ();
13773 return error_mark_node;
13775 /* Otherwise, emit an error about the invalid digraph, but continue
13776 parsing because we got our argument list. */
13777 if (permerror (next_token->location,
13778 "%<<::%> cannot begin a template-argument list"))
13780 static bool hint = false;
13781 inform (next_token->location,
13782 "%<<:%> is an alternate spelling for %<[%>."
13783 " Insert whitespace between %<<%> and %<::%>");
13784 if (!hint && !flag_permissive)
13786 inform (next_token->location, "(if you use %<-fpermissive%> "
13787 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13788 "accept your code)");
13789 hint = true;
13793 else
13795 /* Look for the `<' that starts the template-argument-list. */
13796 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13798 pop_deferring_access_checks ();
13799 return error_mark_node;
13801 /* Parse the arguments. */
13802 arguments = cp_parser_enclosed_template_argument_list (parser);
13805 /* Build a representation of the specialization. */
13806 if (identifier_p (templ))
13807 template_id = build_min_nt_loc (next_token->location,
13808 TEMPLATE_ID_EXPR,
13809 templ, arguments);
13810 else if (DECL_TYPE_TEMPLATE_P (templ)
13811 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13813 bool entering_scope;
13814 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13815 template (rather than some instantiation thereof) only if
13816 is not nested within some other construct. For example, in
13817 "template <typename T> void f(T) { A<T>::", A<T> is just an
13818 instantiation of A. */
13819 entering_scope = (template_parm_scope_p ()
13820 && cp_lexer_next_token_is (parser->lexer,
13821 CPP_SCOPE));
13822 template_id
13823 = finish_template_type (templ, arguments, entering_scope);
13825 else if (variable_template_p (templ))
13827 template_id = lookup_template_variable (templ, arguments);
13829 else
13831 /* If it's not a class-template or a template-template, it should be
13832 a function-template. */
13833 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13834 || TREE_CODE (templ) == OVERLOAD
13835 || BASELINK_P (templ)));
13837 template_id = lookup_template_function (templ, arguments);
13840 /* If parsing tentatively, replace the sequence of tokens that makes
13841 up the template-id with a CPP_TEMPLATE_ID token. That way,
13842 should we re-parse the token stream, we will not have to repeat
13843 the effort required to do the parse, nor will we issue duplicate
13844 error messages about problems during instantiation of the
13845 template. */
13846 if (start_of_id
13847 /* Don't do this if we had a parse error in a declarator; re-parsing
13848 might succeed if a name changes meaning (60361). */
13849 && !(cp_parser_error_occurred (parser)
13850 && cp_parser_parsing_tentatively (parser)
13851 && parser->in_declarator_p))
13853 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13855 /* Reset the contents of the START_OF_ID token. */
13856 token->type = CPP_TEMPLATE_ID;
13857 /* Retrieve any deferred checks. Do not pop this access checks yet
13858 so the memory will not be reclaimed during token replacing below. */
13859 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13860 token->u.tree_check_value->value = template_id;
13861 token->u.tree_check_value->checks = get_deferred_access_checks ();
13862 token->keyword = RID_MAX;
13864 /* Purge all subsequent tokens. */
13865 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13867 /* ??? Can we actually assume that, if template_id ==
13868 error_mark_node, we will have issued a diagnostic to the
13869 user, as opposed to simply marking the tentative parse as
13870 failed? */
13871 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13872 error_at (token->location, "parse error in template argument list");
13875 pop_to_parent_deferring_access_checks ();
13876 return template_id;
13879 /* Parse a template-name.
13881 template-name:
13882 identifier
13884 The standard should actually say:
13886 template-name:
13887 identifier
13888 operator-function-id
13890 A defect report has been filed about this issue.
13892 A conversion-function-id cannot be a template name because they cannot
13893 be part of a template-id. In fact, looking at this code:
13895 a.operator K<int>()
13897 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13898 It is impossible to call a templated conversion-function-id with an
13899 explicit argument list, since the only allowed template parameter is
13900 the type to which it is converting.
13902 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13903 `template' keyword, in a construction like:
13905 T::template f<3>()
13907 In that case `f' is taken to be a template-name, even though there
13908 is no way of knowing for sure.
13910 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13911 name refers to a set of overloaded functions, at least one of which
13912 is a template, or an IDENTIFIER_NODE with the name of the template,
13913 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13914 names are looked up inside uninstantiated templates. */
13916 static tree
13917 cp_parser_template_name (cp_parser* parser,
13918 bool template_keyword_p,
13919 bool check_dependency_p,
13920 bool is_declaration,
13921 enum tag_types tag_type,
13922 bool *is_identifier)
13924 tree identifier;
13925 tree decl;
13926 tree fns;
13927 cp_token *token = cp_lexer_peek_token (parser->lexer);
13929 /* If the next token is `operator', then we have either an
13930 operator-function-id or a conversion-function-id. */
13931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13933 /* We don't know whether we're looking at an
13934 operator-function-id or a conversion-function-id. */
13935 cp_parser_parse_tentatively (parser);
13936 /* Try an operator-function-id. */
13937 identifier = cp_parser_operator_function_id (parser);
13938 /* If that didn't work, try a conversion-function-id. */
13939 if (!cp_parser_parse_definitely (parser))
13941 cp_parser_error (parser, "expected template-name");
13942 return error_mark_node;
13945 /* Look for the identifier. */
13946 else
13947 identifier = cp_parser_identifier (parser);
13949 /* If we didn't find an identifier, we don't have a template-id. */
13950 if (identifier == error_mark_node)
13951 return error_mark_node;
13953 /* If the name immediately followed the `template' keyword, then it
13954 is a template-name. However, if the next token is not `<', then
13955 we do not treat it as a template-name, since it is not being used
13956 as part of a template-id. This enables us to handle constructs
13957 like:
13959 template <typename T> struct S { S(); };
13960 template <typename T> S<T>::S();
13962 correctly. We would treat `S' as a template -- if it were `S<T>'
13963 -- but we do not if there is no `<'. */
13965 if (processing_template_decl
13966 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13968 /* In a declaration, in a dependent context, we pretend that the
13969 "template" keyword was present in order to improve error
13970 recovery. For example, given:
13972 template <typename T> void f(T::X<int>);
13974 we want to treat "X<int>" as a template-id. */
13975 if (is_declaration
13976 && !template_keyword_p
13977 && parser->scope && TYPE_P (parser->scope)
13978 && check_dependency_p
13979 && dependent_scope_p (parser->scope)
13980 /* Do not do this for dtors (or ctors), since they never
13981 need the template keyword before their name. */
13982 && !constructor_name_p (identifier, parser->scope))
13984 cp_token_position start = 0;
13986 /* Explain what went wrong. */
13987 error_at (token->location, "non-template %qD used as template",
13988 identifier);
13989 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13990 parser->scope, identifier);
13991 /* If parsing tentatively, find the location of the "<" token. */
13992 if (cp_parser_simulate_error (parser))
13993 start = cp_lexer_token_position (parser->lexer, true);
13994 /* Parse the template arguments so that we can issue error
13995 messages about them. */
13996 cp_lexer_consume_token (parser->lexer);
13997 cp_parser_enclosed_template_argument_list (parser);
13998 /* Skip tokens until we find a good place from which to
13999 continue parsing. */
14000 cp_parser_skip_to_closing_parenthesis (parser,
14001 /*recovering=*/true,
14002 /*or_comma=*/true,
14003 /*consume_paren=*/false);
14004 /* If parsing tentatively, permanently remove the
14005 template argument list. That will prevent duplicate
14006 error messages from being issued about the missing
14007 "template" keyword. */
14008 if (start)
14009 cp_lexer_purge_tokens_after (parser->lexer, start);
14010 if (is_identifier)
14011 *is_identifier = true;
14012 return identifier;
14015 /* If the "template" keyword is present, then there is generally
14016 no point in doing name-lookup, so we just return IDENTIFIER.
14017 But, if the qualifying scope is non-dependent then we can
14018 (and must) do name-lookup normally. */
14019 if (template_keyword_p
14020 && (!parser->scope
14021 || (TYPE_P (parser->scope)
14022 && dependent_type_p (parser->scope))))
14023 return identifier;
14026 /* Look up the name. */
14027 decl = cp_parser_lookup_name (parser, identifier,
14028 tag_type,
14029 /*is_template=*/true,
14030 /*is_namespace=*/false,
14031 check_dependency_p,
14032 /*ambiguous_decls=*/NULL,
14033 token->location);
14035 decl = strip_using_decl (decl);
14037 /* If DECL is a template, then the name was a template-name. */
14038 if (TREE_CODE (decl) == TEMPLATE_DECL)
14040 if (TREE_DEPRECATED (decl)
14041 && deprecated_state != DEPRECATED_SUPPRESS)
14042 warn_deprecated_use (decl, NULL_TREE);
14044 else
14046 tree fn = NULL_TREE;
14048 /* The standard does not explicitly indicate whether a name that
14049 names a set of overloaded declarations, some of which are
14050 templates, is a template-name. However, such a name should
14051 be a template-name; otherwise, there is no way to form a
14052 template-id for the overloaded templates. */
14053 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14054 if (TREE_CODE (fns) == OVERLOAD)
14055 for (fn = fns; fn; fn = OVL_NEXT (fn))
14056 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14057 break;
14059 if (!fn)
14061 /* The name does not name a template. */
14062 cp_parser_error (parser, "expected template-name");
14063 return error_mark_node;
14067 /* If DECL is dependent, and refers to a function, then just return
14068 its name; we will look it up again during template instantiation. */
14069 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14071 tree scope = ovl_scope (decl);
14072 if (TYPE_P (scope) && dependent_type_p (scope))
14073 return identifier;
14076 return decl;
14079 /* Parse a template-argument-list.
14081 template-argument-list:
14082 template-argument ... [opt]
14083 template-argument-list , template-argument ... [opt]
14085 Returns a TREE_VEC containing the arguments. */
14087 static tree
14088 cp_parser_template_argument_list (cp_parser* parser)
14090 tree fixed_args[10];
14091 unsigned n_args = 0;
14092 unsigned alloced = 10;
14093 tree *arg_ary = fixed_args;
14094 tree vec;
14095 bool saved_in_template_argument_list_p;
14096 bool saved_ice_p;
14097 bool saved_non_ice_p;
14099 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14100 parser->in_template_argument_list_p = true;
14101 /* Even if the template-id appears in an integral
14102 constant-expression, the contents of the argument list do
14103 not. */
14104 saved_ice_p = parser->integral_constant_expression_p;
14105 parser->integral_constant_expression_p = false;
14106 saved_non_ice_p = parser->non_integral_constant_expression_p;
14107 parser->non_integral_constant_expression_p = false;
14109 /* Parse the arguments. */
14112 tree argument;
14114 if (n_args)
14115 /* Consume the comma. */
14116 cp_lexer_consume_token (parser->lexer);
14118 /* Parse the template-argument. */
14119 argument = cp_parser_template_argument (parser);
14121 /* If the next token is an ellipsis, we're expanding a template
14122 argument pack. */
14123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14125 if (argument == error_mark_node)
14127 cp_token *token = cp_lexer_peek_token (parser->lexer);
14128 error_at (token->location,
14129 "expected parameter pack before %<...%>");
14131 /* Consume the `...' token. */
14132 cp_lexer_consume_token (parser->lexer);
14134 /* Make the argument into a TYPE_PACK_EXPANSION or
14135 EXPR_PACK_EXPANSION. */
14136 argument = make_pack_expansion (argument);
14139 if (n_args == alloced)
14141 alloced *= 2;
14143 if (arg_ary == fixed_args)
14145 arg_ary = XNEWVEC (tree, alloced);
14146 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14148 else
14149 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14151 arg_ary[n_args++] = argument;
14153 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14155 vec = make_tree_vec (n_args);
14157 while (n_args--)
14158 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14160 if (arg_ary != fixed_args)
14161 free (arg_ary);
14162 parser->non_integral_constant_expression_p = saved_non_ice_p;
14163 parser->integral_constant_expression_p = saved_ice_p;
14164 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14165 #ifdef ENABLE_CHECKING
14166 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14167 #endif
14168 return vec;
14171 /* Parse a template-argument.
14173 template-argument:
14174 assignment-expression
14175 type-id
14176 id-expression
14178 The representation is that of an assignment-expression, type-id, or
14179 id-expression -- except that the qualified id-expression is
14180 evaluated, so that the value returned is either a DECL or an
14181 OVERLOAD.
14183 Although the standard says "assignment-expression", it forbids
14184 throw-expressions or assignments in the template argument.
14185 Therefore, we use "conditional-expression" instead. */
14187 static tree
14188 cp_parser_template_argument (cp_parser* parser)
14190 tree argument;
14191 bool template_p;
14192 bool address_p;
14193 bool maybe_type_id = false;
14194 cp_token *token = NULL, *argument_start_token = NULL;
14195 location_t loc = 0;
14196 cp_id_kind idk;
14198 /* There's really no way to know what we're looking at, so we just
14199 try each alternative in order.
14201 [temp.arg]
14203 In a template-argument, an ambiguity between a type-id and an
14204 expression is resolved to a type-id, regardless of the form of
14205 the corresponding template-parameter.
14207 Therefore, we try a type-id first. */
14208 cp_parser_parse_tentatively (parser);
14209 argument = cp_parser_template_type_arg (parser);
14210 /* If there was no error parsing the type-id but the next token is a
14211 '>>', our behavior depends on which dialect of C++ we're
14212 parsing. In C++98, we probably found a typo for '> >'. But there
14213 are type-id which are also valid expressions. For instance:
14215 struct X { int operator >> (int); };
14216 template <int V> struct Foo {};
14217 Foo<X () >> 5> r;
14219 Here 'X()' is a valid type-id of a function type, but the user just
14220 wanted to write the expression "X() >> 5". Thus, we remember that we
14221 found a valid type-id, but we still try to parse the argument as an
14222 expression to see what happens.
14224 In C++0x, the '>>' will be considered two separate '>'
14225 tokens. */
14226 if (!cp_parser_error_occurred (parser)
14227 && cxx_dialect == cxx98
14228 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14230 maybe_type_id = true;
14231 cp_parser_abort_tentative_parse (parser);
14233 else
14235 /* If the next token isn't a `,' or a `>', then this argument wasn't
14236 really finished. This means that the argument is not a valid
14237 type-id. */
14238 if (!cp_parser_next_token_ends_template_argument_p (parser))
14239 cp_parser_error (parser, "expected template-argument");
14240 /* If that worked, we're done. */
14241 if (cp_parser_parse_definitely (parser))
14242 return argument;
14244 /* We're still not sure what the argument will be. */
14245 cp_parser_parse_tentatively (parser);
14246 /* Try a template. */
14247 argument_start_token = cp_lexer_peek_token (parser->lexer);
14248 argument = cp_parser_id_expression (parser,
14249 /*template_keyword_p=*/false,
14250 /*check_dependency_p=*/true,
14251 &template_p,
14252 /*declarator_p=*/false,
14253 /*optional_p=*/false);
14254 /* If the next token isn't a `,' or a `>', then this argument wasn't
14255 really finished. */
14256 if (!cp_parser_next_token_ends_template_argument_p (parser))
14257 cp_parser_error (parser, "expected template-argument");
14258 if (!cp_parser_error_occurred (parser))
14260 /* Figure out what is being referred to. If the id-expression
14261 was for a class template specialization, then we will have a
14262 TYPE_DECL at this point. There is no need to do name lookup
14263 at this point in that case. */
14264 if (TREE_CODE (argument) != TYPE_DECL)
14265 argument = cp_parser_lookup_name (parser, argument,
14266 none_type,
14267 /*is_template=*/template_p,
14268 /*is_namespace=*/false,
14269 /*check_dependency=*/true,
14270 /*ambiguous_decls=*/NULL,
14271 argument_start_token->location);
14272 if (TREE_CODE (argument) != TEMPLATE_DECL
14273 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14274 cp_parser_error (parser, "expected template-name");
14276 if (cp_parser_parse_definitely (parser))
14278 if (TREE_DEPRECATED (argument))
14279 warn_deprecated_use (argument, NULL_TREE);
14280 return argument;
14282 /* It must be a non-type argument. There permitted cases are given
14283 in [temp.arg.nontype]:
14285 -- an integral constant-expression of integral or enumeration
14286 type; or
14288 -- the name of a non-type template-parameter; or
14290 -- the name of an object or function with external linkage...
14292 -- the address of an object or function with external linkage...
14294 -- a pointer to member... */
14295 /* Look for a non-type template parameter. */
14296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14298 cp_parser_parse_tentatively (parser);
14299 argument = cp_parser_primary_expression (parser,
14300 /*address_p=*/false,
14301 /*cast_p=*/false,
14302 /*template_arg_p=*/true,
14303 &idk);
14304 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14305 || !cp_parser_next_token_ends_template_argument_p (parser))
14306 cp_parser_simulate_error (parser);
14307 if (cp_parser_parse_definitely (parser))
14308 return argument;
14311 /* If the next token is "&", the argument must be the address of an
14312 object or function with external linkage. */
14313 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14314 if (address_p)
14316 loc = cp_lexer_peek_token (parser->lexer)->location;
14317 cp_lexer_consume_token (parser->lexer);
14319 /* See if we might have an id-expression. */
14320 token = cp_lexer_peek_token (parser->lexer);
14321 if (token->type == CPP_NAME
14322 || token->keyword == RID_OPERATOR
14323 || token->type == CPP_SCOPE
14324 || token->type == CPP_TEMPLATE_ID
14325 || token->type == CPP_NESTED_NAME_SPECIFIER)
14327 cp_parser_parse_tentatively (parser);
14328 argument = cp_parser_primary_expression (parser,
14329 address_p,
14330 /*cast_p=*/false,
14331 /*template_arg_p=*/true,
14332 &idk);
14333 if (cp_parser_error_occurred (parser)
14334 || !cp_parser_next_token_ends_template_argument_p (parser))
14335 cp_parser_abort_tentative_parse (parser);
14336 else
14338 tree probe;
14340 if (INDIRECT_REF_P (argument))
14342 /* Strip the dereference temporarily. */
14343 gcc_assert (REFERENCE_REF_P (argument));
14344 argument = TREE_OPERAND (argument, 0);
14347 /* If we're in a template, we represent a qualified-id referring
14348 to a static data member as a SCOPE_REF even if the scope isn't
14349 dependent so that we can check access control later. */
14350 probe = argument;
14351 if (TREE_CODE (probe) == SCOPE_REF)
14352 probe = TREE_OPERAND (probe, 1);
14353 if (VAR_P (probe))
14355 /* A variable without external linkage might still be a
14356 valid constant-expression, so no error is issued here
14357 if the external-linkage check fails. */
14358 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14359 cp_parser_simulate_error (parser);
14361 else if (is_overloaded_fn (argument))
14362 /* All overloaded functions are allowed; if the external
14363 linkage test does not pass, an error will be issued
14364 later. */
14366 else if (address_p
14367 && (TREE_CODE (argument) == OFFSET_REF
14368 || TREE_CODE (argument) == SCOPE_REF))
14369 /* A pointer-to-member. */
14371 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14373 else
14374 cp_parser_simulate_error (parser);
14376 if (cp_parser_parse_definitely (parser))
14378 if (address_p)
14379 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14380 tf_warning_or_error);
14381 else
14382 argument = convert_from_reference (argument);
14383 return argument;
14387 /* If the argument started with "&", there are no other valid
14388 alternatives at this point. */
14389 if (address_p)
14391 cp_parser_error (parser, "invalid non-type template argument");
14392 return error_mark_node;
14395 /* If the argument wasn't successfully parsed as a type-id followed
14396 by '>>', the argument can only be a constant expression now.
14397 Otherwise, we try parsing the constant-expression tentatively,
14398 because the argument could really be a type-id. */
14399 if (maybe_type_id)
14400 cp_parser_parse_tentatively (parser);
14401 argument = cp_parser_constant_expression (parser);
14403 if (!maybe_type_id)
14404 return argument;
14405 if (!cp_parser_next_token_ends_template_argument_p (parser))
14406 cp_parser_error (parser, "expected template-argument");
14407 if (cp_parser_parse_definitely (parser))
14408 return argument;
14409 /* We did our best to parse the argument as a non type-id, but that
14410 was the only alternative that matched (albeit with a '>' after
14411 it). We can assume it's just a typo from the user, and a
14412 diagnostic will then be issued. */
14413 return cp_parser_template_type_arg (parser);
14416 /* Parse an explicit-instantiation.
14418 explicit-instantiation:
14419 template declaration
14421 Although the standard says `declaration', what it really means is:
14423 explicit-instantiation:
14424 template decl-specifier-seq [opt] declarator [opt] ;
14426 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14427 supposed to be allowed. A defect report has been filed about this
14428 issue.
14430 GNU Extension:
14432 explicit-instantiation:
14433 storage-class-specifier template
14434 decl-specifier-seq [opt] declarator [opt] ;
14435 function-specifier template
14436 decl-specifier-seq [opt] declarator [opt] ; */
14438 static void
14439 cp_parser_explicit_instantiation (cp_parser* parser)
14441 int declares_class_or_enum;
14442 cp_decl_specifier_seq decl_specifiers;
14443 tree extension_specifier = NULL_TREE;
14445 timevar_push (TV_TEMPLATE_INST);
14447 /* Look for an (optional) storage-class-specifier or
14448 function-specifier. */
14449 if (cp_parser_allow_gnu_extensions_p (parser))
14451 extension_specifier
14452 = cp_parser_storage_class_specifier_opt (parser);
14453 if (!extension_specifier)
14454 extension_specifier
14455 = cp_parser_function_specifier_opt (parser,
14456 /*decl_specs=*/NULL);
14459 /* Look for the `template' keyword. */
14460 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14461 /* Let the front end know that we are processing an explicit
14462 instantiation. */
14463 begin_explicit_instantiation ();
14464 /* [temp.explicit] says that we are supposed to ignore access
14465 control while processing explicit instantiation directives. */
14466 push_deferring_access_checks (dk_no_check);
14467 /* Parse a decl-specifier-seq. */
14468 cp_parser_decl_specifier_seq (parser,
14469 CP_PARSER_FLAGS_OPTIONAL,
14470 &decl_specifiers,
14471 &declares_class_or_enum);
14472 /* If there was exactly one decl-specifier, and it declared a class,
14473 and there's no declarator, then we have an explicit type
14474 instantiation. */
14475 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14477 tree type;
14479 type = check_tag_decl (&decl_specifiers,
14480 /*explicit_type_instantiation_p=*/true);
14481 /* Turn access control back on for names used during
14482 template instantiation. */
14483 pop_deferring_access_checks ();
14484 if (type)
14485 do_type_instantiation (type, extension_specifier,
14486 /*complain=*/tf_error);
14488 else
14490 cp_declarator *declarator;
14491 tree decl;
14493 /* Parse the declarator. */
14494 declarator
14495 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14496 /*ctor_dtor_or_conv_p=*/NULL,
14497 /*parenthesized_p=*/NULL,
14498 /*member_p=*/false,
14499 /*friend_p=*/false);
14500 if (declares_class_or_enum & 2)
14501 cp_parser_check_for_definition_in_return_type (declarator,
14502 decl_specifiers.type,
14503 decl_specifiers.locations[ds_type_spec]);
14504 if (declarator != cp_error_declarator)
14506 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14507 permerror (decl_specifiers.locations[ds_inline],
14508 "explicit instantiation shall not use"
14509 " %<inline%> specifier");
14510 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14511 permerror (decl_specifiers.locations[ds_constexpr],
14512 "explicit instantiation shall not use"
14513 " %<constexpr%> specifier");
14515 decl = grokdeclarator (declarator, &decl_specifiers,
14516 NORMAL, 0, &decl_specifiers.attributes);
14517 /* Turn access control back on for names used during
14518 template instantiation. */
14519 pop_deferring_access_checks ();
14520 /* Do the explicit instantiation. */
14521 do_decl_instantiation (decl, extension_specifier);
14523 else
14525 pop_deferring_access_checks ();
14526 /* Skip the body of the explicit instantiation. */
14527 cp_parser_skip_to_end_of_statement (parser);
14530 /* We're done with the instantiation. */
14531 end_explicit_instantiation ();
14533 cp_parser_consume_semicolon_at_end_of_statement (parser);
14535 timevar_pop (TV_TEMPLATE_INST);
14538 /* Parse an explicit-specialization.
14540 explicit-specialization:
14541 template < > declaration
14543 Although the standard says `declaration', what it really means is:
14545 explicit-specialization:
14546 template <> decl-specifier [opt] init-declarator [opt] ;
14547 template <> function-definition
14548 template <> explicit-specialization
14549 template <> template-declaration */
14551 static void
14552 cp_parser_explicit_specialization (cp_parser* parser)
14554 bool need_lang_pop;
14555 cp_token *token = cp_lexer_peek_token (parser->lexer);
14557 /* Look for the `template' keyword. */
14558 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14559 /* Look for the `<'. */
14560 cp_parser_require (parser, CPP_LESS, RT_LESS);
14561 /* Look for the `>'. */
14562 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14563 /* We have processed another parameter list. */
14564 ++parser->num_template_parameter_lists;
14565 /* [temp]
14567 A template ... explicit specialization ... shall not have C
14568 linkage. */
14569 if (current_lang_name == lang_name_c)
14571 error_at (token->location, "template specialization with C linkage");
14572 /* Give it C++ linkage to avoid confusing other parts of the
14573 front end. */
14574 push_lang_context (lang_name_cplusplus);
14575 need_lang_pop = true;
14577 else
14578 need_lang_pop = false;
14579 /* Let the front end know that we are beginning a specialization. */
14580 if (!begin_specialization ())
14582 end_specialization ();
14583 return;
14586 /* If the next keyword is `template', we need to figure out whether
14587 or not we're looking a template-declaration. */
14588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14590 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14591 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14592 cp_parser_template_declaration_after_export (parser,
14593 /*member_p=*/false);
14594 else
14595 cp_parser_explicit_specialization (parser);
14597 else
14598 /* Parse the dependent declaration. */
14599 cp_parser_single_declaration (parser,
14600 /*checks=*/NULL,
14601 /*member_p=*/false,
14602 /*explicit_specialization_p=*/true,
14603 /*friend_p=*/NULL);
14604 /* We're done with the specialization. */
14605 end_specialization ();
14606 /* For the erroneous case of a template with C linkage, we pushed an
14607 implicit C++ linkage scope; exit that scope now. */
14608 if (need_lang_pop)
14609 pop_lang_context ();
14610 /* We're done with this parameter list. */
14611 --parser->num_template_parameter_lists;
14614 /* Parse a type-specifier.
14616 type-specifier:
14617 simple-type-specifier
14618 class-specifier
14619 enum-specifier
14620 elaborated-type-specifier
14621 cv-qualifier
14623 GNU Extension:
14625 type-specifier:
14626 __complex__
14628 Returns a representation of the type-specifier. For a
14629 class-specifier, enum-specifier, or elaborated-type-specifier, a
14630 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14632 The parser flags FLAGS is used to control type-specifier parsing.
14634 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14635 in a decl-specifier-seq.
14637 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14638 class-specifier, enum-specifier, or elaborated-type-specifier, then
14639 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14640 if a type is declared; 2 if it is defined. Otherwise, it is set to
14641 zero.
14643 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14644 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14645 is set to FALSE. */
14647 static tree
14648 cp_parser_type_specifier (cp_parser* parser,
14649 cp_parser_flags flags,
14650 cp_decl_specifier_seq *decl_specs,
14651 bool is_declaration,
14652 int* declares_class_or_enum,
14653 bool* is_cv_qualifier)
14655 tree type_spec = NULL_TREE;
14656 cp_token *token;
14657 enum rid keyword;
14658 cp_decl_spec ds = ds_last;
14660 /* Assume this type-specifier does not declare a new type. */
14661 if (declares_class_or_enum)
14662 *declares_class_or_enum = 0;
14663 /* And that it does not specify a cv-qualifier. */
14664 if (is_cv_qualifier)
14665 *is_cv_qualifier = false;
14666 /* Peek at the next token. */
14667 token = cp_lexer_peek_token (parser->lexer);
14669 /* If we're looking at a keyword, we can use that to guide the
14670 production we choose. */
14671 keyword = token->keyword;
14672 switch (keyword)
14674 case RID_ENUM:
14675 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14676 goto elaborated_type_specifier;
14678 /* Look for the enum-specifier. */
14679 type_spec = cp_parser_enum_specifier (parser);
14680 /* If that worked, we're done. */
14681 if (type_spec)
14683 if (declares_class_or_enum)
14684 *declares_class_or_enum = 2;
14685 if (decl_specs)
14686 cp_parser_set_decl_spec_type (decl_specs,
14687 type_spec,
14688 token,
14689 /*type_definition_p=*/true);
14690 return type_spec;
14692 else
14693 goto elaborated_type_specifier;
14695 /* Any of these indicate either a class-specifier, or an
14696 elaborated-type-specifier. */
14697 case RID_CLASS:
14698 case RID_STRUCT:
14699 case RID_UNION:
14700 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14701 goto elaborated_type_specifier;
14703 /* Parse tentatively so that we can back up if we don't find a
14704 class-specifier. */
14705 cp_parser_parse_tentatively (parser);
14706 /* Look for the class-specifier. */
14707 type_spec = cp_parser_class_specifier (parser);
14708 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14709 /* If that worked, we're done. */
14710 if (cp_parser_parse_definitely (parser))
14712 if (declares_class_or_enum)
14713 *declares_class_or_enum = 2;
14714 if (decl_specs)
14715 cp_parser_set_decl_spec_type (decl_specs,
14716 type_spec,
14717 token,
14718 /*type_definition_p=*/true);
14719 return type_spec;
14722 /* Fall through. */
14723 elaborated_type_specifier:
14724 /* We're declaring (not defining) a class or enum. */
14725 if (declares_class_or_enum)
14726 *declares_class_or_enum = 1;
14728 /* Fall through. */
14729 case RID_TYPENAME:
14730 /* Look for an elaborated-type-specifier. */
14731 type_spec
14732 = (cp_parser_elaborated_type_specifier
14733 (parser,
14734 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14735 is_declaration));
14736 if (decl_specs)
14737 cp_parser_set_decl_spec_type (decl_specs,
14738 type_spec,
14739 token,
14740 /*type_definition_p=*/false);
14741 return type_spec;
14743 case RID_CONST:
14744 ds = ds_const;
14745 if (is_cv_qualifier)
14746 *is_cv_qualifier = true;
14747 break;
14749 case RID_VOLATILE:
14750 ds = ds_volatile;
14751 if (is_cv_qualifier)
14752 *is_cv_qualifier = true;
14753 break;
14755 case RID_RESTRICT:
14756 ds = ds_restrict;
14757 if (is_cv_qualifier)
14758 *is_cv_qualifier = true;
14759 break;
14761 case RID_COMPLEX:
14762 /* The `__complex__' keyword is a GNU extension. */
14763 ds = ds_complex;
14764 break;
14766 default:
14767 break;
14770 /* Handle simple keywords. */
14771 if (ds != ds_last)
14773 if (decl_specs)
14775 set_and_check_decl_spec_loc (decl_specs, ds, token);
14776 decl_specs->any_specifiers_p = true;
14778 return cp_lexer_consume_token (parser->lexer)->u.value;
14781 /* If we do not already have a type-specifier, assume we are looking
14782 at a simple-type-specifier. */
14783 type_spec = cp_parser_simple_type_specifier (parser,
14784 decl_specs,
14785 flags);
14787 /* If we didn't find a type-specifier, and a type-specifier was not
14788 optional in this context, issue an error message. */
14789 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14791 cp_parser_error (parser, "expected type specifier");
14792 return error_mark_node;
14795 return type_spec;
14798 /* Parse a simple-type-specifier.
14800 simple-type-specifier:
14801 :: [opt] nested-name-specifier [opt] type-name
14802 :: [opt] nested-name-specifier template template-id
14803 char
14804 wchar_t
14805 bool
14806 short
14808 long
14809 signed
14810 unsigned
14811 float
14812 double
14813 void
14815 C++0x Extension:
14817 simple-type-specifier:
14818 auto
14819 decltype ( expression )
14820 char16_t
14821 char32_t
14822 __underlying_type ( type-id )
14824 GNU Extension:
14826 simple-type-specifier:
14827 __int128
14828 __typeof__ unary-expression
14829 __typeof__ ( type-id )
14830 __typeof__ ( type-id ) { initializer-list , [opt] }
14832 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14833 appropriately updated. */
14835 static tree
14836 cp_parser_simple_type_specifier (cp_parser* parser,
14837 cp_decl_specifier_seq *decl_specs,
14838 cp_parser_flags flags)
14840 tree type = NULL_TREE;
14841 cp_token *token;
14842 int idx;
14844 /* Peek at the next token. */
14845 token = cp_lexer_peek_token (parser->lexer);
14847 /* If we're looking at a keyword, things are easy. */
14848 switch (token->keyword)
14850 case RID_CHAR:
14851 if (decl_specs)
14852 decl_specs->explicit_char_p = true;
14853 type = char_type_node;
14854 break;
14855 case RID_CHAR16:
14856 type = char16_type_node;
14857 break;
14858 case RID_CHAR32:
14859 type = char32_type_node;
14860 break;
14861 case RID_WCHAR:
14862 type = wchar_type_node;
14863 break;
14864 case RID_BOOL:
14865 type = boolean_type_node;
14866 break;
14867 case RID_SHORT:
14868 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14869 type = short_integer_type_node;
14870 break;
14871 case RID_INT:
14872 if (decl_specs)
14873 decl_specs->explicit_int_p = true;
14874 type = integer_type_node;
14875 break;
14876 case RID_INT_N_0:
14877 case RID_INT_N_1:
14878 case RID_INT_N_2:
14879 case RID_INT_N_3:
14880 idx = token->keyword - RID_INT_N_0;
14881 if (! int_n_enabled_p [idx])
14882 break;
14883 if (decl_specs)
14885 decl_specs->explicit_intN_p = true;
14886 decl_specs->int_n_idx = idx;
14888 type = int_n_trees [idx].signed_type;
14889 break;
14890 case RID_LONG:
14891 if (decl_specs)
14892 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14893 type = long_integer_type_node;
14894 break;
14895 case RID_SIGNED:
14896 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14897 type = integer_type_node;
14898 break;
14899 case RID_UNSIGNED:
14900 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14901 type = unsigned_type_node;
14902 break;
14903 case RID_FLOAT:
14904 type = float_type_node;
14905 break;
14906 case RID_DOUBLE:
14907 type = double_type_node;
14908 break;
14909 case RID_VOID:
14910 type = void_type_node;
14911 break;
14913 case RID_AUTO:
14914 maybe_warn_cpp0x (CPP0X_AUTO);
14915 if (parser->auto_is_implicit_function_template_parm_p)
14917 if (cxx_dialect >= cxx14)
14918 type = synthesize_implicit_template_parm (parser);
14919 else
14920 type = error_mark_node;
14922 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14924 if (cxx_dialect < cxx14)
14925 error_at (token->location,
14926 "use of %<auto%> in lambda parameter declaration "
14927 "only available with "
14928 "-std=c++14 or -std=gnu++14");
14930 else if (cxx_dialect < cxx14)
14931 error_at (token->location,
14932 "use of %<auto%> in parameter declaration "
14933 "only available with "
14934 "-std=c++14 or -std=gnu++14");
14935 else
14936 pedwarn (token->location, OPT_Wpedantic,
14937 "ISO C++ forbids use of %<auto%> in parameter "
14938 "declaration");
14940 else
14941 type = make_auto ();
14942 break;
14944 case RID_DECLTYPE:
14945 /* Since DR 743, decltype can either be a simple-type-specifier by
14946 itself or begin a nested-name-specifier. Parsing it will replace
14947 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14948 handling below decide what to do. */
14949 cp_parser_decltype (parser);
14950 cp_lexer_set_token_position (parser->lexer, token);
14951 break;
14953 case RID_TYPEOF:
14954 /* Consume the `typeof' token. */
14955 cp_lexer_consume_token (parser->lexer);
14956 /* Parse the operand to `typeof'. */
14957 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14958 /* If it is not already a TYPE, take its type. */
14959 if (!TYPE_P (type))
14960 type = finish_typeof (type);
14962 if (decl_specs)
14963 cp_parser_set_decl_spec_type (decl_specs, type,
14964 token,
14965 /*type_definition_p=*/false);
14967 return type;
14969 case RID_UNDERLYING_TYPE:
14970 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14971 if (decl_specs)
14972 cp_parser_set_decl_spec_type (decl_specs, type,
14973 token,
14974 /*type_definition_p=*/false);
14976 return type;
14978 case RID_BASES:
14979 case RID_DIRECT_BASES:
14980 type = cp_parser_trait_expr (parser, token->keyword);
14981 if (decl_specs)
14982 cp_parser_set_decl_spec_type (decl_specs, type,
14983 token,
14984 /*type_definition_p=*/false);
14985 return type;
14986 default:
14987 break;
14990 /* If token is an already-parsed decltype not followed by ::,
14991 it's a simple-type-specifier. */
14992 if (token->type == CPP_DECLTYPE
14993 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14995 type = token->u.value;
14996 if (decl_specs)
14998 cp_parser_set_decl_spec_type (decl_specs, type,
14999 token,
15000 /*type_definition_p=*/false);
15001 /* Remember that we are handling a decltype in order to
15002 implement the resolution of DR 1510 when the argument
15003 isn't instantiation dependent. */
15004 decl_specs->decltype_p = true;
15006 cp_lexer_consume_token (parser->lexer);
15007 return type;
15010 /* If the type-specifier was for a built-in type, we're done. */
15011 if (type)
15013 /* Record the type. */
15014 if (decl_specs
15015 && (token->keyword != RID_SIGNED
15016 && token->keyword != RID_UNSIGNED
15017 && token->keyword != RID_SHORT
15018 && token->keyword != RID_LONG))
15019 cp_parser_set_decl_spec_type (decl_specs,
15020 type,
15021 token,
15022 /*type_definition_p=*/false);
15023 if (decl_specs)
15024 decl_specs->any_specifiers_p = true;
15026 /* Consume the token. */
15027 cp_lexer_consume_token (parser->lexer);
15029 if (type == error_mark_node)
15030 return error_mark_node;
15032 /* There is no valid C++ program where a non-template type is
15033 followed by a "<". That usually indicates that the user thought
15034 that the type was a template. */
15035 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15036 token->location);
15038 return TYPE_NAME (type);
15041 /* The type-specifier must be a user-defined type. */
15042 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15044 bool qualified_p;
15045 bool global_p;
15047 /* Don't gobble tokens or issue error messages if this is an
15048 optional type-specifier. */
15049 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15050 cp_parser_parse_tentatively (parser);
15052 /* Look for the optional `::' operator. */
15053 global_p
15054 = (cp_parser_global_scope_opt (parser,
15055 /*current_scope_valid_p=*/false)
15056 != NULL_TREE);
15057 /* Look for the nested-name specifier. */
15058 qualified_p
15059 = (cp_parser_nested_name_specifier_opt (parser,
15060 /*typename_keyword_p=*/false,
15061 /*check_dependency_p=*/true,
15062 /*type_p=*/false,
15063 /*is_declaration=*/false)
15064 != NULL_TREE);
15065 token = cp_lexer_peek_token (parser->lexer);
15066 /* If we have seen a nested-name-specifier, and the next token
15067 is `template', then we are using the template-id production. */
15068 if (parser->scope
15069 && cp_parser_optional_template_keyword (parser))
15071 /* Look for the template-id. */
15072 type = cp_parser_template_id (parser,
15073 /*template_keyword_p=*/true,
15074 /*check_dependency_p=*/true,
15075 none_type,
15076 /*is_declaration=*/false);
15077 /* If the template-id did not name a type, we are out of
15078 luck. */
15079 if (TREE_CODE (type) != TYPE_DECL)
15081 cp_parser_error (parser, "expected template-id for type");
15082 type = NULL_TREE;
15085 /* Otherwise, look for a type-name. */
15086 else
15087 type = cp_parser_type_name (parser);
15088 /* Keep track of all name-lookups performed in class scopes. */
15089 if (type
15090 && !global_p
15091 && !qualified_p
15092 && TREE_CODE (type) == TYPE_DECL
15093 && identifier_p (DECL_NAME (type)))
15094 maybe_note_name_used_in_class (DECL_NAME (type), type);
15095 /* If it didn't work out, we don't have a TYPE. */
15096 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15097 && !cp_parser_parse_definitely (parser))
15098 type = NULL_TREE;
15099 if (type && decl_specs)
15100 cp_parser_set_decl_spec_type (decl_specs, type,
15101 token,
15102 /*type_definition_p=*/false);
15105 /* If we didn't get a type-name, issue an error message. */
15106 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15108 cp_parser_error (parser, "expected type-name");
15109 return error_mark_node;
15112 if (type && type != error_mark_node)
15114 /* See if TYPE is an Objective-C type, and if so, parse and
15115 accept any protocol references following it. Do this before
15116 the cp_parser_check_for_invalid_template_id() call, because
15117 Objective-C types can be followed by '<...>' which would
15118 enclose protocol names rather than template arguments, and so
15119 everything is fine. */
15120 if (c_dialect_objc () && !parser->scope
15121 && (objc_is_id (type) || objc_is_class_name (type)))
15123 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15124 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15126 /* Clobber the "unqualified" type previously entered into
15127 DECL_SPECS with the new, improved protocol-qualified version. */
15128 if (decl_specs)
15129 decl_specs->type = qual_type;
15131 return qual_type;
15134 /* There is no valid C++ program where a non-template type is
15135 followed by a "<". That usually indicates that the user
15136 thought that the type was a template. */
15137 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15138 none_type,
15139 token->location);
15142 return type;
15145 /* Parse a type-name.
15147 type-name:
15148 class-name
15149 enum-name
15150 typedef-name
15151 simple-template-id [in c++0x]
15153 enum-name:
15154 identifier
15156 typedef-name:
15157 identifier
15159 Returns a TYPE_DECL for the type. */
15161 static tree
15162 cp_parser_type_name (cp_parser* parser)
15164 tree type_decl;
15166 /* We can't know yet whether it is a class-name or not. */
15167 cp_parser_parse_tentatively (parser);
15168 /* Try a class-name. */
15169 type_decl = cp_parser_class_name (parser,
15170 /*typename_keyword_p=*/false,
15171 /*template_keyword_p=*/false,
15172 none_type,
15173 /*check_dependency_p=*/true,
15174 /*class_head_p=*/false,
15175 /*is_declaration=*/false);
15176 /* If it's not a class-name, keep looking. */
15177 if (!cp_parser_parse_definitely (parser))
15179 if (cxx_dialect < cxx11)
15180 /* It must be a typedef-name or an enum-name. */
15181 return cp_parser_nonclass_name (parser);
15183 cp_parser_parse_tentatively (parser);
15184 /* It is either a simple-template-id representing an
15185 instantiation of an alias template... */
15186 type_decl = cp_parser_template_id (parser,
15187 /*template_keyword_p=*/false,
15188 /*check_dependency_p=*/true,
15189 none_type,
15190 /*is_declaration=*/false);
15191 /* Note that this must be an instantiation of an alias template
15192 because [temp.names]/6 says:
15194 A template-id that names an alias template specialization
15195 is a type-name.
15197 Whereas [temp.names]/7 says:
15199 A simple-template-id that names a class template
15200 specialization is a class-name. */
15201 if (type_decl != NULL_TREE
15202 && TREE_CODE (type_decl) == TYPE_DECL
15203 && TYPE_DECL_ALIAS_P (type_decl))
15204 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15205 else
15206 cp_parser_simulate_error (parser);
15208 if (!cp_parser_parse_definitely (parser))
15209 /* ... Or a typedef-name or an enum-name. */
15210 return cp_parser_nonclass_name (parser);
15213 return type_decl;
15216 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15218 enum-name:
15219 identifier
15221 typedef-name:
15222 identifier
15224 Returns a TYPE_DECL for the type. */
15226 static tree
15227 cp_parser_nonclass_name (cp_parser* parser)
15229 tree type_decl;
15230 tree identifier;
15232 cp_token *token = cp_lexer_peek_token (parser->lexer);
15233 identifier = cp_parser_identifier (parser);
15234 if (identifier == error_mark_node)
15235 return error_mark_node;
15237 /* Look up the type-name. */
15238 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15240 type_decl = strip_using_decl (type_decl);
15242 if (TREE_CODE (type_decl) != TYPE_DECL
15243 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15245 /* See if this is an Objective-C type. */
15246 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15247 tree type = objc_get_protocol_qualified_type (identifier, protos);
15248 if (type)
15249 type_decl = TYPE_NAME (type);
15252 /* Issue an error if we did not find a type-name. */
15253 if (TREE_CODE (type_decl) != TYPE_DECL
15254 /* In Objective-C, we have the complication that class names are
15255 normally type names and start declarations (eg, the
15256 "NSObject" in "NSObject *object;"), but can be used in an
15257 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15258 is an expression. So, a classname followed by a dot is not a
15259 valid type-name. */
15260 || (objc_is_class_name (TREE_TYPE (type_decl))
15261 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15263 if (!cp_parser_simulate_error (parser))
15264 cp_parser_name_lookup_error (parser, identifier, type_decl,
15265 NLE_TYPE, token->location);
15266 return error_mark_node;
15268 /* Remember that the name was used in the definition of the
15269 current class so that we can check later to see if the
15270 meaning would have been different after the class was
15271 entirely defined. */
15272 else if (type_decl != error_mark_node
15273 && !parser->scope)
15274 maybe_note_name_used_in_class (identifier, type_decl);
15276 return type_decl;
15279 /* Parse an elaborated-type-specifier. Note that the grammar given
15280 here incorporates the resolution to DR68.
15282 elaborated-type-specifier:
15283 class-key :: [opt] nested-name-specifier [opt] identifier
15284 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15285 enum-key :: [opt] nested-name-specifier [opt] identifier
15286 typename :: [opt] nested-name-specifier identifier
15287 typename :: [opt] nested-name-specifier template [opt]
15288 template-id
15290 GNU extension:
15292 elaborated-type-specifier:
15293 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15294 class-key attributes :: [opt] nested-name-specifier [opt]
15295 template [opt] template-id
15296 enum attributes :: [opt] nested-name-specifier [opt] identifier
15298 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15299 declared `friend'. If IS_DECLARATION is TRUE, then this
15300 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15301 something is being declared.
15303 Returns the TYPE specified. */
15305 static tree
15306 cp_parser_elaborated_type_specifier (cp_parser* parser,
15307 bool is_friend,
15308 bool is_declaration)
15310 enum tag_types tag_type;
15311 tree identifier;
15312 tree type = NULL_TREE;
15313 tree attributes = NULL_TREE;
15314 tree globalscope;
15315 cp_token *token = NULL;
15317 /* See if we're looking at the `enum' keyword. */
15318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15320 /* Consume the `enum' token. */
15321 cp_lexer_consume_token (parser->lexer);
15322 /* Remember that it's an enumeration type. */
15323 tag_type = enum_type;
15324 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15325 enums) is used here. */
15326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15327 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15329 pedwarn (input_location, 0, "elaborated-type-specifier "
15330 "for a scoped enum must not use the %<%D%> keyword",
15331 cp_lexer_peek_token (parser->lexer)->u.value);
15332 /* Consume the `struct' or `class' and parse it anyway. */
15333 cp_lexer_consume_token (parser->lexer);
15335 /* Parse the attributes. */
15336 attributes = cp_parser_attributes_opt (parser);
15338 /* Or, it might be `typename'. */
15339 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15340 RID_TYPENAME))
15342 /* Consume the `typename' token. */
15343 cp_lexer_consume_token (parser->lexer);
15344 /* Remember that it's a `typename' type. */
15345 tag_type = typename_type;
15347 /* Otherwise it must be a class-key. */
15348 else
15350 tag_type = cp_parser_class_key (parser);
15351 if (tag_type == none_type)
15352 return error_mark_node;
15353 /* Parse the attributes. */
15354 attributes = cp_parser_attributes_opt (parser);
15357 /* Look for the `::' operator. */
15358 globalscope = cp_parser_global_scope_opt (parser,
15359 /*current_scope_valid_p=*/false);
15360 /* Look for the nested-name-specifier. */
15361 if (tag_type == typename_type && !globalscope)
15363 if (!cp_parser_nested_name_specifier (parser,
15364 /*typename_keyword_p=*/true,
15365 /*check_dependency_p=*/true,
15366 /*type_p=*/true,
15367 is_declaration))
15368 return error_mark_node;
15370 else
15371 /* Even though `typename' is not present, the proposed resolution
15372 to Core Issue 180 says that in `class A<T>::B', `B' should be
15373 considered a type-name, even if `A<T>' is dependent. */
15374 cp_parser_nested_name_specifier_opt (parser,
15375 /*typename_keyword_p=*/true,
15376 /*check_dependency_p=*/true,
15377 /*type_p=*/true,
15378 is_declaration);
15379 /* For everything but enumeration types, consider a template-id.
15380 For an enumeration type, consider only a plain identifier. */
15381 if (tag_type != enum_type)
15383 bool template_p = false;
15384 tree decl;
15386 /* Allow the `template' keyword. */
15387 template_p = cp_parser_optional_template_keyword (parser);
15388 /* If we didn't see `template', we don't know if there's a
15389 template-id or not. */
15390 if (!template_p)
15391 cp_parser_parse_tentatively (parser);
15392 /* Parse the template-id. */
15393 token = cp_lexer_peek_token (parser->lexer);
15394 decl = cp_parser_template_id (parser, template_p,
15395 /*check_dependency_p=*/true,
15396 tag_type,
15397 is_declaration);
15398 /* If we didn't find a template-id, look for an ordinary
15399 identifier. */
15400 if (!template_p && !cp_parser_parse_definitely (parser))
15402 /* We can get here when cp_parser_template_id, called by
15403 cp_parser_class_name with tag_type == none_type, succeeds
15404 and caches a BASELINK. Then, when called again here,
15405 instead of failing and returning an error_mark_node
15406 returns it (see template/typename17.C in C++11).
15407 ??? Could we diagnose this earlier? */
15408 else if (tag_type == typename_type && BASELINK_P (decl))
15410 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15411 type = error_mark_node;
15413 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15414 in effect, then we must assume that, upon instantiation, the
15415 template will correspond to a class. */
15416 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15417 && tag_type == typename_type)
15418 type = make_typename_type (parser->scope, decl,
15419 typename_type,
15420 /*complain=*/tf_error);
15421 /* If the `typename' keyword is in effect and DECL is not a type
15422 decl, then type is non existent. */
15423 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15425 else if (TREE_CODE (decl) == TYPE_DECL)
15426 type = check_elaborated_type_specifier (tag_type, decl,
15427 /*allow_template_p=*/true);
15428 else if (decl == error_mark_node)
15429 type = error_mark_node;
15432 if (!type)
15434 token = cp_lexer_peek_token (parser->lexer);
15435 identifier = cp_parser_identifier (parser);
15437 if (identifier == error_mark_node)
15439 parser->scope = NULL_TREE;
15440 return error_mark_node;
15443 /* For a `typename', we needn't call xref_tag. */
15444 if (tag_type == typename_type
15445 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15446 return cp_parser_make_typename_type (parser, identifier,
15447 token->location);
15449 /* Template parameter lists apply only if we are not within a
15450 function parameter list. */
15451 bool template_parm_lists_apply
15452 = parser->num_template_parameter_lists;
15453 if (template_parm_lists_apply)
15454 for (cp_binding_level *s = current_binding_level;
15455 s && s->kind != sk_template_parms;
15456 s = s->level_chain)
15457 if (s->kind == sk_function_parms)
15458 template_parm_lists_apply = false;
15460 /* Look up a qualified name in the usual way. */
15461 if (parser->scope)
15463 tree decl;
15464 tree ambiguous_decls;
15466 decl = cp_parser_lookup_name (parser, identifier,
15467 tag_type,
15468 /*is_template=*/false,
15469 /*is_namespace=*/false,
15470 /*check_dependency=*/true,
15471 &ambiguous_decls,
15472 token->location);
15474 /* If the lookup was ambiguous, an error will already have been
15475 issued. */
15476 if (ambiguous_decls)
15477 return error_mark_node;
15479 /* If we are parsing friend declaration, DECL may be a
15480 TEMPLATE_DECL tree node here. However, we need to check
15481 whether this TEMPLATE_DECL results in valid code. Consider
15482 the following example:
15484 namespace N {
15485 template <class T> class C {};
15487 class X {
15488 template <class T> friend class N::C; // #1, valid code
15490 template <class T> class Y {
15491 friend class N::C; // #2, invalid code
15494 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15495 name lookup of `N::C'. We see that friend declaration must
15496 be template for the code to be valid. Note that
15497 processing_template_decl does not work here since it is
15498 always 1 for the above two cases. */
15500 decl = (cp_parser_maybe_treat_template_as_class
15501 (decl, /*tag_name_p=*/is_friend
15502 && template_parm_lists_apply));
15504 if (TREE_CODE (decl) != TYPE_DECL)
15506 cp_parser_diagnose_invalid_type_name (parser,
15507 identifier,
15508 token->location);
15509 return error_mark_node;
15512 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15514 bool allow_template = (template_parm_lists_apply
15515 || DECL_SELF_REFERENCE_P (decl));
15516 type = check_elaborated_type_specifier (tag_type, decl,
15517 allow_template);
15519 if (type == error_mark_node)
15520 return error_mark_node;
15523 /* Forward declarations of nested types, such as
15525 class C1::C2;
15526 class C1::C2::C3;
15528 are invalid unless all components preceding the final '::'
15529 are complete. If all enclosing types are complete, these
15530 declarations become merely pointless.
15532 Invalid forward declarations of nested types are errors
15533 caught elsewhere in parsing. Those that are pointless arrive
15534 here. */
15536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15537 && !is_friend && !processing_explicit_instantiation)
15538 warning (0, "declaration %qD does not declare anything", decl);
15540 type = TREE_TYPE (decl);
15542 else
15544 /* An elaborated-type-specifier sometimes introduces a new type and
15545 sometimes names an existing type. Normally, the rule is that it
15546 introduces a new type only if there is not an existing type of
15547 the same name already in scope. For example, given:
15549 struct S {};
15550 void f() { struct S s; }
15552 the `struct S' in the body of `f' is the same `struct S' as in
15553 the global scope; the existing definition is used. However, if
15554 there were no global declaration, this would introduce a new
15555 local class named `S'.
15557 An exception to this rule applies to the following code:
15559 namespace N { struct S; }
15561 Here, the elaborated-type-specifier names a new type
15562 unconditionally; even if there is already an `S' in the
15563 containing scope this declaration names a new type.
15564 This exception only applies if the elaborated-type-specifier
15565 forms the complete declaration:
15567 [class.name]
15569 A declaration consisting solely of `class-key identifier ;' is
15570 either a redeclaration of the name in the current scope or a
15571 forward declaration of the identifier as a class name. It
15572 introduces the name into the current scope.
15574 We are in this situation precisely when the next token is a `;'.
15576 An exception to the exception is that a `friend' declaration does
15577 *not* name a new type; i.e., given:
15579 struct S { friend struct T; };
15581 `T' is not a new type in the scope of `S'.
15583 Also, `new struct S' or `sizeof (struct S)' never results in the
15584 definition of a new type; a new type can only be declared in a
15585 declaration context. */
15587 tag_scope ts;
15588 bool template_p;
15590 if (is_friend)
15591 /* Friends have special name lookup rules. */
15592 ts = ts_within_enclosing_non_class;
15593 else if (is_declaration
15594 && cp_lexer_next_token_is (parser->lexer,
15595 CPP_SEMICOLON))
15596 /* This is a `class-key identifier ;' */
15597 ts = ts_current;
15598 else
15599 ts = ts_global;
15601 template_p =
15602 (template_parm_lists_apply
15603 && (cp_parser_next_token_starts_class_definition_p (parser)
15604 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15605 /* An unqualified name was used to reference this type, so
15606 there were no qualifying templates. */
15607 if (template_parm_lists_apply
15608 && !cp_parser_check_template_parameters (parser,
15609 /*num_templates=*/0,
15610 token->location,
15611 /*declarator=*/NULL))
15612 return error_mark_node;
15613 type = xref_tag (tag_type, identifier, ts, template_p);
15617 if (type == error_mark_node)
15618 return error_mark_node;
15620 /* Allow attributes on forward declarations of classes. */
15621 if (attributes)
15623 if (TREE_CODE (type) == TYPENAME_TYPE)
15624 warning (OPT_Wattributes,
15625 "attributes ignored on uninstantiated type");
15626 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15627 && ! processing_explicit_instantiation)
15628 warning (OPT_Wattributes,
15629 "attributes ignored on template instantiation");
15630 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15631 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15632 else
15633 warning (OPT_Wattributes,
15634 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15637 if (tag_type != enum_type)
15639 /* Indicate whether this class was declared as a `class' or as a
15640 `struct'. */
15641 if (TREE_CODE (type) == RECORD_TYPE)
15642 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15643 cp_parser_check_class_key (tag_type, type);
15646 /* A "<" cannot follow an elaborated type specifier. If that
15647 happens, the user was probably trying to form a template-id. */
15648 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15649 token->location);
15651 return type;
15654 /* Parse an enum-specifier.
15656 enum-specifier:
15657 enum-head { enumerator-list [opt] }
15658 enum-head { enumerator-list , } [C++0x]
15660 enum-head:
15661 enum-key identifier [opt] enum-base [opt]
15662 enum-key nested-name-specifier identifier enum-base [opt]
15664 enum-key:
15665 enum
15666 enum class [C++0x]
15667 enum struct [C++0x]
15669 enum-base: [C++0x]
15670 : type-specifier-seq
15672 opaque-enum-specifier:
15673 enum-key identifier enum-base [opt] ;
15675 GNU Extensions:
15676 enum-key attributes[opt] identifier [opt] enum-base [opt]
15677 { enumerator-list [opt] }attributes[opt]
15678 enum-key attributes[opt] identifier [opt] enum-base [opt]
15679 { enumerator-list, }attributes[opt] [C++0x]
15681 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15682 if the token stream isn't an enum-specifier after all. */
15684 static tree
15685 cp_parser_enum_specifier (cp_parser* parser)
15687 tree identifier;
15688 tree type = NULL_TREE;
15689 tree prev_scope;
15690 tree nested_name_specifier = NULL_TREE;
15691 tree attributes;
15692 bool scoped_enum_p = false;
15693 bool has_underlying_type = false;
15694 bool nested_being_defined = false;
15695 bool new_value_list = false;
15696 bool is_new_type = false;
15697 bool is_anonymous = false;
15698 tree underlying_type = NULL_TREE;
15699 cp_token *type_start_token = NULL;
15700 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15702 parser->colon_corrects_to_scope_p = false;
15704 /* Parse tentatively so that we can back up if we don't find a
15705 enum-specifier. */
15706 cp_parser_parse_tentatively (parser);
15708 /* Caller guarantees that the current token is 'enum', an identifier
15709 possibly follows, and the token after that is an opening brace.
15710 If we don't have an identifier, fabricate an anonymous name for
15711 the enumeration being defined. */
15712 cp_lexer_consume_token (parser->lexer);
15714 /* Parse the "class" or "struct", which indicates a scoped
15715 enumeration type in C++0x. */
15716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15717 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15719 if (cxx_dialect < cxx11)
15720 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15722 /* Consume the `struct' or `class' token. */
15723 cp_lexer_consume_token (parser->lexer);
15725 scoped_enum_p = true;
15728 attributes = cp_parser_attributes_opt (parser);
15730 /* Clear the qualification. */
15731 parser->scope = NULL_TREE;
15732 parser->qualifying_scope = NULL_TREE;
15733 parser->object_scope = NULL_TREE;
15735 /* Figure out in what scope the declaration is being placed. */
15736 prev_scope = current_scope ();
15738 type_start_token = cp_lexer_peek_token (parser->lexer);
15740 push_deferring_access_checks (dk_no_check);
15741 nested_name_specifier
15742 = cp_parser_nested_name_specifier_opt (parser,
15743 /*typename_keyword_p=*/true,
15744 /*check_dependency_p=*/false,
15745 /*type_p=*/false,
15746 /*is_declaration=*/false);
15748 if (nested_name_specifier)
15750 tree name;
15752 identifier = cp_parser_identifier (parser);
15753 name = cp_parser_lookup_name (parser, identifier,
15754 enum_type,
15755 /*is_template=*/false,
15756 /*is_namespace=*/false,
15757 /*check_dependency=*/true,
15758 /*ambiguous_decls=*/NULL,
15759 input_location);
15760 if (name && name != error_mark_node)
15762 type = TREE_TYPE (name);
15763 if (TREE_CODE (type) == TYPENAME_TYPE)
15765 /* Are template enums allowed in ISO? */
15766 if (template_parm_scope_p ())
15767 pedwarn (type_start_token->location, OPT_Wpedantic,
15768 "%qD is an enumeration template", name);
15769 /* ignore a typename reference, for it will be solved by name
15770 in start_enum. */
15771 type = NULL_TREE;
15774 else if (nested_name_specifier == error_mark_node)
15775 /* We already issued an error. */;
15776 else
15777 error_at (type_start_token->location,
15778 "%qD is not an enumerator-name", identifier);
15780 else
15782 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15783 identifier = cp_parser_identifier (parser);
15784 else
15786 identifier = make_anon_name ();
15787 is_anonymous = true;
15788 if (scoped_enum_p)
15789 error_at (type_start_token->location,
15790 "anonymous scoped enum is not allowed");
15793 pop_deferring_access_checks ();
15795 /* Check for the `:' that denotes a specified underlying type in C++0x.
15796 Note that a ':' could also indicate a bitfield width, however. */
15797 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15799 cp_decl_specifier_seq type_specifiers;
15801 /* Consume the `:'. */
15802 cp_lexer_consume_token (parser->lexer);
15804 /* Parse the type-specifier-seq. */
15805 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15806 /*is_trailing_return=*/false,
15807 &type_specifiers);
15809 /* At this point this is surely not elaborated type specifier. */
15810 if (!cp_parser_parse_definitely (parser))
15811 return NULL_TREE;
15813 if (cxx_dialect < cxx11)
15814 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15816 has_underlying_type = true;
15818 /* If that didn't work, stop. */
15819 if (type_specifiers.type != error_mark_node)
15821 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15822 /*initialized=*/0, NULL);
15823 if (underlying_type == error_mark_node
15824 || check_for_bare_parameter_packs (underlying_type))
15825 underlying_type = NULL_TREE;
15829 /* Look for the `{' but don't consume it yet. */
15830 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15832 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15834 cp_parser_error (parser, "expected %<{%>");
15835 if (has_underlying_type)
15837 type = NULL_TREE;
15838 goto out;
15841 /* An opaque-enum-specifier must have a ';' here. */
15842 if ((scoped_enum_p || underlying_type)
15843 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15845 cp_parser_error (parser, "expected %<;%> or %<{%>");
15846 if (has_underlying_type)
15848 type = NULL_TREE;
15849 goto out;
15854 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15855 return NULL_TREE;
15857 if (nested_name_specifier)
15859 if (CLASS_TYPE_P (nested_name_specifier))
15861 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15862 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15863 push_scope (nested_name_specifier);
15865 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15867 push_nested_namespace (nested_name_specifier);
15871 /* Issue an error message if type-definitions are forbidden here. */
15872 if (!cp_parser_check_type_definition (parser))
15873 type = error_mark_node;
15874 else
15875 /* Create the new type. We do this before consuming the opening
15876 brace so the enum will be recorded as being on the line of its
15877 tag (or the 'enum' keyword, if there is no tag). */
15878 type = start_enum (identifier, type, underlying_type,
15879 scoped_enum_p, &is_new_type);
15881 /* If the next token is not '{' it is an opaque-enum-specifier or an
15882 elaborated-type-specifier. */
15883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15885 timevar_push (TV_PARSE_ENUM);
15886 if (nested_name_specifier
15887 && nested_name_specifier != error_mark_node)
15889 /* The following catches invalid code such as:
15890 enum class S<int>::E { A, B, C }; */
15891 if (!processing_specialization
15892 && CLASS_TYPE_P (nested_name_specifier)
15893 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15894 error_at (type_start_token->location, "cannot add an enumerator "
15895 "list to a template instantiation");
15897 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15899 error_at (type_start_token->location,
15900 "%<%T::%E%> has not been declared",
15901 TYPE_CONTEXT (nested_name_specifier),
15902 nested_name_specifier);
15903 type = error_mark_node;
15905 /* If that scope does not contain the scope in which the
15906 class was originally declared, the program is invalid. */
15907 else if (prev_scope && !is_ancestor (prev_scope,
15908 nested_name_specifier))
15910 if (at_namespace_scope_p ())
15911 error_at (type_start_token->location,
15912 "declaration of %qD in namespace %qD which does not "
15913 "enclose %qD",
15914 type, prev_scope, nested_name_specifier);
15915 else
15916 error_at (type_start_token->location,
15917 "declaration of %qD in %qD which does not "
15918 "enclose %qD",
15919 type, prev_scope, nested_name_specifier);
15920 type = error_mark_node;
15924 if (scoped_enum_p)
15925 begin_scope (sk_scoped_enum, type);
15927 /* Consume the opening brace. */
15928 cp_lexer_consume_token (parser->lexer);
15930 if (type == error_mark_node)
15931 ; /* Nothing to add */
15932 else if (OPAQUE_ENUM_P (type)
15933 || (cxx_dialect > cxx98 && processing_specialization))
15935 new_value_list = true;
15936 SET_OPAQUE_ENUM_P (type, false);
15937 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15939 else
15941 error_at (type_start_token->location,
15942 "multiple definition of %q#T", type);
15943 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15944 "previous definition here");
15945 type = error_mark_node;
15948 if (type == error_mark_node)
15949 cp_parser_skip_to_end_of_block_or_statement (parser);
15950 /* If the next token is not '}', then there are some enumerators. */
15951 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15953 if (is_anonymous && !scoped_enum_p)
15954 pedwarn (type_start_token->location, OPT_Wpedantic,
15955 "ISO C++ forbids empty anonymous enum");
15957 else
15958 cp_parser_enumerator_list (parser, type);
15960 /* Consume the final '}'. */
15961 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15963 if (scoped_enum_p)
15964 finish_scope ();
15965 timevar_pop (TV_PARSE_ENUM);
15967 else
15969 /* If a ';' follows, then it is an opaque-enum-specifier
15970 and additional restrictions apply. */
15971 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15973 if (is_anonymous)
15974 error_at (type_start_token->location,
15975 "opaque-enum-specifier without name");
15976 else if (nested_name_specifier)
15977 error_at (type_start_token->location,
15978 "opaque-enum-specifier must use a simple identifier");
15982 /* Look for trailing attributes to apply to this enumeration, and
15983 apply them if appropriate. */
15984 if (cp_parser_allow_gnu_extensions_p (parser))
15986 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15987 trailing_attr = chainon (trailing_attr, attributes);
15988 cplus_decl_attributes (&type,
15989 trailing_attr,
15990 (int) ATTR_FLAG_TYPE_IN_PLACE);
15993 /* Finish up the enumeration. */
15994 if (type != error_mark_node)
15996 if (new_value_list)
15997 finish_enum_value_list (type);
15998 if (is_new_type)
15999 finish_enum (type);
16002 if (nested_name_specifier)
16004 if (CLASS_TYPE_P (nested_name_specifier))
16006 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16007 pop_scope (nested_name_specifier);
16009 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16011 pop_nested_namespace (nested_name_specifier);
16014 out:
16015 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16016 return type;
16019 /* Parse an enumerator-list. The enumerators all have the indicated
16020 TYPE.
16022 enumerator-list:
16023 enumerator-definition
16024 enumerator-list , enumerator-definition */
16026 static void
16027 cp_parser_enumerator_list (cp_parser* parser, tree type)
16029 while (true)
16031 /* Parse an enumerator-definition. */
16032 cp_parser_enumerator_definition (parser, type);
16034 /* If the next token is not a ',', we've reached the end of
16035 the list. */
16036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16037 break;
16038 /* Otherwise, consume the `,' and keep going. */
16039 cp_lexer_consume_token (parser->lexer);
16040 /* If the next token is a `}', there is a trailing comma. */
16041 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16043 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16044 pedwarn (input_location, OPT_Wpedantic,
16045 "comma at end of enumerator list");
16046 break;
16051 /* Parse an enumerator-definition. The enumerator has the indicated
16052 TYPE.
16054 enumerator-definition:
16055 enumerator
16056 enumerator = constant-expression
16058 enumerator:
16059 identifier */
16061 static void
16062 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16064 tree identifier;
16065 tree value;
16066 location_t loc;
16068 /* Save the input location because we are interested in the location
16069 of the identifier and not the location of the explicit value. */
16070 loc = cp_lexer_peek_token (parser->lexer)->location;
16072 /* Look for the identifier. */
16073 identifier = cp_parser_identifier (parser);
16074 if (identifier == error_mark_node)
16075 return;
16077 /* If the next token is an '=', then there is an explicit value. */
16078 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16080 /* Consume the `=' token. */
16081 cp_lexer_consume_token (parser->lexer);
16082 /* Parse the value. */
16083 value = cp_parser_constant_expression (parser);
16085 else
16086 value = NULL_TREE;
16088 /* If we are processing a template, make sure the initializer of the
16089 enumerator doesn't contain any bare template parameter pack. */
16090 if (check_for_bare_parameter_packs (value))
16091 value = error_mark_node;
16093 /* Create the enumerator. */
16094 build_enumerator (identifier, value, type, loc);
16097 /* Parse a namespace-name.
16099 namespace-name:
16100 original-namespace-name
16101 namespace-alias
16103 Returns the NAMESPACE_DECL for the namespace. */
16105 static tree
16106 cp_parser_namespace_name (cp_parser* parser)
16108 tree identifier;
16109 tree namespace_decl;
16111 cp_token *token = cp_lexer_peek_token (parser->lexer);
16113 /* Get the name of the namespace. */
16114 identifier = cp_parser_identifier (parser);
16115 if (identifier == error_mark_node)
16116 return error_mark_node;
16118 /* Look up the identifier in the currently active scope. Look only
16119 for namespaces, due to:
16121 [basic.lookup.udir]
16123 When looking up a namespace-name in a using-directive or alias
16124 definition, only namespace names are considered.
16126 And:
16128 [basic.lookup.qual]
16130 During the lookup of a name preceding the :: scope resolution
16131 operator, object, function, and enumerator names are ignored.
16133 (Note that cp_parser_qualifying_entity only calls this
16134 function if the token after the name is the scope resolution
16135 operator.) */
16136 namespace_decl = cp_parser_lookup_name (parser, identifier,
16137 none_type,
16138 /*is_template=*/false,
16139 /*is_namespace=*/true,
16140 /*check_dependency=*/true,
16141 /*ambiguous_decls=*/NULL,
16142 token->location);
16143 /* If it's not a namespace, issue an error. */
16144 if (namespace_decl == error_mark_node
16145 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16147 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16148 error_at (token->location, "%qD is not a namespace-name", identifier);
16149 cp_parser_error (parser, "expected namespace-name");
16150 namespace_decl = error_mark_node;
16153 return namespace_decl;
16156 /* Parse a namespace-definition.
16158 namespace-definition:
16159 named-namespace-definition
16160 unnamed-namespace-definition
16162 named-namespace-definition:
16163 original-namespace-definition
16164 extension-namespace-definition
16166 original-namespace-definition:
16167 namespace identifier { namespace-body }
16169 extension-namespace-definition:
16170 namespace original-namespace-name { namespace-body }
16172 unnamed-namespace-definition:
16173 namespace { namespace-body } */
16175 static void
16176 cp_parser_namespace_definition (cp_parser* parser)
16178 tree identifier, attribs;
16179 bool has_visibility;
16180 bool is_inline;
16182 cp_ensure_no_omp_declare_simd (parser);
16183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16185 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16186 is_inline = true;
16187 cp_lexer_consume_token (parser->lexer);
16189 else
16190 is_inline = false;
16192 /* Look for the `namespace' keyword. */
16193 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16195 /* Get the name of the namespace. We do not attempt to distinguish
16196 between an original-namespace-definition and an
16197 extension-namespace-definition at this point. The semantic
16198 analysis routines are responsible for that. */
16199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16200 identifier = cp_parser_identifier (parser);
16201 else
16202 identifier = NULL_TREE;
16204 /* Parse any specified attributes. */
16205 attribs = cp_parser_attributes_opt (parser);
16207 /* Look for the `{' to start the namespace. */
16208 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16209 /* Start the namespace. */
16210 push_namespace (identifier);
16212 /* "inline namespace" is equivalent to a stub namespace definition
16213 followed by a strong using directive. */
16214 if (is_inline)
16216 tree name_space = current_namespace;
16217 /* Set up namespace association. */
16218 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16219 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16220 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16221 /* Import the contents of the inline namespace. */
16222 pop_namespace ();
16223 do_using_directive (name_space);
16224 push_namespace (identifier);
16227 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16229 /* Parse the body of the namespace. */
16230 cp_parser_namespace_body (parser);
16232 if (has_visibility)
16233 pop_visibility (1);
16235 /* Finish the namespace. */
16236 pop_namespace ();
16237 /* Look for the final `}'. */
16238 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16241 /* Parse a namespace-body.
16243 namespace-body:
16244 declaration-seq [opt] */
16246 static void
16247 cp_parser_namespace_body (cp_parser* parser)
16249 cp_parser_declaration_seq_opt (parser);
16252 /* Parse a namespace-alias-definition.
16254 namespace-alias-definition:
16255 namespace identifier = qualified-namespace-specifier ; */
16257 static void
16258 cp_parser_namespace_alias_definition (cp_parser* parser)
16260 tree identifier;
16261 tree namespace_specifier;
16263 cp_token *token = cp_lexer_peek_token (parser->lexer);
16265 /* Look for the `namespace' keyword. */
16266 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16267 /* Look for the identifier. */
16268 identifier = cp_parser_identifier (parser);
16269 if (identifier == error_mark_node)
16270 return;
16271 /* Look for the `=' token. */
16272 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16273 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16275 error_at (token->location, "%<namespace%> definition is not allowed here");
16276 /* Skip the definition. */
16277 cp_lexer_consume_token (parser->lexer);
16278 if (cp_parser_skip_to_closing_brace (parser))
16279 cp_lexer_consume_token (parser->lexer);
16280 return;
16282 cp_parser_require (parser, CPP_EQ, RT_EQ);
16283 /* Look for the qualified-namespace-specifier. */
16284 namespace_specifier
16285 = cp_parser_qualified_namespace_specifier (parser);
16286 /* Look for the `;' token. */
16287 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16289 /* Register the alias in the symbol table. */
16290 do_namespace_alias (identifier, namespace_specifier);
16293 /* Parse a qualified-namespace-specifier.
16295 qualified-namespace-specifier:
16296 :: [opt] nested-name-specifier [opt] namespace-name
16298 Returns a NAMESPACE_DECL corresponding to the specified
16299 namespace. */
16301 static tree
16302 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16304 /* Look for the optional `::'. */
16305 cp_parser_global_scope_opt (parser,
16306 /*current_scope_valid_p=*/false);
16308 /* Look for the optional nested-name-specifier. */
16309 cp_parser_nested_name_specifier_opt (parser,
16310 /*typename_keyword_p=*/false,
16311 /*check_dependency_p=*/true,
16312 /*type_p=*/false,
16313 /*is_declaration=*/true);
16315 return cp_parser_namespace_name (parser);
16318 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16319 access declaration.
16321 using-declaration:
16322 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16323 using :: unqualified-id ;
16325 access-declaration:
16326 qualified-id ;
16330 static bool
16331 cp_parser_using_declaration (cp_parser* parser,
16332 bool access_declaration_p)
16334 cp_token *token;
16335 bool typename_p = false;
16336 bool global_scope_p;
16337 tree decl;
16338 tree identifier;
16339 tree qscope;
16340 int oldcount = errorcount;
16341 cp_token *diag_token = NULL;
16343 if (access_declaration_p)
16345 diag_token = cp_lexer_peek_token (parser->lexer);
16346 cp_parser_parse_tentatively (parser);
16348 else
16350 /* Look for the `using' keyword. */
16351 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16353 /* Peek at the next token. */
16354 token = cp_lexer_peek_token (parser->lexer);
16355 /* See if it's `typename'. */
16356 if (token->keyword == RID_TYPENAME)
16358 /* Remember that we've seen it. */
16359 typename_p = true;
16360 /* Consume the `typename' token. */
16361 cp_lexer_consume_token (parser->lexer);
16365 /* Look for the optional global scope qualification. */
16366 global_scope_p
16367 = (cp_parser_global_scope_opt (parser,
16368 /*current_scope_valid_p=*/false)
16369 != NULL_TREE);
16371 /* If we saw `typename', or didn't see `::', then there must be a
16372 nested-name-specifier present. */
16373 if (typename_p || !global_scope_p)
16375 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16376 /*check_dependency_p=*/true,
16377 /*type_p=*/false,
16378 /*is_declaration=*/true);
16379 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16381 cp_parser_skip_to_end_of_block_or_statement (parser);
16382 return false;
16385 /* Otherwise, we could be in either of the two productions. In that
16386 case, treat the nested-name-specifier as optional. */
16387 else
16388 qscope = cp_parser_nested_name_specifier_opt (parser,
16389 /*typename_keyword_p=*/false,
16390 /*check_dependency_p=*/true,
16391 /*type_p=*/false,
16392 /*is_declaration=*/true);
16393 if (!qscope)
16394 qscope = global_namespace;
16395 else if (UNSCOPED_ENUM_P (qscope))
16396 qscope = CP_TYPE_CONTEXT (qscope);
16398 if (access_declaration_p && cp_parser_error_occurred (parser))
16399 /* Something has already gone wrong; there's no need to parse
16400 further. Since an error has occurred, the return value of
16401 cp_parser_parse_definitely will be false, as required. */
16402 return cp_parser_parse_definitely (parser);
16404 token = cp_lexer_peek_token (parser->lexer);
16405 /* Parse the unqualified-id. */
16406 identifier = cp_parser_unqualified_id (parser,
16407 /*template_keyword_p=*/false,
16408 /*check_dependency_p=*/true,
16409 /*declarator_p=*/true,
16410 /*optional_p=*/false);
16412 if (access_declaration_p)
16414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16415 cp_parser_simulate_error (parser);
16416 if (!cp_parser_parse_definitely (parser))
16417 return false;
16420 /* The function we call to handle a using-declaration is different
16421 depending on what scope we are in. */
16422 if (qscope == error_mark_node || identifier == error_mark_node)
16424 else if (!identifier_p (identifier)
16425 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16426 /* [namespace.udecl]
16428 A using declaration shall not name a template-id. */
16429 error_at (token->location,
16430 "a template-id may not appear in a using-declaration");
16431 else
16433 if (at_class_scope_p ())
16435 /* Create the USING_DECL. */
16436 decl = do_class_using_decl (parser->scope, identifier);
16438 if (decl && typename_p)
16439 USING_DECL_TYPENAME_P (decl) = 1;
16441 if (check_for_bare_parameter_packs (decl))
16443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16444 return false;
16446 else
16447 /* Add it to the list of members in this class. */
16448 finish_member_declaration (decl);
16450 else
16452 decl = cp_parser_lookup_name_simple (parser,
16453 identifier,
16454 token->location);
16455 if (decl == error_mark_node)
16456 cp_parser_name_lookup_error (parser, identifier,
16457 decl, NLE_NULL,
16458 token->location);
16459 else if (check_for_bare_parameter_packs (decl))
16461 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16462 return false;
16464 else if (!at_namespace_scope_p ())
16465 do_local_using_decl (decl, qscope, identifier);
16466 else
16467 do_toplevel_using_decl (decl, qscope, identifier);
16471 /* Look for the final `;'. */
16472 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16474 if (access_declaration_p && errorcount == oldcount)
16475 warning_at (diag_token->location, OPT_Wdeprecated,
16476 "access declarations are deprecated "
16477 "in favour of using-declarations; "
16478 "suggestion: add the %<using%> keyword");
16480 return true;
16483 /* Parse an alias-declaration.
16485 alias-declaration:
16486 using identifier attribute-specifier-seq [opt] = type-id */
16488 static tree
16489 cp_parser_alias_declaration (cp_parser* parser)
16491 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16492 location_t id_location;
16493 cp_declarator *declarator;
16494 cp_decl_specifier_seq decl_specs;
16495 bool member_p;
16496 const char *saved_message = NULL;
16498 /* Look for the `using' keyword. */
16499 cp_token *using_token
16500 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16501 if (using_token == NULL)
16502 return error_mark_node;
16504 id_location = cp_lexer_peek_token (parser->lexer)->location;
16505 id = cp_parser_identifier (parser);
16506 if (id == error_mark_node)
16507 return error_mark_node;
16509 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16510 attributes = cp_parser_attributes_opt (parser);
16511 if (attributes == error_mark_node)
16512 return error_mark_node;
16514 cp_parser_require (parser, CPP_EQ, RT_EQ);
16516 if (cp_parser_error_occurred (parser))
16517 return error_mark_node;
16519 cp_parser_commit_to_tentative_parse (parser);
16521 /* Now we are going to parse the type-id of the declaration. */
16524 [dcl.type]/3 says:
16526 "A type-specifier-seq shall not define a class or enumeration
16527 unless it appears in the type-id of an alias-declaration (7.1.3) that
16528 is not the declaration of a template-declaration."
16530 In other words, if we currently are in an alias template, the
16531 type-id should not define a type.
16533 So let's set parser->type_definition_forbidden_message in that
16534 case; cp_parser_check_type_definition (called by
16535 cp_parser_class_specifier) will then emit an error if a type is
16536 defined in the type-id. */
16537 if (parser->num_template_parameter_lists)
16539 saved_message = parser->type_definition_forbidden_message;
16540 parser->type_definition_forbidden_message =
16541 G_("types may not be defined in alias template declarations");
16544 type = cp_parser_type_id (parser);
16546 /* Restore the error message if need be. */
16547 if (parser->num_template_parameter_lists)
16548 parser->type_definition_forbidden_message = saved_message;
16550 if (type == error_mark_node
16551 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16553 cp_parser_skip_to_end_of_block_or_statement (parser);
16554 return error_mark_node;
16557 /* A typedef-name can also be introduced by an alias-declaration. The
16558 identifier following the using keyword becomes a typedef-name. It has
16559 the same semantics as if it were introduced by the typedef
16560 specifier. In particular, it does not define a new type and it shall
16561 not appear in the type-id. */
16563 clear_decl_specs (&decl_specs);
16564 decl_specs.type = type;
16565 if (attributes != NULL_TREE)
16567 decl_specs.attributes = attributes;
16568 set_and_check_decl_spec_loc (&decl_specs,
16569 ds_attribute,
16570 attrs_token);
16572 set_and_check_decl_spec_loc (&decl_specs,
16573 ds_typedef,
16574 using_token);
16575 set_and_check_decl_spec_loc (&decl_specs,
16576 ds_alias,
16577 using_token);
16579 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16580 declarator->id_loc = id_location;
16582 member_p = at_class_scope_p ();
16583 if (member_p)
16584 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16585 NULL_TREE, attributes);
16586 else
16587 decl = start_decl (declarator, &decl_specs, 0,
16588 attributes, NULL_TREE, &pushed_scope);
16589 if (decl == error_mark_node)
16590 return decl;
16592 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16594 if (pushed_scope)
16595 pop_scope (pushed_scope);
16597 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16598 added into the symbol table; otherwise, return the TYPE_DECL. */
16599 if (DECL_LANG_SPECIFIC (decl)
16600 && DECL_TEMPLATE_INFO (decl)
16601 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16603 decl = DECL_TI_TEMPLATE (decl);
16604 if (member_p)
16605 check_member_template (decl);
16608 return decl;
16611 /* Parse a using-directive.
16613 using-directive:
16614 using namespace :: [opt] nested-name-specifier [opt]
16615 namespace-name ; */
16617 static void
16618 cp_parser_using_directive (cp_parser* parser)
16620 tree namespace_decl;
16621 tree attribs;
16623 /* Look for the `using' keyword. */
16624 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16625 /* And the `namespace' keyword. */
16626 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16627 /* Look for the optional `::' operator. */
16628 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16629 /* And the optional nested-name-specifier. */
16630 cp_parser_nested_name_specifier_opt (parser,
16631 /*typename_keyword_p=*/false,
16632 /*check_dependency_p=*/true,
16633 /*type_p=*/false,
16634 /*is_declaration=*/true);
16635 /* Get the namespace being used. */
16636 namespace_decl = cp_parser_namespace_name (parser);
16637 /* And any specified attributes. */
16638 attribs = cp_parser_attributes_opt (parser);
16639 /* Update the symbol table. */
16640 parse_using_directive (namespace_decl, attribs);
16641 /* Look for the final `;'. */
16642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16645 /* Parse an asm-definition.
16647 asm-definition:
16648 asm ( string-literal ) ;
16650 GNU Extension:
16652 asm-definition:
16653 asm volatile [opt] ( string-literal ) ;
16654 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16655 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16656 : asm-operand-list [opt] ) ;
16657 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16658 : asm-operand-list [opt]
16659 : asm-clobber-list [opt] ) ;
16660 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16661 : asm-clobber-list [opt]
16662 : asm-goto-list ) ; */
16664 static void
16665 cp_parser_asm_definition (cp_parser* parser)
16667 tree string;
16668 tree outputs = NULL_TREE;
16669 tree inputs = NULL_TREE;
16670 tree clobbers = NULL_TREE;
16671 tree labels = NULL_TREE;
16672 tree asm_stmt;
16673 bool volatile_p = false;
16674 bool extended_p = false;
16675 bool invalid_inputs_p = false;
16676 bool invalid_outputs_p = false;
16677 bool goto_p = false;
16678 required_token missing = RT_NONE;
16680 /* Look for the `asm' keyword. */
16681 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16683 if (parser->in_function_body
16684 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16686 error ("%<asm%> in %<constexpr%> function");
16687 cp_function_chain->invalid_constexpr = true;
16690 /* See if the next token is `volatile'. */
16691 if (cp_parser_allow_gnu_extensions_p (parser)
16692 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16694 /* Remember that we saw the `volatile' keyword. */
16695 volatile_p = true;
16696 /* Consume the token. */
16697 cp_lexer_consume_token (parser->lexer);
16699 if (cp_parser_allow_gnu_extensions_p (parser)
16700 && parser->in_function_body
16701 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16703 /* Remember that we saw the `goto' keyword. */
16704 goto_p = true;
16705 /* Consume the token. */
16706 cp_lexer_consume_token (parser->lexer);
16708 /* Look for the opening `('. */
16709 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16710 return;
16711 /* Look for the string. */
16712 string = cp_parser_string_literal (parser, false, false);
16713 if (string == error_mark_node)
16715 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16716 /*consume_paren=*/true);
16717 return;
16720 /* If we're allowing GNU extensions, check for the extended assembly
16721 syntax. Unfortunately, the `:' tokens need not be separated by
16722 a space in C, and so, for compatibility, we tolerate that here
16723 too. Doing that means that we have to treat the `::' operator as
16724 two `:' tokens. */
16725 if (cp_parser_allow_gnu_extensions_p (parser)
16726 && parser->in_function_body
16727 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16728 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16730 bool inputs_p = false;
16731 bool clobbers_p = false;
16732 bool labels_p = false;
16734 /* The extended syntax was used. */
16735 extended_p = true;
16737 /* Look for outputs. */
16738 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16740 /* Consume the `:'. */
16741 cp_lexer_consume_token (parser->lexer);
16742 /* Parse the output-operands. */
16743 if (cp_lexer_next_token_is_not (parser->lexer,
16744 CPP_COLON)
16745 && cp_lexer_next_token_is_not (parser->lexer,
16746 CPP_SCOPE)
16747 && cp_lexer_next_token_is_not (parser->lexer,
16748 CPP_CLOSE_PAREN)
16749 && !goto_p)
16750 outputs = cp_parser_asm_operand_list (parser);
16752 if (outputs == error_mark_node)
16753 invalid_outputs_p = true;
16755 /* If the next token is `::', there are no outputs, and the
16756 next token is the beginning of the inputs. */
16757 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16758 /* The inputs are coming next. */
16759 inputs_p = true;
16761 /* Look for inputs. */
16762 if (inputs_p
16763 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16765 /* Consume the `:' or `::'. */
16766 cp_lexer_consume_token (parser->lexer);
16767 /* Parse the output-operands. */
16768 if (cp_lexer_next_token_is_not (parser->lexer,
16769 CPP_COLON)
16770 && cp_lexer_next_token_is_not (parser->lexer,
16771 CPP_SCOPE)
16772 && cp_lexer_next_token_is_not (parser->lexer,
16773 CPP_CLOSE_PAREN))
16774 inputs = cp_parser_asm_operand_list (parser);
16776 if (inputs == error_mark_node)
16777 invalid_inputs_p = true;
16779 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16780 /* The clobbers are coming next. */
16781 clobbers_p = true;
16783 /* Look for clobbers. */
16784 if (clobbers_p
16785 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16787 clobbers_p = true;
16788 /* Consume the `:' or `::'. */
16789 cp_lexer_consume_token (parser->lexer);
16790 /* Parse the clobbers. */
16791 if (cp_lexer_next_token_is_not (parser->lexer,
16792 CPP_COLON)
16793 && cp_lexer_next_token_is_not (parser->lexer,
16794 CPP_CLOSE_PAREN))
16795 clobbers = cp_parser_asm_clobber_list (parser);
16797 else if (goto_p
16798 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16799 /* The labels are coming next. */
16800 labels_p = true;
16802 /* Look for labels. */
16803 if (labels_p
16804 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16806 labels_p = true;
16807 /* Consume the `:' or `::'. */
16808 cp_lexer_consume_token (parser->lexer);
16809 /* Parse the labels. */
16810 labels = cp_parser_asm_label_list (parser);
16813 if (goto_p && !labels_p)
16814 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16816 else if (goto_p)
16817 missing = RT_COLON_SCOPE;
16819 /* Look for the closing `)'. */
16820 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16821 missing ? missing : RT_CLOSE_PAREN))
16822 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16823 /*consume_paren=*/true);
16824 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16826 if (!invalid_inputs_p && !invalid_outputs_p)
16828 /* Create the ASM_EXPR. */
16829 if (parser->in_function_body)
16831 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16832 inputs, clobbers, labels);
16833 /* If the extended syntax was not used, mark the ASM_EXPR. */
16834 if (!extended_p)
16836 tree temp = asm_stmt;
16837 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16838 temp = TREE_OPERAND (temp, 0);
16840 ASM_INPUT_P (temp) = 1;
16843 else
16844 symtab->finalize_toplevel_asm (string);
16848 /* Declarators [gram.dcl.decl] */
16850 /* Parse an init-declarator.
16852 init-declarator:
16853 declarator initializer [opt]
16855 GNU Extension:
16857 init-declarator:
16858 declarator asm-specification [opt] attributes [opt] initializer [opt]
16860 function-definition:
16861 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16862 function-body
16863 decl-specifier-seq [opt] declarator function-try-block
16865 GNU Extension:
16867 function-definition:
16868 __extension__ function-definition
16870 TM Extension:
16872 function-definition:
16873 decl-specifier-seq [opt] declarator function-transaction-block
16875 The DECL_SPECIFIERS apply to this declarator. Returns a
16876 representation of the entity declared. If MEMBER_P is TRUE, then
16877 this declarator appears in a class scope. The new DECL created by
16878 this declarator is returned.
16880 The CHECKS are access checks that should be performed once we know
16881 what entity is being declared (and, therefore, what classes have
16882 befriended it).
16884 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16885 for a function-definition here as well. If the declarator is a
16886 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16887 be TRUE upon return. By that point, the function-definition will
16888 have been completely parsed.
16890 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16891 is FALSE.
16893 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16894 parsed declaration if it is an uninitialized single declarator not followed
16895 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16896 if present, will not be consumed. If returned, this declarator will be
16897 created with SD_INITIALIZED but will not call cp_finish_decl.
16899 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16900 and there is an initializer, the pointed location_t is set to the
16901 location of the '=' or `(', or '{' in C++11 token introducing the
16902 initializer. */
16904 static tree
16905 cp_parser_init_declarator (cp_parser* parser,
16906 cp_decl_specifier_seq *decl_specifiers,
16907 vec<deferred_access_check, va_gc> *checks,
16908 bool function_definition_allowed_p,
16909 bool member_p,
16910 int declares_class_or_enum,
16911 bool* function_definition_p,
16912 tree* maybe_range_for_decl,
16913 location_t* init_loc)
16915 cp_token *token = NULL, *asm_spec_start_token = NULL,
16916 *attributes_start_token = NULL;
16917 cp_declarator *declarator;
16918 tree prefix_attributes;
16919 tree attributes = NULL;
16920 tree asm_specification;
16921 tree initializer;
16922 tree decl = NULL_TREE;
16923 tree scope;
16924 int is_initialized;
16925 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16926 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16927 "(...)". */
16928 enum cpp_ttype initialization_kind;
16929 bool is_direct_init = false;
16930 bool is_non_constant_init;
16931 int ctor_dtor_or_conv_p;
16932 bool friend_p = cp_parser_friend_p (decl_specifiers);
16933 tree pushed_scope = NULL_TREE;
16934 bool range_for_decl_p = false;
16935 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16936 location_t tmp_init_loc = UNKNOWN_LOCATION;
16938 /* Gather the attributes that were provided with the
16939 decl-specifiers. */
16940 prefix_attributes = decl_specifiers->attributes;
16942 /* Assume that this is not the declarator for a function
16943 definition. */
16944 if (function_definition_p)
16945 *function_definition_p = false;
16947 /* Default arguments are only permitted for function parameters. */
16948 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16949 parser->default_arg_ok_p = false;
16951 /* Defer access checks while parsing the declarator; we cannot know
16952 what names are accessible until we know what is being
16953 declared. */
16954 resume_deferring_access_checks ();
16956 /* Parse the declarator. */
16957 token = cp_lexer_peek_token (parser->lexer);
16958 declarator
16959 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16960 &ctor_dtor_or_conv_p,
16961 /*parenthesized_p=*/NULL,
16962 member_p, friend_p);
16963 /* Gather up the deferred checks. */
16964 stop_deferring_access_checks ();
16966 parser->default_arg_ok_p = saved_default_arg_ok_p;
16968 /* If the DECLARATOR was erroneous, there's no need to go
16969 further. */
16970 if (declarator == cp_error_declarator)
16971 return error_mark_node;
16973 /* Check that the number of template-parameter-lists is OK. */
16974 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16975 token->location))
16976 return error_mark_node;
16978 if (declares_class_or_enum & 2)
16979 cp_parser_check_for_definition_in_return_type (declarator,
16980 decl_specifiers->type,
16981 decl_specifiers->locations[ds_type_spec]);
16983 /* Figure out what scope the entity declared by the DECLARATOR is
16984 located in. `grokdeclarator' sometimes changes the scope, so
16985 we compute it now. */
16986 scope = get_scope_of_declarator (declarator);
16988 /* Perform any lookups in the declared type which were thought to be
16989 dependent, but are not in the scope of the declarator. */
16990 decl_specifiers->type
16991 = maybe_update_decl_type (decl_specifiers->type, scope);
16993 /* If we're allowing GNU extensions, look for an
16994 asm-specification. */
16995 if (cp_parser_allow_gnu_extensions_p (parser))
16997 /* Look for an asm-specification. */
16998 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16999 asm_specification = cp_parser_asm_specification_opt (parser);
17001 else
17002 asm_specification = NULL_TREE;
17004 /* Look for attributes. */
17005 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17006 attributes = cp_parser_attributes_opt (parser);
17008 /* Peek at the next token. */
17009 token = cp_lexer_peek_token (parser->lexer);
17011 bool bogus_implicit_tmpl = false;
17013 if (function_declarator_p (declarator))
17015 /* Check to see if the token indicates the start of a
17016 function-definition. */
17017 if (cp_parser_token_starts_function_definition_p (token))
17019 if (!function_definition_allowed_p)
17021 /* If a function-definition should not appear here, issue an
17022 error message. */
17023 cp_parser_error (parser,
17024 "a function-definition is not allowed here");
17025 return error_mark_node;
17028 location_t func_brace_location
17029 = cp_lexer_peek_token (parser->lexer)->location;
17031 /* Neither attributes nor an asm-specification are allowed
17032 on a function-definition. */
17033 if (asm_specification)
17034 error_at (asm_spec_start_token->location,
17035 "an asm-specification is not allowed "
17036 "on a function-definition");
17037 if (attributes)
17038 error_at (attributes_start_token->location,
17039 "attributes are not allowed "
17040 "on a function-definition");
17041 /* This is a function-definition. */
17042 *function_definition_p = true;
17044 /* Parse the function definition. */
17045 if (member_p)
17046 decl = cp_parser_save_member_function_body (parser,
17047 decl_specifiers,
17048 declarator,
17049 prefix_attributes);
17050 else
17051 decl =
17052 (cp_parser_function_definition_from_specifiers_and_declarator
17053 (parser, decl_specifiers, prefix_attributes, declarator));
17055 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17057 /* This is where the prologue starts... */
17058 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17059 = func_brace_location;
17062 return decl;
17065 else if (parser->fully_implicit_function_template_p)
17067 /* A non-template declaration involving a function parameter list
17068 containing an implicit template parameter will be made into a
17069 template. If the resulting declaration is not going to be an
17070 actual function then finish the template scope here to prevent it.
17071 An error message will be issued once we have a decl to talk about.
17073 FIXME probably we should do type deduction rather than create an
17074 implicit template, but the standard currently doesn't allow it. */
17075 bogus_implicit_tmpl = true;
17076 finish_fully_implicit_template (parser, NULL_TREE);
17079 /* [dcl.dcl]
17081 Only in function declarations for constructors, destructors, and
17082 type conversions can the decl-specifier-seq be omitted.
17084 We explicitly postpone this check past the point where we handle
17085 function-definitions because we tolerate function-definitions
17086 that are missing their return types in some modes. */
17087 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17089 cp_parser_error (parser,
17090 "expected constructor, destructor, or type conversion");
17091 return error_mark_node;
17094 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17095 if (token->type == CPP_EQ
17096 || token->type == CPP_OPEN_PAREN
17097 || token->type == CPP_OPEN_BRACE)
17099 is_initialized = SD_INITIALIZED;
17100 initialization_kind = token->type;
17101 if (maybe_range_for_decl)
17102 *maybe_range_for_decl = error_mark_node;
17103 tmp_init_loc = token->location;
17104 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17105 *init_loc = tmp_init_loc;
17107 if (token->type == CPP_EQ
17108 && function_declarator_p (declarator))
17110 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17111 if (t2->keyword == RID_DEFAULT)
17112 is_initialized = SD_DEFAULTED;
17113 else if (t2->keyword == RID_DELETE)
17114 is_initialized = SD_DELETED;
17117 else
17119 /* If the init-declarator isn't initialized and isn't followed by a
17120 `,' or `;', it's not a valid init-declarator. */
17121 if (token->type != CPP_COMMA
17122 && token->type != CPP_SEMICOLON)
17124 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17125 range_for_decl_p = true;
17126 else
17128 if (!maybe_range_for_decl)
17129 cp_parser_error (parser, "expected initializer");
17130 return error_mark_node;
17133 is_initialized = SD_UNINITIALIZED;
17134 initialization_kind = CPP_EOF;
17137 /* Because start_decl has side-effects, we should only call it if we
17138 know we're going ahead. By this point, we know that we cannot
17139 possibly be looking at any other construct. */
17140 cp_parser_commit_to_tentative_parse (parser);
17142 /* Enter the newly declared entry in the symbol table. If we're
17143 processing a declaration in a class-specifier, we wait until
17144 after processing the initializer. */
17145 if (!member_p)
17147 if (parser->in_unbraced_linkage_specification_p)
17148 decl_specifiers->storage_class = sc_extern;
17149 decl = start_decl (declarator, decl_specifiers,
17150 range_for_decl_p? SD_INITIALIZED : is_initialized,
17151 attributes, prefix_attributes, &pushed_scope);
17152 cp_finalize_omp_declare_simd (parser, decl);
17153 /* Adjust location of decl if declarator->id_loc is more appropriate:
17154 set, and decl wasn't merged with another decl, in which case its
17155 location would be different from input_location, and more accurate. */
17156 if (DECL_P (decl)
17157 && declarator->id_loc != UNKNOWN_LOCATION
17158 && DECL_SOURCE_LOCATION (decl) == input_location)
17159 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17161 else if (scope)
17162 /* Enter the SCOPE. That way unqualified names appearing in the
17163 initializer will be looked up in SCOPE. */
17164 pushed_scope = push_scope (scope);
17166 /* Perform deferred access control checks, now that we know in which
17167 SCOPE the declared entity resides. */
17168 if (!member_p && decl)
17170 tree saved_current_function_decl = NULL_TREE;
17172 /* If the entity being declared is a function, pretend that we
17173 are in its scope. If it is a `friend', it may have access to
17174 things that would not otherwise be accessible. */
17175 if (TREE_CODE (decl) == FUNCTION_DECL)
17177 saved_current_function_decl = current_function_decl;
17178 current_function_decl = decl;
17181 /* Perform access checks for template parameters. */
17182 cp_parser_perform_template_parameter_access_checks (checks);
17184 /* Perform the access control checks for the declarator and the
17185 decl-specifiers. */
17186 perform_deferred_access_checks (tf_warning_or_error);
17188 /* Restore the saved value. */
17189 if (TREE_CODE (decl) == FUNCTION_DECL)
17190 current_function_decl = saved_current_function_decl;
17193 /* Parse the initializer. */
17194 initializer = NULL_TREE;
17195 is_direct_init = false;
17196 is_non_constant_init = true;
17197 if (is_initialized)
17199 if (function_declarator_p (declarator))
17201 if (initialization_kind == CPP_EQ)
17202 initializer = cp_parser_pure_specifier (parser);
17203 else
17205 /* If the declaration was erroneous, we don't really
17206 know what the user intended, so just silently
17207 consume the initializer. */
17208 if (decl != error_mark_node)
17209 error_at (tmp_init_loc, "initializer provided for function");
17210 cp_parser_skip_to_closing_parenthesis (parser,
17211 /*recovering=*/true,
17212 /*or_comma=*/false,
17213 /*consume_paren=*/true);
17216 else
17218 /* We want to record the extra mangling scope for in-class
17219 initializers of class members and initializers of static data
17220 member templates. The former involves deferring
17221 parsing of the initializer until end of class as with default
17222 arguments. So right here we only handle the latter. */
17223 if (!member_p && processing_template_decl)
17224 start_lambda_scope (decl);
17225 initializer = cp_parser_initializer (parser,
17226 &is_direct_init,
17227 &is_non_constant_init);
17228 if (!member_p && processing_template_decl)
17229 finish_lambda_scope ();
17230 if (initializer == error_mark_node)
17231 cp_parser_skip_to_end_of_statement (parser);
17235 /* The old parser allows attributes to appear after a parenthesized
17236 initializer. Mark Mitchell proposed removing this functionality
17237 on the GCC mailing lists on 2002-08-13. This parser accepts the
17238 attributes -- but ignores them. */
17239 if (cp_parser_allow_gnu_extensions_p (parser)
17240 && initialization_kind == CPP_OPEN_PAREN)
17241 if (cp_parser_attributes_opt (parser))
17242 warning (OPT_Wattributes,
17243 "attributes after parenthesized initializer ignored");
17245 /* And now complain about a non-function implicit template. */
17246 if (bogus_implicit_tmpl)
17247 error_at (DECL_SOURCE_LOCATION (decl),
17248 "non-function %qD declared as implicit template", decl);
17250 /* For an in-class declaration, use `grokfield' to create the
17251 declaration. */
17252 if (member_p)
17254 if (pushed_scope)
17256 pop_scope (pushed_scope);
17257 pushed_scope = NULL_TREE;
17259 decl = grokfield (declarator, decl_specifiers,
17260 initializer, !is_non_constant_init,
17261 /*asmspec=*/NULL_TREE,
17262 chainon (attributes, prefix_attributes));
17263 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17264 cp_parser_save_default_args (parser, decl);
17265 cp_finalize_omp_declare_simd (parser, decl);
17268 /* Finish processing the declaration. But, skip member
17269 declarations. */
17270 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17272 cp_finish_decl (decl,
17273 initializer, !is_non_constant_init,
17274 asm_specification,
17275 /* If the initializer is in parentheses, then this is
17276 a direct-initialization, which means that an
17277 `explicit' constructor is OK. Otherwise, an
17278 `explicit' constructor cannot be used. */
17279 ((is_direct_init || !is_initialized)
17280 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17282 else if ((cxx_dialect != cxx98) && friend_p
17283 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17284 /* Core issue #226 (C++0x only): A default template-argument
17285 shall not be specified in a friend class template
17286 declaration. */
17287 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17288 /*is_partial=*/false, /*is_friend_decl=*/1);
17290 if (!friend_p && pushed_scope)
17291 pop_scope (pushed_scope);
17293 if (function_declarator_p (declarator)
17294 && parser->fully_implicit_function_template_p)
17296 if (member_p)
17297 decl = finish_fully_implicit_template (parser, decl);
17298 else
17299 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17302 return decl;
17305 /* Parse a declarator.
17307 declarator:
17308 direct-declarator
17309 ptr-operator declarator
17311 abstract-declarator:
17312 ptr-operator abstract-declarator [opt]
17313 direct-abstract-declarator
17315 GNU Extensions:
17317 declarator:
17318 attributes [opt] direct-declarator
17319 attributes [opt] ptr-operator declarator
17321 abstract-declarator:
17322 attributes [opt] ptr-operator abstract-declarator [opt]
17323 attributes [opt] direct-abstract-declarator
17325 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17326 detect constructor, destructor or conversion operators. It is set
17327 to -1 if the declarator is a name, and +1 if it is a
17328 function. Otherwise it is set to zero. Usually you just want to
17329 test for >0, but internally the negative value is used.
17331 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17332 a decl-specifier-seq unless it declares a constructor, destructor,
17333 or conversion. It might seem that we could check this condition in
17334 semantic analysis, rather than parsing, but that makes it difficult
17335 to handle something like `f()'. We want to notice that there are
17336 no decl-specifiers, and therefore realize that this is an
17337 expression, not a declaration.)
17339 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17340 the declarator is a direct-declarator of the form "(...)".
17342 MEMBER_P is true iff this declarator is a member-declarator.
17344 FRIEND_P is true iff this declarator is a friend. */
17346 static cp_declarator *
17347 cp_parser_declarator (cp_parser* parser,
17348 cp_parser_declarator_kind dcl_kind,
17349 int* ctor_dtor_or_conv_p,
17350 bool* parenthesized_p,
17351 bool member_p, bool friend_p)
17353 cp_declarator *declarator;
17354 enum tree_code code;
17355 cp_cv_quals cv_quals;
17356 tree class_type;
17357 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17359 /* Assume this is not a constructor, destructor, or type-conversion
17360 operator. */
17361 if (ctor_dtor_or_conv_p)
17362 *ctor_dtor_or_conv_p = 0;
17364 if (cp_parser_allow_gnu_extensions_p (parser))
17365 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17367 /* Check for the ptr-operator production. */
17368 cp_parser_parse_tentatively (parser);
17369 /* Parse the ptr-operator. */
17370 code = cp_parser_ptr_operator (parser,
17371 &class_type,
17372 &cv_quals,
17373 &std_attributes);
17375 /* If that worked, then we have a ptr-operator. */
17376 if (cp_parser_parse_definitely (parser))
17378 /* If a ptr-operator was found, then this declarator was not
17379 parenthesized. */
17380 if (parenthesized_p)
17381 *parenthesized_p = true;
17382 /* The dependent declarator is optional if we are parsing an
17383 abstract-declarator. */
17384 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17385 cp_parser_parse_tentatively (parser);
17387 /* Parse the dependent declarator. */
17388 declarator = cp_parser_declarator (parser, dcl_kind,
17389 /*ctor_dtor_or_conv_p=*/NULL,
17390 /*parenthesized_p=*/NULL,
17391 /*member_p=*/false,
17392 friend_p);
17394 /* If we are parsing an abstract-declarator, we must handle the
17395 case where the dependent declarator is absent. */
17396 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17397 && !cp_parser_parse_definitely (parser))
17398 declarator = NULL;
17400 declarator = cp_parser_make_indirect_declarator
17401 (code, class_type, cv_quals, declarator, std_attributes);
17403 /* Everything else is a direct-declarator. */
17404 else
17406 if (parenthesized_p)
17407 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17408 CPP_OPEN_PAREN);
17409 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17410 ctor_dtor_or_conv_p,
17411 member_p, friend_p);
17414 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17415 declarator->attributes = gnu_attributes;
17416 return declarator;
17419 /* Parse a direct-declarator or direct-abstract-declarator.
17421 direct-declarator:
17422 declarator-id
17423 direct-declarator ( parameter-declaration-clause )
17424 cv-qualifier-seq [opt]
17425 ref-qualifier [opt]
17426 exception-specification [opt]
17427 direct-declarator [ constant-expression [opt] ]
17428 ( declarator )
17430 direct-abstract-declarator:
17431 direct-abstract-declarator [opt]
17432 ( parameter-declaration-clause )
17433 cv-qualifier-seq [opt]
17434 ref-qualifier [opt]
17435 exception-specification [opt]
17436 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17437 ( abstract-declarator )
17439 Returns a representation of the declarator. DCL_KIND is
17440 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17441 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17442 we are parsing a direct-declarator. It is
17443 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17444 of ambiguity we prefer an abstract declarator, as per
17445 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17446 as for cp_parser_declarator. */
17448 static cp_declarator *
17449 cp_parser_direct_declarator (cp_parser* parser,
17450 cp_parser_declarator_kind dcl_kind,
17451 int* ctor_dtor_or_conv_p,
17452 bool member_p, bool friend_p)
17454 cp_token *token;
17455 cp_declarator *declarator = NULL;
17456 tree scope = NULL_TREE;
17457 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17458 bool saved_in_declarator_p = parser->in_declarator_p;
17459 bool first = true;
17460 tree pushed_scope = NULL_TREE;
17462 while (true)
17464 /* Peek at the next token. */
17465 token = cp_lexer_peek_token (parser->lexer);
17466 if (token->type == CPP_OPEN_PAREN)
17468 /* This is either a parameter-declaration-clause, or a
17469 parenthesized declarator. When we know we are parsing a
17470 named declarator, it must be a parenthesized declarator
17471 if FIRST is true. For instance, `(int)' is a
17472 parameter-declaration-clause, with an omitted
17473 direct-abstract-declarator. But `((*))', is a
17474 parenthesized abstract declarator. Finally, when T is a
17475 template parameter `(T)' is a
17476 parameter-declaration-clause, and not a parenthesized
17477 named declarator.
17479 We first try and parse a parameter-declaration-clause,
17480 and then try a nested declarator (if FIRST is true).
17482 It is not an error for it not to be a
17483 parameter-declaration-clause, even when FIRST is
17484 false. Consider,
17486 int i (int);
17487 int i (3);
17489 The first is the declaration of a function while the
17490 second is the definition of a variable, including its
17491 initializer.
17493 Having seen only the parenthesis, we cannot know which of
17494 these two alternatives should be selected. Even more
17495 complex are examples like:
17497 int i (int (a));
17498 int i (int (3));
17500 The former is a function-declaration; the latter is a
17501 variable initialization.
17503 Thus again, we try a parameter-declaration-clause, and if
17504 that fails, we back out and return. */
17506 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17508 tree params;
17509 bool is_declarator = false;
17511 /* In a member-declarator, the only valid interpretation
17512 of a parenthesis is the start of a
17513 parameter-declaration-clause. (It is invalid to
17514 initialize a static data member with a parenthesized
17515 initializer; only the "=" form of initialization is
17516 permitted.) */
17517 if (!member_p)
17518 cp_parser_parse_tentatively (parser);
17520 /* Consume the `('. */
17521 cp_lexer_consume_token (parser->lexer);
17522 if (first)
17524 /* If this is going to be an abstract declarator, we're
17525 in a declarator and we can't have default args. */
17526 parser->default_arg_ok_p = false;
17527 parser->in_declarator_p = true;
17530 begin_scope (sk_function_parms, NULL_TREE);
17532 /* Parse the parameter-declaration-clause. */
17533 params = cp_parser_parameter_declaration_clause (parser);
17535 /* Consume the `)'. */
17536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17538 /* If all went well, parse the cv-qualifier-seq,
17539 ref-qualifier and the exception-specification. */
17540 if (member_p || cp_parser_parse_definitely (parser))
17542 cp_cv_quals cv_quals;
17543 cp_virt_specifiers virt_specifiers;
17544 cp_ref_qualifier ref_qual;
17545 tree exception_specification;
17546 tree late_return;
17547 tree attrs;
17548 bool memfn = (member_p || (pushed_scope
17549 && CLASS_TYPE_P (pushed_scope)));
17551 is_declarator = true;
17553 if (ctor_dtor_or_conv_p)
17554 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17555 first = false;
17557 /* Parse the cv-qualifier-seq. */
17558 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17559 /* Parse the ref-qualifier. */
17560 ref_qual = cp_parser_ref_qualifier_opt (parser);
17561 /* And the exception-specification. */
17562 exception_specification
17563 = cp_parser_exception_specification_opt (parser);
17565 attrs = cp_parser_std_attribute_spec_seq (parser);
17567 /* In here, we handle cases where attribute is used after
17568 the function declaration. For example:
17569 void func (int x) __attribute__((vector(..))); */
17570 if (flag_cilkplus
17571 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17573 cp_parser_parse_tentatively (parser);
17574 tree attr = cp_parser_gnu_attributes_opt (parser);
17575 if (cp_lexer_next_token_is_not (parser->lexer,
17576 CPP_SEMICOLON)
17577 && cp_lexer_next_token_is_not (parser->lexer,
17578 CPP_OPEN_BRACE))
17579 cp_parser_abort_tentative_parse (parser);
17580 else if (!cp_parser_parse_definitely (parser))
17582 else
17583 attrs = chainon (attr, attrs);
17585 late_return = (cp_parser_late_return_type_opt
17586 (parser, declarator,
17587 memfn ? cv_quals : -1));
17590 /* Parse the virt-specifier-seq. */
17591 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17593 /* Create the function-declarator. */
17594 declarator = make_call_declarator (declarator,
17595 params,
17596 cv_quals,
17597 virt_specifiers,
17598 ref_qual,
17599 exception_specification,
17600 late_return);
17601 declarator->std_attributes = attrs;
17602 /* Any subsequent parameter lists are to do with
17603 return type, so are not those of the declared
17604 function. */
17605 parser->default_arg_ok_p = false;
17608 /* Remove the function parms from scope. */
17609 pop_bindings_and_leave_scope ();
17611 if (is_declarator)
17612 /* Repeat the main loop. */
17613 continue;
17616 /* If this is the first, we can try a parenthesized
17617 declarator. */
17618 if (first)
17620 bool saved_in_type_id_in_expr_p;
17622 parser->default_arg_ok_p = saved_default_arg_ok_p;
17623 parser->in_declarator_p = saved_in_declarator_p;
17625 /* Consume the `('. */
17626 cp_lexer_consume_token (parser->lexer);
17627 /* Parse the nested declarator. */
17628 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17629 parser->in_type_id_in_expr_p = true;
17630 declarator
17631 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17632 /*parenthesized_p=*/NULL,
17633 member_p, friend_p);
17634 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17635 first = false;
17636 /* Expect a `)'. */
17637 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17638 declarator = cp_error_declarator;
17639 if (declarator == cp_error_declarator)
17640 break;
17642 goto handle_declarator;
17644 /* Otherwise, we must be done. */
17645 else
17646 break;
17648 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17649 && token->type == CPP_OPEN_SQUARE
17650 && !cp_next_tokens_can_be_attribute_p (parser))
17652 /* Parse an array-declarator. */
17653 tree bounds, attrs;
17655 if (ctor_dtor_or_conv_p)
17656 *ctor_dtor_or_conv_p = 0;
17658 first = false;
17659 parser->default_arg_ok_p = false;
17660 parser->in_declarator_p = true;
17661 /* Consume the `['. */
17662 cp_lexer_consume_token (parser->lexer);
17663 /* Peek at the next token. */
17664 token = cp_lexer_peek_token (parser->lexer);
17665 /* If the next token is `]', then there is no
17666 constant-expression. */
17667 if (token->type != CPP_CLOSE_SQUARE)
17669 bool non_constant_p;
17670 bounds
17671 = cp_parser_constant_expression (parser,
17672 /*allow_non_constant=*/true,
17673 &non_constant_p);
17674 if (!non_constant_p)
17675 /* OK */;
17676 else if (error_operand_p (bounds))
17677 /* Already gave an error. */;
17678 else if (!parser->in_function_body
17679 || current_binding_level->kind == sk_function_parms)
17681 /* Normally, the array bound must be an integral constant
17682 expression. However, as an extension, we allow VLAs
17683 in function scopes as long as they aren't part of a
17684 parameter declaration. */
17685 cp_parser_error (parser,
17686 "array bound is not an integer constant");
17687 bounds = error_mark_node;
17689 else if (processing_template_decl
17690 && !type_dependent_expression_p (bounds))
17692 /* Remember this wasn't a constant-expression. */
17693 bounds = build_nop (TREE_TYPE (bounds), bounds);
17694 TREE_SIDE_EFFECTS (bounds) = 1;
17697 else
17698 bounds = NULL_TREE;
17699 /* Look for the closing `]'. */
17700 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17702 declarator = cp_error_declarator;
17703 break;
17706 attrs = cp_parser_std_attribute_spec_seq (parser);
17707 declarator = make_array_declarator (declarator, bounds);
17708 declarator->std_attributes = attrs;
17710 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17713 tree qualifying_scope;
17714 tree unqualified_name;
17715 tree attrs;
17716 special_function_kind sfk;
17717 bool abstract_ok;
17718 bool pack_expansion_p = false;
17719 cp_token *declarator_id_start_token;
17721 /* Parse a declarator-id */
17722 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17723 if (abstract_ok)
17725 cp_parser_parse_tentatively (parser);
17727 /* If we see an ellipsis, we should be looking at a
17728 parameter pack. */
17729 if (token->type == CPP_ELLIPSIS)
17731 /* Consume the `...' */
17732 cp_lexer_consume_token (parser->lexer);
17734 pack_expansion_p = true;
17738 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17739 unqualified_name
17740 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17741 qualifying_scope = parser->scope;
17742 if (abstract_ok)
17744 bool okay = false;
17746 if (!unqualified_name && pack_expansion_p)
17748 /* Check whether an error occurred. */
17749 okay = !cp_parser_error_occurred (parser);
17751 /* We already consumed the ellipsis to mark a
17752 parameter pack, but we have no way to report it,
17753 so abort the tentative parse. We will be exiting
17754 immediately anyway. */
17755 cp_parser_abort_tentative_parse (parser);
17757 else
17758 okay = cp_parser_parse_definitely (parser);
17760 if (!okay)
17761 unqualified_name = error_mark_node;
17762 else if (unqualified_name
17763 && (qualifying_scope
17764 || (!identifier_p (unqualified_name))))
17766 cp_parser_error (parser, "expected unqualified-id");
17767 unqualified_name = error_mark_node;
17771 if (!unqualified_name)
17772 return NULL;
17773 if (unqualified_name == error_mark_node)
17775 declarator = cp_error_declarator;
17776 pack_expansion_p = false;
17777 declarator->parameter_pack_p = false;
17778 break;
17781 attrs = cp_parser_std_attribute_spec_seq (parser);
17783 if (qualifying_scope && at_namespace_scope_p ()
17784 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17786 /* In the declaration of a member of a template class
17787 outside of the class itself, the SCOPE will sometimes
17788 be a TYPENAME_TYPE. For example, given:
17790 template <typename T>
17791 int S<T>::R::i = 3;
17793 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17794 this context, we must resolve S<T>::R to an ordinary
17795 type, rather than a typename type.
17797 The reason we normally avoid resolving TYPENAME_TYPEs
17798 is that a specialization of `S' might render
17799 `S<T>::R' not a type. However, if `S' is
17800 specialized, then this `i' will not be used, so there
17801 is no harm in resolving the types here. */
17802 tree type;
17804 /* Resolve the TYPENAME_TYPE. */
17805 type = resolve_typename_type (qualifying_scope,
17806 /*only_current_p=*/false);
17807 /* If that failed, the declarator is invalid. */
17808 if (TREE_CODE (type) == TYPENAME_TYPE)
17810 if (typedef_variant_p (type))
17811 error_at (declarator_id_start_token->location,
17812 "cannot define member of dependent typedef "
17813 "%qT", type);
17814 else
17815 error_at (declarator_id_start_token->location,
17816 "%<%T::%E%> is not a type",
17817 TYPE_CONTEXT (qualifying_scope),
17818 TYPE_IDENTIFIER (qualifying_scope));
17820 qualifying_scope = type;
17823 sfk = sfk_none;
17825 if (unqualified_name)
17827 tree class_type;
17829 if (qualifying_scope
17830 && CLASS_TYPE_P (qualifying_scope))
17831 class_type = qualifying_scope;
17832 else
17833 class_type = current_class_type;
17835 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17837 tree name_type = TREE_TYPE (unqualified_name);
17838 if (class_type && same_type_p (name_type, class_type))
17840 if (qualifying_scope
17841 && CLASSTYPE_USE_TEMPLATE (name_type))
17843 error_at (declarator_id_start_token->location,
17844 "invalid use of constructor as a template");
17845 inform (declarator_id_start_token->location,
17846 "use %<%T::%D%> instead of %<%T::%D%> to "
17847 "name the constructor in a qualified name",
17848 class_type,
17849 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17850 class_type, name_type);
17851 declarator = cp_error_declarator;
17852 break;
17854 else
17855 unqualified_name = constructor_name (class_type);
17857 else
17859 /* We do not attempt to print the declarator
17860 here because we do not have enough
17861 information about its original syntactic
17862 form. */
17863 cp_parser_error (parser, "invalid declarator");
17864 declarator = cp_error_declarator;
17865 break;
17869 if (class_type)
17871 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17872 sfk = sfk_destructor;
17873 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17874 sfk = sfk_conversion;
17875 else if (/* There's no way to declare a constructor
17876 for an anonymous type, even if the type
17877 got a name for linkage purposes. */
17878 !TYPE_WAS_ANONYMOUS (class_type)
17879 /* Handle correctly (c++/19200):
17881 struct S {
17882 struct T{};
17883 friend void S(T);
17886 and also:
17888 namespace N {
17889 void S();
17892 struct S {
17893 friend void N::S();
17894 }; */
17895 && !(friend_p
17896 && class_type != qualifying_scope)
17897 && constructor_name_p (unqualified_name,
17898 class_type))
17900 unqualified_name = constructor_name (class_type);
17901 sfk = sfk_constructor;
17903 else if (is_overloaded_fn (unqualified_name)
17904 && DECL_CONSTRUCTOR_P (get_first_fn
17905 (unqualified_name)))
17906 sfk = sfk_constructor;
17908 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17909 *ctor_dtor_or_conv_p = -1;
17912 declarator = make_id_declarator (qualifying_scope,
17913 unqualified_name,
17914 sfk);
17915 declarator->std_attributes = attrs;
17916 declarator->id_loc = token->location;
17917 declarator->parameter_pack_p = pack_expansion_p;
17919 if (pack_expansion_p)
17920 maybe_warn_variadic_templates ();
17923 handle_declarator:;
17924 scope = get_scope_of_declarator (declarator);
17925 if (scope)
17927 /* Any names that appear after the declarator-id for a
17928 member are looked up in the containing scope. */
17929 if (at_function_scope_p ())
17931 /* But declarations with qualified-ids can't appear in a
17932 function. */
17933 cp_parser_error (parser, "qualified-id in declaration");
17934 declarator = cp_error_declarator;
17935 break;
17937 pushed_scope = push_scope (scope);
17939 parser->in_declarator_p = true;
17940 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17941 || (declarator && declarator->kind == cdk_id))
17942 /* Default args are only allowed on function
17943 declarations. */
17944 parser->default_arg_ok_p = saved_default_arg_ok_p;
17945 else
17946 parser->default_arg_ok_p = false;
17948 first = false;
17950 /* We're done. */
17951 else
17952 break;
17955 /* For an abstract declarator, we might wind up with nothing at this
17956 point. That's an error; the declarator is not optional. */
17957 if (!declarator)
17958 cp_parser_error (parser, "expected declarator");
17960 /* If we entered a scope, we must exit it now. */
17961 if (pushed_scope)
17962 pop_scope (pushed_scope);
17964 parser->default_arg_ok_p = saved_default_arg_ok_p;
17965 parser->in_declarator_p = saved_in_declarator_p;
17967 return declarator;
17970 /* Parse a ptr-operator.
17972 ptr-operator:
17973 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17974 * cv-qualifier-seq [opt]
17976 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17977 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17979 GNU Extension:
17981 ptr-operator:
17982 & cv-qualifier-seq [opt]
17984 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17985 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17986 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17987 filled in with the TYPE containing the member. *CV_QUALS is
17988 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17989 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17990 Note that the tree codes returned by this function have nothing
17991 to do with the types of trees that will be eventually be created
17992 to represent the pointer or reference type being parsed. They are
17993 just constants with suggestive names. */
17994 static enum tree_code
17995 cp_parser_ptr_operator (cp_parser* parser,
17996 tree* type,
17997 cp_cv_quals *cv_quals,
17998 tree *attributes)
18000 enum tree_code code = ERROR_MARK;
18001 cp_token *token;
18002 tree attrs = NULL_TREE;
18004 /* Assume that it's not a pointer-to-member. */
18005 *type = NULL_TREE;
18006 /* And that there are no cv-qualifiers. */
18007 *cv_quals = TYPE_UNQUALIFIED;
18009 /* Peek at the next token. */
18010 token = cp_lexer_peek_token (parser->lexer);
18012 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18013 if (token->type == CPP_MULT)
18014 code = INDIRECT_REF;
18015 else if (token->type == CPP_AND)
18016 code = ADDR_EXPR;
18017 else if ((cxx_dialect != cxx98) &&
18018 token->type == CPP_AND_AND) /* C++0x only */
18019 code = NON_LVALUE_EXPR;
18021 if (code != ERROR_MARK)
18023 /* Consume the `*', `&' or `&&'. */
18024 cp_lexer_consume_token (parser->lexer);
18026 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18027 `&', if we are allowing GNU extensions. (The only qualifier
18028 that can legally appear after `&' is `restrict', but that is
18029 enforced during semantic analysis. */
18030 if (code == INDIRECT_REF
18031 || cp_parser_allow_gnu_extensions_p (parser))
18032 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18034 attrs = cp_parser_std_attribute_spec_seq (parser);
18035 if (attributes != NULL)
18036 *attributes = attrs;
18038 else
18040 /* Try the pointer-to-member case. */
18041 cp_parser_parse_tentatively (parser);
18042 /* Look for the optional `::' operator. */
18043 cp_parser_global_scope_opt (parser,
18044 /*current_scope_valid_p=*/false);
18045 /* Look for the nested-name specifier. */
18046 token = cp_lexer_peek_token (parser->lexer);
18047 cp_parser_nested_name_specifier (parser,
18048 /*typename_keyword_p=*/false,
18049 /*check_dependency_p=*/true,
18050 /*type_p=*/false,
18051 /*is_declaration=*/false);
18052 /* If we found it, and the next token is a `*', then we are
18053 indeed looking at a pointer-to-member operator. */
18054 if (!cp_parser_error_occurred (parser)
18055 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18057 /* Indicate that the `*' operator was used. */
18058 code = INDIRECT_REF;
18060 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18061 error_at (token->location, "%qD is a namespace", parser->scope);
18062 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18063 error_at (token->location, "cannot form pointer to member of "
18064 "non-class %q#T", parser->scope);
18065 else
18067 /* The type of which the member is a member is given by the
18068 current SCOPE. */
18069 *type = parser->scope;
18070 /* The next name will not be qualified. */
18071 parser->scope = NULL_TREE;
18072 parser->qualifying_scope = NULL_TREE;
18073 parser->object_scope = NULL_TREE;
18074 /* Look for optional c++11 attributes. */
18075 attrs = cp_parser_std_attribute_spec_seq (parser);
18076 if (attributes != NULL)
18077 *attributes = attrs;
18078 /* Look for the optional cv-qualifier-seq. */
18079 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18082 /* If that didn't work we don't have a ptr-operator. */
18083 if (!cp_parser_parse_definitely (parser))
18084 cp_parser_error (parser, "expected ptr-operator");
18087 return code;
18090 /* Parse an (optional) cv-qualifier-seq.
18092 cv-qualifier-seq:
18093 cv-qualifier cv-qualifier-seq [opt]
18095 cv-qualifier:
18096 const
18097 volatile
18099 GNU Extension:
18101 cv-qualifier:
18102 __restrict__
18104 Returns a bitmask representing the cv-qualifiers. */
18106 static cp_cv_quals
18107 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18109 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18111 while (true)
18113 cp_token *token;
18114 cp_cv_quals cv_qualifier;
18116 /* Peek at the next token. */
18117 token = cp_lexer_peek_token (parser->lexer);
18118 /* See if it's a cv-qualifier. */
18119 switch (token->keyword)
18121 case RID_CONST:
18122 cv_qualifier = TYPE_QUAL_CONST;
18123 break;
18125 case RID_VOLATILE:
18126 cv_qualifier = TYPE_QUAL_VOLATILE;
18127 break;
18129 case RID_RESTRICT:
18130 cv_qualifier = TYPE_QUAL_RESTRICT;
18131 break;
18133 default:
18134 cv_qualifier = TYPE_UNQUALIFIED;
18135 break;
18138 if (!cv_qualifier)
18139 break;
18141 if (cv_quals & cv_qualifier)
18143 error_at (token->location, "duplicate cv-qualifier");
18144 cp_lexer_purge_token (parser->lexer);
18146 else
18148 cp_lexer_consume_token (parser->lexer);
18149 cv_quals |= cv_qualifier;
18153 return cv_quals;
18156 /* Parse an (optional) ref-qualifier
18158 ref-qualifier:
18162 Returns cp_ref_qualifier representing ref-qualifier. */
18164 static cp_ref_qualifier
18165 cp_parser_ref_qualifier_opt (cp_parser* parser)
18167 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18169 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18170 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18171 return ref_qual;
18173 while (true)
18175 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18176 cp_token *token = cp_lexer_peek_token (parser->lexer);
18178 switch (token->type)
18180 case CPP_AND:
18181 curr_ref_qual = REF_QUAL_LVALUE;
18182 break;
18184 case CPP_AND_AND:
18185 curr_ref_qual = REF_QUAL_RVALUE;
18186 break;
18188 default:
18189 curr_ref_qual = REF_QUAL_NONE;
18190 break;
18193 if (!curr_ref_qual)
18194 break;
18195 else if (ref_qual)
18197 error_at (token->location, "multiple ref-qualifiers");
18198 cp_lexer_purge_token (parser->lexer);
18200 else
18202 ref_qual = curr_ref_qual;
18203 cp_lexer_consume_token (parser->lexer);
18207 return ref_qual;
18210 /* Parse an (optional) virt-specifier-seq.
18212 virt-specifier-seq:
18213 virt-specifier virt-specifier-seq [opt]
18215 virt-specifier:
18216 override
18217 final
18219 Returns a bitmask representing the virt-specifiers. */
18221 static cp_virt_specifiers
18222 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18224 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18226 while (true)
18228 cp_token *token;
18229 cp_virt_specifiers virt_specifier;
18231 /* Peek at the next token. */
18232 token = cp_lexer_peek_token (parser->lexer);
18233 /* See if it's a virt-specifier-qualifier. */
18234 if (token->type != CPP_NAME)
18235 break;
18236 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18238 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18239 virt_specifier = VIRT_SPEC_OVERRIDE;
18241 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18243 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18244 virt_specifier = VIRT_SPEC_FINAL;
18246 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18248 virt_specifier = VIRT_SPEC_FINAL;
18250 else
18251 break;
18253 if (virt_specifiers & virt_specifier)
18255 error_at (token->location, "duplicate virt-specifier");
18256 cp_lexer_purge_token (parser->lexer);
18258 else
18260 cp_lexer_consume_token (parser->lexer);
18261 virt_specifiers |= virt_specifier;
18264 return virt_specifiers;
18267 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18268 is in scope even though it isn't real. */
18270 void
18271 inject_this_parameter (tree ctype, cp_cv_quals quals)
18273 tree this_parm;
18275 if (current_class_ptr)
18277 /* We don't clear this between NSDMIs. Is it already what we want? */
18278 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18279 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18280 && cp_type_quals (type) == quals)
18281 return;
18284 this_parm = build_this_parm (ctype, quals);
18285 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18286 current_class_ptr = NULL_TREE;
18287 current_class_ref
18288 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18289 current_class_ptr = this_parm;
18292 /* Return true iff our current scope is a non-static data member
18293 initializer. */
18295 bool
18296 parsing_nsdmi (void)
18298 /* We recognize NSDMI context by the context-less 'this' pointer set up
18299 by the function above. */
18300 if (current_class_ptr
18301 && TREE_CODE (current_class_ptr) == PARM_DECL
18302 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18303 return true;
18304 return false;
18307 /* Parse a late-specified return type, if any. This is not a separate
18308 non-terminal, but part of a function declarator, which looks like
18310 -> trailing-type-specifier-seq abstract-declarator(opt)
18312 Returns the type indicated by the type-id.
18314 In addition to this this parses any queued up omp declare simd
18315 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18317 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18318 function. */
18320 static tree
18321 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18322 cp_cv_quals quals)
18324 cp_token *token;
18325 tree type = NULL_TREE;
18326 bool declare_simd_p = (parser->omp_declare_simd
18327 && declarator
18328 && declarator->kind == cdk_id);
18330 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18331 && declarator && declarator->kind == cdk_id);
18333 /* Peek at the next token. */
18334 token = cp_lexer_peek_token (parser->lexer);
18335 /* A late-specified return type is indicated by an initial '->'. */
18336 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18337 return NULL_TREE;
18339 tree save_ccp = current_class_ptr;
18340 tree save_ccr = current_class_ref;
18341 if (quals >= 0)
18343 /* DR 1207: 'this' is in scope in the trailing return type. */
18344 inject_this_parameter (current_class_type, quals);
18347 if (token->type == CPP_DEREF)
18349 /* Consume the ->. */
18350 cp_lexer_consume_token (parser->lexer);
18352 type = cp_parser_trailing_type_id (parser);
18355 if (cilk_simd_fn_vector_p)
18356 declarator->std_attributes
18357 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18358 declarator->std_attributes);
18359 if (declare_simd_p)
18360 declarator->std_attributes
18361 = cp_parser_late_parsing_omp_declare_simd (parser,
18362 declarator->std_attributes);
18364 if (quals >= 0)
18366 current_class_ptr = save_ccp;
18367 current_class_ref = save_ccr;
18370 return type;
18373 /* Parse a declarator-id.
18375 declarator-id:
18376 id-expression
18377 :: [opt] nested-name-specifier [opt] type-name
18379 In the `id-expression' case, the value returned is as for
18380 cp_parser_id_expression if the id-expression was an unqualified-id.
18381 If the id-expression was a qualified-id, then a SCOPE_REF is
18382 returned. The first operand is the scope (either a NAMESPACE_DECL
18383 or TREE_TYPE), but the second is still just a representation of an
18384 unqualified-id. */
18386 static tree
18387 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18389 tree id;
18390 /* The expression must be an id-expression. Assume that qualified
18391 names are the names of types so that:
18393 template <class T>
18394 int S<T>::R::i = 3;
18396 will work; we must treat `S<T>::R' as the name of a type.
18397 Similarly, assume that qualified names are templates, where
18398 required, so that:
18400 template <class T>
18401 int S<T>::R<T>::i = 3;
18403 will work, too. */
18404 id = cp_parser_id_expression (parser,
18405 /*template_keyword_p=*/false,
18406 /*check_dependency_p=*/false,
18407 /*template_p=*/NULL,
18408 /*declarator_p=*/true,
18409 optional_p);
18410 if (id && BASELINK_P (id))
18411 id = BASELINK_FUNCTIONS (id);
18412 return id;
18415 /* Parse a type-id.
18417 type-id:
18418 type-specifier-seq abstract-declarator [opt]
18420 Returns the TYPE specified. */
18422 static tree
18423 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18424 bool is_trailing_return)
18426 cp_decl_specifier_seq type_specifier_seq;
18427 cp_declarator *abstract_declarator;
18429 /* Parse the type-specifier-seq. */
18430 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18431 is_trailing_return,
18432 &type_specifier_seq);
18433 if (type_specifier_seq.type == error_mark_node)
18434 return error_mark_node;
18436 /* There might or might not be an abstract declarator. */
18437 cp_parser_parse_tentatively (parser);
18438 /* Look for the declarator. */
18439 abstract_declarator
18440 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18441 /*parenthesized_p=*/NULL,
18442 /*member_p=*/false,
18443 /*friend_p=*/false);
18444 /* Check to see if there really was a declarator. */
18445 if (!cp_parser_parse_definitely (parser))
18446 abstract_declarator = NULL;
18448 if (type_specifier_seq.type
18449 /* None of the valid uses of 'auto' in C++14 involve the type-id
18450 nonterminal, but it is valid in a trailing-return-type. */
18451 && !(cxx_dialect >= cxx14 && is_trailing_return)
18452 && type_uses_auto (type_specifier_seq.type))
18454 /* A type-id with type 'auto' is only ok if the abstract declarator
18455 is a function declarator with a late-specified return type. */
18456 if (abstract_declarator
18457 && abstract_declarator->kind == cdk_function
18458 && abstract_declarator->u.function.late_return_type)
18459 /* OK */;
18460 else
18462 error ("invalid use of %<auto%>");
18463 return error_mark_node;
18467 return groktypename (&type_specifier_seq, abstract_declarator,
18468 is_template_arg);
18471 static tree cp_parser_type_id (cp_parser *parser)
18473 return cp_parser_type_id_1 (parser, false, false);
18476 static tree cp_parser_template_type_arg (cp_parser *parser)
18478 tree r;
18479 const char *saved_message = parser->type_definition_forbidden_message;
18480 parser->type_definition_forbidden_message
18481 = G_("types may not be defined in template arguments");
18482 r = cp_parser_type_id_1 (parser, true, false);
18483 parser->type_definition_forbidden_message = saved_message;
18484 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18486 error ("invalid use of %<auto%> in template argument");
18487 r = error_mark_node;
18489 return r;
18492 static tree cp_parser_trailing_type_id (cp_parser *parser)
18494 return cp_parser_type_id_1 (parser, false, true);
18497 /* Parse a type-specifier-seq.
18499 type-specifier-seq:
18500 type-specifier type-specifier-seq [opt]
18502 GNU extension:
18504 type-specifier-seq:
18505 attributes type-specifier-seq [opt]
18507 If IS_DECLARATION is true, we are at the start of a "condition" or
18508 exception-declaration, so we might be followed by a declarator-id.
18510 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18511 i.e. we've just seen "->".
18513 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18515 static void
18516 cp_parser_type_specifier_seq (cp_parser* parser,
18517 bool is_declaration,
18518 bool is_trailing_return,
18519 cp_decl_specifier_seq *type_specifier_seq)
18521 bool seen_type_specifier = false;
18522 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18523 cp_token *start_token = NULL;
18525 /* Clear the TYPE_SPECIFIER_SEQ. */
18526 clear_decl_specs (type_specifier_seq);
18528 /* In the context of a trailing return type, enum E { } is an
18529 elaborated-type-specifier followed by a function-body, not an
18530 enum-specifier. */
18531 if (is_trailing_return)
18532 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18534 /* Parse the type-specifiers and attributes. */
18535 while (true)
18537 tree type_specifier;
18538 bool is_cv_qualifier;
18540 /* Check for attributes first. */
18541 if (cp_next_tokens_can_be_attribute_p (parser))
18543 type_specifier_seq->attributes =
18544 chainon (type_specifier_seq->attributes,
18545 cp_parser_attributes_opt (parser));
18546 continue;
18549 /* record the token of the beginning of the type specifier seq,
18550 for error reporting purposes*/
18551 if (!start_token)
18552 start_token = cp_lexer_peek_token (parser->lexer);
18554 /* Look for the type-specifier. */
18555 type_specifier = cp_parser_type_specifier (parser,
18556 flags,
18557 type_specifier_seq,
18558 /*is_declaration=*/false,
18559 NULL,
18560 &is_cv_qualifier);
18561 if (!type_specifier)
18563 /* If the first type-specifier could not be found, this is not a
18564 type-specifier-seq at all. */
18565 if (!seen_type_specifier)
18567 /* Set in_declarator_p to avoid skipping to the semicolon. */
18568 int in_decl = parser->in_declarator_p;
18569 parser->in_declarator_p = true;
18571 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18572 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18573 cp_parser_error (parser, "expected type-specifier");
18575 parser->in_declarator_p = in_decl;
18577 type_specifier_seq->type = error_mark_node;
18578 return;
18580 /* If subsequent type-specifiers could not be found, the
18581 type-specifier-seq is complete. */
18582 break;
18585 seen_type_specifier = true;
18586 /* The standard says that a condition can be:
18588 type-specifier-seq declarator = assignment-expression
18590 However, given:
18592 struct S {};
18593 if (int S = ...)
18595 we should treat the "S" as a declarator, not as a
18596 type-specifier. The standard doesn't say that explicitly for
18597 type-specifier-seq, but it does say that for
18598 decl-specifier-seq in an ordinary declaration. Perhaps it
18599 would be clearer just to allow a decl-specifier-seq here, and
18600 then add a semantic restriction that if any decl-specifiers
18601 that are not type-specifiers appear, the program is invalid. */
18602 if (is_declaration && !is_cv_qualifier)
18603 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18607 /* Return whether the function currently being declared has an associated
18608 template parameter list. */
18610 static bool
18611 function_being_declared_is_template_p (cp_parser* parser)
18613 if (!current_template_parms || processing_template_parmlist)
18614 return false;
18616 if (parser->implicit_template_scope)
18617 return true;
18619 if (at_class_scope_p ()
18620 && TYPE_BEING_DEFINED (current_class_type))
18621 return parser->num_template_parameter_lists != 0;
18623 return ((int) parser->num_template_parameter_lists > template_class_depth
18624 (current_class_type));
18627 /* Parse a parameter-declaration-clause.
18629 parameter-declaration-clause:
18630 parameter-declaration-list [opt] ... [opt]
18631 parameter-declaration-list , ...
18633 Returns a representation for the parameter declarations. A return
18634 value of NULL indicates a parameter-declaration-clause consisting
18635 only of an ellipsis. */
18637 static tree
18638 cp_parser_parameter_declaration_clause (cp_parser* parser)
18640 tree parameters;
18641 cp_token *token;
18642 bool ellipsis_p;
18643 bool is_error;
18645 struct cleanup {
18646 cp_parser* parser;
18647 int auto_is_implicit_function_template_parm_p;
18648 ~cleanup() {
18649 parser->auto_is_implicit_function_template_parm_p
18650 = auto_is_implicit_function_template_parm_p;
18652 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18654 (void) cleanup;
18656 if (!processing_specialization
18657 && !processing_template_parmlist
18658 && !processing_explicit_instantiation)
18659 if (!current_function_decl
18660 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18661 parser->auto_is_implicit_function_template_parm_p = true;
18663 /* Peek at the next token. */
18664 token = cp_lexer_peek_token (parser->lexer);
18665 /* Check for trivial parameter-declaration-clauses. */
18666 if (token->type == CPP_ELLIPSIS)
18668 /* Consume the `...' token. */
18669 cp_lexer_consume_token (parser->lexer);
18670 return NULL_TREE;
18672 else if (token->type == CPP_CLOSE_PAREN)
18673 /* There are no parameters. */
18675 #ifndef NO_IMPLICIT_EXTERN_C
18676 if (in_system_header_at (input_location)
18677 && current_class_type == NULL
18678 && current_lang_name == lang_name_c)
18679 return NULL_TREE;
18680 else
18681 #endif
18682 return void_list_node;
18684 /* Check for `(void)', too, which is a special case. */
18685 else if (token->keyword == RID_VOID
18686 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18687 == CPP_CLOSE_PAREN))
18689 /* Consume the `void' token. */
18690 cp_lexer_consume_token (parser->lexer);
18691 /* There are no parameters. */
18692 return void_list_node;
18695 /* Parse the parameter-declaration-list. */
18696 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18697 /* If a parse error occurred while parsing the
18698 parameter-declaration-list, then the entire
18699 parameter-declaration-clause is erroneous. */
18700 if (is_error)
18701 return NULL;
18703 /* Peek at the next token. */
18704 token = cp_lexer_peek_token (parser->lexer);
18705 /* If it's a `,', the clause should terminate with an ellipsis. */
18706 if (token->type == CPP_COMMA)
18708 /* Consume the `,'. */
18709 cp_lexer_consume_token (parser->lexer);
18710 /* Expect an ellipsis. */
18711 ellipsis_p
18712 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18714 /* It might also be `...' if the optional trailing `,' was
18715 omitted. */
18716 else if (token->type == CPP_ELLIPSIS)
18718 /* Consume the `...' token. */
18719 cp_lexer_consume_token (parser->lexer);
18720 /* And remember that we saw it. */
18721 ellipsis_p = true;
18723 else
18724 ellipsis_p = false;
18726 /* Finish the parameter list. */
18727 if (!ellipsis_p)
18728 parameters = chainon (parameters, void_list_node);
18730 return parameters;
18733 /* Parse a parameter-declaration-list.
18735 parameter-declaration-list:
18736 parameter-declaration
18737 parameter-declaration-list , parameter-declaration
18739 Returns a representation of the parameter-declaration-list, as for
18740 cp_parser_parameter_declaration_clause. However, the
18741 `void_list_node' is never appended to the list. Upon return,
18742 *IS_ERROR will be true iff an error occurred. */
18744 static tree
18745 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18747 tree parameters = NULL_TREE;
18748 tree *tail = &parameters;
18749 bool saved_in_unbraced_linkage_specification_p;
18750 int index = 0;
18752 /* Assume all will go well. */
18753 *is_error = false;
18754 /* The special considerations that apply to a function within an
18755 unbraced linkage specifications do not apply to the parameters
18756 to the function. */
18757 saved_in_unbraced_linkage_specification_p
18758 = parser->in_unbraced_linkage_specification_p;
18759 parser->in_unbraced_linkage_specification_p = false;
18761 /* Look for more parameters. */
18762 while (true)
18764 cp_parameter_declarator *parameter;
18765 tree decl = error_mark_node;
18766 bool parenthesized_p = false;
18767 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18768 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18769 (current_template_parms)) : 0);
18771 /* Parse the parameter. */
18772 parameter
18773 = cp_parser_parameter_declaration (parser,
18774 /*template_parm_p=*/false,
18775 &parenthesized_p);
18777 /* We don't know yet if the enclosing context is deprecated, so wait
18778 and warn in grokparms if appropriate. */
18779 deprecated_state = DEPRECATED_SUPPRESS;
18781 if (parameter)
18783 /* If a function parameter pack was specified and an implicit template
18784 parameter was introduced during cp_parser_parameter_declaration,
18785 change any implicit parameters introduced into packs. */
18786 if (parser->implicit_template_parms
18787 && parameter->declarator
18788 && parameter->declarator->parameter_pack_p)
18790 int latest_template_parm_idx = TREE_VEC_LENGTH
18791 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18793 if (latest_template_parm_idx != template_parm_idx)
18794 parameter->decl_specifiers.type = convert_generic_types_to_packs
18795 (parameter->decl_specifiers.type,
18796 template_parm_idx, latest_template_parm_idx);
18799 decl = grokdeclarator (parameter->declarator,
18800 &parameter->decl_specifiers,
18801 PARM,
18802 parameter->default_argument != NULL_TREE,
18803 &parameter->decl_specifiers.attributes);
18806 deprecated_state = DEPRECATED_NORMAL;
18808 /* If a parse error occurred parsing the parameter declaration,
18809 then the entire parameter-declaration-list is erroneous. */
18810 if (decl == error_mark_node)
18812 *is_error = true;
18813 parameters = error_mark_node;
18814 break;
18817 if (parameter->decl_specifiers.attributes)
18818 cplus_decl_attributes (&decl,
18819 parameter->decl_specifiers.attributes,
18821 if (DECL_NAME (decl))
18822 decl = pushdecl (decl);
18824 if (decl != error_mark_node)
18826 retrofit_lang_decl (decl);
18827 DECL_PARM_INDEX (decl) = ++index;
18828 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18831 /* Add the new parameter to the list. */
18832 *tail = build_tree_list (parameter->default_argument, decl);
18833 tail = &TREE_CHAIN (*tail);
18835 /* Peek at the next token. */
18836 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18837 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18838 /* These are for Objective-C++ */
18839 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18840 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18841 /* The parameter-declaration-list is complete. */
18842 break;
18843 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18845 cp_token *token;
18847 /* Peek at the next token. */
18848 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18849 /* If it's an ellipsis, then the list is complete. */
18850 if (token->type == CPP_ELLIPSIS)
18851 break;
18852 /* Otherwise, there must be more parameters. Consume the
18853 `,'. */
18854 cp_lexer_consume_token (parser->lexer);
18855 /* When parsing something like:
18857 int i(float f, double d)
18859 we can tell after seeing the declaration for "f" that we
18860 are not looking at an initialization of a variable "i",
18861 but rather at the declaration of a function "i".
18863 Due to the fact that the parsing of template arguments
18864 (as specified to a template-id) requires backtracking we
18865 cannot use this technique when inside a template argument
18866 list. */
18867 if (!parser->in_template_argument_list_p
18868 && !parser->in_type_id_in_expr_p
18869 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18870 /* However, a parameter-declaration of the form
18871 "float(f)" (which is a valid declaration of a
18872 parameter "f") can also be interpreted as an
18873 expression (the conversion of "f" to "float"). */
18874 && !parenthesized_p)
18875 cp_parser_commit_to_tentative_parse (parser);
18877 else
18879 cp_parser_error (parser, "expected %<,%> or %<...%>");
18880 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18881 cp_parser_skip_to_closing_parenthesis (parser,
18882 /*recovering=*/true,
18883 /*or_comma=*/false,
18884 /*consume_paren=*/false);
18885 break;
18889 parser->in_unbraced_linkage_specification_p
18890 = saved_in_unbraced_linkage_specification_p;
18892 /* Reset implicit_template_scope if we are about to leave the function
18893 parameter list that introduced it. Note that for out-of-line member
18894 definitions, there will be one or more class scopes before we get to
18895 the template parameter scope. */
18897 if (cp_binding_level *its = parser->implicit_template_scope)
18898 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18900 while (maybe_its->kind == sk_class)
18901 maybe_its = maybe_its->level_chain;
18902 if (maybe_its == its)
18904 parser->implicit_template_parms = 0;
18905 parser->implicit_template_scope = 0;
18909 return parameters;
18912 /* Parse a parameter declaration.
18914 parameter-declaration:
18915 decl-specifier-seq ... [opt] declarator
18916 decl-specifier-seq declarator = assignment-expression
18917 decl-specifier-seq ... [opt] abstract-declarator [opt]
18918 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18920 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18921 declares a template parameter. (In that case, a non-nested `>'
18922 token encountered during the parsing of the assignment-expression
18923 is not interpreted as a greater-than operator.)
18925 Returns a representation of the parameter, or NULL if an error
18926 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18927 true iff the declarator is of the form "(p)". */
18929 static cp_parameter_declarator *
18930 cp_parser_parameter_declaration (cp_parser *parser,
18931 bool template_parm_p,
18932 bool *parenthesized_p)
18934 int declares_class_or_enum;
18935 cp_decl_specifier_seq decl_specifiers;
18936 cp_declarator *declarator;
18937 tree default_argument;
18938 cp_token *token = NULL, *declarator_token_start = NULL;
18939 const char *saved_message;
18941 /* In a template parameter, `>' is not an operator.
18943 [temp.param]
18945 When parsing a default template-argument for a non-type
18946 template-parameter, the first non-nested `>' is taken as the end
18947 of the template parameter-list rather than a greater-than
18948 operator. */
18950 /* Type definitions may not appear in parameter types. */
18951 saved_message = parser->type_definition_forbidden_message;
18952 parser->type_definition_forbidden_message
18953 = G_("types may not be defined in parameter types");
18955 /* Parse the declaration-specifiers. */
18956 cp_parser_decl_specifier_seq (parser,
18957 CP_PARSER_FLAGS_NONE,
18958 &decl_specifiers,
18959 &declares_class_or_enum);
18961 /* Complain about missing 'typename' or other invalid type names. */
18962 if (!decl_specifiers.any_type_specifiers_p
18963 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18964 decl_specifiers.type = error_mark_node;
18966 /* If an error occurred, there's no reason to attempt to parse the
18967 rest of the declaration. */
18968 if (cp_parser_error_occurred (parser))
18970 parser->type_definition_forbidden_message = saved_message;
18971 return NULL;
18974 /* Peek at the next token. */
18975 token = cp_lexer_peek_token (parser->lexer);
18977 /* If the next token is a `)', `,', `=', `>', or `...', then there
18978 is no declarator. However, when variadic templates are enabled,
18979 there may be a declarator following `...'. */
18980 if (token->type == CPP_CLOSE_PAREN
18981 || token->type == CPP_COMMA
18982 || token->type == CPP_EQ
18983 || token->type == CPP_GREATER)
18985 declarator = NULL;
18986 if (parenthesized_p)
18987 *parenthesized_p = false;
18989 /* Otherwise, there should be a declarator. */
18990 else
18992 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18993 parser->default_arg_ok_p = false;
18995 /* After seeing a decl-specifier-seq, if the next token is not a
18996 "(", there is no possibility that the code is a valid
18997 expression. Therefore, if parsing tentatively, we commit at
18998 this point. */
18999 if (!parser->in_template_argument_list_p
19000 /* In an expression context, having seen:
19002 (int((char ...
19004 we cannot be sure whether we are looking at a
19005 function-type (taking a "char" as a parameter) or a cast
19006 of some object of type "char" to "int". */
19007 && !parser->in_type_id_in_expr_p
19008 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19009 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19010 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19011 cp_parser_commit_to_tentative_parse (parser);
19012 /* Parse the declarator. */
19013 declarator_token_start = token;
19014 declarator = cp_parser_declarator (parser,
19015 CP_PARSER_DECLARATOR_EITHER,
19016 /*ctor_dtor_or_conv_p=*/NULL,
19017 parenthesized_p,
19018 /*member_p=*/false,
19019 /*friend_p=*/false);
19020 parser->default_arg_ok_p = saved_default_arg_ok_p;
19021 /* After the declarator, allow more attributes. */
19022 decl_specifiers.attributes
19023 = chainon (decl_specifiers.attributes,
19024 cp_parser_attributes_opt (parser));
19027 /* If the next token is an ellipsis, and we have not seen a
19028 declarator name, and the type of the declarator contains parameter
19029 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19030 a parameter pack expansion expression. Otherwise, leave the
19031 ellipsis for a C-style variadic function. */
19032 token = cp_lexer_peek_token (parser->lexer);
19033 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19035 tree type = decl_specifiers.type;
19037 if (type && DECL_P (type))
19038 type = TREE_TYPE (type);
19040 if (type
19041 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19042 && declarator_can_be_parameter_pack (declarator)
19043 && (!declarator || !declarator->parameter_pack_p)
19044 && uses_parameter_packs (type))
19046 /* Consume the `...'. */
19047 cp_lexer_consume_token (parser->lexer);
19048 maybe_warn_variadic_templates ();
19050 /* Build a pack expansion type */
19051 if (declarator)
19052 declarator->parameter_pack_p = true;
19053 else
19054 decl_specifiers.type = make_pack_expansion (type);
19058 /* The restriction on defining new types applies only to the type
19059 of the parameter, not to the default argument. */
19060 parser->type_definition_forbidden_message = saved_message;
19062 /* If the next token is `=', then process a default argument. */
19063 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19065 token = cp_lexer_peek_token (parser->lexer);
19066 /* If we are defining a class, then the tokens that make up the
19067 default argument must be saved and processed later. */
19068 if (!template_parm_p && at_class_scope_p ()
19069 && TYPE_BEING_DEFINED (current_class_type)
19070 && !LAMBDA_TYPE_P (current_class_type))
19071 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19072 /* Outside of a class definition, we can just parse the
19073 assignment-expression. */
19074 else
19075 default_argument
19076 = cp_parser_default_argument (parser, template_parm_p);
19078 if (!parser->default_arg_ok_p)
19080 if (flag_permissive)
19081 warning (0, "deprecated use of default argument for parameter of non-function");
19082 else
19084 error_at (token->location,
19085 "default arguments are only "
19086 "permitted for function parameters");
19087 default_argument = NULL_TREE;
19090 else if ((declarator && declarator->parameter_pack_p)
19091 || (decl_specifiers.type
19092 && PACK_EXPANSION_P (decl_specifiers.type)))
19094 /* Find the name of the parameter pack. */
19095 cp_declarator *id_declarator = declarator;
19096 while (id_declarator && id_declarator->kind != cdk_id)
19097 id_declarator = id_declarator->declarator;
19099 if (id_declarator && id_declarator->kind == cdk_id)
19100 error_at (declarator_token_start->location,
19101 template_parm_p
19102 ? G_("template parameter pack %qD "
19103 "cannot have a default argument")
19104 : G_("parameter pack %qD cannot have "
19105 "a default argument"),
19106 id_declarator->u.id.unqualified_name);
19107 else
19108 error_at (declarator_token_start->location,
19109 template_parm_p
19110 ? G_("template parameter pack cannot have "
19111 "a default argument")
19112 : G_("parameter pack cannot have a "
19113 "default argument"));
19115 default_argument = NULL_TREE;
19118 else
19119 default_argument = NULL_TREE;
19121 return make_parameter_declarator (&decl_specifiers,
19122 declarator,
19123 default_argument);
19126 /* Parse a default argument and return it.
19128 TEMPLATE_PARM_P is true if this is a default argument for a
19129 non-type template parameter. */
19130 static tree
19131 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19133 tree default_argument = NULL_TREE;
19134 bool saved_greater_than_is_operator_p;
19135 bool saved_local_variables_forbidden_p;
19136 bool non_constant_p, is_direct_init;
19138 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19139 set correctly. */
19140 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19141 parser->greater_than_is_operator_p = !template_parm_p;
19142 /* Local variable names (and the `this' keyword) may not
19143 appear in a default argument. */
19144 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19145 parser->local_variables_forbidden_p = true;
19146 /* Parse the assignment-expression. */
19147 if (template_parm_p)
19148 push_deferring_access_checks (dk_no_deferred);
19149 tree saved_class_ptr = NULL_TREE;
19150 tree saved_class_ref = NULL_TREE;
19151 /* The "this" pointer is not valid in a default argument. */
19152 if (cfun)
19154 saved_class_ptr = current_class_ptr;
19155 cp_function_chain->x_current_class_ptr = NULL_TREE;
19156 saved_class_ref = current_class_ref;
19157 cp_function_chain->x_current_class_ref = NULL_TREE;
19159 default_argument
19160 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19161 /* Restore the "this" pointer. */
19162 if (cfun)
19164 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19165 cp_function_chain->x_current_class_ref = saved_class_ref;
19167 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19168 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19169 if (template_parm_p)
19170 pop_deferring_access_checks ();
19171 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19172 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19174 return default_argument;
19177 /* Parse a function-body.
19179 function-body:
19180 compound_statement */
19182 static void
19183 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19185 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19188 /* Parse a ctor-initializer-opt followed by a function-body. Return
19189 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19190 is true we are parsing a function-try-block. */
19192 static bool
19193 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19194 bool in_function_try_block)
19196 tree body, list;
19197 bool ctor_initializer_p;
19198 const bool check_body_p =
19199 DECL_CONSTRUCTOR_P (current_function_decl)
19200 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19201 tree last = NULL;
19203 /* Begin the function body. */
19204 body = begin_function_body ();
19205 /* Parse the optional ctor-initializer. */
19206 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19208 /* If we're parsing a constexpr constructor definition, we need
19209 to check that the constructor body is indeed empty. However,
19210 before we get to cp_parser_function_body lot of junk has been
19211 generated, so we can't just check that we have an empty block.
19212 Rather we take a snapshot of the outermost block, and check whether
19213 cp_parser_function_body changed its state. */
19214 if (check_body_p)
19216 list = cur_stmt_list;
19217 if (STATEMENT_LIST_TAIL (list))
19218 last = STATEMENT_LIST_TAIL (list)->stmt;
19220 /* Parse the function-body. */
19221 cp_parser_function_body (parser, in_function_try_block);
19222 if (check_body_p)
19223 check_constexpr_ctor_body (last, list, /*complain=*/true);
19224 /* Finish the function body. */
19225 finish_function_body (body);
19227 return ctor_initializer_p;
19230 /* Parse an initializer.
19232 initializer:
19233 = initializer-clause
19234 ( expression-list )
19236 Returns an expression representing the initializer. If no
19237 initializer is present, NULL_TREE is returned.
19239 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19240 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19241 set to TRUE if there is no initializer present. If there is an
19242 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19243 is set to true; otherwise it is set to false. */
19245 static tree
19246 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19247 bool* non_constant_p)
19249 cp_token *token;
19250 tree init;
19252 /* Peek at the next token. */
19253 token = cp_lexer_peek_token (parser->lexer);
19255 /* Let our caller know whether or not this initializer was
19256 parenthesized. */
19257 *is_direct_init = (token->type != CPP_EQ);
19258 /* Assume that the initializer is constant. */
19259 *non_constant_p = false;
19261 if (token->type == CPP_EQ)
19263 /* Consume the `='. */
19264 cp_lexer_consume_token (parser->lexer);
19265 /* Parse the initializer-clause. */
19266 init = cp_parser_initializer_clause (parser, non_constant_p);
19268 else if (token->type == CPP_OPEN_PAREN)
19270 vec<tree, va_gc> *vec;
19271 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19272 /*cast_p=*/false,
19273 /*allow_expansion_p=*/true,
19274 non_constant_p);
19275 if (vec == NULL)
19276 return error_mark_node;
19277 init = build_tree_list_vec (vec);
19278 release_tree_vector (vec);
19280 else if (token->type == CPP_OPEN_BRACE)
19282 cp_lexer_set_source_position (parser->lexer);
19283 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19284 init = cp_parser_braced_list (parser, non_constant_p);
19285 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19287 else
19289 /* Anything else is an error. */
19290 cp_parser_error (parser, "expected initializer");
19291 init = error_mark_node;
19294 return init;
19297 /* Parse an initializer-clause.
19299 initializer-clause:
19300 assignment-expression
19301 braced-init-list
19303 Returns an expression representing the initializer.
19305 If the `assignment-expression' production is used the value
19306 returned is simply a representation for the expression.
19308 Otherwise, calls cp_parser_braced_list. */
19310 static tree
19311 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19313 tree initializer;
19315 /* Assume the expression is constant. */
19316 *non_constant_p = false;
19318 /* If it is not a `{', then we are looking at an
19319 assignment-expression. */
19320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19322 initializer
19323 = cp_parser_constant_expression (parser,
19324 /*allow_non_constant_p=*/true,
19325 non_constant_p);
19327 else
19328 initializer = cp_parser_braced_list (parser, non_constant_p);
19330 return initializer;
19333 /* Parse a brace-enclosed initializer list.
19335 braced-init-list:
19336 { initializer-list , [opt] }
19339 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19340 the elements of the initializer-list (or NULL, if the last
19341 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19342 NULL_TREE. There is no way to detect whether or not the optional
19343 trailing `,' was provided. NON_CONSTANT_P is as for
19344 cp_parser_initializer. */
19346 static tree
19347 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19349 tree initializer;
19351 /* Consume the `{' token. */
19352 cp_lexer_consume_token (parser->lexer);
19353 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19354 initializer = make_node (CONSTRUCTOR);
19355 /* If it's not a `}', then there is a non-trivial initializer. */
19356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19358 /* Parse the initializer list. */
19359 CONSTRUCTOR_ELTS (initializer)
19360 = cp_parser_initializer_list (parser, non_constant_p);
19361 /* A trailing `,' token is allowed. */
19362 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19363 cp_lexer_consume_token (parser->lexer);
19365 else
19366 *non_constant_p = false;
19367 /* Now, there should be a trailing `}'. */
19368 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19369 TREE_TYPE (initializer) = init_list_type_node;
19370 return initializer;
19373 /* Consume tokens up to, and including, the next non-nested closing `]'.
19374 Returns true iff we found a closing `]'. */
19376 static bool
19377 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19379 unsigned square_depth = 0;
19381 while (true)
19383 cp_token * token = cp_lexer_peek_token (parser->lexer);
19385 switch (token->type)
19387 case CPP_EOF:
19388 case CPP_PRAGMA_EOL:
19389 /* If we've run out of tokens, then there is no closing `]'. */
19390 return false;
19392 case CPP_OPEN_SQUARE:
19393 ++square_depth;
19394 break;
19396 case CPP_CLOSE_SQUARE:
19397 if (!square_depth--)
19399 cp_lexer_consume_token (parser->lexer);
19400 return true;
19402 break;
19404 default:
19405 break;
19408 /* Consume the token. */
19409 cp_lexer_consume_token (parser->lexer);
19413 /* Return true if we are looking at an array-designator, false otherwise. */
19415 static bool
19416 cp_parser_array_designator_p (cp_parser *parser)
19418 /* Consume the `['. */
19419 cp_lexer_consume_token (parser->lexer);
19421 cp_lexer_save_tokens (parser->lexer);
19423 /* Skip tokens until the next token is a closing square bracket.
19424 If we find the closing `]', and the next token is a `=', then
19425 we are looking at an array designator. */
19426 bool array_designator_p
19427 = (cp_parser_skip_to_closing_square_bracket (parser)
19428 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19430 /* Roll back the tokens we skipped. */
19431 cp_lexer_rollback_tokens (parser->lexer);
19433 return array_designator_p;
19436 /* Parse an initializer-list.
19438 initializer-list:
19439 initializer-clause ... [opt]
19440 initializer-list , initializer-clause ... [opt]
19442 GNU Extension:
19444 initializer-list:
19445 designation initializer-clause ...[opt]
19446 initializer-list , designation initializer-clause ...[opt]
19448 designation:
19449 . identifier =
19450 identifier :
19451 [ constant-expression ] =
19453 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19454 for the initializer. If the INDEX of the elt is non-NULL, it is the
19455 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19456 as for cp_parser_initializer. */
19458 static vec<constructor_elt, va_gc> *
19459 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19461 vec<constructor_elt, va_gc> *v = NULL;
19463 /* Assume all of the expressions are constant. */
19464 *non_constant_p = false;
19466 /* Parse the rest of the list. */
19467 while (true)
19469 cp_token *token;
19470 tree designator;
19471 tree initializer;
19472 bool clause_non_constant_p;
19474 /* If the next token is an identifier and the following one is a
19475 colon, we are looking at the GNU designated-initializer
19476 syntax. */
19477 if (cp_parser_allow_gnu_extensions_p (parser)
19478 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19481 /* Warn the user that they are using an extension. */
19482 pedwarn (input_location, OPT_Wpedantic,
19483 "ISO C++ does not allow designated initializers");
19484 /* Consume the identifier. */
19485 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19486 /* Consume the `:'. */
19487 cp_lexer_consume_token (parser->lexer);
19489 /* Also handle the C99 syntax, '. id ='. */
19490 else if (cp_parser_allow_gnu_extensions_p (parser)
19491 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19492 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19493 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19495 /* Warn the user that they are using an extension. */
19496 pedwarn (input_location, OPT_Wpedantic,
19497 "ISO C++ does not allow C99 designated initializers");
19498 /* Consume the `.'. */
19499 cp_lexer_consume_token (parser->lexer);
19500 /* Consume the identifier. */
19501 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19502 /* Consume the `='. */
19503 cp_lexer_consume_token (parser->lexer);
19505 /* Also handle C99 array designators, '[ const ] ='. */
19506 else if (cp_parser_allow_gnu_extensions_p (parser)
19507 && !c_dialect_objc ()
19508 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19510 /* In C++11, [ could start a lambda-introducer. */
19511 bool non_const = false;
19513 cp_parser_parse_tentatively (parser);
19515 if (!cp_parser_array_designator_p (parser))
19517 cp_parser_simulate_error (parser);
19518 designator = NULL_TREE;
19520 else
19522 designator = cp_parser_constant_expression (parser, true,
19523 &non_const);
19524 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19525 cp_parser_require (parser, CPP_EQ, RT_EQ);
19528 if (!cp_parser_parse_definitely (parser))
19529 designator = NULL_TREE;
19530 else if (non_const)
19531 require_potential_rvalue_constant_expression (designator);
19533 else
19534 designator = NULL_TREE;
19536 /* Parse the initializer. */
19537 initializer = cp_parser_initializer_clause (parser,
19538 &clause_non_constant_p);
19539 /* If any clause is non-constant, so is the entire initializer. */
19540 if (clause_non_constant_p)
19541 *non_constant_p = true;
19543 /* If we have an ellipsis, this is an initializer pack
19544 expansion. */
19545 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19547 /* Consume the `...'. */
19548 cp_lexer_consume_token (parser->lexer);
19550 /* Turn the initializer into an initializer expansion. */
19551 initializer = make_pack_expansion (initializer);
19554 /* Add it to the vector. */
19555 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19557 /* If the next token is not a comma, we have reached the end of
19558 the list. */
19559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19560 break;
19562 /* Peek at the next token. */
19563 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19564 /* If the next token is a `}', then we're still done. An
19565 initializer-clause can have a trailing `,' after the
19566 initializer-list and before the closing `}'. */
19567 if (token->type == CPP_CLOSE_BRACE)
19568 break;
19570 /* Consume the `,' token. */
19571 cp_lexer_consume_token (parser->lexer);
19574 return v;
19577 /* Classes [gram.class] */
19579 /* Parse a class-name.
19581 class-name:
19582 identifier
19583 template-id
19585 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19586 to indicate that names looked up in dependent types should be
19587 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19588 keyword has been used to indicate that the name that appears next
19589 is a template. TAG_TYPE indicates the explicit tag given before
19590 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19591 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19592 is the class being defined in a class-head. If ENUM_OK is TRUE,
19593 enum-names are also accepted.
19595 Returns the TYPE_DECL representing the class. */
19597 static tree
19598 cp_parser_class_name (cp_parser *parser,
19599 bool typename_keyword_p,
19600 bool template_keyword_p,
19601 enum tag_types tag_type,
19602 bool check_dependency_p,
19603 bool class_head_p,
19604 bool is_declaration,
19605 bool enum_ok)
19607 tree decl;
19608 tree scope;
19609 bool typename_p;
19610 cp_token *token;
19611 tree identifier = NULL_TREE;
19613 /* All class-names start with an identifier. */
19614 token = cp_lexer_peek_token (parser->lexer);
19615 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19617 cp_parser_error (parser, "expected class-name");
19618 return error_mark_node;
19621 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19622 to a template-id, so we save it here. */
19623 scope = parser->scope;
19624 if (scope == error_mark_node)
19625 return error_mark_node;
19627 /* Any name names a type if we're following the `typename' keyword
19628 in a qualified name where the enclosing scope is type-dependent. */
19629 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19630 && dependent_type_p (scope));
19631 /* Handle the common case (an identifier, but not a template-id)
19632 efficiently. */
19633 if (token->type == CPP_NAME
19634 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19636 cp_token *identifier_token;
19637 bool ambiguous_p;
19639 /* Look for the identifier. */
19640 identifier_token = cp_lexer_peek_token (parser->lexer);
19641 ambiguous_p = identifier_token->error_reported;
19642 identifier = cp_parser_identifier (parser);
19643 /* If the next token isn't an identifier, we are certainly not
19644 looking at a class-name. */
19645 if (identifier == error_mark_node)
19646 decl = error_mark_node;
19647 /* If we know this is a type-name, there's no need to look it
19648 up. */
19649 else if (typename_p)
19650 decl = identifier;
19651 else
19653 tree ambiguous_decls;
19654 /* If we already know that this lookup is ambiguous, then
19655 we've already issued an error message; there's no reason
19656 to check again. */
19657 if (ambiguous_p)
19659 cp_parser_simulate_error (parser);
19660 return error_mark_node;
19662 /* If the next token is a `::', then the name must be a type
19663 name.
19665 [basic.lookup.qual]
19667 During the lookup for a name preceding the :: scope
19668 resolution operator, object, function, and enumerator
19669 names are ignored. */
19670 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19671 tag_type = typename_type;
19672 /* Look up the name. */
19673 decl = cp_parser_lookup_name (parser, identifier,
19674 tag_type,
19675 /*is_template=*/false,
19676 /*is_namespace=*/false,
19677 check_dependency_p,
19678 &ambiguous_decls,
19679 identifier_token->location);
19680 if (ambiguous_decls)
19682 if (cp_parser_parsing_tentatively (parser))
19683 cp_parser_simulate_error (parser);
19684 return error_mark_node;
19688 else
19690 /* Try a template-id. */
19691 decl = cp_parser_template_id (parser, template_keyword_p,
19692 check_dependency_p,
19693 tag_type,
19694 is_declaration);
19695 if (decl == error_mark_node)
19696 return error_mark_node;
19699 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19701 /* If this is a typename, create a TYPENAME_TYPE. */
19702 if (typename_p && decl != error_mark_node)
19704 decl = make_typename_type (scope, decl, typename_type,
19705 /*complain=*/tf_error);
19706 if (decl != error_mark_node)
19707 decl = TYPE_NAME (decl);
19710 decl = strip_using_decl (decl);
19712 /* Check to see that it is really the name of a class. */
19713 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19714 && identifier_p (TREE_OPERAND (decl, 0))
19715 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19716 /* Situations like this:
19718 template <typename T> struct A {
19719 typename T::template X<int>::I i;
19722 are problematic. Is `T::template X<int>' a class-name? The
19723 standard does not seem to be definitive, but there is no other
19724 valid interpretation of the following `::'. Therefore, those
19725 names are considered class-names. */
19727 decl = make_typename_type (scope, decl, tag_type, tf_error);
19728 if (decl != error_mark_node)
19729 decl = TYPE_NAME (decl);
19731 else if (TREE_CODE (decl) != TYPE_DECL
19732 || TREE_TYPE (decl) == error_mark_node
19733 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19734 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19735 /* In Objective-C 2.0, a classname followed by '.' starts a
19736 dot-syntax expression, and it's not a type-name. */
19737 || (c_dialect_objc ()
19738 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19739 && objc_is_class_name (decl)))
19740 decl = error_mark_node;
19742 if (decl == error_mark_node)
19743 cp_parser_error (parser, "expected class-name");
19744 else if (identifier && !parser->scope)
19745 maybe_note_name_used_in_class (identifier, decl);
19747 return decl;
19750 /* Parse a class-specifier.
19752 class-specifier:
19753 class-head { member-specification [opt] }
19755 Returns the TREE_TYPE representing the class. */
19757 static tree
19758 cp_parser_class_specifier_1 (cp_parser* parser)
19760 tree type;
19761 tree attributes = NULL_TREE;
19762 bool nested_name_specifier_p;
19763 unsigned saved_num_template_parameter_lists;
19764 bool saved_in_function_body;
19765 unsigned char in_statement;
19766 bool in_switch_statement_p;
19767 bool saved_in_unbraced_linkage_specification_p;
19768 tree old_scope = NULL_TREE;
19769 tree scope = NULL_TREE;
19770 cp_token *closing_brace;
19772 push_deferring_access_checks (dk_no_deferred);
19774 /* Parse the class-head. */
19775 type = cp_parser_class_head (parser,
19776 &nested_name_specifier_p);
19777 /* If the class-head was a semantic disaster, skip the entire body
19778 of the class. */
19779 if (!type)
19781 cp_parser_skip_to_end_of_block_or_statement (parser);
19782 pop_deferring_access_checks ();
19783 return error_mark_node;
19786 /* Look for the `{'. */
19787 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19789 pop_deferring_access_checks ();
19790 return error_mark_node;
19793 cp_ensure_no_omp_declare_simd (parser);
19795 /* Issue an error message if type-definitions are forbidden here. */
19796 cp_parser_check_type_definition (parser);
19797 /* Remember that we are defining one more class. */
19798 ++parser->num_classes_being_defined;
19799 /* Inside the class, surrounding template-parameter-lists do not
19800 apply. */
19801 saved_num_template_parameter_lists
19802 = parser->num_template_parameter_lists;
19803 parser->num_template_parameter_lists = 0;
19804 /* We are not in a function body. */
19805 saved_in_function_body = parser->in_function_body;
19806 parser->in_function_body = false;
19807 /* Or in a loop. */
19808 in_statement = parser->in_statement;
19809 parser->in_statement = 0;
19810 /* Or in a switch. */
19811 in_switch_statement_p = parser->in_switch_statement_p;
19812 parser->in_switch_statement_p = false;
19813 /* We are not immediately inside an extern "lang" block. */
19814 saved_in_unbraced_linkage_specification_p
19815 = parser->in_unbraced_linkage_specification_p;
19816 parser->in_unbraced_linkage_specification_p = false;
19818 /* Start the class. */
19819 if (nested_name_specifier_p)
19821 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19822 old_scope = push_inner_scope (scope);
19824 type = begin_class_definition (type);
19826 if (type == error_mark_node)
19827 /* If the type is erroneous, skip the entire body of the class. */
19828 cp_parser_skip_to_closing_brace (parser);
19829 else
19830 /* Parse the member-specification. */
19831 cp_parser_member_specification_opt (parser);
19833 /* Look for the trailing `}'. */
19834 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19835 /* Look for trailing attributes to apply to this class. */
19836 if (cp_parser_allow_gnu_extensions_p (parser))
19837 attributes = cp_parser_gnu_attributes_opt (parser);
19838 if (type != error_mark_node)
19839 type = finish_struct (type, attributes);
19840 if (nested_name_specifier_p)
19841 pop_inner_scope (old_scope, scope);
19843 /* We've finished a type definition. Check for the common syntax
19844 error of forgetting a semicolon after the definition. We need to
19845 be careful, as we can't just check for not-a-semicolon and be done
19846 with it; the user might have typed:
19848 class X { } c = ...;
19849 class X { } *p = ...;
19851 and so forth. Instead, enumerate all the possible tokens that
19852 might follow this production; if we don't see one of them, then
19853 complain and silently insert the semicolon. */
19855 cp_token *token = cp_lexer_peek_token (parser->lexer);
19856 bool want_semicolon = true;
19858 if (cp_next_tokens_can_be_std_attribute_p (parser))
19859 /* Don't try to parse c++11 attributes here. As per the
19860 grammar, that should be a task for
19861 cp_parser_decl_specifier_seq. */
19862 want_semicolon = false;
19864 switch (token->type)
19866 case CPP_NAME:
19867 case CPP_SEMICOLON:
19868 case CPP_MULT:
19869 case CPP_AND:
19870 case CPP_OPEN_PAREN:
19871 case CPP_CLOSE_PAREN:
19872 case CPP_COMMA:
19873 want_semicolon = false;
19874 break;
19876 /* While it's legal for type qualifiers and storage class
19877 specifiers to follow type definitions in the grammar, only
19878 compiler testsuites contain code like that. Assume that if
19879 we see such code, then what we're really seeing is a case
19880 like:
19882 class X { }
19883 const <type> var = ...;
19887 class Y { }
19888 static <type> func (...) ...
19890 i.e. the qualifier or specifier applies to the next
19891 declaration. To do so, however, we need to look ahead one
19892 more token to see if *that* token is a type specifier.
19894 This code could be improved to handle:
19896 class Z { }
19897 static const <type> var = ...; */
19898 case CPP_KEYWORD:
19899 if (keyword_is_decl_specifier (token->keyword))
19901 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19903 /* Handling user-defined types here would be nice, but very
19904 tricky. */
19905 want_semicolon
19906 = (lookahead->type == CPP_KEYWORD
19907 && keyword_begins_type_specifier (lookahead->keyword));
19909 break;
19910 default:
19911 break;
19914 /* If we don't have a type, then something is very wrong and we
19915 shouldn't try to do anything clever. Likewise for not seeing the
19916 closing brace. */
19917 if (closing_brace && TYPE_P (type) && want_semicolon)
19919 cp_token_position prev
19920 = cp_lexer_previous_token_position (parser->lexer);
19921 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19922 location_t loc = prev_token->location;
19924 if (CLASSTYPE_DECLARED_CLASS (type))
19925 error_at (loc, "expected %<;%> after class definition");
19926 else if (TREE_CODE (type) == RECORD_TYPE)
19927 error_at (loc, "expected %<;%> after struct definition");
19928 else if (TREE_CODE (type) == UNION_TYPE)
19929 error_at (loc, "expected %<;%> after union definition");
19930 else
19931 gcc_unreachable ();
19933 /* Unget one token and smash it to look as though we encountered
19934 a semicolon in the input stream. */
19935 cp_lexer_set_token_position (parser->lexer, prev);
19936 token = cp_lexer_peek_token (parser->lexer);
19937 token->type = CPP_SEMICOLON;
19938 token->keyword = RID_MAX;
19942 /* If this class is not itself within the scope of another class,
19943 then we need to parse the bodies of all of the queued function
19944 definitions. Note that the queued functions defined in a class
19945 are not always processed immediately following the
19946 class-specifier for that class. Consider:
19948 struct A {
19949 struct B { void f() { sizeof (A); } };
19952 If `f' were processed before the processing of `A' were
19953 completed, there would be no way to compute the size of `A'.
19954 Note that the nesting we are interested in here is lexical --
19955 not the semantic nesting given by TYPE_CONTEXT. In particular,
19956 for:
19958 struct A { struct B; };
19959 struct A::B { void f() { } };
19961 there is no need to delay the parsing of `A::B::f'. */
19962 if (--parser->num_classes_being_defined == 0)
19964 tree decl;
19965 tree class_type = NULL_TREE;
19966 tree pushed_scope = NULL_TREE;
19967 unsigned ix;
19968 cp_default_arg_entry *e;
19969 tree save_ccp, save_ccr;
19971 /* In a first pass, parse default arguments to the functions.
19972 Then, in a second pass, parse the bodies of the functions.
19973 This two-phased approach handles cases like:
19975 struct S {
19976 void f() { g(); }
19977 void g(int i = 3);
19981 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19983 decl = e->decl;
19984 /* If there are default arguments that have not yet been processed,
19985 take care of them now. */
19986 if (class_type != e->class_type)
19988 if (pushed_scope)
19989 pop_scope (pushed_scope);
19990 class_type = e->class_type;
19991 pushed_scope = push_scope (class_type);
19993 /* Make sure that any template parameters are in scope. */
19994 maybe_begin_member_template_processing (decl);
19995 /* Parse the default argument expressions. */
19996 cp_parser_late_parsing_default_args (parser, decl);
19997 /* Remove any template parameters from the symbol table. */
19998 maybe_end_member_template_processing ();
20000 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20001 /* Now parse any NSDMIs. */
20002 save_ccp = current_class_ptr;
20003 save_ccr = current_class_ref;
20004 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20006 if (class_type != DECL_CONTEXT (decl))
20008 if (pushed_scope)
20009 pop_scope (pushed_scope);
20010 class_type = DECL_CONTEXT (decl);
20011 pushed_scope = push_scope (class_type);
20013 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20014 cp_parser_late_parsing_nsdmi (parser, decl);
20016 vec_safe_truncate (unparsed_nsdmis, 0);
20017 current_class_ptr = save_ccp;
20018 current_class_ref = save_ccr;
20019 if (pushed_scope)
20020 pop_scope (pushed_scope);
20022 /* Now do some post-NSDMI bookkeeping. */
20023 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20024 after_nsdmi_defaulted_late_checks (class_type);
20025 vec_safe_truncate (unparsed_classes, 0);
20026 after_nsdmi_defaulted_late_checks (type);
20028 /* Now parse the body of the functions. */
20029 if (flag_openmp)
20031 /* OpenMP UDRs need to be parsed before all other functions. */
20032 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20033 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20034 cp_parser_late_parsing_for_member (parser, decl);
20035 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20036 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20037 cp_parser_late_parsing_for_member (parser, decl);
20039 else
20040 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20041 cp_parser_late_parsing_for_member (parser, decl);
20042 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20044 else
20045 vec_safe_push (unparsed_classes, type);
20047 /* Put back any saved access checks. */
20048 pop_deferring_access_checks ();
20050 /* Restore saved state. */
20051 parser->in_switch_statement_p = in_switch_statement_p;
20052 parser->in_statement = in_statement;
20053 parser->in_function_body = saved_in_function_body;
20054 parser->num_template_parameter_lists
20055 = saved_num_template_parameter_lists;
20056 parser->in_unbraced_linkage_specification_p
20057 = saved_in_unbraced_linkage_specification_p;
20059 return type;
20062 static tree
20063 cp_parser_class_specifier (cp_parser* parser)
20065 tree ret;
20066 timevar_push (TV_PARSE_STRUCT);
20067 ret = cp_parser_class_specifier_1 (parser);
20068 timevar_pop (TV_PARSE_STRUCT);
20069 return ret;
20072 /* Parse a class-head.
20074 class-head:
20075 class-key identifier [opt] base-clause [opt]
20076 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20077 class-key nested-name-specifier [opt] template-id
20078 base-clause [opt]
20080 class-virt-specifier:
20081 final
20083 GNU Extensions:
20084 class-key attributes identifier [opt] base-clause [opt]
20085 class-key attributes nested-name-specifier identifier base-clause [opt]
20086 class-key attributes nested-name-specifier [opt] template-id
20087 base-clause [opt]
20089 Upon return BASES is initialized to the list of base classes (or
20090 NULL, if there are none) in the same form returned by
20091 cp_parser_base_clause.
20093 Returns the TYPE of the indicated class. Sets
20094 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20095 involving a nested-name-specifier was used, and FALSE otherwise.
20097 Returns error_mark_node if this is not a class-head.
20099 Returns NULL_TREE if the class-head is syntactically valid, but
20100 semantically invalid in a way that means we should skip the entire
20101 body of the class. */
20103 static tree
20104 cp_parser_class_head (cp_parser* parser,
20105 bool* nested_name_specifier_p)
20107 tree nested_name_specifier;
20108 enum tag_types class_key;
20109 tree id = NULL_TREE;
20110 tree type = NULL_TREE;
20111 tree attributes;
20112 tree bases;
20113 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20114 bool template_id_p = false;
20115 bool qualified_p = false;
20116 bool invalid_nested_name_p = false;
20117 bool invalid_explicit_specialization_p = false;
20118 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20119 tree pushed_scope = NULL_TREE;
20120 unsigned num_templates;
20121 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20122 /* Assume no nested-name-specifier will be present. */
20123 *nested_name_specifier_p = false;
20124 /* Assume no template parameter lists will be used in defining the
20125 type. */
20126 num_templates = 0;
20127 parser->colon_corrects_to_scope_p = false;
20129 /* Look for the class-key. */
20130 class_key = cp_parser_class_key (parser);
20131 if (class_key == none_type)
20132 return error_mark_node;
20134 /* Parse the attributes. */
20135 attributes = cp_parser_attributes_opt (parser);
20137 /* If the next token is `::', that is invalid -- but sometimes
20138 people do try to write:
20140 struct ::S {};
20142 Handle this gracefully by accepting the extra qualifier, and then
20143 issuing an error about it later if this really is a
20144 class-head. If it turns out just to be an elaborated type
20145 specifier, remain silent. */
20146 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20147 qualified_p = true;
20149 push_deferring_access_checks (dk_no_check);
20151 /* Determine the name of the class. Begin by looking for an
20152 optional nested-name-specifier. */
20153 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20154 nested_name_specifier
20155 = cp_parser_nested_name_specifier_opt (parser,
20156 /*typename_keyword_p=*/false,
20157 /*check_dependency_p=*/false,
20158 /*type_p=*/true,
20159 /*is_declaration=*/false);
20160 /* If there was a nested-name-specifier, then there *must* be an
20161 identifier. */
20162 if (nested_name_specifier)
20164 type_start_token = cp_lexer_peek_token (parser->lexer);
20165 /* Although the grammar says `identifier', it really means
20166 `class-name' or `template-name'. You are only allowed to
20167 define a class that has already been declared with this
20168 syntax.
20170 The proposed resolution for Core Issue 180 says that wherever
20171 you see `class T::X' you should treat `X' as a type-name.
20173 It is OK to define an inaccessible class; for example:
20175 class A { class B; };
20176 class A::B {};
20178 We do not know if we will see a class-name, or a
20179 template-name. We look for a class-name first, in case the
20180 class-name is a template-id; if we looked for the
20181 template-name first we would stop after the template-name. */
20182 cp_parser_parse_tentatively (parser);
20183 type = cp_parser_class_name (parser,
20184 /*typename_keyword_p=*/false,
20185 /*template_keyword_p=*/false,
20186 class_type,
20187 /*check_dependency_p=*/false,
20188 /*class_head_p=*/true,
20189 /*is_declaration=*/false);
20190 /* If that didn't work, ignore the nested-name-specifier. */
20191 if (!cp_parser_parse_definitely (parser))
20193 invalid_nested_name_p = true;
20194 type_start_token = cp_lexer_peek_token (parser->lexer);
20195 id = cp_parser_identifier (parser);
20196 if (id == error_mark_node)
20197 id = NULL_TREE;
20199 /* If we could not find a corresponding TYPE, treat this
20200 declaration like an unqualified declaration. */
20201 if (type == error_mark_node)
20202 nested_name_specifier = NULL_TREE;
20203 /* Otherwise, count the number of templates used in TYPE and its
20204 containing scopes. */
20205 else
20207 tree scope;
20209 for (scope = TREE_TYPE (type);
20210 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20211 scope = get_containing_scope (scope))
20212 if (TYPE_P (scope)
20213 && CLASS_TYPE_P (scope)
20214 && CLASSTYPE_TEMPLATE_INFO (scope)
20215 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20216 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20217 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20218 ++num_templates;
20221 /* Otherwise, the identifier is optional. */
20222 else
20224 /* We don't know whether what comes next is a template-id,
20225 an identifier, or nothing at all. */
20226 cp_parser_parse_tentatively (parser);
20227 /* Check for a template-id. */
20228 type_start_token = cp_lexer_peek_token (parser->lexer);
20229 id = cp_parser_template_id (parser,
20230 /*template_keyword_p=*/false,
20231 /*check_dependency_p=*/true,
20232 class_key,
20233 /*is_declaration=*/true);
20234 /* If that didn't work, it could still be an identifier. */
20235 if (!cp_parser_parse_definitely (parser))
20237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20239 type_start_token = cp_lexer_peek_token (parser->lexer);
20240 id = cp_parser_identifier (parser);
20242 else
20243 id = NULL_TREE;
20245 else
20247 template_id_p = true;
20248 ++num_templates;
20252 pop_deferring_access_checks ();
20254 if (id)
20256 cp_parser_check_for_invalid_template_id (parser, id,
20257 class_key,
20258 type_start_token->location);
20260 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20262 /* If it's not a `:' or a `{' then we can't really be looking at a
20263 class-head, since a class-head only appears as part of a
20264 class-specifier. We have to detect this situation before calling
20265 xref_tag, since that has irreversible side-effects. */
20266 if (!cp_parser_next_token_starts_class_definition_p (parser))
20268 cp_parser_error (parser, "expected %<{%> or %<:%>");
20269 type = error_mark_node;
20270 goto out;
20273 /* At this point, we're going ahead with the class-specifier, even
20274 if some other problem occurs. */
20275 cp_parser_commit_to_tentative_parse (parser);
20276 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20278 cp_parser_error (parser,
20279 "cannot specify %<override%> for a class");
20280 type = error_mark_node;
20281 goto out;
20283 /* Issue the error about the overly-qualified name now. */
20284 if (qualified_p)
20286 cp_parser_error (parser,
20287 "global qualification of class name is invalid");
20288 type = error_mark_node;
20289 goto out;
20291 else if (invalid_nested_name_p)
20293 cp_parser_error (parser,
20294 "qualified name does not name a class");
20295 type = error_mark_node;
20296 goto out;
20298 else if (nested_name_specifier)
20300 tree scope;
20302 /* Reject typedef-names in class heads. */
20303 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20305 error_at (type_start_token->location,
20306 "invalid class name in declaration of %qD",
20307 type);
20308 type = NULL_TREE;
20309 goto done;
20312 /* Figure out in what scope the declaration is being placed. */
20313 scope = current_scope ();
20314 /* If that scope does not contain the scope in which the
20315 class was originally declared, the program is invalid. */
20316 if (scope && !is_ancestor (scope, nested_name_specifier))
20318 if (at_namespace_scope_p ())
20319 error_at (type_start_token->location,
20320 "declaration of %qD in namespace %qD which does not "
20321 "enclose %qD",
20322 type, scope, nested_name_specifier);
20323 else
20324 error_at (type_start_token->location,
20325 "declaration of %qD in %qD which does not enclose %qD",
20326 type, scope, nested_name_specifier);
20327 type = NULL_TREE;
20328 goto done;
20330 /* [dcl.meaning]
20332 A declarator-id shall not be qualified except for the
20333 definition of a ... nested class outside of its class
20334 ... [or] the definition or explicit instantiation of a
20335 class member of a namespace outside of its namespace. */
20336 if (scope == nested_name_specifier)
20338 permerror (nested_name_specifier_token_start->location,
20339 "extra qualification not allowed");
20340 nested_name_specifier = NULL_TREE;
20341 num_templates = 0;
20344 /* An explicit-specialization must be preceded by "template <>". If
20345 it is not, try to recover gracefully. */
20346 if (at_namespace_scope_p ()
20347 && parser->num_template_parameter_lists == 0
20348 && template_id_p)
20350 error_at (type_start_token->location,
20351 "an explicit specialization must be preceded by %<template <>%>");
20352 invalid_explicit_specialization_p = true;
20353 /* Take the same action that would have been taken by
20354 cp_parser_explicit_specialization. */
20355 ++parser->num_template_parameter_lists;
20356 begin_specialization ();
20358 /* There must be no "return" statements between this point and the
20359 end of this function; set "type "to the correct return value and
20360 use "goto done;" to return. */
20361 /* Make sure that the right number of template parameters were
20362 present. */
20363 if (!cp_parser_check_template_parameters (parser, num_templates,
20364 type_start_token->location,
20365 /*declarator=*/NULL))
20367 /* If something went wrong, there is no point in even trying to
20368 process the class-definition. */
20369 type = NULL_TREE;
20370 goto done;
20373 /* Look up the type. */
20374 if (template_id_p)
20376 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20377 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20378 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20380 error_at (type_start_token->location,
20381 "function template %qD redeclared as a class template", id);
20382 type = error_mark_node;
20384 else
20386 type = TREE_TYPE (id);
20387 type = maybe_process_partial_specialization (type);
20389 if (nested_name_specifier)
20390 pushed_scope = push_scope (nested_name_specifier);
20392 else if (nested_name_specifier)
20394 tree class_type;
20396 /* Given:
20398 template <typename T> struct S { struct T };
20399 template <typename T> struct S<T>::T { };
20401 we will get a TYPENAME_TYPE when processing the definition of
20402 `S::T'. We need to resolve it to the actual type before we
20403 try to define it. */
20404 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20406 class_type = resolve_typename_type (TREE_TYPE (type),
20407 /*only_current_p=*/false);
20408 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20409 type = TYPE_NAME (class_type);
20410 else
20412 cp_parser_error (parser, "could not resolve typename type");
20413 type = error_mark_node;
20417 if (maybe_process_partial_specialization (TREE_TYPE (type))
20418 == error_mark_node)
20420 type = NULL_TREE;
20421 goto done;
20424 class_type = current_class_type;
20425 /* Enter the scope indicated by the nested-name-specifier. */
20426 pushed_scope = push_scope (nested_name_specifier);
20427 /* Get the canonical version of this type. */
20428 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20429 /* Call push_template_decl if it seems like we should be defining a
20430 template either from the template headers or the type we're
20431 defining, so that we diagnose both extra and missing headers. */
20432 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20433 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20434 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20436 type = push_template_decl (type);
20437 if (type == error_mark_node)
20439 type = NULL_TREE;
20440 goto done;
20444 type = TREE_TYPE (type);
20445 *nested_name_specifier_p = true;
20447 else /* The name is not a nested name. */
20449 /* If the class was unnamed, create a dummy name. */
20450 if (!id)
20451 id = make_anon_name ();
20452 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20453 parser->num_template_parameter_lists);
20456 /* Indicate whether this class was declared as a `class' or as a
20457 `struct'. */
20458 if (TREE_CODE (type) == RECORD_TYPE)
20459 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20460 cp_parser_check_class_key (class_key, type);
20462 /* If this type was already complete, and we see another definition,
20463 that's an error. */
20464 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20466 error_at (type_start_token->location, "redefinition of %q#T",
20467 type);
20468 error_at (type_start_token->location, "previous definition of %q+#T",
20469 type);
20470 type = NULL_TREE;
20471 goto done;
20473 else if (type == error_mark_node)
20474 type = NULL_TREE;
20476 if (type)
20478 /* Apply attributes now, before any use of the class as a template
20479 argument in its base list. */
20480 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20481 fixup_attribute_variants (type);
20484 /* We will have entered the scope containing the class; the names of
20485 base classes should be looked up in that context. For example:
20487 struct A { struct B {}; struct C; };
20488 struct A::C : B {};
20490 is valid. */
20492 /* Get the list of base-classes, if there is one. */
20493 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20495 /* PR59482: enter the class scope so that base-specifiers are looked
20496 up correctly. */
20497 if (type)
20498 pushclass (type);
20499 bases = cp_parser_base_clause (parser);
20500 /* PR59482: get out of the previously pushed class scope so that the
20501 subsequent pops pop the right thing. */
20502 if (type)
20503 popclass ();
20505 else
20506 bases = NULL_TREE;
20508 /* If we're really defining a class, process the base classes.
20509 If they're invalid, fail. */
20510 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20511 && !xref_basetypes (type, bases))
20512 type = NULL_TREE;
20514 done:
20515 /* Leave the scope given by the nested-name-specifier. We will
20516 enter the class scope itself while processing the members. */
20517 if (pushed_scope)
20518 pop_scope (pushed_scope);
20520 if (invalid_explicit_specialization_p)
20522 end_specialization ();
20523 --parser->num_template_parameter_lists;
20526 if (type)
20527 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20528 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20529 CLASSTYPE_FINAL (type) = 1;
20530 out:
20531 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20532 return type;
20535 /* Parse a class-key.
20537 class-key:
20538 class
20539 struct
20540 union
20542 Returns the kind of class-key specified, or none_type to indicate
20543 error. */
20545 static enum tag_types
20546 cp_parser_class_key (cp_parser* parser)
20548 cp_token *token;
20549 enum tag_types tag_type;
20551 /* Look for the class-key. */
20552 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20553 if (!token)
20554 return none_type;
20556 /* Check to see if the TOKEN is a class-key. */
20557 tag_type = cp_parser_token_is_class_key (token);
20558 if (!tag_type)
20559 cp_parser_error (parser, "expected class-key");
20560 return tag_type;
20563 /* Parse a type-parameter-key.
20565 type-parameter-key:
20566 class
20567 typename
20570 static void
20571 cp_parser_type_parameter_key (cp_parser* parser)
20573 /* Look for the type-parameter-key. */
20574 enum tag_types tag_type = none_type;
20575 cp_token *token = cp_lexer_peek_token (parser->lexer);
20576 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20578 cp_lexer_consume_token (parser->lexer);
20579 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20580 /* typename is not allowed in a template template parameter
20581 by the standard until C++1Z. */
20582 pedwarn (token->location, OPT_Wpedantic,
20583 "ISO C++ forbids typename key in template template parameter;"
20584 " use -std=c++1z or -std=gnu++1z");
20586 else
20587 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20589 return;
20592 /* Parse an (optional) member-specification.
20594 member-specification:
20595 member-declaration member-specification [opt]
20596 access-specifier : member-specification [opt] */
20598 static void
20599 cp_parser_member_specification_opt (cp_parser* parser)
20601 while (true)
20603 cp_token *token;
20604 enum rid keyword;
20606 /* Peek at the next token. */
20607 token = cp_lexer_peek_token (parser->lexer);
20608 /* If it's a `}', or EOF then we've seen all the members. */
20609 if (token->type == CPP_CLOSE_BRACE
20610 || token->type == CPP_EOF
20611 || token->type == CPP_PRAGMA_EOL)
20612 break;
20614 /* See if this token is a keyword. */
20615 keyword = token->keyword;
20616 switch (keyword)
20618 case RID_PUBLIC:
20619 case RID_PROTECTED:
20620 case RID_PRIVATE:
20621 /* Consume the access-specifier. */
20622 cp_lexer_consume_token (parser->lexer);
20623 /* Remember which access-specifier is active. */
20624 current_access_specifier = token->u.value;
20625 /* Look for the `:'. */
20626 cp_parser_require (parser, CPP_COLON, RT_COLON);
20627 break;
20629 default:
20630 /* Accept #pragmas at class scope. */
20631 if (token->type == CPP_PRAGMA)
20633 cp_parser_pragma (parser, pragma_member);
20634 break;
20637 /* Otherwise, the next construction must be a
20638 member-declaration. */
20639 cp_parser_member_declaration (parser);
20644 /* Parse a member-declaration.
20646 member-declaration:
20647 decl-specifier-seq [opt] member-declarator-list [opt] ;
20648 function-definition ; [opt]
20649 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20650 using-declaration
20651 template-declaration
20652 alias-declaration
20654 member-declarator-list:
20655 member-declarator
20656 member-declarator-list , member-declarator
20658 member-declarator:
20659 declarator pure-specifier [opt]
20660 declarator constant-initializer [opt]
20661 identifier [opt] : constant-expression
20663 GNU Extensions:
20665 member-declaration:
20666 __extension__ member-declaration
20668 member-declarator:
20669 declarator attributes [opt] pure-specifier [opt]
20670 declarator attributes [opt] constant-initializer [opt]
20671 identifier [opt] attributes [opt] : constant-expression
20673 C++0x Extensions:
20675 member-declaration:
20676 static_assert-declaration */
20678 static void
20679 cp_parser_member_declaration (cp_parser* parser)
20681 cp_decl_specifier_seq decl_specifiers;
20682 tree prefix_attributes;
20683 tree decl;
20684 int declares_class_or_enum;
20685 bool friend_p;
20686 cp_token *token = NULL;
20687 cp_token *decl_spec_token_start = NULL;
20688 cp_token *initializer_token_start = NULL;
20689 int saved_pedantic;
20690 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20692 /* Check for the `__extension__' keyword. */
20693 if (cp_parser_extension_opt (parser, &saved_pedantic))
20695 /* Recurse. */
20696 cp_parser_member_declaration (parser);
20697 /* Restore the old value of the PEDANTIC flag. */
20698 pedantic = saved_pedantic;
20700 return;
20703 /* Check for a template-declaration. */
20704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20706 /* An explicit specialization here is an error condition, and we
20707 expect the specialization handler to detect and report this. */
20708 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20709 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20710 cp_parser_explicit_specialization (parser);
20711 else
20712 cp_parser_template_declaration (parser, /*member_p=*/true);
20714 return;
20717 /* Check for a using-declaration. */
20718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20720 if (cxx_dialect < cxx11)
20722 /* Parse the using-declaration. */
20723 cp_parser_using_declaration (parser,
20724 /*access_declaration_p=*/false);
20725 return;
20727 else
20729 tree decl;
20730 bool alias_decl_expected;
20731 cp_parser_parse_tentatively (parser);
20732 decl = cp_parser_alias_declaration (parser);
20733 /* Note that if we actually see the '=' token after the
20734 identifier, cp_parser_alias_declaration commits the
20735 tentative parse. In that case, we really expects an
20736 alias-declaration. Otherwise, we expect a using
20737 declaration. */
20738 alias_decl_expected =
20739 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20740 cp_parser_parse_definitely (parser);
20742 if (alias_decl_expected)
20743 finish_member_declaration (decl);
20744 else
20745 cp_parser_using_declaration (parser,
20746 /*access_declaration_p=*/false);
20747 return;
20751 /* Check for @defs. */
20752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20754 tree ivar, member;
20755 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20756 ivar = ivar_chains;
20757 while (ivar)
20759 member = ivar;
20760 ivar = TREE_CHAIN (member);
20761 TREE_CHAIN (member) = NULL_TREE;
20762 finish_member_declaration (member);
20764 return;
20767 /* If the next token is `static_assert' we have a static assertion. */
20768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20770 cp_parser_static_assert (parser, /*member_p=*/true);
20771 return;
20774 parser->colon_corrects_to_scope_p = false;
20776 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20777 goto out;
20779 /* Parse the decl-specifier-seq. */
20780 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20781 cp_parser_decl_specifier_seq (parser,
20782 CP_PARSER_FLAGS_OPTIONAL,
20783 &decl_specifiers,
20784 &declares_class_or_enum);
20785 /* Check for an invalid type-name. */
20786 if (!decl_specifiers.any_type_specifiers_p
20787 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20788 goto out;
20789 /* If there is no declarator, then the decl-specifier-seq should
20790 specify a type. */
20791 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20793 /* If there was no decl-specifier-seq, and the next token is a
20794 `;', then we have something like:
20796 struct S { ; };
20798 [class.mem]
20800 Each member-declaration shall declare at least one member
20801 name of the class. */
20802 if (!decl_specifiers.any_specifiers_p)
20804 cp_token *token = cp_lexer_peek_token (parser->lexer);
20805 if (!in_system_header_at (token->location))
20806 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20808 else
20810 tree type;
20812 /* See if this declaration is a friend. */
20813 friend_p = cp_parser_friend_p (&decl_specifiers);
20814 /* If there were decl-specifiers, check to see if there was
20815 a class-declaration. */
20816 type = check_tag_decl (&decl_specifiers,
20817 /*explicit_type_instantiation_p=*/false);
20818 /* Nested classes have already been added to the class, but
20819 a `friend' needs to be explicitly registered. */
20820 if (friend_p)
20822 /* If the `friend' keyword was present, the friend must
20823 be introduced with a class-key. */
20824 if (!declares_class_or_enum && cxx_dialect < cxx11)
20825 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20826 "in C++03 a class-key must be used "
20827 "when declaring a friend");
20828 /* In this case:
20830 template <typename T> struct A {
20831 friend struct A<T>::B;
20834 A<T>::B will be represented by a TYPENAME_TYPE, and
20835 therefore not recognized by check_tag_decl. */
20836 if (!type)
20838 type = decl_specifiers.type;
20839 if (type && TREE_CODE (type) == TYPE_DECL)
20840 type = TREE_TYPE (type);
20842 if (!type || !TYPE_P (type))
20843 error_at (decl_spec_token_start->location,
20844 "friend declaration does not name a class or "
20845 "function");
20846 else
20847 make_friend_class (current_class_type, type,
20848 /*complain=*/true);
20850 /* If there is no TYPE, an error message will already have
20851 been issued. */
20852 else if (!type || type == error_mark_node)
20854 /* An anonymous aggregate has to be handled specially; such
20855 a declaration really declares a data member (with a
20856 particular type), as opposed to a nested class. */
20857 else if (ANON_AGGR_TYPE_P (type))
20859 /* C++11 9.5/6. */
20860 if (decl_specifiers.storage_class != sc_none)
20861 error_at (decl_spec_token_start->location,
20862 "a storage class on an anonymous aggregate "
20863 "in class scope is not allowed");
20865 /* Remove constructors and such from TYPE, now that we
20866 know it is an anonymous aggregate. */
20867 fixup_anonymous_aggr (type);
20868 /* And make the corresponding data member. */
20869 decl = build_decl (decl_spec_token_start->location,
20870 FIELD_DECL, NULL_TREE, type);
20871 /* Add it to the class. */
20872 finish_member_declaration (decl);
20874 else
20875 cp_parser_check_access_in_redeclaration
20876 (TYPE_NAME (type),
20877 decl_spec_token_start->location);
20880 else
20882 bool assume_semicolon = false;
20884 /* Clear attributes from the decl_specifiers but keep them
20885 around as prefix attributes that apply them to the entity
20886 being declared. */
20887 prefix_attributes = decl_specifiers.attributes;
20888 decl_specifiers.attributes = NULL_TREE;
20890 /* See if these declarations will be friends. */
20891 friend_p = cp_parser_friend_p (&decl_specifiers);
20893 /* Keep going until we hit the `;' at the end of the
20894 declaration. */
20895 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20897 tree attributes = NULL_TREE;
20898 tree first_attribute;
20900 /* Peek at the next token. */
20901 token = cp_lexer_peek_token (parser->lexer);
20903 /* Check for a bitfield declaration. */
20904 if (token->type == CPP_COLON
20905 || (token->type == CPP_NAME
20906 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20907 == CPP_COLON))
20909 tree identifier;
20910 tree width;
20912 /* Get the name of the bitfield. Note that we cannot just
20913 check TOKEN here because it may have been invalidated by
20914 the call to cp_lexer_peek_nth_token above. */
20915 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20916 identifier = cp_parser_identifier (parser);
20917 else
20918 identifier = NULL_TREE;
20920 /* Consume the `:' token. */
20921 cp_lexer_consume_token (parser->lexer);
20922 /* Get the width of the bitfield. */
20923 width
20924 = cp_parser_constant_expression (parser);
20926 /* Look for attributes that apply to the bitfield. */
20927 attributes = cp_parser_attributes_opt (parser);
20928 /* Remember which attributes are prefix attributes and
20929 which are not. */
20930 first_attribute = attributes;
20931 /* Combine the attributes. */
20932 attributes = chainon (prefix_attributes, attributes);
20934 /* Create the bitfield declaration. */
20935 decl = grokbitfield (identifier
20936 ? make_id_declarator (NULL_TREE,
20937 identifier,
20938 sfk_none)
20939 : NULL,
20940 &decl_specifiers,
20941 width,
20942 attributes);
20944 else
20946 cp_declarator *declarator;
20947 tree initializer;
20948 tree asm_specification;
20949 int ctor_dtor_or_conv_p;
20951 /* Parse the declarator. */
20952 declarator
20953 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20954 &ctor_dtor_or_conv_p,
20955 /*parenthesized_p=*/NULL,
20956 /*member_p=*/true,
20957 friend_p);
20959 /* If something went wrong parsing the declarator, make sure
20960 that we at least consume some tokens. */
20961 if (declarator == cp_error_declarator)
20963 /* Skip to the end of the statement. */
20964 cp_parser_skip_to_end_of_statement (parser);
20965 /* If the next token is not a semicolon, that is
20966 probably because we just skipped over the body of
20967 a function. So, we consume a semicolon if
20968 present, but do not issue an error message if it
20969 is not present. */
20970 if (cp_lexer_next_token_is (parser->lexer,
20971 CPP_SEMICOLON))
20972 cp_lexer_consume_token (parser->lexer);
20973 goto out;
20976 if (declares_class_or_enum & 2)
20977 cp_parser_check_for_definition_in_return_type
20978 (declarator, decl_specifiers.type,
20979 decl_specifiers.locations[ds_type_spec]);
20981 /* Look for an asm-specification. */
20982 asm_specification = cp_parser_asm_specification_opt (parser);
20983 /* Look for attributes that apply to the declaration. */
20984 attributes = cp_parser_attributes_opt (parser);
20985 /* Remember which attributes are prefix attributes and
20986 which are not. */
20987 first_attribute = attributes;
20988 /* Combine the attributes. */
20989 attributes = chainon (prefix_attributes, attributes);
20991 /* If it's an `=', then we have a constant-initializer or a
20992 pure-specifier. It is not correct to parse the
20993 initializer before registering the member declaration
20994 since the member declaration should be in scope while
20995 its initializer is processed. However, the rest of the
20996 front end does not yet provide an interface that allows
20997 us to handle this correctly. */
20998 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21000 /* In [class.mem]:
21002 A pure-specifier shall be used only in the declaration of
21003 a virtual function.
21005 A member-declarator can contain a constant-initializer
21006 only if it declares a static member of integral or
21007 enumeration type.
21009 Therefore, if the DECLARATOR is for a function, we look
21010 for a pure-specifier; otherwise, we look for a
21011 constant-initializer. When we call `grokfield', it will
21012 perform more stringent semantics checks. */
21013 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21014 if (function_declarator_p (declarator)
21015 || (decl_specifiers.type
21016 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21017 && declarator->kind == cdk_id
21018 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21019 == FUNCTION_TYPE)))
21020 initializer = cp_parser_pure_specifier (parser);
21021 else if (decl_specifiers.storage_class != sc_static)
21022 initializer = cp_parser_save_nsdmi (parser);
21023 else if (cxx_dialect >= cxx11)
21025 bool nonconst;
21026 /* Don't require a constant rvalue in C++11, since we
21027 might want a reference constant. We'll enforce
21028 constancy later. */
21029 cp_lexer_consume_token (parser->lexer);
21030 /* Parse the initializer. */
21031 initializer = cp_parser_initializer_clause (parser,
21032 &nonconst);
21034 else
21035 /* Parse the initializer. */
21036 initializer = cp_parser_constant_initializer (parser);
21038 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21039 && !function_declarator_p (declarator))
21041 bool x;
21042 if (decl_specifiers.storage_class != sc_static)
21043 initializer = cp_parser_save_nsdmi (parser);
21044 else
21045 initializer = cp_parser_initializer (parser, &x, &x);
21047 /* Otherwise, there is no initializer. */
21048 else
21049 initializer = NULL_TREE;
21051 /* See if we are probably looking at a function
21052 definition. We are certainly not looking at a
21053 member-declarator. Calling `grokfield' has
21054 side-effects, so we must not do it unless we are sure
21055 that we are looking at a member-declarator. */
21056 if (cp_parser_token_starts_function_definition_p
21057 (cp_lexer_peek_token (parser->lexer)))
21059 /* The grammar does not allow a pure-specifier to be
21060 used when a member function is defined. (It is
21061 possible that this fact is an oversight in the
21062 standard, since a pure function may be defined
21063 outside of the class-specifier. */
21064 if (initializer && initializer_token_start)
21065 error_at (initializer_token_start->location,
21066 "pure-specifier on function-definition");
21067 decl = cp_parser_save_member_function_body (parser,
21068 &decl_specifiers,
21069 declarator,
21070 attributes);
21071 if (parser->fully_implicit_function_template_p)
21072 decl = finish_fully_implicit_template (parser, decl);
21073 /* If the member was not a friend, declare it here. */
21074 if (!friend_p)
21075 finish_member_declaration (decl);
21076 /* Peek at the next token. */
21077 token = cp_lexer_peek_token (parser->lexer);
21078 /* If the next token is a semicolon, consume it. */
21079 if (token->type == CPP_SEMICOLON)
21080 cp_lexer_consume_token (parser->lexer);
21081 goto out;
21083 else
21084 if (declarator->kind == cdk_function)
21085 declarator->id_loc = token->location;
21086 /* Create the declaration. */
21087 decl = grokfield (declarator, &decl_specifiers,
21088 initializer, /*init_const_expr_p=*/true,
21089 asm_specification, attributes);
21090 if (parser->fully_implicit_function_template_p)
21092 if (friend_p)
21093 finish_fully_implicit_template (parser, 0);
21094 else
21095 decl = finish_fully_implicit_template (parser, decl);
21099 cp_finalize_omp_declare_simd (parser, decl);
21101 /* Reset PREFIX_ATTRIBUTES. */
21102 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21103 attributes = TREE_CHAIN (attributes);
21104 if (attributes)
21105 TREE_CHAIN (attributes) = NULL_TREE;
21107 /* If there is any qualification still in effect, clear it
21108 now; we will be starting fresh with the next declarator. */
21109 parser->scope = NULL_TREE;
21110 parser->qualifying_scope = NULL_TREE;
21111 parser->object_scope = NULL_TREE;
21112 /* If it's a `,', then there are more declarators. */
21113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21115 cp_lexer_consume_token (parser->lexer);
21116 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21118 cp_token *token = cp_lexer_previous_token (parser->lexer);
21119 error_at (token->location,
21120 "stray %<,%> at end of member declaration");
21123 /* If the next token isn't a `;', then we have a parse error. */
21124 else if (cp_lexer_next_token_is_not (parser->lexer,
21125 CPP_SEMICOLON))
21127 /* The next token might be a ways away from where the
21128 actual semicolon is missing. Find the previous token
21129 and use that for our error position. */
21130 cp_token *token = cp_lexer_previous_token (parser->lexer);
21131 error_at (token->location,
21132 "expected %<;%> at end of member declaration");
21134 /* Assume that the user meant to provide a semicolon. If
21135 we were to cp_parser_skip_to_end_of_statement, we might
21136 skip to a semicolon inside a member function definition
21137 and issue nonsensical error messages. */
21138 assume_semicolon = true;
21141 if (decl)
21143 /* Add DECL to the list of members. */
21144 if (!friend_p
21145 /* Explicitly include, eg, NSDMIs, for better error
21146 recovery (c++/58650). */
21147 || !DECL_DECLARES_FUNCTION_P (decl))
21148 finish_member_declaration (decl);
21150 if (TREE_CODE (decl) == FUNCTION_DECL)
21151 cp_parser_save_default_args (parser, decl);
21152 else if (TREE_CODE (decl) == FIELD_DECL
21153 && !DECL_C_BIT_FIELD (decl)
21154 && DECL_INITIAL (decl))
21155 /* Add DECL to the queue of NSDMI to be parsed later. */
21156 vec_safe_push (unparsed_nsdmis, decl);
21159 if (assume_semicolon)
21160 goto out;
21164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21165 out:
21166 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21169 /* Parse a pure-specifier.
21171 pure-specifier:
21174 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21175 Otherwise, ERROR_MARK_NODE is returned. */
21177 static tree
21178 cp_parser_pure_specifier (cp_parser* parser)
21180 cp_token *token;
21182 /* Look for the `=' token. */
21183 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21184 return error_mark_node;
21185 /* Look for the `0' token. */
21186 token = cp_lexer_peek_token (parser->lexer);
21188 if (token->type == CPP_EOF
21189 || token->type == CPP_PRAGMA_EOL)
21190 return error_mark_node;
21192 cp_lexer_consume_token (parser->lexer);
21194 /* Accept = default or = delete in c++0x mode. */
21195 if (token->keyword == RID_DEFAULT
21196 || token->keyword == RID_DELETE)
21198 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21199 return token->u.value;
21202 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21203 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21205 cp_parser_error (parser,
21206 "invalid pure specifier (only %<= 0%> is allowed)");
21207 cp_parser_skip_to_end_of_statement (parser);
21208 return error_mark_node;
21210 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21212 error_at (token->location, "templates may not be %<virtual%>");
21213 return error_mark_node;
21216 return integer_zero_node;
21219 /* Parse a constant-initializer.
21221 constant-initializer:
21222 = constant-expression
21224 Returns a representation of the constant-expression. */
21226 static tree
21227 cp_parser_constant_initializer (cp_parser* parser)
21229 /* Look for the `=' token. */
21230 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21231 return error_mark_node;
21233 /* It is invalid to write:
21235 struct S { static const int i = { 7 }; };
21238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21240 cp_parser_error (parser,
21241 "a brace-enclosed initializer is not allowed here");
21242 /* Consume the opening brace. */
21243 cp_lexer_consume_token (parser->lexer);
21244 /* Skip the initializer. */
21245 cp_parser_skip_to_closing_brace (parser);
21246 /* Look for the trailing `}'. */
21247 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21249 return error_mark_node;
21252 return cp_parser_constant_expression (parser);
21255 /* Derived classes [gram.class.derived] */
21257 /* Parse a base-clause.
21259 base-clause:
21260 : base-specifier-list
21262 base-specifier-list:
21263 base-specifier ... [opt]
21264 base-specifier-list , base-specifier ... [opt]
21266 Returns a TREE_LIST representing the base-classes, in the order in
21267 which they were declared. The representation of each node is as
21268 described by cp_parser_base_specifier.
21270 In the case that no bases are specified, this function will return
21271 NULL_TREE, not ERROR_MARK_NODE. */
21273 static tree
21274 cp_parser_base_clause (cp_parser* parser)
21276 tree bases = NULL_TREE;
21278 /* Look for the `:' that begins the list. */
21279 cp_parser_require (parser, CPP_COLON, RT_COLON);
21281 /* Scan the base-specifier-list. */
21282 while (true)
21284 cp_token *token;
21285 tree base;
21286 bool pack_expansion_p = false;
21288 /* Look for the base-specifier. */
21289 base = cp_parser_base_specifier (parser);
21290 /* Look for the (optional) ellipsis. */
21291 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21293 /* Consume the `...'. */
21294 cp_lexer_consume_token (parser->lexer);
21296 pack_expansion_p = true;
21299 /* Add BASE to the front of the list. */
21300 if (base && base != error_mark_node)
21302 if (pack_expansion_p)
21303 /* Make this a pack expansion type. */
21304 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21306 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21308 TREE_CHAIN (base) = bases;
21309 bases = base;
21312 /* Peek at the next token. */
21313 token = cp_lexer_peek_token (parser->lexer);
21314 /* If it's not a comma, then the list is complete. */
21315 if (token->type != CPP_COMMA)
21316 break;
21317 /* Consume the `,'. */
21318 cp_lexer_consume_token (parser->lexer);
21321 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21322 base class had a qualified name. However, the next name that
21323 appears is certainly not qualified. */
21324 parser->scope = NULL_TREE;
21325 parser->qualifying_scope = NULL_TREE;
21326 parser->object_scope = NULL_TREE;
21328 return nreverse (bases);
21331 /* Parse a base-specifier.
21333 base-specifier:
21334 :: [opt] nested-name-specifier [opt] class-name
21335 virtual access-specifier [opt] :: [opt] nested-name-specifier
21336 [opt] class-name
21337 access-specifier virtual [opt] :: [opt] nested-name-specifier
21338 [opt] class-name
21340 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21341 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21342 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21343 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21345 static tree
21346 cp_parser_base_specifier (cp_parser* parser)
21348 cp_token *token;
21349 bool done = false;
21350 bool virtual_p = false;
21351 bool duplicate_virtual_error_issued_p = false;
21352 bool duplicate_access_error_issued_p = false;
21353 bool class_scope_p, template_p;
21354 tree access = access_default_node;
21355 tree type;
21357 /* Process the optional `virtual' and `access-specifier'. */
21358 while (!done)
21360 /* Peek at the next token. */
21361 token = cp_lexer_peek_token (parser->lexer);
21362 /* Process `virtual'. */
21363 switch (token->keyword)
21365 case RID_VIRTUAL:
21366 /* If `virtual' appears more than once, issue an error. */
21367 if (virtual_p && !duplicate_virtual_error_issued_p)
21369 cp_parser_error (parser,
21370 "%<virtual%> specified more than once in base-specified");
21371 duplicate_virtual_error_issued_p = true;
21374 virtual_p = true;
21376 /* Consume the `virtual' token. */
21377 cp_lexer_consume_token (parser->lexer);
21379 break;
21381 case RID_PUBLIC:
21382 case RID_PROTECTED:
21383 case RID_PRIVATE:
21384 /* If more than one access specifier appears, issue an
21385 error. */
21386 if (access != access_default_node
21387 && !duplicate_access_error_issued_p)
21389 cp_parser_error (parser,
21390 "more than one access specifier in base-specified");
21391 duplicate_access_error_issued_p = true;
21394 access = ridpointers[(int) token->keyword];
21396 /* Consume the access-specifier. */
21397 cp_lexer_consume_token (parser->lexer);
21399 break;
21401 default:
21402 done = true;
21403 break;
21406 /* It is not uncommon to see programs mechanically, erroneously, use
21407 the 'typename' keyword to denote (dependent) qualified types
21408 as base classes. */
21409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21411 token = cp_lexer_peek_token (parser->lexer);
21412 if (!processing_template_decl)
21413 error_at (token->location,
21414 "keyword %<typename%> not allowed outside of templates");
21415 else
21416 error_at (token->location,
21417 "keyword %<typename%> not allowed in this context "
21418 "(the base class is implicitly a type)");
21419 cp_lexer_consume_token (parser->lexer);
21422 /* Look for the optional `::' operator. */
21423 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21424 /* Look for the nested-name-specifier. The simplest way to
21425 implement:
21427 [temp.res]
21429 The keyword `typename' is not permitted in a base-specifier or
21430 mem-initializer; in these contexts a qualified name that
21431 depends on a template-parameter is implicitly assumed to be a
21432 type name.
21434 is to pretend that we have seen the `typename' keyword at this
21435 point. */
21436 cp_parser_nested_name_specifier_opt (parser,
21437 /*typename_keyword_p=*/true,
21438 /*check_dependency_p=*/true,
21439 typename_type,
21440 /*is_declaration=*/true);
21441 /* If the base class is given by a qualified name, assume that names
21442 we see are type names or templates, as appropriate. */
21443 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21444 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21446 if (!parser->scope
21447 && cp_lexer_next_token_is_decltype (parser->lexer))
21448 /* DR 950 allows decltype as a base-specifier. */
21449 type = cp_parser_decltype (parser);
21450 else
21452 /* Otherwise, look for the class-name. */
21453 type = cp_parser_class_name (parser,
21454 class_scope_p,
21455 template_p,
21456 typename_type,
21457 /*check_dependency_p=*/true,
21458 /*class_head_p=*/false,
21459 /*is_declaration=*/true);
21460 type = TREE_TYPE (type);
21463 if (type == error_mark_node)
21464 return error_mark_node;
21466 return finish_base_specifier (type, access, virtual_p);
21469 /* Exception handling [gram.exception] */
21471 /* Parse an (optional) noexcept-specification.
21473 noexcept-specification:
21474 noexcept ( constant-expression ) [opt]
21476 If no noexcept-specification is present, returns NULL_TREE.
21477 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21478 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21479 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21480 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21481 in which case a boolean condition is returned instead. */
21483 static tree
21484 cp_parser_noexcept_specification_opt (cp_parser* parser,
21485 bool require_constexpr,
21486 bool* consumed_expr,
21487 bool return_cond)
21489 cp_token *token;
21490 const char *saved_message;
21492 /* Peek at the next token. */
21493 token = cp_lexer_peek_token (parser->lexer);
21495 /* Is it a noexcept-specification? */
21496 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21498 tree expr;
21499 cp_lexer_consume_token (parser->lexer);
21501 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21503 cp_lexer_consume_token (parser->lexer);
21505 if (require_constexpr)
21507 /* Types may not be defined in an exception-specification. */
21508 saved_message = parser->type_definition_forbidden_message;
21509 parser->type_definition_forbidden_message
21510 = G_("types may not be defined in an exception-specification");
21512 expr = cp_parser_constant_expression (parser);
21514 /* Restore the saved message. */
21515 parser->type_definition_forbidden_message = saved_message;
21517 else
21519 expr = cp_parser_expression (parser);
21520 *consumed_expr = true;
21523 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21525 else
21527 expr = boolean_true_node;
21528 if (!require_constexpr)
21529 *consumed_expr = false;
21532 /* We cannot build a noexcept-spec right away because this will check
21533 that expr is a constexpr. */
21534 if (!return_cond)
21535 return build_noexcept_spec (expr, tf_warning_or_error);
21536 else
21537 return expr;
21539 else
21540 return NULL_TREE;
21543 /* Parse an (optional) exception-specification.
21545 exception-specification:
21546 throw ( type-id-list [opt] )
21548 Returns a TREE_LIST representing the exception-specification. The
21549 TREE_VALUE of each node is a type. */
21551 static tree
21552 cp_parser_exception_specification_opt (cp_parser* parser)
21554 cp_token *token;
21555 tree type_id_list;
21556 const char *saved_message;
21558 /* Peek at the next token. */
21559 token = cp_lexer_peek_token (parser->lexer);
21561 /* Is it a noexcept-specification? */
21562 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21563 false);
21564 if (type_id_list != NULL_TREE)
21565 return type_id_list;
21567 /* If it's not `throw', then there's no exception-specification. */
21568 if (!cp_parser_is_keyword (token, RID_THROW))
21569 return NULL_TREE;
21571 #if 0
21572 /* Enable this once a lot of code has transitioned to noexcept? */
21573 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21574 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21575 "deprecated in C++0x; use %<noexcept%> instead");
21576 #endif
21578 /* Consume the `throw'. */
21579 cp_lexer_consume_token (parser->lexer);
21581 /* Look for the `('. */
21582 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21584 /* Peek at the next token. */
21585 token = cp_lexer_peek_token (parser->lexer);
21586 /* If it's not a `)', then there is a type-id-list. */
21587 if (token->type != CPP_CLOSE_PAREN)
21589 /* Types may not be defined in an exception-specification. */
21590 saved_message = parser->type_definition_forbidden_message;
21591 parser->type_definition_forbidden_message
21592 = G_("types may not be defined in an exception-specification");
21593 /* Parse the type-id-list. */
21594 type_id_list = cp_parser_type_id_list (parser);
21595 /* Restore the saved message. */
21596 parser->type_definition_forbidden_message = saved_message;
21598 else
21599 type_id_list = empty_except_spec;
21601 /* Look for the `)'. */
21602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21604 return type_id_list;
21607 /* Parse an (optional) type-id-list.
21609 type-id-list:
21610 type-id ... [opt]
21611 type-id-list , type-id ... [opt]
21613 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21614 in the order that the types were presented. */
21616 static tree
21617 cp_parser_type_id_list (cp_parser* parser)
21619 tree types = NULL_TREE;
21621 while (true)
21623 cp_token *token;
21624 tree type;
21626 /* Get the next type-id. */
21627 type = cp_parser_type_id (parser);
21628 /* Parse the optional ellipsis. */
21629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21631 /* Consume the `...'. */
21632 cp_lexer_consume_token (parser->lexer);
21634 /* Turn the type into a pack expansion expression. */
21635 type = make_pack_expansion (type);
21637 /* Add it to the list. */
21638 types = add_exception_specifier (types, type, /*complain=*/1);
21639 /* Peek at the next token. */
21640 token = cp_lexer_peek_token (parser->lexer);
21641 /* If it is not a `,', we are done. */
21642 if (token->type != CPP_COMMA)
21643 break;
21644 /* Consume the `,'. */
21645 cp_lexer_consume_token (parser->lexer);
21648 return nreverse (types);
21651 /* Parse a try-block.
21653 try-block:
21654 try compound-statement handler-seq */
21656 static tree
21657 cp_parser_try_block (cp_parser* parser)
21659 tree try_block;
21661 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21662 if (parser->in_function_body
21663 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21664 error ("%<try%> in %<constexpr%> function");
21666 try_block = begin_try_block ();
21667 cp_parser_compound_statement (parser, NULL, true, false);
21668 finish_try_block (try_block);
21669 cp_parser_handler_seq (parser);
21670 finish_handler_sequence (try_block);
21672 return try_block;
21675 /* Parse a function-try-block.
21677 function-try-block:
21678 try ctor-initializer [opt] function-body handler-seq */
21680 static bool
21681 cp_parser_function_try_block (cp_parser* parser)
21683 tree compound_stmt;
21684 tree try_block;
21685 bool ctor_initializer_p;
21687 /* Look for the `try' keyword. */
21688 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21689 return false;
21690 /* Let the rest of the front end know where we are. */
21691 try_block = begin_function_try_block (&compound_stmt);
21692 /* Parse the function-body. */
21693 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21694 (parser, /*in_function_try_block=*/true);
21695 /* We're done with the `try' part. */
21696 finish_function_try_block (try_block);
21697 /* Parse the handlers. */
21698 cp_parser_handler_seq (parser);
21699 /* We're done with the handlers. */
21700 finish_function_handler_sequence (try_block, compound_stmt);
21702 return ctor_initializer_p;
21705 /* Parse a handler-seq.
21707 handler-seq:
21708 handler handler-seq [opt] */
21710 static void
21711 cp_parser_handler_seq (cp_parser* parser)
21713 while (true)
21715 cp_token *token;
21717 /* Parse the handler. */
21718 cp_parser_handler (parser);
21719 /* Peek at the next token. */
21720 token = cp_lexer_peek_token (parser->lexer);
21721 /* If it's not `catch' then there are no more handlers. */
21722 if (!cp_parser_is_keyword (token, RID_CATCH))
21723 break;
21727 /* Parse a handler.
21729 handler:
21730 catch ( exception-declaration ) compound-statement */
21732 static void
21733 cp_parser_handler (cp_parser* parser)
21735 tree handler;
21736 tree declaration;
21738 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21739 handler = begin_handler ();
21740 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21741 declaration = cp_parser_exception_declaration (parser);
21742 finish_handler_parms (declaration, handler);
21743 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21744 cp_parser_compound_statement (parser, NULL, false, false);
21745 finish_handler (handler);
21748 /* Parse an exception-declaration.
21750 exception-declaration:
21751 type-specifier-seq declarator
21752 type-specifier-seq abstract-declarator
21753 type-specifier-seq
21756 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21757 ellipsis variant is used. */
21759 static tree
21760 cp_parser_exception_declaration (cp_parser* parser)
21762 cp_decl_specifier_seq type_specifiers;
21763 cp_declarator *declarator;
21764 const char *saved_message;
21766 /* If it's an ellipsis, it's easy to handle. */
21767 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21769 /* Consume the `...' token. */
21770 cp_lexer_consume_token (parser->lexer);
21771 return NULL_TREE;
21774 /* Types may not be defined in exception-declarations. */
21775 saved_message = parser->type_definition_forbidden_message;
21776 parser->type_definition_forbidden_message
21777 = G_("types may not be defined in exception-declarations");
21779 /* Parse the type-specifier-seq. */
21780 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21781 /*is_trailing_return=*/false,
21782 &type_specifiers);
21783 /* If it's a `)', then there is no declarator. */
21784 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21785 declarator = NULL;
21786 else
21787 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21788 /*ctor_dtor_or_conv_p=*/NULL,
21789 /*parenthesized_p=*/NULL,
21790 /*member_p=*/false,
21791 /*friend_p=*/false);
21793 /* Restore the saved message. */
21794 parser->type_definition_forbidden_message = saved_message;
21796 if (!type_specifiers.any_specifiers_p)
21797 return error_mark_node;
21799 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21802 /* Parse a throw-expression.
21804 throw-expression:
21805 throw assignment-expression [opt]
21807 Returns a THROW_EXPR representing the throw-expression. */
21809 static tree
21810 cp_parser_throw_expression (cp_parser* parser)
21812 tree expression;
21813 cp_token* token;
21815 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21816 token = cp_lexer_peek_token (parser->lexer);
21817 /* Figure out whether or not there is an assignment-expression
21818 following the "throw" keyword. */
21819 if (token->type == CPP_COMMA
21820 || token->type == CPP_SEMICOLON
21821 || token->type == CPP_CLOSE_PAREN
21822 || token->type == CPP_CLOSE_SQUARE
21823 || token->type == CPP_CLOSE_BRACE
21824 || token->type == CPP_COLON)
21825 expression = NULL_TREE;
21826 else
21827 expression = cp_parser_assignment_expression (parser);
21829 return build_throw (expression);
21832 /* GNU Extensions */
21834 /* Parse an (optional) asm-specification.
21836 asm-specification:
21837 asm ( string-literal )
21839 If the asm-specification is present, returns a STRING_CST
21840 corresponding to the string-literal. Otherwise, returns
21841 NULL_TREE. */
21843 static tree
21844 cp_parser_asm_specification_opt (cp_parser* parser)
21846 cp_token *token;
21847 tree asm_specification;
21849 /* Peek at the next token. */
21850 token = cp_lexer_peek_token (parser->lexer);
21851 /* If the next token isn't the `asm' keyword, then there's no
21852 asm-specification. */
21853 if (!cp_parser_is_keyword (token, RID_ASM))
21854 return NULL_TREE;
21856 /* Consume the `asm' token. */
21857 cp_lexer_consume_token (parser->lexer);
21858 /* Look for the `('. */
21859 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21861 /* Look for the string-literal. */
21862 asm_specification = cp_parser_string_literal (parser, false, false);
21864 /* Look for the `)'. */
21865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21867 return asm_specification;
21870 /* Parse an asm-operand-list.
21872 asm-operand-list:
21873 asm-operand
21874 asm-operand-list , asm-operand
21876 asm-operand:
21877 string-literal ( expression )
21878 [ string-literal ] string-literal ( expression )
21880 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21881 each node is the expression. The TREE_PURPOSE is itself a
21882 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21883 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21884 is a STRING_CST for the string literal before the parenthesis. Returns
21885 ERROR_MARK_NODE if any of the operands are invalid. */
21887 static tree
21888 cp_parser_asm_operand_list (cp_parser* parser)
21890 tree asm_operands = NULL_TREE;
21891 bool invalid_operands = false;
21893 while (true)
21895 tree string_literal;
21896 tree expression;
21897 tree name;
21899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21901 /* Consume the `[' token. */
21902 cp_lexer_consume_token (parser->lexer);
21903 /* Read the operand name. */
21904 name = cp_parser_identifier (parser);
21905 if (name != error_mark_node)
21906 name = build_string (IDENTIFIER_LENGTH (name),
21907 IDENTIFIER_POINTER (name));
21908 /* Look for the closing `]'. */
21909 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21911 else
21912 name = NULL_TREE;
21913 /* Look for the string-literal. */
21914 string_literal = cp_parser_string_literal (parser, false, false);
21916 /* Look for the `('. */
21917 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21918 /* Parse the expression. */
21919 expression = cp_parser_expression (parser);
21920 /* Look for the `)'. */
21921 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21923 if (name == error_mark_node
21924 || string_literal == error_mark_node
21925 || expression == error_mark_node)
21926 invalid_operands = true;
21928 /* Add this operand to the list. */
21929 asm_operands = tree_cons (build_tree_list (name, string_literal),
21930 expression,
21931 asm_operands);
21932 /* If the next token is not a `,', there are no more
21933 operands. */
21934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21935 break;
21936 /* Consume the `,'. */
21937 cp_lexer_consume_token (parser->lexer);
21940 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21943 /* Parse an asm-clobber-list.
21945 asm-clobber-list:
21946 string-literal
21947 asm-clobber-list , string-literal
21949 Returns a TREE_LIST, indicating the clobbers in the order that they
21950 appeared. The TREE_VALUE of each node is a STRING_CST. */
21952 static tree
21953 cp_parser_asm_clobber_list (cp_parser* parser)
21955 tree clobbers = NULL_TREE;
21957 while (true)
21959 tree string_literal;
21961 /* Look for the string literal. */
21962 string_literal = cp_parser_string_literal (parser, false, false);
21963 /* Add it to the list. */
21964 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21965 /* If the next token is not a `,', then the list is
21966 complete. */
21967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21968 break;
21969 /* Consume the `,' token. */
21970 cp_lexer_consume_token (parser->lexer);
21973 return clobbers;
21976 /* Parse an asm-label-list.
21978 asm-label-list:
21979 identifier
21980 asm-label-list , identifier
21982 Returns a TREE_LIST, indicating the labels in the order that they
21983 appeared. The TREE_VALUE of each node is a label. */
21985 static tree
21986 cp_parser_asm_label_list (cp_parser* parser)
21988 tree labels = NULL_TREE;
21990 while (true)
21992 tree identifier, label, name;
21994 /* Look for the identifier. */
21995 identifier = cp_parser_identifier (parser);
21996 if (!error_operand_p (identifier))
21998 label = lookup_label (identifier);
21999 if (TREE_CODE (label) == LABEL_DECL)
22001 TREE_USED (label) = 1;
22002 check_goto (label);
22003 name = build_string (IDENTIFIER_LENGTH (identifier),
22004 IDENTIFIER_POINTER (identifier));
22005 labels = tree_cons (name, label, labels);
22008 /* If the next token is not a `,', then the list is
22009 complete. */
22010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22011 break;
22012 /* Consume the `,' token. */
22013 cp_lexer_consume_token (parser->lexer);
22016 return nreverse (labels);
22019 /* Return TRUE iff the next tokens in the stream are possibly the
22020 beginning of a GNU extension attribute. */
22022 static bool
22023 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22025 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22028 /* Return TRUE iff the next tokens in the stream are possibly the
22029 beginning of a standard C++-11 attribute specifier. */
22031 static bool
22032 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22034 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22037 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22038 beginning of a standard C++-11 attribute specifier. */
22040 static bool
22041 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22043 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22045 return (cxx_dialect >= cxx11
22046 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22047 || (token->type == CPP_OPEN_SQUARE
22048 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22049 && token->type == CPP_OPEN_SQUARE)));
22052 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22053 beginning of a GNU extension attribute. */
22055 static bool
22056 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22058 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22060 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22063 /* Return true iff the next tokens can be the beginning of either a
22064 GNU attribute list, or a standard C++11 attribute sequence. */
22066 static bool
22067 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22069 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22070 || cp_next_tokens_can_be_std_attribute_p (parser));
22073 /* Return true iff the next Nth tokens can be the beginning of either
22074 a GNU attribute list, or a standard C++11 attribute sequence. */
22076 static bool
22077 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22079 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22080 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22083 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22084 of GNU attributes, or return NULL. */
22086 static tree
22087 cp_parser_attributes_opt (cp_parser *parser)
22089 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22090 return cp_parser_gnu_attributes_opt (parser);
22091 return cp_parser_std_attribute_spec_seq (parser);
22094 #define CILK_SIMD_FN_CLAUSE_MASK \
22095 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22096 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22097 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22098 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22099 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22101 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22102 vector [(<clauses>)] */
22104 static void
22105 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22107 bool first_p = parser->cilk_simd_fn_info == NULL;
22108 cp_token *token = v_token;
22109 if (first_p)
22111 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22112 parser->cilk_simd_fn_info->error_seen = false;
22113 parser->cilk_simd_fn_info->fndecl_seen = false;
22114 parser->cilk_simd_fn_info->tokens = vNULL;
22116 int paren_scope = 0;
22117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22119 cp_lexer_consume_token (parser->lexer);
22120 v_token = cp_lexer_peek_token (parser->lexer);
22121 paren_scope++;
22123 while (paren_scope > 0)
22125 token = cp_lexer_peek_token (parser->lexer);
22126 if (token->type == CPP_OPEN_PAREN)
22127 paren_scope++;
22128 else if (token->type == CPP_CLOSE_PAREN)
22129 paren_scope--;
22130 /* Do not push the last ')' */
22131 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22132 cp_lexer_consume_token (parser->lexer);
22135 token->type = CPP_PRAGMA_EOL;
22136 parser->lexer->next_token = token;
22137 cp_lexer_consume_token (parser->lexer);
22139 struct cp_token_cache *cp
22140 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22141 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22144 /* Parse an (optional) series of attributes.
22146 attributes:
22147 attributes attribute
22149 attribute:
22150 __attribute__ (( attribute-list [opt] ))
22152 The return value is as for cp_parser_gnu_attribute_list. */
22154 static tree
22155 cp_parser_gnu_attributes_opt (cp_parser* parser)
22157 tree attributes = NULL_TREE;
22159 while (true)
22161 cp_token *token;
22162 tree attribute_list;
22163 bool ok = true;
22165 /* Peek at the next token. */
22166 token = cp_lexer_peek_token (parser->lexer);
22167 /* If it's not `__attribute__', then we're done. */
22168 if (token->keyword != RID_ATTRIBUTE)
22169 break;
22171 /* Consume the `__attribute__' keyword. */
22172 cp_lexer_consume_token (parser->lexer);
22173 /* Look for the two `(' tokens. */
22174 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22175 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22177 /* Peek at the next token. */
22178 token = cp_lexer_peek_token (parser->lexer);
22179 if (token->type != CPP_CLOSE_PAREN)
22180 /* Parse the attribute-list. */
22181 attribute_list = cp_parser_gnu_attribute_list (parser);
22182 else
22183 /* If the next token is a `)', then there is no attribute
22184 list. */
22185 attribute_list = NULL;
22187 /* Look for the two `)' tokens. */
22188 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22189 ok = false;
22190 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22191 ok = false;
22192 if (!ok)
22193 cp_parser_skip_to_end_of_statement (parser);
22195 /* Add these new attributes to the list. */
22196 attributes = chainon (attributes, attribute_list);
22199 return attributes;
22202 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22203 "__vector" or "__vector__." */
22205 static inline bool
22206 is_cilkplus_vector_p (tree name)
22208 if (flag_cilkplus && is_attribute_p ("vector", name))
22209 return true;
22210 return false;
22213 /* Parse a GNU attribute-list.
22215 attribute-list:
22216 attribute
22217 attribute-list , attribute
22219 attribute:
22220 identifier
22221 identifier ( identifier )
22222 identifier ( identifier , expression-list )
22223 identifier ( expression-list )
22225 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22226 to an attribute. The TREE_PURPOSE of each node is the identifier
22227 indicating which attribute is in use. The TREE_VALUE represents
22228 the arguments, if any. */
22230 static tree
22231 cp_parser_gnu_attribute_list (cp_parser* parser)
22233 tree attribute_list = NULL_TREE;
22234 bool save_translate_strings_p = parser->translate_strings_p;
22236 parser->translate_strings_p = false;
22237 while (true)
22239 cp_token *token;
22240 tree identifier;
22241 tree attribute;
22243 /* Look for the identifier. We also allow keywords here; for
22244 example `__attribute__ ((const))' is legal. */
22245 token = cp_lexer_peek_token (parser->lexer);
22246 if (token->type == CPP_NAME
22247 || token->type == CPP_KEYWORD)
22249 tree arguments = NULL_TREE;
22251 /* Consume the token, but save it since we need it for the
22252 SIMD enabled function parsing. */
22253 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22255 /* Save away the identifier that indicates which attribute
22256 this is. */
22257 identifier = (token->type == CPP_KEYWORD)
22258 /* For keywords, use the canonical spelling, not the
22259 parsed identifier. */
22260 ? ridpointers[(int) token->keyword]
22261 : id_token->u.value;
22263 attribute = build_tree_list (identifier, NULL_TREE);
22265 /* Peek at the next token. */
22266 token = cp_lexer_peek_token (parser->lexer);
22267 /* If it's an `(', then parse the attribute arguments. */
22268 if (token->type == CPP_OPEN_PAREN)
22270 vec<tree, va_gc> *vec;
22271 int attr_flag = (attribute_takes_identifier_p (identifier)
22272 ? id_attr : normal_attr);
22273 if (is_cilkplus_vector_p (identifier))
22275 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22276 continue;
22278 else
22279 vec = cp_parser_parenthesized_expression_list
22280 (parser, attr_flag, /*cast_p=*/false,
22281 /*allow_expansion_p=*/false,
22282 /*non_constant_p=*/NULL);
22283 if (vec == NULL)
22284 arguments = error_mark_node;
22285 else
22287 arguments = build_tree_list_vec (vec);
22288 release_tree_vector (vec);
22290 /* Save the arguments away. */
22291 TREE_VALUE (attribute) = arguments;
22293 else if (is_cilkplus_vector_p (identifier))
22295 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22296 continue;
22299 if (arguments != error_mark_node)
22301 /* Add this attribute to the list. */
22302 TREE_CHAIN (attribute) = attribute_list;
22303 attribute_list = attribute;
22306 token = cp_lexer_peek_token (parser->lexer);
22308 /* Now, look for more attributes. If the next token isn't a
22309 `,', we're done. */
22310 if (token->type != CPP_COMMA)
22311 break;
22313 /* Consume the comma and keep going. */
22314 cp_lexer_consume_token (parser->lexer);
22316 parser->translate_strings_p = save_translate_strings_p;
22318 /* We built up the list in reverse order. */
22319 return nreverse (attribute_list);
22322 /* Parse a standard C++11 attribute.
22324 The returned representation is a TREE_LIST which TREE_PURPOSE is
22325 the scoped name of the attribute, and the TREE_VALUE is its
22326 arguments list.
22328 Note that the scoped name of the attribute is itself a TREE_LIST
22329 which TREE_PURPOSE is the namespace of the attribute, and
22330 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22331 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22332 and which TREE_PURPOSE is directly the attribute name.
22334 Clients of the attribute code should use get_attribute_namespace
22335 and get_attribute_name to get the actual namespace and name of
22336 attributes, regardless of their being GNU or C++11 attributes.
22338 attribute:
22339 attribute-token attribute-argument-clause [opt]
22341 attribute-token:
22342 identifier
22343 attribute-scoped-token
22345 attribute-scoped-token:
22346 attribute-namespace :: identifier
22348 attribute-namespace:
22349 identifier
22351 attribute-argument-clause:
22352 ( balanced-token-seq )
22354 balanced-token-seq:
22355 balanced-token [opt]
22356 balanced-token-seq balanced-token
22358 balanced-token:
22359 ( balanced-token-seq )
22360 [ balanced-token-seq ]
22361 { balanced-token-seq }. */
22363 static tree
22364 cp_parser_std_attribute (cp_parser *parser)
22366 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22367 cp_token *token;
22369 /* First, parse name of the the attribute, a.k.a
22370 attribute-token. */
22372 token = cp_lexer_peek_token (parser->lexer);
22373 if (token->type == CPP_NAME)
22374 attr_id = token->u.value;
22375 else if (token->type == CPP_KEYWORD)
22376 attr_id = ridpointers[(int) token->keyword];
22377 else if (token->flags & NAMED_OP)
22378 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22380 if (attr_id == NULL_TREE)
22381 return NULL_TREE;
22383 cp_lexer_consume_token (parser->lexer);
22385 token = cp_lexer_peek_token (parser->lexer);
22386 if (token->type == CPP_SCOPE)
22388 /* We are seeing a scoped attribute token. */
22390 cp_lexer_consume_token (parser->lexer);
22391 attr_ns = attr_id;
22393 token = cp_lexer_consume_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
22400 error_at (token->location,
22401 "expected an identifier for the attribute name");
22402 return error_mark_node;
22404 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22405 NULL_TREE);
22406 token = cp_lexer_peek_token (parser->lexer);
22408 else
22410 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22411 NULL_TREE);
22412 /* C++11 noreturn attribute is equivalent to GNU's. */
22413 if (is_attribute_p ("noreturn", attr_id))
22414 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22415 /* C++14 deprecated attribute is equivalent to GNU's. */
22416 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22418 if (cxx_dialect == cxx11)
22419 pedwarn (token->location, OPT_Wpedantic,
22420 "%<deprecated%> is a C++14 feature;"
22421 " use %<gnu::deprecated%>");
22422 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22426 /* Now parse the optional argument clause of the attribute. */
22428 if (token->type != CPP_OPEN_PAREN)
22429 return attribute;
22432 vec<tree, va_gc> *vec;
22433 int attr_flag = normal_attr;
22435 if (attr_ns == get_identifier ("gnu")
22436 && attribute_takes_identifier_p (attr_id))
22437 /* A GNU attribute that takes an identifier in parameter. */
22438 attr_flag = id_attr;
22440 vec = cp_parser_parenthesized_expression_list
22441 (parser, attr_flag, /*cast_p=*/false,
22442 /*allow_expansion_p=*/true,
22443 /*non_constant_p=*/NULL);
22444 if (vec == NULL)
22445 arguments = error_mark_node;
22446 else
22448 arguments = build_tree_list_vec (vec);
22449 release_tree_vector (vec);
22452 if (arguments == error_mark_node)
22453 attribute = error_mark_node;
22454 else
22455 TREE_VALUE (attribute) = arguments;
22458 return attribute;
22461 /* Parse a list of standard C++-11 attributes.
22463 attribute-list:
22464 attribute [opt]
22465 attribute-list , attribute[opt]
22466 attribute ...
22467 attribute-list , attribute ...
22470 static tree
22471 cp_parser_std_attribute_list (cp_parser *parser)
22473 tree attributes = NULL_TREE, attribute = NULL_TREE;
22474 cp_token *token = NULL;
22476 while (true)
22478 attribute = cp_parser_std_attribute (parser);
22479 if (attribute == error_mark_node)
22480 break;
22481 if (attribute != NULL_TREE)
22483 TREE_CHAIN (attribute) = attributes;
22484 attributes = attribute;
22486 token = cp_lexer_peek_token (parser->lexer);
22487 if (token->type != CPP_COMMA)
22488 break;
22489 cp_lexer_consume_token (parser->lexer);
22491 attributes = nreverse (attributes);
22492 return attributes;
22495 /* Parse a standard C++-11 attribute specifier.
22497 attribute-specifier:
22498 [ [ attribute-list ] ]
22499 alignment-specifier
22501 alignment-specifier:
22502 alignas ( type-id ... [opt] )
22503 alignas ( alignment-expression ... [opt] ). */
22505 static tree
22506 cp_parser_std_attribute_spec (cp_parser *parser)
22508 tree attributes = NULL_TREE;
22509 cp_token *token = cp_lexer_peek_token (parser->lexer);
22511 if (token->type == CPP_OPEN_SQUARE
22512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22514 cp_lexer_consume_token (parser->lexer);
22515 cp_lexer_consume_token (parser->lexer);
22517 attributes = cp_parser_std_attribute_list (parser);
22519 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22520 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22521 cp_parser_skip_to_end_of_statement (parser);
22522 else
22523 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22524 when we are sure that we have actually parsed them. */
22525 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22527 else
22529 tree alignas_expr;
22531 /* Look for an alignment-specifier. */
22533 token = cp_lexer_peek_token (parser->lexer);
22535 if (token->type != CPP_KEYWORD
22536 || token->keyword != RID_ALIGNAS)
22537 return NULL_TREE;
22539 cp_lexer_consume_token (parser->lexer);
22540 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22542 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22544 cp_parser_error (parser, "expected %<(%>");
22545 return error_mark_node;
22548 cp_parser_parse_tentatively (parser);
22549 alignas_expr = cp_parser_type_id (parser);
22551 if (!cp_parser_parse_definitely (parser))
22553 gcc_assert (alignas_expr == error_mark_node
22554 || alignas_expr == NULL_TREE);
22556 alignas_expr =
22557 cp_parser_assignment_expression (parser);
22558 if (alignas_expr == error_mark_node)
22559 cp_parser_skip_to_end_of_statement (parser);
22560 if (alignas_expr == NULL_TREE
22561 || alignas_expr == error_mark_node)
22562 return alignas_expr;
22565 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22567 cp_parser_error (parser, "expected %<)%>");
22568 return error_mark_node;
22571 alignas_expr = cxx_alignas_expr (alignas_expr);
22573 /* Build the C++-11 representation of an 'aligned'
22574 attribute. */
22575 attributes =
22576 build_tree_list (build_tree_list (get_identifier ("gnu"),
22577 get_identifier ("aligned")),
22578 build_tree_list (NULL_TREE, alignas_expr));
22581 return attributes;
22584 /* Parse a standard C++-11 attribute-specifier-seq.
22586 attribute-specifier-seq:
22587 attribute-specifier-seq [opt] attribute-specifier
22590 static tree
22591 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22593 tree attr_specs = NULL;
22595 while (true)
22597 tree attr_spec = cp_parser_std_attribute_spec (parser);
22598 if (attr_spec == NULL_TREE)
22599 break;
22600 if (attr_spec == error_mark_node)
22601 return error_mark_node;
22603 TREE_CHAIN (attr_spec) = attr_specs;
22604 attr_specs = attr_spec;
22607 attr_specs = nreverse (attr_specs);
22608 return attr_specs;
22611 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22612 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22613 current value of the PEDANTIC flag, regardless of whether or not
22614 the `__extension__' keyword is present. The caller is responsible
22615 for restoring the value of the PEDANTIC flag. */
22617 static bool
22618 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22620 /* Save the old value of the PEDANTIC flag. */
22621 *saved_pedantic = pedantic;
22623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22625 /* Consume the `__extension__' token. */
22626 cp_lexer_consume_token (parser->lexer);
22627 /* We're not being pedantic while the `__extension__' keyword is
22628 in effect. */
22629 pedantic = 0;
22631 return true;
22634 return false;
22637 /* Parse a label declaration.
22639 label-declaration:
22640 __label__ label-declarator-seq ;
22642 label-declarator-seq:
22643 identifier , label-declarator-seq
22644 identifier */
22646 static void
22647 cp_parser_label_declaration (cp_parser* parser)
22649 /* Look for the `__label__' keyword. */
22650 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22652 while (true)
22654 tree identifier;
22656 /* Look for an identifier. */
22657 identifier = cp_parser_identifier (parser);
22658 /* If we failed, stop. */
22659 if (identifier == error_mark_node)
22660 break;
22661 /* Declare it as a label. */
22662 finish_label_decl (identifier);
22663 /* If the next token is a `;', stop. */
22664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22665 break;
22666 /* Look for the `,' separating the label declarations. */
22667 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22670 /* Look for the final `;'. */
22671 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22674 /* Support Functions */
22676 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22677 NAME should have one of the representations used for an
22678 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22679 is returned. If PARSER->SCOPE is a dependent type, then a
22680 SCOPE_REF is returned.
22682 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22683 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22684 was formed. Abstractly, such entities should not be passed to this
22685 function, because they do not need to be looked up, but it is
22686 simpler to check for this special case here, rather than at the
22687 call-sites.
22689 In cases not explicitly covered above, this function returns a
22690 DECL, OVERLOAD, or baselink representing the result of the lookup.
22691 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22692 is returned.
22694 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22695 (e.g., "struct") that was used. In that case bindings that do not
22696 refer to types are ignored.
22698 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22699 ignored.
22701 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22702 are ignored.
22704 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22705 types.
22707 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22708 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22709 NULL_TREE otherwise. */
22711 static tree
22712 cp_parser_lookup_name (cp_parser *parser, tree name,
22713 enum tag_types tag_type,
22714 bool is_template,
22715 bool is_namespace,
22716 bool check_dependency,
22717 tree *ambiguous_decls,
22718 location_t name_location)
22720 tree decl;
22721 tree object_type = parser->context->object_type;
22723 /* Assume that the lookup will be unambiguous. */
22724 if (ambiguous_decls)
22725 *ambiguous_decls = NULL_TREE;
22727 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22728 no longer valid. Note that if we are parsing tentatively, and
22729 the parse fails, OBJECT_TYPE will be automatically restored. */
22730 parser->context->object_type = NULL_TREE;
22732 if (name == error_mark_node)
22733 return error_mark_node;
22735 /* A template-id has already been resolved; there is no lookup to
22736 do. */
22737 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22738 return name;
22739 if (BASELINK_P (name))
22741 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22742 == TEMPLATE_ID_EXPR);
22743 return name;
22746 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22747 it should already have been checked to make sure that the name
22748 used matches the type being destroyed. */
22749 if (TREE_CODE (name) == BIT_NOT_EXPR)
22751 tree type;
22753 /* Figure out to which type this destructor applies. */
22754 if (parser->scope)
22755 type = parser->scope;
22756 else if (object_type)
22757 type = object_type;
22758 else
22759 type = current_class_type;
22760 /* If that's not a class type, there is no destructor. */
22761 if (!type || !CLASS_TYPE_P (type))
22762 return error_mark_node;
22763 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22764 lazily_declare_fn (sfk_destructor, type);
22765 if (!CLASSTYPE_DESTRUCTORS (type))
22766 return error_mark_node;
22767 /* If it was a class type, return the destructor. */
22768 return CLASSTYPE_DESTRUCTORS (type);
22771 /* By this point, the NAME should be an ordinary identifier. If
22772 the id-expression was a qualified name, the qualifying scope is
22773 stored in PARSER->SCOPE at this point. */
22774 gcc_assert (identifier_p (name));
22776 /* Perform the lookup. */
22777 if (parser->scope)
22779 bool dependent_p;
22781 if (parser->scope == error_mark_node)
22782 return error_mark_node;
22784 /* If the SCOPE is dependent, the lookup must be deferred until
22785 the template is instantiated -- unless we are explicitly
22786 looking up names in uninstantiated templates. Even then, we
22787 cannot look up the name if the scope is not a class type; it
22788 might, for example, be a template type parameter. */
22789 dependent_p = (TYPE_P (parser->scope)
22790 && dependent_scope_p (parser->scope));
22791 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22792 && dependent_p)
22793 /* Defer lookup. */
22794 decl = error_mark_node;
22795 else
22797 tree pushed_scope = NULL_TREE;
22799 /* If PARSER->SCOPE is a dependent type, then it must be a
22800 class type, and we must not be checking dependencies;
22801 otherwise, we would have processed this lookup above. So
22802 that PARSER->SCOPE is not considered a dependent base by
22803 lookup_member, we must enter the scope here. */
22804 if (dependent_p)
22805 pushed_scope = push_scope (parser->scope);
22807 /* If the PARSER->SCOPE is a template specialization, it
22808 may be instantiated during name lookup. In that case,
22809 errors may be issued. Even if we rollback the current
22810 tentative parse, those errors are valid. */
22811 decl = lookup_qualified_name (parser->scope, name,
22812 tag_type != none_type,
22813 /*complain=*/true);
22815 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22816 lookup result and the nested-name-specifier nominates a class C:
22817 * if the name specified after the nested-name-specifier, when
22818 looked up in C, is the injected-class-name of C (Clause 9), or
22819 * if the name specified after the nested-name-specifier is the
22820 same as the identifier or the simple-template-id's template-
22821 name in the last component of the nested-name-specifier,
22822 the name is instead considered to name the constructor of
22823 class C. [ Note: for example, the constructor is not an
22824 acceptable lookup result in an elaborated-type-specifier so
22825 the constructor would not be used in place of the
22826 injected-class-name. --end note ] Such a constructor name
22827 shall be used only in the declarator-id of a declaration that
22828 names a constructor or in a using-declaration. */
22829 if (tag_type == none_type
22830 && DECL_SELF_REFERENCE_P (decl)
22831 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22832 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22833 tag_type != none_type,
22834 /*complain=*/true);
22836 /* If we have a single function from a using decl, pull it out. */
22837 if (TREE_CODE (decl) == OVERLOAD
22838 && !really_overloaded_fn (decl))
22839 decl = OVL_FUNCTION (decl);
22841 if (pushed_scope)
22842 pop_scope (pushed_scope);
22845 /* If the scope is a dependent type and either we deferred lookup or
22846 we did lookup but didn't find the name, rememeber the name. */
22847 if (decl == error_mark_node && TYPE_P (parser->scope)
22848 && dependent_type_p (parser->scope))
22850 if (tag_type)
22852 tree type;
22854 /* The resolution to Core Issue 180 says that `struct
22855 A::B' should be considered a type-name, even if `A'
22856 is dependent. */
22857 type = make_typename_type (parser->scope, name, tag_type,
22858 /*complain=*/tf_error);
22859 if (type != error_mark_node)
22860 decl = TYPE_NAME (type);
22862 else if (is_template
22863 && (cp_parser_next_token_ends_template_argument_p (parser)
22864 || cp_lexer_next_token_is (parser->lexer,
22865 CPP_CLOSE_PAREN)))
22866 decl = make_unbound_class_template (parser->scope,
22867 name, NULL_TREE,
22868 /*complain=*/tf_error);
22869 else
22870 decl = build_qualified_name (/*type=*/NULL_TREE,
22871 parser->scope, name,
22872 is_template);
22874 parser->qualifying_scope = parser->scope;
22875 parser->object_scope = NULL_TREE;
22877 else if (object_type)
22879 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22880 OBJECT_TYPE is not a class. */
22881 if (CLASS_TYPE_P (object_type))
22882 /* If the OBJECT_TYPE is a template specialization, it may
22883 be instantiated during name lookup. In that case, errors
22884 may be issued. Even if we rollback the current tentative
22885 parse, those errors are valid. */
22886 decl = lookup_member (object_type,
22887 name,
22888 /*protect=*/0,
22889 tag_type != none_type,
22890 tf_warning_or_error);
22891 else
22892 decl = NULL_TREE;
22894 if (!decl)
22895 /* Look it up in the enclosing context. */
22896 decl = lookup_name_real (name, tag_type != none_type,
22897 /*nonclass=*/0,
22898 /*block_p=*/true, is_namespace, 0);
22899 parser->object_scope = object_type;
22900 parser->qualifying_scope = NULL_TREE;
22902 else
22904 decl = lookup_name_real (name, tag_type != none_type,
22905 /*nonclass=*/0,
22906 /*block_p=*/true, is_namespace, 0);
22907 parser->qualifying_scope = NULL_TREE;
22908 parser->object_scope = NULL_TREE;
22911 /* If the lookup failed, let our caller know. */
22912 if (!decl || decl == error_mark_node)
22913 return error_mark_node;
22915 /* Pull out the template from an injected-class-name (or multiple). */
22916 if (is_template)
22917 decl = maybe_get_template_decl_from_type_decl (decl);
22919 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22920 if (TREE_CODE (decl) == TREE_LIST)
22922 if (ambiguous_decls)
22923 *ambiguous_decls = decl;
22924 /* The error message we have to print is too complicated for
22925 cp_parser_error, so we incorporate its actions directly. */
22926 if (!cp_parser_simulate_error (parser))
22928 error_at (name_location, "reference to %qD is ambiguous",
22929 name);
22930 print_candidates (decl);
22932 return error_mark_node;
22935 gcc_assert (DECL_P (decl)
22936 || TREE_CODE (decl) == OVERLOAD
22937 || TREE_CODE (decl) == SCOPE_REF
22938 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22939 || BASELINK_P (decl));
22941 /* If we have resolved the name of a member declaration, check to
22942 see if the declaration is accessible. When the name resolves to
22943 set of overloaded functions, accessibility is checked when
22944 overload resolution is done.
22946 During an explicit instantiation, access is not checked at all,
22947 as per [temp.explicit]. */
22948 if (DECL_P (decl))
22949 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22951 maybe_record_typedef_use (decl);
22953 return decl;
22956 /* Like cp_parser_lookup_name, but for use in the typical case where
22957 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22958 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22960 static tree
22961 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22963 return cp_parser_lookup_name (parser, name,
22964 none_type,
22965 /*is_template=*/false,
22966 /*is_namespace=*/false,
22967 /*check_dependency=*/true,
22968 /*ambiguous_decls=*/NULL,
22969 location);
22972 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22973 the current context, return the TYPE_DECL. If TAG_NAME_P is
22974 true, the DECL indicates the class being defined in a class-head,
22975 or declared in an elaborated-type-specifier.
22977 Otherwise, return DECL. */
22979 static tree
22980 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22982 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22983 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22985 struct A {
22986 template <typename T> struct B;
22989 template <typename T> struct A::B {};
22991 Similarly, in an elaborated-type-specifier:
22993 namespace N { struct X{}; }
22995 struct A {
22996 template <typename T> friend struct N::X;
22999 However, if the DECL refers to a class type, and we are in
23000 the scope of the class, then the name lookup automatically
23001 finds the TYPE_DECL created by build_self_reference rather
23002 than a TEMPLATE_DECL. For example, in:
23004 template <class T> struct S {
23005 S s;
23008 there is no need to handle such case. */
23010 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23011 return DECL_TEMPLATE_RESULT (decl);
23013 return decl;
23016 /* If too many, or too few, template-parameter lists apply to the
23017 declarator, issue an error message. Returns TRUE if all went well,
23018 and FALSE otherwise. */
23020 static bool
23021 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23022 cp_declarator *declarator,
23023 location_t declarator_location)
23025 switch (declarator->kind)
23027 case cdk_id:
23029 unsigned num_templates = 0;
23030 tree scope = declarator->u.id.qualifying_scope;
23032 if (scope)
23033 num_templates = num_template_headers_for_class (scope);
23034 else if (TREE_CODE (declarator->u.id.unqualified_name)
23035 == TEMPLATE_ID_EXPR)
23036 /* If the DECLARATOR has the form `X<y>' then it uses one
23037 additional level of template parameters. */
23038 ++num_templates;
23040 return cp_parser_check_template_parameters
23041 (parser, num_templates, declarator_location, declarator);
23044 case cdk_function:
23045 case cdk_array:
23046 case cdk_pointer:
23047 case cdk_reference:
23048 case cdk_ptrmem:
23049 return (cp_parser_check_declarator_template_parameters
23050 (parser, declarator->declarator, declarator_location));
23052 case cdk_error:
23053 return true;
23055 default:
23056 gcc_unreachable ();
23058 return false;
23061 /* NUM_TEMPLATES were used in the current declaration. If that is
23062 invalid, return FALSE and issue an error messages. Otherwise,
23063 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23064 declarator and we can print more accurate diagnostics. */
23066 static bool
23067 cp_parser_check_template_parameters (cp_parser* parser,
23068 unsigned num_templates,
23069 location_t location,
23070 cp_declarator *declarator)
23072 /* If there are the same number of template classes and parameter
23073 lists, that's OK. */
23074 if (parser->num_template_parameter_lists == num_templates)
23075 return true;
23076 /* If there are more, but only one more, then we are referring to a
23077 member template. That's OK too. */
23078 if (parser->num_template_parameter_lists == num_templates + 1)
23079 return true;
23080 /* If there are more template classes than parameter lists, we have
23081 something like:
23083 template <class T> void S<T>::R<T>::f (); */
23084 if (parser->num_template_parameter_lists < num_templates)
23086 if (declarator && !current_function_decl)
23087 error_at (location, "specializing member %<%T::%E%> "
23088 "requires %<template<>%> syntax",
23089 declarator->u.id.qualifying_scope,
23090 declarator->u.id.unqualified_name);
23091 else if (declarator)
23092 error_at (location, "invalid declaration of %<%T::%E%>",
23093 declarator->u.id.qualifying_scope,
23094 declarator->u.id.unqualified_name);
23095 else
23096 error_at (location, "too few template-parameter-lists");
23097 return false;
23099 /* Otherwise, there are too many template parameter lists. We have
23100 something like:
23102 template <class T> template <class U> void S::f(); */
23103 error_at (location, "too many template-parameter-lists");
23104 return false;
23107 /* Parse an optional `::' token indicating that the following name is
23108 from the global namespace. If so, PARSER->SCOPE is set to the
23109 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23110 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23111 Returns the new value of PARSER->SCOPE, if the `::' token is
23112 present, and NULL_TREE otherwise. */
23114 static tree
23115 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23117 cp_token *token;
23119 /* Peek at the next token. */
23120 token = cp_lexer_peek_token (parser->lexer);
23121 /* If we're looking at a `::' token then we're starting from the
23122 global namespace, not our current location. */
23123 if (token->type == CPP_SCOPE)
23125 /* Consume the `::' token. */
23126 cp_lexer_consume_token (parser->lexer);
23127 /* Set the SCOPE so that we know where to start the lookup. */
23128 parser->scope = global_namespace;
23129 parser->qualifying_scope = global_namespace;
23130 parser->object_scope = NULL_TREE;
23132 return parser->scope;
23134 else if (!current_scope_valid_p)
23136 parser->scope = NULL_TREE;
23137 parser->qualifying_scope = NULL_TREE;
23138 parser->object_scope = NULL_TREE;
23141 return NULL_TREE;
23144 /* Returns TRUE if the upcoming token sequence is the start of a
23145 constructor declarator. If FRIEND_P is true, the declarator is
23146 preceded by the `friend' specifier. */
23148 static bool
23149 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23151 bool constructor_p;
23152 bool outside_class_specifier_p;
23153 tree nested_name_specifier;
23154 cp_token *next_token;
23156 /* The common case is that this is not a constructor declarator, so
23157 try to avoid doing lots of work if at all possible. It's not
23158 valid declare a constructor at function scope. */
23159 if (parser->in_function_body)
23160 return false;
23161 /* And only certain tokens can begin a constructor declarator. */
23162 next_token = cp_lexer_peek_token (parser->lexer);
23163 if (next_token->type != CPP_NAME
23164 && next_token->type != CPP_SCOPE
23165 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23166 && next_token->type != CPP_TEMPLATE_ID)
23167 return false;
23169 /* Parse tentatively; we are going to roll back all of the tokens
23170 consumed here. */
23171 cp_parser_parse_tentatively (parser);
23172 /* Assume that we are looking at a constructor declarator. */
23173 constructor_p = true;
23175 /* Look for the optional `::' operator. */
23176 cp_parser_global_scope_opt (parser,
23177 /*current_scope_valid_p=*/false);
23178 /* Look for the nested-name-specifier. */
23179 nested_name_specifier
23180 = (cp_parser_nested_name_specifier_opt (parser,
23181 /*typename_keyword_p=*/false,
23182 /*check_dependency_p=*/false,
23183 /*type_p=*/false,
23184 /*is_declaration=*/false));
23186 outside_class_specifier_p = (!at_class_scope_p ()
23187 || !TYPE_BEING_DEFINED (current_class_type)
23188 || friend_p);
23190 /* Outside of a class-specifier, there must be a
23191 nested-name-specifier. */
23192 if (!nested_name_specifier && outside_class_specifier_p)
23193 constructor_p = false;
23194 else if (nested_name_specifier == error_mark_node)
23195 constructor_p = false;
23197 /* If we have a class scope, this is easy; DR 147 says that S::S always
23198 names the constructor, and no other qualified name could. */
23199 if (constructor_p && nested_name_specifier
23200 && CLASS_TYPE_P (nested_name_specifier))
23202 tree id = cp_parser_unqualified_id (parser,
23203 /*template_keyword_p=*/false,
23204 /*check_dependency_p=*/false,
23205 /*declarator_p=*/true,
23206 /*optional_p=*/false);
23207 if (is_overloaded_fn (id))
23208 id = DECL_NAME (get_first_fn (id));
23209 if (!constructor_name_p (id, nested_name_specifier))
23210 constructor_p = false;
23212 /* If we still think that this might be a constructor-declarator,
23213 look for a class-name. */
23214 else if (constructor_p)
23216 /* If we have:
23218 template <typename T> struct S {
23219 S();
23222 we must recognize that the nested `S' names a class. */
23223 tree type_decl;
23224 type_decl = cp_parser_class_name (parser,
23225 /*typename_keyword_p=*/false,
23226 /*template_keyword_p=*/false,
23227 none_type,
23228 /*check_dependency_p=*/false,
23229 /*class_head_p=*/false,
23230 /*is_declaration=*/false);
23231 /* If there was no class-name, then this is not a constructor.
23232 Otherwise, if we are in a class-specifier and we aren't
23233 handling a friend declaration, check that its type matches
23234 current_class_type (c++/38313). Note: error_mark_node
23235 is left alone for error recovery purposes. */
23236 constructor_p = (!cp_parser_error_occurred (parser)
23237 && (outside_class_specifier_p
23238 || type_decl == error_mark_node
23239 || same_type_p (current_class_type,
23240 TREE_TYPE (type_decl))));
23242 /* If we're still considering a constructor, we have to see a `(',
23243 to begin the parameter-declaration-clause, followed by either a
23244 `)', an `...', or a decl-specifier. We need to check for a
23245 type-specifier to avoid being fooled into thinking that:
23247 S (f) (int);
23249 is a constructor. (It is actually a function named `f' that
23250 takes one parameter (of type `int') and returns a value of type
23251 `S'. */
23252 if (constructor_p
23253 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23254 constructor_p = false;
23256 if (constructor_p
23257 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23258 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23259 /* A parameter declaration begins with a decl-specifier,
23260 which is either the "attribute" keyword, a storage class
23261 specifier, or (usually) a type-specifier. */
23262 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23264 tree type;
23265 tree pushed_scope = NULL_TREE;
23266 unsigned saved_num_template_parameter_lists;
23268 /* Names appearing in the type-specifier should be looked up
23269 in the scope of the class. */
23270 if (current_class_type)
23271 type = NULL_TREE;
23272 else
23274 type = TREE_TYPE (type_decl);
23275 if (TREE_CODE (type) == TYPENAME_TYPE)
23277 type = resolve_typename_type (type,
23278 /*only_current_p=*/false);
23279 if (TREE_CODE (type) == TYPENAME_TYPE)
23281 cp_parser_abort_tentative_parse (parser);
23282 return false;
23285 pushed_scope = push_scope (type);
23288 /* Inside the constructor parameter list, surrounding
23289 template-parameter-lists do not apply. */
23290 saved_num_template_parameter_lists
23291 = parser->num_template_parameter_lists;
23292 parser->num_template_parameter_lists = 0;
23294 /* Look for the type-specifier. */
23295 cp_parser_type_specifier (parser,
23296 CP_PARSER_FLAGS_NONE,
23297 /*decl_specs=*/NULL,
23298 /*is_declarator=*/true,
23299 /*declares_class_or_enum=*/NULL,
23300 /*is_cv_qualifier=*/NULL);
23302 parser->num_template_parameter_lists
23303 = saved_num_template_parameter_lists;
23305 /* Leave the scope of the class. */
23306 if (pushed_scope)
23307 pop_scope (pushed_scope);
23309 constructor_p = !cp_parser_error_occurred (parser);
23313 /* We did not really want to consume any tokens. */
23314 cp_parser_abort_tentative_parse (parser);
23316 return constructor_p;
23319 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23320 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23321 they must be performed once we are in the scope of the function.
23323 Returns the function defined. */
23325 static tree
23326 cp_parser_function_definition_from_specifiers_and_declarator
23327 (cp_parser* parser,
23328 cp_decl_specifier_seq *decl_specifiers,
23329 tree attributes,
23330 const cp_declarator *declarator)
23332 tree fn;
23333 bool success_p;
23335 /* Begin the function-definition. */
23336 success_p = start_function (decl_specifiers, declarator, attributes);
23338 /* The things we're about to see are not directly qualified by any
23339 template headers we've seen thus far. */
23340 reset_specialization ();
23342 /* If there were names looked up in the decl-specifier-seq that we
23343 did not check, check them now. We must wait until we are in the
23344 scope of the function to perform the checks, since the function
23345 might be a friend. */
23346 perform_deferred_access_checks (tf_warning_or_error);
23348 if (success_p)
23350 cp_finalize_omp_declare_simd (parser, current_function_decl);
23351 parser->omp_declare_simd = NULL;
23354 if (!success_p)
23356 /* Skip the entire function. */
23357 cp_parser_skip_to_end_of_block_or_statement (parser);
23358 fn = error_mark_node;
23360 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23362 /* Seen already, skip it. An error message has already been output. */
23363 cp_parser_skip_to_end_of_block_or_statement (parser);
23364 fn = current_function_decl;
23365 current_function_decl = NULL_TREE;
23366 /* If this is a function from a class, pop the nested class. */
23367 if (current_class_name)
23368 pop_nested_class ();
23370 else
23372 timevar_id_t tv;
23373 if (DECL_DECLARED_INLINE_P (current_function_decl))
23374 tv = TV_PARSE_INLINE;
23375 else
23376 tv = TV_PARSE_FUNC;
23377 timevar_push (tv);
23378 fn = cp_parser_function_definition_after_declarator (parser,
23379 /*inline_p=*/false);
23380 timevar_pop (tv);
23383 return fn;
23386 /* Parse the part of a function-definition that follows the
23387 declarator. INLINE_P is TRUE iff this function is an inline
23388 function defined within a class-specifier.
23390 Returns the function defined. */
23392 static tree
23393 cp_parser_function_definition_after_declarator (cp_parser* parser,
23394 bool inline_p)
23396 tree fn;
23397 bool ctor_initializer_p = false;
23398 bool saved_in_unbraced_linkage_specification_p;
23399 bool saved_in_function_body;
23400 unsigned saved_num_template_parameter_lists;
23401 cp_token *token;
23402 bool fully_implicit_function_template_p
23403 = parser->fully_implicit_function_template_p;
23404 parser->fully_implicit_function_template_p = false;
23405 tree implicit_template_parms
23406 = parser->implicit_template_parms;
23407 parser->implicit_template_parms = 0;
23408 cp_binding_level* implicit_template_scope
23409 = parser->implicit_template_scope;
23410 parser->implicit_template_scope = 0;
23412 saved_in_function_body = parser->in_function_body;
23413 parser->in_function_body = true;
23414 /* If the next token is `return', then the code may be trying to
23415 make use of the "named return value" extension that G++ used to
23416 support. */
23417 token = cp_lexer_peek_token (parser->lexer);
23418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23420 /* Consume the `return' keyword. */
23421 cp_lexer_consume_token (parser->lexer);
23422 /* Look for the identifier that indicates what value is to be
23423 returned. */
23424 cp_parser_identifier (parser);
23425 /* Issue an error message. */
23426 error_at (token->location,
23427 "named return values are no longer supported");
23428 /* Skip tokens until we reach the start of the function body. */
23429 while (true)
23431 cp_token *token = cp_lexer_peek_token (parser->lexer);
23432 if (token->type == CPP_OPEN_BRACE
23433 || token->type == CPP_EOF
23434 || token->type == CPP_PRAGMA_EOL)
23435 break;
23436 cp_lexer_consume_token (parser->lexer);
23439 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23440 anything declared inside `f'. */
23441 saved_in_unbraced_linkage_specification_p
23442 = parser->in_unbraced_linkage_specification_p;
23443 parser->in_unbraced_linkage_specification_p = false;
23444 /* Inside the function, surrounding template-parameter-lists do not
23445 apply. */
23446 saved_num_template_parameter_lists
23447 = parser->num_template_parameter_lists;
23448 parser->num_template_parameter_lists = 0;
23450 start_lambda_scope (current_function_decl);
23452 /* If the next token is `try', `__transaction_atomic', or
23453 `__transaction_relaxed`, then we are looking at either function-try-block
23454 or function-transaction-block. Note that all of these include the
23455 function-body. */
23456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23457 ctor_initializer_p = cp_parser_function_transaction (parser,
23458 RID_TRANSACTION_ATOMIC);
23459 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23460 RID_TRANSACTION_RELAXED))
23461 ctor_initializer_p = cp_parser_function_transaction (parser,
23462 RID_TRANSACTION_RELAXED);
23463 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23464 ctor_initializer_p = cp_parser_function_try_block (parser);
23465 else
23466 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23467 (parser, /*in_function_try_block=*/false);
23469 finish_lambda_scope ();
23471 /* Finish the function. */
23472 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23473 (inline_p ? 2 : 0));
23474 /* Generate code for it, if necessary. */
23475 expand_or_defer_fn (fn);
23476 /* Restore the saved values. */
23477 parser->in_unbraced_linkage_specification_p
23478 = saved_in_unbraced_linkage_specification_p;
23479 parser->num_template_parameter_lists
23480 = saved_num_template_parameter_lists;
23481 parser->in_function_body = saved_in_function_body;
23483 parser->fully_implicit_function_template_p
23484 = fully_implicit_function_template_p;
23485 parser->implicit_template_parms
23486 = implicit_template_parms;
23487 parser->implicit_template_scope
23488 = implicit_template_scope;
23490 if (parser->fully_implicit_function_template_p)
23491 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23493 return fn;
23496 /* Parse a template-declaration, assuming that the `export' (and
23497 `extern') keywords, if present, has already been scanned. MEMBER_P
23498 is as for cp_parser_template_declaration. */
23500 static void
23501 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23503 tree decl = NULL_TREE;
23504 vec<deferred_access_check, va_gc> *checks;
23505 tree parameter_list;
23506 bool friend_p = false;
23507 bool need_lang_pop;
23508 cp_token *token;
23510 /* Look for the `template' keyword. */
23511 token = cp_lexer_peek_token (parser->lexer);
23512 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23513 return;
23515 /* And the `<'. */
23516 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23517 return;
23518 if (at_class_scope_p () && current_function_decl)
23520 /* 14.5.2.2 [temp.mem]
23522 A local class shall not have member templates. */
23523 error_at (token->location,
23524 "invalid declaration of member template in local class");
23525 cp_parser_skip_to_end_of_block_or_statement (parser);
23526 return;
23528 /* [temp]
23530 A template ... shall not have C linkage. */
23531 if (current_lang_name == lang_name_c)
23533 error_at (token->location, "template with C linkage");
23534 /* Give it C++ linkage to avoid confusing other parts of the
23535 front end. */
23536 push_lang_context (lang_name_cplusplus);
23537 need_lang_pop = true;
23539 else
23540 need_lang_pop = false;
23542 /* We cannot perform access checks on the template parameter
23543 declarations until we know what is being declared, just as we
23544 cannot check the decl-specifier list. */
23545 push_deferring_access_checks (dk_deferred);
23547 /* If the next token is `>', then we have an invalid
23548 specialization. Rather than complain about an invalid template
23549 parameter, issue an error message here. */
23550 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23552 cp_parser_error (parser, "invalid explicit specialization");
23553 begin_specialization ();
23554 parameter_list = NULL_TREE;
23556 else
23558 /* Parse the template parameters. */
23559 parameter_list = cp_parser_template_parameter_list (parser);
23562 /* Get the deferred access checks from the parameter list. These
23563 will be checked once we know what is being declared, as for a
23564 member template the checks must be performed in the scope of the
23565 class containing the member. */
23566 checks = get_deferred_access_checks ();
23568 /* Look for the `>'. */
23569 cp_parser_skip_to_end_of_template_parameter_list (parser);
23570 /* We just processed one more parameter list. */
23571 ++parser->num_template_parameter_lists;
23572 /* If the next token is `template', there are more template
23573 parameters. */
23574 if (cp_lexer_next_token_is_keyword (parser->lexer,
23575 RID_TEMPLATE))
23576 cp_parser_template_declaration_after_export (parser, member_p);
23577 else if (cxx_dialect >= cxx11
23578 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23579 decl = cp_parser_alias_declaration (parser);
23580 else
23582 /* There are no access checks when parsing a template, as we do not
23583 know if a specialization will be a friend. */
23584 push_deferring_access_checks (dk_no_check);
23585 token = cp_lexer_peek_token (parser->lexer);
23586 decl = cp_parser_single_declaration (parser,
23587 checks,
23588 member_p,
23589 /*explicit_specialization_p=*/false,
23590 &friend_p);
23591 pop_deferring_access_checks ();
23593 /* If this is a member template declaration, let the front
23594 end know. */
23595 if (member_p && !friend_p && decl)
23597 if (TREE_CODE (decl) == TYPE_DECL)
23598 cp_parser_check_access_in_redeclaration (decl, token->location);
23600 decl = finish_member_template_decl (decl);
23602 else if (friend_p && decl
23603 && DECL_DECLARES_TYPE_P (decl))
23604 make_friend_class (current_class_type, TREE_TYPE (decl),
23605 /*complain=*/true);
23607 /* We are done with the current parameter list. */
23608 --parser->num_template_parameter_lists;
23610 pop_deferring_access_checks ();
23612 /* Finish up. */
23613 finish_template_decl (parameter_list);
23615 /* Check the template arguments for a literal operator template. */
23616 if (decl
23617 && DECL_DECLARES_FUNCTION_P (decl)
23618 && UDLIT_OPER_P (DECL_NAME (decl)))
23620 bool ok = true;
23621 if (parameter_list == NULL_TREE)
23622 ok = false;
23623 else
23625 int num_parms = TREE_VEC_LENGTH (parameter_list);
23626 if (num_parms == 1)
23628 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23629 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23630 if (TREE_TYPE (parm) != char_type_node
23631 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23632 ok = false;
23634 else if (num_parms == 2 && cxx_dialect >= cxx14)
23636 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23637 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23638 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23639 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23640 if (TREE_TYPE (parm) != TREE_TYPE (type)
23641 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23642 ok = false;
23644 else
23645 ok = false;
23647 if (!ok)
23649 if (cxx_dialect >= cxx14)
23650 error ("literal operator template %qD has invalid parameter list."
23651 " Expected non-type template argument pack <char...>"
23652 " or <typename CharT, CharT...>",
23653 decl);
23654 else
23655 error ("literal operator template %qD has invalid parameter list."
23656 " Expected non-type template argument pack <char...>",
23657 decl);
23660 /* Register member declarations. */
23661 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23662 finish_member_declaration (decl);
23663 /* For the erroneous case of a template with C linkage, we pushed an
23664 implicit C++ linkage scope; exit that scope now. */
23665 if (need_lang_pop)
23666 pop_lang_context ();
23667 /* If DECL is a function template, we must return to parse it later.
23668 (Even though there is no definition, there might be default
23669 arguments that need handling.) */
23670 if (member_p && decl
23671 && DECL_DECLARES_FUNCTION_P (decl))
23672 vec_safe_push (unparsed_funs_with_definitions, decl);
23675 /* Perform the deferred access checks from a template-parameter-list.
23676 CHECKS is a TREE_LIST of access checks, as returned by
23677 get_deferred_access_checks. */
23679 static void
23680 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23682 ++processing_template_parmlist;
23683 perform_access_checks (checks, tf_warning_or_error);
23684 --processing_template_parmlist;
23687 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23688 `function-definition' sequence that follows a template header.
23689 If MEMBER_P is true, this declaration appears in a class scope.
23691 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23692 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23694 static tree
23695 cp_parser_single_declaration (cp_parser* parser,
23696 vec<deferred_access_check, va_gc> *checks,
23697 bool member_p,
23698 bool explicit_specialization_p,
23699 bool* friend_p)
23701 int declares_class_or_enum;
23702 tree decl = NULL_TREE;
23703 cp_decl_specifier_seq decl_specifiers;
23704 bool function_definition_p = false;
23705 cp_token *decl_spec_token_start;
23707 /* This function is only used when processing a template
23708 declaration. */
23709 gcc_assert (innermost_scope_kind () == sk_template_parms
23710 || innermost_scope_kind () == sk_template_spec);
23712 /* Defer access checks until we know what is being declared. */
23713 push_deferring_access_checks (dk_deferred);
23715 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23716 alternative. */
23717 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23718 cp_parser_decl_specifier_seq (parser,
23719 CP_PARSER_FLAGS_OPTIONAL,
23720 &decl_specifiers,
23721 &declares_class_or_enum);
23722 if (friend_p)
23723 *friend_p = cp_parser_friend_p (&decl_specifiers);
23725 /* There are no template typedefs. */
23726 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23728 error_at (decl_spec_token_start->location,
23729 "template declaration of %<typedef%>");
23730 decl = error_mark_node;
23733 /* Gather up the access checks that occurred the
23734 decl-specifier-seq. */
23735 stop_deferring_access_checks ();
23737 /* Check for the declaration of a template class. */
23738 if (declares_class_or_enum)
23740 if (cp_parser_declares_only_class_p (parser))
23742 decl = shadow_tag (&decl_specifiers);
23744 /* In this case:
23746 struct C {
23747 friend template <typename T> struct A<T>::B;
23750 A<T>::B will be represented by a TYPENAME_TYPE, and
23751 therefore not recognized by shadow_tag. */
23752 if (friend_p && *friend_p
23753 && !decl
23754 && decl_specifiers.type
23755 && TYPE_P (decl_specifiers.type))
23756 decl = decl_specifiers.type;
23758 if (decl && decl != error_mark_node)
23759 decl = TYPE_NAME (decl);
23760 else
23761 decl = error_mark_node;
23763 /* Perform access checks for template parameters. */
23764 cp_parser_perform_template_parameter_access_checks (checks);
23768 /* Complain about missing 'typename' or other invalid type names. */
23769 if (!decl_specifiers.any_type_specifiers_p
23770 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23772 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23773 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23774 the rest of this declaration. */
23775 decl = error_mark_node;
23776 goto out;
23779 /* If it's not a template class, try for a template function. If
23780 the next token is a `;', then this declaration does not declare
23781 anything. But, if there were errors in the decl-specifiers, then
23782 the error might well have come from an attempted class-specifier.
23783 In that case, there's no need to warn about a missing declarator. */
23784 if (!decl
23785 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23786 || decl_specifiers.type != error_mark_node))
23788 decl = cp_parser_init_declarator (parser,
23789 &decl_specifiers,
23790 checks,
23791 /*function_definition_allowed_p=*/true,
23792 member_p,
23793 declares_class_or_enum,
23794 &function_definition_p,
23795 NULL, NULL);
23797 /* 7.1.1-1 [dcl.stc]
23799 A storage-class-specifier shall not be specified in an explicit
23800 specialization... */
23801 if (decl
23802 && explicit_specialization_p
23803 && decl_specifiers.storage_class != sc_none)
23805 error_at (decl_spec_token_start->location,
23806 "explicit template specialization cannot have a storage class");
23807 decl = error_mark_node;
23810 if (decl && VAR_P (decl))
23811 check_template_variable (decl);
23814 /* Look for a trailing `;' after the declaration. */
23815 if (!function_definition_p
23816 && (decl == error_mark_node
23817 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23818 cp_parser_skip_to_end_of_block_or_statement (parser);
23820 out:
23821 pop_deferring_access_checks ();
23823 /* Clear any current qualification; whatever comes next is the start
23824 of something new. */
23825 parser->scope = NULL_TREE;
23826 parser->qualifying_scope = NULL_TREE;
23827 parser->object_scope = NULL_TREE;
23829 return decl;
23832 /* Parse a cast-expression that is not the operand of a unary "&". */
23834 static tree
23835 cp_parser_simple_cast_expression (cp_parser *parser)
23837 return cp_parser_cast_expression (parser, /*address_p=*/false,
23838 /*cast_p=*/false, /*decltype*/false, NULL);
23841 /* Parse a functional cast to TYPE. Returns an expression
23842 representing the cast. */
23844 static tree
23845 cp_parser_functional_cast (cp_parser* parser, tree type)
23847 vec<tree, va_gc> *vec;
23848 tree expression_list;
23849 tree cast;
23850 bool nonconst_p;
23852 if (!type)
23853 type = error_mark_node;
23855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23857 cp_lexer_set_source_position (parser->lexer);
23858 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23859 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23860 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23861 if (TREE_CODE (type) == TYPE_DECL)
23862 type = TREE_TYPE (type);
23863 return finish_compound_literal (type, expression_list,
23864 tf_warning_or_error);
23868 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23869 /*cast_p=*/true,
23870 /*allow_expansion_p=*/true,
23871 /*non_constant_p=*/NULL);
23872 if (vec == NULL)
23873 expression_list = error_mark_node;
23874 else
23876 expression_list = build_tree_list_vec (vec);
23877 release_tree_vector (vec);
23880 cast = build_functional_cast (type, expression_list,
23881 tf_warning_or_error);
23882 /* [expr.const]/1: In an integral constant expression "only type
23883 conversions to integral or enumeration type can be used". */
23884 if (TREE_CODE (type) == TYPE_DECL)
23885 type = TREE_TYPE (type);
23886 if (cast != error_mark_node
23887 && !cast_valid_in_integral_constant_expression_p (type)
23888 && cp_parser_non_integral_constant_expression (parser,
23889 NIC_CONSTRUCTOR))
23890 return error_mark_node;
23891 return cast;
23894 /* Save the tokens that make up the body of a member function defined
23895 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23896 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23897 specifiers applied to the declaration. Returns the FUNCTION_DECL
23898 for the member function. */
23900 static tree
23901 cp_parser_save_member_function_body (cp_parser* parser,
23902 cp_decl_specifier_seq *decl_specifiers,
23903 cp_declarator *declarator,
23904 tree attributes)
23906 cp_token *first;
23907 cp_token *last;
23908 tree fn;
23910 /* Create the FUNCTION_DECL. */
23911 fn = grokmethod (decl_specifiers, declarator, attributes);
23912 cp_finalize_omp_declare_simd (parser, fn);
23913 /* If something went badly wrong, bail out now. */
23914 if (fn == error_mark_node)
23916 /* If there's a function-body, skip it. */
23917 if (cp_parser_token_starts_function_definition_p
23918 (cp_lexer_peek_token (parser->lexer)))
23919 cp_parser_skip_to_end_of_block_or_statement (parser);
23920 return error_mark_node;
23923 /* Remember it, if there default args to post process. */
23924 cp_parser_save_default_args (parser, fn);
23926 /* Save away the tokens that make up the body of the
23927 function. */
23928 first = parser->lexer->next_token;
23929 /* Handle function try blocks. */
23930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23931 cp_lexer_consume_token (parser->lexer);
23932 /* We can have braced-init-list mem-initializers before the fn body. */
23933 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23935 cp_lexer_consume_token (parser->lexer);
23936 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23938 /* cache_group will stop after an un-nested { } pair, too. */
23939 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23940 break;
23942 /* variadic mem-inits have ... after the ')'. */
23943 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23944 cp_lexer_consume_token (parser->lexer);
23947 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23948 /* Handle function try blocks. */
23949 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23950 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23951 last = parser->lexer->next_token;
23953 /* Save away the inline definition; we will process it when the
23954 class is complete. */
23955 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23956 DECL_PENDING_INLINE_P (fn) = 1;
23958 /* We need to know that this was defined in the class, so that
23959 friend templates are handled correctly. */
23960 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23962 /* Add FN to the queue of functions to be parsed later. */
23963 vec_safe_push (unparsed_funs_with_definitions, fn);
23965 return fn;
23968 /* Save the tokens that make up the in-class initializer for a non-static
23969 data member. Returns a DEFAULT_ARG. */
23971 static tree
23972 cp_parser_save_nsdmi (cp_parser* parser)
23974 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23977 /* Parse a template-argument-list, as well as the trailing ">" (but
23978 not the opening "<"). See cp_parser_template_argument_list for the
23979 return value. */
23981 static tree
23982 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23984 tree arguments;
23985 tree saved_scope;
23986 tree saved_qualifying_scope;
23987 tree saved_object_scope;
23988 bool saved_greater_than_is_operator_p;
23989 int saved_unevaluated_operand;
23990 int saved_inhibit_evaluation_warnings;
23992 /* [temp.names]
23994 When parsing a template-id, the first non-nested `>' is taken as
23995 the end of the template-argument-list rather than a greater-than
23996 operator. */
23997 saved_greater_than_is_operator_p
23998 = parser->greater_than_is_operator_p;
23999 parser->greater_than_is_operator_p = false;
24000 /* Parsing the argument list may modify SCOPE, so we save it
24001 here. */
24002 saved_scope = parser->scope;
24003 saved_qualifying_scope = parser->qualifying_scope;
24004 saved_object_scope = parser->object_scope;
24005 /* We need to evaluate the template arguments, even though this
24006 template-id may be nested within a "sizeof". */
24007 saved_unevaluated_operand = cp_unevaluated_operand;
24008 cp_unevaluated_operand = 0;
24009 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24010 c_inhibit_evaluation_warnings = 0;
24011 /* Parse the template-argument-list itself. */
24012 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24013 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24014 arguments = NULL_TREE;
24015 else
24016 arguments = cp_parser_template_argument_list (parser);
24017 /* Look for the `>' that ends the template-argument-list. If we find
24018 a '>>' instead, it's probably just a typo. */
24019 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24021 if (cxx_dialect != cxx98)
24023 /* In C++0x, a `>>' in a template argument list or cast
24024 expression is considered to be two separate `>'
24025 tokens. So, change the current token to a `>', but don't
24026 consume it: it will be consumed later when the outer
24027 template argument list (or cast expression) is parsed.
24028 Note that this replacement of `>' for `>>' is necessary
24029 even if we are parsing tentatively: in the tentative
24030 case, after calling
24031 cp_parser_enclosed_template_argument_list we will always
24032 throw away all of the template arguments and the first
24033 closing `>', either because the template argument list
24034 was erroneous or because we are replacing those tokens
24035 with a CPP_TEMPLATE_ID token. The second `>' (which will
24036 not have been thrown away) is needed either to close an
24037 outer template argument list or to complete a new-style
24038 cast. */
24039 cp_token *token = cp_lexer_peek_token (parser->lexer);
24040 token->type = CPP_GREATER;
24042 else if (!saved_greater_than_is_operator_p)
24044 /* If we're in a nested template argument list, the '>>' has
24045 to be a typo for '> >'. We emit the error message, but we
24046 continue parsing and we push a '>' as next token, so that
24047 the argument list will be parsed correctly. Note that the
24048 global source location is still on the token before the
24049 '>>', so we need to say explicitly where we want it. */
24050 cp_token *token = cp_lexer_peek_token (parser->lexer);
24051 error_at (token->location, "%<>>%> should be %<> >%> "
24052 "within a nested template argument list");
24054 token->type = CPP_GREATER;
24056 else
24058 /* If this is not a nested template argument list, the '>>'
24059 is a typo for '>'. Emit an error message and continue.
24060 Same deal about the token location, but here we can get it
24061 right by consuming the '>>' before issuing the diagnostic. */
24062 cp_token *token = cp_lexer_consume_token (parser->lexer);
24063 error_at (token->location,
24064 "spurious %<>>%>, use %<>%> to terminate "
24065 "a template argument list");
24068 else
24069 cp_parser_skip_to_end_of_template_parameter_list (parser);
24070 /* The `>' token might be a greater-than operator again now. */
24071 parser->greater_than_is_operator_p
24072 = saved_greater_than_is_operator_p;
24073 /* Restore the SAVED_SCOPE. */
24074 parser->scope = saved_scope;
24075 parser->qualifying_scope = saved_qualifying_scope;
24076 parser->object_scope = saved_object_scope;
24077 cp_unevaluated_operand = saved_unevaluated_operand;
24078 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24080 return arguments;
24083 /* MEMBER_FUNCTION is a member function, or a friend. If default
24084 arguments, or the body of the function have not yet been parsed,
24085 parse them now. */
24087 static void
24088 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24090 timevar_push (TV_PARSE_INMETH);
24091 /* If this member is a template, get the underlying
24092 FUNCTION_DECL. */
24093 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24094 member_function = DECL_TEMPLATE_RESULT (member_function);
24096 /* There should not be any class definitions in progress at this
24097 point; the bodies of members are only parsed outside of all class
24098 definitions. */
24099 gcc_assert (parser->num_classes_being_defined == 0);
24100 /* While we're parsing the member functions we might encounter more
24101 classes. We want to handle them right away, but we don't want
24102 them getting mixed up with functions that are currently in the
24103 queue. */
24104 push_unparsed_function_queues (parser);
24106 /* Make sure that any template parameters are in scope. */
24107 maybe_begin_member_template_processing (member_function);
24109 /* If the body of the function has not yet been parsed, parse it
24110 now. */
24111 if (DECL_PENDING_INLINE_P (member_function))
24113 tree function_scope;
24114 cp_token_cache *tokens;
24116 /* The function is no longer pending; we are processing it. */
24117 tokens = DECL_PENDING_INLINE_INFO (member_function);
24118 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24119 DECL_PENDING_INLINE_P (member_function) = 0;
24121 /* If this is a local class, enter the scope of the containing
24122 function. */
24123 function_scope = current_function_decl;
24124 if (function_scope)
24125 push_function_context ();
24127 /* Push the body of the function onto the lexer stack. */
24128 cp_parser_push_lexer_for_tokens (parser, tokens);
24130 /* Let the front end know that we going to be defining this
24131 function. */
24132 start_preparsed_function (member_function, NULL_TREE,
24133 SF_PRE_PARSED | SF_INCLASS_INLINE);
24135 /* Don't do access checking if it is a templated function. */
24136 if (processing_template_decl)
24137 push_deferring_access_checks (dk_no_check);
24139 /* #pragma omp declare reduction needs special parsing. */
24140 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24142 parser->lexer->in_pragma = true;
24143 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24144 finish_function (/*inline*/2);
24145 cp_check_omp_declare_reduction (member_function);
24147 else
24148 /* Now, parse the body of the function. */
24149 cp_parser_function_definition_after_declarator (parser,
24150 /*inline_p=*/true);
24152 if (processing_template_decl)
24153 pop_deferring_access_checks ();
24155 /* Leave the scope of the containing function. */
24156 if (function_scope)
24157 pop_function_context ();
24158 cp_parser_pop_lexer (parser);
24161 /* Remove any template parameters from the symbol table. */
24162 maybe_end_member_template_processing ();
24164 /* Restore the queue. */
24165 pop_unparsed_function_queues (parser);
24166 timevar_pop (TV_PARSE_INMETH);
24169 /* If DECL contains any default args, remember it on the unparsed
24170 functions queue. */
24172 static void
24173 cp_parser_save_default_args (cp_parser* parser, tree decl)
24175 tree probe;
24177 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24178 probe;
24179 probe = TREE_CHAIN (probe))
24180 if (TREE_PURPOSE (probe))
24182 cp_default_arg_entry entry = {current_class_type, decl};
24183 vec_safe_push (unparsed_funs_with_default_args, entry);
24184 break;
24188 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24189 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24190 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24191 from the parameter-type-list. */
24193 static tree
24194 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24195 tree default_arg, tree parmtype)
24197 cp_token_cache *tokens;
24198 tree parsed_arg;
24199 bool dummy;
24201 if (default_arg == error_mark_node)
24202 return error_mark_node;
24204 /* Push the saved tokens for the default argument onto the parser's
24205 lexer stack. */
24206 tokens = DEFARG_TOKENS (default_arg);
24207 cp_parser_push_lexer_for_tokens (parser, tokens);
24209 start_lambda_scope (decl);
24211 /* Parse the default argument. */
24212 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24213 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24216 finish_lambda_scope ();
24218 if (parsed_arg == error_mark_node)
24219 cp_parser_skip_to_end_of_statement (parser);
24221 if (!processing_template_decl)
24223 /* In a non-template class, check conversions now. In a template,
24224 we'll wait and instantiate these as needed. */
24225 if (TREE_CODE (decl) == PARM_DECL)
24226 parsed_arg = check_default_argument (parmtype, parsed_arg,
24227 tf_warning_or_error);
24228 else
24229 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24232 /* If the token stream has not been completely used up, then
24233 there was extra junk after the end of the default
24234 argument. */
24235 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24237 if (TREE_CODE (decl) == PARM_DECL)
24238 cp_parser_error (parser, "expected %<,%>");
24239 else
24240 cp_parser_error (parser, "expected %<;%>");
24243 /* Revert to the main lexer. */
24244 cp_parser_pop_lexer (parser);
24246 return parsed_arg;
24249 /* FIELD is a non-static data member with an initializer which we saved for
24250 later; parse it now. */
24252 static void
24253 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24255 tree def;
24257 maybe_begin_member_template_processing (field);
24259 push_unparsed_function_queues (parser);
24260 def = cp_parser_late_parse_one_default_arg (parser, field,
24261 DECL_INITIAL (field),
24262 NULL_TREE);
24263 pop_unparsed_function_queues (parser);
24265 maybe_end_member_template_processing ();
24267 DECL_INITIAL (field) = def;
24270 /* FN is a FUNCTION_DECL which may contains a parameter with an
24271 unparsed DEFAULT_ARG. Parse the default args now. This function
24272 assumes that the current scope is the scope in which the default
24273 argument should be processed. */
24275 static void
24276 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24278 bool saved_local_variables_forbidden_p;
24279 tree parm, parmdecl;
24281 /* While we're parsing the default args, we might (due to the
24282 statement expression extension) encounter more classes. We want
24283 to handle them right away, but we don't want them getting mixed
24284 up with default args that are currently in the queue. */
24285 push_unparsed_function_queues (parser);
24287 /* Local variable names (and the `this' keyword) may not appear
24288 in a default argument. */
24289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24290 parser->local_variables_forbidden_p = true;
24292 push_defarg_context (fn);
24294 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24295 parmdecl = DECL_ARGUMENTS (fn);
24296 parm && parm != void_list_node;
24297 parm = TREE_CHAIN (parm),
24298 parmdecl = DECL_CHAIN (parmdecl))
24300 tree default_arg = TREE_PURPOSE (parm);
24301 tree parsed_arg;
24302 vec<tree, va_gc> *insts;
24303 tree copy;
24304 unsigned ix;
24306 if (!default_arg)
24307 continue;
24309 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24310 /* This can happen for a friend declaration for a function
24311 already declared with default arguments. */
24312 continue;
24314 parsed_arg
24315 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24316 default_arg,
24317 TREE_VALUE (parm));
24318 if (parsed_arg == error_mark_node)
24320 continue;
24323 TREE_PURPOSE (parm) = parsed_arg;
24325 /* Update any instantiations we've already created. */
24326 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24327 vec_safe_iterate (insts, ix, &copy); ix++)
24328 TREE_PURPOSE (copy) = parsed_arg;
24331 pop_defarg_context ();
24333 /* Make sure no default arg is missing. */
24334 check_default_args (fn);
24336 /* Restore the state of local_variables_forbidden_p. */
24337 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24339 /* Restore the queue. */
24340 pop_unparsed_function_queues (parser);
24343 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24345 sizeof ... ( identifier )
24347 where the 'sizeof' token has already been consumed. */
24349 static tree
24350 cp_parser_sizeof_pack (cp_parser *parser)
24352 /* Consume the `...'. */
24353 cp_lexer_consume_token (parser->lexer);
24354 maybe_warn_variadic_templates ();
24356 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24357 if (paren)
24358 cp_lexer_consume_token (parser->lexer);
24359 else
24360 permerror (cp_lexer_peek_token (parser->lexer)->location,
24361 "%<sizeof...%> argument must be surrounded by parentheses");
24363 cp_token *token = cp_lexer_peek_token (parser->lexer);
24364 tree name = cp_parser_identifier (parser);
24365 if (name == error_mark_node)
24366 return error_mark_node;
24367 /* The name is not qualified. */
24368 parser->scope = NULL_TREE;
24369 parser->qualifying_scope = NULL_TREE;
24370 parser->object_scope = NULL_TREE;
24371 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24372 if (expr == error_mark_node)
24373 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24374 token->location);
24375 if (TREE_CODE (expr) == TYPE_DECL)
24376 expr = TREE_TYPE (expr);
24377 else if (TREE_CODE (expr) == CONST_DECL)
24378 expr = DECL_INITIAL (expr);
24379 expr = make_pack_expansion (expr);
24381 if (paren)
24382 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24384 return expr;
24387 /* Parse the operand of `sizeof' (or a similar operator). Returns
24388 either a TYPE or an expression, depending on the form of the
24389 input. The KEYWORD indicates which kind of expression we have
24390 encountered. */
24392 static tree
24393 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24395 tree expr = NULL_TREE;
24396 const char *saved_message;
24397 char *tmp;
24398 bool saved_integral_constant_expression_p;
24399 bool saved_non_integral_constant_expression_p;
24401 /* If it's a `...', then we are computing the length of a parameter
24402 pack. */
24403 if (keyword == RID_SIZEOF
24404 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24405 return cp_parser_sizeof_pack (parser);
24407 /* Types cannot be defined in a `sizeof' expression. Save away the
24408 old message. */
24409 saved_message = parser->type_definition_forbidden_message;
24410 /* And create the new one. */
24411 tmp = concat ("types may not be defined in %<",
24412 IDENTIFIER_POINTER (ridpointers[keyword]),
24413 "%> expressions", NULL);
24414 parser->type_definition_forbidden_message = tmp;
24416 /* The restrictions on constant-expressions do not apply inside
24417 sizeof expressions. */
24418 saved_integral_constant_expression_p
24419 = parser->integral_constant_expression_p;
24420 saved_non_integral_constant_expression_p
24421 = parser->non_integral_constant_expression_p;
24422 parser->integral_constant_expression_p = false;
24424 /* Do not actually evaluate the expression. */
24425 ++cp_unevaluated_operand;
24426 ++c_inhibit_evaluation_warnings;
24427 /* If it's a `(', then we might be looking at the type-id
24428 construction. */
24429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24431 tree type = NULL_TREE;
24433 /* We can't be sure yet whether we're looking at a type-id or an
24434 expression. */
24435 cp_parser_parse_tentatively (parser);
24436 /* Note: as a GNU Extension, compound literals are considered
24437 postfix-expressions as they are in C99, so they are valid
24438 arguments to sizeof. See comment in cp_parser_cast_expression
24439 for details. */
24440 if (cp_parser_compound_literal_p (parser))
24441 cp_parser_simulate_error (parser);
24442 else
24444 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24445 parser->in_type_id_in_expr_p = true;
24446 /* Look for the type-id. */
24447 type = cp_parser_type_id (parser);
24448 /* Look for the closing `)'. */
24449 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24450 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24453 /* If all went well, then we're done. */
24454 if (cp_parser_parse_definitely (parser))
24456 cp_decl_specifier_seq decl_specs;
24458 /* Build a trivial decl-specifier-seq. */
24459 clear_decl_specs (&decl_specs);
24460 decl_specs.type = type;
24462 /* Call grokdeclarator to figure out what type this is. */
24463 expr = grokdeclarator (NULL,
24464 &decl_specs,
24465 TYPENAME,
24466 /*initialized=*/0,
24467 /*attrlist=*/NULL);
24471 /* If the type-id production did not work out, then we must be
24472 looking at the unary-expression production. */
24473 if (!expr)
24474 expr = cp_parser_unary_expression (parser);
24476 /* Go back to evaluating expressions. */
24477 --cp_unevaluated_operand;
24478 --c_inhibit_evaluation_warnings;
24480 /* Free the message we created. */
24481 free (tmp);
24482 /* And restore the old one. */
24483 parser->type_definition_forbidden_message = saved_message;
24484 parser->integral_constant_expression_p
24485 = saved_integral_constant_expression_p;
24486 parser->non_integral_constant_expression_p
24487 = saved_non_integral_constant_expression_p;
24489 return expr;
24492 /* If the current declaration has no declarator, return true. */
24494 static bool
24495 cp_parser_declares_only_class_p (cp_parser *parser)
24497 /* If the next token is a `;' or a `,' then there is no
24498 declarator. */
24499 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24500 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24503 /* Update the DECL_SPECS to reflect the storage class indicated by
24504 KEYWORD. */
24506 static void
24507 cp_parser_set_storage_class (cp_parser *parser,
24508 cp_decl_specifier_seq *decl_specs,
24509 enum rid keyword,
24510 cp_token *token)
24512 cp_storage_class storage_class;
24514 if (parser->in_unbraced_linkage_specification_p)
24516 error_at (token->location, "invalid use of %qD in linkage specification",
24517 ridpointers[keyword]);
24518 return;
24520 else if (decl_specs->storage_class != sc_none)
24522 decl_specs->conflicting_specifiers_p = true;
24523 return;
24526 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24527 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24528 && decl_specs->gnu_thread_keyword_p)
24530 pedwarn (decl_specs->locations[ds_thread], 0,
24531 "%<__thread%> before %qD", ridpointers[keyword]);
24534 switch (keyword)
24536 case RID_AUTO:
24537 storage_class = sc_auto;
24538 break;
24539 case RID_REGISTER:
24540 storage_class = sc_register;
24541 break;
24542 case RID_STATIC:
24543 storage_class = sc_static;
24544 break;
24545 case RID_EXTERN:
24546 storage_class = sc_extern;
24547 break;
24548 case RID_MUTABLE:
24549 storage_class = sc_mutable;
24550 break;
24551 default:
24552 gcc_unreachable ();
24554 decl_specs->storage_class = storage_class;
24555 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24557 /* A storage class specifier cannot be applied alongside a typedef
24558 specifier. If there is a typedef specifier present then set
24559 conflicting_specifiers_p which will trigger an error later
24560 on in grokdeclarator. */
24561 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24562 decl_specs->conflicting_specifiers_p = true;
24565 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24566 is true, the type is a class or enum definition. */
24568 static void
24569 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24570 tree type_spec,
24571 cp_token *token,
24572 bool type_definition_p)
24574 decl_specs->any_specifiers_p = true;
24576 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24577 (with, for example, in "typedef int wchar_t;") we remember that
24578 this is what happened. In system headers, we ignore these
24579 declarations so that G++ can work with system headers that are not
24580 C++-safe. */
24581 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24582 && !type_definition_p
24583 && (type_spec == boolean_type_node
24584 || type_spec == char16_type_node
24585 || type_spec == char32_type_node
24586 || type_spec == wchar_type_node)
24587 && (decl_specs->type
24588 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24589 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24590 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24591 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24593 decl_specs->redefined_builtin_type = type_spec;
24594 set_and_check_decl_spec_loc (decl_specs,
24595 ds_redefined_builtin_type_spec,
24596 token);
24597 if (!decl_specs->type)
24599 decl_specs->type = type_spec;
24600 decl_specs->type_definition_p = false;
24601 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24604 else if (decl_specs->type)
24605 decl_specs->multiple_types_p = true;
24606 else
24608 decl_specs->type = type_spec;
24609 decl_specs->type_definition_p = type_definition_p;
24610 decl_specs->redefined_builtin_type = NULL_TREE;
24611 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24615 /* True iff TOKEN is the GNU keyword __thread. */
24617 static bool
24618 token_is__thread (cp_token *token)
24620 gcc_assert (token->keyword == RID_THREAD);
24621 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24624 /* Set the location for a declarator specifier and check if it is
24625 duplicated.
24627 DECL_SPECS is the sequence of declarator specifiers onto which to
24628 set the location.
24630 DS is the single declarator specifier to set which location is to
24631 be set onto the existing sequence of declarators.
24633 LOCATION is the location for the declarator specifier to
24634 consider. */
24636 static void
24637 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24638 cp_decl_spec ds, cp_token *token)
24640 gcc_assert (ds < ds_last);
24642 if (decl_specs == NULL)
24643 return;
24645 source_location location = token->location;
24647 if (decl_specs->locations[ds] == 0)
24649 decl_specs->locations[ds] = location;
24650 if (ds == ds_thread)
24651 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24653 else
24655 if (ds == ds_long)
24657 if (decl_specs->locations[ds_long_long] != 0)
24658 error_at (location,
24659 "%<long long long%> is too long for GCC");
24660 else
24662 decl_specs->locations[ds_long_long] = location;
24663 pedwarn_cxx98 (location,
24664 OPT_Wlong_long,
24665 "ISO C++ 1998 does not support %<long long%>");
24668 else if (ds == ds_thread)
24670 bool gnu = token_is__thread (token);
24671 if (gnu != decl_specs->gnu_thread_keyword_p)
24672 error_at (location,
24673 "both %<__thread%> and %<thread_local%> specified");
24674 else
24675 error_at (location, "duplicate %qD", token->u.value);
24677 else
24679 static const char *const decl_spec_names[] = {
24680 "signed",
24681 "unsigned",
24682 "short",
24683 "long",
24684 "const",
24685 "volatile",
24686 "restrict",
24687 "inline",
24688 "virtual",
24689 "explicit",
24690 "friend",
24691 "typedef",
24692 "using",
24693 "constexpr",
24694 "__complex"
24696 error_at (location,
24697 "duplicate %qs", decl_spec_names[ds]);
24702 /* Return true iff the declarator specifier DS is present in the
24703 sequence of declarator specifiers DECL_SPECS. */
24705 bool
24706 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24707 cp_decl_spec ds)
24709 gcc_assert (ds < ds_last);
24711 if (decl_specs == NULL)
24712 return false;
24714 return decl_specs->locations[ds] != 0;
24717 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24718 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24720 static bool
24721 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24723 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24726 /* Issue an error message indicating that TOKEN_DESC was expected.
24727 If KEYWORD is true, it indicated this function is called by
24728 cp_parser_require_keword and the required token can only be
24729 a indicated keyword. */
24731 static void
24732 cp_parser_required_error (cp_parser *parser,
24733 required_token token_desc,
24734 bool keyword)
24736 switch (token_desc)
24738 case RT_NEW:
24739 cp_parser_error (parser, "expected %<new%>");
24740 return;
24741 case RT_DELETE:
24742 cp_parser_error (parser, "expected %<delete%>");
24743 return;
24744 case RT_RETURN:
24745 cp_parser_error (parser, "expected %<return%>");
24746 return;
24747 case RT_WHILE:
24748 cp_parser_error (parser, "expected %<while%>");
24749 return;
24750 case RT_EXTERN:
24751 cp_parser_error (parser, "expected %<extern%>");
24752 return;
24753 case RT_STATIC_ASSERT:
24754 cp_parser_error (parser, "expected %<static_assert%>");
24755 return;
24756 case RT_DECLTYPE:
24757 cp_parser_error (parser, "expected %<decltype%>");
24758 return;
24759 case RT_OPERATOR:
24760 cp_parser_error (parser, "expected %<operator%>");
24761 return;
24762 case RT_CLASS:
24763 cp_parser_error (parser, "expected %<class%>");
24764 return;
24765 case RT_TEMPLATE:
24766 cp_parser_error (parser, "expected %<template%>");
24767 return;
24768 case RT_NAMESPACE:
24769 cp_parser_error (parser, "expected %<namespace%>");
24770 return;
24771 case RT_USING:
24772 cp_parser_error (parser, "expected %<using%>");
24773 return;
24774 case RT_ASM:
24775 cp_parser_error (parser, "expected %<asm%>");
24776 return;
24777 case RT_TRY:
24778 cp_parser_error (parser, "expected %<try%>");
24779 return;
24780 case RT_CATCH:
24781 cp_parser_error (parser, "expected %<catch%>");
24782 return;
24783 case RT_THROW:
24784 cp_parser_error (parser, "expected %<throw%>");
24785 return;
24786 case RT_LABEL:
24787 cp_parser_error (parser, "expected %<__label__%>");
24788 return;
24789 case RT_AT_TRY:
24790 cp_parser_error (parser, "expected %<@try%>");
24791 return;
24792 case RT_AT_SYNCHRONIZED:
24793 cp_parser_error (parser, "expected %<@synchronized%>");
24794 return;
24795 case RT_AT_THROW:
24796 cp_parser_error (parser, "expected %<@throw%>");
24797 return;
24798 case RT_TRANSACTION_ATOMIC:
24799 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24800 return;
24801 case RT_TRANSACTION_RELAXED:
24802 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24803 return;
24804 default:
24805 break;
24807 if (!keyword)
24809 switch (token_desc)
24811 case RT_SEMICOLON:
24812 cp_parser_error (parser, "expected %<;%>");
24813 return;
24814 case RT_OPEN_PAREN:
24815 cp_parser_error (parser, "expected %<(%>");
24816 return;
24817 case RT_CLOSE_BRACE:
24818 cp_parser_error (parser, "expected %<}%>");
24819 return;
24820 case RT_OPEN_BRACE:
24821 cp_parser_error (parser, "expected %<{%>");
24822 return;
24823 case RT_CLOSE_SQUARE:
24824 cp_parser_error (parser, "expected %<]%>");
24825 return;
24826 case RT_OPEN_SQUARE:
24827 cp_parser_error (parser, "expected %<[%>");
24828 return;
24829 case RT_COMMA:
24830 cp_parser_error (parser, "expected %<,%>");
24831 return;
24832 case RT_SCOPE:
24833 cp_parser_error (parser, "expected %<::%>");
24834 return;
24835 case RT_LESS:
24836 cp_parser_error (parser, "expected %<<%>");
24837 return;
24838 case RT_GREATER:
24839 cp_parser_error (parser, "expected %<>%>");
24840 return;
24841 case RT_EQ:
24842 cp_parser_error (parser, "expected %<=%>");
24843 return;
24844 case RT_ELLIPSIS:
24845 cp_parser_error (parser, "expected %<...%>");
24846 return;
24847 case RT_MULT:
24848 cp_parser_error (parser, "expected %<*%>");
24849 return;
24850 case RT_COMPL:
24851 cp_parser_error (parser, "expected %<~%>");
24852 return;
24853 case RT_COLON:
24854 cp_parser_error (parser, "expected %<:%>");
24855 return;
24856 case RT_COLON_SCOPE:
24857 cp_parser_error (parser, "expected %<:%> or %<::%>");
24858 return;
24859 case RT_CLOSE_PAREN:
24860 cp_parser_error (parser, "expected %<)%>");
24861 return;
24862 case RT_COMMA_CLOSE_PAREN:
24863 cp_parser_error (parser, "expected %<,%> or %<)%>");
24864 return;
24865 case RT_PRAGMA_EOL:
24866 cp_parser_error (parser, "expected end of line");
24867 return;
24868 case RT_NAME:
24869 cp_parser_error (parser, "expected identifier");
24870 return;
24871 case RT_SELECT:
24872 cp_parser_error (parser, "expected selection-statement");
24873 return;
24874 case RT_INTERATION:
24875 cp_parser_error (parser, "expected iteration-statement");
24876 return;
24877 case RT_JUMP:
24878 cp_parser_error (parser, "expected jump-statement");
24879 return;
24880 case RT_CLASS_KEY:
24881 cp_parser_error (parser, "expected class-key");
24882 return;
24883 case RT_CLASS_TYPENAME_TEMPLATE:
24884 cp_parser_error (parser,
24885 "expected %<class%>, %<typename%>, or %<template%>");
24886 return;
24887 default:
24888 gcc_unreachable ();
24891 else
24892 gcc_unreachable ();
24897 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24898 issue an error message indicating that TOKEN_DESC was expected.
24900 Returns the token consumed, if the token had the appropriate type.
24901 Otherwise, returns NULL. */
24903 static cp_token *
24904 cp_parser_require (cp_parser* parser,
24905 enum cpp_ttype type,
24906 required_token token_desc)
24908 if (cp_lexer_next_token_is (parser->lexer, type))
24909 return cp_lexer_consume_token (parser->lexer);
24910 else
24912 /* Output the MESSAGE -- unless we're parsing tentatively. */
24913 if (!cp_parser_simulate_error (parser))
24914 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24915 return NULL;
24919 /* An error message is produced if the next token is not '>'.
24920 All further tokens are skipped until the desired token is
24921 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24923 static void
24924 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24926 /* Current level of '< ... >'. */
24927 unsigned level = 0;
24928 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24929 unsigned nesting_depth = 0;
24931 /* Are we ready, yet? If not, issue error message. */
24932 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24933 return;
24935 /* Skip tokens until the desired token is found. */
24936 while (true)
24938 /* Peek at the next token. */
24939 switch (cp_lexer_peek_token (parser->lexer)->type)
24941 case CPP_LESS:
24942 if (!nesting_depth)
24943 ++level;
24944 break;
24946 case CPP_RSHIFT:
24947 if (cxx_dialect == cxx98)
24948 /* C++0x views the `>>' operator as two `>' tokens, but
24949 C++98 does not. */
24950 break;
24951 else if (!nesting_depth && level-- == 0)
24953 /* We've hit a `>>' where the first `>' closes the
24954 template argument list, and the second `>' is
24955 spurious. Just consume the `>>' and stop; we've
24956 already produced at least one error. */
24957 cp_lexer_consume_token (parser->lexer);
24958 return;
24960 /* Fall through for C++0x, so we handle the second `>' in
24961 the `>>'. */
24963 case CPP_GREATER:
24964 if (!nesting_depth && level-- == 0)
24966 /* We've reached the token we want, consume it and stop. */
24967 cp_lexer_consume_token (parser->lexer);
24968 return;
24970 break;
24972 case CPP_OPEN_PAREN:
24973 case CPP_OPEN_SQUARE:
24974 ++nesting_depth;
24975 break;
24977 case CPP_CLOSE_PAREN:
24978 case CPP_CLOSE_SQUARE:
24979 if (nesting_depth-- == 0)
24980 return;
24981 break;
24983 case CPP_EOF:
24984 case CPP_PRAGMA_EOL:
24985 case CPP_SEMICOLON:
24986 case CPP_OPEN_BRACE:
24987 case CPP_CLOSE_BRACE:
24988 /* The '>' was probably forgotten, don't look further. */
24989 return;
24991 default:
24992 break;
24995 /* Consume this token. */
24996 cp_lexer_consume_token (parser->lexer);
25000 /* If the next token is the indicated keyword, consume it. Otherwise,
25001 issue an error message indicating that TOKEN_DESC was expected.
25003 Returns the token consumed, if the token had the appropriate type.
25004 Otherwise, returns NULL. */
25006 static cp_token *
25007 cp_parser_require_keyword (cp_parser* parser,
25008 enum rid keyword,
25009 required_token token_desc)
25011 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25013 if (token && token->keyword != keyword)
25015 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25016 return NULL;
25019 return token;
25022 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25023 function-definition. */
25025 static bool
25026 cp_parser_token_starts_function_definition_p (cp_token* token)
25028 return (/* An ordinary function-body begins with an `{'. */
25029 token->type == CPP_OPEN_BRACE
25030 /* A ctor-initializer begins with a `:'. */
25031 || token->type == CPP_COLON
25032 /* A function-try-block begins with `try'. */
25033 || token->keyword == RID_TRY
25034 /* A function-transaction-block begins with `__transaction_atomic'
25035 or `__transaction_relaxed'. */
25036 || token->keyword == RID_TRANSACTION_ATOMIC
25037 || token->keyword == RID_TRANSACTION_RELAXED
25038 /* The named return value extension begins with `return'. */
25039 || token->keyword == RID_RETURN);
25042 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25043 definition. */
25045 static bool
25046 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25048 cp_token *token;
25050 token = cp_lexer_peek_token (parser->lexer);
25051 return (token->type == CPP_OPEN_BRACE
25052 || (token->type == CPP_COLON
25053 && !parser->colon_doesnt_start_class_def_p));
25056 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25057 C++0x) ending a template-argument. */
25059 static bool
25060 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25062 cp_token *token;
25064 token = cp_lexer_peek_token (parser->lexer);
25065 return (token->type == CPP_COMMA
25066 || token->type == CPP_GREATER
25067 || token->type == CPP_ELLIPSIS
25068 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25071 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25072 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25074 static bool
25075 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25076 size_t n)
25078 cp_token *token;
25080 token = cp_lexer_peek_nth_token (parser->lexer, n);
25081 if (token->type == CPP_LESS)
25082 return true;
25083 /* Check for the sequence `<::' in the original code. It would be lexed as
25084 `[:', where `[' is a digraph, and there is no whitespace before
25085 `:'. */
25086 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25088 cp_token *token2;
25089 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25090 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25091 return true;
25093 return false;
25096 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25097 or none_type otherwise. */
25099 static enum tag_types
25100 cp_parser_token_is_class_key (cp_token* token)
25102 switch (token->keyword)
25104 case RID_CLASS:
25105 return class_type;
25106 case RID_STRUCT:
25107 return record_type;
25108 case RID_UNION:
25109 return union_type;
25111 default:
25112 return none_type;
25116 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25117 or none_type otherwise or if the token is null. */
25119 static enum tag_types
25120 cp_parser_token_is_type_parameter_key (cp_token* token)
25122 if (!token)
25123 return none_type;
25125 switch (token->keyword)
25127 case RID_CLASS:
25128 return class_type;
25129 case RID_TYPENAME:
25130 return typename_type;
25132 default:
25133 return none_type;
25137 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25139 static void
25140 cp_parser_check_class_key (enum tag_types class_key, tree type)
25142 if (type == error_mark_node)
25143 return;
25144 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25146 if (permerror (input_location, "%qs tag used in naming %q#T",
25147 class_key == union_type ? "union"
25148 : class_key == record_type ? "struct" : "class",
25149 type))
25150 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25151 "%q#T was previously declared here", type);
25155 /* Issue an error message if DECL is redeclared with different
25156 access than its original declaration [class.access.spec/3].
25157 This applies to nested classes and nested class templates.
25158 [class.mem/1]. */
25160 static void
25161 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25163 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25164 return;
25166 if ((TREE_PRIVATE (decl)
25167 != (current_access_specifier == access_private_node))
25168 || (TREE_PROTECTED (decl)
25169 != (current_access_specifier == access_protected_node)))
25170 error_at (location, "%qD redeclared with different access", decl);
25173 /* Look for the `template' keyword, as a syntactic disambiguator.
25174 Return TRUE iff it is present, in which case it will be
25175 consumed. */
25177 static bool
25178 cp_parser_optional_template_keyword (cp_parser *parser)
25180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25182 /* In C++98 the `template' keyword can only be used within templates;
25183 outside templates the parser can always figure out what is a
25184 template and what is not. In C++11, per the resolution of DR 468,
25185 `template' is allowed in cases where it is not strictly necessary. */
25186 if (!processing_template_decl
25187 && pedantic && cxx_dialect == cxx98)
25189 cp_token *token = cp_lexer_peek_token (parser->lexer);
25190 pedwarn (token->location, OPT_Wpedantic,
25191 "in C++98 %<template%> (as a disambiguator) is only "
25192 "allowed within templates");
25193 /* If this part of the token stream is rescanned, the same
25194 error message would be generated. So, we purge the token
25195 from the stream. */
25196 cp_lexer_purge_token (parser->lexer);
25197 return false;
25199 else
25201 /* Consume the `template' keyword. */
25202 cp_lexer_consume_token (parser->lexer);
25203 return true;
25206 return false;
25209 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25210 set PARSER->SCOPE, and perform other related actions. */
25212 static void
25213 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25215 int i;
25216 struct tree_check *check_value;
25217 deferred_access_check *chk;
25218 vec<deferred_access_check, va_gc> *checks;
25220 /* Get the stored value. */
25221 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25222 /* Perform any access checks that were deferred. */
25223 checks = check_value->checks;
25224 if (checks)
25226 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25227 perform_or_defer_access_check (chk->binfo,
25228 chk->decl,
25229 chk->diag_decl, tf_warning_or_error);
25231 /* Set the scope from the stored value. */
25232 parser->scope = check_value->value;
25233 parser->qualifying_scope = check_value->qualifying_scope;
25234 parser->object_scope = NULL_TREE;
25237 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25238 encounter the end of a block before what we were looking for. */
25240 static bool
25241 cp_parser_cache_group (cp_parser *parser,
25242 enum cpp_ttype end,
25243 unsigned depth)
25245 while (true)
25247 cp_token *token = cp_lexer_peek_token (parser->lexer);
25249 /* Abort a parenthesized expression if we encounter a semicolon. */
25250 if ((end == CPP_CLOSE_PAREN || depth == 0)
25251 && token->type == CPP_SEMICOLON)
25252 return true;
25253 /* If we've reached the end of the file, stop. */
25254 if (token->type == CPP_EOF
25255 || (end != CPP_PRAGMA_EOL
25256 && token->type == CPP_PRAGMA_EOL))
25257 return true;
25258 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25259 /* We've hit the end of an enclosing block, so there's been some
25260 kind of syntax error. */
25261 return true;
25263 /* Consume the token. */
25264 cp_lexer_consume_token (parser->lexer);
25265 /* See if it starts a new group. */
25266 if (token->type == CPP_OPEN_BRACE)
25268 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25269 /* In theory this should probably check end == '}', but
25270 cp_parser_save_member_function_body needs it to exit
25271 after either '}' or ')' when called with ')'. */
25272 if (depth == 0)
25273 return false;
25275 else if (token->type == CPP_OPEN_PAREN)
25277 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25278 if (depth == 0 && end == CPP_CLOSE_PAREN)
25279 return false;
25281 else if (token->type == CPP_PRAGMA)
25282 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25283 else if (token->type == end)
25284 return false;
25288 /* Like above, for caching a default argument or NSDMI. Both of these are
25289 terminated by a non-nested comma, but it can be unclear whether or not a
25290 comma is nested in a template argument list unless we do more parsing.
25291 In order to handle this ambiguity, when we encounter a ',' after a '<'
25292 we try to parse what follows as a parameter-declaration-list (in the
25293 case of a default argument) or a member-declarator (in the case of an
25294 NSDMI). If that succeeds, then we stop caching. */
25296 static tree
25297 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25299 unsigned depth = 0;
25300 int maybe_template_id = 0;
25301 cp_token *first_token;
25302 cp_token *token;
25303 tree default_argument;
25305 /* Add tokens until we have processed the entire default
25306 argument. We add the range [first_token, token). */
25307 first_token = cp_lexer_peek_token (parser->lexer);
25308 if (first_token->type == CPP_OPEN_BRACE)
25310 /* For list-initialization, this is straightforward. */
25311 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25312 token = cp_lexer_peek_token (parser->lexer);
25314 else while (true)
25316 bool done = false;
25318 /* Peek at the next token. */
25319 token = cp_lexer_peek_token (parser->lexer);
25320 /* What we do depends on what token we have. */
25321 switch (token->type)
25323 /* In valid code, a default argument must be
25324 immediately followed by a `,' `)', or `...'. */
25325 case CPP_COMMA:
25326 if (depth == 0 && maybe_template_id)
25328 /* If we've seen a '<', we might be in a
25329 template-argument-list. Until Core issue 325 is
25330 resolved, we don't know how this situation ought
25331 to be handled, so try to DTRT. We check whether
25332 what comes after the comma is a valid parameter
25333 declaration list. If it is, then the comma ends
25334 the default argument; otherwise the default
25335 argument continues. */
25336 bool error = false;
25338 /* Set ITALP so cp_parser_parameter_declaration_list
25339 doesn't decide to commit to this parse. */
25340 bool saved_italp = parser->in_template_argument_list_p;
25341 parser->in_template_argument_list_p = true;
25343 cp_parser_parse_tentatively (parser);
25344 cp_lexer_consume_token (parser->lexer);
25346 if (nsdmi)
25348 int ctor_dtor_or_conv_p;
25349 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25350 &ctor_dtor_or_conv_p,
25351 /*parenthesized_p=*/NULL,
25352 /*member_p=*/true,
25353 /*friend_p=*/false);
25355 else
25357 begin_scope (sk_function_parms, NULL_TREE);
25358 cp_parser_parameter_declaration_list (parser, &error);
25359 pop_bindings_and_leave_scope ();
25361 if (!cp_parser_error_occurred (parser) && !error)
25362 done = true;
25363 cp_parser_abort_tentative_parse (parser);
25365 parser->in_template_argument_list_p = saved_italp;
25366 break;
25368 case CPP_CLOSE_PAREN:
25369 case CPP_ELLIPSIS:
25370 /* If we run into a non-nested `;', `}', or `]',
25371 then the code is invalid -- but the default
25372 argument is certainly over. */
25373 case CPP_SEMICOLON:
25374 case CPP_CLOSE_BRACE:
25375 case CPP_CLOSE_SQUARE:
25376 if (depth == 0
25377 /* Handle correctly int n = sizeof ... ( p ); */
25378 && token->type != CPP_ELLIPSIS)
25379 done = true;
25380 /* Update DEPTH, if necessary. */
25381 else if (token->type == CPP_CLOSE_PAREN
25382 || token->type == CPP_CLOSE_BRACE
25383 || token->type == CPP_CLOSE_SQUARE)
25384 --depth;
25385 break;
25387 case CPP_OPEN_PAREN:
25388 case CPP_OPEN_SQUARE:
25389 case CPP_OPEN_BRACE:
25390 ++depth;
25391 break;
25393 case CPP_LESS:
25394 if (depth == 0)
25395 /* This might be the comparison operator, or it might
25396 start a template argument list. */
25397 ++maybe_template_id;
25398 break;
25400 case CPP_RSHIFT:
25401 if (cxx_dialect == cxx98)
25402 break;
25403 /* Fall through for C++0x, which treats the `>>'
25404 operator like two `>' tokens in certain
25405 cases. */
25407 case CPP_GREATER:
25408 if (depth == 0)
25410 /* This might be an operator, or it might close a
25411 template argument list. But if a previous '<'
25412 started a template argument list, this will have
25413 closed it, so we can't be in one anymore. */
25414 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25415 if (maybe_template_id < 0)
25416 maybe_template_id = 0;
25418 break;
25420 /* If we run out of tokens, issue an error message. */
25421 case CPP_EOF:
25422 case CPP_PRAGMA_EOL:
25423 error_at (token->location, "file ends in default argument");
25424 done = true;
25425 break;
25427 case CPP_NAME:
25428 case CPP_SCOPE:
25429 /* In these cases, we should look for template-ids.
25430 For example, if the default argument is
25431 `X<int, double>()', we need to do name lookup to
25432 figure out whether or not `X' is a template; if
25433 so, the `,' does not end the default argument.
25435 That is not yet done. */
25436 break;
25438 default:
25439 break;
25442 /* If we've reached the end, stop. */
25443 if (done)
25444 break;
25446 /* Add the token to the token block. */
25447 token = cp_lexer_consume_token (parser->lexer);
25450 /* Create a DEFAULT_ARG to represent the unparsed default
25451 argument. */
25452 default_argument = make_node (DEFAULT_ARG);
25453 DEFARG_TOKENS (default_argument)
25454 = cp_token_cache_new (first_token, token);
25455 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25457 return default_argument;
25460 /* Begin parsing tentatively. We always save tokens while parsing
25461 tentatively so that if the tentative parsing fails we can restore the
25462 tokens. */
25464 static void
25465 cp_parser_parse_tentatively (cp_parser* parser)
25467 /* Enter a new parsing context. */
25468 parser->context = cp_parser_context_new (parser->context);
25469 /* Begin saving tokens. */
25470 cp_lexer_save_tokens (parser->lexer);
25471 /* In order to avoid repetitive access control error messages,
25472 access checks are queued up until we are no longer parsing
25473 tentatively. */
25474 push_deferring_access_checks (dk_deferred);
25477 /* Commit to the currently active tentative parse. */
25479 static void
25480 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25482 cp_parser_context *context;
25483 cp_lexer *lexer;
25485 /* Mark all of the levels as committed. */
25486 lexer = parser->lexer;
25487 for (context = parser->context; context->next; context = context->next)
25489 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25490 break;
25491 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25492 while (!cp_lexer_saving_tokens (lexer))
25493 lexer = lexer->next;
25494 cp_lexer_commit_tokens (lexer);
25498 /* Commit to the topmost currently active tentative parse.
25500 Note that this function shouldn't be called when there are
25501 irreversible side-effects while in a tentative state. For
25502 example, we shouldn't create a permanent entry in the symbol
25503 table, or issue an error message that might not apply if the
25504 tentative parse is aborted. */
25506 static void
25507 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25509 cp_parser_context *context = parser->context;
25510 cp_lexer *lexer = parser->lexer;
25512 if (context)
25514 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25515 return;
25516 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25518 while (!cp_lexer_saving_tokens (lexer))
25519 lexer = lexer->next;
25520 cp_lexer_commit_tokens (lexer);
25524 /* Abort the currently active tentative parse. All consumed tokens
25525 will be rolled back, and no diagnostics will be issued. */
25527 static void
25528 cp_parser_abort_tentative_parse (cp_parser* parser)
25530 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25531 || errorcount > 0);
25532 cp_parser_simulate_error (parser);
25533 /* Now, pretend that we want to see if the construct was
25534 successfully parsed. */
25535 cp_parser_parse_definitely (parser);
25538 /* Stop parsing tentatively. If a parse error has occurred, restore the
25539 token stream. Otherwise, commit to the tokens we have consumed.
25540 Returns true if no error occurred; false otherwise. */
25542 static bool
25543 cp_parser_parse_definitely (cp_parser* parser)
25545 bool error_occurred;
25546 cp_parser_context *context;
25548 /* Remember whether or not an error occurred, since we are about to
25549 destroy that information. */
25550 error_occurred = cp_parser_error_occurred (parser);
25551 /* Remove the topmost context from the stack. */
25552 context = parser->context;
25553 parser->context = context->next;
25554 /* If no parse errors occurred, commit to the tentative parse. */
25555 if (!error_occurred)
25557 /* Commit to the tokens read tentatively, unless that was
25558 already done. */
25559 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25560 cp_lexer_commit_tokens (parser->lexer);
25562 pop_to_parent_deferring_access_checks ();
25564 /* Otherwise, if errors occurred, roll back our state so that things
25565 are just as they were before we began the tentative parse. */
25566 else
25568 cp_lexer_rollback_tokens (parser->lexer);
25569 pop_deferring_access_checks ();
25571 /* Add the context to the front of the free list. */
25572 context->next = cp_parser_context_free_list;
25573 cp_parser_context_free_list = context;
25575 return !error_occurred;
25578 /* Returns true if we are parsing tentatively and are not committed to
25579 this tentative parse. */
25581 static bool
25582 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25584 return (cp_parser_parsing_tentatively (parser)
25585 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25588 /* Returns nonzero iff an error has occurred during the most recent
25589 tentative parse. */
25591 static bool
25592 cp_parser_error_occurred (cp_parser* parser)
25594 return (cp_parser_parsing_tentatively (parser)
25595 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25598 /* Returns nonzero if GNU extensions are allowed. */
25600 static bool
25601 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25603 return parser->allow_gnu_extensions_p;
25606 /* Objective-C++ Productions */
25609 /* Parse an Objective-C expression, which feeds into a primary-expression
25610 above.
25612 objc-expression:
25613 objc-message-expression
25614 objc-string-literal
25615 objc-encode-expression
25616 objc-protocol-expression
25617 objc-selector-expression
25619 Returns a tree representation of the expression. */
25621 static tree
25622 cp_parser_objc_expression (cp_parser* parser)
25624 /* Try to figure out what kind of declaration is present. */
25625 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25627 switch (kwd->type)
25629 case CPP_OPEN_SQUARE:
25630 return cp_parser_objc_message_expression (parser);
25632 case CPP_OBJC_STRING:
25633 kwd = cp_lexer_consume_token (parser->lexer);
25634 return objc_build_string_object (kwd->u.value);
25636 case CPP_KEYWORD:
25637 switch (kwd->keyword)
25639 case RID_AT_ENCODE:
25640 return cp_parser_objc_encode_expression (parser);
25642 case RID_AT_PROTOCOL:
25643 return cp_parser_objc_protocol_expression (parser);
25645 case RID_AT_SELECTOR:
25646 return cp_parser_objc_selector_expression (parser);
25648 default:
25649 break;
25651 default:
25652 error_at (kwd->location,
25653 "misplaced %<@%D%> Objective-C++ construct",
25654 kwd->u.value);
25655 cp_parser_skip_to_end_of_block_or_statement (parser);
25658 return error_mark_node;
25661 /* Parse an Objective-C message expression.
25663 objc-message-expression:
25664 [ objc-message-receiver objc-message-args ]
25666 Returns a representation of an Objective-C message. */
25668 static tree
25669 cp_parser_objc_message_expression (cp_parser* parser)
25671 tree receiver, messageargs;
25673 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25674 receiver = cp_parser_objc_message_receiver (parser);
25675 messageargs = cp_parser_objc_message_args (parser);
25676 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25678 return objc_build_message_expr (receiver, messageargs);
25681 /* Parse an objc-message-receiver.
25683 objc-message-receiver:
25684 expression
25685 simple-type-specifier
25687 Returns a representation of the type or expression. */
25689 static tree
25690 cp_parser_objc_message_receiver (cp_parser* parser)
25692 tree rcv;
25694 /* An Objective-C message receiver may be either (1) a type
25695 or (2) an expression. */
25696 cp_parser_parse_tentatively (parser);
25697 rcv = cp_parser_expression (parser);
25699 /* If that worked out, fine. */
25700 if (cp_parser_parse_definitely (parser))
25701 return rcv;
25703 cp_parser_parse_tentatively (parser);
25704 rcv = cp_parser_simple_type_specifier (parser,
25705 /*decl_specs=*/NULL,
25706 CP_PARSER_FLAGS_NONE);
25708 if (cp_parser_parse_definitely (parser))
25709 return objc_get_class_reference (rcv);
25711 cp_parser_error (parser, "objective-c++ message receiver expected");
25712 return error_mark_node;
25715 /* Parse the arguments and selectors comprising an Objective-C message.
25717 objc-message-args:
25718 objc-selector
25719 objc-selector-args
25720 objc-selector-args , objc-comma-args
25722 objc-selector-args:
25723 objc-selector [opt] : assignment-expression
25724 objc-selector-args objc-selector [opt] : assignment-expression
25726 objc-comma-args:
25727 assignment-expression
25728 objc-comma-args , assignment-expression
25730 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25731 selector arguments and TREE_VALUE containing a list of comma
25732 arguments. */
25734 static tree
25735 cp_parser_objc_message_args (cp_parser* parser)
25737 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25738 bool maybe_unary_selector_p = true;
25739 cp_token *token = cp_lexer_peek_token (parser->lexer);
25741 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25743 tree selector = NULL_TREE, arg;
25745 if (token->type != CPP_COLON)
25746 selector = cp_parser_objc_selector (parser);
25748 /* Detect if we have a unary selector. */
25749 if (maybe_unary_selector_p
25750 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25751 return build_tree_list (selector, NULL_TREE);
25753 maybe_unary_selector_p = false;
25754 cp_parser_require (parser, CPP_COLON, RT_COLON);
25755 arg = cp_parser_assignment_expression (parser);
25757 sel_args
25758 = chainon (sel_args,
25759 build_tree_list (selector, arg));
25761 token = cp_lexer_peek_token (parser->lexer);
25764 /* Handle non-selector arguments, if any. */
25765 while (token->type == CPP_COMMA)
25767 tree arg;
25769 cp_lexer_consume_token (parser->lexer);
25770 arg = cp_parser_assignment_expression (parser);
25772 addl_args
25773 = chainon (addl_args,
25774 build_tree_list (NULL_TREE, arg));
25776 token = cp_lexer_peek_token (parser->lexer);
25779 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25781 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25782 return build_tree_list (error_mark_node, error_mark_node);
25785 return build_tree_list (sel_args, addl_args);
25788 /* Parse an Objective-C encode expression.
25790 objc-encode-expression:
25791 @encode objc-typename
25793 Returns an encoded representation of the type argument. */
25795 static tree
25796 cp_parser_objc_encode_expression (cp_parser* parser)
25798 tree type;
25799 cp_token *token;
25801 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25803 token = cp_lexer_peek_token (parser->lexer);
25804 type = complete_type (cp_parser_type_id (parser));
25805 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25807 if (!type)
25809 error_at (token->location,
25810 "%<@encode%> must specify a type as an argument");
25811 return error_mark_node;
25814 /* This happens if we find @encode(T) (where T is a template
25815 typename or something dependent on a template typename) when
25816 parsing a template. In that case, we can't compile it
25817 immediately, but we rather create an AT_ENCODE_EXPR which will
25818 need to be instantiated when the template is used.
25820 if (dependent_type_p (type))
25822 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25823 TREE_READONLY (value) = 1;
25824 return value;
25827 return objc_build_encode_expr (type);
25830 /* Parse an Objective-C @defs expression. */
25832 static tree
25833 cp_parser_objc_defs_expression (cp_parser *parser)
25835 tree name;
25837 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25838 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25839 name = cp_parser_identifier (parser);
25840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25842 return objc_get_class_ivars (name);
25845 /* Parse an Objective-C protocol expression.
25847 objc-protocol-expression:
25848 @protocol ( identifier )
25850 Returns a representation of the protocol expression. */
25852 static tree
25853 cp_parser_objc_protocol_expression (cp_parser* parser)
25855 tree proto;
25857 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25858 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25859 proto = cp_parser_identifier (parser);
25860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25862 return objc_build_protocol_expr (proto);
25865 /* Parse an Objective-C selector expression.
25867 objc-selector-expression:
25868 @selector ( objc-method-signature )
25870 objc-method-signature:
25871 objc-selector
25872 objc-selector-seq
25874 objc-selector-seq:
25875 objc-selector :
25876 objc-selector-seq objc-selector :
25878 Returns a representation of the method selector. */
25880 static tree
25881 cp_parser_objc_selector_expression (cp_parser* parser)
25883 tree sel_seq = NULL_TREE;
25884 bool maybe_unary_selector_p = true;
25885 cp_token *token;
25886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25888 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25889 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25890 token = cp_lexer_peek_token (parser->lexer);
25892 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25893 || token->type == CPP_SCOPE)
25895 tree selector = NULL_TREE;
25897 if (token->type != CPP_COLON
25898 || token->type == CPP_SCOPE)
25899 selector = cp_parser_objc_selector (parser);
25901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25902 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25904 /* Detect if we have a unary selector. */
25905 if (maybe_unary_selector_p)
25907 sel_seq = selector;
25908 goto finish_selector;
25910 else
25912 cp_parser_error (parser, "expected %<:%>");
25915 maybe_unary_selector_p = false;
25916 token = cp_lexer_consume_token (parser->lexer);
25918 if (token->type == CPP_SCOPE)
25920 sel_seq
25921 = chainon (sel_seq,
25922 build_tree_list (selector, NULL_TREE));
25923 sel_seq
25924 = chainon (sel_seq,
25925 build_tree_list (NULL_TREE, NULL_TREE));
25927 else
25928 sel_seq
25929 = chainon (sel_seq,
25930 build_tree_list (selector, NULL_TREE));
25932 token = cp_lexer_peek_token (parser->lexer);
25935 finish_selector:
25936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25938 return objc_build_selector_expr (loc, sel_seq);
25941 /* Parse a list of identifiers.
25943 objc-identifier-list:
25944 identifier
25945 objc-identifier-list , identifier
25947 Returns a TREE_LIST of identifier nodes. */
25949 static tree
25950 cp_parser_objc_identifier_list (cp_parser* parser)
25952 tree identifier;
25953 tree list;
25954 cp_token *sep;
25956 identifier = cp_parser_identifier (parser);
25957 if (identifier == error_mark_node)
25958 return error_mark_node;
25960 list = build_tree_list (NULL_TREE, identifier);
25961 sep = cp_lexer_peek_token (parser->lexer);
25963 while (sep->type == CPP_COMMA)
25965 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25966 identifier = cp_parser_identifier (parser);
25967 if (identifier == error_mark_node)
25968 return list;
25970 list = chainon (list, build_tree_list (NULL_TREE,
25971 identifier));
25972 sep = cp_lexer_peek_token (parser->lexer);
25975 return list;
25978 /* Parse an Objective-C alias declaration.
25980 objc-alias-declaration:
25981 @compatibility_alias identifier identifier ;
25983 This function registers the alias mapping with the Objective-C front end.
25984 It returns nothing. */
25986 static void
25987 cp_parser_objc_alias_declaration (cp_parser* parser)
25989 tree alias, orig;
25991 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25992 alias = cp_parser_identifier (parser);
25993 orig = cp_parser_identifier (parser);
25994 objc_declare_alias (alias, orig);
25995 cp_parser_consume_semicolon_at_end_of_statement (parser);
25998 /* Parse an Objective-C class forward-declaration.
26000 objc-class-declaration:
26001 @class objc-identifier-list ;
26003 The function registers the forward declarations with the Objective-C
26004 front end. It returns nothing. */
26006 static void
26007 cp_parser_objc_class_declaration (cp_parser* parser)
26009 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26010 while (true)
26012 tree id;
26014 id = cp_parser_identifier (parser);
26015 if (id == error_mark_node)
26016 break;
26018 objc_declare_class (id);
26020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26021 cp_lexer_consume_token (parser->lexer);
26022 else
26023 break;
26025 cp_parser_consume_semicolon_at_end_of_statement (parser);
26028 /* Parse a list of Objective-C protocol references.
26030 objc-protocol-refs-opt:
26031 objc-protocol-refs [opt]
26033 objc-protocol-refs:
26034 < objc-identifier-list >
26036 Returns a TREE_LIST of identifiers, if any. */
26038 static tree
26039 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26041 tree protorefs = NULL_TREE;
26043 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26045 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26046 protorefs = cp_parser_objc_identifier_list (parser);
26047 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26050 return protorefs;
26053 /* Parse a Objective-C visibility specification. */
26055 static void
26056 cp_parser_objc_visibility_spec (cp_parser* parser)
26058 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26060 switch (vis->keyword)
26062 case RID_AT_PRIVATE:
26063 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26064 break;
26065 case RID_AT_PROTECTED:
26066 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26067 break;
26068 case RID_AT_PUBLIC:
26069 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26070 break;
26071 case RID_AT_PACKAGE:
26072 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26073 break;
26074 default:
26075 return;
26078 /* Eat '@private'/'@protected'/'@public'. */
26079 cp_lexer_consume_token (parser->lexer);
26082 /* Parse an Objective-C method type. Return 'true' if it is a class
26083 (+) method, and 'false' if it is an instance (-) method. */
26085 static inline bool
26086 cp_parser_objc_method_type (cp_parser* parser)
26088 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26089 return true;
26090 else
26091 return false;
26094 /* Parse an Objective-C protocol qualifier. */
26096 static tree
26097 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26099 tree quals = NULL_TREE, node;
26100 cp_token *token = cp_lexer_peek_token (parser->lexer);
26102 node = token->u.value;
26104 while (node && identifier_p (node)
26105 && (node == ridpointers [(int) RID_IN]
26106 || node == ridpointers [(int) RID_OUT]
26107 || node == ridpointers [(int) RID_INOUT]
26108 || node == ridpointers [(int) RID_BYCOPY]
26109 || node == ridpointers [(int) RID_BYREF]
26110 || node == ridpointers [(int) RID_ONEWAY]))
26112 quals = tree_cons (NULL_TREE, node, quals);
26113 cp_lexer_consume_token (parser->lexer);
26114 token = cp_lexer_peek_token (parser->lexer);
26115 node = token->u.value;
26118 return quals;
26121 /* Parse an Objective-C typename. */
26123 static tree
26124 cp_parser_objc_typename (cp_parser* parser)
26126 tree type_name = NULL_TREE;
26128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26130 tree proto_quals, cp_type = NULL_TREE;
26132 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26133 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26135 /* An ObjC type name may consist of just protocol qualifiers, in which
26136 case the type shall default to 'id'. */
26137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26139 cp_type = cp_parser_type_id (parser);
26141 /* If the type could not be parsed, an error has already
26142 been produced. For error recovery, behave as if it had
26143 not been specified, which will use the default type
26144 'id'. */
26145 if (cp_type == error_mark_node)
26147 cp_type = NULL_TREE;
26148 /* We need to skip to the closing parenthesis as
26149 cp_parser_type_id() does not seem to do it for
26150 us. */
26151 cp_parser_skip_to_closing_parenthesis (parser,
26152 /*recovering=*/true,
26153 /*or_comma=*/false,
26154 /*consume_paren=*/false);
26158 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26159 type_name = build_tree_list (proto_quals, cp_type);
26162 return type_name;
26165 /* Check to see if TYPE refers to an Objective-C selector name. */
26167 static bool
26168 cp_parser_objc_selector_p (enum cpp_ttype type)
26170 return (type == CPP_NAME || type == CPP_KEYWORD
26171 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26172 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26173 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26174 || type == CPP_XOR || type == CPP_XOR_EQ);
26177 /* Parse an Objective-C selector. */
26179 static tree
26180 cp_parser_objc_selector (cp_parser* parser)
26182 cp_token *token = cp_lexer_consume_token (parser->lexer);
26184 if (!cp_parser_objc_selector_p (token->type))
26186 error_at (token->location, "invalid Objective-C++ selector name");
26187 return error_mark_node;
26190 /* C++ operator names are allowed to appear in ObjC selectors. */
26191 switch (token->type)
26193 case CPP_AND_AND: return get_identifier ("and");
26194 case CPP_AND_EQ: return get_identifier ("and_eq");
26195 case CPP_AND: return get_identifier ("bitand");
26196 case CPP_OR: return get_identifier ("bitor");
26197 case CPP_COMPL: return get_identifier ("compl");
26198 case CPP_NOT: return get_identifier ("not");
26199 case CPP_NOT_EQ: return get_identifier ("not_eq");
26200 case CPP_OR_OR: return get_identifier ("or");
26201 case CPP_OR_EQ: return get_identifier ("or_eq");
26202 case CPP_XOR: return get_identifier ("xor");
26203 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26204 default: return token->u.value;
26208 /* Parse an Objective-C params list. */
26210 static tree
26211 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26213 tree params = NULL_TREE;
26214 bool maybe_unary_selector_p = true;
26215 cp_token *token = cp_lexer_peek_token (parser->lexer);
26217 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26219 tree selector = NULL_TREE, type_name, identifier;
26220 tree parm_attr = NULL_TREE;
26222 if (token->keyword == RID_ATTRIBUTE)
26223 break;
26225 if (token->type != CPP_COLON)
26226 selector = cp_parser_objc_selector (parser);
26228 /* Detect if we have a unary selector. */
26229 if (maybe_unary_selector_p
26230 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26232 params = selector; /* Might be followed by attributes. */
26233 break;
26236 maybe_unary_selector_p = false;
26237 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26239 /* Something went quite wrong. There should be a colon
26240 here, but there is not. Stop parsing parameters. */
26241 break;
26243 type_name = cp_parser_objc_typename (parser);
26244 /* New ObjC allows attributes on parameters too. */
26245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26246 parm_attr = cp_parser_attributes_opt (parser);
26247 identifier = cp_parser_identifier (parser);
26249 params
26250 = chainon (params,
26251 objc_build_keyword_decl (selector,
26252 type_name,
26253 identifier,
26254 parm_attr));
26256 token = cp_lexer_peek_token (parser->lexer);
26259 if (params == NULL_TREE)
26261 cp_parser_error (parser, "objective-c++ method declaration is expected");
26262 return error_mark_node;
26265 /* We allow tail attributes for the method. */
26266 if (token->keyword == RID_ATTRIBUTE)
26268 *attributes = cp_parser_attributes_opt (parser);
26269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26270 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26271 return params;
26272 cp_parser_error (parser,
26273 "method attributes must be specified at the end");
26274 return error_mark_node;
26277 if (params == NULL_TREE)
26279 cp_parser_error (parser, "objective-c++ method declaration is expected");
26280 return error_mark_node;
26282 return params;
26285 /* Parse the non-keyword Objective-C params. */
26287 static tree
26288 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26289 tree* attributes)
26291 tree params = make_node (TREE_LIST);
26292 cp_token *token = cp_lexer_peek_token (parser->lexer);
26293 *ellipsisp = false; /* Initially, assume no ellipsis. */
26295 while (token->type == CPP_COMMA)
26297 cp_parameter_declarator *parmdecl;
26298 tree parm;
26300 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26301 token = cp_lexer_peek_token (parser->lexer);
26303 if (token->type == CPP_ELLIPSIS)
26305 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26306 *ellipsisp = true;
26307 token = cp_lexer_peek_token (parser->lexer);
26308 break;
26311 /* TODO: parse attributes for tail parameters. */
26312 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26313 parm = grokdeclarator (parmdecl->declarator,
26314 &parmdecl->decl_specifiers,
26315 PARM, /*initialized=*/0,
26316 /*attrlist=*/NULL);
26318 chainon (params, build_tree_list (NULL_TREE, parm));
26319 token = cp_lexer_peek_token (parser->lexer);
26322 /* We allow tail attributes for the method. */
26323 if (token->keyword == RID_ATTRIBUTE)
26325 if (*attributes == NULL_TREE)
26327 *attributes = cp_parser_attributes_opt (parser);
26328 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26329 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26330 return params;
26332 else
26333 /* We have an error, but parse the attributes, so that we can
26334 carry on. */
26335 *attributes = cp_parser_attributes_opt (parser);
26337 cp_parser_error (parser,
26338 "method attributes must be specified at the end");
26339 return error_mark_node;
26342 return params;
26345 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26347 static void
26348 cp_parser_objc_interstitial_code (cp_parser* parser)
26350 cp_token *token = cp_lexer_peek_token (parser->lexer);
26352 /* If the next token is `extern' and the following token is a string
26353 literal, then we have a linkage specification. */
26354 if (token->keyword == RID_EXTERN
26355 && cp_parser_is_pure_string_literal
26356 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26357 cp_parser_linkage_specification (parser);
26358 /* Handle #pragma, if any. */
26359 else if (token->type == CPP_PRAGMA)
26360 cp_parser_pragma (parser, pragma_objc_icode);
26361 /* Allow stray semicolons. */
26362 else if (token->type == CPP_SEMICOLON)
26363 cp_lexer_consume_token (parser->lexer);
26364 /* Mark methods as optional or required, when building protocols. */
26365 else if (token->keyword == RID_AT_OPTIONAL)
26367 cp_lexer_consume_token (parser->lexer);
26368 objc_set_method_opt (true);
26370 else if (token->keyword == RID_AT_REQUIRED)
26372 cp_lexer_consume_token (parser->lexer);
26373 objc_set_method_opt (false);
26375 else if (token->keyword == RID_NAMESPACE)
26376 cp_parser_namespace_definition (parser);
26377 /* Other stray characters must generate errors. */
26378 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26380 cp_lexer_consume_token (parser->lexer);
26381 error ("stray %qs between Objective-C++ methods",
26382 token->type == CPP_OPEN_BRACE ? "{" : "}");
26384 /* Finally, try to parse a block-declaration, or a function-definition. */
26385 else
26386 cp_parser_block_declaration (parser, /*statement_p=*/false);
26389 /* Parse a method signature. */
26391 static tree
26392 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26394 tree rettype, kwdparms, optparms;
26395 bool ellipsis = false;
26396 bool is_class_method;
26398 is_class_method = cp_parser_objc_method_type (parser);
26399 rettype = cp_parser_objc_typename (parser);
26400 *attributes = NULL_TREE;
26401 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26402 if (kwdparms == error_mark_node)
26403 return error_mark_node;
26404 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26405 if (optparms == error_mark_node)
26406 return error_mark_node;
26408 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26411 static bool
26412 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26414 tree tattr;
26415 cp_lexer_save_tokens (parser->lexer);
26416 tattr = cp_parser_attributes_opt (parser);
26417 gcc_assert (tattr) ;
26419 /* If the attributes are followed by a method introducer, this is not allowed.
26420 Dump the attributes and flag the situation. */
26421 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26422 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26423 return true;
26425 /* Otherwise, the attributes introduce some interstitial code, possibly so
26426 rewind to allow that check. */
26427 cp_lexer_rollback_tokens (parser->lexer);
26428 return false;
26431 /* Parse an Objective-C method prototype list. */
26433 static void
26434 cp_parser_objc_method_prototype_list (cp_parser* parser)
26436 cp_token *token = cp_lexer_peek_token (parser->lexer);
26438 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26440 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26442 tree attributes, sig;
26443 bool is_class_method;
26444 if (token->type == CPP_PLUS)
26445 is_class_method = true;
26446 else
26447 is_class_method = false;
26448 sig = cp_parser_objc_method_signature (parser, &attributes);
26449 if (sig == error_mark_node)
26451 cp_parser_skip_to_end_of_block_or_statement (parser);
26452 token = cp_lexer_peek_token (parser->lexer);
26453 continue;
26455 objc_add_method_declaration (is_class_method, sig, attributes);
26456 cp_parser_consume_semicolon_at_end_of_statement (parser);
26458 else if (token->keyword == RID_AT_PROPERTY)
26459 cp_parser_objc_at_property_declaration (parser);
26460 else if (token->keyword == RID_ATTRIBUTE
26461 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26462 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26463 OPT_Wattributes,
26464 "prefix attributes are ignored for methods");
26465 else
26466 /* Allow for interspersed non-ObjC++ code. */
26467 cp_parser_objc_interstitial_code (parser);
26469 token = cp_lexer_peek_token (parser->lexer);
26472 if (token->type != CPP_EOF)
26473 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26474 else
26475 cp_parser_error (parser, "expected %<@end%>");
26477 objc_finish_interface ();
26480 /* Parse an Objective-C method definition list. */
26482 static void
26483 cp_parser_objc_method_definition_list (cp_parser* parser)
26485 cp_token *token = cp_lexer_peek_token (parser->lexer);
26487 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26489 tree meth;
26491 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26493 cp_token *ptk;
26494 tree sig, attribute;
26495 bool is_class_method;
26496 if (token->type == CPP_PLUS)
26497 is_class_method = true;
26498 else
26499 is_class_method = false;
26500 push_deferring_access_checks (dk_deferred);
26501 sig = cp_parser_objc_method_signature (parser, &attribute);
26502 if (sig == error_mark_node)
26504 cp_parser_skip_to_end_of_block_or_statement (parser);
26505 token = cp_lexer_peek_token (parser->lexer);
26506 continue;
26508 objc_start_method_definition (is_class_method, sig, attribute,
26509 NULL_TREE);
26511 /* For historical reasons, we accept an optional semicolon. */
26512 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26513 cp_lexer_consume_token (parser->lexer);
26515 ptk = cp_lexer_peek_token (parser->lexer);
26516 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26517 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26519 perform_deferred_access_checks (tf_warning_or_error);
26520 stop_deferring_access_checks ();
26521 meth = cp_parser_function_definition_after_declarator (parser,
26522 false);
26523 pop_deferring_access_checks ();
26524 objc_finish_method_definition (meth);
26527 /* The following case will be removed once @synthesize is
26528 completely implemented. */
26529 else if (token->keyword == RID_AT_PROPERTY)
26530 cp_parser_objc_at_property_declaration (parser);
26531 else if (token->keyword == RID_AT_SYNTHESIZE)
26532 cp_parser_objc_at_synthesize_declaration (parser);
26533 else if (token->keyword == RID_AT_DYNAMIC)
26534 cp_parser_objc_at_dynamic_declaration (parser);
26535 else if (token->keyword == RID_ATTRIBUTE
26536 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26537 warning_at (token->location, OPT_Wattributes,
26538 "prefix attributes are ignored for methods");
26539 else
26540 /* Allow for interspersed non-ObjC++ code. */
26541 cp_parser_objc_interstitial_code (parser);
26543 token = cp_lexer_peek_token (parser->lexer);
26546 if (token->type != CPP_EOF)
26547 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26548 else
26549 cp_parser_error (parser, "expected %<@end%>");
26551 objc_finish_implementation ();
26554 /* Parse Objective-C ivars. */
26556 static void
26557 cp_parser_objc_class_ivars (cp_parser* parser)
26559 cp_token *token = cp_lexer_peek_token (parser->lexer);
26561 if (token->type != CPP_OPEN_BRACE)
26562 return; /* No ivars specified. */
26564 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26565 token = cp_lexer_peek_token (parser->lexer);
26567 while (token->type != CPP_CLOSE_BRACE
26568 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26570 cp_decl_specifier_seq declspecs;
26571 int decl_class_or_enum_p;
26572 tree prefix_attributes;
26574 cp_parser_objc_visibility_spec (parser);
26576 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26577 break;
26579 cp_parser_decl_specifier_seq (parser,
26580 CP_PARSER_FLAGS_OPTIONAL,
26581 &declspecs,
26582 &decl_class_or_enum_p);
26584 /* auto, register, static, extern, mutable. */
26585 if (declspecs.storage_class != sc_none)
26587 cp_parser_error (parser, "invalid type for instance variable");
26588 declspecs.storage_class = sc_none;
26591 /* thread_local. */
26592 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26594 cp_parser_error (parser, "invalid type for instance variable");
26595 declspecs.locations[ds_thread] = 0;
26598 /* typedef. */
26599 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26601 cp_parser_error (parser, "invalid type for instance variable");
26602 declspecs.locations[ds_typedef] = 0;
26605 prefix_attributes = declspecs.attributes;
26606 declspecs.attributes = NULL_TREE;
26608 /* Keep going until we hit the `;' at the end of the
26609 declaration. */
26610 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26612 tree width = NULL_TREE, attributes, first_attribute, decl;
26613 cp_declarator *declarator = NULL;
26614 int ctor_dtor_or_conv_p;
26616 /* Check for a (possibly unnamed) bitfield declaration. */
26617 token = cp_lexer_peek_token (parser->lexer);
26618 if (token->type == CPP_COLON)
26619 goto eat_colon;
26621 if (token->type == CPP_NAME
26622 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26623 == CPP_COLON))
26625 /* Get the name of the bitfield. */
26626 declarator = make_id_declarator (NULL_TREE,
26627 cp_parser_identifier (parser),
26628 sfk_none);
26630 eat_colon:
26631 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26632 /* Get the width of the bitfield. */
26633 width
26634 = cp_parser_constant_expression (parser);
26636 else
26638 /* Parse the declarator. */
26639 declarator
26640 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26641 &ctor_dtor_or_conv_p,
26642 /*parenthesized_p=*/NULL,
26643 /*member_p=*/false,
26644 /*friend_p=*/false);
26647 /* Look for attributes that apply to the ivar. */
26648 attributes = cp_parser_attributes_opt (parser);
26649 /* Remember which attributes are prefix attributes and
26650 which are not. */
26651 first_attribute = attributes;
26652 /* Combine the attributes. */
26653 attributes = chainon (prefix_attributes, attributes);
26655 if (width)
26656 /* Create the bitfield declaration. */
26657 decl = grokbitfield (declarator, &declspecs,
26658 width,
26659 attributes);
26660 else
26661 decl = grokfield (declarator, &declspecs,
26662 NULL_TREE, /*init_const_expr_p=*/false,
26663 NULL_TREE, attributes);
26665 /* Add the instance variable. */
26666 if (decl != error_mark_node && decl != NULL_TREE)
26667 objc_add_instance_variable (decl);
26669 /* Reset PREFIX_ATTRIBUTES. */
26670 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26671 attributes = TREE_CHAIN (attributes);
26672 if (attributes)
26673 TREE_CHAIN (attributes) = NULL_TREE;
26675 token = cp_lexer_peek_token (parser->lexer);
26677 if (token->type == CPP_COMMA)
26679 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26680 continue;
26682 break;
26685 cp_parser_consume_semicolon_at_end_of_statement (parser);
26686 token = cp_lexer_peek_token (parser->lexer);
26689 if (token->keyword == RID_AT_END)
26690 cp_parser_error (parser, "expected %<}%>");
26692 /* Do not consume the RID_AT_END, so it will be read again as terminating
26693 the @interface of @implementation. */
26694 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26695 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26697 /* For historical reasons, we accept an optional semicolon. */
26698 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26699 cp_lexer_consume_token (parser->lexer);
26702 /* Parse an Objective-C protocol declaration. */
26704 static void
26705 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26707 tree proto, protorefs;
26708 cp_token *tok;
26710 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26713 tok = cp_lexer_peek_token (parser->lexer);
26714 error_at (tok->location, "identifier expected after %<@protocol%>");
26715 cp_parser_consume_semicolon_at_end_of_statement (parser);
26716 return;
26719 /* See if we have a forward declaration or a definition. */
26720 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26722 /* Try a forward declaration first. */
26723 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26725 while (true)
26727 tree id;
26729 id = cp_parser_identifier (parser);
26730 if (id == error_mark_node)
26731 break;
26733 objc_declare_protocol (id, attributes);
26735 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26736 cp_lexer_consume_token (parser->lexer);
26737 else
26738 break;
26740 cp_parser_consume_semicolon_at_end_of_statement (parser);
26743 /* Ok, we got a full-fledged definition (or at least should). */
26744 else
26746 proto = cp_parser_identifier (parser);
26747 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26748 objc_start_protocol (proto, protorefs, attributes);
26749 cp_parser_objc_method_prototype_list (parser);
26753 /* Parse an Objective-C superclass or category. */
26755 static void
26756 cp_parser_objc_superclass_or_category (cp_parser *parser,
26757 bool iface_p,
26758 tree *super,
26759 tree *categ, bool *is_class_extension)
26761 cp_token *next = cp_lexer_peek_token (parser->lexer);
26763 *super = *categ = NULL_TREE;
26764 *is_class_extension = false;
26765 if (next->type == CPP_COLON)
26767 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26768 *super = cp_parser_identifier (parser);
26770 else if (next->type == CPP_OPEN_PAREN)
26772 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26774 /* If there is no category name, and this is an @interface, we
26775 have a class extension. */
26776 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26778 *categ = NULL_TREE;
26779 *is_class_extension = true;
26781 else
26782 *categ = cp_parser_identifier (parser);
26784 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26788 /* Parse an Objective-C class interface. */
26790 static void
26791 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26793 tree name, super, categ, protos;
26794 bool is_class_extension;
26796 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26797 name = cp_parser_identifier (parser);
26798 if (name == error_mark_node)
26800 /* It's hard to recover because even if valid @interface stuff
26801 is to follow, we can't compile it (or validate it) if we
26802 don't even know which class it refers to. Let's assume this
26803 was a stray '@interface' token in the stream and skip it.
26805 return;
26807 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26808 &is_class_extension);
26809 protos = cp_parser_objc_protocol_refs_opt (parser);
26811 /* We have either a class or a category on our hands. */
26812 if (categ || is_class_extension)
26813 objc_start_category_interface (name, categ, protos, attributes);
26814 else
26816 objc_start_class_interface (name, super, protos, attributes);
26817 /* Handle instance variable declarations, if any. */
26818 cp_parser_objc_class_ivars (parser);
26819 objc_continue_interface ();
26822 cp_parser_objc_method_prototype_list (parser);
26825 /* Parse an Objective-C class implementation. */
26827 static void
26828 cp_parser_objc_class_implementation (cp_parser* parser)
26830 tree name, super, categ;
26831 bool is_class_extension;
26833 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26834 name = cp_parser_identifier (parser);
26835 if (name == error_mark_node)
26837 /* It's hard to recover because even if valid @implementation
26838 stuff is to follow, we can't compile it (or validate it) if
26839 we don't even know which class it refers to. Let's assume
26840 this was a stray '@implementation' token in the stream and
26841 skip it.
26843 return;
26845 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26846 &is_class_extension);
26848 /* We have either a class or a category on our hands. */
26849 if (categ)
26850 objc_start_category_implementation (name, categ);
26851 else
26853 objc_start_class_implementation (name, super);
26854 /* Handle instance variable declarations, if any. */
26855 cp_parser_objc_class_ivars (parser);
26856 objc_continue_implementation ();
26859 cp_parser_objc_method_definition_list (parser);
26862 /* Consume the @end token and finish off the implementation. */
26864 static void
26865 cp_parser_objc_end_implementation (cp_parser* parser)
26867 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26868 objc_finish_implementation ();
26871 /* Parse an Objective-C declaration. */
26873 static void
26874 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26876 /* Try to figure out what kind of declaration is present. */
26877 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26879 if (attributes)
26880 switch (kwd->keyword)
26882 case RID_AT_ALIAS:
26883 case RID_AT_CLASS:
26884 case RID_AT_END:
26885 error_at (kwd->location, "attributes may not be specified before"
26886 " the %<@%D%> Objective-C++ keyword",
26887 kwd->u.value);
26888 attributes = NULL;
26889 break;
26890 case RID_AT_IMPLEMENTATION:
26891 warning_at (kwd->location, OPT_Wattributes,
26892 "prefix attributes are ignored before %<@%D%>",
26893 kwd->u.value);
26894 attributes = NULL;
26895 default:
26896 break;
26899 switch (kwd->keyword)
26901 case RID_AT_ALIAS:
26902 cp_parser_objc_alias_declaration (parser);
26903 break;
26904 case RID_AT_CLASS:
26905 cp_parser_objc_class_declaration (parser);
26906 break;
26907 case RID_AT_PROTOCOL:
26908 cp_parser_objc_protocol_declaration (parser, attributes);
26909 break;
26910 case RID_AT_INTERFACE:
26911 cp_parser_objc_class_interface (parser, attributes);
26912 break;
26913 case RID_AT_IMPLEMENTATION:
26914 cp_parser_objc_class_implementation (parser);
26915 break;
26916 case RID_AT_END:
26917 cp_parser_objc_end_implementation (parser);
26918 break;
26919 default:
26920 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26921 kwd->u.value);
26922 cp_parser_skip_to_end_of_block_or_statement (parser);
26926 /* Parse an Objective-C try-catch-finally statement.
26928 objc-try-catch-finally-stmt:
26929 @try compound-statement objc-catch-clause-seq [opt]
26930 objc-finally-clause [opt]
26932 objc-catch-clause-seq:
26933 objc-catch-clause objc-catch-clause-seq [opt]
26935 objc-catch-clause:
26936 @catch ( objc-exception-declaration ) compound-statement
26938 objc-finally-clause:
26939 @finally compound-statement
26941 objc-exception-declaration:
26942 parameter-declaration
26943 '...'
26945 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26947 Returns NULL_TREE.
26949 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26950 for C. Keep them in sync. */
26952 static tree
26953 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26955 location_t location;
26956 tree stmt;
26958 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26959 location = cp_lexer_peek_token (parser->lexer)->location;
26960 objc_maybe_warn_exceptions (location);
26961 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26962 node, lest it get absorbed into the surrounding block. */
26963 stmt = push_stmt_list ();
26964 cp_parser_compound_statement (parser, NULL, false, false);
26965 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26967 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26969 cp_parameter_declarator *parm;
26970 tree parameter_declaration = error_mark_node;
26971 bool seen_open_paren = false;
26973 cp_lexer_consume_token (parser->lexer);
26974 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26975 seen_open_paren = true;
26976 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26978 /* We have "@catch (...)" (where the '...' are literally
26979 what is in the code). Skip the '...'.
26980 parameter_declaration is set to NULL_TREE, and
26981 objc_being_catch_clauses() knows that that means
26982 '...'. */
26983 cp_lexer_consume_token (parser->lexer);
26984 parameter_declaration = NULL_TREE;
26986 else
26988 /* We have "@catch (NSException *exception)" or something
26989 like that. Parse the parameter declaration. */
26990 parm = cp_parser_parameter_declaration (parser, false, NULL);
26991 if (parm == NULL)
26992 parameter_declaration = error_mark_node;
26993 else
26994 parameter_declaration = grokdeclarator (parm->declarator,
26995 &parm->decl_specifiers,
26996 PARM, /*initialized=*/0,
26997 /*attrlist=*/NULL);
26999 if (seen_open_paren)
27000 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27001 else
27003 /* If there was no open parenthesis, we are recovering from
27004 an error, and we are trying to figure out what mistake
27005 the user has made. */
27007 /* If there is an immediate closing parenthesis, the user
27008 probably forgot the opening one (ie, they typed "@catch
27009 NSException *e)". Parse the closing parenthesis and keep
27010 going. */
27011 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27012 cp_lexer_consume_token (parser->lexer);
27014 /* If these is no immediate closing parenthesis, the user
27015 probably doesn't know that parenthesis are required at
27016 all (ie, they typed "@catch NSException *e"). So, just
27017 forget about the closing parenthesis and keep going. */
27019 objc_begin_catch_clause (parameter_declaration);
27020 cp_parser_compound_statement (parser, NULL, false, false);
27021 objc_finish_catch_clause ();
27023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27025 cp_lexer_consume_token (parser->lexer);
27026 location = cp_lexer_peek_token (parser->lexer)->location;
27027 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27028 node, lest it get absorbed into the surrounding block. */
27029 stmt = push_stmt_list ();
27030 cp_parser_compound_statement (parser, NULL, false, false);
27031 objc_build_finally_clause (location, pop_stmt_list (stmt));
27034 return objc_finish_try_stmt ();
27037 /* Parse an Objective-C synchronized statement.
27039 objc-synchronized-stmt:
27040 @synchronized ( expression ) compound-statement
27042 Returns NULL_TREE. */
27044 static tree
27045 cp_parser_objc_synchronized_statement (cp_parser *parser)
27047 location_t location;
27048 tree lock, stmt;
27050 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27052 location = cp_lexer_peek_token (parser->lexer)->location;
27053 objc_maybe_warn_exceptions (location);
27054 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27055 lock = cp_parser_expression (parser);
27056 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27058 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27059 node, lest it get absorbed into the surrounding block. */
27060 stmt = push_stmt_list ();
27061 cp_parser_compound_statement (parser, NULL, false, false);
27063 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27066 /* Parse an Objective-C throw statement.
27068 objc-throw-stmt:
27069 @throw assignment-expression [opt] ;
27071 Returns a constructed '@throw' statement. */
27073 static tree
27074 cp_parser_objc_throw_statement (cp_parser *parser)
27076 tree expr = NULL_TREE;
27077 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27079 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27082 expr = cp_parser_expression (parser);
27084 cp_parser_consume_semicolon_at_end_of_statement (parser);
27086 return objc_build_throw_stmt (loc, expr);
27089 /* Parse an Objective-C statement. */
27091 static tree
27092 cp_parser_objc_statement (cp_parser * parser)
27094 /* Try to figure out what kind of declaration is present. */
27095 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27097 switch (kwd->keyword)
27099 case RID_AT_TRY:
27100 return cp_parser_objc_try_catch_finally_statement (parser);
27101 case RID_AT_SYNCHRONIZED:
27102 return cp_parser_objc_synchronized_statement (parser);
27103 case RID_AT_THROW:
27104 return cp_parser_objc_throw_statement (parser);
27105 default:
27106 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27107 kwd->u.value);
27108 cp_parser_skip_to_end_of_block_or_statement (parser);
27111 return error_mark_node;
27114 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27115 look ahead to see if an objc keyword follows the attributes. This
27116 is to detect the use of prefix attributes on ObjC @interface and
27117 @protocol. */
27119 static bool
27120 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27122 cp_lexer_save_tokens (parser->lexer);
27123 *attrib = cp_parser_attributes_opt (parser);
27124 gcc_assert (*attrib);
27125 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27127 cp_lexer_commit_tokens (parser->lexer);
27128 return true;
27130 cp_lexer_rollback_tokens (parser->lexer);
27131 return false;
27134 /* This routine is a minimal replacement for
27135 c_parser_struct_declaration () used when parsing the list of
27136 types/names or ObjC++ properties. For example, when parsing the
27137 code
27139 @property (readonly) int a, b, c;
27141 this function is responsible for parsing "int a, int b, int c" and
27142 returning the declarations as CHAIN of DECLs.
27144 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27145 similar parsing. */
27146 static tree
27147 cp_parser_objc_struct_declaration (cp_parser *parser)
27149 tree decls = NULL_TREE;
27150 cp_decl_specifier_seq declspecs;
27151 int decl_class_or_enum_p;
27152 tree prefix_attributes;
27154 cp_parser_decl_specifier_seq (parser,
27155 CP_PARSER_FLAGS_NONE,
27156 &declspecs,
27157 &decl_class_or_enum_p);
27159 if (declspecs.type == error_mark_node)
27160 return error_mark_node;
27162 /* auto, register, static, extern, mutable. */
27163 if (declspecs.storage_class != sc_none)
27165 cp_parser_error (parser, "invalid type for property");
27166 declspecs.storage_class = sc_none;
27169 /* thread_local. */
27170 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27172 cp_parser_error (parser, "invalid type for property");
27173 declspecs.locations[ds_thread] = 0;
27176 /* typedef. */
27177 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27179 cp_parser_error (parser, "invalid type for property");
27180 declspecs.locations[ds_typedef] = 0;
27183 prefix_attributes = declspecs.attributes;
27184 declspecs.attributes = NULL_TREE;
27186 /* Keep going until we hit the `;' at the end of the declaration. */
27187 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27189 tree attributes, first_attribute, decl;
27190 cp_declarator *declarator;
27191 cp_token *token;
27193 /* Parse the declarator. */
27194 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27195 NULL, NULL, false, false);
27197 /* Look for attributes that apply to the ivar. */
27198 attributes = cp_parser_attributes_opt (parser);
27199 /* Remember which attributes are prefix attributes and
27200 which are not. */
27201 first_attribute = attributes;
27202 /* Combine the attributes. */
27203 attributes = chainon (prefix_attributes, attributes);
27205 decl = grokfield (declarator, &declspecs,
27206 NULL_TREE, /*init_const_expr_p=*/false,
27207 NULL_TREE, attributes);
27209 if (decl == error_mark_node || decl == NULL_TREE)
27210 return error_mark_node;
27212 /* Reset PREFIX_ATTRIBUTES. */
27213 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27214 attributes = TREE_CHAIN (attributes);
27215 if (attributes)
27216 TREE_CHAIN (attributes) = NULL_TREE;
27218 DECL_CHAIN (decl) = decls;
27219 decls = decl;
27221 token = cp_lexer_peek_token (parser->lexer);
27222 if (token->type == CPP_COMMA)
27224 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27225 continue;
27227 else
27228 break;
27230 return decls;
27233 /* Parse an Objective-C @property declaration. The syntax is:
27235 objc-property-declaration:
27236 '@property' objc-property-attributes[opt] struct-declaration ;
27238 objc-property-attributes:
27239 '(' objc-property-attribute-list ')'
27241 objc-property-attribute-list:
27242 objc-property-attribute
27243 objc-property-attribute-list, objc-property-attribute
27245 objc-property-attribute
27246 'getter' = identifier
27247 'setter' = identifier
27248 'readonly'
27249 'readwrite'
27250 'assign'
27251 'retain'
27252 'copy'
27253 'nonatomic'
27255 For example:
27256 @property NSString *name;
27257 @property (readonly) id object;
27258 @property (retain, nonatomic, getter=getTheName) id name;
27259 @property int a, b, c;
27261 PS: This function is identical to
27262 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27263 static void
27264 cp_parser_objc_at_property_declaration (cp_parser *parser)
27266 /* The following variables hold the attributes of the properties as
27267 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27268 seen. When we see an attribute, we set them to 'true' (if they
27269 are boolean properties) or to the identifier (if they have an
27270 argument, ie, for getter and setter). Note that here we only
27271 parse the list of attributes, check the syntax and accumulate the
27272 attributes that we find. objc_add_property_declaration() will
27273 then process the information. */
27274 bool property_assign = false;
27275 bool property_copy = false;
27276 tree property_getter_ident = NULL_TREE;
27277 bool property_nonatomic = false;
27278 bool property_readonly = false;
27279 bool property_readwrite = false;
27280 bool property_retain = false;
27281 tree property_setter_ident = NULL_TREE;
27283 /* 'properties' is the list of properties that we read. Usually a
27284 single one, but maybe more (eg, in "@property int a, b, c;" there
27285 are three). */
27286 tree properties;
27287 location_t loc;
27289 loc = cp_lexer_peek_token (parser->lexer)->location;
27291 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27293 /* Parse the optional attribute list... */
27294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27296 /* Eat the '('. */
27297 cp_lexer_consume_token (parser->lexer);
27299 while (true)
27301 bool syntax_error = false;
27302 cp_token *token = cp_lexer_peek_token (parser->lexer);
27303 enum rid keyword;
27305 if (token->type != CPP_NAME)
27307 cp_parser_error (parser, "expected identifier");
27308 break;
27310 keyword = C_RID_CODE (token->u.value);
27311 cp_lexer_consume_token (parser->lexer);
27312 switch (keyword)
27314 case RID_ASSIGN: property_assign = true; break;
27315 case RID_COPY: property_copy = true; break;
27316 case RID_NONATOMIC: property_nonatomic = true; break;
27317 case RID_READONLY: property_readonly = true; break;
27318 case RID_READWRITE: property_readwrite = true; break;
27319 case RID_RETAIN: property_retain = true; break;
27321 case RID_GETTER:
27322 case RID_SETTER:
27323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27325 if (keyword == RID_GETTER)
27326 cp_parser_error (parser,
27327 "missing %<=%> (after %<getter%> attribute)");
27328 else
27329 cp_parser_error (parser,
27330 "missing %<=%> (after %<setter%> attribute)");
27331 syntax_error = true;
27332 break;
27334 cp_lexer_consume_token (parser->lexer); /* eat the = */
27335 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27337 cp_parser_error (parser, "expected identifier");
27338 syntax_error = true;
27339 break;
27341 if (keyword == RID_SETTER)
27343 if (property_setter_ident != NULL_TREE)
27345 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27346 cp_lexer_consume_token (parser->lexer);
27348 else
27349 property_setter_ident = cp_parser_objc_selector (parser);
27350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27351 cp_parser_error (parser, "setter name must terminate with %<:%>");
27352 else
27353 cp_lexer_consume_token (parser->lexer);
27355 else
27357 if (property_getter_ident != NULL_TREE)
27359 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27360 cp_lexer_consume_token (parser->lexer);
27362 else
27363 property_getter_ident = cp_parser_objc_selector (parser);
27365 break;
27366 default:
27367 cp_parser_error (parser, "unknown property attribute");
27368 syntax_error = true;
27369 break;
27372 if (syntax_error)
27373 break;
27375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27376 cp_lexer_consume_token (parser->lexer);
27377 else
27378 break;
27381 /* FIXME: "@property (setter, assign);" will generate a spurious
27382 "error: expected ‘)’ before ‘,’ token". This is because
27383 cp_parser_require, unlike the C counterpart, will produce an
27384 error even if we are in error recovery. */
27385 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27387 cp_parser_skip_to_closing_parenthesis (parser,
27388 /*recovering=*/true,
27389 /*or_comma=*/false,
27390 /*consume_paren=*/true);
27394 /* ... and the property declaration(s). */
27395 properties = cp_parser_objc_struct_declaration (parser);
27397 if (properties == error_mark_node)
27399 cp_parser_skip_to_end_of_statement (parser);
27400 /* If the next token is now a `;', consume it. */
27401 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27402 cp_lexer_consume_token (parser->lexer);
27403 return;
27406 if (properties == NULL_TREE)
27407 cp_parser_error (parser, "expected identifier");
27408 else
27410 /* Comma-separated properties are chained together in
27411 reverse order; add them one by one. */
27412 properties = nreverse (properties);
27414 for (; properties; properties = TREE_CHAIN (properties))
27415 objc_add_property_declaration (loc, copy_node (properties),
27416 property_readonly, property_readwrite,
27417 property_assign, property_retain,
27418 property_copy, property_nonatomic,
27419 property_getter_ident, property_setter_ident);
27422 cp_parser_consume_semicolon_at_end_of_statement (parser);
27425 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27427 objc-synthesize-declaration:
27428 @synthesize objc-synthesize-identifier-list ;
27430 objc-synthesize-identifier-list:
27431 objc-synthesize-identifier
27432 objc-synthesize-identifier-list, objc-synthesize-identifier
27434 objc-synthesize-identifier
27435 identifier
27436 identifier = identifier
27438 For example:
27439 @synthesize MyProperty;
27440 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27442 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27443 for C. Keep them in sync.
27445 static void
27446 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27448 tree list = NULL_TREE;
27449 location_t loc;
27450 loc = cp_lexer_peek_token (parser->lexer)->location;
27452 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27453 while (true)
27455 tree property, ivar;
27456 property = cp_parser_identifier (parser);
27457 if (property == error_mark_node)
27459 cp_parser_consume_semicolon_at_end_of_statement (parser);
27460 return;
27462 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27464 cp_lexer_consume_token (parser->lexer);
27465 ivar = cp_parser_identifier (parser);
27466 if (ivar == error_mark_node)
27468 cp_parser_consume_semicolon_at_end_of_statement (parser);
27469 return;
27472 else
27473 ivar = NULL_TREE;
27474 list = chainon (list, build_tree_list (ivar, property));
27475 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27476 cp_lexer_consume_token (parser->lexer);
27477 else
27478 break;
27480 cp_parser_consume_semicolon_at_end_of_statement (parser);
27481 objc_add_synthesize_declaration (loc, list);
27484 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27486 objc-dynamic-declaration:
27487 @dynamic identifier-list ;
27489 For example:
27490 @dynamic MyProperty;
27491 @dynamic MyProperty, AnotherProperty;
27493 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27494 for C. Keep them in sync.
27496 static void
27497 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27499 tree list = NULL_TREE;
27500 location_t loc;
27501 loc = cp_lexer_peek_token (parser->lexer)->location;
27503 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27504 while (true)
27506 tree property;
27507 property = cp_parser_identifier (parser);
27508 if (property == error_mark_node)
27510 cp_parser_consume_semicolon_at_end_of_statement (parser);
27511 return;
27513 list = chainon (list, build_tree_list (NULL, property));
27514 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27515 cp_lexer_consume_token (parser->lexer);
27516 else
27517 break;
27519 cp_parser_consume_semicolon_at_end_of_statement (parser);
27520 objc_add_dynamic_declaration (loc, list);
27524 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27526 /* Returns name of the next clause.
27527 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27528 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27529 returned and the token is consumed. */
27531 static pragma_omp_clause
27532 cp_parser_omp_clause_name (cp_parser *parser)
27534 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27537 result = PRAGMA_OMP_CLAUSE_IF;
27538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27539 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27540 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27541 result = PRAGMA_OACC_CLAUSE_DELETE;
27542 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27543 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27544 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27545 result = PRAGMA_OMP_CLAUSE_FOR;
27546 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27549 const char *p = IDENTIFIER_POINTER (id);
27551 switch (p[0])
27553 case 'a':
27554 if (!strcmp ("aligned", p))
27555 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27556 else if (!strcmp ("async", p))
27557 result = PRAGMA_OACC_CLAUSE_ASYNC;
27558 break;
27559 case 'c':
27560 if (!strcmp ("collapse", p))
27561 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27562 else if (!strcmp ("copy", p))
27563 result = PRAGMA_OACC_CLAUSE_COPY;
27564 else if (!strcmp ("copyin", p))
27565 result = PRAGMA_OMP_CLAUSE_COPYIN;
27566 else if (!strcmp ("copyout", p))
27567 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27568 else if (!strcmp ("copyprivate", p))
27569 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27570 else if (!strcmp ("create", p))
27571 result = PRAGMA_OACC_CLAUSE_CREATE;
27572 break;
27573 case 'd':
27574 if (!strcmp ("depend", p))
27575 result = PRAGMA_OMP_CLAUSE_DEPEND;
27576 else if (!strcmp ("device", p))
27577 result = PRAGMA_OMP_CLAUSE_DEVICE;
27578 else if (!strcmp ("deviceptr", p))
27579 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27580 else if (!strcmp ("dist_schedule", p))
27581 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27582 break;
27583 case 'f':
27584 if (!strcmp ("final", p))
27585 result = PRAGMA_OMP_CLAUSE_FINAL;
27586 else if (!strcmp ("firstprivate", p))
27587 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27588 else if (!strcmp ("from", p))
27589 result = PRAGMA_OMP_CLAUSE_FROM;
27590 break;
27591 case 'h':
27592 if (!strcmp ("host", p))
27593 result = PRAGMA_OACC_CLAUSE_HOST;
27594 break;
27595 case 'i':
27596 if (!strcmp ("inbranch", p))
27597 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27598 break;
27599 case 'l':
27600 if (!strcmp ("lastprivate", p))
27601 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27602 else if (!strcmp ("linear", p))
27603 result = PRAGMA_OMP_CLAUSE_LINEAR;
27604 break;
27605 case 'm':
27606 if (!strcmp ("map", p))
27607 result = PRAGMA_OMP_CLAUSE_MAP;
27608 else if (!strcmp ("mergeable", p))
27609 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27610 else if (flag_cilkplus && !strcmp ("mask", p))
27611 result = PRAGMA_CILK_CLAUSE_MASK;
27612 break;
27613 case 'n':
27614 if (!strcmp ("notinbranch", p))
27615 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27616 else if (!strcmp ("nowait", p))
27617 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27618 else if (flag_cilkplus && !strcmp ("nomask", p))
27619 result = PRAGMA_CILK_CLAUSE_NOMASK;
27620 else if (!strcmp ("num_gangs", p))
27621 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27622 else if (!strcmp ("num_teams", p))
27623 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27624 else if (!strcmp ("num_threads", p))
27625 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27626 else if (!strcmp ("num_workers", p))
27627 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27628 break;
27629 case 'o':
27630 if (!strcmp ("ordered", p))
27631 result = PRAGMA_OMP_CLAUSE_ORDERED;
27632 break;
27633 case 'p':
27634 if (!strcmp ("parallel", p))
27635 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27636 else if (!strcmp ("present", p))
27637 result = PRAGMA_OACC_CLAUSE_PRESENT;
27638 else if (!strcmp ("present_or_copy", p)
27639 || !strcmp ("pcopy", p))
27640 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27641 else if (!strcmp ("present_or_copyin", p)
27642 || !strcmp ("pcopyin", p))
27643 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27644 else if (!strcmp ("present_or_copyout", p)
27645 || !strcmp ("pcopyout", p))
27646 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27647 else if (!strcmp ("present_or_create", p)
27648 || !strcmp ("pcreate", p))
27649 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27650 else if (!strcmp ("proc_bind", p))
27651 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27652 break;
27653 case 'r':
27654 if (!strcmp ("reduction", p))
27655 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27656 break;
27657 case 's':
27658 if (!strcmp ("safelen", p))
27659 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27660 else if (!strcmp ("schedule", p))
27661 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27662 else if (!strcmp ("sections", p))
27663 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27664 else if (!strcmp ("self", p))
27665 result = PRAGMA_OACC_CLAUSE_SELF;
27666 else if (!strcmp ("shared", p))
27667 result = PRAGMA_OMP_CLAUSE_SHARED;
27668 else if (!strcmp ("simdlen", p))
27669 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27670 break;
27671 case 't':
27672 if (!strcmp ("taskgroup", p))
27673 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27674 else if (!strcmp ("thread_limit", p))
27675 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27676 else if (!strcmp ("to", p))
27677 result = PRAGMA_OMP_CLAUSE_TO;
27678 break;
27679 case 'u':
27680 if (!strcmp ("uniform", p))
27681 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27682 else if (!strcmp ("untied", p))
27683 result = PRAGMA_OMP_CLAUSE_UNTIED;
27684 break;
27685 case 'v':
27686 if (!strcmp ("vector_length", p))
27687 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27688 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27689 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27690 break;
27691 case 'w':
27692 if (!strcmp ("wait", p))
27693 result = PRAGMA_OACC_CLAUSE_WAIT;
27694 break;
27698 if (result != PRAGMA_OMP_CLAUSE_NONE)
27699 cp_lexer_consume_token (parser->lexer);
27701 return result;
27704 /* Validate that a clause of the given type does not already exist. */
27706 static void
27707 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27708 const char *name, location_t location)
27710 tree c;
27712 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27713 if (OMP_CLAUSE_CODE (c) == code)
27715 error_at (location, "too many %qs clauses", name);
27716 break;
27720 /* OpenMP 2.5:
27721 variable-list:
27722 identifier
27723 variable-list , identifier
27725 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27726 colon). An opening parenthesis will have been consumed by the caller.
27728 If KIND is nonzero, create the appropriate node and install the decl
27729 in OMP_CLAUSE_DECL and add the node to the head of the list.
27731 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27732 return the list created.
27734 COLON can be NULL if only closing parenthesis should end the list,
27735 or pointer to bool which will receive false if the list is terminated
27736 by closing parenthesis or true if the list is terminated by colon. */
27738 static tree
27739 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27740 tree list, bool *colon)
27742 cp_token *token;
27743 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27744 if (colon)
27746 parser->colon_corrects_to_scope_p = false;
27747 *colon = false;
27749 while (1)
27751 tree name, decl;
27753 token = cp_lexer_peek_token (parser->lexer);
27754 name = cp_parser_id_expression (parser, /*template_p=*/false,
27755 /*check_dependency_p=*/true,
27756 /*template_p=*/NULL,
27757 /*declarator_p=*/false,
27758 /*optional_p=*/false);
27759 if (name == error_mark_node)
27760 goto skip_comma;
27762 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27763 if (decl == error_mark_node)
27764 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27765 token->location);
27766 else if (kind != 0)
27768 switch (kind)
27770 case OMP_CLAUSE__CACHE_:
27771 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27773 error_at (token->location, "expected %<[%>");
27774 decl = error_mark_node;
27775 break;
27777 /* FALL THROUGH. */
27778 case OMP_CLAUSE_MAP:
27779 case OMP_CLAUSE_FROM:
27780 case OMP_CLAUSE_TO:
27781 case OMP_CLAUSE_DEPEND:
27782 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27784 tree low_bound = NULL_TREE, length = NULL_TREE;
27786 parser->colon_corrects_to_scope_p = false;
27787 cp_lexer_consume_token (parser->lexer);
27788 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27789 low_bound = cp_parser_expression (parser);
27790 if (!colon)
27791 parser->colon_corrects_to_scope_p
27792 = saved_colon_corrects_to_scope_p;
27793 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27794 length = integer_one_node;
27795 else
27797 /* Look for `:'. */
27798 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27799 goto skip_comma;
27800 if (!cp_lexer_next_token_is (parser->lexer,
27801 CPP_CLOSE_SQUARE))
27802 length = cp_parser_expression (parser);
27804 /* Look for the closing `]'. */
27805 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27806 RT_CLOSE_SQUARE))
27807 goto skip_comma;
27809 if (kind == OMP_CLAUSE__CACHE_)
27811 if (TREE_CODE (low_bound) != INTEGER_CST
27812 && !TREE_READONLY (low_bound))
27814 error_at (token->location,
27815 "%qD is not a constant", low_bound);
27816 decl = error_mark_node;
27819 if (TREE_CODE (length) != INTEGER_CST
27820 && !TREE_READONLY (length))
27822 error_at (token->location,
27823 "%qD is not a constant", length);
27824 decl = error_mark_node;
27828 decl = tree_cons (low_bound, length, decl);
27830 break;
27831 default:
27832 break;
27835 tree u = build_omp_clause (token->location, kind);
27836 OMP_CLAUSE_DECL (u) = decl;
27837 OMP_CLAUSE_CHAIN (u) = list;
27838 list = u;
27840 else
27841 list = tree_cons (decl, NULL_TREE, list);
27843 get_comma:
27844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27845 break;
27846 cp_lexer_consume_token (parser->lexer);
27849 if (colon)
27850 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27852 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27854 *colon = true;
27855 cp_parser_require (parser, CPP_COLON, RT_COLON);
27856 return list;
27859 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27861 int ending;
27863 /* Try to resync to an unnested comma. Copied from
27864 cp_parser_parenthesized_expression_list. */
27865 skip_comma:
27866 if (colon)
27867 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27868 ending = cp_parser_skip_to_closing_parenthesis (parser,
27869 /*recovering=*/true,
27870 /*or_comma=*/true,
27871 /*consume_paren=*/true);
27872 if (ending < 0)
27873 goto get_comma;
27876 return list;
27879 /* Similarly, but expect leading and trailing parenthesis. This is a very
27880 common case for omp clauses. */
27882 static tree
27883 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27885 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27886 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27887 return list;
27890 /* OpenACC 2.0:
27891 copy ( variable-list )
27892 copyin ( variable-list )
27893 copyout ( variable-list )
27894 create ( variable-list )
27895 delete ( variable-list )
27896 present ( variable-list )
27897 present_or_copy ( variable-list )
27898 pcopy ( variable-list )
27899 present_or_copyin ( variable-list )
27900 pcopyin ( variable-list )
27901 present_or_copyout ( variable-list )
27902 pcopyout ( variable-list )
27903 present_or_create ( variable-list )
27904 pcreate ( variable-list ) */
27906 static tree
27907 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27908 tree list)
27910 enum gomp_map_kind kind;
27911 switch (c_kind)
27913 case PRAGMA_OACC_CLAUSE_COPY:
27914 kind = GOMP_MAP_FORCE_TOFROM;
27915 break;
27916 case PRAGMA_OACC_CLAUSE_COPYIN:
27917 kind = GOMP_MAP_FORCE_TO;
27918 break;
27919 case PRAGMA_OACC_CLAUSE_COPYOUT:
27920 kind = GOMP_MAP_FORCE_FROM;
27921 break;
27922 case PRAGMA_OACC_CLAUSE_CREATE:
27923 kind = GOMP_MAP_FORCE_ALLOC;
27924 break;
27925 case PRAGMA_OACC_CLAUSE_DELETE:
27926 kind = GOMP_MAP_FORCE_DEALLOC;
27927 break;
27928 case PRAGMA_OACC_CLAUSE_DEVICE:
27929 kind = GOMP_MAP_FORCE_TO;
27930 break;
27931 case PRAGMA_OACC_CLAUSE_HOST:
27932 case PRAGMA_OACC_CLAUSE_SELF:
27933 kind = GOMP_MAP_FORCE_FROM;
27934 break;
27935 case PRAGMA_OACC_CLAUSE_PRESENT:
27936 kind = GOMP_MAP_FORCE_PRESENT;
27937 break;
27938 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27939 kind = GOMP_MAP_TOFROM;
27940 break;
27941 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27942 kind = GOMP_MAP_TO;
27943 break;
27944 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27945 kind = GOMP_MAP_FROM;
27946 break;
27947 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27948 kind = GOMP_MAP_ALLOC;
27949 break;
27950 default:
27951 gcc_unreachable ();
27953 tree nl, c;
27954 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27956 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27957 OMP_CLAUSE_SET_MAP_KIND (c, kind);
27959 return nl;
27962 /* OpenACC 2.0:
27963 deviceptr ( variable-list ) */
27965 static tree
27966 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27968 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27969 tree vars, t;
27971 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27972 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27973 variable-list must only allow for pointer variables. */
27974 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27975 for (t = vars; t; t = TREE_CHAIN (t))
27977 tree v = TREE_PURPOSE (t);
27979 /* FIXME diagnostics: Ideally we should keep individual
27980 locations for all the variables in the var list to make the
27981 following errors more precise. Perhaps
27982 c_parser_omp_var_list_parens should construct a list of
27983 locations to go along with the var list. */
27985 if (TREE_CODE (v) != VAR_DECL)
27986 error_at (loc, "%qD is not a variable", v);
27987 else if (TREE_TYPE (v) == error_mark_node)
27989 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27990 error_at (loc, "%qD is not a pointer variable", v);
27992 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27993 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
27994 OMP_CLAUSE_DECL (u) = v;
27995 OMP_CLAUSE_CHAIN (u) = list;
27996 list = u;
27999 return list;
28002 /* OpenACC:
28003 vector_length ( expression ) */
28005 static tree
28006 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28008 tree t, c;
28009 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28010 bool error = false;
28012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28013 return list;
28015 t = cp_parser_condition (parser);
28016 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28018 error_at (location, "expected positive integer expression");
28019 error = true;
28022 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28025 /*or_comma=*/false,
28026 /*consume_paren=*/true);
28027 return list;
28030 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28031 location);
28033 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28034 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28035 OMP_CLAUSE_CHAIN (c) = list;
28036 list = c;
28038 return list;
28041 /* OpenACC 2.0
28042 Parse wait clause or directive parameters. */
28044 static tree
28045 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28047 vec<tree, va_gc> *args;
28048 tree t, args_tree;
28050 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28051 /*cast_p=*/false,
28052 /*allow_expansion_p=*/true,
28053 /*non_constant_p=*/NULL);
28055 if (args == NULL || args->length () == 0)
28057 cp_parser_error (parser, "expected integer expression before ')'");
28058 if (args != NULL)
28059 release_tree_vector (args);
28060 return list;
28063 args_tree = build_tree_list_vec (args);
28065 release_tree_vector (args);
28067 for (t = args_tree; t; t = TREE_CHAIN (t))
28069 tree targ = TREE_VALUE (t);
28071 if (targ != error_mark_node)
28073 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28074 error ("%<wait%> expression must be integral");
28075 else
28077 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28079 mark_rvalue_use (targ);
28080 OMP_CLAUSE_DECL (c) = targ;
28081 OMP_CLAUSE_CHAIN (c) = list;
28082 list = c;
28087 return list;
28090 /* OpenACC:
28091 wait ( int-expr-list ) */
28093 static tree
28094 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28096 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28098 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28099 return list;
28101 list = cp_parser_oacc_wait_list (parser, location, list);
28103 return list;
28106 /* OpenMP 3.0:
28107 collapse ( constant-expression ) */
28109 static tree
28110 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28112 tree c, num;
28113 location_t loc;
28114 HOST_WIDE_INT n;
28116 loc = cp_lexer_peek_token (parser->lexer)->location;
28117 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28118 return list;
28120 num = cp_parser_constant_expression (parser);
28122 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28124 /*or_comma=*/false,
28125 /*consume_paren=*/true);
28127 if (num == error_mark_node)
28128 return list;
28129 num = fold_non_dependent_expr (num);
28130 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28131 || !tree_fits_shwi_p (num)
28132 || (n = tree_to_shwi (num)) <= 0
28133 || (int) n != n)
28135 error_at (loc, "collapse argument needs positive constant integer expression");
28136 return list;
28139 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28140 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28141 OMP_CLAUSE_CHAIN (c) = list;
28142 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28144 return c;
28147 /* OpenMP 2.5:
28148 default ( shared | none ) */
28150 static tree
28151 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28153 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28154 tree c;
28156 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28157 return list;
28158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28160 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28161 const char *p = IDENTIFIER_POINTER (id);
28163 switch (p[0])
28165 case 'n':
28166 if (strcmp ("none", p) != 0)
28167 goto invalid_kind;
28168 kind = OMP_CLAUSE_DEFAULT_NONE;
28169 break;
28171 case 's':
28172 if (strcmp ("shared", p) != 0)
28173 goto invalid_kind;
28174 kind = OMP_CLAUSE_DEFAULT_SHARED;
28175 break;
28177 default:
28178 goto invalid_kind;
28181 cp_lexer_consume_token (parser->lexer);
28183 else
28185 invalid_kind:
28186 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28191 /*or_comma=*/false,
28192 /*consume_paren=*/true);
28194 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28195 return list;
28197 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28198 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28199 OMP_CLAUSE_CHAIN (c) = list;
28200 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28202 return c;
28205 /* OpenMP 3.1:
28206 final ( expression ) */
28208 static tree
28209 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28211 tree t, c;
28213 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28214 return list;
28216 t = cp_parser_condition (parser);
28218 if (t == error_mark_node
28219 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28221 /*or_comma=*/false,
28222 /*consume_paren=*/true);
28224 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28226 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28227 OMP_CLAUSE_FINAL_EXPR (c) = t;
28228 OMP_CLAUSE_CHAIN (c) = list;
28230 return c;
28233 /* OpenMP 2.5:
28234 if ( expression ) */
28236 static tree
28237 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28239 tree t, c;
28241 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28242 return list;
28244 t = cp_parser_condition (parser);
28246 if (t == error_mark_node
28247 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28249 /*or_comma=*/false,
28250 /*consume_paren=*/true);
28252 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28254 c = build_omp_clause (location, OMP_CLAUSE_IF);
28255 OMP_CLAUSE_IF_EXPR (c) = t;
28256 OMP_CLAUSE_CHAIN (c) = list;
28258 return c;
28261 /* OpenMP 3.1:
28262 mergeable */
28264 static tree
28265 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28266 tree list, location_t location)
28268 tree c;
28270 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28271 location);
28273 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28274 OMP_CLAUSE_CHAIN (c) = list;
28275 return c;
28278 /* OpenMP 2.5:
28279 nowait */
28281 static tree
28282 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28283 tree list, location_t location)
28285 tree c;
28287 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28289 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28290 OMP_CLAUSE_CHAIN (c) = list;
28291 return c;
28294 /* OpenACC:
28295 num_gangs ( expression ) */
28297 static tree
28298 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28300 tree t, c;
28301 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28304 return list;
28306 t = cp_parser_condition (parser);
28308 if (t == error_mark_node
28309 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28310 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28311 /*or_comma=*/false,
28312 /*consume_paren=*/true);
28314 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28316 error_at (location, "expected positive integer expression");
28317 return list;
28320 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28322 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28323 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28324 OMP_CLAUSE_CHAIN (c) = list;
28325 list = c;
28327 return list;
28330 /* OpenMP 2.5:
28331 num_threads ( expression ) */
28333 static tree
28334 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28335 location_t location)
28337 tree t, c;
28339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28340 return list;
28342 t = cp_parser_expression (parser);
28344 if (t == error_mark_node
28345 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28347 /*or_comma=*/false,
28348 /*consume_paren=*/true);
28350 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28351 "num_threads", location);
28353 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28354 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28355 OMP_CLAUSE_CHAIN (c) = list;
28357 return c;
28360 /* OpenACC:
28361 num_workers ( expression ) */
28363 static tree
28364 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28366 tree t, c;
28367 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28369 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28370 return list;
28372 t = cp_parser_condition (parser);
28374 if (t == error_mark_node
28375 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28377 /*or_comma=*/false,
28378 /*consume_paren=*/true);
28380 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28382 error_at (location, "expected positive integer expression");
28383 return list;
28386 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28387 location);
28389 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28390 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28391 OMP_CLAUSE_CHAIN (c) = list;
28392 list = c;
28394 return list;
28397 /* OpenMP 2.5:
28398 ordered */
28400 static tree
28401 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28402 tree list, location_t location)
28404 tree c;
28406 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28407 "ordered", location);
28409 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28410 OMP_CLAUSE_CHAIN (c) = list;
28411 return c;
28414 /* OpenMP 2.5:
28415 reduction ( reduction-operator : variable-list )
28417 reduction-operator:
28418 One of: + * - & ^ | && ||
28420 OpenMP 3.1:
28422 reduction-operator:
28423 One of: + * - & ^ | && || min max
28425 OpenMP 4.0:
28427 reduction-operator:
28428 One of: + * - & ^ | && ||
28429 id-expression */
28431 static tree
28432 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28434 enum tree_code code = ERROR_MARK;
28435 tree nlist, c, id = NULL_TREE;
28437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28438 return list;
28440 switch (cp_lexer_peek_token (parser->lexer)->type)
28442 case CPP_PLUS: code = PLUS_EXPR; break;
28443 case CPP_MULT: code = MULT_EXPR; break;
28444 case CPP_MINUS: code = MINUS_EXPR; break;
28445 case CPP_AND: code = BIT_AND_EXPR; break;
28446 case CPP_XOR: code = BIT_XOR_EXPR; break;
28447 case CPP_OR: code = BIT_IOR_EXPR; break;
28448 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28449 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28450 default: break;
28453 if (code != ERROR_MARK)
28454 cp_lexer_consume_token (parser->lexer);
28455 else
28457 bool saved_colon_corrects_to_scope_p;
28458 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28459 parser->colon_corrects_to_scope_p = false;
28460 id = cp_parser_id_expression (parser, /*template_p=*/false,
28461 /*check_dependency_p=*/true,
28462 /*template_p=*/NULL,
28463 /*declarator_p=*/false,
28464 /*optional_p=*/false);
28465 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28466 if (identifier_p (id))
28468 const char *p = IDENTIFIER_POINTER (id);
28470 if (strcmp (p, "min") == 0)
28471 code = MIN_EXPR;
28472 else if (strcmp (p, "max") == 0)
28473 code = MAX_EXPR;
28474 else if (id == ansi_opname (PLUS_EXPR))
28475 code = PLUS_EXPR;
28476 else if (id == ansi_opname (MULT_EXPR))
28477 code = MULT_EXPR;
28478 else if (id == ansi_opname (MINUS_EXPR))
28479 code = MINUS_EXPR;
28480 else if (id == ansi_opname (BIT_AND_EXPR))
28481 code = BIT_AND_EXPR;
28482 else if (id == ansi_opname (BIT_IOR_EXPR))
28483 code = BIT_IOR_EXPR;
28484 else if (id == ansi_opname (BIT_XOR_EXPR))
28485 code = BIT_XOR_EXPR;
28486 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28487 code = TRUTH_ANDIF_EXPR;
28488 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28489 code = TRUTH_ORIF_EXPR;
28490 id = omp_reduction_id (code, id, NULL_TREE);
28491 tree scope = parser->scope;
28492 if (scope)
28493 id = build_qualified_name (NULL_TREE, scope, id, false);
28494 parser->scope = NULL_TREE;
28495 parser->qualifying_scope = NULL_TREE;
28496 parser->object_scope = NULL_TREE;
28498 else
28500 error ("invalid reduction-identifier");
28501 resync_fail:
28502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28503 /*or_comma=*/false,
28504 /*consume_paren=*/true);
28505 return list;
28509 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28510 goto resync_fail;
28512 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28513 NULL);
28514 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28516 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28517 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28520 return nlist;
28523 /* OpenMP 2.5:
28524 schedule ( schedule-kind )
28525 schedule ( schedule-kind , expression )
28527 schedule-kind:
28528 static | dynamic | guided | runtime | auto */
28530 static tree
28531 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28533 tree c, t;
28535 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28536 return list;
28538 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28543 const char *p = IDENTIFIER_POINTER (id);
28545 switch (p[0])
28547 case 'd':
28548 if (strcmp ("dynamic", p) != 0)
28549 goto invalid_kind;
28550 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28551 break;
28553 case 'g':
28554 if (strcmp ("guided", p) != 0)
28555 goto invalid_kind;
28556 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28557 break;
28559 case 'r':
28560 if (strcmp ("runtime", p) != 0)
28561 goto invalid_kind;
28562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28563 break;
28565 default:
28566 goto invalid_kind;
28569 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28570 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28571 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28572 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28573 else
28574 goto invalid_kind;
28575 cp_lexer_consume_token (parser->lexer);
28577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28579 cp_token *token;
28580 cp_lexer_consume_token (parser->lexer);
28582 token = cp_lexer_peek_token (parser->lexer);
28583 t = cp_parser_assignment_expression (parser);
28585 if (t == error_mark_node)
28586 goto resync_fail;
28587 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28588 error_at (token->location, "schedule %<runtime%> does not take "
28589 "a %<chunk_size%> parameter");
28590 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28591 error_at (token->location, "schedule %<auto%> does not take "
28592 "a %<chunk_size%> parameter");
28593 else
28594 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28596 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28597 goto resync_fail;
28599 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28600 goto resync_fail;
28602 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28603 OMP_CLAUSE_CHAIN (c) = list;
28604 return c;
28606 invalid_kind:
28607 cp_parser_error (parser, "invalid schedule kind");
28608 resync_fail:
28609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28610 /*or_comma=*/false,
28611 /*consume_paren=*/true);
28612 return list;
28615 /* OpenMP 3.0:
28616 untied */
28618 static tree
28619 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28620 tree list, location_t location)
28622 tree c;
28624 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28626 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28627 OMP_CLAUSE_CHAIN (c) = list;
28628 return c;
28631 /* OpenMP 4.0:
28632 inbranch
28633 notinbranch */
28635 static tree
28636 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28637 tree list, location_t location)
28639 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28640 tree c = build_omp_clause (location, code);
28641 OMP_CLAUSE_CHAIN (c) = list;
28642 return c;
28645 /* OpenMP 4.0:
28646 parallel
28648 sections
28649 taskgroup */
28651 static tree
28652 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28653 enum omp_clause_code code,
28654 tree list, location_t location)
28656 tree c = build_omp_clause (location, code);
28657 OMP_CLAUSE_CHAIN (c) = list;
28658 return c;
28661 /* OpenMP 4.0:
28662 num_teams ( expression ) */
28664 static tree
28665 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28666 location_t location)
28668 tree t, c;
28670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28671 return list;
28673 t = cp_parser_expression (parser);
28675 if (t == error_mark_node
28676 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28678 /*or_comma=*/false,
28679 /*consume_paren=*/true);
28681 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28682 "num_teams", location);
28684 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28685 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28686 OMP_CLAUSE_CHAIN (c) = list;
28688 return c;
28691 /* OpenMP 4.0:
28692 thread_limit ( expression ) */
28694 static tree
28695 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28696 location_t location)
28698 tree t, c;
28700 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28701 return list;
28703 t = cp_parser_expression (parser);
28705 if (t == error_mark_node
28706 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28708 /*or_comma=*/false,
28709 /*consume_paren=*/true);
28711 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28712 "thread_limit", location);
28714 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28715 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28716 OMP_CLAUSE_CHAIN (c) = list;
28718 return c;
28721 /* OpenMP 4.0:
28722 aligned ( variable-list )
28723 aligned ( variable-list : constant-expression ) */
28725 static tree
28726 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28728 tree nlist, c, alignment = NULL_TREE;
28729 bool colon;
28731 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28732 return list;
28734 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28735 &colon);
28737 if (colon)
28739 alignment = cp_parser_constant_expression (parser);
28741 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28742 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28743 /*or_comma=*/false,
28744 /*consume_paren=*/true);
28746 if (alignment == error_mark_node)
28747 alignment = NULL_TREE;
28750 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28751 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28753 return nlist;
28756 /* OpenMP 4.0:
28757 linear ( variable-list )
28758 linear ( variable-list : expression ) */
28760 static tree
28761 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28762 bool is_cilk_simd_fn)
28764 tree nlist, c, step = integer_one_node;
28765 bool colon;
28767 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28768 return list;
28770 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28771 &colon);
28773 if (colon)
28775 step = cp_parser_expression (parser);
28777 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28779 sorry ("using parameters for %<linear%> step is not supported yet");
28780 step = integer_one_node;
28782 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28784 /*or_comma=*/false,
28785 /*consume_paren=*/true);
28787 if (step == error_mark_node)
28788 return list;
28791 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28792 OMP_CLAUSE_LINEAR_STEP (c) = step;
28794 return nlist;
28797 /* OpenMP 4.0:
28798 safelen ( constant-expression ) */
28800 static tree
28801 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28802 location_t location)
28804 tree t, c;
28806 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28807 return list;
28809 t = cp_parser_constant_expression (parser);
28811 if (t == error_mark_node
28812 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28813 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28814 /*or_comma=*/false,
28815 /*consume_paren=*/true);
28817 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28819 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28820 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28821 OMP_CLAUSE_CHAIN (c) = list;
28823 return c;
28826 /* OpenMP 4.0:
28827 simdlen ( constant-expression ) */
28829 static tree
28830 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28831 location_t location)
28833 tree t, c;
28835 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28836 return list;
28838 t = cp_parser_constant_expression (parser);
28840 if (t == error_mark_node
28841 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28843 /*or_comma=*/false,
28844 /*consume_paren=*/true);
28846 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28848 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28849 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28850 OMP_CLAUSE_CHAIN (c) = list;
28852 return c;
28855 /* OpenMP 4.0:
28856 depend ( depend-kind : variable-list )
28858 depend-kind:
28859 in | out | inout */
28861 static tree
28862 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28864 tree nlist, c;
28865 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28867 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28868 return list;
28870 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28872 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28873 const char *p = IDENTIFIER_POINTER (id);
28875 if (strcmp ("in", p) == 0)
28876 kind = OMP_CLAUSE_DEPEND_IN;
28877 else if (strcmp ("inout", p) == 0)
28878 kind = OMP_CLAUSE_DEPEND_INOUT;
28879 else if (strcmp ("out", p) == 0)
28880 kind = OMP_CLAUSE_DEPEND_OUT;
28881 else
28882 goto invalid_kind;
28884 else
28885 goto invalid_kind;
28887 cp_lexer_consume_token (parser->lexer);
28888 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28889 goto resync_fail;
28891 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28892 NULL);
28894 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28895 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28897 return nlist;
28899 invalid_kind:
28900 cp_parser_error (parser, "invalid depend kind");
28901 resync_fail:
28902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28903 /*or_comma=*/false,
28904 /*consume_paren=*/true);
28905 return list;
28908 /* OpenMP 4.0:
28909 map ( map-kind : variable-list )
28910 map ( variable-list )
28912 map-kind:
28913 alloc | to | from | tofrom */
28915 static tree
28916 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28918 tree nlist, c;
28919 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28921 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28922 return list;
28924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28925 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28927 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28928 const char *p = IDENTIFIER_POINTER (id);
28930 if (strcmp ("alloc", p) == 0)
28931 kind = GOMP_MAP_ALLOC;
28932 else if (strcmp ("to", p) == 0)
28933 kind = GOMP_MAP_TO;
28934 else if (strcmp ("from", p) == 0)
28935 kind = GOMP_MAP_FROM;
28936 else if (strcmp ("tofrom", p) == 0)
28937 kind = GOMP_MAP_TOFROM;
28938 else
28940 cp_parser_error (parser, "invalid map kind");
28941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28942 /*or_comma=*/false,
28943 /*consume_paren=*/true);
28944 return list;
28946 cp_lexer_consume_token (parser->lexer);
28947 cp_lexer_consume_token (parser->lexer);
28950 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28951 NULL);
28953 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28954 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28956 return nlist;
28959 /* OpenMP 4.0:
28960 device ( expression ) */
28962 static tree
28963 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28964 location_t location)
28966 tree t, c;
28968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28969 return list;
28971 t = cp_parser_expression (parser);
28973 if (t == error_mark_node
28974 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28976 /*or_comma=*/false,
28977 /*consume_paren=*/true);
28979 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28980 "device", location);
28982 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28983 OMP_CLAUSE_DEVICE_ID (c) = t;
28984 OMP_CLAUSE_CHAIN (c) = list;
28986 return c;
28989 /* OpenMP 4.0:
28990 dist_schedule ( static )
28991 dist_schedule ( static , expression ) */
28993 static tree
28994 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28995 location_t location)
28997 tree c, t;
28999 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29000 return list;
29002 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29004 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29005 goto invalid_kind;
29006 cp_lexer_consume_token (parser->lexer);
29008 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29010 cp_lexer_consume_token (parser->lexer);
29012 t = cp_parser_assignment_expression (parser);
29014 if (t == error_mark_node)
29015 goto resync_fail;
29016 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29019 goto resync_fail;
29021 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29022 goto resync_fail;
29024 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29025 location);
29026 OMP_CLAUSE_CHAIN (c) = list;
29027 return c;
29029 invalid_kind:
29030 cp_parser_error (parser, "invalid dist_schedule kind");
29031 resync_fail:
29032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29033 /*or_comma=*/false,
29034 /*consume_paren=*/true);
29035 return list;
29038 /* OpenMP 4.0:
29039 proc_bind ( proc-bind-kind )
29041 proc-bind-kind:
29042 master | close | spread */
29044 static tree
29045 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29046 location_t location)
29048 tree c;
29049 enum omp_clause_proc_bind_kind kind;
29051 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29052 return list;
29054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29057 const char *p = IDENTIFIER_POINTER (id);
29059 if (strcmp ("master", p) == 0)
29060 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29061 else if (strcmp ("close", p) == 0)
29062 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29063 else if (strcmp ("spread", p) == 0)
29064 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29065 else
29066 goto invalid_kind;
29068 else
29069 goto invalid_kind;
29071 cp_lexer_consume_token (parser->lexer);
29072 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29073 goto resync_fail;
29075 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29076 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29077 location);
29078 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29079 OMP_CLAUSE_CHAIN (c) = list;
29080 return c;
29082 invalid_kind:
29083 cp_parser_error (parser, "invalid depend kind");
29084 resync_fail:
29085 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29086 /*or_comma=*/false,
29087 /*consume_paren=*/true);
29088 return list;
29091 /* OpenACC:
29092 async [( int-expr )] */
29094 static tree
29095 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29097 tree c, t;
29098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29100 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29102 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29104 cp_lexer_consume_token (parser->lexer);
29106 t = cp_parser_expression (parser);
29107 if (t == error_mark_node
29108 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29109 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29110 /*or_comma=*/false,
29111 /*consume_paren=*/true);
29114 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29116 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29117 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29118 OMP_CLAUSE_CHAIN (c) = list;
29119 list = c;
29121 return list;
29124 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29125 is a bitmask in MASK. Return the list of clauses found. */
29127 static tree
29128 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29129 const char *where, cp_token *pragma_tok,
29130 bool finish_p = true)
29132 tree clauses = NULL;
29133 bool first = true;
29135 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29137 location_t here;
29138 pragma_omp_clause c_kind;
29139 const char *c_name;
29140 tree prev = clauses;
29142 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29143 cp_lexer_consume_token (parser->lexer);
29145 here = cp_lexer_peek_token (parser->lexer)->location;
29146 c_kind = cp_parser_omp_clause_name (parser);
29148 switch (c_kind)
29150 case PRAGMA_OACC_CLAUSE_ASYNC:
29151 clauses = cp_parser_oacc_clause_async (parser, clauses);
29152 c_name = "async";
29153 break;
29154 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29155 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29156 c_name = "collapse";
29157 break;
29158 case PRAGMA_OACC_CLAUSE_COPY:
29159 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29160 c_name = "copy";
29161 break;
29162 case PRAGMA_OACC_CLAUSE_COPYIN:
29163 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29164 c_name = "copyin";
29165 break;
29166 case PRAGMA_OACC_CLAUSE_COPYOUT:
29167 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29168 c_name = "copyout";
29169 break;
29170 case PRAGMA_OACC_CLAUSE_CREATE:
29171 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29172 c_name = "create";
29173 break;
29174 case PRAGMA_OACC_CLAUSE_DELETE:
29175 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29176 c_name = "delete";
29177 break;
29178 case PRAGMA_OACC_CLAUSE_DEVICE:
29179 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29180 c_name = "device";
29181 break;
29182 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29183 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29184 c_name = "deviceptr";
29185 break;
29186 case PRAGMA_OACC_CLAUSE_HOST:
29187 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29188 c_name = "host";
29189 break;
29190 case PRAGMA_OACC_CLAUSE_IF:
29191 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29192 c_name = "if";
29193 break;
29194 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29195 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29196 c_name = "num_gangs";
29197 break;
29198 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29199 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29200 c_name = "num_workers";
29201 break;
29202 case PRAGMA_OACC_CLAUSE_PRESENT:
29203 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29204 c_name = "present";
29205 break;
29206 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29207 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29208 c_name = "present_or_copy";
29209 break;
29210 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29211 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29212 c_name = "present_or_copyin";
29213 break;
29214 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29215 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29216 c_name = "present_or_copyout";
29217 break;
29218 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29219 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29220 c_name = "present_or_create";
29221 break;
29222 case PRAGMA_OACC_CLAUSE_REDUCTION:
29223 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29224 c_name = "reduction";
29225 break;
29226 case PRAGMA_OACC_CLAUSE_SELF:
29227 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29228 c_name = "self";
29229 break;
29230 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29231 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29232 c_name = "vector_length";
29233 break;
29234 case PRAGMA_OACC_CLAUSE_WAIT:
29235 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29236 c_name = "wait";
29237 break;
29238 default:
29239 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29240 goto saw_error;
29243 first = false;
29245 if (((mask >> c_kind) & 1) == 0)
29247 /* Remove the invalid clause(s) from the list to avoid
29248 confusing the rest of the compiler. */
29249 clauses = prev;
29250 error_at (here, "%qs is not valid for %qs", c_name, where);
29254 saw_error:
29255 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29257 if (finish_p)
29258 return finish_omp_clauses (clauses);
29260 return clauses;
29263 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29264 is a bitmask in MASK. Return the list of clauses found; the result
29265 of clause default goes in *pdefault. */
29267 static tree
29268 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29269 const char *where, cp_token *pragma_tok,
29270 bool finish_p = true)
29272 tree clauses = NULL;
29273 bool first = true;
29274 cp_token *token = NULL;
29275 bool cilk_simd_fn = false;
29277 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29279 pragma_omp_clause c_kind;
29280 const char *c_name;
29281 tree prev = clauses;
29283 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29284 cp_lexer_consume_token (parser->lexer);
29286 token = cp_lexer_peek_token (parser->lexer);
29287 c_kind = cp_parser_omp_clause_name (parser);
29289 switch (c_kind)
29291 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29292 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29293 token->location);
29294 c_name = "collapse";
29295 break;
29296 case PRAGMA_OMP_CLAUSE_COPYIN:
29297 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29298 c_name = "copyin";
29299 break;
29300 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29301 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29302 clauses);
29303 c_name = "copyprivate";
29304 break;
29305 case PRAGMA_OMP_CLAUSE_DEFAULT:
29306 clauses = cp_parser_omp_clause_default (parser, clauses,
29307 token->location);
29308 c_name = "default";
29309 break;
29310 case PRAGMA_OMP_CLAUSE_FINAL:
29311 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29312 c_name = "final";
29313 break;
29314 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29315 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29316 clauses);
29317 c_name = "firstprivate";
29318 break;
29319 case PRAGMA_OMP_CLAUSE_IF:
29320 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29321 c_name = "if";
29322 break;
29323 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29325 clauses);
29326 c_name = "lastprivate";
29327 break;
29328 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29329 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29330 token->location);
29331 c_name = "mergeable";
29332 break;
29333 case PRAGMA_OMP_CLAUSE_NOWAIT:
29334 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29335 c_name = "nowait";
29336 break;
29337 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29338 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29339 token->location);
29340 c_name = "num_threads";
29341 break;
29342 case PRAGMA_OMP_CLAUSE_ORDERED:
29343 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29344 token->location);
29345 c_name = "ordered";
29346 break;
29347 case PRAGMA_OMP_CLAUSE_PRIVATE:
29348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29349 clauses);
29350 c_name = "private";
29351 break;
29352 case PRAGMA_OMP_CLAUSE_REDUCTION:
29353 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29354 c_name = "reduction";
29355 break;
29356 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29357 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29358 token->location);
29359 c_name = "schedule";
29360 break;
29361 case PRAGMA_OMP_CLAUSE_SHARED:
29362 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29363 clauses);
29364 c_name = "shared";
29365 break;
29366 case PRAGMA_OMP_CLAUSE_UNTIED:
29367 clauses = cp_parser_omp_clause_untied (parser, clauses,
29368 token->location);
29369 c_name = "untied";
29370 break;
29371 case PRAGMA_OMP_CLAUSE_INBRANCH:
29372 case PRAGMA_CILK_CLAUSE_MASK:
29373 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29374 clauses, token->location);
29375 c_name = "inbranch";
29376 break;
29377 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29378 case PRAGMA_CILK_CLAUSE_NOMASK:
29379 clauses = cp_parser_omp_clause_branch (parser,
29380 OMP_CLAUSE_NOTINBRANCH,
29381 clauses, token->location);
29382 c_name = "notinbranch";
29383 break;
29384 case PRAGMA_OMP_CLAUSE_PARALLEL:
29385 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29386 clauses, token->location);
29387 c_name = "parallel";
29388 if (!first)
29390 clause_not_first:
29391 error_at (token->location, "%qs must be the first clause of %qs",
29392 c_name, where);
29393 clauses = prev;
29395 break;
29396 case PRAGMA_OMP_CLAUSE_FOR:
29397 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29398 clauses, token->location);
29399 c_name = "for";
29400 if (!first)
29401 goto clause_not_first;
29402 break;
29403 case PRAGMA_OMP_CLAUSE_SECTIONS:
29404 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29405 clauses, token->location);
29406 c_name = "sections";
29407 if (!first)
29408 goto clause_not_first;
29409 break;
29410 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29411 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29412 clauses, token->location);
29413 c_name = "taskgroup";
29414 if (!first)
29415 goto clause_not_first;
29416 break;
29417 case PRAGMA_OMP_CLAUSE_TO:
29418 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29419 clauses);
29420 c_name = "to";
29421 break;
29422 case PRAGMA_OMP_CLAUSE_FROM:
29423 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29424 clauses);
29425 c_name = "from";
29426 break;
29427 case PRAGMA_OMP_CLAUSE_UNIFORM:
29428 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29429 clauses);
29430 c_name = "uniform";
29431 break;
29432 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29433 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29434 token->location);
29435 c_name = "num_teams";
29436 break;
29437 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29438 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29439 token->location);
29440 c_name = "thread_limit";
29441 break;
29442 case PRAGMA_OMP_CLAUSE_ALIGNED:
29443 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29444 c_name = "aligned";
29445 break;
29446 case PRAGMA_OMP_CLAUSE_LINEAR:
29447 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29448 cilk_simd_fn = true;
29449 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29450 c_name = "linear";
29451 break;
29452 case PRAGMA_OMP_CLAUSE_DEPEND:
29453 clauses = cp_parser_omp_clause_depend (parser, clauses);
29454 c_name = "depend";
29455 break;
29456 case PRAGMA_OMP_CLAUSE_MAP:
29457 clauses = cp_parser_omp_clause_map (parser, clauses);
29458 c_name = "map";
29459 break;
29460 case PRAGMA_OMP_CLAUSE_DEVICE:
29461 clauses = cp_parser_omp_clause_device (parser, clauses,
29462 token->location);
29463 c_name = "device";
29464 break;
29465 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29466 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29467 token->location);
29468 c_name = "dist_schedule";
29469 break;
29470 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29471 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29472 token->location);
29473 c_name = "proc_bind";
29474 break;
29475 case PRAGMA_OMP_CLAUSE_SAFELEN:
29476 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29477 token->location);
29478 c_name = "safelen";
29479 break;
29480 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29481 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29482 token->location);
29483 c_name = "simdlen";
29484 break;
29485 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29486 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29487 c_name = "simdlen";
29488 break;
29489 default:
29490 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29491 goto saw_error;
29494 first = false;
29496 if (((mask >> c_kind) & 1) == 0)
29498 /* Remove the invalid clause(s) from the list to avoid
29499 confusing the rest of the compiler. */
29500 clauses = prev;
29501 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29504 saw_error:
29505 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29506 no reason to skip to the end. */
29507 if (!(flag_cilkplus && pragma_tok == NULL))
29508 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29509 if (finish_p)
29510 return finish_omp_clauses (clauses);
29511 return clauses;
29514 /* OpenMP 2.5:
29515 structured-block:
29516 statement
29518 In practice, we're also interested in adding the statement to an
29519 outer node. So it is convenient if we work around the fact that
29520 cp_parser_statement calls add_stmt. */
29522 static unsigned
29523 cp_parser_begin_omp_structured_block (cp_parser *parser)
29525 unsigned save = parser->in_statement;
29527 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29528 This preserves the "not within loop or switch" style error messages
29529 for nonsense cases like
29530 void foo() {
29531 #pragma omp single
29532 break;
29535 if (parser->in_statement)
29536 parser->in_statement = IN_OMP_BLOCK;
29538 return save;
29541 static void
29542 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29544 parser->in_statement = save;
29547 static tree
29548 cp_parser_omp_structured_block (cp_parser *parser)
29550 tree stmt = begin_omp_structured_block ();
29551 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29553 cp_parser_statement (parser, NULL_TREE, false, NULL);
29555 cp_parser_end_omp_structured_block (parser, save);
29556 return finish_omp_structured_block (stmt);
29559 /* OpenMP 2.5:
29560 # pragma omp atomic new-line
29561 expression-stmt
29563 expression-stmt:
29564 x binop= expr | x++ | ++x | x-- | --x
29565 binop:
29566 +, *, -, /, &, ^, |, <<, >>
29568 where x is an lvalue expression with scalar type.
29570 OpenMP 3.1:
29571 # pragma omp atomic new-line
29572 update-stmt
29574 # pragma omp atomic read new-line
29575 read-stmt
29577 # pragma omp atomic write new-line
29578 write-stmt
29580 # pragma omp atomic update new-line
29581 update-stmt
29583 # pragma omp atomic capture new-line
29584 capture-stmt
29586 # pragma omp atomic capture new-line
29587 capture-block
29589 read-stmt:
29590 v = x
29591 write-stmt:
29592 x = expr
29593 update-stmt:
29594 expression-stmt | x = x binop expr
29595 capture-stmt:
29596 v = expression-stmt
29597 capture-block:
29598 { v = x; update-stmt; } | { update-stmt; v = x; }
29600 OpenMP 4.0:
29601 update-stmt:
29602 expression-stmt | x = x binop expr | x = expr binop x
29603 capture-stmt:
29604 v = update-stmt
29605 capture-block:
29606 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29608 where x and v are lvalue expressions with scalar type. */
29610 static void
29611 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29613 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29614 tree rhs1 = NULL_TREE, orig_lhs;
29615 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29616 bool structured_block = false;
29617 bool seq_cst = false;
29619 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29621 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29622 const char *p = IDENTIFIER_POINTER (id);
29624 if (!strcmp (p, "seq_cst"))
29626 seq_cst = true;
29627 cp_lexer_consume_token (parser->lexer);
29628 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29629 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29630 cp_lexer_consume_token (parser->lexer);
29633 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29635 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29636 const char *p = IDENTIFIER_POINTER (id);
29638 if (!strcmp (p, "read"))
29639 code = OMP_ATOMIC_READ;
29640 else if (!strcmp (p, "write"))
29641 code = NOP_EXPR;
29642 else if (!strcmp (p, "update"))
29643 code = OMP_ATOMIC;
29644 else if (!strcmp (p, "capture"))
29645 code = OMP_ATOMIC_CAPTURE_NEW;
29646 else
29647 p = NULL;
29648 if (p)
29649 cp_lexer_consume_token (parser->lexer);
29651 if (!seq_cst)
29653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29654 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29655 cp_lexer_consume_token (parser->lexer);
29657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29660 const char *p = IDENTIFIER_POINTER (id);
29662 if (!strcmp (p, "seq_cst"))
29664 seq_cst = true;
29665 cp_lexer_consume_token (parser->lexer);
29669 cp_parser_require_pragma_eol (parser, pragma_tok);
29671 switch (code)
29673 case OMP_ATOMIC_READ:
29674 case NOP_EXPR: /* atomic write */
29675 v = cp_parser_unary_expression (parser);
29676 if (v == error_mark_node)
29677 goto saw_error;
29678 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29679 goto saw_error;
29680 if (code == NOP_EXPR)
29681 lhs = cp_parser_expression (parser);
29682 else
29683 lhs = cp_parser_unary_expression (parser);
29684 if (lhs == error_mark_node)
29685 goto saw_error;
29686 if (code == NOP_EXPR)
29688 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29689 opcode. */
29690 code = OMP_ATOMIC;
29691 rhs = lhs;
29692 lhs = v;
29693 v = NULL_TREE;
29695 goto done;
29696 case OMP_ATOMIC_CAPTURE_NEW:
29697 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29699 cp_lexer_consume_token (parser->lexer);
29700 structured_block = true;
29702 else
29704 v = cp_parser_unary_expression (parser);
29705 if (v == error_mark_node)
29706 goto saw_error;
29707 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29708 goto saw_error;
29710 default:
29711 break;
29714 restart:
29715 lhs = cp_parser_unary_expression (parser);
29716 orig_lhs = lhs;
29717 switch (TREE_CODE (lhs))
29719 case ERROR_MARK:
29720 goto saw_error;
29722 case POSTINCREMENT_EXPR:
29723 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29724 code = OMP_ATOMIC_CAPTURE_OLD;
29725 /* FALLTHROUGH */
29726 case PREINCREMENT_EXPR:
29727 lhs = TREE_OPERAND (lhs, 0);
29728 opcode = PLUS_EXPR;
29729 rhs = integer_one_node;
29730 break;
29732 case POSTDECREMENT_EXPR:
29733 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29734 code = OMP_ATOMIC_CAPTURE_OLD;
29735 /* FALLTHROUGH */
29736 case PREDECREMENT_EXPR:
29737 lhs = TREE_OPERAND (lhs, 0);
29738 opcode = MINUS_EXPR;
29739 rhs = integer_one_node;
29740 break;
29742 case COMPOUND_EXPR:
29743 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29744 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29745 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29746 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29747 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29748 (TREE_OPERAND (lhs, 1), 0), 0)))
29749 == BOOLEAN_TYPE)
29750 /* Undo effects of boolean_increment for post {in,de}crement. */
29751 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29752 /* FALLTHRU */
29753 case MODIFY_EXPR:
29754 if (TREE_CODE (lhs) == MODIFY_EXPR
29755 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29757 /* Undo effects of boolean_increment. */
29758 if (integer_onep (TREE_OPERAND (lhs, 1)))
29760 /* This is pre or post increment. */
29761 rhs = TREE_OPERAND (lhs, 1);
29762 lhs = TREE_OPERAND (lhs, 0);
29763 opcode = NOP_EXPR;
29764 if (code == OMP_ATOMIC_CAPTURE_NEW
29765 && !structured_block
29766 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29767 code = OMP_ATOMIC_CAPTURE_OLD;
29768 break;
29771 /* FALLTHRU */
29772 default:
29773 switch (cp_lexer_peek_token (parser->lexer)->type)
29775 case CPP_MULT_EQ:
29776 opcode = MULT_EXPR;
29777 break;
29778 case CPP_DIV_EQ:
29779 opcode = TRUNC_DIV_EXPR;
29780 break;
29781 case CPP_PLUS_EQ:
29782 opcode = PLUS_EXPR;
29783 break;
29784 case CPP_MINUS_EQ:
29785 opcode = MINUS_EXPR;
29786 break;
29787 case CPP_LSHIFT_EQ:
29788 opcode = LSHIFT_EXPR;
29789 break;
29790 case CPP_RSHIFT_EQ:
29791 opcode = RSHIFT_EXPR;
29792 break;
29793 case CPP_AND_EQ:
29794 opcode = BIT_AND_EXPR;
29795 break;
29796 case CPP_OR_EQ:
29797 opcode = BIT_IOR_EXPR;
29798 break;
29799 case CPP_XOR_EQ:
29800 opcode = BIT_XOR_EXPR;
29801 break;
29802 case CPP_EQ:
29803 enum cp_parser_prec oprec;
29804 cp_token *token;
29805 cp_lexer_consume_token (parser->lexer);
29806 cp_parser_parse_tentatively (parser);
29807 rhs1 = cp_parser_simple_cast_expression (parser);
29808 if (rhs1 == error_mark_node)
29810 cp_parser_abort_tentative_parse (parser);
29811 cp_parser_simple_cast_expression (parser);
29812 goto saw_error;
29814 token = cp_lexer_peek_token (parser->lexer);
29815 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29817 cp_parser_abort_tentative_parse (parser);
29818 cp_parser_parse_tentatively (parser);
29819 rhs = cp_parser_binary_expression (parser, false, true,
29820 PREC_NOT_OPERATOR, NULL);
29821 if (rhs == error_mark_node)
29823 cp_parser_abort_tentative_parse (parser);
29824 cp_parser_binary_expression (parser, false, true,
29825 PREC_NOT_OPERATOR, NULL);
29826 goto saw_error;
29828 switch (TREE_CODE (rhs))
29830 case MULT_EXPR:
29831 case TRUNC_DIV_EXPR:
29832 case RDIV_EXPR:
29833 case PLUS_EXPR:
29834 case MINUS_EXPR:
29835 case LSHIFT_EXPR:
29836 case RSHIFT_EXPR:
29837 case BIT_AND_EXPR:
29838 case BIT_IOR_EXPR:
29839 case BIT_XOR_EXPR:
29840 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29842 if (cp_parser_parse_definitely (parser))
29844 opcode = TREE_CODE (rhs);
29845 rhs1 = TREE_OPERAND (rhs, 0);
29846 rhs = TREE_OPERAND (rhs, 1);
29847 goto stmt_done;
29849 else
29850 goto saw_error;
29852 break;
29853 default:
29854 break;
29856 cp_parser_abort_tentative_parse (parser);
29857 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29859 rhs = cp_parser_expression (parser);
29860 if (rhs == error_mark_node)
29861 goto saw_error;
29862 opcode = NOP_EXPR;
29863 rhs1 = NULL_TREE;
29864 goto stmt_done;
29866 cp_parser_error (parser,
29867 "invalid form of %<#pragma omp atomic%>");
29868 goto saw_error;
29870 if (!cp_parser_parse_definitely (parser))
29871 goto saw_error;
29872 switch (token->type)
29874 case CPP_SEMICOLON:
29875 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29877 code = OMP_ATOMIC_CAPTURE_OLD;
29878 v = lhs;
29879 lhs = NULL_TREE;
29880 lhs1 = rhs1;
29881 rhs1 = NULL_TREE;
29882 cp_lexer_consume_token (parser->lexer);
29883 goto restart;
29885 else if (structured_block)
29887 opcode = NOP_EXPR;
29888 rhs = rhs1;
29889 rhs1 = NULL_TREE;
29890 goto stmt_done;
29892 cp_parser_error (parser,
29893 "invalid form of %<#pragma omp atomic%>");
29894 goto saw_error;
29895 case CPP_MULT:
29896 opcode = MULT_EXPR;
29897 break;
29898 case CPP_DIV:
29899 opcode = TRUNC_DIV_EXPR;
29900 break;
29901 case CPP_PLUS:
29902 opcode = PLUS_EXPR;
29903 break;
29904 case CPP_MINUS:
29905 opcode = MINUS_EXPR;
29906 break;
29907 case CPP_LSHIFT:
29908 opcode = LSHIFT_EXPR;
29909 break;
29910 case CPP_RSHIFT:
29911 opcode = RSHIFT_EXPR;
29912 break;
29913 case CPP_AND:
29914 opcode = BIT_AND_EXPR;
29915 break;
29916 case CPP_OR:
29917 opcode = BIT_IOR_EXPR;
29918 break;
29919 case CPP_XOR:
29920 opcode = BIT_XOR_EXPR;
29921 break;
29922 default:
29923 cp_parser_error (parser,
29924 "invalid operator for %<#pragma omp atomic%>");
29925 goto saw_error;
29927 oprec = TOKEN_PRECEDENCE (token);
29928 gcc_assert (oprec != PREC_NOT_OPERATOR);
29929 if (commutative_tree_code (opcode))
29930 oprec = (enum cp_parser_prec) (oprec - 1);
29931 cp_lexer_consume_token (parser->lexer);
29932 rhs = cp_parser_binary_expression (parser, false, false,
29933 oprec, NULL);
29934 if (rhs == error_mark_node)
29935 goto saw_error;
29936 goto stmt_done;
29937 /* FALLTHROUGH */
29938 default:
29939 cp_parser_error (parser,
29940 "invalid operator for %<#pragma omp atomic%>");
29941 goto saw_error;
29943 cp_lexer_consume_token (parser->lexer);
29945 rhs = cp_parser_expression (parser);
29946 if (rhs == error_mark_node)
29947 goto saw_error;
29948 break;
29950 stmt_done:
29951 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29953 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29954 goto saw_error;
29955 v = cp_parser_unary_expression (parser);
29956 if (v == error_mark_node)
29957 goto saw_error;
29958 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29959 goto saw_error;
29960 lhs1 = cp_parser_unary_expression (parser);
29961 if (lhs1 == error_mark_node)
29962 goto saw_error;
29964 if (structured_block)
29966 cp_parser_consume_semicolon_at_end_of_statement (parser);
29967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29969 done:
29970 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29971 if (!structured_block)
29972 cp_parser_consume_semicolon_at_end_of_statement (parser);
29973 return;
29975 saw_error:
29976 cp_parser_skip_to_end_of_block_or_statement (parser);
29977 if (structured_block)
29979 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29980 cp_lexer_consume_token (parser->lexer);
29981 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29983 cp_parser_skip_to_end_of_block_or_statement (parser);
29984 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29985 cp_lexer_consume_token (parser->lexer);
29991 /* OpenMP 2.5:
29992 # pragma omp barrier new-line */
29994 static void
29995 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29997 cp_parser_require_pragma_eol (parser, pragma_tok);
29998 finish_omp_barrier ();
30001 /* OpenMP 2.5:
30002 # pragma omp critical [(name)] new-line
30003 structured-block */
30005 static tree
30006 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30008 tree stmt, name = NULL;
30010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30012 cp_lexer_consume_token (parser->lexer);
30014 name = cp_parser_identifier (parser);
30016 if (name == error_mark_node
30017 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30018 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30019 /*or_comma=*/false,
30020 /*consume_paren=*/true);
30021 if (name == error_mark_node)
30022 name = NULL;
30024 cp_parser_require_pragma_eol (parser, pragma_tok);
30026 stmt = cp_parser_omp_structured_block (parser);
30027 return c_finish_omp_critical (input_location, stmt, name);
30030 /* OpenMP 2.5:
30031 # pragma omp flush flush-vars[opt] new-line
30033 flush-vars:
30034 ( variable-list ) */
30036 static void
30037 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30040 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30041 cp_parser_require_pragma_eol (parser, pragma_tok);
30043 finish_omp_flush ();
30046 /* Helper function, to parse omp for increment expression. */
30048 static tree
30049 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30051 tree cond = cp_parser_binary_expression (parser, false, true,
30052 PREC_NOT_OPERATOR, NULL);
30053 if (cond == error_mark_node
30054 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30056 cp_parser_skip_to_end_of_statement (parser);
30057 return error_mark_node;
30060 switch (TREE_CODE (cond))
30062 case GT_EXPR:
30063 case GE_EXPR:
30064 case LT_EXPR:
30065 case LE_EXPR:
30066 break;
30067 case NE_EXPR:
30068 if (code == CILK_SIMD || code == CILK_FOR)
30069 break;
30070 /* Fall through: OpenMP disallows NE_EXPR. */
30071 default:
30072 return error_mark_node;
30075 /* If decl is an iterator, preserve LHS and RHS of the relational
30076 expr until finish_omp_for. */
30077 if (decl
30078 && (type_dependent_expression_p (decl)
30079 || CLASS_TYPE_P (TREE_TYPE (decl))))
30080 return cond;
30082 return build_x_binary_op (input_location, TREE_CODE (cond),
30083 TREE_OPERAND (cond, 0), ERROR_MARK,
30084 TREE_OPERAND (cond, 1), ERROR_MARK,
30085 /*overload=*/NULL, tf_warning_or_error);
30088 /* Helper function, to parse omp for increment expression. */
30090 static tree
30091 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30093 cp_token *token = cp_lexer_peek_token (parser->lexer);
30094 enum tree_code op;
30095 tree lhs, rhs;
30096 cp_id_kind idk;
30097 bool decl_first;
30099 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30101 op = (token->type == CPP_PLUS_PLUS
30102 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30103 cp_lexer_consume_token (parser->lexer);
30104 lhs = cp_parser_simple_cast_expression (parser);
30105 if (lhs != decl)
30106 return error_mark_node;
30107 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30110 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30111 if (lhs != decl)
30112 return error_mark_node;
30114 token = cp_lexer_peek_token (parser->lexer);
30115 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30117 op = (token->type == CPP_PLUS_PLUS
30118 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30119 cp_lexer_consume_token (parser->lexer);
30120 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30123 op = cp_parser_assignment_operator_opt (parser);
30124 if (op == ERROR_MARK)
30125 return error_mark_node;
30127 if (op != NOP_EXPR)
30129 rhs = cp_parser_assignment_expression (parser);
30130 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30131 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30134 lhs = cp_parser_binary_expression (parser, false, false,
30135 PREC_ADDITIVE_EXPRESSION, NULL);
30136 token = cp_lexer_peek_token (parser->lexer);
30137 decl_first = lhs == decl;
30138 if (decl_first)
30139 lhs = NULL_TREE;
30140 if (token->type != CPP_PLUS
30141 && token->type != CPP_MINUS)
30142 return error_mark_node;
30146 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30147 cp_lexer_consume_token (parser->lexer);
30148 rhs = cp_parser_binary_expression (parser, false, false,
30149 PREC_ADDITIVE_EXPRESSION, NULL);
30150 token = cp_lexer_peek_token (parser->lexer);
30151 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30153 if (lhs == NULL_TREE)
30155 if (op == PLUS_EXPR)
30156 lhs = rhs;
30157 else
30158 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30159 tf_warning_or_error);
30161 else
30162 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30163 ERROR_MARK, NULL, tf_warning_or_error);
30166 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30168 if (!decl_first)
30170 if (rhs != decl || op == MINUS_EXPR)
30171 return error_mark_node;
30172 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30174 else
30175 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30177 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30180 /* Parse the initialization statement of either an OpenMP for loop or
30181 a Cilk Plus for loop.
30183 Return true if the resulting construct should have an
30184 OMP_CLAUSE_PRIVATE added to it. */
30186 static bool
30187 cp_parser_omp_for_loop_init (cp_parser *parser,
30188 enum tree_code code,
30189 tree &this_pre_body,
30190 vec<tree, va_gc> *for_block,
30191 tree &init,
30192 tree &decl,
30193 tree &real_decl)
30195 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30196 return false;
30198 bool add_private_clause = false;
30200 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30202 init-expr:
30203 var = lb
30204 integer-type var = lb
30205 random-access-iterator-type var = lb
30206 pointer-type var = lb
30208 cp_decl_specifier_seq type_specifiers;
30210 /* First, try to parse as an initialized declaration. See
30211 cp_parser_condition, from whence the bulk of this is copied. */
30213 cp_parser_parse_tentatively (parser);
30214 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30215 /*is_trailing_return=*/false,
30216 &type_specifiers);
30217 if (cp_parser_parse_definitely (parser))
30219 /* If parsing a type specifier seq succeeded, then this
30220 MUST be a initialized declaration. */
30221 tree asm_specification, attributes;
30222 cp_declarator *declarator;
30224 declarator = cp_parser_declarator (parser,
30225 CP_PARSER_DECLARATOR_NAMED,
30226 /*ctor_dtor_or_conv_p=*/NULL,
30227 /*parenthesized_p=*/NULL,
30228 /*member_p=*/false,
30229 /*friend_p=*/false);
30230 attributes = cp_parser_attributes_opt (parser);
30231 asm_specification = cp_parser_asm_specification_opt (parser);
30233 if (declarator == cp_error_declarator)
30234 cp_parser_skip_to_end_of_statement (parser);
30236 else
30238 tree pushed_scope, auto_node;
30240 decl = start_decl (declarator, &type_specifiers,
30241 SD_INITIALIZED, attributes,
30242 /*prefix_attributes=*/NULL_TREE,
30243 &pushed_scope);
30245 auto_node = type_uses_auto (TREE_TYPE (decl));
30246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30248 if (cp_lexer_next_token_is (parser->lexer,
30249 CPP_OPEN_PAREN))
30251 if (code != CILK_SIMD && code != CILK_FOR)
30252 error ("parenthesized initialization is not allowed in "
30253 "OpenMP %<for%> loop");
30254 else
30255 error ("parenthesized initialization is "
30256 "not allowed in for-loop");
30258 else
30259 /* Trigger an error. */
30260 cp_parser_require (parser, CPP_EQ, RT_EQ);
30262 init = error_mark_node;
30263 cp_parser_skip_to_end_of_statement (parser);
30265 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30266 || type_dependent_expression_p (decl)
30267 || auto_node)
30269 bool is_direct_init, is_non_constant_init;
30271 init = cp_parser_initializer (parser,
30272 &is_direct_init,
30273 &is_non_constant_init);
30275 if (auto_node)
30277 TREE_TYPE (decl)
30278 = do_auto_deduction (TREE_TYPE (decl), init,
30279 auto_node);
30281 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30282 && !type_dependent_expression_p (decl))
30283 goto non_class;
30286 cp_finish_decl (decl, init, !is_non_constant_init,
30287 asm_specification,
30288 LOOKUP_ONLYCONVERTING);
30289 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30291 vec_safe_push (for_block, this_pre_body);
30292 init = NULL_TREE;
30294 else
30295 init = pop_stmt_list (this_pre_body);
30296 this_pre_body = NULL_TREE;
30298 else
30300 /* Consume '='. */
30301 cp_lexer_consume_token (parser->lexer);
30302 init = cp_parser_assignment_expression (parser);
30304 non_class:
30305 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30306 init = error_mark_node;
30307 else
30308 cp_finish_decl (decl, NULL_TREE,
30309 /*init_const_expr_p=*/false,
30310 asm_specification,
30311 LOOKUP_ONLYCONVERTING);
30314 if (pushed_scope)
30315 pop_scope (pushed_scope);
30318 else
30320 cp_id_kind idk;
30321 /* If parsing a type specifier sequence failed, then
30322 this MUST be a simple expression. */
30323 if (code == CILK_FOR)
30324 error ("%<_Cilk_for%> allows expression instead of declaration only "
30325 "in C, not in C++");
30326 cp_parser_parse_tentatively (parser);
30327 decl = cp_parser_primary_expression (parser, false, false,
30328 false, &idk);
30329 if (!cp_parser_error_occurred (parser)
30330 && decl
30331 && DECL_P (decl)
30332 && CLASS_TYPE_P (TREE_TYPE (decl)))
30334 tree rhs;
30336 cp_parser_parse_definitely (parser);
30337 cp_parser_require (parser, CPP_EQ, RT_EQ);
30338 rhs = cp_parser_assignment_expression (parser);
30339 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30340 decl, NOP_EXPR,
30341 rhs,
30342 tf_warning_or_error));
30343 add_private_clause = true;
30345 else
30347 decl = NULL;
30348 cp_parser_abort_tentative_parse (parser);
30349 init = cp_parser_expression (parser);
30350 if (init)
30352 if (TREE_CODE (init) == MODIFY_EXPR
30353 || TREE_CODE (init) == MODOP_EXPR)
30354 real_decl = TREE_OPERAND (init, 0);
30358 return add_private_clause;
30361 /* Parse the restricted form of the for statement allowed by OpenMP. */
30363 static tree
30364 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30365 tree *cclauses)
30367 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30368 tree real_decl, initv, condv, incrv, declv;
30369 tree this_pre_body, cl;
30370 location_t loc_first;
30371 bool collapse_err = false;
30372 int i, collapse = 1, nbraces = 0;
30373 vec<tree, va_gc> *for_block = make_tree_vector ();
30375 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30376 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30377 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30379 gcc_assert (collapse >= 1);
30381 declv = make_tree_vec (collapse);
30382 initv = make_tree_vec (collapse);
30383 condv = make_tree_vec (collapse);
30384 incrv = make_tree_vec (collapse);
30386 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30388 for (i = 0; i < collapse; i++)
30390 int bracecount = 0;
30391 bool add_private_clause = false;
30392 location_t loc;
30394 if (code != CILK_FOR
30395 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30397 cp_parser_error (parser, "for statement expected");
30398 return NULL;
30400 if (code == CILK_FOR
30401 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30403 cp_parser_error (parser, "_Cilk_for statement expected");
30404 return NULL;
30406 loc = cp_lexer_consume_token (parser->lexer)->location;
30408 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30409 return NULL;
30411 init = decl = real_decl = NULL;
30412 this_pre_body = push_stmt_list ();
30414 add_private_clause
30415 |= cp_parser_omp_for_loop_init (parser, code,
30416 this_pre_body, for_block,
30417 init, decl, real_decl);
30419 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30420 if (this_pre_body)
30422 this_pre_body = pop_stmt_list (this_pre_body);
30423 if (pre_body)
30425 tree t = pre_body;
30426 pre_body = push_stmt_list ();
30427 add_stmt (t);
30428 add_stmt (this_pre_body);
30429 pre_body = pop_stmt_list (pre_body);
30431 else
30432 pre_body = this_pre_body;
30435 if (decl)
30436 real_decl = decl;
30437 if (cclauses != NULL
30438 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30439 && real_decl != NULL_TREE)
30441 tree *c;
30442 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30443 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30444 && OMP_CLAUSE_DECL (*c) == real_decl)
30446 error_at (loc, "iteration variable %qD"
30447 " should not be firstprivate", real_decl);
30448 *c = OMP_CLAUSE_CHAIN (*c);
30450 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30451 && OMP_CLAUSE_DECL (*c) == real_decl)
30453 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30454 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30455 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30456 OMP_CLAUSE_DECL (l) = real_decl;
30457 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30458 if (code == OMP_SIMD)
30460 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30461 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30463 else
30465 OMP_CLAUSE_CHAIN (l) = clauses;
30466 clauses = l;
30468 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30469 CP_OMP_CLAUSE_INFO (*c) = NULL;
30470 add_private_clause = false;
30472 else
30474 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30475 && OMP_CLAUSE_DECL (*c) == real_decl)
30476 add_private_clause = false;
30477 c = &OMP_CLAUSE_CHAIN (*c);
30481 if (add_private_clause)
30483 tree c;
30484 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30486 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30487 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30488 && OMP_CLAUSE_DECL (c) == decl)
30489 break;
30490 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30491 && OMP_CLAUSE_DECL (c) == decl)
30492 error_at (loc, "iteration variable %qD "
30493 "should not be firstprivate",
30494 decl);
30495 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30496 && OMP_CLAUSE_DECL (c) == decl)
30497 error_at (loc, "iteration variable %qD should not be reduction",
30498 decl);
30500 if (c == NULL)
30502 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30503 OMP_CLAUSE_DECL (c) = decl;
30504 c = finish_omp_clauses (c);
30505 if (c)
30507 OMP_CLAUSE_CHAIN (c) = clauses;
30508 clauses = c;
30513 cond = NULL;
30514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30515 cond = cp_parser_omp_for_cond (parser, decl, code);
30516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30518 incr = NULL;
30519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30521 /* If decl is an iterator, preserve the operator on decl
30522 until finish_omp_for. */
30523 if (real_decl
30524 && ((processing_template_decl
30525 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30526 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30527 incr = cp_parser_omp_for_incr (parser, real_decl);
30528 else
30529 incr = cp_parser_expression (parser);
30530 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30531 SET_EXPR_LOCATION (incr, input_location);
30534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30536 /*or_comma=*/false,
30537 /*consume_paren=*/true);
30539 TREE_VEC_ELT (declv, i) = decl;
30540 TREE_VEC_ELT (initv, i) = init;
30541 TREE_VEC_ELT (condv, i) = cond;
30542 TREE_VEC_ELT (incrv, i) = incr;
30544 if (i == collapse - 1)
30545 break;
30547 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30548 in between the collapsed for loops to be still considered perfectly
30549 nested. Hopefully the final version clarifies this.
30550 For now handle (multiple) {'s and empty statements. */
30551 cp_parser_parse_tentatively (parser);
30554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30555 break;
30556 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30558 cp_lexer_consume_token (parser->lexer);
30559 bracecount++;
30561 else if (bracecount
30562 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30563 cp_lexer_consume_token (parser->lexer);
30564 else
30566 loc = cp_lexer_peek_token (parser->lexer)->location;
30567 error_at (loc, "not enough collapsed for loops");
30568 collapse_err = true;
30569 cp_parser_abort_tentative_parse (parser);
30570 declv = NULL_TREE;
30571 break;
30574 while (1);
30576 if (declv)
30578 cp_parser_parse_definitely (parser);
30579 nbraces += bracecount;
30583 /* Note that we saved the original contents of this flag when we entered
30584 the structured block, and so we don't need to re-save it here. */
30585 if (code == CILK_SIMD || code == CILK_FOR)
30586 parser->in_statement = IN_CILK_SIMD_FOR;
30587 else
30588 parser->in_statement = IN_OMP_FOR;
30590 /* Note that the grammar doesn't call for a structured block here,
30591 though the loop as a whole is a structured block. */
30592 body = push_stmt_list ();
30593 cp_parser_statement (parser, NULL_TREE, false, NULL);
30594 body = pop_stmt_list (body);
30596 if (declv == NULL_TREE)
30597 ret = NULL_TREE;
30598 else
30599 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30600 pre_body, clauses);
30602 while (nbraces)
30604 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30606 cp_lexer_consume_token (parser->lexer);
30607 nbraces--;
30609 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30610 cp_lexer_consume_token (parser->lexer);
30611 else
30613 if (!collapse_err)
30615 error_at (cp_lexer_peek_token (parser->lexer)->location,
30616 "collapsed loops not perfectly nested");
30618 collapse_err = true;
30619 cp_parser_statement_seq_opt (parser, NULL);
30620 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30621 break;
30625 while (!for_block->is_empty ())
30626 add_stmt (pop_stmt_list (for_block->pop ()));
30627 release_tree_vector (for_block);
30629 return ret;
30632 /* Helper function for OpenMP parsing, split clauses and call
30633 finish_omp_clauses on each of the set of clauses afterwards. */
30635 static void
30636 cp_omp_split_clauses (location_t loc, enum tree_code code,
30637 omp_clause_mask mask, tree clauses, tree *cclauses)
30639 int i;
30640 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30641 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30642 if (cclauses[i])
30643 cclauses[i] = finish_omp_clauses (cclauses[i]);
30646 /* OpenMP 4.0:
30647 #pragma omp simd simd-clause[optseq] new-line
30648 for-loop */
30650 #define OMP_SIMD_CLAUSE_MASK \
30651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30659 static tree
30660 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30661 char *p_name, omp_clause_mask mask, tree *cclauses)
30663 tree clauses, sb, ret;
30664 unsigned int save;
30665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30667 strcat (p_name, " simd");
30668 mask |= OMP_SIMD_CLAUSE_MASK;
30669 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30671 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30672 cclauses == NULL);
30673 if (cclauses)
30675 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30676 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30679 sb = begin_omp_structured_block ();
30680 save = cp_parser_begin_omp_structured_block (parser);
30682 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30684 cp_parser_end_omp_structured_block (parser, save);
30685 add_stmt (finish_omp_structured_block (sb));
30687 return ret;
30690 /* OpenMP 2.5:
30691 #pragma omp for for-clause[optseq] new-line
30692 for-loop
30694 OpenMP 4.0:
30695 #pragma omp for simd for-simd-clause[optseq] new-line
30696 for-loop */
30698 #define OMP_FOR_CLAUSE_MASK \
30699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30708 static tree
30709 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30710 char *p_name, omp_clause_mask mask, tree *cclauses)
30712 tree clauses, sb, ret;
30713 unsigned int save;
30714 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30716 strcat (p_name, " for");
30717 mask |= OMP_FOR_CLAUSE_MASK;
30718 if (cclauses)
30719 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30721 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30723 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30724 const char *p = IDENTIFIER_POINTER (id);
30726 if (strcmp (p, "simd") == 0)
30728 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30729 if (cclauses == NULL)
30730 cclauses = cclauses_buf;
30732 cp_lexer_consume_token (parser->lexer);
30733 if (!flag_openmp) /* flag_openmp_simd */
30734 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30735 cclauses);
30736 sb = begin_omp_structured_block ();
30737 save = cp_parser_begin_omp_structured_block (parser);
30738 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30739 cclauses);
30740 cp_parser_end_omp_structured_block (parser, save);
30741 tree body = finish_omp_structured_block (sb);
30742 if (ret == NULL)
30743 return ret;
30744 ret = make_node (OMP_FOR);
30745 TREE_TYPE (ret) = void_type_node;
30746 OMP_FOR_BODY (ret) = body;
30747 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30748 SET_EXPR_LOCATION (ret, loc);
30749 add_stmt (ret);
30750 return ret;
30753 if (!flag_openmp) /* flag_openmp_simd */
30755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30756 return NULL_TREE;
30759 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30760 cclauses == NULL);
30761 if (cclauses)
30763 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30764 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30767 sb = begin_omp_structured_block ();
30768 save = cp_parser_begin_omp_structured_block (parser);
30770 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30772 cp_parser_end_omp_structured_block (parser, save);
30773 add_stmt (finish_omp_structured_block (sb));
30775 return ret;
30778 /* OpenMP 2.5:
30779 # pragma omp master new-line
30780 structured-block */
30782 static tree
30783 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30785 cp_parser_require_pragma_eol (parser, pragma_tok);
30786 return c_finish_omp_master (input_location,
30787 cp_parser_omp_structured_block (parser));
30790 /* OpenMP 2.5:
30791 # pragma omp ordered new-line
30792 structured-block */
30794 static tree
30795 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30797 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30798 cp_parser_require_pragma_eol (parser, pragma_tok);
30799 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30802 /* OpenMP 2.5:
30804 section-scope:
30805 { section-sequence }
30807 section-sequence:
30808 section-directive[opt] structured-block
30809 section-sequence section-directive structured-block */
30811 static tree
30812 cp_parser_omp_sections_scope (cp_parser *parser)
30814 tree stmt, substmt;
30815 bool error_suppress = false;
30816 cp_token *tok;
30818 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30819 return NULL_TREE;
30821 stmt = push_stmt_list ();
30823 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30825 substmt = cp_parser_omp_structured_block (parser);
30826 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30827 add_stmt (substmt);
30830 while (1)
30832 tok = cp_lexer_peek_token (parser->lexer);
30833 if (tok->type == CPP_CLOSE_BRACE)
30834 break;
30835 if (tok->type == CPP_EOF)
30836 break;
30838 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30840 cp_lexer_consume_token (parser->lexer);
30841 cp_parser_require_pragma_eol (parser, tok);
30842 error_suppress = false;
30844 else if (!error_suppress)
30846 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30847 error_suppress = true;
30850 substmt = cp_parser_omp_structured_block (parser);
30851 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30852 add_stmt (substmt);
30854 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30856 substmt = pop_stmt_list (stmt);
30858 stmt = make_node (OMP_SECTIONS);
30859 TREE_TYPE (stmt) = void_type_node;
30860 OMP_SECTIONS_BODY (stmt) = substmt;
30862 add_stmt (stmt);
30863 return stmt;
30866 /* OpenMP 2.5:
30867 # pragma omp sections sections-clause[optseq] newline
30868 sections-scope */
30870 #define OMP_SECTIONS_CLAUSE_MASK \
30871 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30877 static tree
30878 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30879 char *p_name, omp_clause_mask mask, tree *cclauses)
30881 tree clauses, ret;
30882 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30884 strcat (p_name, " sections");
30885 mask |= OMP_SECTIONS_CLAUSE_MASK;
30886 if (cclauses)
30887 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30890 cclauses == NULL);
30891 if (cclauses)
30893 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30894 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30897 ret = cp_parser_omp_sections_scope (parser);
30898 if (ret)
30899 OMP_SECTIONS_CLAUSES (ret) = clauses;
30901 return ret;
30904 /* OpenMP 2.5:
30905 # pragma omp parallel parallel-clause[optseq] new-line
30906 structured-block
30907 # pragma omp parallel for parallel-for-clause[optseq] new-line
30908 structured-block
30909 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30910 structured-block
30912 OpenMP 4.0:
30913 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30914 structured-block */
30916 #define OMP_PARALLEL_CLAUSE_MASK \
30917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
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_DEFAULT) \
30921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30927 static tree
30928 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30929 char *p_name, omp_clause_mask mask, tree *cclauses)
30931 tree stmt, clauses, block;
30932 unsigned int save;
30933 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30935 strcat (p_name, " parallel");
30936 mask |= OMP_PARALLEL_CLAUSE_MASK;
30938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30940 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30941 if (cclauses == NULL)
30942 cclauses = cclauses_buf;
30944 cp_lexer_consume_token (parser->lexer);
30945 if (!flag_openmp) /* flag_openmp_simd */
30946 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30947 block = begin_omp_parallel ();
30948 save = cp_parser_begin_omp_structured_block (parser);
30949 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30950 cp_parser_end_omp_structured_block (parser, save);
30951 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30952 block);
30953 if (ret == NULL_TREE)
30954 return ret;
30955 OMP_PARALLEL_COMBINED (stmt) = 1;
30956 return stmt;
30958 else if (cclauses)
30960 error_at (loc, "expected %<for%> after %qs", p_name);
30961 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30962 return NULL_TREE;
30964 else if (!flag_openmp) /* flag_openmp_simd */
30966 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30967 return NULL_TREE;
30969 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30972 const char *p = IDENTIFIER_POINTER (id);
30973 if (strcmp (p, "sections") == 0)
30975 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30976 cclauses = cclauses_buf;
30978 cp_lexer_consume_token (parser->lexer);
30979 block = begin_omp_parallel ();
30980 save = cp_parser_begin_omp_structured_block (parser);
30981 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30982 cp_parser_end_omp_structured_block (parser, save);
30983 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30984 block);
30985 OMP_PARALLEL_COMBINED (stmt) = 1;
30986 return stmt;
30990 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30992 block = begin_omp_parallel ();
30993 save = cp_parser_begin_omp_structured_block (parser);
30994 cp_parser_statement (parser, NULL_TREE, false, NULL);
30995 cp_parser_end_omp_structured_block (parser, save);
30996 stmt = finish_omp_parallel (clauses, block);
30997 return stmt;
31000 /* OpenMP 2.5:
31001 # pragma omp single single-clause[optseq] new-line
31002 structured-block */
31004 #define OMP_SINGLE_CLAUSE_MASK \
31005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31010 static tree
31011 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31013 tree stmt = make_node (OMP_SINGLE);
31014 TREE_TYPE (stmt) = void_type_node;
31016 OMP_SINGLE_CLAUSES (stmt)
31017 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31018 "#pragma omp single", pragma_tok);
31019 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31021 return add_stmt (stmt);
31024 /* OpenMP 3.0:
31025 # pragma omp task task-clause[optseq] new-line
31026 structured-block */
31028 #define OMP_TASK_CLAUSE_MASK \
31029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31039 static tree
31040 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31042 tree clauses, block;
31043 unsigned int save;
31045 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31046 "#pragma omp task", pragma_tok);
31047 block = begin_omp_task ();
31048 save = cp_parser_begin_omp_structured_block (parser);
31049 cp_parser_statement (parser, NULL_TREE, false, NULL);
31050 cp_parser_end_omp_structured_block (parser, save);
31051 return finish_omp_task (clauses, block);
31054 /* OpenMP 3.0:
31055 # pragma omp taskwait new-line */
31057 static void
31058 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31060 cp_parser_require_pragma_eol (parser, pragma_tok);
31061 finish_omp_taskwait ();
31064 /* OpenMP 3.1:
31065 # pragma omp taskyield new-line */
31067 static void
31068 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31070 cp_parser_require_pragma_eol (parser, pragma_tok);
31071 finish_omp_taskyield ();
31074 /* OpenMP 4.0:
31075 # pragma omp taskgroup new-line
31076 structured-block */
31078 static tree
31079 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31081 cp_parser_require_pragma_eol (parser, pragma_tok);
31082 return c_finish_omp_taskgroup (input_location,
31083 cp_parser_omp_structured_block (parser));
31087 /* OpenMP 2.5:
31088 # pragma omp threadprivate (variable-list) */
31090 static void
31091 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31093 tree vars;
31095 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31096 cp_parser_require_pragma_eol (parser, pragma_tok);
31098 finish_omp_threadprivate (vars);
31101 /* OpenMP 4.0:
31102 # pragma omp cancel cancel-clause[optseq] new-line */
31104 #define OMP_CANCEL_CLAUSE_MASK \
31105 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31111 static void
31112 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31114 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31115 "#pragma omp cancel", pragma_tok);
31116 finish_omp_cancel (clauses);
31119 /* OpenMP 4.0:
31120 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31122 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31128 static void
31129 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31131 tree clauses;
31132 bool point_seen = false;
31134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31137 const char *p = IDENTIFIER_POINTER (id);
31139 if (strcmp (p, "point") == 0)
31141 cp_lexer_consume_token (parser->lexer);
31142 point_seen = true;
31145 if (!point_seen)
31147 cp_parser_error (parser, "expected %<point%>");
31148 cp_parser_require_pragma_eol (parser, pragma_tok);
31149 return;
31152 clauses = cp_parser_omp_all_clauses (parser,
31153 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31154 "#pragma omp cancellation point",
31155 pragma_tok);
31156 finish_omp_cancellation_point (clauses);
31159 /* OpenMP 4.0:
31160 #pragma omp distribute distribute-clause[optseq] new-line
31161 for-loop */
31163 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31169 static tree
31170 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31171 char *p_name, omp_clause_mask mask, tree *cclauses)
31173 tree clauses, sb, ret;
31174 unsigned int save;
31175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31177 strcat (p_name, " distribute");
31178 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31183 const char *p = IDENTIFIER_POINTER (id);
31184 bool simd = false;
31185 bool parallel = false;
31187 if (strcmp (p, "simd") == 0)
31188 simd = true;
31189 else
31190 parallel = strcmp (p, "parallel") == 0;
31191 if (parallel || simd)
31193 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31194 if (cclauses == NULL)
31195 cclauses = cclauses_buf;
31196 cp_lexer_consume_token (parser->lexer);
31197 if (!flag_openmp) /* flag_openmp_simd */
31199 if (simd)
31200 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31201 cclauses);
31202 else
31203 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31204 cclauses);
31206 sb = begin_omp_structured_block ();
31207 save = cp_parser_begin_omp_structured_block (parser);
31208 if (simd)
31209 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31210 cclauses);
31211 else
31212 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31213 cclauses);
31214 cp_parser_end_omp_structured_block (parser, save);
31215 tree body = finish_omp_structured_block (sb);
31216 if (ret == NULL)
31217 return ret;
31218 ret = make_node (OMP_DISTRIBUTE);
31219 TREE_TYPE (ret) = void_type_node;
31220 OMP_FOR_BODY (ret) = body;
31221 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31222 SET_EXPR_LOCATION (ret, loc);
31223 add_stmt (ret);
31224 return ret;
31227 if (!flag_openmp) /* flag_openmp_simd */
31229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31230 return NULL_TREE;
31233 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31234 cclauses == NULL);
31235 if (cclauses)
31237 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31238 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31241 sb = begin_omp_structured_block ();
31242 save = cp_parser_begin_omp_structured_block (parser);
31244 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31246 cp_parser_end_omp_structured_block (parser, save);
31247 add_stmt (finish_omp_structured_block (sb));
31249 return ret;
31252 /* OpenMP 4.0:
31253 # pragma omp teams teams-clause[optseq] new-line
31254 structured-block */
31256 #define OMP_TEAMS_CLAUSE_MASK \
31257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31265 static tree
31266 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31267 char *p_name, omp_clause_mask mask, tree *cclauses)
31269 tree clauses, sb, ret;
31270 unsigned int save;
31271 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31273 strcat (p_name, " teams");
31274 mask |= OMP_TEAMS_CLAUSE_MASK;
31276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31278 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31279 const char *p = IDENTIFIER_POINTER (id);
31280 if (strcmp (p, "distribute") == 0)
31282 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31283 if (cclauses == NULL)
31284 cclauses = cclauses_buf;
31286 cp_lexer_consume_token (parser->lexer);
31287 if (!flag_openmp) /* flag_openmp_simd */
31288 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31289 cclauses);
31290 sb = begin_omp_structured_block ();
31291 save = cp_parser_begin_omp_structured_block (parser);
31292 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31293 cclauses);
31294 cp_parser_end_omp_structured_block (parser, save);
31295 tree body = finish_omp_structured_block (sb);
31296 if (ret == NULL)
31297 return ret;
31298 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31299 ret = make_node (OMP_TEAMS);
31300 TREE_TYPE (ret) = void_type_node;
31301 OMP_TEAMS_CLAUSES (ret) = clauses;
31302 OMP_TEAMS_BODY (ret) = body;
31303 return add_stmt (ret);
31306 if (!flag_openmp) /* flag_openmp_simd */
31308 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31309 return NULL_TREE;
31312 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31313 cclauses == NULL);
31314 if (cclauses)
31316 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31317 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31320 tree stmt = make_node (OMP_TEAMS);
31321 TREE_TYPE (stmt) = void_type_node;
31322 OMP_TEAMS_CLAUSES (stmt) = clauses;
31323 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31325 return add_stmt (stmt);
31328 /* OpenMP 4.0:
31329 # pragma omp target data target-data-clause[optseq] new-line
31330 structured-block */
31332 #define OMP_TARGET_DATA_CLAUSE_MASK \
31333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31337 static tree
31338 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31340 tree stmt = make_node (OMP_TARGET_DATA);
31341 TREE_TYPE (stmt) = void_type_node;
31343 OMP_TARGET_DATA_CLAUSES (stmt)
31344 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31345 "#pragma omp target data", pragma_tok);
31346 keep_next_level (true);
31347 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31349 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31350 return add_stmt (stmt);
31353 /* OpenMP 4.0:
31354 # pragma omp target update target-update-clause[optseq] new-line */
31356 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31357 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31362 static bool
31363 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31364 enum pragma_context context)
31366 if (context == pragma_stmt)
31368 error_at (pragma_tok->location,
31369 "%<#pragma omp target update%> may only be "
31370 "used in compound statements");
31371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31372 return false;
31375 tree clauses
31376 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31377 "#pragma omp target update", pragma_tok);
31378 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31379 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31381 error_at (pragma_tok->location,
31382 "%<#pragma omp target update%> must contain at least one "
31383 "%<from%> or %<to%> clauses");
31384 return false;
31387 tree stmt = make_node (OMP_TARGET_UPDATE);
31388 TREE_TYPE (stmt) = void_type_node;
31389 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31390 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31391 add_stmt (stmt);
31392 return false;
31395 /* OpenMP 4.0:
31396 # pragma omp target target-clause[optseq] new-line
31397 structured-block */
31399 #define OMP_TARGET_CLAUSE_MASK \
31400 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31404 static bool
31405 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31406 enum pragma_context context)
31408 if (context != pragma_stmt && context != pragma_compound)
31410 cp_parser_error (parser, "expected declaration specifiers");
31411 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31412 return false;
31415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31417 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31418 const char *p = IDENTIFIER_POINTER (id);
31420 if (strcmp (p, "teams") == 0)
31422 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31423 char p_name[sizeof ("#pragma omp target teams distribute "
31424 "parallel for simd")];
31426 cp_lexer_consume_token (parser->lexer);
31427 strcpy (p_name, "#pragma omp target");
31428 if (!flag_openmp) /* flag_openmp_simd */
31430 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31431 OMP_TARGET_CLAUSE_MASK,
31432 cclauses);
31433 return stmt != NULL_TREE;
31435 keep_next_level (true);
31436 tree sb = begin_omp_structured_block ();
31437 unsigned save = cp_parser_begin_omp_structured_block (parser);
31438 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31439 OMP_TARGET_CLAUSE_MASK, cclauses);
31440 cp_parser_end_omp_structured_block (parser, save);
31441 tree body = finish_omp_structured_block (sb);
31442 if (ret == NULL_TREE)
31443 return false;
31444 tree stmt = make_node (OMP_TARGET);
31445 TREE_TYPE (stmt) = void_type_node;
31446 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31447 OMP_TARGET_BODY (stmt) = body;
31448 add_stmt (stmt);
31449 return true;
31451 else if (!flag_openmp) /* flag_openmp_simd */
31453 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31454 return false;
31456 else if (strcmp (p, "data") == 0)
31458 cp_lexer_consume_token (parser->lexer);
31459 cp_parser_omp_target_data (parser, pragma_tok);
31460 return true;
31462 else if (strcmp (p, "update") == 0)
31464 cp_lexer_consume_token (parser->lexer);
31465 return cp_parser_omp_target_update (parser, pragma_tok, context);
31469 tree stmt = make_node (OMP_TARGET);
31470 TREE_TYPE (stmt) = void_type_node;
31472 OMP_TARGET_CLAUSES (stmt)
31473 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31474 "#pragma omp target", pragma_tok);
31475 keep_next_level (true);
31476 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31478 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31479 add_stmt (stmt);
31480 return true;
31483 /* OpenACC 2.0:
31484 # pragma acc cache (variable-list) new-line
31487 static tree
31488 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31490 tree stmt, clauses;
31492 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31493 clauses = finish_omp_clauses (clauses);
31495 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31497 stmt = make_node (OACC_CACHE);
31498 TREE_TYPE (stmt) = void_type_node;
31499 OACC_CACHE_CLAUSES (stmt) = clauses;
31500 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31501 add_stmt (stmt);
31503 return stmt;
31506 /* OpenACC 2.0:
31507 # pragma acc data oacc-data-clause[optseq] new-line
31508 structured-block */
31510 #define OACC_DATA_CLAUSE_MASK \
31511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31523 static tree
31524 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31526 tree stmt, clauses, block;
31527 unsigned int save;
31529 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31530 "#pragma acc data", pragma_tok);
31532 block = begin_omp_parallel ();
31533 save = cp_parser_begin_omp_structured_block (parser);
31534 cp_parser_statement (parser, NULL_TREE, false, NULL);
31535 cp_parser_end_omp_structured_block (parser, save);
31536 stmt = finish_oacc_data (clauses, block);
31537 return stmt;
31540 /* OpenACC 2.0:
31541 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31545 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31547 LOC is the location of the #pragma token.
31550 #define OACC_ENTER_DATA_CLAUSE_MASK \
31551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31559 #define OACC_EXIT_DATA_CLAUSE_MASK \
31560 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31566 static tree
31567 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31568 bool enter)
31570 tree stmt, clauses;
31572 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31573 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31575 cp_parser_error (parser, enter
31576 ? "expected %<data%> in %<#pragma acc enter data%>"
31577 : "expected %<data%> in %<#pragma acc exit data%>");
31578 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31579 return NULL_TREE;
31582 const char *p =
31583 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31584 if (strcmp (p, "data") != 0)
31586 cp_parser_error (parser, "invalid pragma");
31587 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31588 return NULL_TREE;
31591 cp_lexer_consume_token (parser->lexer);
31593 if (enter)
31594 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31595 "#pragma acc enter data", pragma_tok);
31596 else
31597 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31598 "#pragma acc exit data", pragma_tok);
31600 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31602 error_at (pragma_tok->location,
31603 "%<#pragma acc enter data%> has no data movement clause");
31604 return NULL_TREE;
31607 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31608 TREE_TYPE (stmt) = void_type_node;
31609 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31610 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31611 add_stmt (stmt);
31612 return stmt;
31615 /* OpenACC 2.0:
31616 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31617 structured-block */
31619 #define OACC_KERNELS_CLAUSE_MASK \
31620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31634 static tree
31635 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31637 tree stmt, clauses, block;
31638 unsigned int save;
31640 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31641 "#pragma acc kernels", pragma_tok);
31643 block = begin_omp_parallel ();
31644 save = cp_parser_begin_omp_structured_block (parser);
31645 cp_parser_statement (parser, NULL_TREE, false, NULL);
31646 cp_parser_end_omp_structured_block (parser, save);
31647 stmt = finish_oacc_kernels (clauses, block);
31648 return stmt;
31651 /* OpenACC 2.0:
31652 # pragma acc loop oacc-loop-clause[optseq] new-line
31653 structured-block */
31655 #define OACC_LOOP_CLAUSE_MASK \
31656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31659 static tree
31660 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31662 tree stmt, clauses, block;
31663 int save;
31665 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31666 "#pragma acc loop", pragma_tok);
31668 block = begin_omp_structured_block ();
31669 save = cp_parser_begin_omp_structured_block (parser);
31670 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31671 cp_parser_end_omp_structured_block (parser, save);
31672 add_stmt (finish_omp_structured_block (block));
31673 return stmt;
31676 /* OpenACC 2.0:
31677 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31678 structured-block */
31680 #define OACC_PARALLEL_CLAUSE_MASK \
31681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31699 static tree
31700 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31702 tree stmt, clauses, block;
31703 unsigned int save;
31705 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31706 "#pragma acc parallel", pragma_tok);
31708 block = begin_omp_parallel ();
31709 save = cp_parser_begin_omp_structured_block (parser);
31710 cp_parser_statement (parser, NULL_TREE, false, NULL);
31711 cp_parser_end_omp_structured_block (parser, save);
31712 stmt = finish_oacc_parallel (clauses, block);
31713 return stmt;
31716 /* OpenACC 2.0:
31717 # pragma acc update oacc-update-clause[optseq] new-line
31720 #define OACC_UPDATE_CLAUSE_MASK \
31721 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31728 static tree
31729 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31731 tree stmt, clauses;
31733 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31734 "#pragma acc update", pragma_tok);
31736 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31738 error_at (pragma_tok->location,
31739 "%<#pragma acc update%> must contain at least one "
31740 "%<device%> or %<host/self%> clause");
31741 return NULL_TREE;
31744 stmt = make_node (OACC_UPDATE);
31745 TREE_TYPE (stmt) = void_type_node;
31746 OACC_UPDATE_CLAUSES (stmt) = clauses;
31747 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31748 add_stmt (stmt);
31749 return stmt;
31752 /* OpenACC 2.0:
31753 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31755 LOC is the location of the #pragma token.
31758 #define OACC_WAIT_CLAUSE_MASK \
31759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31761 static tree
31762 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31764 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31765 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31767 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31768 list = cp_parser_oacc_wait_list (parser, loc, list);
31770 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31771 "#pragma acc wait", pragma_tok);
31773 stmt = c_finish_oacc_wait (loc, list, clauses);
31775 return stmt;
31778 /* OpenMP 4.0:
31779 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31781 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31789 static void
31790 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31791 enum pragma_context context)
31793 bool first_p = parser->omp_declare_simd == NULL;
31794 cp_omp_declare_simd_data data;
31795 if (first_p)
31797 data.error_seen = false;
31798 data.fndecl_seen = false;
31799 data.tokens = vNULL;
31800 parser->omp_declare_simd = &data;
31802 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31803 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31804 cp_lexer_consume_token (parser->lexer);
31805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31806 parser->omp_declare_simd->error_seen = true;
31807 cp_parser_require_pragma_eol (parser, pragma_tok);
31808 struct cp_token_cache *cp
31809 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31810 parser->omp_declare_simd->tokens.safe_push (cp);
31811 if (first_p)
31813 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31814 cp_parser_pragma (parser, context);
31815 switch (context)
31817 case pragma_external:
31818 cp_parser_declaration (parser);
31819 break;
31820 case pragma_member:
31821 cp_parser_member_declaration (parser);
31822 break;
31823 case pragma_objc_icode:
31824 cp_parser_block_declaration (parser, /*statement_p=*/false);
31825 break;
31826 default:
31827 cp_parser_declaration_statement (parser);
31828 break;
31830 if (parser->omp_declare_simd
31831 && !parser->omp_declare_simd->error_seen
31832 && !parser->omp_declare_simd->fndecl_seen)
31833 error_at (pragma_tok->location,
31834 "%<#pragma omp declare simd%> not immediately followed by "
31835 "function declaration or definition");
31836 data.tokens.release ();
31837 parser->omp_declare_simd = NULL;
31841 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31842 This function is modelled similar to the late parsing of omp declare
31843 simd. */
31845 static tree
31846 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31848 struct cp_token_cache *ce;
31849 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31850 int ii = 0;
31852 if (parser->omp_declare_simd != NULL)
31854 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31855 " marked as a Cilk Plus SIMD-enabled function");
31856 XDELETE (parser->cilk_simd_fn_info);
31857 parser->cilk_simd_fn_info = NULL;
31858 return attrs;
31860 if (!info->error_seen && info->fndecl_seen)
31862 error ("vector attribute not immediately followed by a single function"
31863 " declaration or definition");
31864 info->error_seen = true;
31866 if (info->error_seen)
31867 return attrs;
31869 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31871 tree c, cl;
31873 cp_parser_push_lexer_for_tokens (parser, ce);
31874 parser->lexer->in_pragma = true;
31875 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31876 "SIMD-enabled functions attribute",
31877 NULL);
31878 cp_parser_pop_lexer (parser);
31879 if (cl)
31880 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31882 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31883 TREE_CHAIN (c) = attrs;
31884 attrs = c;
31886 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31887 TREE_CHAIN (c) = attrs;
31888 if (processing_template_decl)
31889 ATTR_IS_DEPENDENT (c) = 1;
31890 attrs = c;
31892 info->fndecl_seen = true;
31893 XDELETE (parser->cilk_simd_fn_info);
31894 parser->cilk_simd_fn_info = NULL;
31895 return attrs;
31898 /* Finalize #pragma omp declare simd clauses after direct declarator has
31899 been parsed, and put that into "omp declare simd" attribute. */
31901 static tree
31902 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31904 struct cp_token_cache *ce;
31905 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31906 int i;
31908 if (!data->error_seen && data->fndecl_seen)
31910 error ("%<#pragma omp declare simd%> not immediately followed by "
31911 "a single function declaration or definition");
31912 data->error_seen = true;
31913 return attrs;
31915 if (data->error_seen)
31916 return attrs;
31918 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31920 tree c, cl;
31922 cp_parser_push_lexer_for_tokens (parser, ce);
31923 parser->lexer->in_pragma = true;
31924 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31925 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31926 cp_lexer_consume_token (parser->lexer);
31927 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31928 "#pragma omp declare simd", pragma_tok);
31929 cp_parser_pop_lexer (parser);
31930 if (cl)
31931 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31932 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31933 TREE_CHAIN (c) = attrs;
31934 if (processing_template_decl)
31935 ATTR_IS_DEPENDENT (c) = 1;
31936 attrs = c;
31939 data->fndecl_seen = true;
31940 return attrs;
31944 /* OpenMP 4.0:
31945 # pragma omp declare target new-line
31946 declarations and definitions
31947 # pragma omp end declare target new-line */
31949 static void
31950 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31953 scope_chain->omp_declare_target_attribute++;
31956 static void
31957 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31959 const char *p = "";
31960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31962 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31963 p = IDENTIFIER_POINTER (id);
31965 if (strcmp (p, "declare") == 0)
31967 cp_lexer_consume_token (parser->lexer);
31968 p = "";
31969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31972 p = IDENTIFIER_POINTER (id);
31974 if (strcmp (p, "target") == 0)
31975 cp_lexer_consume_token (parser->lexer);
31976 else
31978 cp_parser_error (parser, "expected %<target%>");
31979 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31980 return;
31983 else
31985 cp_parser_error (parser, "expected %<declare%>");
31986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31987 return;
31989 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31990 if (!scope_chain->omp_declare_target_attribute)
31991 error_at (pragma_tok->location,
31992 "%<#pragma omp end declare target%> without corresponding "
31993 "%<#pragma omp declare target%>");
31994 else
31995 scope_chain->omp_declare_target_attribute--;
31998 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31999 expression and optional initializer clause of
32000 #pragma omp declare reduction. We store the expression(s) as
32001 either 3, 6 or 7 special statements inside of the artificial function's
32002 body. The first two statements are DECL_EXPRs for the artificial
32003 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32004 expression that uses those variables.
32005 If there was any INITIALIZER clause, this is followed by further statements,
32006 the fourth and fifth statements are DECL_EXPRs for the artificial
32007 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32008 constructor variant (first token after open paren is not omp_priv),
32009 then the sixth statement is a statement with the function call expression
32010 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32011 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32012 to initialize the OMP_PRIV artificial variable and there is seventh
32013 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32015 static bool
32016 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32018 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32019 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32020 type = TREE_TYPE (type);
32021 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32022 DECL_ARTIFICIAL (omp_out) = 1;
32023 pushdecl (omp_out);
32024 add_decl_expr (omp_out);
32025 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32026 DECL_ARTIFICIAL (omp_in) = 1;
32027 pushdecl (omp_in);
32028 add_decl_expr (omp_in);
32029 tree combiner;
32030 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32032 keep_next_level (true);
32033 tree block = begin_omp_structured_block ();
32034 combiner = cp_parser_expression (parser);
32035 finish_expr_stmt (combiner);
32036 block = finish_omp_structured_block (block);
32037 add_stmt (block);
32039 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32040 return false;
32042 const char *p = "";
32043 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32045 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32046 p = IDENTIFIER_POINTER (id);
32049 if (strcmp (p, "initializer") == 0)
32051 cp_lexer_consume_token (parser->lexer);
32052 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32053 return false;
32055 p = "";
32056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32058 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32059 p = IDENTIFIER_POINTER (id);
32062 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32063 DECL_ARTIFICIAL (omp_priv) = 1;
32064 pushdecl (omp_priv);
32065 add_decl_expr (omp_priv);
32066 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32067 DECL_ARTIFICIAL (omp_orig) = 1;
32068 pushdecl (omp_orig);
32069 add_decl_expr (omp_orig);
32071 keep_next_level (true);
32072 block = begin_omp_structured_block ();
32074 bool ctor = false;
32075 if (strcmp (p, "omp_priv") == 0)
32077 bool is_direct_init, is_non_constant_init;
32078 ctor = true;
32079 cp_lexer_consume_token (parser->lexer);
32080 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32081 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32082 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32083 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32084 == CPP_CLOSE_PAREN
32085 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32086 == CPP_CLOSE_PAREN))
32088 finish_omp_structured_block (block);
32089 error ("invalid initializer clause");
32090 return false;
32092 initializer = cp_parser_initializer (parser, &is_direct_init,
32093 &is_non_constant_init);
32094 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32095 NULL_TREE, LOOKUP_ONLYCONVERTING);
32097 else
32099 cp_parser_parse_tentatively (parser);
32100 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32101 /*check_dependency_p=*/true,
32102 /*template_p=*/NULL,
32103 /*declarator_p=*/false,
32104 /*optional_p=*/false);
32105 vec<tree, va_gc> *args;
32106 if (fn_name == error_mark_node
32107 || cp_parser_error_occurred (parser)
32108 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32109 || ((args = cp_parser_parenthesized_expression_list
32110 (parser, non_attr, /*cast_p=*/false,
32111 /*allow_expansion_p=*/true,
32112 /*non_constant_p=*/NULL)),
32113 cp_parser_error_occurred (parser)))
32115 finish_omp_structured_block (block);
32116 cp_parser_abort_tentative_parse (parser);
32117 cp_parser_error (parser, "expected id-expression (arguments)");
32118 return false;
32120 unsigned int i;
32121 tree arg;
32122 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32123 if (arg == omp_priv
32124 || (TREE_CODE (arg) == ADDR_EXPR
32125 && TREE_OPERAND (arg, 0) == omp_priv))
32126 break;
32127 cp_parser_abort_tentative_parse (parser);
32128 if (arg == NULL_TREE)
32129 error ("one of the initializer call arguments should be %<omp_priv%>"
32130 " or %<&omp_priv%>");
32131 initializer = cp_parser_postfix_expression (parser, false, false, false,
32132 false, NULL);
32133 finish_expr_stmt (initializer);
32136 block = finish_omp_structured_block (block);
32137 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32138 add_stmt (block);
32140 if (ctor)
32141 add_decl_expr (omp_orig);
32143 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32144 return false;
32147 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32148 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32150 return true;
32153 /* OpenMP 4.0
32154 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32155 initializer-clause[opt] new-line
32157 initializer-clause:
32158 initializer (omp_priv initializer)
32159 initializer (function-name (argument-list)) */
32161 static void
32162 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32163 enum pragma_context)
32165 auto_vec<tree> types;
32166 enum tree_code reduc_code = ERROR_MARK;
32167 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32168 unsigned int i;
32169 cp_token *first_token;
32170 cp_token_cache *cp;
32171 int errs;
32172 void *p;
32174 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32175 p = obstack_alloc (&declarator_obstack, 0);
32177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32178 goto fail;
32180 switch (cp_lexer_peek_token (parser->lexer)->type)
32182 case CPP_PLUS:
32183 reduc_code = PLUS_EXPR;
32184 break;
32185 case CPP_MULT:
32186 reduc_code = MULT_EXPR;
32187 break;
32188 case CPP_MINUS:
32189 reduc_code = MINUS_EXPR;
32190 break;
32191 case CPP_AND:
32192 reduc_code = BIT_AND_EXPR;
32193 break;
32194 case CPP_XOR:
32195 reduc_code = BIT_XOR_EXPR;
32196 break;
32197 case CPP_OR:
32198 reduc_code = BIT_IOR_EXPR;
32199 break;
32200 case CPP_AND_AND:
32201 reduc_code = TRUTH_ANDIF_EXPR;
32202 break;
32203 case CPP_OR_OR:
32204 reduc_code = TRUTH_ORIF_EXPR;
32205 break;
32206 case CPP_NAME:
32207 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32208 break;
32209 default:
32210 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32211 "%<|%>, %<&&%>, %<||%> or identifier");
32212 goto fail;
32215 if (reduc_code != ERROR_MARK)
32216 cp_lexer_consume_token (parser->lexer);
32218 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32219 if (reduc_id == error_mark_node)
32220 goto fail;
32222 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32223 goto fail;
32225 /* Types may not be defined in declare reduction type list. */
32226 const char *saved_message;
32227 saved_message = parser->type_definition_forbidden_message;
32228 parser->type_definition_forbidden_message
32229 = G_("types may not be defined in declare reduction type list");
32230 bool saved_colon_corrects_to_scope_p;
32231 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32232 parser->colon_corrects_to_scope_p = false;
32233 bool saved_colon_doesnt_start_class_def_p;
32234 saved_colon_doesnt_start_class_def_p
32235 = parser->colon_doesnt_start_class_def_p;
32236 parser->colon_doesnt_start_class_def_p = true;
32238 while (true)
32240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32241 type = cp_parser_type_id (parser);
32242 if (type == error_mark_node)
32244 else if (ARITHMETIC_TYPE_P (type)
32245 && (orig_reduc_id == NULL_TREE
32246 || (TREE_CODE (type) != COMPLEX_TYPE
32247 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32248 "min") == 0
32249 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32250 "max") == 0))))
32251 error_at (loc, "predeclared arithmetic type %qT in "
32252 "%<#pragma omp declare reduction%>", type);
32253 else if (TREE_CODE (type) == FUNCTION_TYPE
32254 || TREE_CODE (type) == METHOD_TYPE
32255 || TREE_CODE (type) == ARRAY_TYPE)
32256 error_at (loc, "function or array type %qT in "
32257 "%<#pragma omp declare reduction%>", type);
32258 else if (TREE_CODE (type) == REFERENCE_TYPE)
32259 error_at (loc, "reference type %qT in "
32260 "%<#pragma omp declare reduction%>", type);
32261 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32262 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32263 "%<#pragma omp declare reduction%>", type);
32264 else
32265 types.safe_push (type);
32267 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32268 cp_lexer_consume_token (parser->lexer);
32269 else
32270 break;
32273 /* Restore the saved message. */
32274 parser->type_definition_forbidden_message = saved_message;
32275 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32276 parser->colon_doesnt_start_class_def_p
32277 = saved_colon_doesnt_start_class_def_p;
32279 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32280 || types.is_empty ())
32282 fail:
32283 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32284 goto done;
32287 first_token = cp_lexer_peek_token (parser->lexer);
32288 cp = NULL;
32289 errs = errorcount;
32290 FOR_EACH_VEC_ELT (types, i, type)
32292 tree fntype
32293 = build_function_type_list (void_type_node,
32294 cp_build_reference_type (type, false),
32295 NULL_TREE);
32296 tree this_reduc_id = reduc_id;
32297 if (!dependent_type_p (type))
32298 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32299 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32300 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32301 DECL_ARTIFICIAL (fndecl) = 1;
32302 DECL_EXTERNAL (fndecl) = 1;
32303 DECL_DECLARED_INLINE_P (fndecl) = 1;
32304 DECL_IGNORED_P (fndecl) = 1;
32305 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32306 DECL_ATTRIBUTES (fndecl)
32307 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32308 DECL_ATTRIBUTES (fndecl));
32309 if (processing_template_decl)
32310 fndecl = push_template_decl (fndecl);
32311 bool block_scope = false;
32312 tree block = NULL_TREE;
32313 if (current_function_decl)
32315 block_scope = true;
32316 DECL_CONTEXT (fndecl) = global_namespace;
32317 if (!processing_template_decl)
32318 pushdecl (fndecl);
32320 else if (current_class_type)
32322 if (cp == NULL)
32324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32325 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32326 cp_lexer_consume_token (parser->lexer);
32327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32328 goto fail;
32329 cp = cp_token_cache_new (first_token,
32330 cp_lexer_peek_nth_token (parser->lexer,
32331 2));
32333 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32334 finish_member_declaration (fndecl);
32335 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32336 DECL_PENDING_INLINE_P (fndecl) = 1;
32337 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32338 continue;
32340 else
32342 DECL_CONTEXT (fndecl) = current_namespace;
32343 pushdecl (fndecl);
32345 if (!block_scope)
32346 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32347 else
32348 block = begin_omp_structured_block ();
32349 if (cp)
32351 cp_parser_push_lexer_for_tokens (parser, cp);
32352 parser->lexer->in_pragma = true;
32354 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32356 if (!block_scope)
32357 finish_function (0);
32358 else
32359 DECL_CONTEXT (fndecl) = current_function_decl;
32360 if (cp)
32361 cp_parser_pop_lexer (parser);
32362 goto fail;
32364 if (cp)
32365 cp_parser_pop_lexer (parser);
32366 if (!block_scope)
32367 finish_function (0);
32368 else
32370 DECL_CONTEXT (fndecl) = current_function_decl;
32371 block = finish_omp_structured_block (block);
32372 if (TREE_CODE (block) == BIND_EXPR)
32373 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32374 else if (TREE_CODE (block) == STATEMENT_LIST)
32375 DECL_SAVED_TREE (fndecl) = block;
32376 if (processing_template_decl)
32377 add_decl_expr (fndecl);
32379 cp_check_omp_declare_reduction (fndecl);
32380 if (cp == NULL && types.length () > 1)
32381 cp = cp_token_cache_new (first_token,
32382 cp_lexer_peek_nth_token (parser->lexer, 2));
32383 if (errs != errorcount)
32384 break;
32387 cp_parser_require_pragma_eol (parser, pragma_tok);
32389 done:
32390 /* Free any declarators allocated. */
32391 obstack_free (&declarator_obstack, p);
32394 /* OpenMP 4.0
32395 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32396 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32397 initializer-clause[opt] new-line
32398 #pragma omp declare target new-line */
32400 static void
32401 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32402 enum pragma_context context)
32404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32406 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32407 const char *p = IDENTIFIER_POINTER (id);
32409 if (strcmp (p, "simd") == 0)
32411 cp_lexer_consume_token (parser->lexer);
32412 cp_parser_omp_declare_simd (parser, pragma_tok,
32413 context);
32414 return;
32416 cp_ensure_no_omp_declare_simd (parser);
32417 if (strcmp (p, "reduction") == 0)
32419 cp_lexer_consume_token (parser->lexer);
32420 cp_parser_omp_declare_reduction (parser, pragma_tok,
32421 context);
32422 return;
32424 if (!flag_openmp) /* flag_openmp_simd */
32426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32427 return;
32429 if (strcmp (p, "target") == 0)
32431 cp_lexer_consume_token (parser->lexer);
32432 cp_parser_omp_declare_target (parser, pragma_tok);
32433 return;
32436 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32437 "or %<target%>");
32438 cp_parser_require_pragma_eol (parser, pragma_tok);
32441 /* Main entry point to OpenMP statement pragmas. */
32443 static void
32444 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32446 tree stmt;
32447 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32448 omp_clause_mask mask (0);
32450 switch (pragma_tok->pragma_kind)
32452 case PRAGMA_OACC_CACHE:
32453 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32454 break;
32455 case PRAGMA_OACC_DATA:
32456 stmt = cp_parser_oacc_data (parser, pragma_tok);
32457 break;
32458 case PRAGMA_OACC_ENTER_DATA:
32459 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32460 break;
32461 case PRAGMA_OACC_EXIT_DATA:
32462 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32463 break;
32464 case PRAGMA_OACC_KERNELS:
32465 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32466 break;
32467 case PRAGMA_OACC_LOOP:
32468 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32469 break;
32470 case PRAGMA_OACC_PARALLEL:
32471 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32472 break;
32473 case PRAGMA_OACC_UPDATE:
32474 stmt = cp_parser_oacc_update (parser, pragma_tok);
32475 break;
32476 case PRAGMA_OACC_WAIT:
32477 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32478 break;
32479 case PRAGMA_OMP_ATOMIC:
32480 cp_parser_omp_atomic (parser, pragma_tok);
32481 return;
32482 case PRAGMA_OMP_CRITICAL:
32483 stmt = cp_parser_omp_critical (parser, pragma_tok);
32484 break;
32485 case PRAGMA_OMP_DISTRIBUTE:
32486 strcpy (p_name, "#pragma omp");
32487 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32488 break;
32489 case PRAGMA_OMP_FOR:
32490 strcpy (p_name, "#pragma omp");
32491 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32492 break;
32493 case PRAGMA_OMP_MASTER:
32494 stmt = cp_parser_omp_master (parser, pragma_tok);
32495 break;
32496 case PRAGMA_OMP_ORDERED:
32497 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32498 break;
32499 case PRAGMA_OMP_PARALLEL:
32500 strcpy (p_name, "#pragma omp");
32501 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32502 break;
32503 case PRAGMA_OMP_SECTIONS:
32504 strcpy (p_name, "#pragma omp");
32505 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32506 break;
32507 case PRAGMA_OMP_SIMD:
32508 strcpy (p_name, "#pragma omp");
32509 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32510 break;
32511 case PRAGMA_OMP_SINGLE:
32512 stmt = cp_parser_omp_single (parser, pragma_tok);
32513 break;
32514 case PRAGMA_OMP_TASK:
32515 stmt = cp_parser_omp_task (parser, pragma_tok);
32516 break;
32517 case PRAGMA_OMP_TASKGROUP:
32518 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32519 break;
32520 case PRAGMA_OMP_TEAMS:
32521 strcpy (p_name, "#pragma omp");
32522 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32523 break;
32524 default:
32525 gcc_unreachable ();
32528 if (stmt)
32529 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32532 /* Transactional Memory parsing routines. */
32534 /* Parse a transaction attribute.
32536 txn-attribute:
32537 attribute
32538 [ [ identifier ] ]
32540 ??? Simplify this when C++0x bracket attributes are
32541 implemented properly. */
32543 static tree
32544 cp_parser_txn_attribute_opt (cp_parser *parser)
32546 cp_token *token;
32547 tree attr_name, attr = NULL;
32549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32550 return cp_parser_attributes_opt (parser);
32552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32553 return NULL_TREE;
32554 cp_lexer_consume_token (parser->lexer);
32555 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32556 goto error1;
32558 token = cp_lexer_peek_token (parser->lexer);
32559 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32561 token = cp_lexer_consume_token (parser->lexer);
32563 attr_name = (token->type == CPP_KEYWORD
32564 /* For keywords, use the canonical spelling,
32565 not the parsed identifier. */
32566 ? ridpointers[(int) token->keyword]
32567 : token->u.value);
32568 attr = build_tree_list (attr_name, NULL_TREE);
32570 else
32571 cp_parser_error (parser, "expected identifier");
32573 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32574 error1:
32575 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32576 return attr;
32579 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32581 transaction-statement:
32582 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32583 compound-statement
32584 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32587 static tree
32588 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32590 unsigned char old_in = parser->in_transaction;
32591 unsigned char this_in = 1, new_in;
32592 cp_token *token;
32593 tree stmt, attrs, noex;
32595 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32596 || keyword == RID_TRANSACTION_RELAXED);
32597 token = cp_parser_require_keyword (parser, keyword,
32598 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32599 : RT_TRANSACTION_RELAXED));
32600 gcc_assert (token != NULL);
32602 if (keyword == RID_TRANSACTION_RELAXED)
32603 this_in |= TM_STMT_ATTR_RELAXED;
32604 else
32606 attrs = cp_parser_txn_attribute_opt (parser);
32607 if (attrs)
32608 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32611 /* Parse a noexcept specification. */
32612 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32614 /* Keep track if we're in the lexical scope of an outer transaction. */
32615 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32617 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32619 parser->in_transaction = new_in;
32620 cp_parser_compound_statement (parser, NULL, false, false);
32621 parser->in_transaction = old_in;
32623 finish_transaction_stmt (stmt, NULL, this_in, noex);
32625 return stmt;
32628 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32630 transaction-expression:
32631 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32632 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32635 static tree
32636 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32638 unsigned char old_in = parser->in_transaction;
32639 unsigned char this_in = 1;
32640 cp_token *token;
32641 tree expr, noex;
32642 bool noex_expr;
32644 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32645 || keyword == RID_TRANSACTION_RELAXED);
32647 if (!flag_tm)
32648 error (keyword == RID_TRANSACTION_RELAXED
32649 ? G_("%<__transaction_relaxed%> without transactional memory "
32650 "support enabled")
32651 : G_("%<__transaction_atomic%> without transactional memory "
32652 "support enabled"));
32654 token = cp_parser_require_keyword (parser, keyword,
32655 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32656 : RT_TRANSACTION_RELAXED));
32657 gcc_assert (token != NULL);
32659 if (keyword == RID_TRANSACTION_RELAXED)
32660 this_in |= TM_STMT_ATTR_RELAXED;
32662 /* Set this early. This might mean that we allow transaction_cancel in
32663 an expression that we find out later actually has to be a constexpr.
32664 However, we expect that cxx_constant_value will be able to deal with
32665 this; also, if the noexcept has no constexpr, then what we parse next
32666 really is a transaction's body. */
32667 parser->in_transaction = this_in;
32669 /* Parse a noexcept specification. */
32670 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32671 true);
32673 if (!noex || !noex_expr
32674 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32676 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32678 expr = cp_parser_expression (parser);
32679 expr = finish_parenthesized_expr (expr);
32681 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32683 else
32685 /* The only expression that is available got parsed for the noexcept
32686 already. noexcept is true then. */
32687 expr = noex;
32688 noex = boolean_true_node;
32691 expr = build_transaction_expr (token->location, expr, this_in, noex);
32692 parser->in_transaction = old_in;
32694 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32695 return error_mark_node;
32697 return (flag_tm ? expr : error_mark_node);
32700 /* Parse a function-transaction-block.
32702 function-transaction-block:
32703 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32704 function-body
32705 __transaction_atomic txn-attribute[opt] function-try-block
32706 __transaction_relaxed ctor-initializer[opt] function-body
32707 __transaction_relaxed function-try-block
32710 static bool
32711 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32713 unsigned char old_in = parser->in_transaction;
32714 unsigned char new_in = 1;
32715 tree compound_stmt, stmt, attrs;
32716 bool ctor_initializer_p;
32717 cp_token *token;
32719 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32720 || keyword == RID_TRANSACTION_RELAXED);
32721 token = cp_parser_require_keyword (parser, keyword,
32722 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32723 : RT_TRANSACTION_RELAXED));
32724 gcc_assert (token != NULL);
32726 if (keyword == RID_TRANSACTION_RELAXED)
32727 new_in |= TM_STMT_ATTR_RELAXED;
32728 else
32730 attrs = cp_parser_txn_attribute_opt (parser);
32731 if (attrs)
32732 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32735 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32737 parser->in_transaction = new_in;
32739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32740 ctor_initializer_p = cp_parser_function_try_block (parser);
32741 else
32742 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32743 (parser, /*in_function_try_block=*/false);
32745 parser->in_transaction = old_in;
32747 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32749 return ctor_initializer_p;
32752 /* Parse a __transaction_cancel statement.
32754 cancel-statement:
32755 __transaction_cancel txn-attribute[opt] ;
32756 __transaction_cancel txn-attribute[opt] throw-expression ;
32758 ??? Cancel and throw is not yet implemented. */
32760 static tree
32761 cp_parser_transaction_cancel (cp_parser *parser)
32763 cp_token *token;
32764 bool is_outer = false;
32765 tree stmt, attrs;
32767 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32768 RT_TRANSACTION_CANCEL);
32769 gcc_assert (token != NULL);
32771 attrs = cp_parser_txn_attribute_opt (parser);
32772 if (attrs)
32773 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32775 /* ??? Parse cancel-and-throw here. */
32777 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32779 if (!flag_tm)
32781 error_at (token->location, "%<__transaction_cancel%> without "
32782 "transactional memory support enabled");
32783 return error_mark_node;
32785 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32787 error_at (token->location, "%<__transaction_cancel%> within a "
32788 "%<__transaction_relaxed%>");
32789 return error_mark_node;
32791 else if (is_outer)
32793 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32794 && !is_tm_may_cancel_outer (current_function_decl))
32796 error_at (token->location, "outer %<__transaction_cancel%> not "
32797 "within outer %<__transaction_atomic%>");
32798 error_at (token->location,
32799 " or a %<transaction_may_cancel_outer%> function");
32800 return error_mark_node;
32803 else if (parser->in_transaction == 0)
32805 error_at (token->location, "%<__transaction_cancel%> not within "
32806 "%<__transaction_atomic%>");
32807 return error_mark_node;
32810 stmt = build_tm_abort_call (token->location, is_outer);
32811 add_stmt (stmt);
32813 return stmt;
32816 /* The parser. */
32818 static GTY (()) cp_parser *the_parser;
32821 /* Special handling for the first token or line in the file. The first
32822 thing in the file might be #pragma GCC pch_preprocess, which loads a
32823 PCH file, which is a GC collection point. So we need to handle this
32824 first pragma without benefit of an existing lexer structure.
32826 Always returns one token to the caller in *FIRST_TOKEN. This is
32827 either the true first token of the file, or the first token after
32828 the initial pragma. */
32830 static void
32831 cp_parser_initial_pragma (cp_token *first_token)
32833 tree name = NULL;
32835 cp_lexer_get_preprocessor_token (NULL, first_token);
32836 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32837 return;
32839 cp_lexer_get_preprocessor_token (NULL, first_token);
32840 if (first_token->type == CPP_STRING)
32842 name = first_token->u.value;
32844 cp_lexer_get_preprocessor_token (NULL, first_token);
32845 if (first_token->type != CPP_PRAGMA_EOL)
32846 error_at (first_token->location,
32847 "junk at end of %<#pragma GCC pch_preprocess%>");
32849 else
32850 error_at (first_token->location, "expected string literal");
32852 /* Skip to the end of the pragma. */
32853 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32854 cp_lexer_get_preprocessor_token (NULL, first_token);
32856 /* Now actually load the PCH file. */
32857 if (name)
32858 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32860 /* Read one more token to return to our caller. We have to do this
32861 after reading the PCH file in, since its pointers have to be
32862 live. */
32863 cp_lexer_get_preprocessor_token (NULL, first_token);
32866 /* Parses the grainsize pragma for the _Cilk_for statement.
32867 Syntax:
32868 #pragma cilk grainsize = <VALUE>. */
32870 static void
32871 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32873 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32875 tree exp = cp_parser_binary_expression (parser, false, false,
32876 PREC_NOT_OPERATOR, NULL);
32877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32878 if (!exp || exp == error_mark_node)
32880 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32881 return;
32884 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32886 cp_parser_cilk_for (parser, exp);
32887 else
32888 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32889 "%<#pragma cilk grainsize%> is not followed by "
32890 "%<_Cilk_for%>");
32891 return;
32893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32896 /* Normal parsing of a pragma token. Here we can (and must) use the
32897 regular lexer. */
32899 static bool
32900 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32902 cp_token *pragma_tok;
32903 unsigned int id;
32905 pragma_tok = cp_lexer_consume_token (parser->lexer);
32906 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32907 parser->lexer->in_pragma = true;
32909 id = pragma_tok->pragma_kind;
32910 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32911 cp_ensure_no_omp_declare_simd (parser);
32912 switch (id)
32914 case PRAGMA_GCC_PCH_PREPROCESS:
32915 error_at (pragma_tok->location,
32916 "%<#pragma GCC pch_preprocess%> must be first");
32917 break;
32919 case PRAGMA_OMP_BARRIER:
32920 switch (context)
32922 case pragma_compound:
32923 cp_parser_omp_barrier (parser, pragma_tok);
32924 return false;
32925 case pragma_stmt:
32926 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32927 "used in compound statements");
32928 break;
32929 default:
32930 goto bad_stmt;
32932 break;
32934 case PRAGMA_OMP_FLUSH:
32935 switch (context)
32937 case pragma_compound:
32938 cp_parser_omp_flush (parser, pragma_tok);
32939 return false;
32940 case pragma_stmt:
32941 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32942 "used in compound statements");
32943 break;
32944 default:
32945 goto bad_stmt;
32947 break;
32949 case PRAGMA_OMP_TASKWAIT:
32950 switch (context)
32952 case pragma_compound:
32953 cp_parser_omp_taskwait (parser, pragma_tok);
32954 return false;
32955 case pragma_stmt:
32956 error_at (pragma_tok->location,
32957 "%<#pragma omp taskwait%> may only be "
32958 "used in compound statements");
32959 break;
32960 default:
32961 goto bad_stmt;
32963 break;
32965 case PRAGMA_OMP_TASKYIELD:
32966 switch (context)
32968 case pragma_compound:
32969 cp_parser_omp_taskyield (parser, pragma_tok);
32970 return false;
32971 case pragma_stmt:
32972 error_at (pragma_tok->location,
32973 "%<#pragma omp taskyield%> may only be "
32974 "used in compound statements");
32975 break;
32976 default:
32977 goto bad_stmt;
32979 break;
32981 case PRAGMA_OMP_CANCEL:
32982 switch (context)
32984 case pragma_compound:
32985 cp_parser_omp_cancel (parser, pragma_tok);
32986 return false;
32987 case pragma_stmt:
32988 error_at (pragma_tok->location,
32989 "%<#pragma omp cancel%> may only be "
32990 "used in compound statements");
32991 break;
32992 default:
32993 goto bad_stmt;
32995 break;
32997 case PRAGMA_OMP_CANCELLATION_POINT:
32998 switch (context)
33000 case pragma_compound:
33001 cp_parser_omp_cancellation_point (parser, pragma_tok);
33002 return false;
33003 case pragma_stmt:
33004 error_at (pragma_tok->location,
33005 "%<#pragma omp cancellation point%> may only be "
33006 "used in compound statements");
33007 break;
33008 default:
33009 goto bad_stmt;
33011 break;
33013 case PRAGMA_OMP_THREADPRIVATE:
33014 cp_parser_omp_threadprivate (parser, pragma_tok);
33015 return false;
33017 case PRAGMA_OMP_DECLARE_REDUCTION:
33018 cp_parser_omp_declare (parser, pragma_tok, context);
33019 return false;
33021 case PRAGMA_OACC_CACHE:
33022 case PRAGMA_OACC_DATA:
33023 case PRAGMA_OACC_ENTER_DATA:
33024 case PRAGMA_OACC_EXIT_DATA:
33025 case PRAGMA_OACC_KERNELS:
33026 case PRAGMA_OACC_PARALLEL:
33027 case PRAGMA_OACC_LOOP:
33028 case PRAGMA_OACC_UPDATE:
33029 case PRAGMA_OACC_WAIT:
33030 case PRAGMA_OMP_ATOMIC:
33031 case PRAGMA_OMP_CRITICAL:
33032 case PRAGMA_OMP_DISTRIBUTE:
33033 case PRAGMA_OMP_FOR:
33034 case PRAGMA_OMP_MASTER:
33035 case PRAGMA_OMP_ORDERED:
33036 case PRAGMA_OMP_PARALLEL:
33037 case PRAGMA_OMP_SECTIONS:
33038 case PRAGMA_OMP_SIMD:
33039 case PRAGMA_OMP_SINGLE:
33040 case PRAGMA_OMP_TASK:
33041 case PRAGMA_OMP_TASKGROUP:
33042 case PRAGMA_OMP_TEAMS:
33043 if (context != pragma_stmt && context != pragma_compound)
33044 goto bad_stmt;
33045 cp_parser_omp_construct (parser, pragma_tok);
33046 return true;
33048 case PRAGMA_OMP_TARGET:
33049 return cp_parser_omp_target (parser, pragma_tok, context);
33051 case PRAGMA_OMP_END_DECLARE_TARGET:
33052 cp_parser_omp_end_declare_target (parser, pragma_tok);
33053 return false;
33055 case PRAGMA_OMP_SECTION:
33056 error_at (pragma_tok->location,
33057 "%<#pragma omp section%> may only be used in "
33058 "%<#pragma omp sections%> construct");
33059 break;
33061 case PRAGMA_IVDEP:
33063 if (context == pragma_external)
33065 error_at (pragma_tok->location,
33066 "%<#pragma GCC ivdep%> must be inside a function");
33067 break;
33069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33070 cp_token *tok;
33071 tok = cp_lexer_peek_token (the_parser->lexer);
33072 if (tok->type != CPP_KEYWORD
33073 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33074 && tok->keyword != RID_DO))
33076 cp_parser_error (parser, "for, while or do statement expected");
33077 return false;
33079 cp_parser_iteration_statement (parser, true);
33080 return true;
33083 case PRAGMA_CILK_SIMD:
33084 if (context == pragma_external)
33086 error_at (pragma_tok->location,
33087 "%<#pragma simd%> must be inside a function");
33088 break;
33090 cp_parser_cilk_simd (parser, pragma_tok);
33091 return true;
33093 case PRAGMA_CILK_GRAINSIZE:
33094 if (context == pragma_external)
33096 error_at (pragma_tok->location,
33097 "%<#pragma cilk grainsize%> must be inside a function");
33098 break;
33101 /* Ignore the pragma if Cilk Plus is not enabled. */
33102 if (flag_cilkplus)
33104 cp_parser_cilk_grainsize (parser, pragma_tok);
33105 return true;
33107 else
33109 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33110 "%<#pragma cilk grainsize%>");
33111 break;
33114 default:
33115 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33116 c_invoke_pragma_handler (id);
33117 break;
33119 bad_stmt:
33120 cp_parser_error (parser, "expected declaration specifiers");
33121 break;
33124 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33125 return false;
33128 /* The interface the pragma parsers have to the lexer. */
33130 enum cpp_ttype
33131 pragma_lex (tree *value)
33133 cp_token *tok;
33134 enum cpp_ttype ret;
33136 tok = cp_lexer_peek_token (the_parser->lexer);
33138 ret = tok->type;
33139 *value = tok->u.value;
33141 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33142 ret = CPP_EOF;
33143 else if (ret == CPP_STRING)
33144 *value = cp_parser_string_literal (the_parser, false, false);
33145 else
33147 cp_lexer_consume_token (the_parser->lexer);
33148 if (ret == CPP_KEYWORD)
33149 ret = CPP_NAME;
33152 return ret;
33156 /* External interface. */
33158 /* Parse one entire translation unit. */
33160 void
33161 c_parse_file (void)
33163 static bool already_called = false;
33165 if (already_called)
33166 fatal_error (input_location,
33167 "inter-module optimizations not implemented for C++");
33168 already_called = true;
33170 the_parser = cp_parser_new ();
33171 push_deferring_access_checks (flag_access_control
33172 ? dk_no_deferred : dk_no_check);
33173 cp_parser_translation_unit (the_parser);
33174 the_parser = NULL;
33177 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33178 vectorlength clause:
33179 Syntax:
33180 vectorlength ( constant-expression ) */
33182 static tree
33183 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33184 bool is_simd_fn)
33186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33187 tree expr;
33188 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33189 safelen clause. Thus, vectorlength is represented as OMP 4.0
33190 safelen. For SIMD-enabled function it is represented by OMP 4.0
33191 simdlen. */
33192 if (!is_simd_fn)
33193 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33194 loc);
33195 else
33196 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33197 loc);
33199 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33200 return error_mark_node;
33202 expr = cp_parser_constant_expression (parser);
33203 expr = maybe_constant_value (expr);
33205 /* If expr == error_mark_node, then don't emit any errors nor
33206 create a clause. if any of the above functions returns
33207 error mark node then they would have emitted an error message. */
33208 if (expr == error_mark_node)
33210 else if (!TREE_TYPE (expr)
33211 || !TREE_CONSTANT (expr)
33212 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33213 error_at (loc, "vectorlength must be an integer constant");
33214 else if (TREE_CONSTANT (expr)
33215 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33216 error_at (loc, "vectorlength must be a power of 2");
33217 else
33219 tree c;
33220 if (!is_simd_fn)
33222 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33223 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33224 OMP_CLAUSE_CHAIN (c) = clauses;
33225 clauses = c;
33227 else
33229 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33230 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33231 OMP_CLAUSE_CHAIN (c) = clauses;
33232 clauses = c;
33236 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33237 return error_mark_node;
33238 return clauses;
33241 /* Handles the Cilk Plus #pragma simd linear clause.
33242 Syntax:
33243 linear ( simd-linear-variable-list )
33245 simd-linear-variable-list:
33246 simd-linear-variable
33247 simd-linear-variable-list , simd-linear-variable
33249 simd-linear-variable:
33250 id-expression
33251 id-expression : simd-linear-step
33253 simd-linear-step:
33254 conditional-expression */
33256 static tree
33257 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33259 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33262 return clauses;
33263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33265 cp_parser_error (parser, "expected identifier");
33266 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33267 return error_mark_node;
33270 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33271 parser->colon_corrects_to_scope_p = false;
33272 while (1)
33274 cp_token *token = cp_lexer_peek_token (parser->lexer);
33275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33277 cp_parser_error (parser, "expected variable-name");
33278 clauses = error_mark_node;
33279 break;
33282 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33283 false, false);
33284 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33285 token->location);
33286 if (decl == error_mark_node)
33288 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33289 token->location);
33290 clauses = error_mark_node;
33292 else
33294 tree e = NULL_TREE;
33295 tree step_size = integer_one_node;
33297 /* If present, parse the linear step. Otherwise, assume the default
33298 value of 1. */
33299 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33301 cp_lexer_consume_token (parser->lexer);
33303 e = cp_parser_assignment_expression (parser);
33304 e = maybe_constant_value (e);
33306 if (e == error_mark_node)
33308 /* If an error has occurred, then the whole pragma is
33309 considered ill-formed. Thus, no reason to keep
33310 parsing. */
33311 clauses = error_mark_node;
33312 break;
33314 else if (type_dependent_expression_p (e)
33315 || value_dependent_expression_p (e)
33316 || (TREE_TYPE (e)
33317 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33318 && (TREE_CONSTANT (e)
33319 || DECL_P (e))))
33320 step_size = e;
33321 else
33322 cp_parser_error (parser,
33323 "step size must be an integer constant "
33324 "expression or an integer variable");
33327 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33328 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33329 OMP_CLAUSE_DECL (l) = decl;
33330 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33331 OMP_CLAUSE_CHAIN (l) = clauses;
33332 clauses = l;
33334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33335 cp_lexer_consume_token (parser->lexer);
33336 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33337 break;
33338 else
33340 error_at (cp_lexer_peek_token (parser->lexer)->location,
33341 "expected %<,%> or %<)%> after %qE", decl);
33342 clauses = error_mark_node;
33343 break;
33346 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33347 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33348 return clauses;
33351 /* Returns the name of the next clause. If the clause is not
33352 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33353 token is not consumed. Otherwise, the appropriate enum from the
33354 pragma_simd_clause is returned and the token is consumed. */
33356 static pragma_omp_clause
33357 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33359 pragma_omp_clause clause_type;
33360 cp_token *token = cp_lexer_peek_token (parser->lexer);
33362 if (token->keyword == RID_PRIVATE)
33363 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33364 else if (!token->u.value || token->type != CPP_NAME)
33365 return PRAGMA_CILK_CLAUSE_NONE;
33366 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33367 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33368 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33369 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33370 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33371 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33372 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33373 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33374 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33375 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33376 else
33377 return PRAGMA_CILK_CLAUSE_NONE;
33379 cp_lexer_consume_token (parser->lexer);
33380 return clause_type;
33383 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33385 static tree
33386 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33388 tree clauses = NULL_TREE;
33390 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33391 && clauses != error_mark_node)
33393 pragma_omp_clause c_kind;
33394 c_kind = cp_parser_cilk_simd_clause_name (parser);
33395 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33396 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33397 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33398 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33399 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33400 /* Use the OpenMP 4.0 equivalent function. */
33401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33402 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33403 /* Use the OpenMP 4.0 equivalent function. */
33404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33405 clauses);
33406 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33407 /* Use the OMP 4.0 equivalent function. */
33408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33409 clauses);
33410 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33411 /* Use the OMP 4.0 equivalent function. */
33412 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33413 else
33415 clauses = error_mark_node;
33416 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33417 break;
33421 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33423 if (clauses == error_mark_node)
33424 return error_mark_node;
33425 else
33426 return c_finish_cilk_clauses (clauses);
33429 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33431 static void
33432 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33434 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33436 if (clauses == error_mark_node)
33437 return;
33439 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33441 error_at (cp_lexer_peek_token (parser->lexer)->location,
33442 "for statement expected");
33443 return;
33446 tree sb = begin_omp_structured_block ();
33447 int save = cp_parser_begin_omp_structured_block (parser);
33448 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33449 if (ret)
33450 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33451 cp_parser_end_omp_structured_block (parser, save);
33452 add_stmt (finish_omp_structured_block (sb));
33455 /* Main entry-point for parsing Cilk Plus _Cilk_for
33456 loops. The return value is error_mark_node
33457 when errors happen and CILK_FOR tree on success. */
33459 static tree
33460 cp_parser_cilk_for (cp_parser *parser, tree grain)
33462 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33463 gcc_unreachable ();
33465 tree sb = begin_omp_structured_block ();
33466 int save = cp_parser_begin_omp_structured_block (parser);
33468 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33469 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33470 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33471 clauses = finish_omp_clauses (clauses);
33473 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33474 if (ret)
33475 cpp_validate_cilk_plus_loop (ret);
33476 else
33477 ret = error_mark_node;
33479 cp_parser_end_omp_structured_block (parser, save);
33480 add_stmt (finish_omp_structured_block (sb));
33481 return ret;
33484 /* Create an identifier for a generic parameter type (a synthesized
33485 template parameter implied by `auto' or a concept identifier). */
33487 static GTY(()) int generic_parm_count;
33488 static tree
33489 make_generic_type_name ()
33491 char buf[32];
33492 sprintf (buf, "auto:%d", ++generic_parm_count);
33493 return get_identifier (buf);
33496 /* Predicate that behaves as is_auto_or_concept but matches the parent
33497 node of the generic type rather than the generic type itself. This
33498 allows for type transformation in add_implicit_template_parms. */
33500 static inline bool
33501 tree_type_is_auto_or_concept (const_tree t)
33503 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33506 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33507 (creating a new template parameter list if necessary). Returns the newly
33508 created template type parm. */
33510 tree
33511 synthesize_implicit_template_parm (cp_parser *parser)
33513 gcc_assert (current_binding_level->kind == sk_function_parms);
33515 /* We are either continuing a function template that already contains implicit
33516 template parameters, creating a new fully-implicit function template, or
33517 extending an existing explicit function template with implicit template
33518 parameters. */
33520 cp_binding_level *const entry_scope = current_binding_level;
33522 bool become_template = false;
33523 cp_binding_level *parent_scope = 0;
33525 if (parser->implicit_template_scope)
33527 gcc_assert (parser->implicit_template_parms);
33529 current_binding_level = parser->implicit_template_scope;
33531 else
33533 /* Roll back to the existing template parameter scope (in the case of
33534 extending an explicit function template) or introduce a new template
33535 parameter scope ahead of the function parameter scope (or class scope
33536 in the case of out-of-line member definitions). The function scope is
33537 added back after template parameter synthesis below. */
33539 cp_binding_level *scope = entry_scope;
33541 while (scope->kind == sk_function_parms)
33543 parent_scope = scope;
33544 scope = scope->level_chain;
33546 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33548 /* If not defining a class, then any class scope is a scope level in
33549 an out-of-line member definition. In this case simply wind back
33550 beyond the first such scope to inject the template parameter list.
33551 Otherwise wind back to the class being defined. The latter can
33552 occur in class member friend declarations such as:
33554 class A {
33555 void foo (auto);
33557 class B {
33558 friend void A::foo (auto);
33561 The template parameter list synthesized for the friend declaration
33562 must be injected in the scope of 'B'. This can also occur in
33563 erroneous cases such as:
33565 struct A {
33566 struct B {
33567 void foo (auto);
33569 void B::foo (auto) {}
33572 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33573 but, nevertheless, the template parameter list synthesized for the
33574 declarator should be injected into the scope of 'A' as if the
33575 ill-formed template was specified explicitly. */
33577 while (scope->kind == sk_class && !scope->defining_class_p)
33579 parent_scope = scope;
33580 scope = scope->level_chain;
33584 current_binding_level = scope;
33586 if (scope->kind != sk_template_parms
33587 || !function_being_declared_is_template_p (parser))
33589 /* Introduce a new template parameter list for implicit template
33590 parameters. */
33592 become_template = true;
33594 parser->implicit_template_scope
33595 = begin_scope (sk_template_parms, NULL);
33597 ++processing_template_decl;
33599 parser->fully_implicit_function_template_p = true;
33600 ++parser->num_template_parameter_lists;
33602 else
33604 /* Synthesize implicit template parameters at the end of the explicit
33605 template parameter list. */
33607 gcc_assert (current_template_parms);
33609 parser->implicit_template_scope = scope;
33611 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33612 parser->implicit_template_parms
33613 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33617 /* Synthesize a new template parameter and track the current template
33618 parameter chain with implicit_template_parms. */
33620 tree synth_id = make_generic_type_name ();
33621 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33622 synth_id);
33623 tree new_parm
33624 = process_template_parm (parser->implicit_template_parms,
33625 input_location,
33626 build_tree_list (NULL_TREE, synth_tmpl_parm),
33627 /*non_type=*/false,
33628 /*param_pack=*/false);
33631 if (parser->implicit_template_parms)
33632 parser->implicit_template_parms
33633 = TREE_CHAIN (parser->implicit_template_parms);
33634 else
33635 parser->implicit_template_parms = new_parm;
33637 tree new_type = TREE_TYPE (getdecls ());
33639 /* If creating a fully implicit function template, start the new implicit
33640 template parameter list with this synthesized type, otherwise grow the
33641 current template parameter list. */
33643 if (become_template)
33645 parent_scope->level_chain = current_binding_level;
33647 tree new_parms = make_tree_vec (1);
33648 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33649 current_template_parms = tree_cons (size_int (processing_template_decl),
33650 new_parms, current_template_parms);
33652 else
33654 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33655 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33656 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33657 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33660 current_binding_level = entry_scope;
33662 return new_type;
33665 /* Finish the declaration of a fully implicit function template. Such a
33666 template has no explicit template parameter list so has not been through the
33667 normal template head and tail processing. synthesize_implicit_template_parm
33668 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33669 provided if the declaration is a class member such that its template
33670 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33671 form is returned. Otherwise NULL_TREE is returned. */
33673 tree
33674 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33676 gcc_assert (parser->fully_implicit_function_template_p);
33678 if (member_decl_opt && member_decl_opt != error_mark_node
33679 && DECL_VIRTUAL_P (member_decl_opt))
33681 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33682 "implicit templates may not be %<virtual%>");
33683 DECL_VIRTUAL_P (member_decl_opt) = false;
33686 if (member_decl_opt)
33687 member_decl_opt = finish_member_template_decl (member_decl_opt);
33688 end_template_decl ();
33690 parser->fully_implicit_function_template_p = false;
33691 --parser->num_template_parameter_lists;
33693 return member_decl_opt;
33696 #include "gt-cp-parser.h"